blob: b951dca945a04cd832814d3c86b89099f6341481 [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
Stephen White985741a2019-07-18 11:43:45 -040026#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050027#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040028#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050029
Robert Phillips6db27c22019-05-01 10:43:56 -040030#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040031static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040032#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040033static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040034#endif
35
Brian Salomon57f211b2019-08-21 15:21:09 -040036class GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050037public:
Robert Phillipse8345792019-02-07 10:48:24 -050038 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050039 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050040 , fAtlasManager(nullptr) {
41 }
42
Robert Phillipse8345792019-02-07 10:48:24 -050043 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050044 // this if-test protects against the case where the context is being destroyed
45 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050046 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050047 this->flush();
48 }
49
50 delete fAtlasManager;
51 }
52
53 void abandonContext() override {
54 INHERITED::abandonContext();
55 fAtlasManager->freeAll();
56 }
57
58 void releaseResourcesAndAbandonContext() override {
59 INHERITED::releaseResourcesAndAbandonContext();
60 fAtlasManager->freeAll();
61 }
62
63 void freeGpuResources() override {
64 this->flush();
65 fAtlasManager->freeAll();
66
67 INHERITED::freeGpuResources();
68 }
69
70protected:
Brian Osman7b1678a2019-12-16 09:17:25 -050071 bool init(sk_sp<const GrCaps> caps) override {
72 SkASSERT(caps);
Robert Phillipsa3457b82018-03-08 11:30:12 -050073 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050074
Robert Phillipsbb606772019-02-04 17:50:57 -050075 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
76 this->options(),
77 this->contextID(),
Brian Osman7b1678a2019-12-16 09:17:25 -050078 caps);
Robert Phillipsbb606772019-02-04 17:50:57 -050079
Brian Osman7b1678a2019-12-16 09:17:25 -050080 if (!INHERITED::init(std::move(caps))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050081 return false;
82 }
83
Greg Danielf41b2bd2019-08-22 16:19:24 -040084 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
Greg Daniel93138742019-08-22 17:15:39 -040085 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040086 reduceOpsTaskSplitting = false;
Greg Daniel93138742019-08-22 17:15:39 -040087 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040088 reduceOpsTaskSplitting = true;
Robert Phillips6db27c22019-05-01 10:43:56 -040089 }
90
Greg Danielf41b2bd2019-08-22 16:19:24 -040091 this->setupDrawingManager(true, reduceOpsTaskSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050092
Robert Phillipsbb606772019-02-04 17:50:57 -050093 SkASSERT(this->caps());
94
Robert Phillipsa3457b82018-03-08 11:30:12 -050095 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050096 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050097 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -050098 !(this->caps()->shaderCaps()->floatIs32Bits() ||
99 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500100 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
101 } else {
102 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
103 }
104
Herb Derbya00da612019-03-04 17:10:01 -0500105 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500106 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500107
108 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500109 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500110 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500111 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500112
Robert Phillipsa3457b82018-03-08 11:30:12 -0500113 return true;
114 }
115
116 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
117
118private:
119 GrAtlasManager* fAtlasManager;
120
121 typedef GrContext INHERITED;
122};
123
John Rosascoa9b348f2019-11-08 13:18:15 -0800124#ifdef SK_GL
Robert Phillipsa3457b82018-03-08 11:30:12 -0500125sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
126 GrContextOptions defaultOptions;
127 return MakeGL(std::move(interface), defaultOptions);
128}
129
Brian Salomonc1b9c102018-04-06 09:18:00 -0400130sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
131 return MakeGL(nullptr, options);
132}
133
134sk_sp<GrContext> GrContext::MakeGL() {
135 GrContextOptions defaultOptions;
136 return MakeGL(nullptr, defaultOptions);
137}
138
Robert Phillipsa3457b82018-03-08 11:30:12 -0500139sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
140 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500141 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500142
143 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
144 if (!context->fGpu) {
145 return nullptr;
146 }
147
Brian Osman7b1678a2019-12-16 09:17:25 -0500148 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500149 return nullptr;
150 }
151 return context;
152}
John Rosascoa9b348f2019-11-08 13:18:15 -0800153#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500154
155sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
156 GrContextOptions defaultOptions;
157 return MakeMock(mockOptions, defaultOptions);
158}
159
160sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
161 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500162 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500163
164 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
165 if (!context->fGpu) {
166 return nullptr;
167 }
168
Brian Osman7b1678a2019-12-16 09:17:25 -0500169 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500170 return nullptr;
171 }
Chris Daltona378b452019-12-11 13:24:11 -0500172
173#if GR_TEST_UTILS
174 if (mockOptions && mockOptions->fFailTextureAllocations) {
175 context->testingOnly_setSuppressAllocationWarnings();
176 }
177#endif
178
Robert Phillipsa3457b82018-03-08 11:30:12 -0500179 return context;
180}
181
Mike Kleina55e2142018-10-03 16:34:11 +0000182sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000183#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400184 GrContextOptions defaultOptions;
185 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000186#else
187 return nullptr;
188#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400189}
190
191sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
192 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000193#ifdef SK_VULKAN
194 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500195 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400196
Greg Danielf730c182018-07-02 20:15:37 +0000197 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500198 if (!context->fGpu) {
199 return nullptr;
200 }
201
Brian Osman7b1678a2019-12-16 09:17:25 -0500202 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500203 return nullptr;
204 }
205 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000206#else
207 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000208#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000209}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500210
211#ifdef SK_METAL
212sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
213 GrContextOptions defaultOptions;
214 return MakeMetal(device, queue, defaultOptions);
215}
216
217sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500218 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500219
220 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
221 if (!context->fGpu) {
222 return nullptr;
223 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400224
Brian Osman7b1678a2019-12-16 09:17:25 -0500225 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500226 return nullptr;
227 }
228 return context;
229}
230#endif
231
Stephen White985741a2019-07-18 11:43:45 -0400232#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500233sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400234 GrContextOptions defaultOptions;
235 return MakeDawn(device, defaultOptions);
236}
237
Stephen Whitea521c962019-12-04 09:57:48 -0500238sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Stephen White985741a2019-07-18 11:43:45 -0400239 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
240
241 context->fGpu = GrDawnGpu::Make(device, options, context.get());
242 if (!context->fGpu) {
243 return nullptr;
244 }
245
Brian Osman7b1678a2019-12-16 09:17:25 -0500246 if (!context->init(context->fGpu->refCaps())) {
Stephen White985741a2019-07-18 11:43:45 -0400247 return nullptr;
248 }
249 return context;
250}
251#endif