Layer.cpp revision ddc31c3e2bc6ffe66695c385d23e8ccc3c6dad06
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2007 The Android Open Source Project
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * you may not use this file except in compliance with the License.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See the License for the specific language governing permissions and
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * limitations under the License.
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <stdlib.h>
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <stdint.h>
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include <sys/types.h>
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <cutils/compiler.h>
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <cutils/native_handle.h>
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cutils/properties.h>
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/Errors.h>
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/Log.h>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utils/StopWatch.h>
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <ui/GraphicBuffer.h>
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <ui/PixelFormat.h>
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <surfaceflinger/Surface.h>
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clz.h"
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "DisplayHardware/DisplayHardware.h"
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "DisplayHardware/HWComposer.h"
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "GLExtensions.h"
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "Layer.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "SurfaceFlinger.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "SurfaceTextureLayer.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEBUG_RESIZE    0
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace android {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T> inline T min(T a, T b) {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return a<b ? a : b;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ---------------------------------------------------------------------------
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
53116680a4aac90f2aa7413d9095a592090648e557Ben MurdochLayer::Layer(SurfaceFlinger* flinger,
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        DisplayID display, const sp<Client>& client)
557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    :   LayerBaseClient(flinger, display, client),
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mTextureName(-1U),
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mQueuedFrames(0),
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mCurrentTransform(0),
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mCurrentOpacity(true),
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mFormat(PIXEL_FORMAT_NONE),
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mGLExtensions(GLExtensions::getInstance()),
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mOpaqueLayer(true),
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mNeedsDithering(false),
64c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        mSecure(false),
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        mProtectedByApp(false),
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        mFixedSize(false)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mCurrentCrop.makeInvalid();
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    glGenTextures(1, &mTextureName);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Layer::destroy(RefBase const* base) {
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Layer::onFirstRef()
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LayerBaseClient::onFirstRef();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    setDestroyer(this);
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    struct FrameQueuedListener : public SurfaceTexture::FrameAvailableListener {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        FrameQueuedListener(Layer* layer) : mLayer(layer) { }
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private:
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        wp<Layer> mLayer;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        virtual void onFrameAvailable() {
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            sp<Layer> that(mLayer.promote());
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (that != 0) {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                that->onFrameQueued();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mSurfaceTexture = new SurfaceTextureLayer(mTextureName, this);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mSurfaceTexture->setFrameAvailableListener(new FrameQueuedListener(this));
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    mSurfaceTexture->setSynchronousMode(true);
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    mSurfaceTexture->setBufferCountServer(2);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Layer::~Layer()
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    glDeleteTextures(1, &mTextureName);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Layer::onFrameQueued() {
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (android_atomic_or(1, &mQueuedFrames) == 0) {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mFlinger->signalEvent();
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// called with SurfaceFlinger::mStateLock as soon as the layer is entered
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the purgatory list
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Layer::onRemoved()
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles){
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)sp<ISurface> Layer::createSurface()
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    class BSurface : public BnSurface, public LayerCleaner {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        wp<const Layer> mOwner;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        virtual sp<ISurfaceTexture> getSurfaceTexture() const {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            sp<ISurfaceTexture> res;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            sp<const Layer> that( mOwner.promote() );
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (that != NULL) {
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                res = that->mSurfaceTexture;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            return res;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public:
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        BSurface(const sp<SurfaceFlinger>& flinger,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                const sp<Layer>& layer)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            : LayerCleaner(flinger, layer), mOwner(layer) { }
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    sp<ISurface> sur(new BSurface(mFlinger, this));
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return sur;
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)status_t Layer::setBuffers( uint32_t w, uint32_t h,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            PixelFormat format, uint32_t flags)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // this surfaces pixel format
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PixelFormatInfo info;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_t err = getPixelFormatInfo(format, &info);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (err) return err;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the display's pixel format
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const DisplayHardware& hw(graphicPlane(0).displayHardware());
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    uint32_t const maxSurfaceDims = min(
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            hw.getMaxTextureSize(), hw.getMaxViewportDims());
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // never allow a surface larger than what our underlying GL implementation
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // can handle.
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        return BAD_VALUE;
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PixelFormatInfo displayInfo;
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    getPixelFormatInfo(hw.getFormat(), &displayInfo);
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const uint32_t hwFlags = hw.getFlags();
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mFormat = format;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mSecure = (flags & ISurfaceComposer::eSecure) ? true : false;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mProtectedByApp = (flags & ISurfaceComposer::eProtectedByApp) ? true : false;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mOpaqueLayer = (flags & ISurfaceComposer::eOpaque);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mCurrentOpacity = getOpacityForFormat(format);
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mSurfaceTexture->setDefaultBufferSize(w, h);
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mSurfaceTexture->setDefaultBufferFormat(format);
168a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
169a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // we use the red index
170a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED);
171a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED);
172a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    mNeedsDithering = layerRedsize > displayRedSize;
173a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NO_ERROR;
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Layer::setGeometry(hwc_layer_t* hwcl)
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    hwcl->compositionType = HWC_FRAMEBUFFER;
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    hwcl->hints = 0;
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    hwcl->flags = 0;
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    hwcl->transform = 0;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    hwcl->blending = HWC_BLENDING_NONE;
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // we can't do alpha-fade with the hwc HAL
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const State& s(drawingState());
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (s.alpha < 0xFF) {
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->flags = HWC_SKIP_LAYER;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return;
190a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    }
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // we can only handle simple transformation
193a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if (mOrientation & Transform::ROT_INVALID) {
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        hwcl->flags = HWC_SKIP_LAYER;
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        return;
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // FIXME: shouldn't we take the state's transform into account here?
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Transform tr(Transform(mOrientation) * Transform(mCurrentTransform));
201a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    hwcl->transform = tr.getOrientation();
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!isOpaque()) {
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        hwcl->blending = mPremultipliedAlpha ?
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                HWC_BLENDING_PREMULT : HWC_BLENDING_COVERAGE;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    hwcl->displayFrame.left   = mTransformedBounds.left;
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    hwcl->displayFrame.top    = mTransformedBounds.top;
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    hwcl->displayFrame.right  = mTransformedBounds.right;
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    hwcl->displayFrame.bottom = mTransformedBounds.bottom;
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
213f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    hwcl->visibleRegionScreen.rects =
214f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            reinterpret_cast<hwc_rect_t const *>(
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    visibleRegionScreen.getArray(
216f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                            &hwcl->visibleRegionScreen.numRects));
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Layer::setPerFrameData(hwc_layer_t* hwcl) {
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const sp<GraphicBuffer>& buffer(mActiveBuffer);
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (buffer == NULL) {
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // this can happen if the client never drew into this layer yet,
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // or if we ran out of memory. In that case, don't let
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // HWC handle it.
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->flags |= HWC_SKIP_LAYER;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->handle = NULL;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return;
228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
229f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    hwcl->handle = buffer->handle;
230f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
231f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (isCropped()) {
23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        hwcl->sourceCrop.left   = mCurrentCrop.left;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->sourceCrop.top    = mCurrentCrop.top;
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->sourceCrop.right  = mCurrentCrop.right;
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->sourceCrop.bottom = mCurrentCrop.bottom;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->sourceCrop.left   = 0;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->sourceCrop.top    = 0;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->sourceCrop.right  = buffer->width;
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        hwcl->sourceCrop.bottom = buffer->height;
2415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    }
2425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
244116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstatic inline uint16_t pack565(int r, int g, int b) {
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return (r<<11)|(g<<5)|b;
2465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid Layer::onDraw(const Region& clip) const
2485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu{
2495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    if (CC_UNLIKELY(mActiveBuffer == 0)) {
2505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        // the texture has not been created yet, this Layer has
2515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        // in fact never been drawn into. This happens frequently with
2525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        // SurfaceView because the WindowManager can't know when the client
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // has drawn the first time.
254f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // If there is nothing under us, we paint the screen in black, otherwise
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // we just skip this update.
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        // figure out if there is something below us
2595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        Region under;
2605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        const SurfaceFlinger::LayerVector& drawingLayers(mFlinger->mDrawingState.layersSortedByZ);
261ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        const size_t count = drawingLayers.size();
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        for (size_t i=0 ; i<count ; ++i) {
263ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch            const sp<LayerBase>& layer(drawingLayers[i]);
264ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch            if (layer.get() == static_cast<LayerBase const*>(this))
265ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                break;
266ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch            under.orSelf(layer->visibleRegionScreen);
267ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        }
268ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        // if not everything below us is covered, we plug the holes!
269ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        Region holes(clip.subtract(under));
270ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        if (!holes.isEmpty()) {
271a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            clearWithOpenGL(holes, 0, 0, 0, 1);
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return;
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    GLenum target = mSurfaceTexture->getCurrentTextureTarget();
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    glBindTexture(target, mTextureName);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (getFiltering() || needsFiltering() || isFixedSize() || isCropped()) {
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // TODO: we could be more subtle with isFixedSize()
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
281f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    glEnable(target);
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    glMatrixMode(GL_TEXTURE);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    glLoadMatrixf(mTextureMatrix);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    glMatrixMode(GL_MODELVIEW);
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    drawWithOpenGL(clip);
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    glDisable(target);
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// As documented in libhardware header, formats in the range
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 0x100 - 0x1FF are specific to the HAL implementation, and
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are known to have no alpha channel
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO: move definition for device-specific range into
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// hardware.h, instead of using hard-coded values here.
301#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
302
303bool Layer::getOpacityForFormat(uint32_t format)
304{
305    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
306        return true;
307    }
308    PixelFormatInfo info;
309    status_t err = getPixelFormatInfo(PixelFormat(format), &info);
310    // in case of error (unknown format), we assume no blending
311    return (err || info.h_alpha <= info.l_alpha);
312}
313
314
315bool Layer::isOpaque() const
316{
317    // if we don't have a buffer yet, we're translucent regardless of the
318    // layer's opaque flag.
319    if (mActiveBuffer == 0)
320        return false;
321
322    // if the layer has the opaque flag, then we're always opaque,
323    // otherwise we use the current buffer's format.
324    return mOpaqueLayer || mCurrentOpacity;
325}
326
327bool Layer::isProtected() const
328{
329    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
330    return (activeBuffer != 0) &&
331            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
332}
333
334uint32_t Layer::doTransaction(uint32_t flags)
335{
336    const Layer::State& front(drawingState());
337    const Layer::State& temp(currentState());
338
339    const bool sizeChanged = (front.requested_w != temp.requested_w) ||
340            (front.requested_h != temp.requested_h);
341
342    if (sizeChanged) {
343        // the size changed, we need to ask our client to request a new buffer
344        LOGD_IF(DEBUG_RESIZE,
345                "resize (layer=%p), requested (%dx%d), drawing (%d,%d), "
346                "fixedSize=%d",
347                this,
348                int(temp.requested_w), int(temp.requested_h),
349                int(front.requested_w), int(front.requested_h),
350                isFixedSize());
351
352        if (!isFixedSize()) {
353            // we're being resized and there is a freeze display request,
354            // acquire a freeze lock, so that the screen stays put
355            // until we've redrawn at the new size; this is to avoid
356            // glitches upon orientation changes.
357            if (mFlinger->hasFreezeRequest()) {
358                // if the surface is hidden, don't try to acquire the
359                // freeze lock, since hidden surfaces may never redraw
360                if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
361                    mFreezeLock = mFlinger->getFreezeLock();
362                }
363            }
364
365            // this will make sure LayerBase::doTransaction doesn't update
366            // the drawing state's size
367            Layer::State& editDraw(mDrawingState);
368            editDraw.requested_w = temp.requested_w;
369            editDraw.requested_h = temp.requested_h;
370
371            // record the new size, form this point on, when the client request
372            // a buffer, it'll get the new size.
373            mSurfaceTexture->setDefaultBufferSize(temp.requested_w, temp.requested_h);
374        }
375    }
376
377    if (temp.sequence != front.sequence) {
378        if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
379            // this surface is now hidden, so it shouldn't hold a freeze lock
380            // (it may never redraw, which is fine if it is hidden)
381            mFreezeLock.clear();
382        }
383    }
384
385    return LayerBase::doTransaction(flags);
386}
387
388bool Layer::isFixedSize() const {
389    Mutex::Autolock _l(mLock);
390    return mFixedSize;
391}
392
393void Layer::setFixedSize(bool fixedSize)
394{
395    Mutex::Autolock _l(mLock);
396    mFixedSize = fixedSize;
397}
398
399bool Layer::isCropped() const {
400    return !mCurrentCrop.isEmpty();
401}
402
403// ----------------------------------------------------------------------------
404// pageflip handling...
405// ----------------------------------------------------------------------------
406
407void Layer::lockPageFlip(bool& recomputeVisibleRegions)
408{
409    if (android_atomic_and(0, &mQueuedFrames)) {
410        if (mSurfaceTexture->updateTexImage() < NO_ERROR) {
411            // something happened!
412            recomputeVisibleRegions = true;
413            return;
414        }
415
416        // signal another event if we have more frames waiting
417        if (mSurfaceTexture->getQueuedCount()) {
418            if (android_atomic_or(1, &mQueuedFrames) == 0) {
419                mFlinger->signalEvent();
420            }
421        }
422
423        mActiveBuffer = mSurfaceTexture->getCurrentBuffer();
424        mSurfaceTexture->getTransformMatrix(mTextureMatrix);
425
426        const Rect crop(mSurfaceTexture->getCurrentCrop());
427        const uint32_t transform(mSurfaceTexture->getCurrentTransform());
428        if ((crop != mCurrentCrop) || (transform != mCurrentTransform)) {
429            mCurrentCrop = crop;
430            mCurrentTransform = transform;
431            mFlinger->invalidateHwcGeometry();
432        }
433
434        const bool opacity(getOpacityForFormat(mActiveBuffer->format));
435        if (opacity != mCurrentOpacity) {
436            mCurrentOpacity = opacity;
437            recomputeVisibleRegions = true;
438        }
439
440        const GLenum target(mSurfaceTexture->getCurrentTextureTarget());
441        glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
442        glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
443
444        // update the layer size and release freeze-lock
445        const Layer::State& front(drawingState());
446
447        // FIXME: mPostedDirtyRegion = dirty & bounds
448        mPostedDirtyRegion.set(front.w, front.h);
449
450        sp<GraphicBuffer> newFrontBuffer(mActiveBuffer);
451        if ((newFrontBuffer->getWidth()  == front.requested_w &&
452            newFrontBuffer->getHeight() == front.requested_h) ||
453            isFixedSize())
454        {
455            if ((front.w != front.requested_w) ||
456                (front.h != front.requested_h))
457            {
458                // Here we pretend the transaction happened by updating the
459                // current and drawing states. Drawing state is only accessed
460                // in this thread, no need to have it locked
461                Layer::State& editDraw(mDrawingState);
462                editDraw.w = editDraw.requested_w;
463                editDraw.h = editDraw.requested_h;
464
465                // We also need to update the current state so that we don't
466                // end-up doing too much work during the next transaction.
467                // NOTE: We actually don't need hold the transaction lock here
468                // because State::w and State::h are only accessed from
469                // this thread
470                Layer::State& editTemp(currentState());
471                editTemp.w = editDraw.w;
472                editTemp.h = editDraw.h;
473
474                // recompute visible region
475                recomputeVisibleRegions = true;
476            }
477
478            // we now have the correct size, unfreeze the screen
479            mFreezeLock.clear();
480        }
481    }
482}
483
484void Layer::unlockPageFlip(
485        const Transform& planeTransform, Region& outDirtyRegion)
486{
487    Region dirtyRegion(mPostedDirtyRegion);
488    if (!dirtyRegion.isEmpty()) {
489        mPostedDirtyRegion.clear();
490        // The dirty region is given in the layer's coordinate space
491        // transform the dirty region by the surface's transformation
492        // and the global transformation.
493        const Layer::State& s(drawingState());
494        const Transform tr(planeTransform * s.transform);
495        dirtyRegion = tr.transform(dirtyRegion);
496
497        // At this point, the dirty region is in screen space.
498        // Make sure it's constrained by the visible region (which
499        // is in screen space as well).
500        dirtyRegion.andSelf(visibleRegionScreen);
501        outDirtyRegion.orSelf(dirtyRegion);
502    }
503    if (visibleRegionScreen.isEmpty()) {
504        // an invisible layer should not hold a freeze-lock
505        // (because it may never be updated and therefore never release it)
506        mFreezeLock.clear();
507    }
508}
509
510void Layer::dump(String8& result, char* buffer, size_t SIZE) const
511{
512    LayerBaseClient::dump(result, buffer, SIZE);
513
514    sp<const GraphicBuffer> buf0(mActiveBuffer);
515    uint32_t w0=0, h0=0, s0=0, f0=0;
516    if (buf0 != 0) {
517        w0 = buf0->getWidth();
518        h0 = buf0->getHeight();
519        s0 = buf0->getStride();
520        f0 = buf0->format;
521    }
522    snprintf(buffer, SIZE,
523            "      "
524            "format=%2d, activeBuffer=[%3ux%3u:%3u,%3u],"
525            " freezeLock=%p, queued-frames=%d\n",
526            mFormat, w0, h0, s0,f0,
527            getFreezeLock().get(), mQueuedFrames);
528
529    result.append(buffer);
530
531    if (mSurfaceTexture != 0) {
532        mSurfaceTexture->dump(result, "            ", buffer, SIZE);
533    }
534}
535
536uint32_t Layer::getEffectiveUsage(uint32_t usage) const
537{
538    // TODO: should we do something special if mSecure is set?
539    if (mProtectedByApp) {
540        // need a hardware-protected path to external video sink
541        usage |= GraphicBuffer::USAGE_PROTECTED;
542    }
543    return usage;
544}
545
546// ---------------------------------------------------------------------------
547
548
549}; // namespace android
550