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 | |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 11 | #include "GrBatchAtlas.h" |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 12 | #include "GrIndexBuffer.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 13 | #include "GrTextureProvider.h" |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 14 | #include "GrPathRange.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 15 | |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 16 | class GrBatchAtlas; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 17 | class GrIndexBuffer; |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 18 | class GrPath; |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 19 | class GrRenderTarget; |
| 20 | class GrStencilAttachment; |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 21 | class GrStrokeInfo; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 22 | class GrVertexBuffer; |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 23 | class SkDescriptor; |
| 24 | class SkPath; |
| 25 | class SkTypeface; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 26 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 27 | /** |
| 28 | * An extension of the texture provider for arbitrary resource types. This class is intended for |
| 29 | * use within the Gr code base, not by clients or extensions (e.g. third party GrProcessor |
| 30 | * derivatives). |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 31 | * |
| 32 | * This currently inherits from GrTextureProvider non-publically to force callers to provider |
| 33 | * make a flags (pendingIO) decision and not use the GrTP methods that don't take flags. This |
| 34 | * can be relaxed once http://skbug.com/4156 is fixed. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 35 | */ |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 36 | class GrResourceProvider : protected GrTextureProvider { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 37 | public: |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 38 | GrResourceProvider(GrGpu* gpu, GrResourceCache* cache); |
| 39 | |
| 40 | template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) { |
| 41 | return static_cast<T*>(this->findAndRefResourceByUniqueKey(key)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Either finds and refs, or creates an index buffer for instanced drawing with a specific |
| 46 | * pattern if the index buffer is not found. If the return is non-null, the caller owns |
| 47 | * a ref on the returned GrIndexBuffer. |
| 48 | * |
| 49 | * @param pattern the pattern of indices to repeat |
| 50 | * @param patternSize size in bytes of the pattern |
| 51 | * @param reps number of times to repeat the pattern |
| 52 | * @param vertCount number of vertices the pattern references |
| 53 | * @param key Key to be assigned to the index buffer. |
| 54 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | * @return The index buffer if successful, otherwise nullptr. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 56 | */ |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 57 | const GrIndexBuffer* findOrCreateInstancedIndexBuffer(const uint16_t* pattern, |
| 58 | int patternSize, |
| 59 | int reps, |
| 60 | int vertCount, |
| 61 | const GrUniqueKey& key) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 62 | if (GrIndexBuffer* buffer = this->findAndRefTByUniqueKey<GrIndexBuffer>(key)) { |
| 63 | return buffer; |
| 64 | } |
| 65 | return this->createInstancedIndexBuffer(pattern, patternSize, reps, vertCount, key); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns an index buffer that can be used to render quads. |
| 70 | * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. |
| 71 | * The max number of quads can be queried using GrIndexBuffer::maxQuads(). |
| 72 | * Draw with kTriangles_GrPrimitiveType |
| 73 | * @ return the quad index buffer |
| 74 | */ |
| 75 | const GrIndexBuffer* refQuadIndexBuffer() { |
| 76 | if (GrIndexBuffer* buffer = |
| 77 | this->findAndRefTByUniqueKey<GrIndexBuffer>(fQuadIndexBufferKey)) { |
| 78 | return buffer; |
| 79 | } |
| 80 | return this->createQuadIndexBuffer(); |
| 81 | } |
| 82 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering |
| 85 | * is not supported. |
| 86 | */ |
| 87 | GrPath* createPath(const SkPath&, const GrStrokeInfo&); |
| 88 | GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStrokeInfo&); |
| 89 | GrPathRange* createGlyphs(const SkTypeface*, const SkDescriptor*, const GrStrokeInfo&); |
| 90 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 91 | using GrTextureProvider::assignUniqueKeyToResource; |
| 92 | using GrTextureProvider::findAndRefResourceByUniqueKey; |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 93 | using GrTextureProvider::findAndRefTextureByUniqueKey; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 94 | using GrTextureProvider::abandon; |
| 95 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 96 | enum Flags { |
| 97 | /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be |
| 98 | * set when accessing resources during a GrDrawTarget flush. This includes the execution of |
| 99 | * GrBatch objects. The reason is that these memory operations are done immediately and |
| 100 | * will occur out of order WRT the operations being flushed. |
| 101 | * Make this automatic: http://skbug.com/4156 |
| 102 | */ |
| 103 | kNoPendingIO_Flag = kNoPendingIO_ScratchTextureFlag, |
| 104 | }; |
| 105 | |
| 106 | enum BufferUsage { |
| 107 | /** Caller intends to specify the buffer data rarely with respect to the number of draws |
| 108 | that read the data. */ |
| 109 | kStatic_BufferUsage, |
| 110 | /** Caller intends to respecify the buffer data frequently between draws. */ |
| 111 | kDynamic_BufferUsage, |
| 112 | }; |
| 113 | GrIndexBuffer* createIndexBuffer(size_t size, BufferUsage, uint32_t flags); |
| 114 | GrVertexBuffer* createVertexBuffer(size_t size, BufferUsage, uint32_t flags); |
| 115 | |
| 116 | GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) { |
| 117 | SkASSERT(0 == flags || kNoPendingIO_Flag == flags); |
| 118 | return this->internalCreateApproxTexture(desc, flags); |
| 119 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 120 | |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 121 | /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should |
| 122 | * only be used inside of GrBatch::generateGeometry |
| 123 | * @param GrPixelConfig The pixel config which this atlas will store |
| 124 | * @param width width in pixels of the atlas |
| 125 | * @param height height in pixels of the atlas |
| 126 | * @param numPlotsX The number of plots the atlas should be broken up into in the X |
| 127 | * direction |
| 128 | * @param numPlotsY The number of plots the atlas should be broken up into in the Y |
| 129 | * direction |
| 130 | * @param func An eviction function which will be called whenever the atlas has to |
| 131 | * evict data |
| 132 | * @param data User supplied data which will be passed into func whenver an |
| 133 | * eviction occurs |
| 134 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | * @return An initialized GrBatchAtlas, or nullptr if creation fails |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 136 | */ |
| 137 | GrBatchAtlas* createAtlas(GrPixelConfig, int width, int height, int numPlotsX, int numPlotsY, |
| 138 | GrBatchAtlas::EvictionFunc func, void* data); |
| 139 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 140 | /** |
| 141 | * If passed in render target already has a stencil buffer, return it. Otherwise attempt to |
| 142 | * attach one. |
| 143 | */ |
| 144 | GrStencilAttachment* attachStencilAttachment(GrRenderTarget* rt); |
| 145 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 146 | const GrCaps* caps() { return this->gpu()->caps(); } |
| 147 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 148 | private: |
| 149 | const GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern, |
| 150 | int patternSize, |
| 151 | int reps, |
| 152 | int vertCount, |
| 153 | const GrUniqueKey& key); |
| 154 | |
| 155 | const GrIndexBuffer* createQuadIndexBuffer(); |
| 156 | |
| 157 | GrUniqueKey fQuadIndexBufferKey; |
| 158 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 159 | typedef GrTextureProvider INHERITED; |
| 160 | }; |
| 161 | |
| 162 | #endif |