blob: 62a9e6e0aeb81817917b2a10c001268bd92e9a23 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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
Greg Danielf91aeb22019-06-18 09:58:02 -04008#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrTextureProxyPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
robertphillips76948d42016-05-04 12:47:41 -070017
Brian Salomon58389b92018-03-07 13:01:25 -050018// Deferred version - no data
Brian Salomonbeb7f522019-08-30 16:19:42 -040019GrTextureProxy::GrTextureProxy(const GrBackendFormat& format,
20 const GrSurfaceDesc& srcDesc,
21 GrSurfaceOrigin origin,
22 GrMipMapped mipMapped,
23 GrMipMapsStatus mipMapsStatus,
24 const GrSwizzle& textureSwizzle,
25 SkBackingFit fit,
26 SkBudgeted budgeted,
27 GrProtected isProtected,
28 GrInternalSurfaceFlags surfaceFlags,
29 UseAllocator useAllocator)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040030 : INHERITED(format, srcDesc, GrRenderable::kNo, origin, textureSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -040031 isProtected, surfaceFlags, useAllocator)
Brian Salomon58389b92018-03-07 13:01:25 -050032 , fMipMapped(mipMapped)
Brian Salomonbeb7f522019-08-30 16:19:42 -040033 , fMipMapsStatus(mipMapsStatus) SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
Brian Salomon58389b92018-03-07 13:01:25 -050034 , fProxyProvider(nullptr)
35 , fDeferredUploader(nullptr) {}
36
Chris Dalton706a6ff2017-11-29 22:01:06 -070037// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040038GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback,
39 const GrBackendFormat& format,
40 const GrSurfaceDesc& desc,
41 GrSurfaceOrigin origin,
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, desc, GrRenderable::kNo, origin, texSwizzle, fit,
51 budgeted, isProtected, surfaceFlags, useAllocator)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050052 , fMipMapped(mipMapped)
Brian Salomonbeb7f522019-08-30 16:19:42 -040053 , fMipMapsStatus(mipMapsStatus) SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
Robert Phillips1afd4cd2018-01-08 13:40:32 -050054 , fProxyProvider(nullptr)
Brian Salomon7226c232018-07-30 13:13:17 -040055 , fDeferredUploader(nullptr) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070056
57// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -040058GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf,
59 GrSurfaceOrigin origin,
60 const GrSwizzle& textureSwizzle,
61 UseAllocator useAllocator)
62 : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact, useAllocator)
Greg Daniele252f082017-10-23 16:05:23 -040063 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Chris Dalton95d8ceb2019-07-30 11:17:59 -060064 , fMipMapsStatus(fTarget->asTexture()->texturePriv().mipMapsStatus())
Brian Salomonbeb7f522019-08-30 16:19:42 -040065 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
Robert Phillips1afd4cd2018-01-08 13:40:32 -050066 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040067 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040068 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050069 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
Robert Phillipse5f73282019-06-18 17:15:04 -040070 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040071 }
72}
73
74GrTextureProxy::~GrTextureProxy() {
75 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
76 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
77 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040078
79 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
80 // proxy provider has gone away. In that case there is noone to send the invalid key
81 // message to (Note: in this case we don't want to remove its cached resource).
82 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050083 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
84 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040085 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040087 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040088}
robertphillips76948d42016-05-04 12:47:41 -070089
Robert Phillips10d17212019-04-24 14:09:10 -040090bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040091 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -050092 return false;
93 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -040094 if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false, GrRenderable::kNo,
95 fMipMapped, fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040096 return false;
robertphillips76948d42016-05-04 12:47:41 -070097 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040098
Robert Phillipsb5204762019-06-19 14:12:13 -040099 SkASSERT(!this->peekRenderTarget());
100 SkASSERT(this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400101 return true;
robertphillips76948d42016-05-04 12:47:41 -0700102}
103
Robert Phillips5af44de2017-07-18 14:49:38 -0400104sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400105 sk_sp<GrSurface> surface =
106 this->createSurfaceImpl(resourceProvider, 1,
107 /* needsStencil = */ false, GrRenderable::kNo, fMipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400108 if (!surface) {
109 return nullptr;
110 }
111
Greg Daniele252f082017-10-23 16:05:23 -0400112 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -0400113 SkASSERT(surface->asTexture());
114 return surface;
115}
116
Brian Osman099fa0f2017-10-02 16:38:32 -0400117void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
118 SkASSERT(!fTextureProxy->fDeferredUploader);
119 fTextureProxy->fDeferredUploader = std::move(uploader);
120}
121
122void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -0500123 // The texture proxy's contents may already have been uploaded or instantiation may have failed
Robert Phillipsb5204762019-06-19 14:12:13 -0400124 if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
Brian Osman099fa0f2017-10-02 16:38:32 -0400125 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
126 }
127}
128
129void GrTextureProxyPriv::resetDeferredUploader() {
130 SkASSERT(fTextureProxy->fDeferredUploader);
131 fTextureProxy->fDeferredUploader.reset();
132}
133
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400134GrSamplerState::Filter GrTextureProxy::highestFilterMode() const {
Brian Salomone632dfc2018-08-01 13:01:16 -0400135 return this->hasRestrictedSampling() ? GrSamplerState::Filter::kBilerp
136 : GrSamplerState::Filter::kMipMap;
Robert Phillips49081d12017-05-08 13:41:35 -0400137}
138
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500139GrMipMapped GrTextureProxy::mipMapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400140 if (this->isInstantiated()) {
141 return this->peekTexture()->texturePriv().mipMapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500142 }
143 return fMipMapped;
144}
145
Brian Salomonbb5711a2017-05-17 13:49:59 -0400146size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700147 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400148 this->proxyMipMapped(), !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400149}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400150
Greg Daniel45723ac2018-11-30 10:12:43 -0500151bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
152 const GrTextureProxy* second) {
153 return first->config() == second->config() &&
154 first->textureType() == second->textureType() &&
155 first->backendFormat() == second->backendFormat();
156}
157
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500158void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400159 SkASSERT(key.isValid());
160 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
161
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400162 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500163 if (!fTarget->getUniqueKey().isValid()) {
164 fTarget->resourcePriv().setUniqueKey(key);
165 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400166 SkASSERT(fTarget->getUniqueKey() == key);
167 }
168
169 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500170 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400171}
172
173void GrTextureProxy::clearUniqueKey() {
174 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500175 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400176}
177
Chris Dalton706a6ff2017-11-29 22:01:06 -0700178#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400179void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500180 SkASSERT(!surface->asRenderTarget());
181
182 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
183 SkASSERT(surface->asTexture());
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600184 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400185 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips6ba15ec2018-02-08 16:26:47 -0500186 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500187
Greg Daniel4065d452018-11-16 15:43:41 -0500188 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500189
190 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
191 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Chris Dalton3f7932e2019-08-19 00:39:13 -0600192 SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
193 ((int)surfaceFlags & kGrInternalTextureFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700194}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500195
Chris Dalton706a6ff2017-11-29 22:01:06 -0700196#endif
197