HWComposer.cpp revision e60b0687c8d49871d0c8786cabe6851f7a7783b5
1/*
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
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19// Uncomment this to remove support for HWC_DEVICE_API_VERSION_0_3 and older
20// #define HWC_REMOVE_DEPRECATED_VERSIONS 1
21
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/types.h>
27
28#include <utils/Errors.h>
29#include <utils/String8.h>
30#include <utils/Thread.h>
31#include <utils/Trace.h>
32#include <utils/Vector.h>
33
34#include <ui/GraphicBuffer.h>
35
36#include <hardware/hardware.h>
37#include <hardware/hwcomposer.h>
38
39#include <cutils/log.h>
40#include <cutils/properties.h>
41
42#include "Layer.h"           // needed only for debugging
43#include "LayerBase.h"
44#include "HWComposer.h"
45#include "SurfaceFlinger.h"
46
47namespace android {
48
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.
54
55#if HWC_REMOVE_DEPRECATED_VERSIONS
56// We need complete types to satisfy semantic checks, even though the code
57// paths that use these won't get executed at runtime (and will likely be dead-
58// code-eliminated). When we remove the code to support v0.3 we can remove
59// these as well.
60typedef hwc_layer_1_t hwc_layer_t;
61typedef hwc_display_contents_1_t hwc_layer_list_t;
62typedef hwc_composer_device_1_t hwc_composer_device_t;
63#endif
64
65// This function assumes we've already rejected HWC's with lower-than-required
66// versions. Don't use it for the initial "does HWC meet requirements" check!
67static bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
68    if (HWC_REMOVE_DEPRECATED_VERSIONS &&
69            version <= HWC_DEVICE_API_VERSION_1_0) {
70        return true;
71    } else {
72        return hwc->common.version >= version;
73    }
74}
75
76static bool hwcHasVsyncEvent(const hwc_composer_device_1_t* hwc) {
77    return hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_0_3);
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_display_contents_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
89static int hwcEventControl(hwc_composer_device_1_t* hwc, int dpy,
90        int event, int enabled) {
91    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
92        return hwc->eventControl(hwc, dpy, event, enabled);
93    } else {
94        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
95        return hwc0->methods->eventControl(hwc0, event, enabled);
96    }
97}
98
99static int hwcBlank(hwc_composer_device_1_t* hwc, int dpy, int blank) {
100    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
101        return hwc->blank(hwc, dpy, blank);
102    } else {
103        if (blank) {
104            hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
105            return hwc0->set(hwc0, NULL, NULL, NULL);
106        } else {
107            // HWC 0.x turns the screen on at the next set()
108            return NO_ERROR;
109        }
110    }
111}
112
113static int hwcPrepare(hwc_composer_device_1_t* hwc,
114        size_t numDisplays, hwc_display_contents_1_t** displays) {
115    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
116        return hwc->prepare(hwc, numDisplays, displays);
117    } else {
118        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
119        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
120        // In the past, SurfaceFlinger would pass a NULL list when doing full
121        // OpenGL ES composition. I don't know what, if any, dependencies there
122        // are on this behavior, so I'm playing it safe and preserving it.
123        if (list0->numHwLayers == 0)
124            return hwc0->prepare(hwc0, NULL);
125        else
126            return hwc0->prepare(hwc0, list0);
127    }
128}
129
130static int hwcSet(hwc_composer_device_1_t* hwc, EGLDisplay dpy, EGLSurface sur,
131        size_t numDisplays, hwc_display_contents_1_t** displays) {
132    int err;
133    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
134        displays[0]->dpy = dpy;
135        displays[0]->sur = sur;
136        err = hwc->set(hwc, numDisplays, displays);
137    } else {
138        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
139        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
140        err = hwc0->set(hwc0, dpy, sur, list0);
141    }
142    return err;
143}
144
145static uint32_t& hwcFlags(hwc_composer_device_1_t* hwc,
146        hwc_display_contents_1_t* display) {
147    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
148        return display->flags;
149    } else {
150        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
151        return list0->flags;
152    }
153}
154
155static size_t& hwcNumHwLayers(hwc_composer_device_1_t* hwc,
156        hwc_display_contents_1_t* display) {
157    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
158        return display->numHwLayers;
159    } else {
160        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
161        return list0->numHwLayers;
162    }
163}
164
165static void hwcDump(hwc_composer_device_1_t* hwc, char* buff, int buff_len) {
166    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
167        if (hwc->dump)
168            hwc->dump(hwc, buff, buff_len);
169    } else if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_0_1)) {
170        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
171        if (hwc0->dump)
172            hwc0->dump(hwc0, buff, buff_len);
173    }
174}
175
176// ---------------------------------------------------------------------------
177
178struct HWComposer::cb_context {
179    struct callbacks : public hwc_procs_t {
180        // these are here to facilitate the transition when adding
181        // new callbacks (an implementation can check for NULL before
182        // calling a new callback).
183        void (*zero[4])(void);
184    };
185    callbacks procs;
186    HWComposer* hwc;
187};
188
189// ---------------------------------------------------------------------------
190
191HWComposer::HWComposer(
192        const sp<SurfaceFlinger>& flinger,
193        EventHandler& handler,
194        framebuffer_device_t const* fbDev)
195    : mFlinger(flinger),
196      mModule(0), mHwc(0), mNumDisplays(1), mCapacity(0),
197      mCBContext(new cb_context),
198      mEventHandler(handler),
199      mVSyncCount(0), mDebugForceFakeVSync(false)
200{
201    for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
202        mLists[i] = 0;
203    }
204
205    char value[PROPERTY_VALUE_MAX];
206    property_get("debug.sf.no_hw_vsync", value, "0");
207    mDebugForceFakeVSync = atoi(value);
208
209    bool needVSyncThread = true;
210    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
211    ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
212    if (err == 0) {
213        err = hwc_open_1(mModule, &mHwc);
214        ALOGE_IF(err, "%s device failed to initialize (%s)",
215                HWC_HARDWARE_COMPOSER, strerror(-err));
216        if (err == 0) {
217            if (HWC_REMOVE_DEPRECATED_VERSIONS &&
218                    mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
219                ALOGE("%s device version %#x too old, will not be used",
220                        HWC_HARDWARE_COMPOSER, mHwc->common.version);
221                hwc_close_1(mHwc);
222                mHwc = NULL;
223            }
224        }
225
226        if (mHwc) {
227            if (mHwc->registerProcs) {
228                mCBContext->hwc = this;
229                mCBContext->procs.invalidate = &hook_invalidate;
230                mCBContext->procs.vsync = &hook_vsync;
231                memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
232                mHwc->registerProcs(mHwc, &mCBContext->procs);
233            }
234
235            // these IDs are always reserved
236            mTokens.markBit(HWC_DISPLAY_PRIMARY);
237            mTokens.markBit(HWC_DISPLAY_EXTERNAL);
238
239            // always turn vsync off when we start
240            needVSyncThread = false;
241            if (hwcHasVsyncEvent(mHwc)) {
242                hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
243
244                int period;
245                if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
246                    mDisplayData[0].refresh = nsecs_t(period);
247                }
248            } else {
249                needVSyncThread = true;
250            }
251
252            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
253                mNumDisplays = HWC_NUM_DISPLAY_TYPES;
254
255            // create initial empty display contents for display 0
256            createWorkList(HWC_DISPLAY_PRIMARY, 0);
257        }
258    }
259
260
261    if (fbDev) {
262        if (mDisplayData[HWC_DISPLAY_PRIMARY].refresh == 0) {
263            mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(1e9 / fbDev->fps);
264            ALOGW("getting VSYNC period from fb HAL: %lld", mDisplayData[0].refresh);
265        }
266        mDisplayData[HWC_DISPLAY_PRIMARY].xdpi = fbDev->xdpi;
267        mDisplayData[HWC_DISPLAY_PRIMARY].ydpi = fbDev->ydpi;
268    }
269
270    if (mDisplayData[HWC_DISPLAY_PRIMARY].refresh == 0) {
271        mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(1e9 / 60.0);
272        ALOGW("getting VSYNC period thin air: %lld", mDisplayData[0].refresh);
273    }
274
275    if (needVSyncThread) {
276        // we don't have VSYNC support, we need to fake it
277        mVSyncThread = new VSyncThread(*this);
278    }
279}
280
281HWComposer::~HWComposer() {
282    hwcEventControl(mHwc, 0, EVENT_VSYNC, 0);
283    for (size_t i = 0; i < MAX_DISPLAYS; i++) {
284        free(mLists[i]);
285    }
286    if (mVSyncThread != NULL) {
287        mVSyncThread->requestExitAndWait();
288    }
289    if (mHwc) {
290        hwc_close_1(mHwc);
291    }
292    delete mCBContext;
293}
294
295status_t HWComposer::initCheck() const {
296    return mHwc ? NO_ERROR : NO_INIT;
297}
298
299void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
300    cb_context* ctx = reinterpret_cast<cb_context*>(
301            const_cast<hwc_procs_t*>(procs));
302    ctx->hwc->invalidate();
303}
304
305void HWComposer::hook_vsync(const struct hwc_procs* procs, int dpy,
306        int64_t timestamp) {
307    cb_context* ctx = reinterpret_cast<cb_context*>(
308            const_cast<hwc_procs_t*>(procs));
309    ctx->hwc->vsync(dpy, timestamp);
310}
311
312void HWComposer::invalidate() {
313    mFlinger->repaintEverything();
314}
315
316void HWComposer::vsync(int dpy, int64_t timestamp) {
317    ATRACE_INT("VSYNC", ++mVSyncCount&1);
318    mEventHandler.onVSyncReceived(dpy, timestamp);
319    Mutex::Autolock _l(mLock);
320    mLastHwVSync = timestamp;
321}
322
323int32_t HWComposer::allocateDisplayId() {
324    if (mTokens.isFull()) {
325        return NO_MEMORY;
326    }
327
328    // FIXME: for now we don't support h/w composition wifi displays
329    return -1;
330
331    int32_t id = mTokens.firstUnmarkedBit();
332    mTokens.markBit(id);
333    return id;
334}
335
336status_t HWComposer::freeDisplayId(int32_t id) {
337    if (id < MAX_DISPLAYS) {
338        return BAD_VALUE;
339    }
340    if (!mTokens.hasBit(id)) {
341        return BAD_INDEX;
342    }
343    mTokens.clearBit(id);
344    return NO_ERROR;
345}
346
347nsecs_t HWComposer::getRefreshPeriod() const {
348    return mDisplayData[0].refresh;
349}
350
351nsecs_t HWComposer::getRefreshTimestamp() const {
352    // this returns the last refresh timestamp.
353    // if the last one is not available, we estimate it based on
354    // the refresh period and whatever closest timestamp we have.
355    Mutex::Autolock _l(mLock);
356    nsecs_t now = systemTime(CLOCK_MONOTONIC);
357    return now - ((now - mLastHwVSync) %  mDisplayData[0].refresh);
358}
359
360float HWComposer::getDpiX() const {
361    return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi;
362}
363
364float HWComposer::getDpiY() const {
365    return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi;
366}
367
368void HWComposer::eventControl(int event, int enabled) {
369    status_t err = NO_ERROR;
370    if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
371        if (!mDebugForceFakeVSync) {
372            err = hwcEventControl(mHwc, 0, event, enabled);
373            // error here should not happen -- not sure what we should
374            // do if it does.
375            ALOGE_IF(err, "eventControl(%d, %d) failed %s",
376                    event, enabled, strerror(-err));
377        }
378    }
379
380    if (err == NO_ERROR && mVSyncThread != NULL) {
381        mVSyncThread->setEnabled(enabled);
382    }
383}
384
385status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
386    if (!mTokens.hasBit(id)) {
387        return BAD_INDEX;
388    }
389
390    // FIXME: handle multiple displays
391    if (mHwc) {
392        // TODO: must handle multiple displays here
393        // mLists[0] is NULL only when this is called from the constructor
394        if (!mLists[0] || mCapacity < numLayers) {
395            free(mLists[0]);
396            size_t size = sizeofHwcLayerList(mHwc, numLayers);
397            mLists[0] = (hwc_display_contents_1_t*)malloc(size);
398            mCapacity = numLayers;
399        }
400        hwcFlags(mHwc, mLists[0]) = HWC_GEOMETRY_CHANGED;
401        hwcNumHwLayers(mHwc, mLists[0]) = numLayers;
402        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
403            mLists[0]->flipFenceFd = -1;
404        }
405    }
406    return NO_ERROR;
407}
408
409status_t HWComposer::prepare() {
410    int err = hwcPrepare(mHwc, mNumDisplays,
411            const_cast<hwc_display_contents_1_t**>(mLists));
412
413    if (err == NO_ERROR) {
414
415        // here we're just making sure that "skip" layers are set
416        // to HWC_FRAMEBUFFER and we're also counting how many layers
417        // we have of each type.
418        // It would be nice if we could get rid of this entirely, which I
419        // think is almost possible.
420
421        // TODO: must handle multiple displays here
422        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
423            size_t count = getNumLayers(0);
424            struct hwc_display_contents_1* disp = mLists[0];
425            mDisplayData[0].hasFbComp = false;
426            mDisplayData[0].hasOvComp = false;
427            for (size_t i=0 ; i<count ; i++) {
428                hwc_layer_1_t* l = &disp->hwLayers[i];
429                if (l->flags & HWC_SKIP_LAYER) {
430                    l->compositionType = HWC_FRAMEBUFFER;
431                }
432                if (l->compositionType == HWC_FRAMEBUFFER)
433                    mDisplayData[HWC_DISPLAY_PRIMARY].hasFbComp = true;
434                if (l->compositionType == HWC_OVERLAY)
435                    mDisplayData[HWC_DISPLAY_PRIMARY].hasOvComp = true;
436            }
437        } else {
438            size_t count = getNumLayers(0);
439            hwc_layer_list_t* disp = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
440            mDisplayData[0].hasFbComp = false;
441            mDisplayData[0].hasOvComp = false;
442            for (size_t i=0 ; i<count ; i++) {
443                hwc_layer_t* l = &disp->hwLayers[i];
444                if (l->flags & HWC_SKIP_LAYER) {
445                    l->compositionType = HWC_FRAMEBUFFER;
446                }
447                if (l->compositionType == HWC_FRAMEBUFFER)
448                    mDisplayData[HWC_DISPLAY_PRIMARY].hasFbComp = true;
449                if (l->compositionType == HWC_OVERLAY)
450                    mDisplayData[HWC_DISPLAY_PRIMARY].hasOvComp = true;
451            }
452        }
453    }
454    return (status_t)err;
455}
456
457bool HWComposer::hasHwcComposition(int32_t id) const {
458    if (!mTokens.hasBit(id))
459        return false;
460    return mDisplayData[id].hasOvComp;
461}
462
463bool HWComposer::hasGlesComposition(int32_t id) const {
464    if (!mTokens.hasBit(id))
465        return false;
466    return mDisplayData[id].hasFbComp;
467}
468
469status_t HWComposer::commit() const {
470    int err = NO_ERROR;
471    if (mHwc) {
472        void* fbDisplay = eglGetCurrentDisplay();
473        void* fbSurface = eglGetCurrentSurface(EGL_DRAW);
474        err = hwcSet(mHwc, fbDisplay, fbSurface, mNumDisplays,
475                const_cast<hwc_display_contents_1_t**>(mLists));
476        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
477            if (mLists[0]->flipFenceFd != -1) {
478                close(mLists[0]->flipFenceFd);
479                mLists[0]->flipFenceFd = -1;
480            }
481        }
482        hwcFlags(mHwc, mLists[0]) &= ~HWC_GEOMETRY_CHANGED;
483    }
484    return (status_t)err;
485}
486
487status_t HWComposer::release() const {
488    if (mHwc) {
489        if (hwcHasVsyncEvent(mHwc)) {
490            hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
491        }
492        return (status_t)hwcBlank(mHwc, 0, 1);
493    }
494    return NO_ERROR;
495}
496
497status_t HWComposer::acquire() const {
498    if (mHwc) {
499        return (status_t)hwcBlank(mHwc, 0, 0);
500    }
501    return NO_ERROR;
502}
503
504status_t HWComposer::disable() {
505    if (mHwc) {
506        hwcNumHwLayers(mHwc, mLists[0]) = 0;
507        int err = hwcPrepare(mHwc, mNumDisplays, mLists);
508        return (status_t)err;
509    }
510    return NO_ERROR;
511}
512
513size_t HWComposer::getNumLayers(int32_t id) const { // FIXME: handle multiple displays
514    return mHwc ? hwcNumHwLayers(mHwc, mLists[0]) : 0;
515}
516
517/*
518 * Helper template to implement a concrete HWCLayer
519 * This holds the pointer to the concrete hwc layer type
520 * and implements the "iterable" side of HWCLayer.
521 */
522template<typename CONCRETE, typename HWCTYPE>
523class Iterable : public HWComposer::HWCLayer {
524protected:
525    HWCTYPE* const mLayerList;
526    HWCTYPE* mCurrentLayer;
527    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
528    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
529    inline HWCTYPE* getLayer() { return mCurrentLayer; }
530    virtual ~Iterable() { }
531private:
532    // returns a copy of ourselves
533    virtual HWComposer::HWCLayer* dup() {
534        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
535    }
536    virtual status_t setLayer(size_t index) {
537        mCurrentLayer = &mLayerList[index];
538        return NO_ERROR;
539    }
540};
541
542// #if !HWC_REMOVE_DEPRECATED_VERSIONS
543/*
544 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
545 * This implements the HWCLayer side of HWCIterableLayer.
546 */
547class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
548public:
549    HWCLayerVersion0(hwc_layer_t* layer)
550        : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
551
552    virtual int32_t getCompositionType() const {
553        return getLayer()->compositionType;
554    }
555    virtual uint32_t getHints() const {
556        return getLayer()->hints;
557    }
558    virtual int getAndResetReleaseFenceFd() {
559        // not supported on VERSION_03
560        return -1;
561    }
562    virtual void setAcquireFenceFd(int fenceFd) {
563        if (fenceFd != -1) {
564            ALOGE("HWC 0.x can't handle acquire fences");
565            close(fenceFd);
566        }
567    }
568
569    virtual void setDefaultState() {
570        getLayer()->compositionType = HWC_FRAMEBUFFER;
571        getLayer()->hints = 0;
572        getLayer()->flags = HWC_SKIP_LAYER;
573        getLayer()->transform = 0;
574        getLayer()->blending = HWC_BLENDING_NONE;
575        getLayer()->visibleRegionScreen.numRects = 0;
576        getLayer()->visibleRegionScreen.rects = NULL;
577    }
578    virtual void setSkip(bool skip) {
579        if (skip) {
580            getLayer()->flags |= HWC_SKIP_LAYER;
581        } else {
582            getLayer()->flags &= ~HWC_SKIP_LAYER;
583        }
584    }
585    virtual void setBlending(uint32_t blending) {
586        getLayer()->blending = blending;
587    }
588    virtual void setTransform(uint32_t transform) {
589        getLayer()->transform = transform;
590    }
591    virtual void setFrame(const Rect& frame) {
592        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
593    }
594    virtual void setCrop(const Rect& crop) {
595        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
596    }
597    virtual void setVisibleRegionScreen(const Region& reg) {
598        getLayer()->visibleRegionScreen.rects =
599                reinterpret_cast<hwc_rect_t const *>(
600                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
601    }
602    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
603        if (buffer == 0 || buffer->handle == 0) {
604            getLayer()->compositionType = HWC_FRAMEBUFFER;
605            getLayer()->flags |= HWC_SKIP_LAYER;
606            getLayer()->handle = 0;
607        } else {
608            getLayer()->handle = buffer->handle;
609        }
610    }
611};
612// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
613
614/*
615 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
616 * This implements the HWCLayer side of HWCIterableLayer.
617 */
618class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
619public:
620    HWCLayerVersion1(hwc_layer_1_t* layer)
621        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
622
623    virtual int32_t getCompositionType() const {
624        return getLayer()->compositionType;
625    }
626    virtual uint32_t getHints() const {
627        return getLayer()->hints;
628    }
629    virtual int getAndResetReleaseFenceFd() {
630        int fd = getLayer()->releaseFenceFd;
631        getLayer()->releaseFenceFd = -1;
632        return fd;
633    }
634    virtual void setAcquireFenceFd(int fenceFd) {
635        getLayer()->acquireFenceFd = fenceFd;
636    }
637
638    virtual void setDefaultState() {
639        getLayer()->compositionType = HWC_FRAMEBUFFER;
640        getLayer()->hints = 0;
641        getLayer()->flags = HWC_SKIP_LAYER;
642        getLayer()->transform = 0;
643        getLayer()->blending = HWC_BLENDING_NONE;
644        getLayer()->visibleRegionScreen.numRects = 0;
645        getLayer()->visibleRegionScreen.rects = NULL;
646        getLayer()->acquireFenceFd = -1;
647        getLayer()->releaseFenceFd = -1;
648    }
649    virtual void setSkip(bool skip) {
650        if (skip) {
651            getLayer()->flags |= HWC_SKIP_LAYER;
652        } else {
653            getLayer()->flags &= ~HWC_SKIP_LAYER;
654        }
655    }
656    virtual void setBlending(uint32_t blending) {
657        getLayer()->blending = blending;
658    }
659    virtual void setTransform(uint32_t transform) {
660        getLayer()->transform = transform;
661    }
662    virtual void setFrame(const Rect& frame) {
663        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
664    }
665    virtual void setCrop(const Rect& crop) {
666        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
667    }
668    virtual void setVisibleRegionScreen(const Region& reg) {
669        getLayer()->visibleRegionScreen.rects =
670                reinterpret_cast<hwc_rect_t const *>(
671                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
672    }
673    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
674        if (buffer == 0 || buffer->handle == 0) {
675            getLayer()->compositionType = HWC_FRAMEBUFFER;
676            getLayer()->flags |= HWC_SKIP_LAYER;
677            getLayer()->handle = 0;
678        } else {
679            getLayer()->handle = buffer->handle;
680        }
681    }
682};
683
684/*
685 * returns an iterator initialized at a given index in the layer list
686 */
687HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
688    if (!mTokens.hasBit(id))
689        return LayerListIterator();
690
691    // FIXME: handle multiple displays
692    if (!mHwc || index > hwcNumHwLayers(mHwc, mLists[0]))
693        return LayerListIterator();
694    if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
695        return LayerListIterator(new HWCLayerVersion1(mLists[0]->hwLayers),
696                index);
697    } else {
698        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
699        return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
700    }
701}
702
703/*
704 * returns an iterator on the beginning of the layer list
705 */
706HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
707    return getLayerIterator(id, 0);
708}
709
710/*
711 * returns an iterator on the end of the layer list
712 */
713HWComposer::LayerListIterator HWComposer::end(int32_t id) {
714    return getLayerIterator(id, getNumLayers(id));
715}
716
717void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
718        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
719    if (mHwc) {
720        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
721
722        result.append("Hardware Composer state:\n");
723        result.appendFormat("  mDebugForceFakeVSync=%d\n",
724                mDebugForceFakeVSync);
725        result.appendFormat("  numHwLayers=%u, flags=%08x\n",
726                hwcNumHwLayers(mHwc, mLists[0]), hwcFlags(mHwc, mLists[0]));
727        result.append(
728                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
729                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
730        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
731        for (size_t i=0 ; i<hwcNumHwLayers(mHwc, mLists[0]) ; i++) {
732            hwc_layer_1_t const* lp;
733            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
734                lp = &mLists[0]->hwLayers[i];
735            } else {
736                // FIXME: here we rely on hwc_layer_1_t and hwc_layer_t having the same layout
737                lp = reinterpret_cast<hwc_layer_1_t const*>(&list0->hwLayers[i]);
738            }
739            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
740            int32_t format = -1;
741            if (layer->getLayer() != NULL) {
742                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
743                if (buffer != NULL) {
744                    format = buffer->getPixelFormat();
745                }
746            }
747            const hwc_layer_1_t& l(*lp);
748            result.appendFormat(
749                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
750                    l.compositionType ? "OVERLAY" : "FB",
751                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
752                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
753                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
754                    layer->getName().string());
755        }
756    }
757    if (mHwc) {
758        hwcDump(mHwc, buffer, SIZE);
759        result.append(buffer);
760    }
761}
762
763// ---------------------------------------------------------------------------
764
765HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
766    : mHwc(hwc), mEnabled(false),
767      mNextFakeVSync(0),
768      mRefreshPeriod(hwc.getRefreshPeriod())
769{
770}
771
772void HWComposer::VSyncThread::setEnabled(bool enabled) {
773    Mutex::Autolock _l(mLock);
774    mEnabled = enabled;
775    mCondition.signal();
776}
777
778void HWComposer::VSyncThread::onFirstRef() {
779    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
780}
781
782bool HWComposer::VSyncThread::threadLoop() {
783    { // scope for lock
784        Mutex::Autolock _l(mLock);
785        while (!mEnabled) {
786            mCondition.wait(mLock);
787        }
788    }
789
790    const nsecs_t period = mRefreshPeriod;
791    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
792    nsecs_t next_vsync = mNextFakeVSync;
793    nsecs_t sleep = next_vsync - now;
794    if (sleep < 0) {
795        // we missed, find where the next vsync should be
796        sleep = (period - ((now - next_vsync) % period));
797        next_vsync = now + sleep;
798    }
799    mNextFakeVSync = next_vsync + period;
800
801    struct timespec spec;
802    spec.tv_sec  = next_vsync / 1000000000;
803    spec.tv_nsec = next_vsync % 1000000000;
804
805    int err;
806    do {
807        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
808    } while (err<0 && errno == EINTR);
809
810    if (err == 0) {
811        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
812    }
813
814    return true;
815}
816
817// ---------------------------------------------------------------------------
818}; // namespace android
819