• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <composer-hal/2.1/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::V1_0;
32 using namespace android::hardware::graphics::common::V1_0;
33 using namespace android::hardware::graphics::composer::V2_1;
34 
35 using android::hardware::hidl_handle;
36 using android::hardware::hidl_string;
37 using android::hardware::hidl_vec;
38 using android::hardware::Return;
39 using android::hardware::Void;
40 
41 namespace android {
42 
43 class Fence;
44 
45 namespace dvr {
46 
47 class VrComposerClient;
48 
49 using android::hardware::graphics::common::V1_0::PixelFormat;
50 using android::hardware::graphics::composer::V2_1::hal::ComposerHal;
51 
52 class ComposerView {
53  public:
54   struct ComposerLayer {
55     using Recti = hardware::graphics::composer::V2_1::IComposerClient::Rect;
56     using Rectf = hardware::graphics::composer::V2_1::IComposerClient::FRect;
57     using BlendMode =
58         hardware::graphics::composer::V2_1::IComposerClient::BlendMode;
59 
60     Layer id;
61     sp<GraphicBuffer> buffer;
62     sp<Fence> fence;
63     Recti display_frame;
64     Rectf crop;
65     BlendMode blend_mode;
66     float alpha;
67     uint32_t type;
68     uint32_t app_id;
69     uint32_t z_order;
70     int32_t cursor_x;
71     int32_t cursor_y;
72     IComposerClient::Color color;
73     int32_t dataspace;
74     int32_t transform;
75     std::vector<hwc_rect_t> visible_regions;
76     std::vector<hwc_rect_t> damaged_regions;
77   };
78 
79   struct Frame {
80     Display display_id;
81     // This is set to true to notify the upper layer that the display is
82     // being removed, or left false in the case of a normal frame. The upper
83     // layer tracks display IDs and will handle new ones showing up.
84     bool removed = false;
85     int32_t display_width;
86     int32_t display_height;
87     Config active_config;
88     ColorMode color_mode;
89     IComposerClient::PowerMode power_mode;
90     IComposerClient::Vsync vsync_enabled;
91     float color_transform[16];
92     int32_t color_transform_hint;
93     std::vector<ComposerLayer> layers;
94   };
95 
96   class Observer {
97    public:
~Observer()98     virtual ~Observer() {}
99 
100     // Returns a list of layers that need to be shown together. Layers are
101     // returned in z-order, with the lowest layer first.
102     virtual base::unique_fd OnNewFrame(const Frame& frame) = 0;
103   };
104 
~ComposerView()105   virtual ~ComposerView() {}
106 
107   virtual void ForceDisplaysRefresh() = 0;
108   virtual void RegisterObserver(Observer* observer) = 0;
109   virtual void UnregisterObserver(Observer* observer) = 0;
110 };
111 
112 struct HwcLayer {
113   using Composition =
114       hardware::graphics::composer::V2_1::IComposerClient::Composition;
115 
HwcLayerHwcLayer116   explicit HwcLayer(Layer new_id) { info.id = new_id; }
117 
118   void dumpDebugInfo(std::string* result) const;
119 
120   Composition composition_type;
121   ComposerView::ComposerLayer info;
122   IVrComposerClient::BufferMetadata buffer_metadata;
123 };
124 
125 class HwcDisplay {
126  public:
127   HwcDisplay(int32_t width, int32_t height);
128   ~HwcDisplay();
129 
width()130   int32_t width() const { return width_; }
height()131   int32_t height() const { return height_; }
132 
133   HwcLayer* CreateLayer();
134   bool DestroyLayer(Layer id);
135   HwcLayer* GetLayer(Layer id);
136 
137   bool SetClientTarget(const native_handle_t* handle, base::unique_fd fence);
138   void SetClientTargetMetadata(
139       const IVrComposerClient::BufferMetadata& metadata);
140 
141   void GetChangedCompositionTypes(
142       std::vector<Layer>* layer_ids,
143       std::vector<IComposerClient::Composition>* composition);
144 
145   Error GetFrame(std::vector<ComposerView::ComposerLayer>* out_frame);
146 
147   std::vector<Layer> UpdateLastFrameAndGetLastFrameLayers();
148 
active_config()149   Config active_config() const { return active_config_; }
set_active_config(Config config)150   void set_active_config(Config config) { active_config_ = config; }
151 
color_mode()152   ColorMode color_mode() const { return color_mode_; }
set_color_mode(ColorMode mode)153   void set_color_mode(ColorMode mode) { color_mode_ = mode; }
154 
power_mode()155   IComposerClient::PowerMode power_mode() const { return power_mode_; }
set_power_mode(IComposerClient::PowerMode mode)156   void set_power_mode(IComposerClient::PowerMode mode) { power_mode_ = mode; }
157 
vsync_enabled()158   bool vsync_enabled() const { return vsync_enabled_; }
set_vsync_enabled(bool vsync)159   void set_vsync_enabled(bool vsync) {vsync_enabled_ = vsync;}
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   void dumpDebugInfo(std::string* result) const;
166 
167  private:
168   // The client target buffer and the associated fence.
169   sp<GraphicBuffer> buffer_;
170   IVrComposerClient::BufferMetadata buffer_metadata_;
171   sp<Fence> fence_;
172 
173   // List of currently active layers.
174   std::vector<HwcLayer> layers_;
175 
176   std::vector<Layer> last_frame_layers_ids_;
177 
178   // Layer ID generator.
179   uint64_t layer_ids_ = 1;
180 
181   int32_t width_;
182   int32_t height_;
183 
184   Config active_config_;
185   ColorMode color_mode_;
186   IComposerClient::PowerMode power_mode_;
187   bool vsync_enabled_ = false;
188   float color_transform_[16];
189   int32_t color_transform_hint_;
190 
191   HwcDisplay(const HwcDisplay&) = delete;
192   void operator=(const HwcDisplay&) = delete;
193 };
194 
195 class VrHwc : public IComposer, public ComposerHal, public ComposerView {
196  public:
197   VrHwc();
198   ~VrHwc() override;
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   // ComposerHal
209   bool hasCapability(hwc2_capability_t capability) override;
210 
dumpDebugInfo()211   std::string dumpDebugInfo() override { return {}; }
212   void registerEventCallback(EventCallback* callback) override;
213   void unregisterEventCallback() override;
214 
215   uint32_t getMaxVirtualDisplayCount() override;
216   Error createVirtualDisplay(uint32_t width, uint32_t height,
217       PixelFormat* format, Display* outDisplay) override;
218   Error destroyVirtualDisplay(Display display) override;
219 
220   Error createLayer(Display display, Layer* outLayer) override;
221   Error destroyLayer(Display display, Layer layer) override;
222 
223   Error getActiveConfig(Display display, Config* outConfig) override;
224   Error getClientTargetSupport(Display display,
225           uint32_t width, uint32_t height,
226           PixelFormat format, Dataspace dataspace) override;
227   Error getColorModes(Display display, hidl_vec<ColorMode>* outModes) override;
228   Error getDisplayAttribute(Display display, Config config,
229           IComposerClient::Attribute attribute, int32_t* outValue) override;
230   Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override;
231   Error getDisplayName(Display display, hidl_string* outName) override;
232   Error getDisplayType(Display display,
233           IComposerClient::DisplayType* outType) override;
234   Error getDozeSupport(Display display, bool* outSupport) override;
235   Error getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes,
236           float* outMaxLuminance, float* outMaxAverageLuminance,
237           float* outMinLuminance) override;
238 
239   Error setActiveConfig(Display display, Config config) override;
240   Error setColorMode(Display display, ColorMode mode) override;
241   Error setPowerMode(Display display, IComposerClient::PowerMode mode) override;
242   Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override;
243 
244   Error setColorTransform(Display display, const float* matrix,
245           int32_t hint) override;
246   Error setClientTarget(Display display, buffer_handle_t target,
247           int32_t acquireFence, int32_t dataspace,
248           const std::vector<hwc_rect_t>& damage) override;
249   Error setOutputBuffer(Display display, buffer_handle_t buffer,
250           int32_t releaseFence) override;
251   Error validateDisplay(Display display,
252           std::vector<Layer>* outChangedLayers,
253           std::vector<IComposerClient::Composition>* outCompositionTypes,
254           uint32_t* outDisplayRequestMask,
255           std::vector<Layer>* outRequestedLayers,
256           std::vector<uint32_t>* outRequestMasks) override;
257   Error acceptDisplayChanges(Display display) override;
258   Error presentDisplay(Display display, int32_t* outPresentFence,
259           std::vector<Layer>* outLayers,
260           std::vector<int32_t>* outReleaseFences) override;
261 
262   Error setLayerCursorPosition(Display display, Layer layer,
263           int32_t x, int32_t y) override;
264   Error setLayerBuffer(Display display, Layer layer,
265           buffer_handle_t buffer, int32_t acquireFence) override;
266   Error setLayerSurfaceDamage(Display display, Layer layer,
267           const std::vector<hwc_rect_t>& damage) override;
268   Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override;
269   Error setLayerColor(Display display, Layer layer,
270           IComposerClient::Color color) override;
271   Error setLayerCompositionType(Display display, Layer layer,
272           int32_t type) override;
273   Error setLayerDataspace(Display display, Layer layer,
274           int32_t dataspace) override;
275   Error setLayerDisplayFrame(Display display, Layer layer,
276           const hwc_rect_t& frame) override;
277   Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override;
278   Error setLayerSidebandStream(Display display, Layer layer,
279           buffer_handle_t stream) override;
280   Error setLayerSourceCrop(Display display, Layer layer,
281           const hwc_frect_t& crop) override;
282   Error setLayerTransform(Display display, Layer layer,
283           int32_t transform) override;
284   Error setLayerVisibleRegion(Display display, Layer layer,
285           const std::vector<hwc_rect_t>& visible) override;
286   Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
287 
288   // IComposer:
289   Return<void> getCapabilities(getCapabilities_cb hidl_cb) override;
290   Return<void> dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override;
291   Return<void> createClient(createClient_cb hidl_cb) override;
292 
293   // ComposerView:
294   void ForceDisplaysRefresh() override;
295   void RegisterObserver(Observer* observer) override;
296   void UnregisterObserver(Observer* observer) override;
297 
298  private:
299   class VsyncCallback : public BnVsyncCallback {
300    public:
301     status_t onVsync(int64_t vsync_timestamp) override;
302     void SetEventCallback(EventCallback* callback);
303    private:
304     std::mutex mutex_;
305     EventCallback* callback_;
306   };
307 
308   HwcDisplay* FindDisplay(Display display);
309 
310   // Re-evaluate whether or not we should start making onVsync() callbacks to
311   // the client. We need enableCallback(true) to have been called, and
312   // setVsyncEnabled() to have been called for the primary display. The caller
313   // must have mutex_ locked already.
314   void UpdateVsyncCallbackEnabledLocked();
315 
316   wp<VrComposerClient> client_;
317 
318   // Guard access to internal state from binder threads.
319   std::mutex mutex_;
320 
321   std::unordered_map<Display, std::unique_ptr<HwcDisplay>> displays_;
322   Display display_count_ = 2;
323 
324   EventCallback* event_callback_ = nullptr;
325   Observer* observer_ = nullptr;
326 
327   sp<VsyncCallback> vsync_callback_;
328 
329   VrHwc(const VrHwc&) = delete;
330   void operator=(const VrHwc&) = delete;
331 };
332 
333 }  // namespace dvr
334 }  // namespace android
335 
336 #endif  // ANDROID_DVR_HARDWARE_COMPOSER_IMPL_VR_HWC_H
337