blob: fb5422763ff80b5d43346c09d6feedeefbb38437 [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,
31 const GrSwizzle& outSwizzle,
Robert Phillips84a81202016-11-04 11:59:10 -040032 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050033 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040034 GrProtected isProtected,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040035 GrInternalSurfaceFlags surfaceFlags)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040036 : GrSurfaceProxy(format, desc, GrRenderable::kYes, origin, texSwizzle, fit, budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040037 isProtected, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070038 // for now textures w/ data are always wrapped
Brian Salomon27b4d8d2019-07-22 14:23:45 -040039 , GrRenderTargetProxy(caps, format, desc, sampleCnt, origin, texSwizzle, outSwizzle, fit,
40 budgeted, isProtected, surfaceFlags)
Chris Dalton95d8ceb2019-07-30 11:17:59 -060041 , GrTextureProxy(format, desc, origin, mipMapped, mipMapsStatus, texSwizzle, fit, budgeted,
Chris Dalton3f7932e2019-08-19 00:39:13 -060042 isProtected, surfaceFlags) {
43 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 Daniel457469c2018-02-08 15:05:44 -050049 LazyInstantiationType lazyType,
Greg Daniel4065d452018-11-16 15:43:41 -050050 const GrBackendFormat& format,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050051 const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040052 int sampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -050053 GrSurfaceOrigin origin,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050054 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060055 GrMipMapsStatus mipMapsStatus,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040056 const GrSwizzle& texSwizzle,
57 const GrSwizzle& outSwizzle,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050058 SkBackingFit fit,
59 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040060 GrProtected isProtected,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040061 GrInternalSurfaceFlags surfaceFlags)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040062 : GrSurfaceProxy(std::move(callback), lazyType, format, desc, GrRenderable::kYes, origin,
Brian Salomone8a766b2019-07-19 14:24:36 -040063 texSwizzle, fit, budgeted, isProtected, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070064 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
65 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Brian Salomon27b4d8d2019-07-22 14:23:45 -040066 , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, format, desc, sampleCnt, origin,
67 texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags,
Brian Salomone8a766b2019-07-19 14:24:36 -040068 WrapsVkSecondaryCB::kNo)
Greg Daniel4065d452018-11-16 15:43:41 -050069 , GrTextureProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, mipMapped,
Chris Dalton3f7932e2019-08-19 00:39:13 -060070 mipMapsStatus, texSwizzle, fit, budgeted, isProtected, surfaceFlags) {
71 this->initSurfaceFlags(caps);
72}
Robert Phillips84a81202016-11-04 11:59:10 -040073
74// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050075// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040076// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040077GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040078 GrSurfaceOrigin origin,
79 const GrSwizzle& texSwizzle,
80 const GrSwizzle& outSwizzle)
81 : GrSurfaceProxy(surf, origin, texSwizzle, SkBackingFit::kExact)
82 , GrRenderTargetProxy(surf, origin, texSwizzle, outSwizzle)
83 , GrTextureProxy(surf, origin, texSwizzle) {
Robert Phillips37430132016-11-09 06:50:43 -050084 SkASSERT(surf->asTexture());
85 SkASSERT(surf->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -060086 SkASSERT(fSurfaceFlags == fTarget->surfacePriv().flags());
87 SkASSERT((this->numSamples() <= 1 ||
88 fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) !=
89 this->requiresManualMSAAResolve());
90}
91
92void GrTextureRenderTargetProxy::initSurfaceFlags(const GrCaps& caps) {
93 // FBO 0 should never be wrapped as a texture render target.
94 SkASSERT(!this->rtPriv().glRTFBOIDIs0());
95 if (this->numSamples() > 1 && !caps.msaaResolvesAutomatically()) {
96 // MSAA texture-render-targets always require manual resolve if we are not using a
97 // multisampled-render-to-texture extension.
98 //
99 // NOTE: This is the only instance where we need to set the manual resolve flag on a proxy.
100 // Any other proxies that require manual resolve (e.g., wrapBackendTextureAsRenderTarget())
101 // will be wrapped, and the wrapped version of the GrSurface constructor will automatically
102 // get the manual resolve flag when copying the target GrSurface's flags.
103 fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
104 }
Robert Phillips84a81202016-11-04 11:59:10 -0400105}
106
Brian Salomonbb5711a2017-05-17 13:49:59 -0400107size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600108 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500109 if (colorSamplesPerPixel > 1) {
110 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -0500111 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500112 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400113
Robert Phillips84a81202016-11-04 11:59:10 -0400114 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -0700115 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400116 colorSamplesPerPixel, this->proxyMipMapped(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500117 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -0400118}
119
Robert Phillips10d17212019-04-24 14:09:10 -0400120bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500121 if (LazyState::kNot != this->lazyInstantiationState()) {
122 return false;
123 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400124
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400125 const GrUniqueKey& key = this->getUniqueKey();
126
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400127 if (!this->instantiateImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
128 GrRenderable::kYes, this->mipMapped(),
129 key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400130 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400131 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400132 if (key.isValid()) {
133 SkASSERT(key == this->getUniqueKey());
134 }
135
Robert Phillipsb5204762019-06-19 14:12:13 -0400136 SkASSERT(this->peekRenderTarget());
137 SkASSERT(this->peekTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400138
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400139 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400140}
Robert Phillips5af44de2017-07-18 14:49:38 -0400141
142sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
143 GrResourceProvider* resourceProvider) const {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400144 sk_sp<GrSurface> surface =
145 this->createSurfaceImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
146 GrRenderable::kYes, this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400147 if (!surface) {
148 return nullptr;
149 }
150 SkASSERT(surface->asRenderTarget());
151 SkASSERT(surface->asTexture());
152
153 return surface;
154}
155
Chris Dalton706a6ff2017-11-29 22:01:06 -0700156#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400157void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500158 // Anything checked here should also be checking the GrTextureProxy version
159 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400160 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500161 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500162
163 // Anything checked here should also be checking the GrRenderTargetProxy version
164 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600165 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400166
Brian Salomonc67c31c2018-12-06 10:00:03 -0500167 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
168
Greg Daniel849dce12018-04-24 14:32:53 -0400169 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
170 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500171
172 // Only non-RT textures can be read only.
173 SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly));
174 SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly));
175
Chris Dalton3f7932e2019-08-19 00:39:13 -0600176 SkASSERT(((int)proxyFlags & kGrInternalTextureRenderTargetFlagsMask) ==
177 ((int)surfaceFlags & kGrInternalTextureRenderTargetFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700178}
179#endif
180