Layer.cpp revision cbad735d8cc4ff360341bf12d8c388edcbc78ce3
1/*
2 * Copyright (C) 2007 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#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22#include <math.h>
23
24#include <cutils/compiler.h>
25#include <cutils/native_handle.h>
26#include <cutils/properties.h>
27
28#include <utils/Errors.h>
29#include <utils/Log.h>
30#include <utils/StopWatch.h>
31#include <utils/Trace.h>
32
33#include <ui/GraphicBuffer.h>
34#include <ui/PixelFormat.h>
35
36#include <gui/Surface.h>
37
38#include "clz.h"
39#include "DisplayHardware/DisplayHardware.h"
40#include "DisplayHardware/HWComposer.h"
41#include "GLExtensions.h"
42#include "Layer.h"
43#include "SurfaceFlinger.h"
44#include "SurfaceTextureLayer.h"
45
46#define DEBUG_RESIZE    0
47
48namespace android {
49
50// ---------------------------------------------------------------------------
51
52Layer::Layer(SurfaceFlinger* flinger,
53        DisplayID display, const sp<Client>& client)
54    :   LayerBaseClient(flinger, display, client),
55        mTextureName(-1U),
56        mQueuedFrames(0),
57        mCurrentTransform(0),
58        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
59        mCurrentOpacity(true),
60        mRefreshPending(false),
61        mFrameLatencyNeeded(false),
62        mFrameLatencyOffset(0),
63        mFormat(PIXEL_FORMAT_NONE),
64        mGLExtensions(GLExtensions::getInstance()),
65        mOpaqueLayer(true),
66        mNeedsDithering(false),
67        mSecure(false),
68        mProtectedByApp(false)
69{
70    mCurrentCrop.makeInvalid();
71    glGenTextures(1, &mTextureName);
72}
73
74void Layer::onLayerDisplayed() {
75    if (mFrameLatencyNeeded) {
76        const DisplayHardware& hw(graphicPlane(0).displayHardware());
77        mFrameStats[mFrameLatencyOffset].timestamp = mSurfaceTexture->getTimestamp();
78        mFrameStats[mFrameLatencyOffset].set = systemTime();
79        mFrameStats[mFrameLatencyOffset].vsync = hw.getRefreshTimestamp();
80        mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
81        mFrameLatencyNeeded = false;
82    }
83}
84
85void Layer::onFirstRef()
86{
87    LayerBaseClient::onFirstRef();
88
89    struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
90        FrameQueuedListener(Layer* layer) : mLayer(layer) { }
91    private:
92        wp<Layer> mLayer;
93        virtual void onFrameAvailable() {
94            sp<Layer> that(mLayer.promote());
95            if (that != 0) {
96                that->onFrameQueued();
97            }
98        }
99    };
100
101    // Creates a custom BufferQueue for SurfaceTexture to use
102    sp<BufferQueue> bq = new SurfaceTextureLayer();
103    mSurfaceTexture = new SurfaceTexture(mTextureName, true,
104            GL_TEXTURE_EXTERNAL_OES, false, bq);
105
106    mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
107    mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
108    mSurfaceTexture->setSynchronousMode(true);
109
110#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
111#warning "disabling triple buffering"
112    mSurfaceTexture->setBufferCountServer(2);
113#else
114    mSurfaceTexture->setBufferCountServer(3);
115#endif
116}
117
118Layer::~Layer()
119{
120    mFlinger->postMessageAsync(
121            new SurfaceFlinger::MessageDestroyGLTexture(mTextureName) );
122}
123
124void Layer::onFrameQueued() {
125    android_atomic_inc(&mQueuedFrames);
126    mFlinger->signalLayerUpdate();
127}
128
129// called with SurfaceFlinger::mStateLock as soon as the layer is entered
130// in the purgatory list
131void Layer::onRemoved()
132{
133    mSurfaceTexture->abandon();
134}
135
136void Layer::setName(const String8& name) {
137    LayerBase::setName(name);
138    mSurfaceTexture->setName(name);
139}
140
141void Layer::validateVisibility(const Transform& globalTransform) {
142    LayerBase::validateVisibility(globalTransform);
143
144    // This optimization allows the SurfaceTexture to bake in
145    // the rotation so hardware overlays can be used
146    mSurfaceTexture->setTransformHint(getTransformHint());
147}
148
149sp<ISurface> Layer::createSurface()
150{
151    class BSurface : public BnSurface, public LayerCleaner {
152        wp<const Layer> mOwner;
153        virtual sp<ISurfaceTexture> getSurfaceTexture() const {
154            sp<ISurfaceTexture> res;
155            sp<const Layer> that( mOwner.promote() );
156            if (that != NULL) {
157                res = that->mSurfaceTexture->getBufferQueue();
158            }
159            return res;
160        }
161    public:
162        BSurface(const sp<SurfaceFlinger>& flinger,
163                const sp<Layer>& layer)
164            : LayerCleaner(flinger, layer), mOwner(layer) { }
165    };
166    sp<ISurface> sur(new BSurface(mFlinger, this));
167    return sur;
168}
169
170wp<IBinder> Layer::getSurfaceTextureBinder() const
171{
172    return mSurfaceTexture->getBufferQueue()->asBinder();
173}
174
175status_t Layer::setBuffers( uint32_t w, uint32_t h,
176                            PixelFormat format, uint32_t flags)
177{
178    // this surfaces pixel format
179    PixelFormatInfo info;
180    status_t err = getPixelFormatInfo(format, &info);
181    if (err) {
182        ALOGE("unsupported pixelformat %d", format);
183        return err;
184    }
185
186    // the display's pixel format
187    const DisplayHardware& hw(graphicPlane(0).displayHardware());
188    uint32_t const maxSurfaceDims = min(
189            hw.getMaxTextureSize(), hw.getMaxViewportDims());
190
191    // never allow a surface larger than what our underlying GL implementation
192    // can handle.
193    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
194        ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
195        return BAD_VALUE;
196    }
197
198    PixelFormatInfo displayInfo;
199    getPixelFormatInfo(hw.getFormat(), &displayInfo);
200    const uint32_t hwFlags = hw.getFlags();
201
202    mFormat = format;
203
204    mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
205    mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
206    mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
207    mCurrentOpacity = getOpacityForFormat(format);
208
209    mSurfaceTexture->setDefaultBufferSize(w, h);
210    mSurfaceTexture->setDefaultBufferFormat(format);
211    mSurfaceTexture->setConsumerUsageBits(getEffectiveUsage(0));
212
213    // we use the red index
214    int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
215    int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
216    mNeedsDithering = layerRedsize > displayRedSize;
217
218    return NO_ERROR;
219}
220
221Rect Layer::computeBufferCrop() const {
222    // Start with the SurfaceTexture's buffer crop...
223    Rect crop;
224    if (!mCurrentCrop.isEmpty()) {
225        crop = mCurrentCrop;
226    } else  if (mActiveBuffer != NULL){
227        crop = Rect(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
228    } else {
229        crop = Rect(mTransformedBounds.width(), mTransformedBounds.height());
230    }
231
232    // ... then reduce that in the same proportions as the window crop reduces
233    // the window size.
234    const State& s(drawingState());
235    if (!s.crop.isEmpty()) {
236        // Transform the window crop to match the buffer coordinate system,
237        // which means using the inverse of the current transform set on the
238        // SurfaceTexture.
239        uint32_t invTransform = mCurrentTransform;
240        int winWidth = s.w;
241        int winHeight = s.h;
242        if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
243            invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
244                    NATIVE_WINDOW_TRANSFORM_FLIP_H;
245            winWidth = s.h;
246            winHeight = s.w;
247        }
248        Rect winCrop = s.crop.transform(invTransform, s.w, s.h);
249
250        float xScale = float(crop.width()) / float(winWidth);
251        float yScale = float(crop.height()) / float(winHeight);
252        crop.left += int(ceil(float(winCrop.left) * xScale));
253        crop.top += int(ceil(float(winCrop.top) * yScale));
254        crop.right -= int(ceil(float(winWidth - winCrop.right) * xScale));
255        crop.bottom -= int(ceil(float(winHeight - winCrop.bottom) * yScale));
256    }
257
258    return crop;
259}
260
261void Layer::setGeometry(hwc_layer_t* hwcl)
262{
263    LayerBaseClient::setGeometry(hwcl);
264
265    hwcl->flags &= ~HWC_SKIP_LAYER;
266
267    // we can't do alpha-fade with the hwc HAL
268    const State& s(drawingState());
269    if (s.alpha < 0xFF) {
270        hwcl->flags = HWC_SKIP_LAYER;
271    }
272
273    /*
274     * Transformations are applied in this order:
275     * 1) buffer orientation/flip/mirror
276     * 2) state transformation (window manager)
277     * 3) layer orientation (screen orientation)
278     * mTransform is already the composition of (2) and (3)
279     * (NOTE: the matrices are multiplied in reverse order)
280     */
281
282    const Transform bufferOrientation(mCurrentTransform);
283    const Transform tr(mTransform * bufferOrientation);
284
285    // this gives us only the "orientation" component of the transform
286    const uint32_t finalTransform = tr.getOrientation();
287
288    // we can only handle simple transformation
289    if (finalTransform & Transform::ROT_INVALID) {
290        hwcl->flags = HWC_SKIP_LAYER;
291    } else {
292        hwcl->transform = finalTransform;
293    }
294
295    Rect crop = computeBufferCrop();
296    hwcl->sourceCrop.left   = crop.left;
297    hwcl->sourceCrop.top    = crop.top;
298    hwcl->sourceCrop.right  = crop.right;
299    hwcl->sourceCrop.bottom = crop.bottom;
300}
301
302void Layer::setPerFrameData(hwc_layer_t* hwcl) {
303    const sp<GraphicBuffer>& buffer(mActiveBuffer);
304    if (buffer == NULL) {
305        // this can happen if the client never drew into this layer yet,
306        // or if we ran out of memory. In that case, don't let
307        // HWC handle it.
308        hwcl->flags |= HWC_SKIP_LAYER;
309        hwcl->handle = NULL;
310    } else {
311        hwcl->handle = buffer->handle;
312    }
313}
314
315void Layer::onDraw(const Region& clip) const
316{
317    ATRACE_CALL();
318
319    if (CC_UNLIKELY(mActiveBuffer == 0)) {
320        // the texture has not been created yet, this Layer has
321        // in fact never been drawn into. This happens frequently with
322        // SurfaceView because the WindowManager can't know when the client
323        // has drawn the first time.
324
325        // If there is nothing under us, we paint the screen in black, otherwise
326        // we just skip this update.
327
328        // figure out if there is something below us
329        Region under;
330        const SurfaceFlinger::LayerVector& drawingLayers(
331                mFlinger->mDrawingState.layersSortedByZ);
332        const size_t count = drawingLayers.size();
333        for (size_t i=0 ; i<count ; ++i) {
334            const sp<LayerBase>& layer(drawingLayers[i]);
335            if (layer.get() == static_cast<LayerBase const*>(this))
336                break;
337            under.orSelf(layer->visibleRegionScreen);
338        }
339        // if not everything below us is covered, we plug the holes!
340        Region holes(clip.subtract(under));
341        if (!holes.isEmpty()) {
342            clearWithOpenGL(holes, 0, 0, 0, 1);
343        }
344        return;
345    }
346
347    if (!isProtected()) {
348        // TODO: we could be more subtle with isFixedSize()
349        const bool useFiltering = getFiltering() || needsFiltering() || isFixedSize();
350
351        // Query the texture matrix given our current filtering mode.
352        float textureMatrix[16];
353        mSurfaceTexture->setFilteringEnabled(useFiltering);
354        mSurfaceTexture->getTransformMatrix(textureMatrix);
355
356        // Set things up for texturing.
357        glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
358        GLenum filter = GL_NEAREST;
359        if (useFiltering) {
360            filter = GL_LINEAR;
361        }
362        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
363        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
364        glMatrixMode(GL_TEXTURE);
365        glLoadMatrixf(textureMatrix);
366        glMatrixMode(GL_MODELVIEW);
367        glDisable(GL_TEXTURE_2D);
368        glEnable(GL_TEXTURE_EXTERNAL_OES);
369    } else {
370        glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
371        glMatrixMode(GL_TEXTURE);
372        glLoadIdentity();
373        glMatrixMode(GL_MODELVIEW);
374        glDisable(GL_TEXTURE_EXTERNAL_OES);
375        glEnable(GL_TEXTURE_2D);
376    }
377
378    drawWithOpenGL(clip);
379
380    glDisable(GL_TEXTURE_EXTERNAL_OES);
381    glDisable(GL_TEXTURE_2D);
382}
383
384// As documented in libhardware header, formats in the range
385// 0x100 - 0x1FF are specific to the HAL implementation, and
386// are known to have no alpha channel
387// TODO: move definition for device-specific range into
388// hardware.h, instead of using hard-coded values here.
389#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
390
391bool Layer::getOpacityForFormat(uint32_t format)
392{
393    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
394        return true;
395    }
396    PixelFormatInfo info;
397    status_t err = getPixelFormatInfo(PixelFormat(format), &info);
398    // in case of error (unknown format), we assume no blending
399    return (err || info.h_alpha <= info.l_alpha);
400}
401
402
403bool Layer::isOpaque() const
404{
405    // if we don't have a buffer yet, we're translucent regardless of the
406    // layer's opaque flag.
407    if (mActiveBuffer == 0) {
408        return false;
409    }
410
411    // if the layer has the opaque flag, then we're always opaque,
412    // otherwise we use the current buffer's format.
413    return mOpaqueLayer || mCurrentOpacity;
414}
415
416bool Layer::isProtected() const
417{
418    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
419    return (activeBuffer != 0) &&
420            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
421}
422
423uint32_t Layer::doTransaction(uint32_t flags)
424{
425    ATRACE_CALL();
426
427    const Layer::State& front(drawingState());
428    const Layer::State& temp(currentState());
429
430    const bool sizeChanged = (front.requested_w != temp.requested_w) ||
431            (front.requested_h != temp.requested_h);
432
433    if (sizeChanged) {
434        // the size changed, we need to ask our client to request a new buffer
435        ALOGD_IF(DEBUG_RESIZE,
436                "doTransaction: "
437                "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
438                "scalingMode=%d",
439                this,
440                int(temp.requested_w), int(temp.requested_h),
441                int(front.requested_w), int(front.requested_h),
442                mCurrentScalingMode);
443
444        if (!isFixedSize()) {
445            // this will make sure LayerBase::doTransaction doesn't update
446            // the drawing state's size
447            Layer::State& editDraw(mDrawingState);
448            editDraw.requested_w = temp.requested_w;
449            editDraw.requested_h = temp.requested_h;
450        }
451
452        // record the new size, form this point on, when the client request
453        // a buffer, it'll get the new size.
454        mSurfaceTexture->setDefaultBufferSize(temp.requested_w,
455                temp.requested_h);
456    }
457
458    return LayerBase::doTransaction(flags);
459}
460
461bool Layer::isFixedSize() const {
462    return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
463}
464
465bool Layer::isCropped() const {
466    return !mCurrentCrop.isEmpty();
467}
468
469// ----------------------------------------------------------------------------
470// pageflip handling...
471// ----------------------------------------------------------------------------
472
473bool Layer::onPreComposition() {
474    mRefreshPending = false;
475    return mQueuedFrames > 0;
476}
477
478void Layer::lockPageFlip(bool& recomputeVisibleRegions)
479{
480    ATRACE_CALL();
481
482    if (mQueuedFrames > 0) {
483
484        // if we've already called updateTexImage() without going through
485        // a composition step, we have to skip this layer at this point
486        // because we cannot call updateTeximage() without a corresponding
487        // compositionComplete() call.
488        // we'll trigger an update in onPreComposition().
489        if (mRefreshPending) {
490            mPostedDirtyRegion.clear();
491            return;
492        }
493        mRefreshPending = true;
494
495        // Capture the old state of the layer for comparisons later
496        const bool oldOpacity = isOpaque();
497        sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
498
499        // signal another event if we have more frames pending
500        if (android_atomic_dec(&mQueuedFrames) > 1) {
501            mFlinger->signalLayerUpdate();
502        }
503
504        if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
505            // something happened!
506            recomputeVisibleRegions = true;
507            return;
508        }
509
510        // update the active buffer
511        mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
512        mFrameLatencyNeeded = true;
513
514        if (oldActiveBuffer == NULL && mActiveBuffer != NULL) {
515            // the first time we receive a buffer, we need to trigger a
516            // geometry invalidation.
517            mFlinger->invalidateHwcGeometry();
518        }
519
520        Rect crop(mSurfaceTexture->getCurrentCrop());
521        const uint32_t transform(mSurfaceTexture->getCurrentTransform());
522        const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
523        if ((crop != mCurrentCrop) ||
524            (transform != mCurrentTransform) ||
525            (scalingMode != mCurrentScalingMode))
526        {
527            mCurrentCrop = crop;
528            mCurrentTransform = transform;
529            mCurrentScalingMode = scalingMode;
530            mFlinger->invalidateHwcGeometry();
531        }
532
533        uint32_t bufWidth  = mActiveBuffer->getWidth();
534        uint32_t bufHeight = mActiveBuffer->getHeight();
535        if (oldActiveBuffer != NULL) {
536            if (bufWidth != uint32_t(oldActiveBuffer->width) ||
537                bufHeight != uint32_t(oldActiveBuffer->height)) {
538                mFlinger->invalidateHwcGeometry();
539            }
540        }
541
542        mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
543        if (oldOpacity != isOpaque()) {
544            recomputeVisibleRegions = true;
545        }
546
547        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
548        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
549
550        // update the layer size if needed
551        const Layer::State& front(drawingState());
552
553        // FIXME: mPostedDirtyRegion = dirty & bounds
554        mPostedDirtyRegion.set(front.w, front.h);
555
556        if ((front.w != front.requested_w) ||
557            (front.h != front.requested_h))
558        {
559            // check that we received a buffer of the right size
560            // (Take the buffer's orientation into account)
561            if (mCurrentTransform & Transform::ROT_90) {
562                swap(bufWidth, bufHeight);
563            }
564
565            if (isFixedSize() ||
566                    (bufWidth == front.requested_w &&
567                    bufHeight == front.requested_h))
568            {
569                // Here we pretend the transaction happened by updating the
570                // current and drawing states. Drawing state is only accessed
571                // in this thread, no need to have it locked
572                Layer::State& editDraw(mDrawingState);
573                editDraw.w = editDraw.requested_w;
574                editDraw.h = editDraw.requested_h;
575
576                // We also need to update the current state so that we don't
577                // end-up doing too much work during the next transaction.
578                // NOTE: We actually don't need hold the transaction lock here
579                // because State::w and State::h are only accessed from
580                // this thread
581                Layer::State& editTemp(currentState());
582                editTemp.w = editDraw.w;
583                editTemp.h = editDraw.h;
584
585                // recompute visible region
586                recomputeVisibleRegions = true;
587            }
588
589            ALOGD_IF(DEBUG_RESIZE,
590                    "lockPageFlip : "
591                    "       (layer=%p), buffer (%ux%u, tr=%02x), "
592                    "requested (%dx%d)",
593                    this,
594                    bufWidth, bufHeight, mCurrentTransform,
595                    front.requested_w, front.requested_h);
596        }
597    }
598}
599
600void Layer::unlockPageFlip(
601        const Transform& planeTransform, Region& outDirtyRegion)
602{
603    ATRACE_CALL();
604
605    Region postedRegion(mPostedDirtyRegion);
606    if (!postedRegion.isEmpty()) {
607        mPostedDirtyRegion.clear();
608        if (!visibleRegionScreen.isEmpty()) {
609            // The dirty region is given in the layer's coordinate space
610            // transform the dirty region by the surface's transformation
611            // and the global transformation.
612            const Layer::State& s(drawingState());
613            const Transform tr(planeTransform * s.transform);
614            postedRegion = tr.transform(postedRegion);
615
616            // At this point, the dirty region is in screen space.
617            // Make sure it's constrained by the visible region (which
618            // is in screen space as well).
619            postedRegion.andSelf(visibleRegionScreen);
620            outDirtyRegion.orSelf(postedRegion);
621        }
622    }
623}
624
625void Layer::dump(String8& result, char* buffer, size_t SIZE) const
626{
627    LayerBaseClient::dump(result, buffer, SIZE);
628
629    sp<const GraphicBuffer> buf0(mActiveBuffer);
630    uint32_t w0=0, h0=0, s0=0, f0=0;
631    if (buf0 != 0) {
632        w0 = buf0->getWidth();
633        h0 = buf0->getHeight();
634        s0 = buf0->getStride();
635        f0 = buf0->format;
636    }
637    snprintf(buffer, SIZE,
638            "      "
639            "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
640            " transform-hint=0x%02x, queued-frames=%d, mRefreshPending=%d\n",
641            mFormat, w0, h0, s0,f0,
642            getTransformHint(), mQueuedFrames, mRefreshPending);
643
644    result.append(buffer);
645
646    if (mSurfaceTexture != 0) {
647        mSurfaceTexture->dump(result, "            ", buffer, SIZE);
648    }
649}
650
651void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
652{
653    LayerBaseClient::dumpStats(result, buffer, SIZE);
654    const size_t o = mFrameLatencyOffset;
655    const DisplayHardware& hw(graphicPlane(0).displayHardware());
656    const nsecs_t period = hw.getRefreshPeriod();
657    result.appendFormat("%lld\n", period);
658    for (size_t i=0 ; i<128 ; i++) {
659        const size_t index = (o+i) % 128;
660        const nsecs_t time_app   = mFrameStats[index].timestamp;
661        const nsecs_t time_set   = mFrameStats[index].set;
662        const nsecs_t time_vsync = mFrameStats[index].vsync;
663        result.appendFormat("%lld\t%lld\t%lld\n",
664                time_app,
665                time_vsync,
666                time_set);
667    }
668    result.append("\n");
669}
670
671void Layer::clearStats()
672{
673    LayerBaseClient::clearStats();
674    memset(mFrameStats, 0, sizeof(mFrameStats));
675}
676
677uint32_t Layer::getEffectiveUsage(uint32_t usage) const
678{
679    // TODO: should we do something special if mSecure is set?
680    if (mProtectedByApp) {
681        // need a hardware-protected path to external video sink
682        usage |= GraphicBuffer::USAGE_PROTECTED;
683    }
684    usage |= GraphicBuffer::USAGE_HW_COMPOSER;
685    return usage;
686}
687
688uint32_t Layer::getTransformHint() const {
689    uint32_t orientation = 0;
690    if (!mFlinger->mDebugDisableTransformHint) {
691        orientation = getPlaneOrientation();
692        if (orientation & Transform::ROT_INVALID) {
693            orientation = 0;
694        }
695    }
696    return orientation;
697}
698
699// ---------------------------------------------------------------------------
700
701
702}; // namespace android
703