blob: d3378058dfd7db101927b3889a9d272a0c497de8 [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
20// #define HWC_REMOVE_DEPRECATED_VERSIONS 1
21
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
49// ---------------------------------------------------------------------------
50// Support for HWC_DEVICE_API_VERSION_0_3 and older:
51// Since v0.3 is deprecated and support will be dropped soon, as much as
52// possible the code is written to target v1.0. When using a v0.3 HWC, we
53// allocate v0.3 structures, but assign them to v1.0 pointers. Fields that
54// exist in both versions are located at the same offset, so in most cases we
55// can just use the v1.0 pointer without branches or casts.
56
57#if HWC_REMOVE_DEPRECATED_VERSIONS
58// We need complete types with to satisfy semantic checks, even though the
59// code paths that use these won't get executed at runtime (and will likely be
60// dead-code-eliminated). When we remove the code to support v0.3 we can remove
61// these as well.
62typedef hwc_layer_1_t hwc_layer_t;
63typedef hwc_layer_list_1_t hwc_layer_list_t;
64typedef hwc_composer_device_1_t hwc_composer_device_t;
65#endif
66
67// This function assumes we've already rejected HWC's with lower-than-required
68// versions. Don't use it for the initial "does HWC meet requirements" check!
69static bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
70 if (HWC_REMOVE_DEPRECATED_VERSIONS &&
71 version <= HWC_DEVICE_API_VERSION_1_0) {
72 return true;
73 } else {
74 return hwc->common.version >= version;
75 }
76}
77
78static size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc,
79 size_t numLayers) {
80 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
81 return sizeof(hwc_layer_list_1_t) + numLayers*sizeof(hwc_layer_1_t);
82 } else {
83 return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t);
84 }
85}
86
Mathias Agopiana350ff92010-08-10 17:14:02 -070087// ---------------------------------------------------------------------------
88
Mathias Agopian3e8b8532012-05-13 20:42:01 -070089struct HWComposer::cb_context {
90 struct callbacks : public hwc_procs_t {
91 // these are here to facilitate the transition when adding
92 // new callbacks (an implementation can check for NULL before
93 // calling a new callback).
94 void (*zero[4])(void);
95 };
96 callbacks procs;
97 HWComposer* hwc;
98};
99
100// ---------------------------------------------------------------------------
101
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700102HWComposer::HWComposer(
103 const sp<SurfaceFlinger>& flinger,
104 EventHandler& handler,
105 nsecs_t refreshPeriod)
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700106 : mFlinger(flinger),
107 mModule(0), mHwc(0), mList(0), mCapacity(0),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700108 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700109 mCBContext(new cb_context),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700110 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700111 mRefreshPeriod(refreshPeriod),
112 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700113{
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700114 char value[PROPERTY_VALUE_MAX];
115 property_get("debug.sf.no_hw_vsync", value, "0");
116 mDebugForceFakeVSync = atoi(value);
117
Mathias Agopian028508c2012-07-25 21:12:12 -0700118 bool needVSyncThread = true;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700119 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +0000120 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700121 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700122 err = hwc_open_1(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000123 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -0700124 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700125 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700126 if (HWC_REMOVE_DEPRECATED_VERSIONS &&
127 mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
128 ALOGE("%s device version %#x too old, will not be used",
129 HWC_HARDWARE_COMPOSER, mHwc->common.version);
130 hwc_close_1(mHwc);
131 mHwc = NULL;
132 }
133 }
134
135 if (mHwc) {
Mathias Agopian028508c2012-07-25 21:12:12 -0700136 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
137 // always turn vsync off when we start
138 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
139 needVSyncThread = false;
140 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700141 if (mHwc->registerProcs) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700142 mCBContext->hwc = this;
143 mCBContext->procs.invalidate = &hook_invalidate;
144 mCBContext->procs.vsync = &hook_vsync;
145 mHwc->registerProcs(mHwc, &mCBContext->procs);
146 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700147 }
148 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700149 }
150
151 if (needVSyncThread) {
152 // we don't have VSYNC support, we need to fake it
153 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700154 }
155}
156
157HWComposer::~HWComposer() {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700158 eventControl(EVENT_VSYNC, 0);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700159 free(mList);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700160 if (mVSyncThread != NULL) {
161 mVSyncThread->requestExitAndWait();
162 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700163 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700164 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700165 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700166 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700167}
168
169status_t HWComposer::initCheck() const {
170 return mHwc ? NO_ERROR : NO_INIT;
171}
172
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700173void HWComposer::hook_invalidate(struct hwc_procs* procs) {
174 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
175}
176
Mathias Agopian31d28432012-04-03 16:31:39 -0700177void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
178 reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
179}
180
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700181void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700182 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700183}
184
Mathias Agopian31d28432012-04-03 16:31:39 -0700185void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700186 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700187 mEventHandler.onVSyncReceived(dpy, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700188 Mutex::Autolock _l(mLock);
189 mLastHwVSync = timestamp;
190}
191
192nsecs_t HWComposer::getRefreshTimestamp() const {
193 // this returns the last refresh timestamp.
194 // if the last one is not available, we estimate it based on
195 // the refresh period and whatever closest timestamp we have.
196 Mutex::Autolock _l(mLock);
197 nsecs_t now = systemTime(CLOCK_MONOTONIC);
198 return now - ((now - mLastHwVSync) % mRefreshPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700199}
200
Mathias Agopian03e40722012-04-26 16:11:59 -0700201void HWComposer::eventControl(int event, int enabled) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700202 status_t err = NO_ERROR;
Erik Gilling1a3bf412012-04-06 14:13:32 -0700203 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700204 if (!mDebugForceFakeVSync) {
205 err = mHwc->methods->eventControl(mHwc, event, enabled);
Mathias Agopian03e40722012-04-26 16:11:59 -0700206 // error here should not happen -- not sure what we should
207 // do if it does.
208 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
209 event, enabled, strerror(-err));
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700210 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700211 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700212
213 if (err == NO_ERROR && mVSyncThread != NULL) {
214 mVSyncThread->setEnabled(enabled);
215 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700216}
217
Mathias Agopiana350ff92010-08-10 17:14:02 -0700218status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopian45721772010-08-12 15:03:26 -0700219 if (mHwc) {
220 if (!mList || mCapacity < numLayers) {
221 free(mList);
Jesse Hall5880cc52012-06-05 23:40:32 -0700222 size_t size = sizeofHwcLayerList(mHwc, numLayers);
223 mList = (hwc_layer_list_1_t*)malloc(size);
Mathias Agopian45721772010-08-12 15:03:26 -0700224 mCapacity = numLayers;
225 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700226 mList->flags = HWC_GEOMETRY_CHANGED;
227 mList->numHwLayers = numLayers;
228 }
229 return NO_ERROR;
230}
231
232status_t HWComposer::prepare() const {
233 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700234 if (err == NO_ERROR) {
235 size_t numOVLayers = 0;
236 size_t numFBLayers = 0;
237 size_t count = mList->numHwLayers;
238 for (size_t i=0 ; i<count ; i++) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700239 hwc_layer_1_t* l = NULL;
240 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
241 l = &mList->hwLayers[i];
242 } else {
243 // mList really has hwc_layer_list_t memory layout
244 hwc_layer_list_t* list = (hwc_layer_list_t*)mList;
245 hwc_layer_t* layer = &list->hwLayers[i];
246 l = (hwc_layer_1_t*)layer;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700247 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700248 if (l->flags & HWC_SKIP_LAYER) {
249 l->compositionType = HWC_FRAMEBUFFER;
250 }
251 switch (l->compositionType) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700252 case HWC_OVERLAY:
253 numOVLayers++;
254 break;
255 case HWC_FRAMEBUFFER:
256 numFBLayers++;
257 break;
258 }
259 }
260 mNumOVLayers = numOVLayers;
261 mNumFBLayers = numFBLayers;
262 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700263 return (status_t)err;
264}
265
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700266size_t HWComposer::getLayerCount(int type) const {
267 switch (type) {
268 case HWC_OVERLAY:
269 return mNumOVLayers;
270 case HWC_FRAMEBUFFER:
271 return mNumFBLayers;
272 }
273 return 0;
274}
275
Jesse Hall34a09ba2012-07-29 22:35:34 -0700276status_t HWComposer::commit(void* fbDisplay, void* fbSurface) const {
Mathias Agopian86303202012-07-24 22:46:10 -0700277 int err = NO_ERROR;
278 if (mHwc) {
Jesse Hall34a09ba2012-07-29 22:35:34 -0700279 err = mHwc->set(mHwc, fbDisplay, fbSurface, mList);
Mathias Agopian86303202012-07-24 22:46:10 -0700280 if (mList) {
281 mList->flags &= ~HWC_GEOMETRY_CHANGED;
282 }
Mathias Agopian58959342010-10-07 14:57:04 -0700283 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700284 return (status_t)err;
285}
286
Antti Hatalaf5f27122010-09-09 02:33:05 -0700287status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700288 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700289 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700290 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
291 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700292 int err = mHwc->set(mHwc, NULL, NULL, NULL);
Colin Cross10fbdb62012-07-12 17:56:34 -0700293 if (err < 0) {
294 return (status_t)err;
295 }
296
297 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
298 if (mHwc->methods && mHwc->methods->blank) {
299 err = mHwc->methods->blank(mHwc, 1);
300 }
301 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700302 return (status_t)err;
303 }
304 return NO_ERROR;
305}
306
Colin Cross10fbdb62012-07-12 17:56:34 -0700307status_t HWComposer::acquire() const {
308 if (mHwc) {
309 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
310 if (mHwc->methods && mHwc->methods->blank) {
311 int err = mHwc->methods->blank(mHwc, 0);
312 return (status_t)err;
313 }
314 }
315 }
316
317 return NO_ERROR;
318}
319
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700320status_t HWComposer::disable() {
321 if (mHwc) {
322 free(mList);
323 mList = NULL;
324 int err = mHwc->prepare(mHwc, NULL);
325 return (status_t)err;
326 }
327 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700328}
329
Mathias Agopian45721772010-08-12 15:03:26 -0700330size_t HWComposer::getNumLayers() const {
331 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700332}
333
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700334/*
335 * Helper template to implement a concrete HWCLayer
336 * This holds the pointer to the concrete hwc layer type
337 * and implements the "iterable" side of HWCLayer.
338 */
339template<typename CONCRETE, typename HWCTYPE>
340class Iterable : public HWComposer::HWCLayer {
341protected:
342 HWCTYPE* const mLayerList;
343 HWCTYPE* mCurrentLayer;
344 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
345 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
346 inline HWCTYPE* getLayer() { return mCurrentLayer; }
347 virtual ~Iterable() { }
348private:
349 // returns a copy of ourselves
350 virtual HWComposer::HWCLayer* dup() {
351 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
352 }
353 virtual status_t setLayer(size_t index) {
354 mCurrentLayer = &mLayerList[index];
355 return NO_ERROR;
356 }
357};
358
Jesse Hall5880cc52012-06-05 23:40:32 -0700359// #if !HWC_REMOVE_DEPRECATED_VERSIONS
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700360/*
361 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
362 * This implements the HWCLayer side of HWCIterableLayer.
363 */
Jesse Hall5880cc52012-06-05 23:40:32 -0700364class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700365public:
Jesse Hall5880cc52012-06-05 23:40:32 -0700366 HWCLayerVersion0(hwc_layer_t* layer)
367 : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700368
369 virtual int32_t getCompositionType() const {
370 return getLayer()->compositionType;
371 }
372 virtual uint32_t getHints() const {
373 return getLayer()->hints;
374 }
Jesse Hallef194142012-06-14 14:45:17 -0700375 virtual int getAndResetReleaseFenceFd() {
376 // not supported on VERSION_03
377 return -1;
378 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700379 virtual void setAcquireFenceFd(int fenceFd) {
380 if (fenceFd != -1) {
381 ALOGE("HWC 0.x can't handle acquire fences");
382 close(fenceFd);
383 }
384 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700385
386 virtual void setDefaultState() {
387 getLayer()->compositionType = HWC_FRAMEBUFFER;
388 getLayer()->hints = 0;
389 getLayer()->flags = HWC_SKIP_LAYER;
390 getLayer()->transform = 0;
391 getLayer()->blending = HWC_BLENDING_NONE;
392 getLayer()->visibleRegionScreen.numRects = 0;
393 getLayer()->visibleRegionScreen.rects = NULL;
394 }
395 virtual void setSkip(bool skip) {
396 if (skip) {
397 getLayer()->flags |= HWC_SKIP_LAYER;
398 } else {
399 getLayer()->flags &= ~HWC_SKIP_LAYER;
400 }
401 }
402 virtual void setBlending(uint32_t blending) {
403 getLayer()->blending = blending;
404 }
405 virtual void setTransform(uint32_t transform) {
406 getLayer()->transform = transform;
407 }
408 virtual void setFrame(const Rect& frame) {
409 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
410 }
411 virtual void setCrop(const Rect& crop) {
412 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
413 }
414 virtual void setVisibleRegionScreen(const Region& reg) {
415 getLayer()->visibleRegionScreen.rects =
416 reinterpret_cast<hwc_rect_t const *>(
417 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
418 }
419 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
420 if (buffer == 0 || buffer->handle == 0) {
421 getLayer()->compositionType = HWC_FRAMEBUFFER;
422 getLayer()->flags |= HWC_SKIP_LAYER;
423 getLayer()->handle = 0;
424 } else {
425 getLayer()->handle = buffer->handle;
426 }
427 }
428};
Jesse Hall5880cc52012-06-05 23:40:32 -0700429// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
430
431/*
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 */
504HWComposer::LayerListIterator HWComposer::getLayerIterator(size_t index) {
505 if (!mList || index > mList->numHwLayers) {
506 return LayerListIterator();
507 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700508 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
509 return LayerListIterator(new HWCLayerVersion1(mList->hwLayers), index);
510 } else {
511 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
512 return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
513 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700514}
515
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700516/*
517 * returns an iterator on the beginning of the layer list
518 */
519HWComposer::LayerListIterator HWComposer::begin() {
520 return getLayerIterator(0);
521}
522
523/*
524 * returns an iterator on the end of the layer list
525 */
526HWComposer::LayerListIterator HWComposer::end() {
527 return getLayerIterator(getNumLayers());
528}
529
530
531
Mathias Agopian22da60c2011-09-09 00:49:11 -0700532void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
533 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700534 if (mHwc && mList) {
535 result.append("Hardware Composer state:\n");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700536 result.appendFormat(" mDebugForceFakeVSync=%d\n",
537 mDebugForceFakeVSync);
538 result.appendFormat(" numHwLayers=%u, flags=%08x\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700539 mList->numHwLayers, mList->flags);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700540 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700541 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
542 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
543 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700544 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700545 hwc_layer_1_t l;
546 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
547 l = mList->hwLayers[i];
548 } else {
549 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
550 *(hwc_layer_t*)&l = list0->hwLayers[i];
551 l.acquireFenceFd = l.releaseFenceFd = -1;
552 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700553 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
554 int32_t format = -1;
555 if (layer->getLayer() != NULL) {
556 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
557 if (buffer != NULL) {
558 format = buffer->getPixelFormat();
559 }
560 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700561 result.appendFormat(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700562 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700563 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700564 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700565 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700566 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700567 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700568 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800569 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700570 if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) {
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800571 mHwc->dump(mHwc, buffer, SIZE);
572 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700573 }
574}
575
Mathias Agopiana350ff92010-08-10 17:14:02 -0700576// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700577
578HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
579 : mHwc(hwc), mEnabled(false),
580 mNextFakeVSync(0),
581 mRefreshPeriod(hwc.mRefreshPeriod)
582{
583}
584
585void HWComposer::VSyncThread::setEnabled(bool enabled) {
586 Mutex::Autolock _l(mLock);
587 mEnabled = enabled;
588 mCondition.signal();
589}
590
591void HWComposer::VSyncThread::onFirstRef() {
592 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
593}
594
595bool HWComposer::VSyncThread::threadLoop() {
596 { // scope for lock
597 Mutex::Autolock _l(mLock);
598 while (!mEnabled) {
599 mCondition.wait(mLock);
600 }
601 }
602
603 const nsecs_t period = mRefreshPeriod;
604 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
605 nsecs_t next_vsync = mNextFakeVSync;
606 nsecs_t sleep = next_vsync - now;
607 if (sleep < 0) {
608 // we missed, find where the next vsync should be
609 sleep = (period - ((now - next_vsync) % period));
610 next_vsync = now + sleep;
611 }
612 mNextFakeVSync = next_vsync + period;
613
614 struct timespec spec;
615 spec.tv_sec = next_vsync / 1000000000;
616 spec.tv_nsec = next_vsync % 1000000000;
617
618 int err;
619 do {
620 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
621 } while (err<0 && errno == EINTR);
622
623 if (err == 0) {
624 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
625 }
626
627 return true;
628}
629
630// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700631}; // namespace android