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