blob: 133e5f1f6d7749b1d394424853045d4a10365285 [file] [log] [blame]
Mathias Agopiana350ff92010-08-10 17:14:02 -07001/*
2 * Copyright (C) 2010 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
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
18
19#undef LOG_TAG
20#define LOG_TAG "HWComposer"
Mathias Agopian2965b262012-04-08 15:13:32 -070021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070023#include <inttypes.h>
24#include <math.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070025#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070029#include <sys/types.h>
30
31#include <utils/Errors.h>
Mathias Agopianda27af92012-09-13 18:17:13 -070032#include <utils/misc.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
Mathias Agopian83727852010-09-23 18:13:21 -070034#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070035#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070036#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070037#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <ui/GraphicBuffer.h>
40
Mathias Agopiana350ff92010-08-10 17:14:02 -070041#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070042#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070043
Jesse Hall1c569c42013-04-05 13:44:52 -070044#include <android/configuration.h>
45
Mathias Agopiana350ff92010-08-10 17:14:02 -070046#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070047#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070048
Mathias Agopiana350ff92010-08-10 17:14:02 -070049#include "HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080050#include "HWC2On1Adapter.h"
51#include "HWC2.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070052
53#include "../Layer.h" // needed only for debugging
54#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070055
56namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070057
Jesse Hall72960512013-01-10 16:19:56 -080058#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070059
Mathias Agopiana350ff92010-08-10 17:14:02 -070060// ---------------------------------------------------------------------------
61
Dan Stoza9e56aa02015-11-02 13:00:03 -080062HWComposer::HWComposer(const sp<SurfaceFlinger>& flinger)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070063 : mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080064 mAdapter(),
65 mHwcDevice(),
66 mDisplayData(2),
67 mFreeDisplaySlots(),
68 mHwcDisplaySlots(),
69 mCBContext(),
70 mEventHandler(nullptr),
71 mVSyncCounts(),
72 mRemainingHwcVirtualDisplays(0)
Mathias Agopiana350ff92010-08-10 17:14:02 -070073{
Mathias Agopianbef42c52013-08-21 17:45:46 -070074 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
75 mLastHwVSync[i] = 0;
76 mVSyncCounts[i] = 0;
77 }
78
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070079 loadHwcModule();
Mathias Agopiana350ff92010-08-10 17:14:02 -070080}
81
Dan Stoza9e56aa02015-11-02 13:00:03 -080082HWComposer::~HWComposer() {}
83
84void HWComposer::setEventHandler(EventHandler* handler)
85{
86 if (handler == nullptr) {
87 ALOGE("setEventHandler: Rejected attempt to clear handler");
88 return;
Andy McFaddenbabba182012-09-12 13:14:51 -070089 }
Dan Stoza9e56aa02015-11-02 13:00:03 -080090
91 bool wasNull = (mEventHandler == nullptr);
92 mEventHandler = handler;
93
94 if (wasNull) {
95 auto hotplugHook = std::bind(&HWComposer::hotplug, this,
96 std::placeholders::_1, std::placeholders::_2);
97 mHwcDevice->registerHotplugCallback(hotplugHook);
98 auto invalidateHook = std::bind(&HWComposer::invalidate, this,
99 std::placeholders::_1);
100 mHwcDevice->registerRefreshCallback(invalidateHook);
101 auto vsyncHook = std::bind(&HWComposer::vsync, this,
102 std::placeholders::_1, std::placeholders::_2);
103 mHwcDevice->registerVsyncCallback(vsyncHook);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700104 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105}
106
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700107// Load and prepare the hardware composer module. Sets mHwc.
108void HWComposer::loadHwcModule()
109{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 ALOGV("loadHwcModule");
111
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700112 hw_module_t const* module;
113
114 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800115 ALOGE("%s module not found, aborting", HWC_HARDWARE_MODULE_ID);
116 abort();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700117 }
118
Dan Stoza355f6042016-04-14 14:30:41 -0700119 hw_device_t* device = nullptr;
120 int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device);
121 if (error != 0) {
122 ALOGE("Failed to open HWC device (%s), aborting", strerror(-error));
123 abort();
124 }
125
126 uint32_t majorVersion = (device->version >> 24) & 0xF;
127 if (majorVersion == 2) {
128 mHwcDevice = std::make_unique<HWC2::Device>(
129 reinterpret_cast<hwc2_device_t*>(device));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130 } else {
Dan Stoza355f6042016-04-14 14:30:41 -0700131 mAdapter = std::make_unique<HWC2On1Adapter>(
132 reinterpret_cast<hwc_composer_device_1_t*>(device));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800133 uint8_t minorVersion = mAdapter->getHwc1MinorVersion();
134 if (minorVersion < 1) {
135 ALOGE("Cannot adapt to HWC version %d.%d",
136 static_cast<int32_t>((minorVersion >> 8) & 0xF),
137 static_cast<int32_t>(minorVersion & 0xF));
138 abort();
139 }
140 mHwcDevice = std::make_unique<HWC2::Device>(
141 static_cast<hwc2_device_t*>(mAdapter.get()));
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700142 }
143
Dan Stoza9e56aa02015-11-02 13:00:03 -0800144 mRemainingHwcVirtualDisplays = mHwcDevice->getMaxVirtualDisplayCount();
145}
146
147bool HWComposer::isValidDisplay(int32_t displayId) const {
148 return static_cast<size_t>(displayId) < mDisplayData.size() &&
149 mDisplayData[displayId].hwcDisplay;
150}
151
152void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
153 bool valid = true;
154 switch (from) {
155 case HWC2::Composition::Client:
156 valid = false;
157 break;
158 case HWC2::Composition::Device:
159 case HWC2::Composition::SolidColor:
160 valid = (to == HWC2::Composition::Client);
161 break;
162 case HWC2::Composition::Cursor:
163 case HWC2::Composition::Sideband:
164 valid = (to == HWC2::Composition::Client ||
165 to == HWC2::Composition::Device);
166 break;
167 default:
168 break;
169 }
170
171 if (!valid) {
172 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
173 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700174 }
175}
176
Dan Stoza9e56aa02015-11-02 13:00:03 -0800177void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display,
178 HWC2::Connection connected) {
179 ALOGV("hotplug: %" PRIu64 ", %s", display->getId(),
180 to_string(connected).c_str());
181 int32_t disp = 0;
182 if (!mDisplayData[0].hwcDisplay) {
183 ALOGE_IF(connected != HWC2::Connection::Connected, "Assumed primary"
184 " display would be connected");
185 mDisplayData[0].hwcDisplay = display;
186 mHwcDisplaySlots[display->getId()] = 0;
187 disp = DisplayDevice::DISPLAY_PRIMARY;
188 } else {
189 // Disconnect is handled through HWComposer::disconnectDisplay via
190 // SurfaceFlinger's onHotplugReceived callback handling
191 if (connected == HWC2::Connection::Connected) {
192 mDisplayData[1].hwcDisplay = display;
193 mHwcDisplaySlots[display->getId()] = 1;
194 }
195 disp = DisplayDevice::DISPLAY_EXTERNAL;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700196 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800197 mEventHandler->onHotplugReceived(disp,
198 connected == HWC2::Connection::Connected);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700199}
200
Dan Stoza9e56aa02015-11-02 13:00:03 -0800201void HWComposer::invalidate(const std::shared_ptr<HWC2::Display>& /*display*/) {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700202 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700203}
204
Dan Stoza9e56aa02015-11-02 13:00:03 -0800205void HWComposer::vsync(const std::shared_ptr<HWC2::Display>& display,
206 int64_t timestamp) {
207 auto displayType = HWC2::DisplayType::Invalid;
208 auto error = display->getType(&displayType);
209 if (error != HWC2::Error::None) {
210 ALOGE("vsync: Failed to determine type of display %" PRIu64,
211 display->getId());
Jesse Hall1bd20e02012-08-29 10:47:52 -0700212 return;
213 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700214
Dan Stoza9e56aa02015-11-02 13:00:03 -0800215 if (displayType == HWC2::DisplayType::Virtual) {
216 ALOGE("Virtual display %" PRIu64 " passed to vsync callback",
217 display->getId());
218 return;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700219 }
220
Dan Stoza9e56aa02015-11-02 13:00:03 -0800221 if (mHwcDisplaySlots.count(display->getId()) == 0) {
222 ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback",
223 display->getId());
224 return;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700225 }
226
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227 int32_t disp = mHwcDisplaySlots[display->getId()];
228 {
229 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700230
Dan Stoza9e56aa02015-11-02 13:00:03 -0800231 // There have been reports of HWCs that signal several vsync events
232 // with the same timestamp when turning the display off and on. This
233 // is a bug in the HWC implementation, but filter the extra events
234 // out here so they don't cause havoc downstream.
235 if (timestamp == mLastHwVSync[disp]) {
236 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
237 timestamp);
238 return;
239 }
240
241 mLastHwVSync[disp] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700242 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243
244 char tag[16];
245 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
246 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
247
248 mEventHandler->onVSyncReceived(disp, timestamp);
Jesse Hall1c569c42013-04-05 13:44:52 -0700249}
250
Dan Stoza9e56aa02015-11-02 13:00:03 -0800251status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700252 android_pixel_format_t* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800253 if (mRemainingHwcVirtualDisplays == 0) {
254 ALOGE("allocateVirtualDisplay: No remaining virtual displays");
Mathias Agopiane60b0682012-08-21 23:34:09 -0700255 return NO_MEMORY;
256 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700257
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258 std::shared_ptr<HWC2::Display> display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700259 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
260 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800261 if (error != HWC2::Error::None) {
262 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display");
263 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700264 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800265
266 size_t displaySlot = 0;
267 if (!mFreeDisplaySlots.empty()) {
268 displaySlot = *mFreeDisplaySlots.begin();
269 mFreeDisplaySlots.erase(displaySlot);
270 } else if (mDisplayData.size() < INT32_MAX) {
271 // Don't bother allocating a slot larger than we can return
272 displaySlot = mDisplayData.size();
273 mDisplayData.resize(displaySlot + 1);
274 } else {
275 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot");
276 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700277 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800278
279 mDisplayData[displaySlot].hwcDisplay = display;
280
281 --mRemainingHwcVirtualDisplays;
282 *outId = static_cast<int32_t>(displaySlot);
283
Mathias Agopiane60b0682012-08-21 23:34:09 -0700284 return NO_ERROR;
285}
286
Dan Stoza9e56aa02015-11-02 13:00:03 -0800287std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) {
288 if (!isValidDisplay(displayId)) {
289 ALOGE("Failed to create layer on invalid display %d", displayId);
290 return nullptr;
291 }
292 auto display = mDisplayData[displayId].hwcDisplay;
293 std::shared_ptr<HWC2::Layer> layer;
294 auto error = display->createLayer(&layer);
295 if (error != HWC2::Error::None) {
296 ALOGE("Failed to create layer on display %d: %s (%d)", displayId,
297 to_string(error).c_str(), static_cast<int32_t>(error));
298 return nullptr;
299 }
300 return layer;
301}
302
303nsecs_t HWComposer::getRefreshTimestamp(int32_t disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700304 // this returns the last refresh timestamp.
305 // if the last one is not available, we estimate it based on
306 // the refresh period and whatever closest timestamp we have.
307 Mutex::Autolock _l(mLock);
308 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800309 auto vsyncPeriod = getActiveConfig(disp)->getVsyncPeriod();
310 return now - ((now - mLastHwVSync[disp]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700311}
312
Dan Stoza9e56aa02015-11-02 13:00:03 -0800313bool HWComposer::isConnected(int32_t disp) const {
314 if (!isValidDisplay(disp)) {
315 ALOGE("isConnected: Attempted to access invalid display %d", disp);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700316 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317 }
318 return mDisplayData[disp].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700319}
320
Dan Stoza9e56aa02015-11-02 13:00:03 -0800321std::vector<std::shared_ptr<const HWC2::Display::Config>>
322 HWComposer::getConfigs(int32_t displayId) const {
323 if (!isValidDisplay(displayId)) {
324 ALOGE("getConfigs: Attempted to access invalid display %d", displayId);
325 return {};
326 }
327 auto& displayData = mDisplayData[displayId];
328 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
329 if (displayData.configMap.empty()) {
330 for (size_t i = 0; i < configs.size(); ++i) {
331 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700332 }
333 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800334 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700335}
336
Dan Stoza9e56aa02015-11-02 13:00:03 -0800337std::shared_ptr<const HWC2::Display::Config>
338 HWComposer::getActiveConfig(int32_t displayId) const {
339 if (!isValidDisplay(displayId)) {
340 ALOGE("getActiveConfigs: Attempted to access invalid display %d",
341 displayId);
342 return nullptr;
Mathias Agopian58959342010-10-07 14:57:04 -0700343 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800344 std::shared_ptr<const HWC2::Display::Config> config;
345 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
346 if (error == HWC2::Error::BadConfig) {
347 ALOGV("getActiveConfig: No config active, returning null");
348 return nullptr;
349 } else if (error != HWC2::Error::None) {
350 ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId,
351 to_string(error).c_str(), static_cast<int32_t>(error));
352 return nullptr;
353 } else if (!config) {
354 ALOGE("getActiveConfig returned an unknown config for display %d",
355 displayId);
356 return nullptr;
357 }
358
359 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700360}
361
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600362std::vector<int32_t> HWComposer::getColorModes(int32_t displayId) const {
363 std::vector<int32_t> modes;
364
365 if (!isValidDisplay(displayId)) {
366 ALOGE("getColorModes: Attempted to access invalid display %d",
367 displayId);
368 return modes;
369 }
370 const std::shared_ptr<HWC2::Display>& hwcDisplay =
371 mDisplayData[displayId].hwcDisplay;
372
373 auto error = hwcDisplay->getColorModes(&modes);
374 if (error != HWC2::Error::None) {
375 ALOGE("getColorModes failed for display %d: %s (%d)", displayId,
376 to_string(error).c_str(), static_cast<int32_t>(error));
377 return std::vector<int32_t>();
378 }
379
380 return modes;
381}
382
Dan Stoza9e56aa02015-11-02 13:00:03 -0800383void HWComposer::setVsyncEnabled(int32_t disp, HWC2::Vsync enabled) {
384 if (disp < 0 || disp >= HWC_DISPLAY_VIRTUAL) {
385 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", disp);
386 return;
387 }
388
389 if (!isValidDisplay(disp)) {
390 ALOGE("setVsyncEnabled: Attempted to access invalid display %d", disp);
391 return;
392 }
393
394 // NOTE: we use our own internal lock here because we have to call
395 // into the HWC with the lock held, and we want to make sure
396 // that even if HWC blocks (which it shouldn't), it won't
397 // affect other threads.
398 Mutex::Autolock _l(mVsyncLock);
399 auto& displayData = mDisplayData[disp];
400 if (enabled != displayData.vsyncEnabled) {
401 ATRACE_CALL();
402 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
403 if (error == HWC2::Error::None) {
404 displayData.vsyncEnabled = enabled;
405
406 char tag[16];
407 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
408 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700409 } else {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800410 ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64
411 ": %s (%d)", to_string(enabled).c_str(), disp,
412 mDisplayData[disp].hwcDisplay->getId(),
413 to_string(error).c_str(), static_cast<int32_t>(error));
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700414 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700415 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700416}
417
Dan Stoza9e56aa02015-11-02 13:00:03 -0800418status_t HWComposer::setClientTarget(int32_t displayId,
419 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
420 android_dataspace_t dataspace) {
421 if (!isValidDisplay(displayId)) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700422 return BAD_INDEX;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800423 }
Jesse Hall851cfe82013-03-20 13:44:00 -0700424
Dan Stoza9e56aa02015-11-02 13:00:03 -0800425 ALOGV("setClientTarget for display %d", displayId);
426 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
427 buffer_handle_t handle = nullptr;
428 if ((target != nullptr) && target->getNativeBuffer()) {
429 handle = target->getNativeBuffer()->handle;
430 }
431 auto error = hwcDisplay->setClientTarget(handle, acquireFence, dataspace);
432 if (error != HWC2::Error::None) {
433 ALOGE("Failed to set client target for display %d: %s (%d)", displayId,
434 to_string(error).c_str(), static_cast<int32_t>(error));
435 return BAD_VALUE;
436 }
437
Jesse Hall851cfe82013-03-20 13:44:00 -0700438 return NO_ERROR;
439}
440
Dan Stoza9e56aa02015-11-02 13:00:03 -0800441status_t HWComposer::prepare(DisplayDevice& displayDevice) {
442 ATRACE_CALL();
443
444 Mutex::Autolock _l(mDisplayLock);
445 auto displayId = displayDevice.getHwcDisplayId();
446 if (!isValidDisplay(displayId)) {
447 return BAD_INDEX;
448 }
449
450 auto& displayData = mDisplayData[displayId];
451 auto& hwcDisplay = displayData.hwcDisplay;
452 if (!hwcDisplay->isConnected()) {
453 return NO_ERROR;
454 }
455
456 uint32_t numTypes = 0;
457 uint32_t numRequests = 0;
458 auto error = hwcDisplay->validate(&numTypes, &numRequests);
459 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) {
460 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId,
461 to_string(error).c_str(), static_cast<int32_t>(error));
462 return BAD_INDEX;
463 }
464
465 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition>
466 changedTypes;
467 changedTypes.reserve(numTypes);
468 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
469 if (error != HWC2::Error::None) {
470 ALOGE("prepare: getChangedCompositionTypes failed on display %d: "
471 "%s (%d)", displayId, to_string(error).c_str(),
472 static_cast<int32_t>(error));
473 return BAD_INDEX;
474 }
475
476
477 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
478 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest>
479 layerRequests;
480 layerRequests.reserve(numRequests);
481 error = hwcDisplay->getRequests(&displayData.displayRequests,
482 &layerRequests);
483 if (error != HWC2::Error::None) {
484 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId,
485 to_string(error).c_str(), static_cast<int32_t>(error));
486 return BAD_INDEX;
487 }
488
489 displayData.hasClientComposition = false;
490 displayData.hasDeviceComposition = false;
491 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) {
492 auto hwcLayer = layer->getHwcLayer(displayId);
493
494 if (changedTypes.count(hwcLayer) != 0) {
495 // We pass false so we only update our state and don't call back
496 // into the HWC device
497 validateChange(layer->getCompositionType(displayId),
498 changedTypes[hwcLayer]);
499 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
500 }
501
502 switch (layer->getCompositionType(displayId)) {
503 case HWC2::Composition::Client:
504 displayData.hasClientComposition = true;
505 break;
506 case HWC2::Composition::Device:
507 case HWC2::Composition::SolidColor:
508 case HWC2::Composition::Cursor:
509 case HWC2::Composition::Sideband:
510 displayData.hasDeviceComposition = true;
511 break;
512 default:
513 break;
514 }
515
516 if (layerRequests.count(hwcLayer) != 0 &&
517 layerRequests[hwcLayer] ==
518 HWC2::LayerRequest::ClearClientTarget) {
519 layer->setClearClientTarget(displayId, true);
520 } else {
521 if (layerRequests.count(hwcLayer) != 0) {
522 ALOGE("prepare: Unknown layer request: %s",
523 to_string(layerRequests[hwcLayer]).c_str());
524 }
525 layer->setClearClientTarget(displayId, false);
526 }
527 }
528
529 error = hwcDisplay->acceptChanges();
530 if (error != HWC2::Error::None) {
531 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str());
532 return BAD_INDEX;
533 }
534
535 return NO_ERROR;
536}
537
538bool HWComposer::hasDeviceComposition(int32_t displayId) const {
539 if (!isValidDisplay(displayId)) {
540 ALOGE("hasDeviceComposition: Invalid display %d", displayId);
541 return false;
542 }
543 return mDisplayData[displayId].hasDeviceComposition;
544}
545
546bool HWComposer::hasClientComposition(int32_t displayId) const {
547 if (!isValidDisplay(displayId)) {
548 ALOGE("hasClientComposition: Invalid display %d", displayId);
549 return true;
550 }
551 return mDisplayData[displayId].hasClientComposition;
552}
553
554sp<Fence> HWComposer::getRetireFence(int32_t displayId) const {
555 if (!isValidDisplay(displayId)) {
556 ALOGE("getRetireFence failed for invalid display %d", displayId);
Jesse Hall851cfe82013-03-20 13:44:00 -0700557 return Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558 }
559 return mDisplayData[displayId].lastRetireFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700560}
561
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
563 const std::shared_ptr<HWC2::Layer>& layer) const {
564 if (!isValidDisplay(displayId)) {
565 ALOGE("getLayerReleaseFence: Invalid display");
566 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700567 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568 auto displayFences = mDisplayData[displayId].releaseFences;
569 if (displayFences.count(layer) == 0) {
570 ALOGV("getLayerReleaseFence: Release fence not found");
571 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700572 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700574}
575
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576status_t HWComposer::commit(int32_t displayId) {
577 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700578
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579 if (!isValidDisplay(displayId)) {
580 return BAD_INDEX;
Mathias Agopianc3973602012-08-31 17:51:25 -0700581 }
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700582
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583 auto& displayData = mDisplayData[displayId];
584 auto& hwcDisplay = displayData.hwcDisplay;
585 auto error = hwcDisplay->present(&displayData.lastRetireFence);
586 if (error != HWC2::Error::None) {
587 ALOGE("commit: present failed for display %d: %s (%d)", displayId,
588 to_string(error).c_str(), static_cast<int32_t>(error));
589 return UNKNOWN_ERROR;
590 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700591
Dan Stoza9e56aa02015-11-02 13:00:03 -0800592 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences;
593 error = hwcDisplay->getReleaseFences(&releaseFences);
594 if (error != HWC2::Error::None) {
595 ALOGE("commit: Failed to get release fences for display %d: %s (%d)",
596 displayId, to_string(error).c_str(),
597 static_cast<int32_t>(error));
598 return UNKNOWN_ERROR;
Mathias Agopianf4358632012-08-22 17:16:19 -0700599 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600
601 displayData.releaseFences = std::move(releaseFences);
602
603 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700604}
605
Dan Stoza9e56aa02015-11-02 13:00:03 -0800606status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
607 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
608 if (!isValidDisplay(displayId)) {
609 ALOGE("setPowerMode: Bad display");
610 return BAD_INDEX;
611 }
612 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) {
613 ALOGE("setPowerMode: Virtual display %d passed in, returning",
614 displayId);
615 return BAD_INDEX;
616 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700617
Dan Stoza9e56aa02015-11-02 13:00:03 -0800618 auto mode = static_cast<HWC2::PowerMode>(intMode);
619 if (mode == HWC2::PowerMode::Off) {
620 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
621 }
622
623 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
624 switch (mode) {
625 case HWC2::PowerMode::Off:
626 case HWC2::PowerMode::On:
627 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
628 {
629 auto error = hwcDisplay->setPowerMode(mode);
630 if (error != HWC2::Error::None) {
631 ALOGE("setPowerMode: Unable to set power mode %s for "
632 "display %d: %s (%d)", to_string(mode).c_str(),
633 displayId, to_string(error).c_str(),
634 static_cast<int32_t>(error));
Mathias Agopianda27af92012-09-13 18:17:13 -0700635 }
636 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637 break;
638 case HWC2::PowerMode::Doze:
639 case HWC2::PowerMode::DozeSuspend:
640 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
641 {
642 bool supportsDoze = false;
643 auto error = hwcDisplay->supportsDoze(&supportsDoze);
644 if (error != HWC2::Error::None) {
645 ALOGE("setPowerMode: Unable to query doze support for "
646 "display %d: %s (%d)", displayId,
647 to_string(error).c_str(),
648 static_cast<int32_t>(error));
649 }
650 if (!supportsDoze) {
651 mode = HWC2::PowerMode::On;
652 }
653
654 error = hwcDisplay->setPowerMode(mode);
655 if (error != HWC2::Error::None) {
656 ALOGE("setPowerMode: Unable to set power mode %s for "
657 "display %d: %s (%d)", to_string(mode).c_str(),
658 displayId, to_string(error).c_str(),
659 static_cast<int32_t>(error));
660 }
661 }
662 break;
663 default:
664 ALOGV("setPowerMode: Not calling HWC");
665 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700666 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667
668 return NO_ERROR;
669}
670
671status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
672 if (!isValidDisplay(displayId)) {
673 ALOGE("setActiveConfig: Display %d is not valid", displayId);
674 return BAD_INDEX;
675 }
676
677 auto& displayData = mDisplayData[displayId];
678 if (displayData.configMap.count(configId) == 0) {
679 ALOGE("setActiveConfig: Invalid config %zd", configId);
680 return BAD_INDEX;
681 }
682
683 auto error = displayData.hwcDisplay->setActiveConfig(
684 displayData.configMap[configId]);
685 if (error != HWC2::Error::None) {
686 ALOGE("setActiveConfig: Failed to set config %zu on display %d: "
687 "%s (%d)", configId, displayId, to_string(error).c_str(),
688 static_cast<int32_t>(error));
689 return UNKNOWN_ERROR;
690 }
691
692 return NO_ERROR;
693}
694
695void HWComposer::disconnectDisplay(int displayId) {
696 LOG_ALWAYS_FATAL_IF(displayId < 0);
697 auto& displayData = mDisplayData[displayId];
698
699 auto displayType = HWC2::DisplayType::Invalid;
700 auto error = displayData.hwcDisplay->getType(&displayType);
701 if (error != HWC2::Error::None) {
702 ALOGE("disconnectDisplay: Failed to determine type of display %d",
703 displayId);
704 return;
705 }
706
707 // If this was a virtual display, add its slot back for reuse by future
708 // virtual displays
709 if (displayType == HWC2::DisplayType::Virtual) {
710 mFreeDisplaySlots.insert(displayId);
711 ++mRemainingHwcVirtualDisplays;
712 }
713
714 auto hwcId = displayData.hwcDisplay->getId();
715 mHwcDisplaySlots.erase(hwcId);
716 displayData.reset();
717}
718
719status_t HWComposer::setOutputBuffer(int32_t displayId,
720 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
721 if (!isValidDisplay(displayId)) {
722 ALOGE("setOutputBuffer: Display %d is not valid", displayId);
723 return BAD_INDEX;
724 }
725
726 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
727 auto displayType = HWC2::DisplayType::Invalid;
728 auto error = hwcDisplay->getType(&displayType);
729 if (error != HWC2::Error::None) {
730 ALOGE("setOutputBuffer: Failed to determine type of display %d",
731 displayId);
732 return NAME_NOT_FOUND;
733 }
734
735 if (displayType != HWC2::DisplayType::Virtual) {
736 ALOGE("setOutputBuffer: Display %d is not virtual", displayId);
737 return INVALID_OPERATION;
738 }
739
740 error = hwcDisplay->setOutputBuffer(buffer, acquireFence);
741 if (error != HWC2::Error::None) {
742 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)",
743 displayId, to_string(error).c_str(),
744 static_cast<int32_t>(error));
745 return UNKNOWN_ERROR;
746 }
747
748 return NO_ERROR;
749}
750
751void HWComposer::clearReleaseFences(int32_t displayId) {
752 if (!isValidDisplay(displayId)) {
753 ALOGE("clearReleaseFences: Display %d is not valid", displayId);
754 return;
755 }
756 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700757}
758
Dan Stozac4f471e2016-03-24 09:31:08 -0700759std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities(
760 int32_t displayId) {
761 if (!isValidDisplay(displayId)) {
762 ALOGE("getHdrCapabilities: Display %d is not valid", displayId);
763 return nullptr;
764 }
765
766 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
767 std::unique_ptr<HdrCapabilities> capabilities;
768 auto error = hwcDisplay->getHdrCapabilities(&capabilities);
769 if (error != HWC2::Error::None) {
770 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:"
771 " %s (%d)", displayId, to_string(error).c_str(),
772 static_cast<int32_t>(error));
773 return nullptr;
774 }
775
776 return capabilities;
777}
778
Andy McFadden4df87bd2014-04-21 18:08:54 -0700779// Converts a PixelFormat to a human-readable string. Max 11 chars.
780// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700782static String8 getFormatStr(PixelFormat format) {
783 switch (format) {
784 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
785 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
786 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
787 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
788 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700789 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
790 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700791 default:
792 String8 result;
793 result.appendFormat("? %08x", format);
794 return result;
795 }
796}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700798
Mathias Agopian74d211a2013-04-22 16:55:35 +0200799void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800800 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
801 // all the state going into the layers. This is probably better done in
802 // Layer itself, but it's going to take a bit of work to get there.
803 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700804}
805
Mathias Agopiana350ff92010-08-10 17:14:02 -0700806// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700807
Jesse Halla9a1b002013-02-27 16:39:25 -0800808HWComposer::DisplayData::DisplayData()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800809 : hasClientComposition(false),
810 hasDeviceComposition(false),
811 hwcDisplay(),
812 lastRetireFence(Fence::NO_FENCE),
813 outbufHandle(nullptr),
814 outbufAcquireFence(Fence::NO_FENCE),
815 vsyncEnabled(HWC2::Vsync::Disable) {
816 ALOGV("Created new DisplayData");
817}
Jesse Halla9a1b002013-02-27 16:39:25 -0800818
819HWComposer::DisplayData::~DisplayData() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800820}
821
822void HWComposer::DisplayData::reset() {
823 ALOGV("DisplayData reset");
824 *this = DisplayData();
Jesse Halla9a1b002013-02-27 16:39:25 -0800825}
826
Mathias Agopian2965b262012-04-08 15:13:32 -0700827// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700828}; // namespace android