Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrTextureRenderTargetProxy.h" |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrCaps.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrRenderTarget.h" |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 12 | #include "src/gpu/GrRenderTargetProxyPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrSurfacePriv.h" |
| 14 | #include "src/gpu/GrSurfaceProxyPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 15 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrTextureProxyPriv.h" |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 17 | |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 18 | #ifdef SK_DEBUG |
| 19 | #include "include/gpu/GrDirectContext.h" |
| 20 | #include "src/gpu/GrContextPriv.h" |
| 21 | #endif |
| 22 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 23 | // Deferred version |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 24 | // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 25 | // GrRenderTargetProxy) so its constructor must be explicitly called. |
| 26 | GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 27 | const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 28 | SkISize dimensions, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 29 | int sampleCnt, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 30 | GrMipmapped mipMapped, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 31 | GrMipmapStatus mipmapStatus, |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 32 | SkBackingFit fit, |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 33 | SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 34 | GrProtected isProtected, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 35 | GrInternalSurfaceFlags surfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 36 | UseAllocator useAllocator, |
| 37 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 38 | : GrSurfaceProxy(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 39 | // for now textures w/ data are always wrapped |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 40 | , GrRenderTargetProxy(caps, format, dimensions, sampleCnt, fit, budgeted, isProtected, |
| 41 | surfaceFlags, useAllocator) |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 42 | , GrTextureProxy(format, dimensions, mipMapped, mipmapStatus, fit, budgeted, isProtected, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 43 | surfaceFlags, useAllocator, creatingProvider) { |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 44 | this->initSurfaceFlags(caps); |
| 45 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 46 | |
| 47 | // Lazy-callback version |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 48 | GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps, |
| 49 | LazyInstantiateCallback&& callback, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 50 | const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 51 | SkISize dimensions, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 52 | int sampleCnt, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 53 | GrMipmapped mipMapped, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 54 | GrMipmapStatus mipmapStatus, |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 55 | SkBackingFit fit, |
| 56 | SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 57 | GrProtected isProtected, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 58 | GrInternalSurfaceFlags surfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 59 | UseAllocator useAllocator, |
| 60 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 61 | : GrSurfaceProxy(std::move(callback), format, dimensions, fit, budgeted, isProtected, |
| 62 | surfaceFlags, useAllocator) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 63 | // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null |
| 64 | // callbacks to the texture and RT proxies simply to route to the appropriate constructors. |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 65 | , GrRenderTargetProxy(LazyInstantiateCallback(), format, dimensions, sampleCnt, fit, |
| 66 | budgeted, isProtected, surfaceFlags, useAllocator, |
Greg Daniel | baf8d99 | 2019-10-29 14:14:32 -0400 | [diff] [blame] | 67 | WrapsVkSecondaryCB::kNo) |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 68 | , GrTextureProxy(LazyInstantiateCallback(), format, dimensions, mipMapped, mipmapStatus, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 69 | fit, budgeted, isProtected, surfaceFlags, useAllocator, |
| 70 | creatingProvider) { |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 71 | this->initSurfaceFlags(caps); |
| 72 | } |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 73 | |
| 74 | // Wrapped version |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 75 | // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 76 | // GrRenderTargetProxy) so its constructor must be explicitly called. |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 77 | GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 78 | UseAllocator useAllocator, |
| 79 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 80 | : GrSurfaceProxy(surf, SkBackingFit::kExact, useAllocator) |
| 81 | , GrRenderTargetProxy(surf, useAllocator) |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 82 | , GrTextureProxy(surf, useAllocator, creatingProvider) { |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 83 | SkASSERT(surf->asTexture()); |
| 84 | SkASSERT(surf->asRenderTarget()); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 85 | SkASSERT(fSurfaceFlags == fTarget->surfacePriv().flags()); |
| 86 | SkASSERT((this->numSamples() <= 1 || |
| 87 | fTarget->getContext()->priv().caps()->msaaResolvesAutomatically()) != |
| 88 | this->requiresManualMSAAResolve()); |
| 89 | } |
| 90 | |
| 91 | void GrTextureRenderTargetProxy::initSurfaceFlags(const GrCaps& caps) { |
Jim Van Verth | 5fab909 | 2019-11-25 21:23:53 +0000 | [diff] [blame] | 92 | // FBO 0 should never be wrapped as a texture render target. |
| 93 | SkASSERT(!this->rtPriv().glRTFBOIDIs0()); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 94 | if (this->numSamples() > 1 && !caps.msaaResolvesAutomatically()) { |
| 95 | // MSAA texture-render-targets always require manual resolve if we are not using a |
| 96 | // multisampled-render-to-texture extension. |
| 97 | // |
| 98 | // NOTE: This is the only instance where we need to set the manual resolve flag on a proxy. |
| 99 | // Any other proxies that require manual resolve (e.g., wrapBackendTextureAsRenderTarget()) |
| 100 | // will be wrapped, and the wrapped version of the GrSurface constructor will automatically |
| 101 | // get the manual resolve flag when copying the target GrSurface's flags. |
| 102 | fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve; |
| 103 | } |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Greg Daniel | 7fd7a8a | 2019-10-10 16:10:31 -0400 | [diff] [blame] | 106 | size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 107 | int colorSamplesPerPixel = this->numSamples(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 108 | if (colorSamplesPerPixel > 1) { |
| 109 | // Add one to account for the resolve buffer. |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 110 | ++colorSamplesPerPixel; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 111 | } |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 112 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 113 | // TODO: do we have enough information to improve this worst case estimate? |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 114 | return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 115 | colorSamplesPerPixel, this->proxyMipmapped(), |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 116 | !this->priv().isExact()); |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 119 | bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 120 | if (this->isLazy()) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 121 | return false; |
| 122 | } |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 123 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 124 | const GrUniqueKey& key = this->getUniqueKey(); |
| 125 | |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 126 | if (!this->instantiateImpl(resourceProvider, this->numSamples(), GrRenderable::kYes, |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 127 | this->mipmapped(), key.isValid() ? &key : nullptr)) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 128 | return false; |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 129 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 130 | if (key.isValid()) { |
| 131 | SkASSERT(key == this->getUniqueKey()); |
| 132 | } |
| 133 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 134 | SkASSERT(this->peekRenderTarget()); |
| 135 | SkASSERT(this->peekTexture()); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 136 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 137 | return true; |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 138 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 139 | |
| 140 | sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface( |
| 141 | GrResourceProvider* resourceProvider) const { |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 142 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, this->numSamples(), |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 143 | GrRenderable::kYes, this->mipmapped()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 144 | if (!surface) { |
| 145 | return nullptr; |
| 146 | } |
| 147 | SkASSERT(surface->asRenderTarget()); |
| 148 | SkASSERT(surface->asTexture()); |
| 149 | |
| 150 | return surface; |
| 151 | } |
| 152 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 153 | GrSurfaceProxy::LazySurfaceDesc GrTextureRenderTargetProxy::callbackDesc() const { |
| 154 | SkISize dims; |
| 155 | SkBackingFit fit; |
| 156 | if (this->isFullyLazy()) { |
| 157 | fit = SkBackingFit::kApprox; |
| 158 | dims = {-1, -1}; |
| 159 | } else { |
| 160 | fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox; |
| 161 | dims = this->dimensions(); |
| 162 | } |
| 163 | return { |
| 164 | dims, |
| 165 | fit, |
| 166 | GrRenderable::kYes, |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 167 | this->mipmapped(), |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 168 | this->numSamples(), |
| 169 | this->backendFormat(), |
| 170 | this->isProtected(), |
| 171 | this->isBudgeted(), |
| 172 | }; |
| 173 | } |
| 174 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 175 | #ifdef SK_DEBUG |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 176 | void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 177 | // Anything checked here should also be checking the GrTextureProxy version |
| 178 | SkASSERT(surface->asTexture()); |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 179 | SkASSERT(GrMipmapped::kNo == this->proxyMipmapped() || |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame^] | 180 | GrMipmapped::kYes == surface->asTexture()->mipmapped()); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 181 | |
| 182 | // Anything checked here should also be checking the GrRenderTargetProxy version |
| 183 | SkASSERT(surface->asRenderTarget()); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 184 | SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples()); |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 185 | |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame^] | 186 | SkASSERT(surface->asTexture()->textureType() == this->textureType()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 187 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 188 | GrInternalSurfaceFlags proxyFlags = fSurfaceFlags; |
| 189 | GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags(); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 190 | |
| 191 | // Only non-RT textures can be read only. |
| 192 | SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly)); |
| 193 | SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly)); |
| 194 | |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 195 | SkASSERT(((int)proxyFlags & kGrInternalTextureRenderTargetFlagsMask) == |
| 196 | ((int)surfaceFlags & kGrInternalTextureRenderTargetFlagsMask)); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 197 | } |
| 198 | #endif |
| 199 | |