Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkRefCnt.h" |
| 11 | #include "include/core/SkSurface.h" |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 12 | #include "include/ports/SkCFObject.h" |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tools/sk_app/WindowContext.h" |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 15 | |
| 16 | #import <Metal/Metal.h> |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 17 | #import <QuartzCore/CAMetalLayer.h> |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 18 | |
| 19 | namespace sk_app { |
| 20 | |
| 21 | class MetalWindowContext : public WindowContext { |
| 22 | public: |
| 23 | sk_sp<SkSurface> getBackbufferSurface() override; |
| 24 | |
| 25 | bool isValid() override { return fValid; } |
| 26 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 27 | void swapBuffers() override; |
| 28 | |
| 29 | void setDisplayParams(const DisplayParams& params) override; |
| 30 | |
Jim Van Verth | 5a89ed5 | 2020-11-16 11:06:58 -0500 | [diff] [blame] | 31 | void activate(bool isActive) override; |
| 32 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 33 | protected: |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 34 | static NSURL* CacheURL(); |
| 35 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 36 | MetalWindowContext(const DisplayParams&); |
| 37 | // This should be called by subclass constructor. It is also called when window/display |
| 38 | // parameters change. This will in turn call onInitializeContext(). |
| 39 | void initializeContext(); |
| 40 | virtual bool onInitializeContext() = 0; |
| 41 | |
| 42 | // This should be called by subclass destructor. It is also called when window/display |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 43 | // parameters change prior to initializing a new Metal context. This will in turn call |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 44 | // onDestroyContext(). |
| 45 | void destroyContext(); |
| 46 | virtual void onDestroyContext() = 0; |
| 47 | |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 48 | bool fValid; |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 49 | sk_cfp<id<MTLDevice>> fDevice; |
| 50 | sk_cfp<id<MTLCommandQueue>> fQueue; |
Jim Van Verth | d063e8b | 2019-05-16 10:31:56 -0400 | [diff] [blame] | 51 | CAMetalLayer* fMetalLayer; |
Jim Van Verth | cbf59e0 | 2019-10-17 14:58:37 -0400 | [diff] [blame] | 52 | GrMTLHandle fDrawableHandle; |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 53 | #if GR_METAL_SDK_VERSION >= 230 |
Jim Van Verth | 2db4ba1 | 2021-02-22 09:41:27 -0500 | [diff] [blame] | 54 | // wrapping this in sk_cfp throws up an availability warning, so we'll track lifetime manually |
| 55 | id<MTLBinaryArchive> fPipelineArchive SK_API_AVAILABLE(macos(11.0), ios(14.0)); |
Jim Van Verth | 9e64047 | 2020-11-19 12:59:06 -0500 | [diff] [blame] | 56 | #endif |
Jim Van Verth | be39f71 | 2019-02-08 15:36:14 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | } // namespace sk_app |
| 60 | |
| 61 | #endif |