blob: 5dcd6e332caf16499dada02e474aa76f8dc7fb5f [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);
Fabien Sanglard03be6812017-01-31 12:14:57 -0800610 mZIsDirty = true;
Dan Stozac6998d22015-09-24 17:03:36 -0700611 return Error::None;
612}
613
614Error HWC2On1Adapter::Display::destroyLayer(hwc2_layer_t layerId)
615{
616 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
617
618 const auto mapLayer = mDevice.mLayers.find(layerId);
619 if (mapLayer == mDevice.mLayers.end()) {
620 ALOGV("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer",
621 mId, layerId);
622 return Error::BadLayer;
623 }
624 const auto layer = mapLayer->second;
625 mDevice.mLayers.erase(mapLayer);
626 const auto zRange = mLayers.equal_range(layer);
627 for (auto current = zRange.first; current != zRange.second; ++current) {
628 if (**current == *layer) {
629 current = mLayers.erase(current);
630 break;
631 }
632 }
633 ALOGV("[%" PRIu64 "] destroyed layer %" PRIu64, mId, layerId);
Fabien Sanglard03be6812017-01-31 12:14:57 -0800634 mZIsDirty = true;
Dan Stozac6998d22015-09-24 17:03:36 -0700635 return Error::None;
636}
637
638Error HWC2On1Adapter::Display::getActiveConfig(hwc2_config_t* outConfig)
639{
640 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
641
642 if (!mActiveConfig) {
643 ALOGV("[%" PRIu64 "] getActiveConfig --> %s", mId,
644 to_string(Error::BadConfig).c_str());
645 return Error::BadConfig;
646 }
647 auto configId = mActiveConfig->getId();
648 ALOGV("[%" PRIu64 "] getActiveConfig --> %u", mId, configId);
649 *outConfig = configId;
650 return Error::None;
651}
652
653Error HWC2On1Adapter::Display::getAttribute(hwc2_config_t configId,
654 Attribute attribute, int32_t* outValue)
655{
656 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
657
658 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
659 ALOGV("[%" PRIu64 "] getAttribute failed: bad config (%u)", mId,
660 configId);
661 return Error::BadConfig;
662 }
663 *outValue = mConfigs[configId]->getAttribute(attribute);
664 ALOGV("[%" PRIu64 "] getAttribute(%u, %s) --> %d", mId, configId,
665 to_string(attribute).c_str(), *outValue);
666 return Error::None;
667}
668
669Error HWC2On1Adapter::Display::getChangedCompositionTypes(
670 uint32_t* outNumElements, hwc2_layer_t* outLayers, int32_t* outTypes)
671{
672 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
673
674 if (!mChanges) {
675 ALOGE("[%" PRIu64 "] getChangedCompositionTypes failed: not validated",
676 mId);
677 return Error::NotValidated;
678 }
679
680 if ((outLayers == nullptr) || (outTypes == nullptr)) {
681 *outNumElements = mChanges->getTypeChanges().size();
682 return Error::None;
683 }
684
685 uint32_t numWritten = 0;
686 for (const auto& element : mChanges->getTypeChanges()) {
687 if (numWritten == *outNumElements) {
688 break;
689 }
690 auto layerId = element.first;
691 auto intType = static_cast<int32_t>(element.second);
692 ALOGV("Adding %" PRIu64 " %s", layerId,
693 to_string(element.second).c_str());
694 outLayers[numWritten] = layerId;
695 outTypes[numWritten] = intType;
696 ++numWritten;
697 }
698 *outNumElements = numWritten;
699
700 return Error::None;
701}
702
Dan Stoza076ac672016-03-14 10:47:53 -0700703Error HWC2On1Adapter::Display::getColorModes(uint32_t* outNumModes,
704 int32_t* outModes)
705{
706 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
707
708 if (!outModes) {
709 *outNumModes = mColorModes.size();
710 return Error::None;
711 }
712 uint32_t numModes = std::min(*outNumModes,
713 static_cast<uint32_t>(mColorModes.size()));
714 std::copy_n(mColorModes.cbegin(), numModes, outModes);
715 *outNumModes = numModes;
716 return Error::None;
717}
718
Dan Stozac6998d22015-09-24 17:03:36 -0700719Error HWC2On1Adapter::Display::getConfigs(uint32_t* outNumConfigs,
720 hwc2_config_t* outConfigs)
721{
722 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
723
724 if (!outConfigs) {
725 *outNumConfigs = mConfigs.size();
726 return Error::None;
727 }
728 uint32_t numWritten = 0;
729 for (const auto& config : mConfigs) {
730 if (numWritten == *outNumConfigs) {
731 break;
732 }
733 outConfigs[numWritten] = config->getId();
734 ++numWritten;
735 }
736 *outNumConfigs = numWritten;
737 return Error::None;
738}
739
740Error HWC2On1Adapter::Display::getDozeSupport(int32_t* outSupport)
741{
742 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
743
744 if (mDevice.mHwc1MinorVersion < 4 || mHwc1Id != 0) {
745 *outSupport = 0;
746 } else {
747 *outSupport = 1;
748 }
749 return Error::None;
750}
751
Dan Stozaed40eba2016-03-16 12:33:52 -0700752Error HWC2On1Adapter::Display::getHdrCapabilities(uint32_t* outNumTypes,
753 int32_t* /*outTypes*/, float* /*outMaxLuminance*/,
754 float* /*outMaxAverageLuminance*/, float* /*outMinLuminance*/)
755{
756 // This isn't supported on HWC1, so per the HWC2 header, return numTypes = 0
757 *outNumTypes = 0;
758 return Error::None;
759}
760
Dan Stozac6998d22015-09-24 17:03:36 -0700761Error HWC2On1Adapter::Display::getName(uint32_t* outSize, char* outName)
762{
763 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
764
765 if (!outName) {
766 *outSize = mName.size();
767 return Error::None;
768 }
769 auto numCopied = mName.copy(outName, *outSize);
770 *outSize = numCopied;
771 return Error::None;
772}
773
774Error HWC2On1Adapter::Display::getReleaseFences(uint32_t* outNumElements,
775 hwc2_layer_t* outLayers, int32_t* outFences)
776{
777 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
778
779 uint32_t numWritten = 0;
780 bool outputsNonNull = (outLayers != nullptr) && (outFences != nullptr);
781 for (const auto& layer : mLayers) {
782 if (outputsNonNull && (numWritten == *outNumElements)) {
783 break;
784 }
785
786 auto releaseFence = layer->getReleaseFence();
787 if (releaseFence != Fence::NO_FENCE) {
788 if (outputsNonNull) {
789 outLayers[numWritten] = layer->getId();
790 outFences[numWritten] = releaseFence->dup();
791 }
792 ++numWritten;
793 }
794 }
795 *outNumElements = numWritten;
796
797 return Error::None;
798}
799
800Error HWC2On1Adapter::Display::getRequests(int32_t* outDisplayRequests,
801 uint32_t* outNumElements, hwc2_layer_t* outLayers,
802 int32_t* outLayerRequests)
803{
804 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
805
806 if (!mChanges) {
807 return Error::NotValidated;
808 }
809
810 if (outLayers == nullptr || outLayerRequests == nullptr) {
811 *outNumElements = mChanges->getNumLayerRequests();
812 return Error::None;
813 }
814
Fabien Sanglard601938c2016-11-29 11:10:40 -0800815 // Display requests (HWC2::DisplayRequest) are not supported by hwc1:
816 // A hwc1 has always zero requests for the client.
817 *outDisplayRequests = 0;
818
Dan Stozac6998d22015-09-24 17:03:36 -0700819 uint32_t numWritten = 0;
820 for (const auto& request : mChanges->getLayerRequests()) {
821 if (numWritten == *outNumElements) {
822 break;
823 }
824 outLayers[numWritten] = request.first;
825 outLayerRequests[numWritten] = static_cast<int32_t>(request.second);
826 ++numWritten;
827 }
828
829 return Error::None;
830}
831
832Error HWC2On1Adapter::Display::getType(int32_t* outType)
833{
834 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
835
836 *outType = static_cast<int32_t>(mType);
837 return Error::None;
838}
839
840Error HWC2On1Adapter::Display::present(int32_t* outRetireFence)
841{
842 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
843
844 if (mChanges) {
845 Error error = mDevice.setAllDisplays();
846 if (error != Error::None) {
847 ALOGE("[%" PRIu64 "] present: setAllDisplaysFailed (%s)", mId,
848 to_string(error).c_str());
849 return error;
850 }
851 }
852
853 *outRetireFence = mRetireFence.get()->dup();
854 ALOGV("[%" PRIu64 "] present returning retire fence %d", mId,
855 *outRetireFence);
856
857 return Error::None;
858}
859
860Error HWC2On1Adapter::Display::setActiveConfig(hwc2_config_t configId)
861{
862 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
863
864 auto config = getConfig(configId);
865 if (!config) {
866 return Error::BadConfig;
867 }
Dan Stoza076ac672016-03-14 10:47:53 -0700868 if (config == mActiveConfig) {
869 return Error::None;
Dan Stozac6998d22015-09-24 17:03:36 -0700870 }
Dan Stoza076ac672016-03-14 10:47:53 -0700871
872 if (mDevice.mHwc1MinorVersion >= 4) {
873 uint32_t hwc1Id = 0;
874 auto error = config->getHwc1IdForColorMode(mActiveColorMode, &hwc1Id);
875 if (error != Error::None) {
876 return error;
877 }
878
879 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
880 mHwc1Id, static_cast<int>(hwc1Id));
881 if (intError != 0) {
882 ALOGE("setActiveConfig: Failed to set active config on HWC1 (%d)",
883 intError);
884 return Error::BadConfig;
885 }
886 mActiveConfig = config;
887 }
888
Dan Stozac6998d22015-09-24 17:03:36 -0700889 return Error::None;
890}
891
892Error HWC2On1Adapter::Display::setClientTarget(buffer_handle_t target,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700893 int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/)
Dan Stozac6998d22015-09-24 17:03:36 -0700894{
895 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
896
897 ALOGV("[%" PRIu64 "] setClientTarget(%p, %d)", mId, target, acquireFence);
898 mClientTarget.setBuffer(target);
899 mClientTarget.setFence(acquireFence);
Dan Stoza5cf424b2016-05-20 14:02:39 -0700900 // dataspace and damage can't be used by HWC1, so ignore them
Dan Stozac6998d22015-09-24 17:03:36 -0700901 return Error::None;
902}
903
Michael Wright28f24d02016-07-12 13:30:53 -0700904Error HWC2On1Adapter::Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700905{
906 std::unique_lock<std::recursive_mutex> lock (mStateMutex);
907
908 ALOGV("[%" PRIu64 "] setColorMode(%d)", mId, mode);
909
910 if (mode == mActiveColorMode) {
911 return Error::None;
912 }
913 if (mColorModes.count(mode) == 0) {
914 ALOGE("[%" PRIu64 "] Mode %d not found in mColorModes", mId, mode);
915 return Error::Unsupported;
916 }
917
918 uint32_t hwc1Config = 0;
919 auto error = mActiveConfig->getHwc1IdForColorMode(mode, &hwc1Config);
920 if (error != Error::None) {
921 return error;
922 }
923
924 ALOGV("[%" PRIu64 "] Setting HWC1 config %u", mId, hwc1Config);
925 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
926 mHwc1Id, hwc1Config);
927 if (intError != 0) {
928 ALOGE("[%" PRIu64 "] Failed to set HWC1 config (%d)", mId, intError);
929 return Error::Unsupported;
930 }
931
932 mActiveColorMode = mode;
933 return Error::None;
934}
935
Dan Stoza5df2a862016-03-24 16:19:37 -0700936Error HWC2On1Adapter::Display::setColorTransform(android_color_transform_t hint)
937{
938 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
939
940 ALOGV("%" PRIu64 "] setColorTransform(%d)", mId,
941 static_cast<int32_t>(hint));
942 mHasColorTransform = (hint != HAL_COLOR_TRANSFORM_IDENTITY);
943 return Error::None;
944}
945
Dan Stozac6998d22015-09-24 17:03:36 -0700946Error HWC2On1Adapter::Display::setOutputBuffer(buffer_handle_t buffer,
947 int32_t releaseFence)
948{
949 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
950
951 ALOGV("[%" PRIu64 "] setOutputBuffer(%p, %d)", mId, buffer, releaseFence);
952 mOutputBuffer.setBuffer(buffer);
953 mOutputBuffer.setFence(releaseFence);
954 return Error::None;
955}
956
957static bool isValid(PowerMode mode)
958{
959 switch (mode) {
960 case PowerMode::Off: // Fall-through
961 case PowerMode::DozeSuspend: // Fall-through
962 case PowerMode::Doze: // Fall-through
963 case PowerMode::On: return true;
964 default: return false;
965 }
966}
967
968static int getHwc1PowerMode(PowerMode mode)
969{
970 switch (mode) {
971 case PowerMode::Off: return HWC_POWER_MODE_OFF;
972 case PowerMode::DozeSuspend: return HWC_POWER_MODE_DOZE_SUSPEND;
973 case PowerMode::Doze: return HWC_POWER_MODE_DOZE;
974 case PowerMode::On: return HWC_POWER_MODE_NORMAL;
975 default: return HWC_POWER_MODE_OFF;
976 }
977}
978
979Error HWC2On1Adapter::Display::setPowerMode(PowerMode mode)
980{
981 if (!isValid(mode)) {
982 return Error::BadParameter;
983 }
984 if (mode == mPowerMode) {
985 return Error::None;
986 }
987
988 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
989
990 int error = 0;
991 if (mDevice.mHwc1MinorVersion < 4) {
992 error = mDevice.mHwc1Device->blank(mDevice.mHwc1Device, mHwc1Id,
993 mode == PowerMode::Off);
994 } else {
995 error = mDevice.mHwc1Device->setPowerMode(mDevice.mHwc1Device,
996 mHwc1Id, getHwc1PowerMode(mode));
997 }
998 ALOGE_IF(error != 0, "setPowerMode: Failed to set power mode on HWC1 (%d)",
999 error);
1000
1001 ALOGV("[%" PRIu64 "] setPowerMode(%s)", mId, to_string(mode).c_str());
1002 mPowerMode = mode;
1003 return Error::None;
1004}
1005
1006static bool isValid(Vsync enable) {
1007 switch (enable) {
1008 case Vsync::Enable: // Fall-through
1009 case Vsync::Disable: return true;
1010 default: return false;
1011 }
1012}
1013
1014Error HWC2On1Adapter::Display::setVsyncEnabled(Vsync enable)
1015{
1016 if (!isValid(enable)) {
1017 return Error::BadParameter;
1018 }
1019 if (enable == mVsyncEnabled) {
1020 return Error::None;
1021 }
1022
1023 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1024
1025 int error = mDevice.mHwc1Device->eventControl(mDevice.mHwc1Device,
1026 mHwc1Id, HWC_EVENT_VSYNC, enable == Vsync::Enable);
1027 ALOGE_IF(error != 0, "setVsyncEnabled: Failed to set vsync on HWC1 (%d)",
1028 error);
1029
1030 mVsyncEnabled = enable;
1031 return Error::None;
1032}
1033
1034Error HWC2On1Adapter::Display::validate(uint32_t* outNumTypes,
1035 uint32_t* outNumRequests)
1036{
1037 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1038
1039 ALOGV("[%" PRIu64 "] Entering validate", mId);
1040
1041 if (!mChanges) {
1042 if (!mDevice.prepareAllDisplays()) {
1043 return Error::BadDisplay;
1044 }
1045 }
1046
1047 *outNumTypes = mChanges->getNumTypes();
1048 *outNumRequests = mChanges->getNumLayerRequests();
1049 ALOGV("[%" PRIu64 "] validate --> %u types, %u requests", mId, *outNumTypes,
1050 *outNumRequests);
1051 for (auto request : mChanges->getTypeChanges()) {
1052 ALOGV("Layer %" PRIu64 " --> %s", request.first,
1053 to_string(request.second).c_str());
1054 }
1055 return *outNumTypes > 0 ? Error::HasChanges : Error::None;
1056}
1057
1058// Display helpers
1059
1060Error HWC2On1Adapter::Display::updateLayerZ(hwc2_layer_t layerId, uint32_t z)
1061{
1062 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1063
1064 const auto mapLayer = mDevice.mLayers.find(layerId);
1065 if (mapLayer == mDevice.mLayers.end()) {
1066 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer", mId);
1067 return Error::BadLayer;
1068 }
1069
1070 const auto layer = mapLayer->second;
1071 const auto zRange = mLayers.equal_range(layer);
1072 bool layerOnDisplay = false;
1073 for (auto current = zRange.first; current != zRange.second; ++current) {
1074 if (**current == *layer) {
1075 if ((*current)->getZ() == z) {
1076 // Don't change anything if the Z hasn't changed
1077 return Error::None;
1078 }
1079 current = mLayers.erase(current);
1080 layerOnDisplay = true;
1081 break;
1082 }
1083 }
1084
1085 if (!layerOnDisplay) {
1086 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display",
1087 mId);
1088 return Error::BadLayer;
1089 }
1090
1091 layer->setZ(z);
1092 mLayers.emplace(std::move(layer));
1093 mZIsDirty = true;
1094
1095 return Error::None;
1096}
1097
Dan Stoza076ac672016-03-14 10:47:53 -07001098static constexpr uint32_t ATTRIBUTES_WITH_COLOR[] = {
1099 HWC_DISPLAY_VSYNC_PERIOD,
1100 HWC_DISPLAY_WIDTH,
1101 HWC_DISPLAY_HEIGHT,
1102 HWC_DISPLAY_DPI_X,
1103 HWC_DISPLAY_DPI_Y,
1104 HWC_DISPLAY_COLOR_TRANSFORM,
1105 HWC_DISPLAY_NO_ATTRIBUTE,
1106};
1107
1108static constexpr uint32_t ATTRIBUTES_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001109 HWC_DISPLAY_VSYNC_PERIOD,
1110 HWC_DISPLAY_WIDTH,
1111 HWC_DISPLAY_HEIGHT,
1112 HWC_DISPLAY_DPI_X,
1113 HWC_DISPLAY_DPI_Y,
1114 HWC_DISPLAY_NO_ATTRIBUTE,
1115};
Dan Stozac6998d22015-09-24 17:03:36 -07001116
Dan Stoza076ac672016-03-14 10:47:53 -07001117static constexpr size_t NUM_ATTRIBUTES_WITH_COLOR =
1118 sizeof(ATTRIBUTES_WITH_COLOR) / sizeof(uint32_t);
1119static_assert(sizeof(ATTRIBUTES_WITH_COLOR) > sizeof(ATTRIBUTES_WITHOUT_COLOR),
1120 "Attribute tables have unexpected sizes");
1121
1122static constexpr uint32_t ATTRIBUTE_MAP_WITH_COLOR[] = {
1123 6, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1124 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1125 1, // HWC_DISPLAY_WIDTH = 2,
1126 2, // HWC_DISPLAY_HEIGHT = 3,
1127 3, // HWC_DISPLAY_DPI_X = 4,
1128 4, // HWC_DISPLAY_DPI_Y = 5,
1129 5, // HWC_DISPLAY_COLOR_TRANSFORM = 6,
1130};
1131
1132static constexpr uint32_t ATTRIBUTE_MAP_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001133 5, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1134 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1135 1, // HWC_DISPLAY_WIDTH = 2,
1136 2, // HWC_DISPLAY_HEIGHT = 3,
1137 3, // HWC_DISPLAY_DPI_X = 4,
1138 4, // HWC_DISPLAY_DPI_Y = 5,
1139};
1140
1141template <uint32_t attribute>
1142static constexpr bool attributesMatch()
1143{
Dan Stoza076ac672016-03-14 10:47:53 -07001144 bool match = (attribute ==
1145 ATTRIBUTES_WITH_COLOR[ATTRIBUTE_MAP_WITH_COLOR[attribute]]);
1146 if (attribute == HWC_DISPLAY_COLOR_TRANSFORM) {
1147 return match;
1148 }
1149
1150 return match && (attribute ==
1151 ATTRIBUTES_WITHOUT_COLOR[ATTRIBUTE_MAP_WITHOUT_COLOR[attribute]]);
Dan Stozac6998d22015-09-24 17:03:36 -07001152}
1153static_assert(attributesMatch<HWC_DISPLAY_VSYNC_PERIOD>(),
1154 "Tables out of sync");
1155static_assert(attributesMatch<HWC_DISPLAY_WIDTH>(), "Tables out of sync");
1156static_assert(attributesMatch<HWC_DISPLAY_HEIGHT>(), "Tables out of sync");
1157static_assert(attributesMatch<HWC_DISPLAY_DPI_X>(), "Tables out of sync");
1158static_assert(attributesMatch<HWC_DISPLAY_DPI_Y>(), "Tables out of sync");
Dan Stoza076ac672016-03-14 10:47:53 -07001159static_assert(attributesMatch<HWC_DISPLAY_COLOR_TRANSFORM>(),
1160 "Tables out of sync");
Dan Stozac6998d22015-09-24 17:03:36 -07001161
1162void HWC2On1Adapter::Display::populateConfigs()
1163{
1164 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1165
1166 ALOGV("[%" PRIu64 "] populateConfigs", mId);
1167
1168 if (mHwc1Id == -1) {
1169 ALOGE("populateConfigs: HWC1 ID not set");
1170 return;
1171 }
1172
1173 const size_t MAX_NUM_CONFIGS = 128;
1174 uint32_t configs[MAX_NUM_CONFIGS] = {};
1175 size_t numConfigs = MAX_NUM_CONFIGS;
1176 mDevice.mHwc1Device->getDisplayConfigs(mDevice.mHwc1Device, mHwc1Id,
1177 configs, &numConfigs);
1178
1179 for (size_t c = 0; c < numConfigs; ++c) {
1180 uint32_t hwc1ConfigId = configs[c];
Dan Stoza076ac672016-03-14 10:47:53 -07001181 auto newConfig = std::make_shared<Config>(*this);
Dan Stozac6998d22015-09-24 17:03:36 -07001182
Dan Stoza076ac672016-03-14 10:47:53 -07001183 int32_t values[NUM_ATTRIBUTES_WITH_COLOR] = {};
1184 bool hasColor = true;
1185 auto result = mDevice.mHwc1Device->getDisplayAttributes(
1186 mDevice.mHwc1Device, mHwc1Id, hwc1ConfigId,
1187 ATTRIBUTES_WITH_COLOR, values);
1188 if (result != 0) {
1189 mDevice.mHwc1Device->getDisplayAttributes(mDevice.mHwc1Device,
1190 mHwc1Id, hwc1ConfigId, ATTRIBUTES_WITHOUT_COLOR, values);
1191 hasColor = false;
Dan Stozac6998d22015-09-24 17:03:36 -07001192 }
Dan Stoza076ac672016-03-14 10:47:53 -07001193
1194 auto attributeMap = hasColor ?
1195 ATTRIBUTE_MAP_WITH_COLOR : ATTRIBUTE_MAP_WITHOUT_COLOR;
1196
1197 newConfig->setAttribute(Attribute::VsyncPeriod,
1198 values[attributeMap[HWC_DISPLAY_VSYNC_PERIOD]]);
1199 newConfig->setAttribute(Attribute::Width,
1200 values[attributeMap[HWC_DISPLAY_WIDTH]]);
1201 newConfig->setAttribute(Attribute::Height,
1202 values[attributeMap[HWC_DISPLAY_HEIGHT]]);
1203 newConfig->setAttribute(Attribute::DpiX,
1204 values[attributeMap[HWC_DISPLAY_DPI_X]]);
1205 newConfig->setAttribute(Attribute::DpiY,
1206 values[attributeMap[HWC_DISPLAY_DPI_Y]]);
1207 if (hasColor) {
Michael Wright28f24d02016-07-12 13:30:53 -07001208 // In HWC1, color modes are referred to as color transforms. To avoid confusion with
1209 // the HWC2 concept of color transforms, we internally refer to them as color modes for
1210 // both HWC1 and 2.
1211 newConfig->setAttribute(ColorMode,
Dan Stoza076ac672016-03-14 10:47:53 -07001212 values[attributeMap[HWC_DISPLAY_COLOR_TRANSFORM]]);
1213 }
1214
Michael Wright28f24d02016-07-12 13:30:53 -07001215 // We can only do this after attempting to read the color mode
Dan Stoza076ac672016-03-14 10:47:53 -07001216 newConfig->setHwc1Id(hwc1ConfigId);
1217
1218 for (auto& existingConfig : mConfigs) {
1219 if (existingConfig->merge(*newConfig)) {
1220 ALOGV("Merged config %d with existing config %u: %s",
1221 hwc1ConfigId, existingConfig->getId(),
1222 existingConfig->toString().c_str());
1223 newConfig.reset();
1224 break;
1225 }
1226 }
1227
1228 // If it wasn't merged with any existing config, add it to the end
1229 if (newConfig) {
1230 newConfig->setId(static_cast<hwc2_config_t>(mConfigs.size()));
1231 ALOGV("Found new config %u: %s", newConfig->getId(),
1232 newConfig->toString().c_str());
1233 mConfigs.emplace_back(std::move(newConfig));
1234 }
Dan Stozac6998d22015-09-24 17:03:36 -07001235 }
Dan Stoza076ac672016-03-14 10:47:53 -07001236
1237 initializeActiveConfig();
1238 populateColorModes();
Dan Stozac6998d22015-09-24 17:03:36 -07001239}
1240
1241void HWC2On1Adapter::Display::populateConfigs(uint32_t width, uint32_t height)
1242{
1243 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1244
Dan Stoza076ac672016-03-14 10:47:53 -07001245 mConfigs.emplace_back(std::make_shared<Config>(*this));
Dan Stozac6998d22015-09-24 17:03:36 -07001246 auto& config = mConfigs[0];
1247
1248 config->setAttribute(Attribute::Width, static_cast<int32_t>(width));
1249 config->setAttribute(Attribute::Height, static_cast<int32_t>(height));
Dan Stoza076ac672016-03-14 10:47:53 -07001250 config->setHwc1Id(0);
1251 config->setId(0);
Dan Stozac6998d22015-09-24 17:03:36 -07001252 mActiveConfig = config;
1253}
1254
1255bool HWC2On1Adapter::Display::prepare()
1256{
1257 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1258
1259 // Only prepare display contents for displays HWC1 knows about
1260 if (mHwc1Id == -1) {
1261 return true;
1262 }
1263
1264 // It doesn't make sense to prepare a display for which there is no active
1265 // config, so return early
1266 if (!mActiveConfig) {
1267 ALOGE("[%" PRIu64 "] Attempted to prepare, but no config active", mId);
1268 return false;
1269 }
1270
1271 ALOGV("[%" PRIu64 "] Entering prepare", mId);
1272
1273 auto currentCount = mHwc1RequestedContents ?
1274 mHwc1RequestedContents->numHwLayers : 0;
1275 auto requiredCount = mLayers.size() + 1;
1276 ALOGV("[%" PRIu64 "] Requires %zd layers, %zd allocated in %p", mId,
1277 requiredCount, currentCount, mHwc1RequestedContents.get());
1278
1279 bool layerCountChanged = (currentCount != requiredCount);
1280 if (layerCountChanged) {
1281 reallocateHwc1Contents();
1282 }
1283
1284 bool applyAllState = false;
1285 if (layerCountChanged || mZIsDirty) {
1286 assignHwc1LayerIds();
1287 mZIsDirty = false;
1288 applyAllState = true;
1289 }
1290
1291 mHwc1RequestedContents->retireFenceFd = -1;
1292 mHwc1RequestedContents->flags = 0;
1293 if (isDirty() || applyAllState) {
1294 mHwc1RequestedContents->flags |= HWC_GEOMETRY_CHANGED;
1295 }
1296
1297 for (auto& layer : mLayers) {
1298 auto& hwc1Layer = mHwc1RequestedContents->hwLayers[layer->getHwc1Id()];
1299 hwc1Layer.releaseFenceFd = -1;
Fabien Sanglard16ab8192017-01-31 12:12:10 -08001300 hwc1Layer.acquireFenceFd = -1;
Fabien Sanglard999a7fd2017-02-02 16:59:44 -08001301 ALOGV("Applying states for layer %" PRIu64 " ", layer->getId());
Dan Stozac6998d22015-09-24 17:03:36 -07001302 layer->applyState(hwc1Layer, applyAllState);
1303 }
1304
1305 mHwc1RequestedContents->outbuf = mOutputBuffer.getBuffer();
1306 mHwc1RequestedContents->outbufAcquireFenceFd = mOutputBuffer.getFence();
1307
1308 prepareFramebufferTarget();
1309
1310 return true;
1311}
1312
1313static void cloneHWCRegion(hwc_region_t& region)
1314{
1315 auto size = sizeof(hwc_rect_t) * region.numRects;
1316 auto newRects = static_cast<hwc_rect_t*>(std::malloc(size));
1317 std::copy_n(region.rects, region.numRects, newRects);
1318 region.rects = newRects;
1319}
1320
1321HWC2On1Adapter::Display::HWC1Contents
1322 HWC2On1Adapter::Display::cloneRequestedContents() const
1323{
1324 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1325
1326 size_t size = sizeof(hwc_display_contents_1_t) +
1327 sizeof(hwc_layer_1_t) * (mHwc1RequestedContents->numHwLayers);
1328 auto contents = static_cast<hwc_display_contents_1_t*>(std::malloc(size));
1329 std::memcpy(contents, mHwc1RequestedContents.get(), size);
1330 for (size_t layerId = 0; layerId < contents->numHwLayers; ++layerId) {
1331 auto& layer = contents->hwLayers[layerId];
1332 // Deep copy the regions to avoid double-frees
1333 cloneHWCRegion(layer.visibleRegionScreen);
1334 cloneHWCRegion(layer.surfaceDamage);
1335 }
1336 return HWC1Contents(contents);
1337}
1338
1339void HWC2On1Adapter::Display::setReceivedContents(HWC1Contents contents)
1340{
1341 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1342
1343 mHwc1ReceivedContents = std::move(contents);
1344
1345 mChanges.reset(new Changes);
1346
1347 size_t numLayers = mHwc1ReceivedContents->numHwLayers;
1348 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1349 const auto& receivedLayer = mHwc1ReceivedContents->hwLayers[hwc1Id];
1350 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1351 ALOGE_IF(receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET,
1352 "setReceivedContents: HWC1 layer %zd doesn't have a"
1353 " matching HWC2 layer, and isn't the framebuffer target",
1354 hwc1Id);
1355 continue;
1356 }
1357
1358 Layer& layer = *mHwc1LayerMap[hwc1Id];
1359 updateTypeChanges(receivedLayer, layer);
1360 updateLayerRequests(receivedLayer, layer);
1361 }
1362}
1363
1364bool HWC2On1Adapter::Display::hasChanges() const
1365{
1366 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1367 return mChanges != nullptr;
1368}
1369
1370Error HWC2On1Adapter::Display::set(hwc_display_contents_1& hwcContents)
1371{
1372 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1373
1374 if (!mChanges || (mChanges->getNumTypes() > 0)) {
1375 ALOGE("[%" PRIu64 "] set failed: not validated", mId);
1376 return Error::NotValidated;
1377 }
1378
1379 // Set up the client/framebuffer target
1380 auto numLayers = hwcContents.numHwLayers;
1381
1382 // Close acquire fences on FRAMEBUFFER layers, since they will not be used
1383 // by HWC
1384 for (size_t l = 0; l < numLayers - 1; ++l) {
1385 auto& layer = hwcContents.hwLayers[l];
1386 if (layer.compositionType == HWC_FRAMEBUFFER) {
1387 ALOGV("Closing fence %d for layer %zd", layer.acquireFenceFd, l);
1388 close(layer.acquireFenceFd);
1389 layer.acquireFenceFd = -1;
1390 }
1391 }
1392
1393 auto& clientTargetLayer = hwcContents.hwLayers[numLayers - 1];
1394 if (clientTargetLayer.compositionType == HWC_FRAMEBUFFER_TARGET) {
1395 clientTargetLayer.handle = mClientTarget.getBuffer();
1396 clientTargetLayer.acquireFenceFd = mClientTarget.getFence();
1397 } else {
1398 ALOGE("[%" PRIu64 "] set: last HWC layer wasn't FRAMEBUFFER_TARGET",
1399 mId);
1400 }
1401
1402 mChanges.reset();
1403
1404 return Error::None;
1405}
1406
1407void HWC2On1Adapter::Display::addRetireFence(int fenceFd)
1408{
1409 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1410 mRetireFence.add(fenceFd);
1411}
1412
1413void HWC2On1Adapter::Display::addReleaseFences(
1414 const hwc_display_contents_1_t& hwcContents)
1415{
1416 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1417
1418 size_t numLayers = hwcContents.numHwLayers;
1419 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1420 const auto& receivedLayer = hwcContents.hwLayers[hwc1Id];
1421 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1422 if (receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET) {
1423 ALOGE("addReleaseFences: HWC1 layer %zd doesn't have a"
1424 " matching HWC2 layer, and isn't the framebuffer"
1425 " target", hwc1Id);
1426 }
1427 // Close the framebuffer target release fence since we will use the
1428 // display retire fence instead
1429 if (receivedLayer.releaseFenceFd != -1) {
1430 close(receivedLayer.releaseFenceFd);
1431 }
1432 continue;
1433 }
1434
1435 Layer& layer = *mHwc1LayerMap[hwc1Id];
1436 ALOGV("Adding release fence %d to layer %" PRIu64,
1437 receivedLayer.releaseFenceFd, layer.getId());
1438 layer.addReleaseFence(receivedLayer.releaseFenceFd);
1439 }
1440}
1441
Dan Stoza5df2a862016-03-24 16:19:37 -07001442bool HWC2On1Adapter::Display::hasColorTransform() const
1443{
1444 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1445 return mHasColorTransform;
1446}
1447
Dan Stozac6998d22015-09-24 17:03:36 -07001448static std::string hwc1CompositionString(int32_t type)
1449{
1450 switch (type) {
1451 case HWC_FRAMEBUFFER: return "Framebuffer";
1452 case HWC_OVERLAY: return "Overlay";
1453 case HWC_BACKGROUND: return "Background";
1454 case HWC_FRAMEBUFFER_TARGET: return "FramebufferTarget";
1455 case HWC_SIDEBAND: return "Sideband";
1456 case HWC_CURSOR_OVERLAY: return "CursorOverlay";
1457 default:
1458 return std::string("Unknown (") + std::to_string(type) + ")";
1459 }
1460}
1461
1462static std::string hwc1TransformString(int32_t transform)
1463{
1464 switch (transform) {
1465 case 0: return "None";
1466 case HWC_TRANSFORM_FLIP_H: return "FlipH";
1467 case HWC_TRANSFORM_FLIP_V: return "FlipV";
1468 case HWC_TRANSFORM_ROT_90: return "Rotate90";
1469 case HWC_TRANSFORM_ROT_180: return "Rotate180";
1470 case HWC_TRANSFORM_ROT_270: return "Rotate270";
1471 case HWC_TRANSFORM_FLIP_H_ROT_90: return "FlipHRotate90";
1472 case HWC_TRANSFORM_FLIP_V_ROT_90: return "FlipVRotate90";
1473 default:
1474 return std::string("Unknown (") + std::to_string(transform) + ")";
1475 }
1476}
1477
1478static std::string hwc1BlendModeString(int32_t mode)
1479{
1480 switch (mode) {
1481 case HWC_BLENDING_NONE: return "None";
1482 case HWC_BLENDING_PREMULT: return "Premultiplied";
1483 case HWC_BLENDING_COVERAGE: return "Coverage";
1484 default:
1485 return std::string("Unknown (") + std::to_string(mode) + ")";
1486 }
1487}
1488
1489static std::string rectString(hwc_rect_t rect)
1490{
1491 std::stringstream output;
1492 output << "[" << rect.left << ", " << rect.top << ", ";
1493 output << rect.right << ", " << rect.bottom << "]";
1494 return output.str();
1495}
1496
1497static std::string approximateFloatString(float f)
1498{
1499 if (static_cast<int32_t>(f) == f) {
1500 return std::to_string(static_cast<int32_t>(f));
1501 }
1502 int32_t truncated = static_cast<int32_t>(f * 10);
1503 bool approximate = (static_cast<float>(truncated) != f * 10);
1504 const size_t BUFFER_SIZE = 32;
1505 char buffer[BUFFER_SIZE] = {};
1506 auto bytesWritten = snprintf(buffer, BUFFER_SIZE,
1507 "%s%.1f", approximate ? "~" : "", f);
1508 return std::string(buffer, bytesWritten);
1509}
1510
1511static std::string frectString(hwc_frect_t frect)
1512{
1513 std::stringstream output;
1514 output << "[" << approximateFloatString(frect.left) << ", ";
1515 output << approximateFloatString(frect.top) << ", ";
1516 output << approximateFloatString(frect.right) << ", ";
1517 output << approximateFloatString(frect.bottom) << "]";
1518 return output.str();
1519}
1520
1521static std::string colorString(hwc_color_t color)
1522{
1523 std::stringstream output;
1524 output << "RGBA [";
1525 output << static_cast<int32_t>(color.r) << ", ";
1526 output << static_cast<int32_t>(color.g) << ", ";
1527 output << static_cast<int32_t>(color.b) << ", ";
1528 output << static_cast<int32_t>(color.a) << "]";
1529 return output.str();
1530}
1531
1532static std::string alphaString(float f)
1533{
1534 const size_t BUFFER_SIZE = 8;
1535 char buffer[BUFFER_SIZE] = {};
1536 auto bytesWritten = snprintf(buffer, BUFFER_SIZE, "%.3f", f);
1537 return std::string(buffer, bytesWritten);
1538}
1539
1540static std::string to_string(const hwc_layer_1_t& hwcLayer,
1541 int32_t hwc1MinorVersion)
1542{
1543 const char* fill = " ";
1544
1545 std::stringstream output;
1546
1547 output << " Composition: " <<
1548 hwc1CompositionString(hwcLayer.compositionType);
1549
1550 if (hwcLayer.compositionType == HWC_BACKGROUND) {
1551 output << " Color: " << colorString(hwcLayer.backgroundColor) << '\n';
1552 } else if (hwcLayer.compositionType == HWC_SIDEBAND) {
1553 output << " Stream: " << hwcLayer.sidebandStream << '\n';
1554 } else {
1555 output << " Buffer: " << hwcLayer.handle << "/" <<
1556 hwcLayer.acquireFenceFd << '\n';
1557 }
1558
1559 output << fill << "Display frame: " << rectString(hwcLayer.displayFrame) <<
1560 '\n';
1561
1562 output << fill << "Source crop: ";
1563 if (hwc1MinorVersion >= 3) {
1564 output << frectString(hwcLayer.sourceCropf) << '\n';
1565 } else {
1566 output << rectString(hwcLayer.sourceCropi) << '\n';
1567 }
1568
1569 output << fill << "Transform: " << hwc1TransformString(hwcLayer.transform);
1570 output << " Blend mode: " << hwc1BlendModeString(hwcLayer.blending);
1571 if (hwcLayer.planeAlpha != 0xFF) {
1572 output << " Alpha: " << alphaString(hwcLayer.planeAlpha / 255.0f);
1573 }
1574 output << '\n';
1575
1576 if (hwcLayer.hints != 0) {
1577 output << fill << "Hints:";
1578 if ((hwcLayer.hints & HWC_HINT_TRIPLE_BUFFER) != 0) {
1579 output << " TripleBuffer";
1580 }
1581 if ((hwcLayer.hints & HWC_HINT_CLEAR_FB) != 0) {
1582 output << " ClearFB";
1583 }
1584 output << '\n';
1585 }
1586
1587 if (hwcLayer.flags != 0) {
1588 output << fill << "Flags:";
1589 if ((hwcLayer.flags & HWC_SKIP_LAYER) != 0) {
1590 output << " SkipLayer";
1591 }
1592 if ((hwcLayer.flags & HWC_IS_CURSOR_LAYER) != 0) {
1593 output << " IsCursorLayer";
1594 }
1595 output << '\n';
1596 }
1597
1598 return output.str();
1599}
1600
1601static std::string to_string(const hwc_display_contents_1_t& hwcContents,
1602 int32_t hwc1MinorVersion)
1603{
1604 const char* fill = " ";
1605
1606 std::stringstream output;
1607 output << fill << "Geometry changed: " <<
1608 ((hwcContents.flags & HWC_GEOMETRY_CHANGED) != 0 ? "Y\n" : "N\n");
1609
1610 output << fill << hwcContents.numHwLayers << " Layer" <<
1611 ((hwcContents.numHwLayers == 1) ? "\n" : "s\n");
1612 for (size_t layer = 0; layer < hwcContents.numHwLayers; ++layer) {
1613 output << fill << " Layer " << layer;
1614 output << to_string(hwcContents.hwLayers[layer], hwc1MinorVersion);
1615 }
1616
1617 if (hwcContents.outbuf != nullptr) {
1618 output << fill << "Output buffer: " << hwcContents.outbuf << "/" <<
1619 hwcContents.outbufAcquireFenceFd << '\n';
1620 }
1621
1622 return output.str();
1623}
1624
1625std::string HWC2On1Adapter::Display::dump() const
1626{
1627 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1628
1629 std::stringstream output;
1630
1631 output << " Display " << mId << ": ";
1632 output << to_string(mType) << " ";
1633 output << "HWC1 ID: " << mHwc1Id << " ";
1634 output << "Power mode: " << to_string(mPowerMode) << " ";
1635 output << "Vsync: " << to_string(mVsyncEnabled) << '\n';
1636
Dan Stoza076ac672016-03-14 10:47:53 -07001637 output << " Color modes [active]:";
1638 for (const auto& mode : mColorModes) {
1639 if (mode == mActiveColorMode) {
1640 output << " [" << mode << ']';
Dan Stozac6998d22015-09-24 17:03:36 -07001641 } else {
Dan Stoza076ac672016-03-14 10:47:53 -07001642 output << " " << mode;
Dan Stozac6998d22015-09-24 17:03:36 -07001643 }
1644 }
1645 output << '\n';
1646
Dan Stoza076ac672016-03-14 10:47:53 -07001647 output << " " << mConfigs.size() << " Config" <<
1648 (mConfigs.size() == 1 ? "" : "s") << " (* active)\n";
1649 for (const auto& config : mConfigs) {
1650 output << (config == mActiveConfig ? " * " : " ");
1651 output << config->toString(true) << '\n';
1652 }
1653
Dan Stozac6998d22015-09-24 17:03:36 -07001654 output << " " << mLayers.size() << " Layer" <<
1655 (mLayers.size() == 1 ? "" : "s") << '\n';
1656 for (const auto& layer : mLayers) {
1657 output << layer->dump();
1658 }
1659
1660 output << " Client target: " << mClientTarget.getBuffer() << '\n';
1661
1662 if (mOutputBuffer.getBuffer() != nullptr) {
1663 output << " Output buffer: " << mOutputBuffer.getBuffer() << '\n';
1664 }
1665
1666 if (mHwc1ReceivedContents) {
1667 output << " Last received HWC1 state\n";
1668 output << to_string(*mHwc1ReceivedContents, mDevice.mHwc1MinorVersion);
1669 } else if (mHwc1RequestedContents) {
1670 output << " Last requested HWC1 state\n";
1671 output << to_string(*mHwc1RequestedContents, mDevice.mHwc1MinorVersion);
1672 }
1673
1674 return output.str();
1675}
1676
1677void HWC2On1Adapter::Display::Config::setAttribute(HWC2::Attribute attribute,
1678 int32_t value)
1679{
1680 mAttributes[attribute] = value;
1681}
1682
1683int32_t HWC2On1Adapter::Display::Config::getAttribute(Attribute attribute) const
1684{
1685 if (mAttributes.count(attribute) == 0) {
1686 return -1;
1687 }
1688 return mAttributes.at(attribute);
1689}
1690
Dan Stoza076ac672016-03-14 10:47:53 -07001691void HWC2On1Adapter::Display::Config::setHwc1Id(uint32_t id)
1692{
Michael Wright28f24d02016-07-12 13:30:53 -07001693 android_color_mode_t colorMode = static_cast<android_color_mode_t>(getAttribute(ColorMode));
1694 mHwc1Ids.emplace(colorMode, id);
Dan Stoza076ac672016-03-14 10:47:53 -07001695}
1696
1697bool HWC2On1Adapter::Display::Config::hasHwc1Id(uint32_t id) const
1698{
1699 for (const auto& idPair : mHwc1Ids) {
1700 if (id == idPair.second) {
1701 return true;
1702 }
1703 }
1704 return false;
1705}
1706
Michael Wright28f24d02016-07-12 13:30:53 -07001707Error HWC2On1Adapter::Display::Config::getColorModeForHwc1Id(
1708 uint32_t id, android_color_mode_t* outMode) const
Dan Stoza076ac672016-03-14 10:47:53 -07001709{
1710 for (const auto& idPair : mHwc1Ids) {
1711 if (id == idPair.second) {
Michael Wright28f24d02016-07-12 13:30:53 -07001712 *outMode = idPair.first;
1713 return Error::None;
Dan Stoza076ac672016-03-14 10:47:53 -07001714 }
1715 }
Michael Wright28f24d02016-07-12 13:30:53 -07001716 ALOGE("Unable to find color mode for HWC ID %" PRIu32 " on config %u", id, mId);
1717 return Error::BadParameter;
Dan Stoza076ac672016-03-14 10:47:53 -07001718}
1719
Michael Wright28f24d02016-07-12 13:30:53 -07001720Error HWC2On1Adapter::Display::Config::getHwc1IdForColorMode(android_color_mode_t mode,
Dan Stoza076ac672016-03-14 10:47:53 -07001721 uint32_t* outId) const
1722{
1723 for (const auto& idPair : mHwc1Ids) {
1724 if (mode == idPair.first) {
1725 *outId = idPair.second;
1726 return Error::None;
1727 }
1728 }
1729 ALOGE("Unable to find HWC1 ID for color mode %d on config %u", mode, mId);
1730 return Error::BadParameter;
1731}
1732
1733bool HWC2On1Adapter::Display::Config::merge(const Config& other)
1734{
1735 auto attributes = {HWC2::Attribute::Width, HWC2::Attribute::Height,
1736 HWC2::Attribute::VsyncPeriod, HWC2::Attribute::DpiX,
1737 HWC2::Attribute::DpiY};
1738 for (auto attribute : attributes) {
1739 if (getAttribute(attribute) != other.getAttribute(attribute)) {
1740 return false;
1741 }
1742 }
Michael Wright28f24d02016-07-12 13:30:53 -07001743 android_color_mode_t otherColorMode =
1744 static_cast<android_color_mode_t>(other.getAttribute(ColorMode));
1745 if (mHwc1Ids.count(otherColorMode) != 0) {
Dan Stoza076ac672016-03-14 10:47:53 -07001746 ALOGE("Attempted to merge two configs (%u and %u) which appear to be "
Michael Wright28f24d02016-07-12 13:30:53 -07001747 "identical", mHwc1Ids.at(otherColorMode),
1748 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001749 return false;
1750 }
Michael Wright28f24d02016-07-12 13:30:53 -07001751 mHwc1Ids.emplace(otherColorMode,
1752 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001753 return true;
1754}
1755
Michael Wright28f24d02016-07-12 13:30:53 -07001756std::set<android_color_mode_t> HWC2On1Adapter::Display::Config::getColorModes() const
Dan Stoza076ac672016-03-14 10:47:53 -07001757{
Michael Wright28f24d02016-07-12 13:30:53 -07001758 std::set<android_color_mode_t> colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001759 for (const auto& idPair : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001760 colorModes.emplace(idPair.first);
Dan Stoza076ac672016-03-14 10:47:53 -07001761 }
Michael Wright28f24d02016-07-12 13:30:53 -07001762 return colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001763}
1764
1765std::string HWC2On1Adapter::Display::Config::toString(bool splitLine) const
Dan Stozac6998d22015-09-24 17:03:36 -07001766{
1767 std::string output;
1768
1769 const size_t BUFFER_SIZE = 100;
1770 char buffer[BUFFER_SIZE] = {};
1771 auto writtenBytes = snprintf(buffer, BUFFER_SIZE,
Dan Stoza076ac672016-03-14 10:47:53 -07001772 "%u x %u", mAttributes.at(HWC2::Attribute::Width),
Dan Stozac6998d22015-09-24 17:03:36 -07001773 mAttributes.at(HWC2::Attribute::Height));
1774 output.append(buffer, writtenBytes);
1775
1776 if (mAttributes.count(HWC2::Attribute::VsyncPeriod) != 0) {
1777 std::memset(buffer, 0, BUFFER_SIZE);
1778 writtenBytes = snprintf(buffer, BUFFER_SIZE, " @ %.1f Hz",
1779 1e9 / mAttributes.at(HWC2::Attribute::VsyncPeriod));
1780 output.append(buffer, writtenBytes);
1781 }
1782
1783 if (mAttributes.count(HWC2::Attribute::DpiX) != 0 &&
1784 mAttributes.at(HWC2::Attribute::DpiX) != -1) {
1785 std::memset(buffer, 0, BUFFER_SIZE);
1786 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1787 ", DPI: %.1f x %.1f",
1788 mAttributes.at(HWC2::Attribute::DpiX) / 1000.0f,
1789 mAttributes.at(HWC2::Attribute::DpiY) / 1000.0f);
1790 output.append(buffer, writtenBytes);
1791 }
1792
Dan Stoza076ac672016-03-14 10:47:53 -07001793 std::memset(buffer, 0, BUFFER_SIZE);
1794 if (splitLine) {
1795 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1796 "\n HWC1 ID/Color transform:");
1797 } else {
1798 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1799 ", HWC1 ID/Color transform:");
1800 }
1801 output.append(buffer, writtenBytes);
1802
1803
1804 for (const auto& id : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001805 android_color_mode_t colorMode = id.first;
Dan Stoza076ac672016-03-14 10:47:53 -07001806 uint32_t hwc1Id = id.second;
1807 std::memset(buffer, 0, BUFFER_SIZE);
Michael Wright28f24d02016-07-12 13:30:53 -07001808 if (colorMode == mDisplay.mActiveColorMode) {
Dan Stoza076ac672016-03-14 10:47:53 -07001809 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 } else {
1812 writtenBytes = snprintf(buffer, BUFFER_SIZE, " %u/%d", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001813 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001814 }
1815 output.append(buffer, writtenBytes);
1816 }
1817
Dan Stozac6998d22015-09-24 17:03:36 -07001818 return output;
1819}
1820
1821std::shared_ptr<const HWC2On1Adapter::Display::Config>
1822 HWC2On1Adapter::Display::getConfig(hwc2_config_t configId) const
1823{
1824 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
1825 return nullptr;
1826 }
1827 return mConfigs[configId];
1828}
1829
Dan Stoza076ac672016-03-14 10:47:53 -07001830void HWC2On1Adapter::Display::populateColorModes()
1831{
Michael Wright28f24d02016-07-12 13:30:53 -07001832 mColorModes = mConfigs[0]->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001833 for (const auto& config : mConfigs) {
Michael Wright28f24d02016-07-12 13:30:53 -07001834 std::set<android_color_mode_t> intersection;
1835 auto configModes = config->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001836 std::set_intersection(mColorModes.cbegin(), mColorModes.cend(),
1837 configModes.cbegin(), configModes.cend(),
1838 std::inserter(intersection, intersection.begin()));
1839 std::swap(intersection, mColorModes);
1840 }
1841}
1842
1843void HWC2On1Adapter::Display::initializeActiveConfig()
1844{
1845 if (mDevice.mHwc1Device->getActiveConfig == nullptr) {
1846 ALOGV("getActiveConfig is null, choosing config 0");
1847 mActiveConfig = mConfigs[0];
Michael Wright28f24d02016-07-12 13:30:53 -07001848 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001849 return;
1850 }
1851
1852 auto activeConfig = mDevice.mHwc1Device->getActiveConfig(
1853 mDevice.mHwc1Device, mHwc1Id);
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001854
1855 // Some devices startup without an activeConfig:
1856 // We need to set one ourselves.
1857 if (activeConfig == HWC_ERROR) {
1858 ALOGV("There is no active configuration: Picking the first one: 0.");
1859 const int defaultIndex = 0;
1860 mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, mHwc1Id, defaultIndex);
1861 activeConfig = defaultIndex;
1862 }
1863
1864 for (const auto& config : mConfigs) {
1865 if (config->hasHwc1Id(activeConfig)) {
1866 ALOGE("Setting active config to %d for HWC1 config %u", config->getId(), activeConfig);
1867 mActiveConfig = config;
1868 if (config->getColorModeForHwc1Id(activeConfig, &mActiveColorMode) != Error::None) {
1869 // This should never happen since we checked for the config's presence before
1870 // setting it as active.
1871 ALOGE("Unable to find color mode for active HWC1 config %d", config->getId());
1872 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001873 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001874 break;
Dan Stoza076ac672016-03-14 10:47:53 -07001875 }
1876 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001877 if (!mActiveConfig) {
1878 ALOGV("Unable to find active HWC1 config %u, defaulting to "
1879 "config 0", activeConfig);
1880 mActiveConfig = mConfigs[0];
1881 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
1882 }
1883
1884
1885
1886
Dan Stoza076ac672016-03-14 10:47:53 -07001887}
1888
Dan Stozac6998d22015-09-24 17:03:36 -07001889void HWC2On1Adapter::Display::reallocateHwc1Contents()
1890{
1891 // Allocate an additional layer for the framebuffer target
1892 auto numLayers = mLayers.size() + 1;
1893 size_t size = sizeof(hwc_display_contents_1_t) +
1894 sizeof(hwc_layer_1_t) * numLayers;
1895 ALOGV("[%" PRIu64 "] reallocateHwc1Contents creating %zd layer%s", mId,
1896 numLayers, numLayers != 1 ? "s" : "");
1897 auto contents =
1898 static_cast<hwc_display_contents_1_t*>(std::calloc(size, 1));
1899 contents->numHwLayers = numLayers;
1900 mHwc1RequestedContents.reset(contents);
1901}
1902
1903void HWC2On1Adapter::Display::assignHwc1LayerIds()
1904{
1905 mHwc1LayerMap.clear();
1906 size_t nextHwc1Id = 0;
1907 for (auto& layer : mLayers) {
1908 mHwc1LayerMap[nextHwc1Id] = layer;
1909 layer->setHwc1Id(nextHwc1Id++);
1910 }
1911}
1912
1913void HWC2On1Adapter::Display::updateTypeChanges(const hwc_layer_1_t& hwc1Layer,
1914 const Layer& layer)
1915{
1916 auto layerId = layer.getId();
1917 switch (hwc1Layer.compositionType) {
1918 case HWC_FRAMEBUFFER:
1919 if (layer.getCompositionType() != Composition::Client) {
1920 mChanges->addTypeChange(layerId, Composition::Client);
1921 }
1922 break;
1923 case HWC_OVERLAY:
1924 if (layer.getCompositionType() != Composition::Device) {
1925 mChanges->addTypeChange(layerId, Composition::Device);
1926 }
1927 break;
1928 case HWC_BACKGROUND:
1929 ALOGE_IF(layer.getCompositionType() != Composition::SolidColor,
1930 "updateTypeChanges: HWC1 requested BACKGROUND, but HWC2"
1931 " wasn't expecting SolidColor");
1932 break;
1933 case HWC_FRAMEBUFFER_TARGET:
1934 // Do nothing, since it shouldn't be modified by HWC1
1935 break;
1936 case HWC_SIDEBAND:
1937 ALOGE_IF(layer.getCompositionType() != Composition::Sideband,
1938 "updateTypeChanges: HWC1 requested SIDEBAND, but HWC2"
1939 " wasn't expecting Sideband");
1940 break;
1941 case HWC_CURSOR_OVERLAY:
1942 ALOGE_IF(layer.getCompositionType() != Composition::Cursor,
1943 "updateTypeChanges: HWC1 requested CURSOR_OVERLAY, but"
1944 " HWC2 wasn't expecting Cursor");
1945 break;
1946 }
1947}
1948
1949void HWC2On1Adapter::Display::updateLayerRequests(
1950 const hwc_layer_1_t& hwc1Layer, const Layer& layer)
1951{
1952 if ((hwc1Layer.hints & HWC_HINT_CLEAR_FB) != 0) {
1953 mChanges->addLayerRequest(layer.getId(),
1954 LayerRequest::ClearClientTarget);
1955 }
1956}
1957
1958void HWC2On1Adapter::Display::prepareFramebufferTarget()
1959{
1960 // We check that mActiveConfig is valid in Display::prepare
1961 int32_t width = mActiveConfig->getAttribute(Attribute::Width);
1962 int32_t height = mActiveConfig->getAttribute(Attribute::Height);
1963
1964 auto& hwc1Target = mHwc1RequestedContents->hwLayers[mLayers.size()];
1965 hwc1Target.compositionType = HWC_FRAMEBUFFER_TARGET;
1966 hwc1Target.releaseFenceFd = -1;
1967 hwc1Target.hints = 0;
1968 hwc1Target.flags = 0;
1969 hwc1Target.transform = 0;
1970 hwc1Target.blending = HWC_BLENDING_PREMULT;
1971 if (mDevice.getHwc1MinorVersion() < 3) {
1972 hwc1Target.sourceCropi = {0, 0, width, height};
1973 } else {
1974 hwc1Target.sourceCropf = {0.0f, 0.0f, static_cast<float>(width),
1975 static_cast<float>(height)};
1976 }
1977 hwc1Target.displayFrame = {0, 0, width, height};
1978 hwc1Target.planeAlpha = 255;
1979 hwc1Target.visibleRegionScreen.numRects = 1;
1980 auto rects = static_cast<hwc_rect_t*>(std::malloc(sizeof(hwc_rect_t)));
1981 rects[0].left = 0;
1982 rects[0].top = 0;
1983 rects[0].right = width;
1984 rects[0].bottom = height;
1985 hwc1Target.visibleRegionScreen.rects = rects;
1986
1987 // We will set this to the correct value in set
1988 hwc1Target.acquireFenceFd = -1;
1989}
1990
1991// Layer functions
1992
1993std::atomic<hwc2_layer_t> HWC2On1Adapter::Layer::sNextId(1);
1994
1995HWC2On1Adapter::Layer::Layer(Display& display)
1996 : mId(sNextId++),
1997 mDisplay(display),
Dan Stozafc4e2022016-02-23 11:43:19 -08001998 mDirtyCount(0),
1999 mBuffer(),
2000 mSurfaceDamage(),
Dan Stozac6998d22015-09-24 17:03:36 -07002001 mBlendMode(*this, BlendMode::None),
2002 mColor(*this, {0, 0, 0, 0}),
2003 mCompositionType(*this, Composition::Invalid),
2004 mDisplayFrame(*this, {0, 0, -1, -1}),
2005 mPlaneAlpha(*this, 0.0f),
2006 mSidebandStream(*this, nullptr),
2007 mSourceCrop(*this, {0.0f, 0.0f, -1.0f, -1.0f}),
2008 mTransform(*this, Transform::None),
2009 mVisibleRegion(*this, std::vector<hwc_rect_t>()),
2010 mZ(0),
Dan Stozafc4e2022016-02-23 11:43:19 -08002011 mReleaseFence(),
Dan Stozac6998d22015-09-24 17:03:36 -07002012 mHwc1Id(0),
2013 mHasUnsupportedPlaneAlpha(false) {}
2014
2015bool HWC2On1Adapter::SortLayersByZ::operator()(
2016 const std::shared_ptr<Layer>& lhs, const std::shared_ptr<Layer>& rhs)
2017{
2018 return lhs->getZ() < rhs->getZ();
2019}
2020
2021Error HWC2On1Adapter::Layer::setBuffer(buffer_handle_t buffer,
2022 int32_t acquireFence)
2023{
2024 ALOGV("Setting acquireFence to %d for layer %" PRIu64, acquireFence, mId);
2025 mBuffer.setBuffer(buffer);
2026 mBuffer.setFence(acquireFence);
2027 return Error::None;
2028}
2029
2030Error HWC2On1Adapter::Layer::setCursorPosition(int32_t x, int32_t y)
2031{
2032 if (mCompositionType.getValue() != Composition::Cursor) {
2033 return Error::BadLayer;
2034 }
2035
2036 if (mDisplay.hasChanges()) {
2037 return Error::NotValidated;
2038 }
2039
2040 auto displayId = mDisplay.getHwc1Id();
2041 auto hwc1Device = mDisplay.getDevice().getHwc1Device();
2042 hwc1Device->setCursorPositionAsync(hwc1Device, displayId, x, y);
2043 return Error::None;
2044}
2045
2046Error HWC2On1Adapter::Layer::setSurfaceDamage(hwc_region_t damage)
2047{
2048 mSurfaceDamage.resize(damage.numRects);
2049 std::copy_n(damage.rects, damage.numRects, mSurfaceDamage.begin());
2050 return Error::None;
2051}
2052
2053// Layer state functions
2054
2055Error HWC2On1Adapter::Layer::setBlendMode(BlendMode mode)
2056{
2057 mBlendMode.setPending(mode);
2058 return Error::None;
2059}
2060
2061Error HWC2On1Adapter::Layer::setColor(hwc_color_t color)
2062{
2063 mColor.setPending(color);
2064 return Error::None;
2065}
2066
2067Error HWC2On1Adapter::Layer::setCompositionType(Composition type)
2068{
2069 mCompositionType.setPending(type);
2070 return Error::None;
2071}
2072
Fabien Sanglard999a7fd2017-02-02 16:59:44 -08002073Error HWC2On1Adapter::Layer::setDataspace(android_dataspace_t)
Dan Stoza5df2a862016-03-24 16:19:37 -07002074{
Dan Stoza5df2a862016-03-24 16:19:37 -07002075 return Error::None;
2076}
2077
Dan Stozac6998d22015-09-24 17:03:36 -07002078Error HWC2On1Adapter::Layer::setDisplayFrame(hwc_rect_t frame)
2079{
2080 mDisplayFrame.setPending(frame);
2081 return Error::None;
2082}
2083
2084Error HWC2On1Adapter::Layer::setPlaneAlpha(float alpha)
2085{
2086 mPlaneAlpha.setPending(alpha);
2087 return Error::None;
2088}
2089
2090Error HWC2On1Adapter::Layer::setSidebandStream(const native_handle_t* stream)
2091{
2092 mSidebandStream.setPending(stream);
2093 return Error::None;
2094}
2095
2096Error HWC2On1Adapter::Layer::setSourceCrop(hwc_frect_t crop)
2097{
2098 mSourceCrop.setPending(crop);
2099 return Error::None;
2100}
2101
2102Error HWC2On1Adapter::Layer::setTransform(Transform transform)
2103{
2104 mTransform.setPending(transform);
2105 return Error::None;
2106}
2107
2108Error HWC2On1Adapter::Layer::setVisibleRegion(hwc_region_t rawVisible)
2109{
2110 std::vector<hwc_rect_t> visible(rawVisible.rects,
2111 rawVisible.rects + rawVisible.numRects);
2112 mVisibleRegion.setPending(std::move(visible));
2113 return Error::None;
2114}
2115
2116Error HWC2On1Adapter::Layer::setZ(uint32_t z)
2117{
2118 mZ = z;
2119 return Error::None;
2120}
2121
2122void HWC2On1Adapter::Layer::addReleaseFence(int fenceFd)
2123{
2124 ALOGV("addReleaseFence %d to layer %" PRIu64, fenceFd, mId);
2125 mReleaseFence.add(fenceFd);
2126}
2127
2128const sp<Fence>& HWC2On1Adapter::Layer::getReleaseFence() const
2129{
2130 return mReleaseFence.get();
2131}
2132
2133void HWC2On1Adapter::Layer::applyState(hwc_layer_1_t& hwc1Layer,
2134 bool applyAllState)
2135{
2136 applyCommonState(hwc1Layer, applyAllState);
2137 auto compositionType = mCompositionType.getPendingValue();
2138 if (compositionType == Composition::SolidColor) {
2139 applySolidColorState(hwc1Layer, applyAllState);
2140 } else if (compositionType == Composition::Sideband) {
2141 applySidebandState(hwc1Layer, applyAllState);
2142 } else {
2143 applyBufferState(hwc1Layer);
2144 }
2145 applyCompositionType(hwc1Layer, applyAllState);
2146}
2147
2148// Layer dump helpers
2149
2150static std::string regionStrings(const std::vector<hwc_rect_t>& visibleRegion,
2151 const std::vector<hwc_rect_t>& surfaceDamage)
2152{
2153 std::string regions;
2154 regions += " Visible Region";
2155 regions.resize(40, ' ');
2156 regions += "Surface Damage\n";
2157
2158 size_t numPrinted = 0;
2159 size_t maxSize = std::max(visibleRegion.size(), surfaceDamage.size());
2160 while (numPrinted < maxSize) {
2161 std::string line(" ");
2162 if (visibleRegion.empty() && numPrinted == 0) {
2163 line += "None";
2164 } else if (numPrinted < visibleRegion.size()) {
2165 line += rectString(visibleRegion[numPrinted]);
2166 }
2167 line.resize(40, ' ');
2168 if (surfaceDamage.empty() && numPrinted == 0) {
2169 line += "None";
2170 } else if (numPrinted < surfaceDamage.size()) {
2171 line += rectString(surfaceDamage[numPrinted]);
2172 }
2173 line += '\n';
2174 regions += line;
2175 ++numPrinted;
2176 }
2177 return regions;
2178}
2179
2180std::string HWC2On1Adapter::Layer::dump() const
2181{
2182 std::stringstream output;
2183 const char* fill = " ";
2184
2185 output << fill << to_string(mCompositionType.getPendingValue());
2186 output << " Layer HWC2/1: " << mId << "/" << mHwc1Id << " ";
2187 output << "Z: " << mZ;
2188 if (mCompositionType.getValue() == HWC2::Composition::SolidColor) {
2189 output << " " << colorString(mColor.getValue());
2190 } else if (mCompositionType.getValue() == HWC2::Composition::Sideband) {
2191 output << " Handle: " << mSidebandStream.getValue() << '\n';
2192 } else {
2193 output << " Buffer: " << mBuffer.getBuffer() << "/" <<
2194 mBuffer.getFence() << '\n';
2195 output << fill << " Display frame [LTRB]: " <<
2196 rectString(mDisplayFrame.getValue()) << '\n';
2197 output << fill << " Source crop: " <<
2198 frectString(mSourceCrop.getValue()) << '\n';
2199 output << fill << " Transform: " << to_string(mTransform.getValue());
2200 output << " Blend mode: " << to_string(mBlendMode.getValue());
2201 if (mPlaneAlpha.getValue() != 1.0f) {
2202 output << " Alpha: " <<
2203 alphaString(mPlaneAlpha.getValue()) << '\n';
2204 } else {
2205 output << '\n';
2206 }
2207 output << regionStrings(mVisibleRegion.getValue(), mSurfaceDamage);
2208 }
2209 return output.str();
2210}
2211
2212static int getHwc1Blending(HWC2::BlendMode blendMode)
2213{
2214 switch (blendMode) {
2215 case BlendMode::Coverage: return HWC_BLENDING_COVERAGE;
2216 case BlendMode::Premultiplied: return HWC_BLENDING_PREMULT;
2217 default: return HWC_BLENDING_NONE;
2218 }
2219}
2220
2221void HWC2On1Adapter::Layer::applyCommonState(hwc_layer_1_t& hwc1Layer,
2222 bool applyAllState)
2223{
2224 auto minorVersion = mDisplay.getDevice().getHwc1MinorVersion();
2225 if (applyAllState || mBlendMode.isDirty()) {
2226 hwc1Layer.blending = getHwc1Blending(mBlendMode.getPendingValue());
2227 mBlendMode.latch();
2228 }
2229 if (applyAllState || mDisplayFrame.isDirty()) {
2230 hwc1Layer.displayFrame = mDisplayFrame.getPendingValue();
2231 mDisplayFrame.latch();
2232 }
2233 if (applyAllState || mPlaneAlpha.isDirty()) {
2234 auto pendingAlpha = mPlaneAlpha.getPendingValue();
2235 if (minorVersion < 2) {
2236 mHasUnsupportedPlaneAlpha = pendingAlpha < 1.0f;
2237 } else {
2238 hwc1Layer.planeAlpha =
2239 static_cast<uint8_t>(255.0f * pendingAlpha + 0.5f);
2240 }
2241 mPlaneAlpha.latch();
2242 }
2243 if (applyAllState || mSourceCrop.isDirty()) {
2244 if (minorVersion < 3) {
2245 auto pending = mSourceCrop.getPendingValue();
2246 hwc1Layer.sourceCropi.left =
2247 static_cast<int32_t>(std::ceil(pending.left));
2248 hwc1Layer.sourceCropi.top =
2249 static_cast<int32_t>(std::ceil(pending.top));
2250 hwc1Layer.sourceCropi.right =
2251 static_cast<int32_t>(std::floor(pending.right));
2252 hwc1Layer.sourceCropi.bottom =
2253 static_cast<int32_t>(std::floor(pending.bottom));
2254 } else {
2255 hwc1Layer.sourceCropf = mSourceCrop.getPendingValue();
2256 }
2257 mSourceCrop.latch();
2258 }
2259 if (applyAllState || mTransform.isDirty()) {
2260 hwc1Layer.transform =
2261 static_cast<uint32_t>(mTransform.getPendingValue());
2262 mTransform.latch();
2263 }
2264 if (applyAllState || mVisibleRegion.isDirty()) {
2265 auto& hwc1VisibleRegion = hwc1Layer.visibleRegionScreen;
2266
2267 std::free(const_cast<hwc_rect_t*>(hwc1VisibleRegion.rects));
2268
2269 auto pending = mVisibleRegion.getPendingValue();
2270 hwc_rect_t* newRects = static_cast<hwc_rect_t*>(
2271 std::malloc(sizeof(hwc_rect_t) * pending.size()));
2272 std::copy(pending.begin(), pending.end(), newRects);
2273 hwc1VisibleRegion.rects = const_cast<const hwc_rect_t*>(newRects);
2274 hwc1VisibleRegion.numRects = pending.size();
2275 mVisibleRegion.latch();
2276 }
2277}
2278
2279void HWC2On1Adapter::Layer::applySolidColorState(hwc_layer_1_t& hwc1Layer,
2280 bool applyAllState)
2281{
2282 if (applyAllState || mColor.isDirty()) {
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002283 // If the device does not support background color it is likely to make
2284 // assumption regarding backgroundColor and handle (both fields occupy
2285 // the same location in hwc_layer_1_t union).
2286 // To not confuse these devices we don't set background color and we
2287 // make sure handle is a null pointer.
2288 if (mDisplay.getDevice().supportsBackgroundColor()) {
2289 hwc1Layer.backgroundColor = mColor.getPendingValue();
2290 mHasUnsupportedBackgroundColor = false;
2291 } else {
2292 hwc1Layer.handle = nullptr;
2293 mHasUnsupportedBackgroundColor = true;
2294 }
Dan Stozac6998d22015-09-24 17:03:36 -07002295 mColor.latch();
2296 }
2297}
2298
2299void HWC2On1Adapter::Layer::applySidebandState(hwc_layer_1_t& hwc1Layer,
2300 bool applyAllState)
2301{
2302 if (applyAllState || mSidebandStream.isDirty()) {
2303 hwc1Layer.sidebandStream = mSidebandStream.getPendingValue();
2304 mSidebandStream.latch();
2305 }
2306}
2307
2308void HWC2On1Adapter::Layer::applyBufferState(hwc_layer_1_t& hwc1Layer)
2309{
2310 hwc1Layer.handle = mBuffer.getBuffer();
2311 hwc1Layer.acquireFenceFd = mBuffer.getFence();
2312}
2313
2314void HWC2On1Adapter::Layer::applyCompositionType(hwc_layer_1_t& hwc1Layer,
2315 bool applyAllState)
2316{
Dan Stoza5df2a862016-03-24 16:19:37 -07002317 // HWC1 never supports color transforms or dataspaces and only sometimes
2318 // supports plane alpha (depending on the version). These require us to drop
2319 // some or all layers to client composition.
Fabien Sanglard999a7fd2017-02-02 16:59:44 -08002320 ALOGV("applyCompositionType");
2321 ALOGV("mHasUnsupportedPlaneAlpha = %d", mHasUnsupportedPlaneAlpha);
2322 ALOGV("mDisplay.hasColorTransform() = %d", mDisplay.hasColorTransform());
2323 ALOGV("mHasUnsupportedBackgroundColor = %d", mHasUnsupportedBackgroundColor);
2324
2325 if (mHasUnsupportedPlaneAlpha || mDisplay.hasColorTransform() ||
2326 mHasUnsupportedBackgroundColor) {
Dan Stozac6998d22015-09-24 17:03:36 -07002327 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2328 hwc1Layer.flags = HWC_SKIP_LAYER;
2329 return;
2330 }
2331
2332 if (applyAllState || mCompositionType.isDirty()) {
2333 hwc1Layer.flags = 0;
2334 switch (mCompositionType.getPendingValue()) {
2335 case Composition::Client:
2336 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2337 hwc1Layer.flags |= HWC_SKIP_LAYER;
2338 break;
2339 case Composition::Device:
2340 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2341 break;
2342 case Composition::SolidColor:
Dan Stoza5df47cb2016-09-15 16:38:42 -07002343 // In theory the following line should work, but since the HWC1
2344 // version of SurfaceFlinger never used HWC_BACKGROUND, HWC1
2345 // devices may not work correctly. To be on the safe side, we
2346 // fall back to client composition.
2347 //
2348 // hwc1Layer.compositionType = HWC_BACKGROUND;
2349 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2350 hwc1Layer.flags |= HWC_SKIP_LAYER;
Dan Stozac6998d22015-09-24 17:03:36 -07002351 break;
2352 case Composition::Cursor:
2353 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2354 if (mDisplay.getDevice().getHwc1MinorVersion() >= 4) {
2355 hwc1Layer.hints |= HWC_IS_CURSOR_LAYER;
2356 }
2357 break;
2358 case Composition::Sideband:
2359 if (mDisplay.getDevice().getHwc1MinorVersion() < 4) {
2360 hwc1Layer.compositionType = HWC_SIDEBAND;
2361 } else {
2362 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2363 hwc1Layer.flags |= HWC_SKIP_LAYER;
2364 }
2365 break;
2366 default:
2367 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2368 hwc1Layer.flags |= HWC_SKIP_LAYER;
2369 break;
2370 }
2371 ALOGV("Layer %" PRIu64 " %s set to %d", mId,
2372 to_string(mCompositionType.getPendingValue()).c_str(),
2373 hwc1Layer.compositionType);
2374 ALOGV_IF(hwc1Layer.flags & HWC_SKIP_LAYER, " and skipping");
2375 mCompositionType.latch();
2376 }
2377}
2378
2379// Adapter helpers
2380
2381void HWC2On1Adapter::populateCapabilities()
2382{
2383 ALOGV("populateCapabilities");
2384 if (mHwc1MinorVersion >= 3U) {
2385 int supportedTypes = 0;
2386 auto result = mHwc1Device->query(mHwc1Device,
2387 HWC_DISPLAY_TYPES_SUPPORTED, &supportedTypes);
Fred Fettingerc50c01e2016-06-14 17:53:10 -05002388 if ((result == 0) && ((supportedTypes & HWC_DISPLAY_VIRTUAL_BIT) != 0)) {
Dan Stozac6998d22015-09-24 17:03:36 -07002389 ALOGI("Found support for HWC virtual displays");
2390 mHwc1SupportsVirtualDisplays = true;
2391 }
2392 }
2393 if (mHwc1MinorVersion >= 4U) {
2394 mCapabilities.insert(Capability::SidebandStream);
2395 }
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002396
2397 // Check for HWC background color layer support.
2398 if (mHwc1MinorVersion >= 1U) {
2399 int backgroundColorSupported = 0;
2400 auto result = mHwc1Device->query(mHwc1Device,
2401 HWC_BACKGROUND_LAYER_SUPPORTED,
2402 &backgroundColorSupported);
2403 if ((result == 0) && (backgroundColorSupported == 1)) {
2404 ALOGV("Found support for HWC background color");
2405 mHwc1SupportsBackgroundColor = true;
2406 }
2407 }
Dan Stozac6998d22015-09-24 17:03:36 -07002408}
2409
2410HWC2On1Adapter::Display* HWC2On1Adapter::getDisplay(hwc2_display_t id)
2411{
Dan Stozafc4e2022016-02-23 11:43:19 -08002412 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002413
2414 auto display = mDisplays.find(id);
2415 if (display == mDisplays.end()) {
2416 return nullptr;
2417 }
2418
2419 return display->second.get();
2420}
2421
2422std::tuple<HWC2On1Adapter::Layer*, Error> HWC2On1Adapter::getLayer(
2423 hwc2_display_t displayId, hwc2_layer_t layerId)
2424{
2425 auto display = getDisplay(displayId);
2426 if (!display) {
2427 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadDisplay);
2428 }
2429
2430 auto layerEntry = mLayers.find(layerId);
2431 if (layerEntry == mLayers.end()) {
2432 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2433 }
2434
2435 auto layer = layerEntry->second;
2436 if (layer->getDisplay().getId() != displayId) {
2437 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2438 }
2439 return std::make_tuple(layer.get(), Error::None);
2440}
2441
2442void HWC2On1Adapter::populatePrimary()
2443{
2444 ALOGV("populatePrimary");
2445
Dan Stozafc4e2022016-02-23 11:43:19 -08002446 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002447
2448 auto display =
2449 std::make_shared<Display>(*this, HWC2::DisplayType::Physical);
2450 mHwc1DisplayMap[HWC_DISPLAY_PRIMARY] = display->getId();
2451 display->setHwc1Id(HWC_DISPLAY_PRIMARY);
2452 display->populateConfigs();
2453 mDisplays.emplace(display->getId(), std::move(display));
2454}
2455
2456bool HWC2On1Adapter::prepareAllDisplays()
2457{
2458 ATRACE_CALL();
2459
Dan Stozafc4e2022016-02-23 11:43:19 -08002460 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002461
2462 for (const auto& displayPair : mDisplays) {
2463 auto& display = displayPair.second;
2464 if (!display->prepare()) {
2465 return false;
2466 }
2467 }
2468
Fabien Sanglard7382ed72017-02-11 22:47:36 -08002469 if (mHwc1DisplayMap.count(HWC_DISPLAY_PRIMARY) == 0) {
Dan Stozac6998d22015-09-24 17:03:36 -07002470 ALOGE("prepareAllDisplays: Unable to find primary HWC1 display");
2471 return false;
2472 }
2473
2474 // Always push the primary display
2475 std::vector<HWC2On1Adapter::Display::HWC1Contents> requestedContents;
2476 auto primaryDisplayId = mHwc1DisplayMap[HWC_DISPLAY_PRIMARY];
2477 auto& primaryDisplay = mDisplays[primaryDisplayId];
2478 auto primaryDisplayContents = primaryDisplay->cloneRequestedContents();
2479 requestedContents.push_back(std::move(primaryDisplayContents));
2480
2481 // Push the external display, if present
2482 if (mHwc1DisplayMap.count(HWC_DISPLAY_EXTERNAL) != 0) {
2483 auto externalDisplayId = mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL];
2484 auto& externalDisplay = mDisplays[externalDisplayId];
2485 auto externalDisplayContents =
2486 externalDisplay->cloneRequestedContents();
2487 requestedContents.push_back(std::move(externalDisplayContents));
2488 } else {
2489 // Even if an external display isn't present, we still need to send
2490 // at least two displays down to HWC1
2491 requestedContents.push_back(nullptr);
2492 }
2493
2494 // Push the hardware virtual display, if supported and present
2495 if (mHwc1MinorVersion >= 3) {
2496 if (mHwc1DisplayMap.count(HWC_DISPLAY_VIRTUAL) != 0) {
2497 auto virtualDisplayId = mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL];
2498 auto& virtualDisplay = mDisplays[virtualDisplayId];
2499 auto virtualDisplayContents =
2500 virtualDisplay->cloneRequestedContents();
2501 requestedContents.push_back(std::move(virtualDisplayContents));
2502 } else {
2503 requestedContents.push_back(nullptr);
2504 }
2505 }
2506
2507 mHwc1Contents.clear();
2508 for (auto& displayContents : requestedContents) {
2509 mHwc1Contents.push_back(displayContents.get());
2510 if (!displayContents) {
2511 continue;
2512 }
2513
2514 ALOGV("Display %zd layers:", mHwc1Contents.size() - 1);
2515 for (size_t l = 0; l < displayContents->numHwLayers; ++l) {
2516 auto& layer = displayContents->hwLayers[l];
2517 ALOGV(" %zd: %d", l, layer.compositionType);
2518 }
2519 }
2520
2521 ALOGV("Calling HWC1 prepare");
2522 {
2523 ATRACE_NAME("HWC1 prepare");
2524 mHwc1Device->prepare(mHwc1Device, mHwc1Contents.size(),
2525 mHwc1Contents.data());
2526 }
2527
2528 for (size_t c = 0; c < mHwc1Contents.size(); ++c) {
2529 auto& contents = mHwc1Contents[c];
2530 if (!contents) {
2531 continue;
2532 }
2533 ALOGV("Display %zd layers:", c);
2534 for (size_t l = 0; l < contents->numHwLayers; ++l) {
2535 ALOGV(" %zd: %d", l, contents->hwLayers[l].compositionType);
2536 }
2537 }
2538
2539 // Return the received contents to their respective displays
2540 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2541 if (mHwc1Contents[hwc1Id] == nullptr) {
2542 continue;
2543 }
2544
2545 auto displayId = mHwc1DisplayMap[hwc1Id];
2546 auto& display = mDisplays[displayId];
2547 display->setReceivedContents(std::move(requestedContents[hwc1Id]));
2548 }
2549
2550 return true;
2551}
2552
2553Error HWC2On1Adapter::setAllDisplays()
2554{
2555 ATRACE_CALL();
2556
Dan Stozafc4e2022016-02-23 11:43:19 -08002557 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002558
2559 // Make sure we're ready to validate
2560 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2561 if (mHwc1Contents[hwc1Id] == nullptr) {
2562 continue;
2563 }
2564
2565 auto displayId = mHwc1DisplayMap[hwc1Id];
2566 auto& display = mDisplays[displayId];
2567 Error error = display->set(*mHwc1Contents[hwc1Id]);
2568 if (error != Error::None) {
2569 ALOGE("setAllDisplays: Failed to set display %zd: %s", hwc1Id,
2570 to_string(error).c_str());
2571 return error;
2572 }
2573 }
2574
2575 ALOGV("Calling HWC1 set");
2576 {
2577 ATRACE_NAME("HWC1 set");
2578 mHwc1Device->set(mHwc1Device, mHwc1Contents.size(),
2579 mHwc1Contents.data());
2580 }
2581
2582 // Add retire and release fences
2583 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2584 if (mHwc1Contents[hwc1Id] == nullptr) {
2585 continue;
2586 }
2587
2588 auto displayId = mHwc1DisplayMap[hwc1Id];
2589 auto& display = mDisplays[displayId];
2590 auto retireFenceFd = mHwc1Contents[hwc1Id]->retireFenceFd;
2591 ALOGV("setAllDisplays: Adding retire fence %d to display %zd",
2592 retireFenceFd, hwc1Id);
2593 display->addRetireFence(mHwc1Contents[hwc1Id]->retireFenceFd);
2594 display->addReleaseFences(*mHwc1Contents[hwc1Id]);
2595 }
2596
2597 return Error::None;
2598}
2599
2600void HWC2On1Adapter::hwc1Invalidate()
2601{
2602 ALOGV("Received hwc1Invalidate");
2603
Dan Stozafc4e2022016-02-23 11:43:19 -08002604 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002605
2606 // If the HWC2-side callback hasn't been registered yet, buffer this until
2607 // it is registered
2608 if (mCallbacks.count(Callback::Refresh) == 0) {
2609 mHasPendingInvalidate = true;
2610 return;
2611 }
2612
2613 const auto& callbackInfo = mCallbacks[Callback::Refresh];
2614 std::vector<hwc2_display_t> displays;
2615 for (const auto& displayPair : mDisplays) {
2616 displays.emplace_back(displayPair.first);
2617 }
2618
2619 // Call back without the state lock held
2620 lock.unlock();
2621
2622 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(callbackInfo.pointer);
2623 for (auto display : displays) {
2624 refresh(callbackInfo.data, display);
2625 }
2626}
2627
2628void HWC2On1Adapter::hwc1Vsync(int hwc1DisplayId, int64_t timestamp)
2629{
2630 ALOGV("Received hwc1Vsync(%d, %" PRId64 ")", hwc1DisplayId, timestamp);
2631
Dan Stozafc4e2022016-02-23 11:43:19 -08002632 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002633
2634 // If the HWC2-side callback hasn't been registered yet, buffer this until
2635 // it is registered
2636 if (mCallbacks.count(Callback::Vsync) == 0) {
2637 mPendingVsyncs.emplace_back(hwc1DisplayId, timestamp);
2638 return;
2639 }
2640
2641 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2642 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", hwc1DisplayId);
2643 return;
2644 }
2645
2646 const auto& callbackInfo = mCallbacks[Callback::Vsync];
2647 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
2648
2649 // Call back without the state lock held
2650 lock.unlock();
2651
2652 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(callbackInfo.pointer);
2653 vsync(callbackInfo.data, displayId, timestamp);
2654}
2655
2656void HWC2On1Adapter::hwc1Hotplug(int hwc1DisplayId, int connected)
2657{
2658 ALOGV("Received hwc1Hotplug(%d, %d)", hwc1DisplayId, connected);
2659
2660 if (hwc1DisplayId != HWC_DISPLAY_EXTERNAL) {
2661 ALOGE("hwc1Hotplug: Received hotplug for non-external display");
2662 return;
2663 }
2664
Dan Stozafc4e2022016-02-23 11:43:19 -08002665 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002666
2667 // If the HWC2-side callback hasn't been registered yet, buffer this until
2668 // it is registered
2669 if (mCallbacks.count(Callback::Hotplug) == 0) {
2670 mPendingHotplugs.emplace_back(hwc1DisplayId, connected);
2671 return;
2672 }
2673
2674 hwc2_display_t displayId = UINT64_MAX;
2675 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2676 if (connected == 0) {
2677 ALOGW("hwc1Hotplug: Received disconnect for unconnected display");
2678 return;
2679 }
2680
2681 // Create a new display on connect
2682 auto display = std::make_shared<HWC2On1Adapter::Display>(*this,
2683 HWC2::DisplayType::Physical);
2684 display->setHwc1Id(HWC_DISPLAY_EXTERNAL);
2685 display->populateConfigs();
2686 displayId = display->getId();
2687 mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL] = displayId;
2688 mDisplays.emplace(displayId, std::move(display));
2689 } else {
2690 if (connected != 0) {
2691 ALOGW("hwc1Hotplug: Received connect for previously connected "
2692 "display");
2693 return;
2694 }
2695
2696 // Disconnect an existing display
2697 displayId = mHwc1DisplayMap[hwc1DisplayId];
2698 mHwc1DisplayMap.erase(HWC_DISPLAY_EXTERNAL);
2699 mDisplays.erase(displayId);
2700 }
2701
2702 const auto& callbackInfo = mCallbacks[Callback::Hotplug];
2703
2704 // Call back without the state lock held
2705 lock.unlock();
2706
2707 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(callbackInfo.pointer);
2708 auto hwc2Connected = (connected == 0) ?
2709 HWC2::Connection::Disconnected : HWC2::Connection::Connected;
2710 hotplug(callbackInfo.data, displayId, static_cast<int32_t>(hwc2Connected));
2711}
2712
2713} // namespace android