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