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