1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_GFX_ANDROID_ANDROID_SURFACE_CONTROL_COMPAT_H_
6 #define UI_GFX_ANDROID_ANDROID_SURFACE_CONTROL_COMPAT_H_
7 
8 #include <memory>
9 
10 #include <android/hardware_buffer.h>
11 #include <android/native_window.h>
12 
13 #include "base/files/scoped_file.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/single_thread_task_runner.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/gfx_export.h"
18 #include "ui/gfx/overlay_transform.h"
19 
20 extern "C" {
21 typedef struct ASurfaceControl ASurfaceControl;
22 typedef struct ASurfaceTransaction ASurfaceTransaction;
23 }
24 
25 namespace gfx {
26 class ColorSpace;
27 
28 class GFX_EXPORT SurfaceControl {
29  public:
30   // Check if the platform is capable of supporting the low-level SurfaceControl
31   // API. See also gpu/config/gpu_util's GetAndroidSurfaceControlFeatureStatus
32   // which checks other prerequisites such as Gpufence support before declaring
33   // support for the high-level SurfaceControl feature in Chrome.
34   static bool IsSupported();
35 
36   // Returns true if overlays with |color_space| are supported by the platform.
37   static bool SupportsColorSpace(const gfx::ColorSpace& color_space);
38 
39   // Returns the usage flags required for using an AHardwareBuffer with the
40   // SurfaceControl API, if it is supported.
41   static uint64_t RequiredUsage();
42 
43   // Enables usage bits requires for getting UBWC on Qualcomm devices. Must be
44   // called early at process startup, before any buffer allocations are made.
45   static void EnableQualcommUBWC();
46 
47   // Returns true if tagging a surface with a frame rate value is supported.
48   static bool SupportsSetFrameRate();
49 
50   class GFX_EXPORT Surface : public base::RefCounted<Surface> {
51    public:
52     Surface();
53     Surface(const Surface& parent, const char* name);
54     Surface(ANativeWindow* parent, const char* name);
55 
surface()56     ASurfaceControl* surface() const { return surface_; }
57 
58    private:
59     friend class base::RefCounted<Surface>;
60     ~Surface();
61 
62     ASurfaceControl* surface_ = nullptr;
63 
64     DISALLOW_COPY_AND_ASSIGN(Surface);
65   };
66 
67   struct GFX_EXPORT SurfaceStats {
68     SurfaceStats();
69     ~SurfaceStats();
70 
71     SurfaceStats(SurfaceStats&& other);
72     SurfaceStats& operator=(SurfaceStats&& other);
73 
74     ASurfaceControl* surface = nullptr;
75 
76     // The fence which is signaled when the reads for the previous buffer for
77     // the given |surface| are finished.
78     base::ScopedFD fence;
79   };
80 
81   struct GFX_EXPORT TransactionStats {
82    public:
83     TransactionStats();
84     ~TransactionStats();
85 
86     TransactionStats(TransactionStats&& other);
87     TransactionStats& operator=(TransactionStats&& other);
88 
89     // The fence which is signaled when this transaction is presented by the
90     // display.
91     base::ScopedFD present_fence;
92     std::vector<SurfaceStats> surface_stats;
93     base::TimeTicks latch_time;
94 
95    private:
96     DISALLOW_COPY_AND_ASSIGN(TransactionStats);
97   };
98 
99   class GFX_EXPORT Transaction {
100    public:
101     Transaction();
102     ~Transaction();
103 
104     Transaction(Transaction&& other);
105     Transaction& operator=(Transaction&& other);
106 
107     void SetVisibility(const Surface& surface, bool show);
108     void SetZOrder(const Surface& surface, int32_t z);
109     void SetBuffer(const Surface& surface,
110                    AHardwareBuffer* buffer,
111                    base::ScopedFD fence_fd);
112     void SetGeometry(const Surface& surface,
113                      const gfx::Rect& src,
114                      const gfx::Rect& dst,
115                      gfx::OverlayTransform transform);
116     void SetOpaque(const Surface& surface, bool opaque);
117     void SetDamageRect(const Surface& surface, const gfx::Rect& rect);
118     void SetColorSpace(const Surface& surface,
119                        const gfx::ColorSpace& color_space);
120     void SetFrameRate(const Surface& surface, float frame_rate);
121 
122     // Sets the callback which will be dispatched when the transaction is acked
123     // by the framework.
124     // |task_runner| provides an optional task runner on which the callback
125     // should be run.
126     using OnCompleteCb = base::OnceCallback<void(TransactionStats stats)>;
127     void SetOnCompleteCb(
128         OnCompleteCb cb,
129         scoped_refptr<base::SingleThreadTaskRunner> task_runner);
130 
131     void Apply();
132 
133    private:
134     int id_;
135     ASurfaceTransaction* transaction_;
136 
137     DISALLOW_COPY_AND_ASSIGN(Transaction);
138   };
139 };
140 }  // namespace gfx
141 
142 #endif  // UI_GFX_ANDROID_ANDROID_SURFACE_CONTROL_COMPAT_H_
143