blob: d3ab8ccfa1646d860dbb8d3789f3bcb49126a7dc [file] [log] [blame]
bsalomoned0bcad2015-05-04 10:36:42 -07001/*
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"
kkinnunencabe20c2015-06-01 01:37:26 -070011#include "GrIndexBuffer.h"
12#include "GrPathRendering.h"
bsalomoned0bcad2015-05-04 10:36:42 -070013#include "GrResourceCache.h"
14#include "GrResourceKey.h"
15#include "GrVertexBuffer.h"
16
17GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
18
19GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache) : INHERITED(gpu, cache) {
20 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
21 fQuadIndexBufferKey = gQuadIndexBufferKey;
22}
23
24const 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
bsalomoneae62002015-07-31 13:59:30 -070031 // This is typically used in GrBatchs, so we assume kNoPendingIO.
32 GrIndexBuffer* buffer = this->createIndexBuffer(bufferSize, kStatic_BufferUsage,
33 kNoPendingIO_Flag);
bsalomoned0bcad2015-05-04 10:36:42 -070034 if (!buffer) {
halcanary96fcdcc2015-08-27 07:41:13 -070035 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -070036 }
37 uint16_t* data = (uint16_t*) buffer->map();
halcanary96fcdcc2015-08-27 07:41:13 -070038 bool useTempData = (nullptr == data);
bsalomoned0bcad2015-05-04 10:36:42 -070039 if (useTempData) {
halcanary385fe4d2015-08-26 13:07:48 -070040 data = new uint16_t[reps * patternSize];
bsalomoned0bcad2015-05-04 10:36:42 -070041 }
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();
halcanary96fcdcc2015-08-27 07:41:13 -070052 return nullptr;
bsalomoned0bcad2015-05-04 10:36:42 -070053 }
halcanary385fe4d2015-08-26 13:07:48 -070054 delete[] data;
bsalomoned0bcad2015-05-04 10:36:42 -070055 } else {
56 buffer->unmap();
57 }
58 this->assignUniqueKeyToResource(key, buffer);
59 return buffer;
60}
61
62const 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
bsalomon706f08f2015-05-22 07:35:58 -070070GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& stroke) {
71 SkASSERT(this->gpu()->pathRendering());
72 return this->gpu()->pathRendering()->createPath(path, stroke);
73}
74
75GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
76 const GrStrokeInfo& stroke) {
77 SkASSERT(this->gpu()->pathRendering());
78 return this->gpu()->pathRendering()->createPathRange(gen, stroke);
79}
80
81GrPathRange* 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
bsalomoneae62002015-07-31 13:59:30 -070088GrIndexBuffer* GrResourceProvider::createIndexBuffer(size_t size, BufferUsage usage,
89 uint32_t flags) {
robertphillips1b8e1b52015-06-24 06:54:10 -070090 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -070091 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -070092 }
93
bsalomoneae62002015-07-31 13:59:30 -070094 bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag);
95 bool dynamic = kDynamic_BufferUsage == usage;
robertphillips1b8e1b52015-06-24 06:54:10 -070096 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;
bsalomoneae62002015-07-31 13:59:30 -0700102 GrIndexBuffer::ComputeScratchKey(size, true, &key);
robertphillips1b8e1b52015-06-24 06:54:10 -0700103 uint32_t scratchFlags = 0;
bsalomoneae62002015-07-31 13:59:30 -0700104 if (noPendingIO) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700105 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
106 } else {
107 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
108 }
robertphillips6e83ac72015-08-13 05:19:14 -0700109 GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags);
robertphillips1b8e1b52015-06-24 06:54:10 -0700110 if (resource) {
111 return static_cast<GrIndexBuffer*>(resource);
112 }
113 }
bsalomoneae62002015-07-31 13:59:30 -0700114 return this->gpu()->createIndexBuffer(size, dynamic);
robertphillips1b8e1b52015-06-24 06:54:10 -0700115}
116
bsalomoneae62002015-07-31 13:59:30 -0700117GrVertexBuffer* GrResourceProvider::createVertexBuffer(size_t size, BufferUsage usage,
118 uint32_t flags) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700119 if (this->isAbandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 return nullptr;
robertphillips1b8e1b52015-06-24 06:54:10 -0700121 }
122
bsalomoneae62002015-07-31 13:59:30 -0700123 bool noPendingIO = SkToBool(flags & kNoPendingIO_Flag);
124 bool dynamic = kDynamic_BufferUsage == usage;
robertphillips1b8e1b52015-06-24 06:54:10 -0700125 if (dynamic) {
126 // bin by pow2 with a reasonable min
bsalomoneae62002015-07-31 13:59:30 -0700127 static const uint32_t MIN_SIZE = 1 << 12;
robertphillips1b8e1b52015-06-24 06:54:10 -0700128 size = SkTMax(MIN_SIZE, GrNextPow2(SkToUInt(size)));
129
130 GrScratchKey key;
bsalomoneae62002015-07-31 13:59:30 -0700131 GrVertexBuffer::ComputeScratchKey(size, true, &key);
robertphillips1b8e1b52015-06-24 06:54:10 -0700132 uint32_t scratchFlags = 0;
bsalomoneae62002015-07-31 13:59:30 -0700133 if (noPendingIO) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700134 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
135 } else {
136 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
137 }
robertphillips6e83ac72015-08-13 05:19:14 -0700138 GrGpuResource* resource = this->cache()->findAndRefScratchResource(key, size, scratchFlags);
robertphillips1b8e1b52015-06-24 06:54:10 -0700139 if (resource) {
140 return static_cast<GrVertexBuffer*>(resource);
141 }
142 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700143 return this->gpu()->createVertexBuffer(size, dynamic);
144}
joshualittb356cbc2015-08-05 06:36:39 -0700145
146GrBatchAtlas* 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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return nullptr;
joshualittb356cbc2015-08-05 06:36:39 -0700163 }
halcanary385fe4d2015-08-26 13:07:48 -0700164 return new GrBatchAtlas(texture, numPlotsX, numPlotsY);
joshualittb356cbc2015-08-05 06:36:39 -0700165}