blob: b25efd95f53317bbdbf138508182bf890b90f675 [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
8#include "GrTextureProxy.h"
Brian Osman099fa0f2017-10-02 16:38:32 -04009#include "GrTextureProxyPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070010
Robert Phillipsae7d3f32017-09-21 08:26:08 -040011#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrContextPriv.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040013#include "GrDeferredProxyUploader.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050014#include "GrProxyProvider.h"
Greg Daniel849dce12018-04-24 14:32:53 -040015#include "GrSurfacePriv.h"
Robert Phillipsa4c41b32017-03-15 13:02:45 -040016#include "GrTexturePriv.h"
robertphillips76948d42016-05-04 12:47:41 -070017
Brian Salomon58389b92018-03-07 13:01:25 -050018// Deferred version - with data
19GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrMipMapped mipMapped,
20 SkBackingFit fit, SkBudgeted budgeted, const void* srcData,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040021 size_t /*rowBytes*/, GrInternalSurfaceFlags surfaceFlags)
22 : INHERITED(srcDesc, kTopLeft_GrSurfaceOrigin, fit, budgeted, surfaceFlags)
Greg Danielf6f7b672018-02-15 13:06:26 -050023 , fMipMapped(mipMapped)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040025 , fDeferredUploader(nullptr) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040026 SkASSERT(!srcData); // currently handled in Make()
robertphillips8abb3702016-08-31 14:04:06 -070027}
28
Brian Salomon58389b92018-03-07 13:01:25 -050029// Deferred version - no data
30GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrSurfaceOrigin origin,
31 GrMipMapped mipMapped, SkBackingFit fit, SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040032 GrInternalSurfaceFlags surfaceFlags)
33 : INHERITED(srcDesc, origin, fit, budgeted, surfaceFlags)
Brian Salomon58389b92018-03-07 13:01:25 -050034 , fMipMapped(mipMapped)
35 , fProxyProvider(nullptr)
36 , fDeferredUploader(nullptr) {}
37
Chris Dalton706a6ff2017-11-29 22:01:06 -070038// Lazy-callback version
Greg Daniel457469c2018-02-08 15:05:44 -050039GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
Brian Salomon2a4f9832018-03-03 22:43:43 -050040 const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
41 GrMipMapped mipMapped, SkBackingFit fit, SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040042 GrInternalSurfaceFlags surfaceFlags)
43 : INHERITED(std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050044 , fMipMapped(mipMapped)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050045 , fProxyProvider(nullptr)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040046 , fDeferredUploader(nullptr) {
47}
Chris Dalton706a6ff2017-11-29 22:01:06 -070048
49// Wrapped version
Robert Phillips066f0202017-07-25 10:16:35 -040050GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin)
51 : INHERITED(std::move(surf), origin, SkBackingFit::kExact)
Greg Daniele252f082017-10-23 16:05:23 -040052 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040054 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040055 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050056 fProxyProvider = fTarget->asTexture()->getContext()->contextPriv().proxyProvider();
57 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040058 }
59}
60
61GrTextureProxy::~GrTextureProxy() {
62 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
63 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
64 fTarget = nullptr;
65 if (fUniqueKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050066 fProxyProvider->processInvalidProxyUniqueKey(fUniqueKey, this, false);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040067 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040069 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040070}
robertphillips76948d42016-05-04 12:47:41 -070071
Robert Phillipseee4d6e2017-06-05 09:26:07 -040072bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050073 if (LazyState::kNot != this->lazyInstantiationState()) {
74 return false;
75 }
Brian Salomonbdecacf2018-02-02 20:32:49 -050076 if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false,
Greg Danield2d8e922018-02-12 12:07:39 -050077 kNone_GrSurfaceFlags, fMipMapped,
Robert Phillipsae7d3f32017-09-21 08:26:08 -040078 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040079 return false;
robertphillips76948d42016-05-04 12:47:41 -070080 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040081
Greg Daniele252f082017-10-23 16:05:23 -040082 SkASSERT(!fTarget->asRenderTarget());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040083 SkASSERT(fTarget->asTexture());
84 return true;
robertphillips76948d42016-05-04 12:47:41 -070085}
86
Robert Phillips5af44de2017-07-18 14:49:38 -040087sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050088 sk_sp<GrSurface> surface= this->createSurfaceImpl(resourceProvider, 1,
Robert Phillips65048132017-08-10 08:44:49 -040089 /* needsStencil = */ false,
90 kNone_GrSurfaceFlags,
Greg Danield2d8e922018-02-12 12:07:39 -050091 fMipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -040092 if (!surface) {
93 return nullptr;
94 }
95
Greg Daniele252f082017-10-23 16:05:23 -040096 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -040097 SkASSERT(surface->asTexture());
98 return surface;
99}
100
Brian Osman099fa0f2017-10-02 16:38:32 -0400101void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
102 SkASSERT(!fTextureProxy->fDeferredUploader);
103 fTextureProxy->fDeferredUploader = std::move(uploader);
104}
105
106void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -0500107 // The texture proxy's contents may already have been uploaded or instantiation may have failed
108 if (fTextureProxy->fDeferredUploader && fTextureProxy->fTarget) {
Brian Osman099fa0f2017-10-02 16:38:32 -0400109 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
110 }
111}
112
113void GrTextureProxyPriv::resetDeferredUploader() {
114 SkASSERT(fTextureProxy->fDeferredUploader);
115 fTextureProxy->fDeferredUploader.reset();
116}
117
Robert Phillips49081d12017-05-08 13:41:35 -0400118// This method parallels the highest_filter_mode functions in GrGLTexture & GrVkTexture.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400119GrSamplerState::Filter GrTextureProxy::highestFilterMode() const {
Robert Phillips49081d12017-05-08 13:41:35 -0400120 if (fTarget) {
121 return fTarget->asTexture()->texturePriv().highestFilterMode();
122 }
123
Robert Phillips49081d12017-05-08 13:41:35 -0400124 // In OpenGL, GR_GL_TEXTURE_RECTANGLE and GR_GL_TEXTURE_EXTERNAL (which have a highest filter
125 // mode of bilerp) can only be created via wrapping.
126
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400127 return GrSamplerState::Filter::kMipMap;
Robert Phillips49081d12017-05-08 13:41:35 -0400128}
129
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500130GrMipMapped GrTextureProxy::mipMapped() const {
131 if (this->priv().isInstantiated()) {
132 return this->priv().peekTexture()->texturePriv().mipMapped();
133 }
134 return fMipMapped;
135}
136
Brian Salomonbb5711a2017-05-17 13:49:59 -0400137size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700138 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500139 this->texPriv().proxyMipMapped(), !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400140}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400141
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500142void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400143 SkASSERT(key.isValid());
144 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
145
Robert Phillipsadbe1322018-01-17 13:35:46 -0500146 if (fTarget) {
147 if (!fTarget->getUniqueKey().isValid()) {
148 fTarget->resourcePriv().setUniqueKey(key);
149 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400150 SkASSERT(fTarget->getUniqueKey() == key);
151 }
152
153 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500154 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400155}
156
157void GrTextureProxy::clearUniqueKey() {
158 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500159 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400160}
161
Chris Dalton706a6ff2017-11-29 22:01:06 -0700162#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400163void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500164 SkASSERT(!surface->asRenderTarget());
165
166 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
167 SkASSERT(surface->asTexture());
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500168 SkASSERT(GrMipMapped::kNo == this->texPriv().proxyMipMapped() ||
Robert Phillips6ba15ec2018-02-08 16:26:47 -0500169 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Greg Daniel849dce12018-04-24 14:32:53 -0400170
171 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
172 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
173 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
174 (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700175}
176#endif
177