blob: 79146d09113c37170a75bd9e025b0fc76b193970 [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
10#include "GrGpu.h"
kkinnunencabe20c2015-06-01 01:37:26 -070011#include "GrIndexBuffer.h"
12#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#include "GrVertexBuffer.h"
19
20GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
21
joshualitt6d0872d2016-01-11 08:27:48 -080022GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner)
23 : INHERITED(gpu, cache, owner) {
bsalomoned0bcad2015-05-04 10:36:42 -070024 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
25 fQuadIndexBufferKey = gQuadIndexBufferKey;
26}
27
28const GrIndexBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern,
29 int patternSize,
30 int reps,
31 int vertCount,
32 const GrUniqueKey& key) {
33 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
34
bsalomoneae62002015-07-31 13:59:30 -070035 // This is typically used in GrBatchs, so we assume kNoPendingIO.
36 GrIndexBuffer* buffer = this->createIndexBuffer(bufferSize, kStatic_BufferUsage,
37 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -070038 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -070039 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -070040 }
41 uint16_t* data = (uint16_t*) buffer->map();
halcanary96fcdcc2015-08-27 07:41:13 -070042 bool useTempData = (nullptr == data);
bsalomoned0bcad2015-05-04 10:36:42 -070043 if (useTempData) {
halcanary385fe4d2015-08-26 13:07:48 -070044 data = new uint16_t[reps * patternSize];
bsalomoned0bcad2015-05-04 10:36:42 -070045 }
46 for (int i = 0; i < reps; ++i) {
47 int baseIdx = i * patternSize;
48 uint16_t baseVert = (uint16_t)(i * vertCount);
49 for (int j = 0; j < patternSize; ++j) {
50 data[baseIdx+j] = baseVert + pattern[j];
51 }
52 }
53 if (useTempData) {
54 if (!buffer->updateData(data, bufferSize)) {
55 buffer->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070056 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -070057 }
halcanary385fe4d2015-08-26 13:07:48 -070058 delete[] data;
bsalomoned0bcad2015-05-04 10:36:42 -070059 } else {
60 buffer->unmap();
61 }
62 this->assignUniqueKeyToResource(key, buffer);
63 return buffer;
64}
65
66const GrIndexBuffer* GrResourceProvider::createQuadIndexBuffer() {
67 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
68 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
69 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
70
71 return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
72}
73
bsalomon706f08f2015-05-22 07:35:58 -070074GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& stroke) {
75 SkASSERT(this->gpu()->pathRendering());
76 return this->gpu()->pathRendering()->createPath(path, stroke);
77}
78
79GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
80 const GrStrokeInfo& stroke) {
81 SkASSERT(this->gpu()->pathRendering());
82 return this->gpu()->pathRendering()->createPathRange(gen, stroke);
83}
84
85GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf, const SkDescriptor* desc,
86 const GrStrokeInfo& stroke) {
87
88 SkASSERT(this->gpu()->pathRendering());
89 return this->gpu()->pathRendering()->createGlyphs(tf, desc, stroke);
90}
91
bsalomoneae62002015-07-31 13:59:30 -070092GrIndexBuffer* GrResourceProvider::createIndexBuffer(size_t size, BufferUsage usage,
93 uint32_t flags) {
robertphillips1b8e1b52015-06-24 06:54:10 -070094 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -070095 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -070096 }
97
bsalomoneae62002015-07-31 13:59:30 -070098 bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag);
99 bool dynamic = kDynamic_BufferUsage == usage;
robertphillips1b8e1b52015-06-24 06:54:10 -0700100 if (dynamic) {
101 // 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;
bsalomoneae62002015-07-31 13:59:30 -0700106 GrIndexBuffer::ComputeScratchKey(size, true, &key);
robertphillips1b8e1b52015-06-24 06:54:10 -0700107 uint32_t scratchFlags = 0;
bsalomoneae62002015-07-31 13:59:30 -0700108 if (noPendingIO) {
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);
robertphillips1b8e1b52015-06-24 06:54:10 -0700114 if (resource) {
115 return static_cast<GrIndexBuffer*>(resource);
116 }
117 }
bsalomoneae62002015-07-31 13:59:30 -0700118 return this->gpu()->createIndexBuffer(size, dynamic);
robertphillips1b8e1b52015-06-24 06:54:10 -0700119}
120
bsalomoneae62002015-07-31 13:59:30 -0700121GrVertexBuffer* GrResourceProvider::createVertexBuffer(size_t size, BufferUsage usage,
122 uint32_t flags) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700123 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700124 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700125 }
126
bsalomoneae62002015-07-31 13:59:30 -0700127 bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag);
128 bool dynamic = kDynamic_BufferUsage == usage;
robertphillips1b8e1b52015-06-24 06:54:10 -0700129 if (dynamic) {
130 // bin by pow2 with a reasonable min
bsalomoneae62002015-07-31 13:59:30 -0700131 static const uint32_t MIN_SIZE = 1 << 12;
robertphillips1b8e1b52015-06-24 06:54:10 -0700132 size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size)));
133
134 GrScratchKey key;
bsalomoneae62002015-07-31 13:59:30 -0700135 GrVertexBuffer::ComputeScratchKey(size, true, &key);
robertphillips1b8e1b52015-06-24 06:54:10 -0700136 uint32_t scratchFlags = 0;
bsalomoneae62002015-07-31 13:59:30 -0700137 if (noPendingIO) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700138 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
139 } else {
140 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
141 }
robertphillips6e83ac72015-08-13 05:19:14 -0700142 GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags);
robertphillips1b8e1b52015-06-24 06:54:10 -0700143 if (resource) {
144 return static_cast<GrVertexBuffer*>(resource);
145 }
146 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700147 return this->gpu()->createVertexBuffer(size, dynamic);
148}
joshualittb356cbc2015-08-05 06:36:39 -0700149
jvanverth17aa0472016-01-05 10:41:27 -0800150GrTransferBuffer* GrResourceProvider::createTransferBuffer(size_t size, TransferType type,
151 uint32_t flags) {
152 if (this->isAbandoned()) {
153 return nullptr;
154 }
155
156 //bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag);
157 return this->gpu()->createTransferBuffer(size, type);
158}
159
joshualittb356cbc2015-08-05 06:36:39 -0700160GrBatchAtlas* GrResourceProvider::createAtlas(GrPixelConfig config,
161 int width, int height,
162 int numPlotsX, int numPlotsY,
163 GrBatchAtlas::EvictionFunc func, void* data) {
164 GrSurfaceDesc desc;
165 desc.fFlags = kNone_GrSurfaceFlags;
166 desc.fWidth = width;
167 desc.fHeight = height;
168 desc.fConfig = config;
169
170 // We don't want to flush the context so we claim we're in the middle of flushing so as to
171 // guarantee we do not recieve a texture with pending IO
halcanary6950de62015-11-07 05:29:00 -0800172 // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156)
joshualittb356cbc2015-08-05 06:36:39 -0700173 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
174 GrTexture* texture = this->createApproxTexture(desc, kFlags);
175 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700176 return nullptr;
joshualittb356cbc2015-08-05 06:36:39 -0700177 }
joshualitt8377e802015-11-05 07:14:56 -0800178 GrBatchAtlas* atlas = new GrBatchAtlas(texture, numPlotsX, numPlotsY);
179 atlas->registerEvictionCallback(func, data);
180 return atlas;
joshualittb356cbc2015-08-05 06:36:39 -0700181}
egdanielec00d942015-09-14 12:56:10 -0700182
183GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
184 SkASSERT(rt);
185 if (rt->renderTargetPriv().getStencilAttachment()) {
186 return rt->renderTargetPriv().getStencilAttachment();
187 }
188
189 if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) {
190 GrUniqueKey sbKey;
191
192 int width = rt->width();
193 int height = rt->height();
194#if 0
195 if (this->caps()->oversizedStencilSupport()) {
196 width = SkNextPow2(width);
197 height = SkNextPow2(height);
198 }
199#endif
200 bool newStencil = false;
201 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
202 rt->numStencilSamples(), &sbKey);
203 GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
204 this->findAndRefResourceByUniqueKey(sbKey));
205 if (!stencil) {
206 // Need to try and create a new stencil
207 stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
208 if (stencil) {
209 stencil->resourcePriv().setUniqueKey(sbKey);
210 newStencil = true;
211 }
212 }
213 if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
214 if (newStencil) {
215 // Right now we're clearing the stencil attachment here after it is
bsalomon7ea33f52015-11-22 14:51:00 -0800216 // attached to a RT for the first time. When we start matching
egdanielec00d942015-09-14 12:56:10 -0700217 // stencil buffers with smaller color targets this will no longer
218 // be correct because it won't be guaranteed to clear the entire
219 // sb.
220 // We used to clear down in the GL subclass using a special purpose
221 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
222 // FBO status.
223 this->gpu()->clearStencil(rt);
224 }
225 }
226 }
227 return rt->renderTargetPriv().getStencilAttachment();
228}
229
ericrkf7b8b8a2016-02-24 14:49:51 -0800230GrRenderTarget* GrResourceProvider::wrapBackendTextureAsRenderTarget(
231 const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
232 if (this->isAbandoned()) {
233 return nullptr;
234 }
235 return this->gpu()->wrapBackendTextureAsRenderTarget(desc, ownership);
236}
egdanielec00d942015-09-14 12:56:10 -0700237