blob: ea635e9e15a740ca8da74694e8655f05c3d2a0ce [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
Chris Dalton3f7932e2019-08-19 00:39:13 -060010#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkMathPriv.h"
12#include "src/gpu/GrCaps.h"
Chris Dalton3f7932e2019-08-19 00:39:13 -060013#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrGpuResourcePriv.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040015#include "src/gpu/GrOpsTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrRenderTargetPriv.h"
17#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrSurfacePriv.h"
19#include "src/gpu/GrTextureRenderTargetProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070020
csmartdaltonf9635992016-08-10 11:09:07 -070021// Deferred version
22// TODO: we can probably munge the 'desc' in both the wrapped and deferred
23// cases to make the sampleConfig/numSamples stuff more rational.
Brian Salomonbeb7f522019-08-30 16:19:42 -040024GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps,
25 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050026 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040027 int sampleCount,
Brian Salomonbeb7f522019-08-30 16:19:42 -040028 SkBackingFit fit,
29 SkBudgeted budgeted,
30 GrProtected isProtected,
31 GrInternalSurfaceFlags surfaceFlags,
32 UseAllocator useAllocator)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040033 : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040034 , fSampleCnt(sampleCount)
Greg Danielbaf8d992019-10-29 14:14:32 -040035 , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo) {}
csmartdaltonf9635992016-08-10 11:09:07 -070036
Chris Dalton706a6ff2017-11-29 22:01:06 -070037// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040038GrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback,
39 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050040 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040041 int sampleCount,
Brian Salomonbeb7f522019-08-30 16:19:42 -040042 SkBackingFit fit,
43 SkBudgeted budgeted,
44 GrProtected isProtected,
45 GrInternalSurfaceFlags surfaceFlags,
46 UseAllocator useAllocator,
47 WrapsVkSecondaryCB wrapsVkSecondaryCB)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040048 : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected,
49 surfaceFlags, useAllocator)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040050 , fSampleCnt(sampleCount)
Greg Danielbaf8d992019-10-29 14:14:32 -040051 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070052
csmartdaltonf9635992016-08-10 11:09:07 -070053// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -040054GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf,
Brian Salomonbeb7f522019-08-30 16:19:42 -040055 UseAllocator useAllocator,
Greg Danielb46add82019-01-02 14:51:29 -050056 WrapsVkSecondaryCB wrapsVkSecondaryCB)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040057 : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
Chris Dalton6ce447a2019-06-23 18:07:38 -060058 , fSampleCnt(fTarget->asRenderTarget()->numSamples())
Greg Danielbaf8d992019-10-29 14:14:32 -040059 , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {
Chris Dalton3f7932e2019-08-19 00:39:13 -060060 // The kRequiresManualMSAAResolve flag better not be set if we are not multisampled or if
61 // MSAA resolve should happen automatically.
62 //
63 // From the other side, we don't know enough about the wrapped surface to assert when
64 // kRequiresManualMSAAResolve *should* be set. e.g., The caller might be wrapping a backend
65 // texture as a render target at this point but we wouldn't know it.
66 SkASSERT(!(this->numSamples() <= 1 ||
67 fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) ||
68 !this->requiresManualMSAAResolve());
Robert Phillipsc4f0a822017-06-13 08:11:36 -040069}
robertphillips76948d42016-05-04 12:47:41 -070070
Robert Phillipsec2249f2016-11-09 08:54:35 -050071int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const {
Jim Van Verth5fab9092019-11-25 21:23:53 +000072 return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles();
Robert Phillipsec2249f2016-11-09 08:54:35 -050073}
74
Robert Phillips10d17212019-04-24 14:09:10 -040075bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040076 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -050077 return false;
78 }
Chris Dalton0b68dda2019-11-07 21:08:03 -070079 if (!this->instantiateImpl(resourceProvider, fSampleCnt, GrRenderable::kYes, GrMipMapped::kNo,
80 nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040081 return false;
robertphillips76948d42016-05-04 12:47:41 -070082 }
Robert Phillipsb5204762019-06-19 14:12:13 -040083
84 SkASSERT(this->peekRenderTarget());
85 SkASSERT(!this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040086 return true;
robertphillips76948d42016-05-04 12:47:41 -070087}
88
Chris Dalton215ff332019-07-02 09:38:22 -060089bool GrRenderTargetProxy::canChangeStencilAttachment() const {
90 if (!fTarget) {
91 // If we aren't instantiated, then we definitely are an internal render target. Ganesh is
92 // free to change stencil attachments on internal render targets.
93 return true;
94 }
95 return fTarget->asRenderTarget()->canAttemptStencilAttachment();
96}
97
Robert Phillips5af44de2017-07-18 14:49:38 -040098sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -070099 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, fSampleCnt,
100 GrRenderable::kYes, GrMipMapped::kNo);
Robert Phillips5af44de2017-07-18 14:49:38 -0400101 if (!surface) {
102 return nullptr;
103 }
104 SkASSERT(surface->asRenderTarget());
Greg Daniele252f082017-10-23 16:05:23 -0400105 SkASSERT(!surface->asTexture());
Robert Phillips5af44de2017-07-18 14:49:38 -0400106 return surface;
107}
108
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400109size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600110 int colorSamplesPerPixel = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500111 if (colorSamplesPerPixel > 1) {
112 // Add one for the resolve buffer.
113 ++colorSamplesPerPixel;
114 }
Greg Daniele252f082017-10-23 16:05:23 -0400115
Robert Phillips8bc06d02016-11-01 17:28:40 -0400116 // TODO: do we have enough information to improve this worst case estimate?
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400117 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Greg Daniela00bcad2019-10-11 13:21:48 -0400118 colorSamplesPerPixel, GrMipMapped::kNo, !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400119}
120
Robert Phillipse2f7d182016-12-15 09:23:05 -0500121bool GrRenderTargetProxy::refsWrappedObjects() const {
Robert Phillipsb5204762019-06-19 14:12:13 -0400122 if (!this->isInstantiated()) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500123 return false;
124 }
125
Robert Phillipsb5204762019-06-19 14:12:13 -0400126 GrSurface* surface = this->peekSurface();
127 return surface->resourcePriv().refsWrappedObjects();
Robert Phillipse2f7d182016-12-15 09:23:05 -0500128}
Robert Phillipse8fabb22018-02-04 14:33:21 -0500129
Brian Salomon63410e92020-03-23 18:32:50 -0400130GrSurfaceProxy::LazySurfaceDesc GrRenderTargetProxy::callbackDesc() const {
131 // We only expect exactly sized lazy RT proxies.
132 SkASSERT(!this->isFullyLazy());
133 SkASSERT(this->isFunctionallyExact());
134 return {
135 this->dimensions(),
136 SkBackingFit::kExact,
137 GrRenderable::kYes,
138 GrMipMapped::kNo,
139 this->numSamples(),
140 this->backendFormat(),
141 this->isProtected(),
142 this->isBudgeted(),
143 };
144}
145
Robert Phillipse8fabb22018-02-04 14:33:21 -0500146#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400147void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500148 // We do not check that surface->asTexture returns null since, when replaying DDLs we
149 // can fulfill a renderTarget-only proxy w/ a textureRenderTarget.
Robert Phillipse8fabb22018-02-04 14:33:21 -0500150
151 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
152 SkASSERT(surface->asRenderTarget());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600153 SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
Greg Daniel849dce12018-04-24 14:32:53 -0400154
Greg Daniela070ed72018-04-26 16:31:38 -0400155 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
156 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Robert Phillips350db7c2020-03-06 13:39:41 -0500157 if (proxyFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0 && this->numSamples() == 1) {
158 // Ganesh never internally creates FBO0 proxies or surfaces so this must be a wrapped
159 // proxy. In this case, with no MSAA, rendering to FBO0 is strictly more limited than
160 // rendering to an arbitrary surface so we allow a non-FBO0 surface to be matched with
161 // the proxy.
162 surfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
163 }
Chris Dalton3f7932e2019-08-19 00:39:13 -0600164 SkASSERT(((int)proxyFlags & kGrInternalRenderTargetFlagsMask) ==
165 ((int)surfaceFlags & kGrInternalRenderTargetFlagsMask));
Robert Phillipse8fabb22018-02-04 14:33:21 -0500166}
167#endif