blob: 922e5be320daaf70d00bae144e3e1370a5c948e9 [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
cdalton397536c2016-03-25 12:15:03 -070010#include "GrBuffer.h"
bsalomoned0bcad2015-05-04 10:36:42 -070011#include "GrGpu.h"
kkinnunencabe20c2015-06-01 01:37:26 -070012#include "GrPathRendering.h"
egdanielec00d942015-09-14 12:56:10 -070013#include "GrRenderTarget.h"
14#include "GrRenderTargetPriv.h"
bsalomoned0bcad2015-05-04 10:36:42 -070015#include "GrResourceCache.h"
16#include "GrResourceKey.h"
egdanielec00d942015-09-14 12:56:10 -070017#include "GrStencilAttachment.h"
bsalomoned0bcad2015-05-04 10:36:42 -070018
19GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
20
joshualitt6d0872d2016-01-11 08:27:48 -080021GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
22 : INHERITED(gpu, cache, owner) {
bsalomoned0bcad2015-05-04 10:36:42 -070023 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
24 fQuadIndexBufferKey = gQuadIndexBufferKey;
25}
26
cdalton397536c2016-03-25 12:15:03 -070027const GrBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern,
28 int patternSize,
29 int reps,
30 int vertCount,
31 const GrUniqueKey& key) {
bsalomoned0bcad2015-05-04 10:36:42 -070032 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
33
bsalomoneae62002015-07-31 13:59:30 -070034 // This is typically used in GrBatchs, so we assume kNoPendingIO.
cdaltone2e71c22016-04-07 18:13:29 -070035 GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern,
cdalton397536c2016-03-25 12:15:03 -070036 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -070037 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -070038 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -070039 }
40 uint16_t* data = (uint16_t*) buffer->map();
halcanary96fcdcc2015-08-27 07:41:13 -070041 bool useTempData = (nullptr == data);
bsalomoned0bcad2015-05-04 10:36:42 -070042 if (useTempData) {
halcanary385fe4d2015-08-26 13:07:48 -070043 data = new uint16_t[reps * patternSize];
bsalomoned0bcad2015-05-04 10:36:42 -070044 }
45 for (int i = 0; i < reps; ++i) {
46 int baseIdx = i * patternSize;
47 uint16_t baseVert = (uint16_t)(i * vertCount);
48 for (int j = 0; j < patternSize; ++j) {
49 data[baseIdx+j] = baseVert + pattern[j];
50 }
51 }
52 if (useTempData) {
53 if (!buffer->updateData(data, bufferSize)) {
54 buffer->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070055 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -070056 }
halcanary385fe4d2015-08-26 13:07:48 -070057 delete[] data;
bsalomoned0bcad2015-05-04 10:36:42 -070058 } else {
59 buffer->unmap();
60 }
61 this->assignUniqueKeyToResource(key, buffer);
62 return buffer;
63}
64
cdalton397536c2016-03-25 12:15:03 -070065const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
bsalomoned0bcad2015-05-04 10:36:42 -070066 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
67 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
68 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
69
70 return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
71}
72
bsalomon706f08f2015-05-22 07:35:58 -070073GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& stroke) {
74 SkASSERT(this->gpu()->pathRendering());
75 return this->gpu()->pathRendering()->createPath(path, stroke);
76}
77
78GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
79 const GrStrokeInfo& stroke) {
80 SkASSERT(this->gpu()->pathRendering());
81 return this->gpu()->pathRendering()->createPathRange(gen, stroke);
82}
83
reeda9322c22016-04-12 06:47:05 -070084GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf,
85 const SkScalerContextEffects& effects,
86 const SkDescriptor* desc,
bsalomon706f08f2015-05-22 07:35:58 -070087 const GrStrokeInfo& stroke) {
88
89 SkASSERT(this->gpu()->pathRendering());
reeda9322c22016-04-12 06:47:05 -070090 return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, stroke);
bsalomon706f08f2015-05-22 07:35:58 -070091}
92
cdaltone2e71c22016-04-07 18:13:29 -070093GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -070094 GrAccessPattern accessPattern, uint32_t flags,
95 const void* data) {
robertphillips1b8e1b52015-06-24 06:54:10 -070096 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -070098 }
99
cdalton397536c2016-03-25 12:15:03 -0700100 if (kDynamic_GrAccessPattern == accessPattern) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700101 // bin by pow2 with a reasonable min
102 static const uint32_t MIN_SIZE = 1 << 12;
103 size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size)));
104
105 GrScratchKey key;
cdaltone2e71c22016-04-07 18:13:29 -0700106 GrBuffer::ComputeScratchKeyForDynamicBuffer(size, intendedType, &key);
robertphillips1b8e1b52015-06-24 06:54:10 -0700107 uint32_t scratchFlags = 0;
cdalton397536c2016-03-25 12:15:03 -0700108 if (flags & kNoPendingIO_Flag) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700109 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
110 } else {
111 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
112 }
robertphillips6e83ac72015-08-13 05:19:14 -0700113 GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags);
cdalton1bf3e712016-04-19 10:00:02 -0700114 if (GrBuffer* buffer = static_cast<GrBuffer*>(resource)) {
115 if (data) {
116 buffer->updateData(data, size);
117 }
118 return buffer;
robertphillips1b8e1b52015-06-24 06:54:10 -0700119 }
120 }
cdalton1bf3e712016-04-19 10:00:02 -0700121 return this->gpu()->createBuffer(size, intendedType, accessPattern, data);
jvanverth17aa0472016-01-05 10:41:27 -0800122}
123
joshualittb356cbc2015-08-05 06:36:39 -0700124GrBatchAtlas* GrResourceProvider::createAtlas(GrPixelConfig config,
125 int width, int height,
126 int numPlotsX, int numPlotsY,
127 GrBatchAtlas::EvictionFunc func, void* data) {
128 GrSurfaceDesc desc;
129 desc.fFlags = kNone_GrSurfaceFlags;
130 desc.fWidth = width;
131 desc.fHeight = height;
132 desc.fConfig = config;
133
134 // We don't want to flush the context so we claim we're in the middle of flushing so as to
135 // guarantee we do not recieve a texture with pending IO
halcanary6950de62015-11-07 05:29:00 -0800136 // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156)
joshualittb356cbc2015-08-05 06:36:39 -0700137 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
138 GrTexture* texture = this->createApproxTexture(desc, kFlags);
139 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700140 return nullptr;
joshualittb356cbc2015-08-05 06:36:39 -0700141 }
jvanverthb0ec9832016-04-20 05:54:01 -0700142 GrBatchAtlas* atlas = new GrBatchAtlas(this, texture,
143 numPlotsX, numPlotsY);
joshualitt8377e802015-11-05 07:14:56 -0800144 atlas->registerEvictionCallback(func, data);
145 return atlas;
joshualittb356cbc2015-08-05 06:36:39 -0700146}
egdanielec00d942015-09-14 12:56:10 -0700147
148GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
149 SkASSERT(rt);
150 if (rt->renderTargetPriv().getStencilAttachment()) {
151 return rt->renderTargetPriv().getStencilAttachment();
152 }
153
154 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
155 GrUniqueKey sbKey;
156
157 int width = rt->width();
158 int height = rt->height();
159#if 0
160 if (this->caps()->oversizedStencilSupport()) {
161 width = SkNextPow2(width);
162 height = SkNextPow2(height);
163 }
164#endif
165 bool newStencil = false;
166 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
167 rt->numStencilSamples(), &sbKey);
168 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
169 this->findAndRefResourceByUniqueKey(sbKey));
170 if (!stencil) {
171 // Need to try and create a new stencil
172 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
173 if (stencil) {
174 stencil->resourcePriv().setUniqueKey(sbKey);
175 newStencil = true;
176 }
177 }
178 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
179 if (newStencil) {
180 // Right now we're clearing the stencil attachment here after it is
bsalomon7ea33f52015-11-22 14:51:00 -0800181 // attached to a RT for the first time. When we start matching
egdanielec00d942015-09-14 12:56:10 -0700182 // stencil buffers with smaller color targets this will no longer
183 // be correct because it won't be guaranteed to clear the entire
184 // sb.
185 // We used to clear down in the GL subclass using a special purpose
186 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
187 // FBO status.
188 this->gpu()->clearStencil(rt);
189 }
190 }
191 }
192 return rt->renderTargetPriv().getStencilAttachment();
193}
194
ericrkf7b8b8a2016-02-24 14:49:51 -0800195GrRenderTarget* GrResourceProvider::wrapBackendTextureAsRenderTarget(
kkinnunen49c4c222016-04-01 04:50:37 -0700196 const GrBackendTextureDesc& desc) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800197 if (this->isAbandoned()) {
198 return nullptr;
199 }
kkinnunen49c4c222016-04-01 04:50:37 -0700200 return this->gpu()->wrapBackendTextureAsRenderTarget(desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800201}