blob: 0ca93c8ce9323b83793bd6d98b71d116f9d4048c [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>
Jesse Hall399184a2014-03-03 15:42:54 -080028#include <utils/NativeHandle.h>
Mathias Agopian83727852010-09-23 18:13:21 -070029#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070030#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070031#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070032#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070033
Mathias Agopian921e6ac2012-07-23 23:11:29 -070034#include <ui/GraphicBuffer.h>
35
Mathias Agopiana350ff92010-08-10 17:14:02 -070036#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070037#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
Jesse Hall1c569c42013-04-05 13:44:52 -070039#include <android/configuration.h>
40
Mathias Agopiana350ff92010-08-10 17:14:02 -070041#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070042#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070043
Mathias Agopiana350ff92010-08-10 17:14:02 -070044#include "HWComposer.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070045
46#include "../Layer.h" // needed only for debugging
47#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070048
49namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070050
Jesse Hall72960512013-01-10 16:19:56 -080051#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070052
53static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
54 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070055 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
56}
57
58static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
59 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070060 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
61}
62
63static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
64 uint32_t version) {
65 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070066}
67
Mathias Agopiana350ff92010-08-10 17:14:02 -070068// ---------------------------------------------------------------------------
69
Mathias Agopian3e8b8532012-05-13 20:42:01 -070070struct HWComposer::cb_context {
71 struct callbacks : public hwc_procs_t {
72 // these are here to facilitate the transition when adding
73 // new callbacks (an implementation can check for NULL before
74 // calling a new callback).
75 void (*zero[4])(void);
76 };
77 callbacks procs;
78 HWComposer* hwc;
79};
80
81// ---------------------------------------------------------------------------
82
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070083HWComposer::HWComposer(
84 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070085 EventHandler& handler)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070086 : mFlinger(flinger),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070087 mFbDev(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070088 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070089 mEventHandler(handler),
Mathias Agopianbef42c52013-08-21 17:45:46 -070090 mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -070091{
Jesse Hall9e663de2013-08-16 14:28:37 -070092 for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
Mathias Agopiane60b0682012-08-21 23:34:09 -070093 mLists[i] = 0;
94 }
Jesse Hallb685c542012-07-31 14:32:56 -070095
Mathias Agopianbef42c52013-08-21 17:45:46 -070096 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
97 mLastHwVSync[i] = 0;
98 mVSyncCounts[i] = 0;
99 }
100
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700101 char value[PROPERTY_VALUE_MAX];
102 property_get("debug.sf.no_hw_vsync", value, "0");
103 mDebugForceFakeVSync = atoi(value);
104
Mathias Agopian028508c2012-07-25 21:12:12 -0700105 bool needVSyncThread = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700106
107 // Note: some devices may insist that the FB HAL be opened before HWC.
Jesse Hallef64b752013-03-18 11:28:50 -0700108 int fberr = loadFbHalModule();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700109 loadHwcModule();
110
Mathias Agopianda27af92012-09-13 18:17:13 -0700111 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
112 // close FB HAL if we don't needed it.
113 // FIXME: this is temporary until we're not forced to open FB HAL
114 // before HWC.
115 framebuffer_close(mFbDev);
116 mFbDev = NULL;
117 }
118
Andy McFaddenbabba182012-09-12 13:14:51 -0700119 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
120 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
121 && !mFbDev) {
Jesse Hallef64b752013-03-18 11:28:50 -0700122 ALOGE("ERROR: failed to open framebuffer (%s), aborting",
123 strerror(-fberr));
Andy McFadden43601a22012-09-11 15:15:13 -0700124 abort();
125 }
126
Andy McFadden620685c2012-10-19 12:53:46 -0700127 // these display IDs are always reserved
Jesse Hall9e663de2013-08-16 14:28:37 -0700128 for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
Andy McFadden620685c2012-10-19 12:53:46 -0700129 mAllocatedDisplayIDs.markBit(i);
130 }
131
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700132 if (mHwc) {
133 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
134 (hwcApiVersion(mHwc) >> 24) & 0xff,
135 (hwcApiVersion(mHwc) >> 16) & 0xff);
136 if (mHwc->registerProcs) {
137 mCBContext->hwc = this;
138 mCBContext->procs.invalidate = &hook_invalidate;
139 mCBContext->procs.vsync = &hook_vsync;
140 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
141 mCBContext->procs.hotplug = &hook_hotplug;
142 else
143 mCBContext->procs.hotplug = NULL;
144 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
145 mHwc->registerProcs(mHwc, &mCBContext->procs);
Jesse Hall5880cc52012-06-05 23:40:32 -0700146 }
147
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700148 // don't need a vsync thread if we have a hardware composer
149 needVSyncThread = false;
150 // always turn vsync off when we start
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700151 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700152
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700153 // the number of displays we actually have depends on the
154 // hw composer version
Jesse Hall38efe862013-04-06 23:12:29 -0700155 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
156 // 1.3 adds support for virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700157 mNumDisplays = MAX_HWC_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700158 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
159 // 1.1 adds support for multiple displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700160 mNumDisplays = NUM_BUILTIN_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700161 } else {
162 mNumDisplays = 1;
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700163 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700164 }
165
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700166 if (mFbDev) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700167 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
168 "should only have fbdev if no hwc or hwc is 1.0");
169
Mathias Agopianf4358632012-08-22 17:16:19 -0700170 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
Mathias Agopian38e623b2012-09-20 19:27:07 -0700171 disp.connected = true;
Jesse Halldb276212012-09-07 11:20:56 -0700172 disp.width = mFbDev->width;
173 disp.height = mFbDev->height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700174 disp.format = mFbDev->format;
175 disp.xdpi = mFbDev->xdpi;
176 disp.ydpi = mFbDev->ydpi;
Mathias Agopianf4358632012-08-22 17:16:19 -0700177 if (disp.refresh == 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700178 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
Mathias Agopianf4358632012-08-22 17:16:19 -0700179 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700180 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700181 if (disp.refresh == 0) {
182 disp.refresh = nsecs_t(1e9 / 60.0);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700183 ALOGW("getting VSYNC period from thin air: %lld",
184 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopianf4358632012-08-22 17:16:19 -0700185 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700186 } else if (mHwc) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700187 // here we're guaranteed to have at least HWC 1.1
Jesse Hall9e663de2013-08-16 14:28:37 -0700188 for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700189 queryDisplayProperties(i);
190 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700191 }
192
Mathias Agopian3a778712012-04-09 14:16:47 -0700193 if (needVSyncThread) {
194 // we don't have VSYNC support, we need to fake it
195 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700196 }
197}
198
199HWComposer::~HWComposer() {
Andy McFaddenbabba182012-09-12 13:14:51 -0700200 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700201 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Andy McFaddenbabba182012-09-12 13:14:51 -0700202 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700203 if (mVSyncThread != NULL) {
204 mVSyncThread->requestExitAndWait();
205 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700206 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700207 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700208 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700209 if (mFbDev) {
210 framebuffer_close(mFbDev);
211 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700212 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700213}
214
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700215// Load and prepare the hardware composer module. Sets mHwc.
216void HWComposer::loadHwcModule()
217{
218 hw_module_t const* module;
219
220 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
221 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
222 return;
223 }
224
225 int err = hwc_open_1(module, &mHwc);
226 if (err) {
227 ALOGE("%s device failed to initialize (%s)",
228 HWC_HARDWARE_COMPOSER, strerror(-err));
229 return;
230 }
231
232 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
233 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
234 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
235 ALOGE("%s device version %#x unsupported, will not be used",
236 HWC_HARDWARE_COMPOSER, mHwc->common.version);
237 hwc_close_1(mHwc);
238 mHwc = NULL;
239 return;
240 }
241}
242
243// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
Jesse Hallef64b752013-03-18 11:28:50 -0700244int HWComposer::loadFbHalModule()
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700245{
246 hw_module_t const* module;
247
Jesse Hallef64b752013-03-18 11:28:50 -0700248 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
249 if (err != 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700250 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
Jesse Hallef64b752013-03-18 11:28:50 -0700251 return err;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700252 }
253
Jesse Hallef64b752013-03-18 11:28:50 -0700254 return framebuffer_open(module, &mFbDev);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700255}
256
Mathias Agopiana350ff92010-08-10 17:14:02 -0700257status_t HWComposer::initCheck() const {
258 return mHwc ? NO_ERROR : NO_INIT;
259}
260
Jesse Hallbbd164a2012-08-21 12:05:09 -0700261void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
262 cb_context* ctx = reinterpret_cast<cb_context*>(
263 const_cast<hwc_procs_t*>(procs));
264 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700265}
266
Jesse Hall1bd20e02012-08-29 10:47:52 -0700267void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700268 int64_t timestamp) {
269 cb_context* ctx = reinterpret_cast<cb_context*>(
270 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700271 ctx->hwc->vsync(disp, timestamp);
272}
273
274void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
275 int connected) {
276 cb_context* ctx = reinterpret_cast<cb_context*>(
277 const_cast<hwc_procs_t*>(procs));
278 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700279}
280
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700281void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700282 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700283}
284
Jesse Hall1bd20e02012-08-29 10:47:52 -0700285void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopianbef42c52013-08-21 17:45:46 -0700286 if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
Jesse Hall8e26b282013-10-14 12:36:45 -0700287 {
288 Mutex::Autolock _l(mLock);
289
290 // There have been reports of HWCs that signal several vsync events
291 // with the same timestamp when turning the display off and on. This
292 // is a bug in the HWC implementation, but filter the extra events
293 // out here so they don't cause havoc downstream.
294 if (timestamp == mLastHwVSync[disp]) {
295 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%lld)",
296 timestamp);
297 return;
298 }
299
300 mLastHwVSync[disp] = timestamp;
301 }
302
Mathias Agopianbef42c52013-08-21 17:45:46 -0700303 char tag[16];
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700304 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700305 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
306
307 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700308 }
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700309}
310
Jesse Hall1bd20e02012-08-29 10:47:52 -0700311void HWComposer::hotplug(int disp, int connected) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700312 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700313 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
314 disp, connected);
315 return;
316 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700317 queryDisplayProperties(disp);
Mathias Agopian148994e2012-09-19 17:31:36 -0700318 mEventHandler.onHotplugReceived(disp, bool(connected));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700319}
320
Jesse Hall1c569c42013-04-05 13:44:52 -0700321static float getDefaultDensity(uint32_t height) {
322 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
323 else return ACONFIGURATION_DENSITY_TV;
324}
325
Jesse Hall1bd20e02012-08-29 10:47:52 -0700326static const uint32_t DISPLAY_ATTRIBUTES[] = {
327 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700328 HWC_DISPLAY_WIDTH,
329 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700330 HWC_DISPLAY_DPI_X,
331 HWC_DISPLAY_DPI_Y,
332 HWC_DISPLAY_NO_ATTRIBUTE,
333};
334#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
335
Mathias Agopian1604f772012-09-18 21:54:42 -0700336status_t HWComposer::queryDisplayProperties(int disp) {
337
Mathias Agopianda27af92012-09-13 18:17:13 -0700338 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700339
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700340 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700341 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
342 memset(values, 0, sizeof(values));
343
344 uint32_t config;
345 size_t numConfigs = 1;
346 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700347 if (err != NO_ERROR) {
348 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700349 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700350 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700351 }
352
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700353 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
354 if (err != NO_ERROR) {
355 // we can't get this display's info. turn it off.
356 mDisplayData[disp].connected = false;
357 return err;
358 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700359
Jesse Hall1bd20e02012-08-29 10:47:52 -0700360 int32_t w = 0, h = 0;
361 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
362 switch (DISPLAY_ATTRIBUTES[i]) {
363 case HWC_DISPLAY_VSYNC_PERIOD:
364 mDisplayData[disp].refresh = nsecs_t(values[i]);
365 break;
Jesse Halldb276212012-09-07 11:20:56 -0700366 case HWC_DISPLAY_WIDTH:
367 mDisplayData[disp].width = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700368 break;
Jesse Halldb276212012-09-07 11:20:56 -0700369 case HWC_DISPLAY_HEIGHT:
370 mDisplayData[disp].height = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700371 break;
372 case HWC_DISPLAY_DPI_X:
373 mDisplayData[disp].xdpi = values[i] / 1000.0f;
374 break;
375 case HWC_DISPLAY_DPI_Y:
376 mDisplayData[disp].ydpi = values[i] / 1000.0f;
377 break;
378 default:
Mathias Agopianda27af92012-09-13 18:17:13 -0700379 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
380 i, DISPLAY_ATTRIBUTES[i]);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700381 break;
382 }
383 }
384
Mathias Agopianf5a33922012-09-19 18:16:22 -0700385 // FIXME: what should we set the format to?
386 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
387 mDisplayData[disp].connected = true;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700388 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
Jesse Hall1c569c42013-04-05 13:44:52 -0700389 float dpi = getDefaultDensity(h);
390 mDisplayData[disp].xdpi = dpi;
391 mDisplayData[disp].ydpi = dpi;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700392 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700393 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700394}
395
Jesse Hall1c569c42013-04-05 13:44:52 -0700396status_t HWComposer::setVirtualDisplayProperties(int32_t id,
397 uint32_t w, uint32_t h, uint32_t format) {
398 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
399 !mAllocatedDisplayIDs.hasBit(id)) {
400 return BAD_INDEX;
401 }
402 mDisplayData[id].width = w;
403 mDisplayData[id].height = h;
404 mDisplayData[id].format = format;
405 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h);
406 return NO_ERROR;
407}
408
Mathias Agopiane60b0682012-08-21 23:34:09 -0700409int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700410 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700411 return NO_MEMORY;
412 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700413 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
414 mAllocatedDisplayIDs.markBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800415 mDisplayData[id].connected = true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700416 return id;
417}
418
419status_t HWComposer::freeDisplayId(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700420 if (id < NUM_BUILTIN_DISPLAYS) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700421 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700422 return BAD_VALUE;
423 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700424 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700425 return BAD_INDEX;
426 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700427 mAllocatedDisplayIDs.clearBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800428 mDisplayData[id].connected = false;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700429 return NO_ERROR;
430}
431
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700432nsecs_t HWComposer::getRefreshPeriod(int disp) const {
433 return mDisplayData[disp].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700434}
435
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700436nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700437 // this returns the last refresh timestamp.
438 // if the last one is not available, we estimate it based on
439 // the refresh period and whatever closest timestamp we have.
440 Mutex::Autolock _l(mLock);
441 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopianbef42c52013-08-21 17:45:46 -0700442 return now - ((now - mLastHwVSync[disp]) % mDisplayData[disp].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700443}
444
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800445sp<Fence> HWComposer::getDisplayFence(int disp) const {
446 return mDisplayData[disp].lastDisplayFence;
447}
448
Jesse Halldb276212012-09-07 11:20:56 -0700449uint32_t HWComposer::getWidth(int disp) const {
450 return mDisplayData[disp].width;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700451}
452
Jesse Halldb276212012-09-07 11:20:56 -0700453uint32_t HWComposer::getHeight(int disp) const {
454 return mDisplayData[disp].height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700455}
456
457uint32_t HWComposer::getFormat(int disp) const {
Jesse Hall19e87292013-12-23 21:02:15 -0800458 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
459 return HAL_PIXEL_FORMAT_RGBA_8888;
460 } else {
461 return mDisplayData[disp].format;
462 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700463}
464
465float HWComposer::getDpiX(int disp) const {
466 return mDisplayData[disp].xdpi;
467}
468
469float HWComposer::getDpiY(int disp) const {
470 return mDisplayData[disp].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700471}
472
Mathias Agopianf5a33922012-09-19 18:16:22 -0700473bool HWComposer::isConnected(int disp) const {
474 return mDisplayData[disp].connected;
475}
476
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700477void HWComposer::eventControl(int disp, int event, int enabled) {
478 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
Andy McFadden620685c2012-10-19 12:53:46 -0700479 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
480 event, disp, enabled);
481 return;
482 }
483 if (event != EVENT_VSYNC) {
484 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
485 event, disp, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700486 return;
487 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700488 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700489 if (mHwc && !mDebugForceFakeVSync) {
490 // NOTE: we use our own internal lock here because we have to call
491 // into the HWC with the lock held, and we want to make sure
492 // that even if HWC blocks (which it shouldn't), it won't
493 // affect other threads.
494 Mutex::Autolock _l(mEventControlLock);
495 const int32_t eventBit = 1UL << event;
496 const int32_t newValue = enabled ? eventBit : 0;
497 const int32_t oldValue = mDisplayData[disp].events & eventBit;
498 if (newValue != oldValue) {
499 ATRACE_CALL();
500 err = mHwc->eventControl(mHwc, disp, event, enabled);
501 if (!err) {
502 int32_t& events(mDisplayData[disp].events);
503 events = (events & ~eventBit) | newValue;
504 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700505 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700506 // error here should not happen -- not sure what we should
507 // do if it does.
508 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
509 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700510 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700511
512 if (err == NO_ERROR && mVSyncThread != NULL) {
513 mVSyncThread->setEnabled(enabled);
514 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700515}
516
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700517status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700518 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700519 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700520 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700521
Mathias Agopian45721772010-08-12 15:03:26 -0700522 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700523 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700524 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
525 // we need space for the HWC_FRAMEBUFFER_TARGET
526 numLayers++;
527 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700528 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700529 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700530 + numLayers * sizeof(hwc_layer_1_t);
531 free(disp.list);
532 disp.list = (hwc_display_contents_1_t*)malloc(size);
533 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700534 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700535 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
536 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
537 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800538 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
Mathias Agopianda27af92012-09-13 18:17:13 -0700539 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
540 disp.framebufferTarget->hints = 0;
541 disp.framebufferTarget->flags = 0;
542 disp.framebufferTarget->handle = disp.fbTargetHandle;
543 disp.framebufferTarget->transform = 0;
544 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
Mathias Agopian8f63c202013-09-25 20:44:34 -0700545 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
546 disp.framebufferTarget->sourceCropf.left = 0;
547 disp.framebufferTarget->sourceCropf.top = 0;
548 disp.framebufferTarget->sourceCropf.right = disp.width;
549 disp.framebufferTarget->sourceCropf.bottom = disp.height;
550 } else {
551 disp.framebufferTarget->sourceCrop = r;
552 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700553 disp.framebufferTarget->displayFrame = r;
554 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
555 disp.framebufferTarget->visibleRegionScreen.rects =
556 &disp.framebufferTarget->displayFrame;
557 disp.framebufferTarget->acquireFenceFd = -1;
558 disp.framebufferTarget->releaseFenceFd = -1;
Mathias Agopian70a6e882013-03-21 16:25:12 -0700559 disp.framebufferTarget->planeAlpha = 0xFF;
Mathias Agopianda27af92012-09-13 18:17:13 -0700560 }
Jesse Halldb276212012-09-07 11:20:56 -0700561 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700562 disp.list->flags = HWC_GEOMETRY_CHANGED;
563 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700564 }
565 return NO_ERROR;
566}
567
Mathias Agopianda27af92012-09-13 18:17:13 -0700568status_t HWComposer::setFramebufferTarget(int32_t id,
569 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
570 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
571 return BAD_INDEX;
572 }
573 DisplayData& disp(mDisplayData[id]);
574 if (!disp.framebufferTarget) {
575 // this should never happen, but apparently eglCreateWindowSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800576 // triggers a Surface::queueBuffer() on some
Mathias Agopianda27af92012-09-13 18:17:13 -0700577 // devices (!?) -- log and ignore.
578 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianda27af92012-09-13 18:17:13 -0700579 return NO_ERROR;
580 }
581
582 int acquireFenceFd = -1;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800583 if (acquireFence->isValid()) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700584 acquireFenceFd = acquireFence->dup();
585 }
586
587 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
588 disp.fbTargetHandle = buf->handle;
589 disp.framebufferTarget->handle = disp.fbTargetHandle;
590 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
591 return NO_ERROR;
592}
593
Mathias Agopiane60b0682012-08-21 23:34:09 -0700594status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700595 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700596 DisplayData& disp(mDisplayData[i]);
597 if (disp.framebufferTarget) {
598 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
599 // DO NOT reset the handle field to NULL, because it's possible
600 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
601 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700602 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700603 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
604 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700605 if (!disp.connected && disp.list != NULL) {
606 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
607 i, disp.list->numHwLayers);
608 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700609 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700610 if (mLists[i]) {
Jesse Hall38efe862013-04-06 23:12:29 -0700611 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
Jesse Hallf7a67582013-11-05 16:12:31 -0800612 mLists[i]->outbuf = disp.outbufHandle;
Jesse Halldb276212012-09-07 11:20:56 -0700613 mLists[i]->outbufAcquireFenceFd = -1;
614 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
615 // garbage data to catch improper use
616 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
617 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
618 } else {
619 mLists[i]->dpy = EGL_NO_DISPLAY;
620 mLists[i]->sur = EGL_NO_SURFACE;
621 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700622 }
623 }
Jesse Halldb276212012-09-07 11:20:56 -0700624
Mathias Agopianf4358632012-08-22 17:16:19 -0700625 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700626 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
627
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700628 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700629 // here we're just making sure that "skip" layers are set
630 // to HWC_FRAMEBUFFER and we're also counting how many layers
631 // we have of each type.
Jesse Halld05a17f2013-09-30 16:49:30 -0700632 //
633 // If there are no window layers, we treat the display has having FB
634 // composition, because SurfaceFlinger will use GLES to draw the
635 // wormhole region.
Mathias Agopianf4358632012-08-22 17:16:19 -0700636 for (size_t i=0 ; i<mNumDisplays ; i++) {
637 DisplayData& disp(mDisplayData[i]);
638 disp.hasFbComp = false;
639 disp.hasOvComp = false;
640 if (disp.list) {
641 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
642 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700643
644 //ALOGD("prepare: %d, type=%d, handle=%p",
645 // i, l.compositionType, l.handle);
646
Mathias Agopianf4358632012-08-22 17:16:19 -0700647 if (l.flags & HWC_SKIP_LAYER) {
648 l.compositionType = HWC_FRAMEBUFFER;
649 }
650 if (l.compositionType == HWC_FRAMEBUFFER) {
651 disp.hasFbComp = true;
652 }
653 if (l.compositionType == HWC_OVERLAY) {
654 disp.hasOvComp = true;
655 }
656 }
Jesse Halld05a17f2013-09-30 16:49:30 -0700657 if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
658 disp.hasFbComp = true;
659 }
660 } else {
661 disp.hasFbComp = true;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700662 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700663 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700664 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700665 return (status_t)err;
666}
667
Mathias Agopiane60b0682012-08-21 23:34:09 -0700668bool HWComposer::hasHwcComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700669 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700670 return false;
671 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700672}
673
Mathias Agopiane60b0682012-08-21 23:34:09 -0700674bool HWComposer::hasGlesComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700675 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
676 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700677 return mDisplayData[id].hasFbComp;
678}
679
Jesse Hall13f01cb2013-03-20 11:37:21 -0700680sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700681 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Jesse Hall13f01cb2013-03-20 11:37:21 -0700682 return Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700683
684 int fd = INVALID_OPERATION;
685 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
686 const DisplayData& disp(mDisplayData[id]);
687 if (disp.framebufferTarget) {
688 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700689 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700690 disp.framebufferTarget->releaseFenceFd = -1;
691 }
692 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700693 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700694}
695
Mathias Agopian30bcc612012-08-22 15:39:48 -0700696status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700697 int err = NO_ERROR;
698 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700699 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700700 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700701 // by the (dpy, sur) fields and we are guaranteed to have only
702 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700703 mLists[0]->dpy = eglGetCurrentDisplay();
704 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700705 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700706
Jesse Hallafaf14b2013-03-20 13:42:29 -0700707 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
Jesse Hall80e0a392013-03-15 12:32:10 -0700708 DisplayData& disp(mDisplayData[i]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700709 if (disp.outbufHandle) {
710 mLists[i]->outbuf = disp.outbufHandle;
Jesse Hall80e0a392013-03-15 12:32:10 -0700711 mLists[i]->outbufAcquireFenceFd =
Jesse Hall851cfe82013-03-20 13:44:00 -0700712 disp.outbufAcquireFence->dup();
Jesse Hall80e0a392013-03-15 12:32:10 -0700713 }
714 }
715
Mathias Agopian30bcc612012-08-22 15:39:48 -0700716 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700717
718 for (size_t i=0 ; i<mNumDisplays ; i++) {
719 DisplayData& disp(mDisplayData[i]);
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800720 disp.lastDisplayFence = disp.lastRetireFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800721 disp.lastRetireFence = Fence::NO_FENCE;
Mathias Agopianf4358632012-08-22 17:16:19 -0700722 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700723 if (disp.list->retireFenceFd != -1) {
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800724 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
Jesse Halldb276212012-09-07 11:20:56 -0700725 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700726 }
727 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
728 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700729 }
Mathias Agopian58959342010-10-07 14:57:04 -0700730 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700731 return (status_t)err;
732}
733
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700734status_t HWComposer::release(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700735 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700736 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700737 eventControl(disp, HWC_EVENT_VSYNC, 0);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700738 return (status_t)mHwc->blank(mHwc, disp, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700739 }
740 return NO_ERROR;
741}
742
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700743status_t HWComposer::acquire(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700744 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Colin Cross10fbdb62012-07-12 17:56:34 -0700745 if (mHwc) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700746 return (status_t)mHwc->blank(mHwc, disp, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700747 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700748 return NO_ERROR;
749}
750
Andy McFadden27ec5732012-10-02 19:04:45 -0700751void HWComposer::disconnectDisplay(int disp) {
Andy McFadden5a8f9012012-10-04 19:09:45 -0700752 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
Andy McFadden27ec5732012-10-02 19:04:45 -0700753 DisplayData& dd(mDisplayData[disp]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700754 free(dd.list);
755 dd.list = NULL;
756 dd.framebufferTarget = NULL; // points into dd.list
757 dd.fbTargetHandle = NULL;
758 dd.outbufHandle = NULL;
759 dd.lastRetireFence = Fence::NO_FENCE;
760 dd.lastDisplayFence = Fence::NO_FENCE;
761 dd.outbufAcquireFence = Fence::NO_FENCE;
Andy McFadden27ec5732012-10-02 19:04:45 -0700762}
763
Mathias Agopiancde87a32012-09-13 14:09:01 -0700764int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700765 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700766 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
767 // is supported by the implementation. we can only be in this case
768 // if we have HWC 1.1
769 return HAL_PIXEL_FORMAT_RGBA_8888;
770 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700771 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700772 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700773 }
774}
775
Mathias Agopianda27af92012-09-13 18:17:13 -0700776bool HWComposer::supportsFramebufferTarget() const {
777 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
778}
779
780int HWComposer::fbPost(int32_t id,
781 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700782 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700783 return setFramebufferTarget(id, acquireFence, buffer);
784 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700785 acquireFence->waitForever("HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700786 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700787 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700788}
789
790int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700791 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700792 return NO_ERROR;
793
794 if (mFbDev->compositionComplete) {
795 return mFbDev->compositionComplete(mFbDev);
796 } else {
797 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700798 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700799}
800
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700801void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700802 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700803 const size_t SIZE = 4096;
804 char buffer[SIZE];
805 mFbDev->dump(mFbDev, buffer, SIZE);
806 result.append(buffer);
807 }
808}
809
Jesse Hall851cfe82013-03-20 13:44:00 -0700810status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
811 const sp<GraphicBuffer>& buf) {
812 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
813 return BAD_INDEX;
814 if (id < VIRTUAL_DISPLAY_ID_BASE)
815 return INVALID_OPERATION;
816
817 DisplayData& disp(mDisplayData[id]);
818 disp.outbufHandle = buf->handle;
819 disp.outbufAcquireFence = acquireFence;
820 return NO_ERROR;
821}
822
823sp<Fence> HWComposer::getLastRetireFence(int32_t id) {
824 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
825 return Fence::NO_FENCE;
826 return mDisplayData[id].lastRetireFence;
827}
828
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700829/*
830 * Helper template to implement a concrete HWCLayer
831 * This holds the pointer to the concrete hwc layer type
832 * and implements the "iterable" side of HWCLayer.
833 */
834template<typename CONCRETE, typename HWCTYPE>
835class Iterable : public HWComposer::HWCLayer {
836protected:
837 HWCTYPE* const mLayerList;
838 HWCTYPE* mCurrentLayer;
839 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
840 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
841 inline HWCTYPE* getLayer() { return mCurrentLayer; }
842 virtual ~Iterable() { }
843private:
844 // returns a copy of ourselves
845 virtual HWComposer::HWCLayer* dup() {
846 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
847 }
848 virtual status_t setLayer(size_t index) {
849 mCurrentLayer = &mLayerList[index];
850 return NO_ERROR;
851 }
852};
853
Jesse Hall5880cc52012-06-05 23:40:32 -0700854/*
855 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
856 * This implements the HWCLayer side of HWCIterableLayer.
857 */
858class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800859 struct hwc_composer_device_1* mHwc;
Jesse Hall5880cc52012-06-05 23:40:32 -0700860public:
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800861 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
862 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
Jesse Hall5880cc52012-06-05 23:40:32 -0700863
864 virtual int32_t getCompositionType() const {
865 return getLayer()->compositionType;
866 }
867 virtual uint32_t getHints() const {
868 return getLayer()->hints;
869 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700870 virtual sp<Fence> getAndResetReleaseFence() {
Jesse Hallef194142012-06-14 14:45:17 -0700871 int fd = getLayer()->releaseFenceFd;
872 getLayer()->releaseFenceFd = -1;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700873 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Jesse Hallef194142012-06-14 14:45:17 -0700874 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700875 virtual void setAcquireFenceFd(int fenceFd) {
876 getLayer()->acquireFenceFd = fenceFd;
877 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800878 virtual void setPerFrameDefaultState() {
879 //getLayer()->compositionType = HWC_FRAMEBUFFER;
880 }
881 virtual void setPlaneAlpha(uint8_t alpha) {
882 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
883 getLayer()->planeAlpha = alpha;
884 } else {
Mathias Agopian5fe58b82013-02-07 16:22:50 -0800885 if (alpha < 0xFF) {
886 getLayer()->flags |= HWC_SKIP_LAYER;
887 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800888 }
889 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700890 virtual void setDefaultState() {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800891 hwc_layer_1_t* const l = getLayer();
892 l->compositionType = HWC_FRAMEBUFFER;
893 l->hints = 0;
894 l->flags = HWC_SKIP_LAYER;
895 l->handle = 0;
896 l->transform = 0;
897 l->blending = HWC_BLENDING_NONE;
898 l->visibleRegionScreen.numRects = 0;
899 l->visibleRegionScreen.rects = NULL;
900 l->acquireFenceFd = -1;
901 l->releaseFenceFd = -1;
902 l->planeAlpha = 0xFF;
Jesse Hall5880cc52012-06-05 23:40:32 -0700903 }
904 virtual void setSkip(bool skip) {
905 if (skip) {
906 getLayer()->flags |= HWC_SKIP_LAYER;
907 } else {
908 getLayer()->flags &= ~HWC_SKIP_LAYER;
909 }
910 }
911 virtual void setBlending(uint32_t blending) {
912 getLayer()->blending = blending;
913 }
914 virtual void setTransform(uint32_t transform) {
915 getLayer()->transform = transform;
916 }
917 virtual void setFrame(const Rect& frame) {
Mathias Agopian6b442672013-07-09 21:24:52 -0700918 getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
Jesse Hall5880cc52012-06-05 23:40:32 -0700919 }
Mathias Agopian6b442672013-07-09 21:24:52 -0700920 virtual void setCrop(const FloatRect& crop) {
921 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
922 getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
923 } else {
924 /*
925 * Since h/w composer didn't support a flot crop rect before version 1.3,
926 * using integer coordinates instead produces a different output from the GL code in
927 * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
928 * window size ratio is large and a window crop is defined
929 * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
930 */
931 hwc_rect_t& r = getLayer()->sourceCrop;
932 r.left = int(ceilf(crop.left));
933 r.top = int(ceilf(crop.top));
934 r.right = int(floorf(crop.right));
935 r.bottom= int(floorf(crop.bottom));
936 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700937 }
938 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700939 // Region::getSharedBuffer creates a reference to the underlying
940 // SharedBuffer of this Region, this reference is freed
941 // in onDisplayed()
942 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
943 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
944 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700945 }
Jesse Hall399184a2014-03-03 15:42:54 -0800946 virtual void setSidebandStream(const sp<NativeHandle>& stream) {
947 ALOG_ASSERT(stream->handle() != NULL);
948 getLayer()->compositionType = HWC_SIDEBAND;
949 getLayer()->sidebandStream = stream->handle();
950 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700951 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
952 if (buffer == 0 || buffer->handle == 0) {
953 getLayer()->compositionType = HWC_FRAMEBUFFER;
954 getLayer()->flags |= HWC_SKIP_LAYER;
955 getLayer()->handle = 0;
956 } else {
Jesse Hall399184a2014-03-03 15:42:54 -0800957 if (getLayer()->compositionType == HWC_SIDEBAND) {
958 // If this was a sideband layer but the stream was removed, reset
959 // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare.
960 getLayer()->compositionType = HWC_FRAMEBUFFER;
961 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700962 getLayer()->handle = buffer->handle;
963 }
964 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700965 virtual void onDisplayed() {
966 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
967 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
968 if (sb) {
969 sb->release();
970 // not technically needed but safer
971 visibleRegion.numRects = 0;
972 visibleRegion.rects = NULL;
973 }
Jesse Halle25d0052012-09-05 13:03:10 -0700974
975 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700976 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700977};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700978
979/*
980 * returns an iterator initialized at a given index in the layer list
981 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700982HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700983 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700984 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700985 }
986 const DisplayData& disp(mDisplayData[id]);
987 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700988 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700989 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800990 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700991}
992
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700993/*
994 * returns an iterator on the beginning of the layer list
995 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700996HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700997 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700998}
999
1000/*
1001 * returns an iterator on the end of the layer list
1002 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001003HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -07001004 size_t numLayers = 0;
1005 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
1006 const DisplayData& disp(mDisplayData[id]);
1007 if (mHwc && disp.list) {
1008 numLayers = disp.list->numHwLayers;
1009 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
1010 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
1011 // which we ignore when iterating through the layer list.
1012 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
1013 if (numLayers) {
1014 numLayers--;
1015 }
1016 }
1017 }
1018 }
1019 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001020}
1021
Mathias Agopian74d211a2013-04-22 16:55:35 +02001022void HWComposer::dump(String8& result) const {
Jesse Hallb685c542012-07-31 14:32:56 -07001023 if (mHwc) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001024 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -07001025 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
1026 for (size_t i=0 ; i<mNumDisplays ; i++) {
1027 const DisplayData& disp(mDisplayData[i]);
Jesse Hall02d86562013-03-25 14:43:23 -07001028 if (!disp.connected)
1029 continue;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001030
Mathias Agopian13127d82013-03-05 17:47:11 -08001031 const Vector< sp<Layer> >& visibleLayersSortedByZ =
Mathias Agopiancb558572012-10-04 15:58:54 -07001032 mFlinger->getLayerSortedByZForHwcDisplay(i);
1033
Jesse Hall02d86562013-03-25 14:43:23 -07001034 result.appendFormat(
1035 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
1036 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001037
Jesse Hall02d86562013-03-25 14:43:23 -07001038 if (disp.list) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -07001039 result.appendFormat(
1040 " numHwLayers=%u, flags=%08x\n",
1041 disp.list->numHwLayers, disp.list->flags);
1042
Mathias Agopianf4358632012-08-22 17:16:19 -07001043 result.append(
Jesse Hall353ddc62013-08-20 16:11:50 -07001044 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
1045 "------------+----------+----------+----------+----+-------+----------+---------------------------------+--------------------------------\n");
1046 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____._,_____._,_____._,_____._] | [_____,_____,_____,_____]
Mathias Agopianf4358632012-08-22 17:16:19 -07001047 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1048 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -07001049 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -07001050 String8 name("unknown");
Mathias Agopiancb558572012-10-04 15:58:54 -07001051
Mathias Agopianda27af92012-09-13 18:17:13 -07001052 if (i < visibleLayersSortedByZ.size()) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
1054 const sp<GraphicBuffer>& buffer(
1055 layer->getActiveBuffer());
1056 if (buffer != NULL) {
1057 format = buffer->getPixelFormat();
Mathias Agopianf4358632012-08-22 17:16:19 -07001058 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001059 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -07001060 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001061
1062 int type = l.compositionType;
1063 if (type == HWC_FRAMEBUFFER_TARGET) {
1064 name = "HWC_FRAMEBUFFER_TARGET";
1065 format = disp.format;
1066 }
1067
1068 static char const* compositionTypeName[] = {
1069 "GLES",
1070 "HWC",
1071 "BACKGROUND",
1072 "FB TARGET",
1073 "UNKNOWN"};
1074 if (type >= NELEM(compositionTypeName))
1075 type = NELEM(compositionTypeName) - 1;
1076
Jesse Hall353ddc62013-08-20 16:11:50 -07001077 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1078 result.appendFormat(
1079 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7.1f,%7.1f,%7.1f,%7.1f] | [%5d,%5d,%5d,%5d] %s\n",
1080 compositionTypeName[type],
1081 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1082 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1083 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1084 name.string());
1085 } else {
1086 result.appendFormat(
1087 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7d,%7d,%7d,%7d] | [%5d,%5d,%5d,%5d] %s\n",
1088 compositionTypeName[type],
1089 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1090 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1091 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1092 name.string());
1093 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -07001094 }
1095 }
Mathias Agopian83727852010-09-23 18:13:21 -07001096 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001097 }
Mathias Agopian30bcc612012-08-22 15:39:48 -07001098
1099 if (mHwc && mHwc->dump) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02001100 const size_t SIZE = 4096;
1101 char buffer[SIZE];
Mathias Agopian30bcc612012-08-22 15:39:48 -07001102 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001103 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001104 }
1105}
1106
Mathias Agopiana350ff92010-08-10 17:14:02 -07001107// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -07001108
1109HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1110 : mHwc(hwc), mEnabled(false),
1111 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07001112 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -07001113{
1114}
1115
1116void HWComposer::VSyncThread::setEnabled(bool enabled) {
1117 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -07001118 if (mEnabled != enabled) {
1119 mEnabled = enabled;
1120 mCondition.signal();
1121 }
Mathias Agopian2965b262012-04-08 15:13:32 -07001122}
1123
1124void HWComposer::VSyncThread::onFirstRef() {
1125 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1126}
1127
1128bool HWComposer::VSyncThread::threadLoop() {
1129 { // scope for lock
1130 Mutex::Autolock _l(mLock);
1131 while (!mEnabled) {
1132 mCondition.wait(mLock);
1133 }
1134 }
1135
1136 const nsecs_t period = mRefreshPeriod;
1137 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1138 nsecs_t next_vsync = mNextFakeVSync;
1139 nsecs_t sleep = next_vsync - now;
1140 if (sleep < 0) {
1141 // we missed, find where the next vsync should be
1142 sleep = (period - ((now - next_vsync) % period));
1143 next_vsync = now + sleep;
1144 }
1145 mNextFakeVSync = next_vsync + period;
1146
1147 struct timespec spec;
1148 spec.tv_sec = next_vsync / 1000000000;
1149 spec.tv_nsec = next_vsync % 1000000000;
1150
1151 int err;
1152 do {
1153 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1154 } while (err<0 && errno == EINTR);
1155
1156 if (err == 0) {
1157 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1158 }
1159
1160 return true;
1161}
1162
Jesse Halla9a1b002013-02-27 16:39:25 -08001163HWComposer::DisplayData::DisplayData()
Jesse Hall19e87292013-12-23 21:02:15 -08001164: width(0), height(0), format(HAL_PIXEL_FORMAT_RGBA_8888),
Jesse Halla9a1b002013-02-27 16:39:25 -08001165 xdpi(0.0f), ydpi(0.0f),
1166 refresh(0),
1167 connected(false),
1168 hasFbComp(false), hasOvComp(false),
1169 capacity(0), list(NULL),
1170 framebufferTarget(NULL), fbTargetHandle(0),
1171 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
Jesse Hall851cfe82013-03-20 13:44:00 -07001172 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
Jesse Halla9a1b002013-02-27 16:39:25 -08001173 events(0)
1174{}
1175
1176HWComposer::DisplayData::~DisplayData() {
1177 free(list);
1178}
1179
Mathias Agopian2965b262012-04-08 15:13:32 -07001180// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001181}; // namespace android