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" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 13 | #include "GrResourceCache.h" |
| 14 | #include "GrResourceKey.h" |
| 15 | #include "GrVertexBuffer.h" |
| 16 | |
| 17 | GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 18 | |
| 19 | GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache) : INHERITED(gpu, cache) { |
| 20 | GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 21 | fQuadIndexBufferKey = gQuadIndexBufferKey; |
| 22 | } |
| 23 | |
| 24 | const GrIndexBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern, |
| 25 | int patternSize, |
| 26 | int reps, |
| 27 | int vertCount, |
| 28 | const GrUniqueKey& key) { |
| 29 | size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 30 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 31 | // This is typically used in GrBatchs, so we assume kNoPendingIO. |
| 32 | GrIndexBuffer* buffer = this->createIndexBuffer(bufferSize, kStatic_BufferUsage, |
| 33 | kNoPendingIO_Flag); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 34 | if (!buffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 35 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 36 | } |
| 37 | uint16_t* data = (uint16_t*) buffer->map(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 38 | bool useTempData = (nullptr == data); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 39 | if (useTempData) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 40 | data = new uint16_t[reps * patternSize]; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 41 | } |
| 42 | for (int i = 0; i < reps; ++i) { |
| 43 | int baseIdx = i * patternSize; |
| 44 | uint16_t baseVert = (uint16_t)(i * vertCount); |
| 45 | for (int j = 0; j < patternSize; ++j) { |
| 46 | data[baseIdx+j] = baseVert + pattern[j]; |
| 47 | } |
| 48 | } |
| 49 | if (useTempData) { |
| 50 | if (!buffer->updateData(data, bufferSize)) { |
| 51 | buffer->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 52 | return nullptr; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 53 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 54 | delete[] data; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 55 | } else { |
| 56 | buffer->unmap(); |
| 57 | } |
| 58 | this->assignUniqueKeyToResource(key, buffer); |
| 59 | return buffer; |
| 60 | } |
| 61 | |
| 62 | const GrIndexBuffer* GrResourceProvider::createQuadIndexBuffer() { |
| 63 | static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1; |
| 64 | GR_STATIC_ASSERT(4 * kMaxQuads <= 65535); |
| 65 | static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 }; |
| 66 | |
| 67 | return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey); |
| 68 | } |
| 69 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 70 | GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& stroke) { |
| 71 | SkASSERT(this->gpu()->pathRendering()); |
| 72 | return this->gpu()->pathRendering()->createPath(path, stroke); |
| 73 | } |
| 74 | |
| 75 | GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen, |
| 76 | const GrStrokeInfo& stroke) { |
| 77 | SkASSERT(this->gpu()->pathRendering()); |
| 78 | return this->gpu()->pathRendering()->createPathRange(gen, stroke); |
| 79 | } |
| 80 | |
| 81 | GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf, const SkDescriptor* desc, |
| 82 | const GrStrokeInfo& stroke) { |
| 83 | |
| 84 | SkASSERT(this->gpu()->pathRendering()); |
| 85 | return this->gpu()->pathRendering()->createGlyphs(tf, desc, stroke); |
| 86 | } |
| 87 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 88 | GrIndexBuffer* GrResourceProvider::createIndexBuffer(size_t size, BufferUsage usage, |
| 89 | uint32_t flags) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 90 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 91 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 94 | bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); |
| 95 | bool dynamic = kDynamic_BufferUsage == usage; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 96 | if (dynamic) { |
| 97 | // bin by pow2 with a reasonable min |
| 98 | static const uint32_t MIN_SIZE = 1 << 12; |
| 99 | size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); |
| 100 | |
| 101 | GrScratchKey key; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 102 | GrIndexBuffer::ComputeScratchKey(size, true, &key); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 103 | uint32_t scratchFlags = 0; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 104 | if (noPendingIO) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 105 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 106 | } else { |
| 107 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 108 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 109 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 110 | if (resource) { |
| 111 | return static_cast<GrIndexBuffer*>(resource); |
| 112 | } |
| 113 | } |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 114 | return this->gpu()->createIndexBuffer(size, dynamic); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 115 | } |
| 116 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 117 | GrVertexBuffer* GrResourceProvider::createVertexBuffer(size_t size, BufferUsage usage, |
| 118 | uint32_t flags) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 119 | if (this->isAbandoned()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 120 | return nullptr; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 121 | } |
| 122 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 123 | bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); |
| 124 | bool dynamic = kDynamic_BufferUsage == usage; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 125 | if (dynamic) { |
| 126 | // bin by pow2 with a reasonable min |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 127 | static const uint32_t MIN_SIZE = 1 << 12; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 128 | size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); |
| 129 | |
| 130 | GrScratchKey key; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 131 | GrVertexBuffer::ComputeScratchKey(size, true, &key); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 132 | uint32_t scratchFlags = 0; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 133 | if (noPendingIO) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 134 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 135 | } else { |
| 136 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 137 | } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 138 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 139 | if (resource) { |
| 140 | return static_cast<GrVertexBuffer*>(resource); |
| 141 | } |
| 142 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 143 | return this->gpu()->createVertexBuffer(size, dynamic); |
| 144 | } |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 145 | |
| 146 | GrBatchAtlas* GrResourceProvider::createAtlas(GrPixelConfig config, |
| 147 | int width, int height, |
| 148 | int numPlotsX, int numPlotsY, |
| 149 | GrBatchAtlas::EvictionFunc func, void* data) { |
| 150 | GrSurfaceDesc desc; |
| 151 | desc.fFlags = kNone_GrSurfaceFlags; |
| 152 | desc.fWidth = width; |
| 153 | desc.fHeight = height; |
| 154 | desc.fConfig = config; |
| 155 | |
| 156 | // We don't want to flush the context so we claim we're in the middle of flushing so as to |
| 157 | // guarantee we do not recieve a texture with pending IO |
| 158 | // TODO: Determine how to avoid having to do this. (http://skbug.com/4156) |
| 159 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
| 160 | GrTexture* texture = this->createApproxTexture(desc, kFlags); |
| 161 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 162 | return nullptr; |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 163 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 164 | return new GrBatchAtlas(texture, numPlotsX, numPlotsY); |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 165 | } |