blob: 8ac5a3a844dc94188425f16badc15ee7beaa8397 [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"
11#include "GrResourceCache.h"
12#include "GrResourceKey.h"
13#include "GrVertexBuffer.h"
14
15GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
16
17GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache) : INHERITED(gpu, cache) {
18 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
19 fQuadIndexBufferKey = gQuadIndexBufferKey;
20}
21
22const GrIndexBuffer* GrResourceProvider::createInstancedIndexBuffer(const uint16_t* pattern,
23 int patternSize,
24 int reps,
25 int vertCount,
26 const GrUniqueKey& key) {
27 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
28
robertphillipseea2ff72015-05-14 05:24:53 -070029 GrIndexBuffer* buffer = this->gpu()->createIndexBuffer(bufferSize, /* dynamic = */ false);
bsalomoned0bcad2015-05-04 10:36:42 -070030 if (!buffer) {
31 return NULL;
32 }
33 uint16_t* data = (uint16_t*) buffer->map();
34 bool useTempData = (NULL == data);
35 if (useTempData) {
36 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
37 }
38 for (int i = 0; i < reps; ++i) {
39 int baseIdx = i * patternSize;
40 uint16_t baseVert = (uint16_t)(i * vertCount);
41 for (int j = 0; j < patternSize; ++j) {
42 data[baseIdx+j] = baseVert + pattern[j];
43 }
44 }
45 if (useTempData) {
46 if (!buffer->updateData(data, bufferSize)) {
47 buffer->unref();
48 return NULL;
49 }
50 SkDELETE_ARRAY(data);
51 } else {
52 buffer->unmap();
53 }
54 this->assignUniqueKeyToResource(key, buffer);
55 return buffer;
56}
57
58const GrIndexBuffer* GrResourceProvider::createQuadIndexBuffer() {
59 static const int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
60 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
61 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
62
63 return this->createInstancedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
64}
65
bsalomon706f08f2015-05-22 07:35:58 -070066GrPath* GrResourceProvider::createPath(const SkPath& path, const GrStrokeInfo& stroke) {
67 SkASSERT(this->gpu()->pathRendering());
68 return this->gpu()->pathRendering()->createPath(path, stroke);
69}
70
71GrPathRange* GrResourceProvider::createPathRange(GrPathRange::PathGenerator* gen,
72 const GrStrokeInfo& stroke) {
73 SkASSERT(this->gpu()->pathRendering());
74 return this->gpu()->pathRendering()->createPathRange(gen, stroke);
75}
76
77GrPathRange* GrResourceProvider::createGlyphs(const SkTypeface* tf, const SkDescriptor* desc,
78 const GrStrokeInfo& stroke) {
79
80 SkASSERT(this->gpu()->pathRendering());
81 return this->gpu()->pathRendering()->createGlyphs(tf, desc, stroke);
82}
83