blob: 9f007b314d40310673470b2ce6681ff0edef0662 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
50#include "LayerBlur.h"
51#include "LayerBuffer.h"
52#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopiana1ecca92009-05-21 19:21:59 -070057/* ideally AID_GRAPHICS would be in a semi-public header
58 * or there would be a way to map a user/group name to its id
59 */
60#ifndef AID_GRAPHICS
61#define AID_GRAPHICS 1003
62#endif
63
Mathias Agopian22c67842010-11-01 23:32:18 -070064#ifdef USE_COMPOSITION_BYPASS
65#warning "using COMPOSITION_BYPASS"
66#endif
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073SurfaceFlinger::SurfaceFlinger()
74 : BnSurfaceComposer(), Thread(false),
75 mTransactionFlags(0),
76 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070077 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070080 mHardwareTest("android.permission.HARDWARE_TEST"),
81 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070082 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070083 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mVisibleRegionsDirty(false),
85 mDeferReleaseConsole(false),
86 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070087 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070089 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070092 mDebugInSwapBuffers(0),
93 mLastSwapBufferTime(0),
94 mDebugInTransaction(0),
95 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070096 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 mConsoleSignals(0),
98 mSecureFrameBuffer(0)
99{
100 init();
101}
102
103void SurfaceFlinger::init()
104{
105 LOGI("SurfaceFlinger is starting");
106
107 // debugging stuff...
108 char value[PROPERTY_VALUE_MAX];
109 property_get("debug.sf.showupdates", value, "0");
110 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showbackground", value, "0");
112 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113
Mathias Agopian78fd5012010-04-20 14:51:04 -0700114 LOGI_IF(mDebugRegion, "showupdates enabled");
115 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
118SurfaceFlinger::~SurfaceFlinger()
119{
120 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
124{
125 return graphicPlane(0).displayHardware().getOverlayEngine();
126}
127
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700128sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700130 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131}
132
Mathias Agopian7e27f052010-05-28 14:22:23 -0700133sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
Mathias Agopian96f08192010-06-02 23:28:45 -0700135 sp<ISurfaceComposerClient> bclient;
136 sp<Client> client(new Client(this));
137 status_t err = client->initCheck();
138 if (err == NO_ERROR) {
139 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 return bclient;
142}
143
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700144sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
145{
146 sp<ISurfaceComposerClient> bclient;
147 sp<UserClient> client(new UserClient(this));
148 status_t err = client->initCheck();
149 if (err == NO_ERROR) {
150 bclient = client;
151 }
152 return bclient;
153}
154
155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
157{
158 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
159 const GraphicPlane& plane(mGraphicPlanes[dpy]);
160 return plane;
161}
162
163GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
164{
165 return const_cast<GraphicPlane&>(
166 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
167}
168
169void SurfaceFlinger::bootFinished()
170{
171 const nsecs_t now = systemTime();
172 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700173 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700175 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176}
177
178void SurfaceFlinger::onFirstRef()
179{
180 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
181
182 // Wait for the main thread to be done with its initialization
183 mReadyToRunBarrier.wait();
184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186static inline uint16_t pack565(int r, int g, int b) {
187 return (r<<11)|(g<<5)|b;
188}
189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190status_t SurfaceFlinger::readyToRun()
191{
192 LOGI( "SurfaceFlinger's main thread ready to run. "
193 "Initializing graphics H/W...");
194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 // we only support one display currently
196 int dpy = 0;
197
198 {
199 // initialize the main display
200 GraphicPlane& plane(graphicPlane(dpy));
201 DisplayHardware* const hw = new DisplayHardware(this, dpy);
202 plane.setDisplayHardware(hw);
203 }
204
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700205 // create the shared control-block
206 mServerHeap = new MemoryHeapBase(4096,
207 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
208 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
209
210 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
211 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
212
213 new(mServerCblk) surface_flinger_cblk_t;
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 // initialize primary screen
216 // (other display should be initialized in the same manner, but
217 // asynchronously, as they could come and go. None of this is supported
218 // yet).
219 const GraphicPlane& plane(graphicPlane(dpy));
220 const DisplayHardware& hw = plane.displayHardware();
221 const uint32_t w = hw.getWidth();
222 const uint32_t h = hw.getHeight();
223 const uint32_t f = hw.getFormat();
224 hw.makeCurrent();
225
226 // initialize the shared control block
227 mServerCblk->connected |= 1<<dpy;
228 display_cblk_t* dcblk = mServerCblk->displays + dpy;
229 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800230 dcblk->w = plane.getWidth();
231 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 dcblk->format = f;
233 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
234 dcblk->xdpi = hw.getDpiX();
235 dcblk->ydpi = hw.getDpiY();
236 dcblk->fps = hw.getRefreshRate();
237 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238
239 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
241 glPixelStorei(GL_PACK_ALIGNMENT, 4);
242 glEnableClientState(GL_VERTEX_ARRAY);
243 glEnable(GL_SCISSOR_TEST);
244 glShadeModel(GL_FLAT);
245 glDisable(GL_DITHER);
246 glDisable(GL_CULL_FACE);
247
248 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
249 const uint16_t g1 = pack565(0x17,0x2f,0x17);
250 const uint16_t textureData[4] = { g0, g1, g1, g0 };
251 glGenTextures(1, &mWormholeTexName);
252 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
253 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
254 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
255 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
256 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
257 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
258 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
259
260 glViewport(0, 0, w, h);
261 glMatrixMode(GL_PROJECTION);
262 glLoadIdentity();
263 glOrthof(0, w, h, 0, 0, 1);
264
265 LayerDim::initDimmer(this, w, h);
266
267 mReadyToRunBarrier.open();
268
269 /*
270 * We're now ready to accept clients...
271 */
272
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700273 // start boot animation
274 property_set("ctl.start", "bootanim");
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 return NO_ERROR;
277}
278
279// ----------------------------------------------------------------------------
280#if 0
281#pragma mark -
282#pragma mark Events Handler
283#endif
284
285void SurfaceFlinger::waitForEvent()
286{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700287 while (true) {
288 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800289 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700290 if (UNLIKELY(isFrozen())) {
291 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700292 const nsecs_t now = systemTime();
293 if (mFreezeDisplayTime == 0) {
294 mFreezeDisplayTime = now;
295 }
296 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
297 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700298 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700299
Mathias Agopianbb641242010-05-18 17:06:55 -0700300 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800301
302 // see if we timed out
303 if (isFrozen()) {
304 const nsecs_t now = systemTime();
305 nsecs_t frozenTime = (now - mFreezeDisplayTime);
306 if (frozenTime >= freezeDisplayTimeout) {
307 // we timed out and are still frozen
308 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
309 mFreezeDisplay, mFreezeCount);
310 mFreezeDisplayTime = 0;
311 mFreezeCount = 0;
312 mFreezeDisplay = false;
313 }
314 }
315
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700316 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700317 switch (msg->what) {
318 case MessageQueue::INVALIDATE:
319 // invalidate message, just return to the main loop
320 return;
321 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 }
324}
325
326void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700327 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
330void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700331 // this is the IPC call
332 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333}
334
Mathias Agopianbb641242010-05-18 17:06:55 -0700335status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
336 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337{
Mathias Agopianbb641242010-05-18 17:06:55 -0700338 return mEventQueue.postMessage(msg, reltime, flags);
339}
340
341status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
342 nsecs_t reltime, uint32_t flags)
343{
344 status_t res = mEventQueue.postMessage(msg, reltime, flags);
345 if (res == NO_ERROR) {
346 msg->wait();
347 }
348 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
351// ----------------------------------------------------------------------------
352#if 0
353#pragma mark -
354#pragma mark Main loop
355#endif
356
357bool SurfaceFlinger::threadLoop()
358{
359 waitForEvent();
360
361 // check for transactions
362 if (UNLIKELY(mConsoleSignals)) {
363 handleConsoleEvents();
364 }
365
366 if (LIKELY(mTransactionCount == 0)) {
367 // if we're in a global transaction, don't do anything.
368 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
369 uint32_t transactionFlags = getTransactionFlags(mask);
370 if (LIKELY(transactionFlags)) {
371 handleTransaction(transactionFlags);
372 }
373 }
374
375 // post surfaces (if needed)
376 handlePageFlip();
377
378 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700379 if (LIKELY(hw.canDraw() && !isFrozen())) {
Mathias Agopian35b48d12010-09-13 22:57:58 -0700380
Mathias Agopian22c67842010-11-01 23:32:18 -0700381#ifdef USE_COMPOSITION_BYPASS
382 if (handleBypassLayer()) {
383 unlockClients();
384 return true;
385 }
386#endif
387
388 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700389 const int index = hw.getCurrentBufferIndex();
390 GraphicLog& logger(GraphicLog::getInstance());
391
392 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 handleRepaint();
394
Mathias Agopian74faca22009-09-17 16:18:16 -0700395 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700396 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700397 hw.compositionComplete();
398
Mathias Agopian35b48d12010-09-13 22:57:58 -0700399 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 postFramebuffer();
Mathias Agopian35b48d12010-09-13 22:57:58 -0700401
402 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 } else {
404 // pretend we did the post
Mathias Agopian5dc7e7d2010-12-15 14:41:59 -0800405 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 usleep(16667); // 60 fps period
407 }
408 return true;
409}
410
Mathias Agopian22c67842010-11-01 23:32:18 -0700411bool SurfaceFlinger::handleBypassLayer()
412{
413 sp<Layer> bypassLayer(mBypassLayer.promote());
414 if (bypassLayer != 0) {
415 sp<GraphicBuffer> buffer(bypassLayer->getBypassBuffer());
416 if (buffer!=0 && (buffer->usage & GRALLOC_USAGE_HW_FB)) {
417 const DisplayHardware& hw(graphicPlane(0).displayHardware());
418 hw.postBypassBuffer(buffer->handle);
419 return true;
420 }
421 }
422 return false;
423}
424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425void SurfaceFlinger::postFramebuffer()
426{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 if (!mInvalidRegion.isEmpty()) {
428 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700429 const nsecs_t now = systemTime();
430 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700432 mLastSwapBufferTime = systemTime() - now;
433 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 }
436}
437
438void SurfaceFlinger::handleConsoleEvents()
439{
440 // something to do with the console
441 const DisplayHardware& hw = graphicPlane(0).displayHardware();
442
443 int what = android_atomic_and(0, &mConsoleSignals);
444 if (what & eConsoleAcquired) {
445 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700446 // this is a temporary work-around, eventually this should be called
447 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700448 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 }
450
Mathias Agopian59119e62010-10-11 12:37:43 -0700451 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700452 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 hw.releaseScreen();
455 }
456
457 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700458 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459 hw.releaseScreen();
460 } else {
461 mDeferReleaseConsole = true;
462 }
463 }
464
465 mDirtyRegion.set(hw.bounds());
466}
467
468void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
469{
Mathias Agopian3d579642009-06-04 18:46:21 -0700470 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471
Mathias Agopian4da75192010-08-10 17:19:56 -0700472 /*
473 * Perform and commit the transaction
474 */
475
Mathias Agopian3d579642009-06-04 18:46:21 -0700476 { // scope for the lock
477 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700478 const nsecs_t now = systemTime();
479 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700480 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700481 mLastTransactionTime = systemTime() - now;
482 mDebugInTransaction = 0;
Mathias Agopian4da75192010-08-10 17:19:56 -0700483 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700484 }
485
Mathias Agopian4da75192010-08-10 17:19:56 -0700486 /*
487 * Clean-up all layers that went away
488 * (do this without the lock held)
489 */
Mathias Agopian3d579642009-06-04 18:46:21 -0700490 const size_t count = ditchedLayers.size();
491 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700492 if (ditchedLayers[i] != 0) {
493 //LOGD("ditching layer %p", ditchedLayers[i].get());
494 ditchedLayers[i]->ditch();
495 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700496 }
497}
498
499void SurfaceFlinger::handleTransactionLocked(
500 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
501{
502 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 const size_t count = currentLayers.size();
504
505 /*
506 * Traversal of the children
507 * (perform the transaction for each of them if needed)
508 */
509
510 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
511 if (layersNeedTransaction) {
512 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700513 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
515 if (!trFlags) continue;
516
517 const uint32_t flags = layer->doTransaction(0);
518 if (flags & Layer::eVisibleRegion)
519 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 }
521 }
522
523 /*
524 * Perform our own transaction if needed
525 */
526
527 if (transactionFlags & eTransactionNeeded) {
528 if (mCurrentState.orientation != mDrawingState.orientation) {
529 // the orientation has changed, recompute all visible regions
530 // and invalidate everything.
531
532 const int dpy = 0;
533 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700534 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 GraphicPlane& plane(graphicPlane(dpy));
536 plane.setOrientation(orientation);
537
538 // update the shared control block
539 const DisplayHardware& hw(plane.displayHardware());
540 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
541 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800542 dcblk->w = plane.getWidth();
543 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544
545 mVisibleRegionsDirty = true;
546 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 }
548
549 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
550 // freezing or unfreezing the display -> trigger animation if needed
551 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800552 if (mFreezeDisplay)
553 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 }
555
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700556 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
557 // layers have been added
558 mVisibleRegionsDirty = true;
559 }
560
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 // some layers might have been removed, so
562 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700563 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700564 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700566 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700567 const size_t count = previousLayers.size();
568 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700569 const sp<LayerBase>& layer(previousLayers[i]);
570 if (currentLayers.indexOf( layer ) < 0) {
571 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700572 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700573 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700574 }
575 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
578
579 commitTransaction();
580}
581
582sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
583{
584 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
585}
586
587void SurfaceFlinger::computeVisibleRegions(
588 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
589{
590 const GraphicPlane& plane(graphicPlane(0));
591 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700592 const DisplayHardware& hw(plane.displayHardware());
593 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594
595 Region aboveOpaqueLayers;
596 Region aboveCoveredLayers;
597 Region dirty;
598
599 bool secureFrameBuffer = false;
600
601 size_t i = currentLayers.size();
602 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 layer->validateVisibility(planeTransform);
605
606 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700607 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608
Mathias Agopianab028732010-03-16 16:41:46 -0700609 /*
610 * opaqueRegion: area of a surface that is fully opaque.
611 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700613
614 /*
615 * visibleRegion: area of a surface that is visible on screen
616 * and not fully transparent. This is essentially the layer's
617 * footprint minus the opaque regions above it.
618 * Areas covered by a translucent surface are considered visible.
619 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700621
622 /*
623 * coveredRegion: area of a surface that is covered by all
624 * visible regions above it (which includes the translucent areas).
625 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700627
628
629 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700630 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700632 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700634 visibleRegion.andSelf(screenRegion);
635 if (!visibleRegion.isEmpty()) {
636 // Remove the transparent area from the visible region
637 if (translucent) {
638 visibleRegion.subtractSelf(layer->transparentRegionScreen);
639 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640
Mathias Agopianab028732010-03-16 16:41:46 -0700641 // compute the opaque region
642 const int32_t layerOrientation = layer->getOrientation();
643 if (s.alpha==255 && !translucent &&
644 ((layerOrientation & Transform::ROT_INVALID) == false)) {
645 // the opaque region is the layer's footprint
646 opaqueRegion = visibleRegion;
647 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 }
649 }
650
Mathias Agopianab028732010-03-16 16:41:46 -0700651 // Clip the covered region to the visible region
652 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
653
654 // Update aboveCoveredLayers for next (lower) layer
655 aboveCoveredLayers.orSelf(visibleRegion);
656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 // subtract the opaque region covered by the layers above us
658 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659
660 // compute this layer's dirty region
661 if (layer->contentDirty) {
662 // we need to invalidate the whole region
663 dirty = visibleRegion;
664 // as well, as the old visible region
665 dirty.orSelf(layer->visibleRegionScreen);
666 layer->contentDirty = false;
667 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700668 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700669 * the exposed region consists of two components:
670 * 1) what's VISIBLE now and was COVERED before
671 * 2) what's EXPOSED now less what was EXPOSED before
672 *
673 * note that (1) is conservative, we start with the whole
674 * visible region but only keep what used to be covered by
675 * something -- which mean it may have been exposed.
676 *
677 * (2) handles areas that were not covered by anything but got
678 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700679 */
Mathias Agopianab028732010-03-16 16:41:46 -0700680 const Region newExposed = visibleRegion - coveredRegion;
681 const Region oldVisibleRegion = layer->visibleRegionScreen;
682 const Region oldCoveredRegion = layer->coveredRegionScreen;
683 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
684 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 }
686 dirty.subtractSelf(aboveOpaqueLayers);
687
688 // accumulate to the screen dirty region
689 dirtyRegion.orSelf(dirty);
690
Mathias Agopianab028732010-03-16 16:41:46 -0700691 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693
694 // Store the visible region is screen space
695 layer->setVisibleRegion(visibleRegion);
696 layer->setCoveredRegion(coveredRegion);
697
Mathias Agopian97011222009-07-28 10:57:27 -0700698 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 if (layer->isSecure() && !visibleRegion.isEmpty()) {
700 secureFrameBuffer = true;
701 }
702 }
703
Mathias Agopian97011222009-07-28 10:57:27 -0700704 // invalidate the areas where a layer was removed
705 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
706 mDirtyRegionRemovedLayer.clear();
707
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 mSecureFrameBuffer = secureFrameBuffer;
709 opaqueRegion = aboveOpaqueLayers;
710}
711
712
713void SurfaceFlinger::commitTransaction()
714{
715 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700716 mResizeTransationPending = false;
717 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718}
719
Mathias Agopian22c67842010-11-01 23:32:18 -0700720void SurfaceFlinger::setBypassLayer(const sp<LayerBase>& layer)
721{
722 // if this layer is already the bypass layer, do nothing
723 sp<Layer> cur(mBypassLayer.promote());
Mathias Agopiane24cc7a2010-12-07 21:00:25 -0800724 if (mBypassLayer == layer) {
725 if (cur != NULL) {
726 cur->updateBuffersOrientation();
727 }
Mathias Agopian22c67842010-11-01 23:32:18 -0700728 return;
Mathias Agopiane24cc7a2010-12-07 21:00:25 -0800729 }
Mathias Agopian22c67842010-11-01 23:32:18 -0700730
731 // clear the current bypass layer
732 mBypassLayer.clear();
733 if (cur != 0) {
734 cur->setBypass(false);
735 cur.clear();
736 }
737
738 // set new bypass layer
739 if (layer != 0) {
740 if (layer->setBypass(true)) {
741 mBypassLayer = static_cast<Layer*>(layer.get());
742 }
743 }
744}
745
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746void SurfaceFlinger::handlePageFlip()
747{
748 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700749 LayerVector& currentLayers = const_cast<LayerVector&>(
750 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 visibleRegions |= lockPageFlip(currentLayers);
752
753 const DisplayHardware& hw = graphicPlane(0).displayHardware();
754 const Region screenRegion(hw.bounds());
755 if (visibleRegions) {
756 Region opaqueRegion;
757 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700758
759 /*
760 * rebuild the visible layer list
761 */
762 mVisibleLayersSortedByZ.clear();
763 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
764 size_t count = currentLayers.size();
765 mVisibleLayersSortedByZ.setCapacity(count);
766 for (size_t i=0 ; i<count ; i++) {
767 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
768 mVisibleLayersSortedByZ.add(currentLayers[i]);
769 }
770
Mathias Agopian22c67842010-11-01 23:32:18 -0700771#ifdef USE_COMPOSITION_BYPASS
772 sp<LayerBase> bypassLayer;
773 const size_t numVisibleLayers = mVisibleLayersSortedByZ.size();
774 if (numVisibleLayers == 1) {
775 const sp<LayerBase>& candidate(mVisibleLayersSortedByZ[0]);
776 const Region& visibleRegion(candidate->visibleRegionScreen);
777 const Region reminder(screenRegion.subtract(visibleRegion));
778 if (reminder.isEmpty()) {
779 // fullscreen candidate!
780 bypassLayer = candidate;
781 }
782 }
783 setBypassLayer(bypassLayer);
784#endif
785
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 mWormholeRegion = screenRegion.subtract(opaqueRegion);
787 mVisibleRegionsDirty = false;
788 }
789
790 unlockPageFlip(currentLayers);
791 mDirtyRegion.andSelf(screenRegion);
792}
793
794bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
795{
796 bool recomputeVisibleRegions = false;
797 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700798 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700800 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 layer->lockPageFlip(recomputeVisibleRegions);
802 }
803 return recomputeVisibleRegions;
804}
805
806void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
807{
808 const GraphicPlane& plane(graphicPlane(0));
809 const Transform& planeTransform(plane.transform());
810 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700811 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700813 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814 layer->unlockPageFlip(planeTransform, mDirtyRegion);
815 }
816}
817
Mathias Agopianb8a55602009-06-26 19:06:36 -0700818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819void SurfaceFlinger::handleRepaint()
820{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700821 // compute the invalid region
822 mInvalidRegion.orSelf(mDirtyRegion);
823 if (mInvalidRegion.isEmpty()) {
824 // nothing to do
825 return;
826 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827
828 if (UNLIKELY(mDebugRegion)) {
829 debugFlashRegions();
830 }
831
Mathias Agopianb8a55602009-06-26 19:06:36 -0700832 // set the frame buffer
833 const DisplayHardware& hw(graphicPlane(0).displayHardware());
834 glMatrixMode(GL_MODELVIEW);
835 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836
837 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700838 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
839 (flags & DisplayHardware::BUFFER_PRESERVED))
840 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
842 // takes a rectangle, we must make sure to update that whole
843 // rectangle in that case
844 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700845 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700846 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 mDirtyRegion.set(mInvalidRegion.bounds());
848 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700849 // in the BUFFER_PRESERVED case, obviously, we can update only
850 // what's needed and nothing more.
851 // NOTE: this is NOT a common case, as preserving the backbuffer
852 // is costly and usually involves copying the whole update back.
853 }
854 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700855 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // We need to redraw the rectangle that will be updated
857 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700858 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // rectangle instead of a region (see DisplayHardware::flip())
860 mDirtyRegion.set(mInvalidRegion.bounds());
861 } else {
862 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 mDirtyRegion.set(hw.bounds());
864 mInvalidRegion = mDirtyRegion;
865 }
866 }
867
868 // compose all surfaces
869 composeSurfaces(mDirtyRegion);
870
871 // clear the dirty regions
872 mDirtyRegion.clear();
873}
874
875void SurfaceFlinger::composeSurfaces(const Region& dirty)
876{
877 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
878 // should never happen unless the window manager has a bug
879 // draw something...
880 drawWormhole();
881 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700882 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700883 const size_t count = layers.size();
884 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian45721772010-08-12 15:03:26 -0700885 const sp<LayerBase>& layer(layers[i]);
886 const Region clip(dirty.intersect(layer->visibleRegionScreen));
887 if (!clip.isEmpty()) {
888 layer->draw(clip);
889 }
890 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891}
892
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893void SurfaceFlinger::debugFlashRegions()
894{
Mathias Agopian0a917752010-06-14 21:20:00 -0700895 const DisplayHardware& hw(graphicPlane(0).displayHardware());
896 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700897
Mathias Agopian0a917752010-06-14 21:20:00 -0700898 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
899 (flags & DisplayHardware::BUFFER_PRESERVED))) {
900 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
901 mDirtyRegion.bounds() : hw.bounds());
902 composeSurfaces(repaint);
903 }
904
905 TextureManager::deactivateTextures();
906
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 glDisable(GL_BLEND);
908 glDisable(GL_DITHER);
909 glDisable(GL_SCISSOR_TEST);
910
Mathias Agopian0926f502009-05-04 14:17:04 -0700911 static int toggle = 0;
912 toggle = 1 - toggle;
913 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700914 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700915 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700916 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700917 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918
Mathias Agopian20f68782009-05-11 00:03:41 -0700919 Region::const_iterator it = mDirtyRegion.begin();
920 Region::const_iterator const end = mDirtyRegion.end();
921 while (it != end) {
922 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 GLfloat vertices[][2] = {
924 { r.left, r.top },
925 { r.left, r.bottom },
926 { r.right, r.bottom },
927 { r.right, r.top }
928 };
929 glVertexPointer(2, GL_FLOAT, 0, vertices);
930 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
931 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700932
Mathias Agopianb8a55602009-06-26 19:06:36 -0700933 if (mInvalidRegion.isEmpty()) {
934 mDirtyRegion.dump("mDirtyRegion");
935 mInvalidRegion.dump("mInvalidRegion");
936 }
937 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938
939 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700940 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941
942 glEnable(GL_SCISSOR_TEST);
943 //mDirtyRegion.dump("mDirtyRegion");
944}
945
946void SurfaceFlinger::drawWormhole() const
947{
948 const Region region(mWormholeRegion.intersect(mDirtyRegion));
949 if (region.isEmpty())
950 return;
951
952 const DisplayHardware& hw(graphicPlane(0).displayHardware());
953 const int32_t width = hw.getWidth();
954 const int32_t height = hw.getHeight();
955
956 glDisable(GL_BLEND);
957 glDisable(GL_DITHER);
958
959 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700960 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700961 Region::const_iterator it = region.begin();
962 Region::const_iterator const end = region.end();
963 while (it != end) {
964 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 const GLint sy = height - (r.top + r.height());
966 glScissor(r.left, sy, r.width(), r.height());
967 glClear(GL_COLOR_BUFFER_BIT);
968 }
969 } else {
970 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
971 { width, height }, { 0, height } };
972 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
973 glVertexPointer(2, GL_SHORT, 0, vertices);
974 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
975 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -0700976#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700977 if (GLExtensions::getInstance().haveTextureExternal()) {
978 glDisable(GL_TEXTURE_EXTERNAL_OES);
979 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700980#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 glEnable(GL_TEXTURE_2D);
982 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
983 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
984 glMatrixMode(GL_TEXTURE);
985 glLoadIdentity();
986 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700987 Region::const_iterator it = region.begin();
988 Region::const_iterator const end = region.end();
989 while (it != end) {
990 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 const GLint sy = height - (r.top + r.height());
992 glScissor(r.left, sy, r.width(), r.height());
993 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
994 }
995 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
996 }
997}
998
999void SurfaceFlinger::debugShowFPS() const
1000{
1001 static int mFrameCount;
1002 static int mLastFrameCount = 0;
1003 static nsecs_t mLastFpsTime = 0;
1004 static float mFps = 0;
1005 mFrameCount++;
1006 nsecs_t now = systemTime();
1007 nsecs_t diff = now - mLastFpsTime;
1008 if (diff > ms2ns(250)) {
1009 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1010 mLastFpsTime = now;
1011 mLastFrameCount = mFrameCount;
1012 }
1013 // XXX: mFPS has the value we want
1014 }
1015
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001016status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017{
1018 Mutex::Autolock _l(mStateLock);
1019 addLayer_l(layer);
1020 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1021 return NO_ERROR;
1022}
1023
Mathias Agopian96f08192010-06-02 23:28:45 -07001024status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1025{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001026 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001027 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1028}
1029
1030ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1031 const sp<LayerBaseClient>& lbc)
1032{
Mathias Agopian96f08192010-06-02 23:28:45 -07001033 // attach this layer to the client
Mathias Agopian02cf75e2011-05-03 16:21:41 -07001034 size_t name = client->attachLayer(lbc);
1035
1036 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001037
1038 // add this layer to the current state list
1039 addLayer_l(lbc);
1040
Mathias Agopian02cf75e2011-05-03 16:21:41 -07001041 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001042}
1043
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001044status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045{
1046 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001047 status_t err = purgatorizeLayer_l(layer);
1048 if (err == NO_ERROR)
1049 setTransactionFlags(eTransactionNeeded);
1050 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051}
1052
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001053status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001055 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1056 if (lbc != 0) {
Mathias Agopianf7662af2011-03-09 16:13:31 -08001057 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001058 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1060 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001061 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 return NO_ERROR;
1063 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001064 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065}
1066
Mathias Agopian9a112062009-04-17 19:36:26 -07001067status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1068{
Mathias Agopianf7662af2011-03-09 16:13:31 -08001069 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001070 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001071
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001072 layerBase->onRemoved();
1073
Mathias Agopian3d579642009-06-04 18:46:21 -07001074 // it's possible that we don't find a layer, because it might
1075 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001076 // from the user because there is a race between Client::destroySurface(),
1077 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001078 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1079}
1080
Mathias Agopian96f08192010-06-02 23:28:45 -07001081status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082{
Mathias Agopian96f08192010-06-02 23:28:45 -07001083 layer->forceVisibilityTransaction();
1084 setTransactionFlags(eTraversalNeeded);
1085 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086}
1087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1089{
1090 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1091}
1092
Mathias Agopianbb641242010-05-18 17:06:55 -07001093uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094{
1095 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1096 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001097 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 }
1099 return old;
1100}
1101
1102void SurfaceFlinger::openGlobalTransaction()
1103{
1104 android_atomic_inc(&mTransactionCount);
1105}
1106
1107void SurfaceFlinger::closeGlobalTransaction()
1108{
1109 if (android_atomic_dec(&mTransactionCount) == 1) {
1110 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001111
1112 // if there is a transaction with a resize, wait for it to
1113 // take effect before returning.
1114 Mutex::Autolock _l(mStateLock);
1115 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001116 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1117 if (CC_UNLIKELY(err != NO_ERROR)) {
1118 // just in case something goes wrong in SF, return to the
1119 // called after a few seconds.
1120 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1121 mResizeTransationPending = false;
1122 break;
1123 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001124 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 }
1126}
1127
1128status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1129{
1130 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1131 return BAD_VALUE;
1132
1133 Mutex::Autolock _l(mStateLock);
1134 mCurrentState.freezeDisplay = 1;
1135 setTransactionFlags(eTransactionNeeded);
1136
1137 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001138 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 return NO_ERROR;
1140}
1141
1142status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1143{
1144 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1145 return BAD_VALUE;
1146
1147 Mutex::Autolock _l(mStateLock);
1148 mCurrentState.freezeDisplay = 0;
1149 setTransactionFlags(eTransactionNeeded);
1150
1151 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001152 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 return NO_ERROR;
1154}
1155
Mathias Agopianc08731e2009-03-27 18:11:38 -07001156int SurfaceFlinger::setOrientation(DisplayID dpy,
1157 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158{
1159 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1160 return BAD_VALUE;
1161
1162 Mutex::Autolock _l(mStateLock);
1163 if (mCurrentState.orientation != orientation) {
1164 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001165 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 mCurrentState.orientation = orientation;
1167 setTransactionFlags(eTransactionNeeded);
1168 mTransactionCV.wait(mStateLock);
1169 } else {
1170 orientation = BAD_VALUE;
1171 }
1172 }
1173 return orientation;
1174}
1175
Mathias Agopian96f08192010-06-02 23:28:45 -07001176sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001177 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1179 uint32_t flags)
1180{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001181 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001183
1184 if (int32_t(w|h) < 0) {
1185 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1186 int(w), int(h));
1187 return surfaceHandle;
1188 }
1189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001191 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 switch (flags & eFXSurfaceMask) {
1193 case eFXSurfaceNormal:
1194 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001195 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001197 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1198 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 }
1200 break;
1201 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001202 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 break;
1204 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001205 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 break;
1207 }
1208
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001209 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001211 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001212 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001215 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001216 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001217 params->identity = surfaceHandle->getIdentity();
1218 params->width = w;
1219 params->height = h;
1220 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001221 if (normalLayer != 0) {
1222 Mutex::Autolock _l(mStateLock);
1223 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1224 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001225 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001226
Mathias Agopian96f08192010-06-02 23:28:45 -07001227 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 }
1229
1230 return surfaceHandle;
1231}
1232
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001233sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001234 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001235 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001236 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237{
1238 // initialize the surfaces
1239 switch (format) { // TODO: take h/w into account
1240 case PIXEL_FORMAT_TRANSPARENT:
1241 case PIXEL_FORMAT_TRANSLUCENT:
1242 format = PIXEL_FORMAT_RGBA_8888;
1243 break;
1244 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001245#ifdef NO_RGBX_8888
1246 format = PIXEL_FORMAT_RGB_565;
1247#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001248 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001249#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 break;
1251 }
1252
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001253#ifdef NO_RGBX_8888
1254 if (format == PIXEL_FORMAT_RGBX_8888)
1255 format = PIXEL_FORMAT_RGBA_8888;
1256#endif
1257
Mathias Agopian96f08192010-06-02 23:28:45 -07001258 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001259 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001260 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001262 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 }
1264 return layer;
1265}
1266
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001267sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001268 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001269 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270{
Mathias Agopian96f08192010-06-02 23:28:45 -07001271 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 return layer;
1274}
1275
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001276sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001277 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001278 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279{
Mathias Agopian96f08192010-06-02 23:28:45 -07001280 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 return layer;
1283}
1284
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001285sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001286 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001287 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288{
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 return layer;
1292}
1293
Mathias Agopian96f08192010-06-02 23:28:45 -07001294status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295{
Mathias Agopian9a112062009-04-17 19:36:26 -07001296 /*
1297 * called by the window manager, when a surface should be marked for
1298 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001299 *
1300 * The surface is removed from the current and drawing lists, but placed
1301 * in the purgatory queue, so it's not destroyed right-away (we need
1302 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001303 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001304
Mathias Agopian48d819a2009-09-10 19:41:18 -07001305 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001306 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001308 if (layer != 0) {
1309 err = purgatorizeLayer_l(layer);
1310 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001311 setTransactionFlags(eTransactionNeeded);
1312 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001313 }
1314 return err;
1315}
1316
1317status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1318{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001319 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001320
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001321 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001322 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001323 sp<LayerBaseClient> layer;
1324 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001325 MessageDestroySurface(
1326 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1327 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001328 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001329 sp<LayerBaseClient> l(layer);
1330 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001331 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001332 /*
1333 * remove the layer from the current list -- chances are that it's
1334 * not in the list anyway, because it should have been removed
1335 * already upon request of the client (eg: window manager).
1336 * However, a buggy client could have not done that.
1337 * Since we know we don't have any more clients, we don't need
1338 * to use the purgatory.
1339 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001340 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001341 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1342 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 return true;
1344 }
1345 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001346
Mathias Agopianbb641242010-05-18 17:06:55 -07001347 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 return NO_ERROR;
1349}
1350
1351status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001352 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 int32_t count,
1354 const layer_state_t* states)
1355{
1356 Mutex::Autolock _l(mStateLock);
1357 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001359 const layer_state_t& s(states[i]);
1360 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001361 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 if (what & ePositionChanged) {
1364 if (layer->setPosition(s.x, s.y))
1365 flags |= eTraversalNeeded;
1366 }
1367 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001368 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001370 mCurrentState.layersSortedByZ.removeAt(idx);
1371 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 // we need traversal (state changed)
1373 // AND transaction (list changed)
1374 flags |= eTransactionNeeded|eTraversalNeeded;
1375 }
1376 }
1377 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001378 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001380 mResizeTransationPending = true;
1381 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 }
1383 if (what & eAlphaChanged) {
1384 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1385 flags |= eTraversalNeeded;
1386 }
1387 if (what & eMatrixChanged) {
1388 if (layer->setMatrix(s.matrix))
1389 flags |= eTraversalNeeded;
1390 }
1391 if (what & eTransparentRegionChanged) {
1392 if (layer->setTransparentRegionHint(s.transparentRegion))
1393 flags |= eTraversalNeeded;
1394 }
1395 if (what & eVisibilityChanged) {
1396 if (layer->setFlags(s.flags, s.mask))
1397 flags |= eTraversalNeeded;
1398 }
1399 }
1400 }
1401 if (flags) {
1402 setTransactionFlags(flags);
1403 }
1404 return NO_ERROR;
1405}
1406
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407void SurfaceFlinger::screenReleased(int dpy)
1408{
1409 // this may be called by a signal handler, we can't do too much in here
1410 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1411 signalEvent();
1412}
1413
1414void SurfaceFlinger::screenAcquired(int dpy)
1415{
1416 // this may be called by a signal handler, we can't do too much in here
1417 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1418 signalEvent();
1419}
1420
1421status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1422{
1423 const size_t SIZE = 1024;
1424 char buffer[SIZE];
1425 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001426 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 snprintf(buffer, SIZE, "Permission Denial: "
1428 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1429 IPCThreadState::self()->getCallingPid(),
1430 IPCThreadState::self()->getCallingUid());
1431 result.append(buffer);
1432 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001433
1434 // figure out if we're stuck somewhere
1435 const nsecs_t now = systemTime();
1436 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1437 const nsecs_t inTransaction(mDebugInTransaction);
1438 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1439 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1440
1441 // Try to get the main lock, but don't insist if we can't
1442 // (this would indicate SF is stuck, but we want to be able to
1443 // print something in dumpsys).
1444 int retry = 3;
1445 while (mStateLock.tryLock()<0 && --retry>=0) {
1446 usleep(1000000);
1447 }
1448 const bool locked(retry >= 0);
1449 if (!locked) {
1450 snprintf(buffer, SIZE,
1451 "SurfaceFlinger appears to be unresponsive, "
1452 "dumping anyways (no locks held)\n");
1453 result.append(buffer);
1454 }
1455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1457 const size_t count = currentLayers.size();
1458 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001459 const sp<LayerBase>& layer(currentLayers[i]);
1460 layer->dump(result, buffer, SIZE);
1461 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 s.transparentRegion.dump(result, "transparentRegion");
1463 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1464 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1465 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 mWormholeRegion.dump(result, "WormholeRegion");
1468 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1469 snprintf(buffer, SIZE,
Mathias Agopian22c67842010-11-01 23:32:18 -07001470 " display frozen: %s, freezeCount=%d, orientation=%d, bypass=%p, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 mFreezeDisplay?"yes":"no", mFreezeCount,
Mathias Agopian22c67842010-11-01 23:32:18 -07001472 mCurrentState.orientation, mBypassLayer.unsafe_get(), hw.canDraw());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001474 snprintf(buffer, SIZE,
1475 " last eglSwapBuffers() time: %f us\n"
1476 " last transaction time : %f us\n",
1477 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1478 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001479
Mathias Agopian9795c422009-08-26 16:36:26 -07001480 if (inSwapBuffersDuration || !locked) {
1481 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1482 inSwapBuffersDuration/1000.0);
1483 result.append(buffer);
1484 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001485
Mathias Agopian9795c422009-08-26 16:36:26 -07001486 if (inTransactionDuration || !locked) {
1487 snprintf(buffer, SIZE, " transaction time: %f us\n",
1488 inTransactionDuration/1000.0);
1489 result.append(buffer);
1490 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001491
Mathias Agopian3330b202009-10-05 17:07:12 -07001492 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001493 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001494
1495 if (locked) {
1496 mStateLock.unlock();
1497 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 }
1499 write(fd, result.string(), result.size());
1500 return NO_ERROR;
1501}
1502
1503status_t SurfaceFlinger::onTransact(
1504 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1505{
1506 switch (code) {
1507 case CREATE_CONNECTION:
1508 case OPEN_GLOBAL_TRANSACTION:
1509 case CLOSE_GLOBAL_TRANSACTION:
1510 case SET_ORIENTATION:
1511 case FREEZE_DISPLAY:
1512 case UNFREEZE_DISPLAY:
1513 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001514 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001515 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 {
1517 // codes that require permission check
1518 IPCThreadState* ipc = IPCThreadState::self();
1519 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001520 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001521 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1522 LOGE("Permission Denial: "
1523 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1524 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001526 break;
1527 }
1528 case CAPTURE_SCREEN:
1529 {
1530 // codes that require permission check
1531 IPCThreadState* ipc = IPCThreadState::self();
1532 const int pid = ipc->getCallingPid();
1533 const int uid = ipc->getCallingUid();
1534 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1535 LOGE("Permission Denial: "
1536 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1537 return PERMISSION_DENIED;
1538 }
1539 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540 }
1541 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1544 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001545 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001546 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1547 IPCThreadState* ipc = IPCThreadState::self();
1548 const int pid = ipc->getCallingPid();
1549 const int uid = ipc->getCallingUid();
1550 LOGE("Permission Denial: "
1551 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552 return PERMISSION_DENIED;
1553 }
1554 int n;
1555 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001556 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001557 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 return NO_ERROR;
1559 case 1002: // SHOW_UPDATES
1560 n = data.readInt32();
1561 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1562 return NO_ERROR;
1563 case 1003: // SHOW_BACKGROUND
1564 n = data.readInt32();
1565 mDebugBackground = n ? 1 : 0;
1566 return NO_ERROR;
1567 case 1004:{ // repaint everything
1568 Mutex::Autolock _l(mStateLock);
1569 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1570 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1571 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001572 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001574 case 1005:{ // force transaction
1575 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1576 return NO_ERROR;
1577 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001578 case 1006:{ // enable/disable GraphicLog
1579 int enabled = data.readInt32();
1580 GraphicLog::getInstance().setEnabled(enabled);
1581 return NO_ERROR;
1582 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 case 1007: // set mFreezeCount
1584 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001585 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 return NO_ERROR;
1587 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001588 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 reply->writeInt32(0);
1590 reply->writeInt32(mDebugRegion);
1591 reply->writeInt32(mDebugBackground);
1592 return NO_ERROR;
1593 case 1013: {
1594 Mutex::Autolock _l(mStateLock);
1595 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1596 reply->writeInt32(hw.getPageFlipCount());
1597 }
1598 return NO_ERROR;
1599 }
1600 }
1601 return err;
1602}
1603
Mathias Agopian59119e62010-10-11 12:37:43 -07001604// ---------------------------------------------------------------------------
1605
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001606status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1607 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001608{
Mathias Agopian59119e62010-10-11 12:37:43 -07001609 if (!GLExtensions::getInstance().haveFramebufferObject())
1610 return INVALID_OPERATION;
1611
1612 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001613 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001614 const uint32_t hw_w = hw.getWidth();
1615 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001616 GLfloat u = 1;
1617 GLfloat v = 1;
1618
1619 // make sure to clear all GL error flags
1620 while ( glGetError() != GL_NO_ERROR ) ;
1621
1622 // create a FBO
1623 GLuint name, tname;
1624 glGenTextures(1, &tname);
1625 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001626 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1627 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001628 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001629 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001630 GLint tw = (2 << (31 - clz(hw_w)));
1631 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001632 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1633 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001634 u = GLfloat(hw_w) / tw;
1635 v = GLfloat(hw_h) / th;
1636 }
1637 glGenFramebuffersOES(1, &name);
1638 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001639 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1640 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001641
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001642 // redraw the screen entirely...
1643 glClearColor(0,0,0,1);
1644 glClear(GL_COLOR_BUFFER_BIT);
1645 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1646 const size_t count = layers.size();
1647 for (size_t i=0 ; i<count ; ++i) {
1648 const sp<LayerBase>& layer(layers[i]);
1649 layer->drawForSreenShot();
1650 }
1651
1652 // back to main framebuffer
1653 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1654 glDisable(GL_SCISSOR_TEST);
1655 glDeleteFramebuffersOES(1, &name);
1656
1657 *textureName = tname;
1658 *uOut = u;
1659 *vOut = v;
1660 return NO_ERROR;
1661}
1662
1663// ---------------------------------------------------------------------------
1664
1665status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1666{
1667 status_t result = PERMISSION_DENIED;
1668
1669 if (!GLExtensions::getInstance().haveFramebufferObject())
1670 return INVALID_OPERATION;
1671
1672 // get screen geometry
1673 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1674 const uint32_t hw_w = hw.getWidth();
1675 const uint32_t hw_h = hw.getHeight();
1676 const Region screenBounds(hw.bounds());
1677
1678 GLfloat u, v;
1679 GLuint tname;
1680 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1681 if (result != NO_ERROR) {
1682 return result;
1683 }
1684
1685 GLfloat vtx[8];
1686 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1687 glEnable(GL_TEXTURE_2D);
1688 glBindTexture(GL_TEXTURE_2D, tname);
1689 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1690 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1691 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1692 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1693 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1694 glVertexPointer(2, GL_FLOAT, 0, vtx);
1695
1696 class s_curve_interpolator {
1697 const float nbFrames, s, v;
1698 public:
1699 s_curve_interpolator(int nbFrames, float s)
1700 : nbFrames(1.0f / (nbFrames-1)), s(s),
1701 v(1.0f + expf(-s + 0.5f*s)) {
1702 }
1703 float operator()(int f) {
1704 const float x = f * nbFrames;
1705 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1706 }
1707 };
1708
1709 class v_stretch {
1710 const GLfloat hw_w, hw_h;
1711 public:
1712 v_stretch(uint32_t hw_w, uint32_t hw_h)
1713 : hw_w(hw_w), hw_h(hw_h) {
1714 }
1715 void operator()(GLfloat* vtx, float v) {
1716 const GLfloat w = hw_w + (hw_w * v);
1717 const GLfloat h = hw_h - (hw_h * v);
1718 const GLfloat x = (hw_w - w) * 0.5f;
1719 const GLfloat y = (hw_h - h) * 0.5f;
1720 vtx[0] = x; vtx[1] = y;
1721 vtx[2] = x; vtx[3] = y + h;
1722 vtx[4] = x + w; vtx[5] = y + h;
1723 vtx[6] = x + w; vtx[7] = y;
1724 }
1725 };
1726
1727 class h_stretch {
1728 const GLfloat hw_w, hw_h;
1729 public:
1730 h_stretch(uint32_t hw_w, uint32_t hw_h)
1731 : hw_w(hw_w), hw_h(hw_h) {
1732 }
1733 void operator()(GLfloat* vtx, float v) {
1734 const GLfloat w = hw_w - (hw_w * v);
1735 const GLfloat h = 1.0f;
1736 const GLfloat x = (hw_w - w) * 0.5f;
1737 const GLfloat y = (hw_h - h) * 0.5f;
1738 vtx[0] = x; vtx[1] = y;
1739 vtx[2] = x; vtx[3] = y + h;
1740 vtx[4] = x + w; vtx[5] = y + h;
1741 vtx[6] = x + w; vtx[7] = y;
1742 }
1743 };
1744
1745 // the full animation is 24 frames
1746 const int nbFrames = 12;
1747 s_curve_interpolator itr(nbFrames, 7.5f);
1748 s_curve_interpolator itg(nbFrames, 8.0f);
1749 s_curve_interpolator itb(nbFrames, 8.5f);
1750
1751 v_stretch vverts(hw_w, hw_h);
1752 glEnable(GL_BLEND);
1753 glBlendFunc(GL_ONE, GL_ONE);
1754 for (int i=0 ; i<nbFrames ; i++) {
1755 float x, y, w, h;
1756 const float vr = itr(i);
1757 const float vg = itg(i);
1758 const float vb = itb(i);
1759
1760 // clear screen
1761 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001762 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001763 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001764
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001765 // draw the red plane
1766 vverts(vtx, vr);
1767 glColorMask(1,0,0,1);
1768 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001769
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001770 // draw the green plane
1771 vverts(vtx, vg);
1772 glColorMask(0,1,0,1);
1773 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001774
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001775 // draw the blue plane
1776 vverts(vtx, vb);
1777 glColorMask(0,0,1,1);
1778 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001779
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001780 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001781 glDisable(GL_TEXTURE_2D);
1782 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001783 glColor4f(vg, vg, vg, 1);
1784 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1785 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001786 }
1787
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001788 h_stretch hverts(hw_w, hw_h);
1789 glDisable(GL_BLEND);
1790 glDisable(GL_TEXTURE_2D);
1791 glColorMask(1,1,1,1);
1792 for (int i=0 ; i<nbFrames ; i++) {
1793 const float v = itg(i);
1794 hverts(vtx, v);
1795 glClear(GL_COLOR_BUFFER_BIT);
1796 glColor4f(1-v, 1-v, 1-v, 1);
1797 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1798 hw.flip(screenBounds);
1799 }
1800
1801 glColorMask(1,1,1,1);
1802 glEnable(GL_SCISSOR_TEST);
1803 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1804 glDeleteTextures(1, &tname);
1805 return NO_ERROR;
1806}
1807
1808status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1809{
1810 status_t result = PERMISSION_DENIED;
1811
1812 if (!GLExtensions::getInstance().haveFramebufferObject())
1813 return INVALID_OPERATION;
1814
1815
1816 // get screen geometry
1817 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1818 const uint32_t hw_w = hw.getWidth();
1819 const uint32_t hw_h = hw.getHeight();
1820 const Region screenBounds(hw.bounds());
1821
1822 GLfloat u, v;
1823 GLuint tname;
1824 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1825 if (result != NO_ERROR) {
1826 return result;
1827 }
1828
1829 // back to main framebuffer
1830 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1831 glDisable(GL_SCISSOR_TEST);
1832
1833 GLfloat vtx[8];
1834 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1835 glEnable(GL_TEXTURE_2D);
1836 glBindTexture(GL_TEXTURE_2D, tname);
1837 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1838 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1839 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1840 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1841 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1842 glVertexPointer(2, GL_FLOAT, 0, vtx);
1843
1844 class s_curve_interpolator {
1845 const float nbFrames, s, v;
1846 public:
1847 s_curve_interpolator(int nbFrames, float s)
1848 : nbFrames(1.0f / (nbFrames-1)), s(s),
1849 v(1.0f + expf(-s + 0.5f*s)) {
1850 }
1851 float operator()(int f) {
1852 const float x = f * nbFrames;
1853 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1854 }
1855 };
1856
1857 class v_stretch {
1858 const GLfloat hw_w, hw_h;
1859 public:
1860 v_stretch(uint32_t hw_w, uint32_t hw_h)
1861 : hw_w(hw_w), hw_h(hw_h) {
1862 }
1863 void operator()(GLfloat* vtx, float v) {
1864 const GLfloat w = hw_w + (hw_w * v);
1865 const GLfloat h = hw_h - (hw_h * v);
1866 const GLfloat x = (hw_w - w) * 0.5f;
1867 const GLfloat y = (hw_h - h) * 0.5f;
1868 vtx[0] = x; vtx[1] = y;
1869 vtx[2] = x; vtx[3] = y + h;
1870 vtx[4] = x + w; vtx[5] = y + h;
1871 vtx[6] = x + w; vtx[7] = y;
1872 }
1873 };
1874
1875 class h_stretch {
1876 const GLfloat hw_w, hw_h;
1877 public:
1878 h_stretch(uint32_t hw_w, uint32_t hw_h)
1879 : hw_w(hw_w), hw_h(hw_h) {
1880 }
1881 void operator()(GLfloat* vtx, float v) {
1882 const GLfloat w = hw_w - (hw_w * v);
1883 const GLfloat h = 1.0f;
1884 const GLfloat x = (hw_w - w) * 0.5f;
1885 const GLfloat y = (hw_h - h) * 0.5f;
1886 vtx[0] = x; vtx[1] = y;
1887 vtx[2] = x; vtx[3] = y + h;
1888 vtx[4] = x + w; vtx[5] = y + h;
1889 vtx[6] = x + w; vtx[7] = y;
1890 }
1891 };
1892
Mathias Agopiana6546e52010-10-14 12:33:07 -07001893 // the full animation is 12 frames
1894 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001895 s_curve_interpolator itr(nbFrames, 7.5f);
1896 s_curve_interpolator itg(nbFrames, 8.0f);
1897 s_curve_interpolator itb(nbFrames, 8.5f);
1898
1899 h_stretch hverts(hw_w, hw_h);
1900 glDisable(GL_BLEND);
1901 glDisable(GL_TEXTURE_2D);
1902 glColorMask(1,1,1,1);
1903 for (int i=nbFrames-1 ; i>=0 ; i--) {
1904 const float v = itg(i);
1905 hverts(vtx, v);
1906 glClear(GL_COLOR_BUFFER_BIT);
1907 glColor4f(1-v, 1-v, 1-v, 1);
1908 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1909 hw.flip(screenBounds);
1910 }
1911
Mathias Agopiana6546e52010-10-14 12:33:07 -07001912 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001913 v_stretch vverts(hw_w, hw_h);
1914 glEnable(GL_BLEND);
1915 glBlendFunc(GL_ONE, GL_ONE);
1916 for (int i=nbFrames-1 ; i>=0 ; i--) {
1917 float x, y, w, h;
1918 const float vr = itr(i);
1919 const float vg = itg(i);
1920 const float vb = itb(i);
1921
1922 // clear screen
1923 glColorMask(1,1,1,1);
1924 glClear(GL_COLOR_BUFFER_BIT);
1925 glEnable(GL_TEXTURE_2D);
1926
1927 // draw the red plane
1928 vverts(vtx, vr);
1929 glColorMask(1,0,0,1);
1930 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1931
1932 // draw the green plane
1933 vverts(vtx, vg);
1934 glColorMask(0,1,0,1);
1935 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1936
1937 // draw the blue plane
1938 vverts(vtx, vb);
1939 glColorMask(0,0,1,1);
1940 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1941
1942 hw.flip(screenBounds);
1943 }
1944
1945 glColorMask(1,1,1,1);
1946 glEnable(GL_SCISSOR_TEST);
1947 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07001948 glDeleteTextures(1, &tname);
1949
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001950 return NO_ERROR;
1951}
1952
1953// ---------------------------------------------------------------------------
1954
Mathias Agopianabd671a2010-10-14 14:54:06 -07001955status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001956{
1957 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1958 if (!hw.canDraw()) {
1959 // we're already off
1960 return NO_ERROR;
1961 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07001962 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
1963 electronBeamOffAnimationImplLocked();
1964 }
1965
1966 // always clear the whole screen at the end of the animation
1967 glClearColor(0,0,0,1);
1968 glDisable(GL_SCISSOR_TEST);
1969 glClear(GL_COLOR_BUFFER_BIT);
1970 glEnable(GL_SCISSOR_TEST);
1971 hw.flip( Region(hw.bounds()) );
1972
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001973 hw.setCanDraw(false);
1974 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07001975}
1976
1977status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
1978{
Mathias Agopian59119e62010-10-11 12:37:43 -07001979 class MessageTurnElectronBeamOff : public MessageBase {
1980 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07001981 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07001982 status_t result;
1983 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07001984 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
1985 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07001986 }
1987 status_t getResult() const {
1988 return result;
1989 }
1990 virtual bool handler() {
1991 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07001992 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07001993 return true;
1994 }
1995 };
1996
Mathias Agopianabd671a2010-10-14 14:54:06 -07001997 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07001998 status_t res = postMessageSync(msg);
1999 if (res == NO_ERROR) {
2000 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002001
2002 // work-around: when the power-manager calls us we activate the
2003 // animation. eventually, the "on" animation will be called
2004 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002005 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002006 }
2007 return res;
2008}
2009
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002010// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002011
Mathias Agopianabd671a2010-10-14 14:54:06 -07002012status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002013{
2014 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2015 if (hw.canDraw()) {
2016 // we're already on
2017 return NO_ERROR;
2018 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002019 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2020 electronBeamOnAnimationImplLocked();
2021 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002022 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002023
2024 // make sure to redraw the whole screen when the animation is done
2025 mDirtyRegion.set(hw.bounds());
2026 signalEvent();
2027
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002028 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002029}
2030
2031status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2032{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002033 class MessageTurnElectronBeamOn : public MessageBase {
2034 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002035 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002036 status_t result;
2037 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002038 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2039 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002040 }
2041 status_t getResult() const {
2042 return result;
2043 }
2044 virtual bool handler() {
2045 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002046 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 return true;
2048 }
2049 };
2050
Mathias Agopianabd671a2010-10-14 14:54:06 -07002051 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002052 return NO_ERROR;
2053}
2054
2055// ---------------------------------------------------------------------------
2056
Mathias Agopiandf85c452010-09-29 13:02:36 -07002057status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2058 sp<IMemoryHeap>* heap,
2059 uint32_t* w, uint32_t* h, PixelFormat* f,
2060 uint32_t sw, uint32_t sh)
2061{
2062 status_t result = PERMISSION_DENIED;
2063
2064 // only one display supported for now
2065 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2066 return BAD_VALUE;
2067
2068 if (!GLExtensions::getInstance().haveFramebufferObject())
2069 return INVALID_OPERATION;
2070
2071 // get screen geometry
2072 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2073 const uint32_t hw_w = hw.getWidth();
2074 const uint32_t hw_h = hw.getHeight();
2075
2076 if ((sw > hw_w) || (sh > hw_h))
2077 return BAD_VALUE;
2078
2079 sw = (!sw) ? hw_w : sw;
2080 sh = (!sh) ? hw_h : sh;
2081 const size_t size = sw * sh * 4;
2082
2083 // make sure to clear all GL error flags
2084 while ( glGetError() != GL_NO_ERROR ) ;
2085
2086 // create a FBO
2087 GLuint name, tname;
2088 glGenRenderbuffersOES(1, &tname);
2089 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2090 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2091 glGenFramebuffersOES(1, &name);
2092 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2093 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2094 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2095
2096 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2097 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2098
2099 // invert everything, b/c glReadPixel() below will invert the FB
2100 glViewport(0, 0, sw, sh);
Mathias Agopiancfb676f2010-12-16 18:46:17 -08002101 glScissor(0, 0, sw, sh);
Mathias Agopiandf85c452010-09-29 13:02:36 -07002102 glMatrixMode(GL_PROJECTION);
2103 glPushMatrix();
2104 glLoadIdentity();
2105 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2106 glMatrixMode(GL_MODELVIEW);
2107
2108 // redraw the screen entirely...
2109 glClearColor(0,0,0,1);
2110 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiancfb676f2010-12-16 18:46:17 -08002111
Mathias Agopiandf85c452010-09-29 13:02:36 -07002112 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2113 const size_t count = layers.size();
2114 for (size_t i=0 ; i<count ; ++i) {
2115 const sp<LayerBase>& layer(layers[i]);
2116 layer->drawForSreenShot();
2117 }
2118
2119 // XXX: this is needed on tegra
2120 glScissor(0, 0, sw, sh);
2121
2122 // check for errors and return screen capture
2123 if (glGetError() != GL_NO_ERROR) {
2124 // error while rendering
2125 result = INVALID_OPERATION;
2126 } else {
2127 // allocate shared memory large enough to hold the
2128 // screen capture
2129 sp<MemoryHeapBase> base(
2130 new MemoryHeapBase(size, 0, "screen-capture") );
2131 void* const ptr = base->getBase();
2132 if (ptr) {
2133 // capture the screen with glReadPixels()
2134 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2135 if (glGetError() == GL_NO_ERROR) {
2136 *heap = base;
2137 *w = sw;
2138 *h = sh;
2139 *f = PIXEL_FORMAT_RGBA_8888;
2140 result = NO_ERROR;
2141 }
2142 } else {
2143 result = NO_MEMORY;
2144 }
2145 }
Mathias Agopiandf85c452010-09-29 13:02:36 -07002146 glEnable(GL_SCISSOR_TEST);
2147 glViewport(0, 0, hw_w, hw_h);
2148 glMatrixMode(GL_PROJECTION);
2149 glPopMatrix();
2150 glMatrixMode(GL_MODELVIEW);
2151
2152
2153 } else {
2154 result = BAD_VALUE;
2155 }
2156
2157 // release FBO resources
2158 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2159 glDeleteRenderbuffersOES(1, &tname);
2160 glDeleteFramebuffersOES(1, &name);
Mathias Agopian5dc7e7d2010-12-15 14:41:59 -08002161
2162 hw.compositionComplete();
2163
Mathias Agopiandf85c452010-09-29 13:02:36 -07002164 return result;
2165}
2166
2167
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002168status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2169 sp<IMemoryHeap>* heap,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002170 uint32_t* width, uint32_t* height, PixelFormat* format,
2171 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002172{
2173 // only one display supported for now
2174 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2175 return BAD_VALUE;
2176
2177 if (!GLExtensions::getInstance().haveFramebufferObject())
2178 return INVALID_OPERATION;
2179
2180 class MessageCaptureScreen : public MessageBase {
2181 SurfaceFlinger* flinger;
2182 DisplayID dpy;
2183 sp<IMemoryHeap>* heap;
2184 uint32_t* w;
2185 uint32_t* h;
2186 PixelFormat* f;
Mathias Agopiandf85c452010-09-29 13:02:36 -07002187 uint32_t sw;
2188 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002189 status_t result;
2190 public:
2191 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002192 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
2193 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002194 : flinger(flinger), dpy(dpy),
Mathias Agopiandf85c452010-09-29 13:02:36 -07002195 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002196 {
2197 }
2198 status_t getResult() const {
2199 return result;
2200 }
2201 virtual bool handler() {
2202 Mutex::Autolock _l(flinger->mStateLock);
2203
2204 // if we have secure windows, never allow the screen capture
2205 if (flinger->mSecureFrameBuffer)
2206 return true;
2207
Mathias Agopiandf85c452010-09-29 13:02:36 -07002208 result = flinger->captureScreenImplLocked(dpy,
2209 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002210
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002211 return true;
2212 }
2213 };
2214
2215 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002216 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002217 status_t res = postMessageSync(msg);
2218 if (res == NO_ERROR) {
2219 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2220 }
2221 return res;
2222}
2223
2224// ---------------------------------------------------------------------------
2225
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002226sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2227{
2228 sp<Layer> result;
2229 Mutex::Autolock _l(mStateLock);
2230 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2231 return result;
2232}
2233
2234// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235
Mathias Agopian96f08192010-06-02 23:28:45 -07002236Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002237 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002238{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002239}
2240
Mathias Agopian96f08192010-06-02 23:28:45 -07002241Client::~Client()
2242{
Mathias Agopian96f08192010-06-02 23:28:45 -07002243 const size_t count = mLayers.size();
2244 for (size_t i=0 ; i<count ; i++) {
2245 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2246 if (layer != 0) {
2247 mFlinger->removeLayer(layer);
2248 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002249 }
2250}
2251
Mathias Agopian96f08192010-06-02 23:28:45 -07002252status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002253 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002254}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002255
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002256size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002257{
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002258 Mutex::Autolock _l(mLock);
2259 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002260 mLayers.add(name, layer);
2261 return name;
2262}
2263
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002264void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002265{
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002266 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002267 // we do a linear search here, because this doesn't happen often
2268 const size_t count = mLayers.size();
2269 for (size_t i=0 ; i<count ; i++) {
2270 if (mLayers.valueAt(i) == layer) {
2271 mLayers.removeItemsAt(i, 1);
2272 break;
2273 }
2274 }
2275}
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002276sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2277{
2278 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002279 sp<LayerBaseClient> lbc;
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002280 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002281 if (layer != 0) {
2282 lbc = layer.promote();
2283 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002284 }
2285 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286}
2287
Mathias Agopian96f08192010-06-02 23:28:45 -07002288sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002289 return 0;
2290}
2291ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2292 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002293}
Mathias Agopian96f08192010-06-02 23:28:45 -07002294sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002295 ISurfaceComposerClient::surface_data_t* params, int pid,
2296 const String8& name,
2297 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002298 uint32_t flags)
2299{
Mathias Agopian96f08192010-06-02 23:28:45 -07002300 return mFlinger->createSurface(this, pid, name, params,
2301 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002302}
Mathias Agopian96f08192010-06-02 23:28:45 -07002303status_t Client::destroySurface(SurfaceID sid) {
2304 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002305}
Mathias Agopian96f08192010-06-02 23:28:45 -07002306status_t Client::setState(int32_t count, const layer_state_t* states) {
2307 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308}
2309
2310// ---------------------------------------------------------------------------
2311
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002312UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2313 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2314{
2315 const int pgsize = getpagesize();
2316 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2317
2318 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2319 "SurfaceFlinger Client control-block");
2320
2321 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2322 if (ctrlblk) { // construct the shared structure in-place.
2323 new(ctrlblk) SharedClient;
2324 }
2325}
2326
2327UserClient::~UserClient()
2328{
2329 if (ctrlblk) {
2330 ctrlblk->~SharedClient(); // destroy our shared-structure.
2331 }
2332
2333 /*
2334 * When a UserClient dies, it's unclear what to do exactly.
2335 * We could go ahead and destroy all surfaces linked to that client
2336 * however, it wouldn't be fair to the main Client
2337 * (usually the the window-manager), which might want to re-target
2338 * the layer to another UserClient.
2339 * I think the best is to do nothing, or not much; in most cases the
2340 * WM itself will go ahead and clean things up when it detects a client of
2341 * his has died.
2342 * The remaining question is what to display? currently we keep
2343 * just keep the current buffer.
2344 */
2345}
2346
2347status_t UserClient::initCheck() const {
2348 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2349}
2350
2351void UserClient::detachLayer(const Layer* layer)
2352{
2353 int32_t name = layer->getToken();
2354 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002355 int32_t mask = 1LU<<name;
2356 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2357 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2358 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002359 }
2360}
2361
2362sp<IMemoryHeap> UserClient::getControlBlock() const {
2363 return mCblkHeap;
2364}
2365
2366ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2367{
2368 int32_t name = NAME_NOT_FOUND;
2369 sp<Layer> layer(mFlinger->getLayer(sur));
2370 if (layer == 0) return name;
2371
Mathias Agopian579b3f82010-06-08 19:54:15 -07002372 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002373 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07002374 if ((name >= 0) && (layer->getClient() == this))
2375 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002376
2377 name = 0;
2378 do {
2379 int32_t mask = 1LU<<name;
2380 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2381 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002382 status_t err = layer->setToken(
2383 const_cast<UserClient*>(this), ctrlblk, name);
2384 if (err != NO_ERROR) {
2385 // free the name
2386 android_atomic_and(~mask, &mBitmap);
2387 name = err;
2388 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002389 break;
2390 }
Jamie Gennis8a083922011-02-10 16:18:36 -08002391 if (++name >= SharedBufferStack::NUM_LAYERS_MAX)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002392 name = NO_MEMORY;
2393 } while(name >= 0);
2394
Mathias Agopian53503a92010-06-08 15:40:56 -07002395 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2396 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002397 return name;
2398}
2399
2400sp<ISurface> UserClient::createSurface(
2401 ISurfaceComposerClient::surface_data_t* params, int pid,
2402 const String8& name,
2403 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2404 uint32_t flags) {
2405 return 0;
2406}
2407status_t UserClient::destroySurface(SurfaceID sid) {
2408 return INVALID_OPERATION;
2409}
2410status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2411 return INVALID_OPERATION;
2412}
2413
2414// ---------------------------------------------------------------------------
2415
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002416GraphicPlane::GraphicPlane()
2417 : mHw(0)
2418{
2419}
2420
2421GraphicPlane::~GraphicPlane() {
2422 delete mHw;
2423}
2424
2425bool GraphicPlane::initialized() const {
2426 return mHw ? true : false;
2427}
2428
Mathias Agopian2b92d892010-02-08 15:49:35 -08002429int GraphicPlane::getWidth() const {
2430 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002431}
2432
Mathias Agopian2b92d892010-02-08 15:49:35 -08002433int GraphicPlane::getHeight() const {
2434 return mHeight;
2435}
2436
2437void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2438{
2439 mHw = hw;
2440
2441 // initialize the display orientation transform.
2442 // it's a constant that should come from the display driver.
2443 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2444 char property[PROPERTY_VALUE_MAX];
2445 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2446 //displayOrientation
2447 switch (atoi(property)) {
2448 case 90:
2449 displayOrientation = ISurfaceComposer::eOrientation90;
2450 break;
2451 case 270:
2452 displayOrientation = ISurfaceComposer::eOrientation270;
2453 break;
2454 }
2455 }
2456
2457 const float w = hw->getWidth();
2458 const float h = hw->getHeight();
2459 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2460 &mDisplayTransform);
2461 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2462 mDisplayWidth = h;
2463 mDisplayHeight = w;
2464 } else {
2465 mDisplayWidth = w;
2466 mDisplayHeight = h;
2467 }
2468
2469 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002470}
2471
2472status_t GraphicPlane::orientationToTransfrom(
2473 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002474{
2475 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 switch (orientation) {
2477 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002478 flags = Transform::ROT_0;
2479 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002481 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 break;
2483 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002484 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 break;
2486 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002487 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488 break;
2489 default:
2490 return BAD_VALUE;
2491 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002492 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002493 return NO_ERROR;
2494}
2495
2496status_t GraphicPlane::setOrientation(int orientation)
2497{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498 // If the rotation can be handled in hardware, this is where
2499 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002500
2501 const DisplayHardware& hw(displayHardware());
2502 const float w = mDisplayWidth;
2503 const float h = mDisplayHeight;
2504 mWidth = int(w);
2505 mHeight = int(h);
2506
2507 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002508 GraphicPlane::orientationToTransfrom(orientation, w, h,
2509 &orientationTransform);
2510 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2511 mWidth = int(h);
2512 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002513 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002514
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002515 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002516 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517 return NO_ERROR;
2518}
2519
2520const DisplayHardware& GraphicPlane::displayHardware() const {
2521 return *mHw;
2522}
2523
Mathias Agopian59119e62010-10-11 12:37:43 -07002524DisplayHardware& GraphicPlane::editDisplayHardware() {
2525 return *mHw;
2526}
2527
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002528const Transform& GraphicPlane::transform() const {
2529 return mGlobalTransform;
2530}
2531
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002532EGLDisplay GraphicPlane::getEGLDisplay() const {
2533 return mHw->getEGLDisplay();
2534}
2535
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536// ---------------------------------------------------------------------------
2537
2538}; // namespace android