1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2019 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef GrSurfaceProxyView_DEFINED
9cb93a386Sopenharmony_ci#define GrSurfaceProxyView_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h"
12cb93a386Sopenharmony_ci#include "include/gpu/GrTypes.h"
13cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTargetProxy.h"
14cb93a386Sopenharmony_ci#include "src/gpu/GrSurfaceProxy.h"
15cb93a386Sopenharmony_ci#include "src/gpu/GrSwizzle.h"
16cb93a386Sopenharmony_ci#include "src/gpu/GrTextureProxy.h"
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ciclass GrSurfaceProxyView {
19cb93a386Sopenharmony_cipublic:
20cb93a386Sopenharmony_ci    GrSurfaceProxyView() = default;
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci    GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrSwizzle swizzle)
23cb93a386Sopenharmony_ci            : fProxy(std::move(proxy)), fOrigin(origin), fSwizzle(swizzle) {}
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci    // This entry point is used when we don't care about the origin or the swizzle.
26cb93a386Sopenharmony_ci    explicit GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy)
27cb93a386Sopenharmony_ci            : fProxy(std::move(proxy)), fOrigin(kTopLeft_GrSurfaceOrigin) {}
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_ci    GrSurfaceProxyView(GrSurfaceProxyView&& view) = default;
30cb93a386Sopenharmony_ci    GrSurfaceProxyView(const GrSurfaceProxyView&) = default;
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci    operator bool() const { return SkToBool(fProxy.get()); }
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci    GrSurfaceProxyView& operator=(const GrSurfaceProxyView&) = default;
35cb93a386Sopenharmony_ci    GrSurfaceProxyView& operator=(GrSurfaceProxyView&& view) = default;
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    bool operator==(const GrSurfaceProxyView& view) const {
38cb93a386Sopenharmony_ci        return fProxy->uniqueID() == view.fProxy->uniqueID() &&
39cb93a386Sopenharmony_ci               fOrigin == view.fOrigin &&
40cb93a386Sopenharmony_ci               fSwizzle == view.fSwizzle;
41cb93a386Sopenharmony_ci    }
42cb93a386Sopenharmony_ci    bool operator!=(const GrSurfaceProxyView& other) const { return !(*this == other); }
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    int width() const { return this->proxy()->width(); }
45cb93a386Sopenharmony_ci    int height() const { return this->proxy()->height(); }
46cb93a386Sopenharmony_ci    SkISize dimensions() const { return this->proxy()->dimensions(); }
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    GrMipmapped mipmapped() const {
49cb93a386Sopenharmony_ci        if (const GrTextureProxy* proxy = this->asTextureProxy()) {
50cb93a386Sopenharmony_ci            return proxy->mipmapped();
51cb93a386Sopenharmony_ci        }
52cb93a386Sopenharmony_ci        return GrMipmapped::kNo;
53cb93a386Sopenharmony_ci    }
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci    GrSurfaceProxy* proxy() const { return fProxy.get(); }
56cb93a386Sopenharmony_ci    sk_sp<GrSurfaceProxy> refProxy() const { return fProxy; }
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    GrTextureProxy* asTextureProxy() const {
59cb93a386Sopenharmony_ci        if (!fProxy) {
60cb93a386Sopenharmony_ci            return nullptr;
61cb93a386Sopenharmony_ci        }
62cb93a386Sopenharmony_ci        return fProxy->asTextureProxy();
63cb93a386Sopenharmony_ci    }
64cb93a386Sopenharmony_ci    sk_sp<GrTextureProxy> asTextureProxyRef() const {
65cb93a386Sopenharmony_ci        return sk_ref_sp<GrTextureProxy>(this->asTextureProxy());
66cb93a386Sopenharmony_ci    }
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ci    GrRenderTargetProxy* asRenderTargetProxy() const {
69cb93a386Sopenharmony_ci        if (!fProxy) {
70cb93a386Sopenharmony_ci            return nullptr;
71cb93a386Sopenharmony_ci        }
72cb93a386Sopenharmony_ci        return fProxy->asRenderTargetProxy();
73cb93a386Sopenharmony_ci    }
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci    sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() const {
76cb93a386Sopenharmony_ci        return sk_ref_sp<GrRenderTargetProxy>(this->asRenderTargetProxy());
77cb93a386Sopenharmony_ci    }
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci    GrSurfaceOrigin origin() const { return fOrigin; }
80cb93a386Sopenharmony_ci    GrSwizzle swizzle() const { return fSwizzle; }
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci    void concatSwizzle(GrSwizzle swizzle) { fSwizzle = GrSwizzle::Concat(fSwizzle, swizzle); }
83cb93a386Sopenharmony_ci
84cb93a386Sopenharmony_ci    GrSurfaceProxyView makeSwizzle(GrSwizzle swizzle) const & {
85cb93a386Sopenharmony_ci        return {fProxy, fOrigin, GrSwizzle::Concat(fSwizzle, swizzle)};
86cb93a386Sopenharmony_ci    }
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    GrSurfaceProxyView makeSwizzle(GrSwizzle swizzle) && {
89cb93a386Sopenharmony_ci        return {std::move(fProxy), fOrigin, GrSwizzle::Concat(fSwizzle, swizzle)};
90cb93a386Sopenharmony_ci    }
91cb93a386Sopenharmony_ci
92cb93a386Sopenharmony_ci    void reset() {
93cb93a386Sopenharmony_ci        *this = {};
94cb93a386Sopenharmony_ci    }
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ci    // Helper that copies a rect of a src view'' proxy and then creates a view for the copy with
97cb93a386Sopenharmony_ci    // the same origin and swizzle as the src view.
98cb93a386Sopenharmony_ci    static GrSurfaceProxyView Copy(GrRecordingContext* context,
99cb93a386Sopenharmony_ci                                   GrSurfaceProxyView src,
100cb93a386Sopenharmony_ci                                   GrMipmapped mipMapped,
101cb93a386Sopenharmony_ci                                   SkIRect srcRect,
102cb93a386Sopenharmony_ci                                   SkBackingFit fit,
103cb93a386Sopenharmony_ci                                   SkBudgeted budgeted) {
104cb93a386Sopenharmony_ci        auto copy = GrSurfaceProxy::Copy(context,
105cb93a386Sopenharmony_ci                                         src.refProxy(),
106cb93a386Sopenharmony_ci                                         src.origin(),
107cb93a386Sopenharmony_ci                                         mipMapped,
108cb93a386Sopenharmony_ci                                         srcRect,
109cb93a386Sopenharmony_ci                                         fit,
110cb93a386Sopenharmony_ci                                         budgeted);
111cb93a386Sopenharmony_ci        return {std::move(copy), src.origin(), src.swizzle()};
112cb93a386Sopenharmony_ci    }
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ci    static GrSurfaceProxyView Copy(GrRecordingContext* rContext,
115cb93a386Sopenharmony_ci                                   GrSurfaceProxyView src,
116cb93a386Sopenharmony_ci                                   GrMipmapped mipMapped,
117cb93a386Sopenharmony_ci                                   SkBackingFit fit,
118cb93a386Sopenharmony_ci                                   SkBudgeted budgeted) {
119cb93a386Sopenharmony_ci        auto copy = GrSurfaceProxy::Copy(rContext,
120cb93a386Sopenharmony_ci                                         src.refProxy(),
121cb93a386Sopenharmony_ci                                         src.origin(),
122cb93a386Sopenharmony_ci                                         mipMapped,
123cb93a386Sopenharmony_ci                                         fit,
124cb93a386Sopenharmony_ci                                         budgeted);
125cb93a386Sopenharmony_ci        return {std::move(copy), src.origin(), src.swizzle()};
126cb93a386Sopenharmony_ci    }
127cb93a386Sopenharmony_ci
128cb93a386Sopenharmony_ci    // This does not reset the origin or swizzle, so the View can still be used to access those
129cb93a386Sopenharmony_ci    // properties associated with the detached proxy.
130cb93a386Sopenharmony_ci    sk_sp<GrSurfaceProxy> detachProxy() {
131cb93a386Sopenharmony_ci        return std::move(fProxy);
132cb93a386Sopenharmony_ci    }
133cb93a386Sopenharmony_ci
134cb93a386Sopenharmony_ciprivate:
135cb93a386Sopenharmony_ci    sk_sp<GrSurfaceProxy> fProxy;
136cb93a386Sopenharmony_ci    GrSurfaceOrigin fOrigin = kTopLeft_GrSurfaceOrigin;
137cb93a386Sopenharmony_ci    GrSwizzle fSwizzle;
138cb93a386Sopenharmony_ci};
139cb93a386Sopenharmony_ci
140cb93a386Sopenharmony_ci#endif
141cb93a386Sopenharmony_ci
142