blob: e8c8392a15724e9fe0aade28ad1e01a3e58f7d69 [file] [log] [blame]
Jim Van Verthbe39f712019-02-08 15:36:14 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkRefCnt.h"
11#include "include/core/SkSurface.h"
Jim Van Verth2db4ba12021-02-22 09:41:27 -050012#include "include/ports/SkCFObject.h"
Jim Van Verthbe39f712019-02-08 15:36:14 -050013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tools/sk_app/WindowContext.h"
Jim Van Verthbe39f712019-02-08 15:36:14 -050015
16#import <Metal/Metal.h>
Jim Van Verthd063e8b2019-05-16 10:31:56 -040017#import <QuartzCore/CAMetalLayer.h>
Jim Van Verthbe39f712019-02-08 15:36:14 -050018
19namespace sk_app {
20
21class MetalWindowContext : public WindowContext {
22public:
23 sk_sp<SkSurface> getBackbufferSurface() override;
24
25 bool isValid() override { return fValid; }
26
Jim Van Verthbe39f712019-02-08 15:36:14 -050027 void swapBuffers() override;
28
29 void setDisplayParams(const DisplayParams& params) override;
30
Jim Van Verth5a89ed52020-11-16 11:06:58 -050031 void activate(bool isActive) override;
32
Jim Van Verthbe39f712019-02-08 15:36:14 -050033protected:
Jim Van Verth9e640472020-11-19 12:59:06 -050034 static NSURL* CacheURL();
35
Jim Van Verthbe39f712019-02-08 15:36:14 -050036 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 Verthd063e8b2019-05-16 10:31:56 -040043 // parameters change prior to initializing a new Metal context. This will in turn call
Jim Van Verthbe39f712019-02-08 15:36:14 -050044 // onDestroyContext().
45 void destroyContext();
46 virtual void onDestroyContext() = 0;
47
Jim Van Verthbe39f712019-02-08 15:36:14 -050048 bool fValid;
Jim Van Verth2db4ba12021-02-22 09:41:27 -050049 sk_cfp<id<MTLDevice>> fDevice;
50 sk_cfp<id<MTLCommandQueue>> fQueue;
Jim Van Verthd063e8b2019-05-16 10:31:56 -040051 CAMetalLayer* fMetalLayer;
Jim Van Verthcbf59e02019-10-17 14:58:37 -040052 GrMTLHandle fDrawableHandle;
Jim Van Verth9e640472020-11-19 12:59:06 -050053#if GR_METAL_SDK_VERSION >= 230
Jim Van Verth2db4ba12021-02-22 09:41:27 -050054 // 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 Verth9e640472020-11-19 12:59:06 -050056#endif
Jim Van Verthbe39f712019-02-08 15:36:14 -050057};
58
59} // namespace sk_app
60
61#endif