1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ANDROID_DVR_HARDWARE_COMPOSER_IMPL_VR_HWC_H
17 #define ANDROID_DVR_HARDWARE_COMPOSER_IMPL_VR_HWC_H
18 
19 #include <android-base/unique_fd.h>
20 #include <android/frameworks/vr/composer/1.0/IVrComposerClient.h>
21 #include <android/hardware/graphics/composer/2.1/IComposer.h>
22 #include <ComposerBase.h>
23 #include <ui/Fence.h>
24 #include <ui/GraphicBuffer.h>
25 #include <utils/StrongPointer.h>
26 
27 #include <mutex>
28 #include <unordered_map>
29 
30 using namespace android::frameworks::vr::composer::V1_0;
31 using namespace android::hardware::graphics::common::V1_0;
32 using namespace android::hardware::graphics::composer::V2_1;
33 
34 using android::hardware::hidl_handle;
35 using android::hardware::hidl_string;
36 using android::hardware::hidl_vec;
37 using android::hardware::Return;
38 using android::hardware::Void;
39 
40 namespace android {
41 
42 class Fence;
43 
44 namespace dvr {
45 
46 class VrComposerClient;
47 
48 using android::hardware::graphics::common::V1_0::PixelFormat;
49 using android::hardware::graphics::composer::V2_1::implementation::ComposerBase;
50 
51 class ComposerView {
52  public:
53   struct ComposerLayer {
54     using Recti = hardware::graphics::composer::V2_1::IComposerClient::Rect;
55     using Rectf = hardware::graphics::composer::V2_1::IComposerClient::FRect;
56     using BlendMode =
57         hardware::graphics::composer::V2_1::IComposerClient::BlendMode;
58 
59     Layer id;
60     sp<GraphicBuffer> buffer;
61     sp<Fence> fence;
62     Recti display_frame;
63     Rectf crop;
64     BlendMode blend_mode;
65     float alpha;
66     uint32_t type;
67     uint32_t app_id;
68     uint32_t z_order;
69     int32_t cursor_x;
70     int32_t cursor_y;
71     IComposerClient::Color color;
72     int32_t dataspace;
73     int32_t transform;
74     std::vector<hwc_rect_t> visible_regions;
75     std::vector<hwc_rect_t> damaged_regions;
76   };
77 
78   struct Frame {
79     Display display_id;
80     // This is set to true to notify the upper layer that the display is
81     // being removed, or left false in the case of a normal frame. The upper
82     // layer tracks display IDs and will handle new ones showing up.
83     bool removed = false;
84     int32_t display_width;
85     int32_t display_height;
86     Config active_config;
87     ColorMode color_mode;
88     IComposerClient::PowerMode power_mode;
89     IComposerClient::Vsync vsync_enabled;
90     float color_transform[16];
91     int32_t color_transform_hint;
92     std::vector<ComposerLayer> layers;
93   };
94 
95   class Observer {
96    public:
~Observer()97     virtual ~Observer() {}
98 
99     // Returns a list of layers that need to be shown together. Layers are
100     // returned in z-order, with the lowest layer first.
101     virtual base::unique_fd OnNewFrame(const Frame& frame) = 0;
102   };
103 
~ComposerView()104   virtual ~ComposerView() {}
105 
106   virtual void RegisterObserver(Observer* observer) = 0;
107   virtual void UnregisterObserver(Observer* observer) = 0;
108 };
109 
110 struct HwcLayer {
111   using Composition =
112       hardware::graphics::composer::V2_1::IComposerClient::Composition;
113 
HwcLayerHwcLayer114   HwcLayer(Layer new_id) {
115     info.id = new_id;
116   }
117 
118   Composition composition_type;
119   ComposerView::ComposerLayer info;
120   IVrComposerClient::BufferMetadata buffer_metadata;
121 };
122 
123 class HwcDisplay {
124  public:
125   HwcDisplay(int32_t width, int32_t height);
126   ~HwcDisplay();
127 
width()128   int32_t width() const { return width_; }
height()129   int32_t height() const { return height_; }
130 
131   HwcLayer* CreateLayer();
132   bool DestroyLayer(Layer id);
133   HwcLayer* GetLayer(Layer id);
134 
135   bool SetClientTarget(const native_handle_t* handle, base::unique_fd fence);
136   void SetClientTargetMetadata(
137       const IVrComposerClient::BufferMetadata& metadata);
138 
139   void GetChangedCompositionTypes(
140       std::vector<Layer>* layer_ids,
141       std::vector<IComposerClient::Composition>* composition);
142 
143   Error GetFrame(std::vector<ComposerView::ComposerLayer>* out_frame);
144 
145   std::vector<Layer> UpdateLastFrameAndGetLastFrameLayers();
146 
active_config()147   Config active_config() const { return active_config_; }
set_active_config(Config config)148   void set_active_config(Config config) { active_config_ = config; }
149 
color_mode()150   ColorMode color_mode() const { return color_mode_; }
set_color_mode(ColorMode mode)151   void set_color_mode(ColorMode mode) { color_mode_ = mode; }
152 
power_mode()153   IComposerClient::PowerMode power_mode() const { return power_mode_; }
set_power_mode(IComposerClient::PowerMode mode)154   void set_power_mode(IComposerClient::PowerMode mode) { power_mode_ = mode; }
155 
vsync_enabled()156   IComposerClient::Vsync vsync_enabled() const { return vsync_enabled_; }
set_vsync_enabled(IComposerClient::Vsync vsync)157   void set_vsync_enabled(IComposerClient::Vsync vsync) {
158     vsync_enabled_ = vsync;
159   }
160 
color_transform()161   const float* color_transform() const { return color_transform_; }
color_transform_hint()162   int32_t color_transform_hint() const { return color_transform_hint_; }
163   void SetColorTransform(const float* matrix, int32_t hint);
164 
165  private:
166   // The client target buffer and the associated fence.
167   sp<GraphicBuffer> buffer_;
168   IVrComposerClient::BufferMetadata buffer_metadata_;
169   sp<Fence> fence_;
170 
171   // List of currently active layers.
172   std::vector<HwcLayer> layers_;
173 
174   std::vector<Layer> last_frame_layers_ids_;
175 
176   // Layer ID generator.
177   uint64_t layer_ids_ = 1;
178 
179   int32_t width_;
180   int32_t height_;
181 
182   Config active_config_;
183   ColorMode color_mode_;
184   IComposerClient::PowerMode power_mode_;
185   IComposerClient::Vsync vsync_enabled_;
186   float color_transform_[16];
187   int32_t color_transform_hint_;
188 
189   HwcDisplay(const HwcDisplay&) = delete;
190   void operator=(const HwcDisplay&) = delete;
191 };
192 
193 class VrHwc : public IComposer, public ComposerBase, public ComposerView {
194  public:
195   VrHwc();
196   ~VrHwc() override;
197 
198   bool hasCapability(Capability capability) const;
199 
200   Error setLayerInfo(Display display, Layer layer, uint32_t type,
201                      uint32_t appId);
202   Error setClientTargetMetadata(
203       Display display, const IVrComposerClient::BufferMetadata& metadata);
204   Error setLayerBufferMetadata(
205       Display display, Layer layer,
206       const IVrComposerClient::BufferMetadata& metadata);
207 
208   // ComposerBase
209   void removeClient() override;
210   void enableCallback(bool enable) override;
211 
212   uint32_t getMaxVirtualDisplayCount() override;
213   Error createVirtualDisplay(uint32_t width, uint32_t height,
214       PixelFormat* format, Display* outDisplay) override;
215   Error destroyVirtualDisplay(Display display) override;
216 
217   Error createLayer(Display display, Layer* outLayer) override;
218   Error destroyLayer(Display display, Layer layer) override;
219 
220   Error getActiveConfig(Display display, Config* outConfig) override;
221   Error getClientTargetSupport(Display display,
222           uint32_t width, uint32_t height,
223           PixelFormat format, Dataspace dataspace) override;
224   Error getColorModes(Display display, hidl_vec<ColorMode>* outModes) override;
225   Error getDisplayAttribute(Display display, Config config,
226           IComposerClient::Attribute attribute, int32_t* outValue) override;
227   Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override;
228   Error getDisplayName(Display display, hidl_string* outName) override;
229   Error getDisplayType(Display display,
230           IComposerClient::DisplayType* outType) override;
231   Error getDozeSupport(Display display, bool* outSupport) override;
232   Error getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes,
233           float* outMaxLuminance, float* outMaxAverageLuminance,
234           float* outMinLuminance) override;
235 
236   Error setActiveConfig(Display display, Config config) override;
237   Error setColorMode(Display display, ColorMode mode) override;
238   Error setPowerMode(Display display, IComposerClient::PowerMode mode) override;
239   Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override;
240 
241   Error setColorTransform(Display display, const float* matrix,
242           int32_t hint) override;
243   Error setClientTarget(Display display, buffer_handle_t target,
244           int32_t acquireFence, int32_t dataspace,
245           const std::vector<hwc_rect_t>& damage) override;
246   Error setOutputBuffer(Display display, buffer_handle_t buffer,
247           int32_t releaseFence) override;
248   Error validateDisplay(Display display,
249           std::vector<Layer>* outChangedLayers,
250           std::vector<IComposerClient::Composition>* outCompositionTypes,
251           uint32_t* outDisplayRequestMask,
252           std::vector<Layer>* outRequestedLayers,
253           std::vector<uint32_t>* outRequestMasks) override;
254   Error acceptDisplayChanges(Display display) override;
255   Error presentDisplay(Display display, int32_t* outPresentFence,
256           std::vector<Layer>* outLayers,
257           std::vector<int32_t>* outReleaseFences) override;
258 
259   Error setLayerCursorPosition(Display display, Layer layer,
260           int32_t x, int32_t y) override;
261   Error setLayerBuffer(Display display, Layer layer,
262           buffer_handle_t buffer, int32_t acquireFence) override;
263   Error setLayerSurfaceDamage(Display display, Layer layer,
264           const std::vector<hwc_rect_t>& damage) override;
265   Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override;
266   Error setLayerColor(Display display, Layer layer,
267           IComposerClient::Color color) override;
268   Error setLayerCompositionType(Display display, Layer layer,
269           int32_t type) override;
270   Error setLayerDataspace(Display display, Layer layer,
271           int32_t dataspace) override;
272   Error setLayerDisplayFrame(Display display, Layer layer,
273           const hwc_rect_t& frame) override;
274   Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override;
275   Error setLayerSidebandStream(Display display, Layer layer,
276           buffer_handle_t stream) override;
277   Error setLayerSourceCrop(Display display, Layer layer,
278           const hwc_frect_t& crop) override;
279   Error setLayerTransform(Display display, Layer layer,
280           int32_t transform) override;
281   Error setLayerVisibleRegion(Display display, Layer layer,
282           const std::vector<hwc_rect_t>& visible) override;
283   Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
284 
285   // IComposer:
286   Return<void> getCapabilities(getCapabilities_cb hidl_cb) override;
287   Return<void> dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override;
288   Return<void> createClient(createClient_cb hidl_cb) override;
289 
290   // ComposerView:
291   void RegisterObserver(Observer* observer) override;
292   void UnregisterObserver(Observer* observer) override;
293 
294  private:
295   HwcDisplay* FindDisplay(Display display);
296 
297   wp<VrComposerClient> client_;
298   sp<IComposerCallback> callbacks_;
299 
300   // Guard access to internal state from binder threads.
301   std::mutex mutex_;
302 
303   std::unordered_map<Display, std::unique_ptr<HwcDisplay>> displays_;
304   Display display_count_ = 2;
305 
306   Observer* observer_ = nullptr;
307 
308   VrHwc(const VrHwc&) = delete;
309   void operator=(const VrHwc&) = delete;
310 };
311 
312 
313 ComposerView* GetComposerViewFromIComposer(
314     hardware::graphics::composer::V2_1::IComposer* composer);
315 
316 hardware::graphics::composer::V2_1::IComposer* HIDL_FETCH_IComposer(
317     const char* name);
318 
319 }  // namespace dvr
320 }  // namespace android
321 
322 #endif  // ANDROID_DVR_HARDWARE_COMPOSER_IMPL_VR_HWC_H
323