1 /*
2 * Copyright 2016 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 #include "src/gpu/GrTextureProxy.h"
9 #include "src/gpu/GrTextureProxyPriv.h"
10
11 #include "include/gpu/GrContext.h"
12 #include "src/gpu/GrContextPriv.h"
13 #include "src/gpu/GrDeferredProxyUploader.h"
14 #include "src/gpu/GrProxyProvider.h"
15 #include "src/gpu/GrSurfacePriv.h"
16 #include "src/gpu/GrTexturePriv.h"
17
18 // Deferred version - no data
GrTextureProxy(const GrBackendFormat & format,SkISize dimensions,GrMipMapped mipMapped,GrMipMapsStatus mipMapsStatus,const GrSwizzle & textureSwizzle,SkBackingFit fit,SkBudgeted budgeted,GrProtected isProtected,GrInternalSurfaceFlags surfaceFlags,UseAllocator useAllocator)19 GrTextureProxy::GrTextureProxy(const GrBackendFormat& format,
20 SkISize dimensions,
21 GrMipMapped mipMapped,
22 GrMipMapsStatus mipMapsStatus,
23 const GrSwizzle& textureSwizzle,
24 SkBackingFit fit,
25 SkBudgeted budgeted,
26 GrProtected isProtected,
27 GrInternalSurfaceFlags surfaceFlags,
28 UseAllocator useAllocator)
29 : INHERITED(format, dimensions, GrRenderable::kNo, textureSwizzle, fit, budgeted,
30 isProtected, surfaceFlags, useAllocator)
31 , fMipMapped(mipMapped)
32 , fMipMapsStatus(mipMapsStatus) SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
33 , fProxyProvider(nullptr)
34 , fDeferredUploader(nullptr) {
35 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
36 }
37
38 // Lazy-callback version
GrTextureProxy(LazyInstantiateCallback && callback,const GrBackendFormat & format,SkISize dimensions,GrMipMapped mipMapped,GrMipMapsStatus mipMapsStatus,const GrSwizzle & texSwizzle,SkBackingFit fit,SkBudgeted budgeted,GrProtected isProtected,GrInternalSurfaceFlags surfaceFlags,UseAllocator useAllocator)39 GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback,
40 const GrBackendFormat& format,
41 SkISize dimensions,
42 GrMipMapped mipMapped,
43 GrMipMapsStatus mipMapsStatus,
44 const GrSwizzle& texSwizzle,
45 SkBackingFit fit,
46 SkBudgeted budgeted,
47 GrProtected isProtected,
48 GrInternalSurfaceFlags surfaceFlags,
49 UseAllocator useAllocator)
50 : INHERITED(std::move(callback), format, dimensions, GrRenderable::kNo, texSwizzle, fit,
51 budgeted, isProtected, surfaceFlags, useAllocator)
52 , fMipMapped(mipMapped)
53 , fMipMapsStatus(mipMapsStatus) SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
54 , fProxyProvider(nullptr)
55 , fDeferredUploader(nullptr) {
56 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
57 }
58
59 // Wrapped version
GrTextureProxy(sk_sp<GrSurface> surf,const GrSwizzle & textureSwizzle,UseAllocator useAllocator)60 GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf,
61 const GrSwizzle& textureSwizzle,
62 UseAllocator useAllocator)
63 : INHERITED(std::move(surf), textureSwizzle, SkBackingFit::kExact, useAllocator)
64 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
65 , fMipMapsStatus(fTarget->asTexture()->texturePriv().mipMapsStatus())
66 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
67 , fProxyProvider(nullptr)
68 , fDeferredUploader(nullptr) {
69 if (fTarget->getUniqueKey().isValid()) {
70 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
71 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
72 }
73 }
74
~GrTextureProxy()75 GrTextureProxy::~GrTextureProxy() {
76 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
77 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
78 fTarget = nullptr;
79
80 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
81 // proxy provider has gone away. In that case there is noone to send the invalid key
82 // message to (Note: in this case we don't want to remove its cached resource).
83 if (fUniqueKey.isValid() && fProxyProvider) {
84 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
85 GrProxyProvider::InvalidateGPUResource::kNo);
86 } else {
87 SkASSERT(!fProxyProvider);
88 }
89 }
90
instantiate(GrResourceProvider * resourceProvider)91 bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
92 if (this->isLazy()) {
93 return false;
94 }
95 if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipMapped,
96 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
97 return false;
98 }
99
100 SkASSERT(!this->peekRenderTarget());
101 SkASSERT(this->peekTexture());
102 return true;
103 }
104
createSurface(GrResourceProvider * resourceProvider) const105 sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
106 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo,
107 fMipMapped);
108 if (!surface) {
109 return nullptr;
110 }
111
112 SkASSERT(!surface->asRenderTarget());
113 SkASSERT(surface->asTexture());
114 return surface;
115 }
116
setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader)117 void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
118 SkASSERT(!fTextureProxy->fDeferredUploader);
119 fTextureProxy->fDeferredUploader = std::move(uploader);
120 }
121
scheduleUpload(GrOpFlushState * flushState)122 void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
123 // The texture proxy's contents may already have been uploaded or instantiation may have failed
124 if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
125 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
126 }
127 }
128
resetDeferredUploader()129 void GrTextureProxyPriv::resetDeferredUploader() {
130 SkASSERT(fTextureProxy->fDeferredUploader);
131 fTextureProxy->fDeferredUploader.reset();
132 }
133
mipMapped() const134 GrMipMapped GrTextureProxy::mipMapped() const {
135 if (this->isInstantiated()) {
136 return this->peekTexture()->texturePriv().mipMapped();
137 }
138 return fMipMapped;
139 }
140
onUninstantiatedGpuMemorySize(const GrCaps & caps) const141 size_t GrTextureProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const {
142 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1,
143 this->proxyMipMapped(), !this->priv().isExact());
144 }
145
HighestFilterMode(GrTextureType textureType)146 GrSamplerState::Filter GrTextureProxy::HighestFilterMode(GrTextureType textureType) {
147 return GrTextureTypeHasRestrictedSampling(textureType) ? GrSamplerState::Filter::kBilerp
148 : GrSamplerState::Filter::kMipMap;
149 }
150
ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy * first,const GrSurfaceProxy * second)151 bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first,
152 const GrSurfaceProxy* second) {
153 // In order to be compatible, the proxies should also have the same texture type. This is
154 // checked explicitly since the GrBackendFormat == operator does not compare texture type
155 return first->backendFormat().textureType() == second->backendFormat().textureType() &&
156 first->backendFormat() == second->backendFormat();
157 }
158
setUniqueKey(GrProxyProvider * proxyProvider,const GrUniqueKey & key)159 void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
160 SkASSERT(key.isValid());
161 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
162
163 if (fTarget && fSyncTargetKey) {
164 if (!fTarget->getUniqueKey().isValid()) {
165 fTarget->resourcePriv().setUniqueKey(key);
166 }
167 SkASSERT(fTarget->getUniqueKey() == key);
168 }
169
170 fUniqueKey = key;
171 fProxyProvider = proxyProvider;
172 }
173
clearUniqueKey()174 void GrTextureProxy::clearUniqueKey() {
175 fUniqueKey.reset();
176 fProxyProvider = nullptr;
177 }
178
179 #ifdef SK_DEBUG
onValidateSurface(const GrSurface * surface)180 void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
181 SkASSERT(!surface->asRenderTarget());
182
183 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
184 SkASSERT(surface->asTexture());
185 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
186 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
187 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
188
189 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
190
191 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
192 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
193 SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
194 ((int)surfaceFlags & kGrInternalTextureFlagsMask));
195 }
196
197 #endif
198
199