bsalomon | d309e7a | 2015-04-30 14:18:54 -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 | #ifndef GrResourceProvider_DEFINED |
| 9 | #define GrResourceProvider_DEFINED |
| 10 | |
| 11 | #include "GrTextureProvider.h" |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame^] | 12 | #include "GrPathRange.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 13 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 14 | class GrIndexBuffer; |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame^] | 15 | class GrPath; |
| 16 | class GrStrokeInfo; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 17 | class GrVertexBuffer; |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame^] | 18 | class SkDescriptor; |
| 19 | class SkPath; |
| 20 | class SkTypeface; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 21 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 22 | /** |
| 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 | */ |
| 27 | class GrResourceProvider : public GrTextureProvider { |
| 28 | public: |
| 29 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 30 | 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 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame^] | 75 | /** |
| 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 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 83 | |
| 84 | using GrTextureProvider::assignUniqueKeyToResource; |
| 85 | using GrTextureProvider::findAndRefResourceByUniqueKey; |
| 86 | using GrTextureProvider::abandon; |
| 87 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 88 | private: |
| 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 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 99 | typedef GrTextureProvider INHERITED; |
| 100 | }; |
| 101 | |
| 102 | #endif |