Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [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 | |
| 17 | #ifndef ANDROID_SF_COMPOSER_HAL_H |
| 18 | #define ANDROID_SF_COMPOSER_HAL_H |
| 19 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 20 | #include <memory> |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 21 | #include <string> |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 22 | #include <unordered_map> |
| 23 | #include <utility> |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | #include <android/hardware/graphics/composer/2.1/IComposer.h> |
| 27 | #include <utils/StrongPointer.h> |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 28 | #include <IComposerCommandBuffer.h> |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | namespace Hwc2 { |
| 33 | |
Chia-I Wu | 5901fda | 2016-11-17 10:26:37 +0800 | [diff] [blame] | 34 | using android::hardware::graphics::common::V1_0::ColorMode; |
| 35 | using android::hardware::graphics::common::V1_0::ColorTransform; |
| 36 | using android::hardware::graphics::common::V1_0::Dataspace; |
| 37 | using android::hardware::graphics::common::V1_0::Hdr; |
| 38 | using android::hardware::graphics::common::V1_0::PixelFormat; |
| 39 | using android::hardware::graphics::common::V1_0::Transform; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 40 | |
| 41 | using android::hardware::graphics::composer::V2_1::IComposer; |
| 42 | using android::hardware::graphics::composer::V2_1::IComposerCallback; |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 43 | using android::hardware::graphics::composer::V2_1::IComposerClient; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 44 | using android::hardware::graphics::composer::V2_1::Error; |
| 45 | using android::hardware::graphics::composer::V2_1::Display; |
| 46 | using android::hardware::graphics::composer::V2_1::Layer; |
| 47 | using android::hardware::graphics::composer::V2_1::Config; |
| 48 | |
Daniel Nicoara | 3c9cbd4 | 2017-01-17 12:04:06 -0500 | [diff] [blame] | 49 | using android::hardware::graphics::composer::V2_1::CommandWriterBase; |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 50 | using android::hardware::graphics::composer::V2_1::CommandReaderBase; |
| 51 | |
| 52 | using android::hardware::kSynchronizedReadWrite; |
| 53 | using android::hardware::MessageQueue; |
| 54 | using android::hardware::MQDescriptorSync; |
| 55 | using android::hardware::hidl_vec; |
| 56 | using android::hardware::hidl_handle; |
| 57 | |
| 58 | class CommandReader : public CommandReaderBase { |
| 59 | public: |
| 60 | ~CommandReader(); |
| 61 | |
| 62 | // Parse and execute commands from the command queue. The commands are |
| 63 | // actually return values from the server and will be saved in ReturnData. |
| 64 | Error parse(); |
| 65 | |
| 66 | // Get and clear saved errors. |
| 67 | struct CommandError { |
| 68 | uint32_t location; |
| 69 | Error error; |
| 70 | }; |
| 71 | std::vector<CommandError> takeErrors(); |
| 72 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 73 | bool hasChanges(Display display, uint32_t* outNumChangedCompositionTypes, |
| 74 | uint32_t* outNumLayerRequestMasks) const; |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 75 | |
| 76 | // Get and clear saved changed composition types. |
| 77 | void takeChangedCompositionTypes(Display display, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 78 | std::vector<Layer>* outLayers, |
| 79 | std::vector<IComposerClient::Composition>* outTypes); |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 80 | |
| 81 | // Get and clear saved display requests. |
| 82 | void takeDisplayRequests(Display display, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 83 | uint32_t* outDisplayRequestMask, std::vector<Layer>* outLayers, |
| 84 | std::vector<uint32_t>* outLayerRequestMasks); |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 85 | |
| 86 | // Get and clear saved release fences. |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 87 | void takeReleaseFences(Display display, std::vector<Layer>* outLayers, |
| 88 | std::vector<int>* outReleaseFences); |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 89 | |
| 90 | // Get and clear saved present fence. |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 91 | void takePresentFence(Display display, int* outPresentFence); |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | void resetData(); |
| 95 | |
| 96 | bool parseSelectDisplay(uint16_t length); |
| 97 | bool parseSetError(uint16_t length); |
| 98 | bool parseSetChangedCompositionTypes(uint16_t length); |
| 99 | bool parseSetDisplayRequests(uint16_t length); |
| 100 | bool parseSetPresentFence(uint16_t length); |
| 101 | bool parseSetReleaseFences(uint16_t length); |
| 102 | |
| 103 | struct ReturnData { |
| 104 | uint32_t displayRequests = 0; |
| 105 | |
| 106 | std::vector<Layer> changedLayers; |
| 107 | std::vector<IComposerClient::Composition> compositionTypes; |
| 108 | |
| 109 | std::vector<Layer> requestedLayers; |
| 110 | std::vector<uint32_t> requestMasks; |
| 111 | |
| 112 | int presentFence = -1; |
| 113 | |
| 114 | std::vector<Layer> releasedLayers; |
| 115 | std::vector<int> releaseFences; |
| 116 | }; |
| 117 | |
| 118 | std::vector<CommandError> mErrors; |
| 119 | std::unordered_map<Display, ReturnData> mReturnData; |
| 120 | |
| 121 | // When SELECT_DISPLAY is parsed, this is updated to point to the |
| 122 | // display's return data in mReturnData. We use it to avoid repeated |
| 123 | // map lookups. |
| 124 | ReturnData* mCurrentReturnData; |
| 125 | }; |
| 126 | |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 127 | // Composer is a wrapper to IComposer, a proxy to server-side composer. |
| 128 | class Composer { |
| 129 | public: |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 130 | Composer(bool useVrComposer); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 131 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 132 | std::vector<IComposer::Capability> getCapabilities(); |
| 133 | std::string dumpDebugInfo(); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 134 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 135 | void registerCallback(const sp<IComposerCallback>& callback); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 136 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 137 | uint32_t getMaxVirtualDisplayCount(); |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 138 | bool isUsingVrComposer() const { return mIsUsingVrComposer; } |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 139 | Error createVirtualDisplay(uint32_t width, uint32_t height, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 140 | PixelFormat* format, Display* outDisplay); |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 141 | Error destroyVirtualDisplay(Display display); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 142 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 143 | Error acceptDisplayChanges(Display display); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 144 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 145 | Error createLayer(Display display, Layer* outLayer); |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 146 | Error destroyLayer(Display display, Layer layer); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 147 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 148 | Error getActiveConfig(Display display, Config* outConfig); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 149 | Error getChangedCompositionTypes(Display display, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 150 | std::vector<Layer>* outLayers, |
| 151 | std::vector<IComposerClient::Composition>* outTypes); |
| 152 | Error getColorModes(Display display, std::vector<ColorMode>* outModes); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 153 | Error getDisplayAttribute(Display display, Config config, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 154 | IComposerClient::Attribute attribute, int32_t* outValue); |
| 155 | Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs); |
| 156 | Error getDisplayName(Display display, std::string* outName); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 157 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 158 | Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask, |
| 159 | std::vector<Layer>* outLayers, |
| 160 | std::vector<uint32_t>* outLayerRequestMasks); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 161 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 162 | Error getDisplayType(Display display, |
| 163 | IComposerClient::DisplayType* outType); |
| 164 | Error getDozeSupport(Display display, bool* outSupport); |
| 165 | Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes, |
| 166 | float* outMaxLuminance, float* outMaxAverageLuminance, |
| 167 | float* outMinLuminance); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 168 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 169 | Error getReleaseFences(Display display, std::vector<Layer>* outLayers, |
| 170 | std::vector<int>* outReleaseFences); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 171 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 172 | Error presentDisplay(Display display, int* outPresentFence); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 173 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 174 | Error setActiveConfig(Display display, Config config); |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * The composer caches client targets internally. When target is nullptr, |
| 178 | * the composer uses slot to look up the client target from its cache. |
| 179 | * When target is not nullptr, the cache is updated with the new target. |
| 180 | */ |
| 181 | Error setClientTarget(Display display, uint32_t slot, |
| 182 | const native_handle_t* target, |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 183 | int acquireFence, Dataspace dataspace, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 184 | const std::vector<IComposerClient::Rect>& damage); |
| 185 | Error setColorMode(Display display, ColorMode mode); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 186 | Error setColorTransform(Display display, const float* matrix, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 187 | ColorTransform hint); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 188 | Error setOutputBuffer(Display display, const native_handle_t* buffer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 189 | int releaseFence); |
| 190 | Error setPowerMode(Display display, IComposerClient::PowerMode mode); |
| 191 | Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled); |
| 192 | |
| 193 | Error setClientTargetSlotCount(Display display); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 194 | |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 195 | Error validateDisplay(Display display, uint32_t* outNumTypes, |
| 196 | uint32_t* outNumRequests); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 197 | |
| 198 | Error setCursorPosition(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 199 | int32_t x, int32_t y); |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 200 | /* see setClientTarget for the purpose of slot */ |
| 201 | Error setLayerBuffer(Display display, Layer layer, uint32_t slot, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 202 | const native_handle_t* buffer, int acquireFence); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 203 | Error setLayerSurfaceDamage(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 204 | const std::vector<IComposerClient::Rect>& damage); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 205 | Error setLayerBlendMode(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 206 | IComposerClient::BlendMode mode); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 207 | Error setLayerColor(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 208 | const IComposerClient::Color& color); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 209 | Error setLayerCompositionType(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 210 | IComposerClient::Composition type); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 211 | Error setLayerDataspace(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 212 | Dataspace dataspace); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 213 | Error setLayerDisplayFrame(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 214 | const IComposerClient::Rect& frame); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 215 | Error setLayerPlaneAlpha(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 216 | float alpha); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 217 | Error setLayerSidebandStream(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 218 | const native_handle_t* stream); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 219 | Error setLayerSourceCrop(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 220 | const IComposerClient::FRect& crop); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 221 | Error setLayerTransform(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 222 | Transform transform); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 223 | Error setLayerVisibleRegion(Display display, Layer layer, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 224 | const std::vector<IComposerClient::Rect>& visible); |
| 225 | Error setLayerZOrder(Display display, Layer layer, uint32_t z); |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 226 | Error setLayerInfo(Display display, Layer layer, uint32_t type, |
| 227 | uint32_t appId); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 228 | private: |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 229 | class CommandWriter : public CommandWriterBase { |
| 230 | public: |
| 231 | CommandWriter(uint32_t initialMaxSize); |
| 232 | ~CommandWriter() override; |
| 233 | |
| 234 | void setLayerInfo(uint32_t type, uint32_t appId); |
| 235 | }; |
| 236 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 237 | // Many public functions above simply write a command into the command |
| 238 | // queue to batch the calls. validateDisplay and presentDisplay will call |
| 239 | // this function to execute the command queue. |
| 240 | Error execute(); |
| 241 | |
| 242 | sp<IComposer> mComposer; |
| 243 | sp<IComposerClient> mClient; |
| 244 | |
| 245 | // 64KiB minus a small space for metadata such as read/write pointers |
| 246 | static constexpr size_t kWriterInitialSize = |
| 247 | 64 * 1024 / sizeof(uint32_t) - 16; |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 248 | CommandWriter mWriter; |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 249 | CommandReader mReader; |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 250 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 251 | // When true, the we attach to the vr_hwcomposer service instead of the |
| 252 | // hwcomposer. This allows us to redirect surfaces to 3d surfaces in vr. |
| 253 | const bool mIsUsingVrComposer; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | } // namespace Hwc2 |
| 257 | |
| 258 | } // namespace android |
| 259 | |
| 260 | #endif // ANDROID_SF_COMPOSER_HAL_H |