blob: 474f63342660008e0850b74186731b83985379dd [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
Mathias Agopiana350ff92010-08-10 17:14:02 -070019#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070023#include <sys/types.h>
Mathias Agopian6b442672013-07-09 21:24:52 -070024#include <math.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070025
26#include <utils/Errors.h>
Mathias Agopianda27af92012-09-13 18:17:13 -070027#include <utils/misc.h>
Mathias Agopian83727852010-09-23 18:13:21 -070028#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070029#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070030#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070031#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070032
Mathias Agopian921e6ac2012-07-23 23:11:29 -070033#include <ui/GraphicBuffer.h>
34
Mathias Agopiana350ff92010-08-10 17:14:02 -070035#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070036#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070037
Jesse Hall1c569c42013-04-05 13:44:52 -070038#include <android/configuration.h>
39
Mathias Agopiana350ff92010-08-10 17:14:02 -070040#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070041#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070042
Mathias Agopiana350ff92010-08-10 17:14:02 -070043#include "HWComposer.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070044
45#include "../Layer.h" // needed only for debugging
46#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070047
48namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070049
Jesse Hall72960512013-01-10 16:19:56 -080050#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070051
52static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
53 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070054 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
55}
56
57static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
58 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070059 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
60}
61
62static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
63 uint32_t version) {
64 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070065}
66
Mathias Agopiana350ff92010-08-10 17:14:02 -070067// ---------------------------------------------------------------------------
68
Mathias Agopian3e8b8532012-05-13 20:42:01 -070069struct HWComposer::cb_context {
70 struct callbacks : public hwc_procs_t {
71 // these are here to facilitate the transition when adding
72 // new callbacks (an implementation can check for NULL before
73 // calling a new callback).
74 void (*zero[4])(void);
75 };
76 callbacks procs;
77 HWComposer* hwc;
78};
79
80// ---------------------------------------------------------------------------
81
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070082HWComposer::HWComposer(
83 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070084 EventHandler& handler)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070085 : mFlinger(flinger),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070086 mFbDev(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070087 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070088 mEventHandler(handler),
Mathias Agopianbef42c52013-08-21 17:45:46 -070089 mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -070090{
Jesse Hall9e663de2013-08-16 14:28:37 -070091 for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
Mathias Agopiane60b0682012-08-21 23:34:09 -070092 mLists[i] = 0;
93 }
Jesse Hallb685c542012-07-31 14:32:56 -070094
Mathias Agopianbef42c52013-08-21 17:45:46 -070095 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
96 mLastHwVSync[i] = 0;
97 mVSyncCounts[i] = 0;
98 }
99
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700100 char value[PROPERTY_VALUE_MAX];
101 property_get("debug.sf.no_hw_vsync", value, "0");
102 mDebugForceFakeVSync = atoi(value);
103
Mathias Agopian028508c2012-07-25 21:12:12 -0700104 bool needVSyncThread = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700105
106 // Note: some devices may insist that the FB HAL be opened before HWC.
Jesse Hallef64b752013-03-18 11:28:50 -0700107 int fberr = loadFbHalModule();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700108 loadHwcModule();
109
Mathias Agopianda27af92012-09-13 18:17:13 -0700110 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
111 // close FB HAL if we don't needed it.
112 // FIXME: this is temporary until we're not forced to open FB HAL
113 // before HWC.
114 framebuffer_close(mFbDev);
115 mFbDev = NULL;
116 }
117
Andy McFaddenbabba182012-09-12 13:14:51 -0700118 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
119 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
120 && !mFbDev) {
Jesse Hallef64b752013-03-18 11:28:50 -0700121 ALOGE("ERROR: failed to open framebuffer (%s), aborting",
122 strerror(-fberr));
Andy McFadden43601a22012-09-11 15:15:13 -0700123 abort();
124 }
125
Andy McFadden620685c2012-10-19 12:53:46 -0700126 // these display IDs are always reserved
Jesse Hall9e663de2013-08-16 14:28:37 -0700127 for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
Andy McFadden620685c2012-10-19 12:53:46 -0700128 mAllocatedDisplayIDs.markBit(i);
129 }
130
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700131 if (mHwc) {
132 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
133 (hwcApiVersion(mHwc) >> 24) & 0xff,
134 (hwcApiVersion(mHwc) >> 16) & 0xff);
135 if (mHwc->registerProcs) {
136 mCBContext->hwc = this;
137 mCBContext->procs.invalidate = &hook_invalidate;
138 mCBContext->procs.vsync = &hook_vsync;
139 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
140 mCBContext->procs.hotplug = &hook_hotplug;
141 else
142 mCBContext->procs.hotplug = NULL;
143 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
144 mHwc->registerProcs(mHwc, &mCBContext->procs);
Jesse Hall5880cc52012-06-05 23:40:32 -0700145 }
146
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700147 // don't need a vsync thread if we have a hardware composer
148 needVSyncThread = false;
149 // always turn vsync off when we start
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700150 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700151
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700152 // the number of displays we actually have depends on the
153 // hw composer version
Jesse Hall38efe862013-04-06 23:12:29 -0700154 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
155 // 1.3 adds support for virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700156 mNumDisplays = MAX_HWC_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700157 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
158 // 1.1 adds support for multiple displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700159 mNumDisplays = NUM_BUILTIN_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700160 } else {
161 mNumDisplays = 1;
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700162 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700163 }
164
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700165 if (mFbDev) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700166 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
167 "should only have fbdev if no hwc or hwc is 1.0");
168
Mathias Agopianf4358632012-08-22 17:16:19 -0700169 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
Mathias Agopian38e623b2012-09-20 19:27:07 -0700170 disp.connected = true;
Jesse Halldb276212012-09-07 11:20:56 -0700171 disp.width = mFbDev->width;
172 disp.height = mFbDev->height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700173 disp.format = mFbDev->format;
174 disp.xdpi = mFbDev->xdpi;
175 disp.ydpi = mFbDev->ydpi;
Mathias Agopianf4358632012-08-22 17:16:19 -0700176 if (disp.refresh == 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700177 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
Mathias Agopianf4358632012-08-22 17:16:19 -0700178 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700179 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700180 if (disp.refresh == 0) {
181 disp.refresh = nsecs_t(1e9 / 60.0);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700182 ALOGW("getting VSYNC period from thin air: %lld",
183 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopianf4358632012-08-22 17:16:19 -0700184 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700185 } else if (mHwc) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700186 // here we're guaranteed to have at least HWC 1.1
Jesse Hall9e663de2013-08-16 14:28:37 -0700187 for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700188 queryDisplayProperties(i);
189 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700190 }
191
Mathias Agopian3a778712012-04-09 14:16:47 -0700192 if (needVSyncThread) {
193 // we don't have VSYNC support, we need to fake it
194 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700195 }
196}
197
198HWComposer::~HWComposer() {
Andy McFaddenbabba182012-09-12 13:14:51 -0700199 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700200 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Andy McFaddenbabba182012-09-12 13:14:51 -0700201 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700202 if (mVSyncThread != NULL) {
203 mVSyncThread->requestExitAndWait();
204 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700205 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700206 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700207 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700208 if (mFbDev) {
209 framebuffer_close(mFbDev);
210 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700211 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700212}
213
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700214// Load and prepare the hardware composer module. Sets mHwc.
215void HWComposer::loadHwcModule()
216{
217 hw_module_t const* module;
218
219 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
220 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
221 return;
222 }
223
224 int err = hwc_open_1(module, &mHwc);
225 if (err) {
226 ALOGE("%s device failed to initialize (%s)",
227 HWC_HARDWARE_COMPOSER, strerror(-err));
228 return;
229 }
230
231 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
232 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
233 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
234 ALOGE("%s device version %#x unsupported, will not be used",
235 HWC_HARDWARE_COMPOSER, mHwc->common.version);
236 hwc_close_1(mHwc);
237 mHwc = NULL;
238 return;
239 }
240}
241
242// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
Jesse Hallef64b752013-03-18 11:28:50 -0700243int HWComposer::loadFbHalModule()
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700244{
245 hw_module_t const* module;
246
Jesse Hallef64b752013-03-18 11:28:50 -0700247 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
248 if (err != 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700249 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
Jesse Hallef64b752013-03-18 11:28:50 -0700250 return err;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700251 }
252
Jesse Hallef64b752013-03-18 11:28:50 -0700253 return framebuffer_open(module, &mFbDev);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700254}
255
Mathias Agopiana350ff92010-08-10 17:14:02 -0700256status_t HWComposer::initCheck() const {
257 return mHwc ? NO_ERROR : NO_INIT;
258}
259
Jesse Hallbbd164a2012-08-21 12:05:09 -0700260void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
261 cb_context* ctx = reinterpret_cast<cb_context*>(
262 const_cast<hwc_procs_t*>(procs));
263 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700264}
265
Jesse Hall1bd20e02012-08-29 10:47:52 -0700266void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700267 int64_t timestamp) {
268 cb_context* ctx = reinterpret_cast<cb_context*>(
269 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700270 ctx->hwc->vsync(disp, timestamp);
271}
272
273void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
274 int connected) {
275 cb_context* ctx = reinterpret_cast<cb_context*>(
276 const_cast<hwc_procs_t*>(procs));
277 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700278}
279
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700280void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700281 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700282}
283
Jesse Hall1bd20e02012-08-29 10:47:52 -0700284void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopianbef42c52013-08-21 17:45:46 -0700285 if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
Jesse Hall8e26b282013-10-14 12:36:45 -0700286 {
287 Mutex::Autolock _l(mLock);
288
289 // There have been reports of HWCs that signal several vsync events
290 // with the same timestamp when turning the display off and on. This
291 // is a bug in the HWC implementation, but filter the extra events
292 // out here so they don't cause havoc downstream.
293 if (timestamp == mLastHwVSync[disp]) {
294 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%lld)",
295 timestamp);
296 return;
297 }
298
299 mLastHwVSync[disp] = timestamp;
300 }
301
Mathias Agopianbef42c52013-08-21 17:45:46 -0700302 char tag[16];
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700303 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700304 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
305
306 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700307 }
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700308}
309
Jesse Hall1bd20e02012-08-29 10:47:52 -0700310void HWComposer::hotplug(int disp, int connected) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700311 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700312 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
313 disp, connected);
314 return;
315 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700316 queryDisplayProperties(disp);
Mathias Agopian148994e2012-09-19 17:31:36 -0700317 mEventHandler.onHotplugReceived(disp, bool(connected));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700318}
319
Jesse Hall1c569c42013-04-05 13:44:52 -0700320static float getDefaultDensity(uint32_t height) {
321 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
322 else return ACONFIGURATION_DENSITY_TV;
323}
324
Jesse Hall1bd20e02012-08-29 10:47:52 -0700325static const uint32_t DISPLAY_ATTRIBUTES[] = {
326 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700327 HWC_DISPLAY_WIDTH,
328 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700329 HWC_DISPLAY_DPI_X,
330 HWC_DISPLAY_DPI_Y,
331 HWC_DISPLAY_NO_ATTRIBUTE,
332};
333#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
334
Mathias Agopian1604f772012-09-18 21:54:42 -0700335status_t HWComposer::queryDisplayProperties(int disp) {
336
Mathias Agopianda27af92012-09-13 18:17:13 -0700337 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700338
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700339 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700340 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
341 memset(values, 0, sizeof(values));
342
343 uint32_t config;
344 size_t numConfigs = 1;
345 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700346 if (err != NO_ERROR) {
347 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700348 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700349 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700350 }
351
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700352 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
353 if (err != NO_ERROR) {
354 // we can't get this display's info. turn it off.
355 mDisplayData[disp].connected = false;
356 return err;
357 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700358
Jesse Hall1bd20e02012-08-29 10:47:52 -0700359 int32_t w = 0, h = 0;
360 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
361 switch (DISPLAY_ATTRIBUTES[i]) {
362 case HWC_DISPLAY_VSYNC_PERIOD:
363 mDisplayData[disp].refresh = nsecs_t(values[i]);
364 break;
Jesse Halldb276212012-09-07 11:20:56 -0700365 case HWC_DISPLAY_WIDTH:
366 mDisplayData[disp].width = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700367 break;
Jesse Halldb276212012-09-07 11:20:56 -0700368 case HWC_DISPLAY_HEIGHT:
369 mDisplayData[disp].height = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700370 break;
371 case HWC_DISPLAY_DPI_X:
372 mDisplayData[disp].xdpi = values[i] / 1000.0f;
373 break;
374 case HWC_DISPLAY_DPI_Y:
375 mDisplayData[disp].ydpi = values[i] / 1000.0f;
376 break;
377 default:
Mathias Agopianda27af92012-09-13 18:17:13 -0700378 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
379 i, DISPLAY_ATTRIBUTES[i]);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700380 break;
381 }
382 }
383
Mathias Agopianf5a33922012-09-19 18:16:22 -0700384 // FIXME: what should we set the format to?
385 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
386 mDisplayData[disp].connected = true;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700387 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
Jesse Hall1c569c42013-04-05 13:44:52 -0700388 float dpi = getDefaultDensity(h);
389 mDisplayData[disp].xdpi = dpi;
390 mDisplayData[disp].ydpi = dpi;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700391 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700392 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700393}
394
Jesse Hall1c569c42013-04-05 13:44:52 -0700395status_t HWComposer::setVirtualDisplayProperties(int32_t id,
396 uint32_t w, uint32_t h, uint32_t format) {
397 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
398 !mAllocatedDisplayIDs.hasBit(id)) {
399 return BAD_INDEX;
400 }
401 mDisplayData[id].width = w;
402 mDisplayData[id].height = h;
403 mDisplayData[id].format = format;
404 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h);
405 return NO_ERROR;
406}
407
Mathias Agopiane60b0682012-08-21 23:34:09 -0700408int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700409 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700410 return NO_MEMORY;
411 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700412 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
413 mAllocatedDisplayIDs.markBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800414 mDisplayData[id].connected = true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700415 return id;
416}
417
418status_t HWComposer::freeDisplayId(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700419 if (id < NUM_BUILTIN_DISPLAYS) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700420 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700421 return BAD_VALUE;
422 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700423 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700424 return BAD_INDEX;
425 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700426 mAllocatedDisplayIDs.clearBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800427 mDisplayData[id].connected = false;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700428 return NO_ERROR;
429}
430
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700431nsecs_t HWComposer::getRefreshPeriod(int disp) const {
432 return mDisplayData[disp].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700433}
434
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700435nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700436 // this returns the last refresh timestamp.
437 // if the last one is not available, we estimate it based on
438 // the refresh period and whatever closest timestamp we have.
439 Mutex::Autolock _l(mLock);
440 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700441 return now - ((now - mLastHwVSync[disp]) % mDisplayData[disp].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700442}
443
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800444sp<Fence> HWComposer::getDisplayFence(int disp) const {
445 return mDisplayData[disp].lastDisplayFence;
446}
447
Jesse Halldb276212012-09-07 11:20:56 -0700448uint32_t HWComposer::getWidth(int disp) const {
449 return mDisplayData[disp].width;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700450}
451
Jesse Halldb276212012-09-07 11:20:56 -0700452uint32_t HWComposer::getHeight(int disp) const {
453 return mDisplayData[disp].height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700454}
455
456uint32_t HWComposer::getFormat(int disp) const {
Jesse Hall19e87292013-12-23 21:02:15 -0800457 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
458 return HAL_PIXEL_FORMAT_RGBA_8888;
459 } else {
460 return mDisplayData[disp].format;
461 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700462}
463
464float HWComposer::getDpiX(int disp) const {
465 return mDisplayData[disp].xdpi;
466}
467
468float HWComposer::getDpiY(int disp) const {
469 return mDisplayData[disp].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700470}
471
Mathias Agopianf5a33922012-09-19 18:16:22 -0700472bool HWComposer::isConnected(int disp) const {
473 return mDisplayData[disp].connected;
474}
475
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700476void HWComposer::eventControl(int disp, int event, int enabled) {
477 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
Andy McFadden620685c2012-10-19 12:53:46 -0700478 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
479 event, disp, enabled);
480 return;
481 }
482 if (event != EVENT_VSYNC) {
483 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
484 event, disp, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700485 return;
486 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700487 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700488 if (mHwc && !mDebugForceFakeVSync) {
489 // NOTE: we use our own internal lock here because we have to call
490 // into the HWC with the lock held, and we want to make sure
491 // that even if HWC blocks (which it shouldn't), it won't
492 // affect other threads.
493 Mutex::Autolock _l(mEventControlLock);
494 const int32_t eventBit = 1UL << event;
495 const int32_t newValue = enabled ? eventBit : 0;
496 const int32_t oldValue = mDisplayData[disp].events & eventBit;
497 if (newValue != oldValue) {
498 ATRACE_CALL();
499 err = mHwc->eventControl(mHwc, disp, event, enabled);
500 if (!err) {
501 int32_t& events(mDisplayData[disp].events);
502 events = (events & ~eventBit) | newValue;
503 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700504 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700505 // error here should not happen -- not sure what we should
506 // do if it does.
507 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
508 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700509 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700510
511 if (err == NO_ERROR && mVSyncThread != NULL) {
512 mVSyncThread->setEnabled(enabled);
513 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700514}
515
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700516status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700517 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700518 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700519 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700520
Mathias Agopian45721772010-08-12 15:03:26 -0700521 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700522 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700523 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
524 // we need space for the HWC_FRAMEBUFFER_TARGET
525 numLayers++;
526 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700527 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700528 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700529 + numLayers * sizeof(hwc_layer_1_t);
530 free(disp.list);
531 disp.list = (hwc_display_contents_1_t*)malloc(size);
532 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700533 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700534 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
535 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
536 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800537 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
Mathias Agopianda27af92012-09-13 18:17:13 -0700538 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
539 disp.framebufferTarget->hints = 0;
540 disp.framebufferTarget->flags = 0;
541 disp.framebufferTarget->handle = disp.fbTargetHandle;
542 disp.framebufferTarget->transform = 0;
543 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
Mathias Agopian8f63c202013-09-25 20:44:34 -0700544 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
545 disp.framebufferTarget->sourceCropf.left = 0;
546 disp.framebufferTarget->sourceCropf.top = 0;
547 disp.framebufferTarget->sourceCropf.right = disp.width;
548 disp.framebufferTarget->sourceCropf.bottom = disp.height;
549 } else {
550 disp.framebufferTarget->sourceCrop = r;
551 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700552 disp.framebufferTarget->displayFrame = r;
553 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
554 disp.framebufferTarget->visibleRegionScreen.rects =
555 &disp.framebufferTarget->displayFrame;
556 disp.framebufferTarget->acquireFenceFd = -1;
557 disp.framebufferTarget->releaseFenceFd = -1;
Mathias Agopian70a6e882013-03-21 16:25:12 -0700558 disp.framebufferTarget->planeAlpha = 0xFF;
Mathias Agopianda27af92012-09-13 18:17:13 -0700559 }
Jesse Halldb276212012-09-07 11:20:56 -0700560 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700561 disp.list->flags = HWC_GEOMETRY_CHANGED;
562 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700563 }
564 return NO_ERROR;
565}
566
Mathias Agopianda27af92012-09-13 18:17:13 -0700567status_t HWComposer::setFramebufferTarget(int32_t id,
568 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
569 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
570 return BAD_INDEX;
571 }
572 DisplayData& disp(mDisplayData[id]);
573 if (!disp.framebufferTarget) {
574 // this should never happen, but apparently eglCreateWindowSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800575 // triggers a Surface::queueBuffer() on some
Mathias Agopianda27af92012-09-13 18:17:13 -0700576 // devices (!?) -- log and ignore.
577 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianda27af92012-09-13 18:17:13 -0700578 return NO_ERROR;
579 }
580
581 int acquireFenceFd = -1;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800582 if (acquireFence->isValid()) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700583 acquireFenceFd = acquireFence->dup();
584 }
585
586 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
587 disp.fbTargetHandle = buf->handle;
588 disp.framebufferTarget->handle = disp.fbTargetHandle;
589 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
590 return NO_ERROR;
591}
592
Mathias Agopiane60b0682012-08-21 23:34:09 -0700593status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700594 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700595 DisplayData& disp(mDisplayData[i]);
596 if (disp.framebufferTarget) {
597 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
598 // DO NOT reset the handle field to NULL, because it's possible
599 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
600 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700601 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700602 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
603 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700604 if (!disp.connected && disp.list != NULL) {
605 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
606 i, disp.list->numHwLayers);
607 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700608 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700609 if (mLists[i]) {
Jesse Hall38efe862013-04-06 23:12:29 -0700610 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
Jesse Hallf7a67582013-11-05 16:12:31 -0800611 mLists[i]->outbuf = disp.outbufHandle;
Jesse Halldb276212012-09-07 11:20:56 -0700612 mLists[i]->outbufAcquireFenceFd = -1;
613 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
614 // garbage data to catch improper use
615 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
616 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
617 } else {
618 mLists[i]->dpy = EGL_NO_DISPLAY;
619 mLists[i]->sur = EGL_NO_SURFACE;
620 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700621 }
622 }
Jesse Halldb276212012-09-07 11:20:56 -0700623
Mathias Agopianf4358632012-08-22 17:16:19 -0700624 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700625 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
626
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700627 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700628 // here we're just making sure that "skip" layers are set
629 // to HWC_FRAMEBUFFER and we're also counting how many layers
630 // we have of each type.
Jesse Halld05a17f2013-09-30 16:49:30 -0700631 //
632 // If there are no window layers, we treat the display has having FB
633 // composition, because SurfaceFlinger will use GLES to draw the
634 // wormhole region.
Mathias Agopianf4358632012-08-22 17:16:19 -0700635 for (size_t i=0 ; i<mNumDisplays ; i++) {
636 DisplayData& disp(mDisplayData[i]);
637 disp.hasFbComp = false;
638 disp.hasOvComp = false;
639 if (disp.list) {
640 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
641 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700642
643 //ALOGD("prepare: %d, type=%d, handle=%p",
644 // i, l.compositionType, l.handle);
645
Mathias Agopianf4358632012-08-22 17:16:19 -0700646 if (l.flags & HWC_SKIP_LAYER) {
647 l.compositionType = HWC_FRAMEBUFFER;
648 }
649 if (l.compositionType == HWC_FRAMEBUFFER) {
650 disp.hasFbComp = true;
651 }
652 if (l.compositionType == HWC_OVERLAY) {
653 disp.hasOvComp = true;
654 }
655 }
Jesse Halld05a17f2013-09-30 16:49:30 -0700656 if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
657 disp.hasFbComp = true;
658 }
659 } else {
660 disp.hasFbComp = true;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700661 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700662 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700663 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700664 return (status_t)err;
665}
666
Mathias Agopiane60b0682012-08-21 23:34:09 -0700667bool HWComposer::hasHwcComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700668 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700669 return false;
670 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700671}
672
Mathias Agopiane60b0682012-08-21 23:34:09 -0700673bool HWComposer::hasGlesComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700674 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
675 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700676 return mDisplayData[id].hasFbComp;
677}
678
Jesse Hall13f01cb2013-03-20 11:37:21 -0700679sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700680 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Jesse Hall13f01cb2013-03-20 11:37:21 -0700681 return Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700682
683 int fd = INVALID_OPERATION;
684 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
685 const DisplayData& disp(mDisplayData[id]);
686 if (disp.framebufferTarget) {
687 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700688 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700689 disp.framebufferTarget->releaseFenceFd = -1;
690 }
691 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700692 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700693}
694
Mathias Agopian30bcc612012-08-22 15:39:48 -0700695status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700696 int err = NO_ERROR;
697 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700698 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700699 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700700 // by the (dpy, sur) fields and we are guaranteed to have only
701 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700702 mLists[0]->dpy = eglGetCurrentDisplay();
703 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700704 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700705
Jesse Hallafaf14b2013-03-20 13:42:29 -0700706 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
Jesse Hall80e0a392013-03-15 12:32:10 -0700707 DisplayData& disp(mDisplayData[i]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700708 if (disp.outbufHandle) {
709 mLists[i]->outbuf = disp.outbufHandle;
Jesse Hall80e0a392013-03-15 12:32:10 -0700710 mLists[i]->outbufAcquireFenceFd =
Jesse Hall851cfe82013-03-20 13:44:00 -0700711 disp.outbufAcquireFence->dup();
Jesse Hall80e0a392013-03-15 12:32:10 -0700712 }
713 }
714
Mathias Agopian30bcc612012-08-22 15:39:48 -0700715 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700716
717 for (size_t i=0 ; i<mNumDisplays ; i++) {
718 DisplayData& disp(mDisplayData[i]);
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800719 disp.lastDisplayFence = disp.lastRetireFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800720 disp.lastRetireFence = Fence::NO_FENCE;
Mathias Agopianf4358632012-08-22 17:16:19 -0700721 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700722 if (disp.list->retireFenceFd != -1) {
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800723 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
Jesse Halldb276212012-09-07 11:20:56 -0700724 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700725 }
726 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
727 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700728 }
Mathias Agopian58959342010-10-07 14:57:04 -0700729 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700730 return (status_t)err;
731}
732
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700733status_t HWComposer::release(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700734 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700735 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700736 eventControl(disp, HWC_EVENT_VSYNC, 0);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700737 return (status_t)mHwc->blank(mHwc, disp, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700738 }
739 return NO_ERROR;
740}
741
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700742status_t HWComposer::acquire(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700743 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Colin Cross10fbdb62012-07-12 17:56:34 -0700744 if (mHwc) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700745 return (status_t)mHwc->blank(mHwc, disp, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700746 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700747 return NO_ERROR;
748}
749
Andy McFadden27ec5732012-10-02 19:04:45 -0700750void HWComposer::disconnectDisplay(int disp) {
Andy McFadden5a8f9012012-10-04 19:09:45 -0700751 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
Andy McFadden27ec5732012-10-02 19:04:45 -0700752 DisplayData& dd(mDisplayData[disp]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700753 free(dd.list);
754 dd.list = NULL;
755 dd.framebufferTarget = NULL; // points into dd.list
756 dd.fbTargetHandle = NULL;
757 dd.outbufHandle = NULL;
758 dd.lastRetireFence = Fence::NO_FENCE;
759 dd.lastDisplayFence = Fence::NO_FENCE;
760 dd.outbufAcquireFence = Fence::NO_FENCE;
Andy McFadden27ec5732012-10-02 19:04:45 -0700761}
762
Mathias Agopiancde87a32012-09-13 14:09:01 -0700763int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700764 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700765 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
766 // is supported by the implementation. we can only be in this case
767 // if we have HWC 1.1
768 return HAL_PIXEL_FORMAT_RGBA_8888;
769 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700770 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700771 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700772 }
773}
774
Mathias Agopianda27af92012-09-13 18:17:13 -0700775bool HWComposer::supportsFramebufferTarget() const {
776 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
777}
778
779int HWComposer::fbPost(int32_t id,
780 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700781 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700782 return setFramebufferTarget(id, acquireFence, buffer);
783 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700784 acquireFence->waitForever("HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700785 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700786 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700787}
788
789int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700790 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700791 return NO_ERROR;
792
793 if (mFbDev->compositionComplete) {
794 return mFbDev->compositionComplete(mFbDev);
795 } else {
796 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700797 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700798}
799
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700800void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700801 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700802 const size_t SIZE = 4096;
803 char buffer[SIZE];
804 mFbDev->dump(mFbDev, buffer, SIZE);
805 result.append(buffer);
806 }
807}
808
Jesse Hall851cfe82013-03-20 13:44:00 -0700809status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
810 const sp<GraphicBuffer>& buf) {
811 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
812 return BAD_INDEX;
813 if (id < VIRTUAL_DISPLAY_ID_BASE)
814 return INVALID_OPERATION;
815
816 DisplayData& disp(mDisplayData[id]);
817 disp.outbufHandle = buf->handle;
818 disp.outbufAcquireFence = acquireFence;
819 return NO_ERROR;
820}
821
822sp<Fence> HWComposer::getLastRetireFence(int32_t id) {
823 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
824 return Fence::NO_FENCE;
825 return mDisplayData[id].lastRetireFence;
826}
827
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700828/*
829 * Helper template to implement a concrete HWCLayer
830 * This holds the pointer to the concrete hwc layer type
831 * and implements the "iterable" side of HWCLayer.
832 */
833template<typename CONCRETE, typename HWCTYPE>
834class Iterable : public HWComposer::HWCLayer {
835protected:
836 HWCTYPE* const mLayerList;
837 HWCTYPE* mCurrentLayer;
838 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
839 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
840 inline HWCTYPE* getLayer() { return mCurrentLayer; }
841 virtual ~Iterable() { }
842private:
843 // returns a copy of ourselves
844 virtual HWComposer::HWCLayer* dup() {
845 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
846 }
847 virtual status_t setLayer(size_t index) {
848 mCurrentLayer = &mLayerList[index];
849 return NO_ERROR;
850 }
851};
852
Jesse Hall5880cc52012-06-05 23:40:32 -0700853/*
854 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
855 * This implements the HWCLayer side of HWCIterableLayer.
856 */
857class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800858 struct hwc_composer_device_1* mHwc;
Jesse Hall5880cc52012-06-05 23:40:32 -0700859public:
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800860 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
861 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
Jesse Hall5880cc52012-06-05 23:40:32 -0700862
863 virtual int32_t getCompositionType() const {
864 return getLayer()->compositionType;
865 }
866 virtual uint32_t getHints() const {
867 return getLayer()->hints;
868 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700869 virtual sp<Fence> getAndResetReleaseFence() {
Jesse Hallef194142012-06-14 14:45:17 -0700870 int fd = getLayer()->releaseFenceFd;
871 getLayer()->releaseFenceFd = -1;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700872 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Jesse Hallef194142012-06-14 14:45:17 -0700873 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700874 virtual void setAcquireFenceFd(int fenceFd) {
875 getLayer()->acquireFenceFd = fenceFd;
876 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800877 virtual void setPerFrameDefaultState() {
878 //getLayer()->compositionType = HWC_FRAMEBUFFER;
879 }
880 virtual void setPlaneAlpha(uint8_t alpha) {
881 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
882 getLayer()->planeAlpha = alpha;
883 } else {
Mathias Agopian5fe58b82013-02-07 16:22:50 -0800884 if (alpha < 0xFF) {
885 getLayer()->flags |= HWC_SKIP_LAYER;
886 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800887 }
888 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700889 virtual void setDefaultState() {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800890 hwc_layer_1_t* const l = getLayer();
891 l->compositionType = HWC_FRAMEBUFFER;
892 l->hints = 0;
893 l->flags = HWC_SKIP_LAYER;
894 l->handle = 0;
895 l->transform = 0;
896 l->blending = HWC_BLENDING_NONE;
897 l->visibleRegionScreen.numRects = 0;
898 l->visibleRegionScreen.rects = NULL;
899 l->acquireFenceFd = -1;
900 l->releaseFenceFd = -1;
901 l->planeAlpha = 0xFF;
Jesse Hall5880cc52012-06-05 23:40:32 -0700902 }
903 virtual void setSkip(bool skip) {
904 if (skip) {
905 getLayer()->flags |= HWC_SKIP_LAYER;
906 } else {
907 getLayer()->flags &= ~HWC_SKIP_LAYER;
908 }
909 }
910 virtual void setBlending(uint32_t blending) {
911 getLayer()->blending = blending;
912 }
913 virtual void setTransform(uint32_t transform) {
914 getLayer()->transform = transform;
915 }
916 virtual void setFrame(const Rect& frame) {
Mathias Agopian6b442672013-07-09 21:24:52 -0700917 getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
Jesse Hall5880cc52012-06-05 23:40:32 -0700918 }
Mathias Agopian6b442672013-07-09 21:24:52 -0700919 virtual void setCrop(const FloatRect& crop) {
920 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
921 getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
922 } else {
923 /*
924 * Since h/w composer didn't support a flot crop rect before version 1.3,
925 * using integer coordinates instead produces a different output from the GL code in
926 * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
927 * window size ratio is large and a window crop is defined
928 * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
929 */
930 hwc_rect_t& r = getLayer()->sourceCrop;
931 r.left = int(ceilf(crop.left));
932 r.top = int(ceilf(crop.top));
933 r.right = int(floorf(crop.right));
934 r.bottom= int(floorf(crop.bottom));
935 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700936 }
937 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700938 // Region::getSharedBuffer creates a reference to the underlying
939 // SharedBuffer of this Region, this reference is freed
940 // in onDisplayed()
941 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
942 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
943 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700944 }
945 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
946 if (buffer == 0 || buffer->handle == 0) {
947 getLayer()->compositionType = HWC_FRAMEBUFFER;
948 getLayer()->flags |= HWC_SKIP_LAYER;
949 getLayer()->handle = 0;
950 } else {
951 getLayer()->handle = buffer->handle;
952 }
953 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700954 virtual void onDisplayed() {
955 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
956 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
957 if (sb) {
958 sb->release();
959 // not technically needed but safer
960 visibleRegion.numRects = 0;
961 visibleRegion.rects = NULL;
962 }
Jesse Halle25d0052012-09-05 13:03:10 -0700963
964 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700965 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700966};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700967
968/*
969 * returns an iterator initialized at a given index in the layer list
970 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700971HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700972 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700973 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700974 }
975 const DisplayData& disp(mDisplayData[id]);
976 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700977 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700978 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800979 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700980}
981
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700982/*
983 * returns an iterator on the beginning of the layer list
984 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700985HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700986 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700987}
988
989/*
990 * returns an iterator on the end of the layer list
991 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700992HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700993 size_t numLayers = 0;
994 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
995 const DisplayData& disp(mDisplayData[id]);
996 if (mHwc && disp.list) {
997 numLayers = disp.list->numHwLayers;
998 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
999 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
1000 // which we ignore when iterating through the layer list.
1001 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
1002 if (numLayers) {
1003 numLayers--;
1004 }
1005 }
1006 }
1007 }
1008 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001009}
1010
Mathias Agopian74d211a2013-04-22 16:55:35 +02001011void HWComposer::dump(String8& result) const {
Jesse Hallb685c542012-07-31 14:32:56 -07001012 if (mHwc) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001013 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -07001014 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
1015 for (size_t i=0 ; i<mNumDisplays ; i++) {
1016 const DisplayData& disp(mDisplayData[i]);
Jesse Hall02d86562013-03-25 14:43:23 -07001017 if (!disp.connected)
1018 continue;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001019
Mathias Agopian13127d82013-03-05 17:47:11 -08001020 const Vector< sp<Layer> >& visibleLayersSortedByZ =
Mathias Agopiancb558572012-10-04 15:58:54 -07001021 mFlinger->getLayerSortedByZForHwcDisplay(i);
1022
Jesse Hall02d86562013-03-25 14:43:23 -07001023 result.appendFormat(
1024 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
1025 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001026
Jesse Hall02d86562013-03-25 14:43:23 -07001027 if (disp.list) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001028 result.appendFormat(
1029 " numHwLayers=%u, flags=%08x\n",
1030 disp.list->numHwLayers, disp.list->flags);
1031
Mathias Agopianf4358632012-08-22 17:16:19 -07001032 result.append(
Jesse Hall353ddc62013-08-20 16:11:50 -07001033 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
1034 "------------+----------+----------+----------+----+-------+----------+---------------------------------+--------------------------------\n");
1035 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____._,_____._,_____._,_____._] | [_____,_____,_____,_____]
Mathias Agopianf4358632012-08-22 17:16:19 -07001036 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1037 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -07001038 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -07001039 String8 name("unknown");
Mathias Agopiancb558572012-10-04 15:58:54 -07001040
Mathias Agopianda27af92012-09-13 18:17:13 -07001041 if (i < visibleLayersSortedByZ.size()) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001042 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
1043 const sp<GraphicBuffer>& buffer(
1044 layer->getActiveBuffer());
1045 if (buffer != NULL) {
1046 format = buffer->getPixelFormat();
Mathias Agopianf4358632012-08-22 17:16:19 -07001047 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001048 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -07001049 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001050
1051 int type = l.compositionType;
1052 if (type == HWC_FRAMEBUFFER_TARGET) {
1053 name = "HWC_FRAMEBUFFER_TARGET";
1054 format = disp.format;
1055 }
1056
1057 static char const* compositionTypeName[] = {
1058 "GLES",
1059 "HWC",
1060 "BACKGROUND",
1061 "FB TARGET",
1062 "UNKNOWN"};
1063 if (type >= NELEM(compositionTypeName))
1064 type = NELEM(compositionTypeName) - 1;
1065
Jesse Hall353ddc62013-08-20 16:11:50 -07001066 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1067 result.appendFormat(
1068 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7.1f,%7.1f,%7.1f,%7.1f] | [%5d,%5d,%5d,%5d] %s\n",
1069 compositionTypeName[type],
1070 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1071 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1072 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1073 name.string());
1074 } else {
1075 result.appendFormat(
1076 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7d,%7d,%7d,%7d] | [%5d,%5d,%5d,%5d] %s\n",
1077 compositionTypeName[type],
1078 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1079 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1080 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1081 name.string());
1082 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -07001083 }
1084 }
Mathias Agopian83727852010-09-23 18:13:21 -07001085 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001086 }
Mathias Agopian30bcc612012-08-22 15:39:48 -07001087
1088 if (mHwc && mHwc->dump) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02001089 const size_t SIZE = 4096;
1090 char buffer[SIZE];
Mathias Agopian30bcc612012-08-22 15:39:48 -07001091 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001092 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001093 }
1094}
1095
Mathias Agopiana350ff92010-08-10 17:14:02 -07001096// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -07001097
1098HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1099 : mHwc(hwc), mEnabled(false),
1100 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07001101 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -07001102{
1103}
1104
1105void HWComposer::VSyncThread::setEnabled(bool enabled) {
1106 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -07001107 if (mEnabled != enabled) {
1108 mEnabled = enabled;
1109 mCondition.signal();
1110 }
Mathias Agopian2965b262012-04-08 15:13:32 -07001111}
1112
1113void HWComposer::VSyncThread::onFirstRef() {
1114 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1115}
1116
1117bool HWComposer::VSyncThread::threadLoop() {
1118 { // scope for lock
1119 Mutex::Autolock _l(mLock);
1120 while (!mEnabled) {
1121 mCondition.wait(mLock);
1122 }
1123 }
1124
1125 const nsecs_t period = mRefreshPeriod;
1126 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1127 nsecs_t next_vsync = mNextFakeVSync;
1128 nsecs_t sleep = next_vsync - now;
1129 if (sleep < 0) {
1130 // we missed, find where the next vsync should be
1131 sleep = (period - ((now - next_vsync) % period));
1132 next_vsync = now + sleep;
1133 }
1134 mNextFakeVSync = next_vsync + period;
1135
1136 struct timespec spec;
1137 spec.tv_sec = next_vsync / 1000000000;
1138 spec.tv_nsec = next_vsync % 1000000000;
1139
1140 int err;
1141 do {
1142 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1143 } while (err<0 && errno == EINTR);
1144
1145 if (err == 0) {
1146 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1147 }
1148
1149 return true;
1150}
1151
Jesse Halla9a1b002013-02-27 16:39:25 -08001152HWComposer::DisplayData::DisplayData()
Jesse Hall19e87292013-12-23 21:02:15 -08001153: width(0), height(0), format(HAL_PIXEL_FORMAT_RGBA_8888),
Jesse Halla9a1b002013-02-27 16:39:25 -08001154 xdpi(0.0f), ydpi(0.0f),
1155 refresh(0),
1156 connected(false),
1157 hasFbComp(false), hasOvComp(false),
1158 capacity(0), list(NULL),
1159 framebufferTarget(NULL), fbTargetHandle(0),
1160 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
Jesse Hall851cfe82013-03-20 13:44:00 -07001161 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
Jesse Halla9a1b002013-02-27 16:39:25 -08001162 events(0)
1163{}
1164
1165HWComposer::DisplayData::~DisplayData() {
1166 free(list);
1167}
1168
Mathias Agopian2965b262012-04-08 15:13:32 -07001169// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001170}; // namespace android