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