blob: 4c3f756a65f065af0d8aa2dc90905671c0145cae [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
26
Robert Phillips6db27c22019-05-01 10:43:56 -040027#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
28static const bool kDefaultReduceOpListSplitting = false;
29#else
Robert Phillipsaab728d2019-05-01 18:07:22 -040030static const bool kDefaultReduceOpListSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040031#endif
32
Robert Phillipse8345792019-02-07 10:48:24 -050033class SK_API GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050034public:
Robert Phillipse8345792019-02-07 10:48:24 -050035 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050036 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050037 , fAtlasManager(nullptr) {
38 }
39
Robert Phillipse8345792019-02-07 10:48:24 -050040 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050041 // this if-test protects against the case where the context is being destroyed
42 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050043 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050044 this->flush();
45 }
46
47 delete fAtlasManager;
48 }
49
50 void abandonContext() override {
51 INHERITED::abandonContext();
52 fAtlasManager->freeAll();
53 }
54
55 void releaseResourcesAndAbandonContext() override {
56 INHERITED::releaseResourcesAndAbandonContext();
57 fAtlasManager->freeAll();
58 }
59
60 void freeGpuResources() override {
61 this->flush();
62 fAtlasManager->freeAll();
63
64 INHERITED::freeGpuResources();
65 }
66
67protected:
Robert Phillipsbb606772019-02-04 17:50:57 -050068 bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override {
69 SkASSERT(caps && !FPFactoryCache);
Robert Phillipsa3457b82018-03-08 11:30:12 -050070 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050071
Robert Phillipsbb606772019-02-04 17:50:57 -050072 FPFactoryCache.reset(new GrSkSLFPFactoryCache());
73 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
74 this->options(),
75 this->contextID(),
76 caps, FPFactoryCache);
77
78 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050079 return false;
80 }
81
Robert Phillips6db27c22019-05-01 10:43:56 -040082 bool reduceOpListSplitting = kDefaultReduceOpListSplitting;
83 if (GrContextOptions::Enable::kNo == this->options().fReduceOpListSplitting) {
84 reduceOpListSplitting = false;
85 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpListSplitting) {
86 reduceOpListSplitting = true;
87 }
88
89 this->setupDrawingManager(true, reduceOpListSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050090
Robert Phillipsbb606772019-02-04 17:50:57 -050091 SkASSERT(this->caps());
92
Robert Phillipsa3457b82018-03-08 11:30:12 -050093 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050094 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050095 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -050096 !(this->caps()->shaderCaps()->floatIs32Bits() ||
97 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050098 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
99 } else {
100 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
101 }
102
Herb Derbya00da612019-03-04 17:10:01 -0500103 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500104 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500105
106 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500107 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500108 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500109 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500110
Robert Phillipsa3457b82018-03-08 11:30:12 -0500111 return true;
112 }
113
114 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
115
116private:
117 GrAtlasManager* fAtlasManager;
118
119 typedef GrContext INHERITED;
120};
121
Robert Phillipsa3457b82018-03-08 11:30:12 -0500122sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
123 GrContextOptions defaultOptions;
124 return MakeGL(std::move(interface), defaultOptions);
125}
126
Brian Salomonc1b9c102018-04-06 09:18:00 -0400127sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
128 return MakeGL(nullptr, options);
129}
130
131sk_sp<GrContext> GrContext::MakeGL() {
132 GrContextOptions defaultOptions;
133 return MakeGL(nullptr, defaultOptions);
134}
135
Robert Phillipsa3457b82018-03-08 11:30:12 -0500136sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
137 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500138 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500139
140 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
141 if (!context->fGpu) {
142 return nullptr;
143 }
144
Robert Phillipsbb606772019-02-04 17:50:57 -0500145 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500146 return nullptr;
147 }
148 return context;
149}
150
151sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
152 GrContextOptions defaultOptions;
153 return MakeMock(mockOptions, defaultOptions);
154}
155
156sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
157 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500158 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500159
160 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
161 if (!context->fGpu) {
162 return nullptr;
163 }
164
Robert Phillipsbb606772019-02-04 17:50:57 -0500165 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500166 return nullptr;
167 }
168 return context;
169}
170
Mike Kleina55e2142018-10-03 16:34:11 +0000171sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000172#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400173 GrContextOptions defaultOptions;
174 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000175#else
176 return nullptr;
177#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400178}
179
180sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
181 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000182#ifdef SK_VULKAN
183 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500184 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400185
Greg Danielf730c182018-07-02 20:15:37 +0000186 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500187 if (!context->fGpu) {
188 return nullptr;
189 }
190
Robert Phillipsbb606772019-02-04 17:50:57 -0500191 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500192 return nullptr;
193 }
194 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000195#else
196 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000197#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000198}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500199
200#ifdef SK_METAL
201sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
202 GrContextOptions defaultOptions;
203 return MakeMetal(device, queue, defaultOptions);
204}
205
206sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500207 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500208
209 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
210 if (!context->fGpu) {
211 return nullptr;
212 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400213
Robert Phillipsbb606772019-02-04 17:50:57 -0500214 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500215 return nullptr;
216 }
217 return context;
218}
219#endif
220