SurfaceFlinger.cpp revision 4d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6
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 <stdint.h>
20921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <sys/types.h>
21edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <errno.h>
22edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <math.h>
2363f165fd6b86d04be94d4023e845e98560504a96Keun young Park#include <dlfcn.h>
24921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
25921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <EGL/egl.h>
26921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <GLES/gl.h>
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
28edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <cutils/log.h>
29edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <cutils/properties.h>
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
31c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/IPCThreadState.h>
32c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/IServiceManager.h>
337303c6bf1a8b00a0e7d8165d774a1f259b4ccda9Mathias Agopian#include <binder/MemoryHeapBase.h>
3499b49840d309727678b77403d6cc9f920111623fMathias Agopian#include <binder/PermissionCache.h>
357303c6bf1a8b00a0e7d8165d774a1f259b4ccda9Mathias Agopian
36c666cae2d5995097ec49a87e375e2afdd92802b7Mathias Agopian#include <ui/DisplayInfo.h>
37c666cae2d5995097ec49a87e375e2afdd92802b7Mathias Agopian
38921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <gui/BitTube.h>
391a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <gui/BufferQueue.h>
404803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#include <gui/GuiConfig.h>
411a4d883dcc1725892bfb5c28dec255a233186524Jamie Gennis#include <gui/IDisplayEventConnection.h>
42e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian#include <gui/Surface.h>
43392edd88cb63d71a21a86a02cf9c56ac97637128Jamie Gennis#include <gui/GraphicBufferAlloc.h>
44921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
45921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <ui/GraphicBufferAllocator.h>
46921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <ui/PixelFormat.h>
474803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#include <ui/UiConfig.h>
48d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
49cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian#include <utils/misc.h>
50edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/String8.h>
51edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/String16.h>
52edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/StopWatch.h>
531c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h>
54edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
55921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <private/android_filesystem_config.h>
56edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
57edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "clz.h"
5890ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include "DdmConnection.h"
590f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian#include "DisplayDevice.h"
60db403e8ff0d7727015e1a5009bab20eb7ec205bcMathias Agopian#include "Client.h"
61d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian#include "EventThread.h"
621f7bec634f19c123410a5155c8d282e177c01930Mathias Agopian#include "GLExtensions.h"
63edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "Layer.h"
64edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "LayerDim.h"
65edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "SurfaceFlinger.h"
66edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
67a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian#include "DisplayHardware/FramebufferSurface.h"
68a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "DisplayHardware/HWComposer.h"
69edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
70a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
71bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian#define EGL_VERSION_HW_ANDROID  0x3143
72bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian
73edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define DISPLAY_COUNT       1
74edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
75edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
76edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
77edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7899b49840d309727678b77403d6cc9f920111623fMathias Agopianconst String16 sHardwareTest("android.permission.HARDWARE_TEST");
7999b49840d309727678b77403d6cc9f920111623fMathias Agopianconst String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
8099b49840d309727678b77403d6cc9f920111623fMathias Agopianconst String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
8199b49840d309727678b77403d6cc9f920111623fMathias Agopianconst String16 sDump("android.permission.DUMP");
8299b49840d309727678b77403d6cc9f920111623fMathias Agopian
8399b49840d309727678b77403d6cc9f920111623fMathias Agopian// ---------------------------------------------------------------------------
8499b49840d309727678b77403d6cc9f920111623fMathias Agopian
85edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectSurfaceFlinger::SurfaceFlinger()
86edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    :   BnSurfaceComposer(), Thread(false),
87edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mTransactionFlags(0),
882d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        mTransactionPending(false),
892d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        mAnimTransactionPending(false),
90076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        mLayersRemoved(false),
9152bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        mRepaintEverything(0),
92edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mBootTime(systemTime()),
93edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mVisibleRegionsDirty(false),
94a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mHwWorkListDirty(false),
954b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        mAnimCompositionPending(false),
96edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mDebugRegion(0),
978afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian        mDebugDDMS(0),
9873d3ba9e50be1014aa21ec4bbdc874be394accb4Mathias Agopian        mDebugDisableHWC(0),
99a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian        mDebugDisableTransformHint(0),
1009795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        mDebugInSwapBuffers(0),
1019795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        mLastSwapBufferTime(0),
1029795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        mDebugInTransaction(0),
1039795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        mLastTransactionTime(0),
1045f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        mBootFinished(false)
105edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
106a19954ab377b46dbcb9cbe8a6ab6d458f2e32bcaSteve Block    ALOGI("SurfaceFlinger is starting");
107edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
108edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // debugging stuff...
109edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    char value[PROPERTY_VALUE_MAX];
1108afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian
111edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    property_get("debug.sf.showupdates", value, "0");
112edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mDebugRegion = atoi(value);
1138afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian
1148afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian    property_get("debug.sf.ddms", value, "0");
1158afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian    mDebugDDMS = atoi(value);
1168afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian    if (mDebugDDMS) {
11763f165fd6b86d04be94d4023e845e98560504a96Keun young Park        if (!startDdmConnection()) {
11863f165fd6b86d04be94d4023e845e98560504a96Keun young Park            // start failed, and DDMS debugging not enabled
11963f165fd6b86d04be94d4023e845e98560504a96Keun young Park            mDebugDDMS = 0;
12063f165fd6b86d04be94d4023e845e98560504a96Keun young Park        }
1218afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian    }
122c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian    ALOGI_IF(mDebugRegion, "showupdates enabled");
123c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian    ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
124edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
125edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
12699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianvoid SurfaceFlinger::onFirstRef()
12799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian{
12899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mEventQueue.init(this);
12999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
13099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
13199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
13299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    // Wait for the main thread to be done with its initialization
13399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mReadyToRunBarrier.wait();
13499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
13599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
13699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
137edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectSurfaceFlinger::~SurfaceFlinger()
138edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
139a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
140a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
141a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglTerminate(display);
142edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
143edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
14499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianvoid SurfaceFlinger::binderDied(const wp<IBinder>& who)
14599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian{
14699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    // the window manager died on us. prepare its eulogy.
14799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
14813a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    // restore initial conditions (default device unblank, etc)
14913a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    initializeDisplays();
15099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
15199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    // restart the boot-animation
152a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    startBootAnim();
15399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
15499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
1557e27f05739c8a2655cf0f7faea35614ce0a50278Mathias Agopiansp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
156edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
15796f0819f81293076e652792794a961543e6750d7Mathias Agopian    sp<ISurfaceComposerClient> bclient;
15896f0819f81293076e652792794a961543e6750d7Mathias Agopian    sp<Client> client(new Client(this));
15996f0819f81293076e652792794a961543e6750d7Mathias Agopian    status_t err = client->initCheck();
16096f0819f81293076e652792794a961543e6750d7Mathias Agopian    if (err == NO_ERROR) {
16196f0819f81293076e652792794a961543e6750d7Mathias Agopian        bclient = client;
162edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
163edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return bclient;
164edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
165edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
166dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennissp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
167dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis        bool secure)
168e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian{
169e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    class DisplayToken : public BBinder {
170e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        sp<SurfaceFlinger> flinger;
171e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        virtual ~DisplayToken() {
172e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian             // no more references, this display must be terminated
173e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian             Mutex::Autolock _l(flinger->mStateLock);
174e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian             flinger->mCurrentState.displays.removeItem(this);
175e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian             flinger->setTransactionFlags(eDisplayTransactionNeeded);
176e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian         }
177e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian     public:
178e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        DisplayToken(const sp<SurfaceFlinger>& flinger)
179e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            : flinger(flinger) {
180e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
181e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    };
182e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
183e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    sp<BBinder> token = new DisplayToken(this);
184e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
185e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    Mutex::Autolock _l(mStateLock);
1863ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
1878dfa92fef9759a881e96ee58d59875d35023aab9Andy McFadden    info.displayName = displayName;
188dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    info.isSecure = secure;
189e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    mCurrentState.displays.add(token, info);
190e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
191e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    return token;
192e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
193e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
194692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hallvoid SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
195692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    ALOGW_IF(mBuiltinDisplays[type],
196692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall            "Overwriting display token for display type %d", type);
197692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    mBuiltinDisplays[type] = new BBinder();
198692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    DisplayDeviceState info(type);
199692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    // All non-virtual displays are currently considered secure.
200692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    info.isSecure = true;
201692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    mCurrentState.displays.add(mBuiltinDisplays[type], info);
202692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall}
203692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall
204e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopiansp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
2053ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
206e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
207e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        return NULL;
208e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    }
209692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    return mBuiltinDisplays[id];
210e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
211e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
2129a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennissp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
2139a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis{
2149a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
2159a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    return gba;
2169a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis}
217b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian
218edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid SurfaceFlinger::bootFinished()
219edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
220edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const nsecs_t now = systemTime();
221edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const nsecs_t duration = now - mBootTime;
222a19954ab377b46dbcb9cbe8a6ab6d458f2e32bcaSteve Block    ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
2233330b203039dea366d4981db1408a460134b2d2cMathias Agopian    mBootFinished = true;
2241f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian
2251f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian    // wait patiently for the window manager death
2261f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian    const String16 name("window");
2271f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian    sp<IBinder> window(defaultServiceManager()->getService(name));
2281f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian    if (window != 0) {
229921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
2301f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian    }
2311f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian
2321f339ff3875afad128a8e16ee6395c5fad295826Mathias Agopian    // stop boot animation
233a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    // formerly we would just kill the process, but we now ask it to exit so it
234a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    // can choose where to stop the animation.
235a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    property_set("service.bootanim.exit", "1");
236edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
237edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
238921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopianvoid SurfaceFlinger::deleteTextureAsync(GLuint texture) {
239921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    class MessageDestroyGLTexture : public MessageBase {
240921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        GLuint texture;
241921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    public:
242921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        MessageDestroyGLTexture(GLuint texture)
243921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian            : texture(texture) {
244921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        }
245921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        virtual bool handler() {
246921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian            glDeleteTextures(1, &texture);
247921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian            return true;
248921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        }
249921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    };
250921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    postMessageAsync(new MessageDestroyGLTexture(texture));
251921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian}
252921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
253722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopianstatus_t SurfaceFlinger::selectConfigForAttribute(
254a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian        EGLDisplay dpy,
255a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian        EGLint const* attrs,
256722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        EGLint attribute, EGLint wanted,
257a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian        EGLConfig* outConfig)
258a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian{
259a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLConfig config = NULL;
260a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLint numConfigs = -1, n=0;
261a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglGetConfigs(dpy, NULL, 0, &numConfigs);
262a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLConfig* const configs = new EGLConfig[numConfigs];
263a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
264cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
265722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    if (n) {
266722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        if (attribute != EGL_NONE) {
267722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            for (int i=0 ; i<n ; i++) {
268722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                EGLint value = 0;
269722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                eglGetConfigAttrib(dpy, configs[i], attribute, &value);
270722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                if (wanted == value) {
271722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                    *outConfig = configs[i];
272722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                    delete [] configs;
273722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                    return NO_ERROR;
274722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                }
275722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            }
276722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        } else {
277722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            // just pick the first one
278722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            *outConfig = configs[0];
279a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            delete [] configs;
280a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            return NO_ERROR;
281a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian        }
282a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    }
283a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    delete [] configs;
284a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    return NAME_NOT_FOUND;
285edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
286edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
287722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopianclass EGLAttributeVector {
288722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    struct Attribute;
289722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    class Adder;
290722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    friend class Adder;
291722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    KeyedVector<Attribute, EGLint> mList;
292722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    struct Attribute {
293722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        Attribute() {};
294722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        Attribute(EGLint v) : v(v) { }
295722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        EGLint v;
296722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        bool operator < (const Attribute& other) const {
297722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            // this places EGL_NONE at the end
298722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            EGLint lhs(v);
299722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            EGLint rhs(other.v);
300722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
301722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
302722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            return lhs < rhs;
303722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        }
304722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    };
305722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    class Adder {
306722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        friend class EGLAttributeVector;
307722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        EGLAttributeVector& v;
308722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        EGLint attribute;
309722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        Adder(EGLAttributeVector& v, EGLint attribute)
310722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            : v(v), attribute(attribute) {
311722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        }
312722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    public:
313722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        void operator = (EGLint value) {
314722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            if (attribute != EGL_NONE) {
315722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian                v.mList.add(attribute, value);
316722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            }
317722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        }
318722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        operator EGLint () const { return v.mList[attribute]; }
319722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    };
320722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopianpublic:
321722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    EGLAttributeVector() {
322722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        mList.add(EGL_NONE, EGL_NONE);
323722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    }
324722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    void remove(EGLint attribute) {
325722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        if (attribute != EGL_NONE) {
326722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            mList.removeItem(attribute);
327722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        }
328722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    }
329722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    Adder operator [] (EGLint attribute) {
330722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        return Adder(*this, attribute);
331722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    }
332722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    EGLint operator [] (EGLint attribute) const {
333722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian       return mList[attribute];
334722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    }
335722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    // cast-operator to (EGLint const*)
336722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    operator EGLint const* () const { return &mList.keyAt(0).v; }
337722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian};
338722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian
339a49126087b4494f4ef50873f3a3f6727265f6621Mathias AgopianEGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
340a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
341a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    // it is to be used with WIFI displays
342a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLConfig config;
343a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLint dummy;
344a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    status_t err;
345da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
346722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    EGLAttributeVector attribs;
347722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs[EGL_SURFACE_TYPE]               = EGL_WINDOW_BIT;
348722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs[EGL_RECORDABLE_ANDROID]         = EGL_TRUE;
349722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
350722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs[EGL_RED_SIZE]                   = 8;
351722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs[EGL_GREEN_SIZE]                 = 8;
352722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs[EGL_BLUE_SIZE]                  = 8;
353722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian
354722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
355722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    if (!err)
356722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian        goto success;
357722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian
358722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
359722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
360722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
361722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    err = selectConfigForAttribute(display, attribs,
362722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
363f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    if (!err)
364f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall        goto success;
365f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall
366f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    // maybe we failed because of EGL_RECORDABLE_ANDROID
367f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
368722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs.remove(EGL_RECORDABLE_ANDROID);
369722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    err = selectConfigForAttribute(display, attribs,
370722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
371f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    if (!err)
372f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall        goto success;
373f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall
374f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    // allow less than 24-bit color; the non-gpu-accelerated emulator only
375f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    // supports 16-bit color
376f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
377722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs.remove(EGL_RED_SIZE);
378722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs.remove(EGL_GREEN_SIZE);
379722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    attribs.remove(EGL_BLUE_SIZE);
380722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian    err = selectConfigForAttribute(display, attribs,
381722b98f9dfe8f04de8734630198b99a6cd024118Mathias Agopian            EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
382f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    if (!err)
383f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall        goto success;
384f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall
385f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    // this EGL is too lame for Android
386f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    ALOGE("no suitable EGLConfig found, giving up");
387f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall
388f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    return 0;
389f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall
390f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hallsuccess:
391f21cffa7d707dad10b2974c58c91482f7ca689acJesse Hall    if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
392a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian        ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
393a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    return config;
394a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian}
395edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
396a49126087b4494f4ef50873f3a3f6727265f6621Mathias AgopianEGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
397a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    // Also create our EGLContext
398a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLint contextAttributes[] = {
399a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian#ifdef EGL_IMG_context_priority
400a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian#ifdef HAS_CONTEXT_PRIORITY
401a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian#warning "using EGL_IMG_context_priority"
402a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
403a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian#endif
404a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian#endif
405a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            EGL_NONE, EGL_NONE
406a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    };
407a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
408a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
409a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    return ctxt;
410a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian}
411edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
412cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianvoid SurfaceFlinger::initializeGL(EGLDisplay display) {
413a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    GLExtensions& extensions(GLExtensions::getInstance());
414a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    extensions.initWithGLStrings(
415a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            glGetString(GL_VENDOR),
416a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            glGetString(GL_RENDERER),
417a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            glGetString(GL_VERSION),
418a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            glGetString(GL_EXTENSIONS),
419a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            eglQueryString(display, EGL_VENDOR),
420a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            eglQueryString(display, EGL_VERSION),
421a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            eglQueryString(display, EGL_EXTENSIONS));
4228b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber
423a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
424a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
4257303c6bf1a8b00a0e7d8165d774a1f259b4ccda9Mathias Agopian
426edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
4278b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber    glPixelStorei(GL_PACK_ALIGNMENT, 4);
428edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glEnableClientState(GL_VERTEX_ARRAY);
429edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glShadeModel(GL_FLAT);
430edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glDisable(GL_DITHER);
431edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glDisable(GL_CULL_FACE);
432edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
433a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    struct pack565 {
434a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian        inline uint16_t operator() (int r, int g, int b) const {
435a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            return (r<<11)|(g<<5)|b;
436a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian        }
437a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    } pack565;
438a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian
4399575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
4409575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    glGenTextures(1, &mProtectedTexName);
4419575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
4429575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4439575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4449575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
4459575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
4469575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
4479575f60722f7a4f54384fe0be6938a8de48dc23aJamie Gennis            GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
448edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
449a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    // print some debugging info
450a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    EGLint r,g,b,a;
451a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE,   &r);
452a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
453a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE,  &b);
454a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
455a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("EGL informations:");
456a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("vendor    : %s", extensions.getEglVendor());
457a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("version   : %s", extensions.getEglVersion());
458a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("extensions: %s", extensions.getEglExtension());
459a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
460a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
461a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("OpenGL ES informations:");
462a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("vendor    : %s", extensions.getVendor());
463a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("renderer  : %s", extensions.getRenderer());
464a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("version   : %s", extensions.getVersion());
465a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("extensions: %s", extensions.getExtension());
466a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
467a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
468a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian}
469a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian
470a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopianstatus_t SurfaceFlinger::readyToRun()
471a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian{
472a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    ALOGI(  "SurfaceFlinger's main thread ready to run. "
473a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            "Initializing graphics H/W...");
474a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian
475692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    Mutex::Autolock _l(mStateLock);
476692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall
477b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // initialize EGL for the default display
47834a09ba1efd706323a15633da5044b352988eb5fJesse Hall    mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
47934a09ba1efd706323a15633da5044b352988eb5fJesse Hall    eglInitialize(mEGLDisplay, NULL, NULL);
480a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian
481b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Initialize the H/W composer object.  There may or may not be an
482b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // actual hardware composer underneath.
483b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    mHwc = new HWComposer(this,
484b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            *static_cast<HWComposer::EventHandler *>(this));
485b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
486a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    // initialize the config and context
487cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    EGLint format = mHwc->getVisualID();
48834a09ba1efd706323a15633da5044b352988eb5fJesse Hall    mEGLConfig  = selectEGLConfig(mEGLDisplay, format);
48934a09ba1efd706323a15633da5044b352988eb5fJesse Hall    mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
490a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian
491da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
492da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            "couldn't create EGLContext");
493da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
494cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    // initialize our non-virtual displays
4953ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
496f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
497f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        // set-up the displays that are already connected
4989e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
499dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis            // All non-virtual displays are currently considered secure.
500dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis            bool isSecure = true;
501692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall            createBuiltinDisplayLocked(type);
502692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall            wp<IBinder> token = mBuiltinDisplays[i];
503692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall
504f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian            sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
505e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian            sp<Surface> stc = new Surface(
5062adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden                        static_cast< sp<IGraphicBufferProducer> >(fbs->getBufferQueue()));
507f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian            sp<DisplayDevice> hw = new DisplayDevice(this,
508dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis                    type, isSecure, token, stc, fbs, mEGLConfig);
509f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian            if (i > DisplayDevice::DISPLAY_PRIMARY) {
510c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden                // FIXME: currently we don't get blank/unblank requests
511f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian                // for displays other than the main display, so we always
512f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian                // assume a connected display is unblanked.
513c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden                ALOGD("marking display %d as acquired/unblanked", i);
514f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian                hw->acquireScreen();
515f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian            }
516f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian            mDisplays.add(token, hw);
517f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        }
518e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    }
519cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
520f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    //  we need a GL context current in a few places, when initializing
521f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    //  OpenGL ES (see below), or creating a layer,
522f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    //  or when a texture is (asynchronously) destroyed, and for that
523f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    //  we need a valid surface, so it's convenient to use the main display
524f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    //  for that.
525db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    sp<const DisplayDevice> hw(getDefaultDisplayDevice());
526cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
527a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    //  initialize OpenGL ES
528cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
529cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    initializeGL(mEGLDisplay);
530d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
531028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    // start the EventThread
532028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    mEventThread = new EventThread(this);
533028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    mEventQueue.setEventThread(mEventThread);
534028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian
53592a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    // initialize our drawing state
53692a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    mDrawingState = mCurrentState;
5378630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
538cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
539a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    // We're now ready to accept clients...
540d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian    mReadyToRunBarrier.open();
541d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
54213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    // set initial conditions (e.g. unblank default device)
54313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    initializeDisplays();
54413a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
545a1ecca920e15ce04fe56ebf4f1b52ba711f9eb2dMathias Agopian    // start boot animation
546a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    startBootAnim();
5478b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber
548edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return NO_ERROR;
549edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
550edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
5513ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopianint32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
5523ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
5533ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian            type : mHwc->allocateDisplayId();
5543ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian}
5553ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian
556a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopianvoid SurfaceFlinger::startBootAnim() {
557a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    // start boot animation
558a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    property_set("service.bootanim.exit", "0");
559a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian    property_set("ctl.start", "bootanim");
560a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian}
561a67e418e1fda219f6cc0a7e420bcf5cc4f9fe710Mathias Agopian
562a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopianuint32_t SurfaceFlinger::getMaxTextureSize() const {
563a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    return mMaxTextureSize;
564a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian}
565a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian
566a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopianuint32_t SurfaceFlinger::getMaxViewportDims() const {
567a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian    return mMaxViewportDims[0] < mMaxViewportDims[1] ?
568a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian            mMaxViewportDims[0] : mMaxViewportDims[1];
569a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian}
570a49126087b4494f4ef50873f3a3f6727265f6621Mathias Agopian
571edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
572d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
573582270d69db94286a248bd829f1ae6f910d45124Jamie Gennisbool SurfaceFlinger::authenticateSurfaceTexture(
5742adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden        const sp<IGraphicBufferProducer>& bufferProducer) const {
575134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    Mutex::Autolock _l(mStateLock);
5762adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
577134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
578882e3a39ed770b335a203e233b57127fde1c839eAndy McFadden    // We want to determine whether the IGraphicBufferProducer was created by
579882e3a39ed770b335a203e233b57127fde1c839eAndy McFadden    // SurfaceFlinger.  Check to see if we can find it in the layer list.
580134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
581134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    size_t count = currentLayers.size();
582134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    for (size_t i=0 ; i<count ; i++) {
58313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
5844d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        // Get the consumer interface of SurfaceFlingerConsumer's
585b79f61d41ef053bee1087ec612896c59f95f9686Mathias Agopian        // BufferQueue.  If it's the same Binder object as the graphic
586b79f61d41ef053bee1087ec612896c59f95f9686Mathias Agopian        // buffer producer interface, return success.
5874d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder> lbcBinder = layer->getBufferQueue()->asBinder();
588b79f61d41ef053bee1087ec612896c59f95f9686Mathias Agopian        if (lbcBinder == surfaceTextureBinder) {
589b79f61d41ef053bee1087ec612896c59f95f9686Mathias Agopian            return true;
590134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis        }
591134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    }
592134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
593134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    // Check the layers in the purgatory.  This check is here so that if a
5942adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // GLConsumer gets destroyed before all the clients are done using it,
595582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis    // the error will not be reported as "surface XYZ is not authenticated", but
596134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    // will instead fail later on when the client tries to use the surface,
597134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    // which should be reported as "surface XYZ returned an -ENODEV".  The
598134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    // purgatorized layers are no less authentic than the visible ones, so this
599134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    // should not cause any harm.
600134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    size_t purgatorySize =  mLayerPurgatory.size();
601134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    for (size_t i=0 ; i<purgatorySize ; i++) {
60213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(mLayerPurgatory.itemAt(i));
6034d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder> lbcBinder = layer->getBufferQueue()->asBinder();
604b79f61d41ef053bee1087ec612896c59f95f9686Mathias Agopian        if (lbcBinder == surfaceTextureBinder) {
605b79f61d41ef053bee1087ec612896c59f95f9686Mathias Agopian            return true;
606134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis        }
607134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    }
608134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
609134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis    return false;
610134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis}
611134f0422866e8985188ed10dfbdcb8e6c34b87f7Jamie Gennis
6129d4e3d2f42e93e2d12bacabe97d307d30c3c20ddJeff Brownstatus_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
613692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    int32_t type = NAME_NOT_FOUND;
6141604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
615692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall        if (display == mBuiltinDisplays[i]) {
6161604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            type = i;
6171604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            break;
6181604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
6191604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    }
6201604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
6211604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (type < 0) {
6221604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return type;
623c666cae2d5995097ec49a87e375e2afdd92802b7Mathias Agopian    }
6248b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
6258b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    const HWComposer& hwc(getHwComposer());
6261604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    float xdpi = hwc.getDpiX(type);
6271604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    float ydpi = hwc.getDpiY(type);
6288b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
6298b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    // TODO: Not sure if display density should handled by SF any longer
6308b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    class Density {
6318b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        static int getDensityFromProperty(char const* propName) {
6328b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            char property[PROPERTY_VALUE_MAX];
6338b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            int density = 0;
6348b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            if (property_get(propName, property, NULL) > 0) {
6358b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian                density = atoi(property);
6368b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            }
6378b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            return density;
6388b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        }
6398b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    public:
6408b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        static int getEmuDensity() {
6418b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            return getDensityFromProperty("qemu.sf.lcd_density"); }
6428b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        static int getBuildDensity()  {
6438b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            return getDensityFromProperty("ro.sf.lcd_density"); }
6448b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    };
6451604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
6461604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (type == DisplayDevice::DISPLAY_PRIMARY) {
6471604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // The density of the device is provided by a build property
6481604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        float density = Density::getBuildDensity() / 160.0f;
6491604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        if (density == 0) {
6501604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            // the build doesn't provide a density -- this is wrong!
6511604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            // use xdpi instead
6521604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            ALOGE("ro.sf.lcd_density must be defined as a build property");
6531604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            density = xdpi / 160.0f;
6541604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
6551604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        if (Density::getEmuDensity()) {
6561604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            // if "qemu.sf.lcd_density" is specified, it overrides everything
6571604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            xdpi = ydpi = density = Density::getEmuDensity();
6581604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            density /= 160.0f;
6591604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
6601604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        info->density = density;
6611604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
6621604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // TODO: this needs to go away (currently needed only by webkit)
6631604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        sp<const DisplayDevice> hw(getDefaultDisplayDevice());
6641604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        info->orientation = hw->getOrientation();
6651604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
6661604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    } else {
6671604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // TODO: where should this value come from?
6681604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        static const int TV_DENSITY = 213;
6691604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        info->density = TV_DENSITY / 160.0f;
6701604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        info->orientation = 0;
6718b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    }
6728b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
6731604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    info->w = hwc.getWidth(type);
6741604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    info->h = hwc.getHeight(type);
6758b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    info->xdpi = xdpi;
6768b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    info->ydpi = ydpi;
6771604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    info->fps = float(1e9 / hwc.getRefreshPeriod(type));
678dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis
679dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    // All non-virtual displays are currently considered secure.
680dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis    info->secure = true;
681dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis
682888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    return NO_ERROR;
683c666cae2d5995097ec49a87e375e2afdd92802b7Mathias Agopian}
684c666cae2d5995097ec49a87e375e2afdd92802b7Mathias Agopian
685d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian// ----------------------------------------------------------------------------
686d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
687d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopiansp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
6888aedd4737d6ce8548d2fd5def65b1e1737283821Mathias Agopian    return mEventThread->createEventConnection();
689bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian}
690bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian
691edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
69299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
69399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianvoid SurfaceFlinger::waitForEvent() {
69499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mEventQueue.waitMessage();
69599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
69699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
69799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianvoid SurfaceFlinger::signalTransaction() {
69899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mEventQueue.invalidate();
69999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
70099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
70199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianvoid SurfaceFlinger::signalLayerUpdate() {
70299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mEventQueue.invalidate();
70399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
70499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
70599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianvoid SurfaceFlinger::signalRefresh() {
70699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    mEventQueue.refresh();
70799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
70899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
70999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianstatus_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
71099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        nsecs_t reltime, uint32_t flags) {
71199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    return mEventQueue.postMessage(msg, reltime);
71299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
71399ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
71499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopianstatus_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
71599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        nsecs_t reltime, uint32_t flags) {
71699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    status_t res = mEventQueue.postMessage(msg, reltime);
71799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    if (res == NO_ERROR) {
71899ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        msg->wait();
71999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    }
72099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    return res;
72199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
722edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7234fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianbool SurfaceFlinger::threadLoop() {
724edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    waitForEvent();
72599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    return true;
72699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian}
727edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7283ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopianvoid SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
72943601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    if (mEventThread == NULL) {
73043601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        // This is a temporary workaround for b/7145521.  A non-null pointer
73143601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        // does not mean EventThread has finished initializing, so this
73243601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        // is not a correct fix.
73343601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        ALOGW("WARNING: EventThread not started, ignoring vsync");
73443601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        return;
73543601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    }
7363ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
7373ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian        // we should only receive DisplayDevice::DisplayType from the vsync callback
738148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        mEventThread->onVSyncReceived(type, timestamp);
739148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    }
740148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian}
741148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian
742148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopianvoid SurfaceFlinger::onHotplugReceived(int type, bool connected) {
743148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    if (mEventThread == NULL) {
744148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        // This is a temporary workaround for b/7145521.  A non-null pointer
745148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        // does not mean EventThread has finished initializing, so this
746148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        // is not a correct fix.
747148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        ALOGW("WARNING: EventThread not started, ignoring hotplug");
748148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian        return;
749148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    }
7509e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian
751148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
7529e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        Mutex::Autolock _l(mStateLock);
753692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall        if (connected) {
754692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall            createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
7559e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        } else {
756692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall            mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
757692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall            mBuiltinDisplays[type].clear();
7589e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        }
7599e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        setTransactionFlags(eDisplayTransactionNeeded);
7609e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian
7619e9689c11148521d2c16a121a0b87b062be0714cAndy McFadden        // Defer EventThread notification until SF has updated mDisplays.
7623ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    }
7638630320433bd15aca239522e54e711ef6372ab07Mathias Agopian}
7648630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
76581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid SurfaceFlinger::eventControl(int disp, int event, int enabled) {
76681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    getHwComposer().eventControl(disp, event, enabled);
7678630320433bd15aca239522e54e711ef6372ab07Mathias Agopian}
7688630320433bd15aca239522e54e711ef6372ab07Mathias Agopian
7694fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid SurfaceFlinger::onMessageReceived(int32_t what) {
7701c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
77199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    switch (what) {
7724fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    case MessageQueue::INVALIDATE:
7734fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        handleMessageTransaction();
7744fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        handleMessageInvalidate();
7754fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        signalRefresh();
7764fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        break;
7774fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    case MessageQueue::REFRESH:
7784fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        handleMessageRefresh();
7794fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        break;
7804fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    }
7814fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian}
782edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7834fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid SurfaceFlinger::handleMessageTransaction() {
784e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
7854fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    if (transactionFlags) {
78687baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        handleTransaction(transactionFlags);
7874fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    }
7884fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian}
789edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7904fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid SurfaceFlinger::handleMessageInvalidate() {
791cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    ATRACE_CALL();
79287baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    handlePageFlip();
7934fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian}
7943a3cad30c40c8eb87671262a9fe7f0e214b6a934Mathias Agopian
7954fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid SurfaceFlinger::handleMessageRefresh() {
796cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    ATRACE_CALL();
797cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    preComposition();
798cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    rebuildLayerStacks();
799cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    setUpHWComposer();
800cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    doDebugFlashRegions();
801cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    doComposition();
802cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    postComposition();
803cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian}
804cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
805cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::doDebugFlashRegions()
806cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian{
807cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    // is debugging enabled
808cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    if (CC_LIKELY(!mDebugRegion))
809cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        return;
810cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
811cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const bool repaintEverything = mRepaintEverything;
812cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
813cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        const sp<DisplayDevice>& hw(mDisplays[dpy]);
814cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        if (hw->canDraw()) {
815cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            // transform the dirty region into this screen's coordinate space
816cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
817cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            if (!dirtyRegion.isEmpty()) {
818cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                // redraw the whole screen
819cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                doComposeSurfaces(hw, Region(hw->bounds()));
820cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
821cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                // and draw the dirty region
822cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                glDisable(GL_TEXTURE_EXTERNAL_OES);
823cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                glDisable(GL_TEXTURE_2D);
824cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                glDisable(GL_BLEND);
825cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                glColor4f(1, 0, 1, 1);
826cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                const int32_t height = hw->getHeight();
827cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                Region::const_iterator it = dirtyRegion.begin();
828cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                Region::const_iterator const end = dirtyRegion.end();
829cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                while (it != end) {
830cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                    const Rect& r = *it++;
831cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                    GLfloat vertices[][2] = {
8328f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                            { (GLfloat) r.left,  (GLfloat) (height - r.top) },
8338f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                            { (GLfloat) r.left,  (GLfloat) (height - r.bottom) },
8348f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                            { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
8358f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                            { (GLfloat) r.right, (GLfloat) (height - r.top) }
836cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                    };
837cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                    glVertexPointer(2, GL_FLOAT, 0, vertices);
838cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
839cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                }
840cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                hw->compositionComplete();
841da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                hw->swapBuffers(getHwComposer());
842cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            }
843cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        }
844cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    }
845cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
846cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    postFramebuffer();
847cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
848cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    if (mDebugRegion > 1) {
849cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        usleep(mDebugRegion * 1000);
850cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    }
851bb53b0e4b97634bc31808965f81b3ab4193d0e84Mathias Agopian
852bb53b0e4b97634bc31808965f81b3ab4193d0e84Mathias Agopian    HWComposer& hwc(getHwComposer());
853bb53b0e4b97634bc31808965f81b3ab4193d0e84Mathias Agopian    if (hwc.initCheck() == NO_ERROR) {
854bb53b0e4b97634bc31808965f81b3ab4193d0e84Mathias Agopian        status_t err = hwc.prepare();
855bb53b0e4b97634bc31808965f81b3ab4193d0e84Mathias Agopian        ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
856bb53b0e4b97634bc31808965f81b3ab4193d0e84Mathias Agopian    }
857cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian}
858cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
859cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::preComposition()
860cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian{
861cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    bool needExtraInvalidate = false;
862cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
863cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const size_t count = currentLayers.size();
864cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    for (size_t i=0 ; i<count ; i++) {
865cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        if (currentLayers[i]->onPreComposition()) {
866cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            needExtraInvalidate = true;
867cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        }
868cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    }
869cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    if (needExtraInvalidate) {
870cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        signalLayerUpdate();
871cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    }
872cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian}
873a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
874cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::postComposition()
875cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian{
876cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
877cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const size_t count = currentLayers.size();
878cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    for (size_t i=0 ; i<count ; i++) {
879cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        currentLayers[i]->onPostComposition();
880cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    }
8814b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
8824b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    if (mAnimCompositionPending) {
8834b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        mAnimCompositionPending = false;
8844b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
8854b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        const HWComposer& hwc = getHwComposer();
8864b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
887a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall        if (presentFence->isValid()) {
8884b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            mAnimFrameTracker.setActualPresentFence(presentFence);
8894b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        } else {
8904b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            // The HWC doesn't support present fences, so use the refresh
8914b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            // timestamp instead.
8924b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
8934b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            mAnimFrameTracker.setActualPresentTime(presentTime);
8944b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        }
8954b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        mAnimFrameTracker.advanceFrame();
8964b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    }
897cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian}
898cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian
899cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::rebuildLayerStacks() {
900cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    // rebuild the visible layer list per screen
90152bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    if (CC_UNLIKELY(mVisibleRegionsDirty)) {
902cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        ATRACE_CALL();
90387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        mVisibleRegionsDirty = false;
90487baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        invalidateHwcGeometry();
905ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian
90687baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
90792a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
908ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian            Region opaqueRegion;
909ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian            Region dirtyRegion;
91013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            Vector< sp<Layer> > layersSortedByZ;
9114297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            const sp<DisplayDevice>& hw(mDisplays[dpy]);
9127e7ed7f77a3963a3542bde529d2797a239e2798bMathias Agopian            const Transform& tr(hw->getTransform());
9137e7ed7f77a3963a3542bde529d2797a239e2798bMathias Agopian            const Rect bounds(hw->getBounds());
914ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian            if (hw->canDraw()) {
915ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                SurfaceFlinger::computeVisibleRegions(currentLayers,
916ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                        hw->getLayerStack(), dirtyRegion, opaqueRegion);
9177e7ed7f77a3963a3542bde529d2797a239e2798bMathias Agopian
918ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                const size_t count = currentLayers.size();
919ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                for (size_t i=0 ; i<count ; i++) {
92013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                    const sp<Layer>& layer(currentLayers[i]);
921ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                    const Layer::State& s(layer->drawingState());
922ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                    if (s.layerStack == hw->getLayerStack()) {
923a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                        Region drawRegion(tr.transform(
924a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                                layer->visibleNonTransparentRegion));
925a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                        drawRegion.andSelf(bounds);
926a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                        if (!drawRegion.isEmpty()) {
927ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                            layersSortedByZ.add(layer);
928ce3a0a541c3dde1330551bd7a048cd9d92335c00Mathias Agopian                        }
92987baae104a3e4c2059990b01c393476065c558b0Mathias Agopian                    }
93087baae104a3e4c2059990b01c393476065c558b0Mathias Agopian                }
9313b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian            }
9324297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            hw->setVisibleLayersSortedByZ(layersSortedByZ);
9337e7ed7f77a3963a3542bde529d2797a239e2798bMathias Agopian            hw->undefinedRegion.set(bounds);
9347e7ed7f77a3963a3542bde529d2797a239e2798bMathias Agopian            hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
9357e7ed7f77a3963a3542bde529d2797a239e2798bMathias Agopian            hw->dirtyRegion.orSelf(dirtyRegion);
9363b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian        }
9373b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian    }
938cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian}
9393b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian
940cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::setUpHWComposer() {
94152bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    HWComposer& hwc(getHwComposer());
94252bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    if (hwc.initCheck() == NO_ERROR) {
94352bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        // build the h/w work list
944a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis        if (CC_UNLIKELY(mHwWorkListDirty)) {
945a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis            mHwWorkListDirty = false;
946a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis            for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
947a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                sp<const DisplayDevice> hw(mDisplays[dpy]);
948a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                const int32_t id = hw->getHwcDisplayId();
949a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                if (id >= 0) {
95013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                    const Vector< sp<Layer> >& currentLayers(
951a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                        hw->getVisibleLayersSortedByZ());
952a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                    const size_t count = currentLayers.size();
953a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                    if (hwc.createWorkList(id, count) == NO_ERROR) {
954a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                        HWComposer::LayerListIterator cur = hwc.begin(id);
955a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                        const HWComposer::LayerListIterator end = hwc.end(id);
956a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                        for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
95713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                            const sp<Layer>& layer(currentLayers[i]);
958a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                            layer->setGeometry(hw, *cur);
959a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                            if (mDebugDisableHWC || mDebugRegion) {
960a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                                cur->setSkip(true);
961a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                            }
962a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                        }
963a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                    }
964a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                }
965a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis            }
966a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis        }
967a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis
968a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis        // set the per-frame data
96992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
9704297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            sp<const DisplayDevice> hw(mDisplays[dpy]);
971e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            const int32_t id = hw->getHwcDisplayId();
972e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            if (id >= 0) {
97313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                const Vector< sp<Layer> >& currentLayers(
974cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                    hw->getVisibleLayersSortedByZ());
975e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                const size_t count = currentLayers.size();
976a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                HWComposer::LayerListIterator cur = hwc.begin(id);
977a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                const HWComposer::LayerListIterator end = hwc.end(id);
978a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
979a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                    /*
980a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                     * update the per-frame h/w composer data for each layer
981a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                     * and build the transparent region of the FB
982a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                     */
98313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                    const sp<Layer>& layer(currentLayers[i]);
984a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis                    layer->setPerFrameData(hw, *cur);
9851e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                }
98652bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian            }
98787baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        }
988a4310c8be2dc3406a668ee99020d52187173232fJamie Gennis
98952bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        status_t err = hwc.prepare();
99052bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
99152bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    }
992cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian}
99352bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian
994cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::doComposition() {
995cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    ATRACE_CALL();
99652bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
99792a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
9984297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        const sp<DisplayDevice>& hw(mDisplays[dpy]);
999cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        if (hw->canDraw()) {
1000cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            // transform the dirty region into this screen's coordinate space
1001cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
100202b95105754b1859a97e234b79f41489a4677c20Mathias Agopian
100302b95105754b1859a97e234b79f41489a4677c20Mathias Agopian            // repaint the framebuffer (if needed)
100402b95105754b1859a97e234b79f41489a4677c20Mathias Agopian            doDisplayComposition(hw, dirtyRegion);
100502b95105754b1859a97e234b79f41489a4677c20Mathias Agopian
1006cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            hw->dirtyRegion.clear();
1007cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            hw->flip(hw->swapRegion);
1008cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian            hw->swapRegion.clear();
100987baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        }
101052bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        // inform the h/w that we're done compositing
10114297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        hw->compositionComplete();
10124fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    }
101352bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    postFramebuffer();
1014edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1015edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1016edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid SurfaceFlinger::postFramebuffer()
1017edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1018841cde55549cea7a344a1705b18d57a0c6c8ec45Mathias Agopian    ATRACE_CALL();
1019b048cef231075a5e41d224b73fe11fec62f335b1Mathias Agopian
1020a44b04163957d6086362f6f365443c4c93379031Mathias Agopian    const nsecs_t now = systemTime();
1021a44b04163957d6086362f6f365443c4c93379031Mathias Agopian    mDebugInSwapBuffers = now;
1022c5c5a14c06de249d8e0445fd24699e1d9aa04549Jesse Hall
102352bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    HWComposer& hwc(getHwComposer());
1024ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    if (hwc.initCheck() == NO_ERROR) {
10252a23184e4109060ec772763e80dae2132cf9d2ebMathias Agopian        if (!hwc.supportsFramebufferTarget()) {
10262a23184e4109060ec772763e80dae2132cf9d2ebMathias Agopian            // EGL spec says:
10272a23184e4109060ec772763e80dae2132cf9d2ebMathias Agopian            //   "surface must be bound to the calling thread's current context,
10282a23184e4109060ec772763e80dae2132cf9d2ebMathias Agopian            //    for the current rendering API."
10292a23184e4109060ec772763e80dae2132cf9d2ebMathias Agopian            DisplayDevice::makeCurrent(mEGLDisplay,
10302a23184e4109060ec772763e80dae2132cf9d2ebMathias Agopian                    getDefaultDisplayDevice(), mEGLContext);
10312a23184e4109060ec772763e80dae2132cf9d2ebMathias Agopian        }
1032e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        hwc.commit();
103352bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian    }
103452bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian
103592a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
10364297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        sp<const DisplayDevice> hw(mDisplays[dpy]);
103713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
1038da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        hw->onSwapBuffersCompleted(hwc);
103952bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        const size_t count = currentLayers.size();
1040e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        int32_t id = hw->getHwcDisplayId();
1041e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        if (id >=0 && hwc.initCheck() == NO_ERROR) {
10421e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            HWComposer::LayerListIterator cur = hwc.begin(id);
10431e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            const HWComposer::LayerListIterator end = hwc.end(id);
104452bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian            for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
1045d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian                currentLayers[i]->onLayerDisplayed(hw, &*cur);
104652bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian            }
1047cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian        } else {
104852bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian            for (size_t i = 0; i < count; i++) {
1049d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian                currentLayers[i]->onLayerDisplayed(hw, NULL);
105052bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian            }
1051ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        }
1052e8696a40e09b24b634214684d18526187b316a2fJamie Gennis    }
1053e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
1054a44b04163957d6086362f6f365443c4c93379031Mathias Agopian    mLastSwapBufferTime = systemTime() - now;
1055a44b04163957d6086362f6f365443c4c93379031Mathias Agopian    mDebugInSwapBuffers = 0;
1056edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1057edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
105887baae104a3e4c2059990b01c393476065c558b0Mathias Agopianvoid SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
1059edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1060841cde55549cea7a344a1705b18d57a0c6c8ec45Mathias Agopian    ATRACE_CALL();
1061841cde55549cea7a344a1705b18d57a0c6c8ec45Mathias Agopian
1062ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    Mutex::Autolock _l(mStateLock);
1063ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    const nsecs_t now = systemTime();
1064ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    mDebugInTransaction = now;
1065ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian
1066ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    // Here we're guaranteed that some transaction flags are set
1067ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    // so we can call handleTransactionLocked() unconditionally.
1068ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    // We call getTransactionFlags(), which will also clear the flags,
1069ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    // with mStateLock held to guarantee that mCurrentState won't change
1070ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    // until the transaction is committed.
1071ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian
1072e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    transactionFlags = getTransactionFlags(eTransactionMask);
107387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    handleTransactionLocked(transactionFlags);
1074ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian
1075ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    mLastTransactionTime = systemTime() - now;
1076ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    mDebugInTransaction = 0;
1077ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    invalidateHwcGeometry();
1078ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    // here the transaction has been committed
10793d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian}
1080edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
108187baae104a3e4c2059990b01c393476065c558b0Mathias Agopianvoid SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
10823d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian{
10833d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian    const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
1084edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const size_t count = currentLayers.size();
1085edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1086edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /*
1087edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     * Traversal of the children
1088edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     * (perform the transaction for each of them if needed)
1089edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     */
1090edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
10913559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    if (transactionFlags & eTraversalNeeded) {
1092edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        for (size_t i=0 ; i<count ; i++) {
109313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(currentLayers[i]);
1094edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1095edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            if (!trFlags) continue;
1096edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1097edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            const uint32_t flags = layer->doTransaction(0);
1098edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            if (flags & Layer::eVisibleRegion)
1099edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                mVisibleRegionsDirty = true;
1100edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1101edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1102edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1103edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /*
11043559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian     * Perform display own transactions if needed
1105edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     */
1106edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1107e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    if (transactionFlags & eDisplayTransactionNeeded) {
110892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        // here we take advantage of Vector's copy-on-write semantics to
110992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        // improve performance by skipping the transaction entirely when
111092a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        // know that the lists are identical
1111e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const KeyedVector<  wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1112e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const KeyedVector<  wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
111392a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        if (!curr.isIdenticalTo(draw)) {
1114edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            mVisibleRegionsDirty = true;
111592a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            const size_t cc = curr.size();
111693997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                  size_t dc = draw.size();
111792a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian
111892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // find the displays that were removed
111992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // (ie: in drawing state but not in current state)
112092a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // also handle displays that changed
112192a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // (ie: displays that are in both lists)
112292a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            for (size_t i=0 ; i<dc ; i++) {
1123e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1124e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                if (j < 0) {
112592a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    // in drawing state but not in current state
11263ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian                    if (!draw[i].isMainDisplay()) {
112727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                        // Call makeCurrent() on the primary display so we can
112827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                        // be sure that nothing associated with this display
112927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                        // is current.
1130db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                        const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
113127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                        DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
11323ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian                        mDisplays.removeItem(draw.keyAt(i));
113327ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                        getHwComposer().disconnectDisplay(draw[i].type);
11349e9689c11148521d2c16a121a0b87b062be0714cAndy McFadden                        mEventThread->onHotplugReceived(draw[i].type, false);
113592a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    } else {
113692a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                        ALOGW("trying to remove the main display");
113792a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    }
113892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                } else {
113992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    // this display is in both lists. see if something changed.
1140e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                    const DisplayDeviceState& state(curr[j]);
11413ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian                    const wp<IBinder>& display(curr.keyAt(j));
1142111b2d89221722d38f5b5b3ba65904ec22421839Mathias Agopian                    if (state.surface->asBinder() != draw[i].surface->asBinder()) {
1143e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        // changing the surface is like destroying and
114493997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // recreating the DisplayDevice, so we just remove it
114593997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // from the drawing state, so that it get re-added
114693997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // below.
114793997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        mDisplays.removeItem(display);
114893997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        mDrawingState.displays.removeItemsAt(i);
114993997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        dc--; i--;
115093997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // at this point we must loop to the next item
115193997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        continue;
115292a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    }
115393997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian
1154db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                    const sp<DisplayDevice> disp(getDisplayDevice(display));
115593997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                    if (disp != NULL) {
115693997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        if (state.layerStack != draw[i].layerStack) {
115793997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                            disp->setLayerStack(state.layerStack);
115893997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        }
115900e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                        if ((state.orientation != draw[i].orientation)
116000e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                                || (state.viewport != draw[i].viewport)
116100e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                                || (state.frame != draw[i].frame))
116200e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                        {
116300e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                            disp->setProjection(state.orientation,
11644fb3999cea652617be5125f8a42c257467bf3c77Jeff Brown                                    state.viewport, state.frame);
116593997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        }
116692a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    }
116792a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                }
116892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            }
116992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian
117092a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // find displays that were added
117192a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // (ie: in current state but not in drawing state)
117292a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            for (size_t i=0 ; i<cc ; i++) {
1173e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                if (draw.indexOfKey(curr.keyAt(i)) < 0) {
1174e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                    const DisplayDeviceState& state(curr[i]);
1175cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1176cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    sp<FramebufferSurface> fbs;
1177e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian                    sp<Surface> stc;
1178cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    if (!state.isVirtualDisplay()) {
1179cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1180cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        ALOGE_IF(state.surface!=NULL,
1181cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                                "adding a supported display, but rendering "
1182cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                                "surface is provided (%p), ignoring it",
1183cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                                state.surface.get());
1184cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1185cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        // for supported (by hwc) displays we provide our
1186cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        // own rendering surface
1187f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian                        fbs = new FramebufferSurface(*mHwc, state.type);
1188e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian                        stc = new Surface(
11892adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden                                static_cast< sp<IGraphicBufferProducer> >(
1190dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis                                        fbs->getBufferQueue()));
1191cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    } else {
1192cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        if (state.surface != NULL) {
1193e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian                            stc = new Surface(state.surface);
1194cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        }
1195cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    }
1196cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1197cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    const wp<IBinder>& display(curr.keyAt(i));
1198cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    if (stc != NULL) {
1199cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        sp<DisplayDevice> hw = new DisplayDevice(this,
1200692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall                                state.type, state.isSecure, display, stc, fbs,
1201dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis                                mEGLConfig);
1202cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        hw->setLayerStack(state.layerStack);
1203cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        hw->setProjection(state.orientation,
12044fb3999cea652617be5125f8a42c257467bf3c77Jeff Brown                                state.viewport, state.frame);
12058dfa92fef9759a881e96ee58d59875d35023aab9Andy McFadden                        hw->setDisplayName(state.displayName);
1206cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        mDisplays.add(display, hw);
12079e9689c11148521d2c16a121a0b87b062be0714cAndy McFadden                        mEventThread->onHotplugReceived(state.type, true);
120893997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                    }
120992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                }
121092a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            }
1211edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
12123559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    }
1213edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
12148430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian    if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
12158430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // The transform hint might have changed for some layers
12168430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // (either because a display has changed, or because a layer
12178430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // as changed).
12188430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12198430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // Walk through all the layers in currentLayers,
12208430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // and update their transform hint.
12218430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12228430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // If a layer is visible only on a single display, then that
12238430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // display is used to calculate the hint, otherwise we use the
12248430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // default display.
12258430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12268430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // NOTE: we do this here, rather than in rebuildLayerStacks() so that
12278430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // the hint is set before we acquire a buffer from the surface texture.
12288430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12298430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // NOTE: layer transactions have taken place already, so we use their
12308430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // drawing state. However, SurfaceFlinger's own transaction has not
12318430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // happened yet, so we must use the current state layer list
12328430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // (soon to become the drawing state list).
12338430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12348430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        sp<const DisplayDevice> disp;
12358430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        uint32_t currentlayerStack = 0;
12368430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        for (size_t i=0; i<count; i++) {
12378430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            // NOTE: we rely on the fact that layers are sorted by
12388430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            // layerStack first (so we don't have to traverse the list
12398430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            // of displays for every layer).
124013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(currentLayers[i]);
124113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            uint32_t layerStack = layer->drawingState().layerStack;
12428430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            if (i==0 || currentlayerStack != layerStack) {
12438430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                currentlayerStack = layerStack;
12448430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // figure out if this layerstack is mirrored
12458430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // (more than one display) if so, pick the default display,
12468430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // if not, pick the only display it's on.
12478430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                disp.clear();
12488430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
12498430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                    sp<const DisplayDevice> hw(mDisplays[dpy]);
12508430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                    if (hw->getLayerStack() == currentlayerStack) {
12518430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                        if (disp == NULL) {
12528430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                            disp = hw;
12538430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                        } else {
12548430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                            disp = getDefaultDisplayDevice();
12558430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                            break;
12568430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                        }
12578430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                    }
12588430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                }
12598430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            }
12608430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            if (disp != NULL) {
12618430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // presumably this means this layer is using a layerStack
12628430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // that is not visible on any display
126313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                layer->updateTransformHint(disp);
12648430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            }
12658430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        }
12668430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian    }
12678430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian
12688430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian
12693559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    /*
12703559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian     * Perform our own transaction if needed
12713559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian     */
1272edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1273cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1274cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    if (currentLayers.size() > previousLayers.size()) {
12753559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        // layers have been added
12763559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        mVisibleRegionsDirty = true;
12773559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    }
12783559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian
12793559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    // some layers might have been removed, so
12803559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    // we need to update the regions they're exposing.
12813559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    if (mLayersRemoved) {
12823559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        mLayersRemoved = false;
12833559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        mVisibleRegionsDirty = true;
12843559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        const size_t count = previousLayers.size();
12853559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        for (size_t i=0 ; i<count ; i++) {
128613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(previousLayers[i]);
12873559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian            if (currentLayers.indexOf(layer) < 0) {
12883559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                // this layer is not visible anymore
12893559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                // TODO: we could traverse the tree from front to back and
12903559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                //       compute the actual visible region
12913559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                // TODO: we could cache the transformed region
12921501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                const Layer::State& s(layer->drawingState());
12931501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                Region visibleReg = s.transform.transform(
12941501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                        Region(Rect(s.active.w, s.active.h)));
12951501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                invalidateLayerStack(s.layerStack, visibleReg);
12960aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian            }
1297edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1298edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1299edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1300edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    commitTransaction();
13014fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian}
13024fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian
13034fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid SurfaceFlinger::commitTransaction()
13044fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian{
13054fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    if (!mLayersPendingRemoval.isEmpty()) {
13064fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // Notify removed layers now that they can't be drawn from
13074fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
13084fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            mLayersPendingRemoval[i]->onRemoved();
13094fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        }
13104fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        mLayersPendingRemoval.clear();
13114fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    }
13124fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian
13134b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    // If this transaction is part of a window animation then the next frame
13144b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    // we composite should be considered an animation as well.
13154b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    mAnimCompositionPending = mAnimTransactionPending;
13164b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
13174fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    mDrawingState = mCurrentState;
13182d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    mTransactionPending = false;
13192d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    mAnimTransactionPending = false;
13204fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    mTransactionCV.broadcast();
1321edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1322edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1323edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid SurfaceFlinger::computeVisibleRegions(
132487baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const LayerVector& currentLayers, uint32_t layerStack,
132587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        Region& outDirtyRegion, Region& outOpaqueRegion)
1326edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1327841cde55549cea7a344a1705b18d57a0c6c8ec45Mathias Agopian    ATRACE_CALL();
1328841cde55549cea7a344a1705b18d57a0c6c8ec45Mathias Agopian
1329edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Region aboveOpaqueLayers;
1330edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Region aboveCoveredLayers;
1331edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Region dirty;
1332edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
133387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    outDirtyRegion.clear();
1334edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1335edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t i = currentLayers.size();
1336edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while (i--) {
133713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer = currentLayers[i];
1338edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1339edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // start with the whole surface at its current location
1340970112231e887c8a14441ec4fed55342e019fc8cMathias Agopian        const Layer::State& s(layer->drawingState());
1341edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
134201e29054e672301e4adbbca15b3562a59a20f267Jesse Hall        // only consider the layers on the given layer stack
134387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        if (s.layerStack != layerStack)
134487baae104a3e4c2059990b01c393476065c558b0Mathias Agopian            continue;
134587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian
1346ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        /*
1347ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * opaqueRegion: area of a surface that is fully opaque.
1348ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         */
1349edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        Region opaqueRegion;
1350ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1351ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        /*
1352ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * visibleRegion: area of a surface that is visible on screen
1353ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * and not fully transparent. This is essentially the layer's
1354ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * footprint minus the opaque regions above it.
1355ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * Areas covered by a translucent surface are considered visible.
1356ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         */
1357edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        Region visibleRegion;
1358ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1359ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        /*
1360ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * coveredRegion: area of a surface that is covered by all
1361ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * visible regions above it (which includes the translucent areas).
1362ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         */
1363edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        Region coveredRegion;
1364ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1365a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        /*
1366a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * transparentRegion: area of a surface that is hinted to be completely
1367a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * transparent. This is only used to tell when the layer has no visible
1368a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * non-transparent regions and can be removed from the layer list. It
1369a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * does not affect the visibleRegion of this layer or any layers
1370a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * beneath it. The hint may not be correct if apps don't respect the
1371a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * SurfaceView restrictions (which, sadly, some don't).
1372a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         */
1373a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        Region transparentRegion;
1374a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall
1375ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1376ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // handle hidden surfaces by setting the visible region to empty
1377da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (CC_LIKELY(layer->isVisible())) {
1378a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            const bool translucent = !layer->isOpaque();
13795219a06d61ac4517506500363c5e8a5972dd7ac9Mathias Agopian            Rect bounds(s.transform.transform(layer->computeBounds()));
1380edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            visibleRegion.set(bounds);
1381ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            if (!visibleRegion.isEmpty()) {
1382ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                // Remove the transparent area from the visible region
1383ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                if (translucent) {
13844fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    const Transform tr(s.transform);
13854fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    if (tr.transformed()) {
13864fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                        if (tr.preserveRects()) {
13874fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                            // transform the transparent region
1388a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                            transparentRegion = tr.transform(s.transparentRegion);
13894fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                        } else {
13904fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                            // transformation too complex, can't do the
13914fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                            // transparent region optimization.
1392a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                            transparentRegion.clear();
13934fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                        }
13944fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    } else {
1395a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                        transparentRegion = s.transparentRegion;
13964fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    }
1397ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                }
1398edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1399ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                // compute the opaque region
14004fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                const int32_t layerOrientation = s.transform.getOrientation();
1401ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                if (s.alpha==255 && !translucent &&
1402ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                        ((layerOrientation & Transform::ROT_INVALID) == false)) {
1403ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                    // the opaque region is the layer's footprint
1404ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                    opaqueRegion = visibleRegion;
1405ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                }
1406edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
1407edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1408edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1409ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // Clip the covered region to the visible region
1410ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1411ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1412ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // Update aboveCoveredLayers for next (lower) layer
1413ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        aboveCoveredLayers.orSelf(visibleRegion);
1414ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1415edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // subtract the opaque region covered by the layers above us
1416edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        visibleRegion.subtractSelf(aboveOpaqueLayers);
1417edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1418edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // compute this layer's dirty region
1419edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (layer->contentDirty) {
1420edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // we need to invalidate the whole region
1421edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            dirty = visibleRegion;
1422edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // as well, as the old visible region
14234fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            dirty.orSelf(layer->visibleRegion);
1424edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            layer->contentDirty = false;
1425edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        } else {
1426a8d44f75e1934072713371a2dcd143c63ffcbe0eMathias Agopian            /* compute the exposed region:
1427ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *   the exposed region consists of two components:
1428ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *   1) what's VISIBLE now and was COVERED before
1429ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *   2) what's EXPOSED now less what was EXPOSED before
1430ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *
1431ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * note that (1) is conservative, we start with the whole
1432ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * visible region but only keep what used to be covered by
1433ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * something -- which mean it may have been exposed.
1434ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *
1435ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * (2) handles areas that were not covered by anything but got
1436ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * exposed because of a resize.
1437a8d44f75e1934072713371a2dcd143c63ffcbe0eMathias Agopian             */
1438ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            const Region newExposed = visibleRegion - coveredRegion;
14394fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            const Region oldVisibleRegion = layer->visibleRegion;
14404fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            const Region oldCoveredRegion = layer->coveredRegion;
1441ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1442ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
1443edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1444edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        dirty.subtractSelf(aboveOpaqueLayers);
1445edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1446edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // accumulate to the screen dirty region
144787baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        outDirtyRegion.orSelf(dirty);
1448edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1449ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // Update aboveOpaqueLayers for next (lower) layer
1450edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        aboveOpaqueLayers.orSelf(opaqueRegion);
14518b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber
1452a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        // Store the visible region in screen space
1453edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        layer->setVisibleRegion(visibleRegion);
1454edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        layer->setCoveredRegion(coveredRegion);
1455a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        layer->setVisibleNonTransparentRegion(
1456a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                visibleRegion.subtract(transparentRegion));
1457edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1458edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
145987baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    outOpaqueRegion = aboveOpaqueLayers;
1460edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1461edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
146287baae104a3e4c2059990b01c393476065c558b0Mathias Agopianvoid SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
146387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const Region& dirty) {
146492a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
14654297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        const sp<DisplayDevice>& hw(mDisplays[dpy]);
14664297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        if (hw->getLayerStack() == layerStack) {
14674297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            hw->dirtyRegion.orSelf(dirty);
146892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        }
146992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    }
147087baae104a3e4c2059990b01c393476065c558b0Mathias Agopian}
147187baae104a3e4c2059990b01c393476065c558b0Mathias Agopian
147287baae104a3e4c2059990b01c393476065c558b0Mathias Agopianvoid SurfaceFlinger::handlePageFlip()
1473edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
14744fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    Region dirtyRegion;
147599ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
14764fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    bool visibleRegions = false;
1477cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
14784fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    const size_t count = currentLayers.size();
14794fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    for (size_t i=0 ; i<count ; i++) {
148013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
148187baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const Region dirty(layer->latchBuffer(visibleRegions));
14821501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian        const Layer::State& s(layer->drawingState());
148387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        invalidateLayerStack(s.layerStack, dirty);
14844fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    }
14854da751999358fffa4cefc4c8046dab72045925f6Mathias Agopian
14863b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian    mVisibleRegionsDirty |= visibleRegions;
1487edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1488edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1489ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopianvoid SurfaceFlinger::invalidateHwcGeometry()
1490ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian{
1491ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian    mHwWorkListDirty = true;
1492ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian}
1493ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian
149499ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
1495cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
149687baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const Region& inDirtyRegion)
1497edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
149887baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    Region dirtyRegion(inDirtyRegion);
149987baae104a3e4c2059990b01c393476065c558b0Mathias Agopian
1500b8a5560e1303cb10f5cd482af466fc04d2bdfcabMathias Agopian    // compute the invalid region
15014297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->swapRegion.orSelf(dirtyRegion);
1502edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
15034297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    uint32_t flags = hw->getFlags();
15040f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian    if (flags & DisplayDevice::SWAP_RECTANGLE) {
150529d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian        // we can redraw only what's dirty, but since SWAP_RECTANGLE only
150629d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian        // takes a rectangle, we must make sure to update that whole
150729d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian        // rectangle in that case
15084297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        dirtyRegion.set(hw->swapRegion.bounds());
1509edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    } else {
15100f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian        if (flags & DisplayDevice::PARTIAL_UPDATES) {
151129d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian            // We need to redraw the rectangle that will be updated
1512df3ca30bf663cb8eed88ee3f16fb5e9a65dc00fcMathias Agopian            // (pushed to the framebuffer).
151395a666b0e001ebf738418b89d8e8fd50b18110f8Mathias Agopian            // This is needed because PARTIAL_UPDATES only takes one
15140f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian            // rectangle instead of a region (see DisplayDevice::flip())
15154297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            dirtyRegion.set(hw->swapRegion.bounds());
1516edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        } else {
151729d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian            // we need to redraw everything (the whole screen)
15184297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            dirtyRegion.set(hw->bounds());
15194297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            hw->swapRegion = dirtyRegion;
1520edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1521edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1522edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1523cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    doComposeSurfaces(hw, dirtyRegion);
1524edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
15259c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    // update the swap region and clear the dirty region
15264297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->swapRegion.orSelf(dirtyRegion);
1527da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1528da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // swap buffers (presentation)
1529da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    hw->swapBuffers(getHwComposer());
1530edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1531edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1532cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
1533edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
153485d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const int32_t id = hw->getHwcDisplayId();
15358630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    HWComposer& hwc(getHwComposer());
15361e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    HWComposer::LayerListIterator cur = hwc.begin(id);
15371e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    const HWComposer::LayerListIterator end = hwc.end(id);
1538a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
153985d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
154085d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    if (hasGlesComposition) {
1541da8d0a5c0cf9d41915d3b106cad4aaec3e767c11Mathias Agopian        DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
1542a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
154352bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        // set the frame buffer
154452bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        glMatrixMode(GL_MODELVIEW);
154552bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        glLoadIdentity();
1546a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1547a2f4e56fec0fb36c4a370eb23d6e9dc57f250b59Mathias Agopian        // Never touch the framebuffer if we don't have any framebuffer layers
154885d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        const bool hasHwcComposition = hwc.hasHwcComposition(id);
1549e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        if (hasHwcComposition) {
1550b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // when using overlays, we assume a fully transparent framebuffer
1551b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // NOTE: we could reduce how much we need to clear, for instance
1552b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // remove where there are opaque FB layers. however, on some
1553b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // GPUs doing a "clean slate" glClear might be more efficient.
1554b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // We'll revisit later if needed.
1555b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            glClearColor(0, 0, 0, 0);
1556b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            glClear(GL_COLOR_BUFFER_BIT);
1557b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian        } else {
1558766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // we start with the whole screen area
1559766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            const Region bounds(hw->getBounds());
1560766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1561766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // we remove the scissor part
1562766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // we're left with the letterbox region
1563766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // (common case is that letterbox ends-up being empty)
1564766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            const Region letterbox(bounds.subtract(hw->getScissor()));
1565766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1566766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // compute the area to clear
1567766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            Region region(hw->undefinedRegion.merge(letterbox));
1568766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1569766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // but limit it to the dirty region
1570766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            region.andSelf(dirty);
1571766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1572b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // screen is already cleared here
157387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian            if (!region.isEmpty()) {
1574b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian                // can happen with SurfaceView
157555801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian                drawWormhole(hw, region);
1576b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            }
1577a2f4e56fec0fb36c4a370eb23d6e9dc57f250b59Mathias Agopian        }
1578f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian
1579766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian        if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1580766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // just to be on the safe side, we don't set the
1581f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            // scissor on the main display. It should never be needed
1582f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            // anyways (though in theory it could since the API allows it).
1583f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            const Rect& bounds(hw->getBounds());
1584766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            const Rect& scissor(hw->getScissor());
1585f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            if (scissor != bounds) {
1586f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // scissor doesn't match the screen's dimensions, so we
1587f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // need to clear everything outside of it and enable
1588f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // the GL scissor so we don't draw anything where we shouldn't
1589f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                const GLint height = hw->getHeight();
1590f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                glScissor(scissor.left, height - scissor.bottom,
1591f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                        scissor.getWidth(), scissor.getHeight());
1592f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // enable scissor for this frame
1593f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                glEnable(GL_SCISSOR_TEST);
1594f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            }
1595f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian        }
159685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    }
15974b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian
159885d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    /*
159985d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian     * and then, render the layers targeted at the framebuffer
160085d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian     */
16014b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian
160213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
160385d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const size_t count = layers.size();
160485d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const Transform& tr = hw->getTransform();
160585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    if (cur != end) {
160685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        // we're using h/w composer
160785d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
160813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(layers[i]);
16094fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
161085d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            if (!clip.isEmpty()) {
161185d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                switch (cur->getCompositionType()) {
161285d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    case HWC_OVERLAY: {
161385d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                        if ((cur->getHints() & HWC_HINT_CLEAR_FB)
161485d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                                && i
161585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                                && layer->isOpaque()
161685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                                && hasGlesComposition) {
1617cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                            // never clear the very first layer since we're
1618cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                            // guaranteed the FB is already cleared
1619cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                            layer->clearWithOpenGL(hw, clip);
1620cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                        }
162185d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                        break;
162285d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    }
162385d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    case HWC_FRAMEBUFFER: {
1624cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                        layer->draw(hw, clip);
162585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                        break;
1626a2f4e56fec0fb36c4a370eb23d6e9dc57f250b59Mathias Agopian                    }
1627da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    case HWC_FRAMEBUFFER_TARGET: {
1628da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        // this should not happen as the iterator shouldn't
1629da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        // let us get there.
1630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        break;
1632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
1633cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                }
1634a6b32db164e7834e211261046f3229bf50bc0098Jesse Hall            }
163585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            layer->setAcquireFence(hw, *cur);
163685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        }
163785d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    } else {
163885d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        // we're not using h/w composer
163985d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        for (size_t i=0 ; i<count ; ++i) {
164013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(layers[i]);
164185d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            const Region clip(dirty.intersect(
164285d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    tr.transform(layer->visibleRegion)));
164385d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            if (!clip.isEmpty()) {
164485d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                layer->draw(hw, clip);
164585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            }
16464b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian        }
16474b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian    }
1648f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian
1649f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian    // disable scissor at the end of the frame
1650f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian    glDisable(GL_SCISSOR_TEST);
1651edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1652edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
165355801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopianvoid SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
165455801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        const Region& region) const
1655edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1656f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    glDisable(GL_TEXTURE_EXTERNAL_OES);
1657b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian    glDisable(GL_TEXTURE_2D);
1658f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    glDisable(GL_BLEND);
1659b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian    glColor4f(0,0,0,0);
1660f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian
166155801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian    const int32_t height = hw->getHeight();
1662f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    Region::const_iterator it = region.begin();
1663f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    Region::const_iterator const end = region.end();
1664f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    while (it != end) {
1665f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian        const Rect& r = *it++;
166655801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        GLfloat vertices[][2] = {
16678f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.left,  (GLfloat) (height - r.top) },
16688f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.left,  (GLfloat) (height - r.bottom) },
16698f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
16708f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.right, (GLfloat) (height - r.top) }
167155801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        };
167255801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        glVertexPointer(2, GL_FLOAT, 0, vertices);
1673f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1674f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    }
1675edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1676edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1677ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopianvoid SurfaceFlinger::addClientLayer(const sp<Client>& client,
1678ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopian        const sp<IBinder>& handle,
167913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& lbc)
16801b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian{
168196f0819f81293076e652792794a961543e6750d7Mathias Agopian    // attach this layer to the client
1682ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopian    client->attachLayer(handle, lbc);
16834f113740180b6512b43723c4728f262882dc9b45Mathias Agopian
168496f0819f81293076e652792794a961543e6750d7Mathias Agopian    // add this layer to the current state list
1685921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    Mutex::Autolock _l(mStateLock);
1686921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    mCurrentState.layersSortedByZ.add(lbc);
168796f0819f81293076e652792794a961543e6750d7Mathias Agopian}
168896f0819f81293076e652792794a961543e6750d7Mathias Agopian
168913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
169096f0819f81293076e652792794a961543e6750d7Mathias Agopian{
169196f0819f81293076e652792794a961543e6750d7Mathias Agopian    Mutex::Autolock _l(mStateLock);
169296f0819f81293076e652792794a961543e6750d7Mathias Agopian    status_t err = purgatorizeLayer_l(layer);
169396f0819f81293076e652792794a961543e6750d7Mathias Agopian    if (err == NO_ERROR)
16943559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        setTransactionFlags(eTransactionNeeded);
169596f0819f81293076e652792794a961543e6750d7Mathias Agopian    return err;
1696edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1697edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
169813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::removeLayer_l(const sp<Layer>& layer)
1699edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
170013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
1701edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (index >= 0) {
1702076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        mLayersRemoved = true;
1703edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return NO_ERROR;
1704edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
17053d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian    return status_t(index);
1706edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1707edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
170813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::purgatorizeLayer_l(const sp<Layer>& layer)
17099a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian{
171076cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    // First add the layer to the purgatory list, which makes sure it won't
171176cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    // go away, then remove it from the main list (through a transaction).
171213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    ssize_t err = removeLayer_l(layer);
171376cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    if (err >= 0) {
171413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mLayerPurgatory.add(layer);
171576cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    }
17168c0a3d75c8823e179d19c6303f64e669975a4d85Mathias Agopian
171713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mLayersPendingRemoval.push(layer);
17180b3ad46a26dc3717260fa9347c77f673f3198606Mathias Agopian
17193d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian    // it's possible that we don't find a layer, because it might
17203d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian    // have been destroyed already -- this is not technically an error
172196f0819f81293076e652792794a961543e6750d7Mathias Agopian    // from the user because there is a race between Client::destroySurface(),
17224d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    // ~Client() and ~LayerCleaner().
17239a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
17249a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian}
17259a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian
1726dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopianuint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1727dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian{
1728dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian    return android_atomic_release_load(&mTransactionFlags);
1729dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian}
1730dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian
1731edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectuint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1732edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1733edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return android_atomic_and(~flags, &mTransactionFlags) & flags;
1734edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1735edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1736bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopianuint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
1737edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1738edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1739edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if ((old & flags)==0) { // wake the server up
174099ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        signalTransaction();
1741edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1742edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return old;
1743edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1744edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17458b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopianvoid SurfaceFlinger::setTransactionState(
17468b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian        const Vector<ComposerState>& state,
17478b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian        const Vector<DisplayState>& displays,
17488b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian        uint32_t flags)
17498b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian{
17507c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis    ATRACE_CALL();
1751698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian    Mutex::Autolock _l(mStateLock);
175228378392fd5aa3e0a392c9eb64634055678c3987Jamie Gennis    uint32_t transactionFlags = 0;
1753e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
17542d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    if (flags & eAnimation) {
17552d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        // For window updates that are part of an animation we must wait for
17562d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        // previous animation "frames" to be handled.
17572d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        while (mAnimTransactionPending) {
17587c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis            status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
17592d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            if (CC_UNLIKELY(err != NO_ERROR)) {
17602d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                // just in case something goes wrong in SF, return to the
17617c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis                // caller after a few seconds.
17627c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis                ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
17637c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis                        "waiting for previous animation frame");
17642d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                mAnimTransactionPending = false;
17652d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                break;
17662d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            }
17672d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        }
17682d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    }
17692d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis
1770e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    size_t count = displays.size();
1771e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
1772e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const DisplayState& s(displays[i]);
1773e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        transactionFlags |= setDisplayStateLocked(s);
1774b8d69a55f1c187a35ac41e69de63251f5501b6f4Jamie Gennis    }
1775b8d69a55f1c187a35ac41e69de63251f5501b6f4Jamie Gennis
1776e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    count = state.size();
1777698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
1778698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian        const ComposerState& s(state[i]);
1779d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // Here we need to check that the interface we're given is indeed
1780d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // one of our own. A malicious client could give us a NULL
1781d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // IInterface, or one of its own or even one of our own but a
1782d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // different type. All these situations would cause us to crash.
1783d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        //
1784d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // NOTE: it would be better to use RTTI as we could directly check
1785d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // that we have a Client*. however, RTTI is disabled in Android.
1786d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        if (s.client != NULL) {
1787d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian            sp<IBinder> binder = s.client->asBinder();
1788d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian            if (binder != NULL) {
1789d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                String16 desc(binder->getInterfaceDescriptor());
1790d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                if (desc == ISurfaceComposerClient::descriptor) {
1791d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                    sp<Client> client( static_cast<Client *>(s.client.get()) );
1792d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                    transactionFlags |= setClientStateLocked(client, s.state);
1793d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                }
1794d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian            }
1795d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        }
1796698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian    }
1797386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian
179828378392fd5aa3e0a392c9eb64634055678c3987Jamie Gennis    if (transactionFlags) {
1799386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        // this triggers the transaction
180028378392fd5aa3e0a392c9eb64634055678c3987Jamie Gennis        setTransactionFlags(transactionFlags);
1801698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian
1802386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        // if this is a synchronous transaction, wait for it to take effect
1803386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        // before returning.
1804386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        if (flags & eSynchronous) {
18052d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            mTransactionPending = true;
18062d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        }
18072d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        if (flags & eAnimation) {
18082d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            mAnimTransactionPending = true;
1809386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        }
18102d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        while (mTransactionPending) {
1811386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian            status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1812386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian            if (CC_UNLIKELY(err != NO_ERROR)) {
1813386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian                // just in case something goes wrong in SF, return to the
1814386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian                // called after a few seconds.
18152d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
18162d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                mTransactionPending = false;
1817386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian                break;
1818386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian            }
1819cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian        }
1820edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1821edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1822edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1823e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopianuint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1824e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian{
18259a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall    ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
18269a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall    if (dpyIdx < 0)
18279a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall        return 0;
18289a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall
1829e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    uint32_t flags = 0;
18309a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall    DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
18313ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    if (disp.isValid()) {
1832e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const uint32_t what = s.what;
1833e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & DisplayState::eSurfaceChanged) {
1834e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.surface->asBinder() != s.surface->asBinder()) {
1835e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.surface = s.surface;
1836e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1837e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1838e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1839e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & DisplayState::eLayerStackChanged) {
1840e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.layerStack != s.layerStack) {
1841e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.layerStack = s.layerStack;
1842e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1843e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1844e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
184500e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian        if (what & DisplayState::eDisplayProjectionChanged) {
1846e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.orientation != s.orientation) {
1847e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.orientation = s.orientation;
1848e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1849e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1850e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.frame != s.frame) {
1851e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.frame = s.frame;
1852e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1853e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1854e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.viewport != s.viewport) {
1855e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.viewport = s.viewport;
1856e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1857e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1858e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1859e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    }
1860e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    return flags;
1861e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
1862e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
1863e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopianuint32_t SurfaceFlinger::setClientStateLocked(
1864e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const sp<Client>& client,
1865e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const layer_state_t& s)
1866e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian{
1867e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    uint32_t flags = 0;
186813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Layer> layer(client->getLayerUser(s.surface));
1869e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    if (layer != 0) {
1870e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const uint32_t what = s.what;
1871e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::ePositionChanged) {
1872e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setPosition(s.x, s.y))
1873e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1874e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1875e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eLayerChanged) {
1876e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            // NOTE: index needs to be calculated before we update the state
1877e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1878e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setLayer(s.z)) {
1879e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.removeAt(idx);
1880e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.add(layer);
1881e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // we need traversal (state changed)
1882e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // AND transaction (list changed)
1883e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTransactionNeeded|eTraversalNeeded;
1884e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1885e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1886e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eSizeChanged) {
1887e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setSize(s.w, s.h)) {
1888e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1889e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1890e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1891e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eAlphaChanged) {
1892e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1893e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1894e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1895e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eMatrixChanged) {
1896e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setMatrix(s.matrix))
1897e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1898e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1899e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eTransparentRegionChanged) {
1900e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setTransparentRegionHint(s.transparentRegion))
1901e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1902e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1903e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eVisibilityChanged) {
1904e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setFlags(s.flags, s.mask))
1905e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1906e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1907e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eCropChanged) {
1908e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setCrop(s.crop))
1909e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1910e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1911e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eLayerStackChanged) {
1912e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            // NOTE: index needs to be calculated before we update the state
1913e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1914e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setLayerStack(s.layerStack)) {
1915e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.removeAt(idx);
1916e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.add(layer);
1917e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // we need traversal (state changed)
1918e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // AND transaction (list changed)
1919e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTransactionNeeded|eTraversalNeeded;
1920e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1921e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1922e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    }
1923e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    return flags;
1924e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
1925e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
19264d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopianstatus_t SurfaceFlinger::createLayer(
19270ef4e15a6c12778daf464a4953d7e15e651f49acMathias Agopian        const String8& name,
19280ef4e15a6c12778daf464a4953d7e15e651f49acMathias Agopian        const sp<Client>& client,
19294d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
19304d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
1931edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
19324d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
19336e2d6483fe14362187c78c91dc03024fbb31d431Mathias Agopian    if (int32_t(w|h) < 0) {
1934921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
19356e2d6483fe14362187c78c91dc03024fbb31d431Mathias Agopian                int(w), int(h));
19364d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        return BAD_VALUE;
19376e2d6483fe14362187c78c91dc03024fbb31d431Mathias Agopian    }
19388b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber
19394d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    status_t result = NO_ERROR;
19404d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
19414d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    sp<Layer> layer;
19424d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
19433165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian    switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
19443165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian        case ISurfaceComposerClient::eFXSurfaceNormal:
19454d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            result = createNormalLayer(client,
19464d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    name, w, h, flags, format,
19474d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    handle, gbp, &layer);
1948edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            break;
19493165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian        case ISurfaceComposerClient::eFXSurfaceDim:
19504d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            result = createDimLayer(client,
19514d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    name, w, h, flags,
19524d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    handle, gbp, &layer);
19534d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            break;
19544d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        default:
19554d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            result = BAD_VALUE;
1956edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            break;
1957edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1958edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
19594d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    if (result == NO_ERROR) {
19604d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        addClientLayer(client, *handle, layer);
196196f0819f81293076e652792794a961543e6750d7Mathias Agopian        setTransactionFlags(eTransactionNeeded);
1962edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
19634d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return result;
1964edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1965edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
19664d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopianstatus_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
19674d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
19684d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
1969edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1970edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // initialize the surfaces
197192a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    switch (format) {
1972edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    case PIXEL_FORMAT_TRANSPARENT:
1973edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    case PIXEL_FORMAT_TRANSLUCENT:
1974edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        format = PIXEL_FORMAT_RGBA_8888;
1975edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        break;
1976edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    case PIXEL_FORMAT_OPAQUE:
1977a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#ifdef NO_RGBX_8888
1978a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian        format = PIXEL_FORMAT_RGB_565;
1979a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#else
19808f10540cd59e980dcdb5a8f4f2dbec0d94177f5fMathias Agopian        format = PIXEL_FORMAT_RGBX_8888;
1981a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#endif
1982edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        break;
1983edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1984edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1985a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#ifdef NO_RGBX_8888
1986a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian    if (format == PIXEL_FORMAT_RGBX_8888)
1987a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian        format = PIXEL_FORMAT_RGBA_8888;
1988a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#endif
1989a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian
19904d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *outLayer = new Layer(this, client, name, w, h, flags);
19914d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    status_t err = (*outLayer)->setBuffers(w, h, format, flags);
19924d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    if (err == NO_ERROR) {
19934d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        *handle = (*outLayer)->getHandle();
19944d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        *gbp = (*outLayer)->getBufferQueue();
1995edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
19964d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
19974d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
19984d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return err;
1999edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2000edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
20014d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopianstatus_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
20024d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        const String8& name, uint32_t w, uint32_t h, uint32_t flags,
20034d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
2004edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
20054d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *outLayer = new LayerDim(this, client, name, w, h, flags);
20064d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *handle = (*outLayer)->getHandle();
20074d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *gbp = (*outLayer)->getBufferQueue();
20084d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return NO_ERROR;
2009118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian}
2010118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
2011ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopianstatus_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
20129a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian{
20139a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    /*
20149a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian     * called by the window manager, when a surface should be marked for
20159a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian     * destruction.
20168b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber     *
20170aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian     * The surface is removed from the current and drawing lists, but placed
20180aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian     * in the purgatory queue, so it's not destroyed right-away (we need
20190aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian     * to wait for all client's references to go away first).
20209a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian     */
20219a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian
202248d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian    status_t err = NAME_NOT_FOUND;
20230aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian    Mutex::Autolock _l(mStateLock);
202413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Layer> layer = client->getLayerUser(handle);
2025b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
202648d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian    if (layer != 0) {
202748d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian        err = purgatorizeLayer_l(layer);
202848d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian        if (err == NO_ERROR) {
202913233e067b8f71adc3a0ade5f442265e1f27084bMathias Agopian            setTransactionFlags(eTransactionNeeded);
203048d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian        }
20319a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    }
20329a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    return err;
20339a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian}
20349a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian
203513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
2036edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
20374d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    // called by ~LayerCleaner() when all references are gone
2038ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    status_t err = NO_ERROR;
203913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Layer> l(layer.promote());
2040ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    if (l != NULL) {
2041ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian        Mutex::Autolock _l(mStateLock);
2042ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian        err = removeLayer_l(l);
2043ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian        if (err == NAME_NOT_FOUND) {
2044ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            // The surface wasn't in the current list, which means it was
2045ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            // removed already, which means it is in the purgatory,
2046ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            // and need to be removed from there.
2047ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            ssize_t idx = mLayerPurgatory.remove(l);
2048e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block            ALOGE_IF(idx < 0,
2049ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian                    "layer=%p is not in the purgatory list", l.get());
2050f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian        }
2051e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2052ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian                "error removing layer=%p (%s)", l.get(), strerror(-err));
2053ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    }
2054ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    return err;
2055edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2056edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2057b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian// ---------------------------------------------------------------------------
2058b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
205913a082e160c2d1d8006b93a555a57035213d568bAndy McFaddenvoid SurfaceFlinger::onInitializeDisplays() {
206001e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    // reset screen orientation and use primary layer stack
206113a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    Vector<ComposerState> state;
206213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    Vector<DisplayState> displays;
206313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    DisplayState d;
206401e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    d.what = DisplayState::eDisplayProjectionChanged |
206501e29054e672301e4adbbca15b3562a59a20f267Jesse Hall             DisplayState::eLayerStackChanged;
2066692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
206701e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    d.layerStack = 0;
206813a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    d.orientation = DisplayState::eOrientationDefault;
20694c05dd175ee3bd5119eecf368742b6510a8cfa6cJeff Brown    d.frame.makeInvalid();
20704c05dd175ee3bd5119eecf368742b6510a8cfa6cJeff Brown    d.viewport.makeInvalid();
207113a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    displays.add(d);
207213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    setTransactionState(state, displays, 0);
2073cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    onScreenAcquired(getDefaultDisplayDevice());
207413a082e160c2d1d8006b93a555a57035213d568bAndy McFadden}
207513a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
207613a082e160c2d1d8006b93a555a57035213d568bAndy McFaddenvoid SurfaceFlinger::initializeDisplays() {
207713a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    class MessageScreenInitialized : public MessageBase {
207813a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        SurfaceFlinger* flinger;
207913a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    public:
208013a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
208113a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        virtual bool handler() {
208213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden            flinger->onInitializeDisplays();
208313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden            return true;
208413a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        }
208513a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    };
208613a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    sp<MessageBase> msg = new MessageScreenInitialized(this);
208713a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    postMessageAsync(msg);  // we may be called from main thread, use async message
208813a082e160c2d1d8006b93a555a57035213d568bAndy McFadden}
208913a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
209013a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
2091cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianvoid SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
2092c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2093c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (hw->isScreenAcquired()) {
2094c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        // this is expected, e.g. when power manager wakes up during boot
2095c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        ALOGD(" screen was previously acquired");
2096c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return;
2097c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    }
2098c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
20994297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->acquireScreen();
2100c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    int32_t type = hw->getDisplayType();
2101c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2102c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        // built-in display, tell the HWC
2103c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        getHwComposer().acquire(type);
2104c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
2105c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        if (type == DisplayDevice::DISPLAY_PRIMARY) {
2106c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden            // FIXME: eventthread only knows about the main display right now
2107c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden            mEventThread->onScreenAcquired();
2108c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        }
2109cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
211020128300e0cb7f459a60cfbcddb48190ce6545edMathias Agopian    mVisibleRegionsDirty = true;
211120128300e0cb7f459a60cfbcddb48190ce6545edMathias Agopian    repaintEverything();
2112edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2113edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2114cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianvoid SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
2115c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2116c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (!hw->isScreenAcquired()) {
2117c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        ALOGD(" screen was previously released");
2118c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return;
2119c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    }
2120c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
2121c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    hw->releaseScreen();
2122c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    int32_t type = hw->getDisplayType();
2123c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2124c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        if (type == DisplayDevice::DISPLAY_PRIMARY) {
2125cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian            // FIXME: eventthread only knows about the main display right now
2126cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian            mEventThread->onScreenReleased();
2127cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        }
2128c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
2129c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        // built-in display, tell the HWC
2130c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        getHwComposer().release(type);
2131b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    }
2132c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    mVisibleRegionsDirty = true;
2133c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    // from this point on, SF will stop drawing on this display
2134b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian}
2135b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
2136c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFaddenvoid SurfaceFlinger::unblank(const sp<IBinder>& display) {
2137b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    class MessageScreenAcquired : public MessageBase {
2138db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        SurfaceFlinger& mFlinger;
2139db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        sp<IBinder> mDisplay;
2140b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    public:
2141db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        MessageScreenAcquired(SurfaceFlinger& flinger,
2142db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
2143b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        virtual bool handler() {
2144db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2145db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            if (hw == NULL) {
2146db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGE("Attempt to unblank null display %p", mDisplay.get());
2147db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2148db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGW("Attempt to unblank virtual display");
2149db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else {
2150db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                mFlinger.onScreenAcquired(hw);
2151db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            }
2152b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian            return true;
2153b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        }
2154b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    };
2155db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2156db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    postMessageSync(msg);
2157edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2158edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2159c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFaddenvoid SurfaceFlinger::blank(const sp<IBinder>& display) {
2160b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    class MessageScreenReleased : public MessageBase {
2161db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        SurfaceFlinger& mFlinger;
2162db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        sp<IBinder> mDisplay;
2163b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    public:
2164db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        MessageScreenReleased(SurfaceFlinger& flinger,
2165db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
2166b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        virtual bool handler() {
2167db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2168db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            if (hw == NULL) {
2169db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGE("Attempt to blank null display %p", mDisplay.get());
2170db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2171db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGW("Attempt to blank virtual display");
2172db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else {
2173db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                mFlinger.onScreenReleased(hw);
2174db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            }
2175b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian            return true;
2176b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        }
2177b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    };
2178db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2179db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    postMessageSync(msg);
2180b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian}
2181b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
2182b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian// ---------------------------------------------------------------------------
2183b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
2184edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2185edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
21861d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    const size_t SIZE = 4096;
2187edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    char buffer[SIZE];
2188edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    String8 result;
218999b49840d309727678b77403d6cc9f920111623fMathias Agopian
219099b49840d309727678b77403d6cc9f920111623fMathias Agopian    if (!PermissionCache::checkCallingPermission(sDump)) {
2191edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        snprintf(buffer, SIZE, "Permission Denial: "
2192edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2193edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                IPCThreadState::self()->getCallingPid(),
2194edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                IPCThreadState::self()->getCallingUid());
2195edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        result.append(buffer);
2196edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    } else {
21979795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        // Try to get the main lock, but don't insist if we can't
21989795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        // (this would indicate SF is stuck, but we want to be able to
21999795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        // print something in dumpsys).
22009795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        int retry = 3;
22019795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        while (mStateLock.tryLock()<0 && --retry>=0) {
22029795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian            usleep(1000000);
22039795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        }
22049795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        const bool locked(retry >= 0);
22059795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        if (!locked) {
22068b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber            snprintf(buffer, SIZE,
22079795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian                    "SurfaceFlinger appears to be unresponsive, "
22089795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian                    "dumping anyways (no locks held)\n");
22099795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian            result.append(buffer);
22109795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        }
22119795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian
221282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        bool dumpAll = true;
221382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        size_t index = 0;
221425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        size_t numArgs = args.size();
221525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        if (numArgs) {
221625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            if ((index < numArgs) &&
221725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                    (args[index] == String16("--list"))) {
221825e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                index++;
221925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                listLayersLocked(args, index, result, buffer, SIZE);
222035aadd6be249da4bd4851692e6aff757c91b32a7Mathias Agopian                dumpAll = false;
222125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            }
222225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
222325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            if ((index < numArgs) &&
222425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                    (args[index] == String16("--latency"))) {
222582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                index++;
222682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                dumpStatsLocked(args, index, result, buffer, SIZE);
222735aadd6be249da4bd4851692e6aff757c91b32a7Mathias Agopian                dumpAll = false;
222882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            }
222925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
223025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            if ((index < numArgs) &&
223125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                    (args[index] == String16("--latency-clear"))) {
223225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                index++;
223325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                clearStatsLocked(args, index, result, buffer, SIZE);
223435aadd6be249da4bd4851692e6aff757c91b32a7Mathias Agopian                dumpAll = false;
223525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            }
2236edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
22371b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
223882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        if (dumpAll) {
223982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            dumpAllLocked(result, buffer, SIZE);
224082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        }
224148b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian
224282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        if (locked) {
224382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            mStateLock.unlock();
224448b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian        }
224582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
224682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    write(fd, result.string(), result.size());
224782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    return NO_ERROR;
224882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian}
224948b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian
225025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopianvoid SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
225125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        String8& result, char* buffer, size_t SIZE) const
225225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian{
225325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
225425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const size_t count = currentLayers.size();
225525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
225613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
225725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        snprintf(buffer, SIZE, "%s\n", layer->getName().string());
225825e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        result.append(buffer);
225925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    }
226025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian}
226125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
226282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopianvoid SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
226382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        String8& result, char* buffer, size_t SIZE) const
226482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian{
226582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    String8 name;
226682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    if (index < args.size()) {
226782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        name = String8(args[index]);
226882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        index++;
226982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
227048b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian
22714b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    const nsecs_t period =
22724b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
22734b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    result.appendFormat("%lld\n", period);
22744b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
22754b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    if (name.isEmpty()) {
22764b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        mAnimFrameTracker.dump(result);
22774b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    } else {
22784b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
22794b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        const size_t count = currentLayers.size();
22804b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        for (size_t i=0 ; i<count ; i++) {
228113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(currentLayers[i]);
22824b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            if (name == layer->getName()) {
22834b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis                layer->dumpStats(result, buffer, SIZE);
22844b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            }
228582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        }
228682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
228782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian}
2288ad70186f80355e55ddad2a66b5fd174a1c019c2dMathias Agopian
228925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopianvoid SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
22904b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        String8& result, char* buffer, size_t SIZE)
229125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian{
229225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    String8 name;
229325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    if (index < args.size()) {
229425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        name = String8(args[index]);
229525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        index++;
229625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    }
229725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
229825e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
229925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const size_t count = currentLayers.size();
230025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
230113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
230225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        if (name.isEmpty() || (name == layer->getName())) {
230325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            layer->clearStats();
230425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        }
230525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    }
23064b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
23074b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    mAnimFrameTracker.clear();
230825e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian}
230925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
23104803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
23114803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden{
23124803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    static const char* config =
23134803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " [sf"
23144803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef NO_RGBX_8888
23154803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " NO_RGBX_8888"
23164803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23174803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef HAS_CONTEXT_PRIORITY
23184803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " HAS_CONTEXT_PRIORITY"
23194803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23204803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
23214803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " NEVER_DEFAULT_TO_ASYNC_MODE"
23224803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23234803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
23244803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " TARGET_DISABLE_TRIPLE_BUFFERING"
23254803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23264803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            "]";
23274803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    result.append(config);
23284803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden}
23294803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden
233082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopianvoid SurfaceFlinger::dumpAllLocked(
233182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        String8& result, char* buffer, size_t SIZE) const
233282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian{
233382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    // figure out if we're stuck somewhere
233482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const nsecs_t now = systemTime();
233582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
233682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const nsecs_t inTransaction(mDebugInTransaction);
233782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
233882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2339bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian
234082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
23414803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden     * Dump library configuration.
23424803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden     */
23434803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    result.append("Build configuration:");
23444803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    appendSfConfigString(result);
23454803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    appendUiConfigString(result);
23464803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    appendGuiConfigString(result);
23474803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    result.append("\n");
23484803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden
23494803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    /*
235082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump the visible layer list
235182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
235282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
235382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const size_t count = currentLayers.size();
235482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
235582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
235682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    for (size_t i=0 ; i<count ; i++) {
235713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
235882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        layer->dump(result, buffer, SIZE);
235982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
2360bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian
236182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
236282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump the layers in the purgatory
236382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
2364ad70186f80355e55ddad2a66b5fd174a1c019c2dMathias Agopian
236582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const size_t purgatorySize = mLayerPurgatory.size();
236682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
236782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
236882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    for (size_t i=0 ; i<purgatorySize ; i++) {
236913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(mLayerPurgatory.itemAt(i));
237082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        layer->shortDump(result, buffer, SIZE);
237182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
23721b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
237382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
23745f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian     * Dump Display state
23755f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian     */
23765f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
23778dfa92fef9759a881e96ee58d59875d35023aab9Andy McFadden    snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
23788dfa92fef9759a881e96ee58d59875d35023aab9Andy McFadden    result.append(buffer);
23795f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
23805f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        const sp<const DisplayDevice>& hw(mDisplays[dpy]);
23811d12d8a8e61163b35cf42c51c558a67138014e82Mathias Agopian        hw->dump(result, buffer, SIZE);
23825f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian    }
23835f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
23845f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian    /*
238582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump SurfaceFlinger global state
238682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
23871b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
238882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
238982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
23901b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
2391888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    HWComposer& hwc(getHwComposer());
23924297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    sp<const DisplayDevice> hw(getDefaultDisplayDevice());
239382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const GLExtensions& extensions(GLExtensions::getInstance());
239482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
239582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            extensions.getVendor(),
239682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            extensions.getRenderer(),
239782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            extensions.getVersion());
239882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
2399d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
240082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "EGL : %s\n",
2401d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian            eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
240282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
240373d3ba9e50be1014aa21ec4bbdc874be394accb4Mathias Agopian
240482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
240582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
24069795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian
24074297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->undefinedRegion.dump(result, "undefinedRegion");
240882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE,
240982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  orientation=%d, canDraw=%d\n",
24104297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            hw->getOrientation(), hw->canDraw());
241182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
241282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE,
241382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  last eglSwapBuffers() time: %f us\n"
241482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  last transaction time     : %f us\n"
2415c95dbdc236acf002b5f8aed8c8a9e43047fc75b5Mathias Agopian            "  transaction-flags         : %08x\n"
241682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  refresh-rate              : %f fps\n"
241782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  x-dpi                     : %f\n"
24188b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            "  y-dpi                     : %f\n",
241982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            mLastSwapBufferTime/1000.0,
242082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            mLastTransactionTime/1000.0,
2421c95dbdc236acf002b5f8aed8c8a9e43047fc75b5Mathias Agopian            mTransactionFlags,
2422b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2423b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2424b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwc.getDpiY(HWC_DISPLAY_PRIMARY));
242582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
242682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
242782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "  eglSwapBuffers time: %f us\n",
242882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            inSwapBuffersDuration/1000.0);
242982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
243082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
243182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "  transaction time: %f us\n",
243282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            inTransactionDuration/1000.0);
243382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
243482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
243582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
243682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * VSYNC state
243782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
243882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    mEventThread->dump(result, buffer, SIZE);
243982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
244082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
244182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump HWComposer state
244282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
244382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "h/w composer state:\n");
244482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
244582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "  h/w composer %s and %s\n",
244682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            hwc.initCheck()==NO_ERROR ? "present" : "not present",
244782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                    (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
244882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
2449cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian    hwc.dump(result, buffer, SIZE);
245082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
245182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
245282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump gralloc state
245382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
245482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
245582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    alloc.dump(result);
2456edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2457edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
245813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianconst Vector< sp<Layer> >&
2459cb55857bbde34a06c19dde3db5064d1717a0173eMathias AgopianSurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
2460db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    // Note: mStateLock is held here
2461cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian    return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2462cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian}
2463cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
246463f165fd6b86d04be94d4023e845e98560504a96Keun young Parkbool SurfaceFlinger::startDdmConnection()
246563f165fd6b86d04be94d4023e845e98560504a96Keun young Park{
246663f165fd6b86d04be94d4023e845e98560504a96Keun young Park    void* libddmconnection_dso =
246763f165fd6b86d04be94d4023e845e98560504a96Keun young Park            dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
246863f165fd6b86d04be94d4023e845e98560504a96Keun young Park    if (!libddmconnection_dso) {
246963f165fd6b86d04be94d4023e845e98560504a96Keun young Park        return false;
247063f165fd6b86d04be94d4023e845e98560504a96Keun young Park    }
247163f165fd6b86d04be94d4023e845e98560504a96Keun young Park    void (*DdmConnection_start)(const char* name);
247263f165fd6b86d04be94d4023e845e98560504a96Keun young Park    DdmConnection_start =
247363f165fd6b86d04be94d4023e845e98560504a96Keun young Park            (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
247463f165fd6b86d04be94d4023e845e98560504a96Keun young Park    if (!DdmConnection_start) {
247563f165fd6b86d04be94d4023e845e98560504a96Keun young Park        dlclose(libddmconnection_dso);
247663f165fd6b86d04be94d4023e845e98560504a96Keun young Park        return false;
247763f165fd6b86d04be94d4023e845e98560504a96Keun young Park    }
247863f165fd6b86d04be94d4023e845e98560504a96Keun young Park    (*DdmConnection_start)(getServiceName());
247963f165fd6b86d04be94d4023e845e98560504a96Keun young Park    return true;
248063f165fd6b86d04be94d4023e845e98560504a96Keun young Park}
248163f165fd6b86d04be94d4023e845e98560504a96Keun young Park
2482edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t SurfaceFlinger::onTransact(
2483edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2484edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
2485edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    switch (code) {
2486edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        case CREATE_CONNECTION:
2487698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian        case SET_TRANSACTION_STATE:
2488edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        case BOOT_FINISHED:
24898e533069e5721e55cb9768e140e16546c3a4a8b6Colin Cross        case BLANK:
24908e533069e5721e55cb9768e140e16546c3a4a8b6Colin Cross        case UNBLANK:
2491edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        {
2492edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // codes that require permission check
2493edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            IPCThreadState* ipc = IPCThreadState::self();
2494edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            const int pid = ipc->getCallingPid();
2495a1ecca920e15ce04fe56ebf4f1b52ba711f9eb2dMathias Agopian            const int uid = ipc->getCallingUid();
249699b49840d309727678b77403d6cc9f920111623fMathias Agopian            if ((uid != AID_GRAPHICS) &&
249799b49840d309727678b77403d6cc9f920111623fMathias Agopian                    !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
2498e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block                ALOGE("Permission Denial: "
2499375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian                        "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2500375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian                return PERMISSION_DENIED;
2501edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
25021b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            break;
25031b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
25041b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        case CAPTURE_SCREEN:
25051b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        {
25061b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            // codes that require permission check
25071b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            IPCThreadState* ipc = IPCThreadState::self();
25081b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            const int pid = ipc->getCallingPid();
25091b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            const int uid = ipc->getCallingUid();
251099b49840d309727678b77403d6cc9f920111623fMathias Agopian            if ((uid != AID_GRAPHICS) &&
251199b49840d309727678b77403d6cc9f920111623fMathias Agopian                    !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
2512e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block                ALOGE("Permission Denial: "
25131b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian                        "can't read framebuffer pid=%d, uid=%d", pid, uid);
25141b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian                return PERMISSION_DENIED;
25151b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            }
25161b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            break;
2517edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
2518edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
25191b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
2520edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2521edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
2522b8a5560e1303cb10f5cd482af466fc04d2bdfcabMathias Agopian        CHECK_INTERFACE(ISurfaceComposer, data, reply);
252399ed22412db547c59d3da08114d9d5a586442b30Glenn Kasten        if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
2524375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian            IPCThreadState* ipc = IPCThreadState::self();
2525375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian            const int pid = ipc->getCallingPid();
2526375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian            const int uid = ipc->getCallingUid();
2527e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block            ALOGE("Permission Denial: "
2528375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian                    "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2529edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            return PERMISSION_DENIED;
2530edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
2531edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        int n;
2532edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        switch (code) {
253301b766839e06c32540cef100e3a7710d12cf1eefMathias Agopian            case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
253435b48d10bc9e064201d3d54d2d476314684a7a05Mathias Agopian            case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
2535edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                return NO_ERROR;
2536edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1002:  // SHOW_UPDATES
2537edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                n = data.readInt32();
2538edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
253953331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                invalidateHwcGeometry();
254053331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                repaintEverything();
2541edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                return NO_ERROR;
2542edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1004:{ // repaint everything
254353331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                repaintEverything();
2544cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian                return NO_ERROR;
2545cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian            }
2546cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian            case 1005:{ // force transaction
2547e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                setTransactionFlags(
2548e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        eTransactionNeeded|
2549e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        eDisplayTransactionNeeded|
2550e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        eTraversalNeeded);
2551cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian                return NO_ERROR;
2552edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
25534d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian            case 1006:{ // send empty update
25544d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian                signalRefresh();
25554d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian                return NO_ERROR;
25564d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian            }
255753331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian            case 1008:  // toggle use of hw composer
255853331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                n = data.readInt32();
255953331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                mDebugDisableHWC = n ? 1 : 0;
256053331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                invalidateHwcGeometry();
256153331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                repaintEverything();
256253331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                return NO_ERROR;
2563a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian            case 1009:  // toggle use of transform hint
2564a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                n = data.readInt32();
2565a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                mDebugDisableTransformHint = n ? 1 : 0;
2566a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                invalidateHwcGeometry();
2567a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                repaintEverything();
2568a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                return NO_ERROR;
2569edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1010:  // interrogate.
257001b766839e06c32540cef100e3a7710d12cf1eefMathias Agopian                reply->writeInt32(0);
2571edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                reply->writeInt32(0);
2572edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                reply->writeInt32(mDebugRegion);
2573b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian                reply->writeInt32(0);
257412839bee29bdcc65731b4d42029cc59e2320c5c4Dianne Hackborn                reply->writeInt32(mDebugDisableHWC);
2575edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                return NO_ERROR;
2576edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1013: {
2577edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                Mutex::Autolock _l(mStateLock);
25784297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian                sp<const DisplayDevice> hw(getDefaultDisplayDevice());
25794297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian                reply->writeInt32(hw->getPageFlipCount());
2580edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
2581edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            return NO_ERROR;
2582edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
2583edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
2584edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return err;
2585edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2586edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
258753331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopianvoid SurfaceFlinger::repaintEverything() {
258887baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    android_atomic_or(1, &mRepaintEverything);
258999ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    signalTransaction();
259053331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian}
259153331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian
259259119e658a12279e8fff508f8773843de2d90917Mathias Agopian// ---------------------------------------------------------------------------
25932a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// Capture screen into an IGraphiBufferProducer
25942a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// ---------------------------------------------------------------------------
259559119e658a12279e8fff508f8773843de2d90917Mathias Agopian
25962a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopianstatus_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
25972a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<IGraphicBufferProducer>& producer,
25982a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, uint32_t reqHeight,
25992a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ) {
26002a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26012a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (CC_UNLIKELY(display == 0))
26022a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26032a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26042a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (CC_UNLIKELY(producer == 0))
26052a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26062a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26072a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    class MessageCaptureScreen : public MessageBase {
26082a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        SurfaceFlinger* flinger;
26092a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        sp<IBinder> display;
26102a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        sp<IGraphicBufferProducer> producer;
26112a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, reqHeight;
26122a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t minLayerZ,maxLayerZ;
26132a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        status_t result;
26142a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    public:
26152a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        MessageCaptureScreen(SurfaceFlinger* flinger,
26162a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                const sp<IBinder>& display,
26172a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                const sp<IGraphicBufferProducer>& producer,
26182a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t reqWidth, uint32_t reqHeight,
26192a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t minLayerZ, uint32_t maxLayerZ)
26202a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            : flinger(flinger), display(display), producer(producer),
26212a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              reqWidth(reqWidth), reqHeight(reqHeight),
26222a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
26232a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              result(PERMISSION_DENIED)
26242a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        {
26252a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
26262a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        status_t getResult() const {
26272a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            return result;
26282a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
26292a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        virtual bool handler() {
26302a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            Mutex::Autolock _l(flinger->mStateLock);
26312a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
26322a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            result = flinger->captureScreenImplLocked(hw, producer,
26332a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    reqWidth, reqHeight, minLayerZ, maxLayerZ);
26342a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            return true;
26352a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
26362a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    };
26372a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26382a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<MessageBase> msg = new MessageCaptureScreen(this,
26392a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
26402a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    status_t res = postMessageSync(msg);
26412a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (res == NO_ERROR) {
26422a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
26432a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26442a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    return res;
2645118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian}
2646118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
26472a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopianstatus_t SurfaceFlinger::captureScreenImplLocked(
26482a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<const DisplayDevice>& hw,
26492a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<IGraphicBufferProducer>& producer,
26502a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, uint32_t reqHeight,
26512a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ)
265259119e658a12279e8fff508f8773843de2d90917Mathias Agopian{
265322ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    ATRACE_CALL();
265422ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian
265559119e658a12279e8fff508f8773843de2d90917Mathias Agopian    // get screen geometry
26564297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    const uint32_t hw_w = hw->getWidth();
26574297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    const uint32_t hw_h = hw->getHeight();
26582a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26592a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // if we have secure windows on this display, never allow the screen capture
26602a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (hw->getSecureLayerVisible()) {
26612a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGW("FB is protected: PERMISSION_DENIED");
26622a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return PERMISSION_DENIED;
26632a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26642a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26652a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
26662a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("size mismatch (%d, %d) > (%d, %d)",
26672a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                reqWidth, reqHeight, hw_w, hw_h);
26682a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26692a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26702a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26712a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    reqWidth = (!reqWidth) ? hw_w : reqWidth;
26722a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    reqHeight = (!reqHeight) ? hw_h : reqHeight;
26732a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
26742a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26752a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // Create a surface to render into
26762a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<Surface> surface = new Surface(producer);
26772a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    ANativeWindow* const window = surface.get();
26782a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26792a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // set the buffer size to what the user requested
26802a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    native_window_set_buffers_user_dimensions(window, reqWidth, reqHeight);
26812a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26822a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // and create the corresponding EGLSurface
26832a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    EGLSurface eglSurface = eglCreateWindowSurface(
26842a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            mEGLDisplay, mEGLConfig, window, NULL);
26852a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (eglSurface == EGL_NO_SURFACE) {
26862a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("captureScreenImplLocked: eglCreateWindowSurface() failed 0x%4x",
26872a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                eglGetError());
26882a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26892a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26902a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26912a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (!eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) {
26922a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("captureScreenImplLocked: eglMakeCurrent() failed 0x%4x",
26932a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                eglGetError());
26942a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        eglDestroySurface(mEGLDisplay, eglSurface);
26952a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26962a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
269759119e658a12279e8fff508f8773843de2d90917Mathias Agopian
269859119e658a12279e8fff508f8773843de2d90917Mathias Agopian    // make sure to clear all GL error flags
269959119e658a12279e8fff508f8773843de2d90917Mathias Agopian    while ( glGetError() != GL_NO_ERROR ) ;
270059119e658a12279e8fff508f8773843de2d90917Mathias Agopian
27012a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // set-up our viewport
27022a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glViewport(0, 0, reqWidth, reqHeight);
27032a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glMatrixMode(GL_PROJECTION);
27042a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glLoadIdentity();
27052a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glOrthof(0, hw_w, 0, hw_h, 0, 1);
27062a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glMatrixMode(GL_MODELVIEW);
27072a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glLoadIdentity();
2708bae92d0d605e99a14731add4f11b72413b2835e5Mathias Agopian
27099daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    // redraw the screen entirely...
2710c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian    glDisable(GL_TEXTURE_EXTERNAL_OES);
2711c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian    glDisable(GL_TEXTURE_2D);
27129daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    glClearColor(0,0,0,1);
27139daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    glClear(GL_COLOR_BUFFER_BIT);
27142a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
2715b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian    const LayerVector& layers( mDrawingState.layersSortedByZ );
27169daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    const size_t count = layers.size();
27179daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    for (size_t i=0 ; i<count ; ++i) {
271813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(layers[i]);
2719b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian        const Layer::State& state(layer->drawingState());
2720b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian        if (state.layerStack == hw->getLayerStack()) {
2721b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian            if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2722b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian                if (filtering) layer->setFiltering(true);
2723b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian                layer->draw(hw);
2724b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian                if (filtering) layer->setFiltering(false);
2725b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian            }
27262a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
27279daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    }
272859119e658a12279e8fff508f8773843de2d90917Mathias Agopian
2729d577641411f067c93aabcd2acf7ce06862fb26d3Mathias Agopian    // compositionComplete is needed for older driver
2730d577641411f067c93aabcd2acf7ce06862fb26d3Mathias Agopian    hw->compositionComplete();
2731d577641411f067c93aabcd2acf7ce06862fb26d3Mathias Agopian
27322a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // and finishing things up...
27332a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (eglSwapBuffers(mEGLDisplay, eglSurface) != EGL_TRUE) {
27342a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("captureScreenImplLocked: eglSwapBuffers() failed 0x%4x",
27352a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                eglGetError());
27362a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        eglDestroySurface(mEGLDisplay, eglSurface);
27372a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
27382a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
273959119e658a12279e8fff508f8773843de2d90917Mathias Agopian
27402a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    eglDestroySurface(mEGLDisplay, eglSurface);
27419daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    return NO_ERROR;
27429daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian}
274359119e658a12279e8fff508f8773843de2d90917Mathias Agopian
27449daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian// ---------------------------------------------------------------------------
27452a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// Capture screen into an IMemoryHeap (legacy)
27462a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// ---------------------------------------------------------------------------
27479daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian
27482a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopianstatus_t SurfaceFlinger::captureScreenImplLocked(
27492a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<const DisplayDevice>& hw,
275074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        sp<IMemoryHeap>* heap,
275174c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        uint32_t* w, uint32_t* h, PixelFormat* f,
2752bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t sw, uint32_t sh,
2753bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ)
275474c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian{
2755fddc28d87136b55f0d9613e5f8ecd64a6aca018dMathias Agopian    ATRACE_CALL();
2756fddc28d87136b55f0d9613e5f8ecd64a6aca018dMathias Agopian
27573b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian    if (!GLExtensions::getInstance().haveFramebufferObject()) {
275874c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        return INVALID_OPERATION;
27593b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian    }
276074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
27612a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // create the texture that will receive the screenshot, later we'll
27622a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // attach a FBO to it so we can call glReadPixels().
27632a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    GLuint tname;
27642a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glGenTextures(1, &tname);
27652a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glBindTexture(GL_TEXTURE_2D, tname);
27662a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
27672a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
276874c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
27692a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // the GLConsumer will provide the BufferQueue
27702a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<GLConsumer> consumer = new GLConsumer(tname, true, GL_TEXTURE_2D);
27712a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    consumer->getBufferQueue()->setDefaultBufferFormat(HAL_PIXEL_FORMAT_RGBA_8888);
27722a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
27732a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // call the new screenshot taking code, passing a BufferQueue to it
27742a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    status_t result = captureScreenImplLocked(hw,
27752a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            consumer->getBufferQueue(), sw, sh, minLayerZ, maxLayerZ);
27762a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
27772a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (result == NO_ERROR) {
27782a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        result = consumer->updateTexImage();
27792a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        if (result == NO_ERROR) {
27802a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            // create a FBO
27812a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            GLuint name;
27822a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glGenFramebuffersOES(1, &name);
27832a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
27842a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
27852a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
27862a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
27872a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sp<GraphicBuffer> buf(consumer->getCurrentBuffer());
27882a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sw = buf->getWidth();
27892a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sh = buf->getHeight();
27902a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            size_t size = buf->getStride() * sh * 4;
2791f653b897a449e10d6cbfb6e0812f7b0bb02d6482Mathias Agopian
279274c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            // allocate shared memory large enough to hold the
279374c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            // screen capture
279474c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            sp<MemoryHeapBase> base(
279574c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    new MemoryHeapBase(size, 0, "screen-capture") );
279674c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            void* const ptr = base->getBase();
27977b1905113712281c777b230648d3fbb69ae4f42cMathias Agopian            if (ptr != MAP_FAILED) {
279874c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                // capture the screen with glReadPixels()
2799fddc28d87136b55f0d9613e5f8ecd64a6aca018dMathias Agopian                ScopedTrace _t(ATRACE_TAG, "glReadPixels");
280074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
280174c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                if (glGetError() == GL_NO_ERROR) {
280274c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *heap = base;
280374c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *w = sw;
280474c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *h = sh;
280574c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *f = PIXEL_FORMAT_RGBA_8888;
280674c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    result = NO_ERROR;
2807175264b09c6080b29a23fc9f545d4b99445714bdJeff Sharkey                } else {
2808175264b09c6080b29a23fc9f545d4b99445714bdJeff Sharkey                    result = NO_MEMORY;
280974c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                }
281074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            } else {
281174c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                result = NO_MEMORY;
281274c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            }
28132a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
28142a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            // back to main framebuffer
28152a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
28162a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glDeleteFramebuffersOES(1, &name);
281774c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        }
281874c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian    }
281974c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
28202a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glDeleteTextures(1, &tname);
2821c1d1b0d9b74d8f55346d0b84f369e48ecf2e0d33Mathias Agopian
28222be4e8ff23f24285f4c195ba5537c7a51c13be4dMathias Agopian    DisplayDevice::makeCurrent(mEGLDisplay,
28232be4e8ff23f24285f4c195ba5537c7a51c13be4dMathias Agopian            getDefaultDisplayDevice(), mEGLContext);
28242be4e8ff23f24285f4c195ba5537c7a51c13be4dMathias Agopian
282574c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian    return result;
282674c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian}
282774c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
28289d4e3d2f42e93e2d12bacabe97d307d30c3c20ddJeff Brownstatus_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
28291b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        sp<IMemoryHeap>* heap,
28302a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
28312a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, uint32_t reqHeight,
2832bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ)
28331b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian{
28349d4e3d2f42e93e2d12bacabe97d307d30c3c20ddJeff Brown    if (CC_UNLIKELY(display == 0))
28351b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        return BAD_VALUE;
28361b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
28371b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    class MessageCaptureScreen : public MessageBase {
28381b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        SurfaceFlinger* flinger;
28399d4e3d2f42e93e2d12bacabe97d307d30c3c20ddJeff Brown        sp<IBinder> display;
28401b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        sp<IMemoryHeap>* heap;
28412a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t* outWidth;
28422a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t* outHeight;
28432a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        PixelFormat* outFormat;
28442a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth;
28452a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqHeight;
2846bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t minLayerZ;
2847bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t maxLayerZ;
28481b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        status_t result;
28491b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    public:
28502a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        MessageCaptureScreen(SurfaceFlinger* flinger,
28512a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                const sp<IBinder>& display, sp<IMemoryHeap>* heap,
28522a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
28532a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t reqWidth, uint32_t reqHeight,
2854bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian                uint32_t minLayerZ, uint32_t maxLayerZ)
28552a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            : flinger(flinger), display(display), heap(heap),
28562a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              outWidth(outWidth), outHeight(outHeight), outFormat(outFormat),
28572a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              reqWidth(reqWidth), reqHeight(reqHeight),
2858bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian              minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2859bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian              result(PERMISSION_DENIED)
28601b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        {
28611b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
28621b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        status_t getResult() const {
28631b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            return result;
28641b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
28651b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        virtual bool handler() {
28661b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            Mutex::Autolock _l(flinger->mStateLock);
28672a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
28682a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            result = flinger->captureScreenImplLocked(hw, heap,
28692a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    outWidth, outHeight, outFormat,
28702a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    reqWidth, reqHeight, minLayerZ, maxLayerZ);
28711b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            return true;
28721b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
28731b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    };
28741b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
28752a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<MessageBase> msg = new MessageCaptureScreen(this, display, heap,
28762a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            outWidth, outHeight, outFormat,
28772a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            reqWidth, reqHeight, minLayerZ, maxLayerZ);
28781b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    status_t res = postMessageSync(msg);
28791b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    if (res == NO_ERROR) {
28801b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
28811b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    }
28821b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    return res;
28831b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian}
28841b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
28851b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian// ---------------------------------------------------------------------------
28861b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
2887921e6ac4b7610a178285898d191eb0e3afe906c0Mathias AgopianSurfaceFlinger::LayerVector::LayerVector() {
2888921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian}
2889921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
2890921e6ac4b7610a178285898d191eb0e3afe906c0Mathias AgopianSurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
289113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    : SortedVector<sp<Layer> >(rhs) {
2892921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian}
2893921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
2894921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopianint SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2895921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    const void* rhs) const
2896edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
2897be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    // sort layers per layer-stack, then by z-order and finally by sequence
289813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
289913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
2900be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian
2901be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    uint32_t ls = l->currentState().layerStack;
2902be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    uint32_t rs = r->currentState().layerStack;
2903be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    if (ls != rs)
2904be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian        return ls - rs;
2905be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian
2906921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    uint32_t lz = l->currentState().z;
2907921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    uint32_t rz = r->currentState().z;
2908be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    if (lz != rz)
2909be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian        return lz - rz;
2910be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian
2911be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    return l->sequence - r->sequence;
2912921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian}
2913921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
2914921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian// ---------------------------------------------------------------------------
2915921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
29163ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias AgopianSurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
29173ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    : type(DisplayDevice::DISPLAY_ID_INVALID) {
2918e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
2919e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
29203ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias AgopianSurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
292101e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
2922da8d0a5c0cf9d41915d3b106cad4aaec3e767c11Mathias Agopian    viewport.makeInvalid();
2923da8d0a5c0cf9d41915d3b106cad4aaec3e767c11Mathias Agopian    frame.makeInvalid();
2924b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian}
29257303c6bf1a8b00a0e7d8165d774a1f259b4ccda9Mathias Agopian
2926b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian// ---------------------------------------------------------------------------
292796f0819f81293076e652792794a961543e6750d7Mathias Agopian
2928edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}; // namespace android
2929