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/2.0/IVrComposerClient.h>
21 #include <android/hardware/graphics/composer/2.3/IComposer.h>
22 #include <composer-hal/2.3/ComposerHal.h>
23 #include <private/dvr/vsync_service.h>
24 #include <ui/Fence.h>
25 #include <ui/GraphicBuffer.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <mutex>
29 #include <unordered_map>
30 
31 using namespace android::frameworks::vr::composer::V2_0;
32 using namespace android::hardware::graphics::common::V1_0;
33 using namespace android::hardware::graphics::composer::V2_3;
34 
35 using android::hardware::hidl_bitfield;
36 using android::hardware::hidl_handle;
37 using android::hardware::hidl_string;
38 using android::hardware::hidl_vec;
39 using android::hardware::Return;
40 using android::hardware::Void;
41 using android::hardware::graphics::composer::V2_1::Config;
42 using android::hardware::graphics::composer::V2_1::Display;
43 using android::hardware::graphics::composer::V2_1::Error;
44 using android::hardware::graphics::composer::V2_1::Layer;
45 using android::hardware::graphics::composer::V2_3::IComposerClient;
46 
47 namespace android {
48 
49 class Fence;
50 
51 namespace dvr {
52 
53 class VrComposerClient;
54 
55 using android::hardware::graphics::composer::V2_3::hal::ComposerHal;
56 
57 namespace types = android::hardware::graphics::common;
58 
59 using types::V1_1::RenderIntent;
60 using types::V1_2::ColorMode;
61 using types::V1_2::Dataspace;
62 using types::V1_2::Hdr;
63 using types::V1_2::PixelFormat;
64 
65 class ComposerView {
66  public:
67   struct ComposerLayer {
68     using Recti = hardware::graphics::composer::V2_3::IComposerClient::Rect;
69     using Rectf = hardware::graphics::composer::V2_3::IComposerClient::FRect;
70     using BlendMode =
71         hardware::graphics::composer::V2_3::IComposerClient::BlendMode;
72 
73     Layer id;
74     sp<GraphicBuffer> buffer;
75     sp<Fence> fence;
76     Recti display_frame;
77     Rectf crop;
78     BlendMode blend_mode;
79     float alpha;
80     uint32_t type;
81     uint32_t app_id;
82     uint32_t z_order;
83     int32_t cursor_x;
84     int32_t cursor_y;
85     IComposerClient::Color color;
86     int32_t dataspace;
87     int32_t transform;
88     std::vector<hwc_rect_t> visible_regions;
89     std::vector<hwc_rect_t> damaged_regions;
90   };
91 
92   struct Frame {
93     Display display_id;
94     // This is set to true to notify the upper layer that the display is
95     // being removed, or left false in the case of a normal frame. The upper
96     // layer tracks display IDs and will handle new ones showing up.
97     bool removed = false;
98     int32_t display_width;
99     int32_t display_height;
100     Config active_config;
101     ColorMode color_mode;
102     IComposerClient::PowerMode power_mode;
103     IComposerClient::Vsync vsync_enabled;
104     float color_transform[16];
105     int32_t color_transform_hint;
106     std::vector<ComposerLayer> layers;
107   };
108 
109   class Observer {
110    public:
~Observer()111     virtual ~Observer() {}
112 
113     // Returns a list of layers that need to be shown together. Layers are
114     // returned in z-order, with the lowest layer first.
115     virtual base::unique_fd OnNewFrame(const Frame& frame) = 0;
116   };
117 
~ComposerView()118   virtual ~ComposerView() {}
119 
120   virtual void ForceDisplaysRefresh() = 0;
121   virtual void RegisterObserver(Observer* observer) = 0;
122   virtual void UnregisterObserver(Observer* observer) = 0;
123 };
124 
125 struct HwcLayer {
126   using Composition =
127       hardware::graphics::composer::V2_3::IComposerClient::Composition;
128 
HwcLayerHwcLayer129   explicit HwcLayer(Layer new_id) { info.id = new_id; }
130 
131   void dumpDebugInfo(std::string* result) const;
132 
133   Composition composition_type;
134   ComposerView::ComposerLayer info;
135   IVrComposerClient::BufferMetadata buffer_metadata;
136 };
137 
138 class HwcDisplay {
139  public:
140   HwcDisplay(int32_t width, int32_t height);
141   ~HwcDisplay();
142 
width()143   int32_t width() const { return width_; }
height()144   int32_t height() const { return height_; }
145 
146   HwcLayer* CreateLayer();
147   bool DestroyLayer(Layer id);
148   HwcLayer* GetLayer(Layer id);
149 
150   bool SetClientTarget(const native_handle_t* handle, base::unique_fd fence);
151   void SetClientTargetMetadata(
152       const IVrComposerClient::BufferMetadata& metadata);
153 
154   void GetChangedCompositionTypes(
155       std::vector<Layer>* layer_ids,
156       std::vector<IComposerClient::Composition>* composition);
157 
158   Error GetFrame(std::vector<ComposerView::ComposerLayer>* out_frame);
159 
160   std::vector<Layer> UpdateLastFrameAndGetLastFrameLayers();
161 
active_config()162   Config active_config() const { return active_config_; }
set_active_config(Config config)163   void set_active_config(Config config) { active_config_ = config; }
164 
color_mode()165   ColorMode color_mode() const { return color_mode_; }
set_color_mode(ColorMode mode)166   void set_color_mode(ColorMode mode) { color_mode_ = mode; }
167 
power_mode()168   IComposerClient::PowerMode power_mode() const { return power_mode_; }
set_power_mode(IComposerClient::PowerMode mode)169   void set_power_mode(IComposerClient::PowerMode mode) { power_mode_ = mode; }
170 
vsync_enabled()171   bool vsync_enabled() const { return vsync_enabled_; }
set_vsync_enabled(bool vsync)172   void set_vsync_enabled(bool vsync) {vsync_enabled_ = vsync;}
173 
color_transform()174   const float* color_transform() const { return color_transform_; }
color_transform_hint()175   int32_t color_transform_hint() const { return color_transform_hint_; }
176   void SetColorTransform(const float* matrix, int32_t hint);
177 
178   void dumpDebugInfo(std::string* result) const;
179 
180  private:
181   // The client target buffer and the associated fence.
182   sp<GraphicBuffer> buffer_;
183   IVrComposerClient::BufferMetadata buffer_metadata_;
184   sp<Fence> fence_;
185 
186   // List of currently active layers.
187   std::vector<HwcLayer> layers_;
188 
189   std::vector<Layer> last_frame_layers_ids_;
190 
191   // Layer ID generator.
192   uint64_t layer_ids_ = 1;
193 
194   int32_t width_;
195   int32_t height_;
196 
197   Config active_config_;
198   ColorMode color_mode_;
199   IComposerClient::PowerMode power_mode_;
200   bool vsync_enabled_ = false;
201   float color_transform_[16];
202   int32_t color_transform_hint_;
203 
204   HwcDisplay(const HwcDisplay&) = delete;
205   void operator=(const HwcDisplay&) = delete;
206 };
207 
208 class VrHwc : public IComposer, public ComposerHal, public ComposerView {
209  public:
210   VrHwc();
211   ~VrHwc() override;
212 
213   Error setLayerInfo(Display display, Layer layer, uint32_t type,
214                      uint32_t appId);
215   Error setClientTargetMetadata(
216       Display display, const IVrComposerClient::BufferMetadata& metadata);
217   Error setLayerBufferMetadata(
218       Display display, Layer layer,
219       const IVrComposerClient::BufferMetadata& metadata);
220 
221   // composer::V2_1::ComposerHal
222   bool hasCapability(hwc2_capability_t capability) override;
223 
dumpDebugInfo()224   std::string dumpDebugInfo() override { return {}; }
225 
226   void registerEventCallback(ComposerHal::EventCallback* callback) override;
227   void unregisterEventCallback() override;
228 
229   uint32_t getMaxVirtualDisplayCount() override;
230   Error destroyVirtualDisplay(Display display) override;
231 
232   Error createLayer(Display display, Layer* outLayer) override;
233   Error destroyLayer(Display display, Layer layer) override;
234 
235   Error getActiveConfig(Display display, Config* outConfig) override;
236   Error getDisplayAttribute(Display display, Config config,
237                             IComposerClient::Attribute attribute,
238                             int32_t* outValue) override;
239   Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override;
240   Error getDisplayName(Display display, hidl_string* outName) override;
241   Error getDisplayType(Display display,
242                        IComposerClient::DisplayType* outType) override;
243   Error getDozeSupport(Display display, bool* outSupport) override;
244 
245   Error setActiveConfig(Display display, Config config) override;
246   Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override;
247 
248   Error setColorTransform(Display display, const float* matrix,
249                           int32_t hint) override;
250   Error setClientTarget(Display display, buffer_handle_t target,
251                         int32_t acquireFence, int32_t dataspace,
252                         const std::vector<hwc_rect_t>& damage) override;
253   Error setOutputBuffer(Display display, buffer_handle_t buffer,
254                         int32_t releaseFence) override;
255   Error validateDisplay(
256       Display display, std::vector<Layer>* outChangedLayers,
257       std::vector<IComposerClient::Composition>* outCompositionTypes,
258       uint32_t* outDisplayRequestMask, std::vector<Layer>* outRequestedLayers,
259       std::vector<uint32_t>* outRequestMasks) override;
260   Error acceptDisplayChanges(Display display) override;
261   Error presentDisplay(Display display, int32_t* outPresentFence,
262                        std::vector<Layer>* outLayers,
263                        std::vector<int32_t>* outReleaseFences) override;
264 
265   Error setLayerCursorPosition(Display display, Layer layer, int32_t x,
266                                int32_t y) override;
267   Error setLayerBuffer(Display display, Layer layer, buffer_handle_t buffer,
268                        int32_t acquireFence) override;
269   Error setLayerSurfaceDamage(Display display, Layer layer,
270                               const std::vector<hwc_rect_t>& damage) override;
271   Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override;
272   Error setLayerColor(Display display, Layer layer,
273                       IComposerClient::Color color) override;
274   Error setLayerCompositionType(Display display, Layer layer,
275                                 int32_t type) override;
276   Error setLayerDataspace(Display display, Layer layer,
277                           int32_t dataspace) override;
278   Error setLayerDisplayFrame(Display display, Layer layer,
279                              const hwc_rect_t& frame) override;
280   Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override;
281   Error setLayerSidebandStream(Display display, Layer layer,
282                                buffer_handle_t stream) override;
283   Error setLayerSourceCrop(Display display, Layer layer,
284                            const hwc_frect_t& crop) override;
285   Error setLayerTransform(Display display, Layer layer,
286                           int32_t transform) override;
287   Error setLayerVisibleRegion(Display display, Layer layer,
288                               const std::vector<hwc_rect_t>& visible) override;
289   Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
290 
291   // composer::V2_2::ComposerHal
292   Error setReadbackBuffer(Display display, const native_handle_t* bufferHandle,
293                           android::base::unique_fd fenceFd) override;
294   Error getReadbackBufferFence(Display display,
295                                android::base::unique_fd* outFenceFd) override;
296   Error createVirtualDisplay_2_2(uint32_t width, uint32_t height,
297                                  types::V1_1::PixelFormat* format,
298                                  Display* outDisplay) override;
299   Error setPowerMode_2_2(Display display,
300                          IComposerClient::PowerMode mode) override;
301   Error setLayerFloatColor(Display display, Layer layer,
302                            IComposerClient::FloatColor color) override;
303   Error getRenderIntents(Display display, types::V1_1::ColorMode mode,
304                          std::vector<RenderIntent>* outIntents) override;
305   std::array<float, 16> getDataspaceSaturationMatrix(
306       types::V1_1::Dataspace dataspace) override;
307 
308   // composer::V2_3::ComposerHal
309   Error getHdrCapabilities_2_3(Display display, hidl_vec<Hdr>* outTypes,
310                                float* outMaxLuminance,
311                                float* outMaxAverageLuminance,
312                                float* outMinLuminance) override;
313   Error setLayerPerFrameMetadata_2_3(
314       Display display, Layer layer,
315       const std::vector<IComposerClient::PerFrameMetadata>& metadata) override;
316   Error getPerFrameMetadataKeys_2_3(
317       Display display,
318       std::vector<IComposerClient::PerFrameMetadataKey>* outKeys) override;
319   Error setColorMode_2_3(Display display, ColorMode mode,
320                          RenderIntent intent) override;
321   Error getRenderIntents_2_3(Display display, ColorMode mode,
322                              std::vector<RenderIntent>* outIntents) override;
323   Error getColorModes_2_3(Display display,
324                           hidl_vec<ColorMode>* outModes) override;
325   Error getClientTargetSupport_2_3(Display display, uint32_t width,
326                                    uint32_t height, PixelFormat format,
327                                    Dataspace dataspace) override;
328   Error getReadbackBufferAttributes_2_3(Display display, PixelFormat* outFormat,
329                                         Dataspace* outDataspace) override;
330   Error getDisplayIdentificationData(Display display, uint8_t* outPort,
331                                      std::vector<uint8_t>* outData) override;
332   Error setLayerColorTransform(Display display, Layer layer,
333                                const float* matrix) override;
334   Error getDisplayedContentSamplingAttributes(
335       Display display, PixelFormat& format, Dataspace& dataspace,
336       hidl_bitfield<IComposerClient::FormatColorComponent>& componentMask)
337       override;
338   Error setDisplayedContentSamplingEnabled(
339       Display display, IComposerClient::DisplayedContentSampling enable,
340       hidl_bitfield<IComposerClient::FormatColorComponent> componentMask,
341       uint64_t maxFrames) override;
342   Error getDisplayedContentSample(
343       Display display, uint64_t maxFrames, uint64_t timestamp,
344       uint64_t& frameCount, hidl_vec<uint64_t>& sampleComponent0,
345       hidl_vec<uint64_t>& sampleComponent1,
346       hidl_vec<uint64_t>& sampleComponent2,
347       hidl_vec<uint64_t>& sampleComponent3) override;
348   Error getDisplayCapabilities(Display display,
349                                std::vector<IComposerClient::DisplayCapability>*
350                                    outCapabilities) override;
351   Error setLayerPerFrameMetadataBlobs(
352       Display display, Layer layer,
353       std::vector<IComposerClient::PerFrameMetadataBlob>& blobs) override;
354   Error getDisplayBrightnessSupport(Display display, bool* outSupport) override;
355   Error setDisplayBrightness(Display display, float brightness) override;
356 
357   // IComposer:
358   Return<void> getCapabilities(getCapabilities_cb hidl_cb) override;
359   Return<void> dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override;
360   Return<void> createClient(createClient_cb hidl_cb) override;
361   Return<void> createClient_2_3(
362       IComposer::createClient_2_3_cb hidl_cb) override;
363 
364   // ComposerView:
365   void ForceDisplaysRefresh() override;
366   void RegisterObserver(Observer* observer) override;
367   void UnregisterObserver(Observer* observer) override;
368 
369   Return<void> debug(const hidl_handle& fd,
370                      const hidl_vec<hidl_string>& args) override;
371 
372  private:
373   class VsyncCallback : public BnVsyncCallback {
374    public:
375     status_t onVsync(int64_t vsync_timestamp) override;
376     void SetEventCallback(EventCallback* callback);
377    private:
378     std::mutex mutex_;
379     EventCallback* callback_;
380   };
381 
382   HwcDisplay* FindDisplay(Display display);
383 
384   // Re-evaluate whether or not we should start making onVsync() callbacks to
385   // the client. We need enableCallback(true) to have been called, and
386   // setVsyncEnabled() to have been called for the primary display. The caller
387   // must have mutex_ locked already.
388   void UpdateVsyncCallbackEnabledLocked();
389 
390   wp<VrComposerClient> client_;
391 
392   // Guard access to internal state from binder threads.
393   std::mutex mutex_;
394 
395   std::unordered_map<Display, std::unique_ptr<HwcDisplay>> displays_;
396   Display display_count_ = 2;
397 
398   EventCallback* event_callback_ = nullptr;
399   Observer* observer_ = nullptr;
400 
401   sp<VsyncCallback> vsync_callback_;
402 
403   VrHwc(const VrHwc&) = delete;
404   void operator=(const VrHwc&) = delete;
405 };
406 
407 }  // namespace dvr
408 }  // namespace android
409 
410 #endif  // ANDROID_DVR_HARDWARE_COMPOSER_IMPL_VR_HWC_H
411