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