blob: 9fdb3b8aa30fb1e1df9e2b062d764a7174d97d87 [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
Robert Phillipsa3457b82018-03-08 11:30:12 -05009#include "GrContext.h"
10
11#include "GrContextPriv.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050012#include "GrContextThreadSafeProxy.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050013#include "GrGpu.h"
14
Ethan Nicholas00543112018-07-31 09:44:36 -040015#include "effects/GrSkSLFP.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050016#include "gl/GrGLGpu.h"
17#include "mock/GrMockGpu.h"
Herb Derby081e6f32019-01-16 13:46:02 -050018#include "text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050019#ifdef SK_METAL
20#include "mtl/GrMtlTrampoline.h"
21#endif
22#ifdef SK_VULKAN
23#include "vk/GrVkGpu.h"
24#endif
25
26class SK_API GrDirectContext : public GrContext {
27public:
Robert Phillipsc1541ae2019-02-04 12:05:37 -050028 GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
29 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050030 , fAtlasManager(nullptr) {
31 }
32
33 ~GrDirectContext() override {
34 // this if-test protects against the case where the context is being destroyed
35 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050036 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050037 this->flush();
38 }
39
40 delete fAtlasManager;
41 }
42
43 void abandonContext() override {
44 INHERITED::abandonContext();
45 fAtlasManager->freeAll();
46 }
47
48 void releaseResourcesAndAbandonContext() override {
49 INHERITED::releaseResourcesAndAbandonContext();
50 fAtlasManager->freeAll();
51 }
52
53 void freeGpuResources() override {
54 this->flush();
55 fAtlasManager->freeAll();
56
57 INHERITED::freeGpuResources();
58 }
59
60protected:
Robert Phillipsc1541ae2019-02-04 12:05:37 -050061 bool init() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050062 SkASSERT(fCaps); // should've been set in ctor
63 SkASSERT(!fThreadSafeProxy);
Ethan Nicholas00543112018-07-31 09:44:36 -040064 SkASSERT(!fFPFactoryCache);
65 fFPFactoryCache.reset(new GrSkSLFPFactoryCache());
Robert Phillipsfd0d9702019-02-01 10:19:42 -050066 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->contextID(),
Robert Phillips4217ea72019-01-30 13:08:28 -050067 this->backend(),
Robert Phillipsc1541ae2019-02-04 12:05:37 -050068 this->options(), fFPFactoryCache));
Robert Phillipsa3457b82018-03-08 11:30:12 -050069
Robert Phillipsc1541ae2019-02-04 12:05:37 -050070 if (!INHERITED::initCommon()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050071 return false;
72 }
73
74 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050075 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050076 // multitexturing supported only if range can represent the index + texcoords fully
77 !(fCaps->shaderCaps()->floatIs32Bits() || fCaps->shaderCaps()->integerSupport())) {
78 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
79 } else {
80 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
81 }
82
Robert Phillips9da87e02019-02-04 13:26:26 -050083 GrStrikeCache* glyphCache = this->priv().getGlyphCache();
84 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -050085
86 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -050087 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -050088 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -050089 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -050090
Robert Phillipsa3457b82018-03-08 11:30:12 -050091 return true;
92 }
93
94 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
95
96private:
97 GrAtlasManager* fAtlasManager;
98
99 typedef GrContext INHERITED;
100};
101
Robert Phillipsa3457b82018-03-08 11:30:12 -0500102sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
103 GrContextOptions defaultOptions;
104 return MakeGL(std::move(interface), defaultOptions);
105}
106
Brian Salomonc1b9c102018-04-06 09:18:00 -0400107sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
108 return MakeGL(nullptr, options);
109}
110
111sk_sp<GrContext> GrContext::MakeGL() {
112 GrContextOptions defaultOptions;
113 return MakeGL(nullptr, defaultOptions);
114}
115
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
117 const GrContextOptions& options) {
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500118 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500119
120 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
121 if (!context->fGpu) {
122 return nullptr;
123 }
124
125 context->fCaps = context->fGpu->refCaps();
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500126 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500127 return nullptr;
128 }
129 return context;
130}
131
132sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
133 GrContextOptions defaultOptions;
134 return MakeMock(mockOptions, defaultOptions);
135}
136
137sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
138 const GrContextOptions& options) {
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500139 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500140
141 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
142 if (!context->fGpu) {
143 return nullptr;
144 }
145
146 context->fCaps = context->fGpu->refCaps();
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500147 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500148 return nullptr;
149 }
150 return context;
151}
152
Mike Kleina55e2142018-10-03 16:34:11 +0000153sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000154#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400155 GrContextOptions defaultOptions;
156 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000157#else
158 return nullptr;
159#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400160}
161
162sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
163 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000164#ifdef SK_VULKAN
165 GrContextOptions defaultOptions;
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500166 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400167
Greg Danielf730c182018-07-02 20:15:37 +0000168 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500169 if (!context->fGpu) {
170 return nullptr;
171 }
172
173 context->fCaps = context->fGpu->refCaps();
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500174 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500175 return nullptr;
176 }
177 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000178#else
179 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000180#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000181}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500182
183#ifdef SK_METAL
184sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
185 GrContextOptions defaultOptions;
186 return MakeMetal(device, queue, defaultOptions);
187}
188
189sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500190 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500191
192 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
193 if (!context->fGpu) {
194 return nullptr;
195 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400196
197 context->fCaps = context->fGpu->refCaps();
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500198 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500199 return nullptr;
200 }
201 return context;
202}
203#endif
204