blob: be4f6f078eca2d0f58aa9720c28c37f2000e9298 [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;
Fabien Sanglard16ab8192017-01-31 12:12:10 -08001298 hwc1Layer.acquireFenceFd = -1;
Dan Stozac6998d22015-09-24 17:03:36 -07001299 layer->applyState(hwc1Layer, applyAllState);
1300 }
1301
1302 mHwc1RequestedContents->outbuf = mOutputBuffer.getBuffer();
1303 mHwc1RequestedContents->outbufAcquireFenceFd = mOutputBuffer.getFence();
1304
1305 prepareFramebufferTarget();
1306
1307 return true;
1308}
1309
1310static void cloneHWCRegion(hwc_region_t& region)
1311{
1312 auto size = sizeof(hwc_rect_t) * region.numRects;
1313 auto newRects = static_cast<hwc_rect_t*>(std::malloc(size));
1314 std::copy_n(region.rects, region.numRects, newRects);
1315 region.rects = newRects;
1316}
1317
1318HWC2On1Adapter::Display::HWC1Contents
1319 HWC2On1Adapter::Display::cloneRequestedContents() const
1320{
1321 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1322
1323 size_t size = sizeof(hwc_display_contents_1_t) +
1324 sizeof(hwc_layer_1_t) * (mHwc1RequestedContents->numHwLayers);
1325 auto contents = static_cast<hwc_display_contents_1_t*>(std::malloc(size));
1326 std::memcpy(contents, mHwc1RequestedContents.get(), size);
1327 for (size_t layerId = 0; layerId < contents->numHwLayers; ++layerId) {
1328 auto& layer = contents->hwLayers[layerId];
1329 // Deep copy the regions to avoid double-frees
1330 cloneHWCRegion(layer.visibleRegionScreen);
1331 cloneHWCRegion(layer.surfaceDamage);
1332 }
1333 return HWC1Contents(contents);
1334}
1335
1336void HWC2On1Adapter::Display::setReceivedContents(HWC1Contents contents)
1337{
1338 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1339
1340 mHwc1ReceivedContents = std::move(contents);
1341
1342 mChanges.reset(new Changes);
1343
1344 size_t numLayers = mHwc1ReceivedContents->numHwLayers;
1345 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1346 const auto& receivedLayer = mHwc1ReceivedContents->hwLayers[hwc1Id];
1347 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1348 ALOGE_IF(receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET,
1349 "setReceivedContents: HWC1 layer %zd doesn't have a"
1350 " matching HWC2 layer, and isn't the framebuffer target",
1351 hwc1Id);
1352 continue;
1353 }
1354
1355 Layer& layer = *mHwc1LayerMap[hwc1Id];
1356 updateTypeChanges(receivedLayer, layer);
1357 updateLayerRequests(receivedLayer, layer);
1358 }
1359}
1360
1361bool HWC2On1Adapter::Display::hasChanges() const
1362{
1363 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1364 return mChanges != nullptr;
1365}
1366
1367Error HWC2On1Adapter::Display::set(hwc_display_contents_1& hwcContents)
1368{
1369 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1370
1371 if (!mChanges || (mChanges->getNumTypes() > 0)) {
1372 ALOGE("[%" PRIu64 "] set failed: not validated", mId);
1373 return Error::NotValidated;
1374 }
1375
1376 // Set up the client/framebuffer target
1377 auto numLayers = hwcContents.numHwLayers;
1378
1379 // Close acquire fences on FRAMEBUFFER layers, since they will not be used
1380 // by HWC
1381 for (size_t l = 0; l < numLayers - 1; ++l) {
1382 auto& layer = hwcContents.hwLayers[l];
1383 if (layer.compositionType == HWC_FRAMEBUFFER) {
1384 ALOGV("Closing fence %d for layer %zd", layer.acquireFenceFd, l);
1385 close(layer.acquireFenceFd);
1386 layer.acquireFenceFd = -1;
1387 }
1388 }
1389
1390 auto& clientTargetLayer = hwcContents.hwLayers[numLayers - 1];
1391 if (clientTargetLayer.compositionType == HWC_FRAMEBUFFER_TARGET) {
1392 clientTargetLayer.handle = mClientTarget.getBuffer();
1393 clientTargetLayer.acquireFenceFd = mClientTarget.getFence();
1394 } else {
1395 ALOGE("[%" PRIu64 "] set: last HWC layer wasn't FRAMEBUFFER_TARGET",
1396 mId);
1397 }
1398
1399 mChanges.reset();
1400
1401 return Error::None;
1402}
1403
1404void HWC2On1Adapter::Display::addRetireFence(int fenceFd)
1405{
1406 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1407 mRetireFence.add(fenceFd);
1408}
1409
1410void HWC2On1Adapter::Display::addReleaseFences(
1411 const hwc_display_contents_1_t& hwcContents)
1412{
1413 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1414
1415 size_t numLayers = hwcContents.numHwLayers;
1416 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1417 const auto& receivedLayer = hwcContents.hwLayers[hwc1Id];
1418 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1419 if (receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET) {
1420 ALOGE("addReleaseFences: HWC1 layer %zd doesn't have a"
1421 " matching HWC2 layer, and isn't the framebuffer"
1422 " target", hwc1Id);
1423 }
1424 // Close the framebuffer target release fence since we will use the
1425 // display retire fence instead
1426 if (receivedLayer.releaseFenceFd != -1) {
1427 close(receivedLayer.releaseFenceFd);
1428 }
1429 continue;
1430 }
1431
1432 Layer& layer = *mHwc1LayerMap[hwc1Id];
1433 ALOGV("Adding release fence %d to layer %" PRIu64,
1434 receivedLayer.releaseFenceFd, layer.getId());
1435 layer.addReleaseFence(receivedLayer.releaseFenceFd);
1436 }
1437}
1438
Dan Stoza5df2a862016-03-24 16:19:37 -07001439bool HWC2On1Adapter::Display::hasColorTransform() const
1440{
1441 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1442 return mHasColorTransform;
1443}
1444
Dan Stozac6998d22015-09-24 17:03:36 -07001445static std::string hwc1CompositionString(int32_t type)
1446{
1447 switch (type) {
1448 case HWC_FRAMEBUFFER: return "Framebuffer";
1449 case HWC_OVERLAY: return "Overlay";
1450 case HWC_BACKGROUND: return "Background";
1451 case HWC_FRAMEBUFFER_TARGET: return "FramebufferTarget";
1452 case HWC_SIDEBAND: return "Sideband";
1453 case HWC_CURSOR_OVERLAY: return "CursorOverlay";
1454 default:
1455 return std::string("Unknown (") + std::to_string(type) + ")";
1456 }
1457}
1458
1459static std::string hwc1TransformString(int32_t transform)
1460{
1461 switch (transform) {
1462 case 0: return "None";
1463 case HWC_TRANSFORM_FLIP_H: return "FlipH";
1464 case HWC_TRANSFORM_FLIP_V: return "FlipV";
1465 case HWC_TRANSFORM_ROT_90: return "Rotate90";
1466 case HWC_TRANSFORM_ROT_180: return "Rotate180";
1467 case HWC_TRANSFORM_ROT_270: return "Rotate270";
1468 case HWC_TRANSFORM_FLIP_H_ROT_90: return "FlipHRotate90";
1469 case HWC_TRANSFORM_FLIP_V_ROT_90: return "FlipVRotate90";
1470 default:
1471 return std::string("Unknown (") + std::to_string(transform) + ")";
1472 }
1473}
1474
1475static std::string hwc1BlendModeString(int32_t mode)
1476{
1477 switch (mode) {
1478 case HWC_BLENDING_NONE: return "None";
1479 case HWC_BLENDING_PREMULT: return "Premultiplied";
1480 case HWC_BLENDING_COVERAGE: return "Coverage";
1481 default:
1482 return std::string("Unknown (") + std::to_string(mode) + ")";
1483 }
1484}
1485
1486static std::string rectString(hwc_rect_t rect)
1487{
1488 std::stringstream output;
1489 output << "[" << rect.left << ", " << rect.top << ", ";
1490 output << rect.right << ", " << rect.bottom << "]";
1491 return output.str();
1492}
1493
1494static std::string approximateFloatString(float f)
1495{
1496 if (static_cast<int32_t>(f) == f) {
1497 return std::to_string(static_cast<int32_t>(f));
1498 }
1499 int32_t truncated = static_cast<int32_t>(f * 10);
1500 bool approximate = (static_cast<float>(truncated) != f * 10);
1501 const size_t BUFFER_SIZE = 32;
1502 char buffer[BUFFER_SIZE] = {};
1503 auto bytesWritten = snprintf(buffer, BUFFER_SIZE,
1504 "%s%.1f", approximate ? "~" : "", f);
1505 return std::string(buffer, bytesWritten);
1506}
1507
1508static std::string frectString(hwc_frect_t frect)
1509{
1510 std::stringstream output;
1511 output << "[" << approximateFloatString(frect.left) << ", ";
1512 output << approximateFloatString(frect.top) << ", ";
1513 output << approximateFloatString(frect.right) << ", ";
1514 output << approximateFloatString(frect.bottom) << "]";
1515 return output.str();
1516}
1517
1518static std::string colorString(hwc_color_t color)
1519{
1520 std::stringstream output;
1521 output << "RGBA [";
1522 output << static_cast<int32_t>(color.r) << ", ";
1523 output << static_cast<int32_t>(color.g) << ", ";
1524 output << static_cast<int32_t>(color.b) << ", ";
1525 output << static_cast<int32_t>(color.a) << "]";
1526 return output.str();
1527}
1528
1529static std::string alphaString(float f)
1530{
1531 const size_t BUFFER_SIZE = 8;
1532 char buffer[BUFFER_SIZE] = {};
1533 auto bytesWritten = snprintf(buffer, BUFFER_SIZE, "%.3f", f);
1534 return std::string(buffer, bytesWritten);
1535}
1536
1537static std::string to_string(const hwc_layer_1_t& hwcLayer,
1538 int32_t hwc1MinorVersion)
1539{
1540 const char* fill = " ";
1541
1542 std::stringstream output;
1543
1544 output << " Composition: " <<
1545 hwc1CompositionString(hwcLayer.compositionType);
1546
1547 if (hwcLayer.compositionType == HWC_BACKGROUND) {
1548 output << " Color: " << colorString(hwcLayer.backgroundColor) << '\n';
1549 } else if (hwcLayer.compositionType == HWC_SIDEBAND) {
1550 output << " Stream: " << hwcLayer.sidebandStream << '\n';
1551 } else {
1552 output << " Buffer: " << hwcLayer.handle << "/" <<
1553 hwcLayer.acquireFenceFd << '\n';
1554 }
1555
1556 output << fill << "Display frame: " << rectString(hwcLayer.displayFrame) <<
1557 '\n';
1558
1559 output << fill << "Source crop: ";
1560 if (hwc1MinorVersion >= 3) {
1561 output << frectString(hwcLayer.sourceCropf) << '\n';
1562 } else {
1563 output << rectString(hwcLayer.sourceCropi) << '\n';
1564 }
1565
1566 output << fill << "Transform: " << hwc1TransformString(hwcLayer.transform);
1567 output << " Blend mode: " << hwc1BlendModeString(hwcLayer.blending);
1568 if (hwcLayer.planeAlpha != 0xFF) {
1569 output << " Alpha: " << alphaString(hwcLayer.planeAlpha / 255.0f);
1570 }
1571 output << '\n';
1572
1573 if (hwcLayer.hints != 0) {
1574 output << fill << "Hints:";
1575 if ((hwcLayer.hints & HWC_HINT_TRIPLE_BUFFER) != 0) {
1576 output << " TripleBuffer";
1577 }
1578 if ((hwcLayer.hints & HWC_HINT_CLEAR_FB) != 0) {
1579 output << " ClearFB";
1580 }
1581 output << '\n';
1582 }
1583
1584 if (hwcLayer.flags != 0) {
1585 output << fill << "Flags:";
1586 if ((hwcLayer.flags & HWC_SKIP_LAYER) != 0) {
1587 output << " SkipLayer";
1588 }
1589 if ((hwcLayer.flags & HWC_IS_CURSOR_LAYER) != 0) {
1590 output << " IsCursorLayer";
1591 }
1592 output << '\n';
1593 }
1594
1595 return output.str();
1596}
1597
1598static std::string to_string(const hwc_display_contents_1_t& hwcContents,
1599 int32_t hwc1MinorVersion)
1600{
1601 const char* fill = " ";
1602
1603 std::stringstream output;
1604 output << fill << "Geometry changed: " <<
1605 ((hwcContents.flags & HWC_GEOMETRY_CHANGED) != 0 ? "Y\n" : "N\n");
1606
1607 output << fill << hwcContents.numHwLayers << " Layer" <<
1608 ((hwcContents.numHwLayers == 1) ? "\n" : "s\n");
1609 for (size_t layer = 0; layer < hwcContents.numHwLayers; ++layer) {
1610 output << fill << " Layer " << layer;
1611 output << to_string(hwcContents.hwLayers[layer], hwc1MinorVersion);
1612 }
1613
1614 if (hwcContents.outbuf != nullptr) {
1615 output << fill << "Output buffer: " << hwcContents.outbuf << "/" <<
1616 hwcContents.outbufAcquireFenceFd << '\n';
1617 }
1618
1619 return output.str();
1620}
1621
1622std::string HWC2On1Adapter::Display::dump() const
1623{
1624 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1625
1626 std::stringstream output;
1627
1628 output << " Display " << mId << ": ";
1629 output << to_string(mType) << " ";
1630 output << "HWC1 ID: " << mHwc1Id << " ";
1631 output << "Power mode: " << to_string(mPowerMode) << " ";
1632 output << "Vsync: " << to_string(mVsyncEnabled) << '\n';
1633
Dan Stoza076ac672016-03-14 10:47:53 -07001634 output << " Color modes [active]:";
1635 for (const auto& mode : mColorModes) {
1636 if (mode == mActiveColorMode) {
1637 output << " [" << mode << ']';
Dan Stozac6998d22015-09-24 17:03:36 -07001638 } else {
Dan Stoza076ac672016-03-14 10:47:53 -07001639 output << " " << mode;
Dan Stozac6998d22015-09-24 17:03:36 -07001640 }
1641 }
1642 output << '\n';
1643
Dan Stoza076ac672016-03-14 10:47:53 -07001644 output << " " << mConfigs.size() << " Config" <<
1645 (mConfigs.size() == 1 ? "" : "s") << " (* active)\n";
1646 for (const auto& config : mConfigs) {
1647 output << (config == mActiveConfig ? " * " : " ");
1648 output << config->toString(true) << '\n';
1649 }
1650
Dan Stozac6998d22015-09-24 17:03:36 -07001651 output << " " << mLayers.size() << " Layer" <<
1652 (mLayers.size() == 1 ? "" : "s") << '\n';
1653 for (const auto& layer : mLayers) {
1654 output << layer->dump();
1655 }
1656
1657 output << " Client target: " << mClientTarget.getBuffer() << '\n';
1658
1659 if (mOutputBuffer.getBuffer() != nullptr) {
1660 output << " Output buffer: " << mOutputBuffer.getBuffer() << '\n';
1661 }
1662
1663 if (mHwc1ReceivedContents) {
1664 output << " Last received HWC1 state\n";
1665 output << to_string(*mHwc1ReceivedContents, mDevice.mHwc1MinorVersion);
1666 } else if (mHwc1RequestedContents) {
1667 output << " Last requested HWC1 state\n";
1668 output << to_string(*mHwc1RequestedContents, mDevice.mHwc1MinorVersion);
1669 }
1670
1671 return output.str();
1672}
1673
1674void HWC2On1Adapter::Display::Config::setAttribute(HWC2::Attribute attribute,
1675 int32_t value)
1676{
1677 mAttributes[attribute] = value;
1678}
1679
1680int32_t HWC2On1Adapter::Display::Config::getAttribute(Attribute attribute) const
1681{
1682 if (mAttributes.count(attribute) == 0) {
1683 return -1;
1684 }
1685 return mAttributes.at(attribute);
1686}
1687
Dan Stoza076ac672016-03-14 10:47:53 -07001688void HWC2On1Adapter::Display::Config::setHwc1Id(uint32_t id)
1689{
Michael Wright28f24d02016-07-12 13:30:53 -07001690 android_color_mode_t colorMode = static_cast<android_color_mode_t>(getAttribute(ColorMode));
1691 mHwc1Ids.emplace(colorMode, id);
Dan Stoza076ac672016-03-14 10:47:53 -07001692}
1693
1694bool HWC2On1Adapter::Display::Config::hasHwc1Id(uint32_t id) const
1695{
1696 for (const auto& idPair : mHwc1Ids) {
1697 if (id == idPair.second) {
1698 return true;
1699 }
1700 }
1701 return false;
1702}
1703
Michael Wright28f24d02016-07-12 13:30:53 -07001704Error HWC2On1Adapter::Display::Config::getColorModeForHwc1Id(
1705 uint32_t id, android_color_mode_t* outMode) const
Dan Stoza076ac672016-03-14 10:47:53 -07001706{
1707 for (const auto& idPair : mHwc1Ids) {
1708 if (id == idPair.second) {
Michael Wright28f24d02016-07-12 13:30:53 -07001709 *outMode = idPair.first;
1710 return Error::None;
Dan Stoza076ac672016-03-14 10:47:53 -07001711 }
1712 }
Michael Wright28f24d02016-07-12 13:30:53 -07001713 ALOGE("Unable to find color mode for HWC ID %" PRIu32 " on config %u", id, mId);
1714 return Error::BadParameter;
Dan Stoza076ac672016-03-14 10:47:53 -07001715}
1716
Michael Wright28f24d02016-07-12 13:30:53 -07001717Error HWC2On1Adapter::Display::Config::getHwc1IdForColorMode(android_color_mode_t mode,
Dan Stoza076ac672016-03-14 10:47:53 -07001718 uint32_t* outId) const
1719{
1720 for (const auto& idPair : mHwc1Ids) {
1721 if (mode == idPair.first) {
1722 *outId = idPair.second;
1723 return Error::None;
1724 }
1725 }
1726 ALOGE("Unable to find HWC1 ID for color mode %d on config %u", mode, mId);
1727 return Error::BadParameter;
1728}
1729
1730bool HWC2On1Adapter::Display::Config::merge(const Config& other)
1731{
1732 auto attributes = {HWC2::Attribute::Width, HWC2::Attribute::Height,
1733 HWC2::Attribute::VsyncPeriod, HWC2::Attribute::DpiX,
1734 HWC2::Attribute::DpiY};
1735 for (auto attribute : attributes) {
1736 if (getAttribute(attribute) != other.getAttribute(attribute)) {
1737 return false;
1738 }
1739 }
Michael Wright28f24d02016-07-12 13:30:53 -07001740 android_color_mode_t otherColorMode =
1741 static_cast<android_color_mode_t>(other.getAttribute(ColorMode));
1742 if (mHwc1Ids.count(otherColorMode) != 0) {
Dan Stoza076ac672016-03-14 10:47:53 -07001743 ALOGE("Attempted to merge two configs (%u and %u) which appear to be "
Michael Wright28f24d02016-07-12 13:30:53 -07001744 "identical", mHwc1Ids.at(otherColorMode),
1745 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001746 return false;
1747 }
Michael Wright28f24d02016-07-12 13:30:53 -07001748 mHwc1Ids.emplace(otherColorMode,
1749 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001750 return true;
1751}
1752
Michael Wright28f24d02016-07-12 13:30:53 -07001753std::set<android_color_mode_t> HWC2On1Adapter::Display::Config::getColorModes() const
Dan Stoza076ac672016-03-14 10:47:53 -07001754{
Michael Wright28f24d02016-07-12 13:30:53 -07001755 std::set<android_color_mode_t> colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001756 for (const auto& idPair : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001757 colorModes.emplace(idPair.first);
Dan Stoza076ac672016-03-14 10:47:53 -07001758 }
Michael Wright28f24d02016-07-12 13:30:53 -07001759 return colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001760}
1761
1762std::string HWC2On1Adapter::Display::Config::toString(bool splitLine) const
Dan Stozac6998d22015-09-24 17:03:36 -07001763{
1764 std::string output;
1765
1766 const size_t BUFFER_SIZE = 100;
1767 char buffer[BUFFER_SIZE] = {};
1768 auto writtenBytes = snprintf(buffer, BUFFER_SIZE,
Dan Stoza076ac672016-03-14 10:47:53 -07001769 "%u x %u", mAttributes.at(HWC2::Attribute::Width),
Dan Stozac6998d22015-09-24 17:03:36 -07001770 mAttributes.at(HWC2::Attribute::Height));
1771 output.append(buffer, writtenBytes);
1772
1773 if (mAttributes.count(HWC2::Attribute::VsyncPeriod) != 0) {
1774 std::memset(buffer, 0, BUFFER_SIZE);
1775 writtenBytes = snprintf(buffer, BUFFER_SIZE, " @ %.1f Hz",
1776 1e9 / mAttributes.at(HWC2::Attribute::VsyncPeriod));
1777 output.append(buffer, writtenBytes);
1778 }
1779
1780 if (mAttributes.count(HWC2::Attribute::DpiX) != 0 &&
1781 mAttributes.at(HWC2::Attribute::DpiX) != -1) {
1782 std::memset(buffer, 0, BUFFER_SIZE);
1783 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1784 ", DPI: %.1f x %.1f",
1785 mAttributes.at(HWC2::Attribute::DpiX) / 1000.0f,
1786 mAttributes.at(HWC2::Attribute::DpiY) / 1000.0f);
1787 output.append(buffer, writtenBytes);
1788 }
1789
Dan Stoza076ac672016-03-14 10:47:53 -07001790 std::memset(buffer, 0, BUFFER_SIZE);
1791 if (splitLine) {
1792 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1793 "\n HWC1 ID/Color transform:");
1794 } else {
1795 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1796 ", HWC1 ID/Color transform:");
1797 }
1798 output.append(buffer, writtenBytes);
1799
1800
1801 for (const auto& id : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001802 android_color_mode_t colorMode = id.first;
Dan Stoza076ac672016-03-14 10:47:53 -07001803 uint32_t hwc1Id = id.second;
1804 std::memset(buffer, 0, BUFFER_SIZE);
Michael Wright28f24d02016-07-12 13:30:53 -07001805 if (colorMode == mDisplay.mActiveColorMode) {
Dan Stoza076ac672016-03-14 10:47:53 -07001806 writtenBytes = snprintf(buffer, BUFFER_SIZE, " [%u/%d]", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001807 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001808 } else {
1809 writtenBytes = snprintf(buffer, BUFFER_SIZE, " %u/%d", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001810 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001811 }
1812 output.append(buffer, writtenBytes);
1813 }
1814
Dan Stozac6998d22015-09-24 17:03:36 -07001815 return output;
1816}
1817
1818std::shared_ptr<const HWC2On1Adapter::Display::Config>
1819 HWC2On1Adapter::Display::getConfig(hwc2_config_t configId) const
1820{
1821 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
1822 return nullptr;
1823 }
1824 return mConfigs[configId];
1825}
1826
Dan Stoza076ac672016-03-14 10:47:53 -07001827void HWC2On1Adapter::Display::populateColorModes()
1828{
Michael Wright28f24d02016-07-12 13:30:53 -07001829 mColorModes = mConfigs[0]->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001830 for (const auto& config : mConfigs) {
Michael Wright28f24d02016-07-12 13:30:53 -07001831 std::set<android_color_mode_t> intersection;
1832 auto configModes = config->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001833 std::set_intersection(mColorModes.cbegin(), mColorModes.cend(),
1834 configModes.cbegin(), configModes.cend(),
1835 std::inserter(intersection, intersection.begin()));
1836 std::swap(intersection, mColorModes);
1837 }
1838}
1839
1840void HWC2On1Adapter::Display::initializeActiveConfig()
1841{
1842 if (mDevice.mHwc1Device->getActiveConfig == nullptr) {
1843 ALOGV("getActiveConfig is null, choosing config 0");
1844 mActiveConfig = mConfigs[0];
Michael Wright28f24d02016-07-12 13:30:53 -07001845 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001846 return;
1847 }
1848
1849 auto activeConfig = mDevice.mHwc1Device->getActiveConfig(
1850 mDevice.mHwc1Device, mHwc1Id);
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001851
1852 // Some devices startup without an activeConfig:
1853 // We need to set one ourselves.
1854 if (activeConfig == HWC_ERROR) {
1855 ALOGV("There is no active configuration: Picking the first one: 0.");
1856 const int defaultIndex = 0;
1857 mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, mHwc1Id, defaultIndex);
1858 activeConfig = defaultIndex;
1859 }
1860
1861 for (const auto& config : mConfigs) {
1862 if (config->hasHwc1Id(activeConfig)) {
1863 ALOGE("Setting active config to %d for HWC1 config %u", config->getId(), activeConfig);
1864 mActiveConfig = config;
1865 if (config->getColorModeForHwc1Id(activeConfig, &mActiveColorMode) != Error::None) {
1866 // This should never happen since we checked for the config's presence before
1867 // setting it as active.
1868 ALOGE("Unable to find color mode for active HWC1 config %d", config->getId());
1869 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001870 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001871 break;
Dan Stoza076ac672016-03-14 10:47:53 -07001872 }
1873 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001874 if (!mActiveConfig) {
1875 ALOGV("Unable to find active HWC1 config %u, defaulting to "
1876 "config 0", activeConfig);
1877 mActiveConfig = mConfigs[0];
1878 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
1879 }
1880
1881
1882
1883
Dan Stoza076ac672016-03-14 10:47:53 -07001884}
1885
Dan Stozac6998d22015-09-24 17:03:36 -07001886void HWC2On1Adapter::Display::reallocateHwc1Contents()
1887{
1888 // Allocate an additional layer for the framebuffer target
1889 auto numLayers = mLayers.size() + 1;
1890 size_t size = sizeof(hwc_display_contents_1_t) +
1891 sizeof(hwc_layer_1_t) * numLayers;
1892 ALOGV("[%" PRIu64 "] reallocateHwc1Contents creating %zd layer%s", mId,
1893 numLayers, numLayers != 1 ? "s" : "");
1894 auto contents =
1895 static_cast<hwc_display_contents_1_t*>(std::calloc(size, 1));
1896 contents->numHwLayers = numLayers;
1897 mHwc1RequestedContents.reset(contents);
1898}
1899
1900void HWC2On1Adapter::Display::assignHwc1LayerIds()
1901{
1902 mHwc1LayerMap.clear();
1903 size_t nextHwc1Id = 0;
1904 for (auto& layer : mLayers) {
1905 mHwc1LayerMap[nextHwc1Id] = layer;
1906 layer->setHwc1Id(nextHwc1Id++);
1907 }
1908}
1909
1910void HWC2On1Adapter::Display::updateTypeChanges(const hwc_layer_1_t& hwc1Layer,
1911 const Layer& layer)
1912{
1913 auto layerId = layer.getId();
1914 switch (hwc1Layer.compositionType) {
1915 case HWC_FRAMEBUFFER:
1916 if (layer.getCompositionType() != Composition::Client) {
1917 mChanges->addTypeChange(layerId, Composition::Client);
1918 }
1919 break;
1920 case HWC_OVERLAY:
1921 if (layer.getCompositionType() != Composition::Device) {
1922 mChanges->addTypeChange(layerId, Composition::Device);
1923 }
1924 break;
1925 case HWC_BACKGROUND:
1926 ALOGE_IF(layer.getCompositionType() != Composition::SolidColor,
1927 "updateTypeChanges: HWC1 requested BACKGROUND, but HWC2"
1928 " wasn't expecting SolidColor");
1929 break;
1930 case HWC_FRAMEBUFFER_TARGET:
1931 // Do nothing, since it shouldn't be modified by HWC1
1932 break;
1933 case HWC_SIDEBAND:
1934 ALOGE_IF(layer.getCompositionType() != Composition::Sideband,
1935 "updateTypeChanges: HWC1 requested SIDEBAND, but HWC2"
1936 " wasn't expecting Sideband");
1937 break;
1938 case HWC_CURSOR_OVERLAY:
1939 ALOGE_IF(layer.getCompositionType() != Composition::Cursor,
1940 "updateTypeChanges: HWC1 requested CURSOR_OVERLAY, but"
1941 " HWC2 wasn't expecting Cursor");
1942 break;
1943 }
1944}
1945
1946void HWC2On1Adapter::Display::updateLayerRequests(
1947 const hwc_layer_1_t& hwc1Layer, const Layer& layer)
1948{
1949 if ((hwc1Layer.hints & HWC_HINT_CLEAR_FB) != 0) {
1950 mChanges->addLayerRequest(layer.getId(),
1951 LayerRequest::ClearClientTarget);
1952 }
1953}
1954
1955void HWC2On1Adapter::Display::prepareFramebufferTarget()
1956{
1957 // We check that mActiveConfig is valid in Display::prepare
1958 int32_t width = mActiveConfig->getAttribute(Attribute::Width);
1959 int32_t height = mActiveConfig->getAttribute(Attribute::Height);
1960
1961 auto& hwc1Target = mHwc1RequestedContents->hwLayers[mLayers.size()];
1962 hwc1Target.compositionType = HWC_FRAMEBUFFER_TARGET;
1963 hwc1Target.releaseFenceFd = -1;
1964 hwc1Target.hints = 0;
1965 hwc1Target.flags = 0;
1966 hwc1Target.transform = 0;
1967 hwc1Target.blending = HWC_BLENDING_PREMULT;
1968 if (mDevice.getHwc1MinorVersion() < 3) {
1969 hwc1Target.sourceCropi = {0, 0, width, height};
1970 } else {
1971 hwc1Target.sourceCropf = {0.0f, 0.0f, static_cast<float>(width),
1972 static_cast<float>(height)};
1973 }
1974 hwc1Target.displayFrame = {0, 0, width, height};
1975 hwc1Target.planeAlpha = 255;
1976 hwc1Target.visibleRegionScreen.numRects = 1;
1977 auto rects = static_cast<hwc_rect_t*>(std::malloc(sizeof(hwc_rect_t)));
1978 rects[0].left = 0;
1979 rects[0].top = 0;
1980 rects[0].right = width;
1981 rects[0].bottom = height;
1982 hwc1Target.visibleRegionScreen.rects = rects;
1983
1984 // We will set this to the correct value in set
1985 hwc1Target.acquireFenceFd = -1;
1986}
1987
1988// Layer functions
1989
1990std::atomic<hwc2_layer_t> HWC2On1Adapter::Layer::sNextId(1);
1991
1992HWC2On1Adapter::Layer::Layer(Display& display)
1993 : mId(sNextId++),
1994 mDisplay(display),
Dan Stozafc4e2022016-02-23 11:43:19 -08001995 mDirtyCount(0),
1996 mBuffer(),
1997 mSurfaceDamage(),
Dan Stozac6998d22015-09-24 17:03:36 -07001998 mBlendMode(*this, BlendMode::None),
1999 mColor(*this, {0, 0, 0, 0}),
2000 mCompositionType(*this, Composition::Invalid),
2001 mDisplayFrame(*this, {0, 0, -1, -1}),
2002 mPlaneAlpha(*this, 0.0f),
2003 mSidebandStream(*this, nullptr),
2004 mSourceCrop(*this, {0.0f, 0.0f, -1.0f, -1.0f}),
2005 mTransform(*this, Transform::None),
2006 mVisibleRegion(*this, std::vector<hwc_rect_t>()),
2007 mZ(0),
Dan Stozafc4e2022016-02-23 11:43:19 -08002008 mReleaseFence(),
Dan Stozac6998d22015-09-24 17:03:36 -07002009 mHwc1Id(0),
Dan Stoza5df2a862016-03-24 16:19:37 -07002010 mHasUnsupportedDataspace(false),
Dan Stozac6998d22015-09-24 17:03:36 -07002011 mHasUnsupportedPlaneAlpha(false) {}
2012
2013bool HWC2On1Adapter::SortLayersByZ::operator()(
2014 const std::shared_ptr<Layer>& lhs, const std::shared_ptr<Layer>& rhs)
2015{
2016 return lhs->getZ() < rhs->getZ();
2017}
2018
2019Error HWC2On1Adapter::Layer::setBuffer(buffer_handle_t buffer,
2020 int32_t acquireFence)
2021{
2022 ALOGV("Setting acquireFence to %d for layer %" PRIu64, acquireFence, mId);
2023 mBuffer.setBuffer(buffer);
2024 mBuffer.setFence(acquireFence);
2025 return Error::None;
2026}
2027
2028Error HWC2On1Adapter::Layer::setCursorPosition(int32_t x, int32_t y)
2029{
2030 if (mCompositionType.getValue() != Composition::Cursor) {
2031 return Error::BadLayer;
2032 }
2033
2034 if (mDisplay.hasChanges()) {
2035 return Error::NotValidated;
2036 }
2037
2038 auto displayId = mDisplay.getHwc1Id();
2039 auto hwc1Device = mDisplay.getDevice().getHwc1Device();
2040 hwc1Device->setCursorPositionAsync(hwc1Device, displayId, x, y);
2041 return Error::None;
2042}
2043
2044Error HWC2On1Adapter::Layer::setSurfaceDamage(hwc_region_t damage)
2045{
2046 mSurfaceDamage.resize(damage.numRects);
2047 std::copy_n(damage.rects, damage.numRects, mSurfaceDamage.begin());
2048 return Error::None;
2049}
2050
2051// Layer state functions
2052
2053Error HWC2On1Adapter::Layer::setBlendMode(BlendMode mode)
2054{
2055 mBlendMode.setPending(mode);
2056 return Error::None;
2057}
2058
2059Error HWC2On1Adapter::Layer::setColor(hwc_color_t color)
2060{
2061 mColor.setPending(color);
2062 return Error::None;
2063}
2064
2065Error HWC2On1Adapter::Layer::setCompositionType(Composition type)
2066{
2067 mCompositionType.setPending(type);
2068 return Error::None;
2069}
2070
Dan Stoza5df2a862016-03-24 16:19:37 -07002071Error HWC2On1Adapter::Layer::setDataspace(android_dataspace_t dataspace)
2072{
2073 mHasUnsupportedDataspace = (dataspace != HAL_DATASPACE_UNKNOWN);
2074 return Error::None;
2075}
2076
Dan Stozac6998d22015-09-24 17:03:36 -07002077Error HWC2On1Adapter::Layer::setDisplayFrame(hwc_rect_t frame)
2078{
2079 mDisplayFrame.setPending(frame);
2080 return Error::None;
2081}
2082
2083Error HWC2On1Adapter::Layer::setPlaneAlpha(float alpha)
2084{
2085 mPlaneAlpha.setPending(alpha);
2086 return Error::None;
2087}
2088
2089Error HWC2On1Adapter::Layer::setSidebandStream(const native_handle_t* stream)
2090{
2091 mSidebandStream.setPending(stream);
2092 return Error::None;
2093}
2094
2095Error HWC2On1Adapter::Layer::setSourceCrop(hwc_frect_t crop)
2096{
2097 mSourceCrop.setPending(crop);
2098 return Error::None;
2099}
2100
2101Error HWC2On1Adapter::Layer::setTransform(Transform transform)
2102{
2103 mTransform.setPending(transform);
2104 return Error::None;
2105}
2106
2107Error HWC2On1Adapter::Layer::setVisibleRegion(hwc_region_t rawVisible)
2108{
2109 std::vector<hwc_rect_t> visible(rawVisible.rects,
2110 rawVisible.rects + rawVisible.numRects);
2111 mVisibleRegion.setPending(std::move(visible));
2112 return Error::None;
2113}
2114
2115Error HWC2On1Adapter::Layer::setZ(uint32_t z)
2116{
2117 mZ = z;
2118 return Error::None;
2119}
2120
2121void HWC2On1Adapter::Layer::addReleaseFence(int fenceFd)
2122{
2123 ALOGV("addReleaseFence %d to layer %" PRIu64, fenceFd, mId);
2124 mReleaseFence.add(fenceFd);
2125}
2126
2127const sp<Fence>& HWC2On1Adapter::Layer::getReleaseFence() const
2128{
2129 return mReleaseFence.get();
2130}
2131
2132void HWC2On1Adapter::Layer::applyState(hwc_layer_1_t& hwc1Layer,
2133 bool applyAllState)
2134{
2135 applyCommonState(hwc1Layer, applyAllState);
2136 auto compositionType = mCompositionType.getPendingValue();
2137 if (compositionType == Composition::SolidColor) {
2138 applySolidColorState(hwc1Layer, applyAllState);
2139 } else if (compositionType == Composition::Sideband) {
2140 applySidebandState(hwc1Layer, applyAllState);
2141 } else {
2142 applyBufferState(hwc1Layer);
2143 }
2144 applyCompositionType(hwc1Layer, applyAllState);
2145}
2146
2147// Layer dump helpers
2148
2149static std::string regionStrings(const std::vector<hwc_rect_t>& visibleRegion,
2150 const std::vector<hwc_rect_t>& surfaceDamage)
2151{
2152 std::string regions;
2153 regions += " Visible Region";
2154 regions.resize(40, ' ');
2155 regions += "Surface Damage\n";
2156
2157 size_t numPrinted = 0;
2158 size_t maxSize = std::max(visibleRegion.size(), surfaceDamage.size());
2159 while (numPrinted < maxSize) {
2160 std::string line(" ");
2161 if (visibleRegion.empty() && numPrinted == 0) {
2162 line += "None";
2163 } else if (numPrinted < visibleRegion.size()) {
2164 line += rectString(visibleRegion[numPrinted]);
2165 }
2166 line.resize(40, ' ');
2167 if (surfaceDamage.empty() && numPrinted == 0) {
2168 line += "None";
2169 } else if (numPrinted < surfaceDamage.size()) {
2170 line += rectString(surfaceDamage[numPrinted]);
2171 }
2172 line += '\n';
2173 regions += line;
2174 ++numPrinted;
2175 }
2176 return regions;
2177}
2178
2179std::string HWC2On1Adapter::Layer::dump() const
2180{
2181 std::stringstream output;
2182 const char* fill = " ";
2183
2184 output << fill << to_string(mCompositionType.getPendingValue());
2185 output << " Layer HWC2/1: " << mId << "/" << mHwc1Id << " ";
2186 output << "Z: " << mZ;
2187 if (mCompositionType.getValue() == HWC2::Composition::SolidColor) {
2188 output << " " << colorString(mColor.getValue());
2189 } else if (mCompositionType.getValue() == HWC2::Composition::Sideband) {
2190 output << " Handle: " << mSidebandStream.getValue() << '\n';
2191 } else {
2192 output << " Buffer: " << mBuffer.getBuffer() << "/" <<
2193 mBuffer.getFence() << '\n';
2194 output << fill << " Display frame [LTRB]: " <<
2195 rectString(mDisplayFrame.getValue()) << '\n';
2196 output << fill << " Source crop: " <<
2197 frectString(mSourceCrop.getValue()) << '\n';
2198 output << fill << " Transform: " << to_string(mTransform.getValue());
2199 output << " Blend mode: " << to_string(mBlendMode.getValue());
2200 if (mPlaneAlpha.getValue() != 1.0f) {
2201 output << " Alpha: " <<
2202 alphaString(mPlaneAlpha.getValue()) << '\n';
2203 } else {
2204 output << '\n';
2205 }
2206 output << regionStrings(mVisibleRegion.getValue(), mSurfaceDamage);
2207 }
2208 return output.str();
2209}
2210
2211static int getHwc1Blending(HWC2::BlendMode blendMode)
2212{
2213 switch (blendMode) {
2214 case BlendMode::Coverage: return HWC_BLENDING_COVERAGE;
2215 case BlendMode::Premultiplied: return HWC_BLENDING_PREMULT;
2216 default: return HWC_BLENDING_NONE;
2217 }
2218}
2219
2220void HWC2On1Adapter::Layer::applyCommonState(hwc_layer_1_t& hwc1Layer,
2221 bool applyAllState)
2222{
2223 auto minorVersion = mDisplay.getDevice().getHwc1MinorVersion();
2224 if (applyAllState || mBlendMode.isDirty()) {
2225 hwc1Layer.blending = getHwc1Blending(mBlendMode.getPendingValue());
2226 mBlendMode.latch();
2227 }
2228 if (applyAllState || mDisplayFrame.isDirty()) {
2229 hwc1Layer.displayFrame = mDisplayFrame.getPendingValue();
2230 mDisplayFrame.latch();
2231 }
2232 if (applyAllState || mPlaneAlpha.isDirty()) {
2233 auto pendingAlpha = mPlaneAlpha.getPendingValue();
2234 if (minorVersion < 2) {
2235 mHasUnsupportedPlaneAlpha = pendingAlpha < 1.0f;
2236 } else {
2237 hwc1Layer.planeAlpha =
2238 static_cast<uint8_t>(255.0f * pendingAlpha + 0.5f);
2239 }
2240 mPlaneAlpha.latch();
2241 }
2242 if (applyAllState || mSourceCrop.isDirty()) {
2243 if (minorVersion < 3) {
2244 auto pending = mSourceCrop.getPendingValue();
2245 hwc1Layer.sourceCropi.left =
2246 static_cast<int32_t>(std::ceil(pending.left));
2247 hwc1Layer.sourceCropi.top =
2248 static_cast<int32_t>(std::ceil(pending.top));
2249 hwc1Layer.sourceCropi.right =
2250 static_cast<int32_t>(std::floor(pending.right));
2251 hwc1Layer.sourceCropi.bottom =
2252 static_cast<int32_t>(std::floor(pending.bottom));
2253 } else {
2254 hwc1Layer.sourceCropf = mSourceCrop.getPendingValue();
2255 }
2256 mSourceCrop.latch();
2257 }
2258 if (applyAllState || mTransform.isDirty()) {
2259 hwc1Layer.transform =
2260 static_cast<uint32_t>(mTransform.getPendingValue());
2261 mTransform.latch();
2262 }
2263 if (applyAllState || mVisibleRegion.isDirty()) {
2264 auto& hwc1VisibleRegion = hwc1Layer.visibleRegionScreen;
2265
2266 std::free(const_cast<hwc_rect_t*>(hwc1VisibleRegion.rects));
2267
2268 auto pending = mVisibleRegion.getPendingValue();
2269 hwc_rect_t* newRects = static_cast<hwc_rect_t*>(
2270 std::malloc(sizeof(hwc_rect_t) * pending.size()));
2271 std::copy(pending.begin(), pending.end(), newRects);
2272 hwc1VisibleRegion.rects = const_cast<const hwc_rect_t*>(newRects);
2273 hwc1VisibleRegion.numRects = pending.size();
2274 mVisibleRegion.latch();
2275 }
2276}
2277
2278void HWC2On1Adapter::Layer::applySolidColorState(hwc_layer_1_t& hwc1Layer,
2279 bool applyAllState)
2280{
2281 if (applyAllState || mColor.isDirty()) {
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002282 // If the device does not support background color it is likely to make
2283 // assumption regarding backgroundColor and handle (both fields occupy
2284 // the same location in hwc_layer_1_t union).
2285 // To not confuse these devices we don't set background color and we
2286 // make sure handle is a null pointer.
2287 if (mDisplay.getDevice().supportsBackgroundColor()) {
2288 hwc1Layer.backgroundColor = mColor.getPendingValue();
2289 mHasUnsupportedBackgroundColor = false;
2290 } else {
2291 hwc1Layer.handle = nullptr;
2292 mHasUnsupportedBackgroundColor = true;
2293 }
Dan Stozac6998d22015-09-24 17:03:36 -07002294 mColor.latch();
2295 }
2296}
2297
2298void HWC2On1Adapter::Layer::applySidebandState(hwc_layer_1_t& hwc1Layer,
2299 bool applyAllState)
2300{
2301 if (applyAllState || mSidebandStream.isDirty()) {
2302 hwc1Layer.sidebandStream = mSidebandStream.getPendingValue();
2303 mSidebandStream.latch();
2304 }
2305}
2306
2307void HWC2On1Adapter::Layer::applyBufferState(hwc_layer_1_t& hwc1Layer)
2308{
2309 hwc1Layer.handle = mBuffer.getBuffer();
2310 hwc1Layer.acquireFenceFd = mBuffer.getFence();
2311}
2312
2313void HWC2On1Adapter::Layer::applyCompositionType(hwc_layer_1_t& hwc1Layer,
2314 bool applyAllState)
2315{
Dan Stoza5df2a862016-03-24 16:19:37 -07002316 // HWC1 never supports color transforms or dataspaces and only sometimes
2317 // supports plane alpha (depending on the version). These require us to drop
2318 // some or all layers to client composition.
2319 if (mHasUnsupportedDataspace || mHasUnsupportedPlaneAlpha ||
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002320 mDisplay.hasColorTransform() || mHasUnsupportedBackgroundColor) {
Dan Stozac6998d22015-09-24 17:03:36 -07002321 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2322 hwc1Layer.flags = HWC_SKIP_LAYER;
2323 return;
2324 }
2325
2326 if (applyAllState || mCompositionType.isDirty()) {
2327 hwc1Layer.flags = 0;
2328 switch (mCompositionType.getPendingValue()) {
2329 case Composition::Client:
2330 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2331 hwc1Layer.flags |= HWC_SKIP_LAYER;
2332 break;
2333 case Composition::Device:
2334 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2335 break;
2336 case Composition::SolidColor:
Dan Stoza5df47cb2016-09-15 16:38:42 -07002337 // In theory the following line should work, but since the HWC1
2338 // version of SurfaceFlinger never used HWC_BACKGROUND, HWC1
2339 // devices may not work correctly. To be on the safe side, we
2340 // fall back to client composition.
2341 //
2342 // hwc1Layer.compositionType = HWC_BACKGROUND;
2343 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2344 hwc1Layer.flags |= HWC_SKIP_LAYER;
Dan Stozac6998d22015-09-24 17:03:36 -07002345 break;
2346 case Composition::Cursor:
2347 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2348 if (mDisplay.getDevice().getHwc1MinorVersion() >= 4) {
2349 hwc1Layer.hints |= HWC_IS_CURSOR_LAYER;
2350 }
2351 break;
2352 case Composition::Sideband:
2353 if (mDisplay.getDevice().getHwc1MinorVersion() < 4) {
2354 hwc1Layer.compositionType = HWC_SIDEBAND;
2355 } else {
2356 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2357 hwc1Layer.flags |= HWC_SKIP_LAYER;
2358 }
2359 break;
2360 default:
2361 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2362 hwc1Layer.flags |= HWC_SKIP_LAYER;
2363 break;
2364 }
2365 ALOGV("Layer %" PRIu64 " %s set to %d", mId,
2366 to_string(mCompositionType.getPendingValue()).c_str(),
2367 hwc1Layer.compositionType);
2368 ALOGV_IF(hwc1Layer.flags & HWC_SKIP_LAYER, " and skipping");
2369 mCompositionType.latch();
2370 }
2371}
2372
2373// Adapter helpers
2374
2375void HWC2On1Adapter::populateCapabilities()
2376{
2377 ALOGV("populateCapabilities");
2378 if (mHwc1MinorVersion >= 3U) {
2379 int supportedTypes = 0;
2380 auto result = mHwc1Device->query(mHwc1Device,
2381 HWC_DISPLAY_TYPES_SUPPORTED, &supportedTypes);
Fred Fettingerc50c01e2016-06-14 17:53:10 -05002382 if ((result == 0) && ((supportedTypes & HWC_DISPLAY_VIRTUAL_BIT) != 0)) {
Dan Stozac6998d22015-09-24 17:03:36 -07002383 ALOGI("Found support for HWC virtual displays");
2384 mHwc1SupportsVirtualDisplays = true;
2385 }
2386 }
2387 if (mHwc1MinorVersion >= 4U) {
2388 mCapabilities.insert(Capability::SidebandStream);
2389 }
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002390
2391 // Check for HWC background color layer support.
2392 if (mHwc1MinorVersion >= 1U) {
2393 int backgroundColorSupported = 0;
2394 auto result = mHwc1Device->query(mHwc1Device,
2395 HWC_BACKGROUND_LAYER_SUPPORTED,
2396 &backgroundColorSupported);
2397 if ((result == 0) && (backgroundColorSupported == 1)) {
2398 ALOGV("Found support for HWC background color");
2399 mHwc1SupportsBackgroundColor = true;
2400 }
2401 }
Dan Stozac6998d22015-09-24 17:03:36 -07002402}
2403
2404HWC2On1Adapter::Display* HWC2On1Adapter::getDisplay(hwc2_display_t id)
2405{
Dan Stozafc4e2022016-02-23 11:43:19 -08002406 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002407
2408 auto display = mDisplays.find(id);
2409 if (display == mDisplays.end()) {
2410 return nullptr;
2411 }
2412
2413 return display->second.get();
2414}
2415
2416std::tuple<HWC2On1Adapter::Layer*, Error> HWC2On1Adapter::getLayer(
2417 hwc2_display_t displayId, hwc2_layer_t layerId)
2418{
2419 auto display = getDisplay(displayId);
2420 if (!display) {
2421 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadDisplay);
2422 }
2423
2424 auto layerEntry = mLayers.find(layerId);
2425 if (layerEntry == mLayers.end()) {
2426 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2427 }
2428
2429 auto layer = layerEntry->second;
2430 if (layer->getDisplay().getId() != displayId) {
2431 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2432 }
2433 return std::make_tuple(layer.get(), Error::None);
2434}
2435
2436void HWC2On1Adapter::populatePrimary()
2437{
2438 ALOGV("populatePrimary");
2439
Dan Stozafc4e2022016-02-23 11:43:19 -08002440 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002441
2442 auto display =
2443 std::make_shared<Display>(*this, HWC2::DisplayType::Physical);
2444 mHwc1DisplayMap[HWC_DISPLAY_PRIMARY] = display->getId();
2445 display->setHwc1Id(HWC_DISPLAY_PRIMARY);
2446 display->populateConfigs();
2447 mDisplays.emplace(display->getId(), std::move(display));
2448}
2449
2450bool HWC2On1Adapter::prepareAllDisplays()
2451{
2452 ATRACE_CALL();
2453
Dan Stozafc4e2022016-02-23 11:43:19 -08002454 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002455
2456 for (const auto& displayPair : mDisplays) {
2457 auto& display = displayPair.second;
2458 if (!display->prepare()) {
2459 return false;
2460 }
2461 }
2462
2463 if (mHwc1DisplayMap.count(0) == 0) {
2464 ALOGE("prepareAllDisplays: Unable to find primary HWC1 display");
2465 return false;
2466 }
2467
2468 // Always push the primary display
2469 std::vector<HWC2On1Adapter::Display::HWC1Contents> requestedContents;
2470 auto primaryDisplayId = mHwc1DisplayMap[HWC_DISPLAY_PRIMARY];
2471 auto& primaryDisplay = mDisplays[primaryDisplayId];
2472 auto primaryDisplayContents = primaryDisplay->cloneRequestedContents();
2473 requestedContents.push_back(std::move(primaryDisplayContents));
2474
2475 // Push the external display, if present
2476 if (mHwc1DisplayMap.count(HWC_DISPLAY_EXTERNAL) != 0) {
2477 auto externalDisplayId = mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL];
2478 auto& externalDisplay = mDisplays[externalDisplayId];
2479 auto externalDisplayContents =
2480 externalDisplay->cloneRequestedContents();
2481 requestedContents.push_back(std::move(externalDisplayContents));
2482 } else {
2483 // Even if an external display isn't present, we still need to send
2484 // at least two displays down to HWC1
2485 requestedContents.push_back(nullptr);
2486 }
2487
2488 // Push the hardware virtual display, if supported and present
2489 if (mHwc1MinorVersion >= 3) {
2490 if (mHwc1DisplayMap.count(HWC_DISPLAY_VIRTUAL) != 0) {
2491 auto virtualDisplayId = mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL];
2492 auto& virtualDisplay = mDisplays[virtualDisplayId];
2493 auto virtualDisplayContents =
2494 virtualDisplay->cloneRequestedContents();
2495 requestedContents.push_back(std::move(virtualDisplayContents));
2496 } else {
2497 requestedContents.push_back(nullptr);
2498 }
2499 }
2500
2501 mHwc1Contents.clear();
2502 for (auto& displayContents : requestedContents) {
2503 mHwc1Contents.push_back(displayContents.get());
2504 if (!displayContents) {
2505 continue;
2506 }
2507
2508 ALOGV("Display %zd layers:", mHwc1Contents.size() - 1);
2509 for (size_t l = 0; l < displayContents->numHwLayers; ++l) {
2510 auto& layer = displayContents->hwLayers[l];
2511 ALOGV(" %zd: %d", l, layer.compositionType);
2512 }
2513 }
2514
2515 ALOGV("Calling HWC1 prepare");
2516 {
2517 ATRACE_NAME("HWC1 prepare");
2518 mHwc1Device->prepare(mHwc1Device, mHwc1Contents.size(),
2519 mHwc1Contents.data());
2520 }
2521
2522 for (size_t c = 0; c < mHwc1Contents.size(); ++c) {
2523 auto& contents = mHwc1Contents[c];
2524 if (!contents) {
2525 continue;
2526 }
2527 ALOGV("Display %zd layers:", c);
2528 for (size_t l = 0; l < contents->numHwLayers; ++l) {
2529 ALOGV(" %zd: %d", l, contents->hwLayers[l].compositionType);
2530 }
2531 }
2532
2533 // Return the received contents to their respective displays
2534 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2535 if (mHwc1Contents[hwc1Id] == nullptr) {
2536 continue;
2537 }
2538
2539 auto displayId = mHwc1DisplayMap[hwc1Id];
2540 auto& display = mDisplays[displayId];
2541 display->setReceivedContents(std::move(requestedContents[hwc1Id]));
2542 }
2543
2544 return true;
2545}
2546
2547Error HWC2On1Adapter::setAllDisplays()
2548{
2549 ATRACE_CALL();
2550
Dan Stozafc4e2022016-02-23 11:43:19 -08002551 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002552
2553 // Make sure we're ready to validate
2554 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2555 if (mHwc1Contents[hwc1Id] == nullptr) {
2556 continue;
2557 }
2558
2559 auto displayId = mHwc1DisplayMap[hwc1Id];
2560 auto& display = mDisplays[displayId];
2561 Error error = display->set(*mHwc1Contents[hwc1Id]);
2562 if (error != Error::None) {
2563 ALOGE("setAllDisplays: Failed to set display %zd: %s", hwc1Id,
2564 to_string(error).c_str());
2565 return error;
2566 }
2567 }
2568
2569 ALOGV("Calling HWC1 set");
2570 {
2571 ATRACE_NAME("HWC1 set");
2572 mHwc1Device->set(mHwc1Device, mHwc1Contents.size(),
2573 mHwc1Contents.data());
2574 }
2575
2576 // Add retire and release fences
2577 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2578 if (mHwc1Contents[hwc1Id] == nullptr) {
2579 continue;
2580 }
2581
2582 auto displayId = mHwc1DisplayMap[hwc1Id];
2583 auto& display = mDisplays[displayId];
2584 auto retireFenceFd = mHwc1Contents[hwc1Id]->retireFenceFd;
2585 ALOGV("setAllDisplays: Adding retire fence %d to display %zd",
2586 retireFenceFd, hwc1Id);
2587 display->addRetireFence(mHwc1Contents[hwc1Id]->retireFenceFd);
2588 display->addReleaseFences(*mHwc1Contents[hwc1Id]);
2589 }
2590
2591 return Error::None;
2592}
2593
2594void HWC2On1Adapter::hwc1Invalidate()
2595{
2596 ALOGV("Received hwc1Invalidate");
2597
Dan Stozafc4e2022016-02-23 11:43:19 -08002598 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002599
2600 // If the HWC2-side callback hasn't been registered yet, buffer this until
2601 // it is registered
2602 if (mCallbacks.count(Callback::Refresh) == 0) {
2603 mHasPendingInvalidate = true;
2604 return;
2605 }
2606
2607 const auto& callbackInfo = mCallbacks[Callback::Refresh];
2608 std::vector<hwc2_display_t> displays;
2609 for (const auto& displayPair : mDisplays) {
2610 displays.emplace_back(displayPair.first);
2611 }
2612
2613 // Call back without the state lock held
2614 lock.unlock();
2615
2616 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(callbackInfo.pointer);
2617 for (auto display : displays) {
2618 refresh(callbackInfo.data, display);
2619 }
2620}
2621
2622void HWC2On1Adapter::hwc1Vsync(int hwc1DisplayId, int64_t timestamp)
2623{
2624 ALOGV("Received hwc1Vsync(%d, %" PRId64 ")", hwc1DisplayId, timestamp);
2625
Dan Stozafc4e2022016-02-23 11:43:19 -08002626 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002627
2628 // If the HWC2-side callback hasn't been registered yet, buffer this until
2629 // it is registered
2630 if (mCallbacks.count(Callback::Vsync) == 0) {
2631 mPendingVsyncs.emplace_back(hwc1DisplayId, timestamp);
2632 return;
2633 }
2634
2635 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2636 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", hwc1DisplayId);
2637 return;
2638 }
2639
2640 const auto& callbackInfo = mCallbacks[Callback::Vsync];
2641 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
2642
2643 // Call back without the state lock held
2644 lock.unlock();
2645
2646 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(callbackInfo.pointer);
2647 vsync(callbackInfo.data, displayId, timestamp);
2648}
2649
2650void HWC2On1Adapter::hwc1Hotplug(int hwc1DisplayId, int connected)
2651{
2652 ALOGV("Received hwc1Hotplug(%d, %d)", hwc1DisplayId, connected);
2653
2654 if (hwc1DisplayId != HWC_DISPLAY_EXTERNAL) {
2655 ALOGE("hwc1Hotplug: Received hotplug for non-external display");
2656 return;
2657 }
2658
Dan Stozafc4e2022016-02-23 11:43:19 -08002659 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002660
2661 // If the HWC2-side callback hasn't been registered yet, buffer this until
2662 // it is registered
2663 if (mCallbacks.count(Callback::Hotplug) == 0) {
2664 mPendingHotplugs.emplace_back(hwc1DisplayId, connected);
2665 return;
2666 }
2667
2668 hwc2_display_t displayId = UINT64_MAX;
2669 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2670 if (connected == 0) {
2671 ALOGW("hwc1Hotplug: Received disconnect for unconnected display");
2672 return;
2673 }
2674
2675 // Create a new display on connect
2676 auto display = std::make_shared<HWC2On1Adapter::Display>(*this,
2677 HWC2::DisplayType::Physical);
2678 display->setHwc1Id(HWC_DISPLAY_EXTERNAL);
2679 display->populateConfigs();
2680 displayId = display->getId();
2681 mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL] = displayId;
2682 mDisplays.emplace(displayId, std::move(display));
2683 } else {
2684 if (connected != 0) {
2685 ALOGW("hwc1Hotplug: Received connect for previously connected "
2686 "display");
2687 return;
2688 }
2689
2690 // Disconnect an existing display
2691 displayId = mHwc1DisplayMap[hwc1DisplayId];
2692 mHwc1DisplayMap.erase(HWC_DISPLAY_EXTERNAL);
2693 mDisplays.erase(displayId);
2694 }
2695
2696 const auto& callbackInfo = mCallbacks[Callback::Hotplug];
2697
2698 // Call back without the state lock held
2699 lock.unlock();
2700
2701 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(callbackInfo.pointer);
2702 auto hwc2Connected = (connected == 0) ?
2703 HWC2::Connection::Disconnected : HWC2::Connection::Connected;
2704 hotplug(callbackInfo.data, displayId, static_cast<int32_t>(hwc2Connected));
2705}
2706
2707} // namespace android