Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 | #ifndef GrSurfaceProxyView_DEFINED |
| 9 | #define GrSurfaceProxyView_DEFINED |
| 10 | |
| 11 | #include "include/core/SkRefCnt.h" |
| 12 | #include "include/gpu/GrTypes.h" |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrRenderTargetProxy.h" |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrSurfaceProxy.h" |
| 15 | #include "src/gpu/GrSwizzle.h" |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrTextureProxy.h" |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 17 | |
| 18 | class GrSurfaceProxyView { |
| 19 | public: |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 20 | GrSurfaceProxyView() = default; |
| 21 | |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 22 | GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrSwizzle swizzle) |
Adlai Holler | 8501afd | 2020-07-27 14:38:43 -0400 | [diff] [blame] | 23 | : fProxy(std::move(proxy)), fOrigin(origin), fSwizzle(swizzle) {} |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 24 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 25 | // This entry point is used when we don't care about the origin or the swizzle. |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 26 | explicit GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy) |
Adlai Holler | 8501afd | 2020-07-27 14:38:43 -0400 | [diff] [blame] | 27 | : fProxy(std::move(proxy)), fOrigin(kTopLeft_GrSurfaceOrigin) {} |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 28 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 29 | GrSurfaceProxyView(GrSurfaceProxyView&& view) = default; |
Greg Daniel | 549325c | 2019-10-30 16:19:20 -0400 | [diff] [blame] | 30 | GrSurfaceProxyView(const GrSurfaceProxyView&) = default; |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 31 | |
Greg Daniel | cc21d0c | 2020-02-05 16:58:40 -0500 | [diff] [blame] | 32 | operator bool() const { return SkToBool(fProxy.get()); } |
| 33 | |
Greg Daniel | 549325c | 2019-10-30 16:19:20 -0400 | [diff] [blame] | 34 | GrSurfaceProxyView& operator=(const GrSurfaceProxyView&) = default; |
Greg Daniel | ee72bd9 | 2020-02-06 15:19:22 -0500 | [diff] [blame] | 35 | GrSurfaceProxyView& operator=(GrSurfaceProxyView&& view) = default; |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 36 | |
Greg Daniel | 549325c | 2019-10-30 16:19:20 -0400 | [diff] [blame] | 37 | bool operator==(const GrSurfaceProxyView& view) const { |
| 38 | return fProxy->uniqueID() == view.fProxy->uniqueID() && |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 39 | fOrigin == view.fOrigin && |
| 40 | fSwizzle == view.fSwizzle; |
| 41 | } |
Greg Daniel | 549325c | 2019-10-30 16:19:20 -0400 | [diff] [blame] | 42 | bool operator!=(const GrSurfaceProxyView& other) const { return !(*this == other); } |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 43 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 44 | int width() const { return this->proxy()->width(); } |
| 45 | int height() const { return this->proxy()->height(); } |
| 46 | SkISize dimensions() const { return this->proxy()->dimensions(); } |
| 47 | |
Robert Phillips | 9af0bca | 2021-05-27 19:17:55 -0400 | [diff] [blame] | 48 | GrMipmapped mipmapped() const { |
| 49 | if (const GrTextureProxy* proxy = this->asTextureProxy()) { |
| 50 | return proxy->mipmapped(); |
| 51 | } |
| 52 | return GrMipmapped::kNo; |
| 53 | } |
| 54 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 55 | GrSurfaceProxy* proxy() const { return fProxy.get(); } |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 56 | sk_sp<GrSurfaceProxy> refProxy() const { return fProxy; } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 57 | |
Greg Daniel | 524e28b | 2019-11-01 11:48:53 -0400 | [diff] [blame] | 58 | GrTextureProxy* asTextureProxy() const { |
| 59 | if (!fProxy) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | return fProxy->asTextureProxy(); |
| 63 | } |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 64 | sk_sp<GrTextureProxy> asTextureProxyRef() const { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 65 | return sk_ref_sp<GrTextureProxy>(this->asTextureProxy()); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Greg Daniel | 524e28b | 2019-11-01 11:48:53 -0400 | [diff] [blame] | 68 | GrRenderTargetProxy* asRenderTargetProxy() const { |
| 69 | if (!fProxy) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | return fProxy->asRenderTargetProxy(); |
| 73 | } |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 74 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 75 | sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() const { |
| 76 | return sk_ref_sp<GrRenderTargetProxy>(this->asRenderTargetProxy()); |
| 77 | } |
| 78 | |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 79 | GrSurfaceOrigin origin() const { return fOrigin; } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 80 | GrSwizzle swizzle() const { return fSwizzle; } |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 81 | |
Brian Salomon | b43d699 | 2021-01-05 14:37:40 -0500 | [diff] [blame] | 82 | void concatSwizzle(GrSwizzle swizzle) { fSwizzle = GrSwizzle::Concat(fSwizzle, swizzle); } |
| 83 | |
| 84 | GrSurfaceProxyView makeSwizzle(GrSwizzle swizzle) const & { |
| 85 | return {fProxy, fOrigin, GrSwizzle::Concat(fSwizzle, swizzle)}; |
| 86 | } |
| 87 | |
| 88 | GrSurfaceProxyView makeSwizzle(GrSwizzle swizzle) && { |
| 89 | return {std::move(fProxy), fOrigin, GrSwizzle::Concat(fSwizzle, swizzle)}; |
| 90 | } |
| 91 | |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 92 | void reset() { |
| 93 | *this = {}; |
| 94 | } |
| 95 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 96 | // Helper that copies a rect of a src view'' proxy and then creates a view for the copy with |
| 97 | // the same origin and swizzle as the src view. |
| 98 | static GrSurfaceProxyView Copy(GrRecordingContext* context, |
| 99 | GrSurfaceProxyView src, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 100 | GrMipmapped mipMapped, |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 101 | SkIRect srcRect, |
| 102 | SkBackingFit fit, |
| 103 | SkBudgeted budgeted) { |
Brian Salomon | 982127b | 2021-01-21 10:43:35 -0500 | [diff] [blame] | 104 | auto copy = GrSurfaceProxy::Copy(context, |
| 105 | src.refProxy(), |
| 106 | src.origin(), |
| 107 | mipMapped, |
| 108 | srcRect, |
| 109 | fit, |
| 110 | budgeted); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 111 | return {std::move(copy), src.origin(), src.swizzle()}; |
| 112 | } |
| 113 | |
Robert Phillips | c2132ea | 2021-05-17 14:32:29 -0400 | [diff] [blame] | 114 | static GrSurfaceProxyView Copy(GrRecordingContext* rContext, |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 115 | GrSurfaceProxyView src, |
| 116 | GrMipmapped mipMapped, |
| 117 | SkBackingFit fit, |
| 118 | SkBudgeted budgeted) { |
Robert Phillips | c2132ea | 2021-05-17 14:32:29 -0400 | [diff] [blame] | 119 | auto copy = GrSurfaceProxy::Copy(rContext, |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 120 | src.refProxy(), |
| 121 | src.origin(), |
| 122 | mipMapped, |
| 123 | fit, |
| 124 | budgeted); |
| 125 | return {std::move(copy), src.origin(), src.swizzle()}; |
| 126 | } |
| 127 | |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 128 | // This does not reset the origin or swizzle, so the View can still be used to access those |
Michael Ludwig | adb12e7 | 2019-12-04 16:19:18 -0500 | [diff] [blame] | 129 | // properties associated with the detached proxy. |
| 130 | sk_sp<GrSurfaceProxy> detachProxy() { |
| 131 | return std::move(fProxy); |
| 132 | } |
| 133 | |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 134 | private: |
| 135 | sk_sp<GrSurfaceProxy> fProxy; |
Greg Daniel | 16f5c65 | 2019-10-29 11:26:01 -0400 | [diff] [blame] | 136 | GrSurfaceOrigin fOrigin = kTopLeft_GrSurfaceOrigin; |
Greg Daniel | a83de58 | 2019-10-22 09:33:25 -0400 | [diff] [blame] | 137 | GrSwizzle fSwizzle; |
| 138 | }; |
| 139 | |
| 140 | #endif |
| 141 | |