blob: 70535e2344f3a2d00194badf3b61c8cc8ad85e7f [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
joshualittb356cbc2015-08-05 06:36:39 -070011#include "GrBatchAtlas.h"
cdalton397536c2016-03-25 12:15:03 -070012#include "GrBuffer.h"
Greg Daniel77b53f62016-10-18 11:48:51 -040013#include "GrGpu.h"
bsalomond309e7a2015-04-30 14:18:54 -070014#include "GrTextureProvider.h"
bsalomon706f08f2015-05-22 07:35:58 -070015#include "GrPathRange.h"
bsalomond309e7a2015-04-30 14:18:54 -070016
joshualittb356cbc2015-08-05 06:36:39 -070017class GrBatchAtlas;
bsalomon706f08f2015-05-22 07:35:58 -070018class GrPath;
egdanielec00d942015-09-14 12:56:10 -070019class GrRenderTarget;
joshualitt6d0872d2016-01-11 08:27:48 -080020class GrSingleOwner;
egdanielec00d942015-09-14 12:56:10 -070021class GrStencilAttachment;
bsalomon6663acf2016-05-10 09:14:17 -070022class GrStyle;
bsalomon706f08f2015-05-22 07:35:58 -070023class SkDescriptor;
24class SkPath;
25class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070026
bsalomond309e7a2015-04-30 14:18:54 -070027/**
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).
bsalomoneae62002015-07-31 13:59:30 -070031 *
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
halcanary6950de62015-11-07 05:29:00 -080034 * can be relaxed once https://bug.skia.org/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070035 */
bsalomoneae62002015-07-31 13:59:30 -070036class GrResourceProvider : protected GrTextureProvider {
bsalomond309e7a2015-04-30 14:18:54 -070037public:
joshualitt6d0872d2016-01-11 08:27:48 -080038 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner);
bsalomoned0bcad2015-05-04 10:36:42 -070039
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
cdalton397536c2016-03-25 12:15:03 -070047 * a ref on the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -070048 *
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 *
halcanary96fcdcc2015-08-27 07:41:13 -070055 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -070056 */
cdalton397536c2016-03-25 12:15:03 -070057 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)) {
bsalomoned0bcad2015-05-04 10:36:42 -070063 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.
cdalton397536c2016-03-25 12:15:03 -070071 * The max number of quads is the buffer's index capacity divided by 6.
bsalomoned0bcad2015-05-04 10:36:42 -070072 * Draw with kTriangles_GrPrimitiveType
73 * @ return the quad index buffer
74 */
cdalton397536c2016-03-25 12:15:03 -070075 const GrBuffer* refQuadIndexBuffer() {
76 if (GrBuffer* buffer =
77 this->findAndRefTByUniqueKey<GrBuffer>(fQuadIndexBufferKey)) {
bsalomoned0bcad2015-05-04 10:36:42 -070078 return buffer;
79 }
80 return this->createQuadIndexBuffer();
81 }
82
bsalomon706f08f2015-05-22 07:35:58 -070083 /**
84 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
85 * is not supported.
86 */
bsalomon6663acf2016-05-10 09:14:17 -070087 GrPath* createPath(const SkPath&, const GrStyle&);
88 GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStyle&);
reeda9322c22016-04-12 06:47:05 -070089 GrPathRange* createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
bsalomon6663acf2016-05-10 09:14:17 -070090 const SkDescriptor*, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -070091
bsalomond309e7a2015-04-30 14:18:54 -070092 using GrTextureProvider::assignUniqueKeyToResource;
93 using GrTextureProvider::findAndRefResourceByUniqueKey;
bsalomon473addf2015-10-02 07:49:05 -070094 using GrTextureProvider::findAndRefTextureByUniqueKey;
bsalomond309e7a2015-04-30 14:18:54 -070095 using GrTextureProvider::abandon;
96
bsalomoneae62002015-07-31 13:59:30 -070097 enum Flags {
98 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
Robert Phillipsf2361d22016-10-25 14:20:06 -040099 * set when accessing resources during a GrOpList flush. This includes the execution of
Brian Salomon25a88092016-12-01 09:36:50 -0500100 * GrOp objects. The reason is that these memory operations are done immediately and
bsalomoneae62002015-07-31 13:59:30 -0700101 * will occur out of order WRT the operations being flushed.
halcanary6950de62015-11-07 05:29:00 -0800102 * Make this automatic: https://bug.skia.org/4156
bsalomoneae62002015-07-31 13:59:30 -0700103 */
csmartdalton485a1202016-07-13 10:16:32 -0700104 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,
bsalomoneae62002015-07-31 13:59:30 -0700110 };
111
cdaltone2e71c22016-04-07 18:13:29 -0700112 /**
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.
cdalton1bf3e712016-04-19 10:00:02 -0700119 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700120 *
121 * @return the buffer if successful, otherwise nullptr.
122 */
cdalton1bf3e712016-04-19 10:00:02 -0700123 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, uint32_t flags,
124 const void* data = nullptr);
bsalomoneae62002015-07-31 13:59:30 -0700125
126 GrTexture* createApproxTexture(const GrSurfaceDesc& desc, uint32_t flags) {
127 SkASSERT(0 == flags || kNoPendingIO_Flag == flags);
128 return this->internalCreateApproxTexture(desc, flags);
129 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700130
joshualittb356cbc2015-08-05 06:36:39 -0700131 /** Returns a GrBatchAtlas. This function can be called anywhere, but the returned atlas should
Brian Salomon25a88092016-12-01 09:36:50 -0500132 * only be used inside of GrOp::generateGeometry
joshualittb356cbc2015-08-05 06:36:39 -0700133 * @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 *
halcanary96fcdcc2015-08-27 07:41:13 -0700145 * @return An initialized GrBatchAtlas, or nullptr if creation fails
joshualittb356cbc2015-08-05 06:36:39 -0700146 */
Ben Wagner594f9ed2016-11-08 14:13:39 -0500147 std::unique_ptr<GrBatchAtlas> makeAtlas(GrPixelConfig, int width, int height,
148 int numPlotsX, int numPlotsY,
149 GrBatchAtlas::EvictionFunc func, void* data);
joshualittb356cbc2015-08-05 06:36:39 -0700150
egdanielec00d942015-09-14 12:56:10 -0700151 /**
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
bsalomon473addf2015-10-02 07:49:05 -0700157 const GrCaps* caps() { return this->gpu()->caps(); }
158
ericrkf7b8b8a2016-02-24 14:49:51 -0800159 /**
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 *
kkinnunen49c4c222016-04-01 04:50:37 -0700163 * The texture is wrapped as borrowed. The texture object will not be freed once the
164 * render target is destroyed.
165 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800166 * @return GrRenderTarget object or NULL on failure.
167 */
bungeman6bd52842016-10-27 09:30:08 -0700168 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800169
bsalomoned0bcad2015-05-04 10:36:42 -0700170private:
cdalton397536c2016-03-25 12:15:03 -0700171 const GrBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
172 int patternSize,
173 int reps,
174 int vertCount,
175 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700176
cdalton397536c2016-03-25 12:15:03 -0700177 const GrBuffer* createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700178
179 GrUniqueKey fQuadIndexBufferKey;
180
bsalomond309e7a2015-04-30 14:18:54 -0700181 typedef GrTextureProvider INHERITED;
182};
183
184#endif