SurfaceFlinger.cpp revision 7adb0f8a9fdb961692ffd2f0c65cacb155143f64
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);
11347adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall                        if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
11357adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall                            mEventThread->onHotplugReceived(draw[i].type, false);
113692a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    } else {
113792a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                        ALOGW("trying to remove the main display");
113892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    }
113992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                } else {
114092a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    // this display is in both lists. see if something changed.
1141e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                    const DisplayDeviceState& state(curr[j]);
11423ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian                    const wp<IBinder>& display(curr.keyAt(j));
1143111b2d89221722d38f5b5b3ba65904ec22421839Mathias Agopian                    if (state.surface->asBinder() != draw[i].surface->asBinder()) {
1144e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        // changing the surface is like destroying and
114593997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // recreating the DisplayDevice, so we just remove it
114693997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // from the drawing state, so that it get re-added
114793997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // below.
114893997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        mDisplays.removeItem(display);
114993997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        mDrawingState.displays.removeItemsAt(i);
115093997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        dc--; i--;
115193997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        // at this point we must loop to the next item
115293997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        continue;
115392a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    }
115493997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian
1155db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                    const sp<DisplayDevice> disp(getDisplayDevice(display));
115693997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                    if (disp != NULL) {
115793997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        if (state.layerStack != draw[i].layerStack) {
115893997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                            disp->setLayerStack(state.layerStack);
115993997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        }
116000e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                        if ((state.orientation != draw[i].orientation)
116100e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                                || (state.viewport != draw[i].viewport)
116200e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                                || (state.frame != draw[i].frame))
116300e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                        {
116400e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian                            disp->setProjection(state.orientation,
11654fb3999cea652617be5125f8a42c257467bf3c77Jeff Brown                                    state.viewport, state.frame);
116693997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                        }
116792a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                    }
116892a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                }
116992a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            }
117092a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian
117192a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // find displays that were added
117292a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            // (ie: in current state but not in drawing state)
117392a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            for (size_t i=0 ; i<cc ; i++) {
1174e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                if (draw.indexOfKey(curr.keyAt(i)) < 0) {
1175e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                    const DisplayDeviceState& state(curr[i]);
1176cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1177cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    sp<FramebufferSurface> fbs;
1178e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian                    sp<Surface> stc;
1179cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    if (!state.isVirtualDisplay()) {
1180cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1181cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        ALOGE_IF(state.surface!=NULL,
1182cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                                "adding a supported display, but rendering "
1183cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                                "surface is provided (%p), ignoring it",
1184cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                                state.surface.get());
1185cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1186cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        // for supported (by hwc) displays we provide our
1187cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        // own rendering surface
1188f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian                        fbs = new FramebufferSurface(*mHwc, state.type);
1189e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian                        stc = new Surface(
11902adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden                                static_cast< sp<IGraphicBufferProducer> >(
1191dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis                                        fbs->getBufferQueue()));
1192cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    } else {
1193cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        if (state.surface != NULL) {
1194e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian                            stc = new Surface(state.surface);
1195cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        }
1196cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    }
1197cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
1198cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    const wp<IBinder>& display(curr.keyAt(i));
1199cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                    if (stc != NULL) {
1200cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        sp<DisplayDevice> hw = new DisplayDevice(this,
1201692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall                                state.type, state.isSecure, display, stc, fbs,
1202dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis                                mEGLConfig);
1203cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        hw->setLayerStack(state.layerStack);
1204cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        hw->setProjection(state.orientation,
12054fb3999cea652617be5125f8a42c257467bf3c77Jeff Brown                                state.viewport, state.frame);
12068dfa92fef9759a881e96ee58d59875d35023aab9Andy McFadden                        hw->setDisplayName(state.displayName);
1207cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian                        mDisplays.add(display, hw);
12087adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall                        if (state.type < DisplayDevice::NUM_DISPLAY_TYPES)
12097adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall                            mEventThread->onHotplugReceived(state.type, true);
121093997a8a75942b4d06cf50925de5bede489cc134Mathias Agopian                    }
121192a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian                }
121292a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian            }
1213edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
12143559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    }
1215edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
12168430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian    if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
12178430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // The transform hint might have changed for some layers
12188430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // (either because a display has changed, or because a layer
12198430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // as changed).
12208430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12218430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // Walk through all the layers in currentLayers,
12228430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // and update their transform hint.
12238430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12248430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // If a layer is visible only on a single display, then that
12258430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // display is used to calculate the hint, otherwise we use the
12268430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // default display.
12278430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12288430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // NOTE: we do this here, rather than in rebuildLayerStacks() so that
12298430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // the hint is set before we acquire a buffer from the surface texture.
12308430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12318430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // NOTE: layer transactions have taken place already, so we use their
12328430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // drawing state. However, SurfaceFlinger's own transaction has not
12338430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // happened yet, so we must use the current state layer list
12348430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        // (soon to become the drawing state list).
12358430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        //
12368430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        sp<const DisplayDevice> disp;
12378430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        uint32_t currentlayerStack = 0;
12388430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        for (size_t i=0; i<count; i++) {
12398430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            // NOTE: we rely on the fact that layers are sorted by
12408430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            // layerStack first (so we don't have to traverse the list
12418430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            // of displays for every layer).
124213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(currentLayers[i]);
124313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            uint32_t layerStack = layer->drawingState().layerStack;
12448430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            if (i==0 || currentlayerStack != layerStack) {
12458430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                currentlayerStack = layerStack;
12468430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // figure out if this layerstack is mirrored
12478430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // (more than one display) if so, pick the default display,
12488430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // if not, pick the only display it's on.
12498430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                disp.clear();
12508430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
12518430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                    sp<const DisplayDevice> hw(mDisplays[dpy]);
12528430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                    if (hw->getLayerStack() == currentlayerStack) {
12538430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                        if (disp == NULL) {
12548430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                            disp = hw;
12558430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                        } else {
12568430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                            disp = getDefaultDisplayDevice();
12578430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                            break;
12588430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                        }
12598430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                    }
12608430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                }
12618430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            }
12628430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            if (disp != NULL) {
12638430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // presumably this means this layer is using a layerStack
12648430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian                // that is not visible on any display
126513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                layer->updateTransformHint(disp);
12668430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian            }
12678430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian        }
12688430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian    }
12698430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian
12708430095879d2fa6878e68f8f12da4e704815ac09Mathias Agopian
12713559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    /*
12723559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian     * Perform our own transaction if needed
12733559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian     */
1274edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1275cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1276cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    if (currentLayers.size() > previousLayers.size()) {
12773559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        // layers have been added
12783559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        mVisibleRegionsDirty = true;
12793559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    }
12803559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian
12813559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    // some layers might have been removed, so
12823559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    // we need to update the regions they're exposing.
12833559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian    if (mLayersRemoved) {
12843559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        mLayersRemoved = false;
12853559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        mVisibleRegionsDirty = true;
12863559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        const size_t count = previousLayers.size();
12873559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        for (size_t i=0 ; i<count ; i++) {
128813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(previousLayers[i]);
12893559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian            if (currentLayers.indexOf(layer) < 0) {
12903559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                // this layer is not visible anymore
12913559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                // TODO: we could traverse the tree from front to back and
12923559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                //       compute the actual visible region
12933559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian                // TODO: we could cache the transformed region
12941501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                const Layer::State& s(layer->drawingState());
12951501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                Region visibleReg = s.transform.transform(
12961501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                        Region(Rect(s.active.w, s.active.h)));
12971501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian                invalidateLayerStack(s.layerStack, visibleReg);
12980aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian            }
1299edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1300edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1301edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1302edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    commitTransaction();
13034fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian}
13044fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian
13054fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopianvoid SurfaceFlinger::commitTransaction()
13064fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian{
13074fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    if (!mLayersPendingRemoval.isEmpty()) {
13084fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        // Notify removed layers now that they can't be drawn from
13094fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
13104fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            mLayersPendingRemoval[i]->onRemoved();
13114fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        }
13124fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        mLayersPendingRemoval.clear();
13134fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    }
13144fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian
13154b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    // If this transaction is part of a window animation then the next frame
13164b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    // we composite should be considered an animation as well.
13174b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    mAnimCompositionPending = mAnimTransactionPending;
13184b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
13194fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    mDrawingState = mCurrentState;
13202d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    mTransactionPending = false;
13212d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    mAnimTransactionPending = false;
13224fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    mTransactionCV.broadcast();
1323edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1324edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1325edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid SurfaceFlinger::computeVisibleRegions(
132687baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const LayerVector& currentLayers, uint32_t layerStack,
132787baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        Region& outDirtyRegion, Region& outOpaqueRegion)
1328edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1329841cde55549cea7a344a1705b18d57a0c6c8ec45Mathias Agopian    ATRACE_CALL();
1330841cde55549cea7a344a1705b18d57a0c6c8ec45Mathias Agopian
1331edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Region aboveOpaqueLayers;
1332edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Region aboveCoveredLayers;
1333edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Region dirty;
1334edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
133587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    outDirtyRegion.clear();
1336edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1337edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t i = currentLayers.size();
1338edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while (i--) {
133913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer = currentLayers[i];
1340edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1341edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // start with the whole surface at its current location
1342970112231e887c8a14441ec4fed55342e019fc8cMathias Agopian        const Layer::State& s(layer->drawingState());
1343edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
134401e29054e672301e4adbbca15b3562a59a20f267Jesse Hall        // only consider the layers on the given layer stack
134587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        if (s.layerStack != layerStack)
134687baae104a3e4c2059990b01c393476065c558b0Mathias Agopian            continue;
134787baae104a3e4c2059990b01c393476065c558b0Mathias Agopian
1348ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        /*
1349ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * opaqueRegion: area of a surface that is fully opaque.
1350ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         */
1351edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        Region opaqueRegion;
1352ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1353ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        /*
1354ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * visibleRegion: area of a surface that is visible on screen
1355ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * and not fully transparent. This is essentially the layer's
1356ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * footprint minus the opaque regions above it.
1357ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * Areas covered by a translucent surface are considered visible.
1358ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         */
1359edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        Region visibleRegion;
1360ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1361ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        /*
1362ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * coveredRegion: area of a surface that is covered by all
1363ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         * visible regions above it (which includes the translucent areas).
1364ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian         */
1365edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        Region coveredRegion;
1366ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1367a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        /*
1368a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * transparentRegion: area of a surface that is hinted to be completely
1369a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * transparent. This is only used to tell when the layer has no visible
1370a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * non-transparent regions and can be removed from the layer list. It
1371a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * does not affect the visibleRegion of this layer or any layers
1372a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * beneath it. The hint may not be correct if apps don't respect the
1373a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         * SurfaceView restrictions (which, sadly, some don't).
1374a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall         */
1375a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        Region transparentRegion;
1376a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall
1377ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1378ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // handle hidden surfaces by setting the visible region to empty
1379da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (CC_LIKELY(layer->isVisible())) {
1380a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian            const bool translucent = !layer->isOpaque();
13815219a06d61ac4517506500363c5e8a5972dd7ac9Mathias Agopian            Rect bounds(s.transform.transform(layer->computeBounds()));
1382edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            visibleRegion.set(bounds);
1383ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            if (!visibleRegion.isEmpty()) {
1384ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                // Remove the transparent area from the visible region
1385ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                if (translucent) {
13864fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    const Transform tr(s.transform);
13874fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    if (tr.transformed()) {
13884fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                        if (tr.preserveRects()) {
13894fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                            // transform the transparent region
1390a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                            transparentRegion = tr.transform(s.transparentRegion);
13914fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                        } else {
13924fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                            // transformation too complex, can't do the
13934fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                            // transparent region optimization.
1394a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                            transparentRegion.clear();
13954fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                        }
13964fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    } else {
1397a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                        transparentRegion = s.transparentRegion;
13984fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                    }
1399ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                }
1400edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1401ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                // compute the opaque region
14024fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian                const int32_t layerOrientation = s.transform.getOrientation();
1403ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                if (s.alpha==255 && !translucent &&
1404ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                        ((layerOrientation & Transform::ROT_INVALID) == false)) {
1405ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                    // the opaque region is the layer's footprint
1406ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                    opaqueRegion = visibleRegion;
1407ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian                }
1408edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
1409edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1410edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1411ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // Clip the covered region to the visible region
1412ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1413ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1414ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // Update aboveCoveredLayers for next (lower) layer
1415ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        aboveCoveredLayers.orSelf(visibleRegion);
1416ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian
1417edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // subtract the opaque region covered by the layers above us
1418edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        visibleRegion.subtractSelf(aboveOpaqueLayers);
1419edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1420edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // compute this layer's dirty region
1421edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (layer->contentDirty) {
1422edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // we need to invalidate the whole region
1423edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            dirty = visibleRegion;
1424edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // as well, as the old visible region
14254fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            dirty.orSelf(layer->visibleRegion);
1426edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            layer->contentDirty = false;
1427edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        } else {
1428a8d44f75e1934072713371a2dcd143c63ffcbe0eMathias Agopian            /* compute the exposed region:
1429ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *   the exposed region consists of two components:
1430ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *   1) what's VISIBLE now and was COVERED before
1431ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *   2) what's EXPOSED now less what was EXPOSED before
1432ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *
1433ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * note that (1) is conservative, we start with the whole
1434ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * visible region but only keep what used to be covered by
1435ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * something -- which mean it may have been exposed.
1436ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             *
1437ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * (2) handles areas that were not covered by anything but got
1438ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian             * exposed because of a resize.
1439a8d44f75e1934072713371a2dcd143c63ffcbe0eMathias Agopian             */
1440ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            const Region newExposed = visibleRegion - coveredRegion;
14414fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            const Region oldVisibleRegion = layer->visibleRegion;
14424fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            const Region oldCoveredRegion = layer->coveredRegion;
1443ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1444ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian            dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
1445edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1446edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        dirty.subtractSelf(aboveOpaqueLayers);
1447edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1448edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // accumulate to the screen dirty region
144987baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        outDirtyRegion.orSelf(dirty);
1450edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1451ab02873e8f4e235b0db800521dd0c969ed38c0b9Mathias Agopian        // Update aboveOpaqueLayers for next (lower) layer
1452edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        aboveOpaqueLayers.orSelf(opaqueRegion);
14538b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber
1454a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        // Store the visible region in screen space
1455edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        layer->setVisibleRegion(visibleRegion);
1456edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        layer->setCoveredRegion(coveredRegion);
1457a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall        layer->setVisibleNonTransparentRegion(
1458a8026d21f3f2f09e3416cbd33c277fbd15d8cf4eJesse Hall                visibleRegion.subtract(transparentRegion));
1459edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1460edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
146187baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    outOpaqueRegion = aboveOpaqueLayers;
1462edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1463edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
146487baae104a3e4c2059990b01c393476065c558b0Mathias Agopianvoid SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
146587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const Region& dirty) {
146692a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
14674297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        const sp<DisplayDevice>& hw(mDisplays[dpy]);
14684297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        if (hw->getLayerStack() == layerStack) {
14694297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            hw->dirtyRegion.orSelf(dirty);
147092a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian        }
147192a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    }
147287baae104a3e4c2059990b01c393476065c558b0Mathias Agopian}
147387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian
147487baae104a3e4c2059990b01c393476065c558b0Mathias Agopianvoid SurfaceFlinger::handlePageFlip()
1475edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
14764fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    Region dirtyRegion;
147799ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
14784fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    bool visibleRegions = false;
1479cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
14804fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    const size_t count = currentLayers.size();
14814fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    for (size_t i=0 ; i<count ; i++) {
148213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
148387baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const Region dirty(layer->latchBuffer(visibleRegions));
14841501d54d63c55dc4e8f4c6eeaeac35aca6660ffcMathias Agopian        const Layer::State& s(layer->drawingState());
148587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        invalidateLayerStack(s.layerStack, dirty);
14864fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian    }
14874da751999358fffa4cefc4c8046dab72045925f6Mathias Agopian
14883b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian    mVisibleRegionsDirty |= visibleRegions;
1489edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1490edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1491ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopianvoid SurfaceFlinger::invalidateHwcGeometry()
1492ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian{
1493ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian    mHwWorkListDirty = true;
1494ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian}
1495ad456f9878ff7c176499e7b992f9ff1cb3e9cdeeMathias Agopian
149699ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian
1497cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
149887baae104a3e4c2059990b01c393476065c558b0Mathias Agopian        const Region& inDirtyRegion)
1499edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
150087baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    Region dirtyRegion(inDirtyRegion);
150187baae104a3e4c2059990b01c393476065c558b0Mathias Agopian
1502b8a5560e1303cb10f5cd482af466fc04d2bdfcabMathias Agopian    // compute the invalid region
15034297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->swapRegion.orSelf(dirtyRegion);
1504edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
15054297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    uint32_t flags = hw->getFlags();
15060f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian    if (flags & DisplayDevice::SWAP_RECTANGLE) {
150729d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian        // we can redraw only what's dirty, but since SWAP_RECTANGLE only
150829d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian        // takes a rectangle, we must make sure to update that whole
150929d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian        // rectangle in that case
15104297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian        dirtyRegion.set(hw->swapRegion.bounds());
1511edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    } else {
15120f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian        if (flags & DisplayDevice::PARTIAL_UPDATES) {
151329d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian            // We need to redraw the rectangle that will be updated
1514df3ca30bf663cb8eed88ee3f16fb5e9a65dc00fcMathias Agopian            // (pushed to the framebuffer).
151595a666b0e001ebf738418b89d8e8fd50b18110f8Mathias Agopian            // This is needed because PARTIAL_UPDATES only takes one
15160f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian            // rectangle instead of a region (see DisplayDevice::flip())
15174297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            dirtyRegion.set(hw->swapRegion.bounds());
1518edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        } else {
151929d06ac9a45e564b4f935b91067fc92c2d4c193dMathias Agopian            // we need to redraw everything (the whole screen)
15204297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            dirtyRegion.set(hw->bounds());
15214297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            hw->swapRegion = dirtyRegion;
1522edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1523edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1524edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1525cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian    doComposeSurfaces(hw, dirtyRegion);
1526edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
15279c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    // update the swap region and clear the dirty region
15284297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->swapRegion.orSelf(dirtyRegion);
1529da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1530da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // swap buffers (presentation)
1531da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    hw->swapBuffers(getHwComposer());
1532edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1533edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1534cd60f99aba9e750700a967db30b74a29145739cfMathias Agopianvoid SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
1535edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
153685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const int32_t id = hw->getHwcDisplayId();
15378630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    HWComposer& hwc(getHwComposer());
15381e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    HWComposer::LayerListIterator cur = hwc.begin(id);
15391e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    const HWComposer::LayerListIterator end = hwc.end(id);
1540a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
154185d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
154285d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    if (hasGlesComposition) {
1543da8d0a5c0cf9d41915d3b106cad4aaec3e767c11Mathias Agopian        DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
1544a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
154552bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        // set the frame buffer
154652bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        glMatrixMode(GL_MODELVIEW);
154752bbb1ae239c8a4d05543a23fa8c08467d09c3b2Mathias Agopian        glLoadIdentity();
1548a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1549a2f4e56fec0fb36c4a370eb23d6e9dc57f250b59Mathias Agopian        // Never touch the framebuffer if we don't have any framebuffer layers
155085d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        const bool hasHwcComposition = hwc.hasHwcComposition(id);
1551e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        if (hasHwcComposition) {
1552b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // when using overlays, we assume a fully transparent framebuffer
1553b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // NOTE: we could reduce how much we need to clear, for instance
1554b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // remove where there are opaque FB layers. however, on some
1555b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // GPUs doing a "clean slate" glClear might be more efficient.
1556b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // We'll revisit later if needed.
1557b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            glClearColor(0, 0, 0, 0);
1558b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            glClear(GL_COLOR_BUFFER_BIT);
1559b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian        } else {
1560766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // we start with the whole screen area
1561766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            const Region bounds(hw->getBounds());
1562766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1563766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // we remove the scissor part
1564766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // we're left with the letterbox region
1565766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // (common case is that letterbox ends-up being empty)
1566766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            const Region letterbox(bounds.subtract(hw->getScissor()));
1567766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1568766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // compute the area to clear
1569766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            Region region(hw->undefinedRegion.merge(letterbox));
1570766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1571766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // but limit it to the dirty region
1572766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            region.andSelf(dirty);
1573766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian
1574b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            // screen is already cleared here
157587baae104a3e4c2059990b01c393476065c558b0Mathias Agopian            if (!region.isEmpty()) {
1576b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian                // can happen with SurfaceView
157755801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian                drawWormhole(hw, region);
1578b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian            }
1579a2f4e56fec0fb36c4a370eb23d6e9dc57f250b59Mathias Agopian        }
1580f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian
1581766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian        if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1582766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            // just to be on the safe side, we don't set the
1583f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            // scissor on the main display. It should never be needed
1584f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            // anyways (though in theory it could since the API allows it).
1585f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            const Rect& bounds(hw->getBounds());
1586766dc49c17dda977bf7b93a5fd8da41c0b737611Mathias Agopian            const Rect& scissor(hw->getScissor());
1587f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            if (scissor != bounds) {
1588f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // scissor doesn't match the screen's dimensions, so we
1589f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // need to clear everything outside of it and enable
1590f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // the GL scissor so we don't draw anything where we shouldn't
1591f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                const GLint height = hw->getHeight();
1592f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                glScissor(scissor.left, height - scissor.bottom,
1593f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                        scissor.getWidth(), scissor.getHeight());
1594f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                // enable scissor for this frame
1595f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian                glEnable(GL_SCISSOR_TEST);
1596f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian            }
1597f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian        }
159885d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    }
15994b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian
160085d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    /*
160185d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian     * and then, render the layers targeted at the framebuffer
160285d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian     */
16034b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian
160413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
160585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const size_t count = layers.size();
160685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    const Transform& tr = hw->getTransform();
160785d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    if (cur != end) {
160885d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        // we're using h/w composer
160985d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
161013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(layers[i]);
16114fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian            const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
161285d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            if (!clip.isEmpty()) {
161385d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                switch (cur->getCompositionType()) {
161485d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    case HWC_OVERLAY: {
161585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                        if ((cur->getHints() & HWC_HINT_CLEAR_FB)
161685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                                && i
161785d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                                && layer->isOpaque()
161885d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                                && hasGlesComposition) {
1619cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                            // never clear the very first layer since we're
1620cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                            // guaranteed the FB is already cleared
1621cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                            layer->clearWithOpenGL(hw, clip);
1622cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                        }
162385d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                        break;
162485d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    }
162585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    case HWC_FRAMEBUFFER: {
1626cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                        layer->draw(hw, clip);
162785d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                        break;
1628a2f4e56fec0fb36c4a370eb23d6e9dc57f250b59Mathias Agopian                    }
1629da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    case HWC_FRAMEBUFFER_TARGET: {
1630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        // this should not happen as the iterator shouldn't
1631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        // let us get there.
1632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        break;
1634da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
1635cd60f99aba9e750700a967db30b74a29145739cfMathias Agopian                }
1636a6b32db164e7834e211261046f3229bf50bc0098Jesse Hall            }
163785d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            layer->setAcquireFence(hw, *cur);
163885d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        }
163985d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian    } else {
164085d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        // we're not using h/w composer
164185d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian        for (size_t i=0 ; i<count ; ++i) {
164213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(layers[i]);
164385d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            const Region clip(dirty.intersect(
164485d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                    tr.transform(layer->visibleRegion)));
164585d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            if (!clip.isEmpty()) {
164685d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian                layer->draw(hw, clip);
164785d751cba5d4386c739dbf9dd8f7bbf8c493ade9Mathias Agopian            }
16484b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian        }
16494b2ba53423ac2dc795a5a0bf72f6d787d9dc8950Mathias Agopian    }
1650f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian
1651f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian    // disable scissor at the end of the frame
1652f45c510009edab4a3e93f8d66b2e30aa26759fedMathias Agopian    glDisable(GL_SCISSOR_TEST);
1653edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1654edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
165555801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopianvoid SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
165655801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        const Region& region) const
1657edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1658f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    glDisable(GL_TEXTURE_EXTERNAL_OES);
1659b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian    glDisable(GL_TEXTURE_2D);
1660f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    glDisable(GL_BLEND);
1661b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian    glColor4f(0,0,0,0);
1662f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian
166355801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian    const int32_t height = hw->getHeight();
1664f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    Region::const_iterator it = region.begin();
1665f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    Region::const_iterator const end = region.end();
1666f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    while (it != end) {
1667f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian        const Rect& r = *it++;
166855801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        GLfloat vertices[][2] = {
16698f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.left,  (GLfloat) (height - r.top) },
16708f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.left,  (GLfloat) (height - r.bottom) },
16718f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
16728f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden                { (GLfloat) r.right, (GLfloat) (height - r.top) }
167355801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        };
167455801e41e6d7306d75d8134dd69d7d8cbbfbc63cMathias Agopian        glVertexPointer(2, GL_FLOAT, 0, vertices);
1675f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1676f74e8e0602100e048c13ea262f0c19c3b8738b6fMathias Agopian    }
1677edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1678edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1679ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopianvoid SurfaceFlinger::addClientLayer(const sp<Client>& client,
1680ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopian        const sp<IBinder>& handle,
168113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& lbc)
16821b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian{
168396f0819f81293076e652792794a961543e6750d7Mathias Agopian    // attach this layer to the client
1684ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopian    client->attachLayer(handle, lbc);
16854f113740180b6512b43723c4728f262882dc9b45Mathias Agopian
168696f0819f81293076e652792794a961543e6750d7Mathias Agopian    // add this layer to the current state list
1687921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    Mutex::Autolock _l(mStateLock);
1688921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    mCurrentState.layersSortedByZ.add(lbc);
168996f0819f81293076e652792794a961543e6750d7Mathias Agopian}
169096f0819f81293076e652792794a961543e6750d7Mathias Agopian
169113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
169296f0819f81293076e652792794a961543e6750d7Mathias Agopian{
169396f0819f81293076e652792794a961543e6750d7Mathias Agopian    Mutex::Autolock _l(mStateLock);
169496f0819f81293076e652792794a961543e6750d7Mathias Agopian    status_t err = purgatorizeLayer_l(layer);
169596f0819f81293076e652792794a961543e6750d7Mathias Agopian    if (err == NO_ERROR)
16963559b07a885bcdff51a6dffb8e3a5ac5adf3a220Mathias Agopian        setTransactionFlags(eTransactionNeeded);
169796f0819f81293076e652792794a961543e6750d7Mathias Agopian    return err;
1698edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1699edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
170013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::removeLayer_l(const sp<Layer>& layer)
1701edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
170213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
1703edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (index >= 0) {
1704076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        mLayersRemoved = true;
1705edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return NO_ERROR;
1706edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
17073d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian    return status_t(index);
1708edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1709edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
171013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::purgatorizeLayer_l(const sp<Layer>& layer)
17119a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian{
171276cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    // First add the layer to the purgatory list, which makes sure it won't
171376cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    // go away, then remove it from the main list (through a transaction).
171413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    ssize_t err = removeLayer_l(layer);
171576cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    if (err >= 0) {
171613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mLayerPurgatory.add(layer);
171776cd4ddc6ad664257739b3d3713fd9ebdc9a4ad9Mathias Agopian    }
17188c0a3d75c8823e179d19c6303f64e669975a4d85Mathias Agopian
171913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mLayersPendingRemoval.push(layer);
17200b3ad46a26dc3717260fa9347c77f673f3198606Mathias Agopian
17213d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian    // it's possible that we don't find a layer, because it might
17223d57964a81cd631d80aa9575647e1ce35b5e82d5Mathias Agopian    // have been destroyed already -- this is not technically an error
172396f0819f81293076e652792794a961543e6750d7Mathias Agopian    // from the user because there is a race between Client::destroySurface(),
17244d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    // ~Client() and ~LayerCleaner().
17259a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
17269a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian}
17279a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian
1728dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopianuint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1729dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian{
1730dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian    return android_atomic_release_load(&mTransactionFlags);
1731dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian}
1732dea20b1f343012d58ca9eb381684b26a168dc127Mathias Agopian
1733edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectuint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1734edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1735edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return android_atomic_and(~flags, &mTransactionFlags) & flags;
1736edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1737edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1738bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopianuint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
1739edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1740edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1741edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if ((old & flags)==0) { // wake the server up
174299ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian        signalTransaction();
1743edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1744edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return old;
1745edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1746edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17478b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopianvoid SurfaceFlinger::setTransactionState(
17488b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian        const Vector<ComposerState>& state,
17498b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian        const Vector<DisplayState>& displays,
17508b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian        uint32_t flags)
17518b33f032327f8de0dcc0e6d0d43ed80f834b51f6Mathias Agopian{
17527c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis    ATRACE_CALL();
1753698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian    Mutex::Autolock _l(mStateLock);
175428378392fd5aa3e0a392c9eb64634055678c3987Jamie Gennis    uint32_t transactionFlags = 0;
1755e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
17562d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    if (flags & eAnimation) {
17572d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        // For window updates that are part of an animation we must wait for
17582d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        // previous animation "frames" to be handled.
17592d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        while (mAnimTransactionPending) {
17607c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis            status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
17612d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            if (CC_UNLIKELY(err != NO_ERROR)) {
17622d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                // just in case something goes wrong in SF, return to the
17637c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis                // caller after a few seconds.
17647c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis                ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
17657c41bf7092b75dba6029cd3bbc842d3a2661b0aaJamie Gennis                        "waiting for previous animation frame");
17662d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                mAnimTransactionPending = false;
17672d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                break;
17682d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            }
17692d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        }
17702d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis    }
17712d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis
1772e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    size_t count = displays.size();
1773e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
1774e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const DisplayState& s(displays[i]);
1775e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        transactionFlags |= setDisplayStateLocked(s);
1776b8d69a55f1c187a35ac41e69de63251f5501b6f4Jamie Gennis    }
1777b8d69a55f1c187a35ac41e69de63251f5501b6f4Jamie Gennis
1778e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    count = state.size();
1779698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
1780698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian        const ComposerState& s(state[i]);
1781d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // Here we need to check that the interface we're given is indeed
1782d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // one of our own. A malicious client could give us a NULL
1783d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // IInterface, or one of its own or even one of our own but a
1784d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // different type. All these situations would cause us to crash.
1785d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        //
1786d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // NOTE: it would be better to use RTTI as we could directly check
1787d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        // that we have a Client*. however, RTTI is disabled in Android.
1788d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        if (s.client != NULL) {
1789d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian            sp<IBinder> binder = s.client->asBinder();
1790d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian            if (binder != NULL) {
1791d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                String16 desc(binder->getInterfaceDescriptor());
1792d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                if (desc == ISurfaceComposerClient::descriptor) {
1793d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                    sp<Client> client( static_cast<Client *>(s.client.get()) );
1794d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                    transactionFlags |= setClientStateLocked(client, s.state);
1795d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian                }
1796d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian            }
1797d17e3b5f6cf71eb52bc81f37719254ce08244b34Mathias Agopian        }
1798698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian    }
1799386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian
180028378392fd5aa3e0a392c9eb64634055678c3987Jamie Gennis    if (transactionFlags) {
1801386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        // this triggers the transaction
180228378392fd5aa3e0a392c9eb64634055678c3987Jamie Gennis        setTransactionFlags(transactionFlags);
1803698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian
1804386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        // if this is a synchronous transaction, wait for it to take effect
1805386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        // before returning.
1806386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        if (flags & eSynchronous) {
18072d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            mTransactionPending = true;
18082d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        }
18092d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        if (flags & eAnimation) {
18102d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis            mAnimTransactionPending = true;
1811386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian        }
18122d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis        while (mTransactionPending) {
1813386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian            status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1814386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian            if (CC_UNLIKELY(err != NO_ERROR)) {
1815386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian                // just in case something goes wrong in SF, return to the
1816386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian                // called after a few seconds.
18172d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
18182d5e230292c27d59f4c096bc742a0a19abf811c1Jamie Gennis                mTransactionPending = false;
1819386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian                break;
1820386aa98c4ffb9b805303e683f0a7b925b44d04dbMathias Agopian            }
1821cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian        }
1822edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1823edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1824edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1825e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopianuint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1826e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian{
18279a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall    ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
18289a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall    if (dpyIdx < 0)
18299a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall        return 0;
18309a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall
1831e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    uint32_t flags = 0;
18329a14392256354538f1f43a5e80fe46c2c2b965cbJesse Hall    DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
18333ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    if (disp.isValid()) {
1834e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const uint32_t what = s.what;
1835e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & DisplayState::eSurfaceChanged) {
1836e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.surface->asBinder() != s.surface->asBinder()) {
1837e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.surface = s.surface;
1838e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1839e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1840e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1841e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & DisplayState::eLayerStackChanged) {
1842e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.layerStack != s.layerStack) {
1843e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.layerStack = s.layerStack;
1844e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1845e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1846e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
184700e8c7a88a5b9c4104a71013a713acd3e4d3b77bMathias Agopian        if (what & DisplayState::eDisplayProjectionChanged) {
1848e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.orientation != s.orientation) {
1849e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.orientation = s.orientation;
1850e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1851e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1852e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.frame != s.frame) {
1853e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.frame = s.frame;
1854e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1855e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1856e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (disp.viewport != s.viewport) {
1857e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                disp.viewport = s.viewport;
1858e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eDisplayTransactionNeeded;
1859e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1860e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1861e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    }
1862e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    return flags;
1863e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
1864e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
1865e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopianuint32_t SurfaceFlinger::setClientStateLocked(
1866e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const sp<Client>& client,
1867e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const layer_state_t& s)
1868e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian{
1869e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    uint32_t flags = 0;
187013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Layer> layer(client->getLayerUser(s.surface));
1871e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    if (layer != 0) {
1872e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        const uint32_t what = s.what;
1873e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::ePositionChanged) {
1874e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setPosition(s.x, s.y))
1875e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1876e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1877e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eLayerChanged) {
1878e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            // NOTE: index needs to be calculated before we update the state
1879e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1880e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setLayer(s.z)) {
1881e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.removeAt(idx);
1882e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.add(layer);
1883e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // we need traversal (state changed)
1884e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // AND transaction (list changed)
1885e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTransactionNeeded|eTraversalNeeded;
1886e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1887e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1888e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eSizeChanged) {
1889e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setSize(s.w, s.h)) {
1890e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1891e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1892e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1893e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eAlphaChanged) {
1894e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1895e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1896e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1897e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eMatrixChanged) {
1898e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setMatrix(s.matrix))
1899e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1900e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1901e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eTransparentRegionChanged) {
1902e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setTransparentRegionHint(s.transparentRegion))
1903e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1904e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1905e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eVisibilityChanged) {
1906e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setFlags(s.flags, s.mask))
1907e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1908e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1909e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eCropChanged) {
1910e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setCrop(s.crop))
1911e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTraversalNeeded;
1912e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1913e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        if (what & layer_state_t::eLayerStackChanged) {
1914e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            // NOTE: index needs to be calculated before we update the state
1915e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1916e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            if (layer->setLayerStack(s.layerStack)) {
1917e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.removeAt(idx);
1918e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                mCurrentState.layersSortedByZ.add(layer);
1919e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // we need traversal (state changed)
1920e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                // AND transaction (list changed)
1921e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                flags |= eTransactionNeeded|eTraversalNeeded;
1922e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian            }
1923e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian        }
1924e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    }
1925e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian    return flags;
1926e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
1927e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
19284d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopianstatus_t SurfaceFlinger::createLayer(
19290ef4e15a6c12778daf464a4953d7e15e651f49acMathias Agopian        const String8& name,
19300ef4e15a6c12778daf464a4953d7e15e651f49acMathias Agopian        const sp<Client>& client,
19314d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
19324d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
1933edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
19344d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
19356e2d6483fe14362187c78c91dc03024fbb31d431Mathias Agopian    if (int32_t(w|h) < 0) {
1936921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian        ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
19376e2d6483fe14362187c78c91dc03024fbb31d431Mathias Agopian                int(w), int(h));
19384d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        return BAD_VALUE;
19396e2d6483fe14362187c78c91dc03024fbb31d431Mathias Agopian    }
19408b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber
19414d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    status_t result = NO_ERROR;
19424d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
19434d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    sp<Layer> layer;
19444d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
19453165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian    switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
19463165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian        case ISurfaceComposerClient::eFXSurfaceNormal:
19474d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            result = createNormalLayer(client,
19484d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    name, w, h, flags, format,
19494d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    handle, gbp, &layer);
1950edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            break;
19513165cc21cfea781988407b19bd83292b19f05f55Mathias Agopian        case ISurfaceComposerClient::eFXSurfaceDim:
19524d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            result = createDimLayer(client,
19534d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    name, w, h, flags,
19544d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian                    handle, gbp, &layer);
19554d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            break;
19564d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        default:
19574d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian            result = BAD_VALUE;
1958edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            break;
1959edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1960edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
19614d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    if (result == NO_ERROR) {
19624d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        addClientLayer(client, *handle, layer);
196396f0819f81293076e652792794a961543e6750d7Mathias Agopian        setTransactionFlags(eTransactionNeeded);
1964edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
19654d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return result;
1966edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1967edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
19684d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopianstatus_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
19694d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
19704d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
1971edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1972edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // initialize the surfaces
197392a979a92c34b7de609ce2b1662c73bb8a2728b9Mathias Agopian    switch (format) {
1974edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    case PIXEL_FORMAT_TRANSPARENT:
1975edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    case PIXEL_FORMAT_TRANSLUCENT:
1976edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        format = PIXEL_FORMAT_RGBA_8888;
1977edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        break;
1978edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    case PIXEL_FORMAT_OPAQUE:
1979a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#ifdef NO_RGBX_8888
1980a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian        format = PIXEL_FORMAT_RGB_565;
1981a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#else
19828f10540cd59e980dcdb5a8f4f2dbec0d94177f5fMathias Agopian        format = PIXEL_FORMAT_RGBX_8888;
1983a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#endif
1984edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        break;
1985edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1986edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1987a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#ifdef NO_RGBX_8888
1988a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian    if (format == PIXEL_FORMAT_RGBX_8888)
1989a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian        format = PIXEL_FORMAT_RGBA_8888;
1990a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian#endif
1991a8f3e4e53cad835d0d57b85a6ce1b7416e95ba73Mathias Agopian
19924d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *outLayer = new Layer(this, client, name, w, h, flags);
19934d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    status_t err = (*outLayer)->setBuffers(w, h, format, flags);
19944d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    if (err == NO_ERROR) {
19954d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        *handle = (*outLayer)->getHandle();
19964d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        *gbp = (*outLayer)->getBufferQueue();
1997edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
19984d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
19994d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
20004d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return err;
2001edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2002edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
20034d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopianstatus_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
20044d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        const String8& name, uint32_t w, uint32_t h, uint32_t flags,
20054d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian        sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
2006edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
20074d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *outLayer = new LayerDim(this, client, name, w, h, flags);
20084d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *handle = (*outLayer)->getHandle();
20094d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    *gbp = (*outLayer)->getBufferQueue();
20104d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return NO_ERROR;
2011118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian}
2012118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
2013ac9fa427d4a86745e60a5f7fd8e3ea340c4db907Mathias Agopianstatus_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
20149a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian{
20159a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    /*
20169a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian     * called by the window manager, when a surface should be marked for
20179a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian     * destruction.
20188b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber     *
20190aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian     * The surface is removed from the current and drawing lists, but placed
20200aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian     * in the purgatory queue, so it's not destroyed right-away (we need
20210aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian     * to wait for all client's references to go away first).
20229a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian     */
20239a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian
202448d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian    status_t err = NAME_NOT_FOUND;
20250aa758d64ac530833ce9a311b164c9175a7a054cMathias Agopian    Mutex::Autolock _l(mStateLock);
202613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Layer> layer = client->getLayerUser(handle);
2027b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
202848d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian    if (layer != 0) {
202948d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian        err = purgatorizeLayer_l(layer);
203048d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian        if (err == NO_ERROR) {
203113233e067b8f71adc3a0ade5f442265e1f27084bMathias Agopian            setTransactionFlags(eTransactionNeeded);
203248d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian        }
20339a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    }
20349a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian    return err;
20359a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian}
20369a11206fe793363c0e8897b478cbe6ef8c52b543Mathias Agopian
203713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianstatus_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
2038edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
20394d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    // called by ~LayerCleaner() when all references are gone
2040ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    status_t err = NO_ERROR;
204113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    sp<Layer> l(layer.promote());
2042ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    if (l != NULL) {
2043ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian        Mutex::Autolock _l(mStateLock);
2044ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian        err = removeLayer_l(l);
2045ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian        if (err == NAME_NOT_FOUND) {
2046ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            // The surface wasn't in the current list, which means it was
2047ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            // removed already, which means it is in the purgatory,
2048ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            // and need to be removed from there.
2049ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian            ssize_t idx = mLayerPurgatory.remove(l);
2050e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block            ALOGE_IF(idx < 0,
2051ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian                    "layer=%p is not in the purgatory list", l.get());
2052f1d8e87b09abf963cd5b6a026194c1940fadb7b4Mathias Agopian        }
2053e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2054ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian                "error removing layer=%p (%s)", l.get(), strerror(-err));
2055ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    }
2056ca4d3602c07837d0b2ac6878685a8e327b5f30f0Mathias Agopian    return err;
2057edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2058edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2059b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian// ---------------------------------------------------------------------------
2060b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
206113a082e160c2d1d8006b93a555a57035213d568bAndy McFaddenvoid SurfaceFlinger::onInitializeDisplays() {
206201e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    // reset screen orientation and use primary layer stack
206313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    Vector<ComposerState> state;
206413a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    Vector<DisplayState> displays;
206513a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    DisplayState d;
206601e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    d.what = DisplayState::eDisplayProjectionChanged |
206701e29054e672301e4adbbca15b3562a59a20f267Jesse Hall             DisplayState::eLayerStackChanged;
2068692c723e84e6f2747447d871d468ff50e5c73f19Jesse Hall    d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
206901e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    d.layerStack = 0;
207013a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    d.orientation = DisplayState::eOrientationDefault;
20714c05dd175ee3bd5119eecf368742b6510a8cfa6cJeff Brown    d.frame.makeInvalid();
20724c05dd175ee3bd5119eecf368742b6510a8cfa6cJeff Brown    d.viewport.makeInvalid();
207313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    displays.add(d);
207413a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    setTransactionState(state, displays, 0);
2075cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    onScreenAcquired(getDefaultDisplayDevice());
207613a082e160c2d1d8006b93a555a57035213d568bAndy McFadden}
207713a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
207813a082e160c2d1d8006b93a555a57035213d568bAndy McFaddenvoid SurfaceFlinger::initializeDisplays() {
207913a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    class MessageScreenInitialized : public MessageBase {
208013a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        SurfaceFlinger* flinger;
208113a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    public:
208213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
208313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        virtual bool handler() {
208413a082e160c2d1d8006b93a555a57035213d568bAndy McFadden            flinger->onInitializeDisplays();
208513a082e160c2d1d8006b93a555a57035213d568bAndy McFadden            return true;
208613a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        }
208713a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    };
208813a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    sp<MessageBase> msg = new MessageScreenInitialized(this);
208913a082e160c2d1d8006b93a555a57035213d568bAndy McFadden    postMessageAsync(msg);  // we may be called from main thread, use async message
209013a082e160c2d1d8006b93a555a57035213d568bAndy McFadden}
209113a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
209213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden
2093cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianvoid SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
2094c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2095c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (hw->isScreenAcquired()) {
2096c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        // this is expected, e.g. when power manager wakes up during boot
2097c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        ALOGD(" screen was previously acquired");
2098c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return;
2099c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    }
2100c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
21014297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->acquireScreen();
2102c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    int32_t type = hw->getDisplayType();
2103c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2104c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        // built-in display, tell the HWC
2105c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        getHwComposer().acquire(type);
2106c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
2107c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        if (type == DisplayDevice::DISPLAY_PRIMARY) {
2108c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden            // FIXME: eventthread only knows about the main display right now
2109c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden            mEventThread->onScreenAcquired();
2110c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        }
2111cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
211220128300e0cb7f459a60cfbcddb48190ce6545edMathias Agopian    mVisibleRegionsDirty = true;
211320128300e0cb7f459a60cfbcddb48190ce6545edMathias Agopian    repaintEverything();
2114edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2115edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2116cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianvoid SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
2117c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2118c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (!hw->isScreenAcquired()) {
2119c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        ALOGD(" screen was previously released");
2120c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return;
2121c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    }
2122c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
2123c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    hw->releaseScreen();
2124c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    int32_t type = hw->getDisplayType();
2125c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2126c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        if (type == DisplayDevice::DISPLAY_PRIMARY) {
2127cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian            // FIXME: eventthread only knows about the main display right now
2128cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian            mEventThread->onScreenReleased();
2129cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        }
2130c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden
2131c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        // built-in display, tell the HWC
2132c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        getHwComposer().release(type);
2133b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    }
2134c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    mVisibleRegionsDirty = true;
2135c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    // from this point on, SF will stop drawing on this display
2136b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian}
2137b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
2138c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFaddenvoid SurfaceFlinger::unblank(const sp<IBinder>& display) {
2139b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    class MessageScreenAcquired : public MessageBase {
2140db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        SurfaceFlinger& mFlinger;
2141db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        sp<IBinder> mDisplay;
2142b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    public:
2143db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        MessageScreenAcquired(SurfaceFlinger& flinger,
2144db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
2145b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        virtual bool handler() {
2146db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2147db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            if (hw == NULL) {
2148db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGE("Attempt to unblank null display %p", mDisplay.get());
2149db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2150db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGW("Attempt to unblank virtual display");
2151db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else {
2152db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                mFlinger.onScreenAcquired(hw);
2153db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            }
2154b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian            return true;
2155b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        }
2156b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    };
2157db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2158db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    postMessageSync(msg);
2159edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2160edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2161c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFaddenvoid SurfaceFlinger::blank(const sp<IBinder>& display) {
2162b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    class MessageScreenReleased : public MessageBase {
2163db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        SurfaceFlinger& mFlinger;
2164db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        sp<IBinder> mDisplay;
2165b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    public:
2166db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian        MessageScreenReleased(SurfaceFlinger& flinger,
2167db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
2168b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        virtual bool handler() {
2169db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2170db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            if (hw == NULL) {
2171db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGE("Attempt to blank null display %p", mDisplay.get());
2172db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2173db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                ALOGW("Attempt to blank virtual display");
2174db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            } else {
2175db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian                mFlinger.onScreenReleased(hw);
2176db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian            }
2177b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian            return true;
2178b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian        }
2179b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian    };
2180db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2181db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    postMessageSync(msg);
2182b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian}
2183b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
2184b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian// ---------------------------------------------------------------------------
2185b60314a12f3336b27d73920805ab07cbc498d857Mathias Agopian
2186edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2187edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
21881d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    const size_t SIZE = 4096;
2189edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    char buffer[SIZE];
2190edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    String8 result;
219199b49840d309727678b77403d6cc9f920111623fMathias Agopian
219299b49840d309727678b77403d6cc9f920111623fMathias Agopian    if (!PermissionCache::checkCallingPermission(sDump)) {
2193edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        snprintf(buffer, SIZE, "Permission Denial: "
2194edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2195edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                IPCThreadState::self()->getCallingPid(),
2196edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                IPCThreadState::self()->getCallingUid());
2197edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        result.append(buffer);
2198edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    } else {
21999795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        // Try to get the main lock, but don't insist if we can't
22009795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        // (this would indicate SF is stuck, but we want to be able to
22019795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        // print something in dumpsys).
22029795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        int retry = 3;
22039795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        while (mStateLock.tryLock()<0 && --retry>=0) {
22049795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian            usleep(1000000);
22059795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        }
22069795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        const bool locked(retry >= 0);
22079795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        if (!locked) {
22088b42e8a5d87dcdc8ea31368ab88de49b72ab5432Andreas Huber            snprintf(buffer, SIZE,
22099795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian                    "SurfaceFlinger appears to be unresponsive, "
22109795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian                    "dumping anyways (no locks held)\n");
22119795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian            result.append(buffer);
22129795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian        }
22139795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian
221482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        bool dumpAll = true;
221582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        size_t index = 0;
221625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        size_t numArgs = args.size();
221725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        if (numArgs) {
221825e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            if ((index < numArgs) &&
221925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                    (args[index] == String16("--list"))) {
222025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                index++;
222125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                listLayersLocked(args, index, result, buffer, SIZE);
222235aadd6be249da4bd4851692e6aff757c91b32a7Mathias Agopian                dumpAll = false;
222325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            }
222425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
222525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            if ((index < numArgs) &&
222625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                    (args[index] == String16("--latency"))) {
222782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                index++;
222882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                dumpStatsLocked(args, index, result, buffer, SIZE);
222935aadd6be249da4bd4851692e6aff757c91b32a7Mathias Agopian                dumpAll = false;
223082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            }
223125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
223225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            if ((index < numArgs) &&
223325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                    (args[index] == String16("--latency-clear"))) {
223425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                index++;
223525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian                clearStatsLocked(args, index, result, buffer, SIZE);
223635aadd6be249da4bd4851692e6aff757c91b32a7Mathias Agopian                dumpAll = false;
223725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            }
2238edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
22391b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
224082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        if (dumpAll) {
224182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            dumpAllLocked(result, buffer, SIZE);
224282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        }
224348b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian
224482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        if (locked) {
224582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            mStateLock.unlock();
224648b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian        }
224782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
224882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    write(fd, result.string(), result.size());
224982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    return NO_ERROR;
225082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian}
225148b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian
225225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopianvoid SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
225325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        String8& result, char* buffer, size_t SIZE) const
225425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian{
225525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
225625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const size_t count = currentLayers.size();
225725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
225813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
225925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        snprintf(buffer, SIZE, "%s\n", layer->getName().string());
226025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        result.append(buffer);
226125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    }
226225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian}
226325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
226482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopianvoid SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
226582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        String8& result, char* buffer, size_t SIZE) const
226682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian{
226782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    String8 name;
226882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    if (index < args.size()) {
226982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        name = String8(args[index]);
227082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        index++;
227182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
227248b888aab9fdcfba250722dffbdffe61f11c64f3Mathias Agopian
22734b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    const nsecs_t period =
22744b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
22754b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    result.appendFormat("%lld\n", period);
22764b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
22774b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    if (name.isEmpty()) {
22784b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        mAnimFrameTracker.dump(result);
22794b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    } else {
22804b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
22814b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        const size_t count = currentLayers.size();
22824b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        for (size_t i=0 ; i<count ; i++) {
228313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const sp<Layer>& layer(currentLayers[i]);
22844b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            if (name == layer->getName()) {
22854b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis                layer->dumpStats(result, buffer, SIZE);
22864b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis            }
228782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        }
228882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
228982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian}
2290ad70186f80355e55ddad2a66b5fd174a1c019c2dMathias Agopian
229125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopianvoid SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
22924b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis        String8& result, char* buffer, size_t SIZE)
229325e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian{
229425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    String8 name;
229525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    if (index < args.size()) {
229625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        name = String8(args[index]);
229725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        index++;
229825e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    }
229925e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
230025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
230125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    const size_t count = currentLayers.size();
230225e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
230313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
230425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        if (name.isEmpty() || (name == layer->getName())) {
230525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian            layer->clearStats();
230625e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian        }
230725e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian    }
23084b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis
23094b0eba949cc026ffb2c75313042d8a7bcb3fcf86Jamie Gennis    mAnimFrameTracker.clear();
231025e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian}
231125e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
23124803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
23134803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden{
23144803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    static const char* config =
23154803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " [sf"
23164803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef NO_RGBX_8888
23174803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " NO_RGBX_8888"
23184803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23194803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef HAS_CONTEXT_PRIORITY
23204803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " HAS_CONTEXT_PRIORITY"
23214803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23224803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
23234803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " NEVER_DEFAULT_TO_ASYNC_MODE"
23244803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23254803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
23264803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            " TARGET_DISABLE_TRIPLE_BUFFERING"
23274803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden#endif
23284803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden            "]";
23294803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    result.append(config);
23304803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden}
23314803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden
233282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopianvoid SurfaceFlinger::dumpAllLocked(
233382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        String8& result, char* buffer, size_t SIZE) const
233482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian{
233582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    // figure out if we're stuck somewhere
233682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const nsecs_t now = systemTime();
233782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
233882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const nsecs_t inTransaction(mDebugInTransaction);
233982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
234082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2341bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian
234282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
23434803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden     * Dump library configuration.
23444803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden     */
23454803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    result.append("Build configuration:");
23464803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    appendSfConfigString(result);
23474803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    appendUiConfigString(result);
23484803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    appendGuiConfigString(result);
23494803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    result.append("\n");
23504803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden
23514803b74e2a12a508f7bbfde6f6a962fe3299c61cAndy McFadden    /*
235282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump the visible layer list
235382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
235482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
235582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const size_t count = currentLayers.size();
235682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
235782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
235882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    for (size_t i=0 ; i<count ; i++) {
235913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(currentLayers[i]);
236082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        layer->dump(result, buffer, SIZE);
236182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
2362bc2d79ed7ada6243f3690f94ab512c0ddcdbed12Mathias Agopian
236382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
236482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump the layers in the purgatory
236582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
2366ad70186f80355e55ddad2a66b5fd174a1c019c2dMathias Agopian
236782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const size_t purgatorySize = mLayerPurgatory.size();
236882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
236982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
237082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    for (size_t i=0 ; i<purgatorySize ; i++) {
237113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(mLayerPurgatory.itemAt(i));
237282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        layer->shortDump(result, buffer, SIZE);
237382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    }
23741b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
237582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
23765f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian     * Dump Display state
23775f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian     */
23785f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
23798dfa92fef9759a881e96ee58d59875d35023aab9Andy McFadden    snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
23808dfa92fef9759a881e96ee58d59875d35023aab9Andy McFadden    result.append(buffer);
23815f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
23825f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        const sp<const DisplayDevice>& hw(mDisplays[dpy]);
23831d12d8a8e61163b35cf42c51c558a67138014e82Mathias Agopian        hw->dump(result, buffer, SIZE);
23845f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian    }
23855f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
23865f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian    /*
238782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump SurfaceFlinger global state
238882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
23891b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
239082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
239182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
23921b5e1021b8c9b87113b70d94dfb7b50f8c5b01b8Mathias Agopian
2393888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    HWComposer& hwc(getHwComposer());
23944297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    sp<const DisplayDevice> hw(getDefaultDisplayDevice());
239582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const GLExtensions& extensions(GLExtensions::getInstance());
239682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
239782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            extensions.getVendor(),
239882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            extensions.getRenderer(),
239982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            extensions.getVersion());
240082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
2401d0566bc26fcf6ca396118701fa11900b627f2c09Mathias Agopian
240282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "EGL : %s\n",
2403d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian            eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
240482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
240573d3ba9e50be1014aa21ec4bbdc874be394accb4Mathias Agopian
240682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
240782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
24089795c42e557cfa37b9fd353ef7a2a7977a77f504Mathias Agopian
24094297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    hw->undefinedRegion.dump(result, "undefinedRegion");
241082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE,
241182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  orientation=%d, canDraw=%d\n",
24124297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian            hw->getOrientation(), hw->canDraw());
241382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
241482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE,
241582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  last eglSwapBuffers() time: %f us\n"
241682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  last transaction time     : %f us\n"
2417c95dbdc236acf002b5f8aed8c8a9e43047fc75b5Mathias Agopian            "  transaction-flags         : %08x\n"
241882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  refresh-rate              : %f fps\n"
241982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            "  x-dpi                     : %f\n"
24208b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian            "  y-dpi                     : %f\n",
242182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            mLastSwapBufferTime/1000.0,
242282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            mLastTransactionTime/1000.0,
2423c95dbdc236acf002b5f8aed8c8a9e43047fc75b5Mathias Agopian            mTransactionFlags,
2424b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2425b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2426b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwc.getDpiY(HWC_DISPLAY_PRIMARY));
242782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
242882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
242982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "  eglSwapBuffers time: %f us\n",
243082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            inSwapBuffersDuration/1000.0);
243182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
243282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
243382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "  transaction time: %f us\n",
243482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            inTransactionDuration/1000.0);
243582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
243682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
243782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
243882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * VSYNC state
243982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
244082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    mEventThread->dump(result, buffer, SIZE);
244182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
244282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
244382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump HWComposer state
244482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
244582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "h/w composer state:\n");
244682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
244782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    snprintf(buffer, SIZE, "  h/w composer %s and %s\n",
244882d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian            hwc.initCheck()==NO_ERROR ? "present" : "not present",
244982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian                    (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
245082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    result.append(buffer);
2451cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian    hwc.dump(result, buffer, SIZE);
245282d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
245382d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    /*
245482d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     * Dump gralloc state
245582d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian     */
245682d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
245782d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian    alloc.dump(result);
2458edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2459edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
246013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianconst Vector< sp<Layer> >&
2461cb55857bbde34a06c19dde3db5064d1717a0173eMathias AgopianSurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
2462db9b41fd157279d1b988a854e0d7c5b43c2fac38Mathias Agopian    // Note: mStateLock is held here
2463cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian    return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2464cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian}
2465cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
246663f165fd6b86d04be94d4023e845e98560504a96Keun young Parkbool SurfaceFlinger::startDdmConnection()
246763f165fd6b86d04be94d4023e845e98560504a96Keun young Park{
246863f165fd6b86d04be94d4023e845e98560504a96Keun young Park    void* libddmconnection_dso =
246963f165fd6b86d04be94d4023e845e98560504a96Keun young Park            dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
247063f165fd6b86d04be94d4023e845e98560504a96Keun young Park    if (!libddmconnection_dso) {
247163f165fd6b86d04be94d4023e845e98560504a96Keun young Park        return false;
247263f165fd6b86d04be94d4023e845e98560504a96Keun young Park    }
247363f165fd6b86d04be94d4023e845e98560504a96Keun young Park    void (*DdmConnection_start)(const char* name);
247463f165fd6b86d04be94d4023e845e98560504a96Keun young Park    DdmConnection_start =
247563f165fd6b86d04be94d4023e845e98560504a96Keun young Park            (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
247663f165fd6b86d04be94d4023e845e98560504a96Keun young Park    if (!DdmConnection_start) {
247763f165fd6b86d04be94d4023e845e98560504a96Keun young Park        dlclose(libddmconnection_dso);
247863f165fd6b86d04be94d4023e845e98560504a96Keun young Park        return false;
247963f165fd6b86d04be94d4023e845e98560504a96Keun young Park    }
248063f165fd6b86d04be94d4023e845e98560504a96Keun young Park    (*DdmConnection_start)(getServiceName());
248163f165fd6b86d04be94d4023e845e98560504a96Keun young Park    return true;
248263f165fd6b86d04be94d4023e845e98560504a96Keun young Park}
248363f165fd6b86d04be94d4023e845e98560504a96Keun young Park
2484edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t SurfaceFlinger::onTransact(
2485edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2486edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
2487edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    switch (code) {
2488edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        case CREATE_CONNECTION:
2489698c0873cf2e07bdc7fd1e72169aee2a19fa40d7Mathias Agopian        case SET_TRANSACTION_STATE:
2490edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        case BOOT_FINISHED:
24918e533069e5721e55cb9768e140e16546c3a4a8b6Colin Cross        case BLANK:
24928e533069e5721e55cb9768e140e16546c3a4a8b6Colin Cross        case UNBLANK:
2493edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        {
2494edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // codes that require permission check
2495edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            IPCThreadState* ipc = IPCThreadState::self();
2496edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            const int pid = ipc->getCallingPid();
2497a1ecca920e15ce04fe56ebf4f1b52ba711f9eb2dMathias Agopian            const int uid = ipc->getCallingUid();
249899b49840d309727678b77403d6cc9f920111623fMathias Agopian            if ((uid != AID_GRAPHICS) &&
249999b49840d309727678b77403d6cc9f920111623fMathias Agopian                    !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
2500e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block                ALOGE("Permission Denial: "
2501375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian                        "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2502375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian                return PERMISSION_DENIED;
2503edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
25041b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            break;
25051b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
25061b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        case CAPTURE_SCREEN:
25071b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        {
25081b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            // codes that require permission check
25091b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            IPCThreadState* ipc = IPCThreadState::self();
25101b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            const int pid = ipc->getCallingPid();
25111b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            const int uid = ipc->getCallingUid();
251299b49840d309727678b77403d6cc9f920111623fMathias Agopian            if ((uid != AID_GRAPHICS) &&
251399b49840d309727678b77403d6cc9f920111623fMathias Agopian                    !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
2514e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block                ALOGE("Permission Denial: "
25151b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian                        "can't read framebuffer pid=%d, uid=%d", pid, uid);
25161b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian                return PERMISSION_DENIED;
25171b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            }
25181b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            break;
2519edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
2520edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
25211b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
2522edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2523edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
2524b8a5560e1303cb10f5cd482af466fc04d2bdfcabMathias Agopian        CHECK_INTERFACE(ISurfaceComposer, data, reply);
252599ed22412db547c59d3da08114d9d5a586442b30Glenn Kasten        if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
2526375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian            IPCThreadState* ipc = IPCThreadState::self();
2527375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian            const int pid = ipc->getCallingPid();
2528375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian            const int uid = ipc->getCallingUid();
2529e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block            ALOGE("Permission Denial: "
2530375f56363a8737119ce2222dcfaacbe1cf733fc0Mathias Agopian                    "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2531edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            return PERMISSION_DENIED;
2532edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
2533edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        int n;
2534edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        switch (code) {
253501b766839e06c32540cef100e3a7710d12cf1eefMathias Agopian            case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
253635b48d10bc9e064201d3d54d2d476314684a7a05Mathias Agopian            case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
2537edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                return NO_ERROR;
2538edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1002:  // SHOW_UPDATES
2539edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                n = data.readInt32();
2540edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
254153331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                invalidateHwcGeometry();
254253331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                repaintEverything();
2543edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                return NO_ERROR;
2544edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1004:{ // repaint everything
254553331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                repaintEverything();
2546cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian                return NO_ERROR;
2547cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian            }
2548cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian            case 1005:{ // force transaction
2549e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                setTransactionFlags(
2550e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        eTransactionNeeded|
2551e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        eDisplayTransactionNeeded|
2552e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian                        eTraversalNeeded);
2553cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian                return NO_ERROR;
2554edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
25554d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian            case 1006:{ // send empty update
25564d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian                signalRefresh();
25574d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian                return NO_ERROR;
25584d143eed994778d37eb09bb5d452c26f12bca6e1Mathias Agopian            }
255953331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian            case 1008:  // toggle use of hw composer
256053331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                n = data.readInt32();
256153331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                mDebugDisableHWC = n ? 1 : 0;
256253331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                invalidateHwcGeometry();
256353331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                repaintEverything();
256453331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian                return NO_ERROR;
2565a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian            case 1009:  // toggle use of transform hint
2566a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                n = data.readInt32();
2567a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                mDebugDisableTransformHint = n ? 1 : 0;
2568a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                invalidateHwcGeometry();
2569a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                repaintEverything();
2570a45836466c301d49d8df286b5317dfa99cb83b70Mathias Agopian                return NO_ERROR;
2571edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1010:  // interrogate.
257201b766839e06c32540cef100e3a7710d12cf1eefMathias Agopian                reply->writeInt32(0);
2573edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                reply->writeInt32(0);
2574edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                reply->writeInt32(mDebugRegion);
2575b9494d5c9d44e4a59b6d510fea1665de434f3c6bMathias Agopian                reply->writeInt32(0);
257612839bee29bdcc65731b4d42029cc59e2320c5c4Dianne Hackborn                reply->writeInt32(mDebugDisableHWC);
2577edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                return NO_ERROR;
2578edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            case 1013: {
2579edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                Mutex::Autolock _l(mStateLock);
25804297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian                sp<const DisplayDevice> hw(getDefaultDisplayDevice());
25814297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian                reply->writeInt32(hw->getPageFlipCount());
2582edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
2583edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            return NO_ERROR;
2584edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
2585edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
2586edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return err;
2587edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
2588edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
258953331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopianvoid SurfaceFlinger::repaintEverything() {
259087baae104a3e4c2059990b01c393476065c558b0Mathias Agopian    android_atomic_or(1, &mRepaintEverything);
259199ce5cdeb383216dee95af4d90e47406b0948ea1Mathias Agopian    signalTransaction();
259253331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian}
259353331da007b56e0cb4201728de99c8c29bcfaa9aMathias Agopian
259459119e658a12279e8fff508f8773843de2d90917Mathias Agopian// ---------------------------------------------------------------------------
25952a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// Capture screen into an IGraphiBufferProducer
25962a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// ---------------------------------------------------------------------------
259759119e658a12279e8fff508f8773843de2d90917Mathias Agopian
25982a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopianstatus_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
25992a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<IGraphicBufferProducer>& producer,
26002a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, uint32_t reqHeight,
26012a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ) {
26022a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26032a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (CC_UNLIKELY(display == 0))
26042a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26052a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26062a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (CC_UNLIKELY(producer == 0))
26072a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26082a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26092a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    class MessageCaptureScreen : public MessageBase {
26102a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        SurfaceFlinger* flinger;
26112a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        sp<IBinder> display;
26122a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        sp<IGraphicBufferProducer> producer;
26132a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, reqHeight;
26142a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t minLayerZ,maxLayerZ;
26152a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        status_t result;
26162a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    public:
26172a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        MessageCaptureScreen(SurfaceFlinger* flinger,
26182a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                const sp<IBinder>& display,
26192a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                const sp<IGraphicBufferProducer>& producer,
26202a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t reqWidth, uint32_t reqHeight,
26212a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t minLayerZ, uint32_t maxLayerZ)
26222a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            : flinger(flinger), display(display), producer(producer),
26232a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              reqWidth(reqWidth), reqHeight(reqHeight),
26242a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
26252a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              result(PERMISSION_DENIED)
26262a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        {
26272a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
26282a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        status_t getResult() const {
26292a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            return result;
26302a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
26312a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        virtual bool handler() {
26322a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            Mutex::Autolock _l(flinger->mStateLock);
26332a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
26342a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            result = flinger->captureScreenImplLocked(hw, producer,
26352a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    reqWidth, reqHeight, minLayerZ, maxLayerZ);
26362a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            return true;
26372a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
26382a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    };
26392a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26402a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<MessageBase> msg = new MessageCaptureScreen(this,
26412a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
26422a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    status_t res = postMessageSync(msg);
26432a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (res == NO_ERROR) {
26442a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
26452a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26462a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    return res;
2647118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian}
2648118d0245ee0a3b107055782aa8b555404b6f0280Mathias Agopian
26492a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopianstatus_t SurfaceFlinger::captureScreenImplLocked(
26502a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<const DisplayDevice>& hw,
26512a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<IGraphicBufferProducer>& producer,
26522a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, uint32_t reqHeight,
26532a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ)
265459119e658a12279e8fff508f8773843de2d90917Mathias Agopian{
265522ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian    ATRACE_CALL();
265622ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian
265759119e658a12279e8fff508f8773843de2d90917Mathias Agopian    // get screen geometry
26584297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    const uint32_t hw_w = hw->getWidth();
26594297734c1156fd8ede7e9c61b1e439f9e1c18cd9Mathias Agopian    const uint32_t hw_h = hw->getHeight();
26602a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26612a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // if we have secure windows on this display, never allow the screen capture
26622a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (hw->getSecureLayerVisible()) {
26632a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGW("FB is protected: PERMISSION_DENIED");
26642a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return PERMISSION_DENIED;
26652a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26662a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26672a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
26682a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("size mismatch (%d, %d) > (%d, %d)",
26692a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                reqWidth, reqHeight, hw_w, hw_h);
26702a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26712a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26722a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26732a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    reqWidth = (!reqWidth) ? hw_w : reqWidth;
26742a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    reqHeight = (!reqHeight) ? hw_h : reqHeight;
26752a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
26762a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26772a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // Create a surface to render into
26782a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<Surface> surface = new Surface(producer);
26792a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    ANativeWindow* const window = surface.get();
26802a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26812a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // set the buffer size to what the user requested
26822a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    native_window_set_buffers_user_dimensions(window, reqWidth, reqHeight);
26832a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26842a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // and create the corresponding EGLSurface
26852a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    EGLSurface eglSurface = eglCreateWindowSurface(
26862a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            mEGLDisplay, mEGLConfig, window, NULL);
26872a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (eglSurface == EGL_NO_SURFACE) {
26882a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("captureScreenImplLocked: eglCreateWindowSurface() failed 0x%4x",
26892a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                eglGetError());
26902a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26912a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
26922a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
26932a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (!eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) {
26942a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("captureScreenImplLocked: eglMakeCurrent() failed 0x%4x",
26952a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                eglGetError());
26962a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        eglDestroySurface(mEGLDisplay, eglSurface);
26972a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
26982a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
269959119e658a12279e8fff508f8773843de2d90917Mathias Agopian
270059119e658a12279e8fff508f8773843de2d90917Mathias Agopian    // make sure to clear all GL error flags
270159119e658a12279e8fff508f8773843de2d90917Mathias Agopian    while ( glGetError() != GL_NO_ERROR ) ;
270259119e658a12279e8fff508f8773843de2d90917Mathias Agopian
27032a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // set-up our viewport
27042a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glViewport(0, 0, reqWidth, reqHeight);
27052a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glMatrixMode(GL_PROJECTION);
27062a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glLoadIdentity();
27072a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glOrthof(0, hw_w, 0, hw_h, 0, 1);
27082a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glMatrixMode(GL_MODELVIEW);
27092a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glLoadIdentity();
2710bae92d0d605e99a14731add4f11b72413b2835e5Mathias Agopian
27119daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    // redraw the screen entirely...
2712c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian    glDisable(GL_TEXTURE_EXTERNAL_OES);
2713c492e67810814bf86301abffe1d31598b775cf45Mathias Agopian    glDisable(GL_TEXTURE_2D);
27149daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    glClearColor(0,0,0,1);
27159daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    glClear(GL_COLOR_BUFFER_BIT);
27162a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
2717b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian    const LayerVector& layers( mDrawingState.layersSortedByZ );
27189daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    const size_t count = layers.size();
27199daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    for (size_t i=0 ; i<count ; ++i) {
272013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const sp<Layer>& layer(layers[i]);
2721b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian        const Layer::State& state(layer->drawingState());
2722b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian        if (state.layerStack == hw->getLayerStack()) {
2723b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian            if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2724b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian                if (filtering) layer->setFiltering(true);
2725b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian                layer->draw(hw);
2726b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian                if (filtering) layer->setFiltering(false);
2727b7a5b05b53b81cb24849c7e9934c941f64e05a42Mathias Agopian            }
27282a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        }
27299daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    }
273059119e658a12279e8fff508f8773843de2d90917Mathias Agopian
2731d577641411f067c93aabcd2acf7ce06862fb26d3Mathias Agopian    // compositionComplete is needed for older driver
2732d577641411f067c93aabcd2acf7ce06862fb26d3Mathias Agopian    hw->compositionComplete();
2733d577641411f067c93aabcd2acf7ce06862fb26d3Mathias Agopian
27342a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // and finishing things up...
27352a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (eglSwapBuffers(mEGLDisplay, eglSurface) != EGL_TRUE) {
27362a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        ALOGE("captureScreenImplLocked: eglSwapBuffers() failed 0x%4x",
27372a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                eglGetError());
27382a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        eglDestroySurface(mEGLDisplay, eglSurface);
27392a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        return BAD_VALUE;
27402a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    }
274159119e658a12279e8fff508f8773843de2d90917Mathias Agopian
27422a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    eglDestroySurface(mEGLDisplay, eglSurface);
27439daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian    return NO_ERROR;
27449daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian}
274559119e658a12279e8fff508f8773843de2d90917Mathias Agopian
27469daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian// ---------------------------------------------------------------------------
27472a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// Capture screen into an IMemoryHeap (legacy)
27482a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian// ---------------------------------------------------------------------------
27499daa5c9b9dd286cbbf5d43f7e45a5e9e4048e855Mathias Agopian
27502a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopianstatus_t SurfaceFlinger::captureScreenImplLocked(
27512a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        const sp<const DisplayDevice>& hw,
275274c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        sp<IMemoryHeap>* heap,
275374c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        uint32_t* w, uint32_t* h, PixelFormat* f,
2754bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t sw, uint32_t sh,
2755bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ)
275674c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian{
2757fddc28d87136b55f0d9613e5f8ecd64a6aca018dMathias Agopian    ATRACE_CALL();
2758fddc28d87136b55f0d9613e5f8ecd64a6aca018dMathias Agopian
27593b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian    if (!GLExtensions::getInstance().haveFramebufferObject()) {
276074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        return INVALID_OPERATION;
27613b1d2b6b2bbfb5df46b1059ec52360974e6f1428Mathias Agopian    }
276274c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
27632a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // create the texture that will receive the screenshot, later we'll
27642a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // attach a FBO to it so we can call glReadPixels().
27652a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    GLuint tname;
27662a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glGenTextures(1, &tname);
27672a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glBindTexture(GL_TEXTURE_2D, tname);
27682a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
27692a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
277074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
27712a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // the GLConsumer will provide the BufferQueue
27722a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<GLConsumer> consumer = new GLConsumer(tname, true, GL_TEXTURE_2D);
27732a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    consumer->getBufferQueue()->setDefaultBufferFormat(HAL_PIXEL_FORMAT_RGBA_8888);
27742a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
27752a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    // call the new screenshot taking code, passing a BufferQueue to it
27762a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    status_t result = captureScreenImplLocked(hw,
27772a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            consumer->getBufferQueue(), sw, sh, minLayerZ, maxLayerZ);
27782a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
27792a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    if (result == NO_ERROR) {
27802a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        result = consumer->updateTexImage();
27812a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        if (result == NO_ERROR) {
27822a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            // create a FBO
27832a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            GLuint name;
27842a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glGenFramebuffersOES(1, &name);
27852a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
27862a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
27872a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
27882a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
27892a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sp<GraphicBuffer> buf(consumer->getCurrentBuffer());
27902a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sw = buf->getWidth();
27912a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sh = buf->getHeight();
27926a531717cddf1d30be0946884d21c27e5b6b390cMathias Agopian            size_t size = sw * sh * 4;
2793f653b897a449e10d6cbfb6e0812f7b0bb02d6482Mathias Agopian
279474c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            // allocate shared memory large enough to hold the
279574c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            // screen capture
279674c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            sp<MemoryHeapBase> base(
279774c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    new MemoryHeapBase(size, 0, "screen-capture") );
279874c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            void* const ptr = base->getBase();
27997b1905113712281c777b230648d3fbb69ae4f42cMathias Agopian            if (ptr != MAP_FAILED) {
280074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                // capture the screen with glReadPixels()
2801fddc28d87136b55f0d9613e5f8ecd64a6aca018dMathias Agopian                ScopedTrace _t(ATRACE_TAG, "glReadPixels");
280274c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
280374c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                if (glGetError() == GL_NO_ERROR) {
280474c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *heap = base;
280574c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *w = sw;
280674c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *h = sh;
280774c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    *f = PIXEL_FORMAT_RGBA_8888;
280874c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                    result = NO_ERROR;
2809175264b09c6080b29a23fc9f545d4b99445714bdJeff Sharkey                } else {
2810175264b09c6080b29a23fc9f545d4b99445714bdJeff Sharkey                    result = NO_MEMORY;
281174c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                }
281274c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            } else {
281374c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian                result = NO_MEMORY;
281474c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian            }
28152a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian
28162a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            // back to main framebuffer
28172a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
28182a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            glDeleteFramebuffersOES(1, &name);
281974c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian        }
282074c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian    }
282174c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
28222a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    glDeleteTextures(1, &tname);
2823c1d1b0d9b74d8f55346d0b84f369e48ecf2e0d33Mathias Agopian
28242be4e8ff23f24285f4c195ba5537c7a51c13be4dMathias Agopian    DisplayDevice::makeCurrent(mEGLDisplay,
28252be4e8ff23f24285f4c195ba5537c7a51c13be4dMathias Agopian            getDefaultDisplayDevice(), mEGLContext);
28262be4e8ff23f24285f4c195ba5537c7a51c13be4dMathias Agopian
282774c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian    return result;
282874c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian}
282974c40c0a273dbfd7d10617c4cc1b0c066bfc812eMathias Agopian
28309d4e3d2f42e93e2d12bacabe97d307d30c3c20ddJeff Brownstatus_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
28311b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        sp<IMemoryHeap>* heap,
28322a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
28332a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth, uint32_t reqHeight,
2834bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t minLayerZ, uint32_t maxLayerZ)
28351b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian{
28369d4e3d2f42e93e2d12bacabe97d307d30c3c20ddJeff Brown    if (CC_UNLIKELY(display == 0))
28371b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        return BAD_VALUE;
28381b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
28391b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    class MessageCaptureScreen : public MessageBase {
28401b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        SurfaceFlinger* flinger;
28419d4e3d2f42e93e2d12bacabe97d307d30c3c20ddJeff Brown        sp<IBinder> display;
28421b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        sp<IMemoryHeap>* heap;
28432a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t* outWidth;
28442a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t* outHeight;
28452a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        PixelFormat* outFormat;
28462a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqWidth;
28472a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        uint32_t reqHeight;
2848bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t minLayerZ;
2849bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian        uint32_t maxLayerZ;
28501b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        status_t result;
28511b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    public:
28522a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian        MessageCaptureScreen(SurfaceFlinger* flinger,
28532a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                const sp<IBinder>& display, sp<IMemoryHeap>* heap,
28542a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
28552a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                uint32_t reqWidth, uint32_t reqHeight,
2856bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian                uint32_t minLayerZ, uint32_t maxLayerZ)
28572a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            : flinger(flinger), display(display), heap(heap),
28582a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              outWidth(outWidth), outHeight(outHeight), outFormat(outFormat),
28592a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian              reqWidth(reqWidth), reqHeight(reqHeight),
2860bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian              minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2861bf2c6a6c8f1df40ac94e28b948754bb9739daacaMathias Agopian              result(PERMISSION_DENIED)
28621b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        {
28631b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
28641b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        status_t getResult() const {
28651b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            return result;
28661b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
28671b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        virtual bool handler() {
28681b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            Mutex::Autolock _l(flinger->mStateLock);
28692a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
28702a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            result = flinger->captureScreenImplLocked(hw, heap,
28712a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    outWidth, outHeight, outFormat,
28722a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian                    reqWidth, reqHeight, minLayerZ, maxLayerZ);
28731b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian            return true;
28741b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        }
28751b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    };
28761b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
28772a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian    sp<MessageBase> msg = new MessageCaptureScreen(this, display, heap,
28782a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            outWidth, outHeight, outFormat,
28792a9fc493dfdba67108e4335bb1fe931bc1e2a025Mathias Agopian            reqWidth, reqHeight, minLayerZ, maxLayerZ);
28801b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    status_t res = postMessageSync(msg);
28811b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    if (res == NO_ERROR) {
28821b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian        res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
28831b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    }
28841b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian    return res;
28851b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian}
28861b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
28871b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian// ---------------------------------------------------------------------------
28881b0b30d04304392748a8a4ab5a69e52a19f51b3aMathias Agopian
2889921e6ac4b7610a178285898d191eb0e3afe906c0Mathias AgopianSurfaceFlinger::LayerVector::LayerVector() {
2890921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian}
2891921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
2892921e6ac4b7610a178285898d191eb0e3afe906c0Mathias AgopianSurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
289313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    : SortedVector<sp<Layer> >(rhs) {
2894921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian}
2895921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
2896921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopianint SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2897921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    const void* rhs) const
2898edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
2899be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    // sort layers per layer-stack, then by z-order and finally by sequence
290013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
290113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
2902be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian
2903be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    uint32_t ls = l->currentState().layerStack;
2904be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    uint32_t rs = r->currentState().layerStack;
2905be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    if (ls != rs)
2906be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian        return ls - rs;
2907be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian
2908921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    uint32_t lz = l->currentState().z;
2909921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian    uint32_t rz = r->currentState().z;
2910be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    if (lz != rz)
2911be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian        return lz - rz;
2912be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian
2913be246f86bd6378a5110e81e9d9068ab03c3b077eMathias Agopian    return l->sequence - r->sequence;
2914921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian}
2915921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
2916921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian// ---------------------------------------------------------------------------
2917921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
29183ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias AgopianSurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
29193ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias Agopian    : type(DisplayDevice::DISPLAY_ID_INVALID) {
2920e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian}
2921e57f292595bec48f65c8088b00ff6beea01217e9Mathias Agopian
29223ee454a7bef8bd3d1c9cdd9d17108eb80ebadf2aMathias AgopianSurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
292301e29054e672301e4adbbca15b3562a59a20f267Jesse Hall    : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
2924da8d0a5c0cf9d41915d3b106cad4aaec3e767c11Mathias Agopian    viewport.makeInvalid();
2925da8d0a5c0cf9d41915d3b106cad4aaec3e767c11Mathias Agopian    frame.makeInvalid();
2926b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian}
29277303c6bf1a8b00a0e7d8165d774a1f259b4ccda9Mathias Agopian
2928b7e930db175c192464cebdeb49eb56cf6dd60114Mathias Agopian// ---------------------------------------------------------------------------
292996f0819f81293076e652792794a961543e6750d7Mathias Agopian
2930edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}; // namespace android
2931