blob: 12e6c559852d4bdd0d8027e1815c7a73874514ad [file] [log] [blame]
bsalomond309e7a2015-04-30 14:18:54 -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#ifndef GrResourceProvider_DEFINED
9#define GrResourceProvider_DEFINED
10
11#include "GrTextureProvider.h"
bsalomon706f08f2015-05-22 07:35:58 -070012#include "GrPathRange.h"
bsalomond309e7a2015-04-30 14:18:54 -070013
bsalomoned0bcad2015-05-04 10:36:42 -070014class GrIndexBuffer;
bsalomon706f08f2015-05-22 07:35:58 -070015class GrPath;
16class GrStrokeInfo;
bsalomoned0bcad2015-05-04 10:36:42 -070017class GrVertexBuffer;
bsalomon706f08f2015-05-22 07:35:58 -070018class SkDescriptor;
19class SkPath;
20class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070021
bsalomond309e7a2015-04-30 14:18:54 -070022/**
23 * An extension of the texture provider for arbitrary resource types. This class is intended for
24 * use within the Gr code base, not by clients or extensions (e.g. third party GrProcessor
25 * derivatives).
26 */
27class GrResourceProvider : public GrTextureProvider {
28public:
29
bsalomoned0bcad2015-05-04 10:36:42 -070030 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache);
31
32 template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
33 return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
34 }
35
36 /**
37 * Either finds and refs, or creates an index buffer for instanced drawing with a specific
38 * pattern if the index buffer is not found. If the return is non-null, the caller owns
39 * a ref on the returned GrIndexBuffer.
40 *
41 * @param pattern the pattern of indices to repeat
42 * @param patternSize size in bytes of the pattern
43 * @param reps number of times to repeat the pattern
44 * @param vertCount number of vertices the pattern references
45 * @param key Key to be assigned to the index buffer.
46 *
47 * @return The index buffer if successful, otherwise NULL.
48 */
49 const GrIndexBuffer* refOrCreateInstancedIndexBuffer(const uint16_t* pattern,
50 int patternSize,
51 int reps,
52 int vertCount,
53 const GrUniqueKey& key) {
54 if (GrIndexBuffer* buffer = this->findAndRefTByUniqueKey<GrIndexBuffer>(key)) {
55 return buffer;
56 }
57 return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key);
58 }
59
60 /**
61 * Returns an index buffer that can be used to render quads.
62 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
63 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
64 * Draw with kTriangles_GrPrimitiveType
65 * @ return the quad index buffer
66 */
67 const GrIndexBuffer* refQuadIndexBuffer() {
68 if (GrIndexBuffer* buffer =
69 this->findAndRefTByUniqueKey<GrIndexBuffer>(fQuadIndexBufferKey)) {
70 return buffer;
71 }
72 return this->createQuadIndexBuffer();
73 }
74
bsalomon706f08f2015-05-22 07:35:58 -070075 /**
76 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
77 * is not supported.
78 */
79 GrPath* createPath(const SkPath&, const GrStrokeInfo&);
80 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStrokeInfo&);
81 GrPathRange* createGlyphs(const SkTypeface*, const SkDescriptor*, const GrStrokeInfo&);
82
bsalomond309e7a2015-04-30 14:18:54 -070083
84 using GrTextureProvider::assignUniqueKeyToResource;
85 using GrTextureProvider::findAndRefResourceByUniqueKey;
86 using GrTextureProvider::abandon;
87
bsalomoned0bcad2015-05-04 10:36:42 -070088private:
89 const GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
90 int patternSize,
91 int reps,
92 int vertCount,
93 const GrUniqueKey& key);
94
95 const GrIndexBuffer* createQuadIndexBuffer();
96
97 GrUniqueKey fQuadIndexBufferKey;
98
bsalomond309e7a2015-04-30 14:18:54 -070099 typedef GrTextureProvider INHERITED;
100};
101
102#endif