blob: 3312077236c80ceb004de85aba40de16eb2fb32c [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"
Chris Dalton706a6ff2017-11-29 22:01:06 -070013#include "GrRenderTarget.h"
14#include "GrSurfaceProxyPriv.h"
15
Robert Phillips84a81202016-11-04 11:59:10 -040016// Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -050017// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040018// GrRenderTargetProxy) so its constructor must be explicitly called.
19GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
20 const GrSurfaceDesc& desc,
21 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050022 SkBudgeted budgeted,
23 uint32_t flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070024 : GrSurfaceProxy(desc, fit, budgeted, flags)
25 // for now textures w/ data are always wrapped
26 , GrTextureProxy(desc, fit, budgeted, nullptr, 0, flags)
27 , GrRenderTargetProxy(caps, desc, fit, budgeted, flags) {
28}
29
30// Lazy-callback version
31GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050032 const GrSurfaceDesc& desc,
33 GrMipMapped mipMapped,
34 SkBackingFit fit,
35 SkBudgeted budgeted,
36 uint32_t flags)
37 : GrSurfaceProxy(std::move(callback), desc, fit, budgeted, flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070038 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
39 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Greg Daniel65fa8ca2018-01-10 17:06:31 -050040 , GrTextureProxy(LazyInstantiateCallback(), desc, mipMapped, fit, budgeted, flags)
41 , GrRenderTargetProxy(LazyInstantiateCallback(), desc, fit, budgeted, flags) {
Robert Phillips84a81202016-11-04 11:59:10 -040042}
43
44// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050045// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040046// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040047GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
48 GrSurfaceOrigin origin)
Chris Dalton706a6ff2017-11-29 22:01:06 -070049 : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
50 , GrTextureProxy(surf, origin)
51 , GrRenderTargetProxy(surf, origin) {
Robert Phillips37430132016-11-09 06:50:43 -050052 SkASSERT(surf->asTexture());
53 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040054}
55
Brian Salomonbb5711a2017-05-17 13:49:59 -040056size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050057 int colorSamplesPerPixel = this->numColorSamples();
58 if (colorSamplesPerPixel > 1) {
59 // Add one to account for the resolve buffer.
60 ++colorSamplesPerPixel += 1;
61 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040062
Robert Phillips84a81202016-11-04 11:59:10 -040063 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070064 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
65 colorSamplesPerPixel, this->mipMapped(), !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040066}
67
Robert Phillipseee4d6e2017-06-05 09:26:07 -040068bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050069 if (LazyState::kNot != this->lazyInstantiationState()) {
70 return false;
71 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040072 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
73
Robert Phillipsae7d3f32017-09-21 08:26:08 -040074 const GrUniqueKey& key = this->getUniqueKey();
75
Robert Phillips65048132017-08-10 08:44:49 -040076 if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
Greg Daniele252f082017-10-23 16:05:23 -040077 kFlags, this->mipMapped(), this->mipColorMode(),
Robert Phillipsae7d3f32017-09-21 08:26:08 -040078 key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040079 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -040080 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040081 if (key.isValid()) {
82 SkASSERT(key == this->getUniqueKey());
83 }
84
Robert Phillipseee4d6e2017-06-05 09:26:07 -040085 SkASSERT(fTarget->asRenderTarget());
86 SkASSERT(fTarget->asTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -040087
Robert Phillipseee4d6e2017-06-05 09:26:07 -040088 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -040089}
Robert Phillips5af44de2017-07-18 14:49:38 -040090
91sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
92 GrResourceProvider* resourceProvider) const {
93 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
94
95 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
Robert Phillips65048132017-08-10 08:44:49 -040096 this->needsStencil(), kFlags,
Greg Daniele252f082017-10-23 16:05:23 -040097 this->mipMapped(), this->mipColorMode());
Robert Phillips5af44de2017-07-18 14:49:38 -040098 if (!surface) {
99 return nullptr;
100 }
101 SkASSERT(surface->asRenderTarget());
102 SkASSERT(surface->asTexture());
103
104 return surface;
105}
106
Chris Dalton706a6ff2017-11-29 22:01:06 -0700107#ifdef SK_DEBUG
Robert Phillipse8fabb22018-02-04 14:33:21 -0500108void GrTextureRenderTargetProxy::validateLazySurface(const GrSurface* surface) {
109 // Anything checked here should also be checking the GrTextureProxy version
110 SkASSERT(surface->asTexture());
Robert Phillips4150eea2018-02-07 17:08:21 -0500111 SkASSERT(GrMipMapped::kNo == this->mipMapped() ||
112 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500113
114 // Anything checked here should also be checking the GrRenderTargetProxy version
115 SkASSERT(surface->asRenderTarget());
116 SkASSERT(surface->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700117}
118#endif
119