blob: 07be4fb81bc5051a505fa2af3197d9e8a3847a1b [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)
Adlai Hollere219d1c2020-06-02 11:23:16 -040043 : INHERITED(GrContextThreadSafeProxyPriv::Make(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()) {
Greg Daniel0a2464f2020-05-14 15:45:44 -040051 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050052 }
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 {
Greg Daniel0a2464f2020-05-14 15:45:44 -040068 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050069 fAtlasManager->freeAll();
70
71 INHERITED::freeGpuResources();
72 }
73
74protected:
Adlai Hollere219d1c2020-06-02 11:23:16 -040075 bool init() override {
76 const GrGpu* gpu = this->priv().getGpu();
77 if (!gpu) {
78 return false;
79 }
Robert Phillipsa3457b82018-03-08 11:30:12 -050080
Adlai Hollere219d1c2020-06-02 11:23:16 -040081 fThreadSafeProxy->priv().init(gpu->refCaps());
82 if (!INHERITED::init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050083 return false;
84 }
85
Greg Danielf41b2bd2019-08-22 16:19:24 -040086 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
Greg Daniel93138742019-08-22 17:15:39 -040087 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040088 reduceOpsTaskSplitting = false;
Greg Daniel93138742019-08-22 17:15:39 -040089 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040090 reduceOpsTaskSplitting = true;
Robert Phillips6db27c22019-05-01 10:43:56 -040091 }
92
Greg Danielf41b2bd2019-08-22 16:19:24 -040093 this->setupDrawingManager(true, reduceOpsTaskSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050094
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
Robert Phillips9da87e02019-02-04 13:26:26 -0500105 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500106
Robert Phillips207d24b2020-04-09 10:23:42 -0400107 fAtlasManager = new GrAtlasManager(proxyProvider,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500108 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500109 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500110 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500111
Robert Phillipsa3457b82018-03-08 11:30:12 -0500112 return true;
113 }
114
115 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
116
117private:
118 GrAtlasManager* fAtlasManager;
119
120 typedef GrContext INHERITED;
121};
122
John Rosascoa9b348f2019-11-08 13:18:15 -0800123#ifdef SK_GL
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500124sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500125 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500126 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500127}
128
Brian Salomonc1b9c102018-04-06 09:18:00 -0400129sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
130 return MakeGL(nullptr, options);
131}
132
133sk_sp<GrContext> GrContext::MakeGL() {
134 GrContextOptions defaultOptions;
135 return MakeGL(nullptr, defaultOptions);
136}
137
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500138sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500139 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500140 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500141
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500142 context->fGpu = GrGLGpu::Make(std::move(glInterface), options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400143 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500144 return nullptr;
145 }
146 return context;
147}
John Rosascoa9b348f2019-11-08 13:18:15 -0800148#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500149
150sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
151 GrContextOptions defaultOptions;
152 return MakeMock(mockOptions, defaultOptions);
153}
154
155sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
156 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500157 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500158
159 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400160 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500161 return nullptr;
162 }
Chris Daltona378b452019-12-11 13:24:11 -0500163
Robert Phillipsa3457b82018-03-08 11:30:12 -0500164 return context;
165}
166
Mike Kleina55e2142018-10-03 16:34:11 +0000167sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000168#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400169 GrContextOptions defaultOptions;
170 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000171#else
172 return nullptr;
173#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400174}
175
176sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
177 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000178#ifdef SK_VULKAN
Robert Phillipse8345792019-02-07 10:48:24 -0500179 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400180
Greg Danielf730c182018-07-02 20:15:37 +0000181 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400182 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500183 return nullptr;
184 }
185
Robert Phillipsa3457b82018-03-08 11:30:12 -0500186 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000187#else
188 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000189#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000190}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500191
192#ifdef SK_METAL
193sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
194 GrContextOptions defaultOptions;
195 return MakeMetal(device, queue, defaultOptions);
196}
197
198sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500199 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500200
201 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Adlai Hollere219d1c2020-06-02 11:23:16 -0400202 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500203 return nullptr;
204 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400205
Robert Phillipsa3457b82018-03-08 11:30:12 -0500206 return context;
207}
208#endif
209
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500210#ifdef SK_DIRECT3D
211sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
212 GrContextOptions defaultOptions;
213 return MakeDirect3D(backendContext, defaultOptions);
214}
215
216sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
217 const GrContextOptions& options) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500218 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDirect3D, options));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500219
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500220 context->fGpu = GrD3DGpu::Make(backendContext, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400221 if (!context->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500222 return nullptr;
223 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500224
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500225 return context;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500226}
227#endif
228
Stephen White985741a2019-07-18 11:43:45 -0400229#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500230sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400231 GrContextOptions defaultOptions;
232 return MakeDawn(device, defaultOptions);
233}
234
Stephen Whitea521c962019-12-04 09:57:48 -0500235sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Stephen White985741a2019-07-18 11:43:45 -0400236 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
237
238 context->fGpu = GrDawnGpu::Make(device, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400239 if (!context->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400240 return nullptr;
241 }
242
Stephen White985741a2019-07-18 11:43:45 -0400243 return context;
244}
245#endif