blob: e7b6e186691bcf0ea6f8999c17efc6e2c18a1306 [file] [log] [blame]
Robert Phillips84a81202016-11-04 11:59:10 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrTextureRenderTargetProxy.h"
Robert Phillips84a81202016-11-04 11:59:10 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrCaps.h"
Chris Dalton3f7932e2019-08-19 00:39:13 -060011#include "src/gpu/GrContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040012#include "src/gpu/GrRenderTarget.h"
Chris Dalton3f7932e2019-08-19 00:39:13 -060013#include "src/gpu/GrRenderTargetProxyPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrSurfacePriv.h"
15#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000016#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrTexturePriv.h"
18#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070019
Robert Phillips84a81202016-11-04 11:59:10 -040020// Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -050021// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040022// GrRenderTargetProxy) so its constructor must be explicitly called.
23GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
Greg Daniel4065d452018-11-16 15:43:41 -050024 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050025 SkISize dimensions,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040026 int sampleCnt,
Greg Danielf6f7b672018-02-15 13:06:26 -050027 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060028 GrMipMapsStatus mipMapsStatus,
Robert Phillips84a81202016-11-04 11:59:10 -040029 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050030 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040031 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -040032 GrInternalSurfaceFlags surfaceFlags,
33 UseAllocator useAllocator)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040034 : GrSurfaceProxy(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator)
Chris Dalton706a6ff2017-11-29 22:01:06 -070035 // for now textures w/ data are always wrapped
Brian Salomondf1bd6d2020-03-26 20:37:01 -040036 , GrRenderTargetProxy(caps, format, dimensions, sampleCnt, fit, budgeted, isProtected,
37 surfaceFlags, useAllocator)
38 , GrTextureProxy(format, dimensions, mipMapped, mipMapsStatus, fit, budgeted, isProtected,
39 surfaceFlags, useAllocator) {
Chris Dalton3f7932e2019-08-19 00:39:13 -060040 this->initSurfaceFlags(caps);
41}
Chris Dalton706a6ff2017-11-29 22:01:06 -070042
43// Lazy-callback version
Chris Dalton3f7932e2019-08-19 00:39:13 -060044GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
45 LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -050046 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050047 SkISize dimensions,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040048 int sampleCnt,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050049 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060050 GrMipMapsStatus mipMapsStatus,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050051 SkBackingFit fit,
52 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040053 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -040054 GrInternalSurfaceFlags surfaceFlags,
55 UseAllocator useAllocator)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040056 : GrSurfaceProxy(std::move(callback), format, dimensions, fit, budgeted, isProtected,
57 surfaceFlags, useAllocator)
Chris Dalton706a6ff2017-11-29 22:01:06 -070058 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
59 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Brian Salomondf1bd6d2020-03-26 20:37:01 -040060 , GrRenderTargetProxy(LazyInstantiateCallback(), format, dimensions, sampleCnt, fit,
61 budgeted, isProtected, surfaceFlags, useAllocator,
Greg Danielbaf8d992019-10-29 14:14:32 -040062 WrapsVkSecondaryCB::kNo)
Greg Daniel3a365112020-02-14 10:47:18 -050063 , GrTextureProxy(LazyInstantiateCallback(), format, dimensions, mipMapped, mipMapsStatus,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040064 fit, budgeted, isProtected, surfaceFlags, useAllocator) {
Chris Dalton3f7932e2019-08-19 00:39:13 -060065 this->initSurfaceFlags(caps);
66}
Robert Phillips84a81202016-11-04 11:59:10 -040067
68// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050069// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040070// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040071GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
Brian Salomonbeb7f522019-08-30 16:19:42 -040072 UseAllocator useAllocator)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040073 : GrSurfaceProxy(surf, SkBackingFit::kExact, useAllocator)
74 , GrRenderTargetProxy(surf, useAllocator)
75 , GrTextureProxy(surf, useAllocator) {
Robert Phillips37430132016-11-09 06:50:43 -050076 SkASSERT(surf->asTexture());
77 SkASSERT(surf->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -060078 SkASSERT(fSurfaceFlags == fTarget->surfacePriv().flags());
79 SkASSERT((this->numSamples() <= 1 ||
80 fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) !=
81 this->requiresManualMSAAResolve());
82}
83
84void GrTextureRenderTargetProxy::initSurfaceFlags(const GrCaps& caps) {
Jim Van Verth5fab9092019-11-25 21:23:53 +000085 // FBO 0 should never be wrapped as a texture render target.
86 SkASSERT(!this->rtPriv().glRTFBOIDIs0());
Chris Dalton3f7932e2019-08-19 00:39:13 -060087 if (this->numSamples() > 1 && !caps.msaaResolvesAutomatically()) {
88 // MSAA texture-render-targets always require manual resolve if we are not using a
89 // multisampled-render-to-texture extension.
90 //
91 // NOTE: This is the only instance where we need to set the manual resolve flag on a proxy.
92 // Any other proxies that require manual resolve (e.g., wrapBackendTextureAsRenderTarget())
93 // will be wrapped, and the wrapped version of the GrSurface constructor will automatically
94 // get the manual resolve flag when copying the target GrSurface's flags.
95 fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
96 }
Robert Phillips84a81202016-11-04 11:59:10 -040097}
98
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040099size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600100 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500101 if (colorSamplesPerPixel > 1) {
102 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -0500103 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500104 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400105
Robert Phillips84a81202016-11-04 11:59:10 -0400106 // TODO: do we have enough information to improve this worst case estimate?
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400107 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Greg Daniela00bcad2019-10-11 13:21:48 -0400108 colorSamplesPerPixel, this->proxyMipMapped(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500109 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -0400110}
111
Robert Phillips10d17212019-04-24 14:09:10 -0400112bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400113 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500114 return false;
115 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400116
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400117 const GrUniqueKey& key = this->getUniqueKey();
118
Chris Dalton0b68dda2019-11-07 21:08:03 -0700119 if (!this->instantiateImpl(resourceProvider, this->numSamples(), GrRenderable::kYes,
120 this->mipMapped(), key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400121 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400122 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400123 if (key.isValid()) {
124 SkASSERT(key == this->getUniqueKey());
125 }
126
Robert Phillipsb5204762019-06-19 14:12:13 -0400127 SkASSERT(this->peekRenderTarget());
128 SkASSERT(this->peekTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400129
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400130 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400131}
Robert Phillips5af44de2017-07-18 14:49:38 -0400132
133sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
134 GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -0700135 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numSamples(),
136 GrRenderable::kYes, this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400137 if (!surface) {
138 return nullptr;
139 }
140 SkASSERT(surface->asRenderTarget());
141 SkASSERT(surface->asTexture());
142
143 return surface;
144}
145
Brian Salomon63410e92020-03-23 18:32:50 -0400146GrSurfaceProxy::LazySurfaceDesc GrTextureRenderTargetProxy::callbackDesc() const {
147 SkISize dims;
148 SkBackingFit fit;
149 if (this->isFullyLazy()) {
150 fit = SkBackingFit::kApprox;
151 dims = {-1, -1};
152 } else {
153 fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox;
154 dims = this->dimensions();
155 }
156 return {
157 dims,
158 fit,
159 GrRenderable::kYes,
160 this->mipMapped(),
161 this->numSamples(),
162 this->backendFormat(),
163 this->isProtected(),
164 this->isBudgeted(),
165 };
166}
167
Chris Dalton706a6ff2017-11-29 22:01:06 -0700168#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400169void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500170 // Anything checked here should also be checking the GrTextureProxy version
171 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400172 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500173 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500174
175 // Anything checked here should also be checking the GrRenderTargetProxy version
176 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600177 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400178
Brian Salomonc67c31c2018-12-06 10:00:03 -0500179 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
180
Greg Daniel849dce12018-04-24 14:32:53 -0400181 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
182 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500183
184 // Only non-RT textures can be read only.
185 SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly));
186 SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly));
187
Chris Dalton3f7932e2019-08-19 00:39:13 -0600188 SkASSERT(((int)proxyFlags & kGrInternalTextureRenderTargetFlagsMask) ==
189 ((int)surfaceFlags & kGrInternalTextureRenderTargetFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700190}
191#endif
192