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