blob: b8506ee70fdaa23af5e441bc81f64344018ebeaa [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);
188}
189
Mathias Agopian03e40722012-04-26 16:11:59 -0700190void HWComposer::eventControl(int event, int enabled) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700191 status_t err = NO_ERROR;
Erik Gilling1a3bf412012-04-06 14:13:32 -0700192 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700193 if (!mDebugForceFakeVSync) {
194 err = mHwc->methods->eventControl(mHwc, event, enabled);
Mathias Agopian03e40722012-04-26 16:11:59 -0700195 // error here should not happen -- not sure what we should
196 // do if it does.
197 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
198 event, enabled, strerror(-err));
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700199 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700200 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700201
202 if (err == NO_ERROR && mVSyncThread != NULL) {
203 mVSyncThread->setEnabled(enabled);
204 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700205}
206
Mathias Agopiana350ff92010-08-10 17:14:02 -0700207status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopian45721772010-08-12 15:03:26 -0700208 if (mHwc) {
209 if (!mList || mCapacity < numLayers) {
210 free(mList);
Jesse Hall5880cc52012-06-05 23:40:32 -0700211 size_t size = sizeofHwcLayerList(mHwc, numLayers);
212 mList = (hwc_layer_list_1_t*)malloc(size);
Mathias Agopian45721772010-08-12 15:03:26 -0700213 mCapacity = numLayers;
214 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700215 mList->flags = HWC_GEOMETRY_CHANGED;
216 mList->numHwLayers = numLayers;
217 }
218 return NO_ERROR;
219}
220
221status_t HWComposer::prepare() const {
222 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700223 if (err == NO_ERROR) {
224 size_t numOVLayers = 0;
225 size_t numFBLayers = 0;
226 size_t count = mList->numHwLayers;
227 for (size_t i=0 ; i<count ; i++) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700228 hwc_layer_1_t* l = NULL;
229 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
230 l = &mList->hwLayers[i];
231 } else {
232 // mList really has hwc_layer_list_t memory layout
233 hwc_layer_list_t* list = (hwc_layer_list_t*)mList;
234 hwc_layer_t* layer = &list->hwLayers[i];
235 l = (hwc_layer_1_t*)layer;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700236 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700237 if (l->flags & HWC_SKIP_LAYER) {
238 l->compositionType = HWC_FRAMEBUFFER;
239 }
240 switch (l->compositionType) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700241 case HWC_OVERLAY:
242 numOVLayers++;
243 break;
244 case HWC_FRAMEBUFFER:
245 numFBLayers++;
246 break;
247 }
248 }
249 mNumOVLayers = numOVLayers;
250 mNumFBLayers = numFBLayers;
251 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700252 return (status_t)err;
253}
254
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700255size_t HWComposer::getLayerCount(int type) const {
256 switch (type) {
257 case HWC_OVERLAY:
258 return mNumOVLayers;
259 case HWC_FRAMEBUFFER:
260 return mNumFBLayers;
261 }
262 return 0;
263}
264
Jesse Hall34a09ba2012-07-29 22:35:34 -0700265status_t HWComposer::commit(void* fbDisplay, void* fbSurface) const {
Mathias Agopian86303202012-07-24 22:46:10 -0700266 int err = NO_ERROR;
267 if (mHwc) {
Jesse Hall34a09ba2012-07-29 22:35:34 -0700268 err = mHwc->set(mHwc, fbDisplay, fbSurface, mList);
Mathias Agopian86303202012-07-24 22:46:10 -0700269 if (mList) {
270 mList->flags &= ~HWC_GEOMETRY_CHANGED;
271 }
Mathias Agopian58959342010-10-07 14:57:04 -0700272 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700273 return (status_t)err;
274}
275
Antti Hatalaf5f27122010-09-09 02:33:05 -0700276status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700277 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700278 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700279 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
280 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700281 int err = mHwc->set(mHwc, NULL, NULL, NULL);
Colin Cross10fbdb62012-07-12 17:56:34 -0700282 if (err < 0) {
283 return (status_t)err;
284 }
285
286 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
287 if (mHwc->methods && mHwc->methods->blank) {
288 err = mHwc->methods->blank(mHwc, 1);
289 }
290 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700291 return (status_t)err;
292 }
293 return NO_ERROR;
294}
295
Colin Cross10fbdb62012-07-12 17:56:34 -0700296status_t HWComposer::acquire() const {
297 if (mHwc) {
298 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
299 if (mHwc->methods && mHwc->methods->blank) {
300 int err = mHwc->methods->blank(mHwc, 0);
301 return (status_t)err;
302 }
303 }
304 }
305
306 return NO_ERROR;
307}
308
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700309status_t HWComposer::disable() {
310 if (mHwc) {
311 free(mList);
312 mList = NULL;
313 int err = mHwc->prepare(mHwc, NULL);
314 return (status_t)err;
315 }
316 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700317}
318
Mathias Agopian45721772010-08-12 15:03:26 -0700319size_t HWComposer::getNumLayers() const {
320 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700321}
322
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700323/*
324 * Helper template to implement a concrete HWCLayer
325 * This holds the pointer to the concrete hwc layer type
326 * and implements the "iterable" side of HWCLayer.
327 */
328template<typename CONCRETE, typename HWCTYPE>
329class Iterable : public HWComposer::HWCLayer {
330protected:
331 HWCTYPE* const mLayerList;
332 HWCTYPE* mCurrentLayer;
333 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
334 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
335 inline HWCTYPE* getLayer() { return mCurrentLayer; }
336 virtual ~Iterable() { }
337private:
338 // returns a copy of ourselves
339 virtual HWComposer::HWCLayer* dup() {
340 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
341 }
342 virtual status_t setLayer(size_t index) {
343 mCurrentLayer = &mLayerList[index];
344 return NO_ERROR;
345 }
346};
347
Jesse Hall5880cc52012-06-05 23:40:32 -0700348// #if !HWC_REMOVE_DEPRECATED_VERSIONS
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700349/*
350 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
351 * This implements the HWCLayer side of HWCIterableLayer.
352 */
Jesse Hall5880cc52012-06-05 23:40:32 -0700353class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700354public:
Jesse Hall5880cc52012-06-05 23:40:32 -0700355 HWCLayerVersion0(hwc_layer_t* layer)
356 : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700357
358 virtual int32_t getCompositionType() const {
359 return getLayer()->compositionType;
360 }
361 virtual uint32_t getHints() const {
362 return getLayer()->hints;
363 }
Jesse Hallef194142012-06-14 14:45:17 -0700364 virtual int getAndResetReleaseFenceFd() {
365 // not supported on VERSION_03
366 return -1;
367 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700368 virtual void setAcquireFenceFd(int fenceFd) {
369 if (fenceFd != -1) {
370 ALOGE("HWC 0.x can't handle acquire fences");
371 close(fenceFd);
372 }
373 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700374
375 virtual void setDefaultState() {
376 getLayer()->compositionType = HWC_FRAMEBUFFER;
377 getLayer()->hints = 0;
378 getLayer()->flags = HWC_SKIP_LAYER;
379 getLayer()->transform = 0;
380 getLayer()->blending = HWC_BLENDING_NONE;
381 getLayer()->visibleRegionScreen.numRects = 0;
382 getLayer()->visibleRegionScreen.rects = NULL;
383 }
384 virtual void setSkip(bool skip) {
385 if (skip) {
386 getLayer()->flags |= HWC_SKIP_LAYER;
387 } else {
388 getLayer()->flags &= ~HWC_SKIP_LAYER;
389 }
390 }
391 virtual void setBlending(uint32_t blending) {
392 getLayer()->blending = blending;
393 }
394 virtual void setTransform(uint32_t transform) {
395 getLayer()->transform = transform;
396 }
397 virtual void setFrame(const Rect& frame) {
398 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
399 }
400 virtual void setCrop(const Rect& crop) {
401 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
402 }
403 virtual void setVisibleRegionScreen(const Region& reg) {
404 getLayer()->visibleRegionScreen.rects =
405 reinterpret_cast<hwc_rect_t const *>(
406 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
407 }
408 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
409 if (buffer == 0 || buffer->handle == 0) {
410 getLayer()->compositionType = HWC_FRAMEBUFFER;
411 getLayer()->flags |= HWC_SKIP_LAYER;
412 getLayer()->handle = 0;
413 } else {
414 getLayer()->handle = buffer->handle;
415 }
416 }
417};
Jesse Hall5880cc52012-06-05 23:40:32 -0700418// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
419
420/*
421 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
422 * This implements the HWCLayer side of HWCIterableLayer.
423 */
424class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
425public:
426 HWCLayerVersion1(hwc_layer_1_t* layer)
427 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
428
429 virtual int32_t getCompositionType() const {
430 return getLayer()->compositionType;
431 }
432 virtual uint32_t getHints() const {
433 return getLayer()->hints;
434 }
Jesse Hallef194142012-06-14 14:45:17 -0700435 virtual int getAndResetReleaseFenceFd() {
436 int fd = getLayer()->releaseFenceFd;
437 getLayer()->releaseFenceFd = -1;
438 return fd;
439 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700440 virtual void setAcquireFenceFd(int fenceFd) {
441 getLayer()->acquireFenceFd = fenceFd;
442 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700443
444 virtual void setDefaultState() {
445 getLayer()->compositionType = HWC_FRAMEBUFFER;
446 getLayer()->hints = 0;
447 getLayer()->flags = HWC_SKIP_LAYER;
448 getLayer()->transform = 0;
449 getLayer()->blending = HWC_BLENDING_NONE;
450 getLayer()->visibleRegionScreen.numRects = 0;
451 getLayer()->visibleRegionScreen.rects = NULL;
452 getLayer()->acquireFenceFd = -1;
453 getLayer()->releaseFenceFd = -1;
454 }
455 virtual void setSkip(bool skip) {
456 if (skip) {
457 getLayer()->flags |= HWC_SKIP_LAYER;
458 } else {
459 getLayer()->flags &= ~HWC_SKIP_LAYER;
460 }
461 }
462 virtual void setBlending(uint32_t blending) {
463 getLayer()->blending = blending;
464 }
465 virtual void setTransform(uint32_t transform) {
466 getLayer()->transform = transform;
467 }
468 virtual void setFrame(const Rect& frame) {
469 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
470 }
471 virtual void setCrop(const Rect& crop) {
472 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
473 }
474 virtual void setVisibleRegionScreen(const Region& reg) {
475 getLayer()->visibleRegionScreen.rects =
476 reinterpret_cast<hwc_rect_t const *>(
477 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
478 }
479 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
480 if (buffer == 0 || buffer->handle == 0) {
481 getLayer()->compositionType = HWC_FRAMEBUFFER;
482 getLayer()->flags |= HWC_SKIP_LAYER;
483 getLayer()->handle = 0;
484 } else {
485 getLayer()->handle = buffer->handle;
486 }
487 }
488};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700489
490/*
491 * returns an iterator initialized at a given index in the layer list
492 */
493HWComposer::LayerListIterator HWComposer::getLayerIterator(size_t index) {
494 if (!mList || index > mList->numHwLayers) {
495 return LayerListIterator();
496 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700497 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
498 return LayerListIterator(new HWCLayerVersion1(mList->hwLayers), index);
499 } else {
500 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
501 return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
502 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700503}
504
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700505/*
506 * returns an iterator on the beginning of the layer list
507 */
508HWComposer::LayerListIterator HWComposer::begin() {
509 return getLayerIterator(0);
510}
511
512/*
513 * returns an iterator on the end of the layer list
514 */
515HWComposer::LayerListIterator HWComposer::end() {
516 return getLayerIterator(getNumLayers());
517}
518
519
520
Mathias Agopian22da60c2011-09-09 00:49:11 -0700521void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
522 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700523 if (mHwc && mList) {
524 result.append("Hardware Composer state:\n");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700525 result.appendFormat(" mDebugForceFakeVSync=%d\n",
526 mDebugForceFakeVSync);
527 result.appendFormat(" numHwLayers=%u, flags=%08x\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700528 mList->numHwLayers, mList->flags);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700529 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700530 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
531 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
532 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700533 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700534 hwc_layer_1_t l;
535 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
536 l = mList->hwLayers[i];
537 } else {
538 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
539 *(hwc_layer_t*)&l = list0->hwLayers[i];
540 l.acquireFenceFd = l.releaseFenceFd = -1;
541 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700542 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
543 int32_t format = -1;
544 if (layer->getLayer() != NULL) {
545 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
546 if (buffer != NULL) {
547 format = buffer->getPixelFormat();
548 }
549 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700550 result.appendFormat(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700551 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700552 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700553 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700554 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700555 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700556 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700557 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800558 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700559 if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) {
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800560 mHwc->dump(mHwc, buffer, SIZE);
561 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700562 }
563}
564
Mathias Agopiana350ff92010-08-10 17:14:02 -0700565// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700566
567HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
568 : mHwc(hwc), mEnabled(false),
569 mNextFakeVSync(0),
570 mRefreshPeriod(hwc.mRefreshPeriod)
571{
572}
573
574void HWComposer::VSyncThread::setEnabled(bool enabled) {
575 Mutex::Autolock _l(mLock);
576 mEnabled = enabled;
577 mCondition.signal();
578}
579
580void HWComposer::VSyncThread::onFirstRef() {
581 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
582}
583
584bool HWComposer::VSyncThread::threadLoop() {
585 { // scope for lock
586 Mutex::Autolock _l(mLock);
587 while (!mEnabled) {
588 mCondition.wait(mLock);
589 }
590 }
591
592 const nsecs_t period = mRefreshPeriod;
593 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
594 nsecs_t next_vsync = mNextFakeVSync;
595 nsecs_t sleep = next_vsync - now;
596 if (sleep < 0) {
597 // we missed, find where the next vsync should be
598 sleep = (period - ((now - next_vsync) % period));
599 next_vsync = now + sleep;
600 }
601 mNextFakeVSync = next_vsync + period;
602
603 struct timespec spec;
604 spec.tv_sec = next_vsync / 1000000000;
605 spec.tv_nsec = next_vsync % 1000000000;
606
607 int err;
608 do {
609 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
610 } while (err<0 && errno == EINTR);
611
612 if (err == 0) {
613 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
614 }
615
616 return true;
617}
618
619// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700620}; // namespace android