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 | |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 22 | GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner) |
| 23 | : INHERITED(gpu, cache, owner) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 24 | GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 25 | fQuadIndexBufferKey = gQuadIndexBufferKey; |
| 26 | } |
| 27 | |
| 28 | const 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 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 35 | // This is typically used in GrBatchs, so we assume kNoPendingIO. |
| 36 | GrIndexBuffer* buffer = this->createIndexBuffer(bufferSize, kStatic_BufferUsage, |
| 37 | kNoPendingIO_Flag); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 38 | if (!buffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 40 | } |
| 41 | uint16_t* data = (uint16_t*) buffer->map(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | bool useTempData = (nullptr == data); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 43 | if (useTempData) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 44 | data = new uint16_t[reps * patternSize]; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 45 | } |
| 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(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 57 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 58 | delete[] data; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 59 | } else { |
| 60 | buffer->unmap(); |
| 61 | } |
| 62 | this->assignUniqueKeyToResource(key, buffer); |
| 63 | return buffer; |
| 64 | } |
| 65 | |
| 66 | const 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 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 74 | GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& stroke) { |
| 75 | SkASSERT(this->gpu()->pathRendering()); |
| 76 | return this->gpu()->pathRendering()->createPath(path, stroke); |
| 77 | } |
| 78 | |
| 79 | GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen, |
| 80 | const GrStrokeInfo& stroke) { |
| 81 | SkASSERT(this->gpu()->pathRendering()); |
| 82 | return this->gpu()->pathRendering()->createPathRange(gen, stroke); |
| 83 | } |
| 84 | |
| 85 | GrPathRange* 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 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 92 | GrIndexBuffer* GrResourceProvider::createIndexBuffer(size_t size, BufferUsage usage, |
| 93 | uint32_t flags) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 94 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 96 | } |
| 97 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 98 | bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); |
| 99 | bool dynamic = kDynamic_BufferUsage == usage; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 100 | 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; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 106 | GrIndexBuffer::ComputeScratchKey(size, true, &key); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 107 | uint32_t scratchFlags = 0; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 108 | if (noPendingIO) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 109 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 110 | } else { |
| 111 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 112 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 113 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 114 | if (resource) { |
| 115 | return static_cast<GrIndexBuffer*>(resource); |
| 116 | } |
| 117 | } |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 118 | return this->gpu()->createIndexBuffer(size, dynamic); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 119 | } |
| 120 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 121 | GrVertexBuffer* GrResourceProvider::createVertexBuffer(size_t size, BufferUsage usage, |
| 122 | uint32_t flags) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 123 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 125 | } |
| 126 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 127 | bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); |
| 128 | bool dynamic = kDynamic_BufferUsage == usage; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 129 | if (dynamic) { |
| 130 | // bin by pow2 with a reasonable min |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 131 | static const uint32_t MIN_SIZE = 1 << 12; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 132 | size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); |
| 133 | |
| 134 | GrScratchKey key; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 135 | GrVertexBuffer::ComputeScratchKey(size, true, &key); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 136 | uint32_t scratchFlags = 0; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 137 | if (noPendingIO) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 138 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 139 | } else { |
| 140 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 141 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 142 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 143 | if (resource) { |
| 144 | return static_cast<GrVertexBuffer*>(resource); |
| 145 | } |
| 146 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 147 | return this->gpu()->createVertexBuffer(size, dynamic); |
| 148 | } |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 149 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 150 | GrTransferBuffer* 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 | |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 160 | GrBatchAtlas* 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 |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 172 | // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156) |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 173 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
| 174 | GrTexture* texture = this->createApproxTexture(desc, kFlags); |
| 175 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 176 | return nullptr; |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 177 | } |
joshualitt | 8377e80 | 2015-11-05 07:14:56 -0800 | [diff] [blame] | 178 | GrBatchAtlas* atlas = new GrBatchAtlas(texture, numPlotsX, numPlotsY); |
| 179 | atlas->registerEvictionCallback(func, data); |
| 180 | return atlas; |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 181 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 182 | |
| 183 | GrStencilAttachment* 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 |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 216 | // attached to a RT for the first time. When we start matching |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 217 | // 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 | |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame^] | 230 | GrRenderTarget* GrResourceProvider::wrapBackendTextureAsRenderTarget( |
| 231 | const GrBackendTextureDesc& desc, GrWrapOwnership ownership) { |
| 232 | if (this->isAbandoned()) { |
| 233 | return nullptr; |
| 234 | } |
| 235 | return this->gpu()->wrapBackendTextureAsRenderTarget(desc, ownership); |
| 236 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 237 | |