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" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 12 | #include "GrBuffer.h" |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 13 | #include "GrGpu.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 14 | #include "GrTextureProvider.h" |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 15 | #include "GrPathRange.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 16 | |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 17 | class GrBatchAtlas; |
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 | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 22 | class GrStyle; |
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 |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 34 | * can be relaxed once https://bug.skia.org/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: |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 38 | GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 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 |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 47 | * a ref on the returned GrBuffer. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 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 | */ |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 57 | const GrBuffer* findOrCreateInstancedIndexBuffer(const uint16_t* pattern, |
| 58 | int patternSize, |
| 59 | int reps, |
| 60 | int vertCount, |
| 61 | const GrUniqueKey& key) { |
| 62 | if (GrBuffer* buffer = this->findAndRefTByUniqueKey<GrBuffer>(key)) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 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. |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 71 | * The max number of quads is the buffer's index capacity divided by 6. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 72 | * Draw with kTriangles_GrPrimitiveType |
| 73 | * @ return the quad index buffer |
| 74 | */ |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 75 | const GrBuffer* refQuadIndexBuffer() { |
| 76 | if (GrBuffer* buffer = |
| 77 | this->findAndRefTByUniqueKey<GrBuffer>(fQuadIndexBufferKey)) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 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 | */ |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 87 | GrPath* createPath(const SkPath&, const GrStyle&); |
| 88 | GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStyle&); |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 89 | GrPathRange* createGlyphs(const SkTypeface*, const SkScalerContextEffects&, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 90 | const SkDescriptor*, const GrStyle&); |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 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 |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 99 | * set when accessing resources during a GrOpList flush. This includes the execution of |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame^] | 100 | * GrOp objects. The reason is that these memory operations are done immediately and |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 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 | */ |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 104 | kNoPendingIO_Flag = 0x1, |
| 105 | |
| 106 | /** Normally the caps may indicate a preference for client-side buffers. Set this flag when |
| 107 | * creating a buffer to guarantee it resides in GPU memory. |
| 108 | */ |
| 109 | kRequireGpuMemory_Flag = 0x2, |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Returns a buffer. |
| 114 | * |
| 115 | * @param size minimum size of buffer to return. |
| 116 | * @param intendedType hint to the graphics subsystem about what the buffer will be used for. |
| 117 | * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed. |
| 118 | * @param flags see Flags enum. |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 119 | * @param data optional data with which to initialize the buffer. |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 120 | * |
| 121 | * @return the buffer if successful, otherwise nullptr. |
| 122 | */ |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 123 | GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, uint32_t flags, |
| 124 | const void* data = nullptr); |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 125 | |
| 126 | GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) { |
| 127 | SkASSERT(0 == flags || kNoPendingIO_Flag == flags); |
| 128 | return this->internalCreateApproxTexture(desc, flags); |
| 129 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 130 | |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 131 | /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame^] | 132 | * only be used inside of GrOp::generateGeometry |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 133 | * @param GrPixelConfig The pixel config which this atlas will store |
| 134 | * @param width width in pixels of the atlas |
| 135 | * @param height height in pixels of the atlas |
| 136 | * @param numPlotsX The number of plots the atlas should be broken up into in the X |
| 137 | * direction |
| 138 | * @param numPlotsY The number of plots the atlas should be broken up into in the Y |
| 139 | * direction |
| 140 | * @param func An eviction function which will be called whenever the atlas has to |
| 141 | * evict data |
| 142 | * @param data User supplied data which will be passed into func whenver an |
| 143 | * eviction occurs |
| 144 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | * @return An initialized GrBatchAtlas, or nullptr if creation fails |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 146 | */ |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 147 | std::unique_ptr<GrBatchAtlas> makeAtlas(GrPixelConfig, int width, int height, |
| 148 | int numPlotsX, int numPlotsY, |
| 149 | GrBatchAtlas::EvictionFunc func, void* data); |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 150 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 151 | /** |
| 152 | * If passed in render target already has a stencil buffer, return it. Otherwise attempt to |
| 153 | * attach one. |
| 154 | */ |
| 155 | GrStencilAttachment* attachStencilAttachment(GrRenderTarget* rt); |
| 156 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 157 | const GrCaps* caps() { return this->gpu()->caps(); } |
| 158 | |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 159 | /** |
| 160 | * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided |
| 161 | * texture has a format that cannot be textured from by Skia, but we want to raster to it. |
| 162 | * |
kkinnunen | 49c4c22 | 2016-04-01 04:50:37 -0700 | [diff] [blame] | 163 | * The texture is wrapped as borrowed. The texture object will not be freed once the |
| 164 | * render target is destroyed. |
| 165 | * |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 166 | * @return GrRenderTarget object or NULL on failure. |
| 167 | */ |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 168 | sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 169 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 170 | private: |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 171 | const GrBuffer* createInstancedIndexBuffer(const uint16_t* pattern, |
| 172 | int patternSize, |
| 173 | int reps, |
| 174 | int vertCount, |
| 175 | const GrUniqueKey& key); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 176 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 177 | const GrBuffer* createQuadIndexBuffer(); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 178 | |
| 179 | GrUniqueKey fQuadIndexBufferKey; |
| 180 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 181 | typedef GrTextureProvider INHERITED; |
| 182 | }; |
| 183 | |
| 184 | #endif |