blob: dd139cc0e71609c31f2641ce527ccc89de1143bc [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,
Robert Phillipsc787e492017-02-28 11:26:32 -050031 const GrMipLevel* texels, int mipLevelCount,
32 uint32_t flags = 0);
cblume55f2d2d2016-02-26 13:20:48 -080033
34 /**
35 * This function is a shim which creates a SkTArray<GrMipLevel> of size 1.
36 * It then calls createTexture with that SkTArray.
37 *
bsalomond309e7a2015-04-30 14:18:54 -070038 * @param srcData Pointer to the pixel values (optional).
39 * @param rowBytes The number of bytes between rows of the texture. Zero
40 * implies tightly packed rows. For compressed pixel configs, this
41 * field is ignored.
42 */
bsalomon5ec26ae2016-02-25 08:33:02 -080043 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* srcData,
Robert Phillipsc787e492017-02-28 11:26:32 -050044 size_t rowBytes, uint32_t flags = 0);
bsalomond309e7a2015-04-30 14:18:54 -070045
46 /** Shortcut for creating a texture with no initial data to upload. */
Robert Phillipsc787e492017-02-28 11:26:32 -050047 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, uint32_t flags = 0) {
48 return this->createTexture(desc, budgeted, nullptr, 0, flags);
bsalomond309e7a2015-04-30 14:18:54 -070049 }
50
51 /** Assigns a unique key to the texture. The texture will be findable via this key using
52 findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */
53 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
Robert Phillipsf7cf81a2017-03-02 10:23:52 -050054 SkASSERT(key.isValid());
bsalomond309e7a2015-04-30 14:18:54 -070055 this->assignUniqueKeyToResource(key, texture);
56 }
57
58 /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */
joshualitt6d0872d2016-01-11 08:27:48 -080059 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key);
bsalomond309e7a2015-04-30 14:18:54 -070060
61 /**
62 * Determines whether a texture is associated with the unique key. If the texture is found it
63 * will not be locked or returned. This call does not affect the priority of the resource for
64 * deletion.
65 */
66 bool existsTextureWithUniqueKey(const GrUniqueKey& key) const {
67 return this->existsResourceWithUniqueKey(key);
68 }
69
70 /**
bsalomoneae62002015-07-31 13:59:30 -070071 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
72 * and height as desc specifies. If desc specifies that the texture should be a render target
73 * then result will be a render target. Format and sample count will always match the request.
74 * The contents of the texture are undefined. The caller owns a ref on the returned texture and
75 * must balance with a call to unref.
bsalomond309e7a2015-04-30 14:18:54 -070076 */
Robert Phillipsc787e492017-02-28 11:26:32 -050077 GrTexture* createApproxTexture(const GrSurfaceDesc&, uint32_t flags = 0);
bsalomoneae62002015-07-31 13:59:30 -070078
79 /** Legacy function that no longer should be used. */
bsalomond309e7a2015-04-30 14:18:54 -070080 enum ScratchTexMatch {
bsalomond309e7a2015-04-30 14:18:54 -070081 kExact_ScratchTexMatch,
bsalomond309e7a2015-04-30 14:18:54 -070082 kApprox_ScratchTexMatch
83 };
Robert Phillipsc787e492017-02-28 11:26:32 -050084 GrTexture* refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match, uint32_t flags) {
bsalomoneae62002015-07-31 13:59:30 -070085 if (kApprox_ScratchTexMatch == match) {
Robert Phillipsc787e492017-02-28 11:26:32 -050086 return this->createApproxTexture(desc, flags);
bsalomoneae62002015-07-31 13:59:30 -070087 } else {
Robert Phillipsc787e492017-02-28 11:26:32 -050088 return this->createTexture(desc, SkBudgeted::kYes, flags);
bsalomoneae62002015-07-31 13:59:30 -070089 }
90 }
bsalomond309e7a2015-04-30 14:18:54 -070091
92 ///////////////////////////////////////////////////////////////////////////
93 // Wrapped Backend Surfaces
94
95 /**
96 * Wraps an existing texture with a GrTexture object.
97 *
98 * OpenGL: if the object is a texture Gr may change its GL texture params
99 * when it is drawn.
100 *
bsalomond309e7a2015-04-30 14:18:54 -0700101 * @return GrTexture object or NULL on failure.
102 */
bungeman6bd52842016-10-27 09:30:08 -0700103 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTextureDesc& desc,
104 GrWrapOwnership = kBorrow_GrWrapOwnership);
bsalomond309e7a2015-04-30 14:18:54 -0700105
106 /**
107 * Wraps an existing render target with a GrRenderTarget object. It is
108 * similar to wrapBackendTexture but can be used to draw into surfaces
109 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
bsalomon6dc6f5f2015-06-18 09:12:16 -0700110 * the client will resolve to a texture). Currently wrapped render targets
111 * always use the kBorrow_GrWrapOwnership semantics.
bsalomond309e7a2015-04-30 14:18:54 -0700112 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800113 * @return GrRenderTarget object or NULL on failure.
bsalomond309e7a2015-04-30 14:18:54 -0700114 */
bungeman6bd52842016-10-27 09:30:08 -0700115 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
bsalomond309e7a2015-04-30 14:18:54 -0700116
Robert Phillips784b7bf2016-12-09 13:35:02 -0500117 static const int kMinScratchTextureSize;
118
bsalomond309e7a2015-04-30 14:18:54 -0700119protected:
joshualitt6d0872d2016-01-11 08:27:48 -0800120 GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner);
bsalomond309e7a2015-04-30 14:18:54 -0700121
122 /**
123 * Assigns a unique key to a resource. If the key is associated with another resource that
124 * association is removed and replaced by this resource.
125 */
126 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
127
128 /**
129 * Finds a resource in the cache, based on the specified key. This is intended for use in
130 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
131 * caller must balance with a call to unref().
132 */
133 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
134
135 /**
136 * Determines whether a resource is in the cache. If the resource is found it
137 * will not be locked or returned. This call does not affect the priority of
138 * the resource for deletion.
139 */
140 bool existsResourceWithUniqueKey(const GrUniqueKey& key) const;
141
bsalomoneae62002015-07-31 13:59:30 -0700142 enum ScratchTextureFlags {
143 kExact_ScratchTextureFlag = 0x1,
144 kNoPendingIO_ScratchTextureFlag = 0x2, // (http://skbug.com/4156)
145 kNoCreate_ScratchTextureFlag = 0x4,
Brian Salomonfe647b22016-12-05 13:14:44 -0500146 kLastScratchTextureFlag = kNoCreate_ScratchTextureFlag
bsalomoneae62002015-07-31 13:59:30 -0700147 };
148
149 /** A common impl for GrTextureProvider and GrResourceProvider variants. */
150 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags);
151
152 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
bsalomond309e7a2015-04-30 14:18:54 -0700153
154 void abandon() {
155 fCache = NULL;
156 fGpu = NULL;
157 }
158
bsalomoned0bcad2015-05-04 10:36:42 -0700159 GrResourceCache* cache() { return fCache; }
160 const GrResourceCache* cache() const { return fCache; }
161
162 GrGpu* gpu() { return fGpu; }
163 const GrGpu* gpu() const { return fGpu; }
164
bsalomond309e7a2015-04-30 14:18:54 -0700165 bool isAbandoned() const {
166 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
167 return !SkToBool(fCache);
168 }
169
robertphillips1b8e1b52015-06-24 06:54:10 -0700170private:
bsalomond309e7a2015-04-30 14:18:54 -0700171 GrResourceCache* fCache;
172 GrGpu* fGpu;
joshualitt6d0872d2016-01-11 08:27:48 -0800173
174 // In debug builds we guard against improper thread handling
175 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700176};
177
178#endif