1/*
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
11#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"
16
17class GrBackendRenderTarget;
18class GrBackendSemaphore;
19class GrBackendTexture;
20class GrGpu;
21class GrPath;
22class GrRenderTarget;
23class GrResourceProviderPriv;
24class GrSemaphore;
25class GrSingleOwner;
26class GrStencilAttachment;
27class GrTexture;
28struct GrVkDrawableInfo;
29
30class GrStyle;
31class SkDescriptor;
32class SkPath;
33class SkTypeface;
34
35/**
36 * A factory for arbitrary resource types.
37 */
38class GrResourceProvider {
39public:
40 GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*);
41
42 /**
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 */
47 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) {
50 return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
51 }
52
53 ///////////////////////////////////////////////////////////////////////////
54 // Textures
55
56 /**
57 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
58 * 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.
60 * The contents of the texture are undefined.
61 */
62 sk_sp<GrTexture> createApproxTexture(SkISize dimensions,
63 const GrBackendFormat& format,
64 GrRenderable renderable,
65 int renderTargetSampleCnt,
66 GrProtected isProtected);
67
68 /** Create an exact fit texture with no initial data to upload. */
69 sk_sp<GrTexture> createTexture(SkISize dimensions,
70 const GrBackendFormat& format,
71 GrRenderable renderable,
72 int renderTargetSampleCnt,
73 GrMipMapped mipMapped,
74 SkBudgeted budgeted,
75 GrProtected isProtected);
76
77 /**
78 * Create an exact fit texture with initial data to upload. The color type must be valid
79 * for the format and also describe the texel data. This will ensure any conversions that
80 * need to get applied to the data before upload are applied.
81 */
82 sk_sp<GrTexture> createTexture(SkISize dimensions,
83 const GrBackendFormat& format,
84 GrColorType colorType,
85 GrRenderable renderable,
86 int renderTargetSampleCnt,
87 SkBudgeted budgeted,
88 GrProtected isProtected,
89 const GrMipLevel texels[],
90 int mipLevelCount);
91
92 /**
93 * Create a potentially loose fit texture with the provided data. The color type must be valid
94 * for the format and also describe the texel data. This will ensure any conversions that
95 * need to get applied to the data before upload are applied.
96 */
97 sk_sp<GrTexture> createTexture(SkISize dimensions,
98 const GrBackendFormat&,
99 GrColorType srcColorType,
100 GrRenderable,
101 int renderTargetSampleCnt,
102 SkBudgeted,
103 SkBackingFit,
104 GrProtected,
105 const GrMipLevel& mipLevel);
106
107 /**
108 * Creates a compressed texture. The GrGpu must support the SkImageImage::Compression type.
109 * It will not be renderable.
110 */
111 sk_sp<GrTexture> createCompressedTexture(SkISize dimensions,
112 const GrBackendFormat&,
113 SkBudgeted,
114 GrMipMapped,
115 GrProtected,
116 SkData* data);
117
118 ///////////////////////////////////////////////////////////////////////////
119 // Wrapped Backend Surfaces
120
121 /**
122 * Wraps an existing texture with a GrTexture object.
123 *
124 * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the
125 * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels).
126 *
127 * OpenGL: if the object is a texture Gr may change its GL texture params
128 * when it is drawn.
129 *
130 * @return GrTexture object or NULL on failure.
131 */
132 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex,
133 GrWrapOwnership,
134 GrWrapCacheable,
135 GrIOType);
136
137 sk_sp<GrTexture> wrapCompressedBackendTexture(const GrBackendTexture& tex,
138 GrWrapOwnership,
139 GrWrapCacheable);
140
141 /**
142 * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
143 * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
144 * to the texture.
145 */
146 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
147 int sampleCnt,
148 GrWrapOwnership,
149 GrWrapCacheable);
150
151 /**
152 * Wraps an existing render target with a GrRenderTarget object. It is
153 * similar to wrapBackendTexture but can be used to draw into surfaces
154 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
155 * the client will resolve to a texture). Currently wrapped render targets
156 * always use the kBorrow_GrWrapOwnership and GrWrapCacheable::kNo semantics.
157 *
158 * @return GrRenderTarget object or NULL on failure.
159 */
160 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
161
162 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
163 const GrVkDrawableInfo&);
164
165 static const int kMinScratchTextureSize;
166
167 /**
168 * 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 */
177 sk_sp<const GrGpuBuffer> findOrMakeStaticBuffer(GrGpuBufferType intendedType, size_t size,
178 const void* data, const GrUniqueKey& key);
179
180 /**
181 * 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.
184 *
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 *
191 * @return The index buffer if successful, otherwise nullptr.
192 */
193 sk_sp<const GrGpuBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
194 int patternSize,
195 int reps,
196 int vertCount,
197 const GrUniqueKey& key) {
198 if (auto buffer = this->findByUniqueKey<const GrGpuBuffer>(key)) {
199 return buffer;
200 }
201 return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, &key);
202 }
203
204 /**
205 * Returns an index buffer that can be used to render non-antialiased quads.
206 * Each quad consumes 6 indices (0, 1, 2, 2, 1, 3) and 4 vertices.
207 * Call MaxNumNonAAQuads to get the max allowed number of non-AA quads.
208 * Draw with GrPrimitiveType::kTriangles
209 * @ return the non-AA quad index buffer
210 */
211 sk_sp<const GrGpuBuffer> refNonAAQuadIndexBuffer() {
212 if (!fNonAAQuadIndexBuffer) {
213 fNonAAQuadIndexBuffer = this->createNonAAQuadIndexBuffer();
214 }
215 return fNonAAQuadIndexBuffer;
216 }
217
218 static int MaxNumNonAAQuads();
219 static int NumVertsPerNonAAQuad();
220 static int NumIndicesPerNonAAQuad();
221
222 /**
223 * Returns an index buffer that can be used to render antialiased quads.
224 * Each quad consumes 30 indices and 8 vertices.
225 * Call MaxNumAAQuads to get the max allowed number of AA quads.
226 * Draw with GrPrimitiveType::kTriangles
227 * @ return the AA quad index buffer
228 */
229 sk_sp<const GrGpuBuffer> refAAQuadIndexBuffer() {
230 if (!fAAQuadIndexBuffer) {
231 fAAQuadIndexBuffer = this->createAAQuadIndexBuffer();
232 }
233 return fAAQuadIndexBuffer;
234 }
235
236 static int MaxNumAAQuads();
237 static int NumVertsPerAAQuad();
238 static int NumIndicesPerAAQuad();
239
240 /**
241 * Factories for GrPath objects. It's an error to call these if path rendering
242 * is not supported.
243 */
244 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
245
246 /**
247 * Returns a buffer.
248 *
249 * @param size minimum size of buffer to return.
250 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
251 * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
252 * @param flags see Flags enum.
253 * @param data optional data with which to initialize the buffer.
254 *
255 * @return the buffer if successful, otherwise nullptr.
256 */
257 sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType, GrAccessPattern,
258 const void* data = nullptr);
259
260 /**
261 * If passed in render target already has a stencil buffer with at least "numSamples" samples,
262 * return true. Otherwise attempt to attach one and return true on success.
263 */
264 bool attachStencilAttachment(GrRenderTarget* rt, int numStencilSamples);
265
266 /**
267 * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
268 * texture has a format that cannot be textured from by Skia, but we want to raster to it.
269 *
270 * The texture is wrapped as borrowed. The texture object will not be freed once the
271 * render target is destroyed.
272 *
273 * @return GrRenderTarget object or NULL on failure.
274 */
275 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&, int sampleCnt);
276
277 /**
278 * Assigns a unique key to a resource. If the key is associated with another resource that
279 * association is removed and replaced by this resource.
280 */
281 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
282
283 std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
284
285 enum class SemaphoreWrapType {
286 kWillSignal,
287 kWillWait,
288 };
289
290 std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
291 SemaphoreWrapType wrapType,
292 GrWrapOwnership = kBorrow_GrWrapOwnership);
293
294 void abandon() {
295 fCache = nullptr;
296 fGpu = nullptr;
297 }
298
299 uint32_t contextUniqueID() const { return fCache->contextUniqueID(); }
300 const GrCaps* caps() const { return fCaps.get(); }
301 bool overBudget() const { return fCache->overBudget(); }
302
303 static SkISize MakeApprox(SkISize);
304
305 inline GrResourceProviderPriv priv();
306 inline const GrResourceProviderPriv priv() const;
307
308private:
309 sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
310
311 // Attempts to find a resource in the cache that exactly matches the SkISize. Failing that
312 // it returns null. If non-null, the resulting texture is always budgeted.
313 sk_sp<GrTexture> refScratchTexture(SkISize dimensions,
314 const GrBackendFormat&,
315 GrRenderable,
316 int renderTargetSampleCnt,
317 GrMipMapped,
318 GrProtected);
319
320 /*
321 * Try to find an existing scratch texture that exactly matches 'desc'. If successful
322 * update the budgeting accordingly.
323 */
324 sk_sp<GrTexture> getExactScratch(SkISize dimensions,
325 const GrBackendFormat&,
326 GrRenderable,
327 int renderTargetSampleCnt,
328 SkBudgeted,
329 GrMipMapped,
330 GrProtected);
331
332 // Used to perform any conversions necessary to texel data before creating a texture with
333 // existing data or uploading to a scratch texture.
334 using TempLevels = SkAutoSTMalloc<14, GrMipLevel>;
335 using TempLevelDatas = SkAutoSTArray<14, std::unique_ptr<char[]>>;
336 GrColorType prepareLevels(const GrBackendFormat& format,
337 GrColorType,
338 SkISize baseSize,
339 const GrMipLevel texels[],
340 int mipLevelCount,
341 TempLevels*,
342 TempLevelDatas*) const;
343
344 // GrResourceProvider may be asked to "create" a new texture with initial pixel data to populate
345 // it. In implementation it may pull an existing texture from GrResourceCache and then write the
346 // pixel data to the texture. It takes a width/height for the base level because we may be
347 // using an approximate-sized scratch texture. On success the texture is returned and nullptr
348 // on failure.
349 sk_sp<GrTexture> writePixels(sk_sp<GrTexture> texture,
350 GrColorType colorType,
351 SkISize baseSize,
352 const GrMipLevel texels[],
353 int mipLevelCount) const;
354
355 GrResourceCache* cache() { return fCache; }
356 const GrResourceCache* cache() const { return fCache; }
357
358 friend class GrResourceProviderPriv;
359
360 // Method made available via GrResourceProviderPriv
361 GrGpu* gpu() { return fGpu; }
362 const GrGpu* gpu() const { return fGpu; }
363
364 bool isAbandoned() const {
365 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
366 return !SkToBool(fCache);
367 }
368
369 sk_sp<const GrGpuBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
370 int patternSize,
371 int reps,
372 int vertCount,
373 const GrUniqueKey* key);
374
375 sk_sp<const GrGpuBuffer> createNonAAQuadIndexBuffer();
376 sk_sp<const GrGpuBuffer> createAAQuadIndexBuffer();
377
378 GrResourceCache* fCache;
379 GrGpu* fGpu;
380 sk_sp<const GrCaps> fCaps;
381 sk_sp<const GrGpuBuffer> fNonAAQuadIndexBuffer;
382 sk_sp<const GrGpuBuffer> fAAQuadIndexBuffer;
383
384 // In debug builds we guard against improper thread handling
385 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
386};
387
388#endif
389