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