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) { |
| 35 | return NULL; |
| 36 | } |
| 37 | uint16_t* data = (uint16_t*) buffer->map(); |
| 38 | bool useTempData = (NULL == data); |
| 39 | if (useTempData) { |
| 40 | data = SkNEW_ARRAY(uint16_t, reps * patternSize); |
| 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(); |
| 52 | return NULL; |
| 53 | } |
| 54 | SkDELETE_ARRAY(data); |
| 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()) { |
| 91 | return NULL; |
| 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 | } |
| 109 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, scratchFlags); |
| 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()) { |
| 120 | return NULL; |
| 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 | } |
| 138 | GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, scratchFlags); |
| 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 | } |