blob: c6bcef243ca5ca107c36ea14c668b8e54c126e78 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContextOptions.h"
12#include "include/private/SkImageInfoPriv.h"
13#include "src/core/SkScalerContext.h"
14#include "src/gpu/GrGpuBuffer.h"
15#include "src/gpu/GrResourceCache.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
Brian Salomonf2c2ba92019-07-17 09:59:59 -040074 * and height as desc specifies. If renderable is kYes then the GrTexture will also be a
75 * GrRenderTarget. The texture's 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 */
Brian Salomon4eb38b72019-08-05 12:58:39 -040078 sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc& desc,
79 const GrBackendFormat& format,
80 GrRenderable renderable,
81 int renderTargetSampleCnt,
82 GrProtected isProtected,
83 Flags flags);
Brian Osman32342f02017-03-04 08:12:46 -050084
Brian Salomonf2c2ba92019-07-17 09:59:59 -040085 /** Create an exact fit texture with no initial data to upload. */
Brian Salomon4eb38b72019-08-05 12:58:39 -040086 sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc,
87 const GrBackendFormat& format,
88 GrRenderable renderable,
89 int renderTargetSampleCnt,
90 SkBudgeted budgeted,
91 GrProtected isProtected,
92 Flags flags = Flags::kNone);
Brian Salomonf2c2ba92019-07-17 09:59:59 -040093
Brian Salomon4eb38b72019-08-05 12:58:39 -040094 sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc,
95 const GrBackendFormat& format,
96 GrRenderable renderable,
97 int renderTargetSampleCnt,
98 SkBudgeted budgeted,
99 GrProtected isProtected,
100 const GrMipLevel texels[],
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400101 int mipLevelCount);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400102
103 /** Create a potentially loose fit texture with the provided data */
Brian Salomon4eb38b72019-08-05 12:58:39 -0400104 sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc,
105 const GrBackendFormat& format,
106 GrRenderable renderable,
107 int renderTargetSampleCnt,
108 SkBudgeted budgeted,
109 SkBackingFit fit,
110 GrProtected isProtected,
Brian Salomon2af3e702019-08-11 19:10:31 -0400111 GrColorType srcColorType,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400112 const GrMipLevel& mipLevel,
113 Flags flags);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400114
115 /**
116 * Creates a compressed texture. The GrGpu must support the SkImageImage::Compression type.
117 * This does not currently support MIP maps. It will not be renderable.
Robert Phillipse78b7252017-04-06 07:59:41 -0400118 */
Brian Salomonbb8dde82019-06-27 10:52:13 -0400119 sk_sp<GrTexture> createCompressedTexture(int width, int height, SkImage::CompressionType,
120 SkBudgeted, SkData* data);
121
Brian Osman32342f02017-03-04 08:12:46 -0500122 ///////////////////////////////////////////////////////////////////////////
123 // Wrapped Backend Surfaces
124
125 /**
126 * Wraps an existing texture with a GrTexture object.
127 *
Brian Salomonc67c31c2018-12-06 10:00:03 -0500128 * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the
129 * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels).
130 *
Brian Osman32342f02017-03-04 08:12:46 -0500131 * OpenGL: if the object is a texture Gr may change its GL texture params
132 * when it is drawn.
133 *
134 * @return GrTexture object or NULL on failure.
135 */
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400136 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex, GrColorType, GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500137 GrWrapCacheable, GrIOType);
Brian Osman32342f02017-03-04 08:12:46 -0500138
139 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -0500140 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
Brian Salomond17f6582017-07-19 18:28:58 -0400141 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
142 * to the texture.
143 */
144 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400145 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400146 GrColorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500147 GrWrapOwnership,
148 GrWrapCacheable);
Brian Salomond17f6582017-07-19 18:28:58 -0400149
150 /**
Brian Osman32342f02017-03-04 08:12:46 -0500151 * Wraps an existing render target with a GrRenderTarget object. It is
152 * similar to wrapBackendTexture but can be used to draw into surfaces
153 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
154 * the client will resolve to a texture). Currently wrapped render targets
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500155 * always use the kBorrow_GrWrapOwnership and GrWrapCacheable::kNo semantics.
Brian Osman32342f02017-03-04 08:12:46 -0500156 *
157 * @return GrRenderTarget object or NULL on failure.
158 */
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400159 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&,
160 GrColorType colorType);
Brian Osman32342f02017-03-04 08:12:46 -0500161
Greg Danielb46add82019-01-02 14:51:29 -0500162 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
163 const GrVkDrawableInfo&);
164
Robert Phillips1bfece82017-06-01 13:56:52 -0400165 static const uint32_t kMinScratchTextureSize;
Brian Osman32342f02017-03-04 08:12:46 -0500166
bsalomoned0bcad2015-05-04 10:36:42 -0700167 /**
Chris Dalton5d2de082017-12-19 10:40:23 -0700168 * Either finds and refs, or creates a static buffer with the given parameters and contents.
169 *
170 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
171 * @param size minimum size of buffer to return.
172 * @param data optional data with which to initialize the buffer.
173 * @param key Key to be assigned to the buffer.
174 *
175 * @return The buffer if successful, otherwise nullptr.
176 */
Brian Salomondbf70722019-02-07 11:31:24 -0500177 sk_sp<const GrGpuBuffer> findOrMakeStaticBuffer(GrGpuBufferType intendedType, size_t size,
178 const void* data, const GrUniqueKey& key);
Chris Dalton5d2de082017-12-19 10:40:23 -0700179
180 /**
Chris Daltonff926502017-05-03 14:36:54 -0400181 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
182 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
183 * the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -0700184 *
185 * @param pattern the pattern of indices to repeat
186 * @param patternSize size in bytes of the pattern
187 * @param reps number of times to repeat the pattern
188 * @param vertCount number of vertices the pattern references
189 * @param key Key to be assigned to the index buffer.
190 *
halcanary96fcdcc2015-08-27 07:41:13 -0700191 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -0700192 */
Brian Salomondbf70722019-02-07 11:31:24 -0500193 sk_sp<const GrGpuBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
194 int patternSize,
195 int reps,
196 int vertCount,
197 const GrUniqueKey& key) {
Brian Salomona29dd9d2019-02-07 13:27:18 -0500198 if (auto buffer = this->findByUniqueKey<const GrGpuBuffer>(key)) {
199 return buffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700200 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500201 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, &key);
bsalomoned0bcad2015-05-04 10:36:42 -0700202 }
203
204 /**
205 * Returns an index buffer that can be used to render quads.
Brian Salomon57caa662017-10-18 12:21:05 +0000206 * Six indices per quad: 0, 1, 2, 2, 1, 3, etc.
Brian Salomon763abf02018-05-01 18:49:38 +0000207 * The max number of quads is the buffer's index capacity divided by 6.
Chris Dalton3809bab2017-06-13 10:55:06 -0600208 * Draw with GrPrimitiveType::kTriangles
Brian Salomon763abf02018-05-01 18:49:38 +0000209 * @ return the quad index buffer
bsalomoned0bcad2015-05-04 10:36:42 -0700210 */
Brian Salomondbf70722019-02-07 11:31:24 -0500211 sk_sp<const GrGpuBuffer> refQuadIndexBuffer() {
Brian Salomona29dd9d2019-02-07 13:27:18 -0500212 if (!fQuadIndexBuffer) {
213 fQuadIndexBuffer = this->createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700214 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500215 return fQuadIndexBuffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700216 }
217
Brian Salomon34169692017-08-28 15:32:01 -0400218 static int QuadCountOfQuadBuffer();
219
bsalomon706f08f2015-05-22 07:35:58 -0700220 /**
Ben Wagner3746ac22018-03-29 11:46:24 -0400221 * Factories for GrPath objects. It's an error to call these if path rendering
bsalomon706f08f2015-05-22 07:35:58 -0700222 * is not supported.
223 */
Robert Phillips67d52cf2017-06-05 13:38:13 -0400224 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -0700225
cdaltone2e71c22016-04-07 18:13:29 -0700226 /**
227 * Returns a buffer.
228 *
229 * @param size minimum size of buffer to return.
230 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
231 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
232 * @param flags see Flags enum.
cdalton1bf3e712016-04-19 10:00:02 -0700233 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700234 *
235 * @return the buffer if successful, otherwise nullptr.
236 */
Brian Salomondbf70722019-02-07 11:31:24 -0500237 sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType, GrAccessPattern,
238 const void* data = nullptr);
robertphillips1b8e1b52015-06-24 06:54:10 -0700239
Brian Salomon2ee084e2016-12-16 18:59:19 -0500240 /**
Chris Daltoneffee202019-07-01 22:28:03 -0600241 * If passed in render target already has a stencil buffer with at least "numSamples" samples,
242 * return true. Otherwise attempt to attach one and return true on success.
egdanielec00d942015-09-14 12:56:10 -0700243 */
Chris Daltoneffee202019-07-01 22:28:03 -0600244 bool attachStencilAttachment(GrRenderTarget* rt, int numStencilSamples);
egdanielec00d942015-09-14 12:56:10 -0700245
ericrkf7b8b8a2016-02-24 14:49:51 -0800246 /**
247 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
248 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
249 *
kkinnunen49c4c222016-04-01 04:50:37 -0700250 * The texture is wrapped as borrowed. The texture object will not be freed once the
251 * render target is destroyed.
252 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800253 * @return GrRenderTarget object or NULL on failure.
254 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000255 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400256 int sampleCnt,
257 GrColorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800258
Brian Osman32342f02017-03-04 08:12:46 -0500259 /**
260 * Assigns a unique key to a resource. If the key is associated with another resource that
261 * association is removed and replaced by this resource.
262 */
263 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
264
Greg Daniela5cb7812017-06-16 09:45:32 -0400265 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
266
Greg Daniel17b7c052018-01-09 13:55:33 -0500267 enum class SemaphoreWrapType {
268 kWillSignal,
269 kWillWait,
270 };
271
Greg Daniela5cb7812017-06-16 09:45:32 -0400272 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
Greg Daniel17b7c052018-01-09 13:55:33 -0500273 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400274 GrWrapOwnership = kBorrow_GrWrapOwnership);
Greg Danield85f97d2017-03-07 13:37:21 -0500275
Brian Osman32342f02017-03-04 08:12:46 -0500276 void abandon() {
Robert Phillips26c90e02017-03-14 14:39:29 -0400277 fCache = nullptr;
278 fGpu = nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500279 }
280
Brian Salomon238069b2018-07-11 15:58:57 -0400281 uint32_t contextUniqueID() const { return fCache->contextUniqueID(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400282 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500283 bool overBudget() const { return fCache->overBudget(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400284
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400285 static uint32_t MakeApprox(uint32_t value);
286
Greg Daniel8982dc12018-01-05 12:56:15 -0500287 inline GrResourceProviderPriv priv();
288 inline const GrResourceProviderPriv priv() const;
289
bsalomoned0bcad2015-05-04 10:36:42 -0700290private:
Brian Salomond28a79d2017-10-16 13:01:07 -0400291 sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
Robert Phillips3798c862017-03-27 11:08:16 -0400292
Greg Daniel29bf84f2017-09-25 12:25:12 -0400293 // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
294 // it returns null. If non-null, the resulting texture is always budgeted.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400295 sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc& desc,
296 const GrBackendFormat& format,
297 GrRenderable renderable,
298 int renderTargetSampleCnt,
299 GrProtected isProtected,
300 Flags flags);
Brian Osman32342f02017-03-04 08:12:46 -0500301
Robert Phillips45fdae12017-04-17 12:57:27 -0400302 /*
303 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
304 * update the budgeting accordingly.
305 */
Brian Salomon4eb38b72019-08-05 12:58:39 -0400306 sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc& desc,
307 const GrBackendFormat& format,
308 GrRenderable renderable,
309 int renderTargetSampleCnt,
310 SkBudgeted budgeted,
311 GrProtected isProtected,
312 Flags flags);
Robert Phillips45fdae12017-04-17 12:57:27 -0400313
Brian Osman32342f02017-03-04 08:12:46 -0500314 GrResourceCache* cache() { return fCache; }
315 const GrResourceCache* cache() const { return fCache; }
316
Greg Daniel8982dc12018-01-05 12:56:15 -0500317 friend class GrResourceProviderPriv;
318
319 // Method made available via GrResourceProviderPriv
Brian Osman32342f02017-03-04 08:12:46 -0500320 GrGpu* gpu() { return fGpu; }
321 const GrGpu* gpu() const { return fGpu; }
322
323 bool isAbandoned() const {
324 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
325 return !SkToBool(fCache);
326 }
327
Brian Salomondbf70722019-02-07 11:31:24 -0500328 sk_sp<const GrGpuBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
329 int patternSize,
330 int reps,
331 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500332 const GrUniqueKey* key);
bsalomoned0bcad2015-05-04 10:36:42 -0700333
Brian Salomondbf70722019-02-07 11:31:24 -0500334 sk_sp<const GrGpuBuffer> createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700335
Brian Salomona29dd9d2019-02-07 13:27:18 -0500336 GrResourceCache* fCache;
337 GrGpu* fGpu;
Robert Phillips26c90e02017-03-14 14:39:29 -0400338 sk_sp<const GrCaps> fCaps;
Brian Salomona29dd9d2019-02-07 13:27:18 -0500339 sk_sp<const GrGpuBuffer> fQuadIndexBuffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700340
Brian Osman32342f02017-03-04 08:12:46 -0500341 // In debug builds we guard against improper thread handling
342 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700343};
344
Chris Daltond004e0b2018-09-27 09:28:03 -0600345GR_MAKE_BITFIELD_CLASS_OPS(GrResourceProvider::Flags);
346
bsalomond309e7a2015-04-30 14:18:54 -0700347#endif