blob: bf048a014b312ca70a97cb385beccf41893bcf8d [file] [log] [blame]
bsalomoned0bcad2015-05-04 10:36:42 -07001/*
2 * Copyright 2015 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
8#include "GrResourceProvider.h"
9
Greg Daniela5cb7812017-06-16 09:45:32 -040010#include "GrBackendSemaphore.h"
cdalton397536c2016-03-25 12:15:03 -070011#include "GrBuffer.h"
robertphillips5fa7f302016-07-21 09:21:04 -070012#include "GrCaps.h"
Robert Phillips26c90e02017-03-14 14:39:29 -040013#include "GrContext.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040014#include "GrContextPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070015#include "GrGpu.h"
Robert Phillips67d52cf2017-06-05 13:38:13 -040016#include "GrPath.h"
kkinnunencabe20c2015-06-01 01:37:26 -070017#include "GrPathRendering.h"
egdanielec00d942015-09-14 12:56:10 -070018#include "GrRenderTargetPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070019#include "GrResourceCache.h"
20#include "GrResourceKey.h"
Greg Danield85f97d2017-03-07 13:37:21 -050021#include "GrSemaphore.h"
egdanielec00d942015-09-14 12:56:10 -070022#include "GrStencilAttachment.h"
Robert Phillipsb66b42f2017-03-14 08:53:02 -040023#include "GrSurfaceProxyPriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050024#include "GrTexturePriv.h"
25#include "../private/GrSingleOwner.h"
Robert Phillips45fdae12017-04-17 12:57:27 -040026#include "SkGr.h"
halcanary4dbbd042016-06-07 17:21:10 -070027#include "SkMathPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070028
29GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
30
Robert Phillips1bfece82017-06-01 13:56:52 -040031const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
Brian Osman32342f02017-03-04 08:12:46 -050032
33#define ASSERT_SINGLE_OWNER \
34 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
35
joshualitt6d0872d2016-01-11 08:27:48 -080036GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
Brian Osman32342f02017-03-04 08:12:46 -050037 : fCache(cache)
38 , fGpu(gpu)
39#ifdef SK_DEBUG
40 , fSingleOwner(owner)
41#endif
42 {
Robert Phillips26c90e02017-03-14 14:39:29 -040043 fCaps = sk_ref_sp(fGpu->caps());
44
bsalomoned0bcad2015-05-04 10:36:42 -070045 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
46 fQuadIndexBufferKey = gQuadIndexBufferKey;
47}
48
Robert Phillipsf7a72612017-03-31 10:03:45 -040049bool GrResourceProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -040050 return proxy->priv().isExact() || (SkIsPow2(proxy->width()) && SkIsPow2(proxy->height()));
51}
Brian Osman32342f02017-03-04 08:12:46 -050052
Brian Salomond34edf32017-05-19 15:45:48 -040053bool validate_desc(const GrSurfaceDesc& desc, const GrCaps& caps, int levelCount = 0) {
54 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
55 return false;
56 }
57 if (!caps.isConfigTexturable(desc.fConfig)) {
58 return false;
59 }
60 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
61 if (!caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
62 return false;
63 }
64 } else {
65 if (desc.fSampleCnt) {
66 return false;
67 }
68 }
Brian Osman48c99192017-06-02 08:45:06 -040069 if (levelCount > 1 && (GrPixelConfigIsSint(desc.fConfig) || !caps.mipMapSupport())) {
Brian Salomond34edf32017-05-19 15:45:48 -040070 return false;
71 }
72 return true;
73}
74
Robert Phillips8e8c7552017-07-10 12:06:05 -040075sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -040076 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips8e8c7552017-07-10 12:06:05 -040077 SkDestinationSurfaceColorMode mipColorMode) {
Brian Osman32342f02017-03-04 08:12:46 -050078 ASSERT_SINGLE_OWNER
79
Robert Phillips590533f2017-07-11 14:22:35 -040080 SkASSERT(mipLevelCount >= 1);
Robert Phillips1119dc32017-04-11 12:54:57 -040081
Brian Osman32342f02017-03-04 08:12:46 -050082 if (this->isAbandoned()) {
83 return nullptr;
84 }
Robert Phillips1119dc32017-04-11 12:54:57 -040085
Robert Phillips590533f2017-07-11 14:22:35 -040086 if (!validate_desc(desc, *fCaps, mipLevelCount)) {
Brian Osman32342f02017-03-04 08:12:46 -050087 return nullptr;
88 }
Brian Osman32342f02017-03-04 08:12:46 -050089
Robert Phillips590533f2017-07-11 14:22:35 -040090 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels, mipLevelCount));
Robert Phillipse78b7252017-04-06 07:59:41 -040091 if (tex) {
92 tex->texturePriv().setMipColorMode(mipColorMode);
Robert Phillipsa4c41b32017-03-15 13:02:45 -040093 }
Robert Phillipse78b7252017-04-06 07:59:41 -040094
Robert Phillips8e8c7552017-07-10 12:06:05 -040095 return tex;
Brian Osman32342f02017-03-04 08:12:46 -050096}
97
Robert Phillips45fdae12017-04-17 12:57:27 -040098sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
Greg Daniel21918232017-09-08 14:46:23 -040099 SkBudgeted budgeted, uint32_t flags) {
Robert Phillips45fdae12017-04-17 12:57:27 -0400100 sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
101 if (tex && SkBudgeted::kNo == budgeted) {
102 tex->resourcePriv().makeUnbudgeted();
103 }
104
105 return tex;
106}
107
108static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) {
109 SkColorType colorType;
110 if (!GrPixelConfigToColorType(config, &colorType)) {
111 return false;
112 }
113
114 *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr);
115 return true;
116}
117
118sk_sp<GrTextureProxy> GrResourceProvider::createTextureProxy(const GrSurfaceDesc& desc,
119 SkBudgeted budgeted,
120 const GrMipLevel& mipLevel) {
Robert Phillips774831a2017-04-20 10:19:33 -0400121 ASSERT_SINGLE_OWNER
122
123 if (this->isAbandoned()) {
124 return nullptr;
125 }
126
Robert Phillips45fdae12017-04-17 12:57:27 -0400127 if (!mipLevel.fPixels) {
128 return nullptr;
129 }
130
Brian Salomond34edf32017-05-19 15:45:48 -0400131 if (!validate_desc(desc, *fCaps)) {
132 return nullptr;
133 }
134
Robert Phillips45fdae12017-04-17 12:57:27 -0400135 GrContext* context = fGpu->getContext();
136
Robert Phillips92de6312017-05-23 07:43:48 -0400137 SkImageInfo srcInfo;
Robert Phillips45fdae12017-04-17 12:57:27 -0400138
Robert Phillips92de6312017-05-23 07:43:48 -0400139 if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) {
Greg Daniel21918232017-09-08 14:46:23 -0400140 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, 0);
Robert Phillips066f0202017-07-25 10:16:35 -0400141 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex), desc.fOrigin);
Robert Phillipsfe50d962017-06-01 10:22:03 -0400142 if (proxy) {
143 sk_sp<GrSurfaceContext> sContext =
144 context->contextPriv().makeWrappedSurfaceContext(std::move(proxy), nullptr);
145 if (sContext) {
146 if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) {
147 return sContext->asTextureProxyRef();
148 }
Robert Phillips45fdae12017-04-17 12:57:27 -0400149 }
150 }
151 }
152
Robert Phillips590533f2017-07-11 14:22:35 -0400153 sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, &mipLevel, 1));
Robert Phillips066f0202017-07-25 10:16:35 -0400154 return GrSurfaceProxy::MakeWrapped(std::move(tex), desc.fOrigin);
Robert Phillips45fdae12017-04-17 12:57:27 -0400155}
156
Robert Phillipse78b7252017-04-06 07:59:41 -0400157sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
158 uint32_t flags) {
159 ASSERT_SINGLE_OWNER
160
161 if (this->isAbandoned()) {
162 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500163 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400164
Brian Salomond34edf32017-05-19 15:45:48 -0400165 if (!validate_desc(desc, *fCaps)) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400166 return nullptr;
167 }
168
Greg Daniel21918232017-09-08 14:46:23 -0400169 sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
Robert Phillips92de6312017-05-23 07:43:48 -0400170 if (tex) {
171 return tex;
Robert Phillipse78b7252017-04-06 07:59:41 -0400172 }
173
Robert Phillips67d52cf2017-06-05 13:38:13 -0400174 return fGpu->createTexture(desc, budgeted);
Brian Osman32342f02017-03-04 08:12:46 -0500175}
176
Robert Phillips67d52cf2017-06-05 13:38:13 -0400177sk_sp<GrTexture> GrResourceProvider::createApproxTexture(const GrSurfaceDesc& desc,
178 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500179 ASSERT_SINGLE_OWNER
180 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
Brian Osman32342f02017-03-04 08:12:46 -0500181
Brian Osman32342f02017-03-04 08:12:46 -0500182 if (this->isAbandoned()) {
183 return nullptr;
184 }
Robert Phillips1119dc32017-04-11 12:54:57 -0400185
Brian Salomond34edf32017-05-19 15:45:48 -0400186 if (!validate_desc(desc, *fCaps)) {
187 return nullptr;
188 }
189
Greg Daniel29bf84f2017-09-25 12:25:12 -0400190 SkTCopyOnFirstWrite<GrSurfaceDesc> copyDesc(desc);
191
192 // bin by pow2 with a reasonable min
193 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
194 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
195 GrSurfaceDesc* wdesc = copyDesc.writable();
196 wdesc->fWidth = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fWidth));
197 wdesc->fHeight = SkTMax(kMinScratchTextureSize, GrNextPow2(desc.fHeight));
198 }
199
200 if (auto tex = this->refScratchTexture(*copyDesc, flags)) {
201 return tex;
202 }
203
204 return fGpu->createTexture(*copyDesc, SkBudgeted::kYes);
Brian Osman32342f02017-03-04 08:12:46 -0500205}
206
Greg Daniel29bf84f2017-09-25 12:25:12 -0400207sk_sp<GrTexture> GrResourceProvider::refScratchTexture(const GrSurfaceDesc& desc,
Greg Daniel21918232017-09-08 14:46:23 -0400208 uint32_t flags) {
Brian Osman32342f02017-03-04 08:12:46 -0500209 ASSERT_SINGLE_OWNER
210 SkASSERT(!this->isAbandoned());
Greg Daniel29bf84f2017-09-25 12:25:12 -0400211 SkASSERT(validate_desc(desc, *fCaps));
Brian Osman32342f02017-03-04 08:12:46 -0500212
Brian Salomond17b4a62017-05-23 16:53:47 -0400213 // We could make initial clears work with scratch textures but it is a rare case so we just opt
214 // to fall back to making a new texture.
Greg Daniel29bf84f2017-09-25 12:25:12 -0400215 if (!SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
216 (fGpu->caps()->reuseScratchTextures() || (desc.fFlags & kRenderTarget_GrSurfaceFlag))) {
Brian Osman32342f02017-03-04 08:12:46 -0500217
218 GrScratchKey key;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400219 GrTexturePriv::ComputeScratchKey(desc, &key);
Brian Osman32342f02017-03-04 08:12:46 -0500220 uint32_t scratchFlags = 0;
221 if (kNoPendingIO_Flag & flags) {
222 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
Greg Daniel29bf84f2017-09-25 12:25:12 -0400223 } else if (!(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
Brian Osman32342f02017-03-04 08:12:46 -0500224 // If it is not a render target then it will most likely be populated by
225 // writePixels() which will trigger a flush if the texture has pending IO.
226 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
227 }
228 GrGpuResource* resource = fCache->findAndRefScratchResource(key,
Greg Daniel29bf84f2017-09-25 12:25:12 -0400229 GrSurface::WorstCaseSize(desc),
230 scratchFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500231 if (resource) {
232 GrSurface* surface = static_cast<GrSurface*>(resource);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400233 return sk_sp<GrTexture>(surface->asTexture());
Brian Osman32342f02017-03-04 08:12:46 -0500234 }
235 }
236
Brian Osman32342f02017-03-04 08:12:46 -0500237 return nullptr;
238}
239
Greg Daniel7ef28f32017-04-20 16:41:55 +0000240sk_sp<GrTexture> GrResourceProvider::wrapBackendTexture(const GrBackendTexture& tex,
Brian Osman32342f02017-03-04 08:12:46 -0500241 GrWrapOwnership ownership) {
242 ASSERT_SINGLE_OWNER
243 if (this->isAbandoned()) {
244 return nullptr;
245 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400246 return fGpu->wrapBackendTexture(tex, ownership);
Brian Salomond17f6582017-07-19 18:28:58 -0400247}
248
249sk_sp<GrTexture> GrResourceProvider::wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400250 int sampleCnt,
251 GrWrapOwnership ownership) {
252 ASSERT_SINGLE_OWNER
253 if (this->isAbandoned()) {
254 return nullptr;
255 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400256 return fGpu->wrapRenderableBackendTexture(tex, sampleCnt, ownership);
Brian Osman32342f02017-03-04 08:12:46 -0500257}
258
259sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400260 const GrBackendRenderTarget& backendRT)
Brian Osman32342f02017-03-04 08:12:46 -0500261{
262 ASSERT_SINGLE_OWNER
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400263 return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(backendRT);
Brian Osman32342f02017-03-04 08:12:46 -0500264}
265
266void GrResourceProvider::assignUniqueKeyToResource(const GrUniqueKey& key,
267 GrGpuResource* resource) {
268 ASSERT_SINGLE_OWNER
269 if (this->isAbandoned() || !resource) {
270 return;
271 }
272 resource->resourcePriv().setUniqueKey(key);
273}
274
275GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
276 ASSERT_SINGLE_OWNER
277 return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
278}
279
280GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
281 ASSERT_SINGLE_OWNER
282 GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
283 if (resource) {
284 GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
285 SkASSERT(texture);
286 return texture;
287 }
Ben Wagnera93a14a2017-08-28 10:34:05 -0400288 return nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500289}
290
Robert Phillips646e4292017-06-13 12:44:56 -0400291void GrResourceProvider::assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
292 SkASSERT(key.isValid());
293 this->assignUniqueKeyToResource(key, texture);
294}
295
Robert Phillipsd3749482017-03-14 09:17:43 -0400296void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
297 ASSERT_SINGLE_OWNER
298 SkASSERT(key.isValid());
299 if (this->isAbandoned() || !proxy) {
300 return;
301 }
302
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400303 fCache->assignUniqueKeyToProxy(key, proxy);
Robert Phillipsd3749482017-03-14 09:17:43 -0400304}
305
Robert Phillips066f0202017-07-25 10:16:35 -0400306sk_sp<GrTextureProxy> GrResourceProvider::findProxyByUniqueKey(const GrUniqueKey& key,
307 GrSurfaceOrigin origin) {
Robert Phillipsd3749482017-03-14 09:17:43 -0400308 ASSERT_SINGLE_OWNER
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400309 return this->isAbandoned() ? nullptr : fCache->findProxyByUniqueKey(key, origin);
Robert Phillipsd3749482017-03-14 09:17:43 -0400310}
311
Chris Daltonff926502017-05-03 14:36:54 -0400312const GrBuffer* GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
cdalton397536c2016-03-25 12:15:03 -0700313 int patternSize,
314 int reps,
315 int vertCount,
316 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -0700317 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
318
Brian Salomon09d994e2016-12-21 11:14:46 -0500319 // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
cdaltone2e71c22016-04-07 18:13:29 -0700320 GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern,
cdalton397536c2016-03-25 12:15:03 -0700321 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -0700322 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700323 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700324 }
Mike Klein65e70632017-09-21 13:46:28 -0400325
326 SkAutoTArray<uint16_t> data(reps * patternSize);
bsalomoned0bcad2015-05-04 10:36:42 -0700327 for (int i = 0; i < reps; ++i) {
328 int baseIdx = i * patternSize;
329 uint16_t baseVert = (uint16_t)(i * vertCount);
330 for (int j = 0; j < patternSize; ++j) {
331 data[baseIdx+j] = baseVert + pattern[j];
332 }
333 }
Mike Klein65e70632017-09-21 13:46:28 -0400334
335 if (!buffer->updateData(data.get(), bufferSize)) {
336 buffer->unref();
337 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -0700338 }
Mike Klein65e70632017-09-21 13:46:28 -0400339
bsalomoned0bcad2015-05-04 10:36:42 -0700340 this->assignUniqueKeyToResource(key, buffer);
341 return buffer;
342}
343
Brian Salomon34169692017-08-28 15:32:01 -0400344static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
345
cdalton397536c2016-03-25 12:15:03 -0700346const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -0700347 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
348 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
349
Chris Daltonff926502017-05-03 14:36:54 -0400350 return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
bsalomoned0bcad2015-05-04 10:36:42 -0700351}
352
Brian Salomon34169692017-08-28 15:32:01 -0400353int GrResourceProvider::QuadCountOfQuadBuffer() { return kMaxQuads; }
354
Robert Phillips67d52cf2017-06-05 13:38:13 -0400355sk_sp<GrPath> GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400356 if (this->isAbandoned()) {
357 return nullptr;
358 }
359
bsalomon706f08f2015-05-22 07:35:58 -0700360 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700361 return this->gpu()->pathRendering()->createPath(path, style);
bsalomon706f08f2015-05-22 07:35:58 -0700362}
363
Robert Phillips67d52cf2017-06-05 13:38:13 -0400364sk_sp<GrPathRange> GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
365 const GrStyle& style) {
Robert Phillips0f171812017-09-21 14:25:31 -0400366 if (this->isAbandoned()) {
367 return nullptr;
368 }
369
bsalomon706f08f2015-05-22 07:35:58 -0700370 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700371 return this->gpu()->pathRendering()->createPathRange(gen, style);
bsalomon706f08f2015-05-22 07:35:58 -0700372}
373
Robert Phillips67d52cf2017-06-05 13:38:13 -0400374sk_sp<GrPathRange> GrResourceProvider::createGlyphs(const SkTypeface* tf,
375 const SkScalerContextEffects& effects,
376 const SkDescriptor* desc,
377 const GrStyle& style) {
bsalomon706f08f2015-05-22 07:35:58 -0700378
379 SkASSERT(this->gpu()->pathRendering());
bsalomon6663acf2016-05-10 09:14:17 -0700380 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style);
bsalomon706f08f2015-05-22 07:35:58 -0700381}
382
cdaltone2e71c22016-04-07 18:13:29 -0700383GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700384 GrAccessPattern accessPattern, uint32_t flags,
385 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700386 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700387 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700388 }
cdaltond37fe762016-04-21 07:41:50 -0700389 if (kDynamic_GrAccessPattern != accessPattern) {
390 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
391 }
csmartdalton485a1202016-07-13 10:16:32 -0700392 if (!(flags & kRequireGpuMemory_Flag) &&
393 this->gpu()->caps()->preferClientSideDynamicBuffers() &&
394 GrBufferTypeIsVertexOrIndex(intendedType) &&
395 kDynamic_GrAccessPattern == accessPattern) {
396 return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data);
397 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700398
cdaltond37fe762016-04-21 07:41:50 -0700399 // bin by pow2 with a reasonable min
Robert Phillips9e380472016-10-28 12:15:03 -0400400 static const size_t MIN_SIZE = 1 << 12;
401 size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size));
robertphillips1b8e1b52015-06-24 06:54:10 -0700402
cdaltond37fe762016-04-21 07:41:50 -0700403 GrScratchKey key;
csmartdalton485a1202016-07-13 10:16:32 -0700404 GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
cdaltond37fe762016-04-21 07:41:50 -0700405 uint32_t scratchFlags = 0;
406 if (flags & kNoPendingIO_Flag) {
407 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
408 } else {
409 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
410 }
411 GrBuffer* buffer = static_cast<GrBuffer*>(
412 this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags));
413 if (!buffer) {
414 buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
415 if (!buffer) {
416 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700417 }
418 }
cdaltond37fe762016-04-21 07:41:50 -0700419 if (data) {
420 buffer->updateData(data, size);
421 }
csmartdalton485a1202016-07-13 10:16:32 -0700422 SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs.
cdaltond37fe762016-04-21 07:41:50 -0700423 return buffer;
jvanverth17aa0472016-01-05 10:41:27 -0800424}
425
Robert Phillipsc0192e32017-09-21 12:00:26 -0400426bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
egdanielec00d942015-09-14 12:56:10 -0700427 SkASSERT(rt);
428 if (rt->renderTargetPriv().getStencilAttachment()) {
Robert Phillipsc0192e32017-09-21 12:00:26 -0400429 return true;
egdanielec00d942015-09-14 12:56:10 -0700430 }
431
432 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
433 GrUniqueKey sbKey;
434
435 int width = rt->width();
436 int height = rt->height();
437#if 0
438 if (this->caps()->oversizedStencilSupport()) {
439 width = SkNextPow2(width);
440 height = SkNextPow2(height);
441 }
442#endif
Robert Phillipscb2e2352017-08-30 16:44:40 -0400443 SkDEBUGCODE(bool newStencil = false;)
egdanielec00d942015-09-14 12:56:10 -0700444 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
445 rt->numStencilSamples(), &sbKey);
446 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
447 this->findAndRefResourceByUniqueKey(sbKey));
448 if (!stencil) {
449 // Need to try and create a new stencil
450 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
451 if (stencil) {
Robert Phillipsf7cf81a2017-03-02 10:23:52 -0500452 this->assignUniqueKeyToResource(sbKey, stencil);
Robert Phillipscb2e2352017-08-30 16:44:40 -0400453 SkDEBUGCODE(newStencil = true;)
egdanielec00d942015-09-14 12:56:10 -0700454 }
455 }
456 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
Robert Phillips95214472017-08-08 18:00:03 -0400457#ifdef SK_DEBUG
458 // Fill the SB with an inappropriate value. opLists that use the
459 // SB should clear it properly.
Robert Phillipscb2e2352017-08-30 16:44:40 -0400460 if (newStencil) {
461 SkASSERT(stencil->isDirty());
462 this->gpu()->clearStencil(rt, 0xFFFF);
463 SkASSERT(stencil->isDirty());
464 }
Robert Phillips95214472017-08-08 18:00:03 -0400465#endif
egdanielec00d942015-09-14 12:56:10 -0700466 }
467 }
Robert Phillipsc0192e32017-09-21 12:00:26 -0400468 return SkToBool(rt->renderTargetPriv().getStencilAttachment());
egdanielec00d942015-09-14 12:56:10 -0700469}
470
bungeman6bd52842016-10-27 09:30:08 -0700471sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400472 const GrBackendTexture& tex, int sampleCnt)
bungeman6bd52842016-10-27 09:30:08 -0700473{
ericrkf7b8b8a2016-02-24 14:49:51 -0800474 if (this->isAbandoned()) {
475 return nullptr;
476 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400477 return this->gpu()->wrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800478}
Greg Danield85f97d2017-03-07 13:37:21 -0500479
Greg Daniela5cb7812017-06-16 09:45:32 -0400480sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrResourceProvider::makeSemaphore(bool isOwned) {
481 return fGpu->makeSemaphore(isOwned);
482}
483
484sk_sp<GrSemaphore> GrResourceProvider::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
485 GrWrapOwnership ownership) {
486 ASSERT_SINGLE_OWNER
487 return this->isAbandoned() ? nullptr : fGpu->wrapBackendSemaphore(semaphore, ownership);
Greg Danield85f97d2017-03-07 13:37:21 -0500488}
489
490void GrResourceProvider::takeOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
491 semaphore->resetGpu(fGpu);
492}
493
494void GrResourceProvider::releaseOwnershipOfSemaphore(sk_sp<GrSemaphore> semaphore) {
495 semaphore->resetGpu(nullptr);
496}