| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> | 
|  | 18 | #include <stdio.h> | 
|  | 19 | #include <stdint.h> | 
|  | 20 | #include <unistd.h> | 
|  | 21 | #include <fcntl.h> | 
|  | 22 | #include <errno.h> | 
|  | 23 | #include <math.h> | 
| Jean-Baptiste Queru | a837ba7 | 2009-01-26 11:51:12 -0800 | [diff] [blame] | 24 | #include <limits.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <sys/types.h> | 
|  | 26 | #include <sys/stat.h> | 
|  | 27 | #include <sys/ioctl.h> | 
|  | 28 |  | 
|  | 29 | #include <cutils/log.h> | 
|  | 30 | #include <cutils/properties.h> | 
|  | 31 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 32 | #include <binder/IPCThreadState.h> | 
|  | 33 | #include <binder/IServiceManager.h> | 
| Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 34 | #include <binder/MemoryHeapBase.h> | 
| Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 35 | #include <binder/PermissionCache.h> | 
| Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 36 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <utils/String8.h> | 
|  | 38 | #include <utils/String16.h> | 
|  | 39 | #include <utils/StopWatch.h> | 
|  | 40 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 41 | #include <ui/GraphicBufferAllocator.h> | 
| Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 42 | #include <ui/GraphicLog.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | #include <ui/PixelFormat.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | #include <pixelflinger/pixelflinger.h> | 
|  | 46 | #include <GLES/gl.h> | 
|  | 47 |  | 
|  | 48 | #include "clz.h" | 
| Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 49 | #include "GLExtensions.h" | 
| Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 50 | #include "DdmConnection.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | #include "Layer.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | #include "LayerDim.h" | 
| Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 53 | #include "LayerScreenshot.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | #include "SurfaceFlinger.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 |  | 
|  | 56 | #include "DisplayHardware/DisplayHardware.h" | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 57 | #include "DisplayHardware/HWComposer.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 |  | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 59 | #include <private/surfaceflinger/SharedBufferStack.h> | 
|  | 60 |  | 
| Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 61 | /* ideally AID_GRAPHICS would be in a semi-public header | 
|  | 62 | * or there would be a way to map a user/group name to its id | 
|  | 63 | */ | 
|  | 64 | #ifndef AID_GRAPHICS | 
|  | 65 | #define AID_GRAPHICS 1003 | 
|  | 66 | #endif | 
|  | 67 |  | 
| Mathias Agopian | bc2d79e | 2011-11-29 17:55:46 -0800 | [diff] [blame] | 68 | #define EGL_VERSION_HW_ANDROID  0x3143 | 
|  | 69 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | #define DISPLAY_COUNT       1 | 
|  | 71 |  | 
|  | 72 | namespace android { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | // --------------------------------------------------------------------------- | 
|  | 74 |  | 
| Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 75 | const String16 sHardwareTest("android.permission.HARDWARE_TEST"); | 
|  | 76 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); | 
|  | 77 | const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER"); | 
|  | 78 | const String16 sDump("android.permission.DUMP"); | 
|  | 79 |  | 
|  | 80 | // --------------------------------------------------------------------------- | 
|  | 81 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | SurfaceFlinger::SurfaceFlinger() | 
|  | 83 | :   BnSurfaceComposer(), Thread(false), | 
|  | 84 | mTransactionFlags(0), | 
| Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 85 | mTransationPending(false), | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 86 | mLayersRemoved(false), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | mBootTime(systemTime()), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | mVisibleRegionsDirty(false), | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 89 | mHwWorkListDirty(false), | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 90 | mElectronBeamAnimationMode(0), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | mDebugRegion(0), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | mDebugBackground(0), | 
| Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 93 | mDebugDDMS(0), | 
| Mathias Agopian | 73d3ba9 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 94 | mDebugDisableHWC(0), | 
| Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 95 | mDebugDisableTransformHint(0), | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 96 | mDebugInSwapBuffers(0), | 
|  | 97 | mLastSwapBufferTime(0), | 
|  | 98 | mDebugInTransaction(0), | 
|  | 99 | mLastTransactionTime(0), | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 100 | mBootFinished(false), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | mConsoleSignals(0), | 
|  | 102 | mSecureFrameBuffer(0) | 
|  | 103 | { | 
|  | 104 | init(); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | void SurfaceFlinger::init() | 
|  | 108 | { | 
|  | 109 | LOGI("SurfaceFlinger is starting"); | 
|  | 110 |  | 
|  | 111 | // debugging stuff... | 
|  | 112 | char value[PROPERTY_VALUE_MAX]; | 
| Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 113 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | property_get("debug.sf.showupdates", value, "0"); | 
|  | 115 | mDebugRegion = atoi(value); | 
| Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 116 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | property_get("debug.sf.showbackground", value, "0"); | 
|  | 118 | mDebugBackground = atoi(value); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 |  | 
| Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 120 | property_get("debug.sf.ddms", value, "0"); | 
|  | 121 | mDebugDDMS = atoi(value); | 
|  | 122 | if (mDebugDDMS) { | 
|  | 123 | DdmConnection::start(getServiceName()); | 
|  | 124 | } | 
|  | 125 |  | 
| Mathias Agopian | 78fd501 | 2010-04-20 14:51:04 -0700 | [diff] [blame] | 126 | LOGI_IF(mDebugRegion,       "showupdates enabled"); | 
|  | 127 | LOGI_IF(mDebugBackground,   "showbackground enabled"); | 
| Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 128 | LOGI_IF(mDebugDDMS,         "DDMS debugging enabled"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
|  | 131 | SurfaceFlinger::~SurfaceFlinger() | 
|  | 132 | { | 
|  | 133 | glDeleteTextures(1, &mWormholeTexName); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 136 | sp<IMemoryHeap> SurfaceFlinger::getCblk() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | { | 
| Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 138 | return mServerHeap; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
| Mathias Agopian | 7e27f05 | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 141 | sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | { | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 143 | sp<ISurfaceComposerClient> bclient; | 
|  | 144 | sp<Client> client(new Client(this)); | 
|  | 145 | status_t err = client->initCheck(); | 
|  | 146 | if (err == NO_ERROR) { | 
|  | 147 | bclient = client; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | return bclient; | 
|  | 150 | } | 
|  | 151 |  | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 152 | sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc() | 
|  | 153 | { | 
|  | 154 | sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc()); | 
|  | 155 | return gba; | 
|  | 156 | } | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 157 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const | 
|  | 159 | { | 
|  | 160 | LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy); | 
|  | 161 | const GraphicPlane& plane(mGraphicPlanes[dpy]); | 
|  | 162 | return plane; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) | 
|  | 166 | { | 
|  | 167 | return const_cast<GraphicPlane&>( | 
|  | 168 | const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy)); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | void SurfaceFlinger::bootFinished() | 
|  | 172 | { | 
|  | 173 | const nsecs_t now = systemTime(); | 
|  | 174 | const nsecs_t duration = now - mBootTime; | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 175 | LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 176 | mBootFinished = true; | 
| Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | // wait patiently for the window manager death | 
|  | 179 | const String16 name("window"); | 
|  | 180 | sp<IBinder> window(defaultServiceManager()->getService(name)); | 
|  | 181 | if (window != 0) { | 
|  | 182 | window->linkToDeath(this); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | // stop boot animation | 
| Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 186 | property_set("ctl.stop", "bootanim"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
| Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 189 | void SurfaceFlinger::binderDied(const wp<IBinder>& who) | 
|  | 190 | { | 
|  | 191 | // the window manager died on us. prepare its eulogy. | 
|  | 192 |  | 
| Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 193 | // reset screen orientation | 
|  | 194 | setOrientation(0, eOrientationDefault, 0); | 
|  | 195 |  | 
|  | 196 | // restart the boot-animation | 
|  | 197 | property_set("ctl.start", "bootanim"); | 
|  | 198 | } | 
|  | 199 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | void SurfaceFlinger::onFirstRef() | 
|  | 201 | { | 
|  | 202 | run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY); | 
|  | 203 |  | 
|  | 204 | // Wait for the main thread to be done with its initialization | 
|  | 205 | mReadyToRunBarrier.wait(); | 
|  | 206 | } | 
|  | 207 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | static inline uint16_t pack565(int r, int g, int b) { | 
|  | 209 | return (r<<11)|(g<<5)|b; | 
|  | 210 | } | 
|  | 211 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | status_t SurfaceFlinger::readyToRun() | 
|  | 213 | { | 
|  | 214 | LOGI(   "SurfaceFlinger's main thread ready to run. " | 
|  | 215 | "Initializing graphics H/W..."); | 
|  | 216 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | // we only support one display currently | 
|  | 218 | int dpy = 0; | 
|  | 219 |  | 
|  | 220 | { | 
|  | 221 | // initialize the main display | 
|  | 222 | GraphicPlane& plane(graphicPlane(dpy)); | 
|  | 223 | DisplayHardware* const hw = new DisplayHardware(this, dpy); | 
|  | 224 | plane.setDisplayHardware(hw); | 
|  | 225 | } | 
|  | 226 |  | 
| Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 227 | // create the shared control-block | 
|  | 228 | mServerHeap = new MemoryHeapBase(4096, | 
|  | 229 | MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap"); | 
|  | 230 | LOGE_IF(mServerHeap==0, "can't create shared memory dealer"); | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 231 |  | 
| Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 232 | mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase()); | 
|  | 233 | LOGE_IF(mServerCblk==0, "can't get to shared control block's address"); | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 234 |  | 
| Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 235 | new(mServerCblk) surface_flinger_cblk_t; | 
|  | 236 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | // initialize primary screen | 
|  | 238 | // (other display should be initialized in the same manner, but | 
|  | 239 | // asynchronously, as they could come and go. None of this is supported | 
|  | 240 | // yet). | 
|  | 241 | const GraphicPlane& plane(graphicPlane(dpy)); | 
|  | 242 | const DisplayHardware& hw = plane.displayHardware(); | 
|  | 243 | const uint32_t w = hw.getWidth(); | 
|  | 244 | const uint32_t h = hw.getHeight(); | 
|  | 245 | const uint32_t f = hw.getFormat(); | 
|  | 246 | hw.makeCurrent(); | 
|  | 247 |  | 
|  | 248 | // initialize the shared control block | 
|  | 249 | mServerCblk->connected |= 1<<dpy; | 
|  | 250 | display_cblk_t* dcblk = mServerCblk->displays + dpy; | 
|  | 251 | memset(dcblk, 0, sizeof(display_cblk_t)); | 
| Mathias Agopian | 2b92d89 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 252 | dcblk->w            = plane.getWidth(); | 
|  | 253 | dcblk->h            = plane.getHeight(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | dcblk->format       = f; | 
|  | 255 | dcblk->orientation  = ISurfaceComposer::eOrientationDefault; | 
|  | 256 | dcblk->xdpi         = hw.getDpiX(); | 
|  | 257 | dcblk->ydpi         = hw.getDpiY(); | 
|  | 258 | dcblk->fps          = hw.getRefreshRate(); | 
|  | 259 | dcblk->density      = hw.getDensity(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 |  | 
|  | 261 | // Initialize OpenGL|ES | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 263 | glPixelStorei(GL_PACK_ALIGNMENT, 4); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | glEnableClientState(GL_VERTEX_ARRAY); | 
|  | 265 | glEnable(GL_SCISSOR_TEST); | 
|  | 266 | glShadeModel(GL_FLAT); | 
|  | 267 | glDisable(GL_DITHER); | 
|  | 268 | glDisable(GL_CULL_FACE); | 
|  | 269 |  | 
|  | 270 | const uint16_t g0 = pack565(0x0F,0x1F,0x0F); | 
|  | 271 | const uint16_t g1 = pack565(0x17,0x2f,0x17); | 
| Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 272 | const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 }; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | glGenTextures(1, &mWormholeTexName); | 
|  | 274 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); | 
|  | 275 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 276 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 277 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | 
|  | 278 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | 
|  | 279 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, | 
| Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 280 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData); | 
|  | 281 |  | 
|  | 282 | const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) }; | 
|  | 283 | glGenTextures(1, &mProtectedTexName); | 
|  | 284 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); | 
|  | 285 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 286 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 287 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | 
|  | 288 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | 
|  | 289 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, | 
|  | 290 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 |  | 
|  | 292 | glViewport(0, 0, w, h); | 
|  | 293 | glMatrixMode(GL_PROJECTION); | 
|  | 294 | glLoadIdentity(); | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 295 | // put the origin in the left-bottom corner | 
|  | 296 | glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | mReadyToRunBarrier.open(); | 
|  | 299 |  | 
|  | 300 | /* | 
|  | 301 | *  We're now ready to accept clients... | 
|  | 302 | */ | 
|  | 303 |  | 
| Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 304 | // start boot animation | 
|  | 305 | property_set("ctl.start", "bootanim"); | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 306 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | return NO_ERROR; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | // ---------------------------------------------------------------------------- | 
|  | 311 | #if 0 | 
|  | 312 | #pragma mark - | 
|  | 313 | #pragma mark Events Handler | 
|  | 314 | #endif | 
|  | 315 |  | 
|  | 316 | void SurfaceFlinger::waitForEvent() | 
|  | 317 | { | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 318 | while (true) { | 
|  | 319 | nsecs_t timeout = -1; | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 320 | sp<MessageBase> msg = mEventQueue.waitMessage(timeout); | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 321 | if (msg != 0) { | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 322 | switch (msg->what) { | 
|  | 323 | case MessageQueue::INVALIDATE: | 
|  | 324 | // invalidate message, just return to the main loop | 
|  | 325 | return; | 
|  | 326 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | } | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | void SurfaceFlinger::signalEvent() { | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 332 | mEventQueue.invalidate(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } | 
|  | 334 |  | 
| Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 335 | bool SurfaceFlinger::authenticateSurfaceTexture( | 
|  | 336 | const sp<ISurfaceTexture>& surfaceTexture) const { | 
| Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 337 | Mutex::Autolock _l(mStateLock); | 
| Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 338 | sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder()); | 
| Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 339 |  | 
|  | 340 | // Check the visible layer list for the ISurface | 
|  | 341 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; | 
|  | 342 | size_t count = currentLayers.size(); | 
|  | 343 | for (size_t i=0 ; i<count ; i++) { | 
|  | 344 | const sp<LayerBase>& layer(currentLayers[i]); | 
|  | 345 | sp<LayerBaseClient> lbc(layer->getLayerBaseClient()); | 
| Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 346 | if (lbc != NULL) { | 
|  | 347 | wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder(); | 
|  | 348 | if (lbcBinder == surfaceTextureBinder) { | 
|  | 349 | return true; | 
|  | 350 | } | 
| Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 351 | } | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | // Check the layers in the purgatory.  This check is here so that if a | 
| Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 355 | // SurfaceTexture gets destroyed before all the clients are done using it, | 
|  | 356 | // the error will not be reported as "surface XYZ is not authenticated", but | 
| Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 357 | // will instead fail later on when the client tries to use the surface, | 
|  | 358 | // which should be reported as "surface XYZ returned an -ENODEV".  The | 
|  | 359 | // purgatorized layers are no less authentic than the visible ones, so this | 
|  | 360 | // should not cause any harm. | 
|  | 361 | size_t purgatorySize =  mLayerPurgatory.size(); | 
|  | 362 | for (size_t i=0 ; i<purgatorySize ; i++) { | 
|  | 363 | const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i)); | 
|  | 364 | sp<LayerBaseClient> lbc(layer->getLayerBaseClient()); | 
| Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 365 | if (lbc != NULL) { | 
|  | 366 | wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder(); | 
|  | 367 | if (lbcBinder == surfaceTextureBinder) { | 
|  | 368 | return true; | 
|  | 369 | } | 
| Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 370 | } | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | return false; | 
|  | 374 | } | 
|  | 375 |  | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 376 | status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg, | 
|  | 377 | nsecs_t reltime, uint32_t flags) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | { | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 379 | return mEventQueue.postMessage(msg, reltime, flags); | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg, | 
|  | 383 | nsecs_t reltime, uint32_t flags) | 
|  | 384 | { | 
|  | 385 | status_t res = mEventQueue.postMessage(msg, reltime, flags); | 
|  | 386 | if (res == NO_ERROR) { | 
|  | 387 | msg->wait(); | 
|  | 388 | } | 
|  | 389 | return res; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } | 
|  | 391 |  | 
|  | 392 | // ---------------------------------------------------------------------------- | 
|  | 393 | #if 0 | 
|  | 394 | #pragma mark - | 
|  | 395 | #pragma mark Main loop | 
|  | 396 | #endif | 
|  | 397 |  | 
|  | 398 | bool SurfaceFlinger::threadLoop() | 
|  | 399 | { | 
|  | 400 | waitForEvent(); | 
|  | 401 |  | 
|  | 402 | // check for transactions | 
|  | 403 | if (UNLIKELY(mConsoleSignals)) { | 
|  | 404 | handleConsoleEvents(); | 
|  | 405 | } | 
|  | 406 |  | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 407 | // if we're in a global transaction, don't do anything. | 
|  | 408 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; | 
|  | 409 | uint32_t transactionFlags = peekTransactionFlags(mask); | 
|  | 410 | if (UNLIKELY(transactionFlags)) { | 
|  | 411 | handleTransaction(transactionFlags); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
|  | 414 | // post surfaces (if needed) | 
|  | 415 | handlePageFlip(); | 
|  | 416 |  | 
| Mathias Agopian | 3a3cad3 | 2011-10-18 17:36:28 -0700 | [diff] [blame] | 417 | if (mDirtyRegion.isEmpty()) { | 
|  | 418 | // nothing new to do. | 
|  | 419 | return true; | 
|  | 420 | } | 
|  | 421 |  | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 422 | if (UNLIKELY(mHwWorkListDirty)) { | 
|  | 423 | // build the h/w work list | 
|  | 424 | handleWorkList(); | 
|  | 425 | } | 
|  | 426 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
| Jamie Gennis | a402c4c | 2011-10-14 16:44:08 -0700 | [diff] [blame] | 428 | if (LIKELY(hw.canDraw())) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | // repaint the framebuffer (if needed) | 
| Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 430 |  | 
|  | 431 | const int index = hw.getCurrentBufferIndex(); | 
|  | 432 | GraphicLog& logger(GraphicLog::getInstance()); | 
|  | 433 |  | 
|  | 434 | logger.log(GraphicLog::SF_REPAINT, index); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | handleRepaint(); | 
|  | 436 |  | 
| Mathias Agopian | 74faca2 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 437 | // inform the h/w that we're done compositing | 
| Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 438 | logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index); | 
| Mathias Agopian | 74faca2 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 439 | hw.compositionComplete(); | 
|  | 440 |  | 
| Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 441 | logger.log(GraphicLog::SF_SWAP_BUFFERS, index); | 
| Antti Hatala | 8392b50 | 2010-09-08 06:37:14 -0700 | [diff] [blame] | 442 | postFramebuffer(); | 
|  | 443 |  | 
| Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 444 | logger.log(GraphicLog::SF_REPAINT_DONE, index); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } else { | 
|  | 446 | // pretend we did the post | 
| Mathias Agopian | e6f0984 | 2010-12-15 14:41:59 -0800 | [diff] [blame] | 447 | hw.compositionComplete(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | usleep(16667); // 60 fps period | 
|  | 449 | } | 
|  | 450 | return true; | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | void SurfaceFlinger::postFramebuffer() | 
|  | 454 | { | 
| Mathias Agopian | 3a3cad3 | 2011-10-18 17:36:28 -0700 | [diff] [blame] | 455 | // this should never happen. we do the flip anyways so we don't | 
|  | 456 | // risk to cause a deadlock with hwc | 
|  | 457 | LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty"); | 
| Mathias Agopian | a44b041 | 2011-10-16 18:46:35 -0700 | [diff] [blame] | 458 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 459 | const nsecs_t now = systemTime(); | 
|  | 460 | mDebugInSwapBuffers = now; | 
|  | 461 | hw.flip(mSwapRegion); | 
|  | 462 | mLastSwapBufferTime = systemTime() - now; | 
|  | 463 | mDebugInSwapBuffers = 0; | 
|  | 464 | mSwapRegion.clear(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
|  | 467 | void SurfaceFlinger::handleConsoleEvents() | 
|  | 468 | { | 
|  | 469 | // something to do with the console | 
|  | 470 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); | 
|  | 471 |  | 
|  | 472 | int what = android_atomic_and(0, &mConsoleSignals); | 
|  | 473 | if (what & eConsoleAcquired) { | 
|  | 474 | hw.acquireScreen(); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 475 | // this is a temporary work-around, eventually this should be called | 
|  | 476 | // by the power-manager | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 477 | SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } | 
|  | 479 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | if (what & eConsoleReleased) { | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 481 | if (hw.isScreenAcquired()) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | hw.releaseScreen(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | } | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | mDirtyRegion.set(hw.bounds()); | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) | 
|  | 490 | { | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 491 | Mutex::Autolock _l(mStateLock); | 
|  | 492 | const nsecs_t now = systemTime(); | 
|  | 493 | mDebugInTransaction = now; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 |  | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 495 | // Here we're guaranteed that some transaction flags are set | 
|  | 496 | // so we can call handleTransactionLocked() unconditionally. | 
|  | 497 | // We call getTransactionFlags(), which will also clear the flags, | 
|  | 498 | // with mStateLock held to guarantee that mCurrentState won't change | 
|  | 499 | // until the transaction is committed. | 
| Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 500 |  | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 501 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; | 
|  | 502 | transactionFlags = getTransactionFlags(mask); | 
|  | 503 | handleTransactionLocked(transactionFlags); | 
| Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 504 |  | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 505 | mLastTransactionTime = systemTime() - now; | 
|  | 506 | mDebugInTransaction = 0; | 
|  | 507 | invalidateHwcGeometry(); | 
|  | 508 | // here the transaction has been committed | 
| Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 509 | } | 
|  | 510 |  | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 511 | void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags) | 
| Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 512 | { | 
|  | 513 | const LayerVector& currentLayers(mCurrentState.layersSortedByZ); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | const size_t count = currentLayers.size(); | 
|  | 515 |  | 
|  | 516 | /* | 
|  | 517 | * Traversal of the children | 
|  | 518 | * (perform the transaction for each of them if needed) | 
|  | 519 | */ | 
|  | 520 |  | 
|  | 521 | const bool layersNeedTransaction = transactionFlags & eTraversalNeeded; | 
|  | 522 | if (layersNeedTransaction) { | 
|  | 523 | for (size_t i=0 ; i<count ; i++) { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 524 | const sp<LayerBase>& layer = currentLayers[i]; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); | 
|  | 526 | if (!trFlags) continue; | 
|  | 527 |  | 
|  | 528 | const uint32_t flags = layer->doTransaction(0); | 
|  | 529 | if (flags & Layer::eVisibleRegion) | 
|  | 530 | mVisibleRegionsDirty = true; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | /* | 
|  | 535 | * Perform our own transaction if needed | 
|  | 536 | */ | 
|  | 537 |  | 
|  | 538 | if (transactionFlags & eTransactionNeeded) { | 
|  | 539 | if (mCurrentState.orientation != mDrawingState.orientation) { | 
|  | 540 | // the orientation has changed, recompute all visible regions | 
|  | 541 | // and invalidate everything. | 
|  | 542 |  | 
|  | 543 | const int dpy = 0; | 
|  | 544 | const int orientation = mCurrentState.orientation; | 
| Jeff Brown | 21230c6 | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 545 | // Currently unused: const uint32_t flags = mCurrentState.orientationFlags; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | GraphicPlane& plane(graphicPlane(dpy)); | 
|  | 547 | plane.setOrientation(orientation); | 
|  | 548 |  | 
|  | 549 | // update the shared control block | 
|  | 550 | const DisplayHardware& hw(plane.displayHardware()); | 
|  | 551 | volatile display_cblk_t* dcblk = mServerCblk->displays + dpy; | 
|  | 552 | dcblk->orientation = orientation; | 
| Mathias Agopian | 2b92d89 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 553 | dcblk->w = plane.getWidth(); | 
|  | 554 | dcblk->h = plane.getHeight(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 |  | 
|  | 556 | mVisibleRegionsDirty = true; | 
|  | 557 | mDirtyRegion.set(hw.bounds()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
| Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 560 | if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) { | 
|  | 561 | // layers have been added | 
|  | 562 | mVisibleRegionsDirty = true; | 
|  | 563 | } | 
|  | 564 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | // some layers might have been removed, so | 
|  | 566 | // we need to update the regions they're exposing. | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 567 | if (mLayersRemoved) { | 
| Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 568 | mLayersRemoved = false; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | mVisibleRegionsDirty = true; | 
| Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 570 | const LayerVector& previousLayers(mDrawingState.layersSortedByZ); | 
| Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 571 | const size_t count = previousLayers.size(); | 
|  | 572 | for (size_t i=0 ; i<count ; i++) { | 
| Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 573 | const sp<LayerBase>& layer(previousLayers[i]); | 
|  | 574 | if (currentLayers.indexOf( layer ) < 0) { | 
|  | 575 | // this layer is not visible anymore | 
| Mathias Agopian | 5d7126b | 2009-07-28 14:20:21 -0700 | [diff] [blame] | 576 | mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen); | 
| Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 577 | } | 
|  | 578 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | } | 
|  | 581 |  | 
|  | 582 | commitTransaction(); | 
|  | 583 | } | 
|  | 584 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | void SurfaceFlinger::computeVisibleRegions( | 
| Mathias Agopian | 1bbafb9 | 2011-03-11 16:54:47 -0800 | [diff] [blame] | 586 | const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | { | 
|  | 588 | const GraphicPlane& plane(graphicPlane(0)); | 
|  | 589 | const Transform& planeTransform(plane.transform()); | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 590 | const DisplayHardware& hw(plane.displayHardware()); | 
|  | 591 | const Region screenRegion(hw.bounds()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 |  | 
|  | 593 | Region aboveOpaqueLayers; | 
|  | 594 | Region aboveCoveredLayers; | 
|  | 595 | Region dirty; | 
|  | 596 |  | 
|  | 597 | bool secureFrameBuffer = false; | 
|  | 598 |  | 
|  | 599 | size_t i = currentLayers.size(); | 
|  | 600 | while (i--) { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 601 | const sp<LayerBase>& layer = currentLayers[i]; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | layer->validateVisibility(planeTransform); | 
|  | 603 |  | 
|  | 604 | // start with the whole surface at its current location | 
| Mathias Agopian | 9701122 | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 605 | const Layer::State& s(layer->drawingState()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 |  | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 607 | /* | 
|  | 608 | * opaqueRegion: area of a surface that is fully opaque. | 
|  | 609 | */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | Region opaqueRegion; | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 611 |  | 
|  | 612 | /* | 
|  | 613 | * visibleRegion: area of a surface that is visible on screen | 
|  | 614 | * and not fully transparent. This is essentially the layer's | 
|  | 615 | * footprint minus the opaque regions above it. | 
|  | 616 | * Areas covered by a translucent surface are considered visible. | 
|  | 617 | */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | Region visibleRegion; | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 619 |  | 
|  | 620 | /* | 
|  | 621 | * coveredRegion: area of a surface that is covered by all | 
|  | 622 | * visible regions above it (which includes the translucent areas). | 
|  | 623 | */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | Region coveredRegion; | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 625 |  | 
|  | 626 |  | 
|  | 627 | // handle hidden surfaces by setting the visible region to empty | 
| Mathias Agopian | 9701122 | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 628 | if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) { | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 629 | const bool translucent = !layer->isOpaque(); | 
| Mathias Agopian | 9701122 | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 630 | const Rect bounds(layer->visibleBounds()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | visibleRegion.set(bounds); | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 632 | visibleRegion.andSelf(screenRegion); | 
|  | 633 | if (!visibleRegion.isEmpty()) { | 
|  | 634 | // Remove the transparent area from the visible region | 
|  | 635 | if (translucent) { | 
|  | 636 | visibleRegion.subtractSelf(layer->transparentRegionScreen); | 
|  | 637 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 |  | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 639 | // compute the opaque region | 
|  | 640 | const int32_t layerOrientation = layer->getOrientation(); | 
|  | 641 | if (s.alpha==255 && !translucent && | 
|  | 642 | ((layerOrientation & Transform::ROT_INVALID) == false)) { | 
|  | 643 | // the opaque region is the layer's footprint | 
|  | 644 | opaqueRegion = visibleRegion; | 
|  | 645 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | } | 
|  | 647 | } | 
|  | 648 |  | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 649 | // Clip the covered region to the visible region | 
|  | 650 | coveredRegion = aboveCoveredLayers.intersect(visibleRegion); | 
|  | 651 |  | 
|  | 652 | // Update aboveCoveredLayers for next (lower) layer | 
|  | 653 | aboveCoveredLayers.orSelf(visibleRegion); | 
|  | 654 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | // subtract the opaque region covered by the layers above us | 
|  | 656 | visibleRegion.subtractSelf(aboveOpaqueLayers); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 |  | 
|  | 658 | // compute this layer's dirty region | 
|  | 659 | if (layer->contentDirty) { | 
|  | 660 | // we need to invalidate the whole region | 
|  | 661 | dirty = visibleRegion; | 
|  | 662 | // as well, as the old visible region | 
|  | 663 | dirty.orSelf(layer->visibleRegionScreen); | 
|  | 664 | layer->contentDirty = false; | 
|  | 665 | } else { | 
| Mathias Agopian | a8d44f7 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 666 | /* compute the exposed region: | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 667 | *   the exposed region consists of two components: | 
|  | 668 | *   1) what's VISIBLE now and was COVERED before | 
|  | 669 | *   2) what's EXPOSED now less what was EXPOSED before | 
|  | 670 | * | 
|  | 671 | * note that (1) is conservative, we start with the whole | 
|  | 672 | * visible region but only keep what used to be covered by | 
|  | 673 | * something -- which mean it may have been exposed. | 
|  | 674 | * | 
|  | 675 | * (2) handles areas that were not covered by anything but got | 
|  | 676 | * exposed because of a resize. | 
| Mathias Agopian | a8d44f7 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 677 | */ | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 678 | const Region newExposed = visibleRegion - coveredRegion; | 
|  | 679 | const Region oldVisibleRegion = layer->visibleRegionScreen; | 
|  | 680 | const Region oldCoveredRegion = layer->coveredRegionScreen; | 
|  | 681 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; | 
|  | 682 | dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | } | 
|  | 684 | dirty.subtractSelf(aboveOpaqueLayers); | 
|  | 685 |  | 
|  | 686 | // accumulate to the screen dirty region | 
|  | 687 | dirtyRegion.orSelf(dirty); | 
|  | 688 |  | 
| Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 689 | // Update aboveOpaqueLayers for next (lower) layer | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | aboveOpaqueLayers.orSelf(opaqueRegion); | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 691 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | // Store the visible region is screen space | 
|  | 693 | layer->setVisibleRegion(visibleRegion); | 
|  | 694 | layer->setCoveredRegion(coveredRegion); | 
|  | 695 |  | 
| Mathias Agopian | 9701122 | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 696 | // If a secure layer is partially visible, lock-down the screen! | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | if (layer->isSecure() && !visibleRegion.isEmpty()) { | 
|  | 698 | secureFrameBuffer = true; | 
|  | 699 | } | 
|  | 700 | } | 
|  | 701 |  | 
| Mathias Agopian | 9701122 | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 702 | // invalidate the areas where a layer was removed | 
|  | 703 | dirtyRegion.orSelf(mDirtyRegionRemovedLayer); | 
|  | 704 | mDirtyRegionRemovedLayer.clear(); | 
|  | 705 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | mSecureFrameBuffer = secureFrameBuffer; | 
|  | 707 | opaqueRegion = aboveOpaqueLayers; | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 |  | 
|  | 711 | void SurfaceFlinger::commitTransaction() | 
|  | 712 | { | 
| Jesse Hall | 2f4b68d | 2011-12-02 10:00:00 -0800 | [diff] [blame] | 713 | if (!mLayersPendingRemoval.isEmpty()) { | 
|  | 714 | // Notify removed layers now that they can't be drawn from | 
|  | 715 | for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) { | 
|  | 716 | mLayersPendingRemoval[i]->onRemoved(); | 
|  | 717 | } | 
|  | 718 | mLayersPendingRemoval.clear(); | 
|  | 719 | } | 
|  | 720 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | mDrawingState = mCurrentState; | 
| Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 722 | mTransationPending = false; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 723 | mTransactionCV.broadcast(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | } | 
|  | 725 |  | 
|  | 726 | void SurfaceFlinger::handlePageFlip() | 
|  | 727 | { | 
|  | 728 | bool visibleRegions = mVisibleRegionsDirty; | 
| Mathias Agopian | 1bbafb9 | 2011-03-11 16:54:47 -0800 | [diff] [blame] | 729 | const LayerVector& currentLayers(mDrawingState.layersSortedByZ); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | visibleRegions |= lockPageFlip(currentLayers); | 
|  | 731 |  | 
|  | 732 | const DisplayHardware& hw = graphicPlane(0).displayHardware(); | 
|  | 733 | const Region screenRegion(hw.bounds()); | 
|  | 734 | if (visibleRegions) { | 
|  | 735 | Region opaqueRegion; | 
|  | 736 | computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion); | 
| Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 737 |  | 
|  | 738 | /* | 
|  | 739 | *  rebuild the visible layer list | 
|  | 740 | */ | 
| Mathias Agopian | 1bbafb9 | 2011-03-11 16:54:47 -0800 | [diff] [blame] | 741 | const size_t count = currentLayers.size(); | 
| Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 742 | mVisibleLayersSortedByZ.clear(); | 
| Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 743 | mVisibleLayersSortedByZ.setCapacity(count); | 
|  | 744 | for (size_t i=0 ; i<count ; i++) { | 
|  | 745 | if (!currentLayers[i]->visibleRegionScreen.isEmpty()) | 
|  | 746 | mVisibleLayersSortedByZ.add(currentLayers[i]); | 
|  | 747 | } | 
|  | 748 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | mWormholeRegion = screenRegion.subtract(opaqueRegion); | 
|  | 750 | mVisibleRegionsDirty = false; | 
| Mathias Agopian | ad456f9 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 751 | invalidateHwcGeometry(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | } | 
|  | 753 |  | 
|  | 754 | unlockPageFlip(currentLayers); | 
| Mathias Agopian | 0dfb7b7 | 2011-10-21 15:18:28 -0700 | [diff] [blame] | 755 |  | 
|  | 756 | mDirtyRegion.orSelf(getAndClearInvalidateRegion()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | mDirtyRegion.andSelf(screenRegion); | 
|  | 758 | } | 
|  | 759 |  | 
| Mathias Agopian | ad456f9 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 760 | void SurfaceFlinger::invalidateHwcGeometry() | 
|  | 761 | { | 
|  | 762 | mHwWorkListDirty = true; | 
|  | 763 | } | 
|  | 764 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers) | 
|  | 766 | { | 
|  | 767 | bool recomputeVisibleRegions = false; | 
|  | 768 | size_t count = currentLayers.size(); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 769 | sp<LayerBase> const* layers = currentLayers.array(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | for (size_t i=0 ; i<count ; i++) { | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 771 | const sp<LayerBase>& layer(layers[i]); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | layer->lockPageFlip(recomputeVisibleRegions); | 
|  | 773 | } | 
|  | 774 | return recomputeVisibleRegions; | 
|  | 775 | } | 
|  | 776 |  | 
|  | 777 | void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers) | 
|  | 778 | { | 
|  | 779 | const GraphicPlane& plane(graphicPlane(0)); | 
|  | 780 | const Transform& planeTransform(plane.transform()); | 
|  | 781 | size_t count = currentLayers.size(); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 782 | sp<LayerBase> const* layers = currentLayers.array(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | for (size_t i=0 ; i<count ; i++) { | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 784 | const sp<LayerBase>& layer(layers[i]); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | layer->unlockPageFlip(planeTransform, mDirtyRegion); | 
|  | 786 | } | 
|  | 787 | } | 
|  | 788 |  | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 789 | void SurfaceFlinger::handleWorkList() | 
|  | 790 | { | 
|  | 791 | mHwWorkListDirty = false; | 
|  | 792 | HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer()); | 
|  | 793 | if (hwc.initCheck() == NO_ERROR) { | 
|  | 794 | const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ); | 
|  | 795 | const size_t count = currentLayers.size(); | 
|  | 796 | hwc.createWorkList(count); | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 797 | hwc_layer_t* const cur(hwc.getLayers()); | 
|  | 798 | for (size_t i=0 ; cur && i<count ; i++) { | 
|  | 799 | currentLayers[i]->setGeometry(&cur[i]); | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 800 | if (mDebugDisableHWC || mDebugRegion) { | 
| Mathias Agopian | 73d3ba9 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 801 | cur[i].compositionType = HWC_FRAMEBUFFER; | 
|  | 802 | cur[i].flags |= HWC_SKIP_LAYER; | 
|  | 803 | } | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 804 | } | 
|  | 805 | } | 
|  | 806 | } | 
| Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 807 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | void SurfaceFlinger::handleRepaint() | 
|  | 809 | { | 
| Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 810 | // compute the invalid region | 
| Mathias Agopian | 0656a68 | 2011-09-20 17:22:44 -0700 | [diff] [blame] | 811 | mSwapRegion.orSelf(mDirtyRegion); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 |  | 
|  | 813 | if (UNLIKELY(mDebugRegion)) { | 
|  | 814 | debugFlashRegions(); | 
|  | 815 | } | 
|  | 816 |  | 
| Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 817 | // set the frame buffer | 
|  | 818 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 819 | glMatrixMode(GL_MODELVIEW); | 
|  | 820 | glLoadIdentity(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 |  | 
|  | 822 | uint32_t flags = hw.getFlags(); | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 823 | if ((flags & DisplayHardware::SWAP_RECTANGLE) || | 
|  | 824 | (flags & DisplayHardware::BUFFER_PRESERVED)) | 
| Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 825 | { | 
| Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 826 | // we can redraw only what's dirty, but since SWAP_RECTANGLE only | 
|  | 827 | // takes a rectangle, we must make sure to update that whole | 
|  | 828 | // rectangle in that case | 
|  | 829 | if (flags & DisplayHardware::SWAP_RECTANGLE) { | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 830 | // TODO: we really should be able to pass a region to | 
| Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 831 | // SWAP_RECTANGLE so that we don't have to redraw all this. | 
| Mathias Agopian | 0656a68 | 2011-09-20 17:22:44 -0700 | [diff] [blame] | 832 | mDirtyRegion.set(mSwapRegion.bounds()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | } else { | 
| Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 834 | // in the BUFFER_PRESERVED case, obviously, we can update only | 
|  | 835 | // what's needed and nothing more. | 
|  | 836 | // NOTE: this is NOT a common case, as preserving the backbuffer | 
|  | 837 | // is costly and usually involves copying the whole update back. | 
|  | 838 | } | 
|  | 839 | } else { | 
| Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 840 | if (flags & DisplayHardware::PARTIAL_UPDATES) { | 
| Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 841 | // We need to redraw the rectangle that will be updated | 
|  | 842 | // (pushed to the framebuffer). | 
| Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 843 | // This is needed because PARTIAL_UPDATES only takes one | 
| Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 844 | // rectangle instead of a region (see DisplayHardware::flip()) | 
| Mathias Agopian | 0656a68 | 2011-09-20 17:22:44 -0700 | [diff] [blame] | 845 | mDirtyRegion.set(mSwapRegion.bounds()); | 
| Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 846 | } else { | 
|  | 847 | // we need to redraw everything (the whole screen) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | mDirtyRegion.set(hw.bounds()); | 
| Mathias Agopian | 0656a68 | 2011-09-20 17:22:44 -0700 | [diff] [blame] | 849 | mSwapRegion = mDirtyRegion; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } | 
|  | 851 | } | 
|  | 852 |  | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 853 | setupHardwareComposer(mDirtyRegion); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | composeSurfaces(mDirtyRegion); | 
|  | 855 |  | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 856 | // update the swap region and clear the dirty region | 
|  | 857 | mSwapRegion.orSelf(mDirtyRegion); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | mDirtyRegion.clear(); | 
|  | 859 | } | 
|  | 860 |  | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 861 | void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | { | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 863 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 864 | HWComposer& hwc(hw.getHwComposer()); | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 865 | hwc_layer_t* const cur(hwc.getLayers()); | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 866 | if (!cur) { | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 867 | return; | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 868 | } | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 869 |  | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 870 | const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); | 
|  | 871 | size_t count = layers.size(); | 
|  | 872 |  | 
|  | 873 | LOGE_IF(hwc.getNumLayers() != count, | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 874 | "HAL number of layers (%d) doesn't match surfaceflinger (%d)", | 
|  | 875 | hwc.getNumLayers(), count); | 
|  | 876 |  | 
|  | 877 | // just to be extra-safe, use the smallest count | 
| Erik Gilling | 24925bf | 2010-08-12 23:21:40 -0700 | [diff] [blame] | 878 | if (hwc.initCheck() == NO_ERROR) { | 
|  | 879 | count = count < hwc.getNumLayers() ? count : hwc.getNumLayers(); | 
|  | 880 | } | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 881 |  | 
|  | 882 | /* | 
|  | 883 | *  update the per-frame h/w composer data for each layer | 
|  | 884 | *  and build the transparent region of the FB | 
|  | 885 | */ | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 886 | for (size_t i=0 ; i<count ; i++) { | 
|  | 887 | const sp<LayerBase>& layer(layers[i]); | 
|  | 888 | layer->setPerFrameData(&cur[i]); | 
|  | 889 | } | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 890 | const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 891 | status_t err = hwc.prepare(); | 
|  | 892 | LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err)); | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 893 |  | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 894 | if (err == NO_ERROR) { | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 895 | // what's happening here is tricky. | 
|  | 896 | // we want to clear all the layers with the CLEAR_FB flags | 
|  | 897 | // that are opaque. | 
|  | 898 | // however, since some GPU are efficient at preserving | 
|  | 899 | // the backbuffer, we want to take advantage of that so we do the | 
|  | 900 | // clear only in the dirty region (other areas will be preserved | 
|  | 901 | // on those GPUs). | 
|  | 902 | //   NOTE: on non backbuffer preserving GPU, the dirty region | 
|  | 903 | //   has already been expanded as needed, so the code is correct | 
|  | 904 | //   there too. | 
|  | 905 | // | 
|  | 906 | // However, the content of the framebuffer cannot be trusted when | 
|  | 907 | // we switch to/from FB/OVERLAY, in which case we need to | 
|  | 908 | // expand the dirty region to those areas too. | 
|  | 909 | // | 
|  | 910 | // Note also that there is a special case when switching from | 
|  | 911 | // "no layers in FB" to "some layers in FB", where we need to redraw | 
|  | 912 | // the entire FB, since some areas might contain uninitialized | 
|  | 913 | // data. | 
|  | 914 | // | 
|  | 915 | // Also we want to make sure to not clear areas that belong to | 
| Mathias Agopian | 3a3cad3 | 2011-10-18 17:36:28 -0700 | [diff] [blame] | 916 | // layers above that won't redraw (we would just be erasing them), | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 917 | // that is, we can't erase anything outside the dirty region. | 
|  | 918 |  | 
| Mathias Agopian | f9abeb9 | 2011-09-09 01:47:48 -0700 | [diff] [blame] | 919 | Region transparent; | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 920 |  | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 921 | if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) { | 
|  | 922 | transparent.set(hw.getBounds()); | 
|  | 923 | dirtyInOut = transparent; | 
|  | 924 | } else { | 
|  | 925 | for (size_t i=0 ; i<count ; i++) { | 
|  | 926 | const sp<LayerBase>& layer(layers[i]); | 
|  | 927 | if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) { | 
|  | 928 | transparent.orSelf(layer->visibleRegionScreen); | 
|  | 929 | } | 
|  | 930 | bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER); | 
|  | 931 | if (isOverlay != layer->isOverlay()) { | 
|  | 932 | // we transitioned to/from overlay, so add this layer | 
|  | 933 | // to the dirty region so the framebuffer can be either | 
|  | 934 | // cleared or redrawn. | 
|  | 935 | dirtyInOut.orSelf(layer->visibleRegionScreen); | 
|  | 936 | } | 
|  | 937 | layer->setOverlay(isOverlay); | 
| Mathias Agopian | f20a324 | 2011-01-19 15:24:23 -0800 | [diff] [blame] | 938 | } | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 939 | // don't erase stuff outside the dirty region | 
|  | 940 | transparent.andSelf(dirtyInOut); | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 941 | } | 
|  | 942 |  | 
|  | 943 | /* | 
|  | 944 | *  clear the area of the FB that need to be transparent | 
|  | 945 | */ | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 946 | if (!transparent.isEmpty()) { | 
|  | 947 | glClearColor(0,0,0,0); | 
|  | 948 | Region::const_iterator it = transparent.begin(); | 
|  | 949 | Region::const_iterator const end = transparent.end(); | 
|  | 950 | const int32_t height = hw.getHeight(); | 
|  | 951 | while (it != end) { | 
|  | 952 | const Rect& r(*it++); | 
|  | 953 | const GLint sy = height - (r.top + r.height()); | 
|  | 954 | glScissor(r.left, sy, r.width(), r.height()); | 
|  | 955 | glClear(GL_COLOR_BUFFER_BIT); | 
| Mathias Agopian | f20a324 | 2011-01-19 15:24:23 -0800 | [diff] [blame] | 956 | } | 
| Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 957 | } | 
|  | 958 | } | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 959 | } | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 960 |  | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 961 | void SurfaceFlinger::composeSurfaces(const Region& dirty) | 
|  | 962 | { | 
| Mathias Agopian | cd20eb0 | 2011-09-22 20:57:04 -0700 | [diff] [blame] | 963 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 964 | HWComposer& hwc(hw.getHwComposer()); | 
|  | 965 |  | 
|  | 966 | const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); | 
|  | 967 | if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) { | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 968 | // should never happen unless the window manager has a bug | 
|  | 969 | // draw something... | 
|  | 970 | drawWormhole(); | 
|  | 971 | } | 
|  | 972 |  | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 973 | /* | 
|  | 974 | * and then, render the layers targeted at the framebuffer | 
|  | 975 | */ | 
| Mathias Agopian | cd20eb0 | 2011-09-22 20:57:04 -0700 | [diff] [blame] | 976 | hwc_layer_t* const cur(hwc.getLayers()); | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 977 | const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); | 
|  | 978 | size_t count = layers.size(); | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 979 | for (size_t i=0 ; i<count ; i++) { | 
| Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 980 | if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) { | 
| Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 981 | continue; | 
| Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 982 | } | 
|  | 983 | const sp<LayerBase>& layer(layers[i]); | 
|  | 984 | const Region clip(dirty.intersect(layer->visibleRegionScreen)); | 
|  | 985 | if (!clip.isEmpty()) { | 
|  | 986 | layer->draw(clip); | 
|  | 987 | } | 
|  | 988 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | } | 
|  | 990 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | void SurfaceFlinger::debugFlashRegions() | 
|  | 992 | { | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 993 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 994 | const uint32_t flags = hw.getFlags(); | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 995 | const int32_t height = hw.getHeight(); | 
| Mathias Agopian | 0656a68 | 2011-09-20 17:22:44 -0700 | [diff] [blame] | 996 | if (mSwapRegion.isEmpty()) { | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 997 | return; | 
|  | 998 | } | 
| Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 999 |  | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1000 | if (!((flags & DisplayHardware::SWAP_RECTANGLE) || | 
|  | 1001 | (flags & DisplayHardware::BUFFER_PRESERVED))) { | 
|  | 1002 | const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ? | 
|  | 1003 | mDirtyRegion.bounds() : hw.bounds()); | 
|  | 1004 | composeSurfaces(repaint); | 
|  | 1005 | } | 
|  | 1006 |  | 
| Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 1007 | glDisable(GL_TEXTURE_EXTERNAL_OES); | 
|  | 1008 | glDisable(GL_TEXTURE_2D); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | glDisable(GL_BLEND); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | glDisable(GL_SCISSOR_TEST); | 
|  | 1011 |  | 
| Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1012 | static int toggle = 0; | 
|  | 1013 | toggle = 1 - toggle; | 
|  | 1014 | if (toggle) { | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1015 | glColor4f(1, 0, 1, 1); | 
| Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1016 | } else { | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1017 | glColor4f(1, 1, 0, 1); | 
| Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1018 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 |  | 
| Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1020 | Region::const_iterator it = mDirtyRegion.begin(); | 
|  | 1021 | Region::const_iterator const end = mDirtyRegion.end(); | 
|  | 1022 | while (it != end) { | 
|  | 1023 | const Rect& r = *it++; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | GLfloat vertices[][2] = { | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1025 | { r.left,  height - r.top }, | 
|  | 1026 | { r.left,  height - r.bottom }, | 
|  | 1027 | { r.right, height - r.bottom }, | 
|  | 1028 | { r.right, height - r.top } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | }; | 
|  | 1030 | glVertexPointer(2, GL_FLOAT, 0, vertices); | 
|  | 1031 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 1032 | } | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1033 |  | 
| Mathias Agopian | 0656a68 | 2011-09-20 17:22:44 -0700 | [diff] [blame] | 1034 | hw.flip(mSwapRegion); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 |  | 
|  | 1036 | if (mDebugRegion > 1) | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1037 | usleep(mDebugRegion * 1000); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 |  | 
|  | 1039 | glEnable(GL_SCISSOR_TEST); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | } | 
|  | 1041 |  | 
|  | 1042 | void SurfaceFlinger::drawWormhole() const | 
|  | 1043 | { | 
|  | 1044 | const Region region(mWormholeRegion.intersect(mDirtyRegion)); | 
|  | 1045 | if (region.isEmpty()) | 
|  | 1046 | return; | 
|  | 1047 |  | 
|  | 1048 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 1049 | const int32_t width = hw.getWidth(); | 
|  | 1050 | const int32_t height = hw.getHeight(); | 
|  | 1051 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | if (LIKELY(!mDebugBackground)) { | 
| Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1053 | glClearColor(0,0,0,0); | 
| Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1054 | Region::const_iterator it = region.begin(); | 
|  | 1055 | Region::const_iterator const end = region.end(); | 
|  | 1056 | while (it != end) { | 
|  | 1057 | const Rect& r = *it++; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | const GLint sy = height - (r.top + r.height()); | 
|  | 1059 | glScissor(r.left, sy, r.width(), r.height()); | 
|  | 1060 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 1061 | } | 
|  | 1062 | } else { | 
|  | 1063 | const GLshort vertices[][2] = { { 0, 0 }, { width, 0 }, | 
|  | 1064 | { width, height }, { 0, height }  }; | 
|  | 1065 | const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 },  { 1, 1 }, { 0, 1 } }; | 
| Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 1066 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | glVertexPointer(2, GL_SHORT, 0, vertices); | 
|  | 1068 | glTexCoordPointer(2, GL_SHORT, 0, tcoords); | 
|  | 1069 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
| Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 1070 |  | 
|  | 1071 | glDisable(GL_TEXTURE_EXTERNAL_OES); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | glEnable(GL_TEXTURE_2D); | 
|  | 1073 | glBindTexture(GL_TEXTURE_2D, mWormholeTexName); | 
|  | 1074 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); | 
|  | 1075 | glMatrixMode(GL_TEXTURE); | 
|  | 1076 | glLoadIdentity(); | 
| Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 1077 |  | 
|  | 1078 | glDisable(GL_BLEND); | 
|  | 1079 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1); | 
| Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1081 | Region::const_iterator it = region.begin(); | 
|  | 1082 | Region::const_iterator const end = region.end(); | 
|  | 1083 | while (it != end) { | 
|  | 1084 | const Rect& r = *it++; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | const GLint sy = height - (r.top + r.height()); | 
|  | 1086 | glScissor(r.left, sy, r.width(), r.height()); | 
|  | 1087 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 1088 | } | 
|  | 1089 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1090 | glDisable(GL_TEXTURE_2D); | 
| Mathias Agopian | ebeb709 | 2010-12-14 18:38:36 -0800 | [diff] [blame] | 1091 | glLoadIdentity(); | 
|  | 1092 | glMatrixMode(GL_MODELVIEW); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | } | 
|  | 1094 | } | 
|  | 1095 |  | 
|  | 1096 | void SurfaceFlinger::debugShowFPS() const | 
|  | 1097 | { | 
|  | 1098 | static int mFrameCount; | 
|  | 1099 | static int mLastFrameCount = 0; | 
|  | 1100 | static nsecs_t mLastFpsTime = 0; | 
|  | 1101 | static float mFps = 0; | 
|  | 1102 | mFrameCount++; | 
|  | 1103 | nsecs_t now = systemTime(); | 
|  | 1104 | nsecs_t diff = now - mLastFpsTime; | 
|  | 1105 | if (diff > ms2ns(250)) { | 
|  | 1106 | mFps =  ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff; | 
|  | 1107 | mLastFpsTime = now; | 
|  | 1108 | mLastFrameCount = mFrameCount; | 
|  | 1109 | } | 
|  | 1110 | // XXX: mFPS has the value we want | 
|  | 1111 | } | 
|  | 1112 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1113 | status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | { | 
|  | 1115 | Mutex::Autolock _l(mStateLock); | 
|  | 1116 | addLayer_l(layer); | 
|  | 1117 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); | 
|  | 1118 | return NO_ERROR; | 
|  | 1119 | } | 
|  | 1120 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1121 | status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer) | 
|  | 1122 | { | 
| Mathias Agopian | f6679fc | 2010-08-10 18:09:09 -0700 | [diff] [blame] | 1123 | ssize_t i = mCurrentState.layersSortedByZ.add(layer); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1124 | return (i < 0) ? status_t(i) : status_t(NO_ERROR); | 
|  | 1125 | } | 
|  | 1126 |  | 
|  | 1127 | ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client, | 
|  | 1128 | const sp<LayerBaseClient>& lbc) | 
|  | 1129 | { | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1130 | // attach this layer to the client | 
| Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 1131 | size_t name = client->attachLayer(lbc); | 
|  | 1132 |  | 
|  | 1133 | Mutex::Autolock _l(mStateLock); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1134 |  | 
|  | 1135 | // add this layer to the current state list | 
|  | 1136 | addLayer_l(lbc); | 
|  | 1137 |  | 
| Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 1138 | return ssize_t(name); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1139 | } | 
|  | 1140 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1141 | status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | { | 
|  | 1143 | Mutex::Autolock _l(mStateLock); | 
| Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1144 | status_t err = purgatorizeLayer_l(layer); | 
|  | 1145 | if (err == NO_ERROR) | 
|  | 1146 | setTransactionFlags(eTransactionNeeded); | 
|  | 1147 | return err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | } | 
|  | 1149 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1150 | status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | { | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1152 | sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient()); | 
|  | 1153 | if (lbc != 0) { | 
| Mathias Agopian | 0d15612 | 2011-01-25 20:17:45 -0800 | [diff] [blame] | 1154 | mLayerMap.removeItem( lbc->getSurfaceBinder() ); | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1155 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase); | 
|  | 1157 | if (index >= 0) { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1158 | mLayersRemoved = true; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | return NO_ERROR; | 
|  | 1160 | } | 
| Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1161 | return status_t(index); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | } | 
|  | 1163 |  | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1164 | status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase) | 
|  | 1165 | { | 
| Mathias Agopian | 76cd4dd | 2011-01-14 17:37:42 -0800 | [diff] [blame] | 1166 | // First add the layer to the purgatory list, which makes sure it won't | 
|  | 1167 | // go away, then remove it from the main list (through a transaction). | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1168 | ssize_t err = removeLayer_l(layerBase); | 
| Mathias Agopian | 76cd4dd | 2011-01-14 17:37:42 -0800 | [diff] [blame] | 1169 | if (err >= 0) { | 
|  | 1170 | mLayerPurgatory.add(layerBase); | 
|  | 1171 | } | 
| Mathias Agopian | 8c0a3d7 | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 1172 |  | 
| Jesse Hall | 2f4b68d | 2011-12-02 10:00:00 -0800 | [diff] [blame] | 1173 | mLayersPendingRemoval.push(layerBase); | 
| Mathias Agopian | 0b3ad46 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 1174 |  | 
| Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1175 | // it's possible that we don't find a layer, because it might | 
|  | 1176 | // have been destroyed already -- this is not technically an error | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1177 | // from the user because there is a race between Client::destroySurface(), | 
|  | 1178 | // ~Client() and ~ISurface(). | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1179 | return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err; | 
|  | 1180 | } | 
|  | 1181 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1182 | status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | { | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1184 | layer->forceVisibilityTransaction(); | 
|  | 1185 | setTransactionFlags(eTraversalNeeded); | 
|  | 1186 | return NO_ERROR; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | } | 
|  | 1188 |  | 
| Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 1189 | uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) | 
|  | 1190 | { | 
|  | 1191 | return android_atomic_release_load(&mTransactionFlags); | 
|  | 1192 | } | 
|  | 1193 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) | 
|  | 1195 | { | 
|  | 1196 | return android_atomic_and(~flags, &mTransactionFlags) & flags; | 
|  | 1197 | } | 
|  | 1198 |  | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1199 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | { | 
|  | 1201 | uint32_t old = android_atomic_or(flags, &mTransactionFlags); | 
|  | 1202 | if ((old & flags)==0) { // wake the server up | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1203 | signalEvent(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | } | 
|  | 1205 | return old; | 
|  | 1206 | } | 
|  | 1207 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 |  | 
| Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 1209 | void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state, | 
| Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 1210 | int orientation, uint32_t flags) { | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1211 | Mutex::Autolock _l(mStateLock); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1212 |  | 
| Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 1213 | uint32_t transactionFlags = 0; | 
| Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 1214 | if (mCurrentState.orientation != orientation) { | 
|  | 1215 | if (uint32_t(orientation)<=eOrientation270 || orientation==42) { | 
|  | 1216 | mCurrentState.orientation = orientation; | 
| Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 1217 | transactionFlags |= eTransactionNeeded; | 
| Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 1218 | } else if (orientation != eOrientationUnchanged) { | 
|  | 1219 | LOGW("setTransactionState: ignoring unrecognized orientation: %d", | 
|  | 1220 | orientation); | 
|  | 1221 | } | 
|  | 1222 | } | 
|  | 1223 |  | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1224 | const size_t count = state.size(); | 
|  | 1225 | for (size_t i=0 ; i<count ; i++) { | 
|  | 1226 | const ComposerState& s(state[i]); | 
|  | 1227 | sp<Client> client( static_cast<Client *>(s.client.get()) ); | 
| Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 1228 | transactionFlags |= setClientStateLocked(client, s.state); | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1229 | } | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1230 |  | 
| Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 1231 | if (transactionFlags) { | 
|  | 1232 | // this triggers the transaction | 
|  | 1233 | setTransactionFlags(transactionFlags); | 
|  | 1234 |  | 
|  | 1235 | // if this is a synchronous transaction, wait for it to take effect | 
|  | 1236 | // before returning. | 
|  | 1237 | if (flags & eSynchronous) { | 
|  | 1238 | mTransationPending = true; | 
|  | 1239 | } | 
|  | 1240 | while (mTransationPending) { | 
|  | 1241 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); | 
|  | 1242 | if (CC_UNLIKELY(err != NO_ERROR)) { | 
|  | 1243 | // just in case something goes wrong in SF, return to the | 
|  | 1244 | // called after a few seconds. | 
|  | 1245 | LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!"); | 
|  | 1246 | mTransationPending = false; | 
|  | 1247 | break; | 
|  | 1248 | } | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1249 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | } | 
|  | 1251 | } | 
|  | 1252 |  | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1253 | int SurfaceFlinger::setOrientation(DisplayID dpy, | 
| Mathias Agopian | c08731e | 2009-03-27 18:11:38 -0700 | [diff] [blame] | 1254 | int orientation, uint32_t flags) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | { | 
|  | 1256 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) | 
|  | 1257 | return BAD_VALUE; | 
|  | 1258 |  | 
|  | 1259 | Mutex::Autolock _l(mStateLock); | 
|  | 1260 | if (mCurrentState.orientation != orientation) { | 
|  | 1261 | if (uint32_t(orientation)<=eOrientation270 || orientation==42) { | 
| Jeff Brown | 21230c6 | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 1262 | mCurrentState.orientationFlags = flags; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | mCurrentState.orientation = orientation; | 
|  | 1264 | setTransactionFlags(eTransactionNeeded); | 
|  | 1265 | mTransactionCV.wait(mStateLock); | 
|  | 1266 | } else { | 
|  | 1267 | orientation = BAD_VALUE; | 
|  | 1268 | } | 
|  | 1269 | } | 
|  | 1270 | return orientation; | 
|  | 1271 | } | 
|  | 1272 |  | 
| Mathias Agopian | 0ef4e15 | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 1273 | sp<ISurface> SurfaceFlinger::createSurface( | 
|  | 1274 | ISurfaceComposerClient::surface_data_t* params, | 
|  | 1275 | const String8& name, | 
|  | 1276 | const sp<Client>& client, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | DisplayID d, uint32_t w, uint32_t h, PixelFormat format, | 
|  | 1278 | uint32_t flags) | 
|  | 1279 | { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1280 | sp<LayerBaseClient> layer; | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1281 | sp<ISurface> surfaceHandle; | 
| Mathias Agopian | 6e2d648 | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 1282 |  | 
|  | 1283 | if (int32_t(w|h) < 0) { | 
|  | 1284 | LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)", | 
|  | 1285 | int(w), int(h)); | 
|  | 1286 | return surfaceHandle; | 
|  | 1287 | } | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1288 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | //LOGD("createSurface for pid %d (%d x %d)", pid, w, h); | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1290 | sp<Layer> normalLayer; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | switch (flags & eFXSurfaceMask) { | 
|  | 1292 | case eFXSurfaceNormal: | 
| Mathias Agopian | a5529c8 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 1293 | normalLayer = createNormalSurface(client, d, w, h, flags, format); | 
|  | 1294 | layer = normalLayer; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | break; | 
|  | 1296 | case eFXSurfaceBlur: | 
| Mathias Agopian | 1293a8e | 2010-12-08 17:13:19 -0800 | [diff] [blame] | 1297 | // for now we treat Blur as Dim, until we can implement it | 
|  | 1298 | // efficiently. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | case eFXSurfaceDim: | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1300 | layer = createDimSurface(client, d, w, h, flags); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | break; | 
| Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1302 | case eFXSurfaceScreenshot: | 
|  | 1303 | layer = createScreenshotSurface(client, d, w, h, flags); | 
|  | 1304 | break; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1305 | } | 
|  | 1306 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1307 | if (layer != 0) { | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1308 | layer->initStates(w, h, flags); | 
| Mathias Agopian | 285dbde | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 1309 | layer->setName(name); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1310 | ssize_t token = addClientLayer(client, layer); | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1311 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | surfaceHandle = layer->getSurface(); | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1313 | if (surfaceHandle != 0) { | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1314 | params->token = token; | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1315 | params->identity = layer->getIdentity(); | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1316 | if (normalLayer != 0) { | 
|  | 1317 | Mutex::Autolock _l(mStateLock); | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1318 | mLayerMap.add(layer->getSurfaceBinder(), normalLayer); | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1319 | } | 
| Mathias Agopian | 1c97d2e | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1320 | } | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1321 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1322 | setTransactionFlags(eTransactionNeeded); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | } | 
|  | 1324 |  | 
|  | 1325 | return surfaceHandle; | 
|  | 1326 | } | 
|  | 1327 |  | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1328 | sp<Layer> SurfaceFlinger::createNormalSurface( | 
| Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1329 | const sp<Client>& client, DisplayID display, | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1330 | uint32_t w, uint32_t h, uint32_t flags, | 
| Mathias Agopian | 1c97d2e | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1331 | PixelFormat& format) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | { | 
|  | 1333 | // initialize the surfaces | 
|  | 1334 | switch (format) { // TODO: take h/w into account | 
|  | 1335 | case PIXEL_FORMAT_TRANSPARENT: | 
|  | 1336 | case PIXEL_FORMAT_TRANSLUCENT: | 
|  | 1337 | format = PIXEL_FORMAT_RGBA_8888; | 
|  | 1338 | break; | 
|  | 1339 | case PIXEL_FORMAT_OPAQUE: | 
| Mathias Agopian | a8f3e4e | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1340 | #ifdef NO_RGBX_8888 | 
|  | 1341 | format = PIXEL_FORMAT_RGB_565; | 
|  | 1342 | #else | 
| Mathias Agopian | 8f10540 | 2010-04-05 18:01:24 -0700 | [diff] [blame] | 1343 | format = PIXEL_FORMAT_RGBX_8888; | 
| Mathias Agopian | a8f3e4e | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1344 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | break; | 
|  | 1346 | } | 
|  | 1347 |  | 
| Mathias Agopian | a8f3e4e | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1348 | #ifdef NO_RGBX_8888 | 
|  | 1349 | if (format == PIXEL_FORMAT_RGBX_8888) | 
|  | 1350 | format = PIXEL_FORMAT_RGBA_8888; | 
|  | 1351 | #endif | 
|  | 1352 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1353 | sp<Layer> layer = new Layer(this, display, client); | 
| Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1354 | status_t err = layer->setBuffers(w, h, format, flags); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1355 | if (LIKELY(err != NO_ERROR)) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err)); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1357 | layer.clear(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | } | 
|  | 1359 | return layer; | 
|  | 1360 | } | 
|  | 1361 |  | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 1362 | sp<LayerDim> SurfaceFlinger::createDimSurface( | 
| Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1363 | const sp<Client>& client, DisplayID display, | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1364 | uint32_t w, uint32_t h, uint32_t flags) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | { | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1366 | sp<LayerDim> layer = new LayerDim(this, display, client); | 
| Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1367 | return layer; | 
|  | 1368 | } | 
|  | 1369 |  | 
|  | 1370 | sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface( | 
|  | 1371 | const sp<Client>& client, DisplayID display, | 
|  | 1372 | uint32_t w, uint32_t h, uint32_t flags) | 
|  | 1373 | { | 
|  | 1374 | sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | return layer; | 
|  | 1376 | } | 
|  | 1377 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1378 | status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | { | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1380 | /* | 
|  | 1381 | * called by the window manager, when a surface should be marked for | 
|  | 1382 | * destruction. | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1383 | * | 
| Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1384 | * The surface is removed from the current and drawing lists, but placed | 
|  | 1385 | * in the purgatory queue, so it's not destroyed right-away (we need | 
|  | 1386 | * to wait for all client's references to go away first). | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1387 | */ | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1388 |  | 
| Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1389 | status_t err = NAME_NOT_FOUND; | 
| Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1390 | Mutex::Autolock _l(mStateLock); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1391 | sp<LayerBaseClient> layer = client->getLayerUser(sid); | 
| Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1392 | if (layer != 0) { | 
|  | 1393 | err = purgatorizeLayer_l(layer); | 
|  | 1394 | if (err == NO_ERROR) { | 
| Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1395 | setTransactionFlags(eTransactionNeeded); | 
|  | 1396 | } | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1397 | } | 
|  | 1398 | return err; | 
|  | 1399 | } | 
|  | 1400 |  | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1401 | status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer) | 
| Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1402 | { | 
| Mathias Agopian | 759fdb2 | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 1403 | // called by ~ISurface() when all references are gone | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1404 | status_t err = NO_ERROR; | 
|  | 1405 | sp<LayerBaseClient> l(layer.promote()); | 
|  | 1406 | if (l != NULL) { | 
|  | 1407 | Mutex::Autolock _l(mStateLock); | 
|  | 1408 | err = removeLayer_l(l); | 
|  | 1409 | if (err == NAME_NOT_FOUND) { | 
|  | 1410 | // The surface wasn't in the current list, which means it was | 
|  | 1411 | // removed already, which means it is in the purgatory, | 
|  | 1412 | // and need to be removed from there. | 
|  | 1413 | ssize_t idx = mLayerPurgatory.remove(l); | 
|  | 1414 | LOGE_IF(idx < 0, | 
|  | 1415 | "layer=%p is not in the purgatory list", l.get()); | 
| Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1416 | } | 
| Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1417 | LOGE_IF(err<0 && err != NAME_NOT_FOUND, | 
|  | 1418 | "error removing layer=%p (%s)", l.get(), strerror(-err)); | 
|  | 1419 | } | 
|  | 1420 | return err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | } | 
|  | 1422 |  | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1423 | uint32_t SurfaceFlinger::setClientStateLocked( | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1424 | const sp<Client>& client, | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1425 | const layer_state_t& s) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1426 | { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | uint32_t flags = 0; | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1428 | sp<LayerBaseClient> layer(client->getLayerUser(s.surface)); | 
|  | 1429 | if (layer != 0) { | 
|  | 1430 | const uint32_t what = s.what; | 
|  | 1431 | if (what & ePositionChanged) { | 
|  | 1432 | if (layer->setPosition(s.x, s.y)) | 
|  | 1433 | flags |= eTraversalNeeded; | 
|  | 1434 | } | 
|  | 1435 | if (what & eLayerChanged) { | 
|  | 1436 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); | 
|  | 1437 | if (layer->setLayer(s.z)) { | 
|  | 1438 | mCurrentState.layersSortedByZ.removeAt(idx); | 
|  | 1439 | mCurrentState.layersSortedByZ.add(layer); | 
|  | 1440 | // we need traversal (state changed) | 
|  | 1441 | // AND transaction (list changed) | 
|  | 1442 | flags |= eTransactionNeeded|eTraversalNeeded; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | } | 
|  | 1444 | } | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1445 | if (what & eSizeChanged) { | 
|  | 1446 | if (layer->setSize(s.w, s.h)) { | 
|  | 1447 | flags |= eTraversalNeeded; | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1448 | } | 
|  | 1449 | } | 
|  | 1450 | if (what & eAlphaChanged) { | 
|  | 1451 | if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f))) | 
|  | 1452 | flags |= eTraversalNeeded; | 
|  | 1453 | } | 
|  | 1454 | if (what & eMatrixChanged) { | 
|  | 1455 | if (layer->setMatrix(s.matrix)) | 
|  | 1456 | flags |= eTraversalNeeded; | 
|  | 1457 | } | 
|  | 1458 | if (what & eTransparentRegionChanged) { | 
|  | 1459 | if (layer->setTransparentRegionHint(s.transparentRegion)) | 
|  | 1460 | flags |= eTraversalNeeded; | 
|  | 1461 | } | 
|  | 1462 | if (what & eVisibilityChanged) { | 
|  | 1463 | if (layer->setFlags(s.flags, s.mask)) | 
|  | 1464 | flags |= eTraversalNeeded; | 
|  | 1465 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1466 | } | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1467 | return flags; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | } | 
|  | 1469 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | void SurfaceFlinger::screenReleased(int dpy) | 
|  | 1471 | { | 
|  | 1472 | // this may be called by a signal handler, we can't do too much in here | 
|  | 1473 | android_atomic_or(eConsoleReleased, &mConsoleSignals); | 
|  | 1474 | signalEvent(); | 
|  | 1475 | } | 
|  | 1476 |  | 
|  | 1477 | void SurfaceFlinger::screenAcquired(int dpy) | 
|  | 1478 | { | 
|  | 1479 | // this may be called by a signal handler, we can't do too much in here | 
|  | 1480 | android_atomic_or(eConsoleAcquired, &mConsoleSignals); | 
|  | 1481 | signalEvent(); | 
|  | 1482 | } | 
|  | 1483 |  | 
|  | 1484 | status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args) | 
|  | 1485 | { | 
| Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 1486 | const size_t SIZE = 4096; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | char buffer[SIZE]; | 
|  | 1488 | String8 result; | 
| Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1489 |  | 
|  | 1490 | if (!PermissionCache::checkCallingPermission(sDump)) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | snprintf(buffer, SIZE, "Permission Denial: " | 
|  | 1492 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", | 
|  | 1493 | IPCThreadState::self()->getCallingPid(), | 
|  | 1494 | IPCThreadState::self()->getCallingUid()); | 
|  | 1495 | result.append(buffer); | 
|  | 1496 | } else { | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1497 |  | 
|  | 1498 | // figure out if we're stuck somewhere | 
|  | 1499 | const nsecs_t now = systemTime(); | 
|  | 1500 | const nsecs_t inSwapBuffers(mDebugInSwapBuffers); | 
|  | 1501 | const nsecs_t inTransaction(mDebugInTransaction); | 
|  | 1502 | nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0; | 
|  | 1503 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; | 
|  | 1504 |  | 
|  | 1505 | // Try to get the main lock, but don't insist if we can't | 
|  | 1506 | // (this would indicate SF is stuck, but we want to be able to | 
|  | 1507 | // print something in dumpsys). | 
|  | 1508 | int retry = 3; | 
|  | 1509 | while (mStateLock.tryLock()<0 && --retry>=0) { | 
|  | 1510 | usleep(1000000); | 
|  | 1511 | } | 
|  | 1512 | const bool locked(retry >= 0); | 
|  | 1513 | if (!locked) { | 
| Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1514 | snprintf(buffer, SIZE, | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1515 | "SurfaceFlinger appears to be unresponsive, " | 
|  | 1516 | "dumping anyways (no locks held)\n"); | 
|  | 1517 | result.append(buffer); | 
|  | 1518 | } | 
|  | 1519 |  | 
| Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1520 | /* | 
|  | 1521 | * Dump the visible layer list | 
|  | 1522 | */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; | 
|  | 1524 | const size_t count = currentLayers.size(); | 
| Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1525 | snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count); | 
|  | 1526 | result.append(buffer); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | for (size_t i=0 ; i<count ; i++) { | 
| Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1528 | const sp<LayerBase>& layer(currentLayers[i]); | 
|  | 1529 | layer->dump(result, buffer, SIZE); | 
|  | 1530 | const Layer::State& s(layer->drawingState()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | s.transparentRegion.dump(result, "transparentRegion"); | 
|  | 1532 | layer->transparentRegionScreen.dump(result, "transparentRegionScreen"); | 
|  | 1533 | layer->visibleRegionScreen.dump(result, "visibleRegionScreen"); | 
|  | 1534 | } | 
| Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1535 |  | 
| Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1536 | /* | 
|  | 1537 | * Dump the layers in the purgatory | 
|  | 1538 | */ | 
|  | 1539 |  | 
| Mathias Agopian | bc2d79e | 2011-11-29 17:55:46 -0800 | [diff] [blame] | 1540 | const size_t purgatorySize = mLayerPurgatory.size(); | 
| Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1541 | snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize); | 
|  | 1542 | result.append(buffer); | 
|  | 1543 | for (size_t i=0 ; i<purgatorySize ; i++) { | 
|  | 1544 | const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i)); | 
|  | 1545 | layer->shortDump(result, buffer, SIZE); | 
|  | 1546 | } | 
|  | 1547 |  | 
|  | 1548 | /* | 
|  | 1549 | * Dump SurfaceFlinger global state | 
|  | 1550 | */ | 
|  | 1551 |  | 
| Mathias Agopian | ad70186 | 2011-07-14 18:01:49 -0700 | [diff] [blame] | 1552 | snprintf(buffer, SIZE, "SurfaceFlinger global state:\n"); | 
| Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1553 | result.append(buffer); | 
| Mathias Agopian | ad70186 | 2011-07-14 18:01:49 -0700 | [diff] [blame] | 1554 |  | 
|  | 1555 | const GLExtensions& extensions(GLExtensions::getInstance()); | 
|  | 1556 | snprintf(buffer, SIZE, "GLES: %s, %s, %s\n", | 
|  | 1557 | extensions.getVendor(), | 
|  | 1558 | extensions.getRenderer(), | 
|  | 1559 | extensions.getVersion()); | 
|  | 1560 | result.append(buffer); | 
| Mathias Agopian | bc2d79e | 2011-11-29 17:55:46 -0800 | [diff] [blame] | 1561 |  | 
|  | 1562 | snprintf(buffer, SIZE, "EGL : %s\n", | 
|  | 1563 | eglQueryString(graphicPlane(0).getEGLDisplay(), | 
|  | 1564 | EGL_VERSION_HW_ANDROID)); | 
|  | 1565 | result.append(buffer); | 
|  | 1566 |  | 
| Mathias Agopian | ad70186 | 2011-07-14 18:01:49 -0700 | [diff] [blame] | 1567 | snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension()); | 
|  | 1568 | result.append(buffer); | 
|  | 1569 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | mWormholeRegion.dump(result, "WormholeRegion"); | 
|  | 1571 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 1572 | snprintf(buffer, SIZE, | 
| Jamie Gennis | a402c4c | 2011-10-14 16:44:08 -0700 | [diff] [blame] | 1573 | "  orientation=%d, canDraw=%d\n", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | mCurrentState.orientation, hw.canDraw()); | 
|  | 1575 | result.append(buffer); | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1576 | snprintf(buffer, SIZE, | 
|  | 1577 | "  last eglSwapBuffers() time: %f us\n" | 
| Mathias Agopian | d5e4ef9 | 2011-10-20 17:22:38 -0700 | [diff] [blame] | 1578 | "  last transaction time     : %f us\n" | 
|  | 1579 | "  refresh-rate              : %f fps\n" | 
|  | 1580 | "  x-dpi                     : %f\n" | 
|  | 1581 | "  y-dpi                     : %f\n", | 
|  | 1582 | mLastSwapBufferTime/1000.0, | 
|  | 1583 | mLastTransactionTime/1000.0, | 
|  | 1584 | hw.getRefreshRate(), | 
|  | 1585 | hw.getDpiX(), | 
|  | 1586 | hw.getDpiY()); | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1587 | result.append(buffer); | 
| Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1588 |  | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1589 | if (inSwapBuffersDuration || !locked) { | 
|  | 1590 | snprintf(buffer, SIZE, "  eglSwapBuffers time: %f us\n", | 
|  | 1591 | inSwapBuffersDuration/1000.0); | 
|  | 1592 | result.append(buffer); | 
|  | 1593 | } | 
| Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1594 |  | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1595 | if (inTransactionDuration || !locked) { | 
|  | 1596 | snprintf(buffer, SIZE, "  transaction time: %f us\n", | 
|  | 1597 | inTransactionDuration/1000.0); | 
|  | 1598 | result.append(buffer); | 
|  | 1599 | } | 
| Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1600 |  | 
| Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1601 | /* | 
|  | 1602 | * Dump HWComposer state | 
|  | 1603 | */ | 
| Mathias Agopian | 73d3ba9 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 1604 | HWComposer& hwc(hw.getHwComposer()); | 
|  | 1605 | snprintf(buffer, SIZE, "  h/w composer %s and %s\n", | 
|  | 1606 | hwc.initCheck()==NO_ERROR ? "present" : "not present", | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1607 | (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled"); | 
| Mathias Agopian | 73d3ba9 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 1608 | result.append(buffer); | 
| Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 1609 | hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ); | 
| Mathias Agopian | 73d3ba9 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 1610 |  | 
| Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1611 | /* | 
|  | 1612 | * Dump gralloc state | 
|  | 1613 | */ | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1614 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1615 | alloc.dump(result); | 
| Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 1616 | hw.dump(result); | 
| Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1617 |  | 
|  | 1618 | if (locked) { | 
|  | 1619 | mStateLock.unlock(); | 
|  | 1620 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | } | 
|  | 1622 | write(fd, result.string(), result.size()); | 
|  | 1623 | return NO_ERROR; | 
|  | 1624 | } | 
|  | 1625 |  | 
|  | 1626 | status_t SurfaceFlinger::onTransact( | 
|  | 1627 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 1628 | { | 
|  | 1629 | switch (code) { | 
|  | 1630 | case CREATE_CONNECTION: | 
| Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1631 | case SET_TRANSACTION_STATE: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | case SET_ORIENTATION: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | case BOOT_FINISHED: | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1634 | case TURN_ELECTRON_BEAM_OFF: | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1635 | case TURN_ELECTRON_BEAM_ON: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | { | 
|  | 1637 | // codes that require permission check | 
|  | 1638 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 1639 | const int pid = ipc->getCallingPid(); | 
| Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 1640 | const int uid = ipc->getCallingUid(); | 
| Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1641 | if ((uid != AID_GRAPHICS) && | 
|  | 1642 | !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) { | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1643 | LOGE("Permission Denial: " | 
|  | 1644 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); | 
|  | 1645 | return PERMISSION_DENIED; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | } | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1647 | break; | 
|  | 1648 | } | 
|  | 1649 | case CAPTURE_SCREEN: | 
|  | 1650 | { | 
|  | 1651 | // codes that require permission check | 
|  | 1652 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 1653 | const int pid = ipc->getCallingPid(); | 
|  | 1654 | const int uid = ipc->getCallingUid(); | 
| Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1655 | if ((uid != AID_GRAPHICS) && | 
|  | 1656 | !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1657 | LOGE("Permission Denial: " | 
|  | 1658 | "can't read framebuffer pid=%d, uid=%d", pid, uid); | 
|  | 1659 | return PERMISSION_DENIED; | 
|  | 1660 | } | 
|  | 1661 | break; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | } | 
|  | 1663 | } | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1664 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); | 
|  | 1666 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { | 
| Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 1667 | CHECK_INTERFACE(ISurfaceComposer, data, reply); | 
| Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1668 | if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) { | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1669 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 1670 | const int pid = ipc->getCallingPid(); | 
|  | 1671 | const int uid = ipc->getCallingUid(); | 
|  | 1672 | LOGE("Permission Denial: " | 
|  | 1673 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1674 | return PERMISSION_DENIED; | 
|  | 1675 | } | 
|  | 1676 | int n; | 
|  | 1677 | switch (code) { | 
| Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1678 | case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE | 
| Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 1679 | case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | return NO_ERROR; | 
|  | 1681 | case 1002:  // SHOW_UPDATES | 
|  | 1682 | n = data.readInt32(); | 
|  | 1683 | mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1684 | invalidateHwcGeometry(); | 
|  | 1685 | repaintEverything(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | return NO_ERROR; | 
|  | 1687 | case 1003:  // SHOW_BACKGROUND | 
|  | 1688 | n = data.readInt32(); | 
|  | 1689 | mDebugBackground = n ? 1 : 0; | 
|  | 1690 | return NO_ERROR; | 
|  | 1691 | case 1004:{ // repaint everything | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1692 | repaintEverything(); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1693 | return NO_ERROR; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | } | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1695 | case 1005:{ // force transaction | 
|  | 1696 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); | 
|  | 1697 | return NO_ERROR; | 
|  | 1698 | } | 
| Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 1699 | case 1006:{ // enable/disable GraphicLog | 
|  | 1700 | int enabled = data.readInt32(); | 
|  | 1701 | GraphicLog::getInstance().setEnabled(enabled); | 
|  | 1702 | return NO_ERROR; | 
|  | 1703 | } | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1704 | case 1008:  // toggle use of hw composer | 
|  | 1705 | n = data.readInt32(); | 
|  | 1706 | mDebugDisableHWC = n ? 1 : 0; | 
|  | 1707 | invalidateHwcGeometry(); | 
|  | 1708 | repaintEverything(); | 
|  | 1709 | return NO_ERROR; | 
| Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 1710 | case 1009:  // toggle use of transform hint | 
|  | 1711 | n = data.readInt32(); | 
|  | 1712 | mDebugDisableTransformHint = n ? 1 : 0; | 
|  | 1713 | invalidateHwcGeometry(); | 
|  | 1714 | repaintEverything(); | 
|  | 1715 | return NO_ERROR; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1716 | case 1010:  // interrogate. | 
| Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1717 | reply->writeInt32(0); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1718 | reply->writeInt32(0); | 
|  | 1719 | reply->writeInt32(mDebugRegion); | 
|  | 1720 | reply->writeInt32(mDebugBackground); | 
|  | 1721 | return NO_ERROR; | 
|  | 1722 | case 1013: { | 
|  | 1723 | Mutex::Autolock _l(mStateLock); | 
|  | 1724 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 1725 | reply->writeInt32(hw.getPageFlipCount()); | 
|  | 1726 | } | 
|  | 1727 | return NO_ERROR; | 
|  | 1728 | } | 
|  | 1729 | } | 
|  | 1730 | return err; | 
|  | 1731 | } | 
|  | 1732 |  | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1733 | void SurfaceFlinger::repaintEverything() { | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1734 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
| Mathias Agopian | 0dfb7b7 | 2011-10-21 15:18:28 -0700 | [diff] [blame] | 1735 | const Rect bounds(hw.getBounds()); | 
|  | 1736 | setInvalidateRegion(Region(bounds)); | 
| Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1737 | signalEvent(); | 
|  | 1738 | } | 
|  | 1739 |  | 
| Mathias Agopian | 0dfb7b7 | 2011-10-21 15:18:28 -0700 | [diff] [blame] | 1740 | void SurfaceFlinger::setInvalidateRegion(const Region& reg) { | 
|  | 1741 | Mutex::Autolock _l(mInvalidateLock); | 
|  | 1742 | mInvalidateRegion = reg; | 
|  | 1743 | } | 
|  | 1744 |  | 
|  | 1745 | Region SurfaceFlinger::getAndClearInvalidateRegion() { | 
|  | 1746 | Mutex::Autolock _l(mInvalidateLock); | 
|  | 1747 | Region reg(mInvalidateRegion); | 
|  | 1748 | mInvalidateRegion.clear(); | 
|  | 1749 | return reg; | 
|  | 1750 | } | 
|  | 1751 |  | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1752 | // --------------------------------------------------------------------------- | 
|  | 1753 |  | 
| Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1754 | status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy, | 
|  | 1755 | GLuint* textureName, GLfloat* uOut, GLfloat* vOut) | 
|  | 1756 | { | 
|  | 1757 | Mutex::Autolock _l(mStateLock); | 
|  | 1758 | return renderScreenToTextureLocked(dpy, textureName, uOut, vOut); | 
|  | 1759 | } | 
|  | 1760 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1761 | status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy, | 
|  | 1762 | GLuint* textureName, GLfloat* uOut, GLfloat* vOut) | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1763 | { | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1764 | if (!GLExtensions::getInstance().haveFramebufferObject()) | 
|  | 1765 | return INVALID_OPERATION; | 
|  | 1766 |  | 
|  | 1767 | // get screen geometry | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1768 | const DisplayHardware& hw(graphicPlane(dpy).displayHardware()); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1769 | const uint32_t hw_w = hw.getWidth(); | 
|  | 1770 | const uint32_t hw_h = hw.getHeight(); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1771 | GLfloat u = 1; | 
|  | 1772 | GLfloat v = 1; | 
|  | 1773 |  | 
|  | 1774 | // make sure to clear all GL error flags | 
|  | 1775 | while ( glGetError() != GL_NO_ERROR ) ; | 
|  | 1776 |  | 
|  | 1777 | // create a FBO | 
|  | 1778 | GLuint name, tname; | 
|  | 1779 | glGenTextures(1, &tname); | 
|  | 1780 | glBindTexture(GL_TEXTURE_2D, tname); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1781 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, | 
|  | 1782 | hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1783 | if (glGetError() != GL_NO_ERROR) { | 
| Mathias Agopian | 015fb3f | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 1784 | while ( glGetError() != GL_NO_ERROR ) ; | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1785 | GLint tw = (2 << (31 - clz(hw_w))); | 
|  | 1786 | GLint th = (2 << (31 - clz(hw_h))); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1787 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, | 
|  | 1788 | tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1789 | u = GLfloat(hw_w) / tw; | 
|  | 1790 | v = GLfloat(hw_h) / th; | 
|  | 1791 | } | 
|  | 1792 | glGenFramebuffersOES(1, &name); | 
|  | 1793 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1794 | glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, | 
|  | 1795 | GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1796 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1797 | // redraw the screen entirely... | 
| Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 1798 | glDisable(GL_TEXTURE_EXTERNAL_OES); | 
|  | 1799 | glDisable(GL_TEXTURE_2D); | 
| Mathias Agopian | 62f7114 | 2011-10-26 15:11:59 -0700 | [diff] [blame] | 1800 | glDisable(GL_SCISSOR_TEST); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1801 | glClearColor(0,0,0,1); | 
|  | 1802 | glClear(GL_COLOR_BUFFER_BIT); | 
| Mathias Agopian | 62f7114 | 2011-10-26 15:11:59 -0700 | [diff] [blame] | 1803 | glEnable(GL_SCISSOR_TEST); | 
| Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 1804 | glMatrixMode(GL_MODELVIEW); | 
|  | 1805 | glLoadIdentity(); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1806 | const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ); | 
|  | 1807 | const size_t count = layers.size(); | 
|  | 1808 | for (size_t i=0 ; i<count ; ++i) { | 
|  | 1809 | const sp<LayerBase>& layer(layers[i]); | 
|  | 1810 | layer->drawForSreenShot(); | 
|  | 1811 | } | 
|  | 1812 |  | 
| Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1813 | hw.compositionComplete(); | 
|  | 1814 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1815 | // back to main framebuffer | 
|  | 1816 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); | 
|  | 1817 | glDisable(GL_SCISSOR_TEST); | 
|  | 1818 | glDeleteFramebuffersOES(1, &name); | 
|  | 1819 |  | 
|  | 1820 | *textureName = tname; | 
|  | 1821 | *uOut = u; | 
|  | 1822 | *vOut = v; | 
|  | 1823 | return NO_ERROR; | 
|  | 1824 | } | 
|  | 1825 |  | 
|  | 1826 | // --------------------------------------------------------------------------- | 
|  | 1827 |  | 
|  | 1828 | status_t SurfaceFlinger::electronBeamOffAnimationImplLocked() | 
|  | 1829 | { | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1830 | // get screen geometry | 
|  | 1831 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 1832 | const uint32_t hw_w = hw.getWidth(); | 
|  | 1833 | const uint32_t hw_h = hw.getHeight(); | 
| Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 1834 | const Region screenBounds(hw.getBounds()); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1835 |  | 
|  | 1836 | GLfloat u, v; | 
|  | 1837 | GLuint tname; | 
| Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1838 | status_t result = renderScreenToTextureLocked(0, &tname, &u, &v); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1839 | if (result != NO_ERROR) { | 
|  | 1840 | return result; | 
|  | 1841 | } | 
|  | 1842 |  | 
|  | 1843 | GLfloat vtx[8]; | 
| Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 1844 | const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} }; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1845 | glBindTexture(GL_TEXTURE_2D, tname); | 
|  | 1846 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); | 
|  | 1847 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
|  | 1848 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
|  | 1849 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); | 
|  | 1850 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|  | 1851 | glVertexPointer(2, GL_FLOAT, 0, vtx); | 
|  | 1852 |  | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1853 | /* | 
|  | 1854 | * Texture coordinate mapping | 
|  | 1855 | * | 
|  | 1856 | *                 u | 
|  | 1857 | *    1 +----------+---+ | 
|  | 1858 | *      |     |    |   |  image is inverted | 
|  | 1859 | *      |     V    |   |  w.r.t. the texture | 
|  | 1860 | *  1-v +----------+   |  coordinates | 
|  | 1861 | *      |              | | 
|  | 1862 | *      |              | | 
|  | 1863 | *      |              | | 
|  | 1864 | *    0 +--------------+ | 
|  | 1865 | *      0              1 | 
|  | 1866 | * | 
|  | 1867 | */ | 
|  | 1868 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1869 | class s_curve_interpolator { | 
|  | 1870 | const float nbFrames, s, v; | 
|  | 1871 | public: | 
|  | 1872 | s_curve_interpolator(int nbFrames, float s) | 
|  | 1873 | : nbFrames(1.0f / (nbFrames-1)), s(s), | 
|  | 1874 | v(1.0f + expf(-s + 0.5f*s)) { | 
|  | 1875 | } | 
|  | 1876 | float operator()(int f) { | 
|  | 1877 | const float x = f * nbFrames; | 
|  | 1878 | return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; | 
|  | 1879 | } | 
|  | 1880 | }; | 
|  | 1881 |  | 
|  | 1882 | class v_stretch { | 
|  | 1883 | const GLfloat hw_w, hw_h; | 
|  | 1884 | public: | 
|  | 1885 | v_stretch(uint32_t hw_w, uint32_t hw_h) | 
|  | 1886 | : hw_w(hw_w), hw_h(hw_h) { | 
|  | 1887 | } | 
|  | 1888 | void operator()(GLfloat* vtx, float v) { | 
|  | 1889 | const GLfloat w = hw_w + (hw_w * v); | 
|  | 1890 | const GLfloat h = hw_h - (hw_h * v); | 
|  | 1891 | const GLfloat x = (hw_w - w) * 0.5f; | 
|  | 1892 | const GLfloat y = (hw_h - h) * 0.5f; | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1893 | vtx[0] = x;         vtx[1] = y; | 
|  | 1894 | vtx[2] = x;         vtx[3] = y + h; | 
|  | 1895 | vtx[4] = x + w;     vtx[5] = y + h; | 
|  | 1896 | vtx[6] = x + w;     vtx[7] = y; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1897 | } | 
|  | 1898 | }; | 
|  | 1899 |  | 
|  | 1900 | class h_stretch { | 
|  | 1901 | const GLfloat hw_w, hw_h; | 
|  | 1902 | public: | 
|  | 1903 | h_stretch(uint32_t hw_w, uint32_t hw_h) | 
|  | 1904 | : hw_w(hw_w), hw_h(hw_h) { | 
|  | 1905 | } | 
|  | 1906 | void operator()(GLfloat* vtx, float v) { | 
|  | 1907 | const GLfloat w = hw_w - (hw_w * v); | 
|  | 1908 | const GLfloat h = 1.0f; | 
|  | 1909 | const GLfloat x = (hw_w - w) * 0.5f; | 
|  | 1910 | const GLfloat y = (hw_h - h) * 0.5f; | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1911 | vtx[0] = x;         vtx[1] = y; | 
|  | 1912 | vtx[2] = x;         vtx[3] = y + h; | 
|  | 1913 | vtx[4] = x + w;     vtx[5] = y + h; | 
|  | 1914 | vtx[6] = x + w;     vtx[7] = y; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1915 | } | 
|  | 1916 | }; | 
|  | 1917 |  | 
|  | 1918 | // the full animation is 24 frames | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 1919 | char value[PROPERTY_VALUE_MAX]; | 
|  | 1920 | property_get("debug.sf.electron_frames", value, "24"); | 
|  | 1921 | int nbFrames = (atoi(value) + 1) >> 1; | 
|  | 1922 | if (nbFrames <= 0) // just in case | 
|  | 1923 | nbFrames = 24; | 
|  | 1924 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1925 | s_curve_interpolator itr(nbFrames, 7.5f); | 
|  | 1926 | s_curve_interpolator itg(nbFrames, 8.0f); | 
|  | 1927 | s_curve_interpolator itb(nbFrames, 8.5f); | 
|  | 1928 |  | 
|  | 1929 | v_stretch vverts(hw_w, hw_h); | 
| Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 1930 |  | 
|  | 1931 | glMatrixMode(GL_TEXTURE); | 
|  | 1932 | glLoadIdentity(); | 
|  | 1933 | glMatrixMode(GL_MODELVIEW); | 
|  | 1934 | glLoadIdentity(); | 
|  | 1935 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1936 | glEnable(GL_BLEND); | 
|  | 1937 | glBlendFunc(GL_ONE, GL_ONE); | 
|  | 1938 | for (int i=0 ; i<nbFrames ; i++) { | 
|  | 1939 | float x, y, w, h; | 
|  | 1940 | const float vr = itr(i); | 
|  | 1941 | const float vg = itg(i); | 
|  | 1942 | const float vb = itb(i); | 
|  | 1943 |  | 
|  | 1944 | // clear screen | 
|  | 1945 | glColorMask(1,1,1,1); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1946 | glClear(GL_COLOR_BUFFER_BIT); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1947 | glEnable(GL_TEXTURE_2D); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1948 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1949 | // draw the red plane | 
|  | 1950 | vverts(vtx, vr); | 
|  | 1951 | glColorMask(1,0,0,1); | 
|  | 1952 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1953 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1954 | // draw the green plane | 
|  | 1955 | vverts(vtx, vg); | 
|  | 1956 | glColorMask(0,1,0,1); | 
|  | 1957 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1958 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1959 | // draw the blue plane | 
|  | 1960 | vverts(vtx, vb); | 
|  | 1961 | glColorMask(0,0,1,1); | 
|  | 1962 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1963 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1964 | // draw the white highlight (we use the last vertices) | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1965 | glDisable(GL_TEXTURE_2D); | 
|  | 1966 | glColorMask(1,1,1,1); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1967 | glColor4f(vg, vg, vg, 1); | 
|  | 1968 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 1969 | hw.flip(screenBounds); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1970 | } | 
|  | 1971 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1972 | h_stretch hverts(hw_w, hw_h); | 
|  | 1973 | glDisable(GL_BLEND); | 
|  | 1974 | glDisable(GL_TEXTURE_2D); | 
|  | 1975 | glColorMask(1,1,1,1); | 
|  | 1976 | for (int i=0 ; i<nbFrames ; i++) { | 
|  | 1977 | const float v = itg(i); | 
|  | 1978 | hverts(vtx, v); | 
|  | 1979 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 1980 | glColor4f(1-v, 1-v, 1-v, 1); | 
|  | 1981 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 1982 | hw.flip(screenBounds); | 
|  | 1983 | } | 
|  | 1984 |  | 
|  | 1985 | glColorMask(1,1,1,1); | 
|  | 1986 | glEnable(GL_SCISSOR_TEST); | 
|  | 1987 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); | 
|  | 1988 | glDeleteTextures(1, &tname); | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1989 | glDisable(GL_TEXTURE_2D); | 
| Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 1990 | glDisable(GL_BLEND); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1991 | return NO_ERROR; | 
|  | 1992 | } | 
|  | 1993 |  | 
|  | 1994 | status_t SurfaceFlinger::electronBeamOnAnimationImplLocked() | 
|  | 1995 | { | 
|  | 1996 | status_t result = PERMISSION_DENIED; | 
|  | 1997 |  | 
|  | 1998 | if (!GLExtensions::getInstance().haveFramebufferObject()) | 
|  | 1999 | return INVALID_OPERATION; | 
|  | 2000 |  | 
|  | 2001 |  | 
|  | 2002 | // get screen geometry | 
|  | 2003 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); | 
|  | 2004 | const uint32_t hw_w = hw.getWidth(); | 
|  | 2005 | const uint32_t hw_h = hw.getHeight(); | 
|  | 2006 | const Region screenBounds(hw.bounds()); | 
|  | 2007 |  | 
|  | 2008 | GLfloat u, v; | 
|  | 2009 | GLuint tname; | 
|  | 2010 | result = renderScreenToTextureLocked(0, &tname, &u, &v); | 
|  | 2011 | if (result != NO_ERROR) { | 
|  | 2012 | return result; | 
|  | 2013 | } | 
|  | 2014 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2015 | GLfloat vtx[8]; | 
|  | 2016 | const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} }; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2017 | glBindTexture(GL_TEXTURE_2D, tname); | 
|  | 2018 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | 
|  | 2019 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
|  | 2020 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
|  | 2021 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); | 
|  | 2022 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|  | 2023 | glVertexPointer(2, GL_FLOAT, 0, vtx); | 
|  | 2024 |  | 
|  | 2025 | class s_curve_interpolator { | 
|  | 2026 | const float nbFrames, s, v; | 
|  | 2027 | public: | 
|  | 2028 | s_curve_interpolator(int nbFrames, float s) | 
|  | 2029 | : nbFrames(1.0f / (nbFrames-1)), s(s), | 
|  | 2030 | v(1.0f + expf(-s + 0.5f*s)) { | 
|  | 2031 | } | 
|  | 2032 | float operator()(int f) { | 
|  | 2033 | const float x = f * nbFrames; | 
|  | 2034 | return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; | 
|  | 2035 | } | 
|  | 2036 | }; | 
|  | 2037 |  | 
|  | 2038 | class v_stretch { | 
|  | 2039 | const GLfloat hw_w, hw_h; | 
|  | 2040 | public: | 
|  | 2041 | v_stretch(uint32_t hw_w, uint32_t hw_h) | 
|  | 2042 | : hw_w(hw_w), hw_h(hw_h) { | 
|  | 2043 | } | 
|  | 2044 | void operator()(GLfloat* vtx, float v) { | 
|  | 2045 | const GLfloat w = hw_w + (hw_w * v); | 
|  | 2046 | const GLfloat h = hw_h - (hw_h * v); | 
|  | 2047 | const GLfloat x = (hw_w - w) * 0.5f; | 
|  | 2048 | const GLfloat y = (hw_h - h) * 0.5f; | 
|  | 2049 | vtx[0] = x;         vtx[1] = y; | 
|  | 2050 | vtx[2] = x;         vtx[3] = y + h; | 
|  | 2051 | vtx[4] = x + w;     vtx[5] = y + h; | 
|  | 2052 | vtx[6] = x + w;     vtx[7] = y; | 
|  | 2053 | } | 
|  | 2054 | }; | 
|  | 2055 |  | 
|  | 2056 | class h_stretch { | 
|  | 2057 | const GLfloat hw_w, hw_h; | 
|  | 2058 | public: | 
|  | 2059 | h_stretch(uint32_t hw_w, uint32_t hw_h) | 
|  | 2060 | : hw_w(hw_w), hw_h(hw_h) { | 
|  | 2061 | } | 
|  | 2062 | void operator()(GLfloat* vtx, float v) { | 
|  | 2063 | const GLfloat w = hw_w - (hw_w * v); | 
|  | 2064 | const GLfloat h = 1.0f; | 
|  | 2065 | const GLfloat x = (hw_w - w) * 0.5f; | 
|  | 2066 | const GLfloat y = (hw_h - h) * 0.5f; | 
|  | 2067 | vtx[0] = x;         vtx[1] = y; | 
|  | 2068 | vtx[2] = x;         vtx[3] = y + h; | 
|  | 2069 | vtx[4] = x + w;     vtx[5] = y + h; | 
|  | 2070 | vtx[6] = x + w;     vtx[7] = y; | 
|  | 2071 | } | 
|  | 2072 | }; | 
|  | 2073 |  | 
| Mathias Agopian | a6546e5 | 2010-10-14 12:33:07 -0700 | [diff] [blame] | 2074 | // the full animation is 12 frames | 
|  | 2075 | int nbFrames = 8; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2076 | s_curve_interpolator itr(nbFrames, 7.5f); | 
|  | 2077 | s_curve_interpolator itg(nbFrames, 8.0f); | 
|  | 2078 | s_curve_interpolator itb(nbFrames, 8.5f); | 
|  | 2079 |  | 
|  | 2080 | h_stretch hverts(hw_w, hw_h); | 
|  | 2081 | glDisable(GL_BLEND); | 
|  | 2082 | glDisable(GL_TEXTURE_2D); | 
|  | 2083 | glColorMask(1,1,1,1); | 
|  | 2084 | for (int i=nbFrames-1 ; i>=0 ; i--) { | 
|  | 2085 | const float v = itg(i); | 
|  | 2086 | hverts(vtx, v); | 
|  | 2087 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 2088 | glColor4f(1-v, 1-v, 1-v, 1); | 
|  | 2089 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 2090 | hw.flip(screenBounds); | 
|  | 2091 | } | 
|  | 2092 |  | 
| Mathias Agopian | a6546e5 | 2010-10-14 12:33:07 -0700 | [diff] [blame] | 2093 | nbFrames = 4; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2094 | v_stretch vverts(hw_w, hw_h); | 
|  | 2095 | glEnable(GL_BLEND); | 
|  | 2096 | glBlendFunc(GL_ONE, GL_ONE); | 
|  | 2097 | for (int i=nbFrames-1 ; i>=0 ; i--) { | 
|  | 2098 | float x, y, w, h; | 
|  | 2099 | const float vr = itr(i); | 
|  | 2100 | const float vg = itg(i); | 
|  | 2101 | const float vb = itb(i); | 
|  | 2102 |  | 
|  | 2103 | // clear screen | 
|  | 2104 | glColorMask(1,1,1,1); | 
|  | 2105 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 2106 | glEnable(GL_TEXTURE_2D); | 
|  | 2107 |  | 
|  | 2108 | // draw the red plane | 
|  | 2109 | vverts(vtx, vr); | 
|  | 2110 | glColorMask(1,0,0,1); | 
|  | 2111 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 2112 |  | 
|  | 2113 | // draw the green plane | 
|  | 2114 | vverts(vtx, vg); | 
|  | 2115 | glColorMask(0,1,0,1); | 
|  | 2116 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 2117 |  | 
|  | 2118 | // draw the blue plane | 
|  | 2119 | vverts(vtx, vb); | 
|  | 2120 | glColorMask(0,0,1,1); | 
|  | 2121 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 
|  | 2122 |  | 
|  | 2123 | hw.flip(screenBounds); | 
|  | 2124 | } | 
|  | 2125 |  | 
|  | 2126 | glColorMask(1,1,1,1); | 
|  | 2127 | glEnable(GL_SCISSOR_TEST); | 
|  | 2128 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2129 | glDeleteTextures(1, &tname); | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2130 | glDisable(GL_TEXTURE_2D); | 
| Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 2131 | glDisable(GL_BLEND); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2132 |  | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2133 | return NO_ERROR; | 
|  | 2134 | } | 
|  | 2135 |  | 
|  | 2136 | // --------------------------------------------------------------------------- | 
|  | 2137 |  | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2138 | status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode) | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2139 | { | 
|  | 2140 | DisplayHardware& hw(graphicPlane(0).editDisplayHardware()); | 
|  | 2141 | if (!hw.canDraw()) { | 
|  | 2142 | // we're already off | 
|  | 2143 | return NO_ERROR; | 
|  | 2144 | } | 
| Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 2145 |  | 
|  | 2146 | // turn off hwc while we're doing the animation | 
|  | 2147 | hw.getHwComposer().disable(); | 
|  | 2148 | // and make sure to turn it back on (if needed) next time we compose | 
|  | 2149 | invalidateHwcGeometry(); | 
|  | 2150 |  | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2151 | if (mode & ISurfaceComposer::eElectronBeamAnimationOff) { | 
|  | 2152 | electronBeamOffAnimationImplLocked(); | 
|  | 2153 | } | 
|  | 2154 |  | 
|  | 2155 | // always clear the whole screen at the end of the animation | 
|  | 2156 | glClearColor(0,0,0,1); | 
|  | 2157 | glDisable(GL_SCISSOR_TEST); | 
|  | 2158 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 2159 | glEnable(GL_SCISSOR_TEST); | 
|  | 2160 | hw.flip( Region(hw.bounds()) ); | 
|  | 2161 |  | 
| Mathias Agopian | 015fb3f | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2162 | return NO_ERROR; | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2163 | } | 
|  | 2164 |  | 
|  | 2165 | status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode) | 
|  | 2166 | { | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2167 | class MessageTurnElectronBeamOff : public MessageBase { | 
|  | 2168 | SurfaceFlinger* flinger; | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2169 | int32_t mode; | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2170 | status_t result; | 
|  | 2171 | public: | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2172 | MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode) | 
|  | 2173 | : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2174 | } | 
|  | 2175 | status_t getResult() const { | 
|  | 2176 | return result; | 
|  | 2177 | } | 
|  | 2178 | virtual bool handler() { | 
|  | 2179 | Mutex::Autolock _l(flinger->mStateLock); | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2180 | result = flinger->turnElectronBeamOffImplLocked(mode); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2181 | return true; | 
|  | 2182 | } | 
|  | 2183 | }; | 
|  | 2184 |  | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2185 | sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode); | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2186 | status_t res = postMessageSync(msg); | 
|  | 2187 | if (res == NO_ERROR) { | 
|  | 2188 | res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult(); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2189 |  | 
|  | 2190 | // work-around: when the power-manager calls us we activate the | 
|  | 2191 | // animation. eventually, the "on" animation will be called | 
|  | 2192 | // by the power-manager itself | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2193 | mElectronBeamAnimationMode = mode; | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2194 | } | 
|  | 2195 | return res; | 
|  | 2196 | } | 
|  | 2197 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2198 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2199 |  | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2200 | status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode) | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2201 | { | 
|  | 2202 | DisplayHardware& hw(graphicPlane(0).editDisplayHardware()); | 
|  | 2203 | if (hw.canDraw()) { | 
|  | 2204 | // we're already on | 
|  | 2205 | return NO_ERROR; | 
|  | 2206 | } | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2207 | if (mode & ISurfaceComposer::eElectronBeamAnimationOn) { | 
|  | 2208 | electronBeamOnAnimationImplLocked(); | 
|  | 2209 | } | 
| Mathias Agopian | a7f0373 | 2010-10-14 12:46:24 -0700 | [diff] [blame] | 2210 |  | 
|  | 2211 | // make sure to redraw the whole screen when the animation is done | 
|  | 2212 | mDirtyRegion.set(hw.bounds()); | 
|  | 2213 | signalEvent(); | 
|  | 2214 |  | 
| Mathias Agopian | 015fb3f | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2215 | return NO_ERROR; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2216 | } | 
|  | 2217 |  | 
|  | 2218 | status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode) | 
|  | 2219 | { | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2220 | class MessageTurnElectronBeamOn : public MessageBase { | 
|  | 2221 | SurfaceFlinger* flinger; | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2222 | int32_t mode; | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2223 | status_t result; | 
|  | 2224 | public: | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2225 | MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode) | 
|  | 2226 | : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2227 | } | 
|  | 2228 | status_t getResult() const { | 
|  | 2229 | return result; | 
|  | 2230 | } | 
|  | 2231 | virtual bool handler() { | 
|  | 2232 | Mutex::Autolock _l(flinger->mStateLock); | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2233 | result = flinger->turnElectronBeamOnImplLocked(mode); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2234 | return true; | 
|  | 2235 | } | 
|  | 2236 | }; | 
|  | 2237 |  | 
| Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2238 | postMessageAsync( new MessageTurnElectronBeamOn(this, mode) ); | 
| Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2239 | return NO_ERROR; | 
|  | 2240 | } | 
|  | 2241 |  | 
|  | 2242 | // --------------------------------------------------------------------------- | 
|  | 2243 |  | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2244 | status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy, | 
|  | 2245 | sp<IMemoryHeap>* heap, | 
|  | 2246 | uint32_t* w, uint32_t* h, PixelFormat* f, | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2247 | uint32_t sw, uint32_t sh, | 
|  | 2248 | uint32_t minLayerZ, uint32_t maxLayerZ) | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2249 | { | 
|  | 2250 | status_t result = PERMISSION_DENIED; | 
|  | 2251 |  | 
|  | 2252 | // only one display supported for now | 
|  | 2253 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) | 
|  | 2254 | return BAD_VALUE; | 
|  | 2255 |  | 
|  | 2256 | if (!GLExtensions::getInstance().haveFramebufferObject()) | 
|  | 2257 | return INVALID_OPERATION; | 
|  | 2258 |  | 
|  | 2259 | // get screen geometry | 
|  | 2260 | const DisplayHardware& hw(graphicPlane(dpy).displayHardware()); | 
|  | 2261 | const uint32_t hw_w = hw.getWidth(); | 
|  | 2262 | const uint32_t hw_h = hw.getHeight(); | 
|  | 2263 |  | 
|  | 2264 | if ((sw > hw_w) || (sh > hw_h)) | 
|  | 2265 | return BAD_VALUE; | 
|  | 2266 |  | 
|  | 2267 | sw = (!sw) ? hw_w : sw; | 
|  | 2268 | sh = (!sh) ? hw_h : sh; | 
|  | 2269 | const size_t size = sw * sh * 4; | 
|  | 2270 |  | 
| Mathias Agopian | 1c71a47 | 2011-03-02 18:45:50 -0800 | [diff] [blame] | 2271 | //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d", | 
|  | 2272 | //        sw, sh, minLayerZ, maxLayerZ); | 
| Mathias Agopian | c1d1b0d | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2273 |  | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2274 | // make sure to clear all GL error flags | 
|  | 2275 | while ( glGetError() != GL_NO_ERROR ) ; | 
|  | 2276 |  | 
|  | 2277 | // create a FBO | 
|  | 2278 | GLuint name, tname; | 
|  | 2279 | glGenRenderbuffersOES(1, &tname); | 
|  | 2280 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname); | 
|  | 2281 | glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh); | 
|  | 2282 | glGenFramebuffersOES(1, &name); | 
|  | 2283 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); | 
|  | 2284 | glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, | 
|  | 2285 | GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname); | 
|  | 2286 |  | 
|  | 2287 | GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); | 
| Mathias Agopian | c1d1b0d | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2288 |  | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2289 | if (status == GL_FRAMEBUFFER_COMPLETE_OES) { | 
|  | 2290 |  | 
|  | 2291 | // invert everything, b/c glReadPixel() below will invert the FB | 
|  | 2292 | glViewport(0, 0, sw, sh); | 
| Mathias Agopian | f653b89 | 2010-12-16 18:46:17 -0800 | [diff] [blame] | 2293 | glScissor(0, 0, sw, sh); | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2294 | glEnable(GL_SCISSOR_TEST); | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2295 | glMatrixMode(GL_PROJECTION); | 
|  | 2296 | glPushMatrix(); | 
|  | 2297 | glLoadIdentity(); | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2298 | glOrthof(0, hw_w, hw_h, 0, 0, 1); | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2299 | glMatrixMode(GL_MODELVIEW); | 
|  | 2300 |  | 
|  | 2301 | // redraw the screen entirely... | 
|  | 2302 | glClearColor(0,0,0,1); | 
|  | 2303 | glClear(GL_COLOR_BUFFER_BIT); | 
| Mathias Agopian | f653b89 | 2010-12-16 18:46:17 -0800 | [diff] [blame] | 2304 |  | 
| Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 2305 | const LayerVector& layers(mDrawingState.layersSortedByZ); | 
|  | 2306 | const size_t count = layers.size(); | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2307 | for (size_t i=0 ; i<count ; ++i) { | 
|  | 2308 | const sp<LayerBase>& layer(layers[i]); | 
| Mathias Agopian | b061033 | 2011-08-25 14:36:43 -0700 | [diff] [blame] | 2309 | const uint32_t flags = layer->drawingState().flags; | 
|  | 2310 | if (!(flags & ISurfaceComposer::eLayerHidden)) { | 
|  | 2311 | const uint32_t z = layer->drawingState().z; | 
|  | 2312 | if (z >= minLayerZ && z <= maxLayerZ) { | 
|  | 2313 | layer->drawForSreenShot(); | 
|  | 2314 | } | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2315 | } | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2316 | } | 
|  | 2317 |  | 
|  | 2318 | // XXX: this is needed on tegra | 
| Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2319 | glEnable(GL_SCISSOR_TEST); | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2320 | glScissor(0, 0, sw, sh); | 
|  | 2321 |  | 
|  | 2322 | // check for errors and return screen capture | 
|  | 2323 | if (glGetError() != GL_NO_ERROR) { | 
|  | 2324 | // error while rendering | 
|  | 2325 | result = INVALID_OPERATION; | 
|  | 2326 | } else { | 
|  | 2327 | // allocate shared memory large enough to hold the | 
|  | 2328 | // screen capture | 
|  | 2329 | sp<MemoryHeapBase> base( | 
|  | 2330 | new MemoryHeapBase(size, 0, "screen-capture") ); | 
|  | 2331 | void* const ptr = base->getBase(); | 
|  | 2332 | if (ptr) { | 
|  | 2333 | // capture the screen with glReadPixels() | 
|  | 2334 | glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr); | 
|  | 2335 | if (glGetError() == GL_NO_ERROR) { | 
|  | 2336 | *heap = base; | 
|  | 2337 | *w = sw; | 
|  | 2338 | *h = sh; | 
|  | 2339 | *f = PIXEL_FORMAT_RGBA_8888; | 
|  | 2340 | result = NO_ERROR; | 
|  | 2341 | } | 
|  | 2342 | } else { | 
|  | 2343 | result = NO_MEMORY; | 
|  | 2344 | } | 
|  | 2345 | } | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2346 | glEnable(GL_SCISSOR_TEST); | 
|  | 2347 | glViewport(0, 0, hw_w, hw_h); | 
|  | 2348 | glMatrixMode(GL_PROJECTION); | 
|  | 2349 | glPopMatrix(); | 
|  | 2350 | glMatrixMode(GL_MODELVIEW); | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2351 | } else { | 
|  | 2352 | result = BAD_VALUE; | 
|  | 2353 | } | 
|  | 2354 |  | 
|  | 2355 | // release FBO resources | 
|  | 2356 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); | 
|  | 2357 | glDeleteRenderbuffersOES(1, &tname); | 
|  | 2358 | glDeleteFramebuffersOES(1, &name); | 
| Mathias Agopian | e6f0984 | 2010-12-15 14:41:59 -0800 | [diff] [blame] | 2359 |  | 
|  | 2360 | hw.compositionComplete(); | 
|  | 2361 |  | 
| Mathias Agopian | 1c71a47 | 2011-03-02 18:45:50 -0800 | [diff] [blame] | 2362 | // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK"); | 
| Mathias Agopian | c1d1b0d | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2363 |  | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2364 | return result; | 
|  | 2365 | } | 
|  | 2366 |  | 
|  | 2367 |  | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2368 | status_t SurfaceFlinger::captureScreen(DisplayID dpy, | 
|  | 2369 | sp<IMemoryHeap>* heap, | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2370 | uint32_t* width, uint32_t* height, PixelFormat* format, | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2371 | uint32_t sw, uint32_t sh, | 
|  | 2372 | uint32_t minLayerZ, uint32_t maxLayerZ) | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2373 | { | 
|  | 2374 | // only one display supported for now | 
|  | 2375 | if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) | 
|  | 2376 | return BAD_VALUE; | 
|  | 2377 |  | 
|  | 2378 | if (!GLExtensions::getInstance().haveFramebufferObject()) | 
|  | 2379 | return INVALID_OPERATION; | 
|  | 2380 |  | 
|  | 2381 | class MessageCaptureScreen : public MessageBase { | 
|  | 2382 | SurfaceFlinger* flinger; | 
|  | 2383 | DisplayID dpy; | 
|  | 2384 | sp<IMemoryHeap>* heap; | 
|  | 2385 | uint32_t* w; | 
|  | 2386 | uint32_t* h; | 
|  | 2387 | PixelFormat* f; | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2388 | uint32_t sw; | 
|  | 2389 | uint32_t sh; | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2390 | uint32_t minLayerZ; | 
|  | 2391 | uint32_t maxLayerZ; | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2392 | status_t result; | 
|  | 2393 | public: | 
|  | 2394 | MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy, | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2395 | sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f, | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2396 | uint32_t sw, uint32_t sh, | 
|  | 2397 | uint32_t minLayerZ, uint32_t maxLayerZ) | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2398 | : flinger(flinger), dpy(dpy), | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2399 | heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), | 
|  | 2400 | minLayerZ(minLayerZ), maxLayerZ(maxLayerZ), | 
|  | 2401 | result(PERMISSION_DENIED) | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2402 | { | 
|  | 2403 | } | 
|  | 2404 | status_t getResult() const { | 
|  | 2405 | return result; | 
|  | 2406 | } | 
|  | 2407 | virtual bool handler() { | 
|  | 2408 | Mutex::Autolock _l(flinger->mStateLock); | 
|  | 2409 |  | 
|  | 2410 | // if we have secure windows, never allow the screen capture | 
|  | 2411 | if (flinger->mSecureFrameBuffer) | 
|  | 2412 | return true; | 
|  | 2413 |  | 
| Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2414 | result = flinger->captureScreenImplLocked(dpy, | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2415 | heap, w, h, f, sw, sh, minLayerZ, maxLayerZ); | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2416 |  | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2417 | return true; | 
|  | 2418 | } | 
|  | 2419 | }; | 
|  | 2420 |  | 
|  | 2421 | sp<MessageBase> msg = new MessageCaptureScreen(this, | 
| Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2422 | dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ); | 
| Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2423 | status_t res = postMessageSync(msg); | 
|  | 2424 | if (res == NO_ERROR) { | 
|  | 2425 | res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult(); | 
|  | 2426 | } | 
|  | 2427 | return res; | 
|  | 2428 | } | 
|  | 2429 |  | 
|  | 2430 | // --------------------------------------------------------------------------- | 
|  | 2431 |  | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2432 | sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const | 
|  | 2433 | { | 
|  | 2434 | sp<Layer> result; | 
|  | 2435 | Mutex::Autolock _l(mStateLock); | 
|  | 2436 | result = mLayerMap.valueFor( sur->asBinder() ).promote(); | 
|  | 2437 | return result; | 
|  | 2438 | } | 
|  | 2439 |  | 
|  | 2440 | // --------------------------------------------------------------------------- | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2441 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2442 | Client::Client(const sp<SurfaceFlinger>& flinger) | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2443 | : mFlinger(flinger), mNameGenerator(1) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2444 | { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2445 | } | 
|  | 2446 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2447 | Client::~Client() | 
|  | 2448 | { | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2449 | const size_t count = mLayers.size(); | 
|  | 2450 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2451 | sp<LayerBaseClient> layer(mLayers.valueAt(i).promote()); | 
|  | 2452 | if (layer != 0) { | 
|  | 2453 | mFlinger->removeLayer(layer); | 
|  | 2454 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2455 | } | 
|  | 2456 | } | 
|  | 2457 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2458 | status_t Client::initCheck() const { | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2459 | return NO_ERROR; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2460 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2461 |  | 
| Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2462 | size_t Client::attachLayer(const sp<LayerBaseClient>& layer) | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2463 | { | 
| Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2464 | Mutex::Autolock _l(mLock); | 
|  | 2465 | size_t name = mNameGenerator++; | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2466 | mLayers.add(name, layer); | 
|  | 2467 | return name; | 
|  | 2468 | } | 
|  | 2469 |  | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2470 | void Client::detachLayer(const LayerBaseClient* layer) | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2471 | { | 
| Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2472 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2473 | // we do a linear search here, because this doesn't happen often | 
|  | 2474 | const size_t count = mLayers.size(); | 
|  | 2475 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2476 | if (mLayers.valueAt(i) == layer) { | 
|  | 2477 | mLayers.removeItemsAt(i, 1); | 
|  | 2478 | break; | 
|  | 2479 | } | 
|  | 2480 | } | 
|  | 2481 | } | 
| Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2482 | sp<LayerBaseClient> Client::getLayerUser(int32_t i) const | 
|  | 2483 | { | 
|  | 2484 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2485 | sp<LayerBaseClient> lbc; | 
| Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 2486 | wp<LayerBaseClient> layer(mLayers.valueFor(i)); | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2487 | if (layer != 0) { | 
|  | 2488 | lbc = layer.promote(); | 
|  | 2489 | LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i)); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2490 | } | 
|  | 2491 | return lbc; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2492 | } | 
|  | 2493 |  | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2494 |  | 
|  | 2495 | status_t Client::onTransact( | 
|  | 2496 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 2497 | { | 
|  | 2498 | // these must be checked | 
|  | 2499 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 2500 | const int pid = ipc->getCallingPid(); | 
|  | 2501 | const int uid = ipc->getCallingUid(); | 
|  | 2502 | const int self_pid = getpid(); | 
|  | 2503 | if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) { | 
|  | 2504 | // we're called from a different process, do the real check | 
| Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 2505 | if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger)) | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2506 | { | 
|  | 2507 | LOGE("Permission Denial: " | 
|  | 2508 | "can't openGlobalTransaction pid=%d, uid=%d", pid, uid); | 
|  | 2509 | return PERMISSION_DENIED; | 
|  | 2510 | } | 
|  | 2511 | } | 
|  | 2512 | return BnSurfaceComposerClient::onTransact(code, data, reply, flags); | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2513 | } | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2514 |  | 
|  | 2515 |  | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2516 | sp<ISurface> Client::createSurface( | 
| Mathias Agopian | 0ef4e15 | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 2517 | ISurfaceComposerClient::surface_data_t* params, | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2518 | const String8& name, | 
|  | 2519 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2520 | uint32_t flags) | 
|  | 2521 | { | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2522 | /* | 
|  | 2523 | * createSurface must be called from the GL thread so that it can | 
|  | 2524 | * have access to the GL context. | 
|  | 2525 | */ | 
|  | 2526 |  | 
|  | 2527 | class MessageCreateSurface : public MessageBase { | 
|  | 2528 | sp<ISurface> result; | 
|  | 2529 | SurfaceFlinger* flinger; | 
|  | 2530 | ISurfaceComposerClient::surface_data_t* params; | 
|  | 2531 | Client* client; | 
|  | 2532 | const String8& name; | 
|  | 2533 | DisplayID display; | 
|  | 2534 | uint32_t w, h; | 
|  | 2535 | PixelFormat format; | 
|  | 2536 | uint32_t flags; | 
|  | 2537 | public: | 
|  | 2538 | MessageCreateSurface(SurfaceFlinger* flinger, | 
|  | 2539 | ISurfaceComposerClient::surface_data_t* params, | 
|  | 2540 | const String8& name, Client* client, | 
|  | 2541 | DisplayID display, uint32_t w, uint32_t h, PixelFormat format, | 
|  | 2542 | uint32_t flags) | 
|  | 2543 | : flinger(flinger), params(params), client(client), name(name), | 
|  | 2544 | display(display), w(w), h(h), format(format), flags(flags) | 
|  | 2545 | { | 
|  | 2546 | } | 
|  | 2547 | sp<ISurface> getResult() const { return result; } | 
|  | 2548 | virtual bool handler() { | 
|  | 2549 | result = flinger->createSurface(params, name, client, | 
|  | 2550 | display, w, h, format, flags); | 
|  | 2551 | return true; | 
|  | 2552 | } | 
|  | 2553 | }; | 
|  | 2554 |  | 
|  | 2555 | sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(), | 
|  | 2556 | params, name, this, display, w, h, format, flags); | 
|  | 2557 | mFlinger->postMessageSync(msg); | 
|  | 2558 | return static_cast<MessageCreateSurface*>( msg.get() )->getResult(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | } | 
| Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 2560 | status_t Client::destroySurface(SurfaceID sid) { | 
|  | 2561 | return mFlinger->removeSurface(this, sid); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2562 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2563 |  | 
|  | 2564 | // --------------------------------------------------------------------------- | 
|  | 2565 |  | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2566 | GraphicBufferAlloc::GraphicBufferAlloc() {} | 
|  | 2567 |  | 
|  | 2568 | GraphicBufferAlloc::~GraphicBufferAlloc() {} | 
|  | 2569 |  | 
|  | 2570 | sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h, | 
| Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2571 | PixelFormat format, uint32_t usage, status_t* error) { | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2572 | sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage)); | 
|  | 2573 | status_t err = graphicBuffer->initCheck(); | 
| Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2574 | *error = err; | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2575 | if (err != 0 || graphicBuffer->handle == 0) { | 
| Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2576 | if (err == NO_MEMORY) { | 
|  | 2577 | GraphicBuffer::dumpAllocationsToSystemLog(); | 
|  | 2578 | } | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2579 | LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) " | 
|  | 2580 | "failed (%s), handle=%p", | 
|  | 2581 | w, h, strerror(-err), graphicBuffer->handle); | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2582 | return 0; | 
|  | 2583 | } | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2584 | return graphicBuffer; | 
|  | 2585 | } | 
|  | 2586 |  | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2587 | // --------------------------------------------------------------------------- | 
|  | 2588 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2589 | GraphicPlane::GraphicPlane() | 
|  | 2590 | : mHw(0) | 
|  | 2591 | { | 
|  | 2592 | } | 
|  | 2593 |  | 
|  | 2594 | GraphicPlane::~GraphicPlane() { | 
|  | 2595 | delete mHw; | 
|  | 2596 | } | 
|  | 2597 |  | 
|  | 2598 | bool GraphicPlane::initialized() const { | 
|  | 2599 | return mHw ? true : false; | 
|  | 2600 | } | 
|  | 2601 |  | 
| Mathias Agopian | 2b92d89 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2602 | int GraphicPlane::getWidth() const { | 
|  | 2603 | return mWidth; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | } | 
|  | 2605 |  | 
| Mathias Agopian | 2b92d89 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2606 | int GraphicPlane::getHeight() const { | 
|  | 2607 | return mHeight; | 
|  | 2608 | } | 
|  | 2609 |  | 
|  | 2610 | void GraphicPlane::setDisplayHardware(DisplayHardware *hw) | 
|  | 2611 | { | 
|  | 2612 | mHw = hw; | 
|  | 2613 |  | 
|  | 2614 | // initialize the display orientation transform. | 
|  | 2615 | // it's a constant that should come from the display driver. | 
|  | 2616 | int displayOrientation = ISurfaceComposer::eOrientationDefault; | 
|  | 2617 | char property[PROPERTY_VALUE_MAX]; | 
|  | 2618 | if (property_get("ro.sf.hwrotation", property, NULL) > 0) { | 
|  | 2619 | //displayOrientation | 
|  | 2620 | switch (atoi(property)) { | 
|  | 2621 | case 90: | 
|  | 2622 | displayOrientation = ISurfaceComposer::eOrientation90; | 
|  | 2623 | break; | 
|  | 2624 | case 270: | 
|  | 2625 | displayOrientation = ISurfaceComposer::eOrientation270; | 
|  | 2626 | break; | 
|  | 2627 | } | 
|  | 2628 | } | 
|  | 2629 |  | 
|  | 2630 | const float w = hw->getWidth(); | 
|  | 2631 | const float h = hw->getHeight(); | 
|  | 2632 | GraphicPlane::orientationToTransfrom(displayOrientation, w, h, | 
|  | 2633 | &mDisplayTransform); | 
|  | 2634 | if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) { | 
|  | 2635 | mDisplayWidth = h; | 
|  | 2636 | mDisplayHeight = w; | 
|  | 2637 | } else { | 
|  | 2638 | mDisplayWidth = w; | 
|  | 2639 | mDisplayHeight = h; | 
|  | 2640 | } | 
|  | 2641 |  | 
|  | 2642 | setOrientation(ISurfaceComposer::eOrientationDefault); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2643 | } | 
|  | 2644 |  | 
|  | 2645 | status_t GraphicPlane::orientationToTransfrom( | 
|  | 2646 | int orientation, int w, int h, Transform* tr) | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2647 | { | 
|  | 2648 | uint32_t flags = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2649 | switch (orientation) { | 
|  | 2650 | case ISurfaceComposer::eOrientationDefault: | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2651 | flags = Transform::ROT_0; | 
|  | 2652 | break; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2653 | case ISurfaceComposer::eOrientation90: | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2654 | flags = Transform::ROT_90; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2655 | break; | 
|  | 2656 | case ISurfaceComposer::eOrientation180: | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2657 | flags = Transform::ROT_180; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2658 | break; | 
|  | 2659 | case ISurfaceComposer::eOrientation270: | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2660 | flags = Transform::ROT_270; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | break; | 
|  | 2662 | default: | 
|  | 2663 | return BAD_VALUE; | 
|  | 2664 | } | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2665 | tr->set(flags, w, h); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2666 | return NO_ERROR; | 
|  | 2667 | } | 
|  | 2668 |  | 
|  | 2669 | status_t GraphicPlane::setOrientation(int orientation) | 
|  | 2670 | { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2671 | // If the rotation can be handled in hardware, this is where | 
|  | 2672 | // the magic should happen. | 
| Mathias Agopian | 2b92d89 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2673 |  | 
|  | 2674 | const DisplayHardware& hw(displayHardware()); | 
|  | 2675 | const float w = mDisplayWidth; | 
|  | 2676 | const float h = mDisplayHeight; | 
|  | 2677 | mWidth = int(w); | 
|  | 2678 | mHeight = int(h); | 
|  | 2679 |  | 
|  | 2680 | Transform orientationTransform; | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2681 | GraphicPlane::orientationToTransfrom(orientation, w, h, | 
|  | 2682 | &orientationTransform); | 
|  | 2683 | if (orientation & ISurfaceComposer::eOrientationSwapMask) { | 
|  | 2684 | mWidth = int(h); | 
|  | 2685 | mHeight = int(w); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2686 | } | 
| Mathias Agopian | eda6540 | 2010-02-22 03:15:57 -0800 | [diff] [blame] | 2687 |  | 
| Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 2688 | mOrientation = orientation; | 
| Mathias Agopian | 2b92d89 | 2010-02-08 15:49:35 -0800 | [diff] [blame] | 2689 | mGlobalTransform = mDisplayTransform * orientationTransform; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2690 | return NO_ERROR; | 
|  | 2691 | } | 
|  | 2692 |  | 
|  | 2693 | const DisplayHardware& GraphicPlane::displayHardware() const { | 
|  | 2694 | return *mHw; | 
|  | 2695 | } | 
|  | 2696 |  | 
| Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2697 | DisplayHardware& GraphicPlane::editDisplayHardware() { | 
|  | 2698 | return *mHw; | 
|  | 2699 | } | 
|  | 2700 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2701 | const Transform& GraphicPlane::transform() const { | 
|  | 2702 | return mGlobalTransform; | 
|  | 2703 | } | 
|  | 2704 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2705 | EGLDisplay GraphicPlane::getEGLDisplay() const { | 
|  | 2706 | return mHw->getEGLDisplay(); | 
|  | 2707 | } | 
|  | 2708 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2709 | // --------------------------------------------------------------------------- | 
|  | 2710 |  | 
|  | 2711 | }; // namespace android |