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 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 10 | #include "GrBuffer.h" |
robertphillips | 5fa7f30 | 2016-07-21 09:21:04 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 12 | #include "GrGpu.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 13 | #include "GrPathRendering.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 14 | #include "GrRenderTarget.h" |
| 15 | #include "GrRenderTargetPriv.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 16 | #include "GrResourceCache.h" |
| 17 | #include "GrResourceKey.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 18 | #include "GrStencilAttachment.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 19 | #include "SkMathPriv.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 20 | |
| 21 | GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 22 | |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 23 | GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner) |
| 24 | : INHERITED(gpu, cache, owner) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 25 | GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 26 | fQuadIndexBufferKey = gQuadIndexBufferKey; |
| 27 | } |
| 28 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 29 | const GrBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern, |
| 30 | int patternSize, |
| 31 | int reps, |
| 32 | int vertCount, |
| 33 | const GrUniqueKey& key) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 34 | size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 35 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 36 | // This is typically used in GrBatchs, so we assume kNoPendingIO. |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 37 | GrBuffer* buffer = this->createBuffer(bufferSize, kIndex_GrBufferType, kStatic_GrAccessPattern, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 38 | kNoPendingIO_Flag); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 39 | if (!buffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 41 | } |
| 42 | uint16_t* data = (uint16_t*) buffer->map(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | bool useTempData = (nullptr == data); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 44 | if (useTempData) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 45 | data = new uint16_t[reps * patternSize]; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 46 | } |
| 47 | for (int i = 0; i < reps; ++i) { |
| 48 | int baseIdx = i * patternSize; |
| 49 | uint16_t baseVert = (uint16_t)(i * vertCount); |
| 50 | for (int j = 0; j < patternSize; ++j) { |
| 51 | data[baseIdx+j] = baseVert + pattern[j]; |
| 52 | } |
| 53 | } |
| 54 | if (useTempData) { |
| 55 | if (!buffer->updateData(data, bufferSize)) { |
| 56 | buffer->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 58 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 59 | delete[] data; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 60 | } else { |
| 61 | buffer->unmap(); |
| 62 | } |
| 63 | this->assignUniqueKeyToResource(key, buffer); |
| 64 | return buffer; |
| 65 | } |
| 66 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 67 | const GrBuffer* GrResourceProvider::createQuadIndexBuffer() { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 68 | static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1; |
| 69 | GR_STATIC_ASSERT(4 * kMaxQuads <= 65535); |
| 70 | static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 }; |
| 71 | |
| 72 | return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey); |
| 73 | } |
| 74 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 75 | GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStyle& style) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 76 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 77 | return this->gpu()->pathRendering()->createPath(path, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 81 | const GrStyle& style) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 82 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 83 | return this->gpu()->pathRendering()->createPathRange(gen, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 84 | } |
| 85 | |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 86 | GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf, |
| 87 | const SkScalerContextEffects& effects, |
| 88 | const SkDescriptor* desc, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 89 | const GrStyle& style) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 90 | |
| 91 | SkASSERT(this->gpu()->pathRendering()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 92 | return this->gpu()->pathRendering()->createGlyphs(tf, effects, desc, style); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 93 | } |
| 94 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 95 | GrBuffer* GrResourceProvider::createBuffer(size_t size, GrBufferType intendedType, |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 96 | GrAccessPattern accessPattern, uint32_t flags, |
| 97 | const void* data) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 98 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 100 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 101 | if (kDynamic_GrAccessPattern != accessPattern) { |
| 102 | return this->gpu()->createBuffer(size, intendedType, accessPattern, data); |
| 103 | } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 104 | if (!(flags & kRequireGpuMemory_Flag) && |
| 105 | this->gpu()->caps()->preferClientSideDynamicBuffers() && |
| 106 | GrBufferTypeIsVertexOrIndex(intendedType) && |
| 107 | kDynamic_GrAccessPattern == accessPattern) { |
| 108 | return GrBuffer::CreateCPUBacked(this->gpu(), size, intendedType, data); |
| 109 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 110 | |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 111 | // bin by pow2 with a reasonable min |
Robert Phillips | 9e38047 | 2016-10-28 12:15:03 -0400 | [diff] [blame^] | 112 | static const size_t MIN_SIZE = 1 << 12; |
| 113 | size_t allocSize = SkTMax(MIN_SIZE, GrNextSizePow2(size)); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 114 | |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 115 | GrScratchKey key; |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 116 | GrBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key); |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 117 | uint32_t scratchFlags = 0; |
| 118 | if (flags & kNoPendingIO_Flag) { |
| 119 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 120 | } else { |
| 121 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 122 | } |
| 123 | GrBuffer* buffer = static_cast<GrBuffer*>( |
| 124 | this->cache()->findAndRefScratchResource(key, allocSize, scratchFlags)); |
| 125 | if (!buffer) { |
| 126 | buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern); |
| 127 | if (!buffer) { |
| 128 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 131 | if (data) { |
| 132 | buffer->updateData(data, size); |
| 133 | } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 134 | SkASSERT(!buffer->isCPUBacked()); // We should only cache real VBOs. |
cdalton | d37fe76 | 2016-04-21 07:41:50 -0700 | [diff] [blame] | 135 | return buffer; |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 136 | } |
| 137 | |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 138 | GrBatchAtlas* GrResourceProvider::createAtlas(GrPixelConfig config, |
| 139 | int width, int height, |
| 140 | int numPlotsX, int numPlotsY, |
| 141 | GrBatchAtlas::EvictionFunc func, void* data) { |
| 142 | GrSurfaceDesc desc; |
| 143 | desc.fFlags = kNone_GrSurfaceFlags; |
| 144 | desc.fWidth = width; |
| 145 | desc.fHeight = height; |
| 146 | desc.fConfig = config; |
| 147 | |
| 148 | // We don't want to flush the context so we claim we're in the middle of flushing so as to |
| 149 | // guarantee we do not recieve a texture with pending IO |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 150 | // 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] | 151 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
| 152 | GrTexture* texture = this->createApproxTexture(desc, kFlags); |
| 153 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 154 | return nullptr; |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 155 | } |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 156 | GrBatchAtlas* atlas = new GrBatchAtlas(texture, numPlotsX, numPlotsY); |
joshualitt | 8377e80 | 2015-11-05 07:14:56 -0800 | [diff] [blame] | 157 | atlas->registerEvictionCallback(func, data); |
| 158 | return atlas; |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 159 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 160 | |
| 161 | GrStencilAttachment* GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) { |
| 162 | SkASSERT(rt); |
| 163 | if (rt->renderTargetPriv().getStencilAttachment()) { |
| 164 | return rt->renderTargetPriv().getStencilAttachment(); |
| 165 | } |
| 166 | |
| 167 | if (!rt->wasDestroyed() && rt->canAttemptStencilAttachment()) { |
| 168 | GrUniqueKey sbKey; |
| 169 | |
| 170 | int width = rt->width(); |
| 171 | int height = rt->height(); |
| 172 | #if 0 |
| 173 | if (this->caps()->oversizedStencilSupport()) { |
| 174 | width = SkNextPow2(width); |
| 175 | height = SkNextPow2(height); |
| 176 | } |
| 177 | #endif |
| 178 | bool newStencil = false; |
| 179 | GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height, |
| 180 | rt->numStencilSamples(), &sbKey); |
| 181 | GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>( |
| 182 | this->findAndRefResourceByUniqueKey(sbKey)); |
| 183 | if (!stencil) { |
| 184 | // Need to try and create a new stencil |
| 185 | stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height); |
| 186 | if (stencil) { |
| 187 | stencil->resourcePriv().setUniqueKey(sbKey); |
| 188 | newStencil = true; |
| 189 | } |
| 190 | } |
| 191 | if (rt->renderTargetPriv().attachStencilAttachment(stencil)) { |
| 192 | if (newStencil) { |
| 193 | // Right now we're clearing the stencil attachment here after it is |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 194 | // attached to a RT for the first time. When we start matching |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 195 | // stencil buffers with smaller color targets this will no longer |
| 196 | // be correct because it won't be guaranteed to clear the entire |
| 197 | // sb. |
| 198 | // We used to clear down in the GL subclass using a special purpose |
| 199 | // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported |
| 200 | // FBO status. |
| 201 | this->gpu()->clearStencil(rt); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | return rt->renderTargetPriv().getStencilAttachment(); |
| 206 | } |
| 207 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 208 | sk_sp<GrRenderTarget> GrResourceProvider::wrapBackendTextureAsRenderTarget( |
| 209 | const GrBackendTextureDesc& desc) |
| 210 | { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 211 | if (this->isAbandoned()) { |
| 212 | return nullptr; |
| 213 | } |
kkinnunen | 49c4c22 | 2016-04-01 04:50:37 -0700 | [diff] [blame] | 214 | return this->gpu()->wrapBackendTextureAsRenderTarget(desc); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 215 | } |