| 1 | /* | 
|---|
| 2 | * Copyright 2020 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 |  | 
|---|
| 8 | #ifndef GrDirectContext_DEFINED | 
|---|
| 9 | #define GrDirectContext_DEFINED | 
|---|
| 10 |  | 
|---|
| 11 | #include "include/gpu/GrContext.h" | 
|---|
| 12 |  | 
|---|
| 13 | class GrAtlasManager; | 
|---|
| 14 | class GrSmallPathAtlasMgr; | 
|---|
| 15 |  | 
|---|
| 16 | class SK_API GrDirectContext : public GrContext { | 
|---|
| 17 | public: | 
|---|
| 18 | #ifdef SK_GL | 
|---|
| 19 | /** | 
|---|
| 20 | * Creates a GrDirectContext for a backend context. If no GrGLInterface is provided then the | 
|---|
| 21 | * result of GrGLMakeNativeInterface() is used if it succeeds. | 
|---|
| 22 | */ | 
|---|
| 23 | static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>, const GrContextOptions&); | 
|---|
| 24 | static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>); | 
|---|
| 25 | static sk_sp<GrDirectContext> MakeGL(const GrContextOptions&); | 
|---|
| 26 | static sk_sp<GrDirectContext> MakeGL(); | 
|---|
| 27 | #endif | 
|---|
| 28 |  | 
|---|
| 29 | #ifdef SK_VULKAN | 
|---|
| 30 | /** | 
|---|
| 31 | * The Vulkan context (VkQueue, VkDevice, VkInstance) must be kept alive until the returned | 
|---|
| 32 | * GrDirectContext is destroyed. This also means that any objects created with this | 
|---|
| 33 | * GrDirectContext (e.g. SkSurfaces, SkImages, etc.) must also be released as they may hold | 
|---|
| 34 | * refs on the GrDirectContext. Once all these objects and the GrDirectContext are released, | 
|---|
| 35 | * then it is safe to delete the vulkan objects. | 
|---|
| 36 | */ | 
|---|
| 37 | static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&, const GrContextOptions&); | 
|---|
| 38 | static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&); | 
|---|
| 39 | #endif | 
|---|
| 40 |  | 
|---|
| 41 | #ifdef SK_METAL | 
|---|
| 42 | /** | 
|---|
| 43 | * Makes a GrDirectContext which uses Metal as the backend. The device parameter is an | 
|---|
| 44 | * MTLDevice and queue is an MTLCommandQueue which should be used by the backend. These objects | 
|---|
| 45 | * must have a ref on them which can be transferred to Ganesh which will release the ref | 
|---|
| 46 | * when the GrDirectContext is destroyed. | 
|---|
| 47 | */ | 
|---|
| 48 | static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue, const GrContextOptions&); | 
|---|
| 49 | static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue); | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | #ifdef SK_DIRECT3D | 
|---|
| 53 | /** | 
|---|
| 54 | * Makes a GrDirectContext which uses Direct3D as the backend. The Direct3D context | 
|---|
| 55 | * must be kept alive until the returned GrDirectContext is first destroyed or abandoned. | 
|---|
| 56 | */ | 
|---|
| 57 | static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&, const GrContextOptions&); | 
|---|
| 58 | static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&); | 
|---|
| 59 | #endif | 
|---|
| 60 |  | 
|---|
| 61 | #ifdef SK_DAWN | 
|---|
| 62 | static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&, | 
|---|
| 63 | const GrContextOptions&); | 
|---|
| 64 | static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&); | 
|---|
| 65 | #endif | 
|---|
| 66 |  | 
|---|
| 67 | static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*, const GrContextOptions&); | 
|---|
| 68 | static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*); | 
|---|
| 69 |  | 
|---|
| 70 | ~GrDirectContext() override; | 
|---|
| 71 |  | 
|---|
| 72 | void abandonContext() override; | 
|---|
| 73 |  | 
|---|
| 74 | void releaseResourcesAndAbandonContext() override; | 
|---|
| 75 |  | 
|---|
| 76 | void freeGpuResources() override; | 
|---|
| 77 |  | 
|---|
| 78 | protected: | 
|---|
| 79 | GrDirectContext(GrBackendApi backend, const GrContextOptions& options); | 
|---|
| 80 |  | 
|---|
| 81 | bool init() override; | 
|---|
| 82 |  | 
|---|
| 83 | GrAtlasManager* onGetAtlasManager() override { return fAtlasManager.get(); } | 
|---|
| 84 | GrSmallPathAtlasMgr* onGetSmallPathAtlasMgr() override; | 
|---|
| 85 |  | 
|---|
| 86 | GrDirectContext* asDirectContext() override { return this; } | 
|---|
| 87 |  | 
|---|
| 88 | private: | 
|---|
| 89 | std::unique_ptr<GrAtlasManager> fAtlasManager; | 
|---|
| 90 |  | 
|---|
| 91 | std::unique_ptr<GrSmallPathAtlasMgr> fSmallPathAtlasMgr; | 
|---|
| 92 |  | 
|---|
| 93 | typedef GrContext INHERITED; | 
|---|
| 94 | }; | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | #endif | 
|---|
| 98 |  | 
|---|