blob: 95d5543bbfea41e92b848831c4769029b1f93ed4 [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
8#include "GrTextureRenderTargetProxy.h"
9
Greg Daniel65fa8ca2018-01-10 17:06:31 -050010#include "GrCaps.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070011#include "GrTexture.h"
Robert Phillipse8fabb22018-02-04 14:33:21 -050012#include "GrTexturePriv.h"
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050013#include "GrTextureProxyPriv.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070014#include "GrRenderTarget.h"
Greg Daniel849dce12018-04-24 14:32:53 -040015#include "GrSurfacePriv.h"
Chris Dalton706a6ff2017-11-29 22:01:06 -070016#include "GrSurfaceProxyPriv.h"
17
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,
22 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -050023 GrSurfaceOrigin origin,
Greg Danielf6f7b672018-02-15 13:06:26 -050024 GrMipMapped mipMapped,
Robert Phillips84a81202016-11-04 11:59:10 -040025 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050026 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040027 GrInternalSurfaceFlags surfaceFlags)
28 : GrSurfaceProxy(desc, origin, fit, budgeted, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070029 // for now textures w/ data are always wrapped
Robert Phillipsfe0253f2018-03-16 16:47:25 -040030 , GrTextureProxy(desc, origin, mipMapped, fit, budgeted, surfaceFlags)
31 , GrRenderTargetProxy(caps, desc, origin, fit, budgeted, surfaceFlags) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070032
33// Lazy-callback version
34GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel457469c2018-02-08 15:05:44 -050035 LazyInstantiationType lazyType,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050036 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -050037 GrSurfaceOrigin origin,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050038 GrMipMapped mipMapped,
39 SkBackingFit fit,
40 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040041 GrInternalSurfaceFlags surfaceFlags)
42 : GrSurfaceProxy(std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070043 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
44 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Brian Salomon2a4f9832018-03-03 22:43:43 -050045 , GrTextureProxy(LazyInstantiateCallback(), lazyType, desc, origin, mipMapped, fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040046 budgeted, surfaceFlags)
Brian Salomon2a4f9832018-03-03 22:43:43 -050047 , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, desc, origin, fit, budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040048 surfaceFlags) {}
Robert Phillips84a81202016-11-04 11:59:10 -040049
50// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050051// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040052// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040053GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
54 GrSurfaceOrigin origin)
Chris Dalton706a6ff2017-11-29 22:01:06 -070055 : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
56 , GrTextureProxy(surf, origin)
57 , GrRenderTargetProxy(surf, origin) {
Robert Phillips37430132016-11-09 06:50:43 -050058 SkASSERT(surf->asTexture());
59 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040060}
61
Brian Salomonbb5711a2017-05-17 13:49:59 -040062size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050063 int colorSamplesPerPixel = this->numColorSamples();
64 if (colorSamplesPerPixel > 1) {
65 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -050066 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -050067 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040068
Robert Phillips84a81202016-11-04 11:59:10 -040069 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070070 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050071 colorSamplesPerPixel, this->texPriv().proxyMipMapped(),
72 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040073}
74
Robert Phillipseee4d6e2017-06-05 09:26:07 -040075bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050076 if (LazyState::kNot != this->lazyInstantiationState()) {
77 return false;
78 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -040079 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Brian Salomonbb5711a2017-05-17 13:49:59 -040080
Robert Phillipsae7d3f32017-09-21 08:26:08 -040081 const GrUniqueKey& key = this->getUniqueKey();
82
Robert Phillips65048132017-08-10 08:44:49 -040083 if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
Robert Phillipsfe0253f2018-03-16 16:47:25 -040084 kDescFlags, this->mipMapped(), key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040085 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -040086 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040087 if (key.isValid()) {
88 SkASSERT(key == this->getUniqueKey());
89 }
90
Robert Phillipseee4d6e2017-06-05 09:26:07 -040091 SkASSERT(fTarget->asRenderTarget());
92 SkASSERT(fTarget->asTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -040093
Robert Phillipseee4d6e2017-06-05 09:26:07 -040094 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -040095}
Robert Phillips5af44de2017-07-18 14:49:38 -040096
97sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
98 GrResourceProvider* resourceProvider) const {
Robert Phillipsfe0253f2018-03-16 16:47:25 -040099 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips5af44de2017-07-18 14:49:38 -0400100
101 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400102 this->needsStencil(), kDescFlags,
Greg Danield2d8e922018-02-12 12:07:39 -0500103 this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400104 if (!surface) {
105 return nullptr;
106 }
107 SkASSERT(surface->asRenderTarget());
108 SkASSERT(surface->asTexture());
109
110 return surface;
111}
112
Chris Dalton706a6ff2017-11-29 22:01:06 -0700113#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400114void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500115 // Anything checked here should also be checking the GrTextureProxy version
116 SkASSERT(surface->asTexture());
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500117 SkASSERT(GrMipMapped::kNo == this->texPriv().proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500118 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500119
120 // Anything checked here should also be checking the GrRenderTargetProxy version
121 SkASSERT(surface->asRenderTarget());
122 SkASSERT(surface->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400123
124 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
125 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
126 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
127 (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
Greg Daniela070ed72018-04-26 16:31:38 -0400128 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
129 (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700130}
131#endif
132