blob: 02c31fff29fc1cc7a29118361dee2dabd44d8e9e [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
Mathias Agopian2965b262012-04-08 15:13:32 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070019#include <inttypes.h>
20#include <math.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070021#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070025#include <sys/types.h>
26
27#include <utils/Errors.h>
Mathias Agopianda27af92012-09-13 18:17:13 -070028#include <utils/misc.h>
Jesse Hall399184a2014-03-03 15:42:54 -080029#include <utils/NativeHandle.h>
Mathias Agopian83727852010-09-23 18:13:21 -070030#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070031#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070032#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070033#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070034
Mathias Agopian921e6ac2012-07-23 23:11:29 -070035#include <ui/GraphicBuffer.h>
36
Mathias Agopiana350ff92010-08-10 17:14:02 -070037#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070038#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070039
Jesse Hall1c569c42013-04-05 13:44:52 -070040#include <android/configuration.h>
41
Mathias Agopiana350ff92010-08-10 17:14:02 -070042#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070043#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070044
Mathias Agopiana350ff92010-08-10 17:14:02 -070045#include "HWComposer.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070046
47#include "../Layer.h" // needed only for debugging
48#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070049
50namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070051
Jesse Hall72960512013-01-10 16:19:56 -080052#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070053
54static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
55 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070056 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
57}
58
59static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
60 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070061 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
62}
63
64static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
65 uint32_t version) {
66 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070067}
68
Mathias Agopiana350ff92010-08-10 17:14:02 -070069// ---------------------------------------------------------------------------
70
Mathias Agopian3e8b8532012-05-13 20:42:01 -070071struct HWComposer::cb_context {
72 struct callbacks : public hwc_procs_t {
73 // these are here to facilitate the transition when adding
74 // new callbacks (an implementation can check for NULL before
75 // calling a new callback).
76 void (*zero[4])(void);
77 };
78 callbacks procs;
79 HWComposer* hwc;
80};
81
82// ---------------------------------------------------------------------------
83
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070084HWComposer::HWComposer(
85 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070086 EventHandler& handler)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070087 : mFlinger(flinger),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070088 mFbDev(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070089 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070090 mEventHandler(handler),
Mathias Agopianbef42c52013-08-21 17:45:46 -070091 mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -070092{
Jesse Hall9e663de2013-08-16 14:28:37 -070093 for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
Mathias Agopiane60b0682012-08-21 23:34:09 -070094 mLists[i] = 0;
95 }
Jesse Hallb685c542012-07-31 14:32:56 -070096
Mathias Agopianbef42c52013-08-21 17:45:46 -070097 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
98 mLastHwVSync[i] = 0;
99 mVSyncCounts[i] = 0;
100 }
101
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700102 char value[PROPERTY_VALUE_MAX];
103 property_get("debug.sf.no_hw_vsync", value, "0");
104 mDebugForceFakeVSync = atoi(value);
105
Mathias Agopian028508c2012-07-25 21:12:12 -0700106 bool needVSyncThread = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700107
108 // Note: some devices may insist that the FB HAL be opened before HWC.
Jesse Hallef64b752013-03-18 11:28:50 -0700109 int fberr = loadFbHalModule();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700110 loadHwcModule();
111
Mathias Agopianda27af92012-09-13 18:17:13 -0700112 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
113 // close FB HAL if we don't needed it.
114 // FIXME: this is temporary until we're not forced to open FB HAL
115 // before HWC.
116 framebuffer_close(mFbDev);
117 mFbDev = NULL;
118 }
119
Andy McFaddenbabba182012-09-12 13:14:51 -0700120 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
121 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
122 && !mFbDev) {
Jesse Hallef64b752013-03-18 11:28:50 -0700123 ALOGE("ERROR: failed to open framebuffer (%s), aborting",
124 strerror(-fberr));
Andy McFadden43601a22012-09-11 15:15:13 -0700125 abort();
126 }
127
Andy McFadden620685c2012-10-19 12:53:46 -0700128 // these display IDs are always reserved
Jesse Hall9e663de2013-08-16 14:28:37 -0700129 for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
Andy McFadden620685c2012-10-19 12:53:46 -0700130 mAllocatedDisplayIDs.markBit(i);
131 }
132
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700133 if (mHwc) {
134 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
135 (hwcApiVersion(mHwc) >> 24) & 0xff,
136 (hwcApiVersion(mHwc) >> 16) & 0xff);
137 if (mHwc->registerProcs) {
138 mCBContext->hwc = this;
139 mCBContext->procs.invalidate = &hook_invalidate;
140 mCBContext->procs.vsync = &hook_vsync;
141 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
142 mCBContext->procs.hotplug = &hook_hotplug;
143 else
144 mCBContext->procs.hotplug = NULL;
145 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
146 mHwc->registerProcs(mHwc, &mCBContext->procs);
Jesse Hall5880cc52012-06-05 23:40:32 -0700147 }
148
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700149 // don't need a vsync thread if we have a hardware composer
150 needVSyncThread = false;
151 // always turn vsync off when we start
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700152 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700153
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700154 // the number of displays we actually have depends on the
155 // hw composer version
Jesse Hall38efe862013-04-06 23:12:29 -0700156 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
157 // 1.3 adds support for virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700158 mNumDisplays = MAX_HWC_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700159 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
160 // 1.1 adds support for multiple displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700161 mNumDisplays = NUM_BUILTIN_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700162 } else {
163 mNumDisplays = 1;
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700164 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700165 }
166
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700167 if (mFbDev) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700168 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
169 "should only have fbdev if no hwc or hwc is 1.0");
170
Mathias Agopianf4358632012-08-22 17:16:19 -0700171 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
Mathias Agopian38e623b2012-09-20 19:27:07 -0700172 disp.connected = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700173 disp.format = mFbDev->format;
Dan Stoza7f7da322014-05-02 15:26:25 -0700174 DisplayConfig config = DisplayConfig();
175 config.width = mFbDev->width;
176 config.height = mFbDev->height;
177 config.xdpi = mFbDev->xdpi;
178 config.ydpi = mFbDev->ydpi;
179 config.refresh = nsecs_t(1e9 / mFbDev->fps);
180 disp.configs.push_back(config);
181 disp.currentConfig = 0;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700182 } else if (mHwc) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700183 // here we're guaranteed to have at least HWC 1.1
Jesse Hall9e663de2013-08-16 14:28:37 -0700184 for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700185 queryDisplayProperties(i);
186 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700187 }
188
Mathias Agopian3a778712012-04-09 14:16:47 -0700189 if (needVSyncThread) {
190 // we don't have VSYNC support, we need to fake it
191 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700192 }
193}
194
195HWComposer::~HWComposer() {
Andy McFaddenbabba182012-09-12 13:14:51 -0700196 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700197 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Andy McFaddenbabba182012-09-12 13:14:51 -0700198 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700199 if (mVSyncThread != NULL) {
200 mVSyncThread->requestExitAndWait();
201 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700202 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700203 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700204 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700205 if (mFbDev) {
206 framebuffer_close(mFbDev);
207 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700208 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700209}
210
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700211// Load and prepare the hardware composer module. Sets mHwc.
212void HWComposer::loadHwcModule()
213{
214 hw_module_t const* module;
215
216 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
217 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
218 return;
219 }
220
221 int err = hwc_open_1(module, &mHwc);
222 if (err) {
223 ALOGE("%s device failed to initialize (%s)",
224 HWC_HARDWARE_COMPOSER, strerror(-err));
225 return;
226 }
227
228 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
229 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
230 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
231 ALOGE("%s device version %#x unsupported, will not be used",
232 HWC_HARDWARE_COMPOSER, mHwc->common.version);
233 hwc_close_1(mHwc);
234 mHwc = NULL;
235 return;
236 }
237}
238
239// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
Jesse Hallef64b752013-03-18 11:28:50 -0700240int HWComposer::loadFbHalModule()
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700241{
242 hw_module_t const* module;
243
Jesse Hallef64b752013-03-18 11:28:50 -0700244 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
245 if (err != 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700246 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
Jesse Hallef64b752013-03-18 11:28:50 -0700247 return err;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700248 }
249
Jesse Hallef64b752013-03-18 11:28:50 -0700250 return framebuffer_open(module, &mFbDev);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700251}
252
Mathias Agopiana350ff92010-08-10 17:14:02 -0700253status_t HWComposer::initCheck() const {
254 return mHwc ? NO_ERROR : NO_INIT;
255}
256
Jesse Hallbbd164a2012-08-21 12:05:09 -0700257void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
258 cb_context* ctx = reinterpret_cast<cb_context*>(
259 const_cast<hwc_procs_t*>(procs));
260 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700261}
262
Jesse Hall1bd20e02012-08-29 10:47:52 -0700263void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700264 int64_t timestamp) {
265 cb_context* ctx = reinterpret_cast<cb_context*>(
266 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700267 ctx->hwc->vsync(disp, timestamp);
268}
269
270void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
271 int connected) {
272 cb_context* ctx = reinterpret_cast<cb_context*>(
273 const_cast<hwc_procs_t*>(procs));
274 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700275}
276
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700277void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700278 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700279}
280
Jesse Hall1bd20e02012-08-29 10:47:52 -0700281void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopianbef42c52013-08-21 17:45:46 -0700282 if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
Jesse Hall8e26b282013-10-14 12:36:45 -0700283 {
284 Mutex::Autolock _l(mLock);
285
286 // There have been reports of HWCs that signal several vsync events
287 // with the same timestamp when turning the display off and on. This
288 // is a bug in the HWC implementation, but filter the extra events
289 // out here so they don't cause havoc downstream.
290 if (timestamp == mLastHwVSync[disp]) {
Michael Lentine258ee432014-07-30 16:24:48 -0700291 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
Jesse Hall8e26b282013-10-14 12:36:45 -0700292 timestamp);
293 return;
294 }
295
296 mLastHwVSync[disp] = timestamp;
297 }
298
Mathias Agopianbef42c52013-08-21 17:45:46 -0700299 char tag[16];
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700300 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700301 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
302
303 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700304 }
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700305}
306
Jesse Hall1bd20e02012-08-29 10:47:52 -0700307void HWComposer::hotplug(int disp, int connected) {
Michael Lentine10613dc2015-05-12 18:04:26 -0700308 if (disp >= VIRTUAL_DISPLAY_ID_BASE) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700309 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
310 disp, connected);
311 return;
312 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700313 queryDisplayProperties(disp);
Michael Lentine10613dc2015-05-12 18:04:26 -0700314 // Do not teardown or recreate the primary display
315 if (disp != HWC_DISPLAY_PRIMARY) {
316 mEventHandler.onHotplugReceived(disp, bool(connected));
317 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700318}
319
Jesse Hallbacc28e2014-09-28 12:14:12 -0700320static float getDefaultDensity(uint32_t width, uint32_t height) {
321 // Default density is based on TVs: 1080p displays get XHIGH density,
322 // lower-resolution displays get TV density. Maybe eventually we'll need
323 // to update it for 4K displays, though hopefully those just report
324 // accurate DPI information to begin with. This is also used for virtual
325 // displays and even primary displays with older hwcomposers, so be
326 // careful about orientation.
327
328 uint32_t h = width < height ? width : height;
329 if (h >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
330 else return ACONFIGURATION_DENSITY_TV;
Jesse Hall1c569c42013-04-05 13:44:52 -0700331}
332
Jesse Hall1bd20e02012-08-29 10:47:52 -0700333static const uint32_t DISPLAY_ATTRIBUTES[] = {
334 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700335 HWC_DISPLAY_WIDTH,
336 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700337 HWC_DISPLAY_DPI_X,
338 HWC_DISPLAY_DPI_Y,
339 HWC_DISPLAY_NO_ATTRIBUTE,
340};
341#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
342
Mathias Agopian1604f772012-09-18 21:54:42 -0700343status_t HWComposer::queryDisplayProperties(int disp) {
344
Mathias Agopianda27af92012-09-13 18:17:13 -0700345 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700346
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700347 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700348 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
349 memset(values, 0, sizeof(values));
350
Dan Stoza7f7da322014-05-02 15:26:25 -0700351 const size_t MAX_NUM_CONFIGS = 128;
352 uint32_t configs[MAX_NUM_CONFIGS] = {0};
353 size_t numConfigs = MAX_NUM_CONFIGS;
354 status_t err = mHwc->getDisplayConfigs(mHwc, disp, configs, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700355 if (err != NO_ERROR) {
356 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700357 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700358 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700359 }
360
Dan Stoza7f7da322014-05-02 15:26:25 -0700361 mDisplayData[disp].currentConfig = 0;
362 for (size_t c = 0; c < numConfigs; ++c) {
363 err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
364 DISPLAY_ATTRIBUTES, values);
365 if (err != NO_ERROR) {
366 // we can't get this display's info. turn it off.
367 mDisplayData[disp].connected = false;
368 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700369 }
Dan Stoza7f7da322014-05-02 15:26:25 -0700370
371 DisplayConfig config = DisplayConfig();
372 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
373 switch (DISPLAY_ATTRIBUTES[i]) {
374 case HWC_DISPLAY_VSYNC_PERIOD:
375 config.refresh = nsecs_t(values[i]);
376 break;
377 case HWC_DISPLAY_WIDTH:
378 config.width = values[i];
379 break;
380 case HWC_DISPLAY_HEIGHT:
381 config.height = values[i];
382 break;
383 case HWC_DISPLAY_DPI_X:
384 config.xdpi = values[i] / 1000.0f;
385 break;
386 case HWC_DISPLAY_DPI_Y:
387 config.ydpi = values[i] / 1000.0f;
388 break;
389 default:
Michael Lentineb54ee772014-08-07 14:54:23 -0700390 ALOG_ASSERT(false, "unknown display attribute[%zu] %#x",
Dan Stoza7f7da322014-05-02 15:26:25 -0700391 i, DISPLAY_ATTRIBUTES[i]);
392 break;
393 }
394 }
395
396 if (config.xdpi == 0.0f || config.ydpi == 0.0f) {
Jesse Hallbacc28e2014-09-28 12:14:12 -0700397 float dpi = getDefaultDensity(config.width, config.height);
Dan Stoza7f7da322014-05-02 15:26:25 -0700398 config.xdpi = dpi;
399 config.ydpi = dpi;
400 }
401
402 mDisplayData[disp].configs.push_back(config);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700403 }
404
Mathias Agopianf5a33922012-09-19 18:16:22 -0700405 // FIXME: what should we set the format to?
406 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
407 mDisplayData[disp].connected = true;
Mathias Agopian1604f772012-09-18 21:54:42 -0700408 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700409}
410
Jesse Hall1c569c42013-04-05 13:44:52 -0700411status_t HWComposer::setVirtualDisplayProperties(int32_t id,
412 uint32_t w, uint32_t h, uint32_t format) {
413 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
414 !mAllocatedDisplayIDs.hasBit(id)) {
415 return BAD_INDEX;
416 }
Dan Stoza7f7da322014-05-02 15:26:25 -0700417 size_t configId = mDisplayData[id].currentConfig;
Jesse Hall1c569c42013-04-05 13:44:52 -0700418 mDisplayData[id].format = format;
Dan Stoza7f7da322014-05-02 15:26:25 -0700419 DisplayConfig& config = mDisplayData[id].configs.editItemAt(configId);
420 config.width = w;
421 config.height = h;
Jesse Hallbacc28e2014-09-28 12:14:12 -0700422 config.xdpi = config.ydpi = getDefaultDensity(w, h);
Jesse Hall1c569c42013-04-05 13:44:52 -0700423 return NO_ERROR;
424}
425
Mathias Agopiane60b0682012-08-21 23:34:09 -0700426int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700427 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700428 return NO_MEMORY;
429 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700430 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
431 mAllocatedDisplayIDs.markBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800432 mDisplayData[id].connected = true;
Dan Stoza7f7da322014-05-02 15:26:25 -0700433 mDisplayData[id].configs.resize(1);
434 mDisplayData[id].currentConfig = 0;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700435 return id;
436}
437
438status_t HWComposer::freeDisplayId(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700439 if (id < NUM_BUILTIN_DISPLAYS) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700440 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700441 return BAD_VALUE;
442 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700443 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700444 return BAD_INDEX;
445 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700446 mAllocatedDisplayIDs.clearBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800447 mDisplayData[id].connected = false;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700448 return NO_ERROR;
449}
450
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700451nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700452 // this returns the last refresh timestamp.
453 // if the last one is not available, we estimate it based on
454 // the refresh period and whatever closest timestamp we have.
455 Mutex::Autolock _l(mLock);
456 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Dan Stoza7f7da322014-05-02 15:26:25 -0700457 size_t configId = mDisplayData[disp].currentConfig;
458 return now - ((now - mLastHwVSync[disp]) %
459 mDisplayData[disp].configs[configId].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700460}
461
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800462sp<Fence> HWComposer::getDisplayFence(int disp) const {
463 return mDisplayData[disp].lastDisplayFence;
464}
465
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700466uint32_t HWComposer::getFormat(int disp) const {
Ajay Dudani4e3e30c2015-05-03 11:44:55 -0700467 if (static_cast<uint32_t>(disp) >= MAX_HWC_DISPLAYS || !mAllocatedDisplayIDs.hasBit(disp)) {
Jesse Hall19e87292013-12-23 21:02:15 -0800468 return HAL_PIXEL_FORMAT_RGBA_8888;
469 } else {
470 return mDisplayData[disp].format;
471 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700472}
473
Dan Stoza7f7da322014-05-02 15:26:25 -0700474bool HWComposer::isConnected(int disp) const {
475 return mDisplayData[disp].connected;
476}
477
478uint32_t HWComposer::getWidth(int disp) const {
479 size_t currentConfig = mDisplayData[disp].currentConfig;
480 return mDisplayData[disp].configs[currentConfig].width;
481}
482
483uint32_t HWComposer::getHeight(int disp) const {
484 size_t currentConfig = mDisplayData[disp].currentConfig;
485 return mDisplayData[disp].configs[currentConfig].height;
486}
487
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700488float HWComposer::getDpiX(int disp) const {
Dan Stoza7f7da322014-05-02 15:26:25 -0700489 size_t currentConfig = mDisplayData[disp].currentConfig;
490 return mDisplayData[disp].configs[currentConfig].xdpi;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700491}
492
493float HWComposer::getDpiY(int disp) const {
Dan Stoza7f7da322014-05-02 15:26:25 -0700494 size_t currentConfig = mDisplayData[disp].currentConfig;
495 return mDisplayData[disp].configs[currentConfig].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700496}
497
Dan Stoza7f7da322014-05-02 15:26:25 -0700498nsecs_t HWComposer::getRefreshPeriod(int disp) const {
499 size_t currentConfig = mDisplayData[disp].currentConfig;
500 return mDisplayData[disp].configs[currentConfig].refresh;
501}
502
503const Vector<HWComposer::DisplayConfig>& HWComposer::getConfigs(int disp) const {
504 return mDisplayData[disp].configs;
505}
506
507size_t HWComposer::getCurrentConfig(int disp) const {
508 return mDisplayData[disp].currentConfig;
Mathias Agopianf5a33922012-09-19 18:16:22 -0700509}
510
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700511void HWComposer::eventControl(int disp, int event, int enabled) {
512 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
Andy McFadden620685c2012-10-19 12:53:46 -0700513 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
514 event, disp, enabled);
515 return;
516 }
517 if (event != EVENT_VSYNC) {
518 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
519 event, disp, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700520 return;
521 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700522 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700523 if (mHwc && !mDebugForceFakeVSync) {
524 // NOTE: we use our own internal lock here because we have to call
525 // into the HWC with the lock held, and we want to make sure
526 // that even if HWC blocks (which it shouldn't), it won't
527 // affect other threads.
528 Mutex::Autolock _l(mEventControlLock);
529 const int32_t eventBit = 1UL << event;
530 const int32_t newValue = enabled ? eventBit : 0;
531 const int32_t oldValue = mDisplayData[disp].events & eventBit;
532 if (newValue != oldValue) {
533 ATRACE_CALL();
534 err = mHwc->eventControl(mHwc, disp, event, enabled);
535 if (!err) {
536 int32_t& events(mDisplayData[disp].events);
537 events = (events & ~eventBit) | newValue;
Jesse Hall4d407a02014-08-25 14:44:16 -0700538
539 char tag[16];
540 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
541 ATRACE_INT(tag, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700542 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700543 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700544 // error here should not happen -- not sure what we should
545 // do if it does.
546 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
547 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700548 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700549
550 if (err == NO_ERROR && mVSyncThread != NULL) {
551 mVSyncThread->setEnabled(enabled);
552 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700553}
554
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700555status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700556 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700557 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700558 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700559
Mathias Agopian45721772010-08-12 15:03:26 -0700560 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700561 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700562 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
563 // we need space for the HWC_FRAMEBUFFER_TARGET
564 numLayers++;
565 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700566 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700567 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700568 + numLayers * sizeof(hwc_layer_1_t);
569 free(disp.list);
570 disp.list = (hwc_display_contents_1_t*)malloc(size);
571 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700572 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700573 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
574 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
575 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
Dan Stoza7f7da322014-05-02 15:26:25 -0700576 const DisplayConfig& currentConfig =
577 disp.configs[disp.currentConfig];
578 const hwc_rect_t r = { 0, 0,
579 (int) currentConfig.width, (int) currentConfig.height };
Mathias Agopianda27af92012-09-13 18:17:13 -0700580 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
581 disp.framebufferTarget->hints = 0;
582 disp.framebufferTarget->flags = 0;
583 disp.framebufferTarget->handle = disp.fbTargetHandle;
584 disp.framebufferTarget->transform = 0;
585 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
Mathias Agopian8f63c202013-09-25 20:44:34 -0700586 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
587 disp.framebufferTarget->sourceCropf.left = 0;
588 disp.framebufferTarget->sourceCropf.top = 0;
Dan Stoza7f7da322014-05-02 15:26:25 -0700589 disp.framebufferTarget->sourceCropf.right =
590 currentConfig.width;
591 disp.framebufferTarget->sourceCropf.bottom =
592 currentConfig.height;
Mathias Agopian8f63c202013-09-25 20:44:34 -0700593 } else {
594 disp.framebufferTarget->sourceCrop = r;
595 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700596 disp.framebufferTarget->displayFrame = r;
597 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
598 disp.framebufferTarget->visibleRegionScreen.rects =
599 &disp.framebufferTarget->displayFrame;
600 disp.framebufferTarget->acquireFenceFd = -1;
601 disp.framebufferTarget->releaseFenceFd = -1;
Mathias Agopian70a6e882013-03-21 16:25:12 -0700602 disp.framebufferTarget->planeAlpha = 0xFF;
Mathias Agopianda27af92012-09-13 18:17:13 -0700603 }
Jesse Halldb276212012-09-07 11:20:56 -0700604 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700605 disp.list->flags = HWC_GEOMETRY_CHANGED;
606 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700607 }
608 return NO_ERROR;
609}
610
Mathias Agopianda27af92012-09-13 18:17:13 -0700611status_t HWComposer::setFramebufferTarget(int32_t id,
612 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
613 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
614 return BAD_INDEX;
615 }
616 DisplayData& disp(mDisplayData[id]);
617 if (!disp.framebufferTarget) {
618 // this should never happen, but apparently eglCreateWindowSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800619 // triggers a Surface::queueBuffer() on some
Mathias Agopianda27af92012-09-13 18:17:13 -0700620 // devices (!?) -- log and ignore.
621 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianda27af92012-09-13 18:17:13 -0700622 return NO_ERROR;
623 }
624
625 int acquireFenceFd = -1;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800626 if (acquireFence->isValid()) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700627 acquireFenceFd = acquireFence->dup();
628 }
629
630 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
631 disp.fbTargetHandle = buf->handle;
632 disp.framebufferTarget->handle = disp.fbTargetHandle;
633 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
634 return NO_ERROR;
635}
636
Mathias Agopiane60b0682012-08-21 23:34:09 -0700637status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700638 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700639 DisplayData& disp(mDisplayData[i]);
640 if (disp.framebufferTarget) {
641 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
642 // DO NOT reset the handle field to NULL, because it's possible
643 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
644 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700645 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700646 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
647 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700648 if (!disp.connected && disp.list != NULL) {
Michael Lentine258ee432014-07-30 16:24:48 -0700649 ALOGW("WARNING: disp %zu: connected, non-null list, layers=%zu",
Andy McFadden27ec5732012-10-02 19:04:45 -0700650 i, disp.list->numHwLayers);
651 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700652 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700653 if (mLists[i]) {
Jesse Hall38efe862013-04-06 23:12:29 -0700654 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
Jesse Hallf7a67582013-11-05 16:12:31 -0800655 mLists[i]->outbuf = disp.outbufHandle;
Jesse Halldb276212012-09-07 11:20:56 -0700656 mLists[i]->outbufAcquireFenceFd = -1;
657 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
658 // garbage data to catch improper use
659 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
660 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
661 } else {
662 mLists[i]->dpy = EGL_NO_DISPLAY;
663 mLists[i]->sur = EGL_NO_SURFACE;
664 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700665 }
666 }
Jesse Halldb276212012-09-07 11:20:56 -0700667
Mathias Agopianf4358632012-08-22 17:16:19 -0700668 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700669 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
670
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700671 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700672 // here we're just making sure that "skip" layers are set
673 // to HWC_FRAMEBUFFER and we're also counting how many layers
674 // we have of each type.
Jesse Halld05a17f2013-09-30 16:49:30 -0700675 //
676 // If there are no window layers, we treat the display has having FB
677 // composition, because SurfaceFlinger will use GLES to draw the
678 // wormhole region.
Mathias Agopianf4358632012-08-22 17:16:19 -0700679 for (size_t i=0 ; i<mNumDisplays ; i++) {
680 DisplayData& disp(mDisplayData[i]);
681 disp.hasFbComp = false;
682 disp.hasOvComp = false;
683 if (disp.list) {
684 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
685 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700686
687 //ALOGD("prepare: %d, type=%d, handle=%p",
688 // i, l.compositionType, l.handle);
689
Mathias Agopianf4358632012-08-22 17:16:19 -0700690 if (l.flags & HWC_SKIP_LAYER) {
691 l.compositionType = HWC_FRAMEBUFFER;
692 }
693 if (l.compositionType == HWC_FRAMEBUFFER) {
694 disp.hasFbComp = true;
695 }
696 if (l.compositionType == HWC_OVERLAY) {
697 disp.hasOvComp = true;
698 }
Riley Andrews03414a12014-07-01 14:22:59 -0700699 if (l.compositionType == HWC_CURSOR_OVERLAY) {
700 disp.hasOvComp = true;
701 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700702 }
Jesse Halld05a17f2013-09-30 16:49:30 -0700703 if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
704 disp.hasFbComp = true;
705 }
706 } else {
707 disp.hasFbComp = true;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700708 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700709 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700710 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700711 return (status_t)err;
712}
713
Mathias Agopiane60b0682012-08-21 23:34:09 -0700714bool HWComposer::hasHwcComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700715 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700716 return false;
717 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700718}
719
Mathias Agopiane60b0682012-08-21 23:34:09 -0700720bool HWComposer::hasGlesComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700721 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
722 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700723 return mDisplayData[id].hasFbComp;
724}
725
Jesse Hall13f01cb2013-03-20 11:37:21 -0700726sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700727 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Jesse Hall13f01cb2013-03-20 11:37:21 -0700728 return Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700729
730 int fd = INVALID_OPERATION;
731 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
732 const DisplayData& disp(mDisplayData[id]);
733 if (disp.framebufferTarget) {
734 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700735 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700736 disp.framebufferTarget->releaseFenceFd = -1;
737 }
738 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700739 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700740}
741
Mathias Agopian30bcc612012-08-22 15:39:48 -0700742status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700743 int err = NO_ERROR;
744 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700745 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700746 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700747 // by the (dpy, sur) fields and we are guaranteed to have only
748 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700749 mLists[0]->dpy = eglGetCurrentDisplay();
750 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700751 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700752
Jesse Hallafaf14b2013-03-20 13:42:29 -0700753 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
Jesse Hall80e0a392013-03-15 12:32:10 -0700754 DisplayData& disp(mDisplayData[i]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700755 if (disp.outbufHandle) {
756 mLists[i]->outbuf = disp.outbufHandle;
Jesse Hall80e0a392013-03-15 12:32:10 -0700757 mLists[i]->outbufAcquireFenceFd =
Jesse Hall851cfe82013-03-20 13:44:00 -0700758 disp.outbufAcquireFence->dup();
Jesse Hall80e0a392013-03-15 12:32:10 -0700759 }
760 }
761
Mathias Agopian30bcc612012-08-22 15:39:48 -0700762 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700763
764 for (size_t i=0 ; i<mNumDisplays ; i++) {
765 DisplayData& disp(mDisplayData[i]);
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800766 disp.lastDisplayFence = disp.lastRetireFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800767 disp.lastRetireFence = Fence::NO_FENCE;
Mathias Agopianf4358632012-08-22 17:16:19 -0700768 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700769 if (disp.list->retireFenceFd != -1) {
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800770 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
Jesse Halldb276212012-09-07 11:20:56 -0700771 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700772 }
773 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
774 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700775 }
Mathias Agopian58959342010-10-07 14:57:04 -0700776 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700777 return (status_t)err;
778}
779
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700780status_t HWComposer::setPowerMode(int disp, int mode) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700781 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700782 if (mHwc) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700783 if (mode == HWC_POWER_MODE_OFF) {
784 eventControl(disp, HWC_EVENT_VSYNC, 0);
785 }
786 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
787 return (status_t)mHwc->setPowerMode(mHwc, disp, mode);
788 } else {
789 return (status_t)mHwc->blank(mHwc, disp,
790 mode == HWC_POWER_MODE_OFF ? 1 : 0);
791 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700792 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700793 return NO_ERROR;
794}
795
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700796status_t HWComposer::setActiveConfig(int disp, int mode) {
797 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Michael Lentined3e69142014-08-21 14:43:13 -0700798 DisplayData& dd(mDisplayData[disp]);
799 dd.currentConfig = mode;
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700800 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
801 return (status_t)mHwc->setActiveConfig(mHwc, disp, mode);
802 } else {
803 LOG_FATAL_IF(mode != 0);
804 }
805 return NO_ERROR;
806}
807
Andy McFadden27ec5732012-10-02 19:04:45 -0700808void HWComposer::disconnectDisplay(int disp) {
Andy McFadden5a8f9012012-10-04 19:09:45 -0700809 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
Andy McFadden27ec5732012-10-02 19:04:45 -0700810 DisplayData& dd(mDisplayData[disp]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700811 free(dd.list);
812 dd.list = NULL;
813 dd.framebufferTarget = NULL; // points into dd.list
814 dd.fbTargetHandle = NULL;
815 dd.outbufHandle = NULL;
816 dd.lastRetireFence = Fence::NO_FENCE;
817 dd.lastDisplayFence = Fence::NO_FENCE;
818 dd.outbufAcquireFence = Fence::NO_FENCE;
Naseer Ahmed949ea082014-08-20 16:31:58 -0400819 // clear all the previous configs and repopulate when a new
820 // device is added
821 dd.configs.clear();
Andy McFadden27ec5732012-10-02 19:04:45 -0700822}
823
Mathias Agopiancde87a32012-09-13 14:09:01 -0700824int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700825 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700826 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
827 // is supported by the implementation. we can only be in this case
828 // if we have HWC 1.1
829 return HAL_PIXEL_FORMAT_RGBA_8888;
830 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700831 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700832 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700833 }
834}
835
Mathias Agopianda27af92012-09-13 18:17:13 -0700836bool HWComposer::supportsFramebufferTarget() const {
837 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
838}
839
840int HWComposer::fbPost(int32_t id,
841 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700842 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700843 return setFramebufferTarget(id, acquireFence, buffer);
844 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700845 acquireFence->waitForever("HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700846 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700847 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700848}
849
850int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700851 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700852 return NO_ERROR;
853
854 if (mFbDev->compositionComplete) {
855 return mFbDev->compositionComplete(mFbDev);
856 } else {
857 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700858 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700859}
860
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700861void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700862 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700863 const size_t SIZE = 4096;
864 char buffer[SIZE];
865 mFbDev->dump(mFbDev, buffer, SIZE);
866 result.append(buffer);
867 }
868}
869
Jesse Hall851cfe82013-03-20 13:44:00 -0700870status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
871 const sp<GraphicBuffer>& buf) {
872 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
873 return BAD_INDEX;
874 if (id < VIRTUAL_DISPLAY_ID_BASE)
875 return INVALID_OPERATION;
876
877 DisplayData& disp(mDisplayData[id]);
878 disp.outbufHandle = buf->handle;
879 disp.outbufAcquireFence = acquireFence;
880 return NO_ERROR;
881}
882
Andy McFadden41d67d72014-04-25 16:58:34 -0700883sp<Fence> HWComposer::getLastRetireFence(int32_t id) const {
Jesse Hall851cfe82013-03-20 13:44:00 -0700884 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
885 return Fence::NO_FENCE;
886 return mDisplayData[id].lastRetireFence;
887}
888
Riley Andrews03414a12014-07-01 14:22:59 -0700889status_t HWComposer::setCursorPositionAsync(int32_t id, const Rect& pos)
890{
891 if (mHwc->setCursorPositionAsync) {
892 return (status_t)mHwc->setCursorPositionAsync(mHwc, id, pos.left, pos.top);
893 }
894 else {
895 return NO_ERROR;
896 }
897}
898
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700899/*
900 * Helper template to implement a concrete HWCLayer
901 * This holds the pointer to the concrete hwc layer type
902 * and implements the "iterable" side of HWCLayer.
903 */
904template<typename CONCRETE, typename HWCTYPE>
905class Iterable : public HWComposer::HWCLayer {
906protected:
907 HWCTYPE* const mLayerList;
908 HWCTYPE* mCurrentLayer;
909 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
910 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
911 inline HWCTYPE* getLayer() { return mCurrentLayer; }
912 virtual ~Iterable() { }
913private:
914 // returns a copy of ourselves
915 virtual HWComposer::HWCLayer* dup() {
916 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
917 }
918 virtual status_t setLayer(size_t index) {
919 mCurrentLayer = &mLayerList[index];
920 return NO_ERROR;
921 }
922};
923
Jesse Hall5880cc52012-06-05 23:40:32 -0700924/*
925 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
926 * This implements the HWCLayer side of HWCIterableLayer.
927 */
928class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800929 struct hwc_composer_device_1* mHwc;
Jesse Hall5880cc52012-06-05 23:40:32 -0700930public:
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800931 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
932 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
Jesse Hall5880cc52012-06-05 23:40:32 -0700933
934 virtual int32_t getCompositionType() const {
935 return getLayer()->compositionType;
936 }
937 virtual uint32_t getHints() const {
938 return getLayer()->hints;
939 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700940 virtual sp<Fence> getAndResetReleaseFence() {
Jesse Hallef194142012-06-14 14:45:17 -0700941 int fd = getLayer()->releaseFenceFd;
942 getLayer()->releaseFenceFd = -1;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700943 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Jesse Hallef194142012-06-14 14:45:17 -0700944 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700945 virtual void setAcquireFenceFd(int fenceFd) {
946 getLayer()->acquireFenceFd = fenceFd;
947 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800948 virtual void setPerFrameDefaultState() {
949 //getLayer()->compositionType = HWC_FRAMEBUFFER;
950 }
951 virtual void setPlaneAlpha(uint8_t alpha) {
952 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
953 getLayer()->planeAlpha = alpha;
954 } else {
Mathias Agopian5fe58b82013-02-07 16:22:50 -0800955 if (alpha < 0xFF) {
956 getLayer()->flags |= HWC_SKIP_LAYER;
957 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800958 }
959 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700960 virtual void setDefaultState() {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800961 hwc_layer_1_t* const l = getLayer();
962 l->compositionType = HWC_FRAMEBUFFER;
963 l->hints = 0;
964 l->flags = HWC_SKIP_LAYER;
965 l->handle = 0;
966 l->transform = 0;
967 l->blending = HWC_BLENDING_NONE;
968 l->visibleRegionScreen.numRects = 0;
969 l->visibleRegionScreen.rects = NULL;
970 l->acquireFenceFd = -1;
971 l->releaseFenceFd = -1;
972 l->planeAlpha = 0xFF;
Jesse Hall5880cc52012-06-05 23:40:32 -0700973 }
974 virtual void setSkip(bool skip) {
975 if (skip) {
976 getLayer()->flags |= HWC_SKIP_LAYER;
977 } else {
978 getLayer()->flags &= ~HWC_SKIP_LAYER;
979 }
980 }
Riley Andrews03414a12014-07-01 14:22:59 -0700981 virtual void setIsCursorLayerHint(bool isCursor) {
982 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
983 if (isCursor) {
984 getLayer()->flags |= HWC_IS_CURSOR_LAYER;
985 }
986 else {
987 getLayer()->flags &= ~HWC_IS_CURSOR_LAYER;
988 }
989 }
990 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700991 virtual void setBlending(uint32_t blending) {
992 getLayer()->blending = blending;
993 }
994 virtual void setTransform(uint32_t transform) {
995 getLayer()->transform = transform;
996 }
997 virtual void setFrame(const Rect& frame) {
Mathias Agopian6b442672013-07-09 21:24:52 -0700998 getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
Jesse Hall5880cc52012-06-05 23:40:32 -0700999 }
Mathias Agopian6b442672013-07-09 21:24:52 -07001000 virtual void setCrop(const FloatRect& crop) {
1001 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1002 getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
1003 } else {
1004 /*
1005 * Since h/w composer didn't support a flot crop rect before version 1.3,
1006 * using integer coordinates instead produces a different output from the GL code in
1007 * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
1008 * window size ratio is large and a window crop is defined
1009 * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
1010 */
1011 hwc_rect_t& r = getLayer()->sourceCrop;
1012 r.left = int(ceilf(crop.left));
1013 r.top = int(ceilf(crop.top));
1014 r.right = int(floorf(crop.right));
1015 r.bottom= int(floorf(crop.bottom));
1016 }
Jesse Hall5880cc52012-06-05 23:40:32 -07001017 }
1018 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -07001019 // Region::getSharedBuffer creates a reference to the underlying
1020 // SharedBuffer of this Region, this reference is freed
1021 // in onDisplayed()
1022 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
1023 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
1024 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -07001025 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001026 virtual void setSurfaceDamage(const Region& reg) {
1027 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_5)) {
1028 return;
1029 }
1030 hwc_region_t& surfaceDamage = getLayer()->surfaceDamage;
1031 // We encode default full-screen damage as INVALID_RECT upstream, but as
1032 // 0 rects for HWComposer
1033 if (reg.isRect() && reg.getBounds() == Rect::INVALID_RECT) {
1034 surfaceDamage.numRects = 0;
1035 surfaceDamage.rects = NULL;
1036 return;
1037 }
1038 SharedBuffer const* sb = reg.getSharedBuffer(&surfaceDamage.numRects);
1039 surfaceDamage.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
1040 }
Jesse Hall399184a2014-03-03 15:42:54 -08001041 virtual void setSidebandStream(const sp<NativeHandle>& stream) {
1042 ALOG_ASSERT(stream->handle() != NULL);
1043 getLayer()->compositionType = HWC_SIDEBAND;
1044 getLayer()->sidebandStream = stream->handle();
1045 }
Jesse Hall5880cc52012-06-05 23:40:32 -07001046 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
1047 if (buffer == 0 || buffer->handle == 0) {
1048 getLayer()->compositionType = HWC_FRAMEBUFFER;
1049 getLayer()->flags |= HWC_SKIP_LAYER;
1050 getLayer()->handle = 0;
1051 } else {
Jesse Hall399184a2014-03-03 15:42:54 -08001052 if (getLayer()->compositionType == HWC_SIDEBAND) {
1053 // If this was a sideband layer but the stream was removed, reset
1054 // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare.
1055 getLayer()->compositionType = HWC_FRAMEBUFFER;
1056 }
Jesse Hall5880cc52012-06-05 23:40:32 -07001057 getLayer()->handle = buffer->handle;
1058 }
1059 }
Mathias Agopianc3973602012-08-31 17:51:25 -07001060 virtual void onDisplayed() {
1061 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
1062 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
1063 if (sb) {
1064 sb->release();
1065 // not technically needed but safer
1066 visibleRegion.numRects = 0;
1067 visibleRegion.rects = NULL;
1068 }
Jesse Halle25d0052012-09-05 13:03:10 -07001069
1070 getLayer()->acquireFenceFd = -1;
Dan Stozaee44edd2015-03-23 15:50:23 -07001071
1072 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_5)) {
1073 return;
1074 }
1075
1076 hwc_region_t& surfaceDamage = getLayer()->surfaceDamage;
1077 sb = SharedBuffer::bufferFromData(surfaceDamage.rects);
1078 if (sb) {
1079 sb->release();
1080 surfaceDamage.numRects = 0;
1081 surfaceDamage.rects = NULL;
1082 }
Mathias Agopianc3973602012-08-31 17:51:25 -07001083 }
Jesse Hall5880cc52012-06-05 23:40:32 -07001084};
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001085
1086/*
1087 * returns an iterator initialized at a given index in the layer list
1088 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001089HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -07001090 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001091 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -07001092 }
1093 const DisplayData& disp(mDisplayData[id]);
1094 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001095 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -07001096 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -08001097 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -07001098}
1099
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001100/*
1101 * returns an iterator on the beginning of the layer list
1102 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001103HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001104 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001105}
1106
1107/*
1108 * returns an iterator on the end of the layer list
1109 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001110HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -07001111 size_t numLayers = 0;
1112 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
1113 const DisplayData& disp(mDisplayData[id]);
1114 if (mHwc && disp.list) {
1115 numLayers = disp.list->numHwLayers;
1116 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
1117 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
1118 // which we ignore when iterating through the layer list.
1119 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
1120 if (numLayers) {
1121 numLayers--;
1122 }
1123 }
1124 }
1125 }
1126 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001127}
1128
Andy McFadden4df87bd2014-04-21 18:08:54 -07001129// Converts a PixelFormat to a human-readable string. Max 11 chars.
1130// (Could use a table of prefab String8 objects.)
1131static String8 getFormatStr(PixelFormat format) {
1132 switch (format) {
1133 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
1134 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
1135 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
1136 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
1137 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -07001138 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
1139 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -07001140 default:
1141 String8 result;
1142 result.appendFormat("? %08x", format);
1143 return result;
1144 }
1145}
1146
Mathias Agopian74d211a2013-04-22 16:55:35 +02001147void HWComposer::dump(String8& result) const {
Jesse Hallb685c542012-07-31 14:32:56 -07001148 if (mHwc) {
Andy McFadden4df87bd2014-04-21 18:08:54 -07001149 result.appendFormat("Hardware Composer state (version %08x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -07001150 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
1151 for (size_t i=0 ; i<mNumDisplays ; i++) {
1152 const DisplayData& disp(mDisplayData[i]);
Jesse Hall02d86562013-03-25 14:43:23 -07001153 if (!disp.connected)
1154 continue;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001155
Mathias Agopian13127d82013-03-05 17:47:11 -08001156 const Vector< sp<Layer> >& visibleLayersSortedByZ =
Mathias Agopiancb558572012-10-04 15:58:54 -07001157 mFlinger->getLayerSortedByZForHwcDisplay(i);
1158
Dan Stoza7f7da322014-05-02 15:26:25 -07001159
1160 result.appendFormat(" Display[%zd] configurations (* current):\n", i);
1161 for (size_t c = 0; c < disp.configs.size(); ++c) {
1162 const DisplayConfig& config(disp.configs[c]);
1163 result.appendFormat(" %s%zd: %ux%u, xdpi=%f, ydpi=%f, refresh=%" PRId64 "\n",
1164 c == disp.currentConfig ? "* " : "", c, config.width, config.height,
1165 config.xdpi, config.ydpi, config.refresh);
1166 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001167
Jesse Hall02d86562013-03-25 14:43:23 -07001168 if (disp.list) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001169 result.appendFormat(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001170 " numHwLayers=%zu, flags=%08x\n",
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001171 disp.list->numHwLayers, disp.list->flags);
1172
Mathias Agopianf4358632012-08-22 17:16:19 -07001173 result.append(
Andy McFadden4df87bd2014-04-21 18:08:54 -07001174 " type | handle | hint | flag | tr | blnd | format | source crop (l,t,r,b) | frame | name \n"
1175 "-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------\n");
1176 // " _________ | ________ | ____ | ____ | __ | ____ | ___________ |_____._,_____._,_____._,_____._ |_____,_____,_____,_____ | ___...
Mathias Agopianf4358632012-08-22 17:16:19 -07001177 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1178 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -07001179 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -07001180 String8 name("unknown");
Mathias Agopiancb558572012-10-04 15:58:54 -07001181
Mathias Agopianda27af92012-09-13 18:17:13 -07001182 if (i < visibleLayersSortedByZ.size()) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001183 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
1184 const sp<GraphicBuffer>& buffer(
1185 layer->getActiveBuffer());
1186 if (buffer != NULL) {
1187 format = buffer->getPixelFormat();
Mathias Agopianf4358632012-08-22 17:16:19 -07001188 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001189 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -07001190 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001191
1192 int type = l.compositionType;
1193 if (type == HWC_FRAMEBUFFER_TARGET) {
1194 name = "HWC_FRAMEBUFFER_TARGET";
1195 format = disp.format;
1196 }
1197
1198 static char const* compositionTypeName[] = {
1199 "GLES",
1200 "HWC",
Andy McFadden4df87bd2014-04-21 18:08:54 -07001201 "BKGND",
Mathias Agopianda27af92012-09-13 18:17:13 -07001202 "FB TARGET",
Riley Andrews03414a12014-07-01 14:22:59 -07001203 "SIDEBAND",
1204 "HWC_CURSOR",
Mathias Agopianda27af92012-09-13 18:17:13 -07001205 "UNKNOWN"};
1206 if (type >= NELEM(compositionTypeName))
1207 type = NELEM(compositionTypeName) - 1;
1208
Andy McFadden4df87bd2014-04-21 18:08:54 -07001209 String8 formatStr = getFormatStr(format);
Jesse Hall353ddc62013-08-20 16:11:50 -07001210 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1211 result.appendFormat(
Andy McFadden4df87bd2014-04-21 18:08:54 -07001212 " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d | %s\n",
Jesse Hall353ddc62013-08-20 16:11:50 -07001213 compositionTypeName[type],
Andy McFadden4df87bd2014-04-21 18:08:54 -07001214 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
Jesse Hall353ddc62013-08-20 16:11:50 -07001215 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1216 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1217 name.string());
1218 } else {
1219 result.appendFormat(
Andy McFadden4df87bd2014-04-21 18:08:54 -07001220 " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7d,%7d,%7d,%7d |%5d,%5d,%5d,%5d | %s\n",
Jesse Hall353ddc62013-08-20 16:11:50 -07001221 compositionTypeName[type],
Andy McFadden4df87bd2014-04-21 18:08:54 -07001222 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
Jesse Hall353ddc62013-08-20 16:11:50 -07001223 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1224 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1225 name.string());
1226 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -07001227 }
1228 }
Mathias Agopian83727852010-09-23 18:13:21 -07001229 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001230 }
Mathias Agopian30bcc612012-08-22 15:39:48 -07001231
1232 if (mHwc && mHwc->dump) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02001233 const size_t SIZE = 4096;
1234 char buffer[SIZE];
Mathias Agopian30bcc612012-08-22 15:39:48 -07001235 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001236 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001237 }
1238}
1239
Mathias Agopiana350ff92010-08-10 17:14:02 -07001240// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -07001241
1242HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1243 : mHwc(hwc), mEnabled(false),
1244 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07001245 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -07001246{
1247}
1248
1249void HWComposer::VSyncThread::setEnabled(bool enabled) {
1250 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -07001251 if (mEnabled != enabled) {
1252 mEnabled = enabled;
1253 mCondition.signal();
1254 }
Mathias Agopian2965b262012-04-08 15:13:32 -07001255}
1256
1257void HWComposer::VSyncThread::onFirstRef() {
1258 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1259}
1260
1261bool HWComposer::VSyncThread::threadLoop() {
1262 { // scope for lock
1263 Mutex::Autolock _l(mLock);
1264 while (!mEnabled) {
1265 mCondition.wait(mLock);
1266 }
1267 }
1268
1269 const nsecs_t period = mRefreshPeriod;
1270 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1271 nsecs_t next_vsync = mNextFakeVSync;
1272 nsecs_t sleep = next_vsync - now;
1273 if (sleep < 0) {
1274 // we missed, find where the next vsync should be
1275 sleep = (period - ((now - next_vsync) % period));
1276 next_vsync = now + sleep;
1277 }
1278 mNextFakeVSync = next_vsync + period;
1279
1280 struct timespec spec;
1281 spec.tv_sec = next_vsync / 1000000000;
1282 spec.tv_nsec = next_vsync % 1000000000;
1283
1284 int err;
1285 do {
1286 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1287 } while (err<0 && errno == EINTR);
1288
1289 if (err == 0) {
1290 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1291 }
1292
1293 return true;
1294}
1295
Jesse Halla9a1b002013-02-27 16:39:25 -08001296HWComposer::DisplayData::DisplayData()
Dan Stoza7f7da322014-05-02 15:26:25 -07001297: configs(),
1298 currentConfig(0),
1299 format(HAL_PIXEL_FORMAT_RGBA_8888),
Jesse Halla9a1b002013-02-27 16:39:25 -08001300 connected(false),
1301 hasFbComp(false), hasOvComp(false),
1302 capacity(0), list(NULL),
1303 framebufferTarget(NULL), fbTargetHandle(0),
1304 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
Jesse Hall851cfe82013-03-20 13:44:00 -07001305 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
Jesse Halla9a1b002013-02-27 16:39:25 -08001306 events(0)
1307{}
1308
1309HWComposer::DisplayData::~DisplayData() {
1310 free(list);
1311}
1312
Mathias Agopian2965b262012-04-08 15:13:32 -07001313// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001314}; // namespace android