blob: 8fbc133e7f421ca3a7707c01916efeb843235b50 [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"
Robert Phillipse19babf2020-04-06 13:57:30 -040019#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050021#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050023#endif
24#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050026#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050027#ifdef SK_DIRECT3D
28#include "src/gpu/d3d/GrD3DGpu.h"
29#endif
Stephen White985741a2019-07-18 11:43:45 -040030#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050031#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040032#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050033
Robert Phillips6db27c22019-05-01 10:43:56 -040034#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040035static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040036#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040037static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040038#endif
39
Brian Salomon57f211b2019-08-21 15:21:09 -040040class GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050041public:
Robert Phillipse8345792019-02-07 10:48:24 -050042 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050043 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050044 , fAtlasManager(nullptr) {
45 }
46
Robert Phillipse8345792019-02-07 10:48:24 -050047 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050048 // this if-test protects against the case where the context is being destroyed
49 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050050 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050051 this->flush();
52 }
53
54 delete fAtlasManager;
55 }
56
57 void abandonContext() override {
58 INHERITED::abandonContext();
59 fAtlasManager->freeAll();
60 }
61
62 void releaseResourcesAndAbandonContext() override {
63 INHERITED::releaseResourcesAndAbandonContext();
64 fAtlasManager->freeAll();
65 }
66
67 void freeGpuResources() override {
68 this->flush();
69 fAtlasManager->freeAll();
70
71 INHERITED::freeGpuResources();
72 }
73
74protected:
Brian Osman7b1678a2019-12-16 09:17:25 -050075 bool init(sk_sp<const GrCaps> caps) override {
76 SkASSERT(caps);
Robert Phillipsa3457b82018-03-08 11:30:12 -050077 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050078
Robert Phillipsbb606772019-02-04 17:50:57 -050079 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
80 this->options(),
81 this->contextID(),
Brian Osman7b1678a2019-12-16 09:17:25 -050082 caps);
Robert Phillipsbb606772019-02-04 17:50:57 -050083
Brian Osman7b1678a2019-12-16 09:17:25 -050084 if (!INHERITED::init(std::move(caps))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050085 return false;
86 }
87
Greg Danielf41b2bd2019-08-22 16:19:24 -040088 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
Greg Daniel93138742019-08-22 17:15:39 -040089 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040090 reduceOpsTaskSplitting = false;
Greg Daniel93138742019-08-22 17:15:39 -040091 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040092 reduceOpsTaskSplitting = true;
Robert Phillips6db27c22019-05-01 10:43:56 -040093 }
94
Greg Danielf41b2bd2019-08-22 16:19:24 -040095 this->setupDrawingManager(true, reduceOpsTaskSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050096
Robert Phillipsbb606772019-02-04 17:50:57 -050097 SkASSERT(this->caps());
98
Robert Phillipsa3457b82018-03-08 11:30:12 -050099 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500100 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -0500101 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -0500102 !(this->caps()->shaderCaps()->floatIs32Bits() ||
103 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500104 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
105 } else {
106 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
107 }
108
Herb Derbya00da612019-03-04 17:10:01 -0500109 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500110 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500111
112 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500113 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500114 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500115 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116
Robert Phillipsa3457b82018-03-08 11:30:12 -0500117 return true;
118 }
119
120 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
121
122private:
123 GrAtlasManager* fAtlasManager;
124
125 typedef GrContext INHERITED;
126};
127
John Rosascoa9b348f2019-11-08 13:18:15 -0800128#ifdef SK_GL
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500129sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500130 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500131 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500132}
133
Brian Salomonc1b9c102018-04-06 09:18:00 -0400134sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
135 return MakeGL(nullptr, options);
136}
137
138sk_sp<GrContext> GrContext::MakeGL() {
139 GrContextOptions defaultOptions;
140 return MakeGL(nullptr, defaultOptions);
141}
142
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500143sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500144 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500145 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500146
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500147 context->fGpu = GrGLGpu::Make(std::move(glInterface), options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500148 if (!context->fGpu) {
149 return nullptr;
150 }
151
Brian Osman7b1678a2019-12-16 09:17:25 -0500152 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500153 return nullptr;
154 }
155 return context;
156}
John Rosascoa9b348f2019-11-08 13:18:15 -0800157#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500158
159sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
160 GrContextOptions defaultOptions;
161 return MakeMock(mockOptions, defaultOptions);
162}
163
164sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
165 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500166 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500167
168 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
169 if (!context->fGpu) {
170 return nullptr;
171 }
172
Brian Osman7b1678a2019-12-16 09:17:25 -0500173 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500174 return nullptr;
175 }
Chris Daltona378b452019-12-11 13:24:11 -0500176
Robert Phillipsa3457b82018-03-08 11:30:12 -0500177 return context;
178}
179
Mike Kleina55e2142018-10-03 16:34:11 +0000180sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000181#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400182 GrContextOptions defaultOptions;
183 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000184#else
185 return nullptr;
186#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400187}
188
189sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
190 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000191#ifdef SK_VULKAN
Robert Phillipse8345792019-02-07 10:48:24 -0500192 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400193
Greg Danielf730c182018-07-02 20:15:37 +0000194 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500195 if (!context->fGpu) {
196 return nullptr;
197 }
198
Brian Osman7b1678a2019-12-16 09:17:25 -0500199 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500200 return nullptr;
201 }
202 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000203#else
204 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000205#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000206}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500207
208#ifdef SK_METAL
209sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
210 GrContextOptions defaultOptions;
211 return MakeMetal(device, queue, defaultOptions);
212}
213
214sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500215 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500216
217 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
218 if (!context->fGpu) {
219 return nullptr;
220 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400221
Brian Osman7b1678a2019-12-16 09:17:25 -0500222 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500223 return nullptr;
224 }
225 return context;
226}
227#endif
228
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500229#ifdef SK_DIRECT3D
230sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
231 GrContextOptions defaultOptions;
232 return MakeDirect3D(backendContext, defaultOptions);
233}
234
235sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
236 const GrContextOptions& options) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500237 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDirect3D, options));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500238
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500239 context->fGpu = GrD3DGpu::Make(backendContext, options, context.get());
240 if (!context->fGpu) {
241 return nullptr;
242 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500243
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500244 if (!context->init(context->fGpu->refCaps())) {
245 return nullptr;
246 }
247 return context;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500248}
249#endif
250
Stephen White985741a2019-07-18 11:43:45 -0400251#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500252sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400253 GrContextOptions defaultOptions;
254 return MakeDawn(device, defaultOptions);
255}
256
Stephen Whitea521c962019-12-04 09:57:48 -0500257sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Stephen White985741a2019-07-18 11:43:45 -0400258 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
259
260 context->fGpu = GrDawnGpu::Make(device, options, context.get());
261 if (!context->fGpu) {
262 return nullptr;
263 }
264
Brian Osman7b1678a2019-12-16 09:17:25 -0500265 if (!context->init(context->fGpu->refCaps())) {
Stephen White985741a2019-07-18 11:43:45 -0400266 return nullptr;
267 }
268 return context;
269}
270#endif