blob: be06c8d63a2d0ccb38c207135a7f8b3a2a0bf149 [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"
bsalomon706f08f2015-05-22 07:35:58 -070012#include "GrPathRange.h"
Robert Phillips646e4292017-06-13 12:44:56 -040013#include "SkImageInfo.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040014#include "SkScalerContext.h"
bsalomond309e7a2015-04-30 14:18:54 -070015
Greg Danielbcf612b2017-05-01 13:50:58 +000016class GrBackendRenderTarget;
Greg Daniela5cb7812017-06-16 09:45:32 -040017class GrBackendSemaphore;
18class GrBackendTexture;
Robert Phillips009e9af2017-06-15 14:01:04 -040019class GrGpu;
bsalomon706f08f2015-05-22 07:35:58 -070020class GrPath;
egdanielec00d942015-09-14 12:56:10 -070021class GrRenderTarget;
Robert Phillips646e4292017-06-13 12:44:56 -040022class GrSemaphore;
joshualitt6d0872d2016-01-11 08:27:48 -080023class GrSingleOwner;
egdanielec00d942015-09-14 12:56:10 -070024class GrStencilAttachment;
Robert Phillips646e4292017-06-13 12:44:56 -040025class GrSurfaceProxy;
26class GrTexture;
27class GrTextureProxy;
28
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:
joshualitt6d0872d2016-01-11 08:27:48 -080042 GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner);
bsalomoned0bcad2015-05-04 10:36:42 -070043
44 template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
45 return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
46 }
47
Robert Phillipsae7d3f32017-09-21 08:26:08 -040048 /*
49 * Assigns a unique key to a proxy. The proxy will be findable via this key using
50 * findProxyByUniqueKey(). It is an error if an existing proxy already has a key.
51 */
52 void assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*);
53
54 /*
55 * Finds a proxy by unique key.
56 */
57 sk_sp<GrTextureProxy> findProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin);
58
Brian Osman32342f02017-03-04 08:12:46 -050059 ///////////////////////////////////////////////////////////////////////////
60 // Textures
61
Brian Osman32342f02017-03-04 08:12:46 -050062 /**
63 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
64 * and height as desc specifies. If desc specifies that the texture should be a render target
65 * then result will be a render target. Format and sample count will always match the request.
Robert Phillips67d52cf2017-06-05 13:38:13 -040066 * The contents of the texture are undefined.
Brian Osman32342f02017-03-04 08:12:46 -050067 */
Robert Phillips67d52cf2017-06-05 13:38:13 -040068 sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc&, uint32_t flags);
Brian Osman32342f02017-03-04 08:12:46 -050069
Robert Phillipse78b7252017-04-06 07:59:41 -040070 /** Create an exact fit texture with no initial data to upload.
71 */
Robert Phillips45fdae12017-04-17 12:57:27 -040072 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, uint32_t flags = 0);
73
Robert Phillips8e8c7552017-07-10 12:06:05 -040074 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted,
Robert Phillips590533f2017-07-11 14:22:35 -040075 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips8e8c7552017-07-10 12:06:05 -040076 SkDestinationSurfaceColorMode mipColorMode);
77
Robert Phillips45fdae12017-04-17 12:57:27 -040078 sk_sp<GrTextureProxy> createTextureProxy(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel&);
Robert Phillipse78b7252017-04-06 07:59:41 -040079
Brian Osman32342f02017-03-04 08:12:46 -050080 ///////////////////////////////////////////////////////////////////////////
81 // Wrapped Backend Surfaces
82
83 /**
84 * Wraps an existing texture with a GrTexture object.
85 *
86 * OpenGL: if the object is a texture Gr may change its GL texture params
87 * when it is drawn.
88 *
89 * @return GrTexture object or NULL on failure.
90 */
Greg Daniel7ef28f32017-04-20 16:41:55 +000091 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex,
Brian Osman32342f02017-03-04 08:12:46 -050092 GrWrapOwnership = kBorrow_GrWrapOwnership);
93
94 /**
Brian Salomond17f6582017-07-19 18:28:58 -040095 * This makes the backend texture be renderable. If sampleCnt is > 0 and the underlying API
96 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
97 * to the texture.
98 */
99 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
Brian Salomond17f6582017-07-19 18:28:58 -0400100 int sampleCnt,
101 GrWrapOwnership = kBorrow_GrWrapOwnership);
102
103 /**
Brian Osman32342f02017-03-04 08:12:46 -0500104 * Wraps an existing render target with a GrRenderTarget object. It is
105 * similar to wrapBackendTexture but can be used to draw into surfaces
106 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
107 * the client will resolve to a texture). Currently wrapped render targets
108 * always use the kBorrow_GrWrapOwnership semantics.
109 *
110 * @return GrRenderTarget object or NULL on failure.
111 */
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400112 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
Brian Osman32342f02017-03-04 08:12:46 -0500113
Robert Phillips1bfece82017-06-01 13:56:52 -0400114 static const uint32_t kMinScratchTextureSize;
Brian Osman32342f02017-03-04 08:12:46 -0500115
bsalomoned0bcad2015-05-04 10:36:42 -0700116 /**
Chris Daltonff926502017-05-03 14:36:54 -0400117 * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
118 * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
119 * the returned GrBuffer.
bsalomoned0bcad2015-05-04 10:36:42 -0700120 *
121 * @param pattern the pattern of indices to repeat
122 * @param patternSize size in bytes of the pattern
123 * @param reps number of times to repeat the pattern
124 * @param vertCount number of vertices the pattern references
125 * @param key Key to be assigned to the index buffer.
126 *
halcanary96fcdcc2015-08-27 07:41:13 -0700127 * @return The index buffer if successful, otherwise nullptr.
bsalomoned0bcad2015-05-04 10:36:42 -0700128 */
Chris Daltonff926502017-05-03 14:36:54 -0400129 const GrBuffer* findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
cdalton397536c2016-03-25 12:15:03 -0700130 int patternSize,
131 int reps,
132 int vertCount,
133 const GrUniqueKey& key) {
134 if (GrBuffer* buffer = this->findAndRefTByUniqueKey<GrBuffer>(key)) {
bsalomoned0bcad2015-05-04 10:36:42 -0700135 return buffer;
136 }
Chris Daltonff926502017-05-03 14:36:54 -0400137 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, key);
bsalomoned0bcad2015-05-04 10:36:42 -0700138 }
139
140 /**
141 * Returns an index buffer that can be used to render quads.
142 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
cdalton397536c2016-03-25 12:15:03 -0700143 * The max number of quads is the buffer's index capacity divided by 6.
Chris Dalton3809bab2017-06-13 10:55:06 -0600144 * Draw with GrPrimitiveType::kTriangles
bsalomoned0bcad2015-05-04 10:36:42 -0700145 * @ return the quad index buffer
146 */
cdalton397536c2016-03-25 12:15:03 -0700147 const GrBuffer* refQuadIndexBuffer() {
148 if (GrBuffer* buffer =
149 this->findAndRefTByUniqueKey<GrBuffer>(fQuadIndexBufferKey)) {
bsalomoned0bcad2015-05-04 10:36:42 -0700150 return buffer;
151 }
152 return this->createQuadIndexBuffer();
153 }
154
Brian Salomon34169692017-08-28 15:32:01 -0400155 static int QuadCountOfQuadBuffer();
156
bsalomon706f08f2015-05-22 07:35:58 -0700157 /**
158 * Factories for GrPath and GrPathRange objects. It's an error to call these if path rendering
159 * is not supported.
160 */
Robert Phillips67d52cf2017-06-05 13:38:13 -0400161 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
162 sk_sp<GrPathRange> createPathRange(GrPathRange::PathGenerator*, const GrStyle&);
163 sk_sp<GrPathRange> createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
164 const SkDescriptor*, const GrStyle&);
bsalomon706f08f2015-05-22 07:35:58 -0700165
Brian Osman32342f02017-03-04 08:12:46 -0500166 /** These flags govern which scratch resources we are allowed to return */
bsalomoneae62002015-07-31 13:59:30 -0700167 enum Flags {
Greg Daniel21918232017-09-08 14:46:23 -0400168 kExact_Flag = 0x1,
Brian Osman32342f02017-03-04 08:12:46 -0500169
bsalomoneae62002015-07-31 13:59:30 -0700170 /** 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 -0400171 * set when accessing resources during a GrOpList flush. This includes the execution of
Brian Salomon25a88092016-12-01 09:36:50 -0500172 * GrOp objects. The reason is that these memory operations are done immediately and
bsalomoneae62002015-07-31 13:59:30 -0700173 * will occur out of order WRT the operations being flushed.
halcanary6950de62015-11-07 05:29:00 -0800174 * Make this automatic: https://bug.skia.org/4156
bsalomoneae62002015-07-31 13:59:30 -0700175 */
Brian Osman32342f02017-03-04 08:12:46 -0500176 kNoPendingIO_Flag = 0x2,
177
Greg Daniel21918232017-09-08 14:46:23 -0400178 kNoCreate_Flag = 0x4,
179
csmartdalton485a1202016-07-13 10:16:32 -0700180 /** Normally the caps may indicate a preference for client-side buffers. Set this flag when
181 * creating a buffer to guarantee it resides in GPU memory.
182 */
Greg Daniel21918232017-09-08 14:46:23 -0400183 kRequireGpuMemory_Flag = 0x8,
bsalomoneae62002015-07-31 13:59:30 -0700184 };
185
cdaltone2e71c22016-04-07 18:13:29 -0700186 /**
187 * Returns a buffer.
188 *
189 * @param size minimum size of buffer to return.
190 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
191 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
192 * @param flags see Flags enum.
cdalton1bf3e712016-04-19 10:00:02 -0700193 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700194 *
195 * @return the buffer if successful, otherwise nullptr.
196 */
cdalton1bf3e712016-04-19 10:00:02 -0700197 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern, uint32_t flags,
198 const void* data = nullptr);
bsalomoneae62002015-07-31 13:59:30 -0700199
robertphillips1b8e1b52015-06-24 06:54:10 -0700200
Brian Salomon2ee084e2016-12-16 18:59:19 -0500201 /**
Robert Phillipsc0192e32017-09-21 12:00:26 -0400202 * If passed in render target already has a stencil buffer, return true. Otherwise attempt to
203 * attach one and return true on success.
egdanielec00d942015-09-14 12:56:10 -0700204 */
Robert Phillipsc0192e32017-09-21 12:00:26 -0400205 bool attachStencilAttachment(GrRenderTarget* rt);
egdanielec00d942015-09-14 12:56:10 -0700206
ericrkf7b8b8a2016-02-24 14:49:51 -0800207 /**
208 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
209 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
210 *
kkinnunen49c4c222016-04-01 04:50:37 -0700211 * The texture is wrapped as borrowed. The texture object will not be freed once the
212 * render target is destroyed.
213 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800214 * @return GrRenderTarget object or NULL on failure.
215 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000216 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000217 int sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800218
Brian Osman32342f02017-03-04 08:12:46 -0500219 /**
220 * Assigns a unique key to a resource. If the key is associated with another resource that
221 * association is removed and replaced by this resource.
222 */
223 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
224
225 /**
226 * Finds a resource in the cache, based on the specified key. This is intended for use in
227 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
228 * caller must balance with a call to unref().
229 */
230 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
231
Greg Daniela5cb7812017-06-16 09:45:32 -0400232 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
233
234 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
235 GrWrapOwnership = kBorrow_GrWrapOwnership);
Greg Danield85f97d2017-03-07 13:37:21 -0500236
237 // Takes the GrSemaphore and sets the ownership of the semaphore to the GrGpu object used by
238 // this class. This call is only used when passing a GrSemaphore from one context to another.
239 void takeOwnershipOfSemaphore(sk_sp<GrSemaphore>);
240 // Takes the GrSemaphore and resets the ownership of the semaphore so that it is not owned by
241 // any GrGpu. A follow up call to takeOwnershipofSemaphore must be made so that the underlying
242 // semaphore can be deleted. This call is only used when passing a GrSemaphore from one context
243 // to another.
244 void releaseOwnershipOfSemaphore(sk_sp<GrSemaphore>);
245
Brian Osman32342f02017-03-04 08:12:46 -0500246 void abandon() {
Robert Phillips26c90e02017-03-14 14:39:29 -0400247 fCache = nullptr;
248 fGpu = nullptr;
Brian Osman32342f02017-03-04 08:12:46 -0500249 }
250
Robert Phillipsf7a72612017-03-31 10:03:45 -0400251 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to
252 // determine if it is going to need a texture domain or a full clear.
253 static bool IsFunctionallyExact(GrSurfaceProxy* proxy);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400254
Robert Phillips26c90e02017-03-14 14:39:29 -0400255 const GrCaps* caps() const { return fCaps.get(); }
256
bsalomoned0bcad2015-05-04 10:36:42 -0700257private:
Robert Phillips3798c862017-03-27 11:08:16 -0400258 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key);
Robert Phillips646e4292017-06-13 12:44:56 -0400259 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture);
Robert Phillips3798c862017-03-27 11:08:16 -0400260
Robert Phillips67d52cf2017-06-05 13:38:13 -0400261 sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
Brian Osman32342f02017-03-04 08:12:46 -0500262
Robert Phillips45fdae12017-04-17 12:57:27 -0400263 /*
264 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
265 * update the budgeting accordingly.
266 */
Greg Daniel21918232017-09-08 14:46:23 -0400267 sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&, SkBudgeted, uint32_t flags);
Robert Phillips45fdae12017-04-17 12:57:27 -0400268
Brian Osman32342f02017-03-04 08:12:46 -0500269 GrResourceCache* cache() { return fCache; }
270 const GrResourceCache* cache() const { return fCache; }
271
272 GrGpu* gpu() { return fGpu; }
273 const GrGpu* gpu() const { return fGpu; }
274
275 bool isAbandoned() const {
276 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
277 return !SkToBool(fCache);
278 }
279
Chris Daltonff926502017-05-03 14:36:54 -0400280 const GrBuffer* createPatternedIndexBuffer(const uint16_t* pattern,
cdalton397536c2016-03-25 12:15:03 -0700281 int patternSize,
282 int reps,
283 int vertCount,
284 const GrUniqueKey& key);
bsalomoned0bcad2015-05-04 10:36:42 -0700285
cdalton397536c2016-03-25 12:15:03 -0700286 const GrBuffer* createQuadIndexBuffer();
bsalomoned0bcad2015-05-04 10:36:42 -0700287
Robert Phillips26c90e02017-03-14 14:39:29 -0400288 GrResourceCache* fCache;
289 GrGpu* fGpu;
290 sk_sp<const GrCaps> fCaps;
291 GrUniqueKey fQuadIndexBufferKey;
bsalomoned0bcad2015-05-04 10:36:42 -0700292
Brian Osman32342f02017-03-04 08:12:46 -0500293 // In debug builds we guard against improper thread handling
294 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700295};
296
297#endif