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