Daniel Nicoara | 51a0a56 | 2016-12-08 16:48:20 -0500 | [diff] [blame] | 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 VR_WINDOW_MANAGER_COMPOSER_IMPL_VR_HWC_H_ |
| 17 | #define VR_WINDOW_MANAGER_COMPOSER_IMPL_VR_HWC_H_ |
| 18 | |
| 19 | #include <android/hardware/graphics/composer/2.1/IComposer.h> |
| 20 | #include <ComposerBase.h> |
| 21 | #include <ui/GraphicBufferMapper.h> |
| 22 | #include <utils/StrongPointer.h> |
| 23 | |
| 24 | #include <mutex> |
| 25 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 26 | #include "sync_timeline.h" |
Daniel Nicoara | 51a0a56 | 2016-12-08 16:48:20 -0500 | [diff] [blame] | 27 | |
| 28 | using namespace android::hardware::graphics::common::V1_0; |
| 29 | using namespace android::hardware::graphics::composer::V2_1; |
| 30 | |
| 31 | using android::hardware::hidl_handle; |
| 32 | using android::hardware::hidl_string; |
| 33 | using android::hardware::hidl_vec; |
| 34 | using android::hardware::Return; |
| 35 | using android::hardware::Void; |
| 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | class Fence; |
| 40 | class GraphicBuffer; |
| 41 | |
| 42 | namespace dvr { |
| 43 | |
| 44 | class VrComposerClient; |
| 45 | |
| 46 | using android::hardware::graphics::common::V1_0::PixelFormat; |
| 47 | using android::hardware::graphics::composer::V2_1::implementation::ComposerBase; |
| 48 | |
| 49 | class ComposerView { |
| 50 | public: |
| 51 | struct ComposerLayer { |
| 52 | using Recti = hardware::graphics::composer::V2_1::IComposerClient::Rect; |
| 53 | using Rectf = hardware::graphics::composer::V2_1::IComposerClient::FRect; |
| 54 | using BlendMode = |
| 55 | hardware::graphics::composer::V2_1::IComposerClient::BlendMode; |
| 56 | |
| 57 | // TODO(dnicoara): Add all layer properties. For now just the basics to get |
| 58 | // it going. |
| 59 | sp<GraphicBuffer> buffer; |
| 60 | sp<Fence> fence; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 61 | Recti display_frame; |
Daniel Nicoara | 51a0a56 | 2016-12-08 16:48:20 -0500 | [diff] [blame] | 62 | Rectf crop; |
| 63 | BlendMode blend_mode; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 64 | float alpha; |
| 65 | uint32_t type; |
| 66 | uint32_t app_id; |
Daniel Nicoara | 51a0a56 | 2016-12-08 16:48:20 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | using Frame = std::vector<ComposerLayer>; |
| 70 | |
| 71 | class Observer { |
| 72 | public: |
| 73 | virtual ~Observer() {} |
| 74 | |
| 75 | // Returns a list of layers that need to be shown together. Layers are |
| 76 | // returned in z-order, with the lowest layer first. |
| 77 | virtual void OnNewFrame(const Frame& frame) = 0; |
| 78 | }; |
| 79 | |
| 80 | virtual ~ComposerView() {} |
| 81 | |
| 82 | virtual void RegisterObserver(Observer* observer) = 0; |
| 83 | virtual void UnregisterObserver(Observer* observer) = 0; |
| 84 | |
| 85 | // Called to release the oldest frame received by the observer. |
| 86 | virtual void ReleaseFrame() = 0; |
| 87 | }; |
| 88 | |
| 89 | struct HwcLayer { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 90 | using Composition = |
| 91 | hardware::graphics::composer::V2_1::IComposerClient::Composition; |
| 92 | |
Daniel Nicoara | 51a0a56 | 2016-12-08 16:48:20 -0500 | [diff] [blame] | 93 | HwcLayer(Layer new_id) : id(new_id) {} |
| 94 | |
| 95 | Layer id; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 96 | Composition composition_type; |
| 97 | uint32_t z_order; |
| 98 | ComposerView::ComposerLayer info; |
Daniel Nicoara | 51a0a56 | 2016-12-08 16:48:20 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | class HwcDisplay { |
| 102 | public: |
| 103 | HwcDisplay(); |
| 104 | ~HwcDisplay(); |
| 105 | |
| 106 | bool Initialize(); |
| 107 | |
| 108 | HwcLayer* CreateLayer(); |
| 109 | bool DestroyLayer(Layer id); |
| 110 | HwcLayer* GetLayer(Layer id); |
| 111 | |
| 112 | bool SetClientTarget(const native_handle_t* handle, base::unique_fd fence); |
| 113 | |
| 114 | void GetChangedCompositionTypes( |
| 115 | std::vector<Layer>* layer_ids, |
| 116 | std::vector<IComposerClient::Composition>* composition); |
| 117 | |
| 118 | std::vector<ComposerView::ComposerLayer> GetFrame(); |
| 119 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 120 | void GetReleaseFences(int* present_fence, std::vector<Layer>* layer_ids, |
| 121 | std::vector<int>* fences); |
Daniel Nicoara | 51a0a56 | 2016-12-08 16:48:20 -0500 | [diff] [blame] | 122 | |
| 123 | void ReleaseFrame(); |
| 124 | |
| 125 | private: |
| 126 | // The client target buffer and the associated fence. |
| 127 | // TODO(dnicoara): Replace this with a list of ComposerView::ComposerLayer. |
| 128 | sp<GraphicBuffer> buffer_; |
| 129 | sp<Fence> fence_; |
| 130 | |
| 131 | // List of currently active layers. |
| 132 | std::vector<HwcLayer> layers_; |
| 133 | |
| 134 | // Layer ID generator. |
| 135 | uint64_t layer_ids_ = 1; |
| 136 | |
| 137 | // Creates software sync fences used to signal releasing frames. |
| 138 | SyncTimeline hwc_timeline_; |
| 139 | |
| 140 | // Keeps track of the current fence time. Used in conjunction with |
| 141 | // |hwc_timeline_| to properly signal frame release times. Allows the observer |
| 142 | // to receive multiple presentation frames without calling ReleaseFrame() in |
| 143 | // between each presentation. When the observer is ready to release a frame |
| 144 | // only the oldest presentation frame is affected by the release. |
| 145 | int fence_time_ = 0; |
| 146 | |
| 147 | HwcDisplay(const HwcDisplay&) = delete; |
| 148 | void operator=(const HwcDisplay&) = delete; |
| 149 | }; |
| 150 | |
| 151 | class VrHwc : public IComposer, public ComposerBase, public ComposerView { |
| 152 | public: |
| 153 | VrHwc(); |
| 154 | ~VrHwc() override; |
| 155 | |
| 156 | bool Initialize(); |
| 157 | |
| 158 | bool hasCapability(Capability capability) const; |
| 159 | |
| 160 | Error setLayerInfo(Display display, Layer layer, uint32_t type, |
| 161 | uint32_t appId); |
| 162 | |
| 163 | // ComposerBase |
| 164 | void removeClient() override; |
| 165 | void enableCallback(bool enable) override; |
| 166 | |
| 167 | uint32_t getMaxVirtualDisplayCount() override; |
| 168 | Error createVirtualDisplay(uint32_t width, uint32_t height, |
| 169 | PixelFormat* format, Display* outDisplay) override; |
| 170 | Error destroyVirtualDisplay(Display display) override; |
| 171 | |
| 172 | Error createLayer(Display display, Layer* outLayer) override; |
| 173 | Error destroyLayer(Display display, Layer layer) override; |
| 174 | |
| 175 | Error getActiveConfig(Display display, Config* outConfig) override; |
| 176 | Error getClientTargetSupport(Display display, |
| 177 | uint32_t width, uint32_t height, |
| 178 | PixelFormat format, Dataspace dataspace) override; |
| 179 | Error getColorModes(Display display, hidl_vec<ColorMode>* outModes) override; |
| 180 | Error getDisplayAttribute(Display display, Config config, |
| 181 | IComposerClient::Attribute attribute, int32_t* outValue) override; |
| 182 | Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override; |
| 183 | Error getDisplayName(Display display, hidl_string* outName) override; |
| 184 | Error getDisplayType(Display display, |
| 185 | IComposerClient::DisplayType* outType) override; |
| 186 | Error getDozeSupport(Display display, bool* outSupport) override; |
| 187 | Error getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes, |
| 188 | float* outMaxLuminance, float* outMaxAverageLuminance, |
| 189 | float* outMinLuminance) override; |
| 190 | |
| 191 | Error setActiveConfig(Display display, Config config) override; |
| 192 | Error setColorMode(Display display, ColorMode mode) override; |
| 193 | Error setPowerMode(Display display, IComposerClient::PowerMode mode) override; |
| 194 | Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override; |
| 195 | |
| 196 | Error setColorTransform(Display display, const float* matrix, |
| 197 | int32_t hint) override; |
| 198 | Error setClientTarget(Display display, buffer_handle_t target, |
| 199 | int32_t acquireFence, int32_t dataspace, |
| 200 | const std::vector<hwc_rect_t>& damage) override; |
| 201 | Error setOutputBuffer(Display display, buffer_handle_t buffer, |
| 202 | int32_t releaseFence) override; |
| 203 | Error validateDisplay(Display display, |
| 204 | std::vector<Layer>* outChangedLayers, |
| 205 | std::vector<IComposerClient::Composition>* outCompositionTypes, |
| 206 | uint32_t* outDisplayRequestMask, |
| 207 | std::vector<Layer>* outRequestedLayers, |
| 208 | std::vector<uint32_t>* outRequestMasks) override; |
| 209 | Error acceptDisplayChanges(Display display) override; |
| 210 | Error presentDisplay(Display display, int32_t* outPresentFence, |
| 211 | std::vector<Layer>* outLayers, |
| 212 | std::vector<int32_t>* outReleaseFences) override; |
| 213 | |
| 214 | Error setLayerCursorPosition(Display display, Layer layer, |
| 215 | int32_t x, int32_t y) override; |
| 216 | Error setLayerBuffer(Display display, Layer layer, |
| 217 | buffer_handle_t buffer, int32_t acquireFence) override; |
| 218 | Error setLayerSurfaceDamage(Display display, Layer layer, |
| 219 | const std::vector<hwc_rect_t>& damage) override; |
| 220 | Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override; |
| 221 | Error setLayerColor(Display display, Layer layer, |
| 222 | IComposerClient::Color color) override; |
| 223 | Error setLayerCompositionType(Display display, Layer layer, |
| 224 | int32_t type) override; |
| 225 | Error setLayerDataspace(Display display, Layer layer, |
| 226 | int32_t dataspace) override; |
| 227 | Error setLayerDisplayFrame(Display display, Layer layer, |
| 228 | const hwc_rect_t& frame) override; |
| 229 | Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override; |
| 230 | Error setLayerSidebandStream(Display display, Layer layer, |
| 231 | buffer_handle_t stream) override; |
| 232 | Error setLayerSourceCrop(Display display, Layer layer, |
| 233 | const hwc_frect_t& crop) override; |
| 234 | Error setLayerTransform(Display display, Layer layer, |
| 235 | int32_t transform) override; |
| 236 | Error setLayerVisibleRegion(Display display, Layer layer, |
| 237 | const std::vector<hwc_rect_t>& visible) override; |
| 238 | Error setLayerZOrder(Display display, Layer layer, uint32_t z) override; |
| 239 | |
| 240 | // IComposer: |
| 241 | Return<void> getCapabilities(getCapabilities_cb hidl_cb) override; |
| 242 | Return<void> dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override; |
| 243 | Return<void> createClient(createClient_cb hidl_cb) override; |
| 244 | |
| 245 | // ComposerView: |
| 246 | void RegisterObserver(Observer* observer) override; |
| 247 | void UnregisterObserver(Observer* observer) override; |
| 248 | void ReleaseFrame() override; |
| 249 | |
| 250 | private: |
| 251 | wp<VrComposerClient> client_; |
| 252 | sp<IComposerCallback> callbacks_; |
| 253 | |
| 254 | // Guard access to internal state from binder threads. |
| 255 | std::mutex mutex_; |
| 256 | |
| 257 | HwcDisplay display_; |
| 258 | |
| 259 | Observer* observer_ = nullptr; |
| 260 | |
| 261 | VrHwc(const VrHwc&) = delete; |
| 262 | void operator=(const VrHwc&) = delete; |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | ComposerView* GetComposerViewFromIComposer( |
| 267 | hardware::graphics::composer::V2_1::IComposer* composer); |
| 268 | |
| 269 | hardware::graphics::composer::V2_1::IComposer* HIDL_FETCH_IComposer( |
| 270 | const char* name); |
| 271 | |
| 272 | } // namespace dvr |
| 273 | } // namespace android |
| 274 | |
| 275 | #endif // VR_WINDOW_MANAGER_COMPOSER_IMPL_VR_HWC_H_ |