Layer.cpp revision 45d845190d26ae27de45390460f50a6a8639e2e5
18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)/*
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * Copyright (C) 2007 The Android Open Source Project
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * you may not use this file except in compliance with the License.
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * You may obtain a copy of the License at
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) *
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * See the License for the specific language governing permissions and
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) * limitations under the License.
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) */
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <stdlib.h>
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <stdint.h>
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <sys/types.h>
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <cutils/compiler.h>
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <cutils/native_handle.h>
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <cutils/properties.h>
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <utils/Errors.h>
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <utils/Log.h>
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <utils/StopWatch.h>
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <ui/GraphicBuffer.h>
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <ui/PixelFormat.h>
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <surfaceflinger/Surface.h>
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "clz.h"
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "DisplayHardware/DisplayHardware.h"
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "DisplayHardware/HWComposer.h"
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "GLExtensions.h"
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "Layer.h"
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "SurfaceFlinger.h"
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "SurfaceTextureLayer.h"
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#define DEBUG_RESIZE    0
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace android {
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// ---------------------------------------------------------------------------
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)Layer::Layer(SurfaceFlinger* flinger,
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        DisplayID display, const sp<Client>& client)
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    :   LayerBaseClient(flinger, display, client),
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mTextureName(-1U),
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mQueuedFrames(0),
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mCurrentTransform(0),
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mCurrentOpacity(true),
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mFormat(PIXEL_FORMAT_NONE),
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mGLExtensions(GLExtensions::getInstance()),
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mOpaqueLayer(true),
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mNeedsDithering(false),
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mSecure(false),
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mProtectedByApp(false)
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mCurrentCrop.makeInvalid();
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    glGenTextures(1, &mTextureName);
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void Layer::onFirstRef()
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    LayerBaseClient::onFirstRef();
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        FrameQueuedListener(Layer* layer) : mLayer(layer) { }
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    private:
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        wp<Layer> mLayer;
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        virtual void onFrameAvailable() {
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            sp<Layer> that(mLayer.promote());
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            if (that != 0) {
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                that->onFrameQueued();
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            }
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        }
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    };
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSurfaceTexture->setSynchronousMode(true);
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSurfaceTexture->setBufferCountServer(2);
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)Layer::~Layer()
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    class MessageDestroyGLState : public MessageBase {
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        GLuint texture;
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    public:
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        MessageDestroyGLState(GLuint texture) : texture(texture) { }
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        virtual bool handler() {
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            glDeleteTextures(1, &texture);
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            return true;
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        }
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    };
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mFlinger->postMessageAsync( new MessageDestroyGLState(mTextureName) );
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void Layer::onFrameQueued() {
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    android_atomic_inc(&mQueuedFrames);
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mFlinger->signalEvent();
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// called with SurfaceFlinger::mStateLock as soon as the layer is entered
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// in the purgatory list
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void Layer::onRemoved()
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSurfaceTexture->abandon();
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)sp<ISurface> Layer::createSurface()
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    class BSurface : public BnSurface, public LayerCleaner {
1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        wp<const Layer> mOwner;
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        virtual sp<ISurfaceTexture> getSurfaceTexture() const {
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            sp<ISurfaceTexture> res;
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            sp<const Layer> that( mOwner.promote() );
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            if (that != NULL) {
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                res = that->mSurfaceTexture;
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            }
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            return res;
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        }
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    public:
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        BSurface(const sp<SurfaceFlinger>& flinger,
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                const sp<Layer>& layer)
1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            : LayerCleaner(flinger, layer), mOwner(layer) { }
1318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    };
1328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    sp<ISurface> sur(new BSurface(mFlinger, this));
1338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return sur;
1348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)wp<IBinder> Layer::getSurfaceTextureBinder() const
1378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
1388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return mSurfaceTexture->asBinder();
1398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)status_t Layer::setBuffers( uint32_t w, uint32_t h,
1428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                            PixelFormat format, uint32_t flags)
1438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
1448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // this surfaces pixel format
1458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    PixelFormatInfo info;
1468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    status_t err = getPixelFormatInfo(format, &info);
1478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (err) return err;
1488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // the display's pixel format
1508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DisplayHardware& hw(graphicPlane(0).displayHardware());
1518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    uint32_t const maxSurfaceDims = min(
1528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            hw.getMaxTextureSize(), hw.getMaxViewportDims());
1538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // never allow a surface larger than what our underlying GL implementation
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // can handle.
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        return BAD_VALUE;
1588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    PixelFormatInfo displayInfo;
1618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    getPixelFormatInfo(hw.getFormat(), &displayInfo);
1628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const uint32_t hwFlags = hw.getFlags();
1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mFormat = format;
1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
1698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mCurrentOpacity = getOpacityForFormat(format);
1708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSurfaceTexture->setDefaultBufferSize(w, h);
1728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mSurfaceTexture->setDefaultBufferFormat(format);
1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // we use the red index
1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
1768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mNeedsDithering = layerRedsize > displayRedSize;
1788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return NO_ERROR;
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void Layer::setGeometry(hwc_layer_t* hwcl)
1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles){
1848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    LayerBaseClient::setGeometry(hwcl);
1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    hwcl->flags &= ~HWC_SKIP_LAYER;
1878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // we can't do alpha-fade with the hwc HAL
1898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const State& s(drawingState());
1908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (s.alpha < 0xFF) {
1918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        hwcl->flags = HWC_SKIP_LAYER;
1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
1938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    /*
1958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)     * Transformations are applied in this order:
1968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)     * 1) buffer orientation/flip/mirror
1978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)     * 2) state transformation (window manager)
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)     * 3) layer orientation (screen orientation)
1998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)     * mTransform is already the composition of (2) and (3)
2008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)     * (NOTE: the matrices are multiplied in reverse order)
2018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)     */
2028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const Transform bufferOrientation(mCurrentTransform);
2048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const Transform tr(mTransform * bufferOrientation);
2058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
206    // this gives us only the "orientation" component of the transform
207    const uint32_t finalTransform = tr.getOrientation();
208
209    // we can only handle simple transformation
210    if (finalTransform & Transform::ROT_INVALID) {
211        hwcl->flags = HWC_SKIP_LAYER;
212    } else {
213        hwcl->transform = finalTransform;
214    }
215}
216
217void Layer::setPerFrameData(hwc_layer_t* hwcl) {
218    const sp<GraphicBuffer>& buffer(mActiveBuffer);
219    if (buffer == NULL) {
220        // this can happen if the client never drew into this layer yet,
221        // or if we ran out of memory. In that case, don't let
222        // HWC handle it.
223        hwcl->flags |= HWC_SKIP_LAYER;
224        hwcl->handle = NULL;
225    } else {
226        hwcl->handle = buffer->handle;
227    }
228
229    if (isCropped()) {
230        hwcl->sourceCrop.left   = mCurrentCrop.left;
231        hwcl->sourceCrop.top    = mCurrentCrop.top;
232        hwcl->sourceCrop.right  = mCurrentCrop.right;
233        hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
234    } else {
235        hwcl->sourceCrop.left   = 0;
236        hwcl->sourceCrop.top    = 0;
237        if (buffer != NULL) {
238            hwcl->sourceCrop.right  = buffer->width;
239            hwcl->sourceCrop.bottom = buffer->height;
240        } else {
241            hwcl->sourceCrop.right  = mTransformedBounds.width();
242            hwcl->sourceCrop.bottom = mTransformedBounds.height();
243        }
244    }
245}
246
247void Layer::onDraw(const Region& clip) const
248{
249    if (CC_UNLIKELY(mActiveBuffer == 0)) {
250        // the texture has not been created yet, this Layer has
251        // in fact never been drawn into. This happens frequently with
252        // SurfaceView because the WindowManager can't know when the client
253        // has drawn the first time.
254
255        // If there is nothing under us, we paint the screen in black, otherwise
256        // we just skip this update.
257
258        // figure out if there is something below us
259        Region under;
260        const SurfaceFlinger::LayerVector& drawingLayers(
261                mFlinger->mDrawingState.layersSortedByZ);
262        const size_t count = drawingLayers.size();
263        for (size_t i=0 ; i<count ; ++i) {
264            const sp<LayerBase>& layer(drawingLayers[i]);
265            if (layer.get() == static_cast<LayerBase const*>(this))
266                break;
267            under.orSelf(layer->visibleRegionScreen);
268        }
269        // if not everything below us is covered, we plug the holes!
270        Region holes(clip.subtract(under));
271        if (!holes.isEmpty()) {
272            clearWithOpenGL(holes, 0, 0, 0, 1);
273        }
274        return;
275    }
276
277    const GLenum target = GL_TEXTURE_EXTERNAL_OES;
278    glBindTexture(target, mTextureName);
279    if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
280        // TODO: we could be more subtle with isFixedSize()
281        glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
282        glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
283    } else {
284        glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
285        glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
286    }
287    glEnable(target);
288    glMatrixMode(GL_TEXTURE);
289    glLoadMatrixf(mTextureMatrix);
290    glMatrixMode(GL_MODELVIEW);
291
292    drawWithOpenGL(clip);
293
294    glDisable(target);
295}
296
297// As documented in libhardware header, formats in the range
298// 0x100 - 0x1FF are specific to the HAL implementation, and
299// are known to have no alpha channel
300// TODO: move definition for device-specific range into
301// hardware.h, instead of using hard-coded values here.
302#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
303
304bool Layer::getOpacityForFormat(uint32_t format)
305{
306    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
307        return true;
308    }
309    PixelFormatInfo info;
310    status_t err = getPixelFormatInfo(PixelFormat(format), &info);
311    // in case of error (unknown format), we assume no blending
312    return (err || info.h_alpha <= info.l_alpha);
313}
314
315
316bool Layer::isOpaque() const
317{
318    // if we don't have a buffer yet, we're translucent regardless of the
319    // layer's opaque flag.
320    if (mActiveBuffer == 0) {
321        return false;
322    }
323
324    // if the layer has the opaque flag, then we're always opaque,
325    // otherwise we use the current buffer's format.
326    return mOpaqueLayer || mCurrentOpacity;
327}
328
329bool Layer::isProtected() const
330{
331    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
332    return (activeBuffer != 0) &&
333            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
334}
335
336uint32_t Layer::doTransaction(uint32_t flags)
337{
338    const Layer::State& front(drawingState());
339    const Layer::State& temp(currentState());
340
341    const bool sizeChanged = (front.requested_w != temp.requested_w) ||
342            (front.requested_h != temp.requested_h);
343
344    if (sizeChanged) {
345        // the size changed, we need to ask our client to request a new buffer
346        LOGD_IF(DEBUG_RESIZE,
347                "doTransaction: "
348                "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
349                "scalingMode=%d",
350                this,
351                int(temp.requested_w), int(temp.requested_h),
352                int(front.requested_w), int(front.requested_h),
353                mCurrentScalingMode);
354
355        if (!isFixedSize()) {
356            // we're being resized and there is a freeze display request,
357            // acquire a freeze lock, so that the screen stays put
358            // until we've redrawn at the new size; this is to avoid
359            // glitches upon orientation changes.
360            if (mFlinger->hasFreezeRequest()) {
361                // if the surface is hidden, don't try to acquire the
362                // freeze lock, since hidden surfaces may never redraw
363                if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
364                    mFreezeLock = mFlinger->getFreezeLock();
365                }
366            }
367
368            // this will make sure LayerBase::doTransaction doesn't update
369            // the drawing state's size
370            Layer::State& editDraw(mDrawingState);
371            editDraw.requested_w = temp.requested_w;
372            editDraw.requested_h = temp.requested_h;
373
374            // record the new size, form this point on, when the client request
375            // a buffer, it'll get the new size.
376            mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
377        }
378    }
379
380    if (temp.sequence != front.sequence) {
381        if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
382            // this surface is now hidden, so it shouldn't hold a freeze lock
383            // (it may never redraw, which is fine if it is hidden)
384            mFreezeLock.clear();
385        }
386    }
387
388    return LayerBase::doTransaction(flags);
389}
390
391bool Layer::isFixedSize() const {
392    return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
393}
394
395bool Layer::isCropped() const {
396    return !mCurrentCrop.isEmpty();
397}
398
399// ----------------------------------------------------------------------------
400// pageflip handling...
401// ----------------------------------------------------------------------------
402
403void Layer::lockPageFlip(bool& recomputeVisibleRegions)
404{
405    if (mQueuedFrames > 0) {
406        const bool oldOpacity = isOpaque();
407
408        // signal another event if we have more frames pending
409        if (android_atomic_dec(&mQueuedFrames) > 1) {
410            mFlinger->signalEvent();
411        }
412
413        if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
414            // something happened!
415            recomputeVisibleRegions = true;
416            return;
417        }
418
419        mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
420        mSurfaceTexture->getTransformMatrix(mTextureMatrix);
421
422        const Rect crop(mSurfaceTexture->getCurrentCrop());
423        const uint32_t transform(mSurfaceTexture->getCurrentTransform());
424        const uint32_t scalingMode(mSurfaceTexture->getCurrentScalingMode());
425        if ((crop != mCurrentCrop) ||
426            (transform != mCurrentTransform) ||
427            (scalingMode != mCurrentScalingMode))
428        {
429            mCurrentCrop = crop;
430            mCurrentTransform = transform;
431            mCurrentScalingMode = scalingMode;
432            mFlinger->invalidateHwcGeometry();
433        }
434
435        mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
436        if (oldOpacity != isOpaque()) {
437            recomputeVisibleRegions = true;
438        }
439
440        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
441        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
442
443        // update the layer size and release freeze-lock
444        const Layer::State& front(drawingState());
445
446        // FIXME: mPostedDirtyRegion = dirty & bounds
447        mPostedDirtyRegion.set(front.w, front.h);
448
449
450        if ((front.w != front.requested_w) ||
451            (front.h != front.requested_h))
452        {
453            // check that we received a buffer of the right size
454            // (Take the buffer's orientation into account)
455            sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
456            uint32_t bufWidth  = newFrontBuffer->getWidth();
457            uint32_t bufHeight = newFrontBuffer->getHeight();
458            if (mCurrentTransform & Transform::ROT_90) {
459                swap(bufWidth, bufHeight);
460            }
461
462            if (isFixedSize() ||
463                    (bufWidth == front.requested_w &&
464                    bufHeight == front.requested_h))
465            {
466                // Here we pretend the transaction happened by updating the
467                // current and drawing states. Drawing state is only accessed
468                // in this thread, no need to have it locked
469                Layer::State& editDraw(mDrawingState);
470                editDraw.w = editDraw.requested_w;
471                editDraw.h = editDraw.requested_h;
472
473                // We also need to update the current state so that we don't
474                // end-up doing too much work during the next transaction.
475                // NOTE: We actually don't need hold the transaction lock here
476                // because State::w and State::h are only accessed from
477                // this thread
478                Layer::State& editTemp(currentState());
479                editTemp.w = editDraw.w;
480                editTemp.h = editDraw.h;
481
482                // recompute visible region
483                recomputeVisibleRegions = true;
484
485                // we now have the correct size, unfreeze the screen
486                mFreezeLock.clear();
487            }
488
489            LOGD_IF(DEBUG_RESIZE,
490                    "lockPageFlip : "
491                    "       (layer=%p), buffer (%ux%u, tr=%02x), "
492                    "requested (%dx%d)",
493                    this,
494                    bufWidth, bufHeight, mCurrentTransform,
495                    front.requested_w, front.requested_h);
496        }
497    }
498}
499
500void Layer::unlockPageFlip(
501        const Transform& planeTransform, Region& outDirtyRegion)
502{
503    Region dirtyRegion(mPostedDirtyRegion);
504    if (!dirtyRegion.isEmpty()) {
505        mPostedDirtyRegion.clear();
506        // The dirty region is given in the layer's coordinate space
507        // transform the dirty region by the surface's transformation
508        // and the global transformation.
509        const Layer::State& s(drawingState());
510        const Transform tr(planeTransform * s.transform);
511        dirtyRegion = tr.transform(dirtyRegion);
512
513        // At this point, the dirty region is in screen space.
514        // Make sure it's constrained by the visible region (which
515        // is in screen space as well).
516        dirtyRegion.andSelf(visibleRegionScreen);
517        outDirtyRegion.orSelf(dirtyRegion);
518    }
519    if (visibleRegionScreen.isEmpty()) {
520        // an invisible layer should not hold a freeze-lock
521        // (because it may never be updated and therefore never release it)
522        mFreezeLock.clear();
523    }
524}
525
526void Layer::dump(String8& result, char* buffer, size_t SIZE) const
527{
528    LayerBaseClient::dump(result, buffer, SIZE);
529
530    sp<const GraphicBuffer> buf0(mActiveBuffer);
531    uint32_t w0=0, h0=0, s0=0, f0=0;
532    if (buf0 != 0) {
533        w0 = buf0->getWidth();
534        h0 = buf0->getHeight();
535        s0 = buf0->getStride();
536        f0 = buf0->format;
537    }
538    snprintf(buffer, SIZE,
539            "      "
540            "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
541            " freezeLock=%p, transform-hint=0x%02x, queued-frames=%d\n",
542            mFormat, w0, h0, s0,f0,
543            getFreezeLock().get(), getTransformHint(), mQueuedFrames);
544
545    result.append(buffer);
546
547    if (mSurfaceTexture != 0) {
548        mSurfaceTexture->dump(result, "            ", buffer, SIZE);
549    }
550}
551
552uint32_t Layer::getEffectiveUsage(uint32_t usage) const
553{
554    // TODO: should we do something special if mSecure is set?
555    if (mProtectedByApp) {
556        // need a hardware-protected path to external video sink
557        usage |= GraphicBuffer::USAGE_PROTECTED;
558    }
559    return usage;
560}
561
562uint32_t Layer::getTransformHint() const {
563    uint32_t orientation = 0;
564    if (!mFlinger->mDebugDisableTransformHint) {
565        orientation = getOrientation();
566        if (orientation & Transform::ROT_INVALID) {
567            orientation = 0;
568        }
569    }
570    return orientation;
571}
572
573// ---------------------------------------------------------------------------
574
575
576}; // namespace android
577