blob: 213359b7c05917565f8ec3ced09d873d18b39604 [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,
Greg Daniel2a303902018-02-20 10:25:54 -050039 uint32_t flags,
40 GrRenderTargetFlags renderTargetFlags)
Greg Daniel457469c2018-02-08 15:05:44 -050041 : GrSurfaceProxy(std::move(callback), lazyType, desc, fit, budgeted, flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070042 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
43 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Greg Daniel457469c2018-02-08 15:05:44 -050044 , GrTextureProxy(LazyInstantiateCallback(), lazyType, desc, mipMapped, fit, budgeted, flags)
Greg Daniel2a303902018-02-20 10:25:54 -050045 , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, desc, fit, budgeted, flags,
46 renderTargetFlags) {
Robert Phillips84a81202016-11-04 11:59:10 -040047}
48
49// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050050// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040051// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040052GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
53 GrSurfaceOrigin origin)
Chris Dalton706a6ff2017-11-29 22:01:06 -070054 : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
55 , GrTextureProxy(surf, origin)
56 , GrRenderTargetProxy(surf, origin) {
Robert Phillips37430132016-11-09 06:50:43 -050057 SkASSERT(surf->asTexture());
58 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040059}
60
Brian Salomonbb5711a2017-05-17 13:49:59 -040061size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050062 int colorSamplesPerPixel = this->numColorSamples();
63 if (colorSamplesPerPixel > 1) {
64 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -050065 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -050066 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040067
Robert Phillips84a81202016-11-04 11:59:10 -040068 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070069 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050070 colorSamplesPerPixel, this->texPriv().proxyMipMapped(),
71 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040072}
73
Robert Phillipseee4d6e2017-06-05 09:26:07 -040074bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050075 if (LazyState::kNot != this->lazyInstantiationState()) {
76 return false;
77 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040078 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
79
Robert Phillipsae7d3f32017-09-21 08:26:08 -040080 const GrUniqueKey& key = this->getUniqueKey();
81
Robert Phillips65048132017-08-10 08:44:49 -040082 if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
Greg Danield2d8e922018-02-12 12:07:39 -050083 kFlags, this->mipMapped(), key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040084 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -040085 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040086 if (key.isValid()) {
87 SkASSERT(key == this->getUniqueKey());
88 }
89
Robert Phillipseee4d6e2017-06-05 09:26:07 -040090 SkASSERT(fTarget->asRenderTarget());
91 SkASSERT(fTarget->asTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -040092
Robert Phillipseee4d6e2017-06-05 09:26:07 -040093 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -040094}
Robert Phillips5af44de2017-07-18 14:49:38 -040095
96sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
97 GrResourceProvider* resourceProvider) const {
98 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
99
100 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
Robert Phillips65048132017-08-10 08:44:49 -0400101 this->needsStencil(), kFlags,
Greg Danield2d8e922018-02-12 12:07:39 -0500102 this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400103 if (!surface) {
104 return nullptr;
105 }
106 SkASSERT(surface->asRenderTarget());
107 SkASSERT(surface->asTexture());
108
109 return surface;
110}
111
Chris Dalton706a6ff2017-11-29 22:01:06 -0700112#ifdef SK_DEBUG
Robert Phillipse8fabb22018-02-04 14:33:21 -0500113void GrTextureRenderTargetProxy::validateLazySurface(const GrSurface* surface) {
114 // Anything checked here should also be checking the GrTextureProxy version
115 SkASSERT(surface->asTexture());
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500116 SkASSERT(GrMipMapped::kNo == this->texPriv().proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500117 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500118
119 // Anything checked here should also be checking the GrRenderTargetProxy version
120 SkASSERT(surface->asRenderTarget());
121 SkASSERT(surface->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700122}
123#endif
124