blob: ffd224da06947443c34e6b3a9ec8845fce2eda10 [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
31static const bool kDefaultReduceOpListSplitting = false;
32#else
Robert Phillipsaab728d2019-05-01 18:07:22 -040033static const bool kDefaultReduceOpListSplitting = 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:
Robert Phillipsbb606772019-02-04 17:50:57 -050071 bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override {
72 SkASSERT(caps && !FPFactoryCache);
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 FPFactoryCache.reset(new GrSkSLFPFactoryCache());
76 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
77 this->options(),
78 this->contextID(),
79 caps, FPFactoryCache);
80
81 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050082 return false;
83 }
84
Robert Phillips6db27c22019-05-01 10:43:56 -040085 bool reduceOpListSplitting = kDefaultReduceOpListSplitting;
86 if (GrContextOptions::Enable::kNo == this->options().fReduceOpListSplitting) {
87 reduceOpListSplitting = false;
88 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpListSplitting) {
89 reduceOpListSplitting = true;
90 }
91
92 this->setupDrawingManager(true, reduceOpListSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050093
Robert Phillipsbb606772019-02-04 17:50:57 -050094 SkASSERT(this->caps());
95
Robert Phillipsa3457b82018-03-08 11:30:12 -050096 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050097 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050098 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -050099 !(this->caps()->shaderCaps()->floatIs32Bits() ||
100 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500101 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
102 } else {
103 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
104 }
105
Herb Derbya00da612019-03-04 17:10:01 -0500106 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500107 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500108
109 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500110 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500111 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500112 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500113
Robert Phillipsa3457b82018-03-08 11:30:12 -0500114 return true;
115 }
116
117 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
118
119private:
120 GrAtlasManager* fAtlasManager;
121
122 typedef GrContext INHERITED;
123};
124
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
Robert Phillipsbb606772019-02-04 17:50:57 -0500148 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500149 return nullptr;
150 }
151 return context;
152}
153
154sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
155 GrContextOptions defaultOptions;
156 return MakeMock(mockOptions, defaultOptions);
157}
158
159sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
160 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500161 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500162
163 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
164 if (!context->fGpu) {
165 return nullptr;
166 }
167
Robert Phillipsbb606772019-02-04 17:50:57 -0500168 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500169 return nullptr;
170 }
171 return context;
172}
173
Mike Kleina55e2142018-10-03 16:34:11 +0000174sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000175#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400176 GrContextOptions defaultOptions;
177 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000178#else
179 return nullptr;
180#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400181}
182
183sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
184 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000185#ifdef SK_VULKAN
186 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500187 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400188
Greg Danielf730c182018-07-02 20:15:37 +0000189 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500190 if (!context->fGpu) {
191 return nullptr;
192 }
193
Robert Phillipsbb606772019-02-04 17:50:57 -0500194 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500195 return nullptr;
196 }
197 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000198#else
199 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000200#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000201}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500202
203#ifdef SK_METAL
204sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
205 GrContextOptions defaultOptions;
206 return MakeMetal(device, queue, defaultOptions);
207}
208
209sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500210 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500211
212 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
213 if (!context->fGpu) {
214 return nullptr;
215 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400216
Robert Phillipsbb606772019-02-04 17:50:57 -0500217 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500218 return nullptr;
219 }
220 return context;
221}
222#endif
223
Stephen White985741a2019-07-18 11:43:45 -0400224#ifdef SK_DAWN
225sk_sp<GrContext> GrContext::MakeDawn(const dawn::Device& device) {
226 GrContextOptions defaultOptions;
227 return MakeDawn(device, defaultOptions);
228}
229
230sk_sp<GrContext> GrContext::MakeDawn(const dawn::Device& device, const GrContextOptions& options) {
231 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
232
233 context->fGpu = GrDawnGpu::Make(device, options, context.get());
234 if (!context->fGpu) {
235 return nullptr;
236 }
237
238 if (!context->init(context->fGpu->refCaps(), nullptr)) {
239 return nullptr;
240 }
241 return context;
242}
243#endif