blob: 6d0631d8d39360902f7d236e0f52c868b160c7e5 [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
42#include <EGL/egl.h>
43
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044#include "Layer.h" // needed only for debugging
Mathias Agopian22da60c2011-09-09 00:49:11 -070045#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070046#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070047#include "SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070048
49namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070050
51// ---------------------------------------------------------------------------
52// Support for HWC_DEVICE_API_VERSION_0_3 and older:
53// Since v0.3 is deprecated and support will be dropped soon, as much as
54// possible the code is written to target v1.0. When using a v0.3 HWC, we
55// allocate v0.3 structures, but assign them to v1.0 pointers. Fields that
56// exist in both versions are located at the same offset, so in most cases we
57// can just use the v1.0 pointer without branches or casts.
58
59#if HWC_REMOVE_DEPRECATED_VERSIONS
60// We need complete types with to satisfy semantic checks, even though the
61// code paths that use these won't get executed at runtime (and will likely be
62// dead-code-eliminated). When we remove the code to support v0.3 we can remove
63// these as well.
64typedef hwc_layer_1_t hwc_layer_t;
65typedef hwc_layer_list_1_t hwc_layer_list_t;
66typedef hwc_composer_device_1_t hwc_composer_device_t;
67#endif
68
69// This function assumes we've already rejected HWC's with lower-than-required
70// versions. Don't use it for the initial "does HWC meet requirements" check!
71static bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
72 if (HWC_REMOVE_DEPRECATED_VERSIONS &&
73 version <= HWC_DEVICE_API_VERSION_1_0) {
74 return true;
75 } else {
76 return hwc->common.version >= version;
77 }
78}
79
80static size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc,
81 size_t numLayers) {
82 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
83 return sizeof(hwc_layer_list_1_t) + numLayers*sizeof(hwc_layer_1_t);
84 } else {
85 return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t);
86 }
87}
88
Mathias Agopiana350ff92010-08-10 17:14:02 -070089// ---------------------------------------------------------------------------
90
Mathias Agopian3e8b8532012-05-13 20:42:01 -070091struct HWComposer::cb_context {
92 struct callbacks : public hwc_procs_t {
93 // these are here to facilitate the transition when adding
94 // new callbacks (an implementation can check for NULL before
95 // calling a new callback).
96 void (*zero[4])(void);
97 };
98 callbacks procs;
99 HWComposer* hwc;
100};
101
102// ---------------------------------------------------------------------------
103
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700104HWComposer::HWComposer(
105 const sp<SurfaceFlinger>& flinger,
106 EventHandler& handler,
107 nsecs_t refreshPeriod)
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700108 : mFlinger(flinger),
109 mModule(0), mHwc(0), mList(0), mCapacity(0),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700110 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700111 mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE),
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700112 mCBContext(new cb_context),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700113 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700114 mRefreshPeriod(refreshPeriod),
115 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700116{
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700117 char value[PROPERTY_VALUE_MAX];
118 property_get("debug.sf.no_hw_vsync", value, "0");
119 mDebugForceFakeVSync = atoi(value);
120
Mathias Agopian3a778712012-04-09 14:16:47 -0700121 bool needVSyncThread = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700122 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +0000123 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700124 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700125 err = hwc_open_1(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000126 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -0700127 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700128 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700129 if (HWC_REMOVE_DEPRECATED_VERSIONS &&
130 mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
131 ALOGE("%s device version %#x too old, will not be used",
132 HWC_HARDWARE_COMPOSER, mHwc->common.version);
133 hwc_close_1(mHwc);
134 mHwc = NULL;
135 }
136 }
137
138 if (mHwc) {
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700139 if (mHwc->registerProcs) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700140 mCBContext->hwc = this;
141 mCBContext->procs.invalidate = &hook_invalidate;
142 mCBContext->procs.vsync = &hook_vsync;
143 mHwc->registerProcs(mHwc, &mCBContext->procs);
144 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700145 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700146 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700147 if (mDebugForceFakeVSync) {
148 // make sure to turn h/w vsync off in "fake vsync" mode
149 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
150 }
151 } else {
Mathias Agopian3a778712012-04-09 14:16:47 -0700152 needVSyncThread = true;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700153 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700154 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700155 } else {
156 needVSyncThread = true;
157 }
158
159 if (needVSyncThread) {
160 // we don't have VSYNC support, we need to fake it
161 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700162 }
163}
164
165HWComposer::~HWComposer() {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700166 eventControl(EVENT_VSYNC, 0);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700167 free(mList);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700168 if (mVSyncThread != NULL) {
169 mVSyncThread->requestExitAndWait();
170 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700171 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700172 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700173 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700174 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700175}
176
177status_t HWComposer::initCheck() const {
178 return mHwc ? NO_ERROR : NO_INIT;
179}
180
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700181void HWComposer::hook_invalidate(struct hwc_procs* procs) {
182 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
183}
184
Mathias Agopian31d28432012-04-03 16:31:39 -0700185void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
186 reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
187}
188
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700189void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700190 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700191}
192
Mathias Agopian31d28432012-04-03 16:31:39 -0700193void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700194 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700195 mEventHandler.onVSyncReceived(dpy, timestamp);
196}
197
Mathias Agopian03e40722012-04-26 16:11:59 -0700198void HWComposer::eventControl(int event, int enabled) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700199 status_t err = NO_ERROR;
Erik Gilling1a3bf412012-04-06 14:13:32 -0700200 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700201 if (!mDebugForceFakeVSync) {
202 err = mHwc->methods->eventControl(mHwc, event, enabled);
Mathias Agopian03e40722012-04-26 16:11:59 -0700203 // error here should not happen -- not sure what we should
204 // do if it does.
205 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
206 event, enabled, strerror(-err));
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700207 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700208 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700209
210 if (err == NO_ERROR && mVSyncThread != NULL) {
211 mVSyncThread->setEnabled(enabled);
212 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700213}
214
Mathias Agopiana350ff92010-08-10 17:14:02 -0700215void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
216 mDpy = (hwc_display_t)dpy;
217 mSur = (hwc_surface_t)sur;
218}
219
220status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopian45721772010-08-12 15:03:26 -0700221 if (mHwc) {
222 if (!mList || mCapacity < numLayers) {
223 free(mList);
Jesse Hall5880cc52012-06-05 23:40:32 -0700224 size_t size = sizeofHwcLayerList(mHwc, numLayers);
225 mList = (hwc_layer_list_1_t*)malloc(size);
Mathias Agopian45721772010-08-12 15:03:26 -0700226 mCapacity = numLayers;
227 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700228 mList->flags = HWC_GEOMETRY_CHANGED;
229 mList->numHwLayers = numLayers;
230 }
231 return NO_ERROR;
232}
233
234status_t HWComposer::prepare() const {
235 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700236 if (err == NO_ERROR) {
237 size_t numOVLayers = 0;
238 size_t numFBLayers = 0;
239 size_t count = mList->numHwLayers;
240 for (size_t i=0 ; i<count ; i++) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700241 hwc_layer_1_t* l = NULL;
242 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
243 l = &mList->hwLayers[i];
244 } else {
245 // mList really has hwc_layer_list_t memory layout
246 hwc_layer_list_t* list = (hwc_layer_list_t*)mList;
247 hwc_layer_t* layer = &list->hwLayers[i];
248 l = (hwc_layer_1_t*)layer;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700249 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700250 if (l->flags & HWC_SKIP_LAYER) {
251 l->compositionType = HWC_FRAMEBUFFER;
252 }
253 switch (l->compositionType) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700254 case HWC_OVERLAY:
255 numOVLayers++;
256 break;
257 case HWC_FRAMEBUFFER:
258 numFBLayers++;
259 break;
260 }
261 }
262 mNumOVLayers = numOVLayers;
263 mNumFBLayers = numFBLayers;
264 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700265 return (status_t)err;
266}
267
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700268size_t HWComposer::getLayerCount(int type) const {
269 switch (type) {
270 case HWC_OVERLAY:
271 return mNumOVLayers;
272 case HWC_FRAMEBUFFER:
273 return mNumFBLayers;
274 }
275 return 0;
276}
277
Mathias Agopiana350ff92010-08-10 17:14:02 -0700278status_t HWComposer::commit() const {
279 int err = mHwc->set(mHwc, mDpy, mSur, mList);
Mathias Agopian58959342010-10-07 14:57:04 -0700280 if (mList) {
281 mList->flags &= ~HWC_GEOMETRY_CHANGED;
282 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700283 return (status_t)err;
284}
285
Antti Hatalaf5f27122010-09-09 02:33:05 -0700286status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700287 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700288 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700289 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
290 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700291 int err = mHwc->set(mHwc, NULL, NULL, NULL);
Colin Cross10fbdb62012-07-12 17:56:34 -0700292 if (err < 0) {
293 return (status_t)err;
294 }
295
296 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
297 if (mHwc->methods && mHwc->methods->blank) {
298 err = mHwc->methods->blank(mHwc, 1);
299 }
300 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700301 return (status_t)err;
302 }
303 return NO_ERROR;
304}
305
Colin Cross10fbdb62012-07-12 17:56:34 -0700306status_t HWComposer::acquire() const {
307 if (mHwc) {
308 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
309 if (mHwc->methods && mHwc->methods->blank) {
310 int err = mHwc->methods->blank(mHwc, 0);
311 return (status_t)err;
312 }
313 }
314 }
315
316 return NO_ERROR;
317}
318
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700319status_t HWComposer::disable() {
320 if (mHwc) {
321 free(mList);
322 mList = NULL;
323 int err = mHwc->prepare(mHwc, NULL);
324 return (status_t)err;
325 }
326 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700327}
328
Mathias Agopian45721772010-08-12 15:03:26 -0700329size_t HWComposer::getNumLayers() const {
330 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700331}
332
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700333/*
334 * Helper template to implement a concrete HWCLayer
335 * This holds the pointer to the concrete hwc layer type
336 * and implements the "iterable" side of HWCLayer.
337 */
338template<typename CONCRETE, typename HWCTYPE>
339class Iterable : public HWComposer::HWCLayer {
340protected:
341 HWCTYPE* const mLayerList;
342 HWCTYPE* mCurrentLayer;
343 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
344 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
345 inline HWCTYPE* getLayer() { return mCurrentLayer; }
346 virtual ~Iterable() { }
347private:
348 // returns a copy of ourselves
349 virtual HWComposer::HWCLayer* dup() {
350 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
351 }
352 virtual status_t setLayer(size_t index) {
353 mCurrentLayer = &mLayerList[index];
354 return NO_ERROR;
355 }
356};
357
Jesse Hall5880cc52012-06-05 23:40:32 -0700358// #if !HWC_REMOVE_DEPRECATED_VERSIONS
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700359/*
360 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
361 * This implements the HWCLayer side of HWCIterableLayer.
362 */
Jesse Hall5880cc52012-06-05 23:40:32 -0700363class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700364public:
Jesse Hall5880cc52012-06-05 23:40:32 -0700365 HWCLayerVersion0(hwc_layer_t* layer)
366 : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700367
368 virtual int32_t getCompositionType() const {
369 return getLayer()->compositionType;
370 }
371 virtual uint32_t getHints() const {
372 return getLayer()->hints;
373 }
Jesse Hallef194142012-06-14 14:45:17 -0700374 virtual int getAndResetReleaseFenceFd() {
375 // not supported on VERSION_03
376 return -1;
377 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700378 virtual void setAcquireFenceFd(int fenceFd) {
379 if (fenceFd != -1) {
380 ALOGE("HWC 0.x can't handle acquire fences");
381 close(fenceFd);
382 }
383 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700384
385 virtual void setDefaultState() {
386 getLayer()->compositionType = HWC_FRAMEBUFFER;
387 getLayer()->hints = 0;
388 getLayer()->flags = HWC_SKIP_LAYER;
389 getLayer()->transform = 0;
390 getLayer()->blending = HWC_BLENDING_NONE;
391 getLayer()->visibleRegionScreen.numRects = 0;
392 getLayer()->visibleRegionScreen.rects = NULL;
393 }
394 virtual void setSkip(bool skip) {
395 if (skip) {
396 getLayer()->flags |= HWC_SKIP_LAYER;
397 } else {
398 getLayer()->flags &= ~HWC_SKIP_LAYER;
399 }
400 }
401 virtual void setBlending(uint32_t blending) {
402 getLayer()->blending = blending;
403 }
404 virtual void setTransform(uint32_t transform) {
405 getLayer()->transform = transform;
406 }
407 virtual void setFrame(const Rect& frame) {
408 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
409 }
410 virtual void setCrop(const Rect& crop) {
411 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
412 }
413 virtual void setVisibleRegionScreen(const Region& reg) {
414 getLayer()->visibleRegionScreen.rects =
415 reinterpret_cast<hwc_rect_t const *>(
416 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
417 }
418 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
419 if (buffer == 0 || buffer->handle == 0) {
420 getLayer()->compositionType = HWC_FRAMEBUFFER;
421 getLayer()->flags |= HWC_SKIP_LAYER;
422 getLayer()->handle = 0;
423 } else {
424 getLayer()->handle = buffer->handle;
425 }
426 }
427};
Jesse Hall5880cc52012-06-05 23:40:32 -0700428// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
429
430/*
431 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
432 * This implements the HWCLayer side of HWCIterableLayer.
433 */
434class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
435public:
436 HWCLayerVersion1(hwc_layer_1_t* layer)
437 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
438
439 virtual int32_t getCompositionType() const {
440 return getLayer()->compositionType;
441 }
442 virtual uint32_t getHints() const {
443 return getLayer()->hints;
444 }
Jesse Hallef194142012-06-14 14:45:17 -0700445 virtual int getAndResetReleaseFenceFd() {
446 int fd = getLayer()->releaseFenceFd;
447 getLayer()->releaseFenceFd = -1;
448 return fd;
449 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700450 virtual void setAcquireFenceFd(int fenceFd) {
451 getLayer()->acquireFenceFd = fenceFd;
452 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700453
454 virtual void setDefaultState() {
455 getLayer()->compositionType = HWC_FRAMEBUFFER;
456 getLayer()->hints = 0;
457 getLayer()->flags = HWC_SKIP_LAYER;
458 getLayer()->transform = 0;
459 getLayer()->blending = HWC_BLENDING_NONE;
460 getLayer()->visibleRegionScreen.numRects = 0;
461 getLayer()->visibleRegionScreen.rects = NULL;
462 getLayer()->acquireFenceFd = -1;
463 getLayer()->releaseFenceFd = -1;
464 }
465 virtual void setSkip(bool skip) {
466 if (skip) {
467 getLayer()->flags |= HWC_SKIP_LAYER;
468 } else {
469 getLayer()->flags &= ~HWC_SKIP_LAYER;
470 }
471 }
472 virtual void setBlending(uint32_t blending) {
473 getLayer()->blending = blending;
474 }
475 virtual void setTransform(uint32_t transform) {
476 getLayer()->transform = transform;
477 }
478 virtual void setFrame(const Rect& frame) {
479 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
480 }
481 virtual void setCrop(const Rect& crop) {
482 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
483 }
484 virtual void setVisibleRegionScreen(const Region& reg) {
485 getLayer()->visibleRegionScreen.rects =
486 reinterpret_cast<hwc_rect_t const *>(
487 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
488 }
489 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
490 if (buffer == 0 || buffer->handle == 0) {
491 getLayer()->compositionType = HWC_FRAMEBUFFER;
492 getLayer()->flags |= HWC_SKIP_LAYER;
493 getLayer()->handle = 0;
494 } else {
495 getLayer()->handle = buffer->handle;
496 }
497 }
498};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700499
500/*
501 * returns an iterator initialized at a given index in the layer list
502 */
503HWComposer::LayerListIterator HWComposer::getLayerIterator(size_t index) {
504 if (!mList || index > mList->numHwLayers) {
505 return LayerListIterator();
506 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700507 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
508 return LayerListIterator(new HWCLayerVersion1(mList->hwLayers), index);
509 } else {
510 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
511 return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
512 }
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 */
518HWComposer::LayerListIterator HWComposer::begin() {
519 return getLayerIterator(0);
520}
521
522/*
523 * returns an iterator on the end of the layer list
524 */
525HWComposer::LayerListIterator HWComposer::end() {
526 return getLayerIterator(getNumLayers());
527}
528
529
530
Mathias Agopian22da60c2011-09-09 00:49:11 -0700531void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
532 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700533 if (mHwc && mList) {
534 result.append("Hardware Composer state:\n");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700535 result.appendFormat(" mDebugForceFakeVSync=%d\n",
536 mDebugForceFakeVSync);
537 result.appendFormat(" numHwLayers=%u, flags=%08x\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700538 mList->numHwLayers, mList->flags);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700539 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700540 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
541 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
542 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700543 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700544 hwc_layer_1_t l;
545 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
546 l = mList->hwLayers[i];
547 } else {
548 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
549 *(hwc_layer_t*)&l = list0->hwLayers[i];
550 l.acquireFenceFd = l.releaseFenceFd = -1;
551 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700552 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
553 int32_t format = -1;
554 if (layer->getLayer() != NULL) {
555 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
556 if (buffer != NULL) {
557 format = buffer->getPixelFormat();
558 }
559 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700560 result.appendFormat(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700561 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700562 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700563 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700564 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700565 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700566 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700567 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800568 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700569 if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) {
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800570 mHwc->dump(mHwc, buffer, SIZE);
571 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700572 }
573}
574
Mathias Agopiana350ff92010-08-10 17:14:02 -0700575// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700576
577HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
578 : mHwc(hwc), mEnabled(false),
579 mNextFakeVSync(0),
580 mRefreshPeriod(hwc.mRefreshPeriod)
581{
582}
583
584void HWComposer::VSyncThread::setEnabled(bool enabled) {
585 Mutex::Autolock _l(mLock);
586 mEnabled = enabled;
587 mCondition.signal();
588}
589
590void HWComposer::VSyncThread::onFirstRef() {
591 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
592}
593
594bool HWComposer::VSyncThread::threadLoop() {
595 { // scope for lock
596 Mutex::Autolock _l(mLock);
597 while (!mEnabled) {
598 mCondition.wait(mLock);
599 }
600 }
601
602 const nsecs_t period = mRefreshPeriod;
603 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
604 nsecs_t next_vsync = mNextFakeVSync;
605 nsecs_t sleep = next_vsync - now;
606 if (sleep < 0) {
607 // we missed, find where the next vsync should be
608 sleep = (period - ((now - next_vsync) % period));
609 next_vsync = now + sleep;
610 }
611 mNextFakeVSync = next_vsync + period;
612
613 struct timespec spec;
614 spec.tv_sec = next_vsync / 1000000000;
615 spec.tv_nsec = next_vsync % 1000000000;
616
617 int err;
618 do {
619 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
620 } while (err<0 && errno == EINTR);
621
622 if (err == 0) {
623 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
624 }
625
626 return true;
627}
628
629// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700630}; // namespace android