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