xref: /aosp_15_r20/external/skia/tools/window/MetalWindowContext.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #ifndef MetalWindowContext_DEFINED
8 #define MetalWindowContext_DEFINED
9 
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkSurface.h"
12 #include "include/ports/SkCFObject.h"
13 #include "src/gpu/ganesh/mtl/GrMtlTypesPriv.h"
14 
15 #include "tools/window/WindowContext.h"
16 
17 #import <Metal/Metal.h>
18 #import <QuartzCore/CAMetalLayer.h>
19 
20 namespace skwindow::internal {
21 
22 class MetalWindowContext : public WindowContext {
23 public:
24     sk_sp<SkSurface> getBackbufferSurface() override;
25 
isValid()26     bool isValid() override { return fValid; }
27 
28     void setDisplayParams(std::unique_ptr<const DisplayParams> params) override;
29 
30 protected:
31     MetalWindowContext(std::unique_ptr<const DisplayParams>);
32     // This should be called by subclass constructor. It is also called when window/display
33     // parameters change. This will in turn call onInitializeContext().
34     void initializeContext();
35     virtual bool onInitializeContext() = 0;
36 
37     // This should be called by subclass destructor. It is also called when window/display
38     // parameters change prior to initializing a new Metal context. This will in turn call
39     // onDestroyContext().
40     void destroyContext();
41     virtual void onDestroyContext() = 0;
42 
43     void onSwapBuffers() override;
44 
45     bool                        fValid;
46     sk_cfp<id<MTLDevice>>       fDevice;
47     sk_cfp<id<MTLCommandQueue>> fQueue;
48     CAMetalLayer*               fMetalLayer;
49     GrMTLHandle                 fDrawableHandle;
50 };
51 
52 }   // namespace skwindow::internal
53 
54 #endif
55