blob: ecddcd6d228883470eaed3478af2e5dec7527879 [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"
Greg Danielf41b2bd2019-08-22 16:19:24 -040013#include "src/gpu/GrOpsTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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
Robert Phillips4e105e22020-07-16 09:18:50 -040019#ifdef SK_DEBUG
20#include "include/gpu/GrDirectContext.h"
21#include "src/gpu/GrContextPriv.h"
22#endif
23
csmartdaltonf9635992016-08-10 11:09:07 -070024// Deferred version
25// TODO: we can probably munge the 'desc' in both the wrapped and deferred
26// cases to make the sampleConfig/numSamples stuff more rational.
Brian Salomonbeb7f522019-08-30 16:19:42 -040027GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps,
28 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050029 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040030 int sampleCount,
Brian Salomonbeb7f522019-08-30 16:19:42 -040031 SkBackingFit fit,
32 SkBudgeted budgeted,
33 GrProtected isProtected,
34 GrInternalSurfaceFlags surfaceFlags,
35 UseAllocator useAllocator)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040036 : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040037 , fSampleCnt(sampleCount)
Greg Danielbaf8d992019-10-29 14:14:32 -040038 , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo) {}
csmartdaltonf9635992016-08-10 11:09:07 -070039
Chris Dalton706a6ff2017-11-29 22:01:06 -070040// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040041GrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback,
42 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050043 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040044 int sampleCount,
Brian Salomonbeb7f522019-08-30 16:19:42 -040045 SkBackingFit fit,
46 SkBudgeted budgeted,
47 GrProtected isProtected,
48 GrInternalSurfaceFlags surfaceFlags,
49 UseAllocator useAllocator,
50 WrapsVkSecondaryCB wrapsVkSecondaryCB)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040051 : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected,
52 surfaceFlags, useAllocator)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040053 , fSampleCnt(sampleCount)
Greg Danielbaf8d992019-10-29 14:14:32 -040054 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070055
csmartdaltonf9635992016-08-10 11:09:07 -070056// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -040057GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf,
Brian Salomonbeb7f522019-08-30 16:19:42 -040058 UseAllocator useAllocator,
Greg Danielb46add82019-01-02 14:51:29 -050059 WrapsVkSecondaryCB wrapsVkSecondaryCB)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040060 : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
Chris Dalton6ce447a2019-06-23 18:07:38 -060061 , fSampleCnt(fTarget->asRenderTarget()->numSamples())
Greg Danielbaf8d992019-10-29 14:14:32 -040062 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {
Chris Dalton3f7932e2019-08-19 00:39:13 -060063 // The kRequiresManualMSAAResolve flag better not be set if we are not multisampled or if
64 // MSAA resolve should happen automatically.
65 //
66 // From the other side, we don't know enough about the wrapped surface to assert when
67 // kRequiresManualMSAAResolve *should* be set. e.g., The caller might be wrapping a backend
68 // texture as a render target at this point but we wouldn't know it.
69 SkASSERT(!(this->numSamples() <= 1 ||
70 fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) ||
71 !this->requiresManualMSAAResolve());
Robert Phillipsc4f0a822017-06-13 08:11:36 -040072}
robertphillips76948d42016-05-04 12:47:41 -070073
Robert Phillipsec2249f2016-11-09 08:54:35 -050074int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
Jim Van Verth5fab9092019-11-25 21:23:53 +000075 return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles();
Robert Phillipsec2249f2016-11-09 08:54:35 -050076}
77
Robert Phillips10d17212019-04-24 14:09:10 -040078bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040079 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -050080 return false;
81 }
Chris Dalton0b68dda2019-11-07 21:08:03 -070082 if (!this->instantiateImpl(resourceProvider, fSampleCnt, GrRenderable::kYes, GrMipMapped::kNo,
83 nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040084 return false;
robertphillips76948d42016-05-04 12:47:41 -070085 }
Robert Phillipsb5204762019-06-19 14:12:13 -040086
87 SkASSERT(this->peekRenderTarget());
88 SkASSERT(!this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040089 return true;
robertphillips76948d42016-05-04 12:47:41 -070090}
91
Chris Dalton215ff332019-07-02 09:38:22 -060092bool GrRenderTargetProxy::canChangeStencilAttachment() const {
93 if (!fTarget) {
94 // If we aren't instantiated, then we definitely are an internal render target. Ganesh is
95 // free to change stencil attachments on internal render targets.
96 return true;
97 }
98 return fTarget->asRenderTarget()->canAttemptStencilAttachment();
99}
100
Robert Phillips5af44de2017-07-18 14:49:38 -0400101sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -0700102 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, fSampleCnt,
103 GrRenderable::kYes, GrMipMapped::kNo);
Robert Phillips5af44de2017-07-18 14:49:38 -0400104 if (!surface) {
105 return nullptr;
106 }
107 SkASSERT(surface->asRenderTarget());
Greg Daniele252f082017-10-23 16:05:23 -0400108 SkASSERT(!surface->asTexture());
Robert Phillips5af44de2017-07-18 14:49:38 -0400109 return surface;
110}
111
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400112size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600113 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500114 if (colorSamplesPerPixel > 1) {
115 // Add one for the resolve buffer.
116 ++colorSamplesPerPixel;
117 }
Greg Daniele252f082017-10-23 16:05:23 -0400118
Robert Phillips8bc06d02016-11-01 17:28:40 -0400119 // TODO: do we have enough information to improve this worst case estimate?
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400120 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Greg Daniela00bcad2019-10-11 13:21:48 -0400121 colorSamplesPerPixel, GrMipMapped::kNo, !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400122}
123
Robert Phillipse2f7d182016-12-15 09:23:05 -0500124bool GrRenderTargetProxy::refsWrappedObjects() const {
Robert Phillipsb5204762019-06-19 14:12:13 -0400125 if (!this->isInstantiated()) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500126 return false;
127 }
128
Robert Phillipsb5204762019-06-19 14:12:13 -0400129 GrSurface* surface = this->peekSurface();
130 return surface->resourcePriv().refsWrappedObjects();
Robert Phillipse2f7d182016-12-15 09:23:05 -0500131}
Robert Phillipse8fabb22018-02-04 14:33:21 -0500132
Brian Salomon63410e92020-03-23 18:32:50 -0400133GrSurfaceProxy::LazySurfaceDesc GrRenderTargetProxy::callbackDesc() const {
134 // We only expect exactly sized lazy RT proxies.
135 SkASSERT(!this->isFullyLazy());
136 SkASSERT(this->isFunctionallyExact());
137 return {
138 this->dimensions(),
139 SkBackingFit::kExact,
140 GrRenderable::kYes,
141 GrMipMapped::kNo,
142 this->numSamples(),
143 this->backendFormat(),
144 this->isProtected(),
145 this->isBudgeted(),
146 };
147}
148
Robert Phillipse8fabb22018-02-04 14:33:21 -0500149#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400150void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500151 // We do not check that surface->asTexture returns null since, when replaying DDLs we
152 // can fulfill a renderTarget-only proxy w/ a textureRenderTarget.
Robert Phillipse8fabb22018-02-04 14:33:21 -0500153
154 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
155 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600156 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400157
Greg Daniela070ed72018-04-26 16:31:38 -0400158 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
159 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Robert Phillips350db7c2020-03-06 13:39:41 -0500160 if (proxyFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0 && this->numSamples() == 1) {
161 // Ganesh never internally creates FBO0 proxies or surfaces so this must be a wrapped
162 // proxy. In this case, with no MSAA, rendering to FBO0 is strictly more limited than
163 // rendering to an arbitrary surface so we allow a non-FBO0 surface to be matched with
164 // the proxy.
165 surfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
166 }
Chris Dalton3f7932e2019-08-19 00:39:13 -0600167 SkASSERT(((int)proxyFlags & kGrInternalRenderTargetFlagsMask) ==
168 ((int)surfaceFlags & kGrInternalRenderTargetFlagsMask));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500169}
170#endif