blob: 5ad05c7542e36b957256d3449e989c55d183a7d5 [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*>);
248
249 // Layer functions
250 case FunctionDescriptor::SetCursorPosition:
251 return asFP<HWC2_PFN_SET_CURSOR_POSITION>(
252 layerHook<decltype(&Layer::setCursorPosition),
253 &Layer::setCursorPosition, int32_t, int32_t>);
254 case FunctionDescriptor::SetLayerBuffer:
255 return asFP<HWC2_PFN_SET_LAYER_BUFFER>(
256 layerHook<decltype(&Layer::setBuffer), &Layer::setBuffer,
257 buffer_handle_t, int32_t>);
258 case FunctionDescriptor::SetLayerSurfaceDamage:
259 return asFP<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
260 layerHook<decltype(&Layer::setSurfaceDamage),
261 &Layer::setSurfaceDamage, hwc_region_t>);
262
263 // Layer state functions
264 case FunctionDescriptor::SetLayerBlendMode:
265 return asFP<HWC2_PFN_SET_LAYER_BLEND_MODE>(
266 setLayerBlendModeHook);
267 case FunctionDescriptor::SetLayerColor:
268 return asFP<HWC2_PFN_SET_LAYER_COLOR>(
269 layerHook<decltype(&Layer::setColor), &Layer::setColor,
270 hwc_color_t>);
271 case FunctionDescriptor::SetLayerCompositionType:
272 return asFP<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
273 setLayerCompositionTypeHook);
Dan Stoza5df2a862016-03-24 16:19:37 -0700274 case FunctionDescriptor::SetLayerDataspace:
275 return asFP<HWC2_PFN_SET_LAYER_DATASPACE>(setLayerDataspaceHook);
Dan Stozac6998d22015-09-24 17:03:36 -0700276 case FunctionDescriptor::SetLayerDisplayFrame:
277 return asFP<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
278 layerHook<decltype(&Layer::setDisplayFrame),
279 &Layer::setDisplayFrame, hwc_rect_t>);
280 case FunctionDescriptor::SetLayerPlaneAlpha:
281 return asFP<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
282 layerHook<decltype(&Layer::setPlaneAlpha),
283 &Layer::setPlaneAlpha, float>);
284 case FunctionDescriptor::SetLayerSidebandStream:
285 return asFP<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
286 layerHook<decltype(&Layer::setSidebandStream),
287 &Layer::setSidebandStream, const native_handle_t*>);
288 case FunctionDescriptor::SetLayerSourceCrop:
289 return asFP<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
290 layerHook<decltype(&Layer::setSourceCrop),
291 &Layer::setSourceCrop, hwc_frect_t>);
292 case FunctionDescriptor::SetLayerTransform:
293 return asFP<HWC2_PFN_SET_LAYER_TRANSFORM>(setLayerTransformHook);
294 case FunctionDescriptor::SetLayerVisibleRegion:
295 return asFP<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
296 layerHook<decltype(&Layer::setVisibleRegion),
297 &Layer::setVisibleRegion, hwc_region_t>);
298 case FunctionDescriptor::SetLayerZOrder:
299 return asFP<HWC2_PFN_SET_LAYER_Z_ORDER>(setLayerZOrderHook);
300
301 default:
302 ALOGE("doGetFunction: Unknown function descriptor: %d (%s)",
303 static_cast<int32_t>(descriptor),
304 to_string(descriptor).c_str());
305 return nullptr;
306 }
307}
308
309// Device functions
310
311Error HWC2On1Adapter::createVirtualDisplay(uint32_t width,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800312 uint32_t height, hwc2_display_t* outDisplay) {
Dan Stozafc4e2022016-02-23 11:43:19 -0800313 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700314
315 if (mHwc1VirtualDisplay) {
316 // We have already allocated our only HWC1 virtual display
317 ALOGE("createVirtualDisplay: HWC1 virtual display already allocated");
318 return Error::NoResources;
319 }
320
Dan Stozac6998d22015-09-24 17:03:36 -0700321 mHwc1VirtualDisplay = std::make_shared<HWC2On1Adapter::Display>(*this,
322 HWC2::DisplayType::Virtual);
323 mHwc1VirtualDisplay->populateConfigs(width, height);
324 const auto displayId = mHwc1VirtualDisplay->getId();
325 mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL] = displayId;
326 mHwc1VirtualDisplay->setHwc1Id(HWC_DISPLAY_VIRTUAL);
327 mDisplays.emplace(displayId, mHwc1VirtualDisplay);
328 *outDisplay = displayId;
329
330 return Error::None;
331}
332
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800333Error HWC2On1Adapter::destroyVirtualDisplay(hwc2_display_t displayId) {
Dan Stozafc4e2022016-02-23 11:43:19 -0800334 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700335
336 if (!mHwc1VirtualDisplay || (mHwc1VirtualDisplay->getId() != displayId)) {
337 return Error::BadDisplay;
338 }
339
340 mHwc1VirtualDisplay.reset();
341 mHwc1DisplayMap.erase(HWC_DISPLAY_VIRTUAL);
342 mDisplays.erase(displayId);
343
344 return Error::None;
345}
346
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800347void HWC2On1Adapter::dump(uint32_t* outSize, char* outBuffer) {
Dan Stozac6998d22015-09-24 17:03:36 -0700348 if (outBuffer != nullptr) {
349 auto copiedBytes = mDumpString.copy(outBuffer, *outSize);
350 *outSize = static_cast<uint32_t>(copiedBytes);
351 return;
352 }
353
354 std::stringstream output;
355
356 output << "-- HWC2On1Adapter --\n";
357
358 output << "Adapting to a HWC 1." << static_cast<int>(mHwc1MinorVersion) <<
359 " device\n";
360
361 // Attempt to acquire the lock for 1 second, but proceed without the lock
362 // after that, so we can still get some information if we're deadlocked
Dan Stozafc4e2022016-02-23 11:43:19 -0800363 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex,
364 std::defer_lock);
Dan Stozac6998d22015-09-24 17:03:36 -0700365 lock.try_lock_for(1s);
366
367 if (mCapabilities.empty()) {
368 output << "Capabilities: None\n";
369 } else {
370 output << "Capabilities:\n";
371 for (auto capability : mCapabilities) {
372 output << " " << to_string(capability) << '\n';
373 }
374 }
375
376 output << "Displays:\n";
377 for (const auto& element : mDisplays) {
378 const auto& display = element.second;
379 output << display->dump();
380 }
381 output << '\n';
382
Dan Stozafc4e2022016-02-23 11:43:19 -0800383 // Release the lock before calling into HWC1, and since we no longer require
384 // mutual exclusion to access mCapabilities or mDisplays
385 lock.unlock();
386
Dan Stozac6998d22015-09-24 17:03:36 -0700387 if (mHwc1Device->dump) {
388 output << "HWC1 dump:\n";
389 std::vector<char> hwc1Dump(4096);
390 // Call with size - 1 to preserve a null character at the end
391 mHwc1Device->dump(mHwc1Device, hwc1Dump.data(),
392 static_cast<int>(hwc1Dump.size() - 1));
393 output << hwc1Dump.data();
394 }
395
396 mDumpString = output.str();
397 *outSize = static_cast<uint32_t>(mDumpString.size());
398}
399
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800400uint32_t HWC2On1Adapter::getMaxVirtualDisplayCount() {
Dan Stozac6998d22015-09-24 17:03:36 -0700401 return mHwc1SupportsVirtualDisplays ? 1 : 0;
402}
403
404static bool isValid(Callback descriptor) {
405 switch (descriptor) {
406 case Callback::Hotplug: // Fall-through
407 case Callback::Refresh: // Fall-through
408 case Callback::Vsync: return true;
409 default: return false;
410 }
411}
412
413Error HWC2On1Adapter::registerCallback(Callback descriptor,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800414 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer) {
Dan Stozac6998d22015-09-24 17:03:36 -0700415 if (!isValid(descriptor)) {
416 return Error::BadParameter;
417 }
418
419 ALOGV("registerCallback(%s, %p, %p)", to_string(descriptor).c_str(),
420 callbackData, pointer);
421
Dan Stozafc4e2022016-02-23 11:43:19 -0800422 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -0700423
424 mCallbacks[descriptor] = {callbackData, pointer};
425
426 bool hasPendingInvalidate = false;
427 std::vector<hwc2_display_t> displayIds;
428 std::vector<std::pair<hwc2_display_t, int64_t>> pendingVsyncs;
429 std::vector<std::pair<hwc2_display_t, int>> pendingHotplugs;
430
431 if (descriptor == Callback::Refresh) {
432 hasPendingInvalidate = mHasPendingInvalidate;
433 if (hasPendingInvalidate) {
434 for (auto& displayPair : mDisplays) {
435 displayIds.emplace_back(displayPair.first);
436 }
437 }
438 mHasPendingInvalidate = false;
439 } else if (descriptor == Callback::Vsync) {
440 for (auto pending : mPendingVsyncs) {
441 auto hwc1DisplayId = pending.first;
442 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
443 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d",
444 hwc1DisplayId);
445 continue;
446 }
447 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
448 auto timestamp = pending.second;
449 pendingVsyncs.emplace_back(displayId, timestamp);
450 }
451 mPendingVsyncs.clear();
452 } else if (descriptor == Callback::Hotplug) {
453 // Hotplug the primary display
454 pendingHotplugs.emplace_back(mHwc1DisplayMap[HWC_DISPLAY_PRIMARY],
455 static_cast<int32_t>(Connection::Connected));
456
457 for (auto pending : mPendingHotplugs) {
458 auto hwc1DisplayId = pending.first;
459 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
460 ALOGE("hwc1Hotplug: Couldn't find display for HWC1 id %d",
461 hwc1DisplayId);
462 continue;
463 }
464 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
465 auto connected = pending.second;
466 pendingHotplugs.emplace_back(displayId, connected);
467 }
468 }
469
470 // Call pending callbacks without the state lock held
471 lock.unlock();
472
473 if (hasPendingInvalidate) {
474 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(pointer);
475 for (auto displayId : displayIds) {
476 refresh(callbackData, displayId);
477 }
478 }
479 if (!pendingVsyncs.empty()) {
480 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(pointer);
481 for (auto& pendingVsync : pendingVsyncs) {
482 vsync(callbackData, pendingVsync.first, pendingVsync.second);
483 }
484 }
485 if (!pendingHotplugs.empty()) {
486 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(pointer);
487 for (auto& pendingHotplug : pendingHotplugs) {
488 hotplug(callbackData, pendingHotplug.first, pendingHotplug.second);
489 }
490 }
491 return Error::None;
492}
493
494// Display functions
495
496std::atomic<hwc2_display_t> HWC2On1Adapter::Display::sNextId(1);
497
498HWC2On1Adapter::Display::Display(HWC2On1Adapter& device, HWC2::DisplayType type)
499 : mId(sNextId++),
500 mDevice(device),
Dan Stozac6998d22015-09-24 17:03:36 -0700501 mStateMutex(),
Dan Stozac6998d22015-09-24 17:03:36 -0700502 mHwc1RequestedContents(nullptr),
Dan Stozac6998d22015-09-24 17:03:36 -0700503 mRetireFence(),
504 mChanges(),
505 mHwc1Id(-1),
506 mConfigs(),
507 mActiveConfig(nullptr),
Michael Wrightc75ca512016-07-20 21:34:48 +0100508 mActiveColorMode(static_cast<android_color_mode_t>(-1)),
Dan Stozac6998d22015-09-24 17:03:36 -0700509 mName(),
510 mType(type),
511 mPowerMode(PowerMode::Off),
512 mVsyncEnabled(Vsync::Invalid),
513 mClientTarget(),
514 mOutputBuffer(),
Dan Stoza5df2a862016-03-24 16:19:37 -0700515 mHasColorTransform(false),
Dan Stozafc4e2022016-02-23 11:43:19 -0800516 mLayers(),
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800517 mHwc1LayerMap(),
518 mNumAvailableRects(0),
519 mNextAvailableRect(nullptr),
520 mGeometryChanged(false)
521 {}
Dan Stozac6998d22015-09-24 17:03:36 -0700522
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800523Error HWC2On1Adapter::Display::acceptChanges() {
Dan Stozac6998d22015-09-24 17:03:36 -0700524 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
525
526 if (!mChanges) {
527 ALOGV("[%" PRIu64 "] acceptChanges failed, not validated", mId);
528 return Error::NotValidated;
529 }
530
531 ALOGV("[%" PRIu64 "] acceptChanges", mId);
532
533 for (auto& change : mChanges->getTypeChanges()) {
534 auto layerId = change.first;
535 auto type = change.second;
536 auto layer = mDevice.mLayers[layerId];
537 layer->setCompositionType(type);
538 }
539
540 mChanges->clearTypeChanges();
541
Dan Stozac6998d22015-09-24 17:03:36 -0700542 return Error::None;
543}
544
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800545Error HWC2On1Adapter::Display::createLayer(hwc2_layer_t* outLayerId) {
Dan Stozac6998d22015-09-24 17:03:36 -0700546 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
547
548 auto layer = *mLayers.emplace(std::make_shared<Layer>(*this));
549 mDevice.mLayers.emplace(std::make_pair(layer->getId(), layer));
550 *outLayerId = layer->getId();
551 ALOGV("[%" PRIu64 "] created layer %" PRIu64, mId, *outLayerId);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800552 markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -0700553 return Error::None;
554}
555
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800556Error HWC2On1Adapter::Display::destroyLayer(hwc2_layer_t layerId) {
Dan Stozac6998d22015-09-24 17:03:36 -0700557 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
558
559 const auto mapLayer = mDevice.mLayers.find(layerId);
560 if (mapLayer == mDevice.mLayers.end()) {
561 ALOGV("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer",
562 mId, layerId);
563 return Error::BadLayer;
564 }
565 const auto layer = mapLayer->second;
566 mDevice.mLayers.erase(mapLayer);
567 const auto zRange = mLayers.equal_range(layer);
568 for (auto current = zRange.first; current != zRange.second; ++current) {
569 if (**current == *layer) {
570 current = mLayers.erase(current);
571 break;
572 }
573 }
574 ALOGV("[%" PRIu64 "] destroyed layer %" PRIu64, mId, layerId);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800575 markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -0700576 return Error::None;
577}
578
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800579Error HWC2On1Adapter::Display::getActiveConfig(hwc2_config_t* outConfig) {
Dan Stozac6998d22015-09-24 17:03:36 -0700580 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
581
582 if (!mActiveConfig) {
583 ALOGV("[%" PRIu64 "] getActiveConfig --> %s", mId,
584 to_string(Error::BadConfig).c_str());
585 return Error::BadConfig;
586 }
587 auto configId = mActiveConfig->getId();
588 ALOGV("[%" PRIu64 "] getActiveConfig --> %u", mId, configId);
589 *outConfig = configId;
590 return Error::None;
591}
592
593Error HWC2On1Adapter::Display::getAttribute(hwc2_config_t configId,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800594 Attribute attribute, int32_t* outValue) {
Dan Stozac6998d22015-09-24 17:03:36 -0700595 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
596
597 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
598 ALOGV("[%" PRIu64 "] getAttribute failed: bad config (%u)", mId,
599 configId);
600 return Error::BadConfig;
601 }
602 *outValue = mConfigs[configId]->getAttribute(attribute);
603 ALOGV("[%" PRIu64 "] getAttribute(%u, %s) --> %d", mId, configId,
604 to_string(attribute).c_str(), *outValue);
605 return Error::None;
606}
607
608Error HWC2On1Adapter::Display::getChangedCompositionTypes(
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800609 uint32_t* outNumElements, hwc2_layer_t* outLayers, int32_t* outTypes) {
Dan Stozac6998d22015-09-24 17:03:36 -0700610 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
611
612 if (!mChanges) {
613 ALOGE("[%" PRIu64 "] getChangedCompositionTypes failed: not validated",
614 mId);
615 return Error::NotValidated;
616 }
617
618 if ((outLayers == nullptr) || (outTypes == nullptr)) {
619 *outNumElements = mChanges->getTypeChanges().size();
620 return Error::None;
621 }
622
623 uint32_t numWritten = 0;
624 for (const auto& element : mChanges->getTypeChanges()) {
625 if (numWritten == *outNumElements) {
626 break;
627 }
628 auto layerId = element.first;
629 auto intType = static_cast<int32_t>(element.second);
630 ALOGV("Adding %" PRIu64 " %s", layerId,
631 to_string(element.second).c_str());
632 outLayers[numWritten] = layerId;
633 outTypes[numWritten] = intType;
634 ++numWritten;
635 }
636 *outNumElements = numWritten;
637
638 return Error::None;
639}
640
Dan Stoza076ac672016-03-14 10:47:53 -0700641Error HWC2On1Adapter::Display::getColorModes(uint32_t* outNumModes,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800642 int32_t* outModes) {
Dan Stoza076ac672016-03-14 10:47:53 -0700643 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
644
645 if (!outModes) {
646 *outNumModes = mColorModes.size();
647 return Error::None;
648 }
649 uint32_t numModes = std::min(*outNumModes,
650 static_cast<uint32_t>(mColorModes.size()));
651 std::copy_n(mColorModes.cbegin(), numModes, outModes);
652 *outNumModes = numModes;
653 return Error::None;
654}
655
Dan Stozac6998d22015-09-24 17:03:36 -0700656Error HWC2On1Adapter::Display::getConfigs(uint32_t* outNumConfigs,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800657 hwc2_config_t* outConfigs) {
Dan Stozac6998d22015-09-24 17:03:36 -0700658 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
659
660 if (!outConfigs) {
661 *outNumConfigs = mConfigs.size();
662 return Error::None;
663 }
664 uint32_t numWritten = 0;
665 for (const auto& config : mConfigs) {
666 if (numWritten == *outNumConfigs) {
667 break;
668 }
669 outConfigs[numWritten] = config->getId();
670 ++numWritten;
671 }
672 *outNumConfigs = numWritten;
673 return Error::None;
674}
675
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800676Error HWC2On1Adapter::Display::getDozeSupport(int32_t* outSupport) {
Dan Stozac6998d22015-09-24 17:03:36 -0700677 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
678
679 if (mDevice.mHwc1MinorVersion < 4 || mHwc1Id != 0) {
680 *outSupport = 0;
681 } else {
682 *outSupport = 1;
683 }
684 return Error::None;
685}
686
Dan Stozaed40eba2016-03-16 12:33:52 -0700687Error HWC2On1Adapter::Display::getHdrCapabilities(uint32_t* outNumTypes,
688 int32_t* /*outTypes*/, float* /*outMaxLuminance*/,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800689 float* /*outMaxAverageLuminance*/, float* /*outMinLuminance*/) {
Dan Stozaed40eba2016-03-16 12:33:52 -0700690 // This isn't supported on HWC1, so per the HWC2 header, return numTypes = 0
691 *outNumTypes = 0;
692 return Error::None;
693}
694
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800695Error HWC2On1Adapter::Display::getName(uint32_t* outSize, char* outName) {
Dan Stozac6998d22015-09-24 17:03:36 -0700696 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
697
698 if (!outName) {
699 *outSize = mName.size();
700 return Error::None;
701 }
702 auto numCopied = mName.copy(outName, *outSize);
703 *outSize = numCopied;
704 return Error::None;
705}
706
707Error HWC2On1Adapter::Display::getReleaseFences(uint32_t* outNumElements,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800708 hwc2_layer_t* outLayers, int32_t* outFences) {
Dan Stozac6998d22015-09-24 17:03:36 -0700709 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
710
711 uint32_t numWritten = 0;
712 bool outputsNonNull = (outLayers != nullptr) && (outFences != nullptr);
713 for (const auto& layer : mLayers) {
714 if (outputsNonNull && (numWritten == *outNumElements)) {
715 break;
716 }
717
718 auto releaseFence = layer->getReleaseFence();
Fabien Sanglardc8591472017-03-07 10:10:03 -0800719 if (releaseFence != MiniFence::NO_FENCE) {
Dan Stozac6998d22015-09-24 17:03:36 -0700720 if (outputsNonNull) {
721 outLayers[numWritten] = layer->getId();
722 outFences[numWritten] = releaseFence->dup();
723 }
724 ++numWritten;
725 }
726 }
727 *outNumElements = numWritten;
728
729 return Error::None;
730}
731
732Error HWC2On1Adapter::Display::getRequests(int32_t* outDisplayRequests,
733 uint32_t* outNumElements, hwc2_layer_t* outLayers,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800734 int32_t* outLayerRequests) {
Dan Stozac6998d22015-09-24 17:03:36 -0700735 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
736
737 if (!mChanges) {
738 return Error::NotValidated;
739 }
740
741 if (outLayers == nullptr || outLayerRequests == nullptr) {
742 *outNumElements = mChanges->getNumLayerRequests();
743 return Error::None;
744 }
745
Fabien Sanglard601938c2016-11-29 11:10:40 -0800746 // Display requests (HWC2::DisplayRequest) are not supported by hwc1:
747 // A hwc1 has always zero requests for the client.
748 *outDisplayRequests = 0;
749
Dan Stozac6998d22015-09-24 17:03:36 -0700750 uint32_t numWritten = 0;
751 for (const auto& request : mChanges->getLayerRequests()) {
752 if (numWritten == *outNumElements) {
753 break;
754 }
755 outLayers[numWritten] = request.first;
756 outLayerRequests[numWritten] = static_cast<int32_t>(request.second);
757 ++numWritten;
758 }
759
760 return Error::None;
761}
762
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800763Error HWC2On1Adapter::Display::getType(int32_t* outType) {
Dan Stozac6998d22015-09-24 17:03:36 -0700764 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
765
766 *outType = static_cast<int32_t>(mType);
767 return Error::None;
768}
769
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800770Error HWC2On1Adapter::Display::present(int32_t* outRetireFence) {
Dan Stozac6998d22015-09-24 17:03:36 -0700771 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
772
773 if (mChanges) {
774 Error error = mDevice.setAllDisplays();
775 if (error != Error::None) {
776 ALOGE("[%" PRIu64 "] present: setAllDisplaysFailed (%s)", mId,
777 to_string(error).c_str());
778 return error;
779 }
780 }
781
782 *outRetireFence = mRetireFence.get()->dup();
783 ALOGV("[%" PRIu64 "] present returning retire fence %d", mId,
784 *outRetireFence);
785
786 return Error::None;
787}
788
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800789Error HWC2On1Adapter::Display::setActiveConfig(hwc2_config_t configId) {
Dan Stozac6998d22015-09-24 17:03:36 -0700790 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
791
792 auto config = getConfig(configId);
793 if (!config) {
794 return Error::BadConfig;
795 }
Dan Stoza076ac672016-03-14 10:47:53 -0700796 if (config == mActiveConfig) {
797 return Error::None;
Dan Stozac6998d22015-09-24 17:03:36 -0700798 }
Dan Stoza076ac672016-03-14 10:47:53 -0700799
800 if (mDevice.mHwc1MinorVersion >= 4) {
801 uint32_t hwc1Id = 0;
802 auto error = config->getHwc1IdForColorMode(mActiveColorMode, &hwc1Id);
803 if (error != Error::None) {
804 return error;
805 }
806
807 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
808 mHwc1Id, static_cast<int>(hwc1Id));
809 if (intError != 0) {
810 ALOGE("setActiveConfig: Failed to set active config on HWC1 (%d)",
811 intError);
812 return Error::BadConfig;
813 }
814 mActiveConfig = config;
815 }
816
Dan Stozac6998d22015-09-24 17:03:36 -0700817 return Error::None;
818}
819
820Error HWC2On1Adapter::Display::setClientTarget(buffer_handle_t target,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800821 int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/) {
Dan Stozac6998d22015-09-24 17:03:36 -0700822 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
823
824 ALOGV("[%" PRIu64 "] setClientTarget(%p, %d)", mId, target, acquireFence);
825 mClientTarget.setBuffer(target);
826 mClientTarget.setFence(acquireFence);
Dan Stoza5cf424b2016-05-20 14:02:39 -0700827 // dataspace and damage can't be used by HWC1, so ignore them
Dan Stozac6998d22015-09-24 17:03:36 -0700828 return Error::None;
829}
830
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800831Error HWC2On1Adapter::Display::setColorMode(android_color_mode_t mode) {
Dan Stoza076ac672016-03-14 10:47:53 -0700832 std::unique_lock<std::recursive_mutex> lock (mStateMutex);
833
834 ALOGV("[%" PRIu64 "] setColorMode(%d)", mId, mode);
835
836 if (mode == mActiveColorMode) {
837 return Error::None;
838 }
839 if (mColorModes.count(mode) == 0) {
840 ALOGE("[%" PRIu64 "] Mode %d not found in mColorModes", mId, mode);
841 return Error::Unsupported;
842 }
843
844 uint32_t hwc1Config = 0;
845 auto error = mActiveConfig->getHwc1IdForColorMode(mode, &hwc1Config);
846 if (error != Error::None) {
847 return error;
848 }
849
850 ALOGV("[%" PRIu64 "] Setting HWC1 config %u", mId, hwc1Config);
851 int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device,
852 mHwc1Id, hwc1Config);
853 if (intError != 0) {
854 ALOGE("[%" PRIu64 "] Failed to set HWC1 config (%d)", mId, intError);
855 return Error::Unsupported;
856 }
857
858 mActiveColorMode = mode;
859 return Error::None;
860}
861
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800862Error HWC2On1Adapter::Display::setColorTransform(android_color_transform_t hint) {
Dan Stoza5df2a862016-03-24 16:19:37 -0700863 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
864
865 ALOGV("%" PRIu64 "] setColorTransform(%d)", mId,
866 static_cast<int32_t>(hint));
867 mHasColorTransform = (hint != HAL_COLOR_TRANSFORM_IDENTITY);
868 return Error::None;
869}
870
Dan Stozac6998d22015-09-24 17:03:36 -0700871Error HWC2On1Adapter::Display::setOutputBuffer(buffer_handle_t buffer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800872 int32_t releaseFence) {
Dan Stozac6998d22015-09-24 17:03:36 -0700873 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
874
875 ALOGV("[%" PRIu64 "] setOutputBuffer(%p, %d)", mId, buffer, releaseFence);
876 mOutputBuffer.setBuffer(buffer);
877 mOutputBuffer.setFence(releaseFence);
878 return Error::None;
879}
880
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800881static bool isValid(PowerMode mode) {
Dan Stozac6998d22015-09-24 17:03:36 -0700882 switch (mode) {
883 case PowerMode::Off: // Fall-through
884 case PowerMode::DozeSuspend: // Fall-through
885 case PowerMode::Doze: // Fall-through
886 case PowerMode::On: return true;
Dan Stozac6998d22015-09-24 17:03:36 -0700887 }
888}
889
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800890static int getHwc1PowerMode(PowerMode mode) {
Dan Stozac6998d22015-09-24 17:03:36 -0700891 switch (mode) {
892 case PowerMode::Off: return HWC_POWER_MODE_OFF;
893 case PowerMode::DozeSuspend: return HWC_POWER_MODE_DOZE_SUSPEND;
894 case PowerMode::Doze: return HWC_POWER_MODE_DOZE;
895 case PowerMode::On: return HWC_POWER_MODE_NORMAL;
Dan Stozac6998d22015-09-24 17:03:36 -0700896 }
897}
898
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800899Error HWC2On1Adapter::Display::setPowerMode(PowerMode mode) {
Dan Stozac6998d22015-09-24 17:03:36 -0700900 if (!isValid(mode)) {
901 return Error::BadParameter;
902 }
903 if (mode == mPowerMode) {
904 return Error::None;
905 }
906
907 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
908
909 int error = 0;
910 if (mDevice.mHwc1MinorVersion < 4) {
911 error = mDevice.mHwc1Device->blank(mDevice.mHwc1Device, mHwc1Id,
912 mode == PowerMode::Off);
913 } else {
914 error = mDevice.mHwc1Device->setPowerMode(mDevice.mHwc1Device,
915 mHwc1Id, getHwc1PowerMode(mode));
916 }
917 ALOGE_IF(error != 0, "setPowerMode: Failed to set power mode on HWC1 (%d)",
918 error);
919
920 ALOGV("[%" PRIu64 "] setPowerMode(%s)", mId, to_string(mode).c_str());
921 mPowerMode = mode;
922 return Error::None;
923}
924
925static bool isValid(Vsync enable) {
926 switch (enable) {
927 case Vsync::Enable: // Fall-through
928 case Vsync::Disable: return true;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800929 case Vsync::Invalid: return false;
Dan Stozac6998d22015-09-24 17:03:36 -0700930 }
931}
932
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800933Error HWC2On1Adapter::Display::setVsyncEnabled(Vsync enable) {
Dan Stozac6998d22015-09-24 17:03:36 -0700934 if (!isValid(enable)) {
935 return Error::BadParameter;
936 }
937 if (enable == mVsyncEnabled) {
938 return Error::None;
939 }
940
941 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
942
943 int error = mDevice.mHwc1Device->eventControl(mDevice.mHwc1Device,
944 mHwc1Id, HWC_EVENT_VSYNC, enable == Vsync::Enable);
945 ALOGE_IF(error != 0, "setVsyncEnabled: Failed to set vsync on HWC1 (%d)",
946 error);
947
948 mVsyncEnabled = enable;
949 return Error::None;
950}
951
952Error HWC2On1Adapter::Display::validate(uint32_t* outNumTypes,
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800953 uint32_t* outNumRequests) {
Dan Stozac6998d22015-09-24 17:03:36 -0700954 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
955
Dan Stozac6998d22015-09-24 17:03:36 -0700956 if (!mChanges) {
957 if (!mDevice.prepareAllDisplays()) {
958 return Error::BadDisplay;
959 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800960 } else {
961 ALOGE("Validate was called more than once!");
Dan Stozac6998d22015-09-24 17:03:36 -0700962 }
963
964 *outNumTypes = mChanges->getNumTypes();
965 *outNumRequests = mChanges->getNumLayerRequests();
966 ALOGV("[%" PRIu64 "] validate --> %u types, %u requests", mId, *outNumTypes,
967 *outNumRequests);
968 for (auto request : mChanges->getTypeChanges()) {
969 ALOGV("Layer %" PRIu64 " --> %s", request.first,
970 to_string(request.second).c_str());
971 }
972 return *outNumTypes > 0 ? Error::HasChanges : Error::None;
973}
974
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800975Error HWC2On1Adapter::Display::updateLayerZ(hwc2_layer_t layerId, uint32_t z) {
Dan Stozac6998d22015-09-24 17:03:36 -0700976 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
977
978 const auto mapLayer = mDevice.mLayers.find(layerId);
979 if (mapLayer == mDevice.mLayers.end()) {
980 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer", mId);
981 return Error::BadLayer;
982 }
983
984 const auto layer = mapLayer->second;
985 const auto zRange = mLayers.equal_range(layer);
986 bool layerOnDisplay = false;
987 for (auto current = zRange.first; current != zRange.second; ++current) {
988 if (**current == *layer) {
989 if ((*current)->getZ() == z) {
990 // Don't change anything if the Z hasn't changed
991 return Error::None;
992 }
993 current = mLayers.erase(current);
994 layerOnDisplay = true;
995 break;
996 }
997 }
998
999 if (!layerOnDisplay) {
1000 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display",
1001 mId);
1002 return Error::BadLayer;
1003 }
1004
1005 layer->setZ(z);
1006 mLayers.emplace(std::move(layer));
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001007 markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001008
1009 return Error::None;
1010}
1011
Dan Stoza076ac672016-03-14 10:47:53 -07001012static constexpr uint32_t ATTRIBUTES_WITH_COLOR[] = {
1013 HWC_DISPLAY_VSYNC_PERIOD,
1014 HWC_DISPLAY_WIDTH,
1015 HWC_DISPLAY_HEIGHT,
1016 HWC_DISPLAY_DPI_X,
1017 HWC_DISPLAY_DPI_Y,
1018 HWC_DISPLAY_COLOR_TRANSFORM,
1019 HWC_DISPLAY_NO_ATTRIBUTE,
1020};
1021
1022static constexpr uint32_t ATTRIBUTES_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001023 HWC_DISPLAY_VSYNC_PERIOD,
1024 HWC_DISPLAY_WIDTH,
1025 HWC_DISPLAY_HEIGHT,
1026 HWC_DISPLAY_DPI_X,
1027 HWC_DISPLAY_DPI_Y,
1028 HWC_DISPLAY_NO_ATTRIBUTE,
1029};
Dan Stozac6998d22015-09-24 17:03:36 -07001030
Dan Stoza076ac672016-03-14 10:47:53 -07001031static constexpr size_t NUM_ATTRIBUTES_WITH_COLOR =
1032 sizeof(ATTRIBUTES_WITH_COLOR) / sizeof(uint32_t);
1033static_assert(sizeof(ATTRIBUTES_WITH_COLOR) > sizeof(ATTRIBUTES_WITHOUT_COLOR),
1034 "Attribute tables have unexpected sizes");
1035
1036static constexpr uint32_t ATTRIBUTE_MAP_WITH_COLOR[] = {
1037 6, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1038 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1039 1, // HWC_DISPLAY_WIDTH = 2,
1040 2, // HWC_DISPLAY_HEIGHT = 3,
1041 3, // HWC_DISPLAY_DPI_X = 4,
1042 4, // HWC_DISPLAY_DPI_Y = 5,
1043 5, // HWC_DISPLAY_COLOR_TRANSFORM = 6,
1044};
1045
1046static constexpr uint32_t ATTRIBUTE_MAP_WITHOUT_COLOR[] = {
Dan Stozac6998d22015-09-24 17:03:36 -07001047 5, // HWC_DISPLAY_NO_ATTRIBUTE = 0
1048 0, // HWC_DISPLAY_VSYNC_PERIOD = 1,
1049 1, // HWC_DISPLAY_WIDTH = 2,
1050 2, // HWC_DISPLAY_HEIGHT = 3,
1051 3, // HWC_DISPLAY_DPI_X = 4,
1052 4, // HWC_DISPLAY_DPI_Y = 5,
1053};
1054
1055template <uint32_t attribute>
1056static constexpr bool attributesMatch()
1057{
Dan Stoza076ac672016-03-14 10:47:53 -07001058 bool match = (attribute ==
1059 ATTRIBUTES_WITH_COLOR[ATTRIBUTE_MAP_WITH_COLOR[attribute]]);
1060 if (attribute == HWC_DISPLAY_COLOR_TRANSFORM) {
1061 return match;
1062 }
1063
1064 return match && (attribute ==
1065 ATTRIBUTES_WITHOUT_COLOR[ATTRIBUTE_MAP_WITHOUT_COLOR[attribute]]);
Dan Stozac6998d22015-09-24 17:03:36 -07001066}
1067static_assert(attributesMatch<HWC_DISPLAY_VSYNC_PERIOD>(),
1068 "Tables out of sync");
1069static_assert(attributesMatch<HWC_DISPLAY_WIDTH>(), "Tables out of sync");
1070static_assert(attributesMatch<HWC_DISPLAY_HEIGHT>(), "Tables out of sync");
1071static_assert(attributesMatch<HWC_DISPLAY_DPI_X>(), "Tables out of sync");
1072static_assert(attributesMatch<HWC_DISPLAY_DPI_Y>(), "Tables out of sync");
Dan Stoza076ac672016-03-14 10:47:53 -07001073static_assert(attributesMatch<HWC_DISPLAY_COLOR_TRANSFORM>(),
1074 "Tables out of sync");
Dan Stozac6998d22015-09-24 17:03:36 -07001075
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001076void HWC2On1Adapter::Display::populateConfigs() {
Dan Stozac6998d22015-09-24 17:03:36 -07001077 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1078
1079 ALOGV("[%" PRIu64 "] populateConfigs", mId);
1080
1081 if (mHwc1Id == -1) {
1082 ALOGE("populateConfigs: HWC1 ID not set");
1083 return;
1084 }
1085
1086 const size_t MAX_NUM_CONFIGS = 128;
1087 uint32_t configs[MAX_NUM_CONFIGS] = {};
1088 size_t numConfigs = MAX_NUM_CONFIGS;
1089 mDevice.mHwc1Device->getDisplayConfigs(mDevice.mHwc1Device, mHwc1Id,
1090 configs, &numConfigs);
1091
1092 for (size_t c = 0; c < numConfigs; ++c) {
1093 uint32_t hwc1ConfigId = configs[c];
Dan Stoza076ac672016-03-14 10:47:53 -07001094 auto newConfig = std::make_shared<Config>(*this);
Dan Stozac6998d22015-09-24 17:03:36 -07001095
Dan Stoza076ac672016-03-14 10:47:53 -07001096 int32_t values[NUM_ATTRIBUTES_WITH_COLOR] = {};
1097 bool hasColor = true;
1098 auto result = mDevice.mHwc1Device->getDisplayAttributes(
1099 mDevice.mHwc1Device, mHwc1Id, hwc1ConfigId,
1100 ATTRIBUTES_WITH_COLOR, values);
1101 if (result != 0) {
1102 mDevice.mHwc1Device->getDisplayAttributes(mDevice.mHwc1Device,
1103 mHwc1Id, hwc1ConfigId, ATTRIBUTES_WITHOUT_COLOR, values);
1104 hasColor = false;
Dan Stozac6998d22015-09-24 17:03:36 -07001105 }
Dan Stoza076ac672016-03-14 10:47:53 -07001106
1107 auto attributeMap = hasColor ?
1108 ATTRIBUTE_MAP_WITH_COLOR : ATTRIBUTE_MAP_WITHOUT_COLOR;
1109
1110 newConfig->setAttribute(Attribute::VsyncPeriod,
1111 values[attributeMap[HWC_DISPLAY_VSYNC_PERIOD]]);
1112 newConfig->setAttribute(Attribute::Width,
1113 values[attributeMap[HWC_DISPLAY_WIDTH]]);
1114 newConfig->setAttribute(Attribute::Height,
1115 values[attributeMap[HWC_DISPLAY_HEIGHT]]);
1116 newConfig->setAttribute(Attribute::DpiX,
1117 values[attributeMap[HWC_DISPLAY_DPI_X]]);
1118 newConfig->setAttribute(Attribute::DpiY,
1119 values[attributeMap[HWC_DISPLAY_DPI_Y]]);
1120 if (hasColor) {
Michael Wright28f24d02016-07-12 13:30:53 -07001121 // In HWC1, color modes are referred to as color transforms. To avoid confusion with
1122 // the HWC2 concept of color transforms, we internally refer to them as color modes for
1123 // both HWC1 and 2.
1124 newConfig->setAttribute(ColorMode,
Dan Stoza076ac672016-03-14 10:47:53 -07001125 values[attributeMap[HWC_DISPLAY_COLOR_TRANSFORM]]);
1126 }
1127
Michael Wright28f24d02016-07-12 13:30:53 -07001128 // We can only do this after attempting to read the color mode
Dan Stoza076ac672016-03-14 10:47:53 -07001129 newConfig->setHwc1Id(hwc1ConfigId);
1130
1131 for (auto& existingConfig : mConfigs) {
1132 if (existingConfig->merge(*newConfig)) {
1133 ALOGV("Merged config %d with existing config %u: %s",
1134 hwc1ConfigId, existingConfig->getId(),
1135 existingConfig->toString().c_str());
1136 newConfig.reset();
1137 break;
1138 }
1139 }
1140
1141 // If it wasn't merged with any existing config, add it to the end
1142 if (newConfig) {
1143 newConfig->setId(static_cast<hwc2_config_t>(mConfigs.size()));
1144 ALOGV("Found new config %u: %s", newConfig->getId(),
1145 newConfig->toString().c_str());
1146 mConfigs.emplace_back(std::move(newConfig));
1147 }
Dan Stozac6998d22015-09-24 17:03:36 -07001148 }
Dan Stoza076ac672016-03-14 10:47:53 -07001149
1150 initializeActiveConfig();
1151 populateColorModes();
Dan Stozac6998d22015-09-24 17:03:36 -07001152}
1153
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001154void HWC2On1Adapter::Display::populateConfigs(uint32_t width, uint32_t height) {
Dan Stozac6998d22015-09-24 17:03:36 -07001155 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1156
Dan Stoza076ac672016-03-14 10:47:53 -07001157 mConfigs.emplace_back(std::make_shared<Config>(*this));
Dan Stozac6998d22015-09-24 17:03:36 -07001158 auto& config = mConfigs[0];
1159
1160 config->setAttribute(Attribute::Width, static_cast<int32_t>(width));
1161 config->setAttribute(Attribute::Height, static_cast<int32_t>(height));
Dan Stoza076ac672016-03-14 10:47:53 -07001162 config->setHwc1Id(0);
1163 config->setId(0);
Dan Stozac6998d22015-09-24 17:03:36 -07001164 mActiveConfig = config;
1165}
1166
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001167bool HWC2On1Adapter::Display::prepare() {
Dan Stozac6998d22015-09-24 17:03:36 -07001168 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1169
1170 // Only prepare display contents for displays HWC1 knows about
1171 if (mHwc1Id == -1) {
1172 return true;
1173 }
1174
1175 // It doesn't make sense to prepare a display for which there is no active
1176 // config, so return early
1177 if (!mActiveConfig) {
1178 ALOGE("[%" PRIu64 "] Attempted to prepare, but no config active", mId);
1179 return false;
1180 }
1181
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001182 allocateRequestedContents();
1183 assignHwc1LayerIds();
Dan Stozac6998d22015-09-24 17:03:36 -07001184
1185 mHwc1RequestedContents->retireFenceFd = -1;
1186 mHwc1RequestedContents->flags = 0;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001187 if (mGeometryChanged) {
Dan Stozac6998d22015-09-24 17:03:36 -07001188 mHwc1RequestedContents->flags |= HWC_GEOMETRY_CHANGED;
1189 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001190 mHwc1RequestedContents->outbuf = mOutputBuffer.getBuffer();
1191 mHwc1RequestedContents->outbufAcquireFenceFd = mOutputBuffer.getFence();
Dan Stozac6998d22015-09-24 17:03:36 -07001192
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001193 // +1 is for framebuffer target layer.
1194 mHwc1RequestedContents->numHwLayers = mLayers.size() + 1;
Dan Stozac6998d22015-09-24 17:03:36 -07001195 for (auto& layer : mLayers) {
1196 auto& hwc1Layer = mHwc1RequestedContents->hwLayers[layer->getHwc1Id()];
1197 hwc1Layer.releaseFenceFd = -1;
Fabien Sanglard16ab8192017-01-31 12:12:10 -08001198 hwc1Layer.acquireFenceFd = -1;
Fabien Sanglard999a7fd2017-02-02 16:59:44 -08001199 ALOGV("Applying states for layer %" PRIu64 " ", layer->getId());
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001200 layer->applyState(hwc1Layer);
Dan Stozac6998d22015-09-24 17:03:36 -07001201 }
1202
Dan Stozac6998d22015-09-24 17:03:36 -07001203 prepareFramebufferTarget();
1204
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001205 resetGeometryMarker();
1206
Dan Stozac6998d22015-09-24 17:03:36 -07001207 return true;
1208}
1209
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001210void HWC2On1Adapter::Display::generateChanges() {
Dan Stozac6998d22015-09-24 17:03:36 -07001211 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1212
Dan Stozac6998d22015-09-24 17:03:36 -07001213 mChanges.reset(new Changes);
1214
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001215 size_t numLayers = mHwc1RequestedContents->numHwLayers;
Dan Stozac6998d22015-09-24 17:03:36 -07001216 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001217 const auto& receivedLayer = mHwc1RequestedContents->hwLayers[hwc1Id];
Dan Stozac6998d22015-09-24 17:03:36 -07001218 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1219 ALOGE_IF(receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001220 "generateChanges: HWC1 layer %zd doesn't have a"
Dan Stozac6998d22015-09-24 17:03:36 -07001221 " matching HWC2 layer, and isn't the framebuffer target",
1222 hwc1Id);
1223 continue;
1224 }
1225
1226 Layer& layer = *mHwc1LayerMap[hwc1Id];
1227 updateTypeChanges(receivedLayer, layer);
1228 updateLayerRequests(receivedLayer, layer);
1229 }
1230}
1231
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001232bool HWC2On1Adapter::Display::hasChanges() const {
Dan Stozac6998d22015-09-24 17:03:36 -07001233 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1234 return mChanges != nullptr;
1235}
1236
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001237Error HWC2On1Adapter::Display::set(hwc_display_contents_1& hwcContents) {
Dan Stozac6998d22015-09-24 17:03:36 -07001238 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1239
1240 if (!mChanges || (mChanges->getNumTypes() > 0)) {
1241 ALOGE("[%" PRIu64 "] set failed: not validated", mId);
1242 return Error::NotValidated;
1243 }
1244
1245 // Set up the client/framebuffer target
1246 auto numLayers = hwcContents.numHwLayers;
1247
1248 // Close acquire fences on FRAMEBUFFER layers, since they will not be used
1249 // by HWC
1250 for (size_t l = 0; l < numLayers - 1; ++l) {
1251 auto& layer = hwcContents.hwLayers[l];
1252 if (layer.compositionType == HWC_FRAMEBUFFER) {
1253 ALOGV("Closing fence %d for layer %zd", layer.acquireFenceFd, l);
1254 close(layer.acquireFenceFd);
1255 layer.acquireFenceFd = -1;
1256 }
1257 }
1258
1259 auto& clientTargetLayer = hwcContents.hwLayers[numLayers - 1];
1260 if (clientTargetLayer.compositionType == HWC_FRAMEBUFFER_TARGET) {
1261 clientTargetLayer.handle = mClientTarget.getBuffer();
1262 clientTargetLayer.acquireFenceFd = mClientTarget.getFence();
1263 } else {
1264 ALOGE("[%" PRIu64 "] set: last HWC layer wasn't FRAMEBUFFER_TARGET",
1265 mId);
1266 }
1267
1268 mChanges.reset();
1269
1270 return Error::None;
1271}
1272
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001273void HWC2On1Adapter::Display::addRetireFence(int fenceFd) {
Dan Stozac6998d22015-09-24 17:03:36 -07001274 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1275 mRetireFence.add(fenceFd);
1276}
1277
1278void HWC2On1Adapter::Display::addReleaseFences(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001279 const hwc_display_contents_1_t& hwcContents) {
Dan Stozac6998d22015-09-24 17:03:36 -07001280 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1281
1282 size_t numLayers = hwcContents.numHwLayers;
1283 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) {
1284 const auto& receivedLayer = hwcContents.hwLayers[hwc1Id];
1285 if (mHwc1LayerMap.count(hwc1Id) == 0) {
1286 if (receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET) {
1287 ALOGE("addReleaseFences: HWC1 layer %zd doesn't have a"
1288 " matching HWC2 layer, and isn't the framebuffer"
1289 " target", hwc1Id);
1290 }
1291 // Close the framebuffer target release fence since we will use the
1292 // display retire fence instead
1293 if (receivedLayer.releaseFenceFd != -1) {
1294 close(receivedLayer.releaseFenceFd);
1295 }
1296 continue;
1297 }
1298
1299 Layer& layer = *mHwc1LayerMap[hwc1Id];
1300 ALOGV("Adding release fence %d to layer %" PRIu64,
1301 receivedLayer.releaseFenceFd, layer.getId());
1302 layer.addReleaseFence(receivedLayer.releaseFenceFd);
1303 }
1304}
1305
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001306bool HWC2On1Adapter::Display::hasColorTransform() const {
Dan Stoza5df2a862016-03-24 16:19:37 -07001307 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1308 return mHasColorTransform;
1309}
1310
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001311static std::string hwc1CompositionString(int32_t type) {
Dan Stozac6998d22015-09-24 17:03:36 -07001312 switch (type) {
1313 case HWC_FRAMEBUFFER: return "Framebuffer";
1314 case HWC_OVERLAY: return "Overlay";
1315 case HWC_BACKGROUND: return "Background";
1316 case HWC_FRAMEBUFFER_TARGET: return "FramebufferTarget";
1317 case HWC_SIDEBAND: return "Sideband";
1318 case HWC_CURSOR_OVERLAY: return "CursorOverlay";
1319 default:
1320 return std::string("Unknown (") + std::to_string(type) + ")";
1321 }
1322}
1323
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001324static std::string hwc1TransformString(int32_t transform) {
Dan Stozac6998d22015-09-24 17:03:36 -07001325 switch (transform) {
1326 case 0: return "None";
1327 case HWC_TRANSFORM_FLIP_H: return "FlipH";
1328 case HWC_TRANSFORM_FLIP_V: return "FlipV";
1329 case HWC_TRANSFORM_ROT_90: return "Rotate90";
1330 case HWC_TRANSFORM_ROT_180: return "Rotate180";
1331 case HWC_TRANSFORM_ROT_270: return "Rotate270";
1332 case HWC_TRANSFORM_FLIP_H_ROT_90: return "FlipHRotate90";
1333 case HWC_TRANSFORM_FLIP_V_ROT_90: return "FlipVRotate90";
1334 default:
1335 return std::string("Unknown (") + std::to_string(transform) + ")";
1336 }
1337}
1338
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001339static std::string hwc1BlendModeString(int32_t mode) {
Dan Stozac6998d22015-09-24 17:03:36 -07001340 switch (mode) {
1341 case HWC_BLENDING_NONE: return "None";
1342 case HWC_BLENDING_PREMULT: return "Premultiplied";
1343 case HWC_BLENDING_COVERAGE: return "Coverage";
1344 default:
1345 return std::string("Unknown (") + std::to_string(mode) + ")";
1346 }
1347}
1348
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001349static std::string rectString(hwc_rect_t rect) {
Dan Stozac6998d22015-09-24 17:03:36 -07001350 std::stringstream output;
1351 output << "[" << rect.left << ", " << rect.top << ", ";
1352 output << rect.right << ", " << rect.bottom << "]";
1353 return output.str();
1354}
1355
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001356static std::string approximateFloatString(float f) {
Dan Stozac6998d22015-09-24 17:03:36 -07001357 if (static_cast<int32_t>(f) == f) {
1358 return std::to_string(static_cast<int32_t>(f));
1359 }
1360 int32_t truncated = static_cast<int32_t>(f * 10);
1361 bool approximate = (static_cast<float>(truncated) != f * 10);
1362 const size_t BUFFER_SIZE = 32;
1363 char buffer[BUFFER_SIZE] = {};
1364 auto bytesWritten = snprintf(buffer, BUFFER_SIZE,
1365 "%s%.1f", approximate ? "~" : "", f);
1366 return std::string(buffer, bytesWritten);
1367}
1368
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001369static std::string frectString(hwc_frect_t frect) {
Dan Stozac6998d22015-09-24 17:03:36 -07001370 std::stringstream output;
1371 output << "[" << approximateFloatString(frect.left) << ", ";
1372 output << approximateFloatString(frect.top) << ", ";
1373 output << approximateFloatString(frect.right) << ", ";
1374 output << approximateFloatString(frect.bottom) << "]";
1375 return output.str();
1376}
1377
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001378static std::string colorString(hwc_color_t color) {
Dan Stozac6998d22015-09-24 17:03:36 -07001379 std::stringstream output;
1380 output << "RGBA [";
1381 output << static_cast<int32_t>(color.r) << ", ";
1382 output << static_cast<int32_t>(color.g) << ", ";
1383 output << static_cast<int32_t>(color.b) << ", ";
1384 output << static_cast<int32_t>(color.a) << "]";
1385 return output.str();
1386}
1387
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001388static std::string alphaString(float f) {
Dan Stozac6998d22015-09-24 17:03:36 -07001389 const size_t BUFFER_SIZE = 8;
1390 char buffer[BUFFER_SIZE] = {};
1391 auto bytesWritten = snprintf(buffer, BUFFER_SIZE, "%.3f", f);
1392 return std::string(buffer, bytesWritten);
1393}
1394
1395static std::string to_string(const hwc_layer_1_t& hwcLayer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001396 int32_t hwc1MinorVersion) {
Dan Stozac6998d22015-09-24 17:03:36 -07001397 const char* fill = " ";
1398
1399 std::stringstream output;
1400
1401 output << " Composition: " <<
1402 hwc1CompositionString(hwcLayer.compositionType);
1403
1404 if (hwcLayer.compositionType == HWC_BACKGROUND) {
1405 output << " Color: " << colorString(hwcLayer.backgroundColor) << '\n';
1406 } else if (hwcLayer.compositionType == HWC_SIDEBAND) {
1407 output << " Stream: " << hwcLayer.sidebandStream << '\n';
1408 } else {
1409 output << " Buffer: " << hwcLayer.handle << "/" <<
1410 hwcLayer.acquireFenceFd << '\n';
1411 }
1412
1413 output << fill << "Display frame: " << rectString(hwcLayer.displayFrame) <<
1414 '\n';
1415
1416 output << fill << "Source crop: ";
1417 if (hwc1MinorVersion >= 3) {
1418 output << frectString(hwcLayer.sourceCropf) << '\n';
1419 } else {
1420 output << rectString(hwcLayer.sourceCropi) << '\n';
1421 }
1422
1423 output << fill << "Transform: " << hwc1TransformString(hwcLayer.transform);
1424 output << " Blend mode: " << hwc1BlendModeString(hwcLayer.blending);
1425 if (hwcLayer.planeAlpha != 0xFF) {
1426 output << " Alpha: " << alphaString(hwcLayer.planeAlpha / 255.0f);
1427 }
1428 output << '\n';
1429
1430 if (hwcLayer.hints != 0) {
1431 output << fill << "Hints:";
1432 if ((hwcLayer.hints & HWC_HINT_TRIPLE_BUFFER) != 0) {
1433 output << " TripleBuffer";
1434 }
1435 if ((hwcLayer.hints & HWC_HINT_CLEAR_FB) != 0) {
1436 output << " ClearFB";
1437 }
1438 output << '\n';
1439 }
1440
1441 if (hwcLayer.flags != 0) {
1442 output << fill << "Flags:";
1443 if ((hwcLayer.flags & HWC_SKIP_LAYER) != 0) {
1444 output << " SkipLayer";
1445 }
1446 if ((hwcLayer.flags & HWC_IS_CURSOR_LAYER) != 0) {
1447 output << " IsCursorLayer";
1448 }
1449 output << '\n';
1450 }
1451
1452 return output.str();
1453}
1454
1455static std::string to_string(const hwc_display_contents_1_t& hwcContents,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001456 int32_t hwc1MinorVersion) {
Dan Stozac6998d22015-09-24 17:03:36 -07001457 const char* fill = " ";
1458
1459 std::stringstream output;
1460 output << fill << "Geometry changed: " <<
1461 ((hwcContents.flags & HWC_GEOMETRY_CHANGED) != 0 ? "Y\n" : "N\n");
1462
1463 output << fill << hwcContents.numHwLayers << " Layer" <<
1464 ((hwcContents.numHwLayers == 1) ? "\n" : "s\n");
1465 for (size_t layer = 0; layer < hwcContents.numHwLayers; ++layer) {
1466 output << fill << " Layer " << layer;
1467 output << to_string(hwcContents.hwLayers[layer], hwc1MinorVersion);
1468 }
1469
1470 if (hwcContents.outbuf != nullptr) {
1471 output << fill << "Output buffer: " << hwcContents.outbuf << "/" <<
1472 hwcContents.outbufAcquireFenceFd << '\n';
1473 }
1474
1475 return output.str();
1476}
1477
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001478std::string HWC2On1Adapter::Display::dump() const {
Dan Stozac6998d22015-09-24 17:03:36 -07001479 std::unique_lock<std::recursive_mutex> lock(mStateMutex);
1480
1481 std::stringstream output;
1482
1483 output << " Display " << mId << ": ";
1484 output << to_string(mType) << " ";
1485 output << "HWC1 ID: " << mHwc1Id << " ";
1486 output << "Power mode: " << to_string(mPowerMode) << " ";
1487 output << "Vsync: " << to_string(mVsyncEnabled) << '\n';
1488
Dan Stoza076ac672016-03-14 10:47:53 -07001489 output << " Color modes [active]:";
1490 for (const auto& mode : mColorModes) {
1491 if (mode == mActiveColorMode) {
1492 output << " [" << mode << ']';
Dan Stozac6998d22015-09-24 17:03:36 -07001493 } else {
Dan Stoza076ac672016-03-14 10:47:53 -07001494 output << " " << mode;
Dan Stozac6998d22015-09-24 17:03:36 -07001495 }
1496 }
1497 output << '\n';
1498
Dan Stoza076ac672016-03-14 10:47:53 -07001499 output << " " << mConfigs.size() << " Config" <<
1500 (mConfigs.size() == 1 ? "" : "s") << " (* active)\n";
1501 for (const auto& config : mConfigs) {
1502 output << (config == mActiveConfig ? " * " : " ");
1503 output << config->toString(true) << '\n';
1504 }
1505
Dan Stozac6998d22015-09-24 17:03:36 -07001506 output << " " << mLayers.size() << " Layer" <<
1507 (mLayers.size() == 1 ? "" : "s") << '\n';
1508 for (const auto& layer : mLayers) {
1509 output << layer->dump();
1510 }
1511
1512 output << " Client target: " << mClientTarget.getBuffer() << '\n';
1513
1514 if (mOutputBuffer.getBuffer() != nullptr) {
1515 output << " Output buffer: " << mOutputBuffer.getBuffer() << '\n';
1516 }
1517
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001518 if (mHwc1RequestedContents) {
Dan Stozac6998d22015-09-24 17:03:36 -07001519 output << " Last requested HWC1 state\n";
1520 output << to_string(*mHwc1RequestedContents, mDevice.mHwc1MinorVersion);
1521 }
1522
1523 return output.str();
1524}
1525
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001526hwc_rect_t* HWC2On1Adapter::Display::GetRects(size_t numRects) {
1527 if (numRects == 0) {
1528 return nullptr;
1529 }
1530
1531 if (numRects > mNumAvailableRects) {
1532 // This should NEVER happen since we calculated how many rects the
1533 // display would need.
1534 ALOGE("Rect allocation failure! SF is likely to crash soon!");
1535 return nullptr;
1536
1537 }
1538 hwc_rect_t* rects = mNextAvailableRect;
1539 mNextAvailableRect += numRects;
1540 mNumAvailableRects -= numRects;
1541 return rects;
1542}
1543
1544hwc_display_contents_1* HWC2On1Adapter::Display::getDisplayContents() {
1545 return mHwc1RequestedContents.get();
1546}
1547
Dan Stozac6998d22015-09-24 17:03:36 -07001548void HWC2On1Adapter::Display::Config::setAttribute(HWC2::Attribute attribute,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001549 int32_t value) {
Dan Stozac6998d22015-09-24 17:03:36 -07001550 mAttributes[attribute] = value;
1551}
1552
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001553int32_t HWC2On1Adapter::Display::Config::getAttribute(Attribute attribute) const {
Dan Stozac6998d22015-09-24 17:03:36 -07001554 if (mAttributes.count(attribute) == 0) {
1555 return -1;
1556 }
1557 return mAttributes.at(attribute);
1558}
1559
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001560void HWC2On1Adapter::Display::Config::setHwc1Id(uint32_t id) {
Michael Wright28f24d02016-07-12 13:30:53 -07001561 android_color_mode_t colorMode = static_cast<android_color_mode_t>(getAttribute(ColorMode));
1562 mHwc1Ids.emplace(colorMode, id);
Dan Stoza076ac672016-03-14 10:47:53 -07001563}
1564
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001565bool HWC2On1Adapter::Display::Config::hasHwc1Id(uint32_t id) const {
Dan Stoza076ac672016-03-14 10:47:53 -07001566 for (const auto& idPair : mHwc1Ids) {
1567 if (id == idPair.second) {
1568 return true;
1569 }
1570 }
1571 return false;
1572}
1573
Michael Wright28f24d02016-07-12 13:30:53 -07001574Error HWC2On1Adapter::Display::Config::getColorModeForHwc1Id(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001575 uint32_t id, android_color_mode_t* outMode) const {
Dan Stoza076ac672016-03-14 10:47:53 -07001576 for (const auto& idPair : mHwc1Ids) {
1577 if (id == idPair.second) {
Michael Wright28f24d02016-07-12 13:30:53 -07001578 *outMode = idPair.first;
1579 return Error::None;
Dan Stoza076ac672016-03-14 10:47:53 -07001580 }
1581 }
Michael Wright28f24d02016-07-12 13:30:53 -07001582 ALOGE("Unable to find color mode for HWC ID %" PRIu32 " on config %u", id, mId);
1583 return Error::BadParameter;
Dan Stoza076ac672016-03-14 10:47:53 -07001584}
1585
Michael Wright28f24d02016-07-12 13:30:53 -07001586Error HWC2On1Adapter::Display::Config::getHwc1IdForColorMode(android_color_mode_t mode,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001587 uint32_t* outId) const {
Dan Stoza076ac672016-03-14 10:47:53 -07001588 for (const auto& idPair : mHwc1Ids) {
1589 if (mode == idPair.first) {
1590 *outId = idPair.second;
1591 return Error::None;
1592 }
1593 }
1594 ALOGE("Unable to find HWC1 ID for color mode %d on config %u", mode, mId);
1595 return Error::BadParameter;
1596}
1597
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001598bool HWC2On1Adapter::Display::Config::merge(const Config& other) {
Dan Stoza076ac672016-03-14 10:47:53 -07001599 auto attributes = {HWC2::Attribute::Width, HWC2::Attribute::Height,
1600 HWC2::Attribute::VsyncPeriod, HWC2::Attribute::DpiX,
1601 HWC2::Attribute::DpiY};
1602 for (auto attribute : attributes) {
1603 if (getAttribute(attribute) != other.getAttribute(attribute)) {
1604 return false;
1605 }
1606 }
Michael Wright28f24d02016-07-12 13:30:53 -07001607 android_color_mode_t otherColorMode =
1608 static_cast<android_color_mode_t>(other.getAttribute(ColorMode));
1609 if (mHwc1Ids.count(otherColorMode) != 0) {
Dan Stoza076ac672016-03-14 10:47:53 -07001610 ALOGE("Attempted to merge two configs (%u and %u) which appear to be "
Michael Wright28f24d02016-07-12 13:30:53 -07001611 "identical", mHwc1Ids.at(otherColorMode),
1612 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001613 return false;
1614 }
Michael Wright28f24d02016-07-12 13:30:53 -07001615 mHwc1Ids.emplace(otherColorMode,
1616 other.mHwc1Ids.at(otherColorMode));
Dan Stoza076ac672016-03-14 10:47:53 -07001617 return true;
1618}
1619
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001620std::set<android_color_mode_t> HWC2On1Adapter::Display::Config::getColorModes() const {
Michael Wright28f24d02016-07-12 13:30:53 -07001621 std::set<android_color_mode_t> colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001622 for (const auto& idPair : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001623 colorModes.emplace(idPair.first);
Dan Stoza076ac672016-03-14 10:47:53 -07001624 }
Michael Wright28f24d02016-07-12 13:30:53 -07001625 return colorModes;
Dan Stoza076ac672016-03-14 10:47:53 -07001626}
1627
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001628std::string HWC2On1Adapter::Display::Config::toString(bool splitLine) const {
Dan Stozac6998d22015-09-24 17:03:36 -07001629 std::string output;
1630
1631 const size_t BUFFER_SIZE = 100;
1632 char buffer[BUFFER_SIZE] = {};
1633 auto writtenBytes = snprintf(buffer, BUFFER_SIZE,
Dan Stoza076ac672016-03-14 10:47:53 -07001634 "%u x %u", mAttributes.at(HWC2::Attribute::Width),
Dan Stozac6998d22015-09-24 17:03:36 -07001635 mAttributes.at(HWC2::Attribute::Height));
1636 output.append(buffer, writtenBytes);
1637
1638 if (mAttributes.count(HWC2::Attribute::VsyncPeriod) != 0) {
1639 std::memset(buffer, 0, BUFFER_SIZE);
1640 writtenBytes = snprintf(buffer, BUFFER_SIZE, " @ %.1f Hz",
1641 1e9 / mAttributes.at(HWC2::Attribute::VsyncPeriod));
1642 output.append(buffer, writtenBytes);
1643 }
1644
1645 if (mAttributes.count(HWC2::Attribute::DpiX) != 0 &&
1646 mAttributes.at(HWC2::Attribute::DpiX) != -1) {
1647 std::memset(buffer, 0, BUFFER_SIZE);
1648 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1649 ", DPI: %.1f x %.1f",
1650 mAttributes.at(HWC2::Attribute::DpiX) / 1000.0f,
1651 mAttributes.at(HWC2::Attribute::DpiY) / 1000.0f);
1652 output.append(buffer, writtenBytes);
1653 }
1654
Dan Stoza076ac672016-03-14 10:47:53 -07001655 std::memset(buffer, 0, BUFFER_SIZE);
1656 if (splitLine) {
1657 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1658 "\n HWC1 ID/Color transform:");
1659 } else {
1660 writtenBytes = snprintf(buffer, BUFFER_SIZE,
1661 ", HWC1 ID/Color transform:");
1662 }
1663 output.append(buffer, writtenBytes);
1664
1665
1666 for (const auto& id : mHwc1Ids) {
Michael Wright28f24d02016-07-12 13:30:53 -07001667 android_color_mode_t colorMode = id.first;
Dan Stoza076ac672016-03-14 10:47:53 -07001668 uint32_t hwc1Id = id.second;
1669 std::memset(buffer, 0, BUFFER_SIZE);
Michael Wright28f24d02016-07-12 13:30:53 -07001670 if (colorMode == mDisplay.mActiveColorMode) {
Dan Stoza076ac672016-03-14 10:47:53 -07001671 writtenBytes = snprintf(buffer, BUFFER_SIZE, " [%u/%d]", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001672 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001673 } else {
1674 writtenBytes = snprintf(buffer, BUFFER_SIZE, " %u/%d", hwc1Id,
Michael Wright28f24d02016-07-12 13:30:53 -07001675 colorMode);
Dan Stoza076ac672016-03-14 10:47:53 -07001676 }
1677 output.append(buffer, writtenBytes);
1678 }
1679
Dan Stozac6998d22015-09-24 17:03:36 -07001680 return output;
1681}
1682
1683std::shared_ptr<const HWC2On1Adapter::Display::Config>
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001684 HWC2On1Adapter::Display::getConfig(hwc2_config_t configId) const {
Dan Stozac6998d22015-09-24 17:03:36 -07001685 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) {
1686 return nullptr;
1687 }
1688 return mConfigs[configId];
1689}
1690
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001691void HWC2On1Adapter::Display::populateColorModes() {
Michael Wright28f24d02016-07-12 13:30:53 -07001692 mColorModes = mConfigs[0]->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001693 for (const auto& config : mConfigs) {
Michael Wright28f24d02016-07-12 13:30:53 -07001694 std::set<android_color_mode_t> intersection;
1695 auto configModes = config->getColorModes();
Dan Stoza076ac672016-03-14 10:47:53 -07001696 std::set_intersection(mColorModes.cbegin(), mColorModes.cend(),
1697 configModes.cbegin(), configModes.cend(),
1698 std::inserter(intersection, intersection.begin()));
1699 std::swap(intersection, mColorModes);
1700 }
1701}
1702
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001703void HWC2On1Adapter::Display::initializeActiveConfig() {
Dan Stoza076ac672016-03-14 10:47:53 -07001704 if (mDevice.mHwc1Device->getActiveConfig == nullptr) {
1705 ALOGV("getActiveConfig is null, choosing config 0");
1706 mActiveConfig = mConfigs[0];
Michael Wright28f24d02016-07-12 13:30:53 -07001707 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001708 return;
1709 }
1710
1711 auto activeConfig = mDevice.mHwc1Device->getActiveConfig(
1712 mDevice.mHwc1Device, mHwc1Id);
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001713
1714 // Some devices startup without an activeConfig:
1715 // We need to set one ourselves.
1716 if (activeConfig == HWC_ERROR) {
1717 ALOGV("There is no active configuration: Picking the first one: 0.");
1718 const int defaultIndex = 0;
1719 mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, mHwc1Id, defaultIndex);
1720 activeConfig = defaultIndex;
1721 }
1722
1723 for (const auto& config : mConfigs) {
1724 if (config->hasHwc1Id(activeConfig)) {
1725 ALOGE("Setting active config to %d for HWC1 config %u", config->getId(), activeConfig);
1726 mActiveConfig = config;
1727 if (config->getColorModeForHwc1Id(activeConfig, &mActiveColorMode) != Error::None) {
1728 // This should never happen since we checked for the config's presence before
1729 // setting it as active.
1730 ALOGE("Unable to find color mode for active HWC1 config %d", config->getId());
1731 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Dan Stoza076ac672016-03-14 10:47:53 -07001732 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001733 break;
Dan Stoza076ac672016-03-14 10:47:53 -07001734 }
1735 }
Fabien Sanglardb7432cc2016-11-11 09:40:27 -08001736 if (!mActiveConfig) {
1737 ALOGV("Unable to find active HWC1 config %u, defaulting to "
1738 "config 0", activeConfig);
1739 mActiveConfig = mConfigs[0];
1740 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
1741 }
1742
1743
1744
1745
Dan Stoza076ac672016-03-14 10:47:53 -07001746}
1747
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001748void HWC2On1Adapter::Display::allocateRequestedContents() {
1749 // What needs to be allocated:
1750 // 1 hwc_display_contents_1_t
1751 // 1 hwc_layer_1_t for each layer
1752 // 1 hwc_rect_t for each layer's surfaceDamage
1753 // 1 hwc_rect_t for each layer's visibleRegion
1754 // 1 hwc_layer_1_t for the framebuffer
1755 // 1 hwc_rect_t for the framebuffer's visibleRegion
1756
1757 // Count # of surfaceDamage
1758 size_t numSurfaceDamages = 0;
1759 for (const auto& layer : mLayers) {
1760 numSurfaceDamages += layer->getNumSurfaceDamages();
1761 }
1762
1763 // Count # of visibleRegions (start at 1 for mandatory framebuffer target
1764 // region)
1765 size_t numVisibleRegion = 1;
1766 for (const auto& layer : mLayers) {
1767 numVisibleRegion += layer->getNumVisibleRegions();
1768 }
1769
1770 size_t numRects = numVisibleRegion + numSurfaceDamages;
Dan Stozac6998d22015-09-24 17:03:36 -07001771 auto numLayers = mLayers.size() + 1;
1772 size_t size = sizeof(hwc_display_contents_1_t) +
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001773 sizeof(hwc_layer_1_t) * numLayers +
1774 sizeof(hwc_rect_t) * numRects;
1775 auto contents = static_cast<hwc_display_contents_1_t*>(std::calloc(size, 1));
Dan Stozac6998d22015-09-24 17:03:36 -07001776 mHwc1RequestedContents.reset(contents);
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001777 mNextAvailableRect = reinterpret_cast<hwc_rect_t*>(&contents->hwLayers[numLayers]);
1778 mNumAvailableRects = numRects;
Dan Stozac6998d22015-09-24 17:03:36 -07001779}
1780
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001781void HWC2On1Adapter::Display::assignHwc1LayerIds() {
Dan Stozac6998d22015-09-24 17:03:36 -07001782 mHwc1LayerMap.clear();
1783 size_t nextHwc1Id = 0;
1784 for (auto& layer : mLayers) {
1785 mHwc1LayerMap[nextHwc1Id] = layer;
1786 layer->setHwc1Id(nextHwc1Id++);
1787 }
1788}
1789
1790void HWC2On1Adapter::Display::updateTypeChanges(const hwc_layer_1_t& hwc1Layer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001791 const Layer& layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07001792 auto layerId = layer.getId();
1793 switch (hwc1Layer.compositionType) {
1794 case HWC_FRAMEBUFFER:
1795 if (layer.getCompositionType() != Composition::Client) {
1796 mChanges->addTypeChange(layerId, Composition::Client);
1797 }
1798 break;
1799 case HWC_OVERLAY:
1800 if (layer.getCompositionType() != Composition::Device) {
1801 mChanges->addTypeChange(layerId, Composition::Device);
1802 }
1803 break;
1804 case HWC_BACKGROUND:
1805 ALOGE_IF(layer.getCompositionType() != Composition::SolidColor,
1806 "updateTypeChanges: HWC1 requested BACKGROUND, but HWC2"
1807 " wasn't expecting SolidColor");
1808 break;
1809 case HWC_FRAMEBUFFER_TARGET:
1810 // Do nothing, since it shouldn't be modified by HWC1
1811 break;
1812 case HWC_SIDEBAND:
1813 ALOGE_IF(layer.getCompositionType() != Composition::Sideband,
1814 "updateTypeChanges: HWC1 requested SIDEBAND, but HWC2"
1815 " wasn't expecting Sideband");
1816 break;
1817 case HWC_CURSOR_OVERLAY:
1818 ALOGE_IF(layer.getCompositionType() != Composition::Cursor,
1819 "updateTypeChanges: HWC1 requested CURSOR_OVERLAY, but"
1820 " HWC2 wasn't expecting Cursor");
1821 break;
1822 }
1823}
1824
1825void HWC2On1Adapter::Display::updateLayerRequests(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001826 const hwc_layer_1_t& hwc1Layer, const Layer& layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07001827 if ((hwc1Layer.hints & HWC_HINT_CLEAR_FB) != 0) {
1828 mChanges->addLayerRequest(layer.getId(),
1829 LayerRequest::ClearClientTarget);
1830 }
1831}
1832
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001833void HWC2On1Adapter::Display::prepareFramebufferTarget() {
Dan Stozac6998d22015-09-24 17:03:36 -07001834 // We check that mActiveConfig is valid in Display::prepare
1835 int32_t width = mActiveConfig->getAttribute(Attribute::Width);
1836 int32_t height = mActiveConfig->getAttribute(Attribute::Height);
1837
1838 auto& hwc1Target = mHwc1RequestedContents->hwLayers[mLayers.size()];
1839 hwc1Target.compositionType = HWC_FRAMEBUFFER_TARGET;
1840 hwc1Target.releaseFenceFd = -1;
1841 hwc1Target.hints = 0;
1842 hwc1Target.flags = 0;
1843 hwc1Target.transform = 0;
1844 hwc1Target.blending = HWC_BLENDING_PREMULT;
1845 if (mDevice.getHwc1MinorVersion() < 3) {
1846 hwc1Target.sourceCropi = {0, 0, width, height};
1847 } else {
1848 hwc1Target.sourceCropf = {0.0f, 0.0f, static_cast<float>(width),
1849 static_cast<float>(height)};
1850 }
1851 hwc1Target.displayFrame = {0, 0, width, height};
1852 hwc1Target.planeAlpha = 255;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001853
Dan Stozac6998d22015-09-24 17:03:36 -07001854 hwc1Target.visibleRegionScreen.numRects = 1;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001855 hwc_rect_t* rects = GetRects(1);
Dan Stozac6998d22015-09-24 17:03:36 -07001856 rects[0].left = 0;
1857 rects[0].top = 0;
1858 rects[0].right = width;
1859 rects[0].bottom = height;
1860 hwc1Target.visibleRegionScreen.rects = rects;
1861
1862 // We will set this to the correct value in set
1863 hwc1Target.acquireFenceFd = -1;
1864}
1865
1866// Layer functions
1867
1868std::atomic<hwc2_layer_t> HWC2On1Adapter::Layer::sNextId(1);
1869
1870HWC2On1Adapter::Layer::Layer(Display& display)
1871 : mId(sNextId++),
1872 mDisplay(display),
Dan Stozafc4e2022016-02-23 11:43:19 -08001873 mBuffer(),
1874 mSurfaceDamage(),
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001875 mBlendMode(BlendMode::None),
1876 mColor({0, 0, 0, 0}),
1877 mCompositionType(Composition::Invalid),
1878 mDisplayFrame({0, 0, -1, -1}),
1879 mPlaneAlpha(0.0f),
1880 mSidebandStream(nullptr),
1881 mSourceCrop({0.0f, 0.0f, -1.0f, -1.0f}),
1882 mTransform(Transform::None),
1883 mVisibleRegion(),
Dan Stozac6998d22015-09-24 17:03:36 -07001884 mZ(0),
Dan Stozafc4e2022016-02-23 11:43:19 -08001885 mReleaseFence(),
Dan Stozac6998d22015-09-24 17:03:36 -07001886 mHwc1Id(0),
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001887 mHasUnsupportedPlaneAlpha(false) {}
Dan Stozac6998d22015-09-24 17:03:36 -07001888
1889bool HWC2On1Adapter::SortLayersByZ::operator()(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001890 const std::shared_ptr<Layer>& lhs, const std::shared_ptr<Layer>& rhs) {
Dan Stozac6998d22015-09-24 17:03:36 -07001891 return lhs->getZ() < rhs->getZ();
1892}
1893
1894Error HWC2On1Adapter::Layer::setBuffer(buffer_handle_t buffer,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001895 int32_t acquireFence) {
Dan Stozac6998d22015-09-24 17:03:36 -07001896 ALOGV("Setting acquireFence to %d for layer %" PRIu64, acquireFence, mId);
1897 mBuffer.setBuffer(buffer);
1898 mBuffer.setFence(acquireFence);
1899 return Error::None;
1900}
1901
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001902Error HWC2On1Adapter::Layer::setCursorPosition(int32_t x, int32_t y) {
1903 if (mCompositionType != Composition::Cursor) {
Dan Stozac6998d22015-09-24 17:03:36 -07001904 return Error::BadLayer;
1905 }
1906
1907 if (mDisplay.hasChanges()) {
1908 return Error::NotValidated;
1909 }
1910
1911 auto displayId = mDisplay.getHwc1Id();
1912 auto hwc1Device = mDisplay.getDevice().getHwc1Device();
1913 hwc1Device->setCursorPositionAsync(hwc1Device, displayId, x, y);
1914 return Error::None;
1915}
1916
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001917Error HWC2On1Adapter::Layer::setSurfaceDamage(hwc_region_t damage) {
Fabien Sanglard356bcd42017-02-17 16:14:18 -08001918 // HWC1 supports surface damage starting only with version 1.5.
1919 if (mDisplay.getDevice().mHwc1MinorVersion < 5) {
1920 return Error::None;
1921 }
Dan Stozac6998d22015-09-24 17:03:36 -07001922 mSurfaceDamage.resize(damage.numRects);
1923 std::copy_n(damage.rects, damage.numRects, mSurfaceDamage.begin());
1924 return Error::None;
1925}
1926
1927// Layer state functions
1928
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001929Error HWC2On1Adapter::Layer::setBlendMode(BlendMode mode) {
1930 mBlendMode = mode;
1931 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001932 return Error::None;
1933}
1934
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001935Error HWC2On1Adapter::Layer::setColor(hwc_color_t color) {
1936 mColor = color;
1937 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001938 return Error::None;
1939}
1940
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001941Error HWC2On1Adapter::Layer::setCompositionType(Composition type) {
1942 mCompositionType = type;
1943 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001944 return Error::None;
1945}
1946
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001947Error HWC2On1Adapter::Layer::setDataspace(android_dataspace_t) {
Dan Stoza5df2a862016-03-24 16:19:37 -07001948 return Error::None;
1949}
1950
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001951Error HWC2On1Adapter::Layer::setDisplayFrame(hwc_rect_t frame) {
1952 mDisplayFrame = frame;
1953 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001954 return Error::None;
1955}
1956
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001957Error HWC2On1Adapter::Layer::setPlaneAlpha(float alpha) {
1958 mPlaneAlpha = alpha;
1959 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001960 return Error::None;
1961}
1962
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001963Error HWC2On1Adapter::Layer::setSidebandStream(const native_handle_t* stream) {
1964 mSidebandStream = stream;
1965 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001966 return Error::None;
1967}
1968
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001969Error HWC2On1Adapter::Layer::setSourceCrop(hwc_frect_t crop) {
1970 mSourceCrop = crop;
1971 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001972 return Error::None;
1973}
1974
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001975Error HWC2On1Adapter::Layer::setTransform(Transform transform) {
1976 mTransform = transform;
1977 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001978 return Error::None;
1979}
1980
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001981Error HWC2On1Adapter::Layer::setVisibleRegion(hwc_region_t visible) {
1982 mVisibleRegion.resize(visible.numRects);
1983 std::copy_n(visible.rects, visible.numRects, mVisibleRegion.begin());
1984 mDisplay.markGeometryChanged();
Dan Stozac6998d22015-09-24 17:03:36 -07001985 return Error::None;
1986}
1987
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001988Error HWC2On1Adapter::Layer::setZ(uint32_t z) {
Dan Stozac6998d22015-09-24 17:03:36 -07001989 mZ = z;
1990 return Error::None;
1991}
1992
Fabien Sanglard06e908a2017-02-12 00:08:14 -08001993void HWC2On1Adapter::Layer::addReleaseFence(int fenceFd) {
Dan Stozac6998d22015-09-24 17:03:36 -07001994 ALOGV("addReleaseFence %d to layer %" PRIu64, fenceFd, mId);
1995 mReleaseFence.add(fenceFd);
1996}
1997
Fabien Sanglardc8591472017-03-07 10:10:03 -08001998const sp<MiniFence>& HWC2On1Adapter::Layer::getReleaseFence() const {
Dan Stozac6998d22015-09-24 17:03:36 -07001999 return mReleaseFence.get();
2000}
2001
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002002void HWC2On1Adapter::Layer::applyState(hwc_layer_1_t& hwc1Layer) {
2003 applyCommonState(hwc1Layer);
2004 applyCompositionType(hwc1Layer);
2005 switch (mCompositionType) {
2006 case Composition::SolidColor : applySolidColorState(hwc1Layer); break;
2007 case Composition::Sideband : applySidebandState(hwc1Layer); break;
2008 default: applyBufferState(hwc1Layer); break;
Dan Stozac6998d22015-09-24 17:03:36 -07002009 }
Dan Stozac6998d22015-09-24 17:03:36 -07002010}
2011
Dan Stozac6998d22015-09-24 17:03:36 -07002012static std::string regionStrings(const std::vector<hwc_rect_t>& visibleRegion,
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002013 const std::vector<hwc_rect_t>& surfaceDamage) {
Dan Stozac6998d22015-09-24 17:03:36 -07002014 std::string regions;
2015 regions += " Visible Region";
2016 regions.resize(40, ' ');
2017 regions += "Surface Damage\n";
2018
2019 size_t numPrinted = 0;
2020 size_t maxSize = std::max(visibleRegion.size(), surfaceDamage.size());
2021 while (numPrinted < maxSize) {
2022 std::string line(" ");
2023 if (visibleRegion.empty() && numPrinted == 0) {
2024 line += "None";
2025 } else if (numPrinted < visibleRegion.size()) {
2026 line += rectString(visibleRegion[numPrinted]);
2027 }
2028 line.resize(40, ' ');
2029 if (surfaceDamage.empty() && numPrinted == 0) {
2030 line += "None";
2031 } else if (numPrinted < surfaceDamage.size()) {
2032 line += rectString(surfaceDamage[numPrinted]);
2033 }
2034 line += '\n';
2035 regions += line;
2036 ++numPrinted;
2037 }
2038 return regions;
2039}
2040
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002041std::string HWC2On1Adapter::Layer::dump() const {
Dan Stozac6998d22015-09-24 17:03:36 -07002042 std::stringstream output;
2043 const char* fill = " ";
2044
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002045 output << fill << to_string(mCompositionType);
Dan Stozac6998d22015-09-24 17:03:36 -07002046 output << " Layer HWC2/1: " << mId << "/" << mHwc1Id << " ";
2047 output << "Z: " << mZ;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002048 if (mCompositionType == HWC2::Composition::SolidColor) {
2049 output << " " << colorString(mColor);
2050 } else if (mCompositionType == HWC2::Composition::Sideband) {
2051 output << " Handle: " << mSidebandStream << '\n';
Dan Stozac6998d22015-09-24 17:03:36 -07002052 } else {
2053 output << " Buffer: " << mBuffer.getBuffer() << "/" <<
2054 mBuffer.getFence() << '\n';
2055 output << fill << " Display frame [LTRB]: " <<
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002056 rectString(mDisplayFrame) << '\n';
Dan Stozac6998d22015-09-24 17:03:36 -07002057 output << fill << " Source crop: " <<
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002058 frectString(mSourceCrop) << '\n';
2059 output << fill << " Transform: " << to_string(mTransform);
2060 output << " Blend mode: " << to_string(mBlendMode);
2061 if (mPlaneAlpha != 1.0f) {
Dan Stozac6998d22015-09-24 17:03:36 -07002062 output << " Alpha: " <<
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002063 alphaString(mPlaneAlpha) << '\n';
Dan Stozac6998d22015-09-24 17:03:36 -07002064 } else {
2065 output << '\n';
2066 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002067 output << regionStrings(mVisibleRegion, mSurfaceDamage);
Dan Stozac6998d22015-09-24 17:03:36 -07002068 }
2069 return output.str();
2070}
2071
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002072static int getHwc1Blending(HWC2::BlendMode blendMode) {
Dan Stozac6998d22015-09-24 17:03:36 -07002073 switch (blendMode) {
2074 case BlendMode::Coverage: return HWC_BLENDING_COVERAGE;
2075 case BlendMode::Premultiplied: return HWC_BLENDING_PREMULT;
2076 default: return HWC_BLENDING_NONE;
2077 }
2078}
2079
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002080void HWC2On1Adapter::Layer::applyCommonState(hwc_layer_1_t& hwc1Layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07002081 auto minorVersion = mDisplay.getDevice().getHwc1MinorVersion();
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002082 hwc1Layer.blending = getHwc1Blending(mBlendMode);
2083 hwc1Layer.displayFrame = mDisplayFrame;
Dan Stozac6998d22015-09-24 17:03:36 -07002084
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002085 auto pendingAlpha = mPlaneAlpha;
2086 if (minorVersion < 2) {
2087 mHasUnsupportedPlaneAlpha = pendingAlpha < 1.0f;
2088 } else {
2089 hwc1Layer.planeAlpha =
2090 static_cast<uint8_t>(255.0f * pendingAlpha + 0.5f);
2091 }
Dan Stozac6998d22015-09-24 17:03:36 -07002092
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002093 if (minorVersion < 3) {
2094 auto pending = mSourceCrop;
2095 hwc1Layer.sourceCropi.left =
2096 static_cast<int32_t>(std::ceil(pending.left));
2097 hwc1Layer.sourceCropi.top =
2098 static_cast<int32_t>(std::ceil(pending.top));
2099 hwc1Layer.sourceCropi.right =
2100 static_cast<int32_t>(std::floor(pending.right));
2101 hwc1Layer.sourceCropi.bottom =
2102 static_cast<int32_t>(std::floor(pending.bottom));
2103 } else {
2104 hwc1Layer.sourceCropf = mSourceCrop;
2105 }
2106
2107 hwc1Layer.transform = static_cast<uint32_t>(mTransform);
2108
2109 auto& hwc1VisibleRegion = hwc1Layer.visibleRegionScreen;
2110 hwc1VisibleRegion.numRects = mVisibleRegion.size();
2111 hwc_rect_t* rects = mDisplay.GetRects(hwc1VisibleRegion.numRects);
2112 hwc1VisibleRegion.rects = rects;
2113 for (size_t i = 0; i < mVisibleRegion.size(); i++) {
2114 rects[i] = mVisibleRegion[i];
Dan Stozac6998d22015-09-24 17:03:36 -07002115 }
2116}
2117
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002118void HWC2On1Adapter::Layer::applySolidColorState(hwc_layer_1_t& hwc1Layer) {
2119 // If the device does not support background color it is likely to make
2120 // assumption regarding backgroundColor and handle (both fields occupy
2121 // the same location in hwc_layer_1_t union).
2122 // To not confuse these devices we don't set background color and we
2123 // make sure handle is a null pointer.
2124 if (hasUnsupportedBackgroundColor()) {
2125 hwc1Layer.handle = nullptr;
2126 } else {
2127 hwc1Layer.backgroundColor = mColor;
Dan Stozac6998d22015-09-24 17:03:36 -07002128 }
2129}
2130
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002131void HWC2On1Adapter::Layer::applySidebandState(hwc_layer_1_t& hwc1Layer) {
2132 hwc1Layer.sidebandStream = mSidebandStream;
Dan Stozac6998d22015-09-24 17:03:36 -07002133}
2134
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002135void HWC2On1Adapter::Layer::applyBufferState(hwc_layer_1_t& hwc1Layer) {
Dan Stozac6998d22015-09-24 17:03:36 -07002136 hwc1Layer.handle = mBuffer.getBuffer();
2137 hwc1Layer.acquireFenceFd = mBuffer.getFence();
2138}
2139
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002140void HWC2On1Adapter::Layer::applyCompositionType(hwc_layer_1_t& hwc1Layer) {
Dan Stoza5df2a862016-03-24 16:19:37 -07002141 // HWC1 never supports color transforms or dataspaces and only sometimes
2142 // supports plane alpha (depending on the version). These require us to drop
2143 // some or all layers to client composition.
Fabien Sanglard999a7fd2017-02-02 16:59:44 -08002144 if (mHasUnsupportedPlaneAlpha || mDisplay.hasColorTransform() ||
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002145 hasUnsupportedBackgroundColor()) {
Dan Stozac6998d22015-09-24 17:03:36 -07002146 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2147 hwc1Layer.flags = HWC_SKIP_LAYER;
2148 return;
2149 }
2150
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002151 hwc1Layer.flags = 0;
2152 switch (mCompositionType) {
2153 case Composition::Client:
2154 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2155 hwc1Layer.flags |= HWC_SKIP_LAYER;
2156 break;
2157 case Composition::Device:
2158 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2159 break;
2160 case Composition::SolidColor:
2161 // In theory the following line should work, but since the HWC1
2162 // version of SurfaceFlinger never used HWC_BACKGROUND, HWC1
2163 // devices may not work correctly. To be on the safe side, we
2164 // fall back to client composition.
2165 //
2166 // hwc1Layer.compositionType = HWC_BACKGROUND;
2167 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2168 hwc1Layer.flags |= HWC_SKIP_LAYER;
2169 break;
2170 case Composition::Cursor:
2171 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2172 if (mDisplay.getDevice().getHwc1MinorVersion() >= 4) {
2173 hwc1Layer.hints |= HWC_IS_CURSOR_LAYER;
2174 }
2175 break;
2176 case Composition::Sideband:
2177 if (mDisplay.getDevice().getHwc1MinorVersion() < 4) {
2178 hwc1Layer.compositionType = HWC_SIDEBAND;
2179 } else {
Dan Stozac6998d22015-09-24 17:03:36 -07002180 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2181 hwc1Layer.flags |= HWC_SKIP_LAYER;
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002182 }
2183 break;
2184 default:
2185 hwc1Layer.compositionType = HWC_FRAMEBUFFER;
2186 hwc1Layer.flags |= HWC_SKIP_LAYER;
2187 break;
Dan Stozac6998d22015-09-24 17:03:36 -07002188 }
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002189 ALOGV("Layer %" PRIu64 " %s set to %d", mId,
2190 to_string(mCompositionType).c_str(),
2191 hwc1Layer.compositionType);
2192 ALOGV_IF(hwc1Layer.flags & HWC_SKIP_LAYER, " and skipping");
Dan Stozac6998d22015-09-24 17:03:36 -07002193}
2194
2195// Adapter helpers
2196
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002197void HWC2On1Adapter::populateCapabilities() {
Dan Stozac6998d22015-09-24 17:03:36 -07002198 if (mHwc1MinorVersion >= 3U) {
2199 int supportedTypes = 0;
2200 auto result = mHwc1Device->query(mHwc1Device,
2201 HWC_DISPLAY_TYPES_SUPPORTED, &supportedTypes);
Fred Fettingerc50c01e2016-06-14 17:53:10 -05002202 if ((result == 0) && ((supportedTypes & HWC_DISPLAY_VIRTUAL_BIT) != 0)) {
Dan Stozac6998d22015-09-24 17:03:36 -07002203 ALOGI("Found support for HWC virtual displays");
2204 mHwc1SupportsVirtualDisplays = true;
2205 }
2206 }
2207 if (mHwc1MinorVersion >= 4U) {
2208 mCapabilities.insert(Capability::SidebandStream);
2209 }
Fabien Sanglardeb3db612016-11-18 16:12:31 -08002210
2211 // Check for HWC background color layer support.
2212 if (mHwc1MinorVersion >= 1U) {
2213 int backgroundColorSupported = 0;
2214 auto result = mHwc1Device->query(mHwc1Device,
2215 HWC_BACKGROUND_LAYER_SUPPORTED,
2216 &backgroundColorSupported);
2217 if ((result == 0) && (backgroundColorSupported == 1)) {
2218 ALOGV("Found support for HWC background color");
2219 mHwc1SupportsBackgroundColor = true;
2220 }
2221 }
Dan Stozac6998d22015-09-24 17:03:36 -07002222}
2223
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002224HWC2On1Adapter::Display* HWC2On1Adapter::getDisplay(hwc2_display_t id) {
Dan Stozafc4e2022016-02-23 11:43:19 -08002225 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002226
2227 auto display = mDisplays.find(id);
2228 if (display == mDisplays.end()) {
2229 return nullptr;
2230 }
2231
2232 return display->second.get();
2233}
2234
2235std::tuple<HWC2On1Adapter::Layer*, Error> HWC2On1Adapter::getLayer(
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002236 hwc2_display_t displayId, hwc2_layer_t layerId) {
Dan Stozac6998d22015-09-24 17:03:36 -07002237 auto display = getDisplay(displayId);
2238 if (!display) {
2239 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadDisplay);
2240 }
2241
2242 auto layerEntry = mLayers.find(layerId);
2243 if (layerEntry == mLayers.end()) {
2244 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2245 }
2246
2247 auto layer = layerEntry->second;
2248 if (layer->getDisplay().getId() != displayId) {
2249 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer);
2250 }
2251 return std::make_tuple(layer.get(), Error::None);
2252}
2253
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002254void HWC2On1Adapter::populatePrimary() {
Dan Stozafc4e2022016-02-23 11:43:19 -08002255 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002256
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002257 auto display = std::make_shared<Display>(*this, HWC2::DisplayType::Physical);
Dan Stozac6998d22015-09-24 17:03:36 -07002258 mHwc1DisplayMap[HWC_DISPLAY_PRIMARY] = display->getId();
2259 display->setHwc1Id(HWC_DISPLAY_PRIMARY);
2260 display->populateConfigs();
2261 mDisplays.emplace(display->getId(), std::move(display));
2262}
2263
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002264bool HWC2On1Adapter::prepareAllDisplays() {
Dan Stozac6998d22015-09-24 17:03:36 -07002265 ATRACE_CALL();
2266
Dan Stozafc4e2022016-02-23 11:43:19 -08002267 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002268
2269 for (const auto& displayPair : mDisplays) {
2270 auto& display = displayPair.second;
2271 if (!display->prepare()) {
2272 return false;
2273 }
2274 }
2275
Fabien Sanglard7382ed72017-02-11 22:47:36 -08002276 if (mHwc1DisplayMap.count(HWC_DISPLAY_PRIMARY) == 0) {
Dan Stozac6998d22015-09-24 17:03:36 -07002277 ALOGE("prepareAllDisplays: Unable to find primary HWC1 display");
2278 return false;
2279 }
2280
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002281 // Build an array of hwc_display_contents_1 to call prepare() on HWC1.
2282 mHwc1Contents.clear();
2283
Dan Stozac6998d22015-09-24 17:03:36 -07002284 // Always push the primary display
Dan Stozac6998d22015-09-24 17:03:36 -07002285 auto primaryDisplayId = mHwc1DisplayMap[HWC_DISPLAY_PRIMARY];
2286 auto& primaryDisplay = mDisplays[primaryDisplayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002287 mHwc1Contents.push_back(primaryDisplay->getDisplayContents());
Dan Stozac6998d22015-09-24 17:03:36 -07002288
2289 // Push the external display, if present
2290 if (mHwc1DisplayMap.count(HWC_DISPLAY_EXTERNAL) != 0) {
2291 auto externalDisplayId = mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL];
2292 auto& externalDisplay = mDisplays[externalDisplayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002293 mHwc1Contents.push_back(externalDisplay->getDisplayContents());
Dan Stozac6998d22015-09-24 17:03:36 -07002294 } else {
2295 // Even if an external display isn't present, we still need to send
2296 // at least two displays down to HWC1
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002297 mHwc1Contents.push_back(nullptr);
Dan Stozac6998d22015-09-24 17:03:36 -07002298 }
2299
2300 // Push the hardware virtual display, if supported and present
2301 if (mHwc1MinorVersion >= 3) {
2302 if (mHwc1DisplayMap.count(HWC_DISPLAY_VIRTUAL) != 0) {
2303 auto virtualDisplayId = mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL];
2304 auto& virtualDisplay = mDisplays[virtualDisplayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002305 mHwc1Contents.push_back(virtualDisplay->getDisplayContents());
Dan Stozac6998d22015-09-24 17:03:36 -07002306 } else {
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002307 mHwc1Contents.push_back(nullptr);
Dan Stozac6998d22015-09-24 17:03:36 -07002308 }
2309 }
2310
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002311 for (auto& displayContents : mHwc1Contents) {
Dan Stozac6998d22015-09-24 17:03:36 -07002312 if (!displayContents) {
2313 continue;
2314 }
2315
2316 ALOGV("Display %zd layers:", mHwc1Contents.size() - 1);
2317 for (size_t l = 0; l < displayContents->numHwLayers; ++l) {
2318 auto& layer = displayContents->hwLayers[l];
2319 ALOGV(" %zd: %d", l, layer.compositionType);
2320 }
2321 }
2322
2323 ALOGV("Calling HWC1 prepare");
2324 {
2325 ATRACE_NAME("HWC1 prepare");
2326 mHwc1Device->prepare(mHwc1Device, mHwc1Contents.size(),
2327 mHwc1Contents.data());
2328 }
2329
2330 for (size_t c = 0; c < mHwc1Contents.size(); ++c) {
2331 auto& contents = mHwc1Contents[c];
2332 if (!contents) {
2333 continue;
2334 }
2335 ALOGV("Display %zd layers:", c);
2336 for (size_t l = 0; l < contents->numHwLayers; ++l) {
2337 ALOGV(" %zd: %d", l, contents->hwLayers[l].compositionType);
2338 }
2339 }
2340
2341 // Return the received contents to their respective displays
2342 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2343 if (mHwc1Contents[hwc1Id] == nullptr) {
2344 continue;
2345 }
2346
2347 auto displayId = mHwc1DisplayMap[hwc1Id];
2348 auto& display = mDisplays[displayId];
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002349 display->generateChanges();
Dan Stozac6998d22015-09-24 17:03:36 -07002350 }
2351
2352 return true;
2353}
2354
Fabien Sanglardaf5b6b82017-02-23 11:17:11 -08002355void dumpHWC1Message(hwc_composer_device_1* device, size_t numDisplays,
2356 hwc_display_contents_1_t** displays) {
2357 ALOGV("*****************************");
2358 size_t displayId = 0;
2359 while (displayId < numDisplays) {
2360 hwc_display_contents_1_t* display = displays[displayId];
2361
2362 ALOGV("hwc_display_contents_1_t[%zu] @0x%p", displayId, display);
2363 if (display == nullptr) {
2364 displayId++;
2365 continue;
2366 }
2367 ALOGV(" retirefd:0x%08x", display->retireFenceFd);
2368 ALOGV(" outbuf :0x%p", display->outbuf);
2369 ALOGV(" outbuffd:0x%08x", display->outbufAcquireFenceFd);
2370 ALOGV(" flags :0x%08x", display->flags);
2371 for(size_t layerId=0 ; layerId < display->numHwLayers ; layerId++) {
2372 hwc_layer_1_t& layer = display->hwLayers[layerId];
2373 ALOGV(" Layer[%zu]:", layerId);
2374 ALOGV(" composition : 0x%08x", layer.compositionType);
2375 ALOGV(" hints : 0x%08x", layer.hints);
2376 ALOGV(" flags : 0x%08x", layer.flags);
2377 ALOGV(" handle : 0x%p", layer.handle);
2378 ALOGV(" transform : 0x%08x", layer.transform);
2379 ALOGV(" blending : 0x%08x", layer.blending);
2380 ALOGV(" sourceCropf : %f, %f, %f, %f",
2381 layer.sourceCropf.left,
2382 layer.sourceCropf.top,
2383 layer.sourceCropf.right,
2384 layer.sourceCropf.bottom);
2385 ALOGV(" displayFrame : %d, %d, %d, %d",
2386 layer.displayFrame.left,
2387 layer.displayFrame.left,
2388 layer.displayFrame.left,
2389 layer.displayFrame.left);
2390 hwc_region_t& visReg = layer.visibleRegionScreen;
2391 ALOGV(" visibleRegionScreen: #0x%08zx[@0x%p]",
2392 visReg.numRects,
2393 visReg.rects);
2394 for (size_t visRegId=0; visRegId < visReg.numRects ; visRegId++) {
2395 if (layer.visibleRegionScreen.rects == nullptr) {
2396 ALOGV(" null");
2397 } else {
2398 ALOGV(" visibleRegionScreen[%zu] %d, %d, %d, %d",
2399 visRegId,
2400 visReg.rects[visRegId].left,
2401 visReg.rects[visRegId].top,
2402 visReg.rects[visRegId].right,
2403 visReg.rects[visRegId].bottom);
2404 }
2405 }
2406 ALOGV(" acquireFenceFd : 0x%08x", layer.acquireFenceFd);
2407 ALOGV(" releaseFenceFd : 0x%08x", layer.releaseFenceFd);
2408 ALOGV(" planeAlpha : 0x%08x", layer.planeAlpha);
2409 if (getMinorVersion(device) < 5)
2410 continue;
2411 ALOGV(" surfaceDamage : #0x%08zx[@0x%p]",
2412 layer.surfaceDamage.numRects,
2413 layer.surfaceDamage.rects);
2414 for (size_t sdId=0; sdId < layer.surfaceDamage.numRects ; sdId++) {
2415 if (layer.surfaceDamage.rects == nullptr) {
2416 ALOGV(" null");
2417 } else {
2418 ALOGV(" surfaceDamage[%zu] %d, %d, %d, %d",
2419 sdId,
2420 layer.surfaceDamage.rects[sdId].left,
2421 layer.surfaceDamage.rects[sdId].top,
2422 layer.surfaceDamage.rects[sdId].right,
2423 layer.surfaceDamage.rects[sdId].bottom);
2424 }
2425 }
2426 }
2427 displayId++;
2428 }
2429 ALOGV("-----------------------------");
2430}
2431
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002432Error HWC2On1Adapter::setAllDisplays() {
Dan Stozac6998d22015-09-24 17:03:36 -07002433 ATRACE_CALL();
2434
Dan Stozafc4e2022016-02-23 11:43:19 -08002435 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002436
2437 // Make sure we're ready to validate
2438 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2439 if (mHwc1Contents[hwc1Id] == nullptr) {
2440 continue;
2441 }
2442
2443 auto displayId = mHwc1DisplayMap[hwc1Id];
2444 auto& display = mDisplays[displayId];
2445 Error error = display->set(*mHwc1Contents[hwc1Id]);
2446 if (error != Error::None) {
2447 ALOGE("setAllDisplays: Failed to set display %zd: %s", hwc1Id,
2448 to_string(error).c_str());
2449 return error;
2450 }
2451 }
2452
2453 ALOGV("Calling HWC1 set");
2454 {
2455 ATRACE_NAME("HWC1 set");
Fabien Sanglardaf5b6b82017-02-23 11:17:11 -08002456 //dumpHWC1Message(mHwc1Device, mHwc1Contents.size(), mHwc1Contents.data());
Dan Stozac6998d22015-09-24 17:03:36 -07002457 mHwc1Device->set(mHwc1Device, mHwc1Contents.size(),
2458 mHwc1Contents.data());
2459 }
2460
2461 // Add retire and release fences
2462 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) {
2463 if (mHwc1Contents[hwc1Id] == nullptr) {
2464 continue;
2465 }
2466
2467 auto displayId = mHwc1DisplayMap[hwc1Id];
2468 auto& display = mDisplays[displayId];
2469 auto retireFenceFd = mHwc1Contents[hwc1Id]->retireFenceFd;
2470 ALOGV("setAllDisplays: Adding retire fence %d to display %zd",
2471 retireFenceFd, hwc1Id);
2472 display->addRetireFence(mHwc1Contents[hwc1Id]->retireFenceFd);
2473 display->addReleaseFences(*mHwc1Contents[hwc1Id]);
2474 }
2475
2476 return Error::None;
2477}
2478
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002479void HWC2On1Adapter::hwc1Invalidate() {
Dan Stozac6998d22015-09-24 17:03:36 -07002480 ALOGV("Received hwc1Invalidate");
2481
Dan Stozafc4e2022016-02-23 11:43:19 -08002482 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002483
2484 // If the HWC2-side callback hasn't been registered yet, buffer this until
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002485 // it is registered.
Dan Stozac6998d22015-09-24 17:03:36 -07002486 if (mCallbacks.count(Callback::Refresh) == 0) {
2487 mHasPendingInvalidate = true;
2488 return;
2489 }
2490
2491 const auto& callbackInfo = mCallbacks[Callback::Refresh];
2492 std::vector<hwc2_display_t> displays;
2493 for (const auto& displayPair : mDisplays) {
2494 displays.emplace_back(displayPair.first);
2495 }
2496
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002497 // Call back without the state lock held.
Dan Stozac6998d22015-09-24 17:03:36 -07002498 lock.unlock();
2499
2500 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(callbackInfo.pointer);
2501 for (auto display : displays) {
2502 refresh(callbackInfo.data, display);
2503 }
2504}
2505
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002506void HWC2On1Adapter::hwc1Vsync(int hwc1DisplayId, int64_t timestamp) {
Dan Stozac6998d22015-09-24 17:03:36 -07002507 ALOGV("Received hwc1Vsync(%d, %" PRId64 ")", hwc1DisplayId, timestamp);
2508
Dan Stozafc4e2022016-02-23 11:43:19 -08002509 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002510
2511 // If the HWC2-side callback hasn't been registered yet, buffer this until
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002512 // it is registered.
Dan Stozac6998d22015-09-24 17:03:36 -07002513 if (mCallbacks.count(Callback::Vsync) == 0) {
2514 mPendingVsyncs.emplace_back(hwc1DisplayId, timestamp);
2515 return;
2516 }
2517
2518 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2519 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", hwc1DisplayId);
2520 return;
2521 }
2522
2523 const auto& callbackInfo = mCallbacks[Callback::Vsync];
2524 auto displayId = mHwc1DisplayMap[hwc1DisplayId];
2525
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002526 // Call back without the state lock held.
Dan Stozac6998d22015-09-24 17:03:36 -07002527 lock.unlock();
2528
2529 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(callbackInfo.pointer);
2530 vsync(callbackInfo.data, displayId, timestamp);
2531}
2532
Fabien Sanglard06e908a2017-02-12 00:08:14 -08002533void HWC2On1Adapter::hwc1Hotplug(int hwc1DisplayId, int connected) {
Dan Stozac6998d22015-09-24 17:03:36 -07002534 ALOGV("Received hwc1Hotplug(%d, %d)", hwc1DisplayId, connected);
2535
2536 if (hwc1DisplayId != HWC_DISPLAY_EXTERNAL) {
2537 ALOGE("hwc1Hotplug: Received hotplug for non-external display");
2538 return;
2539 }
2540
Dan Stozafc4e2022016-02-23 11:43:19 -08002541 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex);
Dan Stozac6998d22015-09-24 17:03:36 -07002542
2543 // If the HWC2-side callback hasn't been registered yet, buffer this until
2544 // it is registered
2545 if (mCallbacks.count(Callback::Hotplug) == 0) {
2546 mPendingHotplugs.emplace_back(hwc1DisplayId, connected);
2547 return;
2548 }
2549
2550 hwc2_display_t displayId = UINT64_MAX;
2551 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) {
2552 if (connected == 0) {
2553 ALOGW("hwc1Hotplug: Received disconnect for unconnected display");
2554 return;
2555 }
2556
2557 // Create a new display on connect
2558 auto display = std::make_shared<HWC2On1Adapter::Display>(*this,
2559 HWC2::DisplayType::Physical);
2560 display->setHwc1Id(HWC_DISPLAY_EXTERNAL);
2561 display->populateConfigs();
2562 displayId = display->getId();
2563 mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL] = displayId;
2564 mDisplays.emplace(displayId, std::move(display));
2565 } else {
2566 if (connected != 0) {
2567 ALOGW("hwc1Hotplug: Received connect for previously connected "
2568 "display");
2569 return;
2570 }
2571
2572 // Disconnect an existing display
2573 displayId = mHwc1DisplayMap[hwc1DisplayId];
2574 mHwc1DisplayMap.erase(HWC_DISPLAY_EXTERNAL);
2575 mDisplays.erase(displayId);
2576 }
2577
2578 const auto& callbackInfo = mCallbacks[Callback::Hotplug];
2579
2580 // Call back without the state lock held
2581 lock.unlock();
2582
2583 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(callbackInfo.pointer);
2584 auto hwc2Connected = (connected == 0) ?
2585 HWC2::Connection::Disconnected : HWC2::Connection::Connected;
2586 hotplug(callbackInfo.data, displayId, static_cast<int32_t>(hwc2Connected));
2587}
Dan Stozac6998d22015-09-24 17:03:36 -07002588} // namespace android