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