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