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_GL_ANDROID_ANDROID_SURFACE_CONTROL_COMPAT_H_ 6 #define UI_GL_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/overlay_transform.h" 18 #include "ui/gl/gl_export.h" 19 20 extern "C" { 21 typedef struct ASurfaceControl ASurfaceControl; 22 typedef struct ASurfaceTransaction ASurfaceTransaction; 23 } 24 25 namespace gfx { 26 class ColorSpace; 27 } // namespace gfx 28 29 namespace gl { 30 31 class GL_EXPORT SurfaceControl { 32 public: 33 // Check if the platform is capable of supporting the low-level SurfaceControl 34 // API. See also gpu/config/gpu_util's GetAndroidSurfaceControlFeatureStatus 35 // which checks other prerequisites such as Gpufence support before declaring 36 // support for the high-level SurfaceControl feature in Chrome. 37 static bool IsSupported(); 38 39 // Returns true if overlays with |color_space| are supported by the platform. 40 static bool SupportsColorSpace(const gfx::ColorSpace& color_space); 41 42 // Returns the usage flags required for using an AHardwareBuffer with the 43 // SurfaceControl API, if it is supported. 44 static uint64_t RequiredUsage(); 45 46 // Enables usage bits requires for getting UBWC on Qualcomm devices. Must be 47 // called early at process startup, before any buffer allocations are made. 48 static void EnableQualcommUBWC(); 49 50 class GL_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 GL_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 GL_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 GL_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 121 // Sets the callback which will be dispatched when the transaction is acked 122 // by the framework. 123 // |task_runner| provides an optional task runner on which the callback 124 // should be run. 125 using OnCompleteCb = base::OnceCallback<void(TransactionStats stats)>; 126 void SetOnCompleteCb( 127 OnCompleteCb cb, 128 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 129 void SetParent(const Surface& surface, const Surface* new_parent); 130 131 void Apply(); 132 133 private: 134 int id_; 135 ASurfaceTransaction* transaction_; 136 137 DISALLOW_COPY_AND_ASSIGN(Transaction); 138 }; 139 }; 140 } // namespace gl 141 142 #endif // UI_GL_ANDROID_ANDROID_SURFACE_CONTROL_COMPAT_H_ 143