blob: d2b26c26dfdb65a2bbdba40f780ef45a9cdf1f92 [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 Agopiane2c4f4e2012-04-10 18:25:31 -070046#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070047#include <log/log.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"
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080052#include "ComposerHal.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070053
54#include "../Layer.h" // needed only for debugging
55#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070056
57namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070058
Jesse Hall72960512013-01-10 16:19:56 -080059#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070060
Mathias Agopiana350ff92010-08-10 17:14:02 -070061// ---------------------------------------------------------------------------
62
Steven Thomas3cfac282017-02-06 12:29:30 -080063HWComposer::HWComposer(bool useVrComposer)
64 : mAdapter(),
Dan Stoza9e56aa02015-11-02 13:00:03 -080065 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
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080079 loadHwcModule(useVrComposer);
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.
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800108void HWComposer::loadHwcModule(bool useVrComposer)
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700109{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 ALOGV("loadHwcModule");
111
Chia-I Wuaab99f52016-10-05 12:59:58 +0800112#ifdef BYPASS_IHWC
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800113 (void)useVrComposer; // Silence unused parameter warning.
114
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700115 hw_module_t const* module;
116
117 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800118 ALOGE("%s module not found, aborting", HWC_HARDWARE_MODULE_ID);
119 abort();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700120 }
121
Dan Stoza355f6042016-04-14 14:30:41 -0700122 hw_device_t* device = nullptr;
123 int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device);
124 if (error != 0) {
125 ALOGE("Failed to open HWC device (%s), aborting", strerror(-error));
126 abort();
127 }
128
129 uint32_t majorVersion = (device->version >> 24) & 0xF;
130 if (majorVersion == 2) {
131 mHwcDevice = std::make_unique<HWC2::Device>(
132 reinterpret_cast<hwc2_device_t*>(device));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800133 } else {
Dan Stoza355f6042016-04-14 14:30:41 -0700134 mAdapter = std::make_unique<HWC2On1Adapter>(
135 reinterpret_cast<hwc_composer_device_1_t*>(device));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800136 uint8_t minorVersion = mAdapter->getHwc1MinorVersion();
137 if (minorVersion < 1) {
138 ALOGE("Cannot adapt to HWC version %d.%d",
139 static_cast<int32_t>((minorVersion >> 8) & 0xF),
140 static_cast<int32_t>(minorVersion & 0xF));
141 abort();
142 }
143 mHwcDevice = std::make_unique<HWC2::Device>(
144 static_cast<hwc2_device_t*>(mAdapter.get()));
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700145 }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800146#else
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800147 mHwcDevice = std::make_unique<HWC2::Device>(useVrComposer);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800148#endif
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700149
Dan Stoza9e56aa02015-11-02 13:00:03 -0800150 mRemainingHwcVirtualDisplays = mHwcDevice->getMaxVirtualDisplayCount();
151}
152
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700153bool HWComposer::hasCapability(HWC2::Capability capability) const
154{
155 return mHwcDevice->getCapabilities().count(capability) > 0;
156}
157
Dan Stoza9e56aa02015-11-02 13:00:03 -0800158bool HWComposer::isValidDisplay(int32_t displayId) const {
159 return static_cast<size_t>(displayId) < mDisplayData.size() &&
160 mDisplayData[displayId].hwcDisplay;
161}
162
163void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
164 bool valid = true;
165 switch (from) {
166 case HWC2::Composition::Client:
167 valid = false;
168 break;
169 case HWC2::Composition::Device:
170 case HWC2::Composition::SolidColor:
171 valid = (to == HWC2::Composition::Client);
172 break;
173 case HWC2::Composition::Cursor:
174 case HWC2::Composition::Sideband:
175 valid = (to == HWC2::Composition::Client ||
176 to == HWC2::Composition::Device);
177 break;
178 default:
179 break;
180 }
181
182 if (!valid) {
183 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
184 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700185 }
186}
187
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display,
189 HWC2::Connection connected) {
190 ALOGV("hotplug: %" PRIu64 ", %s", display->getId(),
191 to_string(connected).c_str());
192 int32_t disp = 0;
193 if (!mDisplayData[0].hwcDisplay) {
194 ALOGE_IF(connected != HWC2::Connection::Connected, "Assumed primary"
195 " display would be connected");
196 mDisplayData[0].hwcDisplay = display;
197 mHwcDisplaySlots[display->getId()] = 0;
198 disp = DisplayDevice::DISPLAY_PRIMARY;
199 } else {
200 // Disconnect is handled through HWComposer::disconnectDisplay via
201 // SurfaceFlinger's onHotplugReceived callback handling
202 if (connected == HWC2::Connection::Connected) {
203 mDisplayData[1].hwcDisplay = display;
204 mHwcDisplaySlots[display->getId()] = 1;
205 }
206 disp = DisplayDevice::DISPLAY_EXTERNAL;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700207 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208 mEventHandler->onHotplugReceived(disp,
209 connected == HWC2::Connection::Connected);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700210}
211
Dan Stoza9e56aa02015-11-02 13:00:03 -0800212void HWComposer::invalidate(const std::shared_ptr<HWC2::Display>& /*display*/) {
Steven Thomas3cfac282017-02-06 12:29:30 -0800213 mEventHandler->onInvalidateReceived(this);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700214}
215
Dan Stoza9e56aa02015-11-02 13:00:03 -0800216void HWComposer::vsync(const std::shared_ptr<HWC2::Display>& display,
217 int64_t timestamp) {
218 auto displayType = HWC2::DisplayType::Invalid;
219 auto error = display->getType(&displayType);
220 if (error != HWC2::Error::None) {
221 ALOGE("vsync: Failed to determine type of display %" PRIu64,
222 display->getId());
Jesse Hall1bd20e02012-08-29 10:47:52 -0700223 return;
224 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700225
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226 if (displayType == HWC2::DisplayType::Virtual) {
227 ALOGE("Virtual 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 if (mHwcDisplaySlots.count(display->getId()) == 0) {
233 ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback",
234 display->getId());
235 return;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700236 }
237
Dan Stoza9e56aa02015-11-02 13:00:03 -0800238 int32_t disp = mHwcDisplaySlots[display->getId()];
239 {
240 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700241
Dan Stoza9e56aa02015-11-02 13:00:03 -0800242 // There have been reports of HWCs that signal several vsync events
243 // with the same timestamp when turning the display off and on. This
244 // is a bug in the HWC implementation, but filter the extra events
245 // out here so they don't cause havoc downstream.
246 if (timestamp == mLastHwVSync[disp]) {
247 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
248 timestamp);
249 return;
250 }
251
252 mLastHwVSync[disp] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700253 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800254
255 char tag[16];
256 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
257 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
258
Steven Thomas3cfac282017-02-06 12:29:30 -0800259 mEventHandler->onVSyncReceived(this, disp, timestamp);
Jesse Hall1c569c42013-04-05 13:44:52 -0700260}
261
Dan Stoza9e56aa02015-11-02 13:00:03 -0800262status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700263 android_pixel_format_t* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800264 if (mRemainingHwcVirtualDisplays == 0) {
265 ALOGE("allocateVirtualDisplay: No remaining virtual displays");
Mathias Agopiane60b0682012-08-21 23:34:09 -0700266 return NO_MEMORY;
267 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700268
Dan Stoza9e56aa02015-11-02 13:00:03 -0800269 std::shared_ptr<HWC2::Display> display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700270 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
271 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272 if (error != HWC2::Error::None) {
273 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display");
274 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700275 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800276
277 size_t displaySlot = 0;
278 if (!mFreeDisplaySlots.empty()) {
279 displaySlot = *mFreeDisplaySlots.begin();
280 mFreeDisplaySlots.erase(displaySlot);
281 } else if (mDisplayData.size() < INT32_MAX) {
282 // Don't bother allocating a slot larger than we can return
283 displaySlot = mDisplayData.size();
284 mDisplayData.resize(displaySlot + 1);
285 } else {
286 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot");
287 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700288 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800289
290 mDisplayData[displaySlot].hwcDisplay = display;
291
292 --mRemainingHwcVirtualDisplays;
293 *outId = static_cast<int32_t>(displaySlot);
294
Mathias Agopiane60b0682012-08-21 23:34:09 -0700295 return NO_ERROR;
296}
297
Dan Stoza9e56aa02015-11-02 13:00:03 -0800298std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) {
299 if (!isValidDisplay(displayId)) {
300 ALOGE("Failed to create layer on invalid display %d", displayId);
301 return nullptr;
302 }
303 auto display = mDisplayData[displayId].hwcDisplay;
304 std::shared_ptr<HWC2::Layer> layer;
305 auto error = display->createLayer(&layer);
306 if (error != HWC2::Error::None) {
307 ALOGE("Failed to create layer on display %d: %s (%d)", displayId,
308 to_string(error).c_str(), static_cast<int32_t>(error));
309 return nullptr;
310 }
311 return layer;
312}
313
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800314nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700315 // this returns the last refresh timestamp.
316 // if the last one is not available, we estimate it based on
317 // the refresh period and whatever closest timestamp we have.
318 Mutex::Autolock _l(mLock);
319 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800320 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
321 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700322}
323
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800324bool HWComposer::isConnected(int32_t displayId) const {
325 if (!isValidDisplay(displayId)) {
326 ALOGE("isConnected: Attempted to access invalid display %d", displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700327 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800328 }
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800329 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700330}
331
Dan Stoza9e56aa02015-11-02 13:00:03 -0800332std::vector<std::shared_ptr<const HWC2::Display::Config>>
333 HWComposer::getConfigs(int32_t displayId) const {
334 if (!isValidDisplay(displayId)) {
335 ALOGE("getConfigs: Attempted to access invalid display %d", displayId);
336 return {};
337 }
338 auto& displayData = mDisplayData[displayId];
339 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
340 if (displayData.configMap.empty()) {
341 for (size_t i = 0; i < configs.size(); ++i) {
342 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700343 }
344 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800345 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700346}
347
Dan Stoza9e56aa02015-11-02 13:00:03 -0800348std::shared_ptr<const HWC2::Display::Config>
349 HWComposer::getActiveConfig(int32_t displayId) const {
350 if (!isValidDisplay(displayId)) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800351 ALOGV("getActiveConfigs: Attempted to access invalid display %d",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800352 displayId);
353 return nullptr;
Mathias Agopian58959342010-10-07 14:57:04 -0700354 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800355 std::shared_ptr<const HWC2::Display::Config> config;
356 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
357 if (error == HWC2::Error::BadConfig) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800358 ALOGE("getActiveConfig: No config active, returning null");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800359 return nullptr;
360 } else if (error != HWC2::Error::None) {
361 ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId,
362 to_string(error).c_str(), static_cast<int32_t>(error));
363 return nullptr;
364 } else if (!config) {
365 ALOGE("getActiveConfig returned an unknown config for display %d",
366 displayId);
367 return nullptr;
368 }
369
370 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700371}
372
Michael Wright28f24d02016-07-12 13:30:53 -0700373std::vector<android_color_mode_t> HWComposer::getColorModes(int32_t displayId) const {
374 std::vector<android_color_mode_t> modes;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600375
376 if (!isValidDisplay(displayId)) {
377 ALOGE("getColorModes: Attempted to access invalid display %d",
378 displayId);
379 return modes;
380 }
381 const std::shared_ptr<HWC2::Display>& hwcDisplay =
382 mDisplayData[displayId].hwcDisplay;
383
384 auto error = hwcDisplay->getColorModes(&modes);
385 if (error != HWC2::Error::None) {
386 ALOGE("getColorModes failed for display %d: %s (%d)", displayId,
387 to_string(error).c_str(), static_cast<int32_t>(error));
Michael Wright28f24d02016-07-12 13:30:53 -0700388 return std::vector<android_color_mode_t>();
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600389 }
390
391 return modes;
392}
393
Michael Wright28f24d02016-07-12 13:30:53 -0700394status_t HWComposer::setActiveColorMode(int32_t displayId, android_color_mode_t mode) {
395 if (!isValidDisplay(displayId)) {
396 ALOGE("setActiveColorMode: Display %d is not valid", displayId);
397 return BAD_INDEX;
398 }
399
400 auto& displayData = mDisplayData[displayId];
401 auto error = displayData.hwcDisplay->setColorMode(mode);
402 if (error != HWC2::Error::None) {
403 ALOGE("setActiveConfig: Failed to set color mode %d on display %d: "
404 "%s (%d)", mode, displayId, to_string(error).c_str(),
405 static_cast<int32_t>(error));
406 return UNKNOWN_ERROR;
407 }
408
409 return NO_ERROR;
410}
411
412
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800413void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
414 if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) {
415 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800416 return;
417 }
418
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800419 if (!isValidDisplay(displayId)) {
420 ALOGE("setVsyncEnabled: Attempted to access invalid display %d",
421 displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800422 return;
423 }
424
425 // NOTE: we use our own internal lock here because we have to call
426 // into the HWC with the lock held, and we want to make sure
427 // that even if HWC blocks (which it shouldn't), it won't
428 // affect other threads.
429 Mutex::Autolock _l(mVsyncLock);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800430 auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800431 if (enabled != displayData.vsyncEnabled) {
432 ATRACE_CALL();
433 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
434 if (error == HWC2::Error::None) {
435 displayData.vsyncEnabled = enabled;
436
437 char tag[16];
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800438 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800439 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700440 } else {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800441 ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800442 ": %s (%d)", to_string(enabled).c_str(), displayId,
443 mDisplayData[displayId].hwcDisplay->getId(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800444 to_string(error).c_str(), static_cast<int32_t>(error));
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700445 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700446 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700447}
448
Chia-I Wu06d63de2017-01-04 14:58:51 +0800449status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800450 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
451 android_dataspace_t dataspace) {
452 if (!isValidDisplay(displayId)) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700453 return BAD_INDEX;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800454 }
Jesse Hall851cfe82013-03-20 13:44:00 -0700455
Dan Stoza9e56aa02015-11-02 13:00:03 -0800456 ALOGV("setClientTarget for display %d", displayId);
457 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
458 buffer_handle_t handle = nullptr;
459 if ((target != nullptr) && target->getNativeBuffer()) {
460 handle = target->getNativeBuffer()->handle;
461 }
Chia-I Wu06d63de2017-01-04 14:58:51 +0800462 auto error = hwcDisplay->setClientTarget(slot, handle,
463 acquireFence, dataspace);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800464 if (error != HWC2::Error::None) {
465 ALOGE("Failed to set client target for display %d: %s (%d)", displayId,
466 to_string(error).c_str(), static_cast<int32_t>(error));
467 return BAD_VALUE;
468 }
469
Jesse Hall851cfe82013-03-20 13:44:00 -0700470 return NO_ERROR;
471}
472
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473status_t HWComposer::prepare(DisplayDevice& displayDevice) {
474 ATRACE_CALL();
475
476 Mutex::Autolock _l(mDisplayLock);
477 auto displayId = displayDevice.getHwcDisplayId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700478 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
479 ALOGV("Skipping HWComposer prepare for non-HWC display");
480 return NO_ERROR;
481 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 if (!isValidDisplay(displayId)) {
483 return BAD_INDEX;
484 }
485
486 auto& displayData = mDisplayData[displayId];
487 auto& hwcDisplay = displayData.hwcDisplay;
488 if (!hwcDisplay->isConnected()) {
489 return NO_ERROR;
490 }
491
492 uint32_t numTypes = 0;
493 uint32_t numRequests = 0;
494 auto error = hwcDisplay->validate(&numTypes, &numRequests);
495 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) {
496 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId,
497 to_string(error).c_str(), static_cast<int32_t>(error));
498 return BAD_INDEX;
499 }
500
501 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition>
502 changedTypes;
503 changedTypes.reserve(numTypes);
504 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
505 if (error != HWC2::Error::None) {
506 ALOGE("prepare: getChangedCompositionTypes failed on display %d: "
507 "%s (%d)", displayId, to_string(error).c_str(),
508 static_cast<int32_t>(error));
509 return BAD_INDEX;
510 }
511
512
513 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
514 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest>
515 layerRequests;
516 layerRequests.reserve(numRequests);
517 error = hwcDisplay->getRequests(&displayData.displayRequests,
518 &layerRequests);
519 if (error != HWC2::Error::None) {
520 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId,
521 to_string(error).c_str(), static_cast<int32_t>(error));
522 return BAD_INDEX;
523 }
524
525 displayData.hasClientComposition = false;
526 displayData.hasDeviceComposition = false;
527 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) {
528 auto hwcLayer = layer->getHwcLayer(displayId);
529
530 if (changedTypes.count(hwcLayer) != 0) {
531 // We pass false so we only update our state and don't call back
532 // into the HWC device
533 validateChange(layer->getCompositionType(displayId),
534 changedTypes[hwcLayer]);
535 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
536 }
537
538 switch (layer->getCompositionType(displayId)) {
539 case HWC2::Composition::Client:
540 displayData.hasClientComposition = true;
541 break;
542 case HWC2::Composition::Device:
543 case HWC2::Composition::SolidColor:
544 case HWC2::Composition::Cursor:
545 case HWC2::Composition::Sideband:
546 displayData.hasDeviceComposition = true;
547 break;
548 default:
549 break;
550 }
551
552 if (layerRequests.count(hwcLayer) != 0 &&
553 layerRequests[hwcLayer] ==
554 HWC2::LayerRequest::ClearClientTarget) {
555 layer->setClearClientTarget(displayId, true);
556 } else {
557 if (layerRequests.count(hwcLayer) != 0) {
558 ALOGE("prepare: Unknown layer request: %s",
559 to_string(layerRequests[hwcLayer]).c_str());
560 }
561 layer->setClearClientTarget(displayId, false);
562 }
563 }
564
565 error = hwcDisplay->acceptChanges();
566 if (error != HWC2::Error::None) {
567 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str());
568 return BAD_INDEX;
569 }
570
571 return NO_ERROR;
572}
573
574bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700575 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
576 // Displays without a corresponding HWC display are never composed by
577 // the device
578 return false;
579 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580 if (!isValidDisplay(displayId)) {
581 ALOGE("hasDeviceComposition: Invalid display %d", displayId);
582 return false;
583 }
584 return mDisplayData[displayId].hasDeviceComposition;
585}
586
587bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700588 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
589 // Displays without a corresponding HWC display are always composed by
590 // the client
591 return true;
592 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593 if (!isValidDisplay(displayId)) {
594 ALOGE("hasClientComposition: Invalid display %d", displayId);
595 return true;
596 }
597 return mDisplayData[displayId].hasClientComposition;
598}
599
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800600sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 if (!isValidDisplay(displayId)) {
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800602 ALOGE("getPresentFence failed for invalid display %d", displayId);
Jesse Hall851cfe82013-03-20 13:44:00 -0700603 return Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800604 }
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800605 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700606}
607
Brian Anderson3d4039d2016-09-23 16:31:30 -0700608bool HWComposer::presentFenceRepresentsStartOfScanout() const {
Brian Anderson069b3652016-07-22 10:32:47 -0700609 return mAdapter ? false : true;
610}
611
Dan Stoza9e56aa02015-11-02 13:00:03 -0800612sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
613 const std::shared_ptr<HWC2::Layer>& layer) const {
614 if (!isValidDisplay(displayId)) {
615 ALOGE("getLayerReleaseFence: Invalid display");
616 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700617 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800618 auto displayFences = mDisplayData[displayId].releaseFences;
619 if (displayFences.count(layer) == 0) {
620 ALOGV("getLayerReleaseFence: Release fence not found");
621 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700622 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700624}
625
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800626status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800627 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700628
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629 if (!isValidDisplay(displayId)) {
630 return BAD_INDEX;
Mathias Agopianc3973602012-08-31 17:51:25 -0700631 }
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700632
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633 auto& displayData = mDisplayData[displayId];
634 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800635 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636 if (error != HWC2::Error::None) {
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800637 ALOGE("presentAndGetReleaseFences: failed for display %d: %s (%d)",
638 displayId, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639 return UNKNOWN_ERROR;
640 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700641
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences;
643 error = hwcDisplay->getReleaseFences(&releaseFences);
644 if (error != HWC2::Error::None) {
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800645 ALOGE("presentAndGetReleaseFences: Failed to get release fences "
646 "for display %d: %s (%d)",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 displayId, to_string(error).c_str(),
648 static_cast<int32_t>(error));
649 return UNKNOWN_ERROR;
Mathias Agopianf4358632012-08-22 17:16:19 -0700650 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651
652 displayData.releaseFences = std::move(releaseFences);
653
654 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700655}
656
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
658 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
659 if (!isValidDisplay(displayId)) {
660 ALOGE("setPowerMode: Bad display");
661 return BAD_INDEX;
662 }
663 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) {
664 ALOGE("setPowerMode: Virtual display %d passed in, returning",
665 displayId);
666 return BAD_INDEX;
667 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700668
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669 auto mode = static_cast<HWC2::PowerMode>(intMode);
670 if (mode == HWC2::PowerMode::Off) {
671 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
672 }
673
674 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
675 switch (mode) {
676 case HWC2::PowerMode::Off:
677 case HWC2::PowerMode::On:
678 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
679 {
680 auto error = hwcDisplay->setPowerMode(mode);
681 if (error != HWC2::Error::None) {
682 ALOGE("setPowerMode: Unable to set power mode %s for "
683 "display %d: %s (%d)", to_string(mode).c_str(),
684 displayId, to_string(error).c_str(),
685 static_cast<int32_t>(error));
Mathias Agopianda27af92012-09-13 18:17:13 -0700686 }
687 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 break;
689 case HWC2::PowerMode::Doze:
690 case HWC2::PowerMode::DozeSuspend:
691 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
692 {
693 bool supportsDoze = false;
694 auto error = hwcDisplay->supportsDoze(&supportsDoze);
695 if (error != HWC2::Error::None) {
696 ALOGE("setPowerMode: Unable to query doze support for "
697 "display %d: %s (%d)", displayId,
698 to_string(error).c_str(),
699 static_cast<int32_t>(error));
700 }
701 if (!supportsDoze) {
702 mode = HWC2::PowerMode::On;
703 }
704
705 error = hwcDisplay->setPowerMode(mode);
706 if (error != HWC2::Error::None) {
707 ALOGE("setPowerMode: Unable to set power mode %s for "
708 "display %d: %s (%d)", to_string(mode).c_str(),
709 displayId, to_string(error).c_str(),
710 static_cast<int32_t>(error));
711 }
712 }
713 break;
714 default:
715 ALOGV("setPowerMode: Not calling HWC");
716 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700717 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718
719 return NO_ERROR;
720}
721
722status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
723 if (!isValidDisplay(displayId)) {
724 ALOGE("setActiveConfig: Display %d is not valid", displayId);
725 return BAD_INDEX;
726 }
727
728 auto& displayData = mDisplayData[displayId];
729 if (displayData.configMap.count(configId) == 0) {
730 ALOGE("setActiveConfig: Invalid config %zd", configId);
731 return BAD_INDEX;
732 }
733
734 auto error = displayData.hwcDisplay->setActiveConfig(
735 displayData.configMap[configId]);
736 if (error != HWC2::Error::None) {
737 ALOGE("setActiveConfig: Failed to set config %zu on display %d: "
738 "%s (%d)", configId, displayId, to_string(error).c_str(),
739 static_cast<int32_t>(error));
740 return UNKNOWN_ERROR;
741 }
742
743 return NO_ERROR;
744}
745
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700746status_t HWComposer::setColorTransform(int32_t displayId,
747 const mat4& transform) {
748 if (!isValidDisplay(displayId)) {
749 ALOGE("setColorTransform: Display %d is not valid", displayId);
750 return BAD_INDEX;
751 }
752
753 auto& displayData = mDisplayData[displayId];
754 bool isIdentity = transform == mat4();
755 auto error = displayData.hwcDisplay->setColorTransform(transform,
756 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
757 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
758 if (error != HWC2::Error::None) {
759 ALOGE("setColorTransform: Failed to set transform on display %d: "
760 "%s (%d)", displayId, to_string(error).c_str(),
761 static_cast<int32_t>(error));
762 return UNKNOWN_ERROR;
763 }
764
765 return NO_ERROR;
766}
767
Dan Stoza9e56aa02015-11-02 13:00:03 -0800768void HWComposer::disconnectDisplay(int displayId) {
769 LOG_ALWAYS_FATAL_IF(displayId < 0);
770 auto& displayData = mDisplayData[displayId];
771
772 auto displayType = HWC2::DisplayType::Invalid;
773 auto error = displayData.hwcDisplay->getType(&displayType);
774 if (error != HWC2::Error::None) {
775 ALOGE("disconnectDisplay: Failed to determine type of display %d",
776 displayId);
777 return;
778 }
779
780 // If this was a virtual display, add its slot back for reuse by future
781 // virtual displays
782 if (displayType == HWC2::DisplayType::Virtual) {
783 mFreeDisplaySlots.insert(displayId);
784 ++mRemainingHwcVirtualDisplays;
785 }
786
787 auto hwcId = displayData.hwcDisplay->getId();
788 mHwcDisplaySlots.erase(hwcId);
789 displayData.reset();
790}
791
792status_t HWComposer::setOutputBuffer(int32_t displayId,
793 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
794 if (!isValidDisplay(displayId)) {
795 ALOGE("setOutputBuffer: Display %d is not valid", displayId);
796 return BAD_INDEX;
797 }
798
799 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
800 auto displayType = HWC2::DisplayType::Invalid;
801 auto error = hwcDisplay->getType(&displayType);
802 if (error != HWC2::Error::None) {
803 ALOGE("setOutputBuffer: Failed to determine type of display %d",
804 displayId);
805 return NAME_NOT_FOUND;
806 }
807
808 if (displayType != HWC2::DisplayType::Virtual) {
809 ALOGE("setOutputBuffer: Display %d is not virtual", displayId);
810 return INVALID_OPERATION;
811 }
812
813 error = hwcDisplay->setOutputBuffer(buffer, acquireFence);
814 if (error != HWC2::Error::None) {
815 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)",
816 displayId, to_string(error).c_str(),
817 static_cast<int32_t>(error));
818 return UNKNOWN_ERROR;
819 }
820
821 return NO_ERROR;
822}
823
824void HWComposer::clearReleaseFences(int32_t displayId) {
825 if (!isValidDisplay(displayId)) {
826 ALOGE("clearReleaseFences: Display %d is not valid", displayId);
827 return;
828 }
829 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700830}
831
Dan Stozac4f471e2016-03-24 09:31:08 -0700832std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities(
833 int32_t displayId) {
834 if (!isValidDisplay(displayId)) {
835 ALOGE("getHdrCapabilities: Display %d is not valid", displayId);
836 return nullptr;
837 }
838
839 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
840 std::unique_ptr<HdrCapabilities> capabilities;
841 auto error = hwcDisplay->getHdrCapabilities(&capabilities);
842 if (error != HWC2::Error::None) {
843 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:"
844 " %s (%d)", displayId, to_string(error).c_str(),
845 static_cast<int32_t>(error));
846 return nullptr;
847 }
848
849 return capabilities;
850}
851
Andy McFadden4df87bd2014-04-21 18:08:54 -0700852// Converts a PixelFormat to a human-readable string. Max 11 chars.
853// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800854/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700855static String8 getFormatStr(PixelFormat format) {
856 switch (format) {
857 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
858 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
859 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
860 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
861 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700862 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
863 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700864 default:
865 String8 result;
866 result.appendFormat("? %08x", format);
867 return result;
868 }
869}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800870*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700871
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800872bool HWComposer::isUsingVrComposer() const {
873#ifdef BYPASS_IHWC
874 return false;
875#else
876 return getComposer()->isUsingVrComposer();
877#endif
878}
879
Mathias Agopian74d211a2013-04-22 16:55:35 +0200880void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800881 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
882 // all the state going into the layers. This is probably better done in
883 // Layer itself, but it's going to take a bit of work to get there.
884 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700885}
886
Mathias Agopiana350ff92010-08-10 17:14:02 -0700887// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700888
Jesse Halla9a1b002013-02-27 16:39:25 -0800889HWComposer::DisplayData::DisplayData()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800890 : hasClientComposition(false),
891 hasDeviceComposition(false),
892 hwcDisplay(),
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800893 lastPresentFence(Fence::NO_FENCE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800894 outbufHandle(nullptr),
895 outbufAcquireFence(Fence::NO_FENCE),
896 vsyncEnabled(HWC2::Vsync::Disable) {
897 ALOGV("Created new DisplayData");
898}
Jesse Halla9a1b002013-02-27 16:39:25 -0800899
900HWComposer::DisplayData::~DisplayData() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800901}
902
903void HWComposer::DisplayData::reset() {
904 ALOGV("DisplayData reset");
905 *this = DisplayData();
Jesse Halla9a1b002013-02-27 16:39:25 -0800906}
907
Chia-I Wu06d63de2017-01-04 14:58:51 +0800908void HWComposerBufferCache::clear()
909{
910 mBuffers.clear();
911}
912
913void HWComposerBufferCache::getHwcBuffer(int slot,
914 const sp<GraphicBuffer>& buffer,
915 uint32_t* outSlot, sp<GraphicBuffer>* outBuffer)
916{
917 if (slot == BufferQueue::INVALID_BUFFER_SLOT || slot < 0) {
918 // default to slot 0
919 slot = 0;
920 }
921
922 if (static_cast<size_t>(slot) >= mBuffers.size()) {
923 mBuffers.resize(slot + 1);
924 }
925
926 *outSlot = slot;
927
928 if (mBuffers[slot] == buffer) {
929 // already cached in HWC, skip sending the buffer
930 *outBuffer = nullptr;
931 } else {
932 *outBuffer = buffer;
933
934 // update cache
935 mBuffers[slot] = buffer;
936 }
937}
938
Mathias Agopian2965b262012-04-08 15:13:32 -0700939// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700940}; // namespace android