robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrRenderTargetProxy.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkMathPriv.h" |
| 11 | #include "src/gpu/GrCaps.h" |
| 12 | #include "src/gpu/GrGpuResourcePriv.h" |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrResourceProvider.h" |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrSurface.h" |
Robert Phillips | 4916c31 | 2021-08-02 10:09:00 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceProxyPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrTextureRenderTargetProxy.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 18 | |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 19 | #ifdef SK_DEBUG |
| 20 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrDirectContextPriv.h" |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 22 | #endif |
| 23 | |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 24 | // 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 Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 27 | GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps, |
| 28 | const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 29 | SkISize dimensions, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 30 | int sampleCount, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 31 | SkBackingFit fit, |
| 32 | SkBudgeted budgeted, |
| 33 | GrProtected isProtected, |
| 34 | GrInternalSurfaceFlags surfaceFlags, |
| 35 | UseAllocator useAllocator) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 36 | : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator) |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 37 | , fSampleCnt(sampleCount) |
Greg Daniel | baf8d99 | 2019-10-29 14:14:32 -0400 | [diff] [blame] | 38 | , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo) {} |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 39 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 40 | // Lazy-callback version |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 41 | GrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback, |
| 42 | const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 43 | SkISize dimensions, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 44 | int sampleCount, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 45 | SkBackingFit fit, |
| 46 | SkBudgeted budgeted, |
| 47 | GrProtected isProtected, |
| 48 | GrInternalSurfaceFlags surfaceFlags, |
| 49 | UseAllocator useAllocator, |
| 50 | WrapsVkSecondaryCB wrapsVkSecondaryCB) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 51 | : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected, |
| 52 | surfaceFlags, useAllocator) |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 53 | , fSampleCnt(sampleCount) |
Greg Daniel | baf8d99 | 2019-10-29 14:14:32 -0400 | [diff] [blame] | 54 | , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {} |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 55 | |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 56 | // Wrapped version |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 57 | GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 58 | UseAllocator useAllocator, |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 59 | WrapsVkSecondaryCB wrapsVkSecondaryCB) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 60 | : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator) |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 61 | , fSampleCnt(fTarget->asRenderTarget()->numSamples()) |
Greg Daniel | baf8d99 | 2019-10-29 14:14:32 -0400 | [diff] [blame] | 62 | , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) { |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 63 | // 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 Phillips | c4f0a82 | 2017-06-13 08:11:36 -0400 | [diff] [blame] | 72 | } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 73 | |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 74 | int GrRenderTargetProxy::maxWindowRectangles(const GrCaps& caps) const { |
Jim Van Verth | 5fab909 | 2019-11-25 21:23:53 +0000 | [diff] [blame] | 75 | return this->glRTFBOIDIs0() ? 0 : caps.maxWindowRectangles(); |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 78 | bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 79 | if (this->isLazy()) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 80 | return false; |
| 81 | } |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 82 | if (!this->instantiateImpl(resourceProvider, fSampleCnt, GrRenderable::kYes, GrMipmapped::kNo, |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 83 | nullptr)) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 84 | return false; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 85 | } |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 86 | |
| 87 | SkASSERT(this->peekRenderTarget()); |
| 88 | SkASSERT(!this->peekTexture()); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 89 | return true; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Chris Dalton | 537293bf | 2021-05-03 15:54:24 -0600 | [diff] [blame] | 92 | bool GrRenderTargetProxy::canUseStencil(const GrCaps& caps) const { |
| 93 | if (caps.avoidStencilBuffers() || this->wrapsVkSecondaryCB()) { |
| 94 | return false; |
| 95 | } |
| 96 | if (!this->isInstantiated()) { |
| 97 | if (this->isLazy() && this->backendFormat().backend() == GrBackendApi::kOpenGL) { |
| 98 | // It's possible for wrapped GL render targets to not have stencil. We don't currently |
| 99 | // have an exact way of knowing whether the target will be able to use stencil, so we do |
| 100 | // the best we can: if a lazy GL proxy doesn't have a texture, then it might be a |
| 101 | // wrapped target without stencil, so we conservatively block stencil. |
| 102 | // FIXME: skbug.com/11943: SkSurfaceCharacterization needs a "canUseStencil" flag. |
| 103 | return SkToBool(this->asTextureProxy()); |
| 104 | } else { |
| 105 | // Otherwise the target will definitely not be wrapped. Ganesh is free to attach |
| 106 | // stencils on internal render targets. |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | // Just ask the actual target if we can use stencil. |
| 111 | GrRenderTarget* rt = this->peekRenderTarget(); |
| 112 | // The dmsaa attachment (if any) always supports stencil. The real question is whether the |
| 113 | // non-dmsaa attachment supports stencil. |
| 114 | bool useMSAASurface = rt->numSamples() > 1; |
| 115 | return rt->getStencilAttachment(useMSAASurface) || |
| 116 | rt->canAttemptStencilAttachment(useMSAASurface); |
| 117 | } |
| 118 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 119 | sk_sp<GrSurface> GrRenderTargetProxy::createSurface(GrResourceProvider* resourceProvider) const { |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 120 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, fSampleCnt, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 121 | GrRenderable::kYes, GrMipmapped::kNo); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 122 | if (!surface) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | SkASSERT(surface->asRenderTarget()); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 126 | SkASSERT(!surface->asTexture()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 127 | return surface; |
| 128 | } |
| 129 | |
Greg Daniel | 0eca74c | 2020-10-01 13:46:00 -0400 | [diff] [blame] | 130 | size_t GrRenderTargetProxy::onUninstantiatedGpuMemorySize() const { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 131 | int colorSamplesPerPixel = this->numSamples(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 132 | if (colorSamplesPerPixel > 1) { |
| 133 | // Add one for the resolve buffer. |
| 134 | ++colorSamplesPerPixel; |
| 135 | } |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 136 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 137 | // TODO: do we have enough information to improve this worst case estimate? |
Greg Daniel | 0eca74c | 2020-10-01 13:46:00 -0400 | [diff] [blame] | 138 | return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(), |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 139 | colorSamplesPerPixel, GrMipmapped::kNo, !this->priv().isExact()); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 142 | bool GrRenderTargetProxy::refsWrappedObjects() const { |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 143 | if (!this->isInstantiated()) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 147 | GrSurface* surface = this->peekSurface(); |
| 148 | return surface->resourcePriv().refsWrappedObjects(); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 149 | } |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 150 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 151 | GrSurfaceProxy::LazySurfaceDesc GrRenderTargetProxy::callbackDesc() const { |
| 152 | // We only expect exactly sized lazy RT proxies. |
| 153 | SkASSERT(!this->isFullyLazy()); |
| 154 | SkASSERT(this->isFunctionallyExact()); |
| 155 | return { |
| 156 | this->dimensions(), |
| 157 | SkBackingFit::kExact, |
| 158 | GrRenderable::kYes, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 159 | GrMipmapped::kNo, |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 160 | this->numSamples(), |
| 161 | this->backendFormat(), |
Greg Daniel | e56d31f | 2021-08-16 09:29:56 -0400 | [diff] [blame] | 162 | GrTextureType::kNone, |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 163 | this->isProtected(), |
| 164 | this->isBudgeted(), |
| 165 | }; |
| 166 | } |
| 167 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 168 | #ifdef SK_DEBUG |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 169 | void GrRenderTargetProxy::onValidateSurface(const GrSurface* surface) { |
Robert Phillips | b45f47d | 2019-02-03 17:17:54 -0500 | [diff] [blame] | 170 | // We do not check that surface->asTexture returns null since, when replaying DDLs we |
| 171 | // can fulfill a renderTarget-only proxy w/ a textureRenderTarget. |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 172 | |
| 173 | // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version |
| 174 | SkASSERT(surface->asRenderTarget()); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 175 | SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples()); |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 176 | |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 177 | GrInternalSurfaceFlags proxyFlags = fSurfaceFlags; |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 178 | GrInternalSurfaceFlags surfaceFlags = surface->flags(); |
Robert Phillips | 350db7c | 2020-03-06 13:39:41 -0500 | [diff] [blame] | 179 | if (proxyFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0 && this->numSamples() == 1) { |
| 180 | // Ganesh never internally creates FBO0 proxies or surfaces so this must be a wrapped |
| 181 | // proxy. In this case, with no MSAA, rendering to FBO0 is strictly more limited than |
| 182 | // rendering to an arbitrary surface so we allow a non-FBO0 surface to be matched with |
| 183 | // the proxy. |
| 184 | surfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0; |
| 185 | } |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 186 | SkASSERT(((int)proxyFlags & kGrInternalRenderTargetFlagsMask) == |
| 187 | ((int)surfaceFlags & kGrInternalRenderTargetFlagsMask)); |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 188 | |
| 189 | // We manually check the kVkRTSupportsInputAttachment since we only require it on the surface if |
| 190 | // the proxy has it set. If the proxy doesn't have the flag it is legal for the surface to |
| 191 | // have the flag. |
| 192 | if (proxyFlags & GrInternalSurfaceFlags::kVkRTSupportsInputAttachment) { |
| 193 | SkASSERT(surfaceFlags & GrInternalSurfaceFlags::kVkRTSupportsInputAttachment); |
| 194 | } |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 195 | } |
| 196 | #endif |