blob: 20c1abf15b4ae0a886dbad5a90a59236a697e547 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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
Greg Danielf91aeb22019-06-18 09:58:02 -04008#include "src/gpu/GrRenderTargetProxy.h"
robertphillips76948d42016-05-04 12:47:41 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkMathPriv.h"
11#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrGpuResourcePriv.h"
13#include "src/gpu/GrRenderTargetOpList.h"
14#include "src/gpu/GrRenderTargetPriv.h"
15#include "src/gpu/GrResourceProvider.h"
16#include "src/gpu/GrSurfacePriv.h"
17#include "src/gpu/GrTextureRenderTargetProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070018
csmartdaltonf9635992016-08-10 11:09:07 -070019// Deferred version
20// TODO: we can probably munge the 'desc' in both the wrapped and deferred
21// cases to make the sampleConfig/numSamples stuff more rational.
Greg Daniel4065d452018-11-16 15:43:41 -050022GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps, const GrBackendFormat& format,
23 const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040024 const GrSwizzle& textureSwizzle,
25 const GrSwizzle& outputSwizzle, SkBackingFit fit,
26 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
27 : INHERITED(format, desc, origin, textureSwizzle, fit, budgeted, surfaceFlags)
Brian Salomonbb5711a2017-05-17 13:49:59 -040028 , fSampleCnt(desc.fSampleCnt)
Chris Daltoneffee202019-07-01 22:28:03 -060029 , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo)
30 , fOutputSwizzle(outputSwizzle) {
csmartdaltonf9635992016-08-10 11:09:07 -070031}
32
Chris Dalton706a6ff2017-11-29 22:01:06 -070033// Lazy-callback version
Greg Daniel65fa8ca2018-01-10 17:06:31 -050034GrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback,
Greg Daniel4065d452018-11-16 15:43:41 -050035 LazyInstantiationType lazyType,
36 const GrBackendFormat& format, const GrSurfaceDesc& desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040037 GrSurfaceOrigin origin, const GrSwizzle& textureSwizzle,
38 const GrSwizzle& outputSwizzle, SkBackingFit fit,
Greg Danielb085fa92019-03-05 16:55:12 -050039 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags,
40 WrapsVkSecondaryCB wrapsVkSecondaryCB)
Greg Daniel2c19e7f2019-06-18 13:29:21 -040041 : INHERITED(std::move(callback), lazyType, format, desc, origin, textureSwizzle, fit,
42 budgeted, surfaceFlags)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050043 , fSampleCnt(desc.fSampleCnt)
Chris Daltoneffee202019-07-01 22:28:03 -060044 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB)
45 , fOutputSwizzle(outputSwizzle) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -050046 SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
Chris Dalton706a6ff2017-11-29 22:01:06 -070047}
48
csmartdaltonf9635992016-08-10 11:09:07 -070049// Wrapped version
Greg Danielb46add82019-01-02 14:51:29 -050050GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040051 const GrSwizzle& textureSwizzle,
52 const GrSwizzle& outputSwizzle,
Greg Danielb46add82019-01-02 14:51:29 -050053 WrapsVkSecondaryCB wrapsVkSecondaryCB)
Greg Daniel2c19e7f2019-06-18 13:29:21 -040054 : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact)
Chris Dalton6ce447a2019-06-23 18:07:38 -060055 , fSampleCnt(fTarget->asRenderTarget()->numSamples())
Chris Daltoneffee202019-07-01 22:28:03 -060056 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB)
57 , fOutputSwizzle(outputSwizzle) {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040058}
robertphillips76948d42016-05-04 12:47:41 -070059
Robert Phillipsec2249f2016-11-09 08:54:35 -050060int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
Brian Salomon34a20302018-12-18 12:03:07 -050061 return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles();
Robert Phillipsec2249f2016-11-09 08:54:35 -050062}
63
Robert Phillips10d17212019-04-24 14:09:10 -040064bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050065 if (LazyState::kNot != this->lazyInstantiationState()) {
66 return false;
67 }
Robert Phillipsfe0253f2018-03-16 16:47:25 -040068 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
robertphillips76948d42016-05-04 12:47:41 -070069
Chris Daltoneffee202019-07-01 22:28:03 -060070 if (!this->instantiateImpl(resourceProvider, fSampleCnt, fNumStencilSamples, kDescFlags,
Robert Phillips10d17212019-04-24 14:09:10 -040071 GrMipMapped::kNo, nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040072 return false;
robertphillips76948d42016-05-04 12:47:41 -070073 }
Robert Phillipsb5204762019-06-19 14:12:13 -040074
75 SkASSERT(this->peekRenderTarget());
76 SkASSERT(!this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040077 return true;
robertphillips76948d42016-05-04 12:47:41 -070078}
79
Robert Phillips5af44de2017-07-18 14:49:38 -040080sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
Robert Phillipsfe0253f2018-03-16 16:47:25 -040081 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips5af44de2017-07-18 14:49:38 -040082
Chris Daltoneffee202019-07-01 22:28:03 -060083 sk_sp<GrSurface> surface = this->createSurfaceImpl(
84 resourceProvider, fSampleCnt, fNumStencilSamples, kDescFlags, GrMipMapped::kNo);
Robert Phillips5af44de2017-07-18 14:49:38 -040085 if (!surface) {
86 return nullptr;
87 }
88 SkASSERT(surface->asRenderTarget());
Greg Daniele252f082017-10-23 16:05:23 -040089 SkASSERT(!surface->asTexture());
Robert Phillips5af44de2017-07-18 14:49:38 -040090 return surface;
91}
92
Brian Salomonbb5711a2017-05-17 13:49:59 -040093size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton6ce447a2019-06-23 18:07:38 -060094 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -050095 if (colorSamplesPerPixel > 1) {
96 // Add one for the resolve buffer.
97 ++colorSamplesPerPixel;
98 }
Greg Daniele252f082017-10-23 16:05:23 -040099
Robert Phillips8bc06d02016-11-01 17:28:40 -0400100 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -0700101 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
102 colorSamplesPerPixel, GrMipMapped::kNo, !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400103}
104
Robert Phillipse2f7d182016-12-15 09:23:05 -0500105bool GrRenderTargetProxy::refsWrappedObjects() const {
Robert Phillipsb5204762019-06-19 14:12:13 -0400106 if (!this->isInstantiated()) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500107 return false;
108 }
109
Robert Phillipsb5204762019-06-19 14:12:13 -0400110 GrSurface* surface = this->peekSurface();
111 return surface->resourcePriv().refsWrappedObjects();
Robert Phillipse2f7d182016-12-15 09:23:05 -0500112}
Robert Phillipse8fabb22018-02-04 14:33:21 -0500113
114#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400115void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500116 // We do not check that surface->asTexture returns null since, when replaying DDLs we
117 // can fulfill a renderTarget-only proxy w/ a textureRenderTarget.
Robert Phillipse8fabb22018-02-04 14:33:21 -0500118
119 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
120 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600121 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400122
Greg Daniela070ed72018-04-26 16:31:38 -0400123 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
124 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
125 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
126 (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500127}
128#endif