blob: cda42a8dc712beccaeac514a10b87e4366dd7728 [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 GrTextureProvider_DEFINED
9#define GrTextureProvider_DEFINED
10
11#include "GrTexture.h"
cblume55f2d2d2016-02-26 13:20:48 -080012#include "GrTypes.h"
bsalomond309e7a2015-04-30 14:18:54 -070013
joshualitt6d0872d2016-01-11 08:27:48 -080014class GrSingleOwner;
15
bsalomond309e7a2015-04-30 14:18:54 -070016class SK_API GrTextureProvider {
17public:
18 ///////////////////////////////////////////////////////////////////////////
19 // Textures
20
21 /**
22 * Creates a new texture in the resource cache and returns it. The caller owns a
23 * ref on the returned texture which must be balanced by a call to unref.
24 *
cblume55f2d2d2016-02-26 13:20:48 -080025 * @param desc Description of the texture properties.
26 * @param budgeted Does the texture count against the resource cache budget?
27 * @param texels A contiguous array of mipmap levels
28 * @param mipLevelCount The amount of elements in the texels array
29 */
30 GrTexture* createMipMappedTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
31 const GrMipLevel* texels, int mipLevelCount);
32
33 /**
34 * This function is a shim which creates a SkTArray<GrMipLevel> of size 1.
35 * It then calls createTexture with that SkTArray.
36 *
bsalomond309e7a2015-04-30 14:18:54 -070037 * @param srcData Pointer to the pixel values (optional).
38 * @param rowBytes The number of bytes between rows of the texture. Zero
39 * implies tightly packed rows. For compressed pixel configs, this
40 * field is ignored.
41 */
bsalomon5ec26ae2016-02-25 08:33:02 -080042 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* srcData,
bsalomond309e7a2015-04-30 14:18:54 -070043 size_t rowBytes);
44
45 /** Shortcut for creating a texture with no initial data to upload. */
bsalomon5ec26ae2016-02-25 08:33:02 -080046 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
bsalomone699d0c2016-03-09 06:25:15 -080047 return this->createTexture(desc, budgeted, nullptr, 0);
bsalomond309e7a2015-04-30 14:18:54 -070048 }
49
50 /** Assigns a unique key to the texture. The texture will be findable via this key using
51 findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */
52 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
53 this->assignUniqueKeyToResource(key, texture);
54 }
55
56 /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */
joshualitt6d0872d2016-01-11 08:27:48 -080057 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key);
bsalomond309e7a2015-04-30 14:18:54 -070058
59 /**
60 * Determines whether a texture is associated with the unique key. If the texture is found it
61 * will not be locked or returned. This call does not affect the priority of the resource for
62 * deletion.
63 */
64 bool existsTextureWithUniqueKey(const GrUniqueKey& key) const {
65 return this->existsResourceWithUniqueKey(key);
66 }
67
68 /**
bsalomoneae62002015-07-31 13:59:30 -070069 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
70 * and height as desc specifies. If desc specifies that the texture should be a render target
71 * then result will be a render target. Format and sample count will always match the request.
72 * The contents of the texture are undefined. The caller owns a ref on the returned texture and
73 * must balance with a call to unref.
bsalomond309e7a2015-04-30 14:18:54 -070074 */
bsalomoneae62002015-07-31 13:59:30 -070075 GrTexture* createApproxTexture(const GrSurfaceDesc&);
76
77 /** Legacy function that no longer should be used. */
bsalomond309e7a2015-04-30 14:18:54 -070078 enum ScratchTexMatch {
bsalomond309e7a2015-04-30 14:18:54 -070079 kExact_ScratchTexMatch,
bsalomond309e7a2015-04-30 14:18:54 -070080 kApprox_ScratchTexMatch
81 };
bsalomoneae62002015-07-31 13:59:30 -070082 GrTexture* refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match) {
83 if (kApprox_ScratchTexMatch == match) {
84 return this->createApproxTexture(desc);
85 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -080086 return this->createTexture(desc, SkBudgeted::kYes);
bsalomoneae62002015-07-31 13:59:30 -070087 }
88 }
bsalomond309e7a2015-04-30 14:18:54 -070089
90 ///////////////////////////////////////////////////////////////////////////
91 // Wrapped Backend Surfaces
92
93 /**
94 * Wraps an existing texture with a GrTexture object.
95 *
96 * OpenGL: if the object is a texture Gr may change its GL texture params
97 * when it is drawn.
98 *
bsalomond309e7a2015-04-30 14:18:54 -070099 * @return GrTexture object or NULL on failure.
100 */
bungeman6bd52842016-10-27 09:30:08 -0700101 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTextureDesc& desc,
102 GrWrapOwnership = kBorrow_GrWrapOwnership);
bsalomond309e7a2015-04-30 14:18:54 -0700103
104 /**
105 * Wraps an existing render target with a GrRenderTarget object. It is
106 * similar to wrapBackendTexture but can be used to draw into surfaces
107 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
bsalomon6dc6f5f2015-06-18 09:12:16 -0700108 * the client will resolve to a texture). Currently wrapped render targets
109 * always use the kBorrow_GrWrapOwnership semantics.
bsalomond309e7a2015-04-30 14:18:54 -0700110 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800111 * @return GrRenderTarget object or NULL on failure.
bsalomond309e7a2015-04-30 14:18:54 -0700112 */
bungeman6bd52842016-10-27 09:30:08 -0700113 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
bsalomond309e7a2015-04-30 14:18:54 -0700114
Robert Phillips784b7bf2016-12-09 13:35:02 -0500115 static const int kMinScratchTextureSize;
116
bsalomond309e7a2015-04-30 14:18:54 -0700117protected:
joshualitt6d0872d2016-01-11 08:27:48 -0800118 GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner);
bsalomond309e7a2015-04-30 14:18:54 -0700119
120 /**
121 * Assigns a unique key to a resource. If the key is associated with another resource that
122 * association is removed and replaced by this resource.
123 */
124 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
125
126 /**
127 * Finds a resource in the cache, based on the specified key. This is intended for use in
128 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
129 * caller must balance with a call to unref().
130 */
131 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
132
133 /**
134 * Determines whether a resource is in the cache. If the resource is found it
135 * will not be locked or returned. This call does not affect the priority of
136 * the resource for deletion.
137 */
138 bool existsResourceWithUniqueKey(const GrUniqueKey& key) const;
139
bsalomoneae62002015-07-31 13:59:30 -0700140 enum ScratchTextureFlags {
141 kExact_ScratchTextureFlag = 0x1,
142 kNoPendingIO_ScratchTextureFlag = 0x2, // (http://skbug.com/4156)
143 kNoCreate_ScratchTextureFlag = 0x4,
Brian Salomonfe647b22016-12-05 13:14:44 -0500144 kLastScratchTextureFlag = kNoCreate_ScratchTextureFlag
bsalomoneae62002015-07-31 13:59:30 -0700145 };
146
147 /** A common impl for GrTextureProvider and GrResourceProvider variants. */
148 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags);
149
150 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
bsalomond309e7a2015-04-30 14:18:54 -0700151
152 void abandon() {
153 fCache = NULL;
154 fGpu = NULL;
155 }
156
bsalomoned0bcad2015-05-04 10:36:42 -0700157 GrResourceCache* cache() { return fCache; }
158 const GrResourceCache* cache() const { return fCache; }
159
160 GrGpu* gpu() { return fGpu; }
161 const GrGpu* gpu() const { return fGpu; }
162
bsalomond309e7a2015-04-30 14:18:54 -0700163 bool isAbandoned() const {
164 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
165 return !SkToBool(fCache);
166 }
167
robertphillips1b8e1b52015-06-24 06:54:10 -0700168private:
bsalomond309e7a2015-04-30 14:18:54 -0700169 GrResourceCache* fCache;
170 GrGpu* fGpu;
joshualitt6d0872d2016-01-11 08:27:48 -0800171
172 // In debug builds we guard against improper thread handling
173 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700174};
175
176#endif