blob: 667588402eb8d5e9d69894a773be182a1861f857 [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 "include/gpu/GrTexture.h"
11#include "src/gpu/GrCaps.h"
Chris Dalton3f7932e2019-08-19 00:39:13 -060012#include "src/gpu/GrContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040013#include "src/gpu/GrRenderTarget.h"
Chris Dalton3f7932e2019-08-19 00:39:13 -060014#include "src/gpu/GrRenderTargetProxyPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrSurfacePriv.h"
16#include "src/gpu/GrSurfaceProxyPriv.h"
17#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,
Robert Phillips84a81202016-11-04 11:59:10 -040025 const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040026 int sampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -050027 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -050028 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060029 GrMipMapsStatus mipMapsStatus,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040030 const GrSwizzle& texSwizzle,
Robert Phillips84a81202016-11-04 11:59:10 -040031 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050032 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040033 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -040034 GrInternalSurfaceFlags surfaceFlags,
35 UseAllocator useAllocator)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040036 : GrSurfaceProxy(format, desc, GrRenderable::kYes, origin, texSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -040037 isProtected, surfaceFlags, useAllocator)
Chris Dalton706a6ff2017-11-29 22:01:06 -070038 // for now textures w/ data are always wrapped
Greg Danielbaf8d992019-10-29 14:14:32 -040039 , GrRenderTargetProxy(caps, format, desc, sampleCnt, origin, texSwizzle, fit, budgeted,
40 isProtected, surfaceFlags, useAllocator)
Chris Dalton95d8ceb2019-07-30 11:17:59 -060041 , GrTextureProxy(format, desc, origin, mipMapped, mipMapsStatus, texSwizzle, fit, budgeted,
Brian Salomonbeb7f522019-08-30 16:19:42 -040042 isProtected, surfaceFlags, useAllocator) {
Chris Dalton3f7932e2019-08-19 00:39:13 -060043 this->initSurfaceFlags(caps);
44}
Chris Dalton706a6ff2017-11-29 22:01:06 -070045
46// Lazy-callback version
Chris Dalton3f7932e2019-08-19 00:39:13 -060047GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
48 LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -050049 const GrBackendFormat& format,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050050 const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040051 int sampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -050052 GrSurfaceOrigin origin,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050053 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060054 GrMipMapsStatus mipMapsStatus,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040055 const GrSwizzle& texSwizzle,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050056 SkBackingFit fit,
57 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040058 GrProtected isProtected,
Brian Salomonbeb7f522019-08-30 16:19:42 -040059 GrInternalSurfaceFlags surfaceFlags,
60 UseAllocator useAllocator)
61 : GrSurfaceProxy(std::move(callback), format, desc, GrRenderable::kYes, origin, texSwizzle,
62 fit, budgeted, isProtected, surfaceFlags, useAllocator)
Chris Dalton706a6ff2017-11-29 22:01:06 -070063 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
64 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Brian Salomonbeb7f522019-08-30 16:19:42 -040065 , GrRenderTargetProxy(LazyInstantiateCallback(), format, desc, sampleCnt, origin,
Greg Danielbaf8d992019-10-29 14:14:32 -040066 texSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator,
67 WrapsVkSecondaryCB::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -040068 , GrTextureProxy(LazyInstantiateCallback(), format, desc, origin, mipMapped, mipMapsStatus,
69 texSwizzle, fit, budgeted, isProtected, surfaceFlags, useAllocator) {
Chris Dalton3f7932e2019-08-19 00:39:13 -060070 this->initSurfaceFlags(caps);
71}
Robert Phillips84a81202016-11-04 11:59:10 -040072
73// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050074// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040075// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040076GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040077 GrSurfaceOrigin origin,
78 const GrSwizzle& texSwizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -040079 UseAllocator useAllocator)
80 : GrSurfaceProxy(surf, origin, texSwizzle, SkBackingFit::kExact, useAllocator)
Greg Danielbaf8d992019-10-29 14:14:32 -040081 , GrRenderTargetProxy(surf, origin, texSwizzle, useAllocator)
Brian Salomonbeb7f522019-08-30 16:19:42 -040082 , GrTextureProxy(surf, origin, texSwizzle, useAllocator) {
Robert Phillips37430132016-11-09 06:50:43 -050083 SkASSERT(surf->asTexture());
84 SkASSERT(surf->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -060085 SkASSERT(fSurfaceFlags == fTarget->surfacePriv().flags());
86 SkASSERT((this->numSamples() <= 1 ||
87 fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) !=
88 this->requiresManualMSAAResolve());
89}
90
91void GrTextureRenderTargetProxy::initSurfaceFlags(const GrCaps& caps) {
Jim Van Verth5fab9092019-11-25 21:23:53 +000092 // FBO 0 should never be wrapped as a texture render target.
93 SkASSERT(!this->rtPriv().glRTFBOIDIs0());
Chris Dalton3f7932e2019-08-19 00:39:13 -060094 if (this->numSamples() > 1 && !caps.msaaResolvesAutomatically()) {
95 // MSAA texture-render-targets always require manual resolve if we are not using a
96 // multisampled-render-to-texture extension.
97 //
98 // NOTE: This is the only instance where we need to set the manual resolve flag on a proxy.
99 // Any other proxies that require manual resolve (e.g., wrapBackendTextureAsRenderTarget())
100 // will be wrapped, and the wrapped version of the GrSurface constructor will automatically
101 // get the manual resolve flag when copying the target GrSurface's flags.
102 fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
103 }
Robert Phillips84a81202016-11-04 11:59:10 -0400104}
105
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400106size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600107 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500108 if (colorSamplesPerPixel > 1) {
109 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -0500110 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500111 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400112
Robert Phillips84a81202016-11-04 11:59:10 -0400113 // TODO: do we have enough information to improve this worst case estimate?
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400114 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Greg Daniela00bcad2019-10-11 13:21:48 -0400115 colorSamplesPerPixel, this->proxyMipMapped(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500116 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -0400117}
118
Robert Phillips10d17212019-04-24 14:09:10 -0400119bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400120 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500121 return false;
122 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400123
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400124 const GrUniqueKey& key = this->getUniqueKey();
125
Chris Dalton0b68dda2019-11-07 21:08:03 -0700126 if (!this->instantiateImpl(resourceProvider, this->numSamples(), GrRenderable::kYes,
127 this->mipMapped(), key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400128 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400129 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400130 if (key.isValid()) {
131 SkASSERT(key == this->getUniqueKey());
132 }
133
Robert Phillipsb5204762019-06-19 14:12:13 -0400134 SkASSERT(this->peekRenderTarget());
135 SkASSERT(this->peekTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400136
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400137 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400138}
Robert Phillips5af44de2017-07-18 14:49:38 -0400139
140sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
141 GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -0700142 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numSamples(),
143 GrRenderable::kYes, this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400144 if (!surface) {
145 return nullptr;
146 }
147 SkASSERT(surface->asRenderTarget());
148 SkASSERT(surface->asTexture());
149
150 return surface;
151}
152
Chris Dalton706a6ff2017-11-29 22:01:06 -0700153#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400154void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500155 // Anything checked here should also be checking the GrTextureProxy version
156 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400157 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500158 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500159
160 // Anything checked here should also be checking the GrRenderTargetProxy version
161 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600162 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400163
Brian Salomonc67c31c2018-12-06 10:00:03 -0500164 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
165
Greg Daniel849dce12018-04-24 14:32:53 -0400166 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
167 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500168
169 // Only non-RT textures can be read only.
170 SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly));
171 SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly));
172
Chris Dalton3f7932e2019-08-19 00:39:13 -0600173 SkASSERT(((int)proxyFlags & kGrInternalTextureRenderTargetFlagsMask) ==
174 ((int)surfaceFlags & kGrInternalTextureRenderTargetFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700175}
176#endif
177