Layer.cpp revision ff2ed70fa30f04b90dd1a2c06ec2319e157152d7
1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
171c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS
181c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
19edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdlib.h>
20edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdint.h>
21edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <sys/types.h>
2213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian#include <math.h>
23edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
24a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include <cutils/compiler.h>
25076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <cutils/native_handle.h>
26a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include <cutils/properties.h>
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
28edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Errors.h>
29edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Log.h>
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/StopWatch.h>
311c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h>
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
333330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBuffer.h>
34edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <ui/PixelFormat.h>
359cce325fae8adcf7560a28eef394489f09bad74dMathias Agopian
3690ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/Surface.h>
37edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
38edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "clz.h"
393e25fd8609b100a75721be82d1d499f0ae9083cbMathias Agopian#include "Colorizer.h"
400f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian#include "DisplayDevice.h"
41edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "Layer.h"
42edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "SurfaceFlinger.h"
43a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include "SurfaceTextureLayer.h"
44edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
451b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian#include "DisplayHardware/HWComposer.h"
461b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian
47875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#include "RenderEngine/RenderEngine.h"
48875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian
49edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define DEBUG_RESIZE    0
50edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
51edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
52edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
53edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
54edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
5513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianint32_t Layer::sSequence = 1;
5613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
574d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias AgopianLayer::Layer(SurfaceFlinger* flinger, const sp<Client>& client,
584d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        const String8& name, uint32_t w, uint32_t h, uint32_t flags)
5913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    :   contentDirty(false),
6013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        sequence(uint32_t(android_atomic_inc(&sSequence))),
6113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mFlinger(flinger),
62a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mTextureName(-1U),
6313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mPremultipliedAlpha(true),
6413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mName("unnamed"),
6513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mDebug(false),
6613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mFormat(PIXEL_FORMAT_NONE),
6713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mOpaqueLayer(true),
6813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mTransactionFlags(0),
69a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mQueuedFrames(0),
70a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mCurrentTransform(0),
71933389f75814bb62e8153528f9cff2cb329b77dfMathias Agopian        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
72a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mCurrentOpacity(true),
734d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian        mRefreshPending(false),
7482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        mFrameLatencyNeeded(false),
7513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mFiltering(false),
7613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mNeedsFiltering(false),
775cdc8994a0ecd751a6350b16a1bef8b6b0d09b11Mathias Agopian        mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
78b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian        mSecure(false),
7913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mProtectedByApp(false),
8013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mHasSurface(false),
8113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mClientRef(client)
82edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
83a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mCurrentCrop.makeInvalid();
843f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    mFlinger->getRenderEngine().genTextures(1, &mTextureName);
8549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
864d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
874d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    uint32_t layerFlags = 0;
884d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    if (flags & ISurfaceComposerClient::eHidden)
894d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        layerFlags = layer_state_t::eLayerHidden;
904d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
914d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    if (flags & ISurfaceComposerClient::eNonPremultiplied)
924d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        mPremultipliedAlpha = false;
934d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
944d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mName = name;
954d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
964d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.active.w = w;
974d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.active.h = h;
984d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.active.crop.makeInvalid();
994d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.z = 0;
1004d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.alpha = 0xFF;
1014d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.layerStack = 0;
1024d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.flags = layerFlags;
1034d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.sequence = 0;
1044d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.transform.set(0, 0);
1054d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.requested = mCurrentState.active;
1064d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
1074d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    // drawing state & current state are identical
1084d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mDrawingState = mCurrentState;
1096547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis
1106547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    nsecs_t displayPeriod =
1116547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis            flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1126547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
113e8696a40e09b24b634214684d18526187b316a2fJamie Gennis}
114e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
1153f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianvoid Layer::onFirstRef() {
116bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
117db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    mBufferQueue = new SurfaceTextureLayer(mFlinger);
1183f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(mBufferQueue, mTextureName);
119bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
120bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    mSurfaceFlingerConsumer->setFrameAvailableListener(this);
1214d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mSurfaceFlingerConsumer->setName(mName);
122b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
1237f42a9c47c5a7f40cf02032d286d6bd62f28e650Mathias Agopian#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
1247f42a9c47c5a7f40cf02032d286d6bd62f28e650Mathias Agopian#warning "disabling triple buffering"
125bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    mSurfaceFlingerConsumer->setDefaultMaxBufferCount(2);
1267f42a9c47c5a7f40cf02032d286d6bd62f28e650Mathias Agopian#else
127bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    mSurfaceFlingerConsumer->setDefaultMaxBufferCount(3);
128303d538bb012e82c6b9a98c4930a03455000f761Mathias Agopian#endif
1296905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden
1308430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian    const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
1318430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian    updateTransformHint(hw);
132b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian}
13396f0819f81293076e652792794a961543e6750d7Mathias Agopian
1344d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias AgopianLayer::~Layer() {
13513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Client> c(mClientRef.promote());
13613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (c != 0) {
13713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        c->detachLayer(this);
13813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
139921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    mFlinger->deleteTextureAsync(mTextureName);
1406547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    mFrameTracker.logAndResetStats(mName);
14196f0819f81293076e652792794a961543e6750d7Mathias Agopian}
14296f0819f81293076e652792794a961543e6750d7Mathias Agopian
14313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
14413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// callbacks
14513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
14613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
14713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::onLayerDisplayed(const sp<const DisplayDevice>& hw,
14813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        HWComposer::HWCLayerInterface* layer) {
14913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (layer) {
15013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        layer->onDisplayed();
15113f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence());
15213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
15313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
15413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
155a4a3149a36bc69a06e4824aeae909ab910661070Igor Murashkinvoid Layer::onFrameAvailable() {
1563d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis    android_atomic_inc(&mQueuedFrames);
15799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mFlinger->signalLayerUpdate();
158579b3f88d03d06b897b778bd11818f5104677d1dMathias Agopian}
159579b3f88d03d06b897b778bd11818f5104677d1dMathias Agopian
1606710604286401d4205c27235a252dd0e5008cc08Mathias Agopian// called with SurfaceFlinger::mStateLock from the drawing thread after
1616710604286401d4205c27235a252dd0e5008cc08Mathias Agopian// the layer has been remove from the current state list (and just before
1626710604286401d4205c27235a252dd0e5008cc08Mathias Agopian// it's removed from the drawing state list)
16313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::onRemoved() {
164bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    mSurfaceFlingerConsumer->abandon();
16548d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian}
166cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
16713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
16813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// set-up
16913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
17013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
1711eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopianconst String8& Layer::getName() const {
17213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return mName;
17313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
17413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
17513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t Layer::setBuffers( uint32_t w, uint32_t h,
17613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                            PixelFormat format, uint32_t flags)
177edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
17813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    uint32_t const maxSurfaceDims = min(
17913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
18013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
18113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // never allow a surface larger than what our underlying GL implementation
18213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // can handle.
18313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
18413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
18513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return BAD_VALUE;
18613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
18713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
18813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mFormat = format;
18913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
19013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mSecure = (flags & ISurfaceComposerClient::eSecure) ? true : false;
19113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
19213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mOpaqueLayer = (flags & ISurfaceComposerClient::eOpaque);
19313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentOpacity = getOpacityForFormat(format);
19413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
19513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
19613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
19713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
19813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
19913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return NO_ERROR;
20013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
20113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
2024d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopiansp<IBinder> Layer::getHandle() {
2034d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    Mutex::Autolock _l(mLock);
2044d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
2054d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    LOG_ALWAYS_FATAL_IF(mHasSurface,
2064d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            "Layer::getHandle() has already been called");
2074d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
2084d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mHasSurface = true;
2094d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
210882e3a39ed770b335a203e233b57127fde1c839eAndy McFadden    /*
2114d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian     * The layer handle is just a BBinder object passed to the client
2124d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian     * (remote process) -- we don't keep any reference on our side such that
2134d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian     * the dtor is called when the remote side let go of its reference.
214882e3a39ed770b335a203e233b57127fde1c839eAndy McFadden     *
2154d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian     * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
2164d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian     * this layer when the handle is destroyed.
217882e3a39ed770b335a203e233b57127fde1c839eAndy McFadden     */
2184d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
2194d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    class Handle : public BBinder, public LayerCleaner {
220a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        wp<const Layer> mOwner;
221a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    public:
2224d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
2234d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            : LayerCleaner(flinger, layer), mOwner(layer) {
2244d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        }
225a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    };
226edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2274d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return new Handle(mFlinger, this);
228582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis}
229582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis
230db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopiansp<IGraphicBufferProducer> Layer::getBufferQueue() const {
231db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    return mBufferQueue;
232edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
233edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
23413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
23513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// h/w composer set-up
23613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
23713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
238a8bca8d84b559e7dcca010f7d6514333004020c7Mathias AgopianRect Layer::getContentCrop() const {
239a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian    // this is the crop rectangle that applies to the buffer
240a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian    // itself (as opposed to the window)
241f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    Rect crop;
242f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    if (!mCurrentCrop.isEmpty()) {
243a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian        // if the buffer crop is defined, we use that
244f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        crop = mCurrentCrop;
245a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian    } else if (mActiveBuffer != NULL) {
246a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian        // otherwise we use the whole buffer
247a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian        crop = mActiveBuffer->getBounds();
248f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    } else {
249a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian        // if we don't have a buffer yet, we use an empty/invalid crop
2504fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        crop.makeInvalid();
251f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    }
252f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    return crop;
253f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis}
254f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis
255f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopianstatic Rect reduce(const Rect& win, const Region& exclude) {
256f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    if (CC_LIKELY(exclude.isEmpty())) {
257f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian        return win;
258f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    }
259f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    if (exclude.isRect()) {
260f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian        return win.reduce(exclude.getBounds());
261f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    }
262f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    return Region(win).subtract(exclude).getBounds();
263f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian}
264f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian
26513127d8921356dff794250e04208c3ed60b3a3dfMathias AgopianRect Layer::computeBounds() const {
2661eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& s(getDrawingState());
26713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    Rect win(s.active.w, s.active.h);
26813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (!s.active.crop.isEmpty()) {
26913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        win.intersect(s.active.crop, &win);
27013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
2716c7f25afb75ac155bad0b3bc17c0089d0337d060Mathias Agopian    // subtract the transparent region and snap to the bounds
272f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    return reduce(win, s.activeTransparentRegion);
27313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
27413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
2756b44267a3beb457e220cad0666c039d3a765cdb2Mathias AgopianFloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
27613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // the content crop is the area of the content that gets scaled to the
27713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // layer's size.
2786b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    FloatRect crop(getContentCrop());
27913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
28013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // the active.crop is the area of the window that gets cropped, but not
28113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // scaled in any ways.
2821eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const State& s(getDrawingState());
28313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
28413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // apply the projection's clipping to the window crop in
28513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // layerstack space, and convert-back to layer space.
2866b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // if there are no window scaling involved, this operation will map to full
2876b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // pixels in the buffer.
2886b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
2896b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // a viewport clipping and a window transform. we should use floating point to fix this.
2900e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian
2910e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian    Rect activeCrop(s.active.w, s.active.h);
2920e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian    if (!s.active.crop.isEmpty()) {
2930e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian        activeCrop = s.active.crop;
2940e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian    }
2950e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian
2960e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian    activeCrop = s.transform.transform(activeCrop);
29713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    activeCrop.intersect(hw->getViewport(), &activeCrop);
29813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    activeCrop = s.transform.inverse().transform(activeCrop);
29913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
30013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // paranoia: make sure the window-crop is constrained in the
30113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // window's bounds
30213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop);
30313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
304f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    // subtract the transparent region and snap to the bounds
305f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    activeCrop = reduce(activeCrop, s.activeTransparentRegion);
306f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian
30713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (!activeCrop.isEmpty()) {
30813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // Transform the window crop to match the buffer coordinate system,
30913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // which means using the inverse of the current transform set on the
31013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // SurfaceFlingerConsumer.
3116b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        uint32_t invTransform = mCurrentTransform;
31213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        int winWidth = s.active.w;
31313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        int winHeight = s.active.h;
31413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
31513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
31613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                    NATIVE_WINDOW_TRANSFORM_FLIP_H;
31713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            winWidth = s.active.h;
31813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            winHeight = s.active.w;
31913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        }
32013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const Rect winCrop = activeCrop.transform(
32113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                invTransform, s.active.w, s.active.h);
32213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
3236b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        // below, crop is intersected with winCrop expressed in crop's coordinate space
3246b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        float xScale = crop.getWidth()  / float(winWidth);
3256b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        float yScale = crop.getHeight() / float(winHeight);
32613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
3276b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        float insetL = winCrop.left                 * xScale;
3286b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        float insetT = winCrop.top                  * yScale;
3296b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        float insetR = (winWidth  - winCrop.right ) * xScale;
3306b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        float insetB = (winHeight - winCrop.bottom) * yScale;
33113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
33213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        crop.left   += insetL;
33313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        crop.top    += insetT;
33413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        crop.right  -= insetR;
33513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        crop.bottom -= insetB;
33613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
33713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return crop;
33813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
33913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
3404fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid Layer::setGeometry(
3414297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    const sp<const DisplayDevice>& hw,
3424fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        HWComposer::HWCLayerInterface& layer)
343a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
34413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    layer.setDefaultState();
345a537c0f42e8077baafcbc65844adf1ec8397c040Mathias Agopian
3463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // enable this layer
3473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    layer.setSkip(false);
348a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
349dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    if (isSecure() && !hw->isSecure()) {
350dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis        layer.setSkip(true);
351dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    }
352dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis
35313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // this gives us only the "orientation" component of the transform
3541eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const State& s(getDrawingState());
35513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (!isOpaque() || s.alpha != 0xFF) {
35613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        layer.setBlending(mPremultipliedAlpha ?
35713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                HWC_BLENDING_PREMULT :
35813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                HWC_BLENDING_COVERAGE);
35913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
36013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
36113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // apply the layer's transform, followed by the display's global transform
36213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // here we're guaranteed that the layer's transform preserves rects
36313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    Rect frame(s.transform.transform(computeBounds()));
36413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    frame.intersect(hw->getViewport(), &frame);
36513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Transform& tr(hw->getTransform());
36613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    layer.setFrame(tr.transform(frame));
36713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    layer.setCrop(computeCrop(hw));
3689f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    layer.setPlaneAlpha(s.alpha);
3699f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian
37029a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    /*
37129a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * Transformations are applied in this order:
37229a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 1) buffer orientation/flip/mirror
37329a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 2) state transformation (window manager)
37429a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 3) layer orientation (screen orientation)
37529a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * (NOTE: the matrices are multiplied in reverse order)
37629a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     */
37729a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian
37829a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    const Transform bufferOrientation(mCurrentTransform);
37913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Transform transform(tr * s.transform * bufferOrientation);
38029a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian
38129a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    // this gives us only the "orientation" component of the transform
38213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const uint32_t orientation = transform.getOrientation();
38313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (orientation & Transform::ROT_INVALID) {
38413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // we can only handle simple transformation
3853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        layer.setSkip(true);
386a537c0f42e8077baafcbc65844adf1ec8397c040Mathias Agopian    } else {
38713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        layer.setTransform(orientation);
388a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
389a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
390a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3914297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopianvoid Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
392d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        HWComposer::HWCLayerInterface& layer) {
39313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // we have to set the visible region on every frame because
39413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // we currently free it during onLayerDisplayed(), which is called
39513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // after HWComposer::commit() -- every frame.
39613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // Apply this display's projection's viewport to the visible region
39713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // before giving it to the HWC HAL.
39813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Transform& tr = hw->getTransform();
39913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
40013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    layer.setVisibleRegionScreen(visible);
40113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
4023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // NOTE: buffer can be NULL if the client never drew into this
4033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // layer yet, or if we ran out of memory
40471e83e1202dc87afde9c0c31f31c129f3f99263fMathias Agopian    layer.setBuffer(mActiveBuffer);
405c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall}
406dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall
4074297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopianvoid Layer::setAcquireFence(const sp<const DisplayDevice>& hw,
408d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        HWComposer::HWCLayerInterface& layer) {
409c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall    int fenceFd = -1;
410d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
411d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // TODO: there is a possible optimization here: we only need to set the
412d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // acquire fence the first time a new buffer is acquired on EACH display.
413d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
414d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    if (layer.getCompositionType() == HWC_OVERLAY) {
415bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
4161df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis        if (fence->isValid()) {
417c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall            fenceFd = fence->dup();
418dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            if (fenceFd == -1) {
419dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall                ALOGW("failed to dup layer fence, skipping sync: %d", errno);
420dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            }
421dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        }
422dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
423c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall    layer.setAcquireFenceFd(fenceFd);
424c7f3381c3b2945e441747130eae88214435d0819Mathias Agopian}
425c7f3381c3b2945e441747130eae88214435d0819Mathias Agopian
42613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
42713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// drawing...
42813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
42913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
43013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
43113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    onDraw(hw, clip);
43213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
43313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
43413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::draw(const sp<const DisplayDevice>& hw) {
43513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    onDraw( hw, Region(hw->bounds()) );
43613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
43713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
4384297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopianvoid Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
439edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
4401c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
4411c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
442a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    if (CC_UNLIKELY(mActiveBuffer == 0)) {
443edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // the texture has not been created yet, this Layer has
444179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // in fact never been drawn into. This happens frequently with
445179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // SurfaceView because the WindowManager can't know when the client
446179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // has drawn the first time.
447179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian
448179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // If there is nothing under us, we paint the screen in black, otherwise
449179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // we just skip this update.
450179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian
451179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // figure out if there is something below us
452179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        Region under;
453f7ae69d4bd292110da976c8ae766a8ef083d731fMathias Agopian        const SurfaceFlinger::LayerVector& drawingLayers(
454f7ae69d4bd292110da976c8ae766a8ef083d731fMathias Agopian                mFlinger->mDrawingState.layersSortedByZ);
455179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        const size_t count = drawingLayers.size();
456179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        for (size_t i=0 ; i<count ; ++i) {
45713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(drawingLayers[i]);
45813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            if (layer.get() == static_cast<Layer const*>(this))
459179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian                break;
4604297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
461179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        }
462179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        // if not everything below us is covered, we plug the holes!
463179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        Region holes(clip.subtract(under));
464179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        if (!holes.isEmpty()) {
4651b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian            clearWithOpenGL(hw, holes, 0, 0, 0, 1);
466179169e88e05261196b76d7ddf94aa870aafaf9aMathias Agopian        }
467edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return;
468edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
469a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
47097eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    // Bind the current buffer to the GL texture, and wait for it to be
47197eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    // ready for us to draw into.
472bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    status_t err = mSurfaceFlingerConsumer->bindTextureImage();
473bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    if (err != NO_ERROR) {
47497eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden        ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
475dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        // Go ahead and draw the buffer anyway; no matter what we do the screen
476dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        // is probably going to have something visibly wrong.
477dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
478dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall
479dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
480dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis
481875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    RenderEngine& engine(mFlinger->getRenderEngine());
482875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian
483dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    if (!blackOutLayer) {
484cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        // TODO: we could be more subtle with isFixedSize()
485eba8c688f633f3f3f1b75c2bc64faf799dd2b5f2Mathias Agopian        const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
486cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis
487cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        // Query the texture matrix given our current filtering mode.
488cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        float textureMatrix[16];
489bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
490bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
491cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis
492cbb1a95819ec302ae15e4a1162a8b1349ae5c33eJamie Gennis        // Set things up for texturing.
49349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian        mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
49449457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian        mTexture.setFiltering(useFiltering);
49549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian        mTexture.setMatrix(textureMatrix);
49649457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
49749457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian        engine.setupLayerTexturing(mTexture);
498a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    } else {
499875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian        engine.setupLayerBlackedOut();
500a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    }
5011b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian    drawWithOpenGL(hw, clip);
502875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    engine.disableTexturing();
503edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
504edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
50513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
50613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
5073f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian        float red, float green, float blue, float alpha) const
50813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian{
50919733a32799f792125913e746e8644d16f8dc223Mathias Agopian    RenderEngine& engine(mFlinger->getRenderEngine());
5105cdc8994a0ecd751a6350b16a1bef8b6b0d09b11Mathias Agopian    computeGeometry(hw, mMesh);
51119733a32799f792125913e746e8644d16f8dc223Mathias Agopian    engine.setupFillWithColor(red, green, blue, alpha);
51219733a32799f792125913e746e8644d16f8dc223Mathias Agopian    engine.drawMesh(mMesh);
51313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
51413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
51513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::clearWithOpenGL(
51613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<const DisplayDevice>& hw, const Region& clip) const {
51713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    clearWithOpenGL(hw, clip, 0,0,0,0);
51813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
51913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
52013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::drawWithOpenGL(
52113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<const DisplayDevice>& hw, const Region& clip) const {
52213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const uint32_t fbHeight = hw->getHeight();
5231eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const State& s(getDrawingState());
52413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
5255cdc8994a0ecd751a6350b16a1bef8b6b0d09b11Mathias Agopian    computeGeometry(hw, mMesh);
52613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
52713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    /*
52813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * NOTE: the way we compute the texture coordinates here produces
52913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * different results than when we take the HWC path -- in the later case
53013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * the "source crop" is rounded to texel boundaries.
53113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * This can produce significantly different results when the texture
53213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * is scaled by a large amount.
53313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     *
53413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * The GL code below is more logical (imho), and the difference with
53513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * HWC is due to a limitation of the HWC API to integers -- a question
53613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * is suspend is wether we should ignore this problem or revert to
53713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * GL composition when a buffer scaling is applied (maybe with some
53813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * minimal value)? Or, we could make GL behave like HWC -- but this feel
53913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     * like more of a hack.
54013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian     */
54113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Rect win(computeBounds());
54213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
5433f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    float left   = float(win.left)   / float(s.active.w);
5443f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    float top    = float(win.top)    / float(s.active.h);
5453f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    float right  = float(win.right)  / float(s.active.w);
5463f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    float bottom = float(win.bottom) / float(s.active.h);
54713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
548875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    // TODO: we probably want to generate the texture coords with the mesh
549875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    // here we assume that we only have 4 vertices
550ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
551ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    texCoords[0] = vec2(left, 1.0f - top);
552ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    texCoords[1] = vec2(left, 1.0f - bottom);
553ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    texCoords[2] = vec2(right, 1.0f - bottom);
554ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    texCoords[3] = vec2(right, 1.0f - top);
55513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
556875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    RenderEngine& engine(mFlinger->getRenderEngine());
557875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(), s.alpha);
5585cdc8994a0ecd751a6350b16a1bef8b6b0d09b11Mathias Agopian    engine.drawMesh(mMesh);
559875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian    engine.disableBlending();
56013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
56113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
56213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::setFiltering(bool filtering) {
56313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mFiltering = filtering;
56413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
56513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
56613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::getFiltering() const {
56713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return mFiltering;
56813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
56913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
570ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// As documented in libhardware header, formats in the range
571ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// 0x100 - 0x1FF are specific to the HAL implementation, and
572ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// are known to have no alpha channel
573ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// TODO: move definition for device-specific range into
574ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold// hardware.h, instead of using hard-coded values here.
575ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
576ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold
5775773d3f5b2694c647e010246dff99acc70131289Mathias Agopianbool Layer::getOpacityForFormat(uint32_t format) {
578a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
579a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        return true;
580ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold    }
5815773d3f5b2694c647e010246dff99acc70131289Mathias Agopian    switch (format) {
5825773d3f5b2694c647e010246dff99acc70131289Mathias Agopian        case HAL_PIXEL_FORMAT_RGBA_8888:
5835773d3f5b2694c647e010246dff99acc70131289Mathias Agopian        case HAL_PIXEL_FORMAT_BGRA_8888:
584c2e41222bf02a6579763974f82d65875cfa43481Jesse Hall        case HAL_PIXEL_FORMAT_sRGB_A_8888:
585dd533712f8dd21c0dadfd5ce8a0ad85aa3e96adaMathias Agopian            return false;
5865773d3f5b2694c647e010246dff99acc70131289Mathias Agopian    }
5875773d3f5b2694c647e010246dff99acc70131289Mathias Agopian    // in all other case, we have no blending (also for unknown formats)
588dd533712f8dd21c0dadfd5ce8a0ad85aa3e96adaMathias Agopian    return true;
589ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold}
590ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold
59113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
59213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// local state
59313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
59413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
5953f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianvoid Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh) const
59613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian{
5971eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& s(getDrawingState());
59813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Transform tr(hw->getTransform() * s.transform);
59913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const uint32_t hw_h = hw->getHeight();
60013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    Rect win(s.active.w, s.active.h);
60113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (!s.active.crop.isEmpty()) {
60213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        win.intersect(s.active.crop, &win);
60313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
6046c7f25afb75ac155bad0b3bc17c0089d0337d060Mathias Agopian    // subtract the transparent region and snap to the bounds
605f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    win = reduce(win, s.activeTransparentRegion);
6063f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian
607ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
608ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    position[0] = tr.transform(win.left,  win.top);
609ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    position[1] = tr.transform(win.left,  win.bottom);
610ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    position[2] = tr.transform(win.right, win.bottom);
611ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    position[3] = tr.transform(win.right, win.top);
6123f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    for (size_t i=0 ; i<4 ; i++) {
6135cdc8994a0ecd751a6350b16a1bef8b6b0d09b11Mathias Agopian        position[i].y = hw_h - position[i].y;
61413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
61513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
616ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold
617a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopianbool Layer::isOpaque() const
618a7f669256f93a593c723f05784ef04d3c7d052bbMathias Agopian{
619a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // if we don't have a buffer yet, we're translucent regardless of the
620a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // layer's opaque flag.
621db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis    if (mActiveBuffer == 0) {
622a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        return false;
623db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis    }
624a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
625a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // if the layer has the opaque flag, then we're always opaque,
626a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    // otherwise we use the current buffer's format.
627a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    return mOpaqueLayer || mCurrentOpacity;
628a7f669256f93a593c723f05784ef04d3c7d052bbMathias Agopian}
629a7f669256f93a593c723f05784ef04d3c7d052bbMathias Agopian
6307a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennisbool Layer::isProtected() const
6317a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis{
632a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
6337a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis    return (activeBuffer != 0) &&
6347a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
6357a4d0dfd43558c299e6af6c4910ef76db9db3172Jamie Gennis}
636b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian
63713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::isFixedSize() const {
63813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
63913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
64013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
64113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::isCropped() const {
64213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return !mCurrentCrop.isEmpty();
64313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
64413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
64513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const {
64613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return mNeedsFiltering || hw->needsFiltering();
64713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
64813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
64913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::setVisibleRegion(const Region& visibleRegion) {
65013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // always called from main thread
65113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    this->visibleRegion = visibleRegion;
65213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
65313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
65413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::setCoveredRegion(const Region& coveredRegion) {
65513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // always called from main thread
65613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    this->coveredRegion = coveredRegion;
65713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
65813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
65913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::setVisibleNonTransparentRegion(const Region&
66013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        setVisibleNonTransparentRegion) {
66113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // always called from main thread
66213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
66313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
66413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
66513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
66613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// transaction
66713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
66813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
66913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianuint32_t Layer::doTransaction(uint32_t flags) {
6701c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
6711c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
6721eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& s(getDrawingState());
6731eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& c(getCurrentState());
674edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
6751eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const bool sizeChanged = (c.requested.w != s.requested.w) ||
6761eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                             (c.requested.h != s.requested.h);
677a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
678a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian    if (sizeChanged) {
679cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian        // the size changed, we need to ask our client to request a new buffer
6809d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block        ALOGD_IF(DEBUG_RESIZE,
6816905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden                "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
682419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "  current={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
683419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n"
684419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
685419e196e639c8adb875da2765abcef95017b6d4aMathias Agopian                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
6861eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                this, getName().string(), mCurrentTransform, mCurrentScalingMode,
6871eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.active.w, c.active.h,
6881eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.active.crop.left,
6891eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.active.crop.top,
6901eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.active.crop.right,
6911eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.active.crop.bottom,
6921eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.active.crop.getWidth(),
6931eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.active.crop.getHeight(),
6941eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.w, c.requested.h,
6951eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.crop.left,
6961eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.crop.top,
6971eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.crop.right,
6981eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.crop.bottom,
6991eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.crop.getWidth(),
7001eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.crop.getHeight(),
7011eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.active.w, s.active.h,
7021eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.active.crop.left,
7031eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.active.crop.top,
7041eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.active.crop.right,
7051eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.active.crop.bottom,
7061eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.active.crop.getWidth(),
7071eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.active.crop.getHeight(),
7081eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.requested.w, s.requested.h,
7091eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.requested.crop.left,
7101eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.requested.crop.top,
7111eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.requested.crop.right,
7121eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.requested.crop.bottom,
7131eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.requested.crop.getWidth(),
7141eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                s.requested.crop.getHeight());
715a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
7162a0d5b608447a880beff5149805425f02691442bJamie Gennis        // record the new size, form this point on, when the client request
7172a0d5b608447a880beff5149805425f02691442bJamie Gennis        // a buffer, it'll get the new size.
718bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        mSurfaceFlingerConsumer->setDefaultBufferSize(
7191eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                c.requested.w, c.requested.h);
720edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
721cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
7220cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian    if (!isFixedSize()) {
7230cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
7241eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        const bool resizePending = (c.requested.w != c.active.w) ||
7251eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian                                   (c.requested.h != c.active.h);
7260cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
7270cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian        if (resizePending) {
72813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            // don't let Layer::doTransaction update the drawing state
7290cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // if we have a pending resize, unless we are in fixed-size mode.
7300cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // the drawing state will be updated only once we receive a buffer
7310cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // with the correct size.
7320cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            //
7330cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // in particular, we want to make sure the clip (which is part
7340cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // of the geometry state) is latched together with the size but is
7350cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            // latched immediately when no resizing is involved.
7360cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
7370cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            flags |= eDontUpdateGeometryState;
7380cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian        }
7390cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian    }
7400cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
74113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // always set active to requested, unless we're asked not to
74213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // this is used by Layer, which special cases resizes.
74313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (flags & eDontUpdateGeometryState)  {
74413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    } else {
7451eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        Layer::State& editCurrentState(getCurrentState());
7461eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        editCurrentState.active = c.requested;
74713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
74813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
7491eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    if (s.active != c.active) {
75013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // invalidate and recompute the visible regions if needed
75113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        flags |= Layer::eVisibleRegion;
75213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
75313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
7541eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    if (c.sequence != s.sequence) {
75513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // invalidate and recompute the visible regions if needed
75613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        flags |= eVisibleRegion;
75713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        this->contentDirty = true;
75813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
75913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // we may use linear filtering, if the matrix scales us
7601eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        const uint8_t type = c.transform.getType();
7611eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        mNeedsFiltering = (!c.transform.preserveRects() ||
76213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                (type >= Transform::SCALE));
76313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
76413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
76513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // Commit the transaction
76613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    commitTransaction();
76713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return flags;
768edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
769edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
77013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::commitTransaction() {
77113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mDrawingState = mCurrentState;
772a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian}
773a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
77413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianuint32_t Layer::getTransactionFlags(uint32_t flags) {
77513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return android_atomic_and(~flags, &mTransactionFlags) & flags;
77613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
77713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
77813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianuint32_t Layer::setTransactionFlags(uint32_t flags) {
77913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return android_atomic_or(flags, &mTransactionFlags);
78013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
78113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
78213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setPosition(float x, float y) {
78313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
78413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
78513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
78613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.transform.set(x, y);
78713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
78813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
78913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
79013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setLayer(uint32_t z) {
79113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mCurrentState.z == z)
79213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
79313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
79413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.z = z;
79513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
79613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
79713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
79813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setSize(uint32_t w, uint32_t h) {
79913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
80013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
80113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.requested.w = w;
80213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.requested.h = h;
80313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
80413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
80513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
80613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setAlpha(uint8_t alpha) {
80713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mCurrentState.alpha == alpha)
80813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
80913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
81013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.alpha = alpha;
81113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
81213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
81313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
81413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
81513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
81613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.transform.set(
81713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
81813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
81913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
82013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
82113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setTransparentRegionHint(const Region& transparent) {
8222ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian    mCurrentState.requestedTransparentRegion = transparent;
82313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
82413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
82513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
82613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setFlags(uint8_t flags, uint8_t mask) {
82713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
82813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mCurrentState.flags == newFlags)
82913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
83013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
83113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.flags = newFlags;
83213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
83313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
83413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
83513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setCrop(const Rect& crop) {
83613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mCurrentState.requested.crop == crop)
83713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
83813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
83913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.requested.crop = crop;
84013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
84113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
84213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
84313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
84413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setLayerStack(uint32_t layerStack) {
84513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mCurrentState.layerStack == layerStack)
84613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
84713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
84813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.layerStack = layerStack;
84913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
85013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
851a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian}
852a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
853edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
854edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// pageflip handling...
855edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
856edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
8574d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopianbool Layer::onPreComposition() {
8584d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian    mRefreshPending = false;
8594d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian    return mQueuedFrames > 0;
86099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
86199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
862d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopianvoid Layer::onPostComposition() {
863d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    if (mFrameLatencyNeeded) {
864bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
86582dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis        mFrameTracker.setDesiredPresentTime(desiredPresentTime);
86682dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis
867bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
868789a6c3f1dfe22a1ffea7f39b2098d7842cd1f30Jamie Gennis        if (frameReadyFence->isValid()) {
86982dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            mFrameTracker.setFrameReadyFence(frameReadyFence);
87082dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis        } else {
87182dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            // There was no fence for this frame, so assume that it was ready
87282dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            // to be presented at the desired present time.
87382dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            mFrameTracker.setFrameReadyTime(desiredPresentTime);
87482dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis        }
87582dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis
876d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        const HWComposer& hwc = mFlinger->getHwComposer();
87782dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis        sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
878789a6c3f1dfe22a1ffea7f39b2098d7842cd1f30Jamie Gennis        if (presentFence->isValid()) {
87982dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            mFrameTracker.setActualPresentFence(presentFence);
88082dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis        } else {
88182dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            // The HWC doesn't support present fences, so use the refresh
88282dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            // timestamp instead.
88382dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
88482dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis            mFrameTracker.setActualPresentTime(presentTime);
88582dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis        }
88682dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis
88782dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis        mFrameTracker.advanceFrame();
888d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian        mFrameLatencyNeeded = false;
889d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    }
890d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
891d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
892da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool Layer::isVisible() const {
89313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Layer::State& s(mDrawingState);
89413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return !(s.flags & layer_state_t::eLayerHidden) && s.alpha
89513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            && (mActiveBuffer != NULL);
896da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
897da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
8984fec873a98f7b4380720cd1ad006f74c8cdc73daMathias AgopianRegion Layer::latchBuffer(bool& recomputeVisibleRegions)
899edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
9001c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
9011c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
9024fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    Region outDirtyRegion;
9033d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis    if (mQueuedFrames > 0) {
90499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
90599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // if we've already called updateTexImage() without going through
90699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // a composition step, we have to skip this layer at this point
90799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // because we cannot call updateTeximage() without a corresponding
90899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // compositionComplete() call.
90999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        // we'll trigger an update in onPreComposition().
9104d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian        if (mRefreshPending) {
9114fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            return outDirtyRegion;
91299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        }
91399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
914351a513b12622781de9580b3c96fd0a8578b563bJamie Gennis        // Capture the old state of the layer for comparisons later
915db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis        const bool oldOpacity = isOpaque();
916351a513b12622781de9580b3c96fd0a8578b563bJamie Gennis        sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
917db5230f4441fa8f120f15bdd6fcfc6e75d9c27d0Jamie Gennis
918bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
9192c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            Layer::State& front;
9202c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            Layer::State& current;
9212c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            bool& recomputeVisibleRegions;
9222c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            Reject(Layer::State& front, Layer::State& current,
9232c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    bool& recomputeVisibleRegions)
9242c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                : front(front), current(current),
9252c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                  recomputeVisibleRegions(recomputeVisibleRegions) {
9262c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            }
9272c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9282c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            virtual bool reject(const sp<GraphicBuffer>& buf,
929db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian                    const IGraphicBufferConsumer::BufferItem& item) {
9302c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (buf == NULL) {
9312c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    return false;
9322c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
9332c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9342c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                uint32_t bufWidth  = buf->getWidth();
9352c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                uint32_t bufHeight = buf->getHeight();
9362c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9372c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                // check that we received a buffer of the right size
9382c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                // (Take the buffer's orientation into account)
9392c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (item.mTransform & Transform::ROT_90) {
9402c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    swap(bufWidth, bufHeight);
9412c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
9422c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9432c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
9442c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (front.active != front.requested) {
9452c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9462c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    if (isFixedSize ||
9472c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            (bufWidth == front.requested.w &&
9482c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                             bufHeight == front.requested.h))
9492c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    {
9502c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // Here we pretend the transaction happened by updating the
9512c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // current and drawing states. Drawing state is only accessed
9522c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // in this thread, no need to have it locked
9532c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        front.active = front.requested;
9542c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9552c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // We also need to update the current state so that
9562c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // we don't end-up overwriting the drawing state with
9572c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // this stale current state during the next transaction
9582c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        //
9592c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // NOTE: We don't need to hold the transaction lock here
9602c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // because State::active is only accessed from this thread.
9612c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        current.active = front.active;
9622c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9632c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        // recompute visible region
9642c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        recomputeVisibleRegions = true;
9652c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    }
9662c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9672c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    ALOGD_IF(DEBUG_RESIZE,
9686905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden                            "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
9692c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
9702c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
9716905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden                            bufWidth, bufHeight, item.mTransform, item.mScalingMode,
9722c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.w, front.active.h,
9732c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.left,
9742c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.top,
9752c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.right,
9762c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.bottom,
9772c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.getWidth(),
9782c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.active.crop.getHeight(),
9792c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.w, front.requested.h,
9802c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.left,
9812c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.top,
9822c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.right,
9832c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.bottom,
9842c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.getWidth(),
9852c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                            front.requested.crop.getHeight());
9862c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
9872c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
9882c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                if (!isFixedSize) {
9892c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    if (front.active.w != bufWidth ||
9902c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        front.active.h != bufHeight) {
9914824d40a35333182c2eb3593511b9bcbecd0a943Mathias Agopian                        // reject this buffer
9924ceff3d5efd27c164788bb2b3f0fd17c691a0204Mathias Agopian                        //ALOGD("rejecting buffer: bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}",
9934ceff3d5efd27c164788bb2b3f0fd17c691a0204Mathias Agopian                        //        bufWidth, bufHeight, front.active.w, front.active.h);
9942c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                        return true;
9952c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                    }
9962c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                }
9972ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian
9982ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                // if the transparent region has changed (this test is
9992ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                // conservative, but that's fine, worst case we're doing
10002ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                // a bit of extra work), we latch the new one and we
10012ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                // trigger a visible-region recompute.
10022ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                if (!front.activeTransparentRegion.isTriviallyEqual(
10032ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                        front.requestedTransparentRegion)) {
10042ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                    front.activeTransparentRegion = front.requestedTransparentRegion;
10056c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian
10066c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    // We also need to update the current state so that
10076c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    // we don't end-up overwriting the drawing state with
10086c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    // this stale current state during the next transaction
10096c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    //
10106c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    // NOTE: We don't need to hold the transaction lock here
10116c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    // because State::active is only accessed from this thread.
10126c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    current.activeTransparentRegion = front.activeTransparentRegion;
10136c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian
10146c67f0fe457f758fc875e1178d8fd5258fd8f6dfMathias Agopian                    // recompute visible region
10152ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                    recomputeVisibleRegions = true;
10162ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian                }
10172ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian
10182c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian                return false;
10192c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian            }
10202c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian        };
10212c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
10222c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
10231eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions);
10242c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian
10251585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r);
10261585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        if (updateResult == BufferQueue::PRESENT_LATER) {
10271585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden            // Producer doesn't want buffer to be displayed yet.  Signal a
10281585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden            // layer update so we check again at the next opportunity.
10291585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden            mFlinger->signalLayerUpdate();
10301585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden            return outDirtyRegion;
10311585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        }
10321585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden
10331585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        // Decrement the queued-frames count.  Signal another event if we
10341585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        // have more frames pending.
10351585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        if (android_atomic_dec(&mQueuedFrames) > 1) {
10361585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden            mFlinger->signalLayerUpdate();
10371585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        }
10381585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden
10391585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        if (updateResult != NO_ERROR) {
1040a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            // something happened!
1041a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            recomputeVisibleRegions = true;
10424fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            return outDirtyRegion;
1043a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        }
1044d343e3d5e3177806205b9452b0b43907e28afd9aMathias Agopian
1045351a513b12622781de9580b3c96fd0a8578b563bJamie Gennis        // update the active buffer
1046bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
1047e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian        if (mActiveBuffer == NULL) {
1048e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian            // this can only happen if the very first buffer was rejected.
10494fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            return outDirtyRegion;
1050e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian        }
1051da9584dc295cc5e6d0b49a97c1e45159249d650bMathias Agopian
10524824d40a35333182c2eb3593511b9bcbecd0a943Mathias Agopian        mRefreshPending = true;
1053702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        mFrameLatencyNeeded = true;
1054e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian        if (oldActiveBuffer == NULL) {
10552c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian             // the first time we receive a buffer, we need to trigger a
10562c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian             // geometry invalidation.
1057ab10c5804c250e7f392c8262f687766edec2e9f4Andy McFadden            recomputeVisibleRegions = true;
10582c8207e9627fe6c7a90e31fae8d71ae49df56845Mathias Agopian         }
1059702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
1060bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
1061bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
1062bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden        const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
1063702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        if ((crop != mCurrentCrop) ||
1064702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            (transform != mCurrentTransform) ||
1065702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            (scalingMode != mCurrentScalingMode))
1066702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        {
1067702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            mCurrentCrop = crop;
1068702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            mCurrentTransform = transform;
1069702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            mCurrentScalingMode = scalingMode;
1070ab10c5804c250e7f392c8262f687766edec2e9f4Andy McFadden            recomputeVisibleRegions = true;
1071702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        }
1072702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
1073702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        if (oldActiveBuffer != NULL) {
1074e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian            uint32_t bufWidth  = mActiveBuffer->getWidth();
1075e31564d8eb0ab67e167a888eccce20f5b4e4ef45Mathias Agopian            uint32_t bufHeight = mActiveBuffer->getHeight();
1076702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            if (bufWidth != uint32_t(oldActiveBuffer->width) ||
1077702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian                bufHeight != uint32_t(oldActiveBuffer->height)) {
1078ab10c5804c250e7f392c8262f687766edec2e9f4Andy McFadden                recomputeVisibleRegions = true;
1079702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            }
1080702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        }
1081702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
1082702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
1083702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        if (oldOpacity != isOpaque()) {
1084702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian            recomputeVisibleRegions = true;
1085702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian        }
1086702634a4dad85cfc292618ac91eda6c00f42b7c5Mathias Agopian
10874fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // FIXME: postedRegion should be dirty & bounds
10881eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        const Layer::State& s(getDrawingState());
10891eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        Region dirtyRegion(Rect(s.active.w, s.active.h));
10901c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
10914fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // transform the dirty region to window-manager space
10921eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian        outDirtyRegion = (s.transform.transform(dirtyRegion));
1093edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
10944fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    return outDirtyRegion;
1095edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1096edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
109713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianuint32_t Layer::getEffectiveUsage(uint32_t usage) const
109813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian{
109913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // TODO: should we do something special if mSecure is set?
110013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mProtectedByApp) {
110113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // need a hardware-protected path to external video sink
110213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        usage |= GraphicBuffer::USAGE_PROTECTED;
110313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
110413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    usage |= GraphicBuffer::USAGE_HW_COMPOSER;
110513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return usage;
110613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
110713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
110813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
110913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    uint32_t orientation = 0;
111013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (!mFlinger->mDebugDisableTransformHint) {
111113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // The transform hint is used to improve performance, but we can
111213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // only have a single transform hint, it cannot
111313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // apply to all displays.
111413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const Transform& planeTransform(hw->getTransform());
111513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        orientation = planeTransform.getOrientation();
111613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        if (orientation & Transform::ROT_INVALID) {
111713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            orientation = 0;
111813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        }
111913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
112013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mSurfaceFlingerConsumer->setTransformHint(orientation);
112113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
112213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
112313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
112413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// debugging
112513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
112613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
11273e25fd8609b100a75721be82d1d499f0ae9083cbMathias Agopianvoid Layer::dump(String8& result, Colorizer& colorizer) const
11281b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian{
11291eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& s(getDrawingState());
113013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
11313e25fd8609b100a75721be82d1d499f0ae9083cbMathias Agopian    colorizer.colorize(result, Colorizer::GREEN);
113274d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    result.appendFormat(
113313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            "+ %s %p (%s)\n",
113413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            getTypeId(), this, getName().string());
11353e25fd8609b100a75721be82d1d499f0ae9083cbMathias Agopian    colorizer.reset(result);
113613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
11372ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian    s.activeTransparentRegion.dump(result, "transparentRegion");
113813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    visibleRegion.dump(result, "visibleRegion");
113913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Client> client(mClientRef.promote());
114013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
114174d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    result.appendFormat(            "      "
114213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), crop=(%4d,%4d,%4d,%4d), "
114313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            "isOpaque=%1d, invalidate=%1d, "
114413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n"
114513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            "      client=%p\n",
114613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            s.layerStack, s.z, s.transform.tx(), s.transform.ty(), s.active.w, s.active.h,
114713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            s.active.crop.left, s.active.crop.top,
114813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            s.active.crop.right, s.active.crop.bottom,
114913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            isOpaque(), contentDirty,
115013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            s.alpha, s.flags,
115113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            s.transform[0][0], s.transform[0][1],
115213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            s.transform[1][0], s.transform[1][1],
115313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            client.get());
11541b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
1155a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    sp<const GraphicBuffer> buf0(mActiveBuffer);
1156a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    uint32_t w0=0, h0=0, s0=0, f0=0;
11571b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian    if (buf0 != 0) {
11581b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian        w0 = buf0->getWidth();
11591b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian        h0 = buf0->getHeight();
11601b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian        s0 = buf0->getStride();
1161a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        f0 = buf0->format;
11621b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian    }
116374d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    result.appendFormat(
11641b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian            "      "
1165ad795baecccf239621cbffa0249c8e855296cae6Mathias Agopian            "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
11666905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden            " queued-frames=%d, mRefreshPending=%d\n",
1167a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            mFormat, w0, h0, s0,f0,
11686905205c8d130b6ea3a813c1b9283492ed183367Andy McFadden            mQueuedFrames, mRefreshPending);
11691b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
1170bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    if (mSurfaceFlingerConsumer != 0) {
117174d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian        mSurfaceFlingerConsumer->dump(result, "            ");
1172bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian    }
1173d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian}
1174d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian
117574d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopianvoid Layer::dumpStats(String8& result) const {
117682dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis    mFrameTracker.dump(result);
117782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian}
117882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
117913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::clearStats() {
118082dbc7429f5f9f2b303b31dc5b9f2bfd1bbe6addJamie Gennis    mFrameTracker.clear();
118125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian}
118225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
11836547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennisvoid Layer::logFrameStats() {
11846547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    mFrameTracker.logAndResetStats(mName);
11856547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis}
11866547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis
118713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
118813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
118913127d8921356dff794250e04208c3ed60b3a3dfMathias AgopianLayer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
119013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer)
119113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    : mFlinger(flinger), mLayer(layer) {
1192b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian}
1193b5b7f260da2c1a2a82e0311e2015d49a82f43667Mathias Agopian
119413127d8921356dff794250e04208c3ed60b3a3dfMathias AgopianLayer::LayerCleaner::~LayerCleaner() {
119513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // destroy client resources
119613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mFlinger->onLayerDestroyed(mLayer);
1197a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian}
1198a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian
1199edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
12003f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian}; // namespace android
1201edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
12023f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#if defined(__gl_h_)
12033f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#error "don't include gl/gl.h in this file"
12043f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#endif
1205edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
12063f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#if defined(__gl2_h_)
12073f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#error "don't include gl2/gl2.h in this file"
12083f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#endif
1209