bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1 | /* |
| 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" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 11 | #include "GrIndexBuffer.h" |
| 12 | #include "GrPathRendering.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame^] | 13 | #include "GrRenderTarget.h" |
| 14 | #include "GrRenderTargetPriv.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 15 | #include "GrResourceCache.h" |
| 16 | #include "GrResourceKey.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame^] | 17 | #include "GrStencilAttachment.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 18 | #include "GrVertexBuffer.h" |
| 19 | |
| 20 | GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 21 | |
| 22 | GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache) : INHERITED(gpu, cache) { |
| 23 | GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 24 | fQuadIndexBufferKey = gQuadIndexBufferKey; |
| 25 | } |
| 26 | |
| 27 | const GrIndexBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern, |
| 28 | int patternSize, |
| 29 | int reps, |
| 30 | int vertCount, |
| 31 | const GrUniqueKey& key) { |
| 32 | size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 33 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 34 | // This is typically used in GrBatchs, so we assume kNoPendingIO. |
| 35 | GrIndexBuffer* buffer = this->createIndexBuffer(bufferSize, kStatic_BufferUsage, |
| 36 | kNoPendingIO_Flag); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 37 | if (!buffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 39 | } |
| 40 | uint16_t* data = (uint16_t*) buffer->map(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 41 | bool useTempData = (nullptr == data); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 42 | if (useTempData) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 43 | data = new uint16_t[reps * patternSize]; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 44 | } |
| 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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 56 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 57 | delete[] data; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 58 | } else { |
| 59 | buffer->unmap(); |
| 60 | } |
| 61 | this->assignUniqueKeyToResource(key, buffer); |
| 62 | return buffer; |
| 63 | } |
| 64 | |
| 65 | const GrIndexBuffer* GrResourceProvider::createQuadIndexBuffer() { |
| 66 | 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 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 73 | GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& stroke) { |
| 74 | SkASSERT(this->gpu()->pathRendering()); |
| 75 | return this->gpu()->pathRendering()->createPath(path, stroke); |
| 76 | } |
| 77 | |
| 78 | GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen, |
| 79 | const GrStrokeInfo& stroke) { |
| 80 | SkASSERT(this->gpu()->pathRendering()); |
| 81 | return this->gpu()->pathRendering()->createPathRange(gen, stroke); |
| 82 | } |
| 83 | |
| 84 | GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf, const SkDescriptor* desc, |
| 85 | const GrStrokeInfo& stroke) { |
| 86 | |
| 87 | SkASSERT(this->gpu()->pathRendering()); |
| 88 | return this->gpu()->pathRendering()->createGlyphs(tf, desc, stroke); |
| 89 | } |
| 90 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 91 | GrIndexBuffer* GrResourceProvider::createIndexBuffer(size_t size, BufferUsage usage, |
| 92 | uint32_t flags) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 93 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 94 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 95 | } |
| 96 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 97 | bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); |
| 98 | bool dynamic = kDynamic_BufferUsage == usage; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 99 | if (dynamic) { |
| 100 | // bin by pow2 with a reasonable min |
| 101 | static const uint32_t MIN_SIZE = 1 << 12; |
| 102 | size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); |
| 103 | |
| 104 | GrScratchKey key; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 105 | GrIndexBuffer::ComputeScratchKey(size, true, &key); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 106 | uint32_t scratchFlags = 0; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 107 | if (noPendingIO) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 108 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 109 | } else { |
| 110 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 111 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 112 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 113 | if (resource) { |
| 114 | return static_cast<GrIndexBuffer*>(resource); |
| 115 | } |
| 116 | } |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 117 | return this->gpu()->createIndexBuffer(size, dynamic); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 120 | GrVertexBuffer* GrResourceProvider::createVertexBuffer(size_t size, BufferUsage usage, |
| 121 | uint32_t flags) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 122 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 126 | bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); |
| 127 | bool dynamic = kDynamic_BufferUsage == usage; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 128 | if (dynamic) { |
| 129 | // bin by pow2 with a reasonable min |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 130 | static const uint32_t MIN_SIZE = 1 << 12; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 131 | size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); |
| 132 | |
| 133 | GrScratchKey key; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 134 | GrVertexBuffer::ComputeScratchKey(size, true, &key); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 135 | uint32_t scratchFlags = 0; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 136 | if (noPendingIO) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 137 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 138 | } else { |
| 139 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 140 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 141 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 142 | if (resource) { |
| 143 | return static_cast<GrVertexBuffer*>(resource); |
| 144 | } |
| 145 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 146 | return this->gpu()->createVertexBuffer(size, dynamic); |
| 147 | } |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 148 | |
| 149 | GrBatchAtlas* GrResourceProvider::createAtlas(GrPixelConfig config, |
| 150 | int width, int height, |
| 151 | int numPlotsX, int numPlotsY, |
| 152 | GrBatchAtlas::EvictionFunc func, void* data) { |
| 153 | GrSurfaceDesc desc; |
| 154 | desc.fFlags = kNone_GrSurfaceFlags; |
| 155 | desc.fWidth = width; |
| 156 | desc.fHeight = height; |
| 157 | desc.fConfig = config; |
| 158 | |
| 159 | // We don't want to flush the context so we claim we're in the middle of flushing so as to |
| 160 | // guarantee we do not recieve a texture with pending IO |
| 161 | // TODO: Determine how to avoid having to do this. (http://skbug.com/4156) |
| 162 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
| 163 | GrTexture* texture = this->createApproxTexture(desc, kFlags); |
| 164 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 165 | return nullptr; |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 166 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 167 | return new GrBatchAtlas(texture, numPlotsX, numPlotsY); |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 168 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame^] | 169 | |
| 170 | GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) { |
| 171 | SkASSERT(rt); |
| 172 | if (rt->renderTargetPriv().getStencilAttachment()) { |
| 173 | return rt->renderTargetPriv().getStencilAttachment(); |
| 174 | } |
| 175 | |
| 176 | if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) { |
| 177 | GrUniqueKey sbKey; |
| 178 | |
| 179 | int width = rt->width(); |
| 180 | int height = rt->height(); |
| 181 | #if 0 |
| 182 | if (this->caps()->oversizedStencilSupport()) { |
| 183 | width = SkNextPow2(width); |
| 184 | height = SkNextPow2(height); |
| 185 | } |
| 186 | #endif |
| 187 | bool newStencil = false; |
| 188 | GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height, |
| 189 | rt->numStencilSamples(), &sbKey); |
| 190 | GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>( |
| 191 | this->findAndRefResourceByUniqueKey(sbKey)); |
| 192 | if (!stencil) { |
| 193 | // Need to try and create a new stencil |
| 194 | stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height); |
| 195 | if (stencil) { |
| 196 | stencil->resourcePriv().setUniqueKey(sbKey); |
| 197 | newStencil = true; |
| 198 | } |
| 199 | } |
| 200 | if (rt->renderTargetPriv().attachStencilAttachment(stencil)) { |
| 201 | if (newStencil) { |
| 202 | // Right now we're clearing the stencil attachment here after it is |
| 203 | // attached to an RT for the first time. When we start matching |
| 204 | // stencil buffers with smaller color targets this will no longer |
| 205 | // be correct because it won't be guaranteed to clear the entire |
| 206 | // sb. |
| 207 | // We used to clear down in the GL subclass using a special purpose |
| 208 | // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported |
| 209 | // FBO status. |
| 210 | this->gpu()->clearStencil(rt); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | return rt->renderTargetPriv().getStencilAttachment(); |
| 215 | } |
| 216 | |
| 217 | |