blob: caa108747d65258bdbbdc14a2498d8ff3c91d833 [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"
bsalomon706f08f2015-05-22 07:35:58 -070013#include "GrPathRange.h"
Robert Phillipseafd48a2017-11-16 07:52:08 -050014#include "GrResourceCache.h"
Robert Phillips646e4292017-06-13 12:44:56 -040015#include "SkImageInfo.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040016#include "SkScalerContext.h"
bsalomond309e7a2015-04-30 14:18:54 -070017
Greg Danielbcf612b2017-05-01 13:50:58 +000018class GrBackendRenderTarget;
Greg Daniela5cb7812017-06-16 09:45:32 -040019class GrBackendSemaphore;
20class GrBackendTexture;
Robert Phillips009e9af2017-06-15 14:01:04 -040021class GrGpu;
bsalomon706f08f2015-05-22 07:35:58 -070022class GrPath;
egdanielec00d942015-09-14 12:56:10 -070023class GrRenderTarget;
Greg Daniel8982dc12018-01-05 12:56:15 -050024class GrResourceProviderPriv;
Robert Phillips646e4292017-06-13 12:44:56 -040025class GrSemaphore;
joshualitt6d0872d2016-01-11 08:27:48 -080026class GrSingleOwner;
egdanielec00d942015-09-14 12:56:10 -070027class GrStencilAttachment;
Robert Phillips646e4292017-06-13 12:44:56 -040028class GrTexture;
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:
Robert Phillipsa3f70262018-02-08 10:59:38 -050043 GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*,
44 GrContextOptions::Enable explicitlyAllocateGPUResources);
bsalomoned0bcad2015-05-04 10:36:42 -070045
Brian Salomond28a79d2017-10-16 13:01:07 -040046 /**
47 * Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
48 * must be sure that if a resource of exists in the cache with the given unique key then it is
49 * of type T.
50 */
51 template <typename T>
52 sk_sp<T> findByUniqueKey(const GrUniqueKey& key) {
53 return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
bsalomoned0bcad2015-05-04 10:36:42 -070054 }
55
Brian Osman32342f02017-03-04 08:12:46 -050056 ///////////////////////////////////////////////////////////////////////////
57 // Textures
58
Brian Osman32342f02017-03-04 08:12:46 -050059 /**
60 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
61 * and height as desc specifies. If desc specifies that the texture should be a render target
62 * then result will be a render target. Format and sample count will always match the request.
Robert Phillips67d52cf2017-06-05 13:38:13 -040063 * The contents of the texture are undefined.
Brian Osman32342f02017-03-04 08:12:46 -050064 */
Robert Phillips67d52cf2017-06-05 13:38:13 -040065 sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc&, uint32_t flags);
Brian Osman32342f02017-03-04 08:12:46 -050066
Robert Phillipse78b7252017-04-06 07:59:41 -040067 /** Create an exact fit texture with no initial data to upload.
68 */
Robert Phillips45fdae12017-04-17 12:57:27 -040069 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, uint32_t flags = 0);
70
Robert Phillips8e8c7552017-07-10 12:06:05 -040071 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted,
Robert Phillips590533f2017-07-11 14:22:35 -040072 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips8e8c7552017-07-10 12:06:05 -040073 SkDestinationSurfaceColorMode mipColorMode);
74
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075 // Create a potentially loose fit texture with the provided data
Greg Danielfb3abcd2018-02-02 15:48:33 -050076 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, SkBackingFit,
77 const GrMipLevel&);
Robert Phillipse78b7252017-04-06 07:59:41 -040078
Brian Osman32342f02017-03-04 08:12:46 -050079 ///////////////////////////////////////////////////////////////////////////
80 // Wrapped Backend Surfaces
81
82 /**
83 * Wraps an existing texture with a GrTexture object.
84 *
85 * OpenGL: if the object is a texture Gr may change its GL texture params
86 * when it is drawn.
87 *
88 * @return GrTexture object or NULL on failure.
89 */
Greg Daniel7ef28f32017-04-20 16:41:55 +000090 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex,
Brian Osman32342f02017-03-04 08:12:46 -050091 GrWrapOwnership = kBorrow_GrWrapOwnership);
92
93 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -050094 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
Brian Salomond17f6582017-07-19 18:28:58 -040095 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
96 * to the texture.
97 */
98 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -040099 int sampleCnt,
100 GrWrapOwnership = kBorrow_GrWrapOwnership);
101
102 /**
Brian Osman32342f02017-03-04 08:12:46 -0500103 * Wraps an existing render target with a GrRenderTarget object. It is
104 * similar to wrapBackendTexture but can be used to draw into surfaces
105 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
106 * the client will resolve to a texture). Currently wrapped render targets
107 * always use the kBorrow_GrWrapOwnership semantics.
108 *
109 * @return GrRenderTarget object or NULL on failure.
110 */
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400111 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
Brian Osman32342f02017-03-04 08:12:46 -0500112
Robert Phillips1bfece82017-06-01 13:56:52 -0400113 static const uint32_t kMinScratchTextureSize;
Brian Osman32342f02017-03-04 08:12:46 -0500114
bsalomoned0bcad2015-05-04 10:36:42 -0700115 /**
Chris Dalton5d2de082017-12-19 10:40:23 -0700116 * Either finds and refs, or creates a static buffer with the given parameters and contents.
117 *
118 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
119 * @param size minimum size of buffer to return.
120 * @param data optional data with which to initialize the buffer.
121 * @param key Key to be assigned to the buffer.
122 *
123 * @return The buffer if successful, otherwise nullptr.
124 */
125 sk_sp<const GrBuffer> findOrMakeStaticBuffer(GrBufferType intendedType, size_t size,
126 const void* data, const GrUniqueKey& key);
127
128 /**
Chris Daltonff926502017-05-03 14:36:54 -0400129 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
130 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
131 * the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -0700132 *
133 * @param pattern the pattern of indices to repeat
134 * @param patternSize size in bytes of the pattern
135 * @param reps number of times to repeat the pattern
136 * @param vertCount number of vertices the pattern references
137 * @param key Key to be assigned to the index buffer.
138 *
halcanary96fcdcc2015-08-27 07:41:13 -0700139 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -0700140 */
Brian Salomond28a79d2017-10-16 13:01:07 -0400141 sk_sp<const GrBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
142 int patternSize,
143 int reps,
144 int vertCount,
145 const GrUniqueKey& key) {
146 if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
bsalomoned0bcad2015-05-04 10:36:42 -0700147 return buffer;
148 }
Chris Daltonff926502017-05-03 14:36:54 -0400149 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, key);
bsalomoned0bcad2015-05-04 10:36:42 -0700150 }
151
152 /**
153 * Returns an index buffer that can be used to render quads.
Brian Salomon57caa662017-10-18 12:21:05 +0000154 * Six indices per quad: 0, 1, 2, 2, 1, 3, etc.
cdalton397536c2016-03-25 12:15:03 -0700155 * The max number of quads is the buffer's index capacity divided by 6.
Chris Dalton3809bab2017-06-13 10:55:06 -0600156 * Draw with GrPrimitiveType::kTriangles
bsalomoned0bcad2015-05-04 10:36:42 -0700157 * @ return the quad index buffer
158 */
Brian Salomond28a79d2017-10-16 13:01:07 -0400159 sk_sp<const GrBuffer> refQuadIndexBuffer() {
160 if (auto buffer = this->findByUniqueKey<const GrBuffer>(fQuadIndexBufferKey)) {
bsalomoned0bcad2015-05-04 10:36:42 -0700161 return buffer;
162 }
163 return this->createQuadIndexBuffer();
164 }
165
Brian Salomon34169692017-08-28 15:32:01 -0400166 static int QuadCountOfQuadBuffer();
167
bsalomon706f08f2015-05-22 07:35:58 -0700168 /**
169 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
170 * is not supported.
171 */
Robert Phillips67d52cf2017-06-05 13:38:13 -0400172 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
173 sk_sp<GrPathRange> createPathRange(GrPathRange::PathGenerator*, const GrStyle&);
174 sk_sp<GrPathRange> createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
175 const SkDescriptor*, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -0700176
Brian Osman32342f02017-03-04 08:12:46 -0500177 /** These flags govern which scratch resources we are allowed to return */
bsalomoneae62002015-07-31 13:59:30 -0700178 enum Flags {
179 /** 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 -0400180 * set when accessing resources during a GrOpList flush. This includes the execution of
Brian Salomon25a88092016-12-01 09:36:50 -0500181 * GrOp objects. The reason is that these memory operations are done immediately and
bsalomoneae62002015-07-31 13:59:30 -0700182 * will occur out of order WRT the operations being flushed.
halcanary6950de62015-11-07 05:29:00 -0800183 * Make this automatic: https://bug.skia.org/4156
bsalomoneae62002015-07-31 13:59:30 -0700184 */
Greg Daniel29bf84f2017-09-25 12:25:12 -0400185 kNoPendingIO_Flag = 0x1,
Greg Daniel21918232017-09-08 14:46:23 -0400186
csmartdalton485a1202016-07-13 10:16:32 -0700187 /** Normally the caps may indicate a preference for client-side buffers. Set this flag when
188 * creating a buffer to guarantee it resides in GPU memory.
189 */
Greg Daniel29bf84f2017-09-25 12:25:12 -0400190 kRequireGpuMemory_Flag = 0x2,
bsalomoneae62002015-07-31 13:59:30 -0700191 };
192
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 */
cdalton1bf3e712016-04-19 10:00:02 -0700204 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, uint32_t flags,
205 const void* data = nullptr);
bsalomoneae62002015-07-31 13:59:30 -0700206
robertphillips1b8e1b52015-06-24 06:54:10 -0700207
Brian Salomon2ee084e2016-12-16 18:59:19 -0500208 /**
Robert Phillipsc0192e32017-09-21 12:00:26 -0400209 * If passed in render target already has a stencil buffer, return true. Otherwise attempt to
210 * attach one and return true on success.
egdanielec00d942015-09-14 12:56:10 -0700211 */
Robert Phillipsc0192e32017-09-21 12:00:26 -0400212 bool attachStencilAttachment(GrRenderTarget* rt);
egdanielec00d942015-09-14 12:56:10 -0700213
ericrkf7b8b8a2016-02-24 14:49:51 -0800214 /**
215 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
216 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
217 *
kkinnunen49c4c222016-04-01 04:50:37 -0700218 * The texture is wrapped as borrowed. The texture object will not be freed once the
219 * render target is destroyed.
220 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800221 * @return GrRenderTarget object or NULL on failure.
222 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000223 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000224 int sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800225
Brian Osman32342f02017-03-04 08:12:46 -0500226 /**
227 * Assigns a unique key to a resource. If the key is associated with another resource that
228 * association is removed and replaced by this resource.
229 */
230 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
231
Greg Daniela5cb7812017-06-16 09:45:32 -0400232 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
233
Greg Daniel17b7c052018-01-09 13:55:33 -0500234 enum class SemaphoreWrapType {
235 kWillSignal,
236 kWillWait,
237 };
238
Greg Daniela5cb7812017-06-16 09:45:32 -0400239 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
Greg Daniel17b7c052018-01-09 13:55:33 -0500240 SemaphoreWrapType wrapType,
Greg Daniela5cb7812017-06-16 09:45:32 -0400241 GrWrapOwnership = kBorrow_GrWrapOwnership);
Greg Danield85f97d2017-03-07 13:37:21 -0500242
243 // Takes the GrSemaphore and sets the ownership of the semaphore to the GrGpu object used by
244 // this class. This call is only used when passing a GrSemaphore from one context to another.
245 void takeOwnershipOfSemaphore(sk_sp<GrSemaphore>);
246 // Takes the GrSemaphore and resets the ownership of the semaphore so that it is not owned by
247 // any GrGpu. A follow up call to takeOwnershipofSemaphore must be made so that the underlying
248 // semaphore can be deleted. This call is only used when passing a GrSemaphore from one context
249 // to another.
250 void releaseOwnershipOfSemaphore(sk_sp<GrSemaphore>);
251
Brian Osman32342f02017-03-04 08:12:46 -0500252 void abandon() {
Robert Phillips26c90e02017-03-14 14:39:29 -0400253 fCache = nullptr;
254 fGpu = nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500255 }
256
Robert Phillips26c90e02017-03-14 14:39:29 -0400257 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500258 bool overBudget() const { return fCache->overBudget(); }
Robert Phillips26c90e02017-03-14 14:39:29 -0400259
Greg Daniel8982dc12018-01-05 12:56:15 -0500260 inline GrResourceProviderPriv priv();
261 inline const GrResourceProviderPriv priv() const;
262
Robert Phillips4150eea2018-02-07 17:08:21 -0500263 bool explicitlyAllocateGPUResources() const { return fExplicitlyAllocateGPUResources; }
264
265 bool testingOnly_setExplicitlyAllocateGPUResources(bool newValue);
266
bsalomoned0bcad2015-05-04 10:36:42 -0700267private:
Brian Salomond28a79d2017-10-16 13:01:07 -0400268 sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
Robert Phillips3798c862017-03-27 11:08:16 -0400269
Greg Daniel29bf84f2017-09-25 12:25:12 -0400270 // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
271 // it returns null. If non-null, the resulting texture is always budgeted.
Robert Phillips67d52cf2017-06-05 13:38:13 -0400272 sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500273
Robert Phillips45fdae12017-04-17 12:57:27 -0400274 /*
275 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
276 * update the budgeting accordingly.
277 */
Greg Daniel21918232017-09-08 14:46:23 -0400278 sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&, SkBudgeted, uint32_t flags);
Robert Phillips45fdae12017-04-17 12:57:27 -0400279
Brian Osman32342f02017-03-04 08:12:46 -0500280 GrResourceCache* cache() { return fCache; }
281 const GrResourceCache* cache() const { return fCache; }
282
Greg Daniel8982dc12018-01-05 12:56:15 -0500283 friend class GrResourceProviderPriv;
284
285 // Method made available via GrResourceProviderPriv
Brian Osman32342f02017-03-04 08:12:46 -0500286 GrGpu* gpu() { return fGpu; }
287 const GrGpu* gpu() const { return fGpu; }
288
289 bool isAbandoned() const {
290 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
291 return !SkToBool(fCache);
292 }
293
Brian Salomond28a79d2017-10-16 13:01:07 -0400294 sk_sp<const GrBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
295 int patternSize,
296 int reps,
297 int vertCount,
298 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700299
Brian Salomond28a79d2017-10-16 13:01:07 -0400300 sk_sp<const GrBuffer> createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700301
Robert Phillips26c90e02017-03-14 14:39:29 -0400302 GrResourceCache* fCache;
303 GrGpu* fGpu;
304 sk_sp<const GrCaps> fCaps;
305 GrUniqueKey fQuadIndexBufferKey;
Robert Phillips4150eea2018-02-07 17:08:21 -0500306 bool fExplicitlyAllocateGPUResources;
bsalomoned0bcad2015-05-04 10:36:42 -0700307
Brian Osman32342f02017-03-04 08:12:46 -0500308 // In debug builds we guard against improper thread handling
309 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700310};
311
312#endif