Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 8 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 9 | #include "include/private/GrDirectContext.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrContextThreadSafeProxy.h" |
| 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
| 14 | #include "src/gpu/GrGpu.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/effects/GrSkSLFP.h" |
| 17 | #include "src/gpu/gl/GrGLGpu.h" |
| 18 | #include "src/gpu/mock/GrMockGpu.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 19 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/text/GrStrikeCache.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 21 | #ifdef SK_METAL |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/mtl/GrMtlTrampoline.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 23 | #endif |
| 24 | #ifdef SK_VULKAN |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/vk/GrVkGpu.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 26 | #endif |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 27 | #ifdef SK_DIRECT3D |
| 28 | #include "src/gpu/d3d/GrD3DGpu.h" |
| 29 | #endif |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 30 | #ifdef SK_DAWN |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 31 | #include "src/gpu/dawn/GrDawnGpu.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 32 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 33 | |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 34 | #if GR_TEST_UTILS |
| 35 | # include "include/utils/SkRandom.h" |
| 36 | # if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 37 | # include <sanitizer/lsan_interface.h> |
| 38 | # endif |
| 39 | #endif |
| 40 | |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 41 | #ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 42 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 43 | #else |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 44 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 45 | #endif |
| 46 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 47 | GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options) |
| 48 | : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) |
| 49 | , fAtlasManager(nullptr) { |
| 50 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 51 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 52 | GrDirectContext::~GrDirectContext() { |
| 53 | // this if-test protects against the case where the context is being destroyed |
| 54 | // before having been fully created |
| 55 | if (this->priv().getGpu()) { |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 56 | this->flushAndSubmit(); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 59 | delete fAtlasManager; |
| 60 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 61 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 62 | void GrDirectContext::abandonContext() { |
| 63 | INHERITED::abandonContext(); |
| 64 | fAtlasManager->freeAll(); |
| 65 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 66 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 67 | void GrDirectContext::releaseResourcesAndAbandonContext() { |
| 68 | INHERITED::releaseResourcesAndAbandonContext(); |
| 69 | fAtlasManager->freeAll(); |
| 70 | } |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 71 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 72 | void GrDirectContext::freeGpuResources() { |
| 73 | this->flushAndSubmit(); |
| 74 | fAtlasManager->freeAll(); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 75 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 76 | INHERITED::freeGpuResources(); |
| 77 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 78 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 79 | bool GrDirectContext::init() { |
| 80 | const GrGpu* gpu = this->priv().getGpu(); |
| 81 | if (!gpu) { |
| 82 | return false; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 85 | fThreadSafeProxy->priv().init(gpu->refCaps()); |
| 86 | if (!INHERITED::init()) { |
| 87 | return false; |
| 88 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 89 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 90 | bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting; |
| 91 | if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) { |
| 92 | reduceOpsTaskSplitting = false; |
| 93 | } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) { |
| 94 | reduceOpsTaskSplitting = true; |
| 95 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 96 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 97 | this->setupDrawingManager(true, reduceOpsTaskSplitting); |
| 98 | |
| 99 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
| 100 | if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures || |
| 101 | // multitexturing supported only if range can represent the index + texcoords fully |
| 102 | !(this->caps()->shaderCaps()->floatIs32Bits() || |
| 103 | this->caps()->shaderCaps()->integerSupport())) { |
| 104 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 105 | } else { |
| 106 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 107 | } |
| 108 | |
| 109 | GrProxyProvider* proxyProvider = this->priv().proxyProvider(); |
| 110 | |
| 111 | fAtlasManager = new GrAtlasManager(proxyProvider, |
| 112 | this->options().fGlyphCacheTextureMaximumBytes, |
| 113 | allowMultitexturing); |
| 114 | this->priv().addOnFlushCallbackObject(fAtlasManager); |
| 115 | |
| 116 | return true; |
| 117 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 118 | |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 119 | #ifdef SK_GL |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 120 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 121 | GrContextOptions defaultOptions; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 122 | return MakeGL(std::move(glInterface), defaultOptions); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 125 | sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) { |
| 126 | return MakeGL(nullptr, options); |
| 127 | } |
| 128 | |
| 129 | sk_sp<GrContext> GrContext::MakeGL() { |
| 130 | GrContextOptions defaultOptions; |
| 131 | return MakeGL(nullptr, defaultOptions); |
| 132 | } |
| 133 | |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 134 | #if GR_TEST_UTILS |
| 135 | GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) { |
| 136 | // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a |
| 137 | // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and |
| 138 | // capture that by pointer. However, GrGLFunction doesn't support calling a destructor |
| 139 | // on the thing it captures. So we leak the context. |
| 140 | struct GetErrorContext { |
| 141 | SkRandom fRandom; |
| 142 | GrGLFunction<GrGLGetErrorFn> fGetError; |
| 143 | }; |
| 144 | |
| 145 | auto errorContext = new GetErrorContext; |
| 146 | |
| 147 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 148 | __lsan_ignore_object(errorContext); |
| 149 | #endif |
| 150 | |
| 151 | errorContext->fGetError = original; |
| 152 | |
| 153 | return GrGLFunction<GrGLGetErrorFn>([errorContext]() { |
| 154 | GrGLenum error = errorContext->fGetError(); |
| 155 | if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) { |
| 156 | error = GR_GL_OUT_OF_MEMORY; |
| 157 | } |
| 158 | return error; |
| 159 | }); |
| 160 | } |
| 161 | #endif |
| 162 | |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 163 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface, |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 164 | const GrContextOptions& options) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 165 | sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kOpenGL, options)); |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 166 | #if GR_TEST_UTILS |
| 167 | if (options.fRandomGLOOM) { |
| 168 | auto copy = sk_make_sp<GrGLInterface>(*glInterface); |
| 169 | copy->fFunctions.fGetError = |
| 170 | make_get_error_with_random_oom(glInterface->fFunctions.fGetError); |
| 171 | #if GR_GL_CHECK_ERROR |
| 172 | // Suppress logging GL errors since we'll be synthetically generating them. |
| 173 | copy->suppressErrorLogging(); |
| 174 | #endif |
| 175 | glInterface = std::move(copy); |
| 176 | } |
| 177 | #endif |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 178 | context->fGpu = GrGLGpu::Make(std::move(glInterface), options, context.get()); |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 179 | if (!context->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 180 | return nullptr; |
| 181 | } |
| 182 | return context; |
| 183 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 184 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 185 | |
| 186 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) { |
| 187 | GrContextOptions defaultOptions; |
| 188 | return MakeMock(mockOptions, defaultOptions); |
| 189 | } |
| 190 | |
| 191 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions, |
| 192 | const GrContextOptions& options) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 193 | sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMock, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 194 | |
| 195 | context->fGpu = GrMockGpu::Make(mockOptions, options, context.get()); |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 196 | if (!context->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 197 | return nullptr; |
| 198 | } |
Chris Dalton | a378b45 | 2019-12-11 13:24:11 -0500 | [diff] [blame] | 199 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 200 | return context; |
| 201 | } |
| 202 | |
Mike Klein | a55e214 | 2018-10-03 16:34:11 +0000 | [diff] [blame] | 203 | sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) { |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 204 | #ifdef SK_VULKAN |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 205 | GrContextOptions defaultOptions; |
| 206 | return MakeVulkan(backendContext, defaultOptions); |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 207 | #else |
| 208 | return nullptr; |
| 209 | #endif |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext, |
| 213 | const GrContextOptions& options) { |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 214 | #ifdef SK_VULKAN |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 215 | sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kVulkan, options)); |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 216 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 217 | context->fGpu = GrVkGpu::Make(backendContext, options, context.get()); |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 218 | if (!context->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 219 | return nullptr; |
| 220 | } |
| 221 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 222 | return context; |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 223 | #else |
| 224 | return nullptr; |
Mike Klein | a55e214 | 2018-10-03 16:34:11 +0000 | [diff] [blame] | 225 | #endif |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 226 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 227 | |
| 228 | #ifdef SK_METAL |
| 229 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) { |
| 230 | GrContextOptions defaultOptions; |
| 231 | return MakeMetal(device, queue, defaultOptions); |
| 232 | } |
| 233 | |
| 234 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 235 | sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMetal, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 236 | |
| 237 | context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue); |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 238 | if (!context->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 239 | return nullptr; |
| 240 | } |
Timothy Liang | 4e85e80 | 2018-06-28 16:37:18 -0400 | [diff] [blame] | 241 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 242 | return context; |
| 243 | } |
| 244 | #endif |
| 245 | |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 246 | #ifdef SK_DIRECT3D |
| 247 | sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) { |
| 248 | GrContextOptions defaultOptions; |
| 249 | return MakeDirect3D(backendContext, defaultOptions); |
| 250 | } |
| 251 | |
| 252 | sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext, |
| 253 | const GrContextOptions& options) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 254 | sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kDirect3D, options)); |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 255 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 256 | context->fGpu = GrD3DGpu::Make(backendContext, options, context.get()); |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 257 | if (!context->init()) { |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 258 | return nullptr; |
| 259 | } |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 260 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 261 | return context; |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 262 | } |
| 263 | #endif |
| 264 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 265 | #ifdef SK_DAWN |
Stephen White | a521c96 | 2019-12-04 09:57:48 -0500 | [diff] [blame] | 266 | sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 267 | GrContextOptions defaultOptions; |
| 268 | return MakeDawn(device, defaultOptions); |
| 269 | } |
| 270 | |
Stephen White | a521c96 | 2019-12-04 09:57:48 -0500 | [diff] [blame] | 271 | sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame^] | 272 | sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kDawn, options)); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 273 | |
| 274 | context->fGpu = GrDawnGpu::Make(device, options, context.get()); |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 275 | if (!context->init()) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 276 | return nullptr; |
| 277 | } |
| 278 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 279 | return context; |
| 280 | } |
| 281 | #endif |