Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 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 | |
Adlai Holler | 0ce2c54 | 2020-10-06 14:04:35 -0400 | [diff] [blame] | 11 | #include "include/private/GrContext.h" |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 12 | |
| 13 | class GrAtlasManager; |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 14 | class GrSmallPathAtlasMgr; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 15 | |
Robert Phillips | c7228c6 | 2020-07-14 12:57:39 -0400 | [diff] [blame] | 16 | class SK_API GrDirectContext : public GrContext { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 17 | public: |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 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*); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 69 | |
| 70 | ~GrDirectContext() override; |
| 71 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 72 | /** |
| 73 | * The context normally assumes that no outsider is setting state |
| 74 | * within the underlying 3D API's context/device/whatever. This call informs |
| 75 | * the context that the state was modified and it should resend. Shouldn't |
| 76 | * be called frequently for good performance. |
| 77 | * The flag bits, state, is dependent on which backend is used by the |
| 78 | * context, either GL or D3D (possible in future). |
| 79 | */ |
| 80 | void resetContext(uint32_t state = kAll_GrBackendState); |
| 81 | |
| 82 | /** |
| 83 | * If the backend is GrBackendApi::kOpenGL, then all texture unit/target combinations for which |
| 84 | * the context has modified the bound texture will have texture id 0 bound. This does not |
| 85 | * flush the context. Calling resetContext() does not change the set that will be bound |
| 86 | * to texture id 0 on the next call to resetGLTextureBindings(). After this is called |
| 87 | * all unit/target combinations are considered to have unmodified bindings until the context |
| 88 | * subsequently modifies them (meaning if this is called twice in a row with no intervening |
| 89 | * context usage then the second call is a no-op.) |
| 90 | */ |
| 91 | void resetGLTextureBindings(); |
| 92 | |
| 93 | /** |
| 94 | * Abandons all GPU resources and assumes the underlying backend 3D API context is no longer |
| 95 | * usable. Call this if you have lost the associated GPU context, and thus internal texture, |
| 96 | * buffer, etc. references/IDs are now invalid. Calling this ensures that the destructors of the |
| 97 | * GrContext and any of its created resource objects will not make backend 3D API calls. Content |
| 98 | * rendered but not previously flushed may be lost. After this function is called all subsequent |
| 99 | * calls on the GrContext will fail or be no-ops. |
| 100 | * |
| 101 | * The typical use case for this function is that the underlying 3D context was lost and further |
| 102 | * API calls may crash. |
| 103 | * |
| 104 | * For Vulkan, even if the device becomes lost, the VkQueue, VkDevice, or VkInstance used to |
| 105 | * create the context must be kept alive even after abandoning the context. Those objects must |
| 106 | * live for the lifetime of the context object itself. The reason for this is so that |
| 107 | * we can continue to delete any outstanding GrBackendTextures/RenderTargets which must be |
| 108 | * cleaned up even in a device lost state. |
| 109 | */ |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 110 | void abandonContext() override; |
| 111 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 112 | /** |
| 113 | * Returns true if the context was abandoned or if the if the backend specific context has |
| 114 | * gotten into an unrecoverarble, lost state (e.g. in Vulkan backend if we've gotten a |
| 115 | * VK_ERROR_DEVICE_LOST). If the backend context is lost, this call will also abandon the |
| 116 | * GrContext. |
| 117 | */ |
| 118 | bool abandoned() override; |
| 119 | |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 120 | // TODO: Remove this from public after migrating Chrome. |
| 121 | sk_sp<GrContextThreadSafeProxy> threadSafeProxy(); |
| 122 | |
| 123 | /** |
| 124 | * Checks if the underlying 3D API reported an out-of-memory error. If this returns true it is |
| 125 | * reset and will return false until another out-of-memory error is reported by the 3D API. If |
| 126 | * the context is abandoned then this will report false. |
| 127 | * |
| 128 | * Currently this is implemented for: |
| 129 | * |
| 130 | * OpenGL [ES] - Note that client calls to glGetError() may swallow GL_OUT_OF_MEMORY errors and |
| 131 | * therefore hide the error from Skia. Also, it is not advised to use this in combination with |
| 132 | * enabling GrContextOptions::fSkipGLErrorChecks. That option may prevent the context from ever |
| 133 | * checking the GL context for OOM. |
| 134 | * |
| 135 | * Vulkan - Reports true if VK_ERROR_OUT_OF_HOST_MEMORY or VK_ERROR_OUT_OF_DEVICE_MEMORY has |
| 136 | * occurred. |
| 137 | */ |
| 138 | bool oomed(); |
| 139 | |
| 140 | /** |
| 141 | * This is similar to abandonContext() however the underlying 3D context is not yet lost and |
| 142 | * the context will cleanup all allocated resources before returning. After returning it will |
| 143 | * assume that the underlying context may no longer be valid. |
| 144 | * |
| 145 | * The typical use case for this function is that the client is going to destroy the 3D context |
| 146 | * but can't guarantee that context will be destroyed first (perhaps because it may be ref'ed |
| 147 | * elsewhere by either the client or Skia objects). |
| 148 | * |
| 149 | * For Vulkan, even if the device becomes lost, the VkQueue, VkDevice, or VkInstance used to |
| 150 | * create the context must be alive before calling releaseResourcesAndAbandonContext. |
| 151 | */ |
| 152 | void releaseResourcesAndAbandonContext(); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 153 | |
| 154 | void freeGpuResources() override; |
| 155 | |
| 156 | protected: |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 157 | GrDirectContext(GrBackendApi backend, const GrContextOptions& options); |
| 158 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 159 | bool init() override; |
| 160 | |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 161 | GrAtlasManager* onGetAtlasManager() override { return fAtlasManager.get(); } |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 162 | GrSmallPathAtlasMgr* onGetSmallPathAtlasMgr() override; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 163 | |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 164 | GrDirectContext* asDirectContext() override { return this; } |
| 165 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 166 | private: |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 167 | std::unique_ptr<GrAtlasManager> fAtlasManager; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 168 | |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 169 | std::unique_ptr<GrSmallPathAtlasMgr> fSmallPathAtlasMgr; |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 170 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 171 | using INHERITED = GrContext; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | |
| 175 | #endif |