blob: 18033246f0bd98627f2e80c83d778c8eadaea5fa [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 Salomon2a4f9832018-03-03 22:43:43 -050024 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -050025 GrMipMapped mipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040026 const GrSwizzle& texSwizzle,
27 const GrSwizzle& outSwizzle,
Robert Phillips84a81202016-11-04 11:59:10 -040028 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050029 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040030 GrInternalSurfaceFlags surfaceFlags)
Greg Daniel2c19e7f2019-06-18 13:29:21 -040031 : GrSurfaceProxy(format, desc, origin, texSwizzle, fit, budgeted, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070032 // for now textures w/ data are always wrapped
Greg Daniel2c19e7f2019-06-18 13:29:21 -040033 , GrRenderTargetProxy(caps, format, desc, origin, texSwizzle, outSwizzle, fit, budgeted,
34 surfaceFlags)
35 , GrTextureProxy(format, desc, origin, mipMapped, texSwizzle, fit, budgeted, surfaceFlags) {
36}
Chris Dalton706a6ff2017-11-29 22:01:06 -070037
38// Lazy-callback version
39GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel457469c2018-02-08 15:05:44 -050040 LazyInstantiationType lazyType,
Greg Daniel4065d452018-11-16 15:43:41 -050041 const GrBackendFormat& format,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050042 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -050043 GrSurfaceOrigin origin,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050044 GrMipMapped mipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040045 const GrSwizzle& texSwizzle,
46 const GrSwizzle& outSwizzle,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050047 SkBackingFit fit,
48 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040049 GrInternalSurfaceFlags surfaceFlags)
Greg Daniel2c19e7f2019-06-18 13:29:21 -040050 : GrSurfaceProxy(std::move(callback), lazyType, format, desc, origin, texSwizzle, fit,
51 budgeted, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070052 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
53 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Greg Daniel2c19e7f2019-06-18 13:29:21 -040054 , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, texSwizzle,
55 outSwizzle, fit, budgeted, surfaceFlags, WrapsVkSecondaryCB::kNo)
Greg Daniel4065d452018-11-16 15:43:41 -050056 , GrTextureProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, mipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040057 texSwizzle, fit, budgeted, surfaceFlags) {}
Robert Phillips84a81202016-11-04 11:59:10 -040058
59// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050060// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040061// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040062GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040063 GrSurfaceOrigin origin,
64 const GrSwizzle& texSwizzle,
65 const GrSwizzle& outSwizzle)
66 : GrSurfaceProxy(surf, origin, texSwizzle, SkBackingFit::kExact)
67 , GrRenderTargetProxy(surf, origin, texSwizzle, outSwizzle)
68 , GrTextureProxy(surf, origin, texSwizzle) {
Robert Phillips37430132016-11-09 06:50:43 -050069 SkASSERT(surf->asTexture());
70 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040071}
72
Brian Salomonbb5711a2017-05-17 13:49:59 -040073size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton6ce447a2019-06-23 18:07:38 -060074 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -050075 if (colorSamplesPerPixel > 1) {
76 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -050077 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -050078 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040079
Robert Phillips84a81202016-11-04 11:59:10 -040080 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070081 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -040082 colorSamplesPerPixel, this->proxyMipMapped(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050083 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040084}
85
Robert Phillips10d17212019-04-24 14:09:10 -040086bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050087 if (LazyState::kNot != this->lazyInstantiationState()) {
88 return false;
89 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -040090 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Brian Salomonbb5711a2017-05-17 13:49:59 -040091
Robert Phillipsae7d3f32017-09-21 08:26:08 -040092 const GrUniqueKey& key = this->getUniqueKey();
93
Chris Dalton6ce447a2019-06-23 18:07:38 -060094 if (!this->instantiateImpl(resourceProvider, this->numSamples(), this->needsStencil(),
Robert Phillips10d17212019-04-24 14:09:10 -040095 kDescFlags, this->mipMapped(), key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040096 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -040097 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040098 if (key.isValid()) {
99 SkASSERT(key == this->getUniqueKey());
100 }
101
Robert Phillipsb5204762019-06-19 14:12:13 -0400102 SkASSERT(this->peekRenderTarget());
103 SkASSERT(this->peekTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400104
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400105 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400106}
Robert Phillips5af44de2017-07-18 14:49:38 -0400107
108sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
109 GrResourceProvider* resourceProvider) const {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400110 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips5af44de2017-07-18 14:49:38 -0400111
Chris Dalton6ce447a2019-06-23 18:07:38 -0600112 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numSamples(),
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400113 this->needsStencil(), kDescFlags,
Robert Phillips10d17212019-04-24 14:09:10 -0400114 this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400115 if (!surface) {
116 return nullptr;
117 }
118 SkASSERT(surface->asRenderTarget());
119 SkASSERT(surface->asTexture());
120
121 return surface;
122}
123
Chris Dalton706a6ff2017-11-29 22:01:06 -0700124#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400125void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500126 // Anything checked here should also be checking the GrTextureProxy version
127 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400128 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500129 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500130
131 // Anything checked here should also be checking the GrRenderTargetProxy version
132 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600133 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400134
Brian Salomonc67c31c2018-12-06 10:00:03 -0500135 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
136
Greg Daniel849dce12018-04-24 14:32:53 -0400137 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
138 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500139
140 // Only non-RT textures can be read only.
141 SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly));
142 SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly));
143
Greg Daniela070ed72018-04-26 16:31:38 -0400144 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
145 (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500146 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
147 (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700148}
149#endif
150