blob: 4884d450b94db242bcc66fb99ab7d19c9c6d080b [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"
robertphillips76948d42016-05-04 12:47:41 -07009
Robert Phillips4e105e22020-07-16 09:18:50 -040010#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040011#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrProxyProvider.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040013#include "src/gpu/GrSurface.h"
Adlai Hollera3987cc2021-02-05 15:52:28 -050014#include "src/gpu/GrSurfaceProxyPriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040015#include "src/gpu/GrTexture.h"
robertphillips76948d42016-05-04 12:47:41 -070016
Brian Salomon58389b92018-03-07 13:01:25 -050017// Deferred version - no data
Brian Salomonbeb7f522019-08-30 16:19:42 -040018GrTextureProxy::GrTextureProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050019 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -040020 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -040021 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -040022 SkBackingFit fit,
23 SkBudgeted budgeted,
24 GrProtected isProtected,
25 GrInternalSurfaceFlags surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -040026 UseAllocator useAllocator,
27 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040028 : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator)
Brian Salomon40a40622020-07-21 10:32:07 -040029 , fMipmapped(mipMapped)
Brian Salomon8c82a872020-07-21 12:09:58 -040030 , fMipmapStatus(mipmapStatus)
31 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040032 , fCreatingProvider(creatingProvider)
Adlai Hollera3987cc2021-02-05 15:52:28 -050033 , fProxyProvider(nullptr) {
Stephen White3c0a50f2020-01-16 18:19:54 -050034 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
Brian Salomonbe1084b2021-01-26 13:29:30 -050035 if (this->textureType() == GrTextureType::kExternal) {
36 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
37 }
Stephen White3c0a50f2020-01-16 18:19:54 -050038}
Brian Salomon58389b92018-03-07 13:01:25 -050039
Chris Dalton706a6ff2017-11-29 22:01:06 -070040// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040041GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback,
42 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050043 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -040044 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -040045 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -040046 SkBackingFit fit,
47 SkBudgeted budgeted,
48 GrProtected isProtected,
49 GrInternalSurfaceFlags surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -040050 UseAllocator useAllocator,
51 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040052 : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected,
53 surfaceFlags, useAllocator)
Brian Salomon40a40622020-07-21 10:32:07 -040054 , fMipmapped(mipMapped)
Brian Salomon8c82a872020-07-21 12:09:58 -040055 , fMipmapStatus(mipmapStatus)
56 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040057 , fCreatingProvider(creatingProvider)
Adlai Hollera3987cc2021-02-05 15:52:28 -050058 , fProxyProvider(nullptr) {
Stephen White3c0a50f2020-01-16 18:19:54 -050059 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
Brian Salomonbe1084b2021-01-26 13:29:30 -050060 if (this->textureType() == GrTextureType::kExternal) {
61 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
62 }
Stephen White3c0a50f2020-01-16 18:19:54 -050063}
Chris Dalton706a6ff2017-11-29 22:01:06 -070064
65// Wrapped version
Robert Phillipsf10b2a52020-05-15 10:20:04 -040066GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf,
67 UseAllocator useAllocator,
68 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040069 : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
Brian Salomon4cfae3b2020-07-23 10:33:24 -040070 , fMipmapped(fTarget->asTexture()->mipmapped())
71 , fMipmapStatus(fTarget->asTexture()->mipmapStatus())
Brian Salomon8c82a872020-07-21 12:09:58 -040072 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040073 , fCreatingProvider(creatingProvider)
Adlai Hollera3987cc2021-02-05 15:52:28 -050074 , fProxyProvider(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040075 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050076 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
Robert Phillipse5f73282019-06-18 17:15:04 -040077 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040078 }
Brian Salomonbe1084b2021-01-26 13:29:30 -050079 if (this->textureType() == GrTextureType::kExternal) {
80 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
81 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040082}
83
84GrTextureProxy::~GrTextureProxy() {
85 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
86 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
87 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040088
89 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
90 // proxy provider has gone away. In that case there is noone to send the invalid key
91 // message to (Note: in this case we don't want to remove its cached resource).
92 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050093 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
94 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040095 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050096 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040097 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040098}
robertphillips76948d42016-05-04 12:47:41 -070099
Robert Phillips10d17212019-04-24 14:09:10 -0400100bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400101 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500102 return false;
103 }
Brian Salomon40a40622020-07-21 10:32:07 -0400104 if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipmapped,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700105 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400106 return false;
robertphillips76948d42016-05-04 12:47:41 -0700107 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400108
Robert Phillipsb5204762019-06-19 14:12:13 -0400109 SkASSERT(!this->peekRenderTarget());
110 SkASSERT(this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400111 return true;
robertphillips76948d42016-05-04 12:47:41 -0700112}
113
Robert Phillips5af44de2017-07-18 14:49:38 -0400114sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -0700115 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo,
Brian Salomon40a40622020-07-21 10:32:07 -0400116 fMipmapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400117 if (!surface) {
118 return nullptr;
119 }
120
Greg Daniele252f082017-10-23 16:05:23 -0400121 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -0400122 SkASSERT(surface->asTexture());
123 return surface;
124}
125
Brian Salomon8c82a872020-07-21 12:09:58 -0400126GrMipmapped GrTextureProxy::mipmapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400127 if (this->isInstantiated()) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400128 return this->peekTexture()->mipmapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500129 }
Brian Salomon40a40622020-07-21 10:32:07 -0400130 return fMipmapped;
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500131}
132
Greg Daniel0eca74c2020-10-01 13:46:00 -0400133size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
134 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
135 /*colorSamplesPerPixel=*/1, this->proxyMipmapped(),
136 !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400137}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400138
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500139bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first,
140 const GrSurfaceProxy* second) {
Michael Ludwig08bd1f72019-12-02 14:27:21 -0500141 // In order to be compatible, the proxies should also have the same texture type. This is
142 // checked explicitly since the GrBackendFormat == operator does not compare texture type
Greg Danielc71c7962020-01-14 16:44:18 -0500143 return first->backendFormat().textureType() == second->backendFormat().textureType() &&
Greg Daniel45723ac2018-11-30 10:12:43 -0500144 first->backendFormat() == second->backendFormat();
145}
146
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500147void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400148 SkASSERT(key.isValid());
149 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
150
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400151 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500152 if (!fTarget->getUniqueKey().isValid()) {
153 fTarget->resourcePriv().setUniqueKey(key);
154 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400155 SkASSERT(fTarget->getUniqueKey() == key);
156 }
157
158 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500159 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400160}
161
162void GrTextureProxy::clearUniqueKey() {
163 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400165}
166
Brian Salomon63410e92020-03-23 18:32:50 -0400167GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const {
168 SkISize dims;
169 SkBackingFit fit;
170 if (this->isFullyLazy()) {
171 fit = SkBackingFit::kApprox;
172 dims = {-1, -1};
173 } else {
174 fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox;
175 dims = this->dimensions();
176 }
177 return {
178 dims,
179 fit,
180 GrRenderable::kNo,
Brian Salomon40a40622020-07-21 10:32:07 -0400181 fMipmapped,
Brian Salomon63410e92020-03-23 18:32:50 -0400182 1,
183 this->backendFormat(),
184 this->isProtected(),
185 this->isBudgeted(),
186 };
187}
188
Chris Dalton706a6ff2017-11-29 22:01:06 -0700189#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400190void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500191 SkASSERT(!surface->asRenderTarget());
192
193 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
194 SkASSERT(surface->asTexture());
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600195 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
Brian Salomon8c82a872020-07-21 12:09:58 -0400196 SkASSERT(GrMipmapped::kNo == this->proxyMipmapped() ||
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400197 GrMipmapped::kYes == surface->asTexture()->mipmapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500198
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400199 SkASSERT(surface->asTexture()->textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500200
201 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
Brian Salomonf7f54332020-07-28 09:23:35 -0400202 GrInternalSurfaceFlags surfaceFlags = surface->flags();
Chris Dalton3f7932e2019-08-19 00:39:13 -0600203 SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
204 ((int)surfaceFlags & kGrInternalTextureFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700205}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500206
Chris Dalton706a6ff2017-11-29 22:01:06 -0700207#endif
208