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