blob: 7322c134ceaf067d77a4f93c6847abdbba252855 [file] [log] [blame]
Dan Stozac6998d22015-09-24 17:03:36 -07001/*
2 * Copyright 2015 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//#define LOG_NDEBUG 0
18
19#undef LOG_TAG
20#define LOG_TAG "HWC2On1Adapter"
21#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
23#include "HWC2On1Adapter.h"
24
Dan Stozac6998d22015-09-24 17:03:36 -070025#include <inttypes.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070026
27#include <chrono>
28#include <cstdlib>
Dan Stozac6998d22015-09-24 17:03:36 -070029#include <sstream>
30
Mark Salyzyna5e161b2016-09-29 08:08:05 -070031#include <hardware/hwcomposer.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <utils/Trace.h>
34
Dan Stozac6998d22015-09-24 17:03:36 -070035using namespace std::chrono_literals;
36
37static bool operator==(const hwc_color_t& lhs, const hwc_color_t& rhs) {
38 return lhs.r == rhs.r &&
39 lhs.g == rhs.g &&
40 lhs.b == rhs.b &&
41 lhs.a == rhs.a;
42}
43
44static bool operator==(const hwc_rect_t& lhs, const hwc_rect_t& rhs) {
45 return lhs.left == rhs.left &&
46 lhs.top == rhs.top &&
47 lhs.right == rhs.right &&
48 lhs.bottom == rhs.bottom;
49}
50
51static bool operator==(const hwc_frect_t& lhs, const hwc_frect_t& rhs) {
52 return lhs.left == rhs.left &&
53 lhs.top == rhs.top &&
54 lhs.right == rhs.right &&
55 lhs.bottom == rhs.bottom;
56}
57
58template <typename T>
59static inline bool operator!=(const T& lhs, const T& rhs)
60{
61 return !(lhs == rhs);
62}
63
64static uint8_t getMinorVersion(struct hwc_composer_device_1* device)
65{
66 auto version = device->common.version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
67 return (version >> 16) & 0xF;
68}
69
70template <typename PFN, typename T>
71static hwc2_function_pointer_t asFP(T function)
72{
73 static_assert(std::is_same<PFN, T>::value, "Incompatible function pointer");
74 return reinterpret_cast<hwc2_function_pointer_t>(function);
75}
76
77using namespace HWC2;
78
Michael Wright28f24d02016-07-12 13:30:53 -070079static constexpr Attribute ColorMode = static_cast<Attribute>(6);
Dan Stoza076ac672016-03-14 10:47:53 -070080
Dan Stozac6998d22015-09-24 17:03:36 -070081namespace android {
82
83void HWC2On1Adapter::DisplayContentsDeleter::operator()(
84 hwc_display_contents_1_t* contents)
85{
86 if (contents != nullptr) {
87 for (size_t l = 0; l < contents->numHwLayers; ++l) {
88 auto& layer = contents->hwLayers[l];
89 std::free(const_cast<hwc_rect_t*>(layer.visibleRegionScreen.rects));
Fabien Sanglardec0a9562016-12-13 11:57:33 -080090 std::free(const_cast<hwc_rect_t*>(layer.surfaceDamage.rects));
Dan Stozac6998d22015-09-24 17:03:36 -070091 }
92 }
93 std::free(contents);
94}
95
96class HWC2On1Adapter::Callbacks : public hwc_procs_t {
97 public:
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -070098 explicit Callbacks(HWC2On1Adapter& adapter) : mAdapter(adapter) {
Dan Stozac6998d22015-09-24 17:03:36 -070099 invalidate = &invalidateHook;
100 vsync = &vsyncHook;
101 hotplug = &hotplugHook;
102 }
103
104 static void invalidateHook(const hwc_procs_t* procs) {
105 auto callbacks = static_cast<const Callbacks*>(procs);
106 callbacks->mAdapter.hwc1Invalidate();
107 }
108
109 static void vsyncHook(const hwc_procs_t* procs, int display,
110 int64_t timestamp) {
111 auto callbacks = static_cast<const Callbacks*>(procs);
112 callbacks->mAdapter.hwc1Vsync(display, timestamp);
113 }
114
115 static void hotplugHook(const hwc_procs_t* procs, int display,
116 int connected) {
117 auto callbacks = static_cast<const Callbacks*>(procs);
118 callbacks->mAdapter.hwc1Hotplug(display, connected);
119 }
120
121 private:
122 HWC2On1Adapter& mAdapter;
123};
124
125static int closeHook(hw_device_t* /*device*/)
126{
127 // Do nothing, since the real work is done in the class destructor, but we
128 // need to provide a valid function pointer for hwc2_close to call
129 return 0;
130}
131
132HWC2On1Adapter::HWC2On1Adapter(hwc_composer_device_1_t* hwc1Device)
133 : mDumpString(),
134 mHwc1Device(hwc1Device),
135 mHwc1MinorVersion(getMinorVersion(hwc1Device)),
136 mHwc1SupportsVirtualDisplays(false),
Fabien Sanglardeb3db612016-11-18 16:12:31 -0800137 mHwc1SupportsBackgroundColor(false),
Dan Stozac6998d22015-09-24 17:03:36 -0700138 mHwc1Callbacks(std::make_unique<Callbacks>(*this)),
139 mCapabilities(),
140 mLayers(),
141 mHwc1VirtualDisplay(),
142 mStateMutex(),
143 mCallbacks(),
144 mHasPendingInvalidate(false),
145 mPendingVsyncs(),
146 mPendingHotplugs(),
147 mDisplays(),
148 mHwc1DisplayMap()
149{
150 common.close = closeHook;
151 getCapabilities = getCapabilitiesHook;
152 getFunction = getFunctionHook;
153 populateCapabilities();
154 populatePrimary();
155 mHwc1Device->registerProcs(mHwc1Device,
156 static_cast<const hwc_procs_t*>(mHwc1Callbacks.get()));
157}
158
159HWC2On1Adapter::~HWC2On1Adapter() {
160 hwc_close_1(mHwc1Device);
161}
162
163void HWC2On1Adapter::doGetCapabilities(uint32_t* outCount,
164 int32_t* outCapabilities)
165{
166 if (outCapabilities == nullptr) {
167 *outCount = mCapabilities.size();
168 return;
169 }
170
171 auto capabilityIter = mCapabilities.cbegin();
172 for (size_t written = 0; written < *outCount; ++written) {
173 if (capabilityIter == mCapabilities.cend()) {
174 return;
175 }
176 outCapabilities[written] = static_cast<int32_t>(*capabilityIter);
177 ++capabilityIter;
178 }
179}
180
181hwc2_function_pointer_t HWC2On1Adapter::doGetFunction(
182 FunctionDescriptor descriptor)
183{
184 switch (descriptor) {
185 // Device functions
186 case FunctionDescriptor::CreateVirtualDisplay:
187 return asFP<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
188 createVirtualDisplayHook);
189 case FunctionDescriptor::DestroyVirtualDisplay:
190 return asFP<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
191 destroyVirtualDisplayHook);
192 case FunctionDescriptor::Dump:
193 return asFP<HWC2_PFN_DUMP>(dumpHook);
194 case FunctionDescriptor::GetMaxVirtualDisplayCount:
195 return asFP<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
196 getMaxVirtualDisplayCountHook);
197 case FunctionDescriptor::RegisterCallback:
198 return asFP<HWC2_PFN_REGISTER_CALLBACK>(registerCallbackHook);
199
200 // Display functions
201 case FunctionDescriptor::AcceptDisplayChanges:
202 return asFP<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
203 displayHook<decltype(&Display::acceptChanges),
204 &Display::acceptChanges>);
205 case FunctionDescriptor::CreateLayer:
206 return asFP<HWC2_PFN_CREATE_LAYER>(
207 displayHook<decltype(&Display::createLayer),
208 &Display::createLayer, hwc2_layer_t*>);
209 case FunctionDescriptor::DestroyLayer:
210 return asFP<HWC2_PFN_DESTROY_LAYER>(
211 displayHook<decltype(&Display::destroyLayer),
212 &Display::destroyLayer, hwc2_layer_t>);
213 case FunctionDescriptor::GetActiveConfig:
214 return asFP<HWC2_PFN_GET_ACTIVE_CONFIG>(
215 displayHook<decltype(&Display::getActiveConfig),
216 &Display::getActiveConfig, hwc2_config_t*>);
217 case FunctionDescriptor::GetChangedCompositionTypes:
218 return asFP<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
219 displayHook<decltype(&Display::getChangedCompositionTypes),
220 &Display::getChangedCompositionTypes, uint32_t*,
221 hwc2_layer_t*, int32_t*>);
Dan Stoza076ac672016-03-14 10:47:53 -0700222 case FunctionDescriptor::GetColorModes:
223 return asFP<HWC2_PFN_GET_COLOR_MODES>(
224 displayHook<decltype(&Display::getColorModes),
225 &Display::getColorModes, uint32_t*, int32_t*>);
Dan Stozac6998d22015-09-24 17:03:36 -0700226 case FunctionDescriptor::GetDisplayAttribute:
227 return asFP<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
228 getDisplayAttributeHook);
229 case FunctionDescriptor::GetDisplayConfigs:
230 return asFP<HWC2_PFN_GET_DISPLAY_CONFIGS>(
231 displayHook<decltype(&Display::getConfigs),
232 &Display::getConfigs, uint32_t*, hwc2_config_t*>);
233 case FunctionDescriptor::GetDisplayName:
234 return asFP<HWC2_PFN_GET_DISPLAY_NAME>(
235 displayHook<decltype(&Display::getName),
236 &Display::getName, uint32_t*, char*>);
237 case FunctionDescriptor::GetDisplayRequests:
238 return asFP<HWC2_PFN_GET_DISPLAY_REQUESTS>(
239 displayHook<decltype(&Display::getRequests),
240 &Display::getRequests, int32_t*, uint32_t*, hwc2_layer_t*,
241 int32_t*>);
242 case FunctionDescriptor::GetDisplayType:
243 return asFP<HWC2_PFN_GET_DISPLAY_TYPE>(
244 displayHook<decltype(&Display::getType),
245 &Display::getType, int32_t*>);
246 case FunctionDescriptor::GetDozeSupport:
247 return asFP<HWC2_PFN_GET_DOZE_SUPPORT>(
248 displayHook<decltype(&Display::getDozeSupport),
249 &Display::getDozeSupport, int32_t*>);
Dan Stozaed40eba2016-03-16 12:33:52 -0700250 case FunctionDescriptor::GetHdrCapabilities:
251 return asFP<HWC2_PFN_GET_HDR_CAPABILITIES>(
252 displayHook<decltype(&Display::getHdrCapabilities),
253 &Display::getHdrCapabilities, uint32_t*, int32_t*, float*,
254 float*, float*>);
Dan Stozac6998d22015-09-24 17:03:36 -0700255 case FunctionDescriptor::GetReleaseFences:
256 return asFP<HWC2_PFN_GET_RELEASE_FENCES>(
257 displayHook<decltype(&Display::getReleaseFences),
258 &Display::getReleaseFences, uint32_t*, hwc2_layer_t*,
259 int32_t*>);
260 case FunctionDescriptor::PresentDisplay:
261 return asFP<HWC2_PFN_PRESENT_DISPLAY>(
262 displayHook<decltype(&Display::present),
263 &Display::present, int32_t*>);
264 case FunctionDescriptor::SetActiveConfig:
265 return asFP<HWC2_PFN_SET_ACTIVE_CONFIG>(
266 displayHook<decltype(&Display::setActiveConfig),
267 &Display::setActiveConfig, hwc2_config_t>);
268 case FunctionDescriptor::SetClientTarget:
269 return asFP<HWC2_PFN_SET_CLIENT_TARGET>(
270 displayHook<decltype(&Display::setClientTarget),
271 &Display::setClientTarget, buffer_handle_t, int32_t,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700272 int32_t, hwc_region_t>);
Dan Stoza076ac672016-03-14 10:47:53 -0700273 case FunctionDescriptor::SetColorMode:
Michael Wright28f24d02016-07-12 13:30:53 -0700274 return asFP<HWC2_PFN_SET_COLOR_MODE>(setColorModeHook);
Dan Stoza5df2a862016-03-24 16:19:37 -0700275 case FunctionDescriptor::SetColorTransform:
276 return asFP<HWC2_PFN_SET_COLOR_TRANSFORM>(setColorTransformHook);
Dan Stozac6998d22015-09-24 17:03:36 -0700277 case FunctionDescriptor::SetOutputBuffer:
278 return asFP<HWC2_PFN_SET_OUTPUT_BUFFER>(
279 displayHook<decltype(&Display::setOutputBuffer),
280 &Display::setOutputBuffer, buffer_handle_t, int32_t>);
281 case FunctionDescriptor::SetPowerMode:
282 return asFP<HWC2_PFN_SET_POWER_MODE>(setPowerModeHook);
283 case FunctionDescriptor::SetVsyncEnabled:
284 return asFP<HWC2_PFN_SET_VSYNC_ENABLED>(setVsyncEnabledHook);
285 case FunctionDescriptor::ValidateDisplay:
286 return asFP<HWC2_PFN_VALIDATE_DISPLAY>(
287 displayHook<decltype(&Display::validate),
288 &Display::validate, uint32_t*, uint32_t*>);
289
290 // Layer functions
291 case FunctionDescriptor::SetCursorPosition:
292 return asFP<HWC2_PFN_SET_CURSOR_POSITION>(
293 layerHook<decltype(&Layer::setCursorPosition),
294 &Layer::setCursorPosition, int32_t, int32_t>);
295 case FunctionDescriptor::SetLayerBuffer:
296 return asFP<HWC2_PFN_SET_LAYER_BUFFER>(
297 layerHook<decltype(&Layer::setBuffer), &Layer::setBuffer,
298 buffer_handle_t, int32_t>);
299 case FunctionDescriptor::SetLayerSurfaceDamage:
300 return asFP<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
301 layerHook<decltype(&Layer::setSurfaceDamage),
302 &Layer::setSurfaceDamage, hwc_region_t>);
303
304 // Layer state functions
305 case FunctionDescriptor::SetLayerBlendMode:
306 return asFP<HWC2_PFN_SET_LAYER_BLEND_MODE>(
307 setLayerBlendModeHook);
308 case FunctionDescriptor::SetLayerColor:
309 return asFP<HWC2_PFN_SET_LAYER_COLOR>(
310 layerHook<decltype(&Layer::setColor), &Layer::setColor,
311 hwc_color_t>);
312 case FunctionDescriptor::SetLayerCompositionType:
313 return asFP<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
314 setLayerCompositionTypeHook);
Dan Stoza5df2a862016-03-24 16:19:37 -0700315 case FunctionDescriptor::SetLayerDataspace:
316 return asFP<HWC2_PFN_SET_LAYER_DATASPACE>(setLayerDataspaceHook);
Dan Stozac6998d22015-09-24 17:03:36 -0700317 case FunctionDescriptor::SetLayerDisplayFrame:
318 return asFP<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
319 layerHook<decltype(&Layer::setDisplayFrame),
320 &Layer::setDisplayFrame, hwc_rect_t>);
321 case FunctionDescriptor::SetLayerPlaneAlpha:
322 return asFP<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
323 layerHook<decltype(&Layer::setPlaneAlpha),
324 &Layer::setPlaneAlpha, float>);
325 case FunctionDescriptor::SetLayerSidebandStream:
326 return asFP<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
327 layerHook<decltype(&Layer::setSidebandStream),
328 &Layer::setSidebandStream, const native_handle_t*>);
329 case FunctionDescriptor::SetLayerSourceCrop:
330 return asFP<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
331 layerHook<decltype(&Layer::setSourceCrop),
332 &Layer::setSourceCrop, hwc_frect_t>);
333 case FunctionDescriptor::SetLayerTransform:
334 return asFP<HWC2_PFN_SET_LAYER_TRANSFORM>(setLayerTransformHook);
335 case FunctionDescriptor::SetLayerVisibleRegion:
336 return asFP<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
337 layerHook<decltype(&Layer::setVisibleRegion),
338 &Layer::setVisibleRegion, hwc_region_t>);
339 case FunctionDescriptor::SetLayerZOrder:
340 return asFP<HWC2_PFN_SET_LAYER_Z_ORDER>(setLayerZOrderHook);
341
342 default:
343 ALOGE("doGetFunction: Unknown function descriptor: %d (%s)",
344 static_cast<int32_t>(descriptor),
345 to_string(descriptor).c_str());
346 return nullptr;
347 }
348}
349
350// Device functions
351
352Error HWC2On1Adapter::createVirtualDisplay(uint32_t width,
353 uint32_t height, hwc2_display_t* outDisplay)
354{
Dan Stozafc4e2022016-02-23 11:43:19 -0800355 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700356
357 if (mHwc1VirtualDisplay) {
358 // We have already allocated our only HWC1 virtual display
359 ALOGE("createVirtualDisplay: HWC1 virtual display already allocated");
360 return Error::NoResources;
361 }
362
363 if (MAX_VIRTUAL_DISPLAY_DIMENSION != 0 &&
364 (width > MAX_VIRTUAL_DISPLAY_DIMENSION ||
365 height > MAX_VIRTUAL_DISPLAY_DIMENSION)) {
366 ALOGE("createVirtualDisplay: Can't create a virtual display with"
367 " a dimension > %u (tried %u x %u)",
368 MAX_VIRTUAL_DISPLAY_DIMENSION, width, height);
369 return Error::NoResources;
370 }
371
372 mHwc1VirtualDisplay = std::make_shared<HWC2On1Adapter::Display>(*this,
373 HWC2::DisplayType::Virtual);
374 mHwc1VirtualDisplay->populateConfigs(width, height);
375 const auto displayId = mHwc1VirtualDisplay->getId();
376 mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL] = displayId;
377 mHwc1VirtualDisplay->setHwc1Id(HWC_DISPLAY_VIRTUAL);
378 mDisplays.emplace(displayId, mHwc1VirtualDisplay);
379 *outDisplay = displayId;
380
381 return Error::None;
382}
383
384Error HWC2On1Adapter::destroyVirtualDisplay(hwc2_display_t displayId)
385{
Dan Stozafc4e2022016-02-23 11:43:19 -0800386 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700387
388 if (!mHwc1VirtualDisplay || (mHwc1VirtualDisplay->getId() != displayId)) {
389 return Error::BadDisplay;
390 }
391
392 mHwc1VirtualDisplay.reset();
393 mHwc1DisplayMap.erase(HWC_DISPLAY_VIRTUAL);
394 mDisplays.erase(displayId);
395
396 return Error::None;
397}
398
399void HWC2On1Adapter::dump(uint32_t* outSize, char* outBuffer)
400{
401 if (outBuffer != nullptr) {
402 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
403 *outSize = static_cast<uint32_t>(copiedBytes);
404 return;
405 }
406
407 std::stringstream output;
408
409 output << "-- HWC2On1Adapter --\n";
410
411 output << "Adapting to a HWC 1." << static_cast<int>(mHwc1MinorVersion) <<
412 " device\n";
413
414 // Attempt to acquire the lock for 1 second, but proceed without the lock
415 // after that, so we can still get some information if we're deadlocked
Dan Stozafc4e2022016-02-23 11:43:19 -0800416 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex,
417 std::defer_lock);
Dan Stozac6998d22015-09-24 17:03:36 -0700418 lock.try_lock_for(1s);
419
420 if (mCapabilities.empty()) {
421 output << "Capabilities: None\n";
422 } else {
423 output << "Capabilities:\n";
424 for (auto capability : mCapabilities) {
425 output << " " << to_string(capability) << '\n';
426 }
427 }
428
429 output << "Displays:\n";
430 for (const auto& element : mDisplays) {
431 const auto& display = element.second;
432 output << display->dump();
433 }
434 output << '\n';
435
Dan Stozafc4e2022016-02-23 11:43:19 -0800436 // Release the lock before calling into HWC1, and since we no longer require
437 // mutual exclusion to access mCapabilities or mDisplays
438 lock.unlock();
439
Dan Stozac6998d22015-09-24 17:03:36 -0700440 if (mHwc1Device->dump) {
441 output << "HWC1 dump:\n";
442 std::vector<char> hwc1Dump(4096);
443 // Call with size - 1 to preserve a null character at the end
444 mHwc1Device->dump(mHwc1Device, hwc1Dump.data(),
445 static_cast<int>(hwc1Dump.size() - 1));
446 output << hwc1Dump.data();
447 }
448
449 mDumpString = output.str();
450 *outSize = static_cast<uint32_t>(mDumpString.size());
451}
452
453uint32_t HWC2On1Adapter::getMaxVirtualDisplayCount()
454{
455 return mHwc1SupportsVirtualDisplays ? 1 : 0;
456}
457
458static bool isValid(Callback descriptor) {
459 switch (descriptor) {
460 case Callback::Hotplug: // Fall-through
461 case Callback::Refresh: // Fall-through
462 case Callback::Vsync: return true;
463 default: return false;
464 }
465}
466
467Error HWC2On1Adapter::registerCallback(Callback descriptor,
468 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer)
469{
470 if (!isValid(descriptor)) {
471 return Error::BadParameter;
472 }
473
474 ALOGV("registerCallback(%s, %p, %p)", to_string(descriptor).c_str(),
475 callbackData, pointer);
476
Dan Stozafc4e2022016-02-23 11:43:19 -0800477 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700478
479 mCallbacks[descriptor] = {callbackData, pointer};
480
481 bool hasPendingInvalidate = false;
482 std::vector<hwc2_display_t> displayIds;
483 std::vector<std::pair<hwc2_display_t, int64_t>> pendingVsyncs;
484 std::vector<std::pair<hwc2_display_t, int>> pendingHotplugs;
485
486 if (descriptor == Callback::Refresh) {
487 hasPendingInvalidate = mHasPendingInvalidate;
488 if (hasPendingInvalidate) {
489 for (auto& displayPair : mDisplays) {
490 displayIds.emplace_back(displayPair.first);
491 }
492 }
493 mHasPendingInvalidate = false;
494 } else if (descriptor == Callback::Vsync) {
495 for (auto pending : mPendingVsyncs) {
496 auto hwc1DisplayId = pending.first;
497 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
498 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d",
499 hwc1DisplayId);
500 continue;
501 }
502 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
503 auto timestamp = pending.second;
504 pendingVsyncs.emplace_back(displayId, timestamp);
505 }
506 mPendingVsyncs.clear();
507 } else if (descriptor == Callback::Hotplug) {
508 // Hotplug the primary display
509 pendingHotplugs.emplace_back(mHwc1DisplayMap[HWC_DISPLAY_PRIMARY],
510 static_cast<int32_t>(Connection::Connected));
511
512 for (auto pending : mPendingHotplugs) {
513 auto hwc1DisplayId = pending.first;
514 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
515 ALOGE("hwc1Hotplug: Couldn't find display for HWC1 id %d",
516 hwc1DisplayId);
517 continue;
518 }
519 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
520 auto connected = pending.second;
521 pendingHotplugs.emplace_back(displayId, connected);
522 }
523 }
524
525 // Call pending callbacks without the state lock held
526 lock.unlock();
527
528 if (hasPendingInvalidate) {
529 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(pointer);
530 for (auto displayId : displayIds) {
531 refresh(callbackData, displayId);
532 }
533 }
534 if (!pendingVsyncs.empty()) {
535 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(pointer);
536 for (auto& pendingVsync : pendingVsyncs) {
537 vsync(callbackData, pendingVsync.first, pendingVsync.second);
538 }
539 }
540 if (!pendingHotplugs.empty()) {
541 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(pointer);
542 for (auto& pendingHotplug : pendingHotplugs) {
543 hotplug(callbackData, pendingHotplug.first, pendingHotplug.second);
544 }
545 }
546 return Error::None;
547}
548
549// Display functions
550
551std::atomic<hwc2_display_t> HWC2On1Adapter::Display::sNextId(1);
552
553HWC2On1Adapter::Display::Display(HWC2On1Adapter& device, HWC2::DisplayType type)
554 : mId(sNextId++),
555 mDevice(device),
556 mDirtyCount(0),
557 mStateMutex(),
558 mZIsDirty(false),
559 mHwc1RequestedContents(nullptr),
560 mHwc1ReceivedContents(nullptr),
561 mRetireFence(),
562 mChanges(),
563 mHwc1Id(-1),
564 mConfigs(),
565 mActiveConfig(nullptr),
Michael Wrightc75ca512016-07-20 21:34:48 +0100566 mActiveColorMode(static_cast<android_color_mode_t>(-1)),
Dan Stozac6998d22015-09-24 17:03:36 -0700567 mName(),
568 mType(type),
569 mPowerMode(PowerMode::Off),
570 mVsyncEnabled(Vsync::Invalid),
571 mClientTarget(),
572 mOutputBuffer(),
Dan Stoza5df2a862016-03-24 16:19:37 -0700573 mHasColorTransform(false),
Dan Stozafc4e2022016-02-23 11:43:19 -0800574 mLayers(),
575 mHwc1LayerMap() {}
Dan Stozac6998d22015-09-24 17:03:36 -0700576
577Error HWC2On1Adapter::Display::acceptChanges()
578{
579 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
580
581 if (!mChanges) {
582 ALOGV("[%" PRIu64 "] acceptChanges failed, not validated", mId);
583 return Error::NotValidated;
584 }
585
586 ALOGV("[%" PRIu64 "] acceptChanges", mId);
587
588 for (auto& change : mChanges->getTypeChanges()) {
589 auto layerId = change.first;
590 auto type = change.second;
591 auto layer = mDevice.mLayers[layerId];
592 layer->setCompositionType(type);
593 }
594
595 mChanges->clearTypeChanges();
596
597 mHwc1RequestedContents = std::move(mHwc1ReceivedContents);
598
599 return Error::None;
600}
601
602Error HWC2On1Adapter::Display::createLayer(hwc2_layer_t* outLayerId)
603{
604 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
605
606 auto layer = *mLayers.emplace(std::make_shared<Layer>(*this));
607 mDevice.mLayers.emplace(std::make_pair(layer->getId(), layer));
608 *outLayerId = layer->getId();
609 ALOGV("[%" PRIu64 "] created layer %" PRIu64, mId, *outLayerId);
610 return Error::None;
611}
612
613Error HWC2On1Adapter::Display::destroyLayer(hwc2_layer_t layerId)
614{
615 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
616
617 const auto mapLayer = mDevice.mLayers.find(layerId);
618 if (mapLayer == mDevice.mLayers.end()) {
619 ALOGV("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer",
620 mId, layerId);
621 return Error::BadLayer;
622 }
623 const auto layer = mapLayer->second;
624 mDevice.mLayers.erase(mapLayer);
625 const auto zRange = mLayers.equal_range(layer);
626 for (auto current = zRange.first; current != zRange.second; ++current) {
627 if (**current == *layer) {
628 current = mLayers.erase(current);
629 break;
630 }
631 }
632 ALOGV("[%" PRIu64 "] destroyed layer %" PRIu64, mId, layerId);
633 return Error::None;
634}
635
636Error HWC2On1Adapter::Display::getActiveConfig(hwc2_config_t* outConfig)
637{
638 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
639
640 if (!mActiveConfig) {
641 ALOGV("[%" PRIu64 "] getActiveConfig --> %s", mId,
642 to_string(Error::BadConfig).c_str());
643 return Error::BadConfig;
644 }
645 auto configId = mActiveConfig->getId();
646 ALOGV("[%" PRIu64 "] getActiveConfig --> %u", mId, configId);
647 *outConfig = configId;
648 return Error::None;
649}
650
651Error HWC2On1Adapter::Display::getAttribute(hwc2_config_t configId,
652 Attribute attribute, int32_t* outValue)
653{
654 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
655
656 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
657 ALOGV("[%" PRIu64 "] getAttribute failed: bad config (%u)", mId,
658 configId);
659 return Error::BadConfig;
660 }
661 *outValue = mConfigs[configId]->getAttribute(attribute);
662 ALOGV("[%" PRIu64 "] getAttribute(%u, %s) --> %d", mId, configId,
663 to_string(attribute).c_str(), *outValue);
664 return Error::None;
665}
666
667Error HWC2On1Adapter::Display::getChangedCompositionTypes(
668 uint32_t* outNumElements, hwc2_layer_t* outLayers, int32_t* outTypes)
669{
670 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
671
672 if (!mChanges) {
673 ALOGE("[%" PRIu64 "] getChangedCompositionTypes failed: not validated",
674 mId);
675 return Error::NotValidated;
676 }
677
678 if ((outLayers == nullptr) || (outTypes == nullptr)) {
679 *outNumElements = mChanges->getTypeChanges().size();
680 return Error::None;
681 }
682
683 uint32_t numWritten = 0;
684 for (const auto& element : mChanges->getTypeChanges()) {
685 if (numWritten == *outNumElements) {
686 break;
687 }
688 auto layerId = element.first;
689 auto intType = static_cast<int32_t>(element.second);
690 ALOGV("Adding %" PRIu64 " %s", layerId,
691 to_string(element.second).c_str());
692 outLayers[numWritten] = layerId;
693 outTypes[numWritten] = intType;
694 ++numWritten;
695 }
696 *outNumElements = numWritten;
697
698 return Error::None;
699}
700
Dan Stoza076ac672016-03-14 10:47:53 -0700701Error HWC2On1Adapter::Display::getColorModes(uint32_t* outNumModes,
702 int32_t* outModes)
703{
704 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
705
706 if (!outModes) {
707 *outNumModes = mColorModes.size();
708 return Error::None;
709 }
710 uint32_t numModes = std::min(*outNumModes,
711 static_cast<uint32_t>(mColorModes.size()));
712 std::copy_n(mColorModes.cbegin(), numModes, outModes);
713 *outNumModes = numModes;
714 return Error::None;
715}
716
Dan Stozac6998d22015-09-24 17:03:36 -0700717Error HWC2On1Adapter::Display::getConfigs(uint32_t* outNumConfigs,
718 hwc2_config_t* outConfigs)
719{
720 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
721
722 if (!outConfigs) {
723 *outNumConfigs = mConfigs.size();
724 return Error::None;
725 }
726 uint32_t numWritten = 0;
727 for (const auto& config : mConfigs) {
728 if (numWritten == *outNumConfigs) {
729 break;
730 }
731 outConfigs[numWritten] = config->getId();
732 ++numWritten;
733 }
734 *outNumConfigs = numWritten;
735 return Error::None;
736}
737
738Error HWC2On1Adapter::Display::getDozeSupport(int32_t* outSupport)
739{
740 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
741
742 if (mDevice.mHwc1MinorVersion < 4 || mHwc1Id != 0) {
743 *outSupport = 0;
744 } else {
745 *outSupport = 1;
746 }
747 return Error::None;
748}
749
Dan Stozaed40eba2016-03-16 12:33:52 -0700750Error HWC2On1Adapter::Display::getHdrCapabilities(uint32_t* outNumTypes,
751 int32_t* /*outTypes*/, float* /*outMaxLuminance*/,
752 float* /*outMaxAverageLuminance*/, float* /*outMinLuminance*/)
753{
754 // This isn't supported on HWC1, so per the HWC2 header, return numTypes = 0
755 *outNumTypes = 0;
756 return Error::None;
757}
758
Dan Stozac6998d22015-09-24 17:03:36 -0700759Error HWC2On1Adapter::Display::getName(uint32_t* outSize, char* outName)
760{
761 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
762
763 if (!outName) {
764 *outSize = mName.size();
765 return Error::None;
766 }
767 auto numCopied = mName.copy(outName, *outSize);
768 *outSize = numCopied;
769 return Error::None;
770}
771
772Error HWC2On1Adapter::Display::getReleaseFences(uint32_t* outNumElements,
773 hwc2_layer_t* outLayers, int32_t* outFences)
774{
775 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
776
777 uint32_t numWritten = 0;
778 bool outputsNonNull = (outLayers != nullptr) && (outFences != nullptr);
779 for (const auto& layer : mLayers) {
780 if (outputsNonNull && (numWritten == *outNumElements)) {
781 break;
782 }
783
784 auto releaseFence = layer->getReleaseFence();
785 if (releaseFence != Fence::NO_FENCE) {
786 if (outputsNonNull) {
787 outLayers[numWritten] = layer->getId();
788 outFences[numWritten] = releaseFence->dup();
789 }
790 ++numWritten;
791 }
792 }
793 *outNumElements = numWritten;
794
795 return Error::None;
796}
797
798Error HWC2On1Adapter::Display::getRequests(int32_t* outDisplayRequests,
799 uint32_t* outNumElements, hwc2_layer_t* outLayers,
800 int32_t* outLayerRequests)
801{
802 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
803
804 if (!mChanges) {
805 return Error::NotValidated;
806 }
807
808 if (outLayers == nullptr || outLayerRequests == nullptr) {
809 *outNumElements = mChanges->getNumLayerRequests();
810 return Error::None;
811 }
812
Fabien Sanglard601938c2016-11-29 11:10:40 -0800813 // Display requests (HWC2::DisplayRequest) are not supported by hwc1:
814 // A hwc1 has always zero requests for the client.
815 *outDisplayRequests = 0;
816
Dan Stozac6998d22015-09-24 17:03:36 -0700817 uint32_t numWritten = 0;
818 for (const auto& request : mChanges->getLayerRequests()) {
819 if (numWritten == *outNumElements) {
820 break;
821 }
822 outLayers[numWritten] = request.first;
823 outLayerRequests[numWritten] = static_cast<int32_t>(request.second);
824 ++numWritten;
825 }
826
827 return Error::None;
828}
829
830Error HWC2On1Adapter::Display::getType(int32_t* outType)
831{
832 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
833
834 *outType = static_cast<int32_t>(mType);
835 return Error::None;
836}
837
838Error HWC2On1Adapter::Display::present(int32_t* outRetireFence)
839{
840 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
841
842 if (mChanges) {
843 Error error = mDevice.setAllDisplays();
844 if (error != Error::None) {
845 ALOGE("[%" PRIu64 "] present: setAllDisplaysFailed (%s)", mId,
846 to_string(error).c_str());
847 return error;
848 }
849 }
850
851 *outRetireFence = mRetireFence.get()->dup();
852 ALOGV("[%" PRIu64 "] present returning retire fence %d", mId,
853 *outRetireFence);
854
855 return Error::None;
856}
857
858Error HWC2On1Adapter::Display::setActiveConfig(hwc2_config_t configId)
859{
860 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
861
862 auto config = getConfig(configId);
863 if (!config) {
864 return Error::BadConfig;
865 }
Dan Stoza076ac672016-03-14 10:47:53 -0700866 if (config == mActiveConfig) {
867 return Error::None;
Dan Stozac6998d22015-09-24 17:03:36 -0700868 }
Dan Stoza076ac672016-03-14 10:47:53 -0700869
870 if (mDevice.mHwc1MinorVersion >= 4) {
871 uint32_t hwc1Id = 0;
872 auto error = config->getHwc1IdForColorMode(mActiveColorMode, &hwc1Id);
873 if (error != Error::None) {
874 return error;
875 }
876
877 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
878 mHwc1Id, static_cast<int>(hwc1Id));
879 if (intError != 0) {
880 ALOGE("setActiveConfig: Failed to set active config on HWC1 (%d)",
881 intError);
882 return Error::BadConfig;
883 }
884 mActiveConfig = config;
885 }
886
Dan Stozac6998d22015-09-24 17:03:36 -0700887 return Error::None;
888}
889
890Error HWC2On1Adapter::Display::setClientTarget(buffer_handle_t target,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700891 int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/)
Dan Stozac6998d22015-09-24 17:03:36 -0700892{
893 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
894
895 ALOGV("[%" PRIu64 "] setClientTarget(%p, %d)", mId, target, acquireFence);
896 mClientTarget.setBuffer(target);
897 mClientTarget.setFence(acquireFence);
Dan Stoza5cf424b2016-05-20 14:02:39 -0700898 // dataspace and damage can't be used by HWC1, so ignore them
Dan Stozac6998d22015-09-24 17:03:36 -0700899 return Error::None;
900}
901
Michael Wright28f24d02016-07-12 13:30:53 -0700902Error HWC2On1Adapter::Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700903{
904 std::unique_lock<std::recursive_mutex> lock (mStateMutex);
905
906 ALOGV("[%" PRIu64 "] setColorMode(%d)", mId, mode);
907
908 if (mode == mActiveColorMode) {
909 return Error::None;
910 }
911 if (mColorModes.count(mode) == 0) {
912 ALOGE("[%" PRIu64 "] Mode %d not found in mColorModes", mId, mode);
913 return Error::Unsupported;
914 }
915
916 uint32_t hwc1Config = 0;
917 auto error = mActiveConfig->getHwc1IdForColorMode(mode, &hwc1Config);
918 if (error != Error::None) {
919 return error;
920 }
921
922 ALOGV("[%" PRIu64 "] Setting HWC1 config %u", mId, hwc1Config);
923 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
924 mHwc1Id, hwc1Config);
925 if (intError != 0) {
926 ALOGE("[%" PRIu64 "] Failed to set HWC1 config (%d)", mId, intError);
927 return Error::Unsupported;
928 }
929
930 mActiveColorMode = mode;
931 return Error::None;
932}
933
Dan Stoza5df2a862016-03-24 16:19:37 -0700934Error HWC2On1Adapter::Display::setColorTransform(android_color_transform_t hint)
935{
936 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
937
938 ALOGV("%" PRIu64 "] setColorTransform(%d)", mId,
939 static_cast<int32_t>(hint));
940 mHasColorTransform = (hint != HAL_COLOR_TRANSFORM_IDENTITY);
941 return Error::None;
942}
943
Dan Stozac6998d22015-09-24 17:03:36 -0700944Error HWC2On1Adapter::Display::setOutputBuffer(buffer_handle_t buffer,
945 int32_t releaseFence)
946{
947 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
948
949 ALOGV("[%" PRIu64 "] setOutputBuffer(%p, %d)", mId, buffer, releaseFence);
950 mOutputBuffer.setBuffer(buffer);
951 mOutputBuffer.setFence(releaseFence);
952 return Error::None;
953}
954
955static bool isValid(PowerMode mode)
956{
957 switch (mode) {
958 case PowerMode::Off: // Fall-through
959 case PowerMode::DozeSuspend: // Fall-through
960 case PowerMode::Doze: // Fall-through
961 case PowerMode::On: return true;
962 default: return false;
963 }
964}
965
966static int getHwc1PowerMode(PowerMode mode)
967{
968 switch (mode) {
969 case PowerMode::Off: return HWC_POWER_MODE_OFF;
970 case PowerMode::DozeSuspend: return HWC_POWER_MODE_DOZE_SUSPEND;
971 case PowerMode::Doze: return HWC_POWER_MODE_DOZE;
972 case PowerMode::On: return HWC_POWER_MODE_NORMAL;
973 default: return HWC_POWER_MODE_OFF;
974 }
975}
976
977Error HWC2On1Adapter::Display::setPowerMode(PowerMode mode)
978{
979 if (!isValid(mode)) {
980 return Error::BadParameter;
981 }
982 if (mode == mPowerMode) {
983 return Error::None;
984 }
985
986 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
987
988 int error = 0;
989 if (mDevice.mHwc1MinorVersion < 4) {
990 error = mDevice.mHwc1Device->blank(mDevice.mHwc1Device, mHwc1Id,
991 mode == PowerMode::Off);
992 } else {
993 error = mDevice.mHwc1Device->setPowerMode(mDevice.mHwc1Device,
994 mHwc1Id, getHwc1PowerMode(mode));
995 }
996 ALOGE_IF(error != 0, "setPowerMode: Failed to set power mode on HWC1 (%d)",
997 error);
998
999 ALOGV("[%" PRIu64 "] setPowerMode(%s)", mId, to_string(mode).c_str());
1000 mPowerMode = mode;
1001 return Error::None;
1002}
1003
1004static bool isValid(Vsync enable) {
1005 switch (enable) {
1006 case Vsync::Enable: // Fall-through
1007 case Vsync::Disable: return true;
1008 default: return false;
1009 }
1010}
1011
1012Error HWC2On1Adapter::Display::setVsyncEnabled(Vsync enable)
1013{
1014 if (!isValid(enable)) {
1015 return Error::BadParameter;
1016 }
1017 if (enable == mVsyncEnabled) {
1018 return Error::None;
1019 }
1020
1021 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1022
1023 int error = mDevice.mHwc1Device->eventControl(mDevice.mHwc1Device,
1024 mHwc1Id, HWC_EVENT_VSYNC, enable == Vsync::Enable);
1025 ALOGE_IF(error != 0, "setVsyncEnabled: Failed to set vsync on HWC1 (%d)",
1026 error);
1027
1028 mVsyncEnabled = enable;
1029 return Error::None;
1030}
1031
1032Error HWC2On1Adapter::Display::validate(uint32_t* outNumTypes,
1033 uint32_t* outNumRequests)
1034{
1035 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1036
1037 ALOGV("[%" PRIu64 "] Entering validate", mId);
1038
1039 if (!mChanges) {
1040 if (!mDevice.prepareAllDisplays()) {
1041 return Error::BadDisplay;
1042 }
1043 }
1044
1045 *outNumTypes = mChanges->getNumTypes();
1046 *outNumRequests = mChanges->getNumLayerRequests();
1047 ALOGV("[%" PRIu64 "] validate --> %u types, %u requests", mId, *outNumTypes,
1048 *outNumRequests);
1049 for (auto request : mChanges->getTypeChanges()) {
1050 ALOGV("Layer %" PRIu64 " --> %s", request.first,
1051 to_string(request.second).c_str());
1052 }
1053 return *outNumTypes > 0 ? Error::HasChanges : Error::None;
1054}
1055
1056// Display helpers
1057
1058Error HWC2On1Adapter::Display::updateLayerZ(hwc2_layer_t layerId, uint32_t z)
1059{
1060 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1061
1062 const auto mapLayer = mDevice.mLayers.find(layerId);
1063 if (mapLayer == mDevice.mLayers.end()) {
1064 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer", mId);
1065 return Error::BadLayer;
1066 }
1067
1068 const auto layer = mapLayer->second;
1069 const auto zRange = mLayers.equal_range(layer);
1070 bool layerOnDisplay = false;
1071 for (auto current = zRange.first; current != zRange.second; ++current) {
1072 if (**current == *layer) {
1073 if ((*current)->getZ() == z) {
1074 // Don't change anything if the Z hasn't changed
1075 return Error::None;
1076 }
1077 current = mLayers.erase(current);
1078 layerOnDisplay = true;
1079 break;
1080 }
1081 }
1082
1083 if (!layerOnDisplay) {
1084 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display",
1085 mId);
1086 return Error::BadLayer;
1087 }
1088
1089 layer->setZ(z);
1090 mLayers.emplace(std::move(layer));
1091 mZIsDirty = true;
1092
1093 return Error::None;
1094}
1095
Dan Stoza076ac672016-03-14 10:47:53 -07001096static constexpr uint32_t ATTRIBUTES_WITH_COLOR[] = {
1097 HWC_DISPLAY_VSYNC_PERIOD,
1098 HWC_DISPLAY_WIDTH,
1099 HWC_DISPLAY_HEIGHT,
1100 HWC_DISPLAY_DPI_X,
1101 HWC_DISPLAY_DPI_Y,
1102 HWC_DISPLAY_COLOR_TRANSFORM,
1103 HWC_DISPLAY_NO_ATTRIBUTE,
1104};
1105
1106static constexpr uint32_t ATTRIBUTES_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001107 HWC_DISPLAY_VSYNC_PERIOD,
1108 HWC_DISPLAY_WIDTH,
1109 HWC_DISPLAY_HEIGHT,
1110 HWC_DISPLAY_DPI_X,
1111 HWC_DISPLAY_DPI_Y,
1112 HWC_DISPLAY_NO_ATTRIBUTE,
1113};
Dan Stozac6998d22015-09-24 17:03:36 -07001114
Dan Stoza076ac672016-03-14 10:47:53 -07001115static constexpr size_t NUM_ATTRIBUTES_WITH_COLOR =
1116 sizeof(ATTRIBUTES_WITH_COLOR) / sizeof(uint32_t);
1117static_assert(sizeof(ATTRIBUTES_WITH_COLOR) > sizeof(ATTRIBUTES_WITHOUT_COLOR),
1118 "Attribute tables have unexpected sizes");
1119
1120static constexpr uint32_t ATTRIBUTE_MAP_WITH_COLOR[] = {
1121 6, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1122 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1123 1, // HWC_DISPLAY_WIDTH = 2,
1124 2, // HWC_DISPLAY_HEIGHT = 3,
1125 3, // HWC_DISPLAY_DPI_X = 4,
1126 4, // HWC_DISPLAY_DPI_Y = 5,
1127 5, // HWC_DISPLAY_COLOR_TRANSFORM = 6,
1128};
1129
1130static constexpr uint32_t ATTRIBUTE_MAP_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001131 5, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1132 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1133 1, // HWC_DISPLAY_WIDTH = 2,
1134 2, // HWC_DISPLAY_HEIGHT = 3,
1135 3, // HWC_DISPLAY_DPI_X = 4,
1136 4, // HWC_DISPLAY_DPI_Y = 5,
1137};
1138
1139template <uint32_t attribute>
1140static constexpr bool attributesMatch()
1141{
Dan Stoza076ac672016-03-14 10:47:53 -07001142 bool match = (attribute ==
1143 ATTRIBUTES_WITH_COLOR[ATTRIBUTE_MAP_WITH_COLOR[attribute]]);
1144 if (attribute == HWC_DISPLAY_COLOR_TRANSFORM) {
1145 return match;
1146 }
1147
1148 return match && (attribute ==
1149 ATTRIBUTES_WITHOUT_COLOR[ATTRIBUTE_MAP_WITHOUT_COLOR[attribute]]);
Dan Stozac6998d22015-09-24 17:03:36 -07001150}
1151static_assert(attributesMatch<HWC_DISPLAY_VSYNC_PERIOD>(),
1152 "Tables out of sync");
1153static_assert(attributesMatch<HWC_DISPLAY_WIDTH>(), "Tables out of sync");
1154static_assert(attributesMatch<HWC_DISPLAY_HEIGHT>(), "Tables out of sync");
1155static_assert(attributesMatch<HWC_DISPLAY_DPI_X>(), "Tables out of sync");
1156static_assert(attributesMatch<HWC_DISPLAY_DPI_Y>(), "Tables out of sync");
Dan Stoza076ac672016-03-14 10:47:53 -07001157static_assert(attributesMatch<HWC_DISPLAY_COLOR_TRANSFORM>(),
1158 "Tables out of sync");
Dan Stozac6998d22015-09-24 17:03:36 -07001159
1160void HWC2On1Adapter::Display::populateConfigs()
1161{
1162 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1163
1164 ALOGV("[%" PRIu64 "] populateConfigs", mId);
1165
1166 if (mHwc1Id == -1) {
1167 ALOGE("populateConfigs: HWC1 ID not set");
1168 return;
1169 }
1170
1171 const size_t MAX_NUM_CONFIGS = 128;
1172 uint32_t configs[MAX_NUM_CONFIGS] = {};
1173 size_t numConfigs = MAX_NUM_CONFIGS;
1174 mDevice.mHwc1Device->getDisplayConfigs(mDevice.mHwc1Device, mHwc1Id,
1175 configs, &numConfigs);
1176
1177 for (size_t c = 0; c < numConfigs; ++c) {
1178 uint32_t hwc1ConfigId = configs[c];
Dan Stoza076ac672016-03-14 10:47:53 -07001179 auto newConfig = std::make_shared<Config>(*this);
Dan Stozac6998d22015-09-24 17:03:36 -07001180
Dan Stoza076ac672016-03-14 10:47:53 -07001181 int32_t values[NUM_ATTRIBUTES_WITH_COLOR] = {};
1182 bool hasColor = true;
1183 auto result = mDevice.mHwc1Device->getDisplayAttributes(
1184 mDevice.mHwc1Device, mHwc1Id, hwc1ConfigId,
1185 ATTRIBUTES_WITH_COLOR, values);
1186 if (result != 0) {
1187 mDevice.mHwc1Device->getDisplayAttributes(mDevice.mHwc1Device,
1188 mHwc1Id, hwc1ConfigId, ATTRIBUTES_WITHOUT_COLOR, values);
1189 hasColor = false;
Dan Stozac6998d22015-09-24 17:03:36 -07001190 }
Dan Stoza076ac672016-03-14 10:47:53 -07001191
1192 auto attributeMap = hasColor ?
1193 ATTRIBUTE_MAP_WITH_COLOR : ATTRIBUTE_MAP_WITHOUT_COLOR;
1194
1195 newConfig->setAttribute(Attribute::VsyncPeriod,
1196 values[attributeMap[HWC_DISPLAY_VSYNC_PERIOD]]);
1197 newConfig->setAttribute(Attribute::Width,
1198 values[attributeMap[HWC_DISPLAY_WIDTH]]);
1199 newConfig->setAttribute(Attribute::Height,
1200 values[attributeMap[HWC_DISPLAY_HEIGHT]]);
1201 newConfig->setAttribute(Attribute::DpiX,
1202 values[attributeMap[HWC_DISPLAY_DPI_X]]);
1203 newConfig->setAttribute(Attribute::DpiY,
1204 values[attributeMap[HWC_DISPLAY_DPI_Y]]);
1205 if (hasColor) {
Michael Wright28f24d02016-07-12 13:30:53 -07001206 // In HWC1, color modes are referred to as color transforms. To avoid confusion with
1207 // the HWC2 concept of color transforms, we internally refer to them as color modes for
1208 // both HWC1 and 2.
1209 newConfig->setAttribute(ColorMode,
Dan Stoza076ac672016-03-14 10:47:53 -07001210 values[attributeMap[HWC_DISPLAY_COLOR_TRANSFORM]]);
1211 }
1212
Michael Wright28f24d02016-07-12 13:30:53 -07001213 // We can only do this after attempting to read the color mode
Dan Stoza076ac672016-03-14 10:47:53 -07001214 newConfig->setHwc1Id(hwc1ConfigId);
1215
1216 for (auto& existingConfig : mConfigs) {
1217 if (existingConfig->merge(*newConfig)) {
1218 ALOGV("Merged config %d with existing config %u: %s",
1219 hwc1ConfigId, existingConfig->getId(),
1220 existingConfig->toString().c_str());
1221 newConfig.reset();
1222 break;
1223 }
1224 }
1225
1226 // If it wasn't merged with any existing config, add it to the end
1227 if (newConfig) {
1228 newConfig->setId(static_cast<hwc2_config_t>(mConfigs.size()));
1229 ALOGV("Found new config %u: %s", newConfig->getId(),
1230 newConfig->toString().c_str());
1231 mConfigs.emplace_back(std::move(newConfig));
1232 }
Dan Stozac6998d22015-09-24 17:03:36 -07001233 }
Dan Stoza076ac672016-03-14 10:47:53 -07001234
1235 initializeActiveConfig();
1236 populateColorModes();
Dan Stozac6998d22015-09-24 17:03:36 -07001237}
1238
1239void HWC2On1Adapter::Display::populateConfigs(uint32_t width, uint32_t height)
1240{
1241 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1242
Dan Stoza076ac672016-03-14 10:47:53 -07001243 mConfigs.emplace_back(std::make_shared<Config>(*this));
Dan Stozac6998d22015-09-24 17:03:36 -07001244 auto& config = mConfigs[0];
1245
1246 config->setAttribute(Attribute::Width, static_cast<int32_t>(width));
1247 config->setAttribute(Attribute::Height, static_cast<int32_t>(height));
Dan Stoza076ac672016-03-14 10:47:53 -07001248 config->setHwc1Id(0);
1249 config->setId(0);
Dan Stozac6998d22015-09-24 17:03:36 -07001250 mActiveConfig = config;
1251}
1252
1253bool HWC2On1Adapter::Display::prepare()
1254{
1255 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1256
1257 // Only prepare display contents for displays HWC1 knows about
1258 if (mHwc1Id == -1) {
1259 return true;
1260 }
1261
1262 // It doesn't make sense to prepare a display for which there is no active
1263 // config, so return early
1264 if (!mActiveConfig) {
1265 ALOGE("[%" PRIu64 "] Attempted to prepare, but no config active", mId);
1266 return false;
1267 }
1268
1269 ALOGV("[%" PRIu64 "] Entering prepare", mId);
1270
1271 auto currentCount = mHwc1RequestedContents ?
1272 mHwc1RequestedContents->numHwLayers : 0;
1273 auto requiredCount = mLayers.size() + 1;
1274 ALOGV("[%" PRIu64 "] Requires %zd layers, %zd allocated in %p", mId,
1275 requiredCount, currentCount, mHwc1RequestedContents.get());
1276
1277 bool layerCountChanged = (currentCount != requiredCount);
1278 if (layerCountChanged) {
1279 reallocateHwc1Contents();
1280 }
1281
1282 bool applyAllState = false;
1283 if (layerCountChanged || mZIsDirty) {
1284 assignHwc1LayerIds();
1285 mZIsDirty = false;
1286 applyAllState = true;
1287 }
1288
1289 mHwc1RequestedContents->retireFenceFd = -1;
1290 mHwc1RequestedContents->flags = 0;
1291 if (isDirty() || applyAllState) {
1292 mHwc1RequestedContents->flags |= HWC_GEOMETRY_CHANGED;
1293 }
1294
1295 for (auto& layer : mLayers) {
1296 auto& hwc1Layer = mHwc1RequestedContents->hwLayers[layer->getHwc1Id()];
1297 hwc1Layer.releaseFenceFd = -1;
1298 layer->applyState(hwc1Layer, applyAllState);
1299 }
1300
1301 mHwc1RequestedContents->outbuf = mOutputBuffer.getBuffer();
1302 mHwc1RequestedContents->outbufAcquireFenceFd = mOutputBuffer.getFence();
1303
1304 prepareFramebufferTarget();
1305
1306 return true;
1307}
1308
1309static void cloneHWCRegion(hwc_region_t& region)
1310{
1311 auto size = sizeof(hwc_rect_t) * region.numRects;
1312 auto newRects = static_cast<hwc_rect_t*>(std::malloc(size));
1313 std::copy_n(region.rects, region.numRects, newRects);
1314 region.rects = newRects;
1315}
1316
1317HWC2On1Adapter::Display::HWC1Contents
1318 HWC2On1Adapter::Display::cloneRequestedContents() const
1319{
1320 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1321
1322 size_t size = sizeof(hwc_display_contents_1_t) +
1323 sizeof(hwc_layer_1_t) * (mHwc1RequestedContents->numHwLayers);
1324 auto contents = static_cast<hwc_display_contents_1_t*>(std::malloc(size));
1325 std::memcpy(contents, mHwc1RequestedContents.get(), size);
1326 for (size_t layerId = 0; layerId < contents->numHwLayers; ++layerId) {
1327 auto& layer = contents->hwLayers[layerId];
1328 // Deep copy the regions to avoid double-frees
1329 cloneHWCRegion(layer.visibleRegionScreen);
1330 cloneHWCRegion(layer.surfaceDamage);
1331 }
1332 return HWC1Contents(contents);
1333}
1334
1335void HWC2On1Adapter::Display::setReceivedContents(HWC1Contents contents)
1336{
1337 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1338
1339 mHwc1ReceivedContents = std::move(contents);
1340
1341 mChanges.reset(new Changes);
1342
1343 size_t numLayers = mHwc1ReceivedContents->numHwLayers;
1344 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1345 const auto& receivedLayer = mHwc1ReceivedContents->hwLayers[hwc1Id];
1346 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1347 ALOGE_IF(receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET,
1348 "setReceivedContents: HWC1 layer %zd doesn't have a"
1349 " matching HWC2 layer, and isn't the framebuffer target",
1350 hwc1Id);
1351 continue;
1352 }
1353
1354 Layer& layer = *mHwc1LayerMap[hwc1Id];
1355 updateTypeChanges(receivedLayer, layer);
1356 updateLayerRequests(receivedLayer, layer);
1357 }
1358}
1359
1360bool HWC2On1Adapter::Display::hasChanges() const
1361{
1362 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1363 return mChanges != nullptr;
1364}
1365
1366Error HWC2On1Adapter::Display::set(hwc_display_contents_1& hwcContents)
1367{
1368 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1369
1370 if (!mChanges || (mChanges->getNumTypes() > 0)) {
1371 ALOGE("[%" PRIu64 "] set failed: not validated", mId);
1372 return Error::NotValidated;
1373 }
1374
1375 // Set up the client/framebuffer target
1376 auto numLayers = hwcContents.numHwLayers;
1377
1378 // Close acquire fences on FRAMEBUFFER layers, since they will not be used
1379 // by HWC
1380 for (size_t l = 0; l < numLayers - 1; ++l) {
1381 auto& layer = hwcContents.hwLayers[l];
1382 if (layer.compositionType == HWC_FRAMEBUFFER) {
1383 ALOGV("Closing fence %d for layer %zd", layer.acquireFenceFd, l);
1384 close(layer.acquireFenceFd);
1385 layer.acquireFenceFd = -1;
1386 }
1387 }
1388
1389 auto& clientTargetLayer = hwcContents.hwLayers[numLayers - 1];
1390 if (clientTargetLayer.compositionType == HWC_FRAMEBUFFER_TARGET) {
1391 clientTargetLayer.handle = mClientTarget.getBuffer();
1392 clientTargetLayer.acquireFenceFd = mClientTarget.getFence();
1393 } else {
1394 ALOGE("[%" PRIu64 "] set: last HWC layer wasn't FRAMEBUFFER_TARGET",
1395 mId);
1396 }
1397
1398 mChanges.reset();
1399
1400 return Error::None;
1401}
1402
1403void HWC2On1Adapter::Display::addRetireFence(int fenceFd)
1404{
1405 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1406 mRetireFence.add(fenceFd);
1407}
1408
1409void HWC2On1Adapter::Display::addReleaseFences(
1410 const hwc_display_contents_1_t& hwcContents)
1411{
1412 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1413
1414 size_t numLayers = hwcContents.numHwLayers;
1415 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1416 const auto& receivedLayer = hwcContents.hwLayers[hwc1Id];
1417 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1418 if (receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET) {
1419 ALOGE("addReleaseFences: HWC1 layer %zd doesn't have a"
1420 " matching HWC2 layer, and isn't the framebuffer"
1421 " target", hwc1Id);
1422 }
1423 // Close the framebuffer target release fence since we will use the
1424 // display retire fence instead
1425 if (receivedLayer.releaseFenceFd != -1) {
1426 close(receivedLayer.releaseFenceFd);
1427 }
1428 continue;
1429 }
1430
1431 Layer& layer = *mHwc1LayerMap[hwc1Id];
1432 ALOGV("Adding release fence %d to layer %" PRIu64,
1433 receivedLayer.releaseFenceFd, layer.getId());
1434 layer.addReleaseFence(receivedLayer.releaseFenceFd);
1435 }
1436}
1437
Dan Stoza5df2a862016-03-24 16:19:37 -07001438bool HWC2On1Adapter::Display::hasColorTransform() const
1439{
1440 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1441 return mHasColorTransform;
1442}
1443
Dan Stozac6998d22015-09-24 17:03:36 -07001444static std::string hwc1CompositionString(int32_t type)
1445{
1446 switch (type) {
1447 case HWC_FRAMEBUFFER: return "Framebuffer";
1448 case HWC_OVERLAY: return "Overlay";
1449 case HWC_BACKGROUND: return "Background";
1450 case HWC_FRAMEBUFFER_TARGET: return "FramebufferTarget";
1451 case HWC_SIDEBAND: return "Sideband";
1452 case HWC_CURSOR_OVERLAY: return "CursorOverlay";
1453 default:
1454 return std::string("Unknown (") + std::to_string(type) + ")";
1455 }
1456}
1457
1458static std::string hwc1TransformString(int32_t transform)
1459{
1460 switch (transform) {
1461 case 0: return "None";
1462 case HWC_TRANSFORM_FLIP_H: return "FlipH";
1463 case HWC_TRANSFORM_FLIP_V: return "FlipV";
1464 case HWC_TRANSFORM_ROT_90: return "Rotate90";
1465 case HWC_TRANSFORM_ROT_180: return "Rotate180";
1466 case HWC_TRANSFORM_ROT_270: return "Rotate270";
1467 case HWC_TRANSFORM_FLIP_H_ROT_90: return "FlipHRotate90";
1468 case HWC_TRANSFORM_FLIP_V_ROT_90: return "FlipVRotate90";
1469 default:
1470 return std::string("Unknown (") + std::to_string(transform) + ")";
1471 }
1472}
1473
1474static std::string hwc1BlendModeString(int32_t mode)
1475{
1476 switch (mode) {
1477 case HWC_BLENDING_NONE: return "None";
1478 case HWC_BLENDING_PREMULT: return "Premultiplied";
1479 case HWC_BLENDING_COVERAGE: return "Coverage";
1480 default:
1481 return std::string("Unknown (") + std::to_string(mode) + ")";
1482 }
1483}
1484
1485static std::string rectString(hwc_rect_t rect)
1486{
1487 std::stringstream output;
1488 output << "[" << rect.left << ", " << rect.top << ", ";
1489 output << rect.right << ", " << rect.bottom << "]";
1490 return output.str();
1491}
1492
1493static std::string approximateFloatString(float f)
1494{
1495 if (static_cast<int32_t>(f) == f) {
1496 return std::to_string(static_cast<int32_t>(f));
1497 }
1498 int32_t truncated = static_cast<int32_t>(f * 10);
1499 bool approximate = (static_cast<float>(truncated) != f * 10);
1500 const size_t BUFFER_SIZE = 32;
1501 char buffer[BUFFER_SIZE] = {};
1502 auto bytesWritten = snprintf(buffer, BUFFER_SIZE,
1503 "%s%.1f", approximate ? "~" : "", f);
1504 return std::string(buffer, bytesWritten);
1505}
1506
1507static std::string frectString(hwc_frect_t frect)
1508{
1509 std::stringstream output;
1510 output << "[" << approximateFloatString(frect.left) << ", ";
1511 output << approximateFloatString(frect.top) << ", ";
1512 output << approximateFloatString(frect.right) << ", ";
1513 output << approximateFloatString(frect.bottom) << "]";
1514 return output.str();
1515}
1516
1517static std::string colorString(hwc_color_t color)
1518{
1519 std::stringstream output;
1520 output << "RGBA [";
1521 output << static_cast<int32_t>(color.r) << ", ";
1522 output << static_cast<int32_t>(color.g) << ", ";
1523 output << static_cast<int32_t>(color.b) << ", ";
1524 output << static_cast<int32_t>(color.a) << "]";
1525 return output.str();
1526}
1527
1528static std::string alphaString(float f)
1529{
1530 const size_t BUFFER_SIZE = 8;
1531 char buffer[BUFFER_SIZE] = {};
1532 auto bytesWritten = snprintf(buffer, BUFFER_SIZE, "%.3f", f);
1533 return std::string(buffer, bytesWritten);
1534}
1535
1536static std::string to_string(const hwc_layer_1_t& hwcLayer,
1537 int32_t hwc1MinorVersion)
1538{
1539 const char* fill = " ";
1540
1541 std::stringstream output;
1542
1543 output << " Composition: " <<
1544 hwc1CompositionString(hwcLayer.compositionType);
1545
1546 if (hwcLayer.compositionType == HWC_BACKGROUND) {
1547 output << " Color: " << colorString(hwcLayer.backgroundColor) << '\n';
1548 } else if (hwcLayer.compositionType == HWC_SIDEBAND) {
1549 output << " Stream: " << hwcLayer.sidebandStream << '\n';
1550 } else {
1551 output << " Buffer: " << hwcLayer.handle << "/" <<
1552 hwcLayer.acquireFenceFd << '\n';
1553 }
1554
1555 output << fill << "Display frame: " << rectString(hwcLayer.displayFrame) <<
1556 '\n';
1557
1558 output << fill << "Source crop: ";
1559 if (hwc1MinorVersion >= 3) {
1560 output << frectString(hwcLayer.sourceCropf) << '\n';
1561 } else {
1562 output << rectString(hwcLayer.sourceCropi) << '\n';
1563 }
1564
1565 output << fill << "Transform: " << hwc1TransformString(hwcLayer.transform);
1566 output << " Blend mode: " << hwc1BlendModeString(hwcLayer.blending);
1567 if (hwcLayer.planeAlpha != 0xFF) {
1568 output << " Alpha: " << alphaString(hwcLayer.planeAlpha / 255.0f);
1569 }
1570 output << '\n';
1571
1572 if (hwcLayer.hints != 0) {
1573 output << fill << "Hints:";
1574 if ((hwcLayer.hints & HWC_HINT_TRIPLE_BUFFER) != 0) {
1575 output << " TripleBuffer";
1576 }
1577 if ((hwcLayer.hints & HWC_HINT_CLEAR_FB) != 0) {
1578 output << " ClearFB";
1579 }
1580 output << '\n';
1581 }
1582
1583 if (hwcLayer.flags != 0) {
1584 output << fill << "Flags:";
1585 if ((hwcLayer.flags & HWC_SKIP_LAYER) != 0) {
1586 output << " SkipLayer";
1587 }
1588 if ((hwcLayer.flags & HWC_IS_CURSOR_LAYER) != 0) {
1589 output << " IsCursorLayer";
1590 }
1591 output << '\n';
1592 }
1593
1594 return output.str();
1595}
1596
1597static std::string to_string(const hwc_display_contents_1_t& hwcContents,
1598 int32_t hwc1MinorVersion)
1599{
1600 const char* fill = " ";
1601
1602 std::stringstream output;
1603 output << fill << "Geometry changed: " <<
1604 ((hwcContents.flags & HWC_GEOMETRY_CHANGED) != 0 ? "Y\n" : "N\n");
1605
1606 output << fill << hwcContents.numHwLayers << " Layer" <<
1607 ((hwcContents.numHwLayers == 1) ? "\n" : "s\n");
1608 for (size_t layer = 0; layer < hwcContents.numHwLayers; ++layer) {
1609 output << fill << " Layer " << layer;
1610 output << to_string(hwcContents.hwLayers[layer], hwc1MinorVersion);
1611 }
1612
1613 if (hwcContents.outbuf != nullptr) {
1614 output << fill << "Output buffer: " << hwcContents.outbuf << "/" <<
1615 hwcContents.outbufAcquireFenceFd << '\n';
1616 }
1617
1618 return output.str();
1619}
1620
1621std::string HWC2On1Adapter::Display::dump() const
1622{
1623 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1624
1625 std::stringstream output;
1626
1627 output << " Display " << mId << ": ";
1628 output << to_string(mType) << " ";
1629 output << "HWC1 ID: " << mHwc1Id << " ";
1630 output << "Power mode: " << to_string(mPowerMode) << " ";
1631 output << "Vsync: " << to_string(mVsyncEnabled) << '\n';
1632
Dan Stoza076ac672016-03-14 10:47:53 -07001633 output << " Color modes [active]:";
1634 for (const auto& mode : mColorModes) {
1635 if (mode == mActiveColorMode) {
1636 output << " [" << mode << ']';
Dan Stozac6998d22015-09-24 17:03:36 -07001637 } else {
Dan Stoza076ac672016-03-14 10:47:53 -07001638 output << " " << mode;
Dan Stozac6998d22015-09-24 17:03:36 -07001639 }
1640 }
1641 output << '\n';
1642
Dan Stoza076ac672016-03-14 10:47:53 -07001643 output << " " << mConfigs.size() << " Config" <<
1644 (mConfigs.size() == 1 ? "" : "s") << " (* active)\n";
1645 for (const auto& config : mConfigs) {
1646 output << (config == mActiveConfig ? " * " : " ");
1647 output << config->toString(true) << '\n';
1648 }
1649
Dan Stozac6998d22015-09-24 17:03:36 -07001650 output << " " << mLayers.size() << " Layer" <<
1651 (mLayers.size() == 1 ? "" : "s") << '\n';
1652 for (const auto& layer : mLayers) {
1653 output << layer->dump();
1654 }
1655
1656 output << " Client target: " << mClientTarget.getBuffer() << '\n';
1657
1658 if (mOutputBuffer.getBuffer() != nullptr) {
1659 output << " Output buffer: " << mOutputBuffer.getBuffer() << '\n';
1660 }
1661
1662 if (mHwc1ReceivedContents) {
1663 output << " Last received HWC1 state\n";
1664 output << to_string(*mHwc1ReceivedContents, mDevice.mHwc1MinorVersion);
1665 } else if (mHwc1RequestedContents) {
1666 output << " Last requested HWC1 state\n";
1667 output << to_string(*mHwc1RequestedContents, mDevice.mHwc1MinorVersion);
1668 }
1669
1670 return output.str();
1671}
1672
1673void HWC2On1Adapter::Display::Config::setAttribute(HWC2::Attribute attribute,
1674 int32_t value)
1675{
1676 mAttributes[attribute] = value;
1677}
1678
1679int32_t HWC2On1Adapter::Display::Config::getAttribute(Attribute attribute) const
1680{
1681 if (mAttributes.count(attribute) == 0) {
1682 return -1;
1683 }
1684 return mAttributes.at(attribute);
1685}
1686
Dan Stoza076ac672016-03-14 10:47:53 -07001687void HWC2On1Adapter::Display::Config::setHwc1Id(uint32_t id)
1688{
Michael Wright28f24d02016-07-12 13:30:53 -07001689 android_color_mode_t colorMode = static_cast<android_color_mode_t>(getAttribute(ColorMode));
1690 mHwc1Ids.emplace(colorMode, id);
Dan Stoza076ac672016-03-14 10:47:53 -07001691}
1692
1693bool HWC2On1Adapter::Display::Config::hasHwc1Id(uint32_t id) const
1694{
1695 for (const auto& idPair : mHwc1Ids) {
1696 if (id == idPair.second) {
1697 return true;
1698 }
1699 }
1700 return false;
1701}
1702
Michael Wright28f24d02016-07-12 13:30:53 -07001703Error HWC2On1Adapter::Display::Config::getColorModeForHwc1Id(
1704 uint32_t id, android_color_mode_t* outMode) const
Dan Stoza076ac672016-03-14 10:47:53 -07001705{
1706 for (const auto& idPair : mHwc1Ids) {
1707 if (id == idPair.second) {
Michael Wright28f24d02016-07-12 13:30:53 -07001708 *outMode = idPair.first;
1709 return Error::None;
Dan Stoza076ac672016-03-14 10:47:53 -07001710 }
1711 }
Michael Wright28f24d02016-07-12 13:30:53 -07001712 ALOGE("Unable to find color mode for HWC ID %" PRIu32 " on config %u", id, mId);
1713 return Error::BadParameter;
Dan Stoza076ac672016-03-14 10:47:53 -07001714}
1715
Michael Wright28f24d02016-07-12 13:30:53 -07001716Error HWC2On1Adapter::Display::Config::getHwc1IdForColorMode(android_color_mode_t mode,
Dan Stoza076ac672016-03-14 10:47:53 -07001717 uint32_t* outId) const
1718{
1719 for (const auto& idPair : mHwc1Ids) {
1720 if (mode == idPair.first) {
1721 *outId = idPair.second;
1722 return Error::None;
1723 }
1724 }
1725 ALOGE("Unable to find HWC1 ID for color mode %d on config %u", mode, mId);
1726 return Error::BadParameter;
1727}
1728
1729bool HWC2On1Adapter::Display::Config::merge(const Config& other)
1730{
1731 auto attributes = {HWC2::Attribute::Width, HWC2::Attribute::Height,
1732 HWC2::Attribute::VsyncPeriod, HWC2::Attribute::DpiX,
1733 HWC2::Attribute::DpiY};
1734 for (auto attribute : attributes) {
1735 if (getAttribute(attribute) != other.getAttribute(attribute)) {
1736 return false;
1737 }
1738 }
Michael Wright28f24d02016-07-12 13:30:53 -07001739 android_color_mode_t otherColorMode =
1740 static_cast<android_color_mode_t>(other.getAttribute(ColorMode));
1741 if (mHwc1Ids.count(otherColorMode) != 0) {
Dan Stoza076ac672016-03-14 10:47:53 -07001742 ALOGE("Attempted to merge two configs (%u and %u) which appear to be "
Michael Wright28f24d02016-07-12 13:30:53 -07001743 "identical", mHwc1Ids.at(otherColorMode),
1744 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001745 return false;
1746 }
Michael Wright28f24d02016-07-12 13:30:53 -07001747 mHwc1Ids.emplace(otherColorMode,
1748 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001749 return true;
1750}
1751
Michael Wright28f24d02016-07-12 13:30:53 -07001752std::set<android_color_mode_t> HWC2On1Adapter::Display::Config::getColorModes() const
Dan Stoza076ac672016-03-14 10:47:53 -07001753{
Michael Wright28f24d02016-07-12 13:30:53 -07001754 std::set<android_color_mode_t> colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001755 for (const auto& idPair : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001756 colorModes.emplace(idPair.first);
Dan Stoza076ac672016-03-14 10:47:53 -07001757 }
Michael Wright28f24d02016-07-12 13:30:53 -07001758 return colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001759}
1760
1761std::string HWC2On1Adapter::Display::Config::toString(bool splitLine) const
Dan Stozac6998d22015-09-24 17:03:36 -07001762{
1763 std::string output;
1764
1765 const size_t BUFFER_SIZE = 100;
1766 char buffer[BUFFER_SIZE] = {};
1767 auto writtenBytes = snprintf(buffer, BUFFER_SIZE,
Dan Stoza076ac672016-03-14 10:47:53 -07001768 "%u x %u", mAttributes.at(HWC2::Attribute::Width),
Dan Stozac6998d22015-09-24 17:03:36 -07001769 mAttributes.at(HWC2::Attribute::Height));
1770 output.append(buffer, writtenBytes);
1771
1772 if (mAttributes.count(HWC2::Attribute::VsyncPeriod) != 0) {
1773 std::memset(buffer, 0, BUFFER_SIZE);
1774 writtenBytes = snprintf(buffer, BUFFER_SIZE, " @ %.1f Hz",
1775 1e9 / mAttributes.at(HWC2::Attribute::VsyncPeriod));
1776 output.append(buffer, writtenBytes);
1777 }
1778
1779 if (mAttributes.count(HWC2::Attribute::DpiX) != 0 &&
1780 mAttributes.at(HWC2::Attribute::DpiX) != -1) {
1781 std::memset(buffer, 0, BUFFER_SIZE);
1782 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1783 ", DPI: %.1f x %.1f",
1784 mAttributes.at(HWC2::Attribute::DpiX) / 1000.0f,
1785 mAttributes.at(HWC2::Attribute::DpiY) / 1000.0f);
1786 output.append(buffer, writtenBytes);
1787 }
1788
Dan Stoza076ac672016-03-14 10:47:53 -07001789 std::memset(buffer, 0, BUFFER_SIZE);
1790 if (splitLine) {
1791 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1792 "\n HWC1 ID/Color transform:");
1793 } else {
1794 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1795 ", HWC1 ID/Color transform:");
1796 }
1797 output.append(buffer, writtenBytes);
1798
1799
1800 for (const auto& id : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001801 android_color_mode_t colorMode = id.first;
Dan Stoza076ac672016-03-14 10:47:53 -07001802 uint32_t hwc1Id = id.second;
1803 std::memset(buffer, 0, BUFFER_SIZE);
Michael Wright28f24d02016-07-12 13:30:53 -07001804 if (colorMode == mDisplay.mActiveColorMode) {
Dan Stoza076ac672016-03-14 10:47:53 -07001805 writtenBytes = snprintf(buffer, BUFFER_SIZE, " [%u/%d]", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001806 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001807 } else {
1808 writtenBytes = snprintf(buffer, BUFFER_SIZE, " %u/%d", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001809 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001810 }
1811 output.append(buffer, writtenBytes);
1812 }
1813
Dan Stozac6998d22015-09-24 17:03:36 -07001814 return output;
1815}
1816
1817std::shared_ptr<const HWC2On1Adapter::Display::Config>
1818 HWC2On1Adapter::Display::getConfig(hwc2_config_t configId) const
1819{
1820 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
1821 return nullptr;
1822 }
1823 return mConfigs[configId];
1824}
1825
Dan Stoza076ac672016-03-14 10:47:53 -07001826void HWC2On1Adapter::Display::populateColorModes()
1827{
Michael Wright28f24d02016-07-12 13:30:53 -07001828 mColorModes = mConfigs[0]->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001829 for (const auto& config : mConfigs) {
Michael Wright28f24d02016-07-12 13:30:53 -07001830 std::set<android_color_mode_t> intersection;
1831 auto configModes = config->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001832 std::set_intersection(mColorModes.cbegin(), mColorModes.cend(),
1833 configModes.cbegin(), configModes.cend(),
1834 std::inserter(intersection, intersection.begin()));
1835 std::swap(intersection, mColorModes);
1836 }
1837}
1838
1839void HWC2On1Adapter::Display::initializeActiveConfig()
1840{
1841 if (mDevice.mHwc1Device->getActiveConfig == nullptr) {
1842 ALOGV("getActiveConfig is null, choosing config 0");
1843 mActiveConfig = mConfigs[0];
Michael Wright28f24d02016-07-12 13:30:53 -07001844 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001845 return;
1846 }
1847
1848 auto activeConfig = mDevice.mHwc1Device->getActiveConfig(
1849 mDevice.mHwc1Device, mHwc1Id);
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001850
1851 // Some devices startup without an activeConfig:
1852 // We need to set one ourselves.
1853 if (activeConfig == HWC_ERROR) {
1854 ALOGV("There is no active configuration: Picking the first one: 0.");
1855 const int defaultIndex = 0;
1856 mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, mHwc1Id, defaultIndex);
1857 activeConfig = defaultIndex;
1858 }
1859
1860 for (const auto& config : mConfigs) {
1861 if (config->hasHwc1Id(activeConfig)) {
1862 ALOGE("Setting active config to %d for HWC1 config %u", config->getId(), activeConfig);
1863 mActiveConfig = config;
1864 if (config->getColorModeForHwc1Id(activeConfig, &mActiveColorMode) != Error::None) {
1865 // This should never happen since we checked for the config's presence before
1866 // setting it as active.
1867 ALOGE("Unable to find color mode for active HWC1 config %d", config->getId());
1868 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001869 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001870 break;
Dan Stoza076ac672016-03-14 10:47:53 -07001871 }
1872 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001873 if (!mActiveConfig) {
1874 ALOGV("Unable to find active HWC1 config %u, defaulting to "
1875 "config 0", activeConfig);
1876 mActiveConfig = mConfigs[0];
1877 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
1878 }
1879
1880
1881
1882
Dan Stoza076ac672016-03-14 10:47:53 -07001883}
1884
Dan Stozac6998d22015-09-24 17:03:36 -07001885void HWC2On1Adapter::Display::reallocateHwc1Contents()
1886{
1887 // Allocate an additional layer for the framebuffer target
1888 auto numLayers = mLayers.size() + 1;
1889 size_t size = sizeof(hwc_display_contents_1_t) +
1890 sizeof(hwc_layer_1_t) * numLayers;
1891 ALOGV("[%" PRIu64 "] reallocateHwc1Contents creating %zd layer%s", mId,
1892 numLayers, numLayers != 1 ? "s" : "");
1893 auto contents =
1894 static_cast<hwc_display_contents_1_t*>(std::calloc(size, 1));
1895 contents->numHwLayers = numLayers;
1896 mHwc1RequestedContents.reset(contents);
1897}
1898
1899void HWC2On1Adapter::Display::assignHwc1LayerIds()
1900{
1901 mHwc1LayerMap.clear();
1902 size_t nextHwc1Id = 0;
1903 for (auto& layer : mLayers) {
1904 mHwc1LayerMap[nextHwc1Id] = layer;
1905 layer->setHwc1Id(nextHwc1Id++);
1906 }
1907}
1908
1909void HWC2On1Adapter::Display::updateTypeChanges(const hwc_layer_1_t& hwc1Layer,
1910 const Layer& layer)
1911{
1912 auto layerId = layer.getId();
1913 switch (hwc1Layer.compositionType) {
1914 case HWC_FRAMEBUFFER:
1915 if (layer.getCompositionType() != Composition::Client) {
1916 mChanges->addTypeChange(layerId, Composition::Client);
1917 }
1918 break;
1919 case HWC_OVERLAY:
1920 if (layer.getCompositionType() != Composition::Device) {
1921 mChanges->addTypeChange(layerId, Composition::Device);
1922 }
1923 break;
1924 case HWC_BACKGROUND:
1925 ALOGE_IF(layer.getCompositionType() != Composition::SolidColor,
1926 "updateTypeChanges: HWC1 requested BACKGROUND, but HWC2"
1927 " wasn't expecting SolidColor");
1928 break;
1929 case HWC_FRAMEBUFFER_TARGET:
1930 // Do nothing, since it shouldn't be modified by HWC1
1931 break;
1932 case HWC_SIDEBAND:
1933 ALOGE_IF(layer.getCompositionType() != Composition::Sideband,
1934 "updateTypeChanges: HWC1 requested SIDEBAND, but HWC2"
1935 " wasn't expecting Sideband");
1936 break;
1937 case HWC_CURSOR_OVERLAY:
1938 ALOGE_IF(layer.getCompositionType() != Composition::Cursor,
1939 "updateTypeChanges: HWC1 requested CURSOR_OVERLAY, but"
1940 " HWC2 wasn't expecting Cursor");
1941 break;
1942 }
1943}
1944
1945void HWC2On1Adapter::Display::updateLayerRequests(
1946 const hwc_layer_1_t& hwc1Layer, const Layer& layer)
1947{
1948 if ((hwc1Layer.hints & HWC_HINT_CLEAR_FB) != 0) {
1949 mChanges->addLayerRequest(layer.getId(),
1950 LayerRequest::ClearClientTarget);
1951 }
1952}
1953
1954void HWC2On1Adapter::Display::prepareFramebufferTarget()
1955{
1956 // We check that mActiveConfig is valid in Display::prepare
1957 int32_t width = mActiveConfig->getAttribute(Attribute::Width);
1958 int32_t height = mActiveConfig->getAttribute(Attribute::Height);
1959
1960 auto& hwc1Target = mHwc1RequestedContents->hwLayers[mLayers.size()];
1961 hwc1Target.compositionType = HWC_FRAMEBUFFER_TARGET;
1962 hwc1Target.releaseFenceFd = -1;
1963 hwc1Target.hints = 0;
1964 hwc1Target.flags = 0;
1965 hwc1Target.transform = 0;
1966 hwc1Target.blending = HWC_BLENDING_PREMULT;
1967 if (mDevice.getHwc1MinorVersion() < 3) {
1968 hwc1Target.sourceCropi = {0, 0, width, height};
1969 } else {
1970 hwc1Target.sourceCropf = {0.0f, 0.0f, static_cast<float>(width),
1971 static_cast<float>(height)};
1972 }
1973 hwc1Target.displayFrame = {0, 0, width, height};
1974 hwc1Target.planeAlpha = 255;
1975 hwc1Target.visibleRegionScreen.numRects = 1;
1976 auto rects = static_cast<hwc_rect_t*>(std::malloc(sizeof(hwc_rect_t)));
1977 rects[0].left = 0;
1978 rects[0].top = 0;
1979 rects[0].right = width;
1980 rects[0].bottom = height;
1981 hwc1Target.visibleRegionScreen.rects = rects;
1982
1983 // We will set this to the correct value in set
1984 hwc1Target.acquireFenceFd = -1;
1985}
1986
1987// Layer functions
1988
1989std::atomic<hwc2_layer_t> HWC2On1Adapter::Layer::sNextId(1);
1990
1991HWC2On1Adapter::Layer::Layer(Display& display)
1992 : mId(sNextId++),
1993 mDisplay(display),
Dan Stozafc4e2022016-02-23 11:43:19 -08001994 mDirtyCount(0),
1995 mBuffer(),
1996 mSurfaceDamage(),
Dan Stozac6998d22015-09-24 17:03:36 -07001997 mBlendMode(*this, BlendMode::None),
1998 mColor(*this, {0, 0, 0, 0}),
1999 mCompositionType(*this, Composition::Invalid),
2000 mDisplayFrame(*this, {0, 0, -1, -1}),
2001 mPlaneAlpha(*this, 0.0f),
2002 mSidebandStream(*this, nullptr),
2003 mSourceCrop(*this, {0.0f, 0.0f, -1.0f, -1.0f}),
2004 mTransform(*this, Transform::None),
2005 mVisibleRegion(*this, std::vector<hwc_rect_t>()),
2006 mZ(0),
Dan Stozafc4e2022016-02-23 11:43:19 -08002007 mReleaseFence(),
Dan Stozac6998d22015-09-24 17:03:36 -07002008 mHwc1Id(0),
Dan Stoza5df2a862016-03-24 16:19:37 -07002009 mHasUnsupportedDataspace(false),
Dan Stozac6998d22015-09-24 17:03:36 -07002010 mHasUnsupportedPlaneAlpha(false) {}
2011
2012bool HWC2On1Adapter::SortLayersByZ::operator()(
2013 const std::shared_ptr<Layer>& lhs, const std::shared_ptr<Layer>& rhs)
2014{
2015 return lhs->getZ() < rhs->getZ();
2016}
2017
2018Error HWC2On1Adapter::Layer::setBuffer(buffer_handle_t buffer,
2019 int32_t acquireFence)
2020{
2021 ALOGV("Setting acquireFence to %d for layer %" PRIu64, acquireFence, mId);
2022 mBuffer.setBuffer(buffer);
2023 mBuffer.setFence(acquireFence);
2024 return Error::None;
2025}
2026
2027Error HWC2On1Adapter::Layer::setCursorPosition(int32_t x, int32_t y)
2028{
2029 if (mCompositionType.getValue() != Composition::Cursor) {
2030 return Error::BadLayer;
2031 }
2032
2033 if (mDisplay.hasChanges()) {
2034 return Error::NotValidated;
2035 }
2036
2037 auto displayId = mDisplay.getHwc1Id();
2038 auto hwc1Device = mDisplay.getDevice().getHwc1Device();
2039 hwc1Device->setCursorPositionAsync(hwc1Device, displayId, x, y);
2040 return Error::None;
2041}
2042
2043Error HWC2On1Adapter::Layer::setSurfaceDamage(hwc_region_t damage)
2044{
2045 mSurfaceDamage.resize(damage.numRects);
2046 std::copy_n(damage.rects, damage.numRects, mSurfaceDamage.begin());
2047 return Error::None;
2048}
2049
2050// Layer state functions
2051
2052Error HWC2On1Adapter::Layer::setBlendMode(BlendMode mode)
2053{
2054 mBlendMode.setPending(mode);
2055 return Error::None;
2056}
2057
2058Error HWC2On1Adapter::Layer::setColor(hwc_color_t color)
2059{
2060 mColor.setPending(color);
2061 return Error::None;
2062}
2063
2064Error HWC2On1Adapter::Layer::setCompositionType(Composition type)
2065{
2066 mCompositionType.setPending(type);
2067 return Error::None;
2068}
2069
Dan Stoza5df2a862016-03-24 16:19:37 -07002070Error HWC2On1Adapter::Layer::setDataspace(android_dataspace_t dataspace)
2071{
2072 mHasUnsupportedDataspace = (dataspace != HAL_DATASPACE_UNKNOWN);
2073 return Error::None;
2074}
2075
Dan Stozac6998d22015-09-24 17:03:36 -07002076Error HWC2On1Adapter::Layer::setDisplayFrame(hwc_rect_t frame)
2077{
2078 mDisplayFrame.setPending(frame);
2079 return Error::None;
2080}
2081
2082Error HWC2On1Adapter::Layer::setPlaneAlpha(float alpha)
2083{
2084 mPlaneAlpha.setPending(alpha);
2085 return Error::None;
2086}
2087
2088Error HWC2On1Adapter::Layer::setSidebandStream(const native_handle_t* stream)
2089{
2090 mSidebandStream.setPending(stream);
2091 return Error::None;
2092}
2093
2094Error HWC2On1Adapter::Layer::setSourceCrop(hwc_frect_t crop)
2095{
2096 mSourceCrop.setPending(crop);
2097 return Error::None;
2098}
2099
2100Error HWC2On1Adapter::Layer::setTransform(Transform transform)
2101{
2102 mTransform.setPending(transform);
2103 return Error::None;
2104}
2105
2106Error HWC2On1Adapter::Layer::setVisibleRegion(hwc_region_t rawVisible)
2107{
2108 std::vector<hwc_rect_t> visible(rawVisible.rects,
2109 rawVisible.rects + rawVisible.numRects);
2110 mVisibleRegion.setPending(std::move(visible));
2111 return Error::None;
2112}
2113
2114Error HWC2On1Adapter::Layer::setZ(uint32_t z)
2115{
2116 mZ = z;
2117 return Error::None;
2118}
2119
2120void HWC2On1Adapter::Layer::addReleaseFence(int fenceFd)
2121{
2122 ALOGV("addReleaseFence %d to layer %" PRIu64, fenceFd, mId);
2123 mReleaseFence.add(fenceFd);
2124}
2125
2126const sp<Fence>& HWC2On1Adapter::Layer::getReleaseFence() const
2127{
2128 return mReleaseFence.get();
2129}
2130
2131void HWC2On1Adapter::Layer::applyState(hwc_layer_1_t& hwc1Layer,
2132 bool applyAllState)
2133{
2134 applyCommonState(hwc1Layer, applyAllState);
2135 auto compositionType = mCompositionType.getPendingValue();
2136 if (compositionType == Composition::SolidColor) {
2137 applySolidColorState(hwc1Layer, applyAllState);
2138 } else if (compositionType == Composition::Sideband) {
2139 applySidebandState(hwc1Layer, applyAllState);
2140 } else {
2141 applyBufferState(hwc1Layer);
2142 }
2143 applyCompositionType(hwc1Layer, applyAllState);
2144}
2145
2146// Layer dump helpers
2147
2148static std::string regionStrings(const std::vector<hwc_rect_t>& visibleRegion,
2149 const std::vector<hwc_rect_t>& surfaceDamage)
2150{
2151 std::string regions;
2152 regions += " Visible Region";
2153 regions.resize(40, ' ');
2154 regions += "Surface Damage\n";
2155
2156 size_t numPrinted = 0;
2157 size_t maxSize = std::max(visibleRegion.size(), surfaceDamage.size());
2158 while (numPrinted < maxSize) {
2159 std::string line(" ");
2160 if (visibleRegion.empty() && numPrinted == 0) {
2161 line += "None";
2162 } else if (numPrinted < visibleRegion.size()) {
2163 line += rectString(visibleRegion[numPrinted]);
2164 }
2165 line.resize(40, ' ');
2166 if (surfaceDamage.empty() && numPrinted == 0) {
2167 line += "None";
2168 } else if (numPrinted < surfaceDamage.size()) {
2169 line += rectString(surfaceDamage[numPrinted]);
2170 }
2171 line += '\n';
2172 regions += line;
2173 ++numPrinted;
2174 }
2175 return regions;
2176}
2177
2178std::string HWC2On1Adapter::Layer::dump() const
2179{
2180 std::stringstream output;
2181 const char* fill = " ";
2182
2183 output << fill << to_string(mCompositionType.getPendingValue());
2184 output << " Layer HWC2/1: " << mId << "/" << mHwc1Id << " ";
2185 output << "Z: " << mZ;
2186 if (mCompositionType.getValue() == HWC2::Composition::SolidColor) {
2187 output << " " << colorString(mColor.getValue());
2188 } else if (mCompositionType.getValue() == HWC2::Composition::Sideband) {
2189 output << " Handle: " << mSidebandStream.getValue() << '\n';
2190 } else {
2191 output << " Buffer: " << mBuffer.getBuffer() << "/" <<
2192 mBuffer.getFence() << '\n';
2193 output << fill << " Display frame [LTRB]: " <<
2194 rectString(mDisplayFrame.getValue()) << '\n';
2195 output << fill << " Source crop: " <<
2196 frectString(mSourceCrop.getValue()) << '\n';
2197 output << fill << " Transform: " << to_string(mTransform.getValue());
2198 output << " Blend mode: " << to_string(mBlendMode.getValue());
2199 if (mPlaneAlpha.getValue() != 1.0f) {
2200 output << " Alpha: " <<
2201 alphaString(mPlaneAlpha.getValue()) << '\n';
2202 } else {
2203 output << '\n';
2204 }
2205 output << regionStrings(mVisibleRegion.getValue(), mSurfaceDamage);
2206 }
2207 return output.str();
2208}
2209
2210static int getHwc1Blending(HWC2::BlendMode blendMode)
2211{
2212 switch (blendMode) {
2213 case BlendMode::Coverage: return HWC_BLENDING_COVERAGE;
2214 case BlendMode::Premultiplied: return HWC_BLENDING_PREMULT;
2215 default: return HWC_BLENDING_NONE;
2216 }
2217}
2218
2219void HWC2On1Adapter::Layer::applyCommonState(hwc_layer_1_t& hwc1Layer,
2220 bool applyAllState)
2221{
2222 auto minorVersion = mDisplay.getDevice().getHwc1MinorVersion();
2223 if (applyAllState || mBlendMode.isDirty()) {
2224 hwc1Layer.blending = getHwc1Blending(mBlendMode.getPendingValue());
2225 mBlendMode.latch();
2226 }
2227 if (applyAllState || mDisplayFrame.isDirty()) {
2228 hwc1Layer.displayFrame = mDisplayFrame.getPendingValue();
2229 mDisplayFrame.latch();
2230 }
2231 if (applyAllState || mPlaneAlpha.isDirty()) {
2232 auto pendingAlpha = mPlaneAlpha.getPendingValue();
2233 if (minorVersion < 2) {
2234 mHasUnsupportedPlaneAlpha = pendingAlpha < 1.0f;
2235 } else {
2236 hwc1Layer.planeAlpha =
2237 static_cast<uint8_t>(255.0f * pendingAlpha + 0.5f);
2238 }
2239 mPlaneAlpha.latch();
2240 }
2241 if (applyAllState || mSourceCrop.isDirty()) {
2242 if (minorVersion < 3) {
2243 auto pending = mSourceCrop.getPendingValue();
2244 hwc1Layer.sourceCropi.left =
2245 static_cast<int32_t>(std::ceil(pending.left));
2246 hwc1Layer.sourceCropi.top =
2247 static_cast<int32_t>(std::ceil(pending.top));
2248 hwc1Layer.sourceCropi.right =
2249 static_cast<int32_t>(std::floor(pending.right));
2250 hwc1Layer.sourceCropi.bottom =
2251 static_cast<int32_t>(std::floor(pending.bottom));
2252 } else {
2253 hwc1Layer.sourceCropf = mSourceCrop.getPendingValue();
2254 }
2255 mSourceCrop.latch();
2256 }
2257 if (applyAllState || mTransform.isDirty()) {
2258 hwc1Layer.transform =
2259 static_cast<uint32_t>(mTransform.getPendingValue());
2260 mTransform.latch();
2261 }
2262 if (applyAllState || mVisibleRegion.isDirty()) {
2263 auto& hwc1VisibleRegion = hwc1Layer.visibleRegionScreen;
2264
2265 std::free(const_cast<hwc_rect_t*>(hwc1VisibleRegion.rects));
2266
2267 auto pending = mVisibleRegion.getPendingValue();
2268 hwc_rect_t* newRects = static_cast<hwc_rect_t*>(
2269 std::malloc(sizeof(hwc_rect_t) * pending.size()));
2270 std::copy(pending.begin(), pending.end(), newRects);
2271 hwc1VisibleRegion.rects = const_cast<const hwc_rect_t*>(newRects);
2272 hwc1VisibleRegion.numRects = pending.size();
2273 mVisibleRegion.latch();
2274 }
2275}
2276
2277void HWC2On1Adapter::Layer::applySolidColorState(hwc_layer_1_t& hwc1Layer,
2278 bool applyAllState)
2279{
2280 if (applyAllState || mColor.isDirty()) {
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002281 // If the device does not support background color it is likely to make
2282 // assumption regarding backgroundColor and handle (both fields occupy
2283 // the same location in hwc_layer_1_t union).
2284 // To not confuse these devices we don't set background color and we
2285 // make sure handle is a null pointer.
2286 if (mDisplay.getDevice().supportsBackgroundColor()) {
2287 hwc1Layer.backgroundColor = mColor.getPendingValue();
2288 mHasUnsupportedBackgroundColor = false;
2289 } else {
2290 hwc1Layer.handle = nullptr;
2291 mHasUnsupportedBackgroundColor = true;
2292 }
Dan Stozac6998d22015-09-24 17:03:36 -07002293 mColor.latch();
2294 }
2295}
2296
2297void HWC2On1Adapter::Layer::applySidebandState(hwc_layer_1_t& hwc1Layer,
2298 bool applyAllState)
2299{
2300 if (applyAllState || mSidebandStream.isDirty()) {
2301 hwc1Layer.sidebandStream = mSidebandStream.getPendingValue();
2302 mSidebandStream.latch();
2303 }
2304}
2305
2306void HWC2On1Adapter::Layer::applyBufferState(hwc_layer_1_t& hwc1Layer)
2307{
2308 hwc1Layer.handle = mBuffer.getBuffer();
2309 hwc1Layer.acquireFenceFd = mBuffer.getFence();
2310}
2311
2312void HWC2On1Adapter::Layer::applyCompositionType(hwc_layer_1_t& hwc1Layer,
2313 bool applyAllState)
2314{
Dan Stoza5df2a862016-03-24 16:19:37 -07002315 // HWC1 never supports color transforms or dataspaces and only sometimes
2316 // supports plane alpha (depending on the version). These require us to drop
2317 // some or all layers to client composition.
2318 if (mHasUnsupportedDataspace || mHasUnsupportedPlaneAlpha ||
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002319 mDisplay.hasColorTransform() || mHasUnsupportedBackgroundColor) {
Dan Stozac6998d22015-09-24 17:03:36 -07002320 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2321 hwc1Layer.flags = HWC_SKIP_LAYER;
2322 return;
2323 }
2324
2325 if (applyAllState || mCompositionType.isDirty()) {
2326 hwc1Layer.flags = 0;
2327 switch (mCompositionType.getPendingValue()) {
2328 case Composition::Client:
2329 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2330 hwc1Layer.flags |= HWC_SKIP_LAYER;
2331 break;
2332 case Composition::Device:
2333 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2334 break;
2335 case Composition::SolidColor:
Dan Stoza5df47cb2016-09-15 16:38:42 -07002336 // In theory the following line should work, but since the HWC1
2337 // version of SurfaceFlinger never used HWC_BACKGROUND, HWC1
2338 // devices may not work correctly. To be on the safe side, we
2339 // fall back to client composition.
2340 //
2341 // hwc1Layer.compositionType = HWC_BACKGROUND;
2342 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2343 hwc1Layer.flags |= HWC_SKIP_LAYER;
Dan Stozac6998d22015-09-24 17:03:36 -07002344 break;
2345 case Composition::Cursor:
2346 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2347 if (mDisplay.getDevice().getHwc1MinorVersion() >= 4) {
2348 hwc1Layer.hints |= HWC_IS_CURSOR_LAYER;
2349 }
2350 break;
2351 case Composition::Sideband:
2352 if (mDisplay.getDevice().getHwc1MinorVersion() < 4) {
2353 hwc1Layer.compositionType = HWC_SIDEBAND;
2354 } else {
2355 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2356 hwc1Layer.flags |= HWC_SKIP_LAYER;
2357 }
2358 break;
2359 default:
2360 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2361 hwc1Layer.flags |= HWC_SKIP_LAYER;
2362 break;
2363 }
2364 ALOGV("Layer %" PRIu64 " %s set to %d", mId,
2365 to_string(mCompositionType.getPendingValue()).c_str(),
2366 hwc1Layer.compositionType);
2367 ALOGV_IF(hwc1Layer.flags & HWC_SKIP_LAYER, " and skipping");
2368 mCompositionType.latch();
2369 }
2370}
2371
2372// Adapter helpers
2373
2374void HWC2On1Adapter::populateCapabilities()
2375{
2376 ALOGV("populateCapabilities");
2377 if (mHwc1MinorVersion >= 3U) {
2378 int supportedTypes = 0;
2379 auto result = mHwc1Device->query(mHwc1Device,
2380 HWC_DISPLAY_TYPES_SUPPORTED, &supportedTypes);
Fred Fettingerc50c01e2016-06-14 17:53:10 -05002381 if ((result == 0) && ((supportedTypes & HWC_DISPLAY_VIRTUAL_BIT) != 0)) {
Dan Stozac6998d22015-09-24 17:03:36 -07002382 ALOGI("Found support for HWC virtual displays");
2383 mHwc1SupportsVirtualDisplays = true;
2384 }
2385 }
2386 if (mHwc1MinorVersion >= 4U) {
2387 mCapabilities.insert(Capability::SidebandStream);
2388 }
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002389
2390 // Check for HWC background color layer support.
2391 if (mHwc1MinorVersion >= 1U) {
2392 int backgroundColorSupported = 0;
2393 auto result = mHwc1Device->query(mHwc1Device,
2394 HWC_BACKGROUND_LAYER_SUPPORTED,
2395 &backgroundColorSupported);
2396 if ((result == 0) && (backgroundColorSupported == 1)) {
2397 ALOGV("Found support for HWC background color");
2398 mHwc1SupportsBackgroundColor = true;
2399 }
2400 }
Dan Stozac6998d22015-09-24 17:03:36 -07002401}
2402
2403HWC2On1Adapter::Display* HWC2On1Adapter::getDisplay(hwc2_display_t id)
2404{
Dan Stozafc4e2022016-02-23 11:43:19 -08002405 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002406
2407 auto display = mDisplays.find(id);
2408 if (display == mDisplays.end()) {
2409 return nullptr;
2410 }
2411
2412 return display->second.get();
2413}
2414
2415std::tuple<HWC2On1Adapter::Layer*, Error> HWC2On1Adapter::getLayer(
2416 hwc2_display_t displayId, hwc2_layer_t layerId)
2417{
2418 auto display = getDisplay(displayId);
2419 if (!display) {
2420 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadDisplay);
2421 }
2422
2423 auto layerEntry = mLayers.find(layerId);
2424 if (layerEntry == mLayers.end()) {
2425 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2426 }
2427
2428 auto layer = layerEntry->second;
2429 if (layer->getDisplay().getId() != displayId) {
2430 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2431 }
2432 return std::make_tuple(layer.get(), Error::None);
2433}
2434
2435void HWC2On1Adapter::populatePrimary()
2436{
2437 ALOGV("populatePrimary");
2438
Dan Stozafc4e2022016-02-23 11:43:19 -08002439 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002440
2441 auto display =
2442 std::make_shared<Display>(*this, HWC2::DisplayType::Physical);
2443 mHwc1DisplayMap[HWC_DISPLAY_PRIMARY] = display->getId();
2444 display->setHwc1Id(HWC_DISPLAY_PRIMARY);
2445 display->populateConfigs();
2446 mDisplays.emplace(display->getId(), std::move(display));
2447}
2448
2449bool HWC2On1Adapter::prepareAllDisplays()
2450{
2451 ATRACE_CALL();
2452
Dan Stozafc4e2022016-02-23 11:43:19 -08002453 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002454
2455 for (const auto& displayPair : mDisplays) {
2456 auto& display = displayPair.second;
2457 if (!display->prepare()) {
2458 return false;
2459 }
2460 }
2461
2462 if (mHwc1DisplayMap.count(0) == 0) {
2463 ALOGE("prepareAllDisplays: Unable to find primary HWC1 display");
2464 return false;
2465 }
2466
2467 // Always push the primary display
2468 std::vector<HWC2On1Adapter::Display::HWC1Contents> requestedContents;
2469 auto primaryDisplayId = mHwc1DisplayMap[HWC_DISPLAY_PRIMARY];
2470 auto& primaryDisplay = mDisplays[primaryDisplayId];
2471 auto primaryDisplayContents = primaryDisplay->cloneRequestedContents();
2472 requestedContents.push_back(std::move(primaryDisplayContents));
2473
2474 // Push the external display, if present
2475 if (mHwc1DisplayMap.count(HWC_DISPLAY_EXTERNAL) != 0) {
2476 auto externalDisplayId = mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL];
2477 auto& externalDisplay = mDisplays[externalDisplayId];
2478 auto externalDisplayContents =
2479 externalDisplay->cloneRequestedContents();
2480 requestedContents.push_back(std::move(externalDisplayContents));
2481 } else {
2482 // Even if an external display isn't present, we still need to send
2483 // at least two displays down to HWC1
2484 requestedContents.push_back(nullptr);
2485 }
2486
2487 // Push the hardware virtual display, if supported and present
2488 if (mHwc1MinorVersion >= 3) {
2489 if (mHwc1DisplayMap.count(HWC_DISPLAY_VIRTUAL) != 0) {
2490 auto virtualDisplayId = mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL];
2491 auto& virtualDisplay = mDisplays[virtualDisplayId];
2492 auto virtualDisplayContents =
2493 virtualDisplay->cloneRequestedContents();
2494 requestedContents.push_back(std::move(virtualDisplayContents));
2495 } else {
2496 requestedContents.push_back(nullptr);
2497 }
2498 }
2499
2500 mHwc1Contents.clear();
2501 for (auto& displayContents : requestedContents) {
2502 mHwc1Contents.push_back(displayContents.get());
2503 if (!displayContents) {
2504 continue;
2505 }
2506
2507 ALOGV("Display %zd layers:", mHwc1Contents.size() - 1);
2508 for (size_t l = 0; l < displayContents->numHwLayers; ++l) {
2509 auto& layer = displayContents->hwLayers[l];
2510 ALOGV(" %zd: %d", l, layer.compositionType);
2511 }
2512 }
2513
2514 ALOGV("Calling HWC1 prepare");
2515 {
2516 ATRACE_NAME("HWC1 prepare");
2517 mHwc1Device->prepare(mHwc1Device, mHwc1Contents.size(),
2518 mHwc1Contents.data());
2519 }
2520
2521 for (size_t c = 0; c < mHwc1Contents.size(); ++c) {
2522 auto& contents = mHwc1Contents[c];
2523 if (!contents) {
2524 continue;
2525 }
2526 ALOGV("Display %zd layers:", c);
2527 for (size_t l = 0; l < contents->numHwLayers; ++l) {
2528 ALOGV(" %zd: %d", l, contents->hwLayers[l].compositionType);
2529 }
2530 }
2531
2532 // Return the received contents to their respective displays
2533 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2534 if (mHwc1Contents[hwc1Id] == nullptr) {
2535 continue;
2536 }
2537
2538 auto displayId = mHwc1DisplayMap[hwc1Id];
2539 auto& display = mDisplays[displayId];
2540 display->setReceivedContents(std::move(requestedContents[hwc1Id]));
2541 }
2542
2543 return true;
2544}
2545
2546Error HWC2On1Adapter::setAllDisplays()
2547{
2548 ATRACE_CALL();
2549
Dan Stozafc4e2022016-02-23 11:43:19 -08002550 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002551
2552 // Make sure we're ready to validate
2553 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2554 if (mHwc1Contents[hwc1Id] == nullptr) {
2555 continue;
2556 }
2557
2558 auto displayId = mHwc1DisplayMap[hwc1Id];
2559 auto& display = mDisplays[displayId];
2560 Error error = display->set(*mHwc1Contents[hwc1Id]);
2561 if (error != Error::None) {
2562 ALOGE("setAllDisplays: Failed to set display %zd: %s", hwc1Id,
2563 to_string(error).c_str());
2564 return error;
2565 }
2566 }
2567
2568 ALOGV("Calling HWC1 set");
2569 {
2570 ATRACE_NAME("HWC1 set");
2571 mHwc1Device->set(mHwc1Device, mHwc1Contents.size(),
2572 mHwc1Contents.data());
2573 }
2574
2575 // Add retire and release fences
2576 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2577 if (mHwc1Contents[hwc1Id] == nullptr) {
2578 continue;
2579 }
2580
2581 auto displayId = mHwc1DisplayMap[hwc1Id];
2582 auto& display = mDisplays[displayId];
2583 auto retireFenceFd = mHwc1Contents[hwc1Id]->retireFenceFd;
2584 ALOGV("setAllDisplays: Adding retire fence %d to display %zd",
2585 retireFenceFd, hwc1Id);
2586 display->addRetireFence(mHwc1Contents[hwc1Id]->retireFenceFd);
2587 display->addReleaseFences(*mHwc1Contents[hwc1Id]);
2588 }
2589
2590 return Error::None;
2591}
2592
2593void HWC2On1Adapter::hwc1Invalidate()
2594{
2595 ALOGV("Received hwc1Invalidate");
2596
Dan Stozafc4e2022016-02-23 11:43:19 -08002597 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002598
2599 // If the HWC2-side callback hasn't been registered yet, buffer this until
2600 // it is registered
2601 if (mCallbacks.count(Callback::Refresh) == 0) {
2602 mHasPendingInvalidate = true;
2603 return;
2604 }
2605
2606 const auto& callbackInfo = mCallbacks[Callback::Refresh];
2607 std::vector<hwc2_display_t> displays;
2608 for (const auto& displayPair : mDisplays) {
2609 displays.emplace_back(displayPair.first);
2610 }
2611
2612 // Call back without the state lock held
2613 lock.unlock();
2614
2615 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(callbackInfo.pointer);
2616 for (auto display : displays) {
2617 refresh(callbackInfo.data, display);
2618 }
2619}
2620
2621void HWC2On1Adapter::hwc1Vsync(int hwc1DisplayId, int64_t timestamp)
2622{
2623 ALOGV("Received hwc1Vsync(%d, %" PRId64 ")", hwc1DisplayId, timestamp);
2624
Dan Stozafc4e2022016-02-23 11:43:19 -08002625 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002626
2627 // If the HWC2-side callback hasn't been registered yet, buffer this until
2628 // it is registered
2629 if (mCallbacks.count(Callback::Vsync) == 0) {
2630 mPendingVsyncs.emplace_back(hwc1DisplayId, timestamp);
2631 return;
2632 }
2633
2634 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2635 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", hwc1DisplayId);
2636 return;
2637 }
2638
2639 const auto& callbackInfo = mCallbacks[Callback::Vsync];
2640 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
2641
2642 // Call back without the state lock held
2643 lock.unlock();
2644
2645 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(callbackInfo.pointer);
2646 vsync(callbackInfo.data, displayId, timestamp);
2647}
2648
2649void HWC2On1Adapter::hwc1Hotplug(int hwc1DisplayId, int connected)
2650{
2651 ALOGV("Received hwc1Hotplug(%d, %d)", hwc1DisplayId, connected);
2652
2653 if (hwc1DisplayId != HWC_DISPLAY_EXTERNAL) {
2654 ALOGE("hwc1Hotplug: Received hotplug for non-external display");
2655 return;
2656 }
2657
Dan Stozafc4e2022016-02-23 11:43:19 -08002658 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002659
2660 // If the HWC2-side callback hasn't been registered yet, buffer this until
2661 // it is registered
2662 if (mCallbacks.count(Callback::Hotplug) == 0) {
2663 mPendingHotplugs.emplace_back(hwc1DisplayId, connected);
2664 return;
2665 }
2666
2667 hwc2_display_t displayId = UINT64_MAX;
2668 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2669 if (connected == 0) {
2670 ALOGW("hwc1Hotplug: Received disconnect for unconnected display");
2671 return;
2672 }
2673
2674 // Create a new display on connect
2675 auto display = std::make_shared<HWC2On1Adapter::Display>(*this,
2676 HWC2::DisplayType::Physical);
2677 display->setHwc1Id(HWC_DISPLAY_EXTERNAL);
2678 display->populateConfigs();
2679 displayId = display->getId();
2680 mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL] = displayId;
2681 mDisplays.emplace(displayId, std::move(display));
2682 } else {
2683 if (connected != 0) {
2684 ALOGW("hwc1Hotplug: Received connect for previously connected "
2685 "display");
2686 return;
2687 }
2688
2689 // Disconnect an existing display
2690 displayId = mHwc1DisplayMap[hwc1DisplayId];
2691 mHwc1DisplayMap.erase(HWC_DISPLAY_EXTERNAL);
2692 mDisplays.erase(displayId);
2693 }
2694
2695 const auto& callbackInfo = mCallbacks[Callback::Hotplug];
2696
2697 // Call back without the state lock held
2698 lock.unlock();
2699
2700 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(callbackInfo.pointer);
2701 auto hwc2Connected = (connected == 0) ?
2702 HWC2::Connection::Disconnected : HWC2::Connection::Connected;
2703 hotplug(callbackInfo.data, displayId, static_cast<int32_t>(hwc2Connected));
2704}
2705
2706} // namespace android