blob: 2d4ef927fa1e4fd85148e80f1ee9b5239635832c [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
Robert Phillipsa3f70262018-02-08 10:59:38 -050011#include "GrContextOptions.h"
Brian Salomondbf70722019-02-07 11:31:24 -050012#include "GrGpuBuffer.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;
Greg Danielb46add82019-01-02 14:51:29 -050028struct GrVkDrawableInfo;
Robert Phillips646e4292017-06-13 12:44:56 -040029
bsalomon6663acf2016-05-10 09:14:17 -070030class GrStyle;
bsalomon706f08f2015-05-22 07:35:58 -070031class SkDescriptor;
32class SkPath;
33class SkTypeface;
bsalomoned0bcad2015-05-04 10:36:42 -070034
bsalomond309e7a2015-04-30 14:18:54 -070035/**
Brian Osman32342f02017-03-04 08:12:46 -050036 * A factory for arbitrary resource types. This class is intended for use within the Gr code base.
bsalomoneae62002015-07-31 13:59:30 -070037 *
Brian Osman32342f02017-03-04 08:12:46 -050038 * Some members force callers to make a flags (pendingIO) decision. This can be relaxed once
39 * https://bug.skia.org/4156 is fixed.
bsalomond309e7a2015-04-30 14:18:54 -070040 */
Brian Osman32342f02017-03-04 08:12:46 -050041class GrResourceProvider {
bsalomond309e7a2015-04-30 14:18:54 -070042public:
Chris Daltond004e0b2018-09-27 09:28:03 -060043 /** These flags govern which scratch resources we are allowed to return */
44 enum class Flags {
45 kNone = 0x0,
46
47 /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
48 * set when accessing resources during a GrOpList flush. This includes the execution of
49 * GrOp objects. The reason is that these memory operations are done immediately and
50 * will occur out of order WRT the operations being flushed.
51 * Make this automatic: https://bug.skia.org/4156
52 */
53 kNoPendingIO = 0x1,
Chris Daltond004e0b2018-09-27 09:28:03 -060054 };
55
Robert Phillips12c46292019-04-23 07:36:17 -040056 GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*);
bsalomoned0bcad2015-05-04 10:36:42 -070057
Brian Salomond28a79d2017-10-16 13:01:07 -040058 /**
59 * Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
60 * must be sure that if a resource of exists in the cache with the given unique key then it is
61 * of type T.
62 */
Brian Salomondbf70722019-02-07 11:31:24 -050063 template <typename T = GrGpuResource>
64 typename std::enable_if<std::is_base_of<GrGpuResource, T>::value, sk_sp<T>>::type
65 findByUniqueKey(const GrUniqueKey& key) {
Brian Salomond28a79d2017-10-16 13:01:07 -040066 return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
bsalomoned0bcad2015-05-04 10:36:42 -070067 }
68
Brian Osman32342f02017-03-04 08:12:46 -050069 ///////////////////////////////////////////////////////////////////////////
70 // Textures
71
Brian Osman32342f02017-03-04 08:12:46 -050072 /**
73 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
74 * and height as desc specifies. If desc specifies that the texture should be a render target
75 * then result will be a render target. Format and sample count will always match the request.
Robert Phillips67d52cf2017-06-05 13:38:13 -040076 * The contents of the texture are undefined.
Brian Osman32342f02017-03-04 08:12:46 -050077 */
Chris Daltond004e0b2018-09-27 09:28:03 -060078 sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc&, Flags);
Brian Osman32342f02017-03-04 08:12:46 -050079
Robert Phillipse78b7252017-04-06 07:59:41 -040080 /** Create an exact fit texture with no initial data to upload.
81 */
Chris Daltond004e0b2018-09-27 09:28:03 -060082 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, Flags = Flags::kNone);
Robert Phillips45fdae12017-04-17 12:57:27 -040083
Brian Salomon58389b92018-03-07 13:01:25 -050084 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel texels[],
Brian Osman2b23c4b2018-06-01 12:25:08 -040085 int mipLevelCount);
Robert Phillips8e8c7552017-07-10 12:06:05 -040086
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087 // Create a potentially loose fit texture with the provided data
Greg Danielfb3abcd2018-02-02 15:48:33 -050088 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, SkBackingFit,
Chris Daltond004e0b2018-09-27 09:28:03 -060089 const GrMipLevel&, Flags);
Robert Phillipse78b7252017-04-06 07:59:41 -040090
Brian Osman32342f02017-03-04 08:12:46 -050091 ///////////////////////////////////////////////////////////////////////////
92 // Wrapped Backend Surfaces
93
94 /**
95 * Wraps an existing texture with a GrTexture object.
96 *
Brian Salomonc67c31c2018-12-06 10:00:03 -050097 * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the
98 * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels).
99 *
Brian Osman32342f02017-03-04 08:12:46 -0500100 * OpenGL: if the object is a texture Gr may change its GL texture params
101 * when it is drawn.
102 *
103 * @return GrTexture object or NULL on failure.
104 */
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500105 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex, GrWrapOwnership,
106 GrWrapCacheable, GrIOType);
Brian Osman32342f02017-03-04 08:12:46 -0500107
108 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -0500109 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
Brian Salomond17f6582017-07-19 18:28:58 -0400110 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
111 * to the texture.
112 */
113 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400114 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500115 GrWrapOwnership,
116 GrWrapCacheable);
Brian Salomond17f6582017-07-19 18:28:58 -0400117
118 /**
Brian Osman32342f02017-03-04 08:12:46 -0500119 * Wraps an existing render target with a GrRenderTarget object. It is
120 * similar to wrapBackendTexture but can be used to draw into surfaces
121 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
122 * the client will resolve to a texture). Currently wrapped render targets
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500123 * always use the kBorrow_GrWrapOwnership and GrWrapCacheable::kNo semantics.
Brian Osman32342f02017-03-04 08:12:46 -0500124 *
125 * @return GrRenderTarget object or NULL on failure.
126 */
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400127 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
Brian Osman32342f02017-03-04 08:12:46 -0500128
Greg Danielb46add82019-01-02 14:51:29 -0500129 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
130 const GrVkDrawableInfo&);
131
Robert Phillips1bfece82017-06-01 13:56:52 -0400132 static const uint32_t kMinScratchTextureSize;
Brian Osman32342f02017-03-04 08:12:46 -0500133
bsalomoned0bcad2015-05-04 10:36:42 -0700134 /**
Chris Dalton5d2de082017-12-19 10:40:23 -0700135 * Either finds and refs, or creates a static buffer with the given parameters and contents.
136 *
137 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
138 * @param size minimum size of buffer to return.
139 * @param data optional data with which to initialize the buffer.
140 * @param key Key to be assigned to the buffer.
141 *
142 * @return The buffer if successful, otherwise nullptr.
143 */
Brian Salomondbf70722019-02-07 11:31:24 -0500144 sk_sp<const GrGpuBuffer> findOrMakeStaticBuffer(GrGpuBufferType intendedType, size_t size,
145 const void* data, const GrUniqueKey& key);
Chris Dalton5d2de082017-12-19 10:40:23 -0700146
147 /**
Chris Daltonff926502017-05-03 14:36:54 -0400148 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
149 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
150 * the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -0700151 *
152 * @param pattern the pattern of indices to repeat
153 * @param patternSize size in bytes of the pattern
154 * @param reps number of times to repeat the pattern
155 * @param vertCount number of vertices the pattern references
156 * @param key Key to be assigned to the index buffer.
157 *
halcanary96fcdcc2015-08-27 07:41:13 -0700158 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -0700159 */
Brian Salomondbf70722019-02-07 11:31:24 -0500160 sk_sp<const GrGpuBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
161 int patternSize,
162 int reps,
163 int vertCount,
164 const GrUniqueKey& key) {
Brian Salomona29dd9d2019-02-07 13:27:18 -0500165 if (auto buffer = this->findByUniqueKey<const GrGpuBuffer>(key)) {
166 return buffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700167 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500168 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, &key);
bsalomoned0bcad2015-05-04 10:36:42 -0700169 }
170
171 /**
172 * Returns an index buffer that can be used to render quads.
Brian Salomon57caa662017-10-18 12:21:05 +0000173 * Six indices per quad: 0, 1, 2, 2, 1, 3, etc.
Brian Salomon763abf02018-05-01 18:49:38 +0000174 * The max number of quads is the buffer's index capacity divided by 6.
Chris Dalton3809bab2017-06-13 10:55:06 -0600175 * Draw with GrPrimitiveType::kTriangles
Brian Salomon763abf02018-05-01 18:49:38 +0000176 * @ return the quad index buffer
bsalomoned0bcad2015-05-04 10:36:42 -0700177 */
Brian Salomondbf70722019-02-07 11:31:24 -0500178 sk_sp<const GrGpuBuffer> refQuadIndexBuffer() {
Brian Salomona29dd9d2019-02-07 13:27:18 -0500179 if (!fQuadIndexBuffer) {
180 fQuadIndexBuffer = this->createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700181 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500182 return fQuadIndexBuffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700183 }
184
Brian Salomon34169692017-08-28 15:32:01 -0400185 static int QuadCountOfQuadBuffer();
186
bsalomon706f08f2015-05-22 07:35:58 -0700187 /**
Ben Wagner3746ac22018-03-29 11:46:24 -0400188 * Factories for GrPath objects. It's an error to call these if path rendering
bsalomon706f08f2015-05-22 07:35:58 -0700189 * is not supported.
190 */
Robert Phillips67d52cf2017-06-05 13:38:13 -0400191 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -0700192
cdaltone2e71c22016-04-07 18:13:29 -0700193 /**
194 * Returns a buffer.
195 *
196 * @param size minimum size of buffer to return.
197 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
198 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
199 * @param flags see Flags enum.
cdalton1bf3e712016-04-19 10:00:02 -0700200 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700201 *
202 * @return the buffer if successful, otherwise nullptr.
203 */
Brian Salomondbf70722019-02-07 11:31:24 -0500204 sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType, GrAccessPattern,
205 const void* data = nullptr);
robertphillips1b8e1b52015-06-24 06:54:10 -0700206
Brian Salomon2ee084e2016-12-16 18:59:19 -0500207 /**
Robert Phillipsc0192e32017-09-21 12:00:26 -0400208 * If passed in render target already has a stencil buffer, return true. Otherwise attempt to
209 * attach one and return true on success.
egdanielec00d942015-09-14 12:56:10 -0700210 */
Robert Phillipsc0192e32017-09-21 12:00:26 -0400211 bool attachStencilAttachment(GrRenderTarget* rt);
egdanielec00d942015-09-14 12:56:10 -0700212
ericrkf7b8b8a2016-02-24 14:49:51 -0800213 /**
214 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
215 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
216 *
kkinnunen49c4c222016-04-01 04:50:37 -0700217 * The texture is wrapped as borrowed. The texture object will not be freed once the
218 * render target is destroyed.
219 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800220 * @return GrRenderTarget object or NULL on failure.
221 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000222 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000223 int sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800224
Brian Osman32342f02017-03-04 08:12:46 -0500225 /**
226 * Assigns a unique key to a resource. If the key is associated with another resource that
227 * association is removed and replaced by this resource.
228 */
229 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
230
Greg Daniela5cb7812017-06-16 09:45:32 -0400231 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
232
Greg Daniel17b7c052018-01-09 13:55:33 -0500233 enum class SemaphoreWrapType {
234 kWillSignal,
235 kWillWait,
236 };
237
Greg Daniela5cb7812017-06-16 09:45:32 -0400238 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
Greg Daniel17b7c052018-01-09 13:55:33 -0500239 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400240 GrWrapOwnership = kBorrow_GrWrapOwnership);
Greg Danield85f97d2017-03-07 13:37:21 -0500241
Brian Osman32342f02017-03-04 08:12:46 -0500242 void abandon() {
Robert Phillips26c90e02017-03-14 14:39:29 -0400243 fCache = nullptr;
244 fGpu = nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500245 }
246
Brian Salomon238069b2018-07-11 15:58:57 -0400247 uint32_t contextUniqueID() const { return fCache->contextUniqueID(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400248 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500249 bool overBudget() const { return fCache->overBudget(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400250
Greg Daniel8982dc12018-01-05 12:56:15 -0500251 inline GrResourceProviderPriv priv();
252 inline const GrResourceProviderPriv priv() const;
253
bsalomoned0bcad2015-05-04 10:36:42 -0700254private:
Brian Salomond28a79d2017-10-16 13:01:07 -0400255 sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
Robert Phillips3798c862017-03-27 11:08:16 -0400256
Greg Daniel29bf84f2017-09-25 12:25:12 -0400257 // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
258 // it returns null. If non-null, the resulting texture is always budgeted.
Chris Daltond004e0b2018-09-27 09:28:03 -0600259 sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc&, Flags);
Brian Osman32342f02017-03-04 08:12:46 -0500260
Robert Phillips45fdae12017-04-17 12:57:27 -0400261 /*
262 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
263 * update the budgeting accordingly.
264 */
Chris Daltond004e0b2018-09-27 09:28:03 -0600265 sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&, SkBudgeted, Flags);
Robert Phillips45fdae12017-04-17 12:57:27 -0400266
Brian Osman32342f02017-03-04 08:12:46 -0500267 GrResourceCache* cache() { return fCache; }
268 const GrResourceCache* cache() const { return fCache; }
269
Greg Daniel8982dc12018-01-05 12:56:15 -0500270 friend class GrResourceProviderPriv;
271
272 // Method made available via GrResourceProviderPriv
Brian Osman32342f02017-03-04 08:12:46 -0500273 GrGpu* gpu() { return fGpu; }
274 const GrGpu* gpu() const { return fGpu; }
275
276 bool isAbandoned() const {
277 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
278 return !SkToBool(fCache);
279 }
280
Brian Salomondbf70722019-02-07 11:31:24 -0500281 sk_sp<const GrGpuBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
282 int patternSize,
283 int reps,
284 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500285 const GrUniqueKey* key);
bsalomoned0bcad2015-05-04 10:36:42 -0700286
Brian Salomondbf70722019-02-07 11:31:24 -0500287 sk_sp<const GrGpuBuffer> createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700288
Brian Salomona29dd9d2019-02-07 13:27:18 -0500289 GrResourceCache* fCache;
290 GrGpu* fGpu;
Robert Phillips26c90e02017-03-14 14:39:29 -0400291 sk_sp<const GrCaps> fCaps;
Brian Salomona29dd9d2019-02-07 13:27:18 -0500292 sk_sp<const GrGpuBuffer> fQuadIndexBuffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700293
Brian Osman32342f02017-03-04 08:12:46 -0500294 // In debug builds we guard against improper thread handling
295 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700296};
297
Chris Daltond004e0b2018-09-27 09:28:03 -0600298GR_MAKE_BITFIELD_CLASS_OPS(GrResourceProvider::Flags);
299
bsalomond309e7a2015-04-30 14:18:54 -0700300#endif