blob: 4ac58c59845919a57172868918740ed1d05ea352 [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
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700147bool HWComposer::hasCapability(HWC2::Capability capability) const
148{
149 return mHwcDevice->getCapabilities().count(capability) > 0;
150}
151
Dan Stoza9e56aa02015-11-02 13:00:03 -0800152bool HWComposer::isValidDisplay(int32_t displayId) const {
153 return static_cast<size_t>(displayId) < mDisplayData.size() &&
154 mDisplayData[displayId].hwcDisplay;
155}
156
157void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
158 bool valid = true;
159 switch (from) {
160 case HWC2::Composition::Client:
161 valid = false;
162 break;
163 case HWC2::Composition::Device:
164 case HWC2::Composition::SolidColor:
165 valid = (to == HWC2::Composition::Client);
166 break;
167 case HWC2::Composition::Cursor:
168 case HWC2::Composition::Sideband:
169 valid = (to == HWC2::Composition::Client ||
170 to == HWC2::Composition::Device);
171 break;
172 default:
173 break;
174 }
175
176 if (!valid) {
177 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
178 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700179 }
180}
181
Dan Stoza9e56aa02015-11-02 13:00:03 -0800182void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display,
183 HWC2::Connection connected) {
184 ALOGV("hotplug: %" PRIu64 ", %s", display->getId(),
185 to_string(connected).c_str());
186 int32_t disp = 0;
187 if (!mDisplayData[0].hwcDisplay) {
188 ALOGE_IF(connected != HWC2::Connection::Connected, "Assumed primary"
189 " display would be connected");
190 mDisplayData[0].hwcDisplay = display;
191 mHwcDisplaySlots[display->getId()] = 0;
192 disp = DisplayDevice::DISPLAY_PRIMARY;
193 } else {
194 // Disconnect is handled through HWComposer::disconnectDisplay via
195 // SurfaceFlinger's onHotplugReceived callback handling
196 if (connected == HWC2::Connection::Connected) {
197 mDisplayData[1].hwcDisplay = display;
198 mHwcDisplaySlots[display->getId()] = 1;
199 }
200 disp = DisplayDevice::DISPLAY_EXTERNAL;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700201 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800202 mEventHandler->onHotplugReceived(disp,
203 connected == HWC2::Connection::Connected);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700204}
205
Dan Stoza9e56aa02015-11-02 13:00:03 -0800206void HWComposer::invalidate(const std::shared_ptr<HWC2::Display>& /*display*/) {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700207 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700208}
209
Dan Stoza9e56aa02015-11-02 13:00:03 -0800210void HWComposer::vsync(const std::shared_ptr<HWC2::Display>& display,
211 int64_t timestamp) {
212 auto displayType = HWC2::DisplayType::Invalid;
213 auto error = display->getType(&displayType);
214 if (error != HWC2::Error::None) {
215 ALOGE("vsync: Failed to determine type of display %" PRIu64,
216 display->getId());
Jesse Hall1bd20e02012-08-29 10:47:52 -0700217 return;
218 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700219
Dan Stoza9e56aa02015-11-02 13:00:03 -0800220 if (displayType == HWC2::DisplayType::Virtual) {
221 ALOGE("Virtual display %" PRIu64 " passed to vsync callback",
222 display->getId());
223 return;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700224 }
225
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226 if (mHwcDisplaySlots.count(display->getId()) == 0) {
227 ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback",
228 display->getId());
229 return;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700230 }
231
Dan Stoza9e56aa02015-11-02 13:00:03 -0800232 int32_t disp = mHwcDisplaySlots[display->getId()];
233 {
234 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700235
Dan Stoza9e56aa02015-11-02 13:00:03 -0800236 // There have been reports of HWCs that signal several vsync events
237 // with the same timestamp when turning the display off and on. This
238 // is a bug in the HWC implementation, but filter the extra events
239 // out here so they don't cause havoc downstream.
240 if (timestamp == mLastHwVSync[disp]) {
241 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
242 timestamp);
243 return;
244 }
245
246 mLastHwVSync[disp] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700247 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800248
249 char tag[16];
250 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
251 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
252
253 mEventHandler->onVSyncReceived(disp, timestamp);
Jesse Hall1c569c42013-04-05 13:44:52 -0700254}
255
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700257 android_pixel_format_t* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258 if (mRemainingHwcVirtualDisplays == 0) {
259 ALOGE("allocateVirtualDisplay: No remaining virtual displays");
Mathias Agopiane60b0682012-08-21 23:34:09 -0700260 return NO_MEMORY;
261 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700262
Dan Stoza9e56aa02015-11-02 13:00:03 -0800263 std::shared_ptr<HWC2::Display> display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700264 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
265 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800266 if (error != HWC2::Error::None) {
267 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display");
268 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700269 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800270
271 size_t displaySlot = 0;
272 if (!mFreeDisplaySlots.empty()) {
273 displaySlot = *mFreeDisplaySlots.begin();
274 mFreeDisplaySlots.erase(displaySlot);
275 } else if (mDisplayData.size() < INT32_MAX) {
276 // Don't bother allocating a slot larger than we can return
277 displaySlot = mDisplayData.size();
278 mDisplayData.resize(displaySlot + 1);
279 } else {
280 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot");
281 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700282 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800283
284 mDisplayData[displaySlot].hwcDisplay = display;
285
286 --mRemainingHwcVirtualDisplays;
287 *outId = static_cast<int32_t>(displaySlot);
288
Mathias Agopiane60b0682012-08-21 23:34:09 -0700289 return NO_ERROR;
290}
291
Dan Stoza9e56aa02015-11-02 13:00:03 -0800292std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) {
293 if (!isValidDisplay(displayId)) {
294 ALOGE("Failed to create layer on invalid display %d", displayId);
295 return nullptr;
296 }
297 auto display = mDisplayData[displayId].hwcDisplay;
298 std::shared_ptr<HWC2::Layer> layer;
299 auto error = display->createLayer(&layer);
300 if (error != HWC2::Error::None) {
301 ALOGE("Failed to create layer on display %d: %s (%d)", displayId,
302 to_string(error).c_str(), static_cast<int32_t>(error));
303 return nullptr;
304 }
305 return layer;
306}
307
308nsecs_t HWComposer::getRefreshTimestamp(int32_t disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700309 // this returns the last refresh timestamp.
310 // if the last one is not available, we estimate it based on
311 // the refresh period and whatever closest timestamp we have.
312 Mutex::Autolock _l(mLock);
313 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314 auto vsyncPeriod = getActiveConfig(disp)->getVsyncPeriod();
315 return now - ((now - mLastHwVSync[disp]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700316}
317
Dan Stoza9e56aa02015-11-02 13:00:03 -0800318bool HWComposer::isConnected(int32_t disp) const {
319 if (!isValidDisplay(disp)) {
320 ALOGE("isConnected: Attempted to access invalid display %d", disp);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700321 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800322 }
323 return mDisplayData[disp].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700324}
325
Dan Stoza9e56aa02015-11-02 13:00:03 -0800326std::vector<std::shared_ptr<const HWC2::Display::Config>>
327 HWComposer::getConfigs(int32_t displayId) const {
328 if (!isValidDisplay(displayId)) {
329 ALOGE("getConfigs: Attempted to access invalid display %d", displayId);
330 return {};
331 }
332 auto& displayData = mDisplayData[displayId];
333 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
334 if (displayData.configMap.empty()) {
335 for (size_t i = 0; i < configs.size(); ++i) {
336 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700337 }
338 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800339 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700340}
341
Dan Stoza9e56aa02015-11-02 13:00:03 -0800342std::shared_ptr<const HWC2::Display::Config>
343 HWComposer::getActiveConfig(int32_t displayId) const {
344 if (!isValidDisplay(displayId)) {
345 ALOGE("getActiveConfigs: Attempted to access invalid display %d",
346 displayId);
347 return nullptr;
Mathias Agopian58959342010-10-07 14:57:04 -0700348 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800349 std::shared_ptr<const HWC2::Display::Config> config;
350 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
351 if (error == HWC2::Error::BadConfig) {
352 ALOGV("getActiveConfig: No config active, returning null");
353 return nullptr;
354 } else if (error != HWC2::Error::None) {
355 ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId,
356 to_string(error).c_str(), static_cast<int32_t>(error));
357 return nullptr;
358 } else if (!config) {
359 ALOGE("getActiveConfig returned an unknown config for display %d",
360 displayId);
361 return nullptr;
362 }
363
364 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700365}
366
Michael Wright28f24d02016-07-12 13:30:53 -0700367std::vector<android_color_mode_t> HWComposer::getColorModes(int32_t displayId) const {
368 std::vector<android_color_mode_t> modes;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600369
370 if (!isValidDisplay(displayId)) {
371 ALOGE("getColorModes: Attempted to access invalid display %d",
372 displayId);
373 return modes;
374 }
375 const std::shared_ptr<HWC2::Display>& hwcDisplay =
376 mDisplayData[displayId].hwcDisplay;
377
378 auto error = hwcDisplay->getColorModes(&modes);
379 if (error != HWC2::Error::None) {
380 ALOGE("getColorModes failed for display %d: %s (%d)", displayId,
381 to_string(error).c_str(), static_cast<int32_t>(error));
Michael Wright28f24d02016-07-12 13:30:53 -0700382 return std::vector<android_color_mode_t>();
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600383 }
384
385 return modes;
386}
387
Michael Wright28f24d02016-07-12 13:30:53 -0700388status_t HWComposer::setActiveColorMode(int32_t displayId, android_color_mode_t mode) {
389 if (!isValidDisplay(displayId)) {
390 ALOGE("setActiveColorMode: Display %d is not valid", displayId);
391 return BAD_INDEX;
392 }
393
394 auto& displayData = mDisplayData[displayId];
395 auto error = displayData.hwcDisplay->setColorMode(mode);
396 if (error != HWC2::Error::None) {
397 ALOGE("setActiveConfig: Failed to set color mode %d on display %d: "
398 "%s (%d)", mode, displayId, to_string(error).c_str(),
399 static_cast<int32_t>(error));
400 return UNKNOWN_ERROR;
401 }
402
403 return NO_ERROR;
404}
405
406
Dan Stoza9e56aa02015-11-02 13:00:03 -0800407void HWComposer::setVsyncEnabled(int32_t disp, HWC2::Vsync enabled) {
408 if (disp < 0 || disp >= HWC_DISPLAY_VIRTUAL) {
409 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", disp);
410 return;
411 }
412
413 if (!isValidDisplay(disp)) {
414 ALOGE("setVsyncEnabled: Attempted to access invalid display %d", disp);
415 return;
416 }
417
418 // NOTE: we use our own internal lock here because we have to call
419 // into the HWC with the lock held, and we want to make sure
420 // that even if HWC blocks (which it shouldn't), it won't
421 // affect other threads.
422 Mutex::Autolock _l(mVsyncLock);
423 auto& displayData = mDisplayData[disp];
424 if (enabled != displayData.vsyncEnabled) {
425 ATRACE_CALL();
426 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
427 if (error == HWC2::Error::None) {
428 displayData.vsyncEnabled = enabled;
429
430 char tag[16];
431 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
432 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700433 } else {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800434 ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64
435 ": %s (%d)", to_string(enabled).c_str(), disp,
436 mDisplayData[disp].hwcDisplay->getId(),
437 to_string(error).c_str(), static_cast<int32_t>(error));
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700438 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700439 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700440}
441
Dan Stoza9e56aa02015-11-02 13:00:03 -0800442status_t HWComposer::setClientTarget(int32_t displayId,
443 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
444 android_dataspace_t dataspace) {
445 if (!isValidDisplay(displayId)) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700446 return BAD_INDEX;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800447 }
Jesse Hall851cfe82013-03-20 13:44:00 -0700448
Dan Stoza9e56aa02015-11-02 13:00:03 -0800449 ALOGV("setClientTarget for display %d", displayId);
450 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
451 buffer_handle_t handle = nullptr;
452 if ((target != nullptr) && target->getNativeBuffer()) {
453 handle = target->getNativeBuffer()->handle;
454 }
455 auto error = hwcDisplay->setClientTarget(handle, acquireFence, dataspace);
456 if (error != HWC2::Error::None) {
457 ALOGE("Failed to set client target for display %d: %s (%d)", displayId,
458 to_string(error).c_str(), static_cast<int32_t>(error));
459 return BAD_VALUE;
460 }
461
Jesse Hall851cfe82013-03-20 13:44:00 -0700462 return NO_ERROR;
463}
464
Dan Stoza9e56aa02015-11-02 13:00:03 -0800465status_t HWComposer::prepare(DisplayDevice& displayDevice) {
466 ATRACE_CALL();
467
468 Mutex::Autolock _l(mDisplayLock);
469 auto displayId = displayDevice.getHwcDisplayId();
470 if (!isValidDisplay(displayId)) {
471 return BAD_INDEX;
472 }
473
474 auto& displayData = mDisplayData[displayId];
475 auto& hwcDisplay = displayData.hwcDisplay;
476 if (!hwcDisplay->isConnected()) {
477 return NO_ERROR;
478 }
479
480 uint32_t numTypes = 0;
481 uint32_t numRequests = 0;
482 auto error = hwcDisplay->validate(&numTypes, &numRequests);
483 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) {
484 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId,
485 to_string(error).c_str(), static_cast<int32_t>(error));
486 return BAD_INDEX;
487 }
488
489 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition>
490 changedTypes;
491 changedTypes.reserve(numTypes);
492 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
493 if (error != HWC2::Error::None) {
494 ALOGE("prepare: getChangedCompositionTypes failed on display %d: "
495 "%s (%d)", displayId, to_string(error).c_str(),
496 static_cast<int32_t>(error));
497 return BAD_INDEX;
498 }
499
500
501 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
502 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest>
503 layerRequests;
504 layerRequests.reserve(numRequests);
505 error = hwcDisplay->getRequests(&displayData.displayRequests,
506 &layerRequests);
507 if (error != HWC2::Error::None) {
508 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId,
509 to_string(error).c_str(), static_cast<int32_t>(error));
510 return BAD_INDEX;
511 }
512
513 displayData.hasClientComposition = false;
514 displayData.hasDeviceComposition = false;
515 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) {
516 auto hwcLayer = layer->getHwcLayer(displayId);
517
518 if (changedTypes.count(hwcLayer) != 0) {
519 // We pass false so we only update our state and don't call back
520 // into the HWC device
521 validateChange(layer->getCompositionType(displayId),
522 changedTypes[hwcLayer]);
523 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
524 }
525
526 switch (layer->getCompositionType(displayId)) {
527 case HWC2::Composition::Client:
528 displayData.hasClientComposition = true;
529 break;
530 case HWC2::Composition::Device:
531 case HWC2::Composition::SolidColor:
532 case HWC2::Composition::Cursor:
533 case HWC2::Composition::Sideband:
534 displayData.hasDeviceComposition = true;
535 break;
536 default:
537 break;
538 }
539
540 if (layerRequests.count(hwcLayer) != 0 &&
541 layerRequests[hwcLayer] ==
542 HWC2::LayerRequest::ClearClientTarget) {
543 layer->setClearClientTarget(displayId, true);
544 } else {
545 if (layerRequests.count(hwcLayer) != 0) {
546 ALOGE("prepare: Unknown layer request: %s",
547 to_string(layerRequests[hwcLayer]).c_str());
548 }
549 layer->setClearClientTarget(displayId, false);
550 }
551 }
552
553 error = hwcDisplay->acceptChanges();
554 if (error != HWC2::Error::None) {
555 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str());
556 return BAD_INDEX;
557 }
558
559 return NO_ERROR;
560}
561
562bool HWComposer::hasDeviceComposition(int32_t displayId) const {
563 if (!isValidDisplay(displayId)) {
564 ALOGE("hasDeviceComposition: Invalid display %d", displayId);
565 return false;
566 }
567 return mDisplayData[displayId].hasDeviceComposition;
568}
569
570bool HWComposer::hasClientComposition(int32_t displayId) const {
571 if (!isValidDisplay(displayId)) {
572 ALOGE("hasClientComposition: Invalid display %d", displayId);
573 return true;
574 }
575 return mDisplayData[displayId].hasClientComposition;
576}
577
578sp<Fence> HWComposer::getRetireFence(int32_t displayId) const {
579 if (!isValidDisplay(displayId)) {
580 ALOGE("getRetireFence failed for invalid display %d", displayId);
Jesse Hall851cfe82013-03-20 13:44:00 -0700581 return Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 }
583 return mDisplayData[displayId].lastRetireFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700584}
585
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
587 const std::shared_ptr<HWC2::Layer>& layer) const {
588 if (!isValidDisplay(displayId)) {
589 ALOGE("getLayerReleaseFence: Invalid display");
590 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700591 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800592 auto displayFences = mDisplayData[displayId].releaseFences;
593 if (displayFences.count(layer) == 0) {
594 ALOGV("getLayerReleaseFence: Release fence not found");
595 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700596 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800597 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700598}
599
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600status_t HWComposer::commit(int32_t displayId) {
601 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700602
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603 if (!isValidDisplay(displayId)) {
604 return BAD_INDEX;
Mathias Agopianc3973602012-08-31 17:51:25 -0700605 }
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700606
Dan Stoza9e56aa02015-11-02 13:00:03 -0800607 auto& displayData = mDisplayData[displayId];
608 auto& hwcDisplay = displayData.hwcDisplay;
609 auto error = hwcDisplay->present(&displayData.lastRetireFence);
610 if (error != HWC2::Error::None) {
611 ALOGE("commit: present failed for display %d: %s (%d)", displayId,
612 to_string(error).c_str(), static_cast<int32_t>(error));
613 return UNKNOWN_ERROR;
614 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700615
Dan Stoza9e56aa02015-11-02 13:00:03 -0800616 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences;
617 error = hwcDisplay->getReleaseFences(&releaseFences);
618 if (error != HWC2::Error::None) {
619 ALOGE("commit: Failed to get release fences for display %d: %s (%d)",
620 displayId, to_string(error).c_str(),
621 static_cast<int32_t>(error));
622 return UNKNOWN_ERROR;
Mathias Agopianf4358632012-08-22 17:16:19 -0700623 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624
625 displayData.releaseFences = std::move(releaseFences);
626
627 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700628}
629
Dan Stoza9e56aa02015-11-02 13:00:03 -0800630status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
631 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
632 if (!isValidDisplay(displayId)) {
633 ALOGE("setPowerMode: Bad display");
634 return BAD_INDEX;
635 }
636 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) {
637 ALOGE("setPowerMode: Virtual display %d passed in, returning",
638 displayId);
639 return BAD_INDEX;
640 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700641
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642 auto mode = static_cast<HWC2::PowerMode>(intMode);
643 if (mode == HWC2::PowerMode::Off) {
644 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
645 }
646
647 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
648 switch (mode) {
649 case HWC2::PowerMode::Off:
650 case HWC2::PowerMode::On:
651 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
652 {
653 auto error = hwcDisplay->setPowerMode(mode);
654 if (error != HWC2::Error::None) {
655 ALOGE("setPowerMode: Unable to set power mode %s for "
656 "display %d: %s (%d)", to_string(mode).c_str(),
657 displayId, to_string(error).c_str(),
658 static_cast<int32_t>(error));
Mathias Agopianda27af92012-09-13 18:17:13 -0700659 }
660 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661 break;
662 case HWC2::PowerMode::Doze:
663 case HWC2::PowerMode::DozeSuspend:
664 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
665 {
666 bool supportsDoze = false;
667 auto error = hwcDisplay->supportsDoze(&supportsDoze);
668 if (error != HWC2::Error::None) {
669 ALOGE("setPowerMode: Unable to query doze support for "
670 "display %d: %s (%d)", displayId,
671 to_string(error).c_str(),
672 static_cast<int32_t>(error));
673 }
674 if (!supportsDoze) {
675 mode = HWC2::PowerMode::On;
676 }
677
678 error = hwcDisplay->setPowerMode(mode);
679 if (error != HWC2::Error::None) {
680 ALOGE("setPowerMode: Unable to set power mode %s for "
681 "display %d: %s (%d)", to_string(mode).c_str(),
682 displayId, to_string(error).c_str(),
683 static_cast<int32_t>(error));
684 }
685 }
686 break;
687 default:
688 ALOGV("setPowerMode: Not calling HWC");
689 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700690 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800691
692 return NO_ERROR;
693}
694
695status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
696 if (!isValidDisplay(displayId)) {
697 ALOGE("setActiveConfig: Display %d is not valid", displayId);
698 return BAD_INDEX;
699 }
700
701 auto& displayData = mDisplayData[displayId];
702 if (displayData.configMap.count(configId) == 0) {
703 ALOGE("setActiveConfig: Invalid config %zd", configId);
704 return BAD_INDEX;
705 }
706
707 auto error = displayData.hwcDisplay->setActiveConfig(
708 displayData.configMap[configId]);
709 if (error != HWC2::Error::None) {
710 ALOGE("setActiveConfig: Failed to set config %zu on display %d: "
711 "%s (%d)", configId, displayId, to_string(error).c_str(),
712 static_cast<int32_t>(error));
713 return UNKNOWN_ERROR;
714 }
715
716 return NO_ERROR;
717}
718
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700719status_t HWComposer::setColorTransform(int32_t displayId,
720 const mat4& transform) {
721 if (!isValidDisplay(displayId)) {
722 ALOGE("setColorTransform: Display %d is not valid", displayId);
723 return BAD_INDEX;
724 }
725
726 auto& displayData = mDisplayData[displayId];
727 bool isIdentity = transform == mat4();
728 auto error = displayData.hwcDisplay->setColorTransform(transform,
729 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
730 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
731 if (error != HWC2::Error::None) {
732 ALOGE("setColorTransform: Failed to set transform on display %d: "
733 "%s (%d)", displayId, to_string(error).c_str(),
734 static_cast<int32_t>(error));
735 return UNKNOWN_ERROR;
736 }
737
738 return NO_ERROR;
739}
740
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741void HWComposer::disconnectDisplay(int displayId) {
742 LOG_ALWAYS_FATAL_IF(displayId < 0);
743 auto& displayData = mDisplayData[displayId];
744
745 auto displayType = HWC2::DisplayType::Invalid;
746 auto error = displayData.hwcDisplay->getType(&displayType);
747 if (error != HWC2::Error::None) {
748 ALOGE("disconnectDisplay: Failed to determine type of display %d",
749 displayId);
750 return;
751 }
752
753 // If this was a virtual display, add its slot back for reuse by future
754 // virtual displays
755 if (displayType == HWC2::DisplayType::Virtual) {
756 mFreeDisplaySlots.insert(displayId);
757 ++mRemainingHwcVirtualDisplays;
758 }
759
760 auto hwcId = displayData.hwcDisplay->getId();
761 mHwcDisplaySlots.erase(hwcId);
762 displayData.reset();
763}
764
765status_t HWComposer::setOutputBuffer(int32_t displayId,
766 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
767 if (!isValidDisplay(displayId)) {
768 ALOGE("setOutputBuffer: Display %d is not valid", displayId);
769 return BAD_INDEX;
770 }
771
772 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
773 auto displayType = HWC2::DisplayType::Invalid;
774 auto error = hwcDisplay->getType(&displayType);
775 if (error != HWC2::Error::None) {
776 ALOGE("setOutputBuffer: Failed to determine type of display %d",
777 displayId);
778 return NAME_NOT_FOUND;
779 }
780
781 if (displayType != HWC2::DisplayType::Virtual) {
782 ALOGE("setOutputBuffer: Display %d is not virtual", displayId);
783 return INVALID_OPERATION;
784 }
785
786 error = hwcDisplay->setOutputBuffer(buffer, acquireFence);
787 if (error != HWC2::Error::None) {
788 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)",
789 displayId, to_string(error).c_str(),
790 static_cast<int32_t>(error));
791 return UNKNOWN_ERROR;
792 }
793
794 return NO_ERROR;
795}
796
797void HWComposer::clearReleaseFences(int32_t displayId) {
798 if (!isValidDisplay(displayId)) {
799 ALOGE("clearReleaseFences: Display %d is not valid", displayId);
800 return;
801 }
802 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700803}
804
Dan Stozac4f471e2016-03-24 09:31:08 -0700805std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities(
806 int32_t displayId) {
807 if (!isValidDisplay(displayId)) {
808 ALOGE("getHdrCapabilities: Display %d is not valid", displayId);
809 return nullptr;
810 }
811
812 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
813 std::unique_ptr<HdrCapabilities> capabilities;
814 auto error = hwcDisplay->getHdrCapabilities(&capabilities);
815 if (error != HWC2::Error::None) {
816 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:"
817 " %s (%d)", displayId, to_string(error).c_str(),
818 static_cast<int32_t>(error));
819 return nullptr;
820 }
821
822 return capabilities;
823}
824
Andy McFadden4df87bd2014-04-21 18:08:54 -0700825// Converts a PixelFormat to a human-readable string. Max 11 chars.
826// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800827/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700828static String8 getFormatStr(PixelFormat format) {
829 switch (format) {
830 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
831 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
832 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
833 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
834 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700835 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
836 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700837 default:
838 String8 result;
839 result.appendFormat("? %08x", format);
840 return result;
841 }
842}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800843*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700844
Mathias Agopian74d211a2013-04-22 16:55:35 +0200845void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800846 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
847 // all the state going into the layers. This is probably better done in
848 // Layer itself, but it's going to take a bit of work to get there.
849 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700850}
851
Mathias Agopiana350ff92010-08-10 17:14:02 -0700852// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700853
Jesse Halla9a1b002013-02-27 16:39:25 -0800854HWComposer::DisplayData::DisplayData()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800855 : hasClientComposition(false),
856 hasDeviceComposition(false),
857 hwcDisplay(),
858 lastRetireFence(Fence::NO_FENCE),
859 outbufHandle(nullptr),
860 outbufAcquireFence(Fence::NO_FENCE),
861 vsyncEnabled(HWC2::Vsync::Disable) {
862 ALOGV("Created new DisplayData");
863}
Jesse Halla9a1b002013-02-27 16:39:25 -0800864
865HWComposer::DisplayData::~DisplayData() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800866}
867
868void HWComposer::DisplayData::reset() {
869 ALOGV("DisplayData reset");
870 *this = DisplayData();
Jesse Halla9a1b002013-02-27 16:39:25 -0800871}
872
Mathias Agopian2965b262012-04-08 15:13:32 -0700873// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700874}; // namespace android