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