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