blob: e013bfff0fa74d331ce78db40836a6ee7e243bbc [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 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700101 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 */
113 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
114
115protected:
joshualitt6d0872d2016-01-11 08:27:48 -0800116 GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner);
bsalomond309e7a2015-04-30 14:18:54 -0700117
118 /**
119 * Assigns a unique key to a resource. If the key is associated with another resource that
120 * association is removed and replaced by this resource.
121 */
122 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
123
124 /**
125 * Finds a resource in the cache, based on the specified key. This is intended for use in
126 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
127 * caller must balance with a call to unref().
128 */
129 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
130
131 /**
132 * Determines whether a resource is in the cache. If the resource is found it
133 * will not be locked or returned. This call does not affect the priority of
134 * the resource for deletion.
135 */
136 bool existsResourceWithUniqueKey(const GrUniqueKey& key) const;
137
bsalomoneae62002015-07-31 13:59:30 -0700138 enum ScratchTextureFlags {
139 kExact_ScratchTextureFlag = 0x1,
140 kNoPendingIO_ScratchTextureFlag = 0x2, // (http://skbug.com/4156)
141 kNoCreate_ScratchTextureFlag = 0x4,
142 };
143
144 /** A common impl for GrTextureProvider and GrResourceProvider variants. */
145 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags);
146
147 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
bsalomond309e7a2015-04-30 14:18:54 -0700148
149 void abandon() {
150 fCache = NULL;
151 fGpu = NULL;
152 }
153
bsalomoned0bcad2015-05-04 10:36:42 -0700154 GrResourceCache* cache() { return fCache; }
155 const GrResourceCache* cache() const { return fCache; }
156
157 GrGpu* gpu() { return fGpu; }
158 const GrGpu* gpu() const { return fGpu; }
159
bsalomond309e7a2015-04-30 14:18:54 -0700160 bool isAbandoned() const {
161 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
162 return !SkToBool(fCache);
163 }
164
robertphillips1b8e1b52015-06-24 06:54:10 -0700165private:
bsalomond309e7a2015-04-30 14:18:54 -0700166 GrResourceCache* fCache;
167 GrGpu* fGpu;
joshualitt6d0872d2016-01-11 08:27:48 -0800168
169 // In debug builds we guard against improper thread handling
170 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700171};
172
173#endif