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