xref: /aosp_15_r20/external/skia/src/gpu/graphite/TextureProxyView.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2022 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #ifndef skgpu_graphite_TextureProxyView_DEFINED
9*c8dee2aaSAndroid Build Coastguard Worker #define skgpu_graphite_TextureProxyView_DEFINED
10*c8dee2aaSAndroid Build Coastguard Worker 
11*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkRect.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkRefCnt.h"
13*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/graphite/GraphiteTypes.h"
14*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/Swizzle.h"
15*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/TextureProxy.h"
16*c8dee2aaSAndroid Build Coastguard Worker 
17*c8dee2aaSAndroid Build Coastguard Worker enum class SkBackingFit;
18*c8dee2aaSAndroid Build Coastguard Worker 
19*c8dee2aaSAndroid Build Coastguard Worker namespace skgpu::graphite {
20*c8dee2aaSAndroid Build Coastguard Worker 
21*c8dee2aaSAndroid Build Coastguard Worker class Recorder;
22*c8dee2aaSAndroid Build Coastguard Worker 
23*c8dee2aaSAndroid Build Coastguard Worker class TextureProxyView {
24*c8dee2aaSAndroid Build Coastguard Worker public:
25*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView() = default;
26*c8dee2aaSAndroid Build Coastguard Worker 
TextureProxyView(sk_sp<TextureProxy> proxy,Swizzle swizzle)27*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView(sk_sp<TextureProxy> proxy, Swizzle swizzle)
28*c8dee2aaSAndroid Build Coastguard Worker             : fProxy(std::move(proxy)), fSwizzle(swizzle) {}
29*c8dee2aaSAndroid Build Coastguard Worker 
TextureProxyView(sk_sp<TextureProxy> proxy,Swizzle swizzle,Origin origin)30*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView(sk_sp<TextureProxy> proxy, Swizzle swizzle, Origin origin)
31*c8dee2aaSAndroid Build Coastguard Worker             : fProxy(std::move(proxy)), fSwizzle(swizzle), fOrigin(origin) {}
32*c8dee2aaSAndroid Build Coastguard Worker 
33*c8dee2aaSAndroid Build Coastguard Worker     // This entry point is used when we don't care about the swizzle and assume TopLeft origin.
TextureProxyView(sk_sp<TextureProxy> proxy)34*c8dee2aaSAndroid Build Coastguard Worker     explicit TextureProxyView(sk_sp<TextureProxy> proxy)
35*c8dee2aaSAndroid Build Coastguard Worker             : fProxy(std::move(proxy)) {}
36*c8dee2aaSAndroid Build Coastguard Worker 
37*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView(TextureProxyView&& view) = default;
38*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView(const TextureProxyView&) = default;
39*c8dee2aaSAndroid Build Coastguard Worker 
40*c8dee2aaSAndroid Build Coastguard Worker     explicit operator bool() const { return SkToBool(fProxy.get()); }
41*c8dee2aaSAndroid Build Coastguard Worker 
42*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView& operator=(const TextureProxyView&) = default;
43*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView& operator=(TextureProxyView&& view) = default;
44*c8dee2aaSAndroid Build Coastguard Worker 
45*c8dee2aaSAndroid Build Coastguard Worker     bool operator==(const TextureProxyView& view) const {
46*c8dee2aaSAndroid Build Coastguard Worker         return fProxy == view.fProxy &&
47*c8dee2aaSAndroid Build Coastguard Worker                fSwizzle == view.fSwizzle &&
48*c8dee2aaSAndroid Build Coastguard Worker                fOrigin == view.fOrigin;
49*c8dee2aaSAndroid Build Coastguard Worker     }
50*c8dee2aaSAndroid Build Coastguard Worker     bool operator!=(const TextureProxyView& other) const { return !(*this == other); }
51*c8dee2aaSAndroid Build Coastguard Worker 
width()52*c8dee2aaSAndroid Build Coastguard Worker     int width() const { return this->proxy()->dimensions().width(); }
height()53*c8dee2aaSAndroid Build Coastguard Worker     int height() const { return this->proxy()->dimensions().height(); }
dimensions()54*c8dee2aaSAndroid Build Coastguard Worker     SkISize dimensions() const { return this->proxy()->dimensions(); }
55*c8dee2aaSAndroid Build Coastguard Worker 
mipmapped()56*c8dee2aaSAndroid Build Coastguard Worker     skgpu::Mipmapped mipmapped() const {
57*c8dee2aaSAndroid Build Coastguard Worker         if (const TextureProxy* proxy = this->proxy()) {
58*c8dee2aaSAndroid Build Coastguard Worker             return proxy->mipmapped();
59*c8dee2aaSAndroid Build Coastguard Worker         }
60*c8dee2aaSAndroid Build Coastguard Worker         return skgpu::Mipmapped::kNo;
61*c8dee2aaSAndroid Build Coastguard Worker     }
62*c8dee2aaSAndroid Build Coastguard Worker 
proxy()63*c8dee2aaSAndroid Build Coastguard Worker     TextureProxy* proxy() const { return fProxy.get(); }
refProxy()64*c8dee2aaSAndroid Build Coastguard Worker     sk_sp<TextureProxy> refProxy() const { return fProxy; }
65*c8dee2aaSAndroid Build Coastguard Worker 
swizzle()66*c8dee2aaSAndroid Build Coastguard Worker     Swizzle swizzle() const { return fSwizzle; }
origin()67*c8dee2aaSAndroid Build Coastguard Worker     Origin origin() const { return fOrigin; }
68*c8dee2aaSAndroid Build Coastguard Worker 
concatSwizzle(Swizzle swizzle)69*c8dee2aaSAndroid Build Coastguard Worker     void concatSwizzle(Swizzle swizzle) {
70*c8dee2aaSAndroid Build Coastguard Worker         fSwizzle = skgpu::Swizzle::Concat(fSwizzle, swizzle);
71*c8dee2aaSAndroid Build Coastguard Worker     }
72*c8dee2aaSAndroid Build Coastguard Worker 
73*c8dee2aaSAndroid Build Coastguard Worker     // makeSwizzle returns a new view with 'swizzle' composed on to this view's existing swizzle
makeSwizzle(Swizzle swizzle)74*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView makeSwizzle(Swizzle swizzle) const & {
75*c8dee2aaSAndroid Build Coastguard Worker         return {fProxy, Swizzle::Concat(fSwizzle, swizzle), fOrigin};
76*c8dee2aaSAndroid Build Coastguard Worker     }
77*c8dee2aaSAndroid Build Coastguard Worker 
makeSwizzle(Swizzle swizzle)78*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView makeSwizzle(Swizzle swizzle) && {
79*c8dee2aaSAndroid Build Coastguard Worker         return {std::move(fProxy), Swizzle::Concat(fSwizzle, swizzle), fOrigin};
80*c8dee2aaSAndroid Build Coastguard Worker     }
81*c8dee2aaSAndroid Build Coastguard Worker 
82*c8dee2aaSAndroid Build Coastguard Worker     // resetSwizzle returns a new view that uses 'swizzle' and disregards this view's prior swizzle.
replaceSwizzle(Swizzle swizzle)83*c8dee2aaSAndroid Build Coastguard Worker     TextureProxyView replaceSwizzle(Swizzle swizzle) const {
84*c8dee2aaSAndroid Build Coastguard Worker         return {fProxy, swizzle, fOrigin};
85*c8dee2aaSAndroid Build Coastguard Worker     }
86*c8dee2aaSAndroid Build Coastguard Worker 
reset()87*c8dee2aaSAndroid Build Coastguard Worker     void reset() {
88*c8dee2aaSAndroid Build Coastguard Worker         *this = {};
89*c8dee2aaSAndroid Build Coastguard Worker     }
90*c8dee2aaSAndroid Build Coastguard Worker 
91*c8dee2aaSAndroid Build Coastguard Worker     // This does not reset the swizzle, so the View can still be used to access those
92*c8dee2aaSAndroid Build Coastguard Worker     // properties associated with the detached proxy.
detachProxy()93*c8dee2aaSAndroid Build Coastguard Worker     sk_sp<TextureProxy> detachProxy() {
94*c8dee2aaSAndroid Build Coastguard Worker         return std::move(fProxy);
95*c8dee2aaSAndroid Build Coastguard Worker     }
96*c8dee2aaSAndroid Build Coastguard Worker 
97*c8dee2aaSAndroid Build Coastguard Worker private:
98*c8dee2aaSAndroid Build Coastguard Worker     sk_sp<TextureProxy> fProxy;
99*c8dee2aaSAndroid Build Coastguard Worker     Swizzle fSwizzle;
100*c8dee2aaSAndroid Build Coastguard Worker     Origin fOrigin = Origin::kTopLeft;
101*c8dee2aaSAndroid Build Coastguard Worker };
102*c8dee2aaSAndroid Build Coastguard Worker 
103*c8dee2aaSAndroid Build Coastguard Worker } // namespace skgpu::graphite
104*c8dee2aaSAndroid Build Coastguard Worker 
105*c8dee2aaSAndroid Build Coastguard Worker #endif // skgpu_graphite_TextureProxyView_DEFINED
106