| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrResourceProvider.h" | 8 #include "GrResourceProvider.h" |
| 9 | 9 |
| 10 #include "GrBuffer.h" |
| 10 #include "GrGpu.h" | 11 #include "GrGpu.h" |
| 11 #include "GrIndexBuffer.h" | |
| 12 #include "GrPathRendering.h" | 12 #include "GrPathRendering.h" |
| 13 #include "GrRenderTarget.h" | 13 #include "GrRenderTarget.h" |
| 14 #include "GrRenderTargetPriv.h" | 14 #include "GrRenderTargetPriv.h" |
| 15 #include "GrResourceCache.h" | 15 #include "GrResourceCache.h" |
| 16 #include "GrResourceKey.h" | 16 #include "GrResourceKey.h" |
| 17 #include "GrStencilAttachment.h" | 17 #include "GrStencilAttachment.h" |
| 18 #include "GrVertexBuffer.h" | |
| 19 | 18 |
| 20 GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); | 19 GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 21 | 20 |
| 22 GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSin
gleOwner* owner) | 21 GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSin
gleOwner* owner) |
| 23 : INHERITED(gpu, cache, owner) { | 22 : INHERITED(gpu, cache, owner) { |
| 24 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); | 23 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); |
| 25 fQuadIndexBufferKey = gQuadIndexBufferKey; | 24 fQuadIndexBufferKey = gQuadIndexBufferKey; |
| 26 } | 25 } |
| 27 | 26 |
| 28 const GrIndexBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16
_t* pattern, | 27 const GrBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* p
attern, |
| 29 int patternS
ize, | 28 int patternSize, |
| 30 int reps, | 29 int reps, |
| 31 int vertCoun
t, | 30 int vertCount, |
| 32 const GrUniq
ueKey& key) { | 31 const GrUniqueKey
& key) { |
| 33 size_t bufferSize = patternSize * reps * sizeof(uint16_t); | 32 size_t bufferSize = patternSize * reps * sizeof(uint16_t); |
| 34 | 33 |
| 35 // This is typically used in GrBatchs, so we assume kNoPendingIO. | 34 // This is typically used in GrBatchs, so we assume kNoPendingIO. |
| 36 GrIndexBuffer* buffer = this->createIndexBuffer(bufferSize, kStatic_BufferUs
age, | 35 GrBuffer* buffer = this->createBuffer(kIndex_GrBufferType, bufferSize, kStat
ic_GrAccessPattern, |
| 37 kNoPendingIO_Flag); | 36 kNoPendingIO_Flag); |
| 38 if (!buffer) { | 37 if (!buffer) { |
| 39 return nullptr; | 38 return nullptr; |
| 40 } | 39 } |
| 41 uint16_t* data = (uint16_t*) buffer->map(); | 40 uint16_t* data = (uint16_t*) buffer->map(); |
| 42 bool useTempData = (nullptr == data); | 41 bool useTempData = (nullptr == data); |
| 43 if (useTempData) { | 42 if (useTempData) { |
| 44 data = new uint16_t[reps * patternSize]; | 43 data = new uint16_t[reps * patternSize]; |
| 45 } | 44 } |
| 46 for (int i = 0; i < reps; ++i) { | 45 for (int i = 0; i < reps; ++i) { |
| 47 int baseIdx = i * patternSize; | 46 int baseIdx = i * patternSize; |
| 48 uint16_t baseVert = (uint16_t)(i * vertCount); | 47 uint16_t baseVert = (uint16_t)(i * vertCount); |
| 49 for (int j = 0; j < patternSize; ++j) { | 48 for (int j = 0; j < patternSize; ++j) { |
| 50 data[baseIdx+j] = baseVert + pattern[j]; | 49 data[baseIdx+j] = baseVert + pattern[j]; |
| 51 } | 50 } |
| 52 } | 51 } |
| 53 if (useTempData) { | 52 if (useTempData) { |
| 54 if (!buffer->updateData(data, bufferSize)) { | 53 if (!buffer->updateData(data, bufferSize)) { |
| 55 buffer->unref(); | 54 buffer->unref(); |
| 56 return nullptr; | 55 return nullptr; |
| 57 } | 56 } |
| 58 delete[] data; | 57 delete[] data; |
| 59 } else { | 58 } else { |
| 60 buffer->unmap(); | 59 buffer->unmap(); |
| 61 } | 60 } |
| 62 this->assignUniqueKeyToResource(key, buffer); | 61 this->assignUniqueKeyToResource(key, buffer); |
| 63 return buffer; | 62 return buffer; |
| 64 } | 63 } |
| 65 | 64 |
| 66 const GrIndexBuffer* GrResourceProvider::createQuadIndexBuffer() { | 65 const GrBuffer* GrResourceProvider::createQuadIndexBuffer() { |
| 67 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1; | 66 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1; |
| 68 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535); | 67 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535); |
| 69 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 }; | 68 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 }; |
| 70 | 69 |
| 71 return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadInde
xBufferKey); | 70 return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadInde
xBufferKey); |
| 72 } | 71 } |
| 73 | 72 |
| 74 GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& s
troke) { | 73 GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& s
troke) { |
| 75 SkASSERT(this->gpu()->pathRendering()); | 74 SkASSERT(this->gpu()->pathRendering()); |
| 76 return this->gpu()->pathRendering()->createPath(path, stroke); | 75 return this->gpu()->pathRendering()->createPath(path, stroke); |
| 77 } | 76 } |
| 78 | 77 |
| 79 GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen
, | 78 GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen
, |
| 80 const GrStrokeInfo& stroke) { | 79 const GrStrokeInfo& stroke) { |
| 81 SkASSERT(this->gpu()->pathRendering()); | 80 SkASSERT(this->gpu()->pathRendering()); |
| 82 return this->gpu()->pathRendering()->createPathRange(gen, stroke); | 81 return this->gpu()->pathRendering()->createPathRange(gen, stroke); |
| 83 } | 82 } |
| 84 | 83 |
| 85 GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf, const SkDesc
riptor* desc, | 84 GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf, const SkDesc
riptor* desc, |
| 86 const GrStrokeInfo& stroke) { | 85 const GrStrokeInfo& stroke) { |
| 87 | 86 |
| 88 SkASSERT(this->gpu()->pathRendering()); | 87 SkASSERT(this->gpu()->pathRendering()); |
| 89 return this->gpu()->pathRendering()->createGlyphs(tf, desc, stroke); | 88 return this->gpu()->pathRendering()->createGlyphs(tf, desc, stroke); |
| 90 } | 89 } |
| 91 | 90 |
| 92 GrIndexBuffer* GrResourceProvider::createIndexBuffer(size_t size, BufferUsage us
age, | 91 GrBuffer* GrResourceProvider::createBuffer(GrBufferType type, size_t size, |
| 93 uint32_t flags) { | 92 GrAccessPattern accessPattern, uint32
_t flags) { |
| 94 if (this->isAbandoned()) { | 93 if (this->isAbandoned()) { |
| 95 return nullptr; | 94 return nullptr; |
| 96 } | 95 } |
| 97 | 96 |
| 98 bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); | 97 if (kDynamic_GrAccessPattern == accessPattern) { |
| 99 bool dynamic = kDynamic_BufferUsage == usage; | |
| 100 if (dynamic) { | |
| 101 // bin by pow2 with a reasonable min | 98 // bin by pow2 with a reasonable min |
| 102 static const uint32_t MIN_SIZE = 1 << 12; | 99 static const uint32_t MIN_SIZE = 1 << 12; |
| 103 size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); | 100 size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); |
| 104 | 101 |
| 105 GrScratchKey key; | 102 GrScratchKey key; |
| 106 GrIndexBuffer::ComputeScratchKey(size, true, &key); | 103 GrBuffer::ComputeScratchKeyForDynamicBuffer(type, size, &key); |
| 107 uint32_t scratchFlags = 0; | 104 uint32_t scratchFlags = 0; |
| 108 if (noPendingIO) { | 105 if (flags & kNoPendingIO_Flag) { |
| 109 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; | 106 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 110 } else { | 107 } else { |
| 111 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; | 108 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 112 } | 109 } |
| 113 GrGpuResource* resource = this->cache()->findAndRefScratchResource(key,
size, scratchFlags); | 110 GrGpuResource* resource = this->cache()->findAndRefScratchResource(key,
size, scratchFlags); |
| 114 if (resource) { | 111 if (resource) { |
| 115 return static_cast<GrIndexBuffer*>(resource); | 112 return static_cast<GrBuffer*>(resource); |
| 116 } | 113 } |
| 117 } | 114 } |
| 118 return this->gpu()->createIndexBuffer(size, dynamic); | 115 return this->gpu()->createBuffer(type, size, accessPattern); |
| 119 } | |
| 120 | |
| 121 GrVertexBuffer* GrResourceProvider::createVertexBuffer(size_t size, BufferUsage
usage, | |
| 122 uint32_t flags) { | |
| 123 if (this->isAbandoned()) { | |
| 124 return nullptr; | |
| 125 } | |
| 126 | |
| 127 bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag); | |
| 128 bool dynamic = kDynamic_BufferUsage == usage; | |
| 129 if (dynamic) { | |
| 130 // bin by pow2 with a reasonable min | |
| 131 static const uint32_t MIN_SIZE = 1 << 12; | |
| 132 size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size))); | |
| 133 | |
| 134 GrScratchKey key; | |
| 135 GrVertexBuffer::ComputeScratchKey(size, true, &key); | |
| 136 uint32_t scratchFlags = 0; | |
| 137 if (noPendingIO) { | |
| 138 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; | |
| 139 } else { | |
| 140 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; | |
| 141 } | |
| 142 GrGpuResource* resource = this->cache()->findAndRefScratchResource(key,
size, scratchFlags); | |
| 143 if (resource) { | |
| 144 return static_cast<GrVertexBuffer*>(resource); | |
| 145 } | |
| 146 } | |
| 147 return this->gpu()->createVertexBuffer(size, dynamic); | |
| 148 } | |
| 149 | |
| 150 GrTransferBuffer* GrResourceProvider::createTransferBuffer(size_t size, Transfer
Type 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 } | 116 } |
| 159 | 117 |
| 160 GrBatchAtlas* GrResourceProvider::createAtlas(GrPixelConfig config, | 118 GrBatchAtlas* GrResourceProvider::createAtlas(GrPixelConfig config, |
| 161 int width, int height, | 119 int width, int height, |
| 162 int numPlotsX, int numPlotsY, | 120 int numPlotsX, int numPlotsY, |
| 163 GrBatchAtlas::EvictionFunc func, v
oid* data) { | 121 GrBatchAtlas::EvictionFunc func, v
oid* data) { |
| 164 GrSurfaceDesc desc; | 122 GrSurfaceDesc desc; |
| 165 desc.fFlags = kNone_GrSurfaceFlags; | 123 desc.fFlags = kNone_GrSurfaceFlags; |
| 166 desc.fWidth = width; | 124 desc.fWidth = width; |
| 167 desc.fHeight = height; | 125 desc.fHeight = height; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 186 } |
| 229 | 187 |
| 230 GrRenderTarget* GrResourceProvider::wrapBackendTextureAsRenderTarget( | 188 GrRenderTarget* GrResourceProvider::wrapBackendTextureAsRenderTarget( |
| 231 const GrBackendTextureDesc& desc, GrWrapOwnership ownership) { | 189 const GrBackendTextureDesc& desc, GrWrapOwnership ownership) { |
| 232 if (this->isAbandoned()) { | 190 if (this->isAbandoned()) { |
| 233 return nullptr; | 191 return nullptr; |
| 234 } | 192 } |
| 235 return this->gpu()->wrapBackendTextureAsRenderTarget(desc, ownership); | 193 return this->gpu()->wrapBackendTextureAsRenderTarget(desc, ownership); |
| 236 } | 194 } |
| 237 | 195 |
| OLD | NEW |