blob: 03297cae8b8ed484930988d3a45bd043b62bc081 [file] [log] [blame]
Dan Stozac6998d22015-09-24 17:03:36 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Fabien Sanglarde29055f2017-03-08 11:36:46 -080017#include "hwc2on1adapter/HWC2On1Adapter.h"
18
Dan Stozac6998d22015-09-24 17:03:36 -070019//#define LOG_NDEBUG 0
20
21#undef LOG_TAG
22#define LOG_TAG "HWC2On1Adapter"
23#define ATRACE_TAG ATRACE_TAG_GRAPHICS
24
Dan Stozac6998d22015-09-24 17:03:36 -070025
Dan Stozac6998d22015-09-24 17:03:36 -070026#include <inttypes.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027
28#include <chrono>
29#include <cstdlib>
Dan Stozac6998d22015-09-24 17:03:36 -070030#include <sstream>
31
Mark Salyzyna5e161b2016-09-29 08:08:05 -070032#include <hardware/hwcomposer.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070033#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034#include <utils/Trace.h>
35
Dan Stozac6998d22015-09-24 17:03:36 -070036using namespace std::chrono_literals;
37
Dan Stozac6998d22015-09-24 17:03:36 -070038static 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
44template <typename PFN, typename T>
45static 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
51using namespace HWC2;
52
Michael Wright28f24d02016-07-12 13:30:53 -070053static constexpr Attribute ColorMode = static_cast<Attribute>(6);
Dan Stoza076ac672016-03-14 10:47:53 -070054
Dan Stozac6998d22015-09-24 17:03:36 -070055namespace android {
56
Dan Stozac6998d22015-09-24 17:03:36 -070057class HWC2On1Adapter::Callbacks : public hwc_procs_t {
58 public:
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -070059 explicit Callbacks(HWC2On1Adapter& adapter) : mAdapter(adapter) {
Dan Stozac6998d22015-09-24 17:03:36 -070060 invalidate = &invalidateHook;
61 vsync = &vsyncHook;
62 hotplug = &hotplugHook;
63 }
64
65 static void invalidateHook(const hwc_procs_t* procs) {
66 auto callbacks = static_cast<const Callbacks*>(procs);
67 callbacks->mAdapter.hwc1Invalidate();
68 }
69
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
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
86static 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
93HWC2On1Adapter::HWC2On1Adapter(hwc_composer_device_1_t* hwc1Device)
94 : mDumpString(),
95 mHwc1Device(hwc1Device),
96 mHwc1MinorVersion(getMinorVersion(hwc1Device)),
97 mHwc1SupportsVirtualDisplays(false),
Fabien Sanglardeb3db612016-11-18 16:12:31 -080098 mHwc1SupportsBackgroundColor(false),
Dan Stozac6998d22015-09-24 17:03:36 -070099 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
120HWC2On1Adapter::~HWC2On1Adapter() {
121 hwc_close_1(mHwc1Device);
122}
123
124void HWC2On1Adapter::doGetCapabilities(uint32_t* outCount,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800125 int32_t* outCapabilities) {
Dan Stozac6998d22015-09-24 17:03:36 -0700126 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
141hwc2_function_pointer_t HWC2On1Adapter::doGetFunction(
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800142 FunctionDescriptor descriptor) {
Dan Stozac6998d22015-09-24 17:03:36 -0700143 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*>);
Dan Stoza076ac672016-03-14 10:47:53 -0700181 case FunctionDescriptor::GetColorModes:
182 return asFP<HWC2_PFN_GET_COLOR_MODES>(
183 displayHook<decltype(&Display::getColorModes),
184 &Display::getColorModes, uint32_t*, int32_t*>);
Dan Stozac6998d22015-09-24 17:03:36 -0700185 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*>);
Dan Stozaed40eba2016-03-16 12:33:52 -0700209 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*>);
Dan Stozac6998d22015-09-24 17:03:36 -0700214 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,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700231 int32_t, hwc_region_t>);
Dan Stoza076ac672016-03-14 10:47:53 -0700232 case FunctionDescriptor::SetColorMode:
Michael Wright28f24d02016-07-12 13:30:53 -0700233 return asFP<HWC2_PFN_SET_COLOR_MODE>(setColorModeHook);
Dan Stoza5df2a862016-03-24 16:19:37 -0700234 case FunctionDescriptor::SetColorTransform:
235 return asFP<HWC2_PFN_SET_COLOR_TRANSFORM>(setColorTransformHook);
Dan Stozac6998d22015-09-24 17:03:36 -0700236 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*>);
Fabien Sanglard9e45be32017-03-17 11:16:52 -0700248 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>);
Dan Stozac6998d22015-09-24 17:03:36 -0700253
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);
Dan Stoza5df2a862016-03-24 16:19:37 -0700279 case FunctionDescriptor::SetLayerDataspace:
280 return asFP<HWC2_PFN_SET_LAYER_DATASPACE>(setLayerDataspaceHook);
Dan Stozac6998d22015-09-24 17:03:36 -0700281 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
316Error HWC2On1Adapter::createVirtualDisplay(uint32_t width,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800317 uint32_t height, hwc2_display_t* outDisplay) {
Dan Stozafc4e2022016-02-23 11:43:19 -0800318 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700319
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
Dan Stozac6998d22015-09-24 17:03:36 -0700326 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
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800338Error HWC2On1Adapter::destroyVirtualDisplay(hwc2_display_t displayId) {
Dan Stozafc4e2022016-02-23 11:43:19 -0800339 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700340
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
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800352void HWC2On1Adapter::dump(uint32_t* outSize, char* outBuffer) {
Dan Stozac6998d22015-09-24 17:03:36 -0700353 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
Dan Stozafc4e2022016-02-23 11:43:19 -0800368 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex,
369 std::defer_lock);
Dan Stozac6998d22015-09-24 17:03:36 -0700370 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
Dan Stozafc4e2022016-02-23 11:43:19 -0800388 // 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
Dan Stozac6998d22015-09-24 17:03:36 -0700392 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
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800405uint32_t HWC2On1Adapter::getMaxVirtualDisplayCount() {
Dan Stozac6998d22015-09-24 17:03:36 -0700406 return mHwc1SupportsVirtualDisplays ? 1 : 0;
407}
408
409static 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
418Error HWC2On1Adapter::registerCallback(Callback descriptor,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800419 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer) {
Dan Stozac6998d22015-09-24 17:03:36 -0700420 if (!isValid(descriptor)) {
421 return Error::BadParameter;
422 }
423
424 ALOGV("registerCallback(%s, %p, %p)", to_string(descriptor).c_str(),
425 callbackData, pointer);
426
Dan Stozafc4e2022016-02-23 11:43:19 -0800427 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700428
429 mCallbacks[descriptor] = {callbackData, pointer};
430
431 bool hasPendingInvalidate = false;
432 std::vector<hwc2_display_t> displayIds;
433 std::vector<std::pair<hwc2_display_t, int64_t>> pendingVsyncs;
434 std::vector<std::pair<hwc2_display_t, int>> pendingHotplugs;
435
436 if (descriptor == Callback::Refresh) {
437 hasPendingInvalidate = mHasPendingInvalidate;
438 if (hasPendingInvalidate) {
439 for (auto& displayPair : mDisplays) {
440 displayIds.emplace_back(displayPair.first);
441 }
442 }
443 mHasPendingInvalidate = false;
444 } else if (descriptor == Callback::Vsync) {
445 for (auto pending : mPendingVsyncs) {
446 auto hwc1DisplayId = pending.first;
447 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
448 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d",
449 hwc1DisplayId);
450 continue;
451 }
452 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
453 auto timestamp = pending.second;
454 pendingVsyncs.emplace_back(displayId, timestamp);
455 }
456 mPendingVsyncs.clear();
457 } else if (descriptor == Callback::Hotplug) {
458 // Hotplug the primary display
459 pendingHotplugs.emplace_back(mHwc1DisplayMap[HWC_DISPLAY_PRIMARY],
460 static_cast<int32_t>(Connection::Connected));
461
462 for (auto pending : mPendingHotplugs) {
463 auto hwc1DisplayId = pending.first;
464 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
465 ALOGE("hwc1Hotplug: Couldn't find display for HWC1 id %d",
466 hwc1DisplayId);
467 continue;
468 }
469 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
470 auto connected = pending.second;
471 pendingHotplugs.emplace_back(displayId, connected);
472 }
473 }
474
475 // Call pending callbacks without the state lock held
476 lock.unlock();
477
478 if (hasPendingInvalidate) {
479 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(pointer);
480 for (auto displayId : displayIds) {
481 refresh(callbackData, displayId);
482 }
483 }
484 if (!pendingVsyncs.empty()) {
485 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(pointer);
486 for (auto& pendingVsync : pendingVsyncs) {
487 vsync(callbackData, pendingVsync.first, pendingVsync.second);
488 }
489 }
490 if (!pendingHotplugs.empty()) {
491 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(pointer);
492 for (auto& pendingHotplug : pendingHotplugs) {
493 hotplug(callbackData, pendingHotplug.first, pendingHotplug.second);
494 }
495 }
496 return Error::None;
497}
498
499// Display functions
500
501std::atomic<hwc2_display_t> HWC2On1Adapter::Display::sNextId(1);
502
503HWC2On1Adapter::Display::Display(HWC2On1Adapter& device, HWC2::DisplayType type)
504 : mId(sNextId++),
505 mDevice(device),
Dan Stozac6998d22015-09-24 17:03:36 -0700506 mStateMutex(),
Dan Stozac6998d22015-09-24 17:03:36 -0700507 mHwc1RequestedContents(nullptr),
Dan Stozac6998d22015-09-24 17:03:36 -0700508 mRetireFence(),
509 mChanges(),
510 mHwc1Id(-1),
511 mConfigs(),
512 mActiveConfig(nullptr),
Michael Wrightc75ca512016-07-20 21:34:48 +0100513 mActiveColorMode(static_cast<android_color_mode_t>(-1)),
Dan Stozac6998d22015-09-24 17:03:36 -0700514 mName(),
515 mType(type),
516 mPowerMode(PowerMode::Off),
517 mVsyncEnabled(Vsync::Invalid),
518 mClientTarget(),
519 mOutputBuffer(),
Dan Stoza5df2a862016-03-24 16:19:37 -0700520 mHasColorTransform(false),
Dan Stozafc4e2022016-02-23 11:43:19 -0800521 mLayers(),
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800522 mHwc1LayerMap(),
523 mNumAvailableRects(0),
524 mNextAvailableRect(nullptr),
525 mGeometryChanged(false)
526 {}
Dan Stozac6998d22015-09-24 17:03:36 -0700527
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800528Error HWC2On1Adapter::Display::acceptChanges() {
Dan Stozac6998d22015-09-24 17:03:36 -0700529 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
530
531 if (!mChanges) {
532 ALOGV("[%" PRIu64 "] acceptChanges failed, not validated", mId);
533 return Error::NotValidated;
534 }
535
536 ALOGV("[%" PRIu64 "] acceptChanges", mId);
537
538 for (auto& change : mChanges->getTypeChanges()) {
539 auto layerId = change.first;
540 auto type = change.second;
541 auto layer = mDevice.mLayers[layerId];
542 layer->setCompositionType(type);
543 }
544
545 mChanges->clearTypeChanges();
546
Dan Stozac6998d22015-09-24 17:03:36 -0700547 return Error::None;
548}
549
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800550Error HWC2On1Adapter::Display::createLayer(hwc2_layer_t* outLayerId) {
Dan Stozac6998d22015-09-24 17:03:36 -0700551 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
552
553 auto layer = *mLayers.emplace(std::make_shared<Layer>(*this));
554 mDevice.mLayers.emplace(std::make_pair(layer->getId(), layer));
555 *outLayerId = layer->getId();
556 ALOGV("[%" PRIu64 "] created layer %" PRIu64, mId, *outLayerId);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800557 markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -0700558 return Error::None;
559}
560
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800561Error HWC2On1Adapter::Display::destroyLayer(hwc2_layer_t layerId) {
Dan Stozac6998d22015-09-24 17:03:36 -0700562 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
563
564 const auto mapLayer = mDevice.mLayers.find(layerId);
565 if (mapLayer == mDevice.mLayers.end()) {
566 ALOGV("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer",
567 mId, layerId);
568 return Error::BadLayer;
569 }
570 const auto layer = mapLayer->second;
571 mDevice.mLayers.erase(mapLayer);
572 const auto zRange = mLayers.equal_range(layer);
573 for (auto current = zRange.first; current != zRange.second; ++current) {
574 if (**current == *layer) {
575 current = mLayers.erase(current);
576 break;
577 }
578 }
579 ALOGV("[%" PRIu64 "] destroyed layer %" PRIu64, mId, layerId);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800580 markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -0700581 return Error::None;
582}
583
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800584Error HWC2On1Adapter::Display::getActiveConfig(hwc2_config_t* outConfig) {
Dan Stozac6998d22015-09-24 17:03:36 -0700585 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
586
587 if (!mActiveConfig) {
588 ALOGV("[%" PRIu64 "] getActiveConfig --> %s", mId,
589 to_string(Error::BadConfig).c_str());
590 return Error::BadConfig;
591 }
592 auto configId = mActiveConfig->getId();
593 ALOGV("[%" PRIu64 "] getActiveConfig --> %u", mId, configId);
594 *outConfig = configId;
595 return Error::None;
596}
597
598Error HWC2On1Adapter::Display::getAttribute(hwc2_config_t configId,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800599 Attribute attribute, int32_t* outValue) {
Dan Stozac6998d22015-09-24 17:03:36 -0700600 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
601
602 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
603 ALOGV("[%" PRIu64 "] getAttribute failed: bad config (%u)", mId,
604 configId);
605 return Error::BadConfig;
606 }
607 *outValue = mConfigs[configId]->getAttribute(attribute);
608 ALOGV("[%" PRIu64 "] getAttribute(%u, %s) --> %d", mId, configId,
609 to_string(attribute).c_str(), *outValue);
610 return Error::None;
611}
612
613Error HWC2On1Adapter::Display::getChangedCompositionTypes(
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800614 uint32_t* outNumElements, hwc2_layer_t* outLayers, int32_t* outTypes) {
Dan Stozac6998d22015-09-24 17:03:36 -0700615 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
616
617 if (!mChanges) {
618 ALOGE("[%" PRIu64 "] getChangedCompositionTypes failed: not validated",
619 mId);
620 return Error::NotValidated;
621 }
622
623 if ((outLayers == nullptr) || (outTypes == nullptr)) {
624 *outNumElements = mChanges->getTypeChanges().size();
625 return Error::None;
626 }
627
628 uint32_t numWritten = 0;
629 for (const auto& element : mChanges->getTypeChanges()) {
630 if (numWritten == *outNumElements) {
631 break;
632 }
633 auto layerId = element.first;
634 auto intType = static_cast<int32_t>(element.second);
635 ALOGV("Adding %" PRIu64 " %s", layerId,
636 to_string(element.second).c_str());
637 outLayers[numWritten] = layerId;
638 outTypes[numWritten] = intType;
639 ++numWritten;
640 }
641 *outNumElements = numWritten;
642
643 return Error::None;
644}
645
Dan Stoza076ac672016-03-14 10:47:53 -0700646Error HWC2On1Adapter::Display::getColorModes(uint32_t* outNumModes,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800647 int32_t* outModes) {
Dan Stoza076ac672016-03-14 10:47:53 -0700648 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
649
650 if (!outModes) {
651 *outNumModes = mColorModes.size();
652 return Error::None;
653 }
654 uint32_t numModes = std::min(*outNumModes,
655 static_cast<uint32_t>(mColorModes.size()));
656 std::copy_n(mColorModes.cbegin(), numModes, outModes);
657 *outNumModes = numModes;
658 return Error::None;
659}
660
Dan Stozac6998d22015-09-24 17:03:36 -0700661Error HWC2On1Adapter::Display::getConfigs(uint32_t* outNumConfigs,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800662 hwc2_config_t* outConfigs) {
Dan Stozac6998d22015-09-24 17:03:36 -0700663 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
664
665 if (!outConfigs) {
666 *outNumConfigs = mConfigs.size();
667 return Error::None;
668 }
669 uint32_t numWritten = 0;
670 for (const auto& config : mConfigs) {
671 if (numWritten == *outNumConfigs) {
672 break;
673 }
674 outConfigs[numWritten] = config->getId();
675 ++numWritten;
676 }
677 *outNumConfigs = numWritten;
678 return Error::None;
679}
680
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800681Error HWC2On1Adapter::Display::getDozeSupport(int32_t* outSupport) {
Dan Stozac6998d22015-09-24 17:03:36 -0700682 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
683
684 if (mDevice.mHwc1MinorVersion < 4 || mHwc1Id != 0) {
685 *outSupport = 0;
686 } else {
687 *outSupport = 1;
688 }
689 return Error::None;
690}
691
Dan Stozaed40eba2016-03-16 12:33:52 -0700692Error HWC2On1Adapter::Display::getHdrCapabilities(uint32_t* outNumTypes,
693 int32_t* /*outTypes*/, float* /*outMaxLuminance*/,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800694 float* /*outMaxAverageLuminance*/, float* /*outMinLuminance*/) {
Dan Stozaed40eba2016-03-16 12:33:52 -0700695 // This isn't supported on HWC1, so per the HWC2 header, return numTypes = 0
696 *outNumTypes = 0;
697 return Error::None;
698}
699
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800700Error HWC2On1Adapter::Display::getName(uint32_t* outSize, char* outName) {
Dan Stozac6998d22015-09-24 17:03:36 -0700701 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
702
703 if (!outName) {
704 *outSize = mName.size();
705 return Error::None;
706 }
707 auto numCopied = mName.copy(outName, *outSize);
708 *outSize = numCopied;
709 return Error::None;
710}
711
712Error HWC2On1Adapter::Display::getReleaseFences(uint32_t* outNumElements,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800713 hwc2_layer_t* outLayers, int32_t* outFences) {
Dan Stozac6998d22015-09-24 17:03:36 -0700714 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
715
716 uint32_t numWritten = 0;
717 bool outputsNonNull = (outLayers != nullptr) && (outFences != nullptr);
718 for (const auto& layer : mLayers) {
719 if (outputsNonNull && (numWritten == *outNumElements)) {
720 break;
721 }
722
723 auto releaseFence = layer->getReleaseFence();
Fabien Sanglardc8591472017-03-07 10:10:03 -0800724 if (releaseFence != MiniFence::NO_FENCE) {
Dan Stozac6998d22015-09-24 17:03:36 -0700725 if (outputsNonNull) {
726 outLayers[numWritten] = layer->getId();
727 outFences[numWritten] = releaseFence->dup();
728 }
729 ++numWritten;
730 }
731 }
732 *outNumElements = numWritten;
733
734 return Error::None;
735}
736
737Error HWC2On1Adapter::Display::getRequests(int32_t* outDisplayRequests,
738 uint32_t* outNumElements, hwc2_layer_t* outLayers,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800739 int32_t* outLayerRequests) {
Dan Stozac6998d22015-09-24 17:03:36 -0700740 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
741
742 if (!mChanges) {
743 return Error::NotValidated;
744 }
745
746 if (outLayers == nullptr || outLayerRequests == nullptr) {
747 *outNumElements = mChanges->getNumLayerRequests();
748 return Error::None;
749 }
750
Fabien Sanglard601938c2016-11-29 11:10:40 -0800751 // Display requests (HWC2::DisplayRequest) are not supported by hwc1:
752 // A hwc1 has always zero requests for the client.
753 *outDisplayRequests = 0;
754
Dan Stozac6998d22015-09-24 17:03:36 -0700755 uint32_t numWritten = 0;
756 for (const auto& request : mChanges->getLayerRequests()) {
757 if (numWritten == *outNumElements) {
758 break;
759 }
760 outLayers[numWritten] = request.first;
761 outLayerRequests[numWritten] = static_cast<int32_t>(request.second);
762 ++numWritten;
763 }
764
765 return Error::None;
766}
767
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800768Error HWC2On1Adapter::Display::getType(int32_t* outType) {
Dan Stozac6998d22015-09-24 17:03:36 -0700769 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
770
771 *outType = static_cast<int32_t>(mType);
772 return Error::None;
773}
774
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800775Error HWC2On1Adapter::Display::present(int32_t* outRetireFence) {
Dan Stozac6998d22015-09-24 17:03:36 -0700776 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
777
778 if (mChanges) {
779 Error error = mDevice.setAllDisplays();
780 if (error != Error::None) {
781 ALOGE("[%" PRIu64 "] present: setAllDisplaysFailed (%s)", mId,
782 to_string(error).c_str());
783 return error;
784 }
785 }
786
787 *outRetireFence = mRetireFence.get()->dup();
788 ALOGV("[%" PRIu64 "] present returning retire fence %d", mId,
789 *outRetireFence);
790
791 return Error::None;
792}
793
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800794Error HWC2On1Adapter::Display::setActiveConfig(hwc2_config_t configId) {
Dan Stozac6998d22015-09-24 17:03:36 -0700795 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
796
797 auto config = getConfig(configId);
798 if (!config) {
799 return Error::BadConfig;
800 }
Dan Stoza076ac672016-03-14 10:47:53 -0700801 if (config == mActiveConfig) {
802 return Error::None;
Dan Stozac6998d22015-09-24 17:03:36 -0700803 }
Dan Stoza076ac672016-03-14 10:47:53 -0700804
805 if (mDevice.mHwc1MinorVersion >= 4) {
806 uint32_t hwc1Id = 0;
807 auto error = config->getHwc1IdForColorMode(mActiveColorMode, &hwc1Id);
808 if (error != Error::None) {
809 return error;
810 }
811
812 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
813 mHwc1Id, static_cast<int>(hwc1Id));
814 if (intError != 0) {
815 ALOGE("setActiveConfig: Failed to set active config on HWC1 (%d)",
816 intError);
817 return Error::BadConfig;
818 }
819 mActiveConfig = config;
820 }
821
Dan Stozac6998d22015-09-24 17:03:36 -0700822 return Error::None;
823}
824
825Error HWC2On1Adapter::Display::setClientTarget(buffer_handle_t target,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800826 int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/) {
Dan Stozac6998d22015-09-24 17:03:36 -0700827 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
828
829 ALOGV("[%" PRIu64 "] setClientTarget(%p, %d)", mId, target, acquireFence);
830 mClientTarget.setBuffer(target);
831 mClientTarget.setFence(acquireFence);
Dan Stoza5cf424b2016-05-20 14:02:39 -0700832 // dataspace and damage can't be used by HWC1, so ignore them
Dan Stozac6998d22015-09-24 17:03:36 -0700833 return Error::None;
834}
835
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800836Error HWC2On1Adapter::Display::setColorMode(android_color_mode_t mode) {
Dan Stoza076ac672016-03-14 10:47:53 -0700837 std::unique_lock<std::recursive_mutex> lock (mStateMutex);
838
839 ALOGV("[%" PRIu64 "] setColorMode(%d)", mId, mode);
840
841 if (mode == mActiveColorMode) {
842 return Error::None;
843 }
844 if (mColorModes.count(mode) == 0) {
845 ALOGE("[%" PRIu64 "] Mode %d not found in mColorModes", mId, mode);
846 return Error::Unsupported;
847 }
848
849 uint32_t hwc1Config = 0;
850 auto error = mActiveConfig->getHwc1IdForColorMode(mode, &hwc1Config);
851 if (error != Error::None) {
852 return error;
853 }
854
855 ALOGV("[%" PRIu64 "] Setting HWC1 config %u", mId, hwc1Config);
856 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
857 mHwc1Id, hwc1Config);
858 if (intError != 0) {
859 ALOGE("[%" PRIu64 "] Failed to set HWC1 config (%d)", mId, intError);
860 return Error::Unsupported;
861 }
862
863 mActiveColorMode = mode;
864 return Error::None;
865}
866
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800867Error HWC2On1Adapter::Display::setColorTransform(android_color_transform_t hint) {
Dan Stoza5df2a862016-03-24 16:19:37 -0700868 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
869
870 ALOGV("%" PRIu64 "] setColorTransform(%d)", mId,
871 static_cast<int32_t>(hint));
872 mHasColorTransform = (hint != HAL_COLOR_TRANSFORM_IDENTITY);
873 return Error::None;
874}
875
Dan Stozac6998d22015-09-24 17:03:36 -0700876Error HWC2On1Adapter::Display::setOutputBuffer(buffer_handle_t buffer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800877 int32_t releaseFence) {
Dan Stozac6998d22015-09-24 17:03:36 -0700878 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
879
880 ALOGV("[%" PRIu64 "] setOutputBuffer(%p, %d)", mId, buffer, releaseFence);
881 mOutputBuffer.setBuffer(buffer);
882 mOutputBuffer.setFence(releaseFence);
883 return Error::None;
884}
885
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800886static bool isValid(PowerMode mode) {
Dan Stozac6998d22015-09-24 17:03:36 -0700887 switch (mode) {
888 case PowerMode::Off: // Fall-through
889 case PowerMode::DozeSuspend: // Fall-through
890 case PowerMode::Doze: // Fall-through
891 case PowerMode::On: return true;
Dan Stozac6998d22015-09-24 17:03:36 -0700892 }
893}
894
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800895static int getHwc1PowerMode(PowerMode mode) {
Dan Stozac6998d22015-09-24 17:03:36 -0700896 switch (mode) {
897 case PowerMode::Off: return HWC_POWER_MODE_OFF;
898 case PowerMode::DozeSuspend: return HWC_POWER_MODE_DOZE_SUSPEND;
899 case PowerMode::Doze: return HWC_POWER_MODE_DOZE;
900 case PowerMode::On: return HWC_POWER_MODE_NORMAL;
Dan Stozac6998d22015-09-24 17:03:36 -0700901 }
902}
903
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800904Error HWC2On1Adapter::Display::setPowerMode(PowerMode mode) {
Dan Stozac6998d22015-09-24 17:03:36 -0700905 if (!isValid(mode)) {
906 return Error::BadParameter;
907 }
908 if (mode == mPowerMode) {
909 return Error::None;
910 }
911
912 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
913
914 int error = 0;
915 if (mDevice.mHwc1MinorVersion < 4) {
916 error = mDevice.mHwc1Device->blank(mDevice.mHwc1Device, mHwc1Id,
917 mode == PowerMode::Off);
918 } else {
919 error = mDevice.mHwc1Device->setPowerMode(mDevice.mHwc1Device,
920 mHwc1Id, getHwc1PowerMode(mode));
921 }
922 ALOGE_IF(error != 0, "setPowerMode: Failed to set power mode on HWC1 (%d)",
923 error);
924
925 ALOGV("[%" PRIu64 "] setPowerMode(%s)", mId, to_string(mode).c_str());
926 mPowerMode = mode;
927 return Error::None;
928}
929
930static bool isValid(Vsync enable) {
931 switch (enable) {
932 case Vsync::Enable: // Fall-through
933 case Vsync::Disable: return true;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800934 case Vsync::Invalid: return false;
Dan Stozac6998d22015-09-24 17:03:36 -0700935 }
936}
937
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800938Error HWC2On1Adapter::Display::setVsyncEnabled(Vsync enable) {
Dan Stozac6998d22015-09-24 17:03:36 -0700939 if (!isValid(enable)) {
940 return Error::BadParameter;
941 }
942 if (enable == mVsyncEnabled) {
943 return Error::None;
944 }
945
946 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
947
948 int error = mDevice.mHwc1Device->eventControl(mDevice.mHwc1Device,
949 mHwc1Id, HWC_EVENT_VSYNC, enable == Vsync::Enable);
950 ALOGE_IF(error != 0, "setVsyncEnabled: Failed to set vsync on HWC1 (%d)",
951 error);
952
953 mVsyncEnabled = enable;
954 return Error::None;
955}
956
957Error HWC2On1Adapter::Display::validate(uint32_t* outNumTypes,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800958 uint32_t* outNumRequests) {
Dan Stozac6998d22015-09-24 17:03:36 -0700959 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
960
Dan Stozac6998d22015-09-24 17:03:36 -0700961 if (!mChanges) {
962 if (!mDevice.prepareAllDisplays()) {
963 return Error::BadDisplay;
964 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800965 } else {
966 ALOGE("Validate was called more than once!");
Dan Stozac6998d22015-09-24 17:03:36 -0700967 }
968
969 *outNumTypes = mChanges->getNumTypes();
970 *outNumRequests = mChanges->getNumLayerRequests();
971 ALOGV("[%" PRIu64 "] validate --> %u types, %u requests", mId, *outNumTypes,
972 *outNumRequests);
973 for (auto request : mChanges->getTypeChanges()) {
974 ALOGV("Layer %" PRIu64 " --> %s", request.first,
975 to_string(request.second).c_str());
976 }
977 return *outNumTypes > 0 ? Error::HasChanges : Error::None;
978}
979
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800980Error HWC2On1Adapter::Display::updateLayerZ(hwc2_layer_t layerId, uint32_t z) {
Dan Stozac6998d22015-09-24 17:03:36 -0700981 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
982
983 const auto mapLayer = mDevice.mLayers.find(layerId);
984 if (mapLayer == mDevice.mLayers.end()) {
985 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer", mId);
986 return Error::BadLayer;
987 }
988
989 const auto layer = mapLayer->second;
990 const auto zRange = mLayers.equal_range(layer);
991 bool layerOnDisplay = false;
992 for (auto current = zRange.first; current != zRange.second; ++current) {
993 if (**current == *layer) {
994 if ((*current)->getZ() == z) {
995 // Don't change anything if the Z hasn't changed
996 return Error::None;
997 }
998 current = mLayers.erase(current);
999 layerOnDisplay = true;
1000 break;
1001 }
1002 }
1003
1004 if (!layerOnDisplay) {
1005 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display",
1006 mId);
1007 return Error::BadLayer;
1008 }
1009
1010 layer->setZ(z);
1011 mLayers.emplace(std::move(layer));
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001012 markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001013
1014 return Error::None;
1015}
1016
Fabien Sanglard9e45be32017-03-17 11:16:52 -07001017Error HWC2On1Adapter::Display::getClientTargetSupport(uint32_t width, uint32_t height,
1018 int32_t format, int32_t dataspace){
1019 if (mActiveConfig == nullptr) {
1020 return Error::Unsupported;
1021 }
1022
1023 if (width == mActiveConfig->getAttribute(Attribute::Width) &&
1024 height == mActiveConfig->getAttribute(Attribute::Height) &&
1025 format == HAL_PIXEL_FORMAT_RGBA_8888 &&
1026 dataspace == HAL_DATASPACE_UNKNOWN) {
1027 return Error::None;
1028 }
1029
1030 return Error::Unsupported;
1031}
1032
Dan Stoza076ac672016-03-14 10:47:53 -07001033static constexpr uint32_t ATTRIBUTES_WITH_COLOR[] = {
1034 HWC_DISPLAY_VSYNC_PERIOD,
1035 HWC_DISPLAY_WIDTH,
1036 HWC_DISPLAY_HEIGHT,
1037 HWC_DISPLAY_DPI_X,
1038 HWC_DISPLAY_DPI_Y,
1039 HWC_DISPLAY_COLOR_TRANSFORM,
1040 HWC_DISPLAY_NO_ATTRIBUTE,
1041};
1042
1043static constexpr uint32_t ATTRIBUTES_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001044 HWC_DISPLAY_VSYNC_PERIOD,
1045 HWC_DISPLAY_WIDTH,
1046 HWC_DISPLAY_HEIGHT,
1047 HWC_DISPLAY_DPI_X,
1048 HWC_DISPLAY_DPI_Y,
1049 HWC_DISPLAY_NO_ATTRIBUTE,
1050};
Dan Stozac6998d22015-09-24 17:03:36 -07001051
Dan Stoza076ac672016-03-14 10:47:53 -07001052static constexpr size_t NUM_ATTRIBUTES_WITH_COLOR =
1053 sizeof(ATTRIBUTES_WITH_COLOR) / sizeof(uint32_t);
1054static_assert(sizeof(ATTRIBUTES_WITH_COLOR) > sizeof(ATTRIBUTES_WITHOUT_COLOR),
1055 "Attribute tables have unexpected sizes");
1056
1057static constexpr uint32_t ATTRIBUTE_MAP_WITH_COLOR[] = {
1058 6, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1059 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1060 1, // HWC_DISPLAY_WIDTH = 2,
1061 2, // HWC_DISPLAY_HEIGHT = 3,
1062 3, // HWC_DISPLAY_DPI_X = 4,
1063 4, // HWC_DISPLAY_DPI_Y = 5,
1064 5, // HWC_DISPLAY_COLOR_TRANSFORM = 6,
1065};
1066
1067static constexpr uint32_t ATTRIBUTE_MAP_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001068 5, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1069 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1070 1, // HWC_DISPLAY_WIDTH = 2,
1071 2, // HWC_DISPLAY_HEIGHT = 3,
1072 3, // HWC_DISPLAY_DPI_X = 4,
1073 4, // HWC_DISPLAY_DPI_Y = 5,
1074};
1075
1076template <uint32_t attribute>
1077static constexpr bool attributesMatch()
1078{
Dan Stoza076ac672016-03-14 10:47:53 -07001079 bool match = (attribute ==
1080 ATTRIBUTES_WITH_COLOR[ATTRIBUTE_MAP_WITH_COLOR[attribute]]);
1081 if (attribute == HWC_DISPLAY_COLOR_TRANSFORM) {
1082 return match;
1083 }
1084
1085 return match && (attribute ==
1086 ATTRIBUTES_WITHOUT_COLOR[ATTRIBUTE_MAP_WITHOUT_COLOR[attribute]]);
Dan Stozac6998d22015-09-24 17:03:36 -07001087}
1088static_assert(attributesMatch<HWC_DISPLAY_VSYNC_PERIOD>(),
1089 "Tables out of sync");
1090static_assert(attributesMatch<HWC_DISPLAY_WIDTH>(), "Tables out of sync");
1091static_assert(attributesMatch<HWC_DISPLAY_HEIGHT>(), "Tables out of sync");
1092static_assert(attributesMatch<HWC_DISPLAY_DPI_X>(), "Tables out of sync");
1093static_assert(attributesMatch<HWC_DISPLAY_DPI_Y>(), "Tables out of sync");
Dan Stoza076ac672016-03-14 10:47:53 -07001094static_assert(attributesMatch<HWC_DISPLAY_COLOR_TRANSFORM>(),
1095 "Tables out of sync");
Dan Stozac6998d22015-09-24 17:03:36 -07001096
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001097void HWC2On1Adapter::Display::populateConfigs() {
Dan Stozac6998d22015-09-24 17:03:36 -07001098 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1099
1100 ALOGV("[%" PRIu64 "] populateConfigs", mId);
1101
1102 if (mHwc1Id == -1) {
1103 ALOGE("populateConfigs: HWC1 ID not set");
1104 return;
1105 }
1106
1107 const size_t MAX_NUM_CONFIGS = 128;
1108 uint32_t configs[MAX_NUM_CONFIGS] = {};
1109 size_t numConfigs = MAX_NUM_CONFIGS;
1110 mDevice.mHwc1Device->getDisplayConfigs(mDevice.mHwc1Device, mHwc1Id,
1111 configs, &numConfigs);
1112
1113 for (size_t c = 0; c < numConfigs; ++c) {
1114 uint32_t hwc1ConfigId = configs[c];
Dan Stoza076ac672016-03-14 10:47:53 -07001115 auto newConfig = std::make_shared<Config>(*this);
Dan Stozac6998d22015-09-24 17:03:36 -07001116
Dan Stoza076ac672016-03-14 10:47:53 -07001117 int32_t values[NUM_ATTRIBUTES_WITH_COLOR] = {};
1118 bool hasColor = true;
1119 auto result = mDevice.mHwc1Device->getDisplayAttributes(
1120 mDevice.mHwc1Device, mHwc1Id, hwc1ConfigId,
1121 ATTRIBUTES_WITH_COLOR, values);
1122 if (result != 0) {
1123 mDevice.mHwc1Device->getDisplayAttributes(mDevice.mHwc1Device,
1124 mHwc1Id, hwc1ConfigId, ATTRIBUTES_WITHOUT_COLOR, values);
1125 hasColor = false;
Dan Stozac6998d22015-09-24 17:03:36 -07001126 }
Dan Stoza076ac672016-03-14 10:47:53 -07001127
1128 auto attributeMap = hasColor ?
1129 ATTRIBUTE_MAP_WITH_COLOR : ATTRIBUTE_MAP_WITHOUT_COLOR;
1130
1131 newConfig->setAttribute(Attribute::VsyncPeriod,
1132 values[attributeMap[HWC_DISPLAY_VSYNC_PERIOD]]);
1133 newConfig->setAttribute(Attribute::Width,
1134 values[attributeMap[HWC_DISPLAY_WIDTH]]);
1135 newConfig->setAttribute(Attribute::Height,
1136 values[attributeMap[HWC_DISPLAY_HEIGHT]]);
1137 newConfig->setAttribute(Attribute::DpiX,
1138 values[attributeMap[HWC_DISPLAY_DPI_X]]);
1139 newConfig->setAttribute(Attribute::DpiY,
1140 values[attributeMap[HWC_DISPLAY_DPI_Y]]);
1141 if (hasColor) {
Michael Wright28f24d02016-07-12 13:30:53 -07001142 // In HWC1, color modes are referred to as color transforms. To avoid confusion with
1143 // the HWC2 concept of color transforms, we internally refer to them as color modes for
1144 // both HWC1 and 2.
1145 newConfig->setAttribute(ColorMode,
Dan Stoza076ac672016-03-14 10:47:53 -07001146 values[attributeMap[HWC_DISPLAY_COLOR_TRANSFORM]]);
1147 }
1148
Michael Wright28f24d02016-07-12 13:30:53 -07001149 // We can only do this after attempting to read the color mode
Dan Stoza076ac672016-03-14 10:47:53 -07001150 newConfig->setHwc1Id(hwc1ConfigId);
1151
1152 for (auto& existingConfig : mConfigs) {
1153 if (existingConfig->merge(*newConfig)) {
1154 ALOGV("Merged config %d with existing config %u: %s",
1155 hwc1ConfigId, existingConfig->getId(),
1156 existingConfig->toString().c_str());
1157 newConfig.reset();
1158 break;
1159 }
1160 }
1161
1162 // If it wasn't merged with any existing config, add it to the end
1163 if (newConfig) {
1164 newConfig->setId(static_cast<hwc2_config_t>(mConfigs.size()));
1165 ALOGV("Found new config %u: %s", newConfig->getId(),
1166 newConfig->toString().c_str());
1167 mConfigs.emplace_back(std::move(newConfig));
1168 }
Dan Stozac6998d22015-09-24 17:03:36 -07001169 }
Dan Stoza076ac672016-03-14 10:47:53 -07001170
1171 initializeActiveConfig();
1172 populateColorModes();
Dan Stozac6998d22015-09-24 17:03:36 -07001173}
1174
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001175void HWC2On1Adapter::Display::populateConfigs(uint32_t width, uint32_t height) {
Dan Stozac6998d22015-09-24 17:03:36 -07001176 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1177
Dan Stoza076ac672016-03-14 10:47:53 -07001178 mConfigs.emplace_back(std::make_shared<Config>(*this));
Dan Stozac6998d22015-09-24 17:03:36 -07001179 auto& config = mConfigs[0];
1180
1181 config->setAttribute(Attribute::Width, static_cast<int32_t>(width));
1182 config->setAttribute(Attribute::Height, static_cast<int32_t>(height));
Dan Stoza076ac672016-03-14 10:47:53 -07001183 config->setHwc1Id(0);
1184 config->setId(0);
Dan Stozac6998d22015-09-24 17:03:36 -07001185 mActiveConfig = config;
1186}
1187
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001188bool HWC2On1Adapter::Display::prepare() {
Dan Stozac6998d22015-09-24 17:03:36 -07001189 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1190
1191 // Only prepare display contents for displays HWC1 knows about
1192 if (mHwc1Id == -1) {
1193 return true;
1194 }
1195
1196 // It doesn't make sense to prepare a display for which there is no active
1197 // config, so return early
1198 if (!mActiveConfig) {
1199 ALOGE("[%" PRIu64 "] Attempted to prepare, but no config active", mId);
1200 return false;
1201 }
1202
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001203 allocateRequestedContents();
1204 assignHwc1LayerIds();
Dan Stozac6998d22015-09-24 17:03:36 -07001205
1206 mHwc1RequestedContents->retireFenceFd = -1;
1207 mHwc1RequestedContents->flags = 0;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001208 if (mGeometryChanged) {
Dan Stozac6998d22015-09-24 17:03:36 -07001209 mHwc1RequestedContents->flags |= HWC_GEOMETRY_CHANGED;
1210 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001211 mHwc1RequestedContents->outbuf = mOutputBuffer.getBuffer();
1212 mHwc1RequestedContents->outbufAcquireFenceFd = mOutputBuffer.getFence();
Dan Stozac6998d22015-09-24 17:03:36 -07001213
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001214 // +1 is for framebuffer target layer.
1215 mHwc1RequestedContents->numHwLayers = mLayers.size() + 1;
Dan Stozac6998d22015-09-24 17:03:36 -07001216 for (auto& layer : mLayers) {
1217 auto& hwc1Layer = mHwc1RequestedContents->hwLayers[layer->getHwc1Id()];
1218 hwc1Layer.releaseFenceFd = -1;
Fabien Sanglard16ab8192017-01-31 12:12:10 -08001219 hwc1Layer.acquireFenceFd = -1;
Fabien Sanglard999a7fd2017-02-02 16:59:44 -08001220 ALOGV("Applying states for layer %" PRIu64 " ", layer->getId());
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001221 layer->applyState(hwc1Layer);
Dan Stozac6998d22015-09-24 17:03:36 -07001222 }
1223
Dan Stozac6998d22015-09-24 17:03:36 -07001224 prepareFramebufferTarget();
1225
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001226 resetGeometryMarker();
1227
Dan Stozac6998d22015-09-24 17:03:36 -07001228 return true;
1229}
1230
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001231void HWC2On1Adapter::Display::generateChanges() {
Dan Stozac6998d22015-09-24 17:03:36 -07001232 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1233
Dan Stozac6998d22015-09-24 17:03:36 -07001234 mChanges.reset(new Changes);
1235
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001236 size_t numLayers = mHwc1RequestedContents->numHwLayers;
Dan Stozac6998d22015-09-24 17:03:36 -07001237 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001238 const auto& receivedLayer = mHwc1RequestedContents->hwLayers[hwc1Id];
Dan Stozac6998d22015-09-24 17:03:36 -07001239 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1240 ALOGE_IF(receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001241 "generateChanges: HWC1 layer %zd doesn't have a"
Dan Stozac6998d22015-09-24 17:03:36 -07001242 " matching HWC2 layer, and isn't the framebuffer target",
1243 hwc1Id);
1244 continue;
1245 }
1246
1247 Layer& layer = *mHwc1LayerMap[hwc1Id];
1248 updateTypeChanges(receivedLayer, layer);
1249 updateLayerRequests(receivedLayer, layer);
1250 }
1251}
1252
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001253bool HWC2On1Adapter::Display::hasChanges() const {
Dan Stozac6998d22015-09-24 17:03:36 -07001254 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1255 return mChanges != nullptr;
1256}
1257
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001258Error HWC2On1Adapter::Display::set(hwc_display_contents_1& hwcContents) {
Dan Stozac6998d22015-09-24 17:03:36 -07001259 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1260
1261 if (!mChanges || (mChanges->getNumTypes() > 0)) {
1262 ALOGE("[%" PRIu64 "] set failed: not validated", mId);
1263 return Error::NotValidated;
1264 }
1265
1266 // Set up the client/framebuffer target
1267 auto numLayers = hwcContents.numHwLayers;
1268
1269 // Close acquire fences on FRAMEBUFFER layers, since they will not be used
1270 // by HWC
1271 for (size_t l = 0; l < numLayers - 1; ++l) {
1272 auto& layer = hwcContents.hwLayers[l];
1273 if (layer.compositionType == HWC_FRAMEBUFFER) {
1274 ALOGV("Closing fence %d for layer %zd", layer.acquireFenceFd, l);
1275 close(layer.acquireFenceFd);
1276 layer.acquireFenceFd = -1;
1277 }
1278 }
1279
1280 auto& clientTargetLayer = hwcContents.hwLayers[numLayers - 1];
1281 if (clientTargetLayer.compositionType == HWC_FRAMEBUFFER_TARGET) {
1282 clientTargetLayer.handle = mClientTarget.getBuffer();
1283 clientTargetLayer.acquireFenceFd = mClientTarget.getFence();
1284 } else {
1285 ALOGE("[%" PRIu64 "] set: last HWC layer wasn't FRAMEBUFFER_TARGET",
1286 mId);
1287 }
1288
1289 mChanges.reset();
1290
1291 return Error::None;
1292}
1293
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001294void HWC2On1Adapter::Display::addRetireFence(int fenceFd) {
Dan Stozac6998d22015-09-24 17:03:36 -07001295 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1296 mRetireFence.add(fenceFd);
1297}
1298
1299void HWC2On1Adapter::Display::addReleaseFences(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001300 const hwc_display_contents_1_t& hwcContents) {
Dan Stozac6998d22015-09-24 17:03:36 -07001301 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1302
1303 size_t numLayers = hwcContents.numHwLayers;
1304 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1305 const auto& receivedLayer = hwcContents.hwLayers[hwc1Id];
1306 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1307 if (receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET) {
1308 ALOGE("addReleaseFences: HWC1 layer %zd doesn't have a"
1309 " matching HWC2 layer, and isn't the framebuffer"
1310 " target", hwc1Id);
1311 }
1312 // Close the framebuffer target release fence since we will use the
1313 // display retire fence instead
1314 if (receivedLayer.releaseFenceFd != -1) {
1315 close(receivedLayer.releaseFenceFd);
1316 }
1317 continue;
1318 }
1319
1320 Layer& layer = *mHwc1LayerMap[hwc1Id];
1321 ALOGV("Adding release fence %d to layer %" PRIu64,
1322 receivedLayer.releaseFenceFd, layer.getId());
1323 layer.addReleaseFence(receivedLayer.releaseFenceFd);
1324 }
1325}
1326
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001327bool HWC2On1Adapter::Display::hasColorTransform() const {
Dan Stoza5df2a862016-03-24 16:19:37 -07001328 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1329 return mHasColorTransform;
1330}
1331
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001332static std::string hwc1CompositionString(int32_t type) {
Dan Stozac6998d22015-09-24 17:03:36 -07001333 switch (type) {
1334 case HWC_FRAMEBUFFER: return "Framebuffer";
1335 case HWC_OVERLAY: return "Overlay";
1336 case HWC_BACKGROUND: return "Background";
1337 case HWC_FRAMEBUFFER_TARGET: return "FramebufferTarget";
1338 case HWC_SIDEBAND: return "Sideband";
1339 case HWC_CURSOR_OVERLAY: return "CursorOverlay";
1340 default:
1341 return std::string("Unknown (") + std::to_string(type) + ")";
1342 }
1343}
1344
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001345static std::string hwc1TransformString(int32_t transform) {
Dan Stozac6998d22015-09-24 17:03:36 -07001346 switch (transform) {
1347 case 0: return "None";
1348 case HWC_TRANSFORM_FLIP_H: return "FlipH";
1349 case HWC_TRANSFORM_FLIP_V: return "FlipV";
1350 case HWC_TRANSFORM_ROT_90: return "Rotate90";
1351 case HWC_TRANSFORM_ROT_180: return "Rotate180";
1352 case HWC_TRANSFORM_ROT_270: return "Rotate270";
1353 case HWC_TRANSFORM_FLIP_H_ROT_90: return "FlipHRotate90";
1354 case HWC_TRANSFORM_FLIP_V_ROT_90: return "FlipVRotate90";
1355 default:
1356 return std::string("Unknown (") + std::to_string(transform) + ")";
1357 }
1358}
1359
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001360static std::string hwc1BlendModeString(int32_t mode) {
Dan Stozac6998d22015-09-24 17:03:36 -07001361 switch (mode) {
1362 case HWC_BLENDING_NONE: return "None";
1363 case HWC_BLENDING_PREMULT: return "Premultiplied";
1364 case HWC_BLENDING_COVERAGE: return "Coverage";
1365 default:
1366 return std::string("Unknown (") + std::to_string(mode) + ")";
1367 }
1368}
1369
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001370static std::string rectString(hwc_rect_t rect) {
Dan Stozac6998d22015-09-24 17:03:36 -07001371 std::stringstream output;
1372 output << "[" << rect.left << ", " << rect.top << ", ";
1373 output << rect.right << ", " << rect.bottom << "]";
1374 return output.str();
1375}
1376
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001377static std::string approximateFloatString(float f) {
Dan Stozac6998d22015-09-24 17:03:36 -07001378 if (static_cast<int32_t>(f) == f) {
1379 return std::to_string(static_cast<int32_t>(f));
1380 }
1381 int32_t truncated = static_cast<int32_t>(f * 10);
1382 bool approximate = (static_cast<float>(truncated) != f * 10);
1383 const size_t BUFFER_SIZE = 32;
1384 char buffer[BUFFER_SIZE] = {};
1385 auto bytesWritten = snprintf(buffer, BUFFER_SIZE,
1386 "%s%.1f", approximate ? "~" : "", f);
1387 return std::string(buffer, bytesWritten);
1388}
1389
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001390static std::string frectString(hwc_frect_t frect) {
Dan Stozac6998d22015-09-24 17:03:36 -07001391 std::stringstream output;
1392 output << "[" << approximateFloatString(frect.left) << ", ";
1393 output << approximateFloatString(frect.top) << ", ";
1394 output << approximateFloatString(frect.right) << ", ";
1395 output << approximateFloatString(frect.bottom) << "]";
1396 return output.str();
1397}
1398
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001399static std::string colorString(hwc_color_t color) {
Dan Stozac6998d22015-09-24 17:03:36 -07001400 std::stringstream output;
1401 output << "RGBA [";
1402 output << static_cast<int32_t>(color.r) << ", ";
1403 output << static_cast<int32_t>(color.g) << ", ";
1404 output << static_cast<int32_t>(color.b) << ", ";
1405 output << static_cast<int32_t>(color.a) << "]";
1406 return output.str();
1407}
1408
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001409static std::string alphaString(float f) {
Dan Stozac6998d22015-09-24 17:03:36 -07001410 const size_t BUFFER_SIZE = 8;
1411 char buffer[BUFFER_SIZE] = {};
1412 auto bytesWritten = snprintf(buffer, BUFFER_SIZE, "%.3f", f);
1413 return std::string(buffer, bytesWritten);
1414}
1415
1416static std::string to_string(const hwc_layer_1_t& hwcLayer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001417 int32_t hwc1MinorVersion) {
Dan Stozac6998d22015-09-24 17:03:36 -07001418 const char* fill = " ";
1419
1420 std::stringstream output;
1421
1422 output << " Composition: " <<
1423 hwc1CompositionString(hwcLayer.compositionType);
1424
1425 if (hwcLayer.compositionType == HWC_BACKGROUND) {
1426 output << " Color: " << colorString(hwcLayer.backgroundColor) << '\n';
1427 } else if (hwcLayer.compositionType == HWC_SIDEBAND) {
1428 output << " Stream: " << hwcLayer.sidebandStream << '\n';
1429 } else {
1430 output << " Buffer: " << hwcLayer.handle << "/" <<
1431 hwcLayer.acquireFenceFd << '\n';
1432 }
1433
1434 output << fill << "Display frame: " << rectString(hwcLayer.displayFrame) <<
1435 '\n';
1436
1437 output << fill << "Source crop: ";
1438 if (hwc1MinorVersion >= 3) {
1439 output << frectString(hwcLayer.sourceCropf) << '\n';
1440 } else {
1441 output << rectString(hwcLayer.sourceCropi) << '\n';
1442 }
1443
1444 output << fill << "Transform: " << hwc1TransformString(hwcLayer.transform);
1445 output << " Blend mode: " << hwc1BlendModeString(hwcLayer.blending);
1446 if (hwcLayer.planeAlpha != 0xFF) {
1447 output << " Alpha: " << alphaString(hwcLayer.planeAlpha / 255.0f);
1448 }
1449 output << '\n';
1450
1451 if (hwcLayer.hints != 0) {
1452 output << fill << "Hints:";
1453 if ((hwcLayer.hints & HWC_HINT_TRIPLE_BUFFER) != 0) {
1454 output << " TripleBuffer";
1455 }
1456 if ((hwcLayer.hints & HWC_HINT_CLEAR_FB) != 0) {
1457 output << " ClearFB";
1458 }
1459 output << '\n';
1460 }
1461
1462 if (hwcLayer.flags != 0) {
1463 output << fill << "Flags:";
1464 if ((hwcLayer.flags & HWC_SKIP_LAYER) != 0) {
1465 output << " SkipLayer";
1466 }
1467 if ((hwcLayer.flags & HWC_IS_CURSOR_LAYER) != 0) {
1468 output << " IsCursorLayer";
1469 }
1470 output << '\n';
1471 }
1472
1473 return output.str();
1474}
1475
1476static std::string to_string(const hwc_display_contents_1_t& hwcContents,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001477 int32_t hwc1MinorVersion) {
Dan Stozac6998d22015-09-24 17:03:36 -07001478 const char* fill = " ";
1479
1480 std::stringstream output;
1481 output << fill << "Geometry changed: " <<
1482 ((hwcContents.flags & HWC_GEOMETRY_CHANGED) != 0 ? "Y\n" : "N\n");
1483
1484 output << fill << hwcContents.numHwLayers << " Layer" <<
1485 ((hwcContents.numHwLayers == 1) ? "\n" : "s\n");
1486 for (size_t layer = 0; layer < hwcContents.numHwLayers; ++layer) {
1487 output << fill << " Layer " << layer;
1488 output << to_string(hwcContents.hwLayers[layer], hwc1MinorVersion);
1489 }
1490
1491 if (hwcContents.outbuf != nullptr) {
1492 output << fill << "Output buffer: " << hwcContents.outbuf << "/" <<
1493 hwcContents.outbufAcquireFenceFd << '\n';
1494 }
1495
1496 return output.str();
1497}
1498
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001499std::string HWC2On1Adapter::Display::dump() const {
Dan Stozac6998d22015-09-24 17:03:36 -07001500 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1501
1502 std::stringstream output;
1503
1504 output << " Display " << mId << ": ";
1505 output << to_string(mType) << " ";
1506 output << "HWC1 ID: " << mHwc1Id << " ";
1507 output << "Power mode: " << to_string(mPowerMode) << " ";
1508 output << "Vsync: " << to_string(mVsyncEnabled) << '\n';
1509
Dan Stoza076ac672016-03-14 10:47:53 -07001510 output << " Color modes [active]:";
1511 for (const auto& mode : mColorModes) {
1512 if (mode == mActiveColorMode) {
1513 output << " [" << mode << ']';
Dan Stozac6998d22015-09-24 17:03:36 -07001514 } else {
Dan Stoza076ac672016-03-14 10:47:53 -07001515 output << " " << mode;
Dan Stozac6998d22015-09-24 17:03:36 -07001516 }
1517 }
1518 output << '\n';
1519
Dan Stoza076ac672016-03-14 10:47:53 -07001520 output << " " << mConfigs.size() << " Config" <<
1521 (mConfigs.size() == 1 ? "" : "s") << " (* active)\n";
1522 for (const auto& config : mConfigs) {
1523 output << (config == mActiveConfig ? " * " : " ");
1524 output << config->toString(true) << '\n';
1525 }
1526
Dan Stozac6998d22015-09-24 17:03:36 -07001527 output << " " << mLayers.size() << " Layer" <<
1528 (mLayers.size() == 1 ? "" : "s") << '\n';
1529 for (const auto& layer : mLayers) {
1530 output << layer->dump();
1531 }
1532
1533 output << " Client target: " << mClientTarget.getBuffer() << '\n';
1534
1535 if (mOutputBuffer.getBuffer() != nullptr) {
1536 output << " Output buffer: " << mOutputBuffer.getBuffer() << '\n';
1537 }
1538
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001539 if (mHwc1RequestedContents) {
Dan Stozac6998d22015-09-24 17:03:36 -07001540 output << " Last requested HWC1 state\n";
1541 output << to_string(*mHwc1RequestedContents, mDevice.mHwc1MinorVersion);
1542 }
1543
1544 return output.str();
1545}
1546
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001547hwc_rect_t* HWC2On1Adapter::Display::GetRects(size_t numRects) {
1548 if (numRects == 0) {
1549 return nullptr;
1550 }
1551
1552 if (numRects > mNumAvailableRects) {
1553 // This should NEVER happen since we calculated how many rects the
1554 // display would need.
1555 ALOGE("Rect allocation failure! SF is likely to crash soon!");
1556 return nullptr;
1557
1558 }
1559 hwc_rect_t* rects = mNextAvailableRect;
1560 mNextAvailableRect += numRects;
1561 mNumAvailableRects -= numRects;
1562 return rects;
1563}
1564
1565hwc_display_contents_1* HWC2On1Adapter::Display::getDisplayContents() {
1566 return mHwc1RequestedContents.get();
1567}
1568
Dan Stozac6998d22015-09-24 17:03:36 -07001569void HWC2On1Adapter::Display::Config::setAttribute(HWC2::Attribute attribute,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001570 int32_t value) {
Dan Stozac6998d22015-09-24 17:03:36 -07001571 mAttributes[attribute] = value;
1572}
1573
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001574int32_t HWC2On1Adapter::Display::Config::getAttribute(Attribute attribute) const {
Dan Stozac6998d22015-09-24 17:03:36 -07001575 if (mAttributes.count(attribute) == 0) {
1576 return -1;
1577 }
1578 return mAttributes.at(attribute);
1579}
1580
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001581void HWC2On1Adapter::Display::Config::setHwc1Id(uint32_t id) {
Michael Wright28f24d02016-07-12 13:30:53 -07001582 android_color_mode_t colorMode = static_cast<android_color_mode_t>(getAttribute(ColorMode));
1583 mHwc1Ids.emplace(colorMode, id);
Dan Stoza076ac672016-03-14 10:47:53 -07001584}
1585
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001586bool HWC2On1Adapter::Display::Config::hasHwc1Id(uint32_t id) const {
Dan Stoza076ac672016-03-14 10:47:53 -07001587 for (const auto& idPair : mHwc1Ids) {
1588 if (id == idPair.second) {
1589 return true;
1590 }
1591 }
1592 return false;
1593}
1594
Michael Wright28f24d02016-07-12 13:30:53 -07001595Error HWC2On1Adapter::Display::Config::getColorModeForHwc1Id(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001596 uint32_t id, android_color_mode_t* outMode) const {
Dan Stoza076ac672016-03-14 10:47:53 -07001597 for (const auto& idPair : mHwc1Ids) {
1598 if (id == idPair.second) {
Michael Wright28f24d02016-07-12 13:30:53 -07001599 *outMode = idPair.first;
1600 return Error::None;
Dan Stoza076ac672016-03-14 10:47:53 -07001601 }
1602 }
Michael Wright28f24d02016-07-12 13:30:53 -07001603 ALOGE("Unable to find color mode for HWC ID %" PRIu32 " on config %u", id, mId);
1604 return Error::BadParameter;
Dan Stoza076ac672016-03-14 10:47:53 -07001605}
1606
Michael Wright28f24d02016-07-12 13:30:53 -07001607Error HWC2On1Adapter::Display::Config::getHwc1IdForColorMode(android_color_mode_t mode,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001608 uint32_t* outId) const {
Dan Stoza076ac672016-03-14 10:47:53 -07001609 for (const auto& idPair : mHwc1Ids) {
1610 if (mode == idPair.first) {
1611 *outId = idPair.second;
1612 return Error::None;
1613 }
1614 }
1615 ALOGE("Unable to find HWC1 ID for color mode %d on config %u", mode, mId);
1616 return Error::BadParameter;
1617}
1618
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001619bool HWC2On1Adapter::Display::Config::merge(const Config& other) {
Dan Stoza076ac672016-03-14 10:47:53 -07001620 auto attributes = {HWC2::Attribute::Width, HWC2::Attribute::Height,
1621 HWC2::Attribute::VsyncPeriod, HWC2::Attribute::DpiX,
1622 HWC2::Attribute::DpiY};
1623 for (auto attribute : attributes) {
1624 if (getAttribute(attribute) != other.getAttribute(attribute)) {
1625 return false;
1626 }
1627 }
Michael Wright28f24d02016-07-12 13:30:53 -07001628 android_color_mode_t otherColorMode =
1629 static_cast<android_color_mode_t>(other.getAttribute(ColorMode));
1630 if (mHwc1Ids.count(otherColorMode) != 0) {
Dan Stoza076ac672016-03-14 10:47:53 -07001631 ALOGE("Attempted to merge two configs (%u and %u) which appear to be "
Michael Wright28f24d02016-07-12 13:30:53 -07001632 "identical", mHwc1Ids.at(otherColorMode),
1633 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001634 return false;
1635 }
Michael Wright28f24d02016-07-12 13:30:53 -07001636 mHwc1Ids.emplace(otherColorMode,
1637 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001638 return true;
1639}
1640
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001641std::set<android_color_mode_t> HWC2On1Adapter::Display::Config::getColorModes() const {
Michael Wright28f24d02016-07-12 13:30:53 -07001642 std::set<android_color_mode_t> colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001643 for (const auto& idPair : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001644 colorModes.emplace(idPair.first);
Dan Stoza076ac672016-03-14 10:47:53 -07001645 }
Michael Wright28f24d02016-07-12 13:30:53 -07001646 return colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001647}
1648
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001649std::string HWC2On1Adapter::Display::Config::toString(bool splitLine) const {
Dan Stozac6998d22015-09-24 17:03:36 -07001650 std::string output;
1651
1652 const size_t BUFFER_SIZE = 100;
1653 char buffer[BUFFER_SIZE] = {};
1654 auto writtenBytes = snprintf(buffer, BUFFER_SIZE,
Dan Stoza076ac672016-03-14 10:47:53 -07001655 "%u x %u", mAttributes.at(HWC2::Attribute::Width),
Dan Stozac6998d22015-09-24 17:03:36 -07001656 mAttributes.at(HWC2::Attribute::Height));
1657 output.append(buffer, writtenBytes);
1658
1659 if (mAttributes.count(HWC2::Attribute::VsyncPeriod) != 0) {
1660 std::memset(buffer, 0, BUFFER_SIZE);
1661 writtenBytes = snprintf(buffer, BUFFER_SIZE, " @ %.1f Hz",
1662 1e9 / mAttributes.at(HWC2::Attribute::VsyncPeriod));
1663 output.append(buffer, writtenBytes);
1664 }
1665
1666 if (mAttributes.count(HWC2::Attribute::DpiX) != 0 &&
1667 mAttributes.at(HWC2::Attribute::DpiX) != -1) {
1668 std::memset(buffer, 0, BUFFER_SIZE);
1669 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1670 ", DPI: %.1f x %.1f",
1671 mAttributes.at(HWC2::Attribute::DpiX) / 1000.0f,
1672 mAttributes.at(HWC2::Attribute::DpiY) / 1000.0f);
1673 output.append(buffer, writtenBytes);
1674 }
1675
Dan Stoza076ac672016-03-14 10:47:53 -07001676 std::memset(buffer, 0, BUFFER_SIZE);
1677 if (splitLine) {
1678 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1679 "\n HWC1 ID/Color transform:");
1680 } else {
1681 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1682 ", HWC1 ID/Color transform:");
1683 }
1684 output.append(buffer, writtenBytes);
1685
1686
1687 for (const auto& id : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001688 android_color_mode_t colorMode = id.first;
Dan Stoza076ac672016-03-14 10:47:53 -07001689 uint32_t hwc1Id = id.second;
1690 std::memset(buffer, 0, BUFFER_SIZE);
Michael Wright28f24d02016-07-12 13:30:53 -07001691 if (colorMode == mDisplay.mActiveColorMode) {
Dan Stoza076ac672016-03-14 10:47:53 -07001692 writtenBytes = snprintf(buffer, BUFFER_SIZE, " [%u/%d]", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001693 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001694 } else {
1695 writtenBytes = snprintf(buffer, BUFFER_SIZE, " %u/%d", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001696 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001697 }
1698 output.append(buffer, writtenBytes);
1699 }
1700
Dan Stozac6998d22015-09-24 17:03:36 -07001701 return output;
1702}
1703
1704std::shared_ptr<const HWC2On1Adapter::Display::Config>
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001705 HWC2On1Adapter::Display::getConfig(hwc2_config_t configId) const {
Dan Stozac6998d22015-09-24 17:03:36 -07001706 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
1707 return nullptr;
1708 }
1709 return mConfigs[configId];
1710}
1711
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001712void HWC2On1Adapter::Display::populateColorModes() {
Michael Wright28f24d02016-07-12 13:30:53 -07001713 mColorModes = mConfigs[0]->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001714 for (const auto& config : mConfigs) {
Michael Wright28f24d02016-07-12 13:30:53 -07001715 std::set<android_color_mode_t> intersection;
1716 auto configModes = config->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001717 std::set_intersection(mColorModes.cbegin(), mColorModes.cend(),
1718 configModes.cbegin(), configModes.cend(),
1719 std::inserter(intersection, intersection.begin()));
1720 std::swap(intersection, mColorModes);
1721 }
1722}
1723
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001724void HWC2On1Adapter::Display::initializeActiveConfig() {
Dan Stoza076ac672016-03-14 10:47:53 -07001725 if (mDevice.mHwc1Device->getActiveConfig == nullptr) {
1726 ALOGV("getActiveConfig is null, choosing config 0");
1727 mActiveConfig = mConfigs[0];
Michael Wright28f24d02016-07-12 13:30:53 -07001728 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001729 return;
1730 }
1731
1732 auto activeConfig = mDevice.mHwc1Device->getActiveConfig(
1733 mDevice.mHwc1Device, mHwc1Id);
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001734
1735 // Some devices startup without an activeConfig:
1736 // We need to set one ourselves.
1737 if (activeConfig == HWC_ERROR) {
1738 ALOGV("There is no active configuration: Picking the first one: 0.");
1739 const int defaultIndex = 0;
1740 mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, mHwc1Id, defaultIndex);
1741 activeConfig = defaultIndex;
1742 }
1743
1744 for (const auto& config : mConfigs) {
1745 if (config->hasHwc1Id(activeConfig)) {
1746 ALOGE("Setting active config to %d for HWC1 config %u", config->getId(), activeConfig);
1747 mActiveConfig = config;
1748 if (config->getColorModeForHwc1Id(activeConfig, &mActiveColorMode) != Error::None) {
1749 // This should never happen since we checked for the config's presence before
1750 // setting it as active.
1751 ALOGE("Unable to find color mode for active HWC1 config %d", config->getId());
1752 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001753 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001754 break;
Dan Stoza076ac672016-03-14 10:47:53 -07001755 }
1756 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001757 if (!mActiveConfig) {
1758 ALOGV("Unable to find active HWC1 config %u, defaulting to "
1759 "config 0", activeConfig);
1760 mActiveConfig = mConfigs[0];
1761 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
1762 }
1763
1764
1765
1766
Dan Stoza076ac672016-03-14 10:47:53 -07001767}
1768
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001769void HWC2On1Adapter::Display::allocateRequestedContents() {
1770 // What needs to be allocated:
1771 // 1 hwc_display_contents_1_t
1772 // 1 hwc_layer_1_t for each layer
1773 // 1 hwc_rect_t for each layer's surfaceDamage
1774 // 1 hwc_rect_t for each layer's visibleRegion
1775 // 1 hwc_layer_1_t for the framebuffer
1776 // 1 hwc_rect_t for the framebuffer's visibleRegion
1777
1778 // Count # of surfaceDamage
1779 size_t numSurfaceDamages = 0;
1780 for (const auto& layer : mLayers) {
1781 numSurfaceDamages += layer->getNumSurfaceDamages();
1782 }
1783
1784 // Count # of visibleRegions (start at 1 for mandatory framebuffer target
1785 // region)
1786 size_t numVisibleRegion = 1;
1787 for (const auto& layer : mLayers) {
1788 numVisibleRegion += layer->getNumVisibleRegions();
1789 }
1790
1791 size_t numRects = numVisibleRegion + numSurfaceDamages;
Dan Stozac6998d22015-09-24 17:03:36 -07001792 auto numLayers = mLayers.size() + 1;
1793 size_t size = sizeof(hwc_display_contents_1_t) +
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001794 sizeof(hwc_layer_1_t) * numLayers +
1795 sizeof(hwc_rect_t) * numRects;
1796 auto contents = static_cast<hwc_display_contents_1_t*>(std::calloc(size, 1));
Dan Stozac6998d22015-09-24 17:03:36 -07001797 mHwc1RequestedContents.reset(contents);
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001798 mNextAvailableRect = reinterpret_cast<hwc_rect_t*>(&contents->hwLayers[numLayers]);
1799 mNumAvailableRects = numRects;
Dan Stozac6998d22015-09-24 17:03:36 -07001800}
1801
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001802void HWC2On1Adapter::Display::assignHwc1LayerIds() {
Dan Stozac6998d22015-09-24 17:03:36 -07001803 mHwc1LayerMap.clear();
1804 size_t nextHwc1Id = 0;
1805 for (auto& layer : mLayers) {
1806 mHwc1LayerMap[nextHwc1Id] = layer;
1807 layer->setHwc1Id(nextHwc1Id++);
1808 }
1809}
1810
1811void HWC2On1Adapter::Display::updateTypeChanges(const hwc_layer_1_t& hwc1Layer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001812 const Layer& layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07001813 auto layerId = layer.getId();
1814 switch (hwc1Layer.compositionType) {
1815 case HWC_FRAMEBUFFER:
1816 if (layer.getCompositionType() != Composition::Client) {
1817 mChanges->addTypeChange(layerId, Composition::Client);
1818 }
1819 break;
1820 case HWC_OVERLAY:
1821 if (layer.getCompositionType() != Composition::Device) {
1822 mChanges->addTypeChange(layerId, Composition::Device);
1823 }
1824 break;
1825 case HWC_BACKGROUND:
1826 ALOGE_IF(layer.getCompositionType() != Composition::SolidColor,
1827 "updateTypeChanges: HWC1 requested BACKGROUND, but HWC2"
1828 " wasn't expecting SolidColor");
1829 break;
1830 case HWC_FRAMEBUFFER_TARGET:
1831 // Do nothing, since it shouldn't be modified by HWC1
1832 break;
1833 case HWC_SIDEBAND:
1834 ALOGE_IF(layer.getCompositionType() != Composition::Sideband,
1835 "updateTypeChanges: HWC1 requested SIDEBAND, but HWC2"
1836 " wasn't expecting Sideband");
1837 break;
1838 case HWC_CURSOR_OVERLAY:
1839 ALOGE_IF(layer.getCompositionType() != Composition::Cursor,
1840 "updateTypeChanges: HWC1 requested CURSOR_OVERLAY, but"
1841 " HWC2 wasn't expecting Cursor");
1842 break;
1843 }
1844}
1845
1846void HWC2On1Adapter::Display::updateLayerRequests(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001847 const hwc_layer_1_t& hwc1Layer, const Layer& layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07001848 if ((hwc1Layer.hints & HWC_HINT_CLEAR_FB) != 0) {
1849 mChanges->addLayerRequest(layer.getId(),
1850 LayerRequest::ClearClientTarget);
1851 }
1852}
1853
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001854void HWC2On1Adapter::Display::prepareFramebufferTarget() {
Dan Stozac6998d22015-09-24 17:03:36 -07001855 // We check that mActiveConfig is valid in Display::prepare
1856 int32_t width = mActiveConfig->getAttribute(Attribute::Width);
1857 int32_t height = mActiveConfig->getAttribute(Attribute::Height);
1858
1859 auto& hwc1Target = mHwc1RequestedContents->hwLayers[mLayers.size()];
1860 hwc1Target.compositionType = HWC_FRAMEBUFFER_TARGET;
1861 hwc1Target.releaseFenceFd = -1;
1862 hwc1Target.hints = 0;
1863 hwc1Target.flags = 0;
1864 hwc1Target.transform = 0;
1865 hwc1Target.blending = HWC_BLENDING_PREMULT;
1866 if (mDevice.getHwc1MinorVersion() < 3) {
1867 hwc1Target.sourceCropi = {0, 0, width, height};
1868 } else {
1869 hwc1Target.sourceCropf = {0.0f, 0.0f, static_cast<float>(width),
1870 static_cast<float>(height)};
1871 }
1872 hwc1Target.displayFrame = {0, 0, width, height};
1873 hwc1Target.planeAlpha = 255;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001874
Dan Stozac6998d22015-09-24 17:03:36 -07001875 hwc1Target.visibleRegionScreen.numRects = 1;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001876 hwc_rect_t* rects = GetRects(1);
Dan Stozac6998d22015-09-24 17:03:36 -07001877 rects[0].left = 0;
1878 rects[0].top = 0;
1879 rects[0].right = width;
1880 rects[0].bottom = height;
1881 hwc1Target.visibleRegionScreen.rects = rects;
1882
1883 // We will set this to the correct value in set
1884 hwc1Target.acquireFenceFd = -1;
1885}
1886
1887// Layer functions
1888
1889std::atomic<hwc2_layer_t> HWC2On1Adapter::Layer::sNextId(1);
1890
1891HWC2On1Adapter::Layer::Layer(Display& display)
1892 : mId(sNextId++),
1893 mDisplay(display),
Dan Stozafc4e2022016-02-23 11:43:19 -08001894 mBuffer(),
1895 mSurfaceDamage(),
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001896 mBlendMode(BlendMode::None),
1897 mColor({0, 0, 0, 0}),
1898 mCompositionType(Composition::Invalid),
1899 mDisplayFrame({0, 0, -1, -1}),
1900 mPlaneAlpha(0.0f),
1901 mSidebandStream(nullptr),
1902 mSourceCrop({0.0f, 0.0f, -1.0f, -1.0f}),
1903 mTransform(Transform::None),
1904 mVisibleRegion(),
Dan Stozac6998d22015-09-24 17:03:36 -07001905 mZ(0),
Dan Stozafc4e2022016-02-23 11:43:19 -08001906 mReleaseFence(),
Dan Stozac6998d22015-09-24 17:03:36 -07001907 mHwc1Id(0),
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001908 mHasUnsupportedPlaneAlpha(false) {}
Dan Stozac6998d22015-09-24 17:03:36 -07001909
1910bool HWC2On1Adapter::SortLayersByZ::operator()(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001911 const std::shared_ptr<Layer>& lhs, const std::shared_ptr<Layer>& rhs) {
Dan Stozac6998d22015-09-24 17:03:36 -07001912 return lhs->getZ() < rhs->getZ();
1913}
1914
1915Error HWC2On1Adapter::Layer::setBuffer(buffer_handle_t buffer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001916 int32_t acquireFence) {
Dan Stozac6998d22015-09-24 17:03:36 -07001917 ALOGV("Setting acquireFence to %d for layer %" PRIu64, acquireFence, mId);
1918 mBuffer.setBuffer(buffer);
1919 mBuffer.setFence(acquireFence);
1920 return Error::None;
1921}
1922
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001923Error HWC2On1Adapter::Layer::setCursorPosition(int32_t x, int32_t y) {
1924 if (mCompositionType != Composition::Cursor) {
Dan Stozac6998d22015-09-24 17:03:36 -07001925 return Error::BadLayer;
1926 }
1927
1928 if (mDisplay.hasChanges()) {
1929 return Error::NotValidated;
1930 }
1931
1932 auto displayId = mDisplay.getHwc1Id();
1933 auto hwc1Device = mDisplay.getDevice().getHwc1Device();
1934 hwc1Device->setCursorPositionAsync(hwc1Device, displayId, x, y);
1935 return Error::None;
1936}
1937
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001938Error HWC2On1Adapter::Layer::setSurfaceDamage(hwc_region_t damage) {
Fabien Sanglard356bcd42017-02-17 16:14:18 -08001939 // HWC1 supports surface damage starting only with version 1.5.
1940 if (mDisplay.getDevice().mHwc1MinorVersion < 5) {
1941 return Error::None;
1942 }
Dan Stozac6998d22015-09-24 17:03:36 -07001943 mSurfaceDamage.resize(damage.numRects);
1944 std::copy_n(damage.rects, damage.numRects, mSurfaceDamage.begin());
1945 return Error::None;
1946}
1947
1948// Layer state functions
1949
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001950Error HWC2On1Adapter::Layer::setBlendMode(BlendMode mode) {
1951 mBlendMode = mode;
1952 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001953 return Error::None;
1954}
1955
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001956Error HWC2On1Adapter::Layer::setColor(hwc_color_t color) {
1957 mColor = color;
1958 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001959 return Error::None;
1960}
1961
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001962Error HWC2On1Adapter::Layer::setCompositionType(Composition type) {
1963 mCompositionType = type;
1964 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001965 return Error::None;
1966}
1967
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001968Error HWC2On1Adapter::Layer::setDataspace(android_dataspace_t) {
Dan Stoza5df2a862016-03-24 16:19:37 -07001969 return Error::None;
1970}
1971
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001972Error HWC2On1Adapter::Layer::setDisplayFrame(hwc_rect_t frame) {
1973 mDisplayFrame = frame;
1974 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001975 return Error::None;
1976}
1977
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001978Error HWC2On1Adapter::Layer::setPlaneAlpha(float alpha) {
1979 mPlaneAlpha = alpha;
1980 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001981 return Error::None;
1982}
1983
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001984Error HWC2On1Adapter::Layer::setSidebandStream(const native_handle_t* stream) {
1985 mSidebandStream = stream;
1986 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001987 return Error::None;
1988}
1989
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001990Error HWC2On1Adapter::Layer::setSourceCrop(hwc_frect_t crop) {
1991 mSourceCrop = crop;
1992 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001993 return Error::None;
1994}
1995
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001996Error HWC2On1Adapter::Layer::setTransform(Transform transform) {
1997 mTransform = transform;
1998 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001999 return Error::None;
2000}
2001
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002002Error HWC2On1Adapter::Layer::setVisibleRegion(hwc_region_t visible) {
2003 mVisibleRegion.resize(visible.numRects);
2004 std::copy_n(visible.rects, visible.numRects, mVisibleRegion.begin());
2005 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07002006 return Error::None;
2007}
2008
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002009Error HWC2On1Adapter::Layer::setZ(uint32_t z) {
Dan Stozac6998d22015-09-24 17:03:36 -07002010 mZ = z;
2011 return Error::None;
2012}
2013
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002014void HWC2On1Adapter::Layer::addReleaseFence(int fenceFd) {
Dan Stozac6998d22015-09-24 17:03:36 -07002015 ALOGV("addReleaseFence %d to layer %" PRIu64, fenceFd, mId);
2016 mReleaseFence.add(fenceFd);
2017}
2018
Fabien Sanglardc8591472017-03-07 10:10:03 -08002019const sp<MiniFence>& HWC2On1Adapter::Layer::getReleaseFence() const {
Dan Stozac6998d22015-09-24 17:03:36 -07002020 return mReleaseFence.get();
2021}
2022
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002023void HWC2On1Adapter::Layer::applyState(hwc_layer_1_t& hwc1Layer) {
2024 applyCommonState(hwc1Layer);
2025 applyCompositionType(hwc1Layer);
2026 switch (mCompositionType) {
2027 case Composition::SolidColor : applySolidColorState(hwc1Layer); break;
2028 case Composition::Sideband : applySidebandState(hwc1Layer); break;
2029 default: applyBufferState(hwc1Layer); break;
Dan Stozac6998d22015-09-24 17:03:36 -07002030 }
Dan Stozac6998d22015-09-24 17:03:36 -07002031}
2032
Dan Stozac6998d22015-09-24 17:03:36 -07002033static std::string regionStrings(const std::vector<hwc_rect_t>& visibleRegion,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002034 const std::vector<hwc_rect_t>& surfaceDamage) {
Dan Stozac6998d22015-09-24 17:03:36 -07002035 std::string regions;
2036 regions += " Visible Region";
2037 regions.resize(40, ' ');
2038 regions += "Surface Damage\n";
2039
2040 size_t numPrinted = 0;
2041 size_t maxSize = std::max(visibleRegion.size(), surfaceDamage.size());
2042 while (numPrinted < maxSize) {
2043 std::string line(" ");
2044 if (visibleRegion.empty() && numPrinted == 0) {
2045 line += "None";
2046 } else if (numPrinted < visibleRegion.size()) {
2047 line += rectString(visibleRegion[numPrinted]);
2048 }
2049 line.resize(40, ' ');
2050 if (surfaceDamage.empty() && numPrinted == 0) {
2051 line += "None";
2052 } else if (numPrinted < surfaceDamage.size()) {
2053 line += rectString(surfaceDamage[numPrinted]);
2054 }
2055 line += '\n';
2056 regions += line;
2057 ++numPrinted;
2058 }
2059 return regions;
2060}
2061
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002062std::string HWC2On1Adapter::Layer::dump() const {
Dan Stozac6998d22015-09-24 17:03:36 -07002063 std::stringstream output;
2064 const char* fill = " ";
2065
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002066 output << fill << to_string(mCompositionType);
Dan Stozac6998d22015-09-24 17:03:36 -07002067 output << " Layer HWC2/1: " << mId << "/" << mHwc1Id << " ";
2068 output << "Z: " << mZ;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002069 if (mCompositionType == HWC2::Composition::SolidColor) {
2070 output << " " << colorString(mColor);
2071 } else if (mCompositionType == HWC2::Composition::Sideband) {
2072 output << " Handle: " << mSidebandStream << '\n';
Dan Stozac6998d22015-09-24 17:03:36 -07002073 } else {
2074 output << " Buffer: " << mBuffer.getBuffer() << "/" <<
2075 mBuffer.getFence() << '\n';
2076 output << fill << " Display frame [LTRB]: " <<
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002077 rectString(mDisplayFrame) << '\n';
Dan Stozac6998d22015-09-24 17:03:36 -07002078 output << fill << " Source crop: " <<
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002079 frectString(mSourceCrop) << '\n';
2080 output << fill << " Transform: " << to_string(mTransform);
2081 output << " Blend mode: " << to_string(mBlendMode);
2082 if (mPlaneAlpha != 1.0f) {
Dan Stozac6998d22015-09-24 17:03:36 -07002083 output << " Alpha: " <<
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002084 alphaString(mPlaneAlpha) << '\n';
Dan Stozac6998d22015-09-24 17:03:36 -07002085 } else {
2086 output << '\n';
2087 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002088 output << regionStrings(mVisibleRegion, mSurfaceDamage);
Dan Stozac6998d22015-09-24 17:03:36 -07002089 }
2090 return output.str();
2091}
2092
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002093static int getHwc1Blending(HWC2::BlendMode blendMode) {
Dan Stozac6998d22015-09-24 17:03:36 -07002094 switch (blendMode) {
2095 case BlendMode::Coverage: return HWC_BLENDING_COVERAGE;
2096 case BlendMode::Premultiplied: return HWC_BLENDING_PREMULT;
2097 default: return HWC_BLENDING_NONE;
2098 }
2099}
2100
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002101void HWC2On1Adapter::Layer::applyCommonState(hwc_layer_1_t& hwc1Layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07002102 auto minorVersion = mDisplay.getDevice().getHwc1MinorVersion();
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002103 hwc1Layer.blending = getHwc1Blending(mBlendMode);
2104 hwc1Layer.displayFrame = mDisplayFrame;
Dan Stozac6998d22015-09-24 17:03:36 -07002105
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002106 auto pendingAlpha = mPlaneAlpha;
2107 if (minorVersion < 2) {
2108 mHasUnsupportedPlaneAlpha = pendingAlpha < 1.0f;
2109 } else {
2110 hwc1Layer.planeAlpha =
2111 static_cast<uint8_t>(255.0f * pendingAlpha + 0.5f);
2112 }
Dan Stozac6998d22015-09-24 17:03:36 -07002113
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002114 if (minorVersion < 3) {
2115 auto pending = mSourceCrop;
2116 hwc1Layer.sourceCropi.left =
2117 static_cast<int32_t>(std::ceil(pending.left));
2118 hwc1Layer.sourceCropi.top =
2119 static_cast<int32_t>(std::ceil(pending.top));
2120 hwc1Layer.sourceCropi.right =
2121 static_cast<int32_t>(std::floor(pending.right));
2122 hwc1Layer.sourceCropi.bottom =
2123 static_cast<int32_t>(std::floor(pending.bottom));
2124 } else {
2125 hwc1Layer.sourceCropf = mSourceCrop;
2126 }
2127
2128 hwc1Layer.transform = static_cast<uint32_t>(mTransform);
2129
2130 auto& hwc1VisibleRegion = hwc1Layer.visibleRegionScreen;
2131 hwc1VisibleRegion.numRects = mVisibleRegion.size();
2132 hwc_rect_t* rects = mDisplay.GetRects(hwc1VisibleRegion.numRects);
2133 hwc1VisibleRegion.rects = rects;
2134 for (size_t i = 0; i < mVisibleRegion.size(); i++) {
2135 rects[i] = mVisibleRegion[i];
Dan Stozac6998d22015-09-24 17:03:36 -07002136 }
2137}
2138
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002139void HWC2On1Adapter::Layer::applySolidColorState(hwc_layer_1_t& hwc1Layer) {
2140 // If the device does not support background color it is likely to make
2141 // assumption regarding backgroundColor and handle (both fields occupy
2142 // the same location in hwc_layer_1_t union).
2143 // To not confuse these devices we don't set background color and we
2144 // make sure handle is a null pointer.
2145 if (hasUnsupportedBackgroundColor()) {
2146 hwc1Layer.handle = nullptr;
2147 } else {
2148 hwc1Layer.backgroundColor = mColor;
Dan Stozac6998d22015-09-24 17:03:36 -07002149 }
2150}
2151
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002152void HWC2On1Adapter::Layer::applySidebandState(hwc_layer_1_t& hwc1Layer) {
2153 hwc1Layer.sidebandStream = mSidebandStream;
Dan Stozac6998d22015-09-24 17:03:36 -07002154}
2155
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002156void HWC2On1Adapter::Layer::applyBufferState(hwc_layer_1_t& hwc1Layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07002157 hwc1Layer.handle = mBuffer.getBuffer();
2158 hwc1Layer.acquireFenceFd = mBuffer.getFence();
2159}
2160
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002161void HWC2On1Adapter::Layer::applyCompositionType(hwc_layer_1_t& hwc1Layer) {
Dan Stoza5df2a862016-03-24 16:19:37 -07002162 // HWC1 never supports color transforms or dataspaces and only sometimes
2163 // supports plane alpha (depending on the version). These require us to drop
2164 // some or all layers to client composition.
Fabien Sanglard999a7fd2017-02-02 16:59:44 -08002165 if (mHasUnsupportedPlaneAlpha || mDisplay.hasColorTransform() ||
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002166 hasUnsupportedBackgroundColor()) {
Dan Stozac6998d22015-09-24 17:03:36 -07002167 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2168 hwc1Layer.flags = HWC_SKIP_LAYER;
2169 return;
2170 }
2171
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002172 hwc1Layer.flags = 0;
2173 switch (mCompositionType) {
2174 case Composition::Client:
2175 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2176 hwc1Layer.flags |= HWC_SKIP_LAYER;
2177 break;
2178 case Composition::Device:
2179 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2180 break;
2181 case Composition::SolidColor:
2182 // In theory the following line should work, but since the HWC1
2183 // version of SurfaceFlinger never used HWC_BACKGROUND, HWC1
2184 // devices may not work correctly. To be on the safe side, we
2185 // fall back to client composition.
2186 //
2187 // hwc1Layer.compositionType = HWC_BACKGROUND;
2188 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2189 hwc1Layer.flags |= HWC_SKIP_LAYER;
2190 break;
2191 case Composition::Cursor:
2192 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2193 if (mDisplay.getDevice().getHwc1MinorVersion() >= 4) {
2194 hwc1Layer.hints |= HWC_IS_CURSOR_LAYER;
2195 }
2196 break;
2197 case Composition::Sideband:
2198 if (mDisplay.getDevice().getHwc1MinorVersion() < 4) {
2199 hwc1Layer.compositionType = HWC_SIDEBAND;
2200 } else {
Dan Stozac6998d22015-09-24 17:03:36 -07002201 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2202 hwc1Layer.flags |= HWC_SKIP_LAYER;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002203 }
2204 break;
2205 default:
2206 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2207 hwc1Layer.flags |= HWC_SKIP_LAYER;
2208 break;
Dan Stozac6998d22015-09-24 17:03:36 -07002209 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002210 ALOGV("Layer %" PRIu64 " %s set to %d", mId,
2211 to_string(mCompositionType).c_str(),
2212 hwc1Layer.compositionType);
2213 ALOGV_IF(hwc1Layer.flags & HWC_SKIP_LAYER, " and skipping");
Dan Stozac6998d22015-09-24 17:03:36 -07002214}
2215
2216// Adapter helpers
2217
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002218void HWC2On1Adapter::populateCapabilities() {
Dan Stozac6998d22015-09-24 17:03:36 -07002219 if (mHwc1MinorVersion >= 3U) {
2220 int supportedTypes = 0;
2221 auto result = mHwc1Device->query(mHwc1Device,
2222 HWC_DISPLAY_TYPES_SUPPORTED, &supportedTypes);
Fred Fettingerc50c01e2016-06-14 17:53:10 -05002223 if ((result == 0) && ((supportedTypes & HWC_DISPLAY_VIRTUAL_BIT) != 0)) {
Dan Stozac6998d22015-09-24 17:03:36 -07002224 ALOGI("Found support for HWC virtual displays");
2225 mHwc1SupportsVirtualDisplays = true;
2226 }
2227 }
2228 if (mHwc1MinorVersion >= 4U) {
2229 mCapabilities.insert(Capability::SidebandStream);
2230 }
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002231
2232 // Check for HWC background color layer support.
2233 if (mHwc1MinorVersion >= 1U) {
2234 int backgroundColorSupported = 0;
2235 auto result = mHwc1Device->query(mHwc1Device,
2236 HWC_BACKGROUND_LAYER_SUPPORTED,
2237 &backgroundColorSupported);
2238 if ((result == 0) && (backgroundColorSupported == 1)) {
2239 ALOGV("Found support for HWC background color");
2240 mHwc1SupportsBackgroundColor = true;
2241 }
2242 }
Dan Stozac6998d22015-09-24 17:03:36 -07002243}
2244
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002245HWC2On1Adapter::Display* HWC2On1Adapter::getDisplay(hwc2_display_t id) {
Dan Stozafc4e2022016-02-23 11:43:19 -08002246 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002247
2248 auto display = mDisplays.find(id);
2249 if (display == mDisplays.end()) {
2250 return nullptr;
2251 }
2252
2253 return display->second.get();
2254}
2255
2256std::tuple<HWC2On1Adapter::Layer*, Error> HWC2On1Adapter::getLayer(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002257 hwc2_display_t displayId, hwc2_layer_t layerId) {
Dan Stozac6998d22015-09-24 17:03:36 -07002258 auto display = getDisplay(displayId);
2259 if (!display) {
2260 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadDisplay);
2261 }
2262
2263 auto layerEntry = mLayers.find(layerId);
2264 if (layerEntry == mLayers.end()) {
2265 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2266 }
2267
2268 auto layer = layerEntry->second;
2269 if (layer->getDisplay().getId() != displayId) {
2270 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2271 }
2272 return std::make_tuple(layer.get(), Error::None);
2273}
2274
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002275void HWC2On1Adapter::populatePrimary() {
Dan Stozafc4e2022016-02-23 11:43:19 -08002276 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002277
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002278 auto display = std::make_shared<Display>(*this, HWC2::DisplayType::Physical);
Dan Stozac6998d22015-09-24 17:03:36 -07002279 mHwc1DisplayMap[HWC_DISPLAY_PRIMARY] = display->getId();
2280 display->setHwc1Id(HWC_DISPLAY_PRIMARY);
2281 display->populateConfigs();
2282 mDisplays.emplace(display->getId(), std::move(display));
2283}
2284
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002285bool HWC2On1Adapter::prepareAllDisplays() {
Dan Stozac6998d22015-09-24 17:03:36 -07002286 ATRACE_CALL();
2287
Dan Stozafc4e2022016-02-23 11:43:19 -08002288 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002289
2290 for (const auto& displayPair : mDisplays) {
2291 auto& display = displayPair.second;
2292 if (!display->prepare()) {
2293 return false;
2294 }
2295 }
2296
Fabien Sanglard7382ed72017-02-11 22:47:36 -08002297 if (mHwc1DisplayMap.count(HWC_DISPLAY_PRIMARY) == 0) {
Dan Stozac6998d22015-09-24 17:03:36 -07002298 ALOGE("prepareAllDisplays: Unable to find primary HWC1 display");
2299 return false;
2300 }
2301
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002302 // Build an array of hwc_display_contents_1 to call prepare() on HWC1.
2303 mHwc1Contents.clear();
2304
Dan Stozac6998d22015-09-24 17:03:36 -07002305 // Always push the primary display
Dan Stozac6998d22015-09-24 17:03:36 -07002306 auto primaryDisplayId = mHwc1DisplayMap[HWC_DISPLAY_PRIMARY];
2307 auto& primaryDisplay = mDisplays[primaryDisplayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002308 mHwc1Contents.push_back(primaryDisplay->getDisplayContents());
Dan Stozac6998d22015-09-24 17:03:36 -07002309
2310 // Push the external display, if present
2311 if (mHwc1DisplayMap.count(HWC_DISPLAY_EXTERNAL) != 0) {
2312 auto externalDisplayId = mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL];
2313 auto& externalDisplay = mDisplays[externalDisplayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002314 mHwc1Contents.push_back(externalDisplay->getDisplayContents());
Dan Stozac6998d22015-09-24 17:03:36 -07002315 } else {
2316 // Even if an external display isn't present, we still need to send
2317 // at least two displays down to HWC1
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002318 mHwc1Contents.push_back(nullptr);
Dan Stozac6998d22015-09-24 17:03:36 -07002319 }
2320
2321 // Push the hardware virtual display, if supported and present
2322 if (mHwc1MinorVersion >= 3) {
2323 if (mHwc1DisplayMap.count(HWC_DISPLAY_VIRTUAL) != 0) {
2324 auto virtualDisplayId = mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL];
2325 auto& virtualDisplay = mDisplays[virtualDisplayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002326 mHwc1Contents.push_back(virtualDisplay->getDisplayContents());
Dan Stozac6998d22015-09-24 17:03:36 -07002327 } else {
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002328 mHwc1Contents.push_back(nullptr);
Dan Stozac6998d22015-09-24 17:03:36 -07002329 }
2330 }
2331
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002332 for (auto& displayContents : mHwc1Contents) {
Dan Stozac6998d22015-09-24 17:03:36 -07002333 if (!displayContents) {
2334 continue;
2335 }
2336
2337 ALOGV("Display %zd layers:", mHwc1Contents.size() - 1);
2338 for (size_t l = 0; l < displayContents->numHwLayers; ++l) {
2339 auto& layer = displayContents->hwLayers[l];
2340 ALOGV(" %zd: %d", l, layer.compositionType);
2341 }
2342 }
2343
2344 ALOGV("Calling HWC1 prepare");
2345 {
2346 ATRACE_NAME("HWC1 prepare");
2347 mHwc1Device->prepare(mHwc1Device, mHwc1Contents.size(),
2348 mHwc1Contents.data());
2349 }
2350
2351 for (size_t c = 0; c < mHwc1Contents.size(); ++c) {
2352 auto& contents = mHwc1Contents[c];
2353 if (!contents) {
2354 continue;
2355 }
2356 ALOGV("Display %zd layers:", c);
2357 for (size_t l = 0; l < contents->numHwLayers; ++l) {
2358 ALOGV(" %zd: %d", l, contents->hwLayers[l].compositionType);
2359 }
2360 }
2361
2362 // Return the received contents to their respective displays
2363 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2364 if (mHwc1Contents[hwc1Id] == nullptr) {
2365 continue;
2366 }
2367
2368 auto displayId = mHwc1DisplayMap[hwc1Id];
2369 auto& display = mDisplays[displayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002370 display->generateChanges();
Dan Stozac6998d22015-09-24 17:03:36 -07002371 }
2372
2373 return true;
2374}
2375
Fabien Sanglardaf5b6b82017-02-23 11:17:11 -08002376void dumpHWC1Message(hwc_composer_device_1* device, size_t numDisplays,
2377 hwc_display_contents_1_t** displays) {
2378 ALOGV("*****************************");
2379 size_t displayId = 0;
2380 while (displayId < numDisplays) {
2381 hwc_display_contents_1_t* display = displays[displayId];
2382
2383 ALOGV("hwc_display_contents_1_t[%zu] @0x%p", displayId, display);
2384 if (display == nullptr) {
2385 displayId++;
2386 continue;
2387 }
2388 ALOGV(" retirefd:0x%08x", display->retireFenceFd);
2389 ALOGV(" outbuf :0x%p", display->outbuf);
2390 ALOGV(" outbuffd:0x%08x", display->outbufAcquireFenceFd);
2391 ALOGV(" flags :0x%08x", display->flags);
2392 for(size_t layerId=0 ; layerId < display->numHwLayers ; layerId++) {
2393 hwc_layer_1_t& layer = display->hwLayers[layerId];
2394 ALOGV(" Layer[%zu]:", layerId);
2395 ALOGV(" composition : 0x%08x", layer.compositionType);
2396 ALOGV(" hints : 0x%08x", layer.hints);
2397 ALOGV(" flags : 0x%08x", layer.flags);
2398 ALOGV(" handle : 0x%p", layer.handle);
2399 ALOGV(" transform : 0x%08x", layer.transform);
2400 ALOGV(" blending : 0x%08x", layer.blending);
2401 ALOGV(" sourceCropf : %f, %f, %f, %f",
2402 layer.sourceCropf.left,
2403 layer.sourceCropf.top,
2404 layer.sourceCropf.right,
2405 layer.sourceCropf.bottom);
2406 ALOGV(" displayFrame : %d, %d, %d, %d",
2407 layer.displayFrame.left,
2408 layer.displayFrame.left,
2409 layer.displayFrame.left,
2410 layer.displayFrame.left);
2411 hwc_region_t& visReg = layer.visibleRegionScreen;
2412 ALOGV(" visibleRegionScreen: #0x%08zx[@0x%p]",
2413 visReg.numRects,
2414 visReg.rects);
2415 for (size_t visRegId=0; visRegId < visReg.numRects ; visRegId++) {
2416 if (layer.visibleRegionScreen.rects == nullptr) {
2417 ALOGV(" null");
2418 } else {
2419 ALOGV(" visibleRegionScreen[%zu] %d, %d, %d, %d",
2420 visRegId,
2421 visReg.rects[visRegId].left,
2422 visReg.rects[visRegId].top,
2423 visReg.rects[visRegId].right,
2424 visReg.rects[visRegId].bottom);
2425 }
2426 }
2427 ALOGV(" acquireFenceFd : 0x%08x", layer.acquireFenceFd);
2428 ALOGV(" releaseFenceFd : 0x%08x", layer.releaseFenceFd);
2429 ALOGV(" planeAlpha : 0x%08x", layer.planeAlpha);
2430 if (getMinorVersion(device) < 5)
2431 continue;
2432 ALOGV(" surfaceDamage : #0x%08zx[@0x%p]",
2433 layer.surfaceDamage.numRects,
2434 layer.surfaceDamage.rects);
2435 for (size_t sdId=0; sdId < layer.surfaceDamage.numRects ; sdId++) {
2436 if (layer.surfaceDamage.rects == nullptr) {
2437 ALOGV(" null");
2438 } else {
2439 ALOGV(" surfaceDamage[%zu] %d, %d, %d, %d",
2440 sdId,
2441 layer.surfaceDamage.rects[sdId].left,
2442 layer.surfaceDamage.rects[sdId].top,
2443 layer.surfaceDamage.rects[sdId].right,
2444 layer.surfaceDamage.rects[sdId].bottom);
2445 }
2446 }
2447 }
2448 displayId++;
2449 }
2450 ALOGV("-----------------------------");
2451}
2452
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002453Error HWC2On1Adapter::setAllDisplays() {
Dan Stozac6998d22015-09-24 17:03:36 -07002454 ATRACE_CALL();
2455
Dan Stozafc4e2022016-02-23 11:43:19 -08002456 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002457
2458 // Make sure we're ready to validate
2459 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2460 if (mHwc1Contents[hwc1Id] == nullptr) {
2461 continue;
2462 }
2463
2464 auto displayId = mHwc1DisplayMap[hwc1Id];
2465 auto& display = mDisplays[displayId];
2466 Error error = display->set(*mHwc1Contents[hwc1Id]);
2467 if (error != Error::None) {
2468 ALOGE("setAllDisplays: Failed to set display %zd: %s", hwc1Id,
2469 to_string(error).c_str());
2470 return error;
2471 }
2472 }
2473
2474 ALOGV("Calling HWC1 set");
2475 {
2476 ATRACE_NAME("HWC1 set");
Fabien Sanglardaf5b6b82017-02-23 11:17:11 -08002477 //dumpHWC1Message(mHwc1Device, mHwc1Contents.size(), mHwc1Contents.data());
Dan Stozac6998d22015-09-24 17:03:36 -07002478 mHwc1Device->set(mHwc1Device, mHwc1Contents.size(),
2479 mHwc1Contents.data());
2480 }
2481
2482 // Add retire and release fences
2483 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2484 if (mHwc1Contents[hwc1Id] == nullptr) {
2485 continue;
2486 }
2487
2488 auto displayId = mHwc1DisplayMap[hwc1Id];
2489 auto& display = mDisplays[displayId];
2490 auto retireFenceFd = mHwc1Contents[hwc1Id]->retireFenceFd;
2491 ALOGV("setAllDisplays: Adding retire fence %d to display %zd",
2492 retireFenceFd, hwc1Id);
2493 display->addRetireFence(mHwc1Contents[hwc1Id]->retireFenceFd);
2494 display->addReleaseFences(*mHwc1Contents[hwc1Id]);
2495 }
2496
2497 return Error::None;
2498}
2499
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002500void HWC2On1Adapter::hwc1Invalidate() {
Dan Stozac6998d22015-09-24 17:03:36 -07002501 ALOGV("Received hwc1Invalidate");
2502
Dan Stozafc4e2022016-02-23 11:43:19 -08002503 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002504
2505 // If the HWC2-side callback hasn't been registered yet, buffer this until
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002506 // it is registered.
Dan Stozac6998d22015-09-24 17:03:36 -07002507 if (mCallbacks.count(Callback::Refresh) == 0) {
2508 mHasPendingInvalidate = true;
2509 return;
2510 }
2511
2512 const auto& callbackInfo = mCallbacks[Callback::Refresh];
2513 std::vector<hwc2_display_t> displays;
2514 for (const auto& displayPair : mDisplays) {
2515 displays.emplace_back(displayPair.first);
2516 }
2517
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002518 // Call back without the state lock held.
Dan Stozac6998d22015-09-24 17:03:36 -07002519 lock.unlock();
2520
2521 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(callbackInfo.pointer);
2522 for (auto display : displays) {
2523 refresh(callbackInfo.data, display);
2524 }
2525}
2526
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002527void HWC2On1Adapter::hwc1Vsync(int hwc1DisplayId, int64_t timestamp) {
Dan Stozac6998d22015-09-24 17:03:36 -07002528 ALOGV("Received hwc1Vsync(%d, %" PRId64 ")", hwc1DisplayId, timestamp);
2529
Dan Stozafc4e2022016-02-23 11:43:19 -08002530 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002531
2532 // If the HWC2-side callback hasn't been registered yet, buffer this until
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002533 // it is registered.
Dan Stozac6998d22015-09-24 17:03:36 -07002534 if (mCallbacks.count(Callback::Vsync) == 0) {
2535 mPendingVsyncs.emplace_back(hwc1DisplayId, timestamp);
2536 return;
2537 }
2538
2539 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2540 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", hwc1DisplayId);
2541 return;
2542 }
2543
2544 const auto& callbackInfo = mCallbacks[Callback::Vsync];
2545 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
2546
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002547 // Call back without the state lock held.
Dan Stozac6998d22015-09-24 17:03:36 -07002548 lock.unlock();
2549
2550 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(callbackInfo.pointer);
2551 vsync(callbackInfo.data, displayId, timestamp);
2552}
2553
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002554void HWC2On1Adapter::hwc1Hotplug(int hwc1DisplayId, int connected) {
Dan Stozac6998d22015-09-24 17:03:36 -07002555 ALOGV("Received hwc1Hotplug(%d, %d)", hwc1DisplayId, connected);
2556
2557 if (hwc1DisplayId != HWC_DISPLAY_EXTERNAL) {
2558 ALOGE("hwc1Hotplug: Received hotplug for non-external display");
2559 return;
2560 }
2561
Dan Stozafc4e2022016-02-23 11:43:19 -08002562 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002563
2564 // If the HWC2-side callback hasn't been registered yet, buffer this until
2565 // it is registered
2566 if (mCallbacks.count(Callback::Hotplug) == 0) {
2567 mPendingHotplugs.emplace_back(hwc1DisplayId, connected);
2568 return;
2569 }
2570
2571 hwc2_display_t displayId = UINT64_MAX;
2572 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2573 if (connected == 0) {
2574 ALOGW("hwc1Hotplug: Received disconnect for unconnected display");
2575 return;
2576 }
2577
2578 // Create a new display on connect
2579 auto display = std::make_shared<HWC2On1Adapter::Display>(*this,
2580 HWC2::DisplayType::Physical);
2581 display->setHwc1Id(HWC_DISPLAY_EXTERNAL);
2582 display->populateConfigs();
2583 displayId = display->getId();
2584 mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL] = displayId;
2585 mDisplays.emplace(displayId, std::move(display));
2586 } else {
2587 if (connected != 0) {
2588 ALOGW("hwc1Hotplug: Received connect for previously connected "
2589 "display");
2590 return;
2591 }
2592
2593 // Disconnect an existing display
2594 displayId = mHwc1DisplayMap[hwc1DisplayId];
2595 mHwc1DisplayMap.erase(HWC_DISPLAY_EXTERNAL);
2596 mDisplays.erase(displayId);
2597 }
2598
2599 const auto& callbackInfo = mCallbacks[Callback::Hotplug];
2600
2601 // Call back without the state lock held
2602 lock.unlock();
2603
2604 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(callbackInfo.pointer);
2605 auto hwc2Connected = (connected == 0) ?
2606 HWC2::Connection::Disconnected : HWC2::Connection::Connected;
2607 hotplug(callbackInfo.data, displayId, static_cast<int32_t>(hwc2Connected));
2608}
Dan Stozac6998d22015-09-24 17:03:36 -07002609} // namespace android