1 /* 2 * Copyright 2018 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 #pragma once 18 19 #ifndef LOG_TAG 20 #warning "HwcHal.h included without LOG_TAG" 21 #endif 22 23 #include <type_traits> 24 25 #include <composer-hal/2.2/ComposerHal.h> 26 #include <composer-passthrough/2.1/HwcHal.h> 27 28 namespace android { 29 namespace hardware { 30 namespace graphics { 31 namespace composer { 32 namespace V2_2 { 33 namespace passthrough { 34 35 namespace detail { 36 37 using common::V1_1::ColorMode; 38 using common::V1_1::Dataspace; 39 using common::V1_1::PixelFormat; 40 using common::V1_1::RenderIntent; 41 using V2_1::Display; 42 using V2_1::Error; 43 using V2_1::Layer; 44 45 // HwcHalImpl implements V2_*::hal::ComposerHal on top of hwcomposer2 46 template <typename Hal> 47 class HwcHalImpl : public V2_1::passthrough::detail::HwcHalImpl<Hal> { 48 public: 49 // XXX when can we return Error::UNSUPPORTED? 50 getPerFrameMetadataKeys(Display display,std::vector<IComposerClient::PerFrameMetadataKey> * outKeys)51 Error getPerFrameMetadataKeys( 52 Display display, std::vector<IComposerClient::PerFrameMetadataKey>* outKeys) override { 53 if (!mDispatch.getPerFrameMetadataKeys) { 54 return Error::UNSUPPORTED; 55 } 56 57 uint32_t count = 0; 58 int32_t error = mDispatch.getPerFrameMetadataKeys(mDevice, display, &count, nullptr); 59 if (error != HWC2_ERROR_NONE) { 60 return static_cast<Error>(error); 61 } 62 63 std::vector<IComposerClient::PerFrameMetadataKey> keys(count); 64 error = mDispatch.getPerFrameMetadataKeys( 65 mDevice, display, &count, 66 reinterpret_cast<std::underlying_type<IComposerClient::PerFrameMetadataKey>::type*>( 67 keys.data())); 68 if (error != HWC2_ERROR_NONE) { 69 return static_cast<Error>(error); 70 } 71 72 keys.resize(count); 73 *outKeys = std::move(keys); 74 return Error::NONE; 75 } 76 setLayerPerFrameMetadata(Display display,Layer layer,const std::vector<IComposerClient::PerFrameMetadata> & metadata)77 Error setLayerPerFrameMetadata( 78 Display display, Layer layer, 79 const std::vector<IComposerClient::PerFrameMetadata>& metadata) override { 80 if (!mDispatch.setLayerPerFrameMetadata) { 81 return Error::UNSUPPORTED; 82 } 83 84 std::vector<int32_t> keys; 85 std::vector<float> values; 86 keys.reserve(metadata.size()); 87 values.reserve(metadata.size()); 88 for (const auto& m : metadata) { 89 keys.push_back(static_cast<int32_t>(m.key)); 90 values.push_back(m.value); 91 } 92 93 int32_t error = mDispatch.setLayerPerFrameMetadata(mDevice, display, layer, metadata.size(), 94 keys.data(), values.data()); 95 return static_cast<Error>(error); 96 } 97 getReadbackBufferAttributes(Display display,PixelFormat * outFormat,Dataspace * outDataspace)98 Error getReadbackBufferAttributes(Display display, PixelFormat* outFormat, 99 Dataspace* outDataspace) override { 100 if (!mDispatch.getReadbackBufferAttributes) { 101 return Error::UNSUPPORTED; 102 } 103 104 int32_t format = 0; 105 int32_t dataspace = 0; 106 int32_t error = 107 mDispatch.getReadbackBufferAttributes(mDevice, display, &format, &dataspace); 108 if (error == HWC2_ERROR_NONE) { 109 *outFormat = static_cast<PixelFormat>(format); 110 *outDataspace = static_cast<Dataspace>(dataspace); 111 } 112 return static_cast<Error>(error); 113 } 114 setReadbackBuffer(Display display,const native_handle_t * bufferHandle,base::unique_fd fenceFd)115 Error setReadbackBuffer(Display display, const native_handle_t* bufferHandle, 116 base::unique_fd fenceFd) override { 117 if (!mDispatch.setReadbackBuffer) { 118 return Error::UNSUPPORTED; 119 } 120 121 int32_t error = 122 mDispatch.setReadbackBuffer(mDevice, display, bufferHandle, fenceFd.release()); 123 return static_cast<Error>(error); 124 } 125 getReadbackBufferFence(Display display,base::unique_fd * outFenceFd)126 Error getReadbackBufferFence(Display display, base::unique_fd* outFenceFd) override { 127 if (!mDispatch.getReadbackBufferFence) { 128 return Error::UNSUPPORTED; 129 } 130 131 int32_t fenceFd = -1; 132 int32_t error = mDispatch.getReadbackBufferFence(mDevice, display, &fenceFd); 133 outFenceFd->reset(fenceFd); 134 return static_cast<Error>(error); 135 } 136 createVirtualDisplay_2_2(uint32_t width,uint32_t height,PixelFormat * format,Display * outDisplay)137 Error createVirtualDisplay_2_2(uint32_t width, uint32_t height, PixelFormat* format, 138 Display* outDisplay) override { 139 return createVirtualDisplay( 140 width, height, reinterpret_cast<common::V1_0::PixelFormat*>(format), outDisplay); 141 } 142 getClientTargetSupport_2_2(Display display,uint32_t width,uint32_t height,PixelFormat format,Dataspace dataspace)143 Error getClientTargetSupport_2_2(Display display, uint32_t width, uint32_t height, 144 PixelFormat format, Dataspace dataspace) override { 145 return getClientTargetSupport(display, width, height, 146 static_cast<common::V1_0::PixelFormat>(format), 147 static_cast<common::V1_0::Dataspace>(dataspace)); 148 } 149 setPowerMode_2_2(Display display,IComposerClient::PowerMode mode)150 Error setPowerMode_2_2(Display display, IComposerClient::PowerMode mode) override { 151 if (mode == IComposerClient::PowerMode::ON_SUSPEND) { 152 return Error::UNSUPPORTED; 153 } 154 return setPowerMode(display, static_cast<V2_1::IComposerClient::PowerMode>(mode)); 155 } 156 setLayerFloatColor(Display display,Layer layer,IComposerClient::FloatColor color)157 Error setLayerFloatColor(Display display, Layer layer, 158 IComposerClient::FloatColor color) override { 159 if (!mDispatch.setLayerFloatColor) { 160 return Error::UNSUPPORTED; 161 } 162 163 int32_t error = mDispatch.setLayerFloatColor( 164 mDevice, display, layer, hwc_float_color{color.r, color.g, color.b, color.a}); 165 return static_cast<Error>(error); 166 } 167 getColorModes_2_2(Display display,hidl_vec<ColorMode> * outModes)168 Error getColorModes_2_2(Display display, hidl_vec<ColorMode>* outModes) override { 169 return getColorModes(display, 170 reinterpret_cast<hidl_vec<common::V1_0::ColorMode>*>(outModes)); 171 } 172 getRenderIntents(Display display,ColorMode mode,std::vector<RenderIntent> * outIntents)173 Error getRenderIntents(Display display, ColorMode mode, 174 std::vector<RenderIntent>* outIntents) override { 175 if (!mDispatch.getRenderIntents) { 176 IComposerClient::DisplayType type; 177 if (getDisplayType(display, &type) == Error::BAD_DISPLAY) { 178 return Error::BAD_DISPLAY; 179 } 180 if (mode < ColorMode::NATIVE || mode > ColorMode::DISPLAY_P3) { 181 return Error::BAD_PARAMETER; 182 } 183 184 *outIntents = std::vector<RenderIntent>({RenderIntent::COLORIMETRIC}); 185 return Error::NONE; 186 } 187 188 uint32_t count = 0; 189 int32_t error = 190 mDispatch.getRenderIntents(mDevice, display, int32_t(mode), &count, nullptr); 191 if (error != HWC2_ERROR_NONE) { 192 return static_cast<Error>(error); 193 } 194 195 std::vector<RenderIntent> intents(count); 196 error = mDispatch.getRenderIntents( 197 mDevice, display, int32_t(mode), &count, 198 reinterpret_cast<std::underlying_type<RenderIntent>::type*>(intents.data())); 199 if (error != HWC2_ERROR_NONE) { 200 return static_cast<Error>(error); 201 } 202 intents.resize(count); 203 204 *outIntents = std::move(intents); 205 return Error::NONE; 206 } 207 setColorMode_2_2(Display display,ColorMode mode,RenderIntent intent)208 Error setColorMode_2_2(Display display, ColorMode mode, RenderIntent intent) override { 209 if (!mDispatch.setColorModeWithRenderIntent) { 210 if (intent < RenderIntent::COLORIMETRIC || intent > RenderIntent::TONE_MAP_ENHANCE) { 211 return Error::BAD_PARAMETER; 212 } 213 if (intent != RenderIntent::COLORIMETRIC) { 214 return Error::UNSUPPORTED; 215 } 216 return setColorMode(display, static_cast<common::V1_0::ColorMode>(mode)); 217 } 218 219 int32_t err = mDispatch.setColorModeWithRenderIntent( 220 mDevice, display, static_cast<int32_t>(mode), static_cast<int32_t>(intent)); 221 return static_cast<Error>(err); 222 } 223 getDataspaceSaturationMatrix(Dataspace dataspace)224 std::array<float, 16> getDataspaceSaturationMatrix(Dataspace dataspace) override { 225 std::array<float, 16> matrix; 226 227 int32_t error = HWC2_ERROR_UNSUPPORTED; 228 if (mDispatch.getDataspaceSaturationMatrix) { 229 error = mDispatch.getDataspaceSaturationMatrix(mDevice, static_cast<int32_t>(dataspace), 230 matrix.data()); 231 } 232 if (error != HWC2_ERROR_NONE) { 233 return std::array<float, 16>{ 234 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 235 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 236 }; 237 } 238 239 return matrix; 240 } 241 242 protected: 243 template <typename T> initOptionalDispatch(hwc2_function_descriptor_t desc,T * outPfn)244 bool initOptionalDispatch(hwc2_function_descriptor_t desc, T* outPfn) { 245 auto pfn = mDevice->getFunction(mDevice, desc); 246 if (pfn) { 247 *outPfn = reinterpret_cast<T>(pfn); 248 return true; 249 } else { 250 return false; 251 } 252 } 253 initDispatch()254 bool initDispatch() override { 255 if (!BaseType2_1::initDispatch()) { 256 return false; 257 } 258 259 initOptionalDispatch(HWC2_FUNCTION_SET_LAYER_FLOAT_COLOR, &mDispatch.setLayerFloatColor); 260 261 initOptionalDispatch(HWC2_FUNCTION_SET_LAYER_PER_FRAME_METADATA, 262 &mDispatch.setLayerPerFrameMetadata); 263 initOptionalDispatch(HWC2_FUNCTION_GET_PER_FRAME_METADATA_KEYS, 264 &mDispatch.getPerFrameMetadataKeys); 265 266 initOptionalDispatch(HWC2_FUNCTION_SET_READBACK_BUFFER, &mDispatch.setReadbackBuffer); 267 initOptionalDispatch(HWC2_FUNCTION_GET_READBACK_BUFFER_ATTRIBUTES, 268 &mDispatch.getReadbackBufferAttributes); 269 initOptionalDispatch(HWC2_FUNCTION_GET_READBACK_BUFFER_FENCE, 270 &mDispatch.getReadbackBufferFence); 271 272 initOptionalDispatch(HWC2_FUNCTION_GET_RENDER_INTENTS, &mDispatch.getRenderIntents); 273 initOptionalDispatch(HWC2_FUNCTION_SET_COLOR_MODE_WITH_RENDER_INTENT, 274 &mDispatch.setColorModeWithRenderIntent); 275 initOptionalDispatch(HWC2_FUNCTION_GET_DATASPACE_SATURATION_MATRIX, 276 &mDispatch.getDataspaceSaturationMatrix); 277 278 return true; 279 } 280 281 struct { 282 HWC2_PFN_SET_LAYER_FLOAT_COLOR setLayerFloatColor; 283 HWC2_PFN_SET_LAYER_PER_FRAME_METADATA setLayerPerFrameMetadata; 284 HWC2_PFN_GET_PER_FRAME_METADATA_KEYS getPerFrameMetadataKeys; 285 HWC2_PFN_SET_READBACK_BUFFER setReadbackBuffer; 286 HWC2_PFN_GET_READBACK_BUFFER_ATTRIBUTES getReadbackBufferAttributes; 287 HWC2_PFN_GET_READBACK_BUFFER_FENCE getReadbackBufferFence; 288 HWC2_PFN_GET_RENDER_INTENTS getRenderIntents; 289 HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT setColorModeWithRenderIntent; 290 HWC2_PFN_GET_DATASPACE_SATURATION_MATRIX getDataspaceSaturationMatrix; 291 } mDispatch = {}; 292 293 private: 294 using BaseType2_1 = V2_1::passthrough::detail::HwcHalImpl<Hal>; 295 using BaseType2_1::getColorModes; 296 using BaseType2_1::getDisplayType; 297 using BaseType2_1::mDevice; 298 using BaseType2_1::setColorMode; 299 using BaseType2_1::createVirtualDisplay; 300 using BaseType2_1::getClientTargetSupport; 301 using BaseType2_1::setPowerMode; 302 }; 303 304 } // namespace detail 305 306 using HwcHal = detail::HwcHalImpl<hal::ComposerHal>; 307 308 } // namespace passthrough 309 } // namespace V2_2 310 } // namespace composer 311 } // namespace graphics 312 } // namespace hardware 313 } // namespace android 314