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