blob: 7d77bbf492c7c8ddb6f13fe7f771edc7831f9ea9 [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/**
Robert Phillipsaee18c92019-09-06 11:48:27 -040036 * A factory for arbitrary resource types.
bsalomond309e7a2015-04-30 14:18:54 -070037 */
Brian Osman32342f02017-03-04 08:12:46 -050038class GrResourceProvider {
bsalomond309e7a2015-04-30 14:18:54 -070039public:
Robert Phillips12c46292019-04-23 07:36:17 -040040 GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*);
bsalomoned0bcad2015-05-04 10:36:42 -070041
Brian Salomond28a79d2017-10-16 13:01:07 -040042 /**
43 * Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
44 * must be sure that if a resource of exists in the cache with the given unique key then it is
45 * of type T.
46 */
Brian Salomondbf70722019-02-07 11:31:24 -050047 template <typename T = GrGpuResource>
48 typename std::enable_if<std::is_base_of<GrGpuResource, T>::value, sk_sp<T>>::type
49 findByUniqueKey(const GrUniqueKey& key) {
Brian Salomond28a79d2017-10-16 13:01:07 -040050 return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
bsalomoned0bcad2015-05-04 10:36:42 -070051 }
52
Brian Osman32342f02017-03-04 08:12:46 -050053 ///////////////////////////////////////////////////////////////////////////
54 // Textures
55
Brian Osman32342f02017-03-04 08:12:46 -050056 /**
57 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
Brian Salomonf2c2ba92019-07-17 09:59:59 -040058 * and height as desc specifies. If renderable is kYes then the GrTexture will also be a
59 * GrRenderTarget. The texture's format and sample count will always match the request.
Robert Phillips67d52cf2017-06-05 13:38:13 -040060 * The contents of the texture are undefined.
Brian Osman32342f02017-03-04 08:12:46 -050061 */
Brian Salomon4eb38b72019-08-05 12:58:39 -040062 sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc& desc,
63 const GrBackendFormat& format,
64 GrRenderable renderable,
65 int renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -040066 GrProtected isProtected);
Brian Osman32342f02017-03-04 08:12:46 -050067
Brian Salomonf2c2ba92019-07-17 09:59:59 -040068 /** Create an exact fit texture with no initial data to upload. */
Brian Salomon4eb38b72019-08-05 12:58:39 -040069 sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc,
70 const GrBackendFormat& format,
71 GrRenderable renderable,
72 int renderTargetSampleCnt,
73 SkBudgeted budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -040074 GrProtected isProtected);
Brian Salomonf2c2ba92019-07-17 09:59:59 -040075
Brian Salomon4eb38b72019-08-05 12:58:39 -040076 sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc,
77 const GrBackendFormat& format,
78 GrRenderable renderable,
79 int renderTargetSampleCnt,
80 SkBudgeted budgeted,
81 GrProtected isProtected,
82 const GrMipLevel texels[],
Brian Salomon27b4d8d2019-07-22 14:23:45 -040083 int mipLevelCount);
Brian Salomonf2c2ba92019-07-17 09:59:59 -040084
85 /** Create a potentially loose fit texture with the provided data */
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 SkBackingFit fit,
92 GrProtected isProtected,
Brian Salomon2af3e702019-08-11 19:10:31 -040093 GrColorType srcColorType,
Robert Phillipsaee18c92019-09-06 11:48:27 -040094 const GrMipLevel& mipLevel);
Brian Salomonf2c2ba92019-07-17 09:59:59 -040095
96 /**
97 * Creates a compressed texture. The GrGpu must support the SkImageImage::Compression type.
98 * This does not currently support MIP maps. It will not be renderable.
Robert Phillipse78b7252017-04-06 07:59:41 -040099 */
Greg Daniel7bfc9132019-08-14 14:23:53 -0400100 sk_sp<GrTexture> createCompressedTexture(int width, int height, const GrBackendFormat&,
101 SkImage::CompressionType, SkBudgeted, SkData* data);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400102
Brian Osman32342f02017-03-04 08:12:46 -0500103 ///////////////////////////////////////////////////////////////////////////
104 // Wrapped Backend Surfaces
105
106 /**
107 * Wraps an existing texture with a GrTexture object.
108 *
Brian Salomonc67c31c2018-12-06 10:00:03 -0500109 * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the
110 * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels).
111 *
Brian Osman32342f02017-03-04 08:12:46 -0500112 * OpenGL: if the object is a texture Gr may change its GL texture params
113 * when it is drawn.
114 *
115 * @return GrTexture object or NULL on failure.
116 */
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400117 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex, GrColorType, GrWrapOwnership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500118 GrWrapCacheable, GrIOType);
Brian Osman32342f02017-03-04 08:12:46 -0500119
120 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -0500121 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
Brian Salomond17f6582017-07-19 18:28:58 -0400122 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
123 * to the texture.
124 */
125 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400126 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400127 GrColorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500128 GrWrapOwnership,
129 GrWrapCacheable);
Brian Salomond17f6582017-07-19 18:28:58 -0400130
131 /**
Brian Osman32342f02017-03-04 08:12:46 -0500132 * Wraps an existing render target with a GrRenderTarget object. It is
133 * similar to wrapBackendTexture but can be used to draw into surfaces
134 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
135 * the client will resolve to a texture). Currently wrapped render targets
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500136 * always use the kBorrow_GrWrapOwnership and GrWrapCacheable::kNo semantics.
Brian Osman32342f02017-03-04 08:12:46 -0500137 *
138 * @return GrRenderTarget object or NULL on failure.
139 */
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400140 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&,
141 GrColorType colorType);
Brian Osman32342f02017-03-04 08:12:46 -0500142
Greg Danielb46add82019-01-02 14:51:29 -0500143 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
144 const GrVkDrawableInfo&);
145
Robert Phillips1bfece82017-06-01 13:56:52 -0400146 static const uint32_t kMinScratchTextureSize;
Brian Osman32342f02017-03-04 08:12:46 -0500147
bsalomoned0bcad2015-05-04 10:36:42 -0700148 /**
Chris Dalton5d2de082017-12-19 10:40:23 -0700149 * Either finds and refs, or creates a static buffer with the given parameters and contents.
150 *
151 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
152 * @param size minimum size of buffer to return.
153 * @param data optional data with which to initialize the buffer.
154 * @param key Key to be assigned to the buffer.
155 *
156 * @return The buffer if successful, otherwise nullptr.
157 */
Brian Salomondbf70722019-02-07 11:31:24 -0500158 sk_sp<const GrGpuBuffer> findOrMakeStaticBuffer(GrGpuBufferType intendedType, size_t size,
159 const void* data, const GrUniqueKey& key);
Chris Dalton5d2de082017-12-19 10:40:23 -0700160
161 /**
Chris Daltonff926502017-05-03 14:36:54 -0400162 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
163 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
164 * the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -0700165 *
166 * @param pattern the pattern of indices to repeat
167 * @param patternSize size in bytes of the pattern
168 * @param reps number of times to repeat the pattern
169 * @param vertCount number of vertices the pattern references
170 * @param key Key to be assigned to the index buffer.
171 *
halcanary96fcdcc2015-08-27 07:41:13 -0700172 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -0700173 */
Brian Salomondbf70722019-02-07 11:31:24 -0500174 sk_sp<const GrGpuBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
175 int patternSize,
176 int reps,
177 int vertCount,
178 const GrUniqueKey& key) {
Brian Salomona29dd9d2019-02-07 13:27:18 -0500179 if (auto buffer = this->findByUniqueKey<const GrGpuBuffer>(key)) {
180 return buffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700181 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500182 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, &key);
bsalomoned0bcad2015-05-04 10:36:42 -0700183 }
184
185 /**
186 * Returns an index buffer that can be used to render quads.
Brian Salomon57caa662017-10-18 12:21:05 +0000187 * Six indices per quad: 0, 1, 2, 2, 1, 3, etc.
Brian Salomon763abf02018-05-01 18:49:38 +0000188 * The max number of quads is the buffer's index capacity divided by 6.
Chris Dalton3809bab2017-06-13 10:55:06 -0600189 * Draw with GrPrimitiveType::kTriangles
Brian Salomon763abf02018-05-01 18:49:38 +0000190 * @ return the quad index buffer
bsalomoned0bcad2015-05-04 10:36:42 -0700191 */
Brian Salomondbf70722019-02-07 11:31:24 -0500192 sk_sp<const GrGpuBuffer> refQuadIndexBuffer() {
Brian Salomona29dd9d2019-02-07 13:27:18 -0500193 if (!fQuadIndexBuffer) {
194 fQuadIndexBuffer = this->createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700195 }
Brian Salomona29dd9d2019-02-07 13:27:18 -0500196 return fQuadIndexBuffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700197 }
198
Brian Salomon34169692017-08-28 15:32:01 -0400199 static int QuadCountOfQuadBuffer();
200
bsalomon706f08f2015-05-22 07:35:58 -0700201 /**
Ben Wagner3746ac22018-03-29 11:46:24 -0400202 * Factories for GrPath objects. It's an error to call these if path rendering
bsalomon706f08f2015-05-22 07:35:58 -0700203 * is not supported.
204 */
Robert Phillips67d52cf2017-06-05 13:38:13 -0400205 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -0700206
cdaltone2e71c22016-04-07 18:13:29 -0700207 /**
208 * Returns a buffer.
209 *
210 * @param size minimum size of buffer to return.
211 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
212 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
213 * @param flags see Flags enum.
cdalton1bf3e712016-04-19 10:00:02 -0700214 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700215 *
216 * @return the buffer if successful, otherwise nullptr.
217 */
Brian Salomondbf70722019-02-07 11:31:24 -0500218 sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType, GrAccessPattern,
219 const void* data = nullptr);
robertphillips1b8e1b52015-06-24 06:54:10 -0700220
Brian Salomon2ee084e2016-12-16 18:59:19 -0500221 /**
Chris Daltoneffee202019-07-01 22:28:03 -0600222 * If passed in render target already has a stencil buffer with at least "numSamples" samples,
223 * return true. Otherwise attempt to attach one and return true on success.
egdanielec00d942015-09-14 12:56:10 -0700224 */
Chris Daltoneffee202019-07-01 22:28:03 -0600225 bool attachStencilAttachment(GrRenderTarget* rt, int numStencilSamples);
egdanielec00d942015-09-14 12:56:10 -0700226
ericrkf7b8b8a2016-02-24 14:49:51 -0800227 /**
228 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
229 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
230 *
kkinnunen49c4c222016-04-01 04:50:37 -0700231 * The texture is wrapped as borrowed. The texture object will not be freed once the
232 * render target is destroyed.
233 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800234 * @return GrRenderTarget object or NULL on failure.
235 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000236 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400237 int sampleCnt,
238 GrColorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800239
Brian Osman32342f02017-03-04 08:12:46 -0500240 /**
241 * Assigns a unique key to a resource. If the key is associated with another resource that
242 * association is removed and replaced by this resource.
243 */
244 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
245
Greg Daniela5cb7812017-06-16 09:45:32 -0400246 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
247
Greg Daniel17b7c052018-01-09 13:55:33 -0500248 enum class SemaphoreWrapType {
249 kWillSignal,
250 kWillWait,
251 };
252
Greg Daniela5cb7812017-06-16 09:45:32 -0400253 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
Greg Daniel17b7c052018-01-09 13:55:33 -0500254 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400255 GrWrapOwnership = kBorrow_GrWrapOwnership);
Greg Danield85f97d2017-03-07 13:37:21 -0500256
Brian Osman32342f02017-03-04 08:12:46 -0500257 void abandon() {
Robert Phillips26c90e02017-03-14 14:39:29 -0400258 fCache = nullptr;
259 fGpu = nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500260 }
261
Brian Salomon238069b2018-07-11 15:58:57 -0400262 uint32_t contextUniqueID() const { return fCache->contextUniqueID(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400263 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500264 bool overBudget() const { return fCache->overBudget(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400265
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400266 static uint32_t MakeApprox(uint32_t value);
267
Greg Daniel8982dc12018-01-05 12:56:15 -0500268 inline GrResourceProviderPriv priv();
269 inline const GrResourceProviderPriv priv() const;
270
bsalomoned0bcad2015-05-04 10:36:42 -0700271private:
Brian Salomond28a79d2017-10-16 13:01:07 -0400272 sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
Robert Phillips3798c862017-03-27 11:08:16 -0400273
Greg Daniel29bf84f2017-09-25 12:25:12 -0400274 // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
275 // it returns null. If non-null, the resulting texture is always budgeted.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400276 sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc& desc,
277 const GrBackendFormat& format,
278 GrRenderable renderable,
279 int renderTargetSampleCnt,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400280 GrProtected isProtected);
Brian Osman32342f02017-03-04 08:12:46 -0500281
Robert Phillips45fdae12017-04-17 12:57:27 -0400282 /*
283 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
284 * update the budgeting accordingly.
285 */
Brian Salomon4eb38b72019-08-05 12:58:39 -0400286 sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc& desc,
287 const GrBackendFormat& format,
288 GrRenderable renderable,
289 int renderTargetSampleCnt,
290 SkBudgeted budgeted,
Robert Phillipsaee18c92019-09-06 11:48:27 -0400291 GrProtected isProtected);
Robert Phillips45fdae12017-04-17 12:57:27 -0400292
Brian Osman32342f02017-03-04 08:12:46 -0500293 GrResourceCache* cache() { return fCache; }
294 const GrResourceCache* cache() const { return fCache; }
295
Greg Daniel8982dc12018-01-05 12:56:15 -0500296 friend class GrResourceProviderPriv;
297
298 // Method made available via GrResourceProviderPriv
Brian Osman32342f02017-03-04 08:12:46 -0500299 GrGpu* gpu() { return fGpu; }
300 const GrGpu* gpu() const { return fGpu; }
301
302 bool isAbandoned() const {
303 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
304 return !SkToBool(fCache);
305 }
306
Brian Salomondbf70722019-02-07 11:31:24 -0500307 sk_sp<const GrGpuBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
308 int patternSize,
309 int reps,
310 int vertCount,
Brian Salomona29dd9d2019-02-07 13:27:18 -0500311 const GrUniqueKey* key);
bsalomoned0bcad2015-05-04 10:36:42 -0700312
Brian Salomondbf70722019-02-07 11:31:24 -0500313 sk_sp<const GrGpuBuffer> createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700314
Brian Salomona29dd9d2019-02-07 13:27:18 -0500315 GrResourceCache* fCache;
316 GrGpu* fGpu;
Robert Phillips26c90e02017-03-14 14:39:29 -0400317 sk_sp<const GrCaps> fCaps;
Brian Salomona29dd9d2019-02-07 13:27:18 -0500318 sk_sp<const GrGpuBuffer> fQuadIndexBuffer;
bsalomoned0bcad2015-05-04 10:36:42 -0700319
Brian Osman32342f02017-03-04 08:12:46 -0500320 // In debug builds we guard against improper thread handling
321 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700322};
323
bsalomond309e7a2015-04-30 14:18:54 -0700324#endif