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