blob: c86136f1e23c9f4945a63d88fa5af40d57ebc09a [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
cdalton397536c2016-03-25 12:15:03 -070011#include "GrBuffer.h"
Robert Phillipsa3f70262018-02-08 10:59:38 -050012#include "GrContextOptions.h"
Robert Phillipseafd48a2017-11-16 07:52:08 -050013#include "GrResourceCache.h"
Cary Clark65e409f2018-02-28 15:01:05 -050014#include "SkImageInfoPriv.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040015#include "SkScalerContext.h"
bsalomond309e7a2015-04-30 14:18:54 -070016
Greg Danielbcf612b2017-05-01 13:50:58 +000017class GrBackendRenderTarget;
Greg Daniela5cb7812017-06-16 09:45:32 -040018class GrBackendSemaphore;
19class GrBackendTexture;
Robert Phillips009e9af2017-06-15 14:01:04 -040020class GrGpu;
bsalomon706f08f2015-05-22 07:35:58 -070021class GrPath;
egdanielec00d942015-09-14 12:56:10 -070022class GrRenderTarget;
Greg Daniel8982dc12018-01-05 12:56:15 -050023class GrResourceProviderPriv;
Robert Phillips646e4292017-06-13 12:44:56 -040024class GrSemaphore;
joshualitt6d0872d2016-01-11 08:27:48 -080025class GrSingleOwner;
egdanielec00d942015-09-14 12:56:10 -070026class GrStencilAttachment;
Robert Phillips646e4292017-06-13 12:44:56 -040027class GrTexture;
Robert Phillips646e4292017-06-13 12:44:56 -040028
bsalomon6663acf2016-05-10 09:14:17 -070029class GrStyle;
bsalomon706f08f2015-05-22 07:35:58 -070030class SkDescriptor;
31class SkPath;
32class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070033
bsalomond309e7a2015-04-30 14:18:54 -070034/**
Brian Osman32342f02017-03-04 08:12:46 -050035 * A factory for arbitrary resource types. This class is intended for use within the Gr code base.
bsalomoneae62002015-07-31 13:59:30 -070036 *
Brian Osman32342f02017-03-04 08:12:46 -050037 * Some members force callers to make a flags (pendingIO) decision. This can be relaxed once
38 * https://bug.skia.org/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070039 */
Brian Osman32342f02017-03-04 08:12:46 -050040class GrResourceProvider {
bsalomond309e7a2015-04-30 14:18:54 -070041public:
Chris Daltond004e0b2018-09-27 09:28:03 -060042 /** These flags govern which scratch resources we are allowed to return */
43 enum class Flags {
44 kNone = 0x0,
45
46 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
47 * set when accessing resources during a GrOpList flush. This includes the execution of
48 * GrOp objects. The reason is that these memory operations are done immediately and
49 * will occur out of order WRT the operations being flushed.
50 * Make this automatic: https://bug.skia.org/4156
51 */
52 kNoPendingIO = 0x1,
53
54 /** Normally the caps may indicate a preference for client-side buffers. Set this flag when
55 * creating a buffer to guarantee it resides in GPU memory.
56 */
57 kRequireGpuMemory = 0x2,
58 };
59
Robert Phillipsa3f70262018-02-08 10:59:38 -050060 GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*,
61 GrContextOptions::Enable explicitlyAllocateGPUResources);
bsalomoned0bcad2015-05-04 10:36:42 -070062
Brian Salomond28a79d2017-10-16 13:01:07 -040063 /**
64 * Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
65 * must be sure that if a resource of exists in the cache with the given unique key then it is
66 * of type T.
67 */
68 template <typename T>
69 sk_sp<T> findByUniqueKey(const GrUniqueKey& key) {
70 return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
bsalomoned0bcad2015-05-04 10:36:42 -070071 }
72
Brian Osman32342f02017-03-04 08:12:46 -050073 ///////////////////////////////////////////////////////////////////////////
74 // Textures
75
Brian Osman32342f02017-03-04 08:12:46 -050076 /**
77 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
78 * and height as desc specifies. If desc specifies that the texture should be a render target
79 * then result will be a render target. Format and sample count will always match the request.
Robert Phillips67d52cf2017-06-05 13:38:13 -040080 * The contents of the texture are undefined.
Brian Osman32342f02017-03-04 08:12:46 -050081 */
Chris Daltond004e0b2018-09-27 09:28:03 -060082 sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc&, Flags);
Brian Osman32342f02017-03-04 08:12:46 -050083
Robert Phillipse78b7252017-04-06 07:59:41 -040084 /** Create an exact fit texture with no initial data to upload.
85 */
Chris Daltond004e0b2018-09-27 09:28:03 -060086 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, Flags = Flags::kNone);
Robert Phillips45fdae12017-04-17 12:57:27 -040087
Brian Salomon58389b92018-03-07 13:01:25 -050088 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel texels[],
Brian Osman2b23c4b2018-06-01 12:25:08 -040089 int mipLevelCount);
Robert Phillips8e8c7552017-07-10 12:06:05 -040090
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 // Create a potentially loose fit texture with the provided data
Greg Danielfb3abcd2018-02-02 15:48:33 -050092 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, SkBackingFit,
Chris Daltond004e0b2018-09-27 09:28:03 -060093 const GrMipLevel&, Flags);
Robert Phillipse78b7252017-04-06 07:59:41 -040094
Brian Osman32342f02017-03-04 08:12:46 -050095 ///////////////////////////////////////////////////////////////////////////
96 // Wrapped Backend Surfaces
97
98 /**
99 * Wraps an existing texture with a GrTexture object.
100 *
Brian Salomonc67c31c2018-12-06 10:00:03 -0500101 * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the
102 * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels).
103 *
Brian Osman32342f02017-03-04 08:12:46 -0500104 * OpenGL: if the object is a texture Gr may change its GL texture params
105 * when it is drawn.
106 *
107 * @return GrTexture object or NULL on failure.
108 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000109 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500110 GrWrapOwnership /* = kBorrow_GrWrapOwnership*/,
111 GrIOType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500112 bool purgeImmediately = false);
Brian Osman32342f02017-03-04 08:12:46 -0500113
114 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -0500115 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
Brian Salomond17f6582017-07-19 18:28:58 -0400116 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
117 * to the texture.
118 */
119 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400120 int sampleCnt,
121 GrWrapOwnership = kBorrow_GrWrapOwnership);
122
123 /**
Brian Osman32342f02017-03-04 08:12:46 -0500124 * Wraps an existing render target with a GrRenderTarget object. It is
125 * similar to wrapBackendTexture but can be used to draw into surfaces
126 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
127 * the client will resolve to a texture). Currently wrapped render targets
128 * always use the kBorrow_GrWrapOwnership semantics.
129 *
130 * @return GrRenderTarget object or NULL on failure.
131 */
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400132 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
Brian Osman32342f02017-03-04 08:12:46 -0500133
Robert Phillips1bfece82017-06-01 13:56:52 -0400134 static const uint32_t kMinScratchTextureSize;
Brian Osman32342f02017-03-04 08:12:46 -0500135
bsalomoned0bcad2015-05-04 10:36:42 -0700136 /**
Chris Dalton5d2de082017-12-19 10:40:23 -0700137 * Either finds and refs, or creates a static buffer with the given parameters and contents.
138 *
139 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
140 * @param size minimum size of buffer to return.
141 * @param data optional data with which to initialize the buffer.
142 * @param key Key to be assigned to the buffer.
143 *
144 * @return The buffer if successful, otherwise nullptr.
145 */
146 sk_sp<const GrBuffer> findOrMakeStaticBuffer(GrBufferType intendedType, size_t size,
147 const void* data, const GrUniqueKey& key);
148
149 /**
Chris Daltonff926502017-05-03 14:36:54 -0400150 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
151 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
152 * the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -0700153 *
154 * @param pattern the pattern of indices to repeat
155 * @param patternSize size in bytes of the pattern
156 * @param reps number of times to repeat the pattern
157 * @param vertCount number of vertices the pattern references
158 * @param key Key to be assigned to the index buffer.
159 *
halcanary96fcdcc2015-08-27 07:41:13 -0700160 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -0700161 */
Brian Salomond28a79d2017-10-16 13:01:07 -0400162 sk_sp<const GrBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
163 int patternSize,
164 int reps,
165 int vertCount,
166 const GrUniqueKey& key) {
167 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
Kevin Lubickf7621cb2018-04-16 15:51:44 -0400168 return std::move(buffer);
bsalomoned0bcad2015-05-04 10:36:42 -0700169 }
Chris Daltonff926502017-05-03 14:36:54 -0400170 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, key);
bsalomoned0bcad2015-05-04 10:36:42 -0700171 }
172
173 /**
174 * Returns an index buffer that can be used to render quads.
Brian Salomon57caa662017-10-18 12:21:05 +0000175 * Six indices per quad: 0, 1, 2, 2, 1, 3, etc.
Brian Salomon763abf02018-05-01 18:49:38 +0000176 * The max number of quads is the buffer's index capacity divided by 6.
Chris Dalton3809bab2017-06-13 10:55:06 -0600177 * Draw with GrPrimitiveType::kTriangles
Brian Salomon763abf02018-05-01 18:49:38 +0000178 * @ return the quad index buffer
bsalomoned0bcad2015-05-04 10:36:42 -0700179 */
Brian Salomond28a79d2017-10-16 13:01:07 -0400180 sk_sp<const GrBuffer> refQuadIndexBuffer() {
181 if (auto buffer = this->findByUniqueKey<const GrBuffer>(fQuadIndexBufferKey)) {
bsalomoned0bcad2015-05-04 10:36:42 -0700182 return buffer;
183 }
184 return this->createQuadIndexBuffer();
185 }
186
Brian Salomon34169692017-08-28 15:32:01 -0400187 static int QuadCountOfQuadBuffer();
188
bsalomon706f08f2015-05-22 07:35:58 -0700189 /**
Ben Wagner3746ac22018-03-29 11:46:24 -0400190 * Factories for GrPath objects. It's an error to call these if path rendering
bsalomon706f08f2015-05-22 07:35:58 -0700191 * is not supported.
192 */
Robert Phillips67d52cf2017-06-05 13:38:13 -0400193 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -0700194
cdaltone2e71c22016-04-07 18:13:29 -0700195 /**
196 * Returns a buffer.
197 *
198 * @param size minimum size of buffer to return.
199 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
200 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
201 * @param flags see Flags enum.
cdalton1bf3e712016-04-19 10:00:02 -0700202 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700203 *
204 * @return the buffer if successful, otherwise nullptr.
205 */
Chris Daltond004e0b2018-09-27 09:28:03 -0600206 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, Flags,
cdalton1bf3e712016-04-19 10:00:02 -0700207 const void* data = nullptr);
bsalomoneae62002015-07-31 13:59:30 -0700208
robertphillips1b8e1b52015-06-24 06:54:10 -0700209
Brian Salomon2ee084e2016-12-16 18:59:19 -0500210 /**
Robert Phillipsc0192e32017-09-21 12:00:26 -0400211 * If passed in render target already has a stencil buffer, return true. Otherwise attempt to
212 * attach one and return true on success.
egdanielec00d942015-09-14 12:56:10 -0700213 */
Robert Phillipsc0192e32017-09-21 12:00:26 -0400214 bool attachStencilAttachment(GrRenderTarget* rt);
egdanielec00d942015-09-14 12:56:10 -0700215
ericrkf7b8b8a2016-02-24 14:49:51 -0800216 /**
217 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
218 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
219 *
kkinnunen49c4c222016-04-01 04:50:37 -0700220 * The texture is wrapped as borrowed. The texture object will not be freed once the
221 * render target is destroyed.
222 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800223 * @return GrRenderTarget object or NULL on failure.
224 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000225 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000226 int sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800227
Brian Osman32342f02017-03-04 08:12:46 -0500228 /**
229 * Assigns a unique key to a resource. If the key is associated with another resource that
230 * association is removed and replaced by this resource.
231 */
232 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
233
Greg Daniela5cb7812017-06-16 09:45:32 -0400234 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
235
Greg Daniel17b7c052018-01-09 13:55:33 -0500236 enum class SemaphoreWrapType {
237 kWillSignal,
238 kWillWait,
239 };
240
Greg Daniela5cb7812017-06-16 09:45:32 -0400241 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
Greg Daniel17b7c052018-01-09 13:55:33 -0500242 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400243 GrWrapOwnership = kBorrow_GrWrapOwnership);
Greg Danield85f97d2017-03-07 13:37:21 -0500244
Brian Osman32342f02017-03-04 08:12:46 -0500245 void abandon() {
Robert Phillips26c90e02017-03-14 14:39:29 -0400246 fCache = nullptr;
247 fGpu = nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500248 }
249
Brian Salomon238069b2018-07-11 15:58:57 -0400250 uint32_t contextUniqueID() const { return fCache->contextUniqueID(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400251 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500252 bool overBudget() const { return fCache->overBudget(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400253
Greg Daniel8982dc12018-01-05 12:56:15 -0500254 inline GrResourceProviderPriv priv();
255 inline const GrResourceProviderPriv priv() const;
256
Robert Phillips4150eea2018-02-07 17:08:21 -0500257 bool explicitlyAllocateGPUResources() const { return fExplicitlyAllocateGPUResources; }
258
259 bool testingOnly_setExplicitlyAllocateGPUResources(bool newValue);
260
bsalomoned0bcad2015-05-04 10:36:42 -0700261private:
Brian Salomond28a79d2017-10-16 13:01:07 -0400262 sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
Robert Phillips3798c862017-03-27 11:08:16 -0400263
Greg Daniel29bf84f2017-09-25 12:25:12 -0400264 // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
265 // it returns null. If non-null, the resulting texture is always budgeted.
Chris Daltond004e0b2018-09-27 09:28:03 -0600266 sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc&, Flags);
Brian Osman32342f02017-03-04 08:12:46 -0500267
Robert Phillips45fdae12017-04-17 12:57:27 -0400268 /*
269 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
270 * update the budgeting accordingly.
271 */
Chris Daltond004e0b2018-09-27 09:28:03 -0600272 sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&, SkBudgeted, Flags);
Robert Phillips45fdae12017-04-17 12:57:27 -0400273
Brian Osman32342f02017-03-04 08:12:46 -0500274 GrResourceCache* cache() { return fCache; }
275 const GrResourceCache* cache() const { return fCache; }
276
Greg Daniel8982dc12018-01-05 12:56:15 -0500277 friend class GrResourceProviderPriv;
278
279 // Method made available via GrResourceProviderPriv
Brian Osman32342f02017-03-04 08:12:46 -0500280 GrGpu* gpu() { return fGpu; }
281 const GrGpu* gpu() const { return fGpu; }
282
283 bool isAbandoned() const {
284 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
285 return !SkToBool(fCache);
286 }
287
Brian Salomond28a79d2017-10-16 13:01:07 -0400288 sk_sp<const GrBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
289 int patternSize,
290 int reps,
291 int vertCount,
292 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700293
Brian Salomond28a79d2017-10-16 13:01:07 -0400294 sk_sp<const GrBuffer> createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700295
Brian Salomon763abf02018-05-01 18:49:38 +0000296 GrResourceCache* fCache;
297 GrGpu* fGpu;
Robert Phillips26c90e02017-03-14 14:39:29 -0400298 sk_sp<const GrCaps> fCaps;
Brian Salomon763abf02018-05-01 18:49:38 +0000299 GrUniqueKey fQuadIndexBufferKey;
300 bool fExplicitlyAllocateGPUResources;
bsalomoned0bcad2015-05-04 10:36:42 -0700301
Brian Osman32342f02017-03-04 08:12:46 -0500302 // In debug builds we guard against improper thread handling
303 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700304};
305
Chris Daltond004e0b2018-09-27 09:28:03 -0600306GR_MAKE_BITFIELD_CLASS_OPS(GrResourceProvider::Flags);
307
bsalomond309e7a2015-04-30 14:18:54 -0700308#endif