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