blob: c4ea8cca2ec11c4dec7101cd7da05dd8d3b10647 [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;
Jesse Halldb276212012-09-07 11:20:56 -0700173 disp.width = mFbDev->width;
174 disp.height = mFbDev->height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700175 disp.format = mFbDev->format;
176 disp.xdpi = mFbDev->xdpi;
177 disp.ydpi = mFbDev->ydpi;
Mathias Agopianf4358632012-08-22 17:16:19 -0700178 if (disp.refresh == 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700179 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
Mathias Agopianf4358632012-08-22 17:16:19 -0700180 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700181 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700182 if (disp.refresh == 0) {
183 disp.refresh = nsecs_t(1e9 / 60.0);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700184 ALOGW("getting VSYNC period from thin air: %lld",
185 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopianf4358632012-08-22 17:16:19 -0700186 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700187 } else if (mHwc) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700188 // here we're guaranteed to have at least HWC 1.1
Jesse Hall9e663de2013-08-16 14:28:37 -0700189 for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700190 queryDisplayProperties(i);
191 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700192 }
193
Mathias Agopian3a778712012-04-09 14:16:47 -0700194 if (needVSyncThread) {
195 // we don't have VSYNC support, we need to fake it
196 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700197 }
198}
199
200HWComposer::~HWComposer() {
Andy McFaddenbabba182012-09-12 13:14:51 -0700201 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700202 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Andy McFaddenbabba182012-09-12 13:14:51 -0700203 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700204 if (mVSyncThread != NULL) {
205 mVSyncThread->requestExitAndWait();
206 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700207 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700208 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700209 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700210 if (mFbDev) {
211 framebuffer_close(mFbDev);
212 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700213 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700214}
215
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700216// Load and prepare the hardware composer module. Sets mHwc.
217void HWComposer::loadHwcModule()
218{
219 hw_module_t const* module;
220
221 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
222 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
223 return;
224 }
225
226 int err = hwc_open_1(module, &mHwc);
227 if (err) {
228 ALOGE("%s device failed to initialize (%s)",
229 HWC_HARDWARE_COMPOSER, strerror(-err));
230 return;
231 }
232
233 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
234 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
235 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
236 ALOGE("%s device version %#x unsupported, will not be used",
237 HWC_HARDWARE_COMPOSER, mHwc->common.version);
238 hwc_close_1(mHwc);
239 mHwc = NULL;
240 return;
241 }
242}
243
244// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
Jesse Hallef64b752013-03-18 11:28:50 -0700245int HWComposer::loadFbHalModule()
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700246{
247 hw_module_t const* module;
248
Jesse Hallef64b752013-03-18 11:28:50 -0700249 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
250 if (err != 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700251 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
Jesse Hallef64b752013-03-18 11:28:50 -0700252 return err;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700253 }
254
Jesse Hallef64b752013-03-18 11:28:50 -0700255 return framebuffer_open(module, &mFbDev);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700256}
257
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258status_t HWComposer::initCheck() const {
259 return mHwc ? NO_ERROR : NO_INIT;
260}
261
Jesse Hallbbd164a2012-08-21 12:05:09 -0700262void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
263 cb_context* ctx = reinterpret_cast<cb_context*>(
264 const_cast<hwc_procs_t*>(procs));
265 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700266}
267
Jesse Hall1bd20e02012-08-29 10:47:52 -0700268void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700269 int64_t timestamp) {
270 cb_context* ctx = reinterpret_cast<cb_context*>(
271 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700272 ctx->hwc->vsync(disp, timestamp);
273}
274
275void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
276 int connected) {
277 cb_context* ctx = reinterpret_cast<cb_context*>(
278 const_cast<hwc_procs_t*>(procs));
279 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700280}
281
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700282void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700283 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700284}
285
Jesse Hall1bd20e02012-08-29 10:47:52 -0700286void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopianbef42c52013-08-21 17:45:46 -0700287 if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
Jesse Hall8e26b282013-10-14 12:36:45 -0700288 {
289 Mutex::Autolock _l(mLock);
290
291 // There have been reports of HWCs that signal several vsync events
292 // with the same timestamp when turning the display off and on. This
293 // is a bug in the HWC implementation, but filter the extra events
294 // out here so they don't cause havoc downstream.
295 if (timestamp == mLastHwVSync[disp]) {
296 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%lld)",
297 timestamp);
298 return;
299 }
300
301 mLastHwVSync[disp] = timestamp;
302 }
303
Mathias Agopianbef42c52013-08-21 17:45:46 -0700304 char tag[16];
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700305 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700306 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
307
308 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700309 }
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700310}
311
Jesse Hall1bd20e02012-08-29 10:47:52 -0700312void HWComposer::hotplug(int disp, int connected) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700313 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700314 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
315 disp, connected);
316 return;
317 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700318 queryDisplayProperties(disp);
Mathias Agopian148994e2012-09-19 17:31:36 -0700319 mEventHandler.onHotplugReceived(disp, bool(connected));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700320}
321
Jesse Hall1c569c42013-04-05 13:44:52 -0700322static float getDefaultDensity(uint32_t height) {
323 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
324 else return ACONFIGURATION_DENSITY_TV;
325}
326
Jesse Hall1bd20e02012-08-29 10:47:52 -0700327static const uint32_t DISPLAY_ATTRIBUTES[] = {
328 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700329 HWC_DISPLAY_WIDTH,
330 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700331 HWC_DISPLAY_DPI_X,
332 HWC_DISPLAY_DPI_Y,
333 HWC_DISPLAY_NO_ATTRIBUTE,
334};
335#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
336
Mathias Agopian1604f772012-09-18 21:54:42 -0700337status_t HWComposer::queryDisplayProperties(int disp) {
338
Mathias Agopianda27af92012-09-13 18:17:13 -0700339 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700340
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700341 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700342 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
343 memset(values, 0, sizeof(values));
344
345 uint32_t config;
346 size_t numConfigs = 1;
347 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700348 if (err != NO_ERROR) {
349 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700350 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700351 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700352 }
353
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700354 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
355 if (err != NO_ERROR) {
356 // we can't get this display's info. turn it off.
357 mDisplayData[disp].connected = false;
358 return err;
359 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700360
Jesse Hall1bd20e02012-08-29 10:47:52 -0700361 int32_t w = 0, h = 0;
362 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
363 switch (DISPLAY_ATTRIBUTES[i]) {
364 case HWC_DISPLAY_VSYNC_PERIOD:
365 mDisplayData[disp].refresh = nsecs_t(values[i]);
366 break;
Jesse Halldb276212012-09-07 11:20:56 -0700367 case HWC_DISPLAY_WIDTH:
368 mDisplayData[disp].width = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700369 break;
Jesse Halldb276212012-09-07 11:20:56 -0700370 case HWC_DISPLAY_HEIGHT:
371 mDisplayData[disp].height = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700372 break;
373 case HWC_DISPLAY_DPI_X:
374 mDisplayData[disp].xdpi = values[i] / 1000.0f;
375 break;
376 case HWC_DISPLAY_DPI_Y:
377 mDisplayData[disp].ydpi = values[i] / 1000.0f;
378 break;
379 default:
Mathias Agopianda27af92012-09-13 18:17:13 -0700380 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
381 i, DISPLAY_ATTRIBUTES[i]);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700382 break;
383 }
384 }
385
Mathias Agopianf5a33922012-09-19 18:16:22 -0700386 // FIXME: what should we set the format to?
387 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
388 mDisplayData[disp].connected = true;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700389 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
Jesse Hall1c569c42013-04-05 13:44:52 -0700390 float dpi = getDefaultDensity(h);
391 mDisplayData[disp].xdpi = dpi;
392 mDisplayData[disp].ydpi = dpi;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700393 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700394 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700395}
396
Jesse Hall1c569c42013-04-05 13:44:52 -0700397status_t HWComposer::setVirtualDisplayProperties(int32_t id,
398 uint32_t w, uint32_t h, uint32_t format) {
399 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
400 !mAllocatedDisplayIDs.hasBit(id)) {
401 return BAD_INDEX;
402 }
403 mDisplayData[id].width = w;
404 mDisplayData[id].height = h;
405 mDisplayData[id].format = format;
406 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h);
407 return NO_ERROR;
408}
409
Mathias Agopiane60b0682012-08-21 23:34:09 -0700410int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700411 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700412 return NO_MEMORY;
413 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700414 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
415 mAllocatedDisplayIDs.markBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800416 mDisplayData[id].connected = true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700417 return id;
418}
419
420status_t HWComposer::freeDisplayId(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700421 if (id < NUM_BUILTIN_DISPLAYS) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700422 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700423 return BAD_VALUE;
424 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700425 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700426 return BAD_INDEX;
427 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700428 mAllocatedDisplayIDs.clearBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800429 mDisplayData[id].connected = false;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700430 return NO_ERROR;
431}
432
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700433nsecs_t HWComposer::getRefreshPeriod(int disp) const {
434 return mDisplayData[disp].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700435}
436
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700437nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700438 // this returns the last refresh timestamp.
439 // if the last one is not available, we estimate it based on
440 // the refresh period and whatever closest timestamp we have.
441 Mutex::Autolock _l(mLock);
442 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700443 return now - ((now - mLastHwVSync[disp]) % mDisplayData[disp].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700444}
445
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800446sp<Fence> HWComposer::getDisplayFence(int disp) const {
447 return mDisplayData[disp].lastDisplayFence;
448}
449
Jesse Halldb276212012-09-07 11:20:56 -0700450uint32_t HWComposer::getWidth(int disp) const {
451 return mDisplayData[disp].width;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700452}
453
Jesse Halldb276212012-09-07 11:20:56 -0700454uint32_t HWComposer::getHeight(int disp) const {
455 return mDisplayData[disp].height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700456}
457
458uint32_t HWComposer::getFormat(int disp) const {
Jesse Hall19e87292013-12-23 21:02:15 -0800459 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
460 return HAL_PIXEL_FORMAT_RGBA_8888;
461 } else {
462 return mDisplayData[disp].format;
463 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700464}
465
466float HWComposer::getDpiX(int disp) const {
467 return mDisplayData[disp].xdpi;
468}
469
470float HWComposer::getDpiY(int disp) const {
471 return mDisplayData[disp].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700472}
473
Mathias Agopianf5a33922012-09-19 18:16:22 -0700474bool HWComposer::isConnected(int disp) const {
475 return mDisplayData[disp].connected;
476}
477
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700478void HWComposer::eventControl(int disp, int event, int enabled) {
479 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
Andy McFadden620685c2012-10-19 12:53:46 -0700480 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
481 event, disp, enabled);
482 return;
483 }
484 if (event != EVENT_VSYNC) {
485 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
486 event, disp, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700487 return;
488 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700489 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700490 if (mHwc && !mDebugForceFakeVSync) {
491 // NOTE: we use our own internal lock here because we have to call
492 // into the HWC with the lock held, and we want to make sure
493 // that even if HWC blocks (which it shouldn't), it won't
494 // affect other threads.
495 Mutex::Autolock _l(mEventControlLock);
496 const int32_t eventBit = 1UL << event;
497 const int32_t newValue = enabled ? eventBit : 0;
498 const int32_t oldValue = mDisplayData[disp].events & eventBit;
499 if (newValue != oldValue) {
500 ATRACE_CALL();
501 err = mHwc->eventControl(mHwc, disp, event, enabled);
502 if (!err) {
503 int32_t& events(mDisplayData[disp].events);
504 events = (events & ~eventBit) | newValue;
505 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700506 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700507 // error here should not happen -- not sure what we should
508 // do if it does.
509 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
510 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700511 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700512
513 if (err == NO_ERROR && mVSyncThread != NULL) {
514 mVSyncThread->setEnabled(enabled);
515 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700516}
517
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700518status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700519 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700520 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700521 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700522
Mathias Agopian45721772010-08-12 15:03:26 -0700523 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700524 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700525 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
526 // we need space for the HWC_FRAMEBUFFER_TARGET
527 numLayers++;
528 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700529 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700530 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700531 + numLayers * sizeof(hwc_layer_1_t);
532 free(disp.list);
533 disp.list = (hwc_display_contents_1_t*)malloc(size);
534 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700535 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700536 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
537 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
538 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800539 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
Mathias Agopianda27af92012-09-13 18:17:13 -0700540 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
541 disp.framebufferTarget->hints = 0;
542 disp.framebufferTarget->flags = 0;
543 disp.framebufferTarget->handle = disp.fbTargetHandle;
544 disp.framebufferTarget->transform = 0;
545 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
Mathias Agopian8f63c202013-09-25 20:44:34 -0700546 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
547 disp.framebufferTarget->sourceCropf.left = 0;
548 disp.framebufferTarget->sourceCropf.top = 0;
549 disp.framebufferTarget->sourceCropf.right = disp.width;
550 disp.framebufferTarget->sourceCropf.bottom = disp.height;
551 } else {
552 disp.framebufferTarget->sourceCrop = r;
553 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700554 disp.framebufferTarget->displayFrame = r;
555 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
556 disp.framebufferTarget->visibleRegionScreen.rects =
557 &disp.framebufferTarget->displayFrame;
558 disp.framebufferTarget->acquireFenceFd = -1;
559 disp.framebufferTarget->releaseFenceFd = -1;
Mathias Agopian70a6e882013-03-21 16:25:12 -0700560 disp.framebufferTarget->planeAlpha = 0xFF;
Mathias Agopianda27af92012-09-13 18:17:13 -0700561 }
Jesse Halldb276212012-09-07 11:20:56 -0700562 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700563 disp.list->flags = HWC_GEOMETRY_CHANGED;
564 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700565 }
566 return NO_ERROR;
567}
568
Mathias Agopianda27af92012-09-13 18:17:13 -0700569status_t HWComposer::setFramebufferTarget(int32_t id,
570 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
571 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
572 return BAD_INDEX;
573 }
574 DisplayData& disp(mDisplayData[id]);
575 if (!disp.framebufferTarget) {
576 // this should never happen, but apparently eglCreateWindowSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800577 // triggers a Surface::queueBuffer() on some
Mathias Agopianda27af92012-09-13 18:17:13 -0700578 // devices (!?) -- log and ignore.
579 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianda27af92012-09-13 18:17:13 -0700580 return NO_ERROR;
581 }
582
583 int acquireFenceFd = -1;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800584 if (acquireFence->isValid()) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700585 acquireFenceFd = acquireFence->dup();
586 }
587
588 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
589 disp.fbTargetHandle = buf->handle;
590 disp.framebufferTarget->handle = disp.fbTargetHandle;
591 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
592 return NO_ERROR;
593}
594
Mathias Agopiane60b0682012-08-21 23:34:09 -0700595status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700596 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700597 DisplayData& disp(mDisplayData[i]);
598 if (disp.framebufferTarget) {
599 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
600 // DO NOT reset the handle field to NULL, because it's possible
601 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
602 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700603 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700604 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
605 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700606 if (!disp.connected && disp.list != NULL) {
607 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
608 i, disp.list->numHwLayers);
609 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700610 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700611 if (mLists[i]) {
Jesse Hall38efe862013-04-06 23:12:29 -0700612 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
Jesse Hallf7a67582013-11-05 16:12:31 -0800613 mLists[i]->outbuf = disp.outbufHandle;
Jesse Halldb276212012-09-07 11:20:56 -0700614 mLists[i]->outbufAcquireFenceFd = -1;
615 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
616 // garbage data to catch improper use
617 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
618 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
619 } else {
620 mLists[i]->dpy = EGL_NO_DISPLAY;
621 mLists[i]->sur = EGL_NO_SURFACE;
622 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700623 }
624 }
Jesse Halldb276212012-09-07 11:20:56 -0700625
Mathias Agopianf4358632012-08-22 17:16:19 -0700626 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700627 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
628
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700629 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700630 // here we're just making sure that "skip" layers are set
631 // to HWC_FRAMEBUFFER and we're also counting how many layers
632 // we have of each type.
Jesse Halld05a17f2013-09-30 16:49:30 -0700633 //
634 // If there are no window layers, we treat the display has having FB
635 // composition, because SurfaceFlinger will use GLES to draw the
636 // wormhole region.
Mathias Agopianf4358632012-08-22 17:16:19 -0700637 for (size_t i=0 ; i<mNumDisplays ; i++) {
638 DisplayData& disp(mDisplayData[i]);
639 disp.hasFbComp = false;
640 disp.hasOvComp = false;
641 if (disp.list) {
642 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
643 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700644
645 //ALOGD("prepare: %d, type=%d, handle=%p",
646 // i, l.compositionType, l.handle);
647
Mathias Agopianf4358632012-08-22 17:16:19 -0700648 if (l.flags & HWC_SKIP_LAYER) {
649 l.compositionType = HWC_FRAMEBUFFER;
650 }
651 if (l.compositionType == HWC_FRAMEBUFFER) {
652 disp.hasFbComp = true;
653 }
654 if (l.compositionType == HWC_OVERLAY) {
655 disp.hasOvComp = true;
656 }
657 }
Jesse Halld05a17f2013-09-30 16:49:30 -0700658 if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
659 disp.hasFbComp = true;
660 }
661 } else {
662 disp.hasFbComp = true;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700663 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700664 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700665 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700666 return (status_t)err;
667}
668
Mathias Agopiane60b0682012-08-21 23:34:09 -0700669bool HWComposer::hasHwcComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700670 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700671 return false;
672 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700673}
674
Mathias Agopiane60b0682012-08-21 23:34:09 -0700675bool HWComposer::hasGlesComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700676 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
677 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700678 return mDisplayData[id].hasFbComp;
679}
680
Jesse Hall13f01cb2013-03-20 11:37:21 -0700681sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700682 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Jesse Hall13f01cb2013-03-20 11:37:21 -0700683 return Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700684
685 int fd = INVALID_OPERATION;
686 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
687 const DisplayData& disp(mDisplayData[id]);
688 if (disp.framebufferTarget) {
689 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700690 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700691 disp.framebufferTarget->releaseFenceFd = -1;
692 }
693 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700694 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700695}
696
Mathias Agopian30bcc612012-08-22 15:39:48 -0700697status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700698 int err = NO_ERROR;
699 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700700 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700701 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700702 // by the (dpy, sur) fields and we are guaranteed to have only
703 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700704 mLists[0]->dpy = eglGetCurrentDisplay();
705 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700706 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700707
Jesse Hallafaf14b2013-03-20 13:42:29 -0700708 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
Jesse Hall80e0a392013-03-15 12:32:10 -0700709 DisplayData& disp(mDisplayData[i]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700710 if (disp.outbufHandle) {
711 mLists[i]->outbuf = disp.outbufHandle;
Jesse Hall80e0a392013-03-15 12:32:10 -0700712 mLists[i]->outbufAcquireFenceFd =
Jesse Hall851cfe82013-03-20 13:44:00 -0700713 disp.outbufAcquireFence->dup();
Jesse Hall80e0a392013-03-15 12:32:10 -0700714 }
715 }
716
Mathias Agopian30bcc612012-08-22 15:39:48 -0700717 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700718
719 for (size_t i=0 ; i<mNumDisplays ; i++) {
720 DisplayData& disp(mDisplayData[i]);
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800721 disp.lastDisplayFence = disp.lastRetireFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800722 disp.lastRetireFence = Fence::NO_FENCE;
Mathias Agopianf4358632012-08-22 17:16:19 -0700723 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700724 if (disp.list->retireFenceFd != -1) {
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800725 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
Jesse Halldb276212012-09-07 11:20:56 -0700726 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700727 }
728 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
729 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700730 }
Mathias Agopian58959342010-10-07 14:57:04 -0700731 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700732 return (status_t)err;
733}
734
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700735status_t HWComposer::release(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700736 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700737 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700738 eventControl(disp, HWC_EVENT_VSYNC, 0);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700739 return (status_t)mHwc->blank(mHwc, disp, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700740 }
741 return NO_ERROR;
742}
743
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700744status_t HWComposer::acquire(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700745 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Colin Cross10fbdb62012-07-12 17:56:34 -0700746 if (mHwc) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700747 return (status_t)mHwc->blank(mHwc, disp, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700748 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700749 return NO_ERROR;
750}
751
Andy McFadden27ec5732012-10-02 19:04:45 -0700752void HWComposer::disconnectDisplay(int disp) {
Andy McFadden5a8f9012012-10-04 19:09:45 -0700753 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
Andy McFadden27ec5732012-10-02 19:04:45 -0700754 DisplayData& dd(mDisplayData[disp]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700755 free(dd.list);
756 dd.list = NULL;
757 dd.framebufferTarget = NULL; // points into dd.list
758 dd.fbTargetHandle = NULL;
759 dd.outbufHandle = NULL;
760 dd.lastRetireFence = Fence::NO_FENCE;
761 dd.lastDisplayFence = Fence::NO_FENCE;
762 dd.outbufAcquireFence = Fence::NO_FENCE;
Andy McFadden27ec5732012-10-02 19:04:45 -0700763}
764
Mathias Agopiancde87a32012-09-13 14:09:01 -0700765int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700766 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700767 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
768 // is supported by the implementation. we can only be in this case
769 // if we have HWC 1.1
770 return HAL_PIXEL_FORMAT_RGBA_8888;
771 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700772 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700773 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700774 }
775}
776
Mathias Agopianda27af92012-09-13 18:17:13 -0700777bool HWComposer::supportsFramebufferTarget() const {
778 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
779}
780
781int HWComposer::fbPost(int32_t id,
782 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700783 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700784 return setFramebufferTarget(id, acquireFence, buffer);
785 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700786 acquireFence->waitForever("HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700787 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700788 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700789}
790
791int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700792 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700793 return NO_ERROR;
794
795 if (mFbDev->compositionComplete) {
796 return mFbDev->compositionComplete(mFbDev);
797 } else {
798 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700799 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700800}
801
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700802void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700803 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700804 const size_t SIZE = 4096;
805 char buffer[SIZE];
806 mFbDev->dump(mFbDev, buffer, SIZE);
807 result.append(buffer);
808 }
809}
810
Jesse Hall851cfe82013-03-20 13:44:00 -0700811status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
812 const sp<GraphicBuffer>& buf) {
813 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
814 return BAD_INDEX;
815 if (id < VIRTUAL_DISPLAY_ID_BASE)
816 return INVALID_OPERATION;
817
818 DisplayData& disp(mDisplayData[id]);
819 disp.outbufHandle = buf->handle;
820 disp.outbufAcquireFence = acquireFence;
821 return NO_ERROR;
822}
823
824sp<Fence> HWComposer::getLastRetireFence(int32_t id) {
825 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
826 return Fence::NO_FENCE;
827 return mDisplayData[id].lastRetireFence;
828}
829
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700830/*
831 * Helper template to implement a concrete HWCLayer
832 * This holds the pointer to the concrete hwc layer type
833 * and implements the "iterable" side of HWCLayer.
834 */
835template<typename CONCRETE, typename HWCTYPE>
836class Iterable : public HWComposer::HWCLayer {
837protected:
838 HWCTYPE* const mLayerList;
839 HWCTYPE* mCurrentLayer;
840 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
841 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
842 inline HWCTYPE* getLayer() { return mCurrentLayer; }
843 virtual ~Iterable() { }
844private:
845 // returns a copy of ourselves
846 virtual HWComposer::HWCLayer* dup() {
847 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
848 }
849 virtual status_t setLayer(size_t index) {
850 mCurrentLayer = &mLayerList[index];
851 return NO_ERROR;
852 }
853};
854
Jesse Hall5880cc52012-06-05 23:40:32 -0700855/*
856 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
857 * This implements the HWCLayer side of HWCIterableLayer.
858 */
859class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800860 struct hwc_composer_device_1* mHwc;
Jesse Hall5880cc52012-06-05 23:40:32 -0700861public:
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800862 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
863 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
Jesse Hall5880cc52012-06-05 23:40:32 -0700864
865 virtual int32_t getCompositionType() const {
866 return getLayer()->compositionType;
867 }
868 virtual uint32_t getHints() const {
869 return getLayer()->hints;
870 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700871 virtual sp<Fence> getAndResetReleaseFence() {
Jesse Hallef194142012-06-14 14:45:17 -0700872 int fd = getLayer()->releaseFenceFd;
873 getLayer()->releaseFenceFd = -1;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700874 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Jesse Hallef194142012-06-14 14:45:17 -0700875 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700876 virtual void setAcquireFenceFd(int fenceFd) {
877 getLayer()->acquireFenceFd = fenceFd;
878 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800879 virtual void setPerFrameDefaultState() {
880 //getLayer()->compositionType = HWC_FRAMEBUFFER;
881 }
882 virtual void setPlaneAlpha(uint8_t alpha) {
883 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
884 getLayer()->planeAlpha = alpha;
885 } else {
Mathias Agopian5fe58b82013-02-07 16:22:50 -0800886 if (alpha < 0xFF) {
887 getLayer()->flags |= HWC_SKIP_LAYER;
888 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800889 }
890 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700891 virtual void setDefaultState() {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800892 hwc_layer_1_t* const l = getLayer();
893 l->compositionType = HWC_FRAMEBUFFER;
894 l->hints = 0;
895 l->flags = HWC_SKIP_LAYER;
896 l->handle = 0;
897 l->transform = 0;
898 l->blending = HWC_BLENDING_NONE;
899 l->visibleRegionScreen.numRects = 0;
900 l->visibleRegionScreen.rects = NULL;
901 l->acquireFenceFd = -1;
902 l->releaseFenceFd = -1;
903 l->planeAlpha = 0xFF;
Jesse Hall5880cc52012-06-05 23:40:32 -0700904 }
905 virtual void setSkip(bool skip) {
906 if (skip) {
907 getLayer()->flags |= HWC_SKIP_LAYER;
908 } else {
909 getLayer()->flags &= ~HWC_SKIP_LAYER;
910 }
911 }
912 virtual void setBlending(uint32_t blending) {
913 getLayer()->blending = blending;
914 }
915 virtual void setTransform(uint32_t transform) {
916 getLayer()->transform = transform;
917 }
918 virtual void setFrame(const Rect& frame) {
Mathias Agopian6b442672013-07-09 21:24:52 -0700919 getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
Jesse Hall5880cc52012-06-05 23:40:32 -0700920 }
Mathias Agopian6b442672013-07-09 21:24:52 -0700921 virtual void setCrop(const FloatRect& crop) {
922 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
923 getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
924 } else {
925 /*
926 * Since h/w composer didn't support a flot crop rect before version 1.3,
927 * using integer coordinates instead produces a different output from the GL code in
928 * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
929 * window size ratio is large and a window crop is defined
930 * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
931 */
932 hwc_rect_t& r = getLayer()->sourceCrop;
933 r.left = int(ceilf(crop.left));
934 r.top = int(ceilf(crop.top));
935 r.right = int(floorf(crop.right));
936 r.bottom= int(floorf(crop.bottom));
937 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700938 }
939 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700940 // Region::getSharedBuffer creates a reference to the underlying
941 // SharedBuffer of this Region, this reference is freed
942 // in onDisplayed()
943 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
944 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
945 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700946 }
Jesse Hall399184a2014-03-03 15:42:54 -0800947 virtual void setSidebandStream(const sp<NativeHandle>& stream) {
948 ALOG_ASSERT(stream->handle() != NULL);
949 getLayer()->compositionType = HWC_SIDEBAND;
950 getLayer()->sidebandStream = stream->handle();
951 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700952 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
953 if (buffer == 0 || buffer->handle == 0) {
954 getLayer()->compositionType = HWC_FRAMEBUFFER;
955 getLayer()->flags |= HWC_SKIP_LAYER;
956 getLayer()->handle = 0;
957 } else {
Jesse Hall399184a2014-03-03 15:42:54 -0800958 if (getLayer()->compositionType == HWC_SIDEBAND) {
959 // If this was a sideband layer but the stream was removed, reset
960 // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare.
961 getLayer()->compositionType = HWC_FRAMEBUFFER;
962 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700963 getLayer()->handle = buffer->handle;
964 }
965 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700966 virtual void onDisplayed() {
967 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
968 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
969 if (sb) {
970 sb->release();
971 // not technically needed but safer
972 visibleRegion.numRects = 0;
973 visibleRegion.rects = NULL;
974 }
Jesse Halle25d0052012-09-05 13:03:10 -0700975
976 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700977 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700978};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700979
980/*
981 * returns an iterator initialized at a given index in the layer list
982 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700983HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700984 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700985 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700986 }
987 const DisplayData& disp(mDisplayData[id]);
988 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700989 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700990 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800991 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700992}
993
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700994/*
995 * returns an iterator on the beginning of the layer list
996 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700997HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700998 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700999}
1000
1001/*
1002 * returns an iterator on the end of the layer list
1003 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001004HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -07001005 size_t numLayers = 0;
1006 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
1007 const DisplayData& disp(mDisplayData[id]);
1008 if (mHwc && disp.list) {
1009 numLayers = disp.list->numHwLayers;
1010 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
1011 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
1012 // which we ignore when iterating through the layer list.
1013 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
1014 if (numLayers) {
1015 numLayers--;
1016 }
1017 }
1018 }
1019 }
1020 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001021}
1022
Andy McFadden4df87bd2014-04-21 18:08:54 -07001023// Converts a PixelFormat to a human-readable string. Max 11 chars.
1024// (Could use a table of prefab String8 objects.)
1025static String8 getFormatStr(PixelFormat format) {
1026 switch (format) {
1027 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
1028 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
1029 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
1030 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
1031 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
1032 case PIXEL_FORMAT_sRGB_A_8888: return String8("sRGB_A_8888");
1033 case PIXEL_FORMAT_sRGB_X_8888: return String8("sRGB_x_8888");
1034 default:
1035 String8 result;
1036 result.appendFormat("? %08x", format);
1037 return result;
1038 }
1039}
1040
Mathias Agopian74d211a2013-04-22 16:55:35 +02001041void HWComposer::dump(String8& result) const {
Jesse Hallb685c542012-07-31 14:32:56 -07001042 if (mHwc) {
Andy McFadden4df87bd2014-04-21 18:08:54 -07001043 result.appendFormat("Hardware Composer state (version %08x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -07001044 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
1045 for (size_t i=0 ; i<mNumDisplays ; i++) {
1046 const DisplayData& disp(mDisplayData[i]);
Jesse Hall02d86562013-03-25 14:43:23 -07001047 if (!disp.connected)
1048 continue;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001049
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 const Vector< sp<Layer> >& visibleLayersSortedByZ =
Mathias Agopiancb558572012-10-04 15:58:54 -07001051 mFlinger->getLayerSortedByZForHwcDisplay(i);
1052
Jesse Hall02d86562013-03-25 14:43:23 -07001053 result.appendFormat(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001054 " Display[%zd] : %ux%u, xdpi=%f, ydpi=%f, refresh=%" PRId64 "\n",
Jesse Hall02d86562013-03-25 14:43:23 -07001055 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001056
Jesse Hall02d86562013-03-25 14:43:23 -07001057 if (disp.list) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001058 result.appendFormat(
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001059 " numHwLayers=%zu, flags=%08x\n",
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001060 disp.list->numHwLayers, disp.list->flags);
1061
Mathias Agopianf4358632012-08-22 17:16:19 -07001062 result.append(
Andy McFadden4df87bd2014-04-21 18:08:54 -07001063 " type | handle | hint | flag | tr | blnd | format | source crop (l,t,r,b) | frame | name \n"
1064 "-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------\n");
1065 // " _________ | ________ | ____ | ____ | __ | ____ | ___________ |_____._,_____._,_____._,_____._ |_____,_____,_____,_____ | ___...
Mathias Agopianf4358632012-08-22 17:16:19 -07001066 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1067 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -07001068 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -07001069 String8 name("unknown");
Mathias Agopiancb558572012-10-04 15:58:54 -07001070
Mathias Agopianda27af92012-09-13 18:17:13 -07001071 if (i < visibleLayersSortedByZ.size()) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
1073 const sp<GraphicBuffer>& buffer(
1074 layer->getActiveBuffer());
1075 if (buffer != NULL) {
1076 format = buffer->getPixelFormat();
Mathias Agopianf4358632012-08-22 17:16:19 -07001077 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001078 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -07001079 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001080
1081 int type = l.compositionType;
1082 if (type == HWC_FRAMEBUFFER_TARGET) {
1083 name = "HWC_FRAMEBUFFER_TARGET";
1084 format = disp.format;
1085 }
1086
1087 static char const* compositionTypeName[] = {
1088 "GLES",
1089 "HWC",
Andy McFadden4df87bd2014-04-21 18:08:54 -07001090 "BKGND",
Mathias Agopianda27af92012-09-13 18:17:13 -07001091 "FB TARGET",
1092 "UNKNOWN"};
1093 if (type >= NELEM(compositionTypeName))
1094 type = NELEM(compositionTypeName) - 1;
1095
Andy McFadden4df87bd2014-04-21 18:08:54 -07001096 String8 formatStr = getFormatStr(format);
Jesse Hall353ddc62013-08-20 16:11:50 -07001097 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1098 result.appendFormat(
Andy McFadden4df87bd2014-04-21 18:08:54 -07001099 " %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 -07001100 compositionTypeName[type],
Andy McFadden4df87bd2014-04-21 18:08:54 -07001101 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
Jesse Hall353ddc62013-08-20 16:11:50 -07001102 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1103 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1104 name.string());
1105 } else {
1106 result.appendFormat(
Andy McFadden4df87bd2014-04-21 18:08:54 -07001107 " %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 -07001108 compositionTypeName[type],
Andy McFadden4df87bd2014-04-21 18:08:54 -07001109 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
Jesse Hall353ddc62013-08-20 16:11:50 -07001110 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1111 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1112 name.string());
1113 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -07001114 }
1115 }
Mathias Agopian83727852010-09-23 18:13:21 -07001116 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001117 }
Mathias Agopian30bcc612012-08-22 15:39:48 -07001118
1119 if (mHwc && mHwc->dump) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02001120 const size_t SIZE = 4096;
1121 char buffer[SIZE];
Mathias Agopian30bcc612012-08-22 15:39:48 -07001122 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001123 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001124 }
1125}
1126
Mathias Agopiana350ff92010-08-10 17:14:02 -07001127// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -07001128
1129HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1130 : mHwc(hwc), mEnabled(false),
1131 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07001132 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -07001133{
1134}
1135
1136void HWComposer::VSyncThread::setEnabled(bool enabled) {
1137 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -07001138 if (mEnabled != enabled) {
1139 mEnabled = enabled;
1140 mCondition.signal();
1141 }
Mathias Agopian2965b262012-04-08 15:13:32 -07001142}
1143
1144void HWComposer::VSyncThread::onFirstRef() {
1145 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1146}
1147
1148bool HWComposer::VSyncThread::threadLoop() {
1149 { // scope for lock
1150 Mutex::Autolock _l(mLock);
1151 while (!mEnabled) {
1152 mCondition.wait(mLock);
1153 }
1154 }
1155
1156 const nsecs_t period = mRefreshPeriod;
1157 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1158 nsecs_t next_vsync = mNextFakeVSync;
1159 nsecs_t sleep = next_vsync - now;
1160 if (sleep < 0) {
1161 // we missed, find where the next vsync should be
1162 sleep = (period - ((now - next_vsync) % period));
1163 next_vsync = now + sleep;
1164 }
1165 mNextFakeVSync = next_vsync + period;
1166
1167 struct timespec spec;
1168 spec.tv_sec = next_vsync / 1000000000;
1169 spec.tv_nsec = next_vsync % 1000000000;
1170
1171 int err;
1172 do {
1173 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1174 } while (err<0 && errno == EINTR);
1175
1176 if (err == 0) {
1177 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1178 }
1179
1180 return true;
1181}
1182
Jesse Halla9a1b002013-02-27 16:39:25 -08001183HWComposer::DisplayData::DisplayData()
Jesse Hall19e87292013-12-23 21:02:15 -08001184: width(0), height(0), format(HAL_PIXEL_FORMAT_RGBA_8888),
Jesse Halla9a1b002013-02-27 16:39:25 -08001185 xdpi(0.0f), ydpi(0.0f),
1186 refresh(0),
1187 connected(false),
1188 hasFbComp(false), hasOvComp(false),
1189 capacity(0), list(NULL),
1190 framebufferTarget(NULL), fbTargetHandle(0),
1191 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
Jesse Hall851cfe82013-03-20 13:44:00 -07001192 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
Jesse Halla9a1b002013-02-27 16:39:25 -08001193 events(0)
1194{}
1195
1196HWComposer::DisplayData::~DisplayData() {
1197 free(list);
1198}
1199
Mathias Agopian2965b262012-04-08 15:13:32 -07001200// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001201}; // namespace android