blob: 756e1d665a57ac2e03bd08b76858c42050c74ef0 [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
Chris Dalton215ff332019-07-02 09:38:22 -060080bool GrRenderTargetProxy::canChangeStencilAttachment() const {
81 if (!fTarget) {
82 // If we aren't instantiated, then we definitely are an internal render target. Ganesh is
83 // free to change stencil attachments on internal render targets.
84 return true;
85 }
86 return fTarget->asRenderTarget()->canAttemptStencilAttachment();
87}
88
Robert Phillips5af44de2017-07-18 14:49:38 -040089sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
Robert Phillipsfe0253f2018-03-16 16:47:25 -040090 static constexpr GrSurfaceDescFlags kDescFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips5af44de2017-07-18 14:49:38 -040091
Chris Daltoneffee202019-07-01 22:28:03 -060092 sk_sp<GrSurface> surface = this->createSurfaceImpl(
93 resourceProvider, fSampleCnt, fNumStencilSamples, kDescFlags, GrMipMapped::kNo);
Robert Phillips5af44de2017-07-18 14:49:38 -040094 if (!surface) {
95 return nullptr;
96 }
97 SkASSERT(surface->asRenderTarget());
Greg Daniele252f082017-10-23 16:05:23 -040098 SkASSERT(!surface->asTexture());
Robert Phillips5af44de2017-07-18 14:49:38 -040099 return surface;
100}
101
Brian Salomonbb5711a2017-05-17 13:49:59 -0400102size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600103 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500104 if (colorSamplesPerPixel > 1) {
105 // Add one for the resolve buffer.
106 ++colorSamplesPerPixel;
107 }
Greg Daniele252f082017-10-23 16:05:23 -0400108
Robert Phillips8bc06d02016-11-01 17:28:40 -0400109 // TODO: do we have enough information to improve this worst case estimate?
Chris Dalton706a6ff2017-11-29 22:01:06 -0700110 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
111 colorSamplesPerPixel, GrMipMapped::kNo, !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400112}
113
Robert Phillipse2f7d182016-12-15 09:23:05 -0500114bool GrRenderTargetProxy::refsWrappedObjects() const {
Robert Phillipsb5204762019-06-19 14:12:13 -0400115 if (!this->isInstantiated()) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500116 return false;
117 }
118
Robert Phillipsb5204762019-06-19 14:12:13 -0400119 GrSurface* surface = this->peekSurface();
120 return surface->resourcePriv().refsWrappedObjects();
Robert Phillipse2f7d182016-12-15 09:23:05 -0500121}
Robert Phillipse8fabb22018-02-04 14:33:21 -0500122
123#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400124void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500125 // We do not check that surface->asTexture returns null since, when replaying DDLs we
126 // can fulfill a renderTarget-only proxy w/ a textureRenderTarget.
Robert Phillipse8fabb22018-02-04 14:33:21 -0500127
128 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
129 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600130 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400131
Greg Daniela070ed72018-04-26 16:31:38 -0400132 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
133 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
134 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
135 (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500136}
137#endif