blob: 75c228d304b09d1084b04a95e0dc787ce663747d [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
Jesse Hall5880cc52012-06-05 23:40:32 -070019// Uncomment this to remove support for HWC_DEVICE_API_VERSION_0_3 and older
Mathias Agopian30bcc612012-08-22 15:39:48 -070020#define HWC_REMOVE_DEPRECATED_VERSIONS 1
Jesse Hall5880cc52012-06-05 23:40:32 -070021
Mathias Agopiana350ff92010-08-10 17:14:02 -070022#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070026#include <sys/types.h>
27
28#include <utils/Errors.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
39#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070040#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070041
Mathias Agopian921e6ac2012-07-23 23:11:29 -070042#include "Layer.h" // needed only for debugging
Mathias Agopian22da60c2011-09-09 00:49:11 -070043#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070044#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070045#include "SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070046
47namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070048
Jesse Hall9eb1eb52012-08-29 10:39:38 -070049#define MIN_HWC_HEADER_VERSION 0
50
51static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
52 uint32_t hwcVersion = hwc->common.version;
53 if (MIN_HWC_HEADER_VERSION == 0 &&
54 (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
55 // legacy version encoding
56 hwcVersion <<= 16;
57 }
58 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
59}
60
61static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
62 uint32_t hwcVersion = hwc->common.version;
63 if (MIN_HWC_HEADER_VERSION == 0 &&
64 (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
65 // legacy version encoding
66 hwcVersion <<= 16;
67 }
68 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
69}
70
71static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
72 uint32_t version) {
73 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070074}
75
Mathias Agopiana350ff92010-08-10 17:14:02 -070076// ---------------------------------------------------------------------------
77
Mathias Agopian3e8b8532012-05-13 20:42:01 -070078struct HWComposer::cb_context {
79 struct callbacks : public hwc_procs_t {
80 // these are here to facilitate the transition when adding
81 // new callbacks (an implementation can check for NULL before
82 // calling a new callback).
83 void (*zero[4])(void);
84 };
85 callbacks procs;
86 HWComposer* hwc;
87};
88
89// ---------------------------------------------------------------------------
90
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070091HWComposer::HWComposer(
92 const sp<SurfaceFlinger>& flinger,
Mathias Agopian8b736f12012-08-13 17:54:26 -070093 EventHandler& handler,
94 framebuffer_device_t const* fbDev)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070095 : mFlinger(flinger),
Mathias Agopianf4358632012-08-22 17:16:19 -070096 mModule(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070097 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070098 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070099 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700100{
Mathias Agopiane60b0682012-08-21 23:34:09 -0700101 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
102 mLists[i] = 0;
103 }
Jesse Hallb685c542012-07-31 14:32:56 -0700104
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.no_hw_vsync", value, "0");
107 mDebugForceFakeVSync = atoi(value);
108
Mathias Agopian028508c2012-07-25 21:12:12 -0700109 bool needVSyncThread = true;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700110 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +0000111 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700112 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700113 err = hwc_open_1(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000114 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -0700115 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700116 if (err == 0) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700117 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
118 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
119 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
120 ALOGE("%s device version %#x unsupported, will not be used",
Jesse Hall5880cc52012-06-05 23:40:32 -0700121 HWC_HARDWARE_COMPOSER, mHwc->common.version);
122 hwc_close_1(mHwc);
123 mHwc = NULL;
124 }
125 }
126
127 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700128 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
129 (hwcApiVersion(mHwc) >> 24) & 0xff,
130 (hwcApiVersion(mHwc) >> 16) & 0xff);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700131 if (mHwc->registerProcs) {
132 mCBContext->hwc = this;
133 mCBContext->procs.invalidate = &hook_invalidate;
134 mCBContext->procs.vsync = &hook_vsync;
135 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
136 mHwc->registerProcs(mHwc, &mCBContext->procs);
137 }
138
Jesse Hallb685c542012-07-31 14:32:56 -0700139 // always turn vsync off when we start
140 needVSyncThread = false;
Mathias Agopian30bcc612012-08-22 15:39:48 -0700141 mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
142
143 int period;
144 if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700145 mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(period);
Mathias Agopian028508c2012-07-25 21:12:12 -0700146 }
Jesse Hallb685c542012-07-31 14:32:56 -0700147
Mathias Agopianf4358632012-08-22 17:16:19 -0700148 // these IDs are always reserved
149 for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
150 mAllocatedDisplayIDs.markBit(i);
151 // TODO: we query xdpi / ydpi / refresh
152 }
Jesse Hall8f971ff2012-08-22 11:50:00 -0700153
Mathias Agopianf4358632012-08-22 17:16:19 -0700154 // the number of displays we actually have depends on the
155 // hw composer version
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700156 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700157 // 1.2 adds support for virtual displays
158 mNumDisplays = MAX_DISPLAYS;
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700159 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
160 // 1.1 adds support for multiple displays
161 mNumDisplays = HWC_NUM_DISPLAY_TYPES;
162 } else {
163 mNumDisplays = 1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700164 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700165 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700166 }
167
Mathias Agopian8b736f12012-08-13 17:54:26 -0700168 if (fbDev) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700169 // if we're here it means we are on version 1.0
170 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
171 disp.xdpi = fbDev->xdpi;
172 disp.ydpi = fbDev->ydpi;
173 if (disp.refresh == 0) {
174 disp.refresh = nsecs_t(1e9 / fbDev->fps);
175 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700176 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700177 if (disp.refresh == 0) {
178 disp.refresh = nsecs_t(1e9 / 60.0);
179 ALOGW("getting VSYNC period thin air: %lld", mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
180 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700181 }
182
Mathias Agopian3a778712012-04-09 14:16:47 -0700183 if (needVSyncThread) {
184 // we don't have VSYNC support, we need to fake it
185 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700186 }
187}
188
189HWComposer::~HWComposer() {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700190 mHwc->eventControl(mHwc, 0, EVENT_VSYNC, 0);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700191 if (mVSyncThread != NULL) {
192 mVSyncThread->requestExitAndWait();
193 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700194 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700195 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700196 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700197 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700198}
199
200status_t HWComposer::initCheck() const {
201 return mHwc ? NO_ERROR : NO_INIT;
202}
203
Jesse Hallbbd164a2012-08-21 12:05:09 -0700204void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
205 cb_context* ctx = reinterpret_cast<cb_context*>(
206 const_cast<hwc_procs_t*>(procs));
207 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700208}
209
Jesse Hallbbd164a2012-08-21 12:05:09 -0700210void HWComposer::hook_vsync(const struct hwc_procs* procs, int dpy,
211 int64_t timestamp) {
212 cb_context* ctx = reinterpret_cast<cb_context*>(
213 const_cast<hwc_procs_t*>(procs));
214 ctx->hwc->vsync(dpy, timestamp);
Mathias Agopian31d28432012-04-03 16:31:39 -0700215}
216
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700217void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700218 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700219}
220
Mathias Agopian31d28432012-04-03 16:31:39 -0700221void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700222 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700223 mEventHandler.onVSyncReceived(dpy, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700224 Mutex::Autolock _l(mLock);
225 mLastHwVSync = timestamp;
226}
227
Mathias Agopiane60b0682012-08-21 23:34:09 -0700228int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700229 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700230 return NO_MEMORY;
231 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700232 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
233 mAllocatedDisplayIDs.markBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700234 return id;
235}
236
237status_t HWComposer::freeDisplayId(int32_t id) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700238 if (id < HWC_NUM_DISPLAY_TYPES) {
239 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700240 return BAD_VALUE;
241 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700242 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700243 return BAD_INDEX;
244 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700245 mAllocatedDisplayIDs.clearBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700246 return NO_ERROR;
247}
248
Mathias Agopian888c8222012-08-04 21:10:38 -0700249nsecs_t HWComposer::getRefreshPeriod() const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700250 return mDisplayData[HWC_DISPLAY_PRIMARY].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700251}
252
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700253nsecs_t HWComposer::getRefreshTimestamp() const {
254 // this returns the last refresh timestamp.
255 // if the last one is not available, we estimate it based on
256 // the refresh period and whatever closest timestamp we have.
257 Mutex::Autolock _l(mLock);
258 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopianf4358632012-08-22 17:16:19 -0700259 return now - ((now - mLastHwVSync) % mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700260}
261
Mathias Agopian8b736f12012-08-13 17:54:26 -0700262float HWComposer::getDpiX() const {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700263 return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700264}
265
266float HWComposer::getDpiY() const {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700267 return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700268}
269
Mathias Agopian03e40722012-04-26 16:11:59 -0700270void HWComposer::eventControl(int event, int enabled) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700271 status_t err = NO_ERROR;
Mathias Agopian30bcc612012-08-22 15:39:48 -0700272 if (mHwc) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700273 if (!mDebugForceFakeVSync) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700274 err = mHwc->eventControl(mHwc, 0, event, enabled);
Mathias Agopian03e40722012-04-26 16:11:59 -0700275 // error here should not happen -- not sure what we should
276 // do if it does.
277 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
278 event, enabled, strerror(-err));
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700279 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700280 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700281
282 if (err == NO_ERROR && mVSyncThread != NULL) {
283 mVSyncThread->setEnabled(enabled);
284 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700285}
286
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700287status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700288 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700289 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700290 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700291
Mathias Agopian45721772010-08-12 15:03:26 -0700292 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700293 DisplayData& disp(mDisplayData[id]);
Andy McFadden13a082e2012-08-24 10:16:42 -0700294 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700295 const size_t size = sizeof(hwc_display_contents_1_t)
296 + numLayers * sizeof(hwc_layer_1_t);
297 free(disp.list);
298 disp.list = (hwc_display_contents_1_t*)malloc(size);
299 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700300 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700301 disp.list->flags = HWC_GEOMETRY_CHANGED;
302 disp.list->numHwLayers = numLayers;
303 disp.list->flipFenceFd = -1;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700304 }
305 return NO_ERROR;
306}
307
Mathias Agopiane60b0682012-08-21 23:34:09 -0700308status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700309 for (size_t i=0 ; i<mNumDisplays ; i++) {
310 mLists[i] = mDisplayData[i].list;
311 if (mLists[i]) {
312 mLists[i]->dpy = EGL_NO_DISPLAY;
313 mLists[i]->sur = EGL_NO_SURFACE;
314 }
315 }
316 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700317 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700318 // here we're just making sure that "skip" layers are set
319 // to HWC_FRAMEBUFFER and we're also counting how many layers
320 // we have of each type.
Mathias Agopianf4358632012-08-22 17:16:19 -0700321 for (size_t i=0 ; i<mNumDisplays ; i++) {
322 DisplayData& disp(mDisplayData[i]);
323 disp.hasFbComp = false;
324 disp.hasOvComp = false;
325 if (disp.list) {
326 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
327 hwc_layer_1_t& l = disp.list->hwLayers[i];
328 if (l.flags & HWC_SKIP_LAYER) {
329 l.compositionType = HWC_FRAMEBUFFER;
330 }
331 if (l.compositionType == HWC_FRAMEBUFFER) {
332 disp.hasFbComp = true;
333 }
334 if (l.compositionType == HWC_OVERLAY) {
335 disp.hasOvComp = true;
336 }
337 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700338 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700339 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700340 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700341 return (status_t)err;
342}
343
Mathias Agopiane60b0682012-08-21 23:34:09 -0700344bool HWComposer::hasHwcComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700345 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700346 return false;
347 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700348}
349
Mathias Agopiane60b0682012-08-21 23:34:09 -0700350bool HWComposer::hasGlesComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700351 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700352 return false;
353 return mDisplayData[id].hasFbComp;
354}
355
Mathias Agopian30bcc612012-08-22 15:39:48 -0700356status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700357 int err = NO_ERROR;
358 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700359 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700360 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700361 // by the (dpy, sur) fields and we are guaranteed to have only
362 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700363 mLists[0]->dpy = eglGetCurrentDisplay();
364 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700365 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700366
Mathias Agopian30bcc612012-08-22 15:39:48 -0700367 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700368
369 for (size_t i=0 ; i<mNumDisplays ; i++) {
370 DisplayData& disp(mDisplayData[i]);
371 if (disp.list) {
372 if (disp.list->flipFenceFd != -1) {
373 close(disp.list->flipFenceFd);
374 disp.list->flipFenceFd = -1;
375 }
376 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
377 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700378 }
Mathias Agopian58959342010-10-07 14:57:04 -0700379 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700380 return (status_t)err;
381}
382
Antti Hatalaf5f27122010-09-09 02:33:05 -0700383status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700384 if (mHwc) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700385 mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
386 return (status_t)mHwc->blank(mHwc, 0, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700387 }
388 return NO_ERROR;
389}
390
Colin Cross10fbdb62012-07-12 17:56:34 -0700391status_t HWComposer::acquire() const {
392 if (mHwc) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700393 return (status_t)mHwc->blank(mHwc, 0, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700394 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700395 return NO_ERROR;
396}
397
Mathias Agopianf4358632012-08-22 17:16:19 -0700398size_t HWComposer::getNumLayers(int32_t id) const {
399 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
400 return 0;
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700401 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700402 return (mHwc && mDisplayData[id].list) ?
403 mDisplayData[id].list->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700404}
405
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700406/*
407 * Helper template to implement a concrete HWCLayer
408 * This holds the pointer to the concrete hwc layer type
409 * and implements the "iterable" side of HWCLayer.
410 */
411template<typename CONCRETE, typename HWCTYPE>
412class Iterable : public HWComposer::HWCLayer {
413protected:
414 HWCTYPE* const mLayerList;
415 HWCTYPE* mCurrentLayer;
416 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
417 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
418 inline HWCTYPE* getLayer() { return mCurrentLayer; }
419 virtual ~Iterable() { }
420private:
421 // returns a copy of ourselves
422 virtual HWComposer::HWCLayer* dup() {
423 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
424 }
425 virtual status_t setLayer(size_t index) {
426 mCurrentLayer = &mLayerList[index];
427 return NO_ERROR;
428 }
429};
430
Jesse Hall5880cc52012-06-05 23:40:32 -0700431/*
432 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
433 * This implements the HWCLayer side of HWCIterableLayer.
434 */
435class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
436public:
437 HWCLayerVersion1(hwc_layer_1_t* layer)
438 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
439
440 virtual int32_t getCompositionType() const {
441 return getLayer()->compositionType;
442 }
443 virtual uint32_t getHints() const {
444 return getLayer()->hints;
445 }
Jesse Hallef194142012-06-14 14:45:17 -0700446 virtual int getAndResetReleaseFenceFd() {
447 int fd = getLayer()->releaseFenceFd;
448 getLayer()->releaseFenceFd = -1;
449 return fd;
450 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700451 virtual void setAcquireFenceFd(int fenceFd) {
452 getLayer()->acquireFenceFd = fenceFd;
453 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700454
455 virtual void setDefaultState() {
456 getLayer()->compositionType = HWC_FRAMEBUFFER;
457 getLayer()->hints = 0;
458 getLayer()->flags = HWC_SKIP_LAYER;
459 getLayer()->transform = 0;
460 getLayer()->blending = HWC_BLENDING_NONE;
461 getLayer()->visibleRegionScreen.numRects = 0;
462 getLayer()->visibleRegionScreen.rects = NULL;
463 getLayer()->acquireFenceFd = -1;
464 getLayer()->releaseFenceFd = -1;
465 }
466 virtual void setSkip(bool skip) {
467 if (skip) {
468 getLayer()->flags |= HWC_SKIP_LAYER;
469 } else {
470 getLayer()->flags &= ~HWC_SKIP_LAYER;
471 }
472 }
473 virtual void setBlending(uint32_t blending) {
474 getLayer()->blending = blending;
475 }
476 virtual void setTransform(uint32_t transform) {
477 getLayer()->transform = transform;
478 }
479 virtual void setFrame(const Rect& frame) {
480 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
481 }
482 virtual void setCrop(const Rect& crop) {
483 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
484 }
485 virtual void setVisibleRegionScreen(const Region& reg) {
486 getLayer()->visibleRegionScreen.rects =
487 reinterpret_cast<hwc_rect_t const *>(
488 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
489 }
490 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
491 if (buffer == 0 || buffer->handle == 0) {
492 getLayer()->compositionType = HWC_FRAMEBUFFER;
493 getLayer()->flags |= HWC_SKIP_LAYER;
494 getLayer()->handle = 0;
495 } else {
496 getLayer()->handle = buffer->handle;
497 }
498 }
499};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700500
501/*
502 * returns an iterator initialized at a given index in the layer list
503 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700504HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700505 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700506 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700507 }
508 const DisplayData& disp(mDisplayData[id]);
509 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700510 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700511 }
512 return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700513}
514
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700515/*
516 * returns an iterator on the beginning of the layer list
517 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700518HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700519 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700520}
521
522/*
523 * returns an iterator on the end of the layer list
524 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700525HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700526 return getLayerIterator(id, getNumLayers(id));
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700527}
528
Mathias Agopian22da60c2011-09-09 00:49:11 -0700529void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
530 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700531 if (mHwc) {
Mathias Agopian83727852010-09-23 18:13:21 -0700532 result.append("Hardware Composer state:\n");
Mathias Agopianf4358632012-08-22 17:16:19 -0700533 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
534 for (size_t i=0 ; i<mNumDisplays ; i++) {
535 const DisplayData& disp(mDisplayData[i]);
536 if (disp.list) {
537 result.appendFormat(" id=%d, numHwLayers=%u, flags=%08x\n",
538 i, disp.list->numHwLayers, disp.list->flags);
539 result.append(
540 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
541 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
542 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
543 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
544 const hwc_layer_1_t&l = disp.list->hwLayers[i];
545 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
546 int32_t format = -1;
547 if (layer->getLayer() != NULL) {
548 const sp<GraphicBuffer>& buffer(
549 layer->getLayer()->getActiveBuffer());
550 if (buffer != NULL) {
551 format = buffer->getPixelFormat();
552 }
553 }
554 result.appendFormat(
555 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
556 l.compositionType ? "OVERLAY" : "FB",
557 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
558 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
559 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
560 layer->getName().string());
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700561 }
562 }
Mathias Agopian83727852010-09-23 18:13:21 -0700563 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800564 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700565
566 if (mHwc && mHwc->dump) {
567 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800568 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700569 }
570}
571
Mathias Agopiana350ff92010-08-10 17:14:02 -0700572// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700573
574HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
575 : mHwc(hwc), mEnabled(false),
576 mNextFakeVSync(0),
Mathias Agopiane60b0682012-08-21 23:34:09 -0700577 mRefreshPeriod(hwc.getRefreshPeriod())
Mathias Agopian2965b262012-04-08 15:13:32 -0700578{
579}
580
581void HWComposer::VSyncThread::setEnabled(bool enabled) {
582 Mutex::Autolock _l(mLock);
583 mEnabled = enabled;
584 mCondition.signal();
585}
586
587void HWComposer::VSyncThread::onFirstRef() {
588 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
589}
590
591bool HWComposer::VSyncThread::threadLoop() {
592 { // scope for lock
593 Mutex::Autolock _l(mLock);
594 while (!mEnabled) {
595 mCondition.wait(mLock);
596 }
597 }
598
599 const nsecs_t period = mRefreshPeriod;
600 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
601 nsecs_t next_vsync = mNextFakeVSync;
602 nsecs_t sleep = next_vsync - now;
603 if (sleep < 0) {
604 // we missed, find where the next vsync should be
605 sleep = (period - ((now - next_vsync) % period));
606 next_vsync = now + sleep;
607 }
608 mNextFakeVSync = next_vsync + period;
609
610 struct timespec spec;
611 spec.tv_sec = next_vsync / 1000000000;
612 spec.tv_nsec = next_vsync % 1000000000;
613
614 int err;
615 do {
616 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
617 } while (err<0 && errno == EINTR);
618
619 if (err == 0) {
620 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
621 }
622
623 return true;
624}
625
626// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700627}; // namespace android