blob: e75589ec97ab030c69d6795c6b0e3a334f280e9a [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"
reedc9b5f8b2015-10-22 13:20:20 -070012#include "SkImageFilter.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 *
25 * @param desc Description of the texture properties.
26 * @param budgeted Does the texture count against the resource cache budget?
27 * @param srcData Pointer to the pixel values (optional).
28 * @param rowBytes The number of bytes between rows of the texture. Zero
29 * implies tightly packed rows. For compressed pixel configs, this
30 * field is ignored.
31 */
bsalomon5ec26ae2016-02-25 08:33:02 -080032 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, const void* srcData,
bsalomond309e7a2015-04-30 14:18:54 -070033 size_t rowBytes);
34
35 /** Shortcut for creating a texture with no initial data to upload. */
bsalomon5ec26ae2016-02-25 08:33:02 -080036 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
bsalomond309e7a2015-04-30 14:18:54 -070037 return this->createTexture(desc, budgeted, NULL, 0);
38 }
39
40 /** Assigns a unique key to the texture. The texture will be findable via this key using
41 findTextureByUniqueKey(). If an existing texture has this key, it's key will be removed. */
42 void assignUniqueKeyToTexture(const GrUniqueKey& key, GrTexture* texture) {
43 this->assignUniqueKeyToResource(key, texture);
44 }
45
46 /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */
joshualitt6d0872d2016-01-11 08:27:48 -080047 GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key);
bsalomond309e7a2015-04-30 14:18:54 -070048
49 /**
50 * Determines whether a texture is associated with the unique key. If the texture is found it
51 * will not be locked or returned. This call does not affect the priority of the resource for
52 * deletion.
53 */
54 bool existsTextureWithUniqueKey(const GrUniqueKey& key) const {
55 return this->existsResourceWithUniqueKey(key);
56 }
57
58 /**
bsalomoneae62002015-07-31 13:59:30 -070059 * Finds a texture that approximately matches the descriptor. Will be at least as large in width
60 * and height as desc specifies. If desc specifies that the texture should be a render target
61 * then result will be a render target. Format and sample count will always match the request.
62 * The contents of the texture are undefined. The caller owns a ref on the returned texture and
63 * must balance with a call to unref.
bsalomond309e7a2015-04-30 14:18:54 -070064 */
bsalomoneae62002015-07-31 13:59:30 -070065 GrTexture* createApproxTexture(const GrSurfaceDesc&);
66
67 /** Legacy function that no longer should be used. */
bsalomond309e7a2015-04-30 14:18:54 -070068 enum ScratchTexMatch {
bsalomond309e7a2015-04-30 14:18:54 -070069 kExact_ScratchTexMatch,
bsalomond309e7a2015-04-30 14:18:54 -070070 kApprox_ScratchTexMatch
71 };
bsalomoneae62002015-07-31 13:59:30 -070072 GrTexture* refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match) {
73 if (kApprox_ScratchTexMatch == match) {
74 return this->createApproxTexture(desc);
75 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -080076 return this->createTexture(desc, SkBudgeted::kYes);
bsalomoneae62002015-07-31 13:59:30 -070077 }
78 }
bsalomond309e7a2015-04-30 14:18:54 -070079
80 ///////////////////////////////////////////////////////////////////////////
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 *
bsalomond309e7a2015-04-30 14:18:54 -070089 * @return GrTexture object or NULL on failure.
90 */
bsalomon6dc6f5f2015-06-18 09:12:16 -070091 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc,
92 GrWrapOwnership = kBorrow_GrWrapOwnership);
bsalomond309e7a2015-04-30 14:18:54 -070093
94 /**
95 * Wraps an existing render target with a GrRenderTarget object. It is
96 * similar to wrapBackendTexture but can be used to draw into surfaces
97 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
bsalomon6dc6f5f2015-06-18 09:12:16 -070098 * the client will resolve to a texture). Currently wrapped render targets
99 * always use the kBorrow_GrWrapOwnership semantics.
bsalomond309e7a2015-04-30 14:18:54 -0700100 *
ericrkf7b8b8a2016-02-24 14:49:51 -0800101 * @return GrRenderTarget object or NULL on failure.
bsalomond309e7a2015-04-30 14:18:54 -0700102 */
103 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
104
105protected:
joshualitt6d0872d2016-01-11 08:27:48 -0800106 GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner);
bsalomond309e7a2015-04-30 14:18:54 -0700107
108 /**
109 * Assigns a unique key to a resource. If the key is associated with another resource that
110 * association is removed and replaced by this resource.
111 */
112 void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
113
114 /**
115 * Finds a resource in the cache, based on the specified key. This is intended for use in
116 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
117 * caller must balance with a call to unref().
118 */
119 GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
120
121 /**
122 * Determines whether a resource is in the cache. If the resource is found it
123 * will not be locked or returned. This call does not affect the priority of
124 * the resource for deletion.
125 */
126 bool existsResourceWithUniqueKey(const GrUniqueKey& key) const;
127
bsalomoneae62002015-07-31 13:59:30 -0700128 enum ScratchTextureFlags {
129 kExact_ScratchTextureFlag = 0x1,
130 kNoPendingIO_ScratchTextureFlag = 0x2, // (http://skbug.com/4156)
131 kNoCreate_ScratchTextureFlag = 0x4,
132 };
133
134 /** A common impl for GrTextureProvider and GrResourceProvider variants. */
135 GrTexture* internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchTextureFlags);
136
137 GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
bsalomond309e7a2015-04-30 14:18:54 -0700138
139 void abandon() {
140 fCache = NULL;
141 fGpu = NULL;
142 }
143
bsalomoned0bcad2015-05-04 10:36:42 -0700144 GrResourceCache* cache() { return fCache; }
145 const GrResourceCache* cache() const { return fCache; }
146
147 GrGpu* gpu() { return fGpu; }
148 const GrGpu* gpu() const { return fGpu; }
149
bsalomond309e7a2015-04-30 14:18:54 -0700150 bool isAbandoned() const {
151 SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
152 return !SkToBool(fCache);
153 }
154
robertphillips1b8e1b52015-06-24 06:54:10 -0700155private:
bsalomond309e7a2015-04-30 14:18:54 -0700156 GrResourceCache* fCache;
157 GrGpu* fGpu;
joshualitt6d0872d2016-01-11 08:27:48 -0800158
159 // In debug builds we guard against improper thread handling
160 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
bsalomond309e7a2015-04-30 14:18:54 -0700161};
162
163#endif