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