blob: 6b23633c06ff040cc1999c6a8fc82de1ba0178c3 [file] [log] [blame]
Robert Phillipsa3457b82018-03-08 11:30:12 -05001/*
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 Daniel54bfb182018-11-20 17:12:36 -05008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/gpu/GrContext.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContextThreadSafeProxy.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrContextThreadSafeProxyPriv.h"
14#include "src/gpu/GrGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/effects/GrSkSLFP.h"
17#include "src/gpu/gl/GrGLGpu.h"
18#include "src/gpu/mock/GrMockGpu.h"
19#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050020#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050022#endif
23#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050025#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050026#ifdef SK_DIRECT3D
27#include "src/gpu/d3d/GrD3DGpu.h"
28#endif
Stephen White985741a2019-07-18 11:43:45 -040029#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050030#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040031#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050032
Robert Phillips6db27c22019-05-01 10:43:56 -040033#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040034static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040035#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040036static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040037#endif
38
Brian Salomon57f211b2019-08-21 15:21:09 -040039class GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050040public:
Robert Phillipse8345792019-02-07 10:48:24 -050041 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050042 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050043 , fAtlasManager(nullptr) {
44 }
45
Robert Phillipse8345792019-02-07 10:48:24 -050046 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050047 // this if-test protects against the case where the context is being destroyed
48 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050049 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050050 this->flush();
51 }
52
53 delete fAtlasManager;
54 }
55
56 void abandonContext() override {
57 INHERITED::abandonContext();
58 fAtlasManager->freeAll();
59 }
60
61 void releaseResourcesAndAbandonContext() override {
62 INHERITED::releaseResourcesAndAbandonContext();
63 fAtlasManager->freeAll();
64 }
65
66 void freeGpuResources() override {
67 this->flush();
68 fAtlasManager->freeAll();
69
70 INHERITED::freeGpuResources();
71 }
72
73protected:
Brian Osman7b1678a2019-12-16 09:17:25 -050074 bool init(sk_sp<const GrCaps> caps) override {
75 SkASSERT(caps);
Robert Phillipsa3457b82018-03-08 11:30:12 -050076 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050077
Robert Phillipsbb606772019-02-04 17:50:57 -050078 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
79 this->options(),
80 this->contextID(),
Brian Osman7b1678a2019-12-16 09:17:25 -050081 caps);
Robert Phillipsbb606772019-02-04 17:50:57 -050082
Brian Osman7b1678a2019-12-16 09:17:25 -050083 if (!INHERITED::init(std::move(caps))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050084 return false;
85 }
86
Greg Danielf41b2bd2019-08-22 16:19:24 -040087 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
Greg Daniel93138742019-08-22 17:15:39 -040088 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040089 reduceOpsTaskSplitting = false;
Greg Daniel93138742019-08-22 17:15:39 -040090 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040091 reduceOpsTaskSplitting = true;
Robert Phillips6db27c22019-05-01 10:43:56 -040092 }
93
Greg Danielf41b2bd2019-08-22 16:19:24 -040094 this->setupDrawingManager(true, reduceOpsTaskSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050095
Robert Phillipsbb606772019-02-04 17:50:57 -050096 SkASSERT(this->caps());
97
Robert Phillipsa3457b82018-03-08 11:30:12 -050098 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050099 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -0500100 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -0500101 !(this->caps()->shaderCaps()->floatIs32Bits() ||
102 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500103 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
104 } else {
105 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
106 }
107
Herb Derbya00da612019-03-04 17:10:01 -0500108 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500109 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500110
111 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500112 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500113 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500114 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500115
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116 return true;
117 }
118
119 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
120
121private:
122 GrAtlasManager* fAtlasManager;
123
124 typedef GrContext INHERITED;
125};
126
John Rosascoa9b348f2019-11-08 13:18:15 -0800127#ifdef SK_GL
Robert Phillipsa3457b82018-03-08 11:30:12 -0500128sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
129 GrContextOptions defaultOptions;
130 return MakeGL(std::move(interface), defaultOptions);
131}
132
Brian Salomonc1b9c102018-04-06 09:18:00 -0400133sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
134 return MakeGL(nullptr, options);
135}
136
137sk_sp<GrContext> GrContext::MakeGL() {
138 GrContextOptions defaultOptions;
139 return MakeGL(nullptr, defaultOptions);
140}
141
Robert Phillipsa3457b82018-03-08 11:30:12 -0500142sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
143 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500144 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500145
146 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
147 if (!context->fGpu) {
148 return nullptr;
149 }
150
Brian Osman7b1678a2019-12-16 09:17:25 -0500151 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500152 return nullptr;
153 }
154 return context;
155}
John Rosascoa9b348f2019-11-08 13:18:15 -0800156#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500157
158sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
159 GrContextOptions defaultOptions;
160 return MakeMock(mockOptions, defaultOptions);
161}
162
163sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
164 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500165 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500166
167 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
168 if (!context->fGpu) {
169 return nullptr;
170 }
171
Brian Osman7b1678a2019-12-16 09:17:25 -0500172 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500173 return nullptr;
174 }
Chris Daltona378b452019-12-11 13:24:11 -0500175
Robert Phillipsa3457b82018-03-08 11:30:12 -0500176 return context;
177}
178
Mike Kleina55e2142018-10-03 16:34:11 +0000179sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000180#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400181 GrContextOptions defaultOptions;
182 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000183#else
184 return nullptr;
185#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400186}
187
188sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
189 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000190#ifdef SK_VULKAN
Robert Phillipse8345792019-02-07 10:48:24 -0500191 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400192
Greg Danielf730c182018-07-02 20:15:37 +0000193 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500194 if (!context->fGpu) {
195 return nullptr;
196 }
197
Brian Osman7b1678a2019-12-16 09:17:25 -0500198 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500199 return nullptr;
200 }
201 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000202#else
203 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000204#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000205}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500206
207#ifdef SK_METAL
208sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
209 GrContextOptions defaultOptions;
210 return MakeMetal(device, queue, defaultOptions);
211}
212
213sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500214 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500215
216 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
217 if (!context->fGpu) {
218 return nullptr;
219 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400220
Brian Osman7b1678a2019-12-16 09:17:25 -0500221 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500222 return nullptr;
223 }
224 return context;
225}
226#endif
227
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500228#ifdef SK_DIRECT3D
229sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
230 GrContextOptions defaultOptions;
231 return MakeDirect3D(backendContext, defaultOptions);
232}
233
234sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
235 const GrContextOptions& options) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500236 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDirect3D, options));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500237
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500238 context->fGpu = GrD3DGpu::Make(backendContext, options, context.get());
239 if (!context->fGpu) {
240 return nullptr;
241 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500242
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500243 if (!context->init(context->fGpu->refCaps())) {
244 return nullptr;
245 }
246 return context;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500247}
248#endif
249
Stephen White985741a2019-07-18 11:43:45 -0400250#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500251sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400252 GrContextOptions defaultOptions;
253 return MakeDawn(device, defaultOptions);
254}
255
Stephen Whitea521c962019-12-04 09:57:48 -0500256sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Stephen White985741a2019-07-18 11:43:45 -0400257 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
258
259 context->fGpu = GrDawnGpu::Make(device, options, context.get());
260 if (!context->fGpu) {
261 return nullptr;
262 }
263
Brian Osman7b1678a2019-12-16 09:17:25 -0500264 if (!context->init(context->fGpu->refCaps())) {
Stephen White985741a2019-07-18 11:43:45 -0400265 return nullptr;
266 }
267 return context;
268}
269#endif