blob: 1742f1b2d2f0c88f46ebc6b9f2333dd04e8df170 [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 {
Mathias Agopian86303202012-07-24 22:46:10 -0700279 int err = NO_ERROR;
280 if (mHwc) {
281 err = mHwc->set(mHwc, mDpy, mSur, mList);
282 if (mList) {
283 mList->flags &= ~HWC_GEOMETRY_CHANGED;
284 }
285 } else {
286 eglSwapBuffers(mDpy, mSur);
Mathias Agopian58959342010-10-07 14:57:04 -0700287 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700288 return (status_t)err;
289}
290
Antti Hatalaf5f27122010-09-09 02:33:05 -0700291status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700292 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700293 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700294 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
295 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700296 int err = mHwc->set(mHwc, NULL, NULL, NULL);
Colin Cross10fbdb62012-07-12 17:56:34 -0700297 if (err < 0) {
298 return (status_t)err;
299 }
300
301 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
302 if (mHwc->methods && mHwc->methods->blank) {
303 err = mHwc->methods->blank(mHwc, 1);
304 }
305 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700306 return (status_t)err;
307 }
308 return NO_ERROR;
309}
310
Colin Cross10fbdb62012-07-12 17:56:34 -0700311status_t HWComposer::acquire() const {
312 if (mHwc) {
313 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
314 if (mHwc->methods && mHwc->methods->blank) {
315 int err = mHwc->methods->blank(mHwc, 0);
316 return (status_t)err;
317 }
318 }
319 }
320
321 return NO_ERROR;
322}
323
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700324status_t HWComposer::disable() {
325 if (mHwc) {
326 free(mList);
327 mList = NULL;
328 int err = mHwc->prepare(mHwc, NULL);
329 return (status_t)err;
330 }
331 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700332}
333
Mathias Agopian45721772010-08-12 15:03:26 -0700334size_t HWComposer::getNumLayers() const {
335 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700336}
337
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700338/*
339 * Helper template to implement a concrete HWCLayer
340 * This holds the pointer to the concrete hwc layer type
341 * and implements the "iterable" side of HWCLayer.
342 */
343template<typename CONCRETE, typename HWCTYPE>
344class Iterable : public HWComposer::HWCLayer {
345protected:
346 HWCTYPE* const mLayerList;
347 HWCTYPE* mCurrentLayer;
348 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
349 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
350 inline HWCTYPE* getLayer() { return mCurrentLayer; }
351 virtual ~Iterable() { }
352private:
353 // returns a copy of ourselves
354 virtual HWComposer::HWCLayer* dup() {
355 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
356 }
357 virtual status_t setLayer(size_t index) {
358 mCurrentLayer = &mLayerList[index];
359 return NO_ERROR;
360 }
361};
362
Jesse Hall5880cc52012-06-05 23:40:32 -0700363// #if !HWC_REMOVE_DEPRECATED_VERSIONS
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700364/*
365 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
366 * This implements the HWCLayer side of HWCIterableLayer.
367 */
Jesse Hall5880cc52012-06-05 23:40:32 -0700368class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700369public:
Jesse Hall5880cc52012-06-05 23:40:32 -0700370 HWCLayerVersion0(hwc_layer_t* layer)
371 : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700372
373 virtual int32_t getCompositionType() const {
374 return getLayer()->compositionType;
375 }
376 virtual uint32_t getHints() const {
377 return getLayer()->hints;
378 }
Jesse Hallef194142012-06-14 14:45:17 -0700379 virtual int getAndResetReleaseFenceFd() {
380 // not supported on VERSION_03
381 return -1;
382 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700383 virtual void setAcquireFenceFd(int fenceFd) {
384 if (fenceFd != -1) {
385 ALOGE("HWC 0.x can't handle acquire fences");
386 close(fenceFd);
387 }
388 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700389
390 virtual void setDefaultState() {
391 getLayer()->compositionType = HWC_FRAMEBUFFER;
392 getLayer()->hints = 0;
393 getLayer()->flags = HWC_SKIP_LAYER;
394 getLayer()->transform = 0;
395 getLayer()->blending = HWC_BLENDING_NONE;
396 getLayer()->visibleRegionScreen.numRects = 0;
397 getLayer()->visibleRegionScreen.rects = NULL;
398 }
399 virtual void setSkip(bool skip) {
400 if (skip) {
401 getLayer()->flags |= HWC_SKIP_LAYER;
402 } else {
403 getLayer()->flags &= ~HWC_SKIP_LAYER;
404 }
405 }
406 virtual void setBlending(uint32_t blending) {
407 getLayer()->blending = blending;
408 }
409 virtual void setTransform(uint32_t transform) {
410 getLayer()->transform = transform;
411 }
412 virtual void setFrame(const Rect& frame) {
413 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
414 }
415 virtual void setCrop(const Rect& crop) {
416 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
417 }
418 virtual void setVisibleRegionScreen(const Region& reg) {
419 getLayer()->visibleRegionScreen.rects =
420 reinterpret_cast<hwc_rect_t const *>(
421 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
422 }
423 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
424 if (buffer == 0 || buffer->handle == 0) {
425 getLayer()->compositionType = HWC_FRAMEBUFFER;
426 getLayer()->flags |= HWC_SKIP_LAYER;
427 getLayer()->handle = 0;
428 } else {
429 getLayer()->handle = buffer->handle;
430 }
431 }
432};
Jesse Hall5880cc52012-06-05 23:40:32 -0700433// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
434
435/*
436 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
437 * This implements the HWCLayer side of HWCIterableLayer.
438 */
439class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
440public:
441 HWCLayerVersion1(hwc_layer_1_t* layer)
442 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
443
444 virtual int32_t getCompositionType() const {
445 return getLayer()->compositionType;
446 }
447 virtual uint32_t getHints() const {
448 return getLayer()->hints;
449 }
Jesse Hallef194142012-06-14 14:45:17 -0700450 virtual int getAndResetReleaseFenceFd() {
451 int fd = getLayer()->releaseFenceFd;
452 getLayer()->releaseFenceFd = -1;
453 return fd;
454 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700455 virtual void setAcquireFenceFd(int fenceFd) {
456 getLayer()->acquireFenceFd = fenceFd;
457 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700458
459 virtual void setDefaultState() {
460 getLayer()->compositionType = HWC_FRAMEBUFFER;
461 getLayer()->hints = 0;
462 getLayer()->flags = HWC_SKIP_LAYER;
463 getLayer()->transform = 0;
464 getLayer()->blending = HWC_BLENDING_NONE;
465 getLayer()->visibleRegionScreen.numRects = 0;
466 getLayer()->visibleRegionScreen.rects = NULL;
467 getLayer()->acquireFenceFd = -1;
468 getLayer()->releaseFenceFd = -1;
469 }
470 virtual void setSkip(bool skip) {
471 if (skip) {
472 getLayer()->flags |= HWC_SKIP_LAYER;
473 } else {
474 getLayer()->flags &= ~HWC_SKIP_LAYER;
475 }
476 }
477 virtual void setBlending(uint32_t blending) {
478 getLayer()->blending = blending;
479 }
480 virtual void setTransform(uint32_t transform) {
481 getLayer()->transform = transform;
482 }
483 virtual void setFrame(const Rect& frame) {
484 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
485 }
486 virtual void setCrop(const Rect& crop) {
487 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
488 }
489 virtual void setVisibleRegionScreen(const Region& reg) {
490 getLayer()->visibleRegionScreen.rects =
491 reinterpret_cast<hwc_rect_t const *>(
492 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
493 }
494 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
495 if (buffer == 0 || buffer->handle == 0) {
496 getLayer()->compositionType = HWC_FRAMEBUFFER;
497 getLayer()->flags |= HWC_SKIP_LAYER;
498 getLayer()->handle = 0;
499 } else {
500 getLayer()->handle = buffer->handle;
501 }
502 }
503};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700504
505/*
506 * returns an iterator initialized at a given index in the layer list
507 */
508HWComposer::LayerListIterator HWComposer::getLayerIterator(size_t index) {
509 if (!mList || index > mList->numHwLayers) {
510 return LayerListIterator();
511 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700512 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
513 return LayerListIterator(new HWCLayerVersion1(mList->hwLayers), index);
514 } else {
515 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
516 return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
517 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700518}
519
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700520/*
521 * returns an iterator on the beginning of the layer list
522 */
523HWComposer::LayerListIterator HWComposer::begin() {
524 return getLayerIterator(0);
525}
526
527/*
528 * returns an iterator on the end of the layer list
529 */
530HWComposer::LayerListIterator HWComposer::end() {
531 return getLayerIterator(getNumLayers());
532}
533
534
535
Mathias Agopian22da60c2011-09-09 00:49:11 -0700536void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
537 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700538 if (mHwc && mList) {
539 result.append("Hardware Composer state:\n");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700540 result.appendFormat(" mDebugForceFakeVSync=%d\n",
541 mDebugForceFakeVSync);
542 result.appendFormat(" numHwLayers=%u, flags=%08x\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700543 mList->numHwLayers, mList->flags);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700544 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700545 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
546 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
547 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700548 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700549 hwc_layer_1_t l;
550 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
551 l = mList->hwLayers[i];
552 } else {
553 hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
554 *(hwc_layer_t*)&l = list0->hwLayers[i];
555 l.acquireFenceFd = l.releaseFenceFd = -1;
556 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700557 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
558 int32_t format = -1;
559 if (layer->getLayer() != NULL) {
560 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
561 if (buffer != NULL) {
562 format = buffer->getPixelFormat();
563 }
564 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700565 result.appendFormat(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700566 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700567 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700568 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700569 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700570 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700571 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700572 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800573 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700574 if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) {
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800575 mHwc->dump(mHwc, buffer, SIZE);
576 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700577 }
578}
579
Mathias Agopiana350ff92010-08-10 17:14:02 -0700580// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700581
582HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
583 : mHwc(hwc), mEnabled(false),
584 mNextFakeVSync(0),
585 mRefreshPeriod(hwc.mRefreshPeriod)
586{
587}
588
589void HWComposer::VSyncThread::setEnabled(bool enabled) {
590 Mutex::Autolock _l(mLock);
591 mEnabled = enabled;
592 mCondition.signal();
593}
594
595void HWComposer::VSyncThread::onFirstRef() {
596 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
597}
598
599bool HWComposer::VSyncThread::threadLoop() {
600 { // scope for lock
601 Mutex::Autolock _l(mLock);
602 while (!mEnabled) {
603 mCondition.wait(mLock);
604 }
605 }
606
607 const nsecs_t period = mRefreshPeriod;
608 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
609 nsecs_t next_vsync = mNextFakeVSync;
610 nsecs_t sleep = next_vsync - now;
611 if (sleep < 0) {
612 // we missed, find where the next vsync should be
613 sleep = (period - ((now - next_vsync) % period));
614 next_vsync = now + sleep;
615 }
616 mNextFakeVSync = next_vsync + period;
617
618 struct timespec spec;
619 spec.tv_sec = next_vsync / 1000000000;
620 spec.tv_nsec = next_vsync % 1000000000;
621
622 int err;
623 do {
624 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
625 } while (err<0 && errno == EINTR);
626
627 if (err == 0) {
628 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
629 }
630
631 return true;
632}
633
634// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700635}; // namespace android