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.3/ComposerHal.h>
26 #include <composer-passthrough/2.2/HwcHal.h>
27
28 namespace android {
29 namespace hardware {
30 namespace graphics {
31 namespace composer {
32 namespace V2_3 {
33 namespace passthrough {
34
35 namespace detail {
36
37 using common::V1_1::RenderIntent;
38 using common::V1_2::ColorMode;
39 using common::V1_2::Dataspace;
40 using common::V1_2::Hdr;
41 using common::V1_2::PixelFormat;
42 using V2_1::Display;
43 using V2_1::Error;
44
45 namespace {
46
isIdentityMatrix(const float * matrix)47 bool isIdentityMatrix(const float* matrix) {
48 if (matrix[0] == 1.0 && matrix[1] == 0.0 && matrix[2] == 0.0 && matrix[3] == 0.0 &&
49 matrix[4] == 0.0 && matrix[5] == 1.0 && matrix[6] == 0.0 && matrix[7] == 0.0 &&
50 matrix[8] == 0.0 && matrix[9] == 0.0 && matrix[10] == 1.0 && matrix[11] == 0.0 &&
51 matrix[12] == 0.0 && matrix[13] == 0.0 && matrix[14] == 0.0 && matrix[15] == 1.0) {
52 return true;
53 }
54 return false;
55 }
56
57 } // namespace
58
59 // HwcHalImpl implements V2_*::hal::ComposerHal on top of hwcomposer2
60 template <typename Hal>
61 class HwcHalImpl : public V2_2::passthrough::detail::HwcHalImpl<Hal> {
62 public:
getPerFrameMetadataKeys_2_3(Display display,std::vector<IComposerClient::PerFrameMetadataKey> * outKeys)63 Error getPerFrameMetadataKeys_2_3(
64 Display display, std::vector<IComposerClient::PerFrameMetadataKey>* outKeys) override {
65 std::vector<V2_2::IComposerClient::PerFrameMetadataKey> castKeys;
66 Error error = getPerFrameMetadataKeys(display, &castKeys);
67 if (error != Error::NONE) {
68 return error;
69 }
70 outKeys->clear();
71 for (auto key : castKeys) {
72 outKeys->push_back(static_cast<IComposerClient::PerFrameMetadataKey>(key));
73 }
74 return Error::NONE;
75 }
76
setLayerPerFrameMetadata_2_3(Display display,Layer layer,const std::vector<IComposerClient::PerFrameMetadata> & metadata)77 Error setLayerPerFrameMetadata_2_3(
78 Display display, Layer layer,
79 const std::vector<IComposerClient::PerFrameMetadata>& metadata) override {
80 return setLayerPerFrameMetadata(
81 display, layer,
82 reinterpret_cast<const std::vector<V2_2::IComposerClient::PerFrameMetadata>&>(
83 metadata));
84 }
85
setColorMode_2_3(Display display,ColorMode mode,RenderIntent intent)86 Error setColorMode_2_3(Display display, ColorMode mode, RenderIntent intent) override {
87 return setColorMode_2_2(display, static_cast<common::V1_1::ColorMode>(mode), intent);
88 }
89
getRenderIntents_2_3(Display display,ColorMode mode,std::vector<RenderIntent> * outIntents)90 Error getRenderIntents_2_3(Display display, ColorMode mode,
91 std::vector<RenderIntent>* outIntents) override {
92 return getRenderIntents(display, static_cast<common::V1_1::ColorMode>(mode), outIntents);
93 }
94
getColorModes_2_3(Display display,hidl_vec<ColorMode> * outModes)95 Error getColorModes_2_3(Display display, hidl_vec<ColorMode>* outModes) override {
96 return getColorModes_2_2(display,
97 reinterpret_cast<hidl_vec<common::V1_1::ColorMode>*>(outModes));
98 }
99
getHdrCapabilities_2_3(Display display,hidl_vec<Hdr> * outTypes,float * outMaxLuminance,float * outMaxAverageLuminance,float * outMinLuminance)100 Error getHdrCapabilities_2_3(Display display, hidl_vec<Hdr>* outTypes, float* outMaxLuminance,
101 float* outMaxAverageLuminance, float* outMinLuminance) override {
102 return getHdrCapabilities(display, reinterpret_cast<hidl_vec<common::V1_0::Hdr>*>(outTypes),
103 outMaxLuminance, outMaxAverageLuminance, outMinLuminance);
104 }
105
getClientTargetSupport_2_3(Display display,uint32_t width,uint32_t height,PixelFormat format,Dataspace dataspace)106 Error getClientTargetSupport_2_3(Display display, uint32_t width, uint32_t height,
107 PixelFormat format, Dataspace dataspace) override {
108 return getClientTargetSupport_2_2(display, width, height,
109 static_cast<common::V1_1::PixelFormat>(format),
110 static_cast<common::V1_1::Dataspace>(dataspace));
111 }
112
getReadbackBufferAttributes_2_3(Display display,PixelFormat * outFormat,Dataspace * outDataspace)113 Error getReadbackBufferAttributes_2_3(Display display, PixelFormat* outFormat,
114 Dataspace* outDataspace) override {
115 return getReadbackBufferAttributes(
116 display, reinterpret_cast<common::V1_1::PixelFormat*>(outFormat),
117 reinterpret_cast<common::V1_1::Dataspace*>(outDataspace));
118 }
119
getDisplayIdentificationData(Display display,uint8_t * outPort,std::vector<uint8_t> * outData)120 Error getDisplayIdentificationData(Display display, uint8_t* outPort,
121 std::vector<uint8_t>* outData) override {
122 if (!mDispatch.getDisplayIdentificationData) {
123 return Error::UNSUPPORTED;
124 }
125
126 uint32_t size = 0;
127 int32_t error =
128 mDispatch.getDisplayIdentificationData(mDevice, display, outPort, &size, nullptr);
129 if (error != HWC2_ERROR_NONE) {
130 return static_cast<Error>(error);
131 }
132
133 std::vector<uint8_t> data(size);
134 error =
135 mDispatch.getDisplayIdentificationData(mDevice, display, outPort, &size, data.data());
136 if (error != HWC2_ERROR_NONE) {
137 return static_cast<Error>(error);
138 }
139
140 data.resize(size);
141 *outData = std::move(data);
142 return Error::NONE;
143 }
144
setLayerColorTransform(Display display,Layer layer,const float * matrix)145 Error setLayerColorTransform(Display display, Layer layer, const float* matrix) override {
146 if (!mDispatch.setLayerColorTransform) {
147 if (isIdentityMatrix(matrix)) {
148 // If an identity matrix is set, then we can remove the layer from client
149 // composition list.
150 mClientCompositionLayers[display].erase(layer);
151 return Error::UNSUPPORTED;
152 }
153 // if setLayerColorTransform is not implemented, per spec we want to make sure the
154 // layer marked as client composition, and thus we maintain a list, and mark all these
155 // layers as client composition later before validate the display.
156 mClientCompositionLayers[display].insert(layer);
157 return Error::UNSUPPORTED;
158 }
159 int32_t err = mDispatch.setLayerColorTransform(mDevice, display, layer, matrix);
160 return static_cast<Error>(err);
161 }
162
getDisplayedContentSamplingAttributes(uint64_t display,PixelFormat & format,Dataspace & dataspace,hidl_bitfield<IComposerClient::FormatColorComponent> & componentMask)163 Error getDisplayedContentSamplingAttributes(
164 uint64_t display, PixelFormat& format, Dataspace& dataspace,
165 hidl_bitfield<IComposerClient::FormatColorComponent>& componentMask) override {
166 if (!mDispatch.getDisplayedContentSamplingAttributes) {
167 return Error::UNSUPPORTED;
168 }
169 int32_t formatRaw = 0;
170 int32_t dataspaceRaw = 0;
171 uint8_t componentMaskRaw = 0;
172 int32_t errorRaw = mDispatch.getDisplayedContentSamplingAttributes(
173 mDevice, display, &formatRaw, &dataspaceRaw, &componentMaskRaw);
174 auto error = static_cast<Error>(errorRaw);
175 if (error == Error::NONE) {
176 format = static_cast<PixelFormat>(formatRaw);
177 dataspace = static_cast<Dataspace>(dataspaceRaw);
178 componentMask =
179 static_cast<hidl_bitfield<IComposerClient::FormatColorComponent>>(componentMaskRaw);
180 }
181 return error;
182 };
183
setDisplayedContentSamplingEnabled(uint64_t display,IComposerClient::DisplayedContentSampling enable,hidl_bitfield<IComposerClient::FormatColorComponent> componentMask,uint64_t maxFrames)184 Error setDisplayedContentSamplingEnabled(
185 uint64_t display, IComposerClient::DisplayedContentSampling enable,
186 hidl_bitfield<IComposerClient::FormatColorComponent> componentMask,
187 uint64_t maxFrames) override {
188 if (!mDispatch.setDisplayedContentSamplingEnabled) {
189 return Error::UNSUPPORTED;
190 }
191 return static_cast<Error>(mDispatch.setDisplayedContentSamplingEnabled(
192 mDevice, display, static_cast<int32_t>(enable), componentMask, maxFrames));
193 }
194
getDisplayedContentSample(uint64_t display,uint64_t maxFrames,uint64_t timestamp,uint64_t & frameCount,hidl_vec<uint64_t> & sampleComponent0,hidl_vec<uint64_t> & sampleComponent1,hidl_vec<uint64_t> & sampleComponent2,hidl_vec<uint64_t> & sampleComponent3)195 Error getDisplayedContentSample(uint64_t display, uint64_t maxFrames, uint64_t timestamp,
196 uint64_t& frameCount, hidl_vec<uint64_t>& sampleComponent0,
197 hidl_vec<uint64_t>& sampleComponent1,
198 hidl_vec<uint64_t>& sampleComponent2,
199 hidl_vec<uint64_t>& sampleComponent3) override {
200 if (!mDispatch.getDisplayedContentSample) {
201 return Error::UNSUPPORTED;
202 }
203
204 int32_t size[4] = {0};
205 auto errorRaw = mDispatch.getDisplayedContentSample(mDevice, display, maxFrames, timestamp,
206 &frameCount, size, nullptr);
207 if (errorRaw != HWC2_ERROR_NONE) {
208 return static_cast<Error>(errorRaw);
209 }
210
211 sampleComponent0.resize(size[0]);
212 sampleComponent1.resize(size[1]);
213 sampleComponent2.resize(size[2]);
214 sampleComponent3.resize(size[3]);
215 uint64_t* samples[] = {sampleComponent0.data(), sampleComponent1.data(),
216 sampleComponent2.data(), sampleComponent3.data()};
217 errorRaw = mDispatch.getDisplayedContentSample(mDevice, display, maxFrames, timestamp,
218 &frameCount, size, samples);
219 return static_cast<Error>(errorRaw);
220 }
221
getDisplayCapabilities(Display display,std::vector<IComposerClient::DisplayCapability> * outCapabilities)222 Error getDisplayCapabilities(
223 Display display,
224 std::vector<IComposerClient::DisplayCapability>* outCapabilities) override {
225 uint32_t count = 0;
226 int32_t error = mDispatch.getDisplayCapabilities(mDevice, display, &count, nullptr);
227 if (error != HWC2_ERROR_NONE) {
228 return static_cast<Error>(error);
229 }
230 outCapabilities->resize(count);
231 error = mDispatch.getDisplayCapabilities(
232 mDevice, display, &count,
233 reinterpret_cast<std::underlying_type<IComposerClient::DisplayCapability>::type*>(
234 outCapabilities->data()));
235 if (error != HWC2_ERROR_NONE) {
236 *outCapabilities = std::vector<IComposerClient::DisplayCapability>();
237 return static_cast<Error>(error);
238 }
239 return Error::NONE;
240 }
241
setLayerPerFrameMetadataBlobs(Display display,Layer layer,std::vector<IComposerClient::PerFrameMetadataBlob> & metadata)242 Error setLayerPerFrameMetadataBlobs(
243 Display display, Layer layer,
244 std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) override {
245 if (!mDispatch.setLayerPerFrameMetadataBlobs) {
246 return Error::UNSUPPORTED;
247 }
248
249 std::vector<IComposerClient::PerFrameMetadataKey> keys;
250 std::vector<uint32_t> sizes;
251 std::vector<uint8_t> blobs;
252
253 for (auto metadataBlob : metadata) {
254 keys.push_back(metadataBlob.key);
255 sizes.push_back(metadataBlob.blob.size());
256
257 int writeIndex = blobs.size();
258 blobs.resize(blobs.size() + metadataBlob.blob.size());
259 memcpy(blobs.data() + writeIndex, metadataBlob.blob.data(), metadataBlob.blob.size());
260 }
261
262 int32_t err = mDispatch.setLayerPerFrameMetadataBlobs(
263 mDevice, display, layer, static_cast<uint32_t>(metadata.size()),
264 reinterpret_cast<int32_t*>(keys.data()), reinterpret_cast<uint32_t*>(sizes.data()),
265 blobs.data());
266 return static_cast<Error>(err);
267 }
268
getDisplayBrightnessSupport(Display display,bool * outSupport)269 Error getDisplayBrightnessSupport(Display display, bool* outSupport) {
270 if (!mDispatch.getDisplayBrightnessSupport) {
271 // Preemptively set to false.
272 *outSupport = false;
273 // Try to query from getDisplayCapabilities.
274 std::vector<IComposerClient::DisplayCapability> capabilities;
275 Error error = getDisplayCapabilities(display, &capabilities);
276 if (error != Error::NONE) {
277 // This function is not registered, always return UNSUPPORTED.
278 return Error::UNSUPPORTED;
279 }
280 *outSupport =
281 std::find(capabilities.begin(), capabilities.end(),
282 IComposerClient::DisplayCapability::BRIGHTNESS) != capabilities.end();
283 // This function is not registered, always return UNSUPPORTED.
284 return Error::UNSUPPORTED;
285 }
286 bool support = false;
287 int32_t error = mDispatch.getDisplayBrightnessSupport(mDevice, display, &support);
288 *outSupport = support;
289 return static_cast<Error>(error);
290 }
291
setDisplayBrightness(Display display,float brightness)292 Error setDisplayBrightness(Display display, float brightness) {
293 if (std::isnan(brightness) || brightness > 1.0f ||
294 (brightness < 0.0f && brightness != -1.0f)) {
295 return Error::BAD_PARAMETER;
296 }
297 int32_t error = mDispatch.setDisplayBrightness(mDevice, display, brightness);
298 return static_cast<Error>(error);
299 }
300
301 protected:
initDispatch()302 bool initDispatch() override {
303 if (!BaseType2_2::initDispatch()) {
304 return false;
305 }
306
307 if (!BaseType2_1::initDispatch(HWC2_FUNCTION_GET_DISPLAY_CAPABILITIES,
308 &mDispatch.getDisplayCapabilities) ||
309 !BaseType2_1::initDispatch(HWC2_FUNCTION_SET_DISPLAY_BRIGHTNESS,
310 &mDispatch.setDisplayBrightness)) {
311 return false;
312 }
313
314 this->initOptionalDispatch(HWC2_FUNCTION_GET_DISPLAY_IDENTIFICATION_DATA,
315 &mDispatch.getDisplayIdentificationData);
316 this->initOptionalDispatch(HWC2_FUNCTION_SET_LAYER_COLOR_TRANSFORM,
317 &mDispatch.setLayerColorTransform);
318 this->initOptionalDispatch(HWC2_FUNCTION_GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES,
319 &mDispatch.getDisplayedContentSamplingAttributes);
320 this->initOptionalDispatch(HWC2_FUNCTION_SET_DISPLAYED_CONTENT_SAMPLING_ENABLED,
321 &mDispatch.setDisplayedContentSamplingEnabled);
322 this->initOptionalDispatch(HWC2_FUNCTION_GET_DISPLAYED_CONTENT_SAMPLE,
323 &mDispatch.getDisplayedContentSample);
324 this->initOptionalDispatch(HWC2_FUNCTION_SET_LAYER_PER_FRAME_METADATA_BLOBS,
325 &mDispatch.setLayerPerFrameMetadataBlobs);
326 this->initOptionalDispatch(HWC2_FUNCTION_GET_DISPLAY_BRIGHTNESS_SUPPORT,
327 &mDispatch.getDisplayBrightnessSupport);
328 return true;
329 }
330
getChangedCompositionTypes(Display display,uint32_t * outTypesCount,Layer * outChangedLayers,IComposerClient::Composition * outCompositionTypes)331 int32_t getChangedCompositionTypes(Display display, uint32_t* outTypesCount,
332 Layer* outChangedLayers,
333 IComposerClient::Composition* outCompositionTypes) override {
334 if (outChangedLayers == nullptr && outCompositionTypes == nullptr) {
335 uint32_t typesCount = 0;
336 int32_t error = BaseType2_1::getChangedCompositionTypesInternal(display, &typesCount,
337 nullptr, nullptr);
338 if (error != HWC2_ERROR_NONE) {
339 return error;
340 }
341 mChangedLayersCache[display].resize(typesCount);
342 mCompositionTypesCache[display].resize(typesCount);
343 error = BaseType2_1::getChangedCompositionTypesInternal(
344 display, &typesCount, mChangedLayersCache[display].data(),
345 mCompositionTypesCache[display].data());
346 if (error != HWC2_ERROR_NONE) {
347 return error;
348 }
349 for (Layer layer : mClientCompositionLayers[display]) {
350 bool exist = false;
351 for (uint32_t i = 0; i < typesCount; ++i) {
352 if (mChangedLayersCache[display][i] == layer) {
353 exist = true;
354 break;
355 }
356 }
357 if (!exist) {
358 mChangedLayersCache[display].push_back(layer);
359 mCompositionTypesCache[display].push_back(IComposerClient::Composition::CLIENT);
360 }
361 }
362 *outTypesCount = mChangedLayersCache[display].size();
363 return error;
364 }
365 for (uint32_t i = 0; i < *outTypesCount; ++i) {
366 if (outChangedLayers != nullptr) {
367 outChangedLayers[i] = mChangedLayersCache[display][i];
368 }
369 if (outCompositionTypes != nullptr) {
370 outCompositionTypes[i] = mCompositionTypesCache[display][i];
371 }
372 }
373 return HWC2_ERROR_NONE;
374 }
375
onLayerDestroyed(Display display,Layer layer)376 void onLayerDestroyed(Display display, Layer layer) override {
377 if (mClientCompositionLayers.find(display) == mClientCompositionLayers.end()) {
378 return;
379 }
380 mClientCompositionLayers[display].erase(layer);
381 }
382
onBeforeValidateDisplay(Display display)383 void onBeforeValidateDisplay(Display display) override {
384 if (mClientCompositionLayers.find(display) == mClientCompositionLayers.end()) {
385 return;
386 }
387
388 // clear the cache proactively so that we don't hold too much memory over time.
389 mChangedLayersCache[display].clear();
390 mCompositionTypesCache[display].clear();
391
392 // SET_LAYER_COLOR_TRANSFORM is optional, and thus if it's not implemented, we need to
393 // follow the spec to make sure those layers marked as client composition before validate
394 // the display.
395 if (!mDispatch.setLayerColorTransform) {
396 for (Layer layer : mClientCompositionLayers[display]) {
397 BaseType2_1::setLayerCompositionType(
398 display, layer, static_cast<int32_t>(IComposerClient::Composition::CLIENT));
399 }
400 }
401 }
402
403 private:
404 struct {
405 HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA getDisplayIdentificationData;
406 HWC2_PFN_SET_LAYER_COLOR_TRANSFORM setLayerColorTransform;
407 HWC2_PFN_GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES getDisplayedContentSamplingAttributes;
408 HWC2_PFN_SET_DISPLAYED_CONTENT_SAMPLING_ENABLED setDisplayedContentSamplingEnabled;
409 HWC2_PFN_GET_DISPLAYED_CONTENT_SAMPLE getDisplayedContentSample;
410 HWC2_PFN_GET_DISPLAY_CAPABILITIES getDisplayCapabilities;
411 HWC2_PFN_SET_LAYER_PER_FRAME_METADATA_BLOBS setLayerPerFrameMetadataBlobs;
412 HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT getDisplayBrightnessSupport;
413 HWC2_PFN_SET_DISPLAY_BRIGHTNESS setDisplayBrightness;
414 } mDispatch = {};
415
416 using BaseType2_2 = V2_2::passthrough::detail::HwcHalImpl<Hal>;
417 using BaseType2_1 = V2_1::passthrough::detail::HwcHalImpl<Hal>;
418 using BaseType2_1::getHdrCapabilities;
419 using BaseType2_1::mDevice;
420 using BaseType2_2::getClientTargetSupport_2_2;
421 using BaseType2_2::getColorModes_2_2;
422 using BaseType2_2::getPerFrameMetadataKeys;
423 using BaseType2_2::getReadbackBufferAttributes;
424 using BaseType2_2::getRenderIntents;
425 using BaseType2_2::setColorMode_2_2;
426 using BaseType2_2::setLayerPerFrameMetadata;
427 std::map<Display, std::set<Layer>> mClientCompositionLayers;
428 std::map<Display, std::vector<Layer>> mChangedLayersCache;
429 std::map<Display, std::vector<IComposerClient::Composition>> mCompositionTypesCache;
430 };
431
432 } // namespace detail
433
434 using HwcHal = detail::HwcHalImpl<hal::ComposerHal>;
435
436 } // namespace passthrough
437 } // namespace V2_3
438 } // namespace composer
439 } // namespace graphics
440 } // namespace hardware
441 } // namespace android
442