blob: fe12a0a97592c426b9851d51764236641adbe74d [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"
12#include "GrRenderTarget.h"
13#include "GrSurfaceProxyPriv.h"
14
Robert Phillips84a81202016-11-04 11:59:10 -040015// Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -050016// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040017// GrRenderTargetProxy) so its constructor must be explicitly called.
18GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
19 const GrSurfaceDesc& desc,
20 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050021 SkBudgeted budgeted,
22 uint32_t flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070023 : GrSurfaceProxy(desc, fit, budgeted, flags)
24 // for now textures w/ data are always wrapped
25 , GrTextureProxy(desc, fit, budgeted, nullptr, 0, flags)
26 , GrRenderTargetProxy(caps, desc, fit, budgeted, flags) {
27}
28
29// Lazy-callback version
30GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050031 const GrSurfaceDesc& desc,
32 GrMipMapped mipMapped,
33 SkBackingFit fit,
34 SkBudgeted budgeted,
35 uint32_t flags)
36 : GrSurfaceProxy(std::move(callback), desc, fit, budgeted, flags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070037 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
38 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Greg Daniel65fa8ca2018-01-10 17:06:31 -050039 , GrTextureProxy(LazyInstantiateCallback(), desc, mipMapped, fit, budgeted, flags)
40 , GrRenderTargetProxy(LazyInstantiateCallback(), desc, fit, budgeted, flags) {
Robert Phillips84a81202016-11-04 11:59:10 -040041}
42
43// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050044// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040045// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040046GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
47 GrSurfaceOrigin origin)
Chris Dalton706a6ff2017-11-29 22:01:06 -070048 : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
49 , GrTextureProxy(surf, origin)
50 , GrRenderTargetProxy(surf, origin) {
Robert Phillips37430132016-11-09 06:50:43 -050051 SkASSERT(surf->asTexture());
52 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040053}
54
Brian Salomonbb5711a2017-05-17 13:49:59 -040055size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
56 int colorSamplesPerPixel = this->numColorSamples() + 1;
57
Robert Phillips84a81202016-11-04 11:59:10 -040058 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070059 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
60 colorSamplesPerPixel, this->mipMapped(), !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040061}
62
Robert Phillipseee4d6e2017-06-05 09:26:07 -040063bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040064 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
65
Robert Phillipsae7d3f32017-09-21 08:26:08 -040066 const GrUniqueKey& key = this->getUniqueKey();
67
Robert Phillips65048132017-08-10 08:44:49 -040068 if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
Greg Daniele252f082017-10-23 16:05:23 -040069 kFlags, this->mipMapped(), this->mipColorMode(),
Robert Phillipsae7d3f32017-09-21 08:26:08 -040070 key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040071 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -040072 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040073 if (key.isValid()) {
74 SkASSERT(key == this->getUniqueKey());
75 }
76
Robert Phillipseee4d6e2017-06-05 09:26:07 -040077 SkASSERT(fTarget->asRenderTarget());
78 SkASSERT(fTarget->asTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -040079
Robert Phillipseee4d6e2017-06-05 09:26:07 -040080 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -040081}
Robert Phillips5af44de2017-07-18 14:49:38 -040082
83sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
84 GrResourceProvider* resourceProvider) const {
85 static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
86
87 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
Robert Phillips65048132017-08-10 08:44:49 -040088 this->needsStencil(), kFlags,
Greg Daniele252f082017-10-23 16:05:23 -040089 this->mipMapped(), this->mipColorMode());
Robert Phillips5af44de2017-07-18 14:49:38 -040090 if (!surface) {
91 return nullptr;
92 }
93 SkASSERT(surface->asRenderTarget());
94 SkASSERT(surface->asTexture());
95
96 return surface;
97}
98
Chris Dalton706a6ff2017-11-29 22:01:06 -070099#ifdef SK_DEBUG
100void GrTextureRenderTargetProxy::validateLazyTexture(const GrTexture* texture) {
101 SkASSERT(texture->asRenderTarget());
102 SkASSERT(texture->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
103 SkASSERT(GrMipMapped::kNo == this->mipMapped());
104}
105#endif
106