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