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