blob: 5314cb09e7a45da63b41079522fdfcd93fe00d12 [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;
Mathias Agopian34cd9b72011-05-03 17:04:02 -0700369 uint32_t transactionFlags = peekTransactionFlags(mask);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370 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 Agopian34cd9b72011-05-03 17:04:02 -0700480
481 // Here we're guaranteed that some transaction flags are set
482 // so we can call handleTransactionLocked() unconditionally.
483 // We call getTransactionFlags(), which will also clear the flags,
484 // with mStateLock held to guarantee that mCurrentState won't change
485 // until the transaction is commited.
486
487 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
488 transactionFlags = getTransactionFlags(mask);
Mathias Agopian3d579642009-06-04 18:46:21 -0700489 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian34cd9b72011-05-03 17:04:02 -0700490
Mathias Agopian9795c422009-08-26 16:36:26 -0700491 mLastTransactionTime = systemTime() - now;
492 mDebugInTransaction = 0;
Mathias Agopian4da75192010-08-10 17:19:56 -0700493 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700494 }
495
Mathias Agopian4da75192010-08-10 17:19:56 -0700496 /*
497 * Clean-up all layers that went away
498 * (do this without the lock held)
499 */
Mathias Agopian3d579642009-06-04 18:46:21 -0700500 const size_t count = ditchedLayers.size();
501 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700502 if (ditchedLayers[i] != 0) {
503 //LOGD("ditching layer %p", ditchedLayers[i].get());
504 ditchedLayers[i]->ditch();
505 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700506 }
507}
508
509void SurfaceFlinger::handleTransactionLocked(
510 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
511{
512 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 const size_t count = currentLayers.size();
514
515 /*
516 * Traversal of the children
517 * (perform the transaction for each of them if needed)
518 */
519
520 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
521 if (layersNeedTransaction) {
522 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700523 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
525 if (!trFlags) continue;
526
527 const uint32_t flags = layer->doTransaction(0);
528 if (flags & Layer::eVisibleRegion)
529 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530 }
531 }
532
533 /*
534 * Perform our own transaction if needed
535 */
536
537 if (transactionFlags & eTransactionNeeded) {
538 if (mCurrentState.orientation != mDrawingState.orientation) {
539 // the orientation has changed, recompute all visible regions
540 // and invalidate everything.
541
542 const int dpy = 0;
543 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700544 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 GraphicPlane& plane(graphicPlane(dpy));
546 plane.setOrientation(orientation);
547
548 // update the shared control block
549 const DisplayHardware& hw(plane.displayHardware());
550 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
551 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800552 dcblk->w = plane.getWidth();
553 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554
555 mVisibleRegionsDirty = true;
556 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 }
558
559 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
560 // freezing or unfreezing the display -> trigger animation if needed
561 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800562 if (mFreezeDisplay)
563 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564 }
565
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700566 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
567 // layers have been added
568 mVisibleRegionsDirty = true;
569 }
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 // some layers might have been removed, so
572 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700573 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700574 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700576 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700577 const size_t count = previousLayers.size();
578 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700579 const sp<LayerBase>& layer(previousLayers[i]);
580 if (currentLayers.indexOf( layer ) < 0) {
581 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700582 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700583 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700584 }
585 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 }
588
589 commitTransaction();
590}
591
592sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
593{
594 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
595}
596
597void SurfaceFlinger::computeVisibleRegions(
598 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
599{
600 const GraphicPlane& plane(graphicPlane(0));
601 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700602 const DisplayHardware& hw(plane.displayHardware());
603 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604
605 Region aboveOpaqueLayers;
606 Region aboveCoveredLayers;
607 Region dirty;
608
609 bool secureFrameBuffer = false;
610
611 size_t i = currentLayers.size();
612 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700613 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 layer->validateVisibility(planeTransform);
615
616 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700617 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618
Mathias Agopianab028732010-03-16 16:41:46 -0700619 /*
620 * opaqueRegion: area of a surface that is fully opaque.
621 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700623
624 /*
625 * visibleRegion: area of a surface that is visible on screen
626 * and not fully transparent. This is essentially the layer's
627 * footprint minus the opaque regions above it.
628 * Areas covered by a translucent surface are considered visible.
629 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700631
632 /*
633 * coveredRegion: area of a surface that is covered by all
634 * visible regions above it (which includes the translucent areas).
635 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700637
638
639 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700640 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700642 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700644 visibleRegion.andSelf(screenRegion);
645 if (!visibleRegion.isEmpty()) {
646 // Remove the transparent area from the visible region
647 if (translucent) {
648 visibleRegion.subtractSelf(layer->transparentRegionScreen);
649 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
Mathias Agopianab028732010-03-16 16:41:46 -0700651 // compute the opaque region
652 const int32_t layerOrientation = layer->getOrientation();
653 if (s.alpha==255 && !translucent &&
654 ((layerOrientation & Transform::ROT_INVALID) == false)) {
655 // the opaque region is the layer's footprint
656 opaqueRegion = visibleRegion;
657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 }
659 }
660
Mathias Agopianab028732010-03-16 16:41:46 -0700661 // Clip the covered region to the visible region
662 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
663
664 // Update aboveCoveredLayers for next (lower) layer
665 aboveCoveredLayers.orSelf(visibleRegion);
666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 // subtract the opaque region covered by the layers above us
668 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669
670 // compute this layer's dirty region
671 if (layer->contentDirty) {
672 // we need to invalidate the whole region
673 dirty = visibleRegion;
674 // as well, as the old visible region
675 dirty.orSelf(layer->visibleRegionScreen);
676 layer->contentDirty = false;
677 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700678 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700679 * the exposed region consists of two components:
680 * 1) what's VISIBLE now and was COVERED before
681 * 2) what's EXPOSED now less what was EXPOSED before
682 *
683 * note that (1) is conservative, we start with the whole
684 * visible region but only keep what used to be covered by
685 * something -- which mean it may have been exposed.
686 *
687 * (2) handles areas that were not covered by anything but got
688 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700689 */
Mathias Agopianab028732010-03-16 16:41:46 -0700690 const Region newExposed = visibleRegion - coveredRegion;
691 const Region oldVisibleRegion = layer->visibleRegionScreen;
692 const Region oldCoveredRegion = layer->coveredRegionScreen;
693 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
694 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 }
696 dirty.subtractSelf(aboveOpaqueLayers);
697
698 // accumulate to the screen dirty region
699 dirtyRegion.orSelf(dirty);
700
Mathias Agopianab028732010-03-16 16:41:46 -0700701 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703
704 // Store the visible region is screen space
705 layer->setVisibleRegion(visibleRegion);
706 layer->setCoveredRegion(coveredRegion);
707
Mathias Agopian97011222009-07-28 10:57:27 -0700708 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 if (layer->isSecure() && !visibleRegion.isEmpty()) {
710 secureFrameBuffer = true;
711 }
712 }
713
Mathias Agopian97011222009-07-28 10:57:27 -0700714 // invalidate the areas where a layer was removed
715 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
716 mDirtyRegionRemovedLayer.clear();
717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 mSecureFrameBuffer = secureFrameBuffer;
719 opaqueRegion = aboveOpaqueLayers;
720}
721
722
723void SurfaceFlinger::commitTransaction()
724{
725 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700726 mResizeTransationPending = false;
727 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728}
729
Mathias Agopian22c67842010-11-01 23:32:18 -0700730void SurfaceFlinger::setBypassLayer(const sp<LayerBase>& layer)
731{
732 // if this layer is already the bypass layer, do nothing
733 sp<Layer> cur(mBypassLayer.promote());
Mathias Agopiane24cc7a2010-12-07 21:00:25 -0800734 if (mBypassLayer == layer) {
735 if (cur != NULL) {
736 cur->updateBuffersOrientation();
737 }
Mathias Agopian22c67842010-11-01 23:32:18 -0700738 return;
Mathias Agopiane24cc7a2010-12-07 21:00:25 -0800739 }
Mathias Agopian22c67842010-11-01 23:32:18 -0700740
741 // clear the current bypass layer
742 mBypassLayer.clear();
743 if (cur != 0) {
744 cur->setBypass(false);
745 cur.clear();
746 }
747
748 // set new bypass layer
749 if (layer != 0) {
750 if (layer->setBypass(true)) {
751 mBypassLayer = static_cast<Layer*>(layer.get());
752 }
753 }
754}
755
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756void SurfaceFlinger::handlePageFlip()
757{
758 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700759 LayerVector& currentLayers = const_cast<LayerVector&>(
760 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 visibleRegions |= lockPageFlip(currentLayers);
762
763 const DisplayHardware& hw = graphicPlane(0).displayHardware();
764 const Region screenRegion(hw.bounds());
765 if (visibleRegions) {
766 Region opaqueRegion;
767 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700768
769 /*
770 * rebuild the visible layer list
771 */
772 mVisibleLayersSortedByZ.clear();
773 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
774 size_t count = currentLayers.size();
775 mVisibleLayersSortedByZ.setCapacity(count);
776 for (size_t i=0 ; i<count ; i++) {
777 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
778 mVisibleLayersSortedByZ.add(currentLayers[i]);
779 }
780
Mathias Agopian22c67842010-11-01 23:32:18 -0700781#ifdef USE_COMPOSITION_BYPASS
782 sp<LayerBase> bypassLayer;
783 const size_t numVisibleLayers = mVisibleLayersSortedByZ.size();
784 if (numVisibleLayers == 1) {
785 const sp<LayerBase>& candidate(mVisibleLayersSortedByZ[0]);
786 const Region& visibleRegion(candidate->visibleRegionScreen);
787 const Region reminder(screenRegion.subtract(visibleRegion));
788 if (reminder.isEmpty()) {
789 // fullscreen candidate!
790 bypassLayer = candidate;
791 }
792 }
793 setBypassLayer(bypassLayer);
794#endif
795
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 mWormholeRegion = screenRegion.subtract(opaqueRegion);
797 mVisibleRegionsDirty = false;
798 }
799
800 unlockPageFlip(currentLayers);
801 mDirtyRegion.andSelf(screenRegion);
802}
803
804bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
805{
806 bool recomputeVisibleRegions = false;
807 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700808 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700810 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 layer->lockPageFlip(recomputeVisibleRegions);
812 }
813 return recomputeVisibleRegions;
814}
815
816void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
817{
818 const GraphicPlane& plane(graphicPlane(0));
819 const Transform& planeTransform(plane.transform());
820 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700821 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700823 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824 layer->unlockPageFlip(planeTransform, mDirtyRegion);
825 }
826}
827
Mathias Agopianb8a55602009-06-26 19:06:36 -0700828
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829void SurfaceFlinger::handleRepaint()
830{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700831 // compute the invalid region
832 mInvalidRegion.orSelf(mDirtyRegion);
833 if (mInvalidRegion.isEmpty()) {
834 // nothing to do
835 return;
836 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837
838 if (UNLIKELY(mDebugRegion)) {
839 debugFlashRegions();
840 }
841
Mathias Agopianb8a55602009-06-26 19:06:36 -0700842 // set the frame buffer
843 const DisplayHardware& hw(graphicPlane(0).displayHardware());
844 glMatrixMode(GL_MODELVIEW);
845 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846
847 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700848 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
849 (flags & DisplayHardware::BUFFER_PRESERVED))
850 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700851 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
852 // takes a rectangle, we must make sure to update that whole
853 // rectangle in that case
854 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700855 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 mDirtyRegion.set(mInvalidRegion.bounds());
858 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // in the BUFFER_PRESERVED case, obviously, we can update only
860 // what's needed and nothing more.
861 // NOTE: this is NOT a common case, as preserving the backbuffer
862 // is costly and usually involves copying the whole update back.
863 }
864 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700865 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700866 // We need to redraw the rectangle that will be updated
867 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700868 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700869 // rectangle instead of a region (see DisplayHardware::flip())
870 mDirtyRegion.set(mInvalidRegion.bounds());
871 } else {
872 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 mDirtyRegion.set(hw.bounds());
874 mInvalidRegion = mDirtyRegion;
875 }
876 }
877
878 // compose all surfaces
879 composeSurfaces(mDirtyRegion);
880
881 // clear the dirty regions
882 mDirtyRegion.clear();
883}
884
885void SurfaceFlinger::composeSurfaces(const Region& dirty)
886{
887 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
888 // should never happen unless the window manager has a bug
889 // draw something...
890 drawWormhole();
891 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700892 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700893 const size_t count = layers.size();
894 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian45721772010-08-12 15:03:26 -0700895 const sp<LayerBase>& layer(layers[i]);
896 const Region clip(dirty.intersect(layer->visibleRegionScreen));
897 if (!clip.isEmpty()) {
898 layer->draw(clip);
899 }
900 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901}
902
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903void SurfaceFlinger::debugFlashRegions()
904{
Mathias Agopian0a917752010-06-14 21:20:00 -0700905 const DisplayHardware& hw(graphicPlane(0).displayHardware());
906 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700907
Mathias Agopian0a917752010-06-14 21:20:00 -0700908 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
909 (flags & DisplayHardware::BUFFER_PRESERVED))) {
910 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
911 mDirtyRegion.bounds() : hw.bounds());
912 composeSurfaces(repaint);
913 }
914
915 TextureManager::deactivateTextures();
916
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 glDisable(GL_BLEND);
918 glDisable(GL_DITHER);
919 glDisable(GL_SCISSOR_TEST);
920
Mathias Agopian0926f502009-05-04 14:17:04 -0700921 static int toggle = 0;
922 toggle = 1 - toggle;
923 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700924 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700925 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700926 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700927 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928
Mathias Agopian20f68782009-05-11 00:03:41 -0700929 Region::const_iterator it = mDirtyRegion.begin();
930 Region::const_iterator const end = mDirtyRegion.end();
931 while (it != end) {
932 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 GLfloat vertices[][2] = {
934 { r.left, r.top },
935 { r.left, r.bottom },
936 { r.right, r.bottom },
937 { r.right, r.top }
938 };
939 glVertexPointer(2, GL_FLOAT, 0, vertices);
940 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
941 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700942
Mathias Agopianb8a55602009-06-26 19:06:36 -0700943 if (mInvalidRegion.isEmpty()) {
944 mDirtyRegion.dump("mDirtyRegion");
945 mInvalidRegion.dump("mInvalidRegion");
946 }
947 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948
949 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700950 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951
952 glEnable(GL_SCISSOR_TEST);
953 //mDirtyRegion.dump("mDirtyRegion");
954}
955
956void SurfaceFlinger::drawWormhole() const
957{
958 const Region region(mWormholeRegion.intersect(mDirtyRegion));
959 if (region.isEmpty())
960 return;
961
962 const DisplayHardware& hw(graphicPlane(0).displayHardware());
963 const int32_t width = hw.getWidth();
964 const int32_t height = hw.getHeight();
965
966 glDisable(GL_BLEND);
967 glDisable(GL_DITHER);
968
969 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700970 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700971 Region::const_iterator it = region.begin();
972 Region::const_iterator const end = region.end();
973 while (it != end) {
974 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 const GLint sy = height - (r.top + r.height());
976 glScissor(r.left, sy, r.width(), r.height());
977 glClear(GL_COLOR_BUFFER_BIT);
978 }
979 } else {
980 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
981 { width, height }, { 0, height } };
982 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
983 glVertexPointer(2, GL_SHORT, 0, vertices);
984 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
985 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -0700986#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700987 if (GLExtensions::getInstance().haveTextureExternal()) {
988 glDisable(GL_TEXTURE_EXTERNAL_OES);
989 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700990#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 glEnable(GL_TEXTURE_2D);
992 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
993 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
994 glMatrixMode(GL_TEXTURE);
995 glLoadIdentity();
996 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700997 Region::const_iterator it = region.begin();
998 Region::const_iterator const end = region.end();
999 while (it != end) {
1000 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 const GLint sy = height - (r.top + r.height());
1002 glScissor(r.left, sy, r.width(), r.height());
1003 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1004 }
1005 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1006 }
1007}
1008
1009void SurfaceFlinger::debugShowFPS() const
1010{
1011 static int mFrameCount;
1012 static int mLastFrameCount = 0;
1013 static nsecs_t mLastFpsTime = 0;
1014 static float mFps = 0;
1015 mFrameCount++;
1016 nsecs_t now = systemTime();
1017 nsecs_t diff = now - mLastFpsTime;
1018 if (diff > ms2ns(250)) {
1019 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1020 mLastFpsTime = now;
1021 mLastFrameCount = mFrameCount;
1022 }
1023 // XXX: mFPS has the value we want
1024 }
1025
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001026status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027{
1028 Mutex::Autolock _l(mStateLock);
1029 addLayer_l(layer);
1030 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1031 return NO_ERROR;
1032}
1033
Mathias Agopian96f08192010-06-02 23:28:45 -07001034status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1035{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001036 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001037 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1038}
1039
1040ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1041 const sp<LayerBaseClient>& lbc)
1042{
Mathias Agopian96f08192010-06-02 23:28:45 -07001043 // attach this layer to the client
Mathias Agopian02cf75e2011-05-03 16:21:41 -07001044 size_t name = client->attachLayer(lbc);
1045
1046 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001047
1048 // add this layer to the current state list
1049 addLayer_l(lbc);
1050
Mathias Agopian02cf75e2011-05-03 16:21:41 -07001051 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001052}
1053
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001054status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055{
1056 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001057 status_t err = purgatorizeLayer_l(layer);
1058 if (err == NO_ERROR)
1059 setTransactionFlags(eTransactionNeeded);
1060 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061}
1062
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001063status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001065 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1066 if (lbc != 0) {
Mathias Agopianf7662af2011-03-09 16:13:31 -08001067 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001068 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1070 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 return NO_ERROR;
1073 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001074 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075}
1076
Mathias Agopian9a112062009-04-17 19:36:26 -07001077status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1078{
Mathias Agopianf7662af2011-03-09 16:13:31 -08001079 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001080 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001081
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001082 layerBase->onRemoved();
1083
Mathias Agopian3d579642009-06-04 18:46:21 -07001084 // it's possible that we don't find a layer, because it might
1085 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001086 // from the user because there is a race between Client::destroySurface(),
1087 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001088 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1089}
1090
Mathias Agopian96f08192010-06-02 23:28:45 -07001091status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092{
Mathias Agopian96f08192010-06-02 23:28:45 -07001093 layer->forceVisibilityTransaction();
1094 setTransactionFlags(eTraversalNeeded);
1095 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096}
1097
Mathias Agopian34cd9b72011-05-03 17:04:02 -07001098uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1099{
1100 return android_atomic_release_load(&mTransactionFlags);
1101}
1102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1104{
1105 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1106}
1107
Mathias Agopianbb641242010-05-18 17:06:55 -07001108uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109{
1110 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1111 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001112 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 }
1114 return old;
1115}
1116
1117void SurfaceFlinger::openGlobalTransaction()
1118{
1119 android_atomic_inc(&mTransactionCount);
1120}
1121
1122void SurfaceFlinger::closeGlobalTransaction()
1123{
1124 if (android_atomic_dec(&mTransactionCount) == 1) {
1125 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001126
1127 // if there is a transaction with a resize, wait for it to
1128 // take effect before returning.
1129 Mutex::Autolock _l(mStateLock);
1130 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001131 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1132 if (CC_UNLIKELY(err != NO_ERROR)) {
1133 // just in case something goes wrong in SF, return to the
1134 // called after a few seconds.
1135 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1136 mResizeTransationPending = false;
1137 break;
1138 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001139 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 }
1141}
1142
1143status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1144{
1145 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1146 return BAD_VALUE;
1147
1148 Mutex::Autolock _l(mStateLock);
1149 mCurrentState.freezeDisplay = 1;
1150 setTransactionFlags(eTransactionNeeded);
1151
1152 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001153 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154 return NO_ERROR;
1155}
1156
1157status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1158{
1159 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1160 return BAD_VALUE;
1161
1162 Mutex::Autolock _l(mStateLock);
1163 mCurrentState.freezeDisplay = 0;
1164 setTransactionFlags(eTransactionNeeded);
1165
1166 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001167 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 return NO_ERROR;
1169}
1170
Mathias Agopianc08731e2009-03-27 18:11:38 -07001171int SurfaceFlinger::setOrientation(DisplayID dpy,
1172 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173{
1174 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1175 return BAD_VALUE;
1176
1177 Mutex::Autolock _l(mStateLock);
1178 if (mCurrentState.orientation != orientation) {
1179 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001180 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 mCurrentState.orientation = orientation;
1182 setTransactionFlags(eTransactionNeeded);
1183 mTransactionCV.wait(mStateLock);
1184 } else {
1185 orientation = BAD_VALUE;
1186 }
1187 }
1188 return orientation;
1189}
1190
Mathias Agopian96f08192010-06-02 23:28:45 -07001191sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001192 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1194 uint32_t flags)
1195{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001196 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001198
1199 if (int32_t(w|h) < 0) {
1200 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1201 int(w), int(h));
1202 return surfaceHandle;
1203 }
1204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001206 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 switch (flags & eFXSurfaceMask) {
1208 case eFXSurfaceNormal:
1209 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1213 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 }
1215 break;
1216 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001217 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 break;
1219 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001220 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 break;
1222 }
1223
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001224 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001225 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001226 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001227 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001230 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001232 params->identity = surfaceHandle->getIdentity();
1233 params->width = w;
1234 params->height = h;
1235 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001236 if (normalLayer != 0) {
1237 Mutex::Autolock _l(mStateLock);
1238 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1239 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001240 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001241
Mathias Agopian96f08192010-06-02 23:28:45 -07001242 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 }
1244
1245 return surfaceHandle;
1246}
1247
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001248sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001249 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001250 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001251 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252{
1253 // initialize the surfaces
1254 switch (format) { // TODO: take h/w into account
1255 case PIXEL_FORMAT_TRANSPARENT:
1256 case PIXEL_FORMAT_TRANSLUCENT:
1257 format = PIXEL_FORMAT_RGBA_8888;
1258 break;
1259 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001260#ifdef NO_RGBX_8888
1261 format = PIXEL_FORMAT_RGB_565;
1262#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001263 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001264#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 break;
1266 }
1267
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001268#ifdef NO_RGBX_8888
1269 if (format == PIXEL_FORMAT_RGBX_8888)
1270 format = PIXEL_FORMAT_RGBA_8888;
1271#endif
1272
Mathias Agopian96f08192010-06-02 23:28:45 -07001273 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001274 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001275 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001277 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 }
1279 return layer;
1280}
1281
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001282sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001283 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001284 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285{
Mathias Agopian96f08192010-06-02 23:28:45 -07001286 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 return layer;
1289}
1290
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001291sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001292 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001293 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294{
Mathias Agopian96f08192010-06-02 23:28:45 -07001295 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 return layer;
1298}
1299
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001300sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001301 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001302 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303{
Mathias Agopian96f08192010-06-02 23:28:45 -07001304 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 return layer;
1307}
1308
Mathias Agopian96f08192010-06-02 23:28:45 -07001309status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310{
Mathias Agopian9a112062009-04-17 19:36:26 -07001311 /*
1312 * called by the window manager, when a surface should be marked for
1313 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001314 *
1315 * The surface is removed from the current and drawing lists, but placed
1316 * in the purgatory queue, so it's not destroyed right-away (we need
1317 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001318 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001319
Mathias Agopian48d819a2009-09-10 19:41:18 -07001320 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001321 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001323 if (layer != 0) {
1324 err = purgatorizeLayer_l(layer);
1325 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001326 setTransactionFlags(eTransactionNeeded);
1327 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001328 }
1329 return err;
1330}
1331
1332status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1333{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001334 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001335
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001336 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001337 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001338 sp<LayerBaseClient> layer;
1339 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 MessageDestroySurface(
1341 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1342 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001344 sp<LayerBaseClient> l(layer);
1345 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001346 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001347 /*
1348 * remove the layer from the current list -- chances are that it's
1349 * not in the list anyway, because it should have been removed
1350 * already upon request of the client (eg: window manager).
1351 * However, a buggy client could have not done that.
1352 * Since we know we don't have any more clients, we don't need
1353 * to use the purgatory.
1354 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001355 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001356 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1357 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001358 return true;
1359 }
1360 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001361
Mathias Agopianbb641242010-05-18 17:06:55 -07001362 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 return NO_ERROR;
1364}
1365
1366status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001367 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 int32_t count,
1369 const layer_state_t* states)
1370{
1371 Mutex::Autolock _l(mStateLock);
1372 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001374 const layer_state_t& s(states[i]);
1375 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001376 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 if (what & ePositionChanged) {
1379 if (layer->setPosition(s.x, s.y))
1380 flags |= eTraversalNeeded;
1381 }
1382 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001383 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001385 mCurrentState.layersSortedByZ.removeAt(idx);
1386 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 // we need traversal (state changed)
1388 // AND transaction (list changed)
1389 flags |= eTransactionNeeded|eTraversalNeeded;
1390 }
1391 }
1392 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001393 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001395 mResizeTransationPending = true;
1396 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 }
1398 if (what & eAlphaChanged) {
1399 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eMatrixChanged) {
1403 if (layer->setMatrix(s.matrix))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eTransparentRegionChanged) {
1407 if (layer->setTransparentRegionHint(s.transparentRegion))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eVisibilityChanged) {
1411 if (layer->setFlags(s.flags, s.mask))
1412 flags |= eTraversalNeeded;
1413 }
1414 }
1415 }
1416 if (flags) {
1417 setTransactionFlags(flags);
1418 }
1419 return NO_ERROR;
1420}
1421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422void SurfaceFlinger::screenReleased(int dpy)
1423{
1424 // this may be called by a signal handler, we can't do too much in here
1425 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1426 signalEvent();
1427}
1428
1429void SurfaceFlinger::screenAcquired(int dpy)
1430{
1431 // this may be called by a signal handler, we can't do too much in here
1432 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1433 signalEvent();
1434}
1435
1436status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1437{
1438 const size_t SIZE = 1024;
1439 char buffer[SIZE];
1440 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001441 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 snprintf(buffer, SIZE, "Permission Denial: "
1443 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1444 IPCThreadState::self()->getCallingPid(),
1445 IPCThreadState::self()->getCallingUid());
1446 result.append(buffer);
1447 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001448
1449 // figure out if we're stuck somewhere
1450 const nsecs_t now = systemTime();
1451 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1452 const nsecs_t inTransaction(mDebugInTransaction);
1453 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1454 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1455
1456 // Try to get the main lock, but don't insist if we can't
1457 // (this would indicate SF is stuck, but we want to be able to
1458 // print something in dumpsys).
1459 int retry = 3;
1460 while (mStateLock.tryLock()<0 && --retry>=0) {
1461 usleep(1000000);
1462 }
1463 const bool locked(retry >= 0);
1464 if (!locked) {
1465 snprintf(buffer, SIZE,
1466 "SurfaceFlinger appears to be unresponsive, "
1467 "dumping anyways (no locks held)\n");
1468 result.append(buffer);
1469 }
1470
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1472 const size_t count = currentLayers.size();
1473 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001474 const sp<LayerBase>& layer(currentLayers[i]);
1475 layer->dump(result, buffer, SIZE);
1476 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 s.transparentRegion.dump(result, "transparentRegion");
1478 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1479 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1480 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001481
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 mWormholeRegion.dump(result, "WormholeRegion");
1483 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1484 snprintf(buffer, SIZE,
Mathias Agopian22c67842010-11-01 23:32:18 -07001485 " display frozen: %s, freezeCount=%d, orientation=%d, bypass=%p, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 mFreezeDisplay?"yes":"no", mFreezeCount,
Mathias Agopian22c67842010-11-01 23:32:18 -07001487 mCurrentState.orientation, mBypassLayer.unsafe_get(), hw.canDraw());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001489 snprintf(buffer, SIZE,
1490 " last eglSwapBuffers() time: %f us\n"
1491 " last transaction time : %f us\n",
1492 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1493 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001494
Mathias Agopian9795c422009-08-26 16:36:26 -07001495 if (inSwapBuffersDuration || !locked) {
1496 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1497 inSwapBuffersDuration/1000.0);
1498 result.append(buffer);
1499 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001500
Mathias Agopian9795c422009-08-26 16:36:26 -07001501 if (inTransactionDuration || !locked) {
1502 snprintf(buffer, SIZE, " transaction time: %f us\n",
1503 inTransactionDuration/1000.0);
1504 result.append(buffer);
1505 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001506
Mathias Agopian3330b202009-10-05 17:07:12 -07001507 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001508 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001509
1510 if (locked) {
1511 mStateLock.unlock();
1512 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 }
1514 write(fd, result.string(), result.size());
1515 return NO_ERROR;
1516}
1517
1518status_t SurfaceFlinger::onTransact(
1519 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1520{
1521 switch (code) {
1522 case CREATE_CONNECTION:
1523 case OPEN_GLOBAL_TRANSACTION:
1524 case CLOSE_GLOBAL_TRANSACTION:
1525 case SET_ORIENTATION:
1526 case FREEZE_DISPLAY:
1527 case UNFREEZE_DISPLAY:
1528 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001529 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001530 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 {
1532 // codes that require permission check
1533 IPCThreadState* ipc = IPCThreadState::self();
1534 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001535 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001536 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1537 LOGE("Permission Denial: "
1538 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1539 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001541 break;
1542 }
1543 case CAPTURE_SCREEN:
1544 {
1545 // codes that require permission check
1546 IPCThreadState* ipc = IPCThreadState::self();
1547 const int pid = ipc->getCallingPid();
1548 const int uid = ipc->getCallingUid();
1549 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1550 LOGE("Permission Denial: "
1551 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1552 return PERMISSION_DENIED;
1553 }
1554 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555 }
1556 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1559 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001560 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001561 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1562 IPCThreadState* ipc = IPCThreadState::self();
1563 const int pid = ipc->getCallingPid();
1564 const int uid = ipc->getCallingUid();
1565 LOGE("Permission Denial: "
1566 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 return PERMISSION_DENIED;
1568 }
1569 int n;
1570 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001571 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001572 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 return NO_ERROR;
1574 case 1002: // SHOW_UPDATES
1575 n = data.readInt32();
1576 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1577 return NO_ERROR;
1578 case 1003: // SHOW_BACKGROUND
1579 n = data.readInt32();
1580 mDebugBackground = n ? 1 : 0;
1581 return NO_ERROR;
1582 case 1004:{ // repaint everything
1583 Mutex::Autolock _l(mStateLock);
1584 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1585 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1586 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001587 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001589 case 1005:{ // force transaction
1590 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1591 return NO_ERROR;
1592 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001593 case 1006:{ // enable/disable GraphicLog
1594 int enabled = data.readInt32();
1595 GraphicLog::getInstance().setEnabled(enabled);
1596 return NO_ERROR;
1597 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 case 1007: // set mFreezeCount
1599 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001600 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601 return NO_ERROR;
1602 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001603 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 reply->writeInt32(0);
1605 reply->writeInt32(mDebugRegion);
1606 reply->writeInt32(mDebugBackground);
1607 return NO_ERROR;
1608 case 1013: {
1609 Mutex::Autolock _l(mStateLock);
1610 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1611 reply->writeInt32(hw.getPageFlipCount());
1612 }
1613 return NO_ERROR;
1614 }
1615 }
1616 return err;
1617}
1618
Mathias Agopian59119e62010-10-11 12:37:43 -07001619// ---------------------------------------------------------------------------
1620
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001621status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1622 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001623{
Mathias Agopian59119e62010-10-11 12:37:43 -07001624 if (!GLExtensions::getInstance().haveFramebufferObject())
1625 return INVALID_OPERATION;
1626
1627 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001628 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001629 const uint32_t hw_w = hw.getWidth();
1630 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001631 GLfloat u = 1;
1632 GLfloat v = 1;
1633
1634 // make sure to clear all GL error flags
1635 while ( glGetError() != GL_NO_ERROR ) ;
1636
1637 // create a FBO
1638 GLuint name, tname;
1639 glGenTextures(1, &tname);
1640 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001641 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1642 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001643 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001644 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001645 GLint tw = (2 << (31 - clz(hw_w)));
1646 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001647 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1648 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001649 u = GLfloat(hw_w) / tw;
1650 v = GLfloat(hw_h) / th;
1651 }
1652 glGenFramebuffersOES(1, &name);
1653 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001654 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1655 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001656
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001657 // redraw the screen entirely...
1658 glClearColor(0,0,0,1);
1659 glClear(GL_COLOR_BUFFER_BIT);
1660 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1661 const size_t count = layers.size();
1662 for (size_t i=0 ; i<count ; ++i) {
1663 const sp<LayerBase>& layer(layers[i]);
1664 layer->drawForSreenShot();
1665 }
1666
1667 // back to main framebuffer
1668 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1669 glDisable(GL_SCISSOR_TEST);
1670 glDeleteFramebuffersOES(1, &name);
1671
1672 *textureName = tname;
1673 *uOut = u;
1674 *vOut = v;
1675 return NO_ERROR;
1676}
1677
1678// ---------------------------------------------------------------------------
1679
1680status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1681{
1682 status_t result = PERMISSION_DENIED;
1683
1684 if (!GLExtensions::getInstance().haveFramebufferObject())
1685 return INVALID_OPERATION;
1686
1687 // get screen geometry
1688 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1689 const uint32_t hw_w = hw.getWidth();
1690 const uint32_t hw_h = hw.getHeight();
1691 const Region screenBounds(hw.bounds());
1692
1693 GLfloat u, v;
1694 GLuint tname;
1695 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1696 if (result != NO_ERROR) {
1697 return result;
1698 }
1699
1700 GLfloat vtx[8];
1701 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1702 glEnable(GL_TEXTURE_2D);
1703 glBindTexture(GL_TEXTURE_2D, tname);
1704 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1705 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1706 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1707 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1708 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1709 glVertexPointer(2, GL_FLOAT, 0, vtx);
1710
1711 class s_curve_interpolator {
1712 const float nbFrames, s, v;
1713 public:
1714 s_curve_interpolator(int nbFrames, float s)
1715 : nbFrames(1.0f / (nbFrames-1)), s(s),
1716 v(1.0f + expf(-s + 0.5f*s)) {
1717 }
1718 float operator()(int f) {
1719 const float x = f * nbFrames;
1720 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1721 }
1722 };
1723
1724 class v_stretch {
1725 const GLfloat hw_w, hw_h;
1726 public:
1727 v_stretch(uint32_t hw_w, uint32_t hw_h)
1728 : hw_w(hw_w), hw_h(hw_h) {
1729 }
1730 void operator()(GLfloat* vtx, float v) {
1731 const GLfloat w = hw_w + (hw_w * v);
1732 const GLfloat h = hw_h - (hw_h * v);
1733 const GLfloat x = (hw_w - w) * 0.5f;
1734 const GLfloat y = (hw_h - h) * 0.5f;
1735 vtx[0] = x; vtx[1] = y;
1736 vtx[2] = x; vtx[3] = y + h;
1737 vtx[4] = x + w; vtx[5] = y + h;
1738 vtx[6] = x + w; vtx[7] = y;
1739 }
1740 };
1741
1742 class h_stretch {
1743 const GLfloat hw_w, hw_h;
1744 public:
1745 h_stretch(uint32_t hw_w, uint32_t hw_h)
1746 : hw_w(hw_w), hw_h(hw_h) {
1747 }
1748 void operator()(GLfloat* vtx, float v) {
1749 const GLfloat w = hw_w - (hw_w * v);
1750 const GLfloat h = 1.0f;
1751 const GLfloat x = (hw_w - w) * 0.5f;
1752 const GLfloat y = (hw_h - h) * 0.5f;
1753 vtx[0] = x; vtx[1] = y;
1754 vtx[2] = x; vtx[3] = y + h;
1755 vtx[4] = x + w; vtx[5] = y + h;
1756 vtx[6] = x + w; vtx[7] = y;
1757 }
1758 };
1759
1760 // the full animation is 24 frames
1761 const int nbFrames = 12;
1762 s_curve_interpolator itr(nbFrames, 7.5f);
1763 s_curve_interpolator itg(nbFrames, 8.0f);
1764 s_curve_interpolator itb(nbFrames, 8.5f);
1765
1766 v_stretch vverts(hw_w, hw_h);
1767 glEnable(GL_BLEND);
1768 glBlendFunc(GL_ONE, GL_ONE);
1769 for (int i=0 ; i<nbFrames ; i++) {
1770 float x, y, w, h;
1771 const float vr = itr(i);
1772 const float vg = itg(i);
1773 const float vb = itb(i);
1774
1775 // clear screen
1776 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001777 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001778 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001779
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001780 // draw the red plane
1781 vverts(vtx, vr);
1782 glColorMask(1,0,0,1);
1783 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001784
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001785 // draw the green plane
1786 vverts(vtx, vg);
1787 glColorMask(0,1,0,1);
1788 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001789
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001790 // draw the blue plane
1791 vverts(vtx, vb);
1792 glColorMask(0,0,1,1);
1793 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001794
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001795 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001796 glDisable(GL_TEXTURE_2D);
1797 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001798 glColor4f(vg, vg, vg, 1);
1799 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1800 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001801 }
1802
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001803 h_stretch hverts(hw_w, hw_h);
1804 glDisable(GL_BLEND);
1805 glDisable(GL_TEXTURE_2D);
1806 glColorMask(1,1,1,1);
1807 for (int i=0 ; i<nbFrames ; i++) {
1808 const float v = itg(i);
1809 hverts(vtx, v);
1810 glClear(GL_COLOR_BUFFER_BIT);
1811 glColor4f(1-v, 1-v, 1-v, 1);
1812 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1813 hw.flip(screenBounds);
1814 }
1815
1816 glColorMask(1,1,1,1);
1817 glEnable(GL_SCISSOR_TEST);
1818 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1819 glDeleteTextures(1, &tname);
1820 return NO_ERROR;
1821}
1822
1823status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1824{
1825 status_t result = PERMISSION_DENIED;
1826
1827 if (!GLExtensions::getInstance().haveFramebufferObject())
1828 return INVALID_OPERATION;
1829
1830
1831 // get screen geometry
1832 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1833 const uint32_t hw_w = hw.getWidth();
1834 const uint32_t hw_h = hw.getHeight();
1835 const Region screenBounds(hw.bounds());
1836
1837 GLfloat u, v;
1838 GLuint tname;
1839 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1840 if (result != NO_ERROR) {
1841 return result;
1842 }
1843
1844 // back to main framebuffer
1845 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1846 glDisable(GL_SCISSOR_TEST);
1847
1848 GLfloat vtx[8];
1849 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1850 glEnable(GL_TEXTURE_2D);
1851 glBindTexture(GL_TEXTURE_2D, tname);
1852 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1853 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1854 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1855 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1856 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1857 glVertexPointer(2, GL_FLOAT, 0, vtx);
1858
1859 class s_curve_interpolator {
1860 const float nbFrames, s, v;
1861 public:
1862 s_curve_interpolator(int nbFrames, float s)
1863 : nbFrames(1.0f / (nbFrames-1)), s(s),
1864 v(1.0f + expf(-s + 0.5f*s)) {
1865 }
1866 float operator()(int f) {
1867 const float x = f * nbFrames;
1868 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1869 }
1870 };
1871
1872 class v_stretch {
1873 const GLfloat hw_w, hw_h;
1874 public:
1875 v_stretch(uint32_t hw_w, uint32_t hw_h)
1876 : hw_w(hw_w), hw_h(hw_h) {
1877 }
1878 void operator()(GLfloat* vtx, float v) {
1879 const GLfloat w = hw_w + (hw_w * v);
1880 const GLfloat h = hw_h - (hw_h * v);
1881 const GLfloat x = (hw_w - w) * 0.5f;
1882 const GLfloat y = (hw_h - h) * 0.5f;
1883 vtx[0] = x; vtx[1] = y;
1884 vtx[2] = x; vtx[3] = y + h;
1885 vtx[4] = x + w; vtx[5] = y + h;
1886 vtx[6] = x + w; vtx[7] = y;
1887 }
1888 };
1889
1890 class h_stretch {
1891 const GLfloat hw_w, hw_h;
1892 public:
1893 h_stretch(uint32_t hw_w, uint32_t hw_h)
1894 : hw_w(hw_w), hw_h(hw_h) {
1895 }
1896 void operator()(GLfloat* vtx, float v) {
1897 const GLfloat w = hw_w - (hw_w * v);
1898 const GLfloat h = 1.0f;
1899 const GLfloat x = (hw_w - w) * 0.5f;
1900 const GLfloat y = (hw_h - h) * 0.5f;
1901 vtx[0] = x; vtx[1] = y;
1902 vtx[2] = x; vtx[3] = y + h;
1903 vtx[4] = x + w; vtx[5] = y + h;
1904 vtx[6] = x + w; vtx[7] = y;
1905 }
1906 };
1907
Mathias Agopiana6546e52010-10-14 12:33:07 -07001908 // the full animation is 12 frames
1909 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001910 s_curve_interpolator itr(nbFrames, 7.5f);
1911 s_curve_interpolator itg(nbFrames, 8.0f);
1912 s_curve_interpolator itb(nbFrames, 8.5f);
1913
1914 h_stretch hverts(hw_w, hw_h);
1915 glDisable(GL_BLEND);
1916 glDisable(GL_TEXTURE_2D);
1917 glColorMask(1,1,1,1);
1918 for (int i=nbFrames-1 ; i>=0 ; i--) {
1919 const float v = itg(i);
1920 hverts(vtx, v);
1921 glClear(GL_COLOR_BUFFER_BIT);
1922 glColor4f(1-v, 1-v, 1-v, 1);
1923 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1924 hw.flip(screenBounds);
1925 }
1926
Mathias Agopiana6546e52010-10-14 12:33:07 -07001927 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001928 v_stretch vverts(hw_w, hw_h);
1929 glEnable(GL_BLEND);
1930 glBlendFunc(GL_ONE, GL_ONE);
1931 for (int i=nbFrames-1 ; i>=0 ; i--) {
1932 float x, y, w, h;
1933 const float vr = itr(i);
1934 const float vg = itg(i);
1935 const float vb = itb(i);
1936
1937 // clear screen
1938 glColorMask(1,1,1,1);
1939 glClear(GL_COLOR_BUFFER_BIT);
1940 glEnable(GL_TEXTURE_2D);
1941
1942 // draw the red plane
1943 vverts(vtx, vr);
1944 glColorMask(1,0,0,1);
1945 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1946
1947 // draw the green plane
1948 vverts(vtx, vg);
1949 glColorMask(0,1,0,1);
1950 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1951
1952 // draw the blue plane
1953 vverts(vtx, vb);
1954 glColorMask(0,0,1,1);
1955 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1956
1957 hw.flip(screenBounds);
1958 }
1959
1960 glColorMask(1,1,1,1);
1961 glEnable(GL_SCISSOR_TEST);
1962 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07001963 glDeleteTextures(1, &tname);
1964
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001965 return NO_ERROR;
1966}
1967
1968// ---------------------------------------------------------------------------
1969
Mathias Agopianabd671a2010-10-14 14:54:06 -07001970status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971{
1972 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1973 if (!hw.canDraw()) {
1974 // we're already off
1975 return NO_ERROR;
1976 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07001977 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
1978 electronBeamOffAnimationImplLocked();
1979 }
1980
1981 // always clear the whole screen at the end of the animation
1982 glClearColor(0,0,0,1);
1983 glDisable(GL_SCISSOR_TEST);
1984 glClear(GL_COLOR_BUFFER_BIT);
1985 glEnable(GL_SCISSOR_TEST);
1986 hw.flip( Region(hw.bounds()) );
1987
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001988 hw.setCanDraw(false);
1989 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07001990}
1991
1992status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
1993{
Mathias Agopian59119e62010-10-11 12:37:43 -07001994 class MessageTurnElectronBeamOff : public MessageBase {
1995 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07001996 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07001997 status_t result;
1998 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07001999 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2000 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002001 }
2002 status_t getResult() const {
2003 return result;
2004 }
2005 virtual bool handler() {
2006 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002007 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002008 return true;
2009 }
2010 };
2011
Mathias Agopianabd671a2010-10-14 14:54:06 -07002012 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002013 status_t res = postMessageSync(msg);
2014 if (res == NO_ERROR) {
2015 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002016
2017 // work-around: when the power-manager calls us we activate the
2018 // animation. eventually, the "on" animation will be called
2019 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002020 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002021 }
2022 return res;
2023}
2024
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002026
Mathias Agopianabd671a2010-10-14 14:54:06 -07002027status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002028{
2029 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2030 if (hw.canDraw()) {
2031 // we're already on
2032 return NO_ERROR;
2033 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002034 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2035 electronBeamOnAnimationImplLocked();
2036 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002037 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002038
2039 // make sure to redraw the whole screen when the animation is done
2040 mDirtyRegion.set(hw.bounds());
2041 signalEvent();
2042
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002043 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044}
2045
2046status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2047{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002048 class MessageTurnElectronBeamOn : public MessageBase {
2049 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002050 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 status_t result;
2052 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002053 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2054 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002055 }
2056 status_t getResult() const {
2057 return result;
2058 }
2059 virtual bool handler() {
2060 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002061 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002062 return true;
2063 }
2064 };
2065
Mathias Agopianabd671a2010-10-14 14:54:06 -07002066 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002067 return NO_ERROR;
2068}
2069
2070// ---------------------------------------------------------------------------
2071
Mathias Agopiandf85c452010-09-29 13:02:36 -07002072status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2073 sp<IMemoryHeap>* heap,
2074 uint32_t* w, uint32_t* h, PixelFormat* f,
2075 uint32_t sw, uint32_t sh)
2076{
2077 status_t result = PERMISSION_DENIED;
2078
2079 // only one display supported for now
2080 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2081 return BAD_VALUE;
2082
2083 if (!GLExtensions::getInstance().haveFramebufferObject())
2084 return INVALID_OPERATION;
2085
2086 // get screen geometry
2087 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2088 const uint32_t hw_w = hw.getWidth();
2089 const uint32_t hw_h = hw.getHeight();
2090
2091 if ((sw > hw_w) || (sh > hw_h))
2092 return BAD_VALUE;
2093
2094 sw = (!sw) ? hw_w : sw;
2095 sh = (!sh) ? hw_h : sh;
2096 const size_t size = sw * sh * 4;
2097
2098 // make sure to clear all GL error flags
2099 while ( glGetError() != GL_NO_ERROR ) ;
2100
2101 // create a FBO
2102 GLuint name, tname;
2103 glGenRenderbuffersOES(1, &tname);
2104 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2105 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2106 glGenFramebuffersOES(1, &name);
2107 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2108 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2109 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2110
2111 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2112 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2113
2114 // invert everything, b/c glReadPixel() below will invert the FB
2115 glViewport(0, 0, sw, sh);
Mathias Agopiancfb676f2010-12-16 18:46:17 -08002116 glScissor(0, 0, sw, sh);
Mathias Agopiandf85c452010-09-29 13:02:36 -07002117 glMatrixMode(GL_PROJECTION);
2118 glPushMatrix();
2119 glLoadIdentity();
2120 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2121 glMatrixMode(GL_MODELVIEW);
2122
2123 // redraw the screen entirely...
2124 glClearColor(0,0,0,1);
2125 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiancfb676f2010-12-16 18:46:17 -08002126
Mathias Agopiandf85c452010-09-29 13:02:36 -07002127 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2128 const size_t count = layers.size();
2129 for (size_t i=0 ; i<count ; ++i) {
2130 const sp<LayerBase>& layer(layers[i]);
2131 layer->drawForSreenShot();
2132 }
2133
2134 // XXX: this is needed on tegra
2135 glScissor(0, 0, sw, sh);
2136
2137 // check for errors and return screen capture
2138 if (glGetError() != GL_NO_ERROR) {
2139 // error while rendering
2140 result = INVALID_OPERATION;
2141 } else {
2142 // allocate shared memory large enough to hold the
2143 // screen capture
2144 sp<MemoryHeapBase> base(
2145 new MemoryHeapBase(size, 0, "screen-capture") );
2146 void* const ptr = base->getBase();
2147 if (ptr) {
2148 // capture the screen with glReadPixels()
2149 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2150 if (glGetError() == GL_NO_ERROR) {
2151 *heap = base;
2152 *w = sw;
2153 *h = sh;
2154 *f = PIXEL_FORMAT_RGBA_8888;
2155 result = NO_ERROR;
2156 }
2157 } else {
2158 result = NO_MEMORY;
2159 }
2160 }
Mathias Agopiandf85c452010-09-29 13:02:36 -07002161 glEnable(GL_SCISSOR_TEST);
2162 glViewport(0, 0, hw_w, hw_h);
2163 glMatrixMode(GL_PROJECTION);
2164 glPopMatrix();
2165 glMatrixMode(GL_MODELVIEW);
2166
2167
2168 } else {
2169 result = BAD_VALUE;
2170 }
2171
2172 // release FBO resources
2173 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2174 glDeleteRenderbuffersOES(1, &tname);
2175 glDeleteFramebuffersOES(1, &name);
Mathias Agopian5dc7e7d2010-12-15 14:41:59 -08002176
2177 hw.compositionComplete();
2178
Mathias Agopiandf85c452010-09-29 13:02:36 -07002179 return result;
2180}
2181
2182
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002183status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2184 sp<IMemoryHeap>* heap,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002185 uint32_t* width, uint32_t* height, PixelFormat* format,
2186 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002187{
2188 // only one display supported for now
2189 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2190 return BAD_VALUE;
2191
2192 if (!GLExtensions::getInstance().haveFramebufferObject())
2193 return INVALID_OPERATION;
2194
2195 class MessageCaptureScreen : public MessageBase {
2196 SurfaceFlinger* flinger;
2197 DisplayID dpy;
2198 sp<IMemoryHeap>* heap;
2199 uint32_t* w;
2200 uint32_t* h;
2201 PixelFormat* f;
Mathias Agopiandf85c452010-09-29 13:02:36 -07002202 uint32_t sw;
2203 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002204 status_t result;
2205 public:
2206 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002207 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
2208 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002209 : flinger(flinger), dpy(dpy),
Mathias Agopiandf85c452010-09-29 13:02:36 -07002210 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002211 {
2212 }
2213 status_t getResult() const {
2214 return result;
2215 }
2216 virtual bool handler() {
2217 Mutex::Autolock _l(flinger->mStateLock);
2218
2219 // if we have secure windows, never allow the screen capture
2220 if (flinger->mSecureFrameBuffer)
2221 return true;
2222
Mathias Agopiandf85c452010-09-29 13:02:36 -07002223 result = flinger->captureScreenImplLocked(dpy,
2224 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002225
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002226 return true;
2227 }
2228 };
2229
2230 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002231 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002232 status_t res = postMessageSync(msg);
2233 if (res == NO_ERROR) {
2234 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2235 }
2236 return res;
2237}
2238
2239// ---------------------------------------------------------------------------
2240
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002241sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2242{
2243 sp<Layer> result;
2244 Mutex::Autolock _l(mStateLock);
2245 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2246 return result;
2247}
2248
2249// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002250
Mathias Agopian96f08192010-06-02 23:28:45 -07002251Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002252 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002253{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002254}
2255
Mathias Agopian96f08192010-06-02 23:28:45 -07002256Client::~Client()
2257{
Mathias Agopian96f08192010-06-02 23:28:45 -07002258 const size_t count = mLayers.size();
2259 for (size_t i=0 ; i<count ; i++) {
2260 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2261 if (layer != 0) {
2262 mFlinger->removeLayer(layer);
2263 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002264 }
2265}
2266
Mathias Agopian96f08192010-06-02 23:28:45 -07002267status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002268 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002269}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002270
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002271size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002272{
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002273 Mutex::Autolock _l(mLock);
2274 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002275 mLayers.add(name, layer);
2276 return name;
2277}
2278
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002279void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002280{
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002281 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002282 // we do a linear search here, because this doesn't happen often
2283 const size_t count = mLayers.size();
2284 for (size_t i=0 ; i<count ; i++) {
2285 if (mLayers.valueAt(i) == layer) {
2286 mLayers.removeItemsAt(i, 1);
2287 break;
2288 }
2289 }
2290}
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002291sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2292{
2293 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002294 sp<LayerBaseClient> lbc;
Mathias Agopian02cf75e2011-05-03 16:21:41 -07002295 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002296 if (layer != 0) {
2297 lbc = layer.promote();
2298 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002299 }
2300 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002301}
2302
Mathias Agopian96f08192010-06-02 23:28:45 -07002303sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002304 return 0;
2305}
2306ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2307 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308}
Mathias Agopian96f08192010-06-02 23:28:45 -07002309sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002310 ISurfaceComposerClient::surface_data_t* params, int pid,
2311 const String8& name,
2312 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002313 uint32_t flags)
2314{
Mathias Agopian96f08192010-06-02 23:28:45 -07002315 return mFlinger->createSurface(this, pid, name, params,
2316 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002317}
Mathias Agopian96f08192010-06-02 23:28:45 -07002318status_t Client::destroySurface(SurfaceID sid) {
2319 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002320}
Mathias Agopian96f08192010-06-02 23:28:45 -07002321status_t Client::setState(int32_t count, const layer_state_t* states) {
2322 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002323}
2324
2325// ---------------------------------------------------------------------------
2326
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002327UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2328 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2329{
2330 const int pgsize = getpagesize();
2331 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2332
2333 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2334 "SurfaceFlinger Client control-block");
2335
2336 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2337 if (ctrlblk) { // construct the shared structure in-place.
2338 new(ctrlblk) SharedClient;
2339 }
2340}
2341
2342UserClient::~UserClient()
2343{
2344 if (ctrlblk) {
2345 ctrlblk->~SharedClient(); // destroy our shared-structure.
2346 }
2347
2348 /*
2349 * When a UserClient dies, it's unclear what to do exactly.
2350 * We could go ahead and destroy all surfaces linked to that client
2351 * however, it wouldn't be fair to the main Client
2352 * (usually the the window-manager), which might want to re-target
2353 * the layer to another UserClient.
2354 * I think the best is to do nothing, or not much; in most cases the
2355 * WM itself will go ahead and clean things up when it detects a client of
2356 * his has died.
2357 * The remaining question is what to display? currently we keep
2358 * just keep the current buffer.
2359 */
2360}
2361
2362status_t UserClient::initCheck() const {
2363 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2364}
2365
2366void UserClient::detachLayer(const Layer* layer)
2367{
2368 int32_t name = layer->getToken();
2369 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002370 int32_t mask = 1LU<<name;
2371 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2372 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2373 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002374 }
2375}
2376
2377sp<IMemoryHeap> UserClient::getControlBlock() const {
2378 return mCblkHeap;
2379}
2380
2381ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2382{
2383 int32_t name = NAME_NOT_FOUND;
2384 sp<Layer> layer(mFlinger->getLayer(sur));
2385 if (layer == 0) return name;
2386
Mathias Agopian579b3f82010-06-08 19:54:15 -07002387 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002388 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07002389 if ((name >= 0) && (layer->getClient() == this))
2390 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002391
2392 name = 0;
2393 do {
2394 int32_t mask = 1LU<<name;
2395 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2396 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002397 status_t err = layer->setToken(
2398 const_cast<UserClient*>(this), ctrlblk, name);
2399 if (err != NO_ERROR) {
2400 // free the name
2401 android_atomic_and(~mask, &mBitmap);
2402 name = err;
2403 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002404 break;
2405 }
Jamie Gennis8a083922011-02-10 16:18:36 -08002406 if (++name >= SharedBufferStack::NUM_LAYERS_MAX)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002407 name = NO_MEMORY;
2408 } while(name >= 0);
2409
Mathias Agopian53503a92010-06-08 15:40:56 -07002410 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2411 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002412 return name;
2413}
2414
2415sp<ISurface> UserClient::createSurface(
2416 ISurfaceComposerClient::surface_data_t* params, int pid,
2417 const String8& name,
2418 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2419 uint32_t flags) {
2420 return 0;
2421}
2422status_t UserClient::destroySurface(SurfaceID sid) {
2423 return INVALID_OPERATION;
2424}
2425status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2426 return INVALID_OPERATION;
2427}
2428
2429// ---------------------------------------------------------------------------
2430
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002431GraphicPlane::GraphicPlane()
2432 : mHw(0)
2433{
2434}
2435
2436GraphicPlane::~GraphicPlane() {
2437 delete mHw;
2438}
2439
2440bool GraphicPlane::initialized() const {
2441 return mHw ? true : false;
2442}
2443
Mathias Agopian2b92d892010-02-08 15:49:35 -08002444int GraphicPlane::getWidth() const {
2445 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002446}
2447
Mathias Agopian2b92d892010-02-08 15:49:35 -08002448int GraphicPlane::getHeight() const {
2449 return mHeight;
2450}
2451
2452void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2453{
2454 mHw = hw;
2455
2456 // initialize the display orientation transform.
2457 // it's a constant that should come from the display driver.
2458 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2459 char property[PROPERTY_VALUE_MAX];
2460 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2461 //displayOrientation
2462 switch (atoi(property)) {
2463 case 90:
2464 displayOrientation = ISurfaceComposer::eOrientation90;
2465 break;
2466 case 270:
2467 displayOrientation = ISurfaceComposer::eOrientation270;
2468 break;
2469 }
2470 }
2471
2472 const float w = hw->getWidth();
2473 const float h = hw->getHeight();
2474 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2475 &mDisplayTransform);
2476 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2477 mDisplayWidth = h;
2478 mDisplayHeight = w;
2479 } else {
2480 mDisplayWidth = w;
2481 mDisplayHeight = h;
2482 }
2483
2484 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485}
2486
2487status_t GraphicPlane::orientationToTransfrom(
2488 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002489{
2490 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 switch (orientation) {
2492 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002493 flags = Transform::ROT_0;
2494 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002495 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002496 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497 break;
2498 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002499 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500 break;
2501 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002502 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002503 break;
2504 default:
2505 return BAD_VALUE;
2506 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002507 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 return NO_ERROR;
2509}
2510
2511status_t GraphicPlane::setOrientation(int orientation)
2512{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002513 // If the rotation can be handled in hardware, this is where
2514 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002515
2516 const DisplayHardware& hw(displayHardware());
2517 const float w = mDisplayWidth;
2518 const float h = mDisplayHeight;
2519 mWidth = int(w);
2520 mHeight = int(h);
2521
2522 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002523 GraphicPlane::orientationToTransfrom(orientation, w, h,
2524 &orientationTransform);
2525 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2526 mWidth = int(h);
2527 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002528 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002529
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002530 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002531 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532 return NO_ERROR;
2533}
2534
2535const DisplayHardware& GraphicPlane::displayHardware() const {
2536 return *mHw;
2537}
2538
Mathias Agopian59119e62010-10-11 12:37:43 -07002539DisplayHardware& GraphicPlane::editDisplayHardware() {
2540 return *mHw;
2541}
2542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543const Transform& GraphicPlane::transform() const {
2544 return mGlobalTransform;
2545}
2546
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002547EGLDisplay GraphicPlane::getEGLDisplay() const {
2548 return mHw->getEGLDisplay();
2549}
2550
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551// ---------------------------------------------------------------------------
2552
2553}; // namespace android