2 * Copyright 2019 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #ifndef MetalWindowContext_DEFINED
8 #define MetalWindowContext_DEFINED
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkSurface.h"
12 #include "include/ports/SkCFObject.h"
13 #include "include/private/gpu/ganesh/GrMtlTypesPriv.h"
15 #include "tools/sk_app/WindowContext.h"
17 #import <Metal/Metal.h>
18 #import <QuartzCore/CAMetalLayer.h>
22 class MetalWindowContext : public WindowContext {
24 sk_sp<SkSurface> getBackbufferSurface() override;
26 bool isValid() override { return fValid; }
28 void swapBuffers() override;
30 void setDisplayParams(const DisplayParams& params) override;
32 void activate(bool isActive) override;
35 static NSURL* CacheURL();
37 MetalWindowContext(const DisplayParams&);
38 // This should be called by subclass constructor. It is also called when window/display
39 // parameters change. This will in turn call onInitializeContext().
40 void initializeContext();
41 virtual bool onInitializeContext() = 0;
43 // This should be called by subclass destructor. It is also called when window/display
44 // parameters change prior to initializing a new Metal context. This will in turn call
45 // onDestroyContext().
46 void destroyContext();
47 virtual void onDestroyContext() = 0;
50 sk_cfp<id<MTLDevice>> fDevice;
51 sk_cfp<id<MTLCommandQueue>> fQueue;
52 CAMetalLayer* fMetalLayer;
53 GrMTLHandle fDrawableHandle;
54 #if GR_METAL_SDK_VERSION >= 230
55 // wrapping this in sk_cfp throws up an availability warning, so we'll track lifetime manually
56 id<MTLBinaryArchive> fPipelineArchive SK_API_AVAILABLE(macos(11.0), ios(14.0));