blob: 50077c4c0f56b30e27d34a7b07b3e70845908782 [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"
15#include "GrSurfaceProxyPriv.h"
16
Robert Phillips84a81202016-11-04 11:59:10 -040017// Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -050018// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040019// GrRenderTargetProxy) so its constructor must be explicitly called.
20GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
21 const GrSurfaceDesc& desc,
Greg Danielf6f7b672018-02-15 13:06:26 -050022 GrMipMapped mipMapped,
Robert Phillips84a81202016-11-04 11:59:10 -040023 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050024 SkBudgeted budgeted,
25 uint32_t flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070026 : GrSurfaceProxy(desc, fit, budgeted, flags)
27 // for now textures w/ data are always wrapped
Greg Danielf6f7b672018-02-15 13:06:26 -050028 , GrTextureProxy(desc, mipMapped, fit, budgeted, nullptr, 0, flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070029 , GrRenderTargetProxy(caps, desc, fit, budgeted, flags) {
30}
31
32// Lazy-callback version
33GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel457469c2018-02-08 15:05:44 -050034 LazyInstantiationType lazyType,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050035 const GrSurfaceDesc& desc,
36 GrMipMapped mipMapped,
37 SkBackingFit fit,
38 SkBudgeted budgeted,
39 uint32_t flags)
Greg Daniel457469c2018-02-08 15:05:44 -050040 : GrSurfaceProxy(std::move(callback), lazyType, desc, fit, budgeted, flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070041 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
42 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Greg Daniel457469c2018-02-08 15:05:44 -050043 , GrTextureProxy(LazyInstantiateCallback(), lazyType, desc, mipMapped, fit, budgeted, flags)
44 , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, desc, fit, budgeted, flags) {
Robert Phillips84a81202016-11-04 11:59:10 -040045}
46
47// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050048// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040049// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040050GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
51 GrSurfaceOrigin origin)
Chris Dalton706a6ff2017-11-29 22:01:06 -070052 : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
53 , GrTextureProxy(surf, origin)
54 , GrRenderTargetProxy(surf, origin) {
Robert Phillips37430132016-11-09 06:50:43 -050055 SkASSERT(surf->asTexture());
56 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040057}
58
Brian Salomonbb5711a2017-05-17 13:49:59 -040059size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050060 int colorSamplesPerPixel = this->numColorSamples();
61 if (colorSamplesPerPixel > 1) {
62 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -050063 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -050064 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040065
Robert Phillips84a81202016-11-04 11:59:10 -040066 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070067 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050068 colorSamplesPerPixel, this->texPriv().proxyMipMapped(),
69 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040070}
71
Robert Phillipseee4d6e2017-06-05 09:26:07 -040072bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050073 if (LazyState::kNot != this->lazyInstantiationState()) {
74 return false;
75 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040076 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
77
Robert Phillipsae7d3f32017-09-21 08:26:08 -040078 const GrUniqueKey& key = this->getUniqueKey();
79
Robert Phillips65048132017-08-10 08:44:49 -040080 if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
Greg Danield2d8e922018-02-12 12:07:39 -050081 kFlags, this->mipMapped(), key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040082 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -040083 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040084 if (key.isValid()) {
85 SkASSERT(key == this->getUniqueKey());
86 }
87
Robert Phillipseee4d6e2017-06-05 09:26:07 -040088 SkASSERT(fTarget->asRenderTarget());
89 SkASSERT(fTarget->asTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -040090
Robert Phillipseee4d6e2017-06-05 09:26:07 -040091 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -040092}
Robert Phillips5af44de2017-07-18 14:49:38 -040093
94sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
95 GrResourceProvider* resourceProvider) const {
96 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
97
98 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
Robert Phillips65048132017-08-10 08:44:49 -040099 this->needsStencil(), kFlags,
Greg Danield2d8e922018-02-12 12:07:39 -0500100 this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400101 if (!surface) {
102 return nullptr;
103 }
104 SkASSERT(surface->asRenderTarget());
105 SkASSERT(surface->asTexture());
106
107 return surface;
108}
109
Chris Dalton706a6ff2017-11-29 22:01:06 -0700110#ifdef SK_DEBUG
Robert Phillipse8fabb22018-02-04 14:33:21 -0500111void GrTextureRenderTargetProxy::validateLazySurface(const GrSurface* surface) {
112 // Anything checked here should also be checking the GrTextureProxy version
113 SkASSERT(surface->asTexture());
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500114 SkASSERT(GrMipMapped::kNo == this->texPriv().proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500115 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500116
117 // Anything checked here should also be checking the GrRenderTargetProxy version
118 SkASSERT(surface->asRenderTarget());
119 SkASSERT(surface->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700120}
121#endif
122