blob: fba48e7a14dc6e30c758e9107569bc6891c42232 [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,
Brian Salomon7226c232018-07-30 13:13:17 -040025 GrTextureType textureType,
Robert Phillips84a81202016-11-04 11:59:10 -040026 SkBackingFit fit,
Robert Phillipsc787e492017-02-28 11:26:32 -050027 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040028 GrInternalSurfaceFlags surfaceFlags)
29 : GrSurfaceProxy(desc, origin, fit, budgeted, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070030 // for now textures w/ data are always wrapped
Brian Osman086679b2018-09-10 16:26:51 -040031 , GrRenderTargetProxy(caps, desc, origin, fit, budgeted, surfaceFlags)
32 , GrTextureProxy(desc, origin, mipMapped, textureType, fit, budgeted, surfaceFlags) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070033
34// Lazy-callback version
35GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel457469c2018-02-08 15:05:44 -050036 LazyInstantiationType lazyType,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050037 const GrSurfaceDesc& desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -050038 GrSurfaceOrigin origin,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050039 GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -040040 GrTextureType textureType,
Greg Daniel65fa8ca2018-01-10 17:06:31 -050041 SkBackingFit fit,
42 SkBudgeted budgeted,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040043 GrInternalSurfaceFlags surfaceFlags)
44 : GrSurfaceProxy(std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags)
Chris Dalton706a6ff2017-11-29 22:01:06 -070045 // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
46 // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
Brian Salomon2a4f9832018-03-03 22:43:43 -050047 , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, desc, origin, fit, budgeted,
Brian Osman086679b2018-09-10 16:26:51 -040048 surfaceFlags)
49 , GrTextureProxy(LazyInstantiateCallback(), lazyType, desc, origin, mipMapped, textureType,
50 fit, budgeted, surfaceFlags) {}
Robert Phillips84a81202016-11-04 11:59:10 -040051
52// Wrapped version
Robert Phillipsc787e492017-02-28 11:26:32 -050053// This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
Robert Phillips84a81202016-11-04 11:59:10 -040054// GrRenderTargetProxy) so its constructor must be explicitly called.
Robert Phillips066f0202017-07-25 10:16:35 -040055GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
56 GrSurfaceOrigin origin)
Chris Dalton706a6ff2017-11-29 22:01:06 -070057 : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
Brian Osman086679b2018-09-10 16:26:51 -040058 , GrRenderTargetProxy(surf, origin)
59 , GrTextureProxy(surf, origin) {
Robert Phillips37430132016-11-09 06:50:43 -050060 SkASSERT(surf->asTexture());
61 SkASSERT(surf->asRenderTarget());
Robert Phillips84a81202016-11-04 11:59:10 -040062}
63
Brian Salomonbb5711a2017-05-17 13:49:59 -040064size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050065 int colorSamplesPerPixel = this->numColorSamples();
66 if (colorSamplesPerPixel > 1) {
67 // Add one to account for the resolve buffer.
Greg Danielf6f7b672018-02-15 13:06:26 -050068 ++colorSamplesPerPixel;
Brian Salomonbdecacf2018-02-02 20:32:49 -050069 }
Brian Salomonbb5711a2017-05-17 13:49:59 -040070
Robert Phillips84a81202016-11-04 11:59:10 -040071 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -070072 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -040073 colorSamplesPerPixel, this->proxyMipMapped(),
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050074 !this->priv().isExact());
Robert Phillips84a81202016-11-04 11:59:10 -040075}
76
Robert Phillipseee4d6e2017-06-05 09:26:07 -040077bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050078 if (LazyState::kNot != this->lazyInstantiationState()) {
79 return false;
80 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -040081 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Brian Salomonbb5711a2017-05-17 13:49:59 -040082
Robert Phillipsae7d3f32017-09-21 08:26:08 -040083 const GrUniqueKey& key = this->getUniqueKey();
84
Robert Phillips65048132017-08-10 08:44:49 -040085 if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), this->needsStencil(),
Robert Phillipsfe0253f2018-03-16 16:47:25 -040086 kDescFlags, this->mipMapped(), key.isValid() ? &key : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040087 return false;
Brian Salomonbb5711a2017-05-17 13:49:59 -040088 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040089 if (key.isValid()) {
90 SkASSERT(key == this->getUniqueKey());
91 }
92
Robert Phillipseee4d6e2017-06-05 09:26:07 -040093 SkASSERT(fTarget->asRenderTarget());
94 SkASSERT(fTarget->asTexture());
Brian Salomonbb5711a2017-05-17 13:49:59 -040095
Robert Phillipseee4d6e2017-06-05 09:26:07 -040096 return true;
Brian Salomonbb5711a2017-05-17 13:49:59 -040097}
Robert Phillips5af44de2017-07-18 14:49:38 -040098
99sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
100 GrResourceProvider* resourceProvider) const {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400101 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips5af44de2017-07-18 14:49:38 -0400102
103 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numStencilSamples(),
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400104 this->needsStencil(), kDescFlags,
Greg Danield2d8e922018-02-12 12:07:39 -0500105 this->mipMapped());
Robert Phillips5af44de2017-07-18 14:49:38 -0400106 if (!surface) {
107 return nullptr;
108 }
109 SkASSERT(surface->asRenderTarget());
110 SkASSERT(surface->asTexture());
111
112 return surface;
113}
114
Chris Dalton706a6ff2017-11-29 22:01:06 -0700115#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400116void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500117 // Anything checked here should also be checking the GrTextureProxy version
118 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400119 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips4150eea2018-02-07 17:08:21 -0500120 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Robert Phillipse8fabb22018-02-04 14:33:21 -0500121
122 // Anything checked here should also be checking the GrRenderTargetProxy version
123 SkASSERT(surface->asRenderTarget());
124 SkASSERT(surface->asRenderTarget()->numStencilSamples() == this->numStencilSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400125
126 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
127 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Greg Daniela070ed72018-04-26 16:31:38 -0400128 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
129 (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400130 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700131}
132#endif
133