blob: 3d17a67f2ef5339b4ae88f3b357f56351f0e277a [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/GrRenderTarget.h"
11#include "include/gpu/GrTexture.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrSurfacePriv.h"
14#include "src/gpu/GrSurfaceProxyPriv.h"
15#include "src/gpu/GrTexturePriv.h"
16#include "src/gpu/GrTextureProxyPriv.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070017
Robert Phillips84a81202016-11-04 11:59:10 -040018// Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -050019// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040020// GrRenderTargetProxy) so its constructor must be explicitly called.
21GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
Greg Daniel4065d452018-11-16 15:43:41 -050022 const GrBackendFormat& format,
Robert Phillips84a81202016-11-04 11:59:10 -040023 const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040024 int sampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -050025 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -050026 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060027 GrMipMapsStatus mipMapsStatus,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040028 const GrSwizzle& texSwizzle,
29 const GrSwizzle& outSwizzle,
Robert Phillips84a81202016-11-04 11:59:10 -040030 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050031 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040032 GrProtected isProtected,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040033 GrInternalSurfaceFlags surfaceFlags)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040034 : GrSurfaceProxy(format, desc, GrRenderable::kYes, origin, texSwizzle, fit, budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040035 isProtected, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070036 // for now textures w/ data are always wrapped
Brian Salomon27b4d8d2019-07-22 14:23:45 -040037 , GrRenderTargetProxy(caps, format, desc, sampleCnt, origin, texSwizzle, outSwizzle, fit,
38 budgeted, isProtected, surfaceFlags)
Chris Dalton95d8ceb2019-07-30 11:17:59 -060039 , GrTextureProxy(format, desc, origin, mipMapped, mipMapsStatus, texSwizzle, fit, budgeted,
40 isProtected, surfaceFlags) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070041
42// Lazy-callback version
43GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel457469c2018-02-08 15:05:44 -050044 LazyInstantiationType lazyType,
Greg Daniel4065d452018-11-16 15:43:41 -050045 const GrBackendFormat& format,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050046 const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040047 int sampleCnt,
Brian Salomon2a4f9832018-03-03 22:43:43 -050048 GrSurfaceOrigin origin,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050049 GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060050 GrMipMapsStatus mipMapsStatus,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040051 const GrSwizzle& texSwizzle,
52 const GrSwizzle& outSwizzle,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050053 SkBackingFit fit,
54 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040055 GrProtected isProtected,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040056 GrInternalSurfaceFlags surfaceFlags)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040057 : GrSurfaceProxy(std::move(callback), lazyType, format, desc, GrRenderable::kYes, origin,
Brian Salomone8a766b2019-07-19 14:24:36 -040058 texSwizzle, fit, budgeted, isProtected, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070059 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
60 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Brian Salomon27b4d8d2019-07-22 14:23:45 -040061 , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, format, desc, sampleCnt, origin,
62 texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags,
Brian Salomone8a766b2019-07-19 14:24:36 -040063 WrapsVkSecondaryCB::kNo)
Greg Daniel4065d452018-11-16 15:43:41 -050064 , GrTextureProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060065 mipMapsStatus, texSwizzle, fit, budgeted, isProtected, surfaceFlags) {}
Robert Phillips84a81202016-11-04 11:59:10 -040066
67// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050068// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040069// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040070GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040071 GrSurfaceOrigin origin,
72 const GrSwizzle& texSwizzle,
73 const GrSwizzle& outSwizzle)
74 : GrSurfaceProxy(surf, origin, texSwizzle, SkBackingFit::kExact)
75 , GrRenderTargetProxy(surf, origin, texSwizzle, outSwizzle)
76 , GrTextureProxy(surf, origin, texSwizzle) {
Robert Phillips37430132016-11-09 06:50:43 -050077 SkASSERT(surf->asTexture());
78 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040079}
80
Brian Salomonbb5711a2017-05-17 13:49:59 -040081size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton6ce447a2019-06-23 18:07:38 -060082 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -050083 if (colorSamplesPerPixel > 1) {
84 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -050085 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -050086 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040087
Robert Phillips84a81202016-11-04 11:59:10 -040088 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070089 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -040090 colorSamplesPerPixel, this->proxyMipMapped(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050091 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040092}
93
Robert Phillips10d17212019-04-24 14:09:10 -040094bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050095 if (LazyState::kNot != this->lazyInstantiationState()) {
96 return false;
97 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040098
Robert Phillipsae7d3f32017-09-21 08:26:08 -040099 const GrUniqueKey& key = this->getUniqueKey();
100
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400101 if (!this->instantiateImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
102 GrRenderable::kYes, this->mipMapped(),
103 key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400104 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400105 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400106 if (key.isValid()) {
107 SkASSERT(key == this->getUniqueKey());
108 }
109
Robert Phillipsb5204762019-06-19 14:12:13 -0400110 SkASSERT(this->peekRenderTarget());
111 SkASSERT(this->peekTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400112
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400113 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400114}
Robert Phillips5af44de2017-07-18 14:49:38 -0400115
116sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
117 GrResourceProvider* resourceProvider) const {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400118 sk_sp<GrSurface> surface =
119 this->createSurfaceImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
120 GrRenderable::kYes, this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400121 if (!surface) {
122 return nullptr;
123 }
124 SkASSERT(surface->asRenderTarget());
125 SkASSERT(surface->asTexture());
126
127 return surface;
128}
129
Chris Dalton706a6ff2017-11-29 22:01:06 -0700130#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400131void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500132 // Anything checked here should also be checking the GrTextureProxy version
133 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400134 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500135 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500136
137 // Anything checked here should also be checking the GrRenderTargetProxy version
138 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600139 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400140
Brian Salomonc67c31c2018-12-06 10:00:03 -0500141 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
142
Greg Daniel849dce12018-04-24 14:32:53 -0400143 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
144 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500145
146 // Only non-RT textures can be read only.
147 SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly));
148 SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly));
149
Greg Daniela070ed72018-04-26 16:31:38 -0400150 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
151 (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500152 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
153 (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700154}
155#endif
156