blob: a04301855faf5577556d7bb370af9bfac1b9de29 [file] [log] [blame]
Greg Daniela83de582019-10-22 09:33:25 -04001/*
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 Daniel3912a4b2020-01-14 09:56:04 -050013#include "src/gpu/GrRenderTargetProxy.h"
Greg Daniela83de582019-10-22 09:33:25 -040014#include "src/gpu/GrSurfaceProxy.h"
15#include "src/gpu/GrSwizzle.h"
Greg Danielc52db712020-01-28 17:03:46 -050016#include "src/gpu/GrTextureProxy.h"
Greg Daniela83de582019-10-22 09:33:25 -040017
18class GrSurfaceProxyView {
19public:
Greg Daniel16f5c652019-10-29 11:26:01 -040020 GrSurfaceProxyView() = default;
21
Greg Daniela83de582019-10-22 09:33:25 -040022 GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrSwizzle swizzle)
Adlai Holler8501afd2020-07-27 14:38:43 -040023 : fProxy(std::move(proxy)), fOrigin(origin), fSwizzle(swizzle) {}
Greg Daniela83de582019-10-22 09:33:25 -040024
Greg Daniel16f5c652019-10-29 11:26:01 -040025 // This entry point is used when we don't care about the origin or the swizzle.
Greg Danield2ccbb52020-02-05 10:45:39 -050026 explicit GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy)
Adlai Holler8501afd2020-07-27 14:38:43 -040027 : fProxy(std::move(proxy)), fOrigin(kTopLeft_GrSurfaceOrigin) {}
Greg Daniela83de582019-10-22 09:33:25 -040028
Greg Daniel16f5c652019-10-29 11:26:01 -040029 GrSurfaceProxyView(GrSurfaceProxyView&& view) = default;
Greg Daniel549325c2019-10-30 16:19:20 -040030 GrSurfaceProxyView(const GrSurfaceProxyView&) = default;
Greg Daniel16f5c652019-10-29 11:26:01 -040031
Greg Danielcc21d0c2020-02-05 16:58:40 -050032 operator bool() const { return SkToBool(fProxy.get()); }
33
Greg Daniel549325c2019-10-30 16:19:20 -040034 GrSurfaceProxyView& operator=(const GrSurfaceProxyView&) = default;
Greg Danielee72bd92020-02-06 15:19:22 -050035 GrSurfaceProxyView& operator=(GrSurfaceProxyView&& view) = default;
Greg Daniel16f5c652019-10-29 11:26:01 -040036
Greg Daniel549325c2019-10-30 16:19:20 -040037 bool operator==(const GrSurfaceProxyView& view) const {
38 return fProxy->uniqueID() == view.fProxy->uniqueID() &&
Greg Daniel16f5c652019-10-29 11:26:01 -040039 fOrigin == view.fOrigin &&
40 fSwizzle == view.fSwizzle;
41 }
Greg Daniel549325c2019-10-30 16:19:20 -040042 bool operator!=(const GrSurfaceProxyView& other) const { return !(*this == other); }
Greg Daniel16f5c652019-10-29 11:26:01 -040043
Greg Daniel026a60c2020-02-12 10:53:51 -050044 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 Phillips9af0bca2021-05-27 19:17:55 -040048 GrMipmapped mipmapped() const {
49 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
50 return proxy->mipmapped();
51 }
52 return GrMipmapped::kNo;
53 }
54
Greg Daniel16f5c652019-10-29 11:26:01 -040055 GrSurfaceProxy* proxy() const { return fProxy.get(); }
Greg Danielc61d7e32020-02-04 14:27:45 -050056 sk_sp<GrSurfaceProxy> refProxy() const { return fProxy; }
Greg Daniel3912a4b2020-01-14 09:56:04 -050057
Greg Daniel524e28b2019-11-01 11:48:53 -040058 GrTextureProxy* asTextureProxy() const {
59 if (!fProxy) {
60 return nullptr;
61 }
62 return fProxy->asTextureProxy();
63 }
Greg Daniel9715b6c2019-12-10 15:03:10 -050064 sk_sp<GrTextureProxy> asTextureProxyRef() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -050065 return sk_ref_sp<GrTextureProxy>(this->asTextureProxy());
Greg Daniel9715b6c2019-12-10 15:03:10 -050066 }
67
Greg Daniel524e28b2019-11-01 11:48:53 -040068 GrRenderTargetProxy* asRenderTargetProxy() const {
69 if (!fProxy) {
70 return nullptr;
71 }
72 return fProxy->asRenderTargetProxy();
73 }
Greg Daniela83de582019-10-22 09:33:25 -040074
Greg Daniel3912a4b2020-01-14 09:56:04 -050075 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() const {
76 return sk_ref_sp<GrRenderTargetProxy>(this->asRenderTargetProxy());
77 }
78
Greg Daniela83de582019-10-22 09:33:25 -040079 GrSurfaceOrigin origin() const { return fOrigin; }
Greg Daniel3912a4b2020-01-14 09:56:04 -050080 GrSwizzle swizzle() const { return fSwizzle; }
Greg Daniela83de582019-10-22 09:33:25 -040081
Brian Salomonb43d6992021-01-05 14:37:40 -050082 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 Daniel16f5c652019-10-29 11:26:01 -040092 void reset() {
93 *this = {};
94 }
95
Brian Salomonc5243782020-04-02 12:50:34 -040096 // 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 Salomon7e67dca2020-07-21 09:27:25 -0400100 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400101 SkIRect srcRect,
102 SkBackingFit fit,
103 SkBudgeted budgeted) {
Brian Salomon982127b2021-01-21 10:43:35 -0500104 auto copy = GrSurfaceProxy::Copy(context,
105 src.refProxy(),
106 src.origin(),
107 mipMapped,
108 srcRect,
109 fit,
110 budgeted);
Brian Salomonc5243782020-04-02 12:50:34 -0400111 return {std::move(copy), src.origin(), src.swizzle()};
112 }
113
Robert Phillipsc2132ea2021-05-17 14:32:29 -0400114 static GrSurfaceProxyView Copy(GrRecordingContext* rContext,
Brian Salomond0924f32021-02-03 10:15:31 -0500115 GrSurfaceProxyView src,
116 GrMipmapped mipMapped,
117 SkBackingFit fit,
118 SkBudgeted budgeted) {
Robert Phillipsc2132ea2021-05-17 14:32:29 -0400119 auto copy = GrSurfaceProxy::Copy(rContext,
Brian Salomond0924f32021-02-03 10:15:31 -0500120 src.refProxy(),
121 src.origin(),
122 mipMapped,
123 fit,
124 budgeted);
125 return {std::move(copy), src.origin(), src.swizzle()};
126 }
127
Greg Danielc52db712020-01-28 17:03:46 -0500128 // This does not reset the origin or swizzle, so the View can still be used to access those
Michael Ludwigadb12e72019-12-04 16:19:18 -0500129 // properties associated with the detached proxy.
130 sk_sp<GrSurfaceProxy> detachProxy() {
131 return std::move(fProxy);
132 }
133
Greg Daniela83de582019-10-22 09:33:25 -0400134private:
135 sk_sp<GrSurfaceProxy> fProxy;
Greg Daniel16f5c652019-10-29 11:26:01 -0400136 GrSurfaceOrigin fOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniela83de582019-10-22 09:33:25 -0400137 GrSwizzle fSwizzle;
138};
139
140#endif
141