blob: af0f95a25c1c3f236b5b57306d68b4518181ba36 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
50#include "LayerBlur.h"
51#include "LayerBuffer.h"
52#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopiana1ecca92009-05-21 19:21:59 -070057/* ideally AID_GRAPHICS would be in a semi-public header
58 * or there would be a way to map a user/group name to its id
59 */
60#ifndef AID_GRAPHICS
61#define AID_GRAPHICS 1003
62#endif
63
Mathias Agopian22c67842010-11-01 23:32:18 -070064#ifdef USE_COMPOSITION_BYPASS
65#warning "using COMPOSITION_BYPASS"
66#endif
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073SurfaceFlinger::SurfaceFlinger()
74 : BnSurfaceComposer(), Thread(false),
75 mTransactionFlags(0),
76 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070077 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070080 mHardwareTest("android.permission.HARDWARE_TEST"),
81 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070082 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070083 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mVisibleRegionsDirty(false),
85 mDeferReleaseConsole(false),
86 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070087 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070089 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070092 mDebugInSwapBuffers(0),
93 mLastSwapBufferTime(0),
94 mDebugInTransaction(0),
95 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070096 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 mConsoleSignals(0),
98 mSecureFrameBuffer(0)
99{
100 init();
101}
102
103void SurfaceFlinger::init()
104{
105 LOGI("SurfaceFlinger is starting");
106
107 // debugging stuff...
108 char value[PROPERTY_VALUE_MAX];
109 property_get("debug.sf.showupdates", value, "0");
110 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showbackground", value, "0");
112 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113
Mathias Agopian78fd5012010-04-20 14:51:04 -0700114 LOGI_IF(mDebugRegion, "showupdates enabled");
115 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
118SurfaceFlinger::~SurfaceFlinger()
119{
120 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
124{
125 return graphicPlane(0).displayHardware().getOverlayEngine();
126}
127
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700128sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700130 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131}
132
Mathias Agopian7e27f052010-05-28 14:22:23 -0700133sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
Mathias Agopian96f08192010-06-02 23:28:45 -0700135 sp<ISurfaceComposerClient> bclient;
136 sp<Client> client(new Client(this));
137 status_t err = client->initCheck();
138 if (err == NO_ERROR) {
139 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 return bclient;
142}
143
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700144sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
145{
146 sp<ISurfaceComposerClient> bclient;
147 sp<UserClient> client(new UserClient(this));
148 status_t err = client->initCheck();
149 if (err == NO_ERROR) {
150 bclient = client;
151 }
152 return bclient;
153}
154
155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
157{
158 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
159 const GraphicPlane& plane(mGraphicPlanes[dpy]);
160 return plane;
161}
162
163GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
164{
165 return const_cast<GraphicPlane&>(
166 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
167}
168
169void SurfaceFlinger::bootFinished()
170{
171 const nsecs_t now = systemTime();
172 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700173 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700175 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176}
177
178void SurfaceFlinger::onFirstRef()
179{
180 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
181
182 // Wait for the main thread to be done with its initialization
183 mReadyToRunBarrier.wait();
184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186static inline uint16_t pack565(int r, int g, int b) {
187 return (r<<11)|(g<<5)|b;
188}
189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190status_t SurfaceFlinger::readyToRun()
191{
192 LOGI( "SurfaceFlinger's main thread ready to run. "
193 "Initializing graphics H/W...");
194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 // we only support one display currently
196 int dpy = 0;
197
198 {
199 // initialize the main display
200 GraphicPlane& plane(graphicPlane(dpy));
201 DisplayHardware* const hw = new DisplayHardware(this, dpy);
202 plane.setDisplayHardware(hw);
203 }
204
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700205 // create the shared control-block
206 mServerHeap = new MemoryHeapBase(4096,
207 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
208 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
209
210 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
211 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
212
213 new(mServerCblk) surface_flinger_cblk_t;
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 // initialize primary screen
216 // (other display should be initialized in the same manner, but
217 // asynchronously, as they could come and go. None of this is supported
218 // yet).
219 const GraphicPlane& plane(graphicPlane(dpy));
220 const DisplayHardware& hw = plane.displayHardware();
221 const uint32_t w = hw.getWidth();
222 const uint32_t h = hw.getHeight();
223 const uint32_t f = hw.getFormat();
224 hw.makeCurrent();
225
226 // initialize the shared control block
227 mServerCblk->connected |= 1<<dpy;
228 display_cblk_t* dcblk = mServerCblk->displays + dpy;
229 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800230 dcblk->w = plane.getWidth();
231 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 dcblk->format = f;
233 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
234 dcblk->xdpi = hw.getDpiX();
235 dcblk->ydpi = hw.getDpiY();
236 dcblk->fps = hw.getRefreshRate();
237 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238
239 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
241 glPixelStorei(GL_PACK_ALIGNMENT, 4);
242 glEnableClientState(GL_VERTEX_ARRAY);
243 glEnable(GL_SCISSOR_TEST);
244 glShadeModel(GL_FLAT);
245 glDisable(GL_DITHER);
246 glDisable(GL_CULL_FACE);
247
248 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
249 const uint16_t g1 = pack565(0x17,0x2f,0x17);
250 const uint16_t textureData[4] = { g0, g1, g1, g0 };
251 glGenTextures(1, &mWormholeTexName);
252 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
253 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
254 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
255 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
256 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
257 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
258 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
259
260 glViewport(0, 0, w, h);
261 glMatrixMode(GL_PROJECTION);
262 glLoadIdentity();
263 glOrthof(0, w, h, 0, 0, 1);
264
265 LayerDim::initDimmer(this, w, h);
266
267 mReadyToRunBarrier.open();
268
269 /*
270 * We're now ready to accept clients...
271 */
272
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700273 // start boot animation
274 property_set("ctl.start", "bootanim");
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 return NO_ERROR;
277}
278
279// ----------------------------------------------------------------------------
280#if 0
281#pragma mark -
282#pragma mark Events Handler
283#endif
284
285void SurfaceFlinger::waitForEvent()
286{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700287 while (true) {
288 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800289 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700290 if (UNLIKELY(isFrozen())) {
291 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700292 const nsecs_t now = systemTime();
293 if (mFreezeDisplayTime == 0) {
294 mFreezeDisplayTime = now;
295 }
296 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
297 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700298 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700299
Mathias Agopianbb641242010-05-18 17:06:55 -0700300 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800301
302 // see if we timed out
303 if (isFrozen()) {
304 const nsecs_t now = systemTime();
305 nsecs_t frozenTime = (now - mFreezeDisplayTime);
306 if (frozenTime >= freezeDisplayTimeout) {
307 // we timed out and are still frozen
308 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
309 mFreezeDisplay, mFreezeCount);
310 mFreezeDisplayTime = 0;
311 mFreezeCount = 0;
312 mFreezeDisplay = false;
313 }
314 }
315
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700316 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700317 switch (msg->what) {
318 case MessageQueue::INVALIDATE:
319 // invalidate message, just return to the main loop
320 return;
321 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 }
324}
325
326void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700327 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
330void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700331 // this is the IPC call
332 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333}
334
Mathias Agopianbb641242010-05-18 17:06:55 -0700335status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
336 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337{
Mathias Agopianbb641242010-05-18 17:06:55 -0700338 return mEventQueue.postMessage(msg, reltime, flags);
339}
340
341status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
342 nsecs_t reltime, uint32_t flags)
343{
344 status_t res = mEventQueue.postMessage(msg, reltime, flags);
345 if (res == NO_ERROR) {
346 msg->wait();
347 }
348 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
351// ----------------------------------------------------------------------------
352#if 0
353#pragma mark -
354#pragma mark Main loop
355#endif
356
357bool SurfaceFlinger::threadLoop()
358{
359 waitForEvent();
360
361 // check for transactions
362 if (UNLIKELY(mConsoleSignals)) {
363 handleConsoleEvents();
364 }
365
366 if (LIKELY(mTransactionCount == 0)) {
367 // if we're in a global transaction, don't do anything.
368 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
369 uint32_t transactionFlags = getTransactionFlags(mask);
370 if (LIKELY(transactionFlags)) {
371 handleTransaction(transactionFlags);
372 }
373 }
374
375 // post surfaces (if needed)
376 handlePageFlip();
377
378 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700379 if (LIKELY(hw.canDraw() && !isFrozen())) {
Mathias Agopian35b48d12010-09-13 22:57:58 -0700380
Mathias Agopian22c67842010-11-01 23:32:18 -0700381#ifdef USE_COMPOSITION_BYPASS
382 if (handleBypassLayer()) {
383 unlockClients();
384 return true;
385 }
386#endif
387
388 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700389 const int index = hw.getCurrentBufferIndex();
390 GraphicLog& logger(GraphicLog::getInstance());
391
392 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 handleRepaint();
394
Mathias Agopian74faca22009-09-17 16:18:16 -0700395 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700396 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700397 hw.compositionComplete();
398
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 // release the clients before we flip ('cause flip might block)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700400 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402
Mathias Agopian35b48d12010-09-13 22:57:58 -0700403 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 postFramebuffer();
Mathias Agopian35b48d12010-09-13 22:57:58 -0700405
406 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 } else {
408 // pretend we did the post
409 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 usleep(16667); // 60 fps period
411 }
412 return true;
413}
414
Mathias Agopian22c67842010-11-01 23:32:18 -0700415bool SurfaceFlinger::handleBypassLayer()
416{
417 sp<Layer> bypassLayer(mBypassLayer.promote());
418 if (bypassLayer != 0) {
419 sp<GraphicBuffer> buffer(bypassLayer->getBypassBuffer());
420 if (buffer!=0 && (buffer->usage & GRALLOC_USAGE_HW_FB)) {
421 const DisplayHardware& hw(graphicPlane(0).displayHardware());
422 hw.postBypassBuffer(buffer->handle);
423 return true;
424 }
425 }
426 return false;
427}
428
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429void SurfaceFlinger::postFramebuffer()
430{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 if (!mInvalidRegion.isEmpty()) {
432 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700433 const nsecs_t now = systemTime();
434 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700436 mLastSwapBufferTime = systemTime() - now;
437 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 }
440}
441
442void SurfaceFlinger::handleConsoleEvents()
443{
444 // something to do with the console
445 const DisplayHardware& hw = graphicPlane(0).displayHardware();
446
447 int what = android_atomic_and(0, &mConsoleSignals);
448 if (what & eConsoleAcquired) {
449 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700450 // this is a temporary work-around, eventually this should be called
451 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700452 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 }
454
Mathias Agopian59119e62010-10-11 12:37:43 -0700455 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700456 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458 hw.releaseScreen();
459 }
460
461 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700462 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 hw.releaseScreen();
464 } else {
465 mDeferReleaseConsole = true;
466 }
467 }
468
469 mDirtyRegion.set(hw.bounds());
470}
471
472void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
473{
Mathias Agopian3d579642009-06-04 18:46:21 -0700474 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475
Mathias Agopian4da75192010-08-10 17:19:56 -0700476 /*
477 * Perform and commit the transaction
478 */
479
Mathias Agopian3d579642009-06-04 18:46:21 -0700480 { // scope for the lock
481 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700482 const nsecs_t now = systemTime();
483 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700484 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700485 mLastTransactionTime = systemTime() - now;
486 mDebugInTransaction = 0;
Mathias Agopian4da75192010-08-10 17:19:56 -0700487 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700488 }
489
Mathias Agopian4da75192010-08-10 17:19:56 -0700490 /*
491 * Clean-up all layers that went away
492 * (do this without the lock held)
493 */
Mathias Agopian3d579642009-06-04 18:46:21 -0700494 const size_t count = ditchedLayers.size();
495 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700496 if (ditchedLayers[i] != 0) {
497 //LOGD("ditching layer %p", ditchedLayers[i].get());
498 ditchedLayers[i]->ditch();
499 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700500 }
501}
502
503void SurfaceFlinger::handleTransactionLocked(
504 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
505{
506 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 const size_t count = currentLayers.size();
508
509 /*
510 * Traversal of the children
511 * (perform the transaction for each of them if needed)
512 */
513
514 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
515 if (layersNeedTransaction) {
516 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
519 if (!trFlags) continue;
520
521 const uint32_t flags = layer->doTransaction(0);
522 if (flags & Layer::eVisibleRegion)
523 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 }
525 }
526
527 /*
528 * Perform our own transaction if needed
529 */
530
531 if (transactionFlags & eTransactionNeeded) {
532 if (mCurrentState.orientation != mDrawingState.orientation) {
533 // the orientation has changed, recompute all visible regions
534 // and invalidate everything.
535
536 const int dpy = 0;
537 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700538 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 GraphicPlane& plane(graphicPlane(dpy));
540 plane.setOrientation(orientation);
541
542 // update the shared control block
543 const DisplayHardware& hw(plane.displayHardware());
544 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
545 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800546 dcblk->w = plane.getWidth();
547 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548
549 mVisibleRegionsDirty = true;
550 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 }
552
553 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
554 // freezing or unfreezing the display -> trigger animation if needed
555 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800556 if (mFreezeDisplay)
557 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 }
559
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700560 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
561 // layers have been added
562 mVisibleRegionsDirty = true;
563 }
564
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 // some layers might have been removed, so
566 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700568 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700570 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700571 const size_t count = previousLayers.size();
572 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700573 const sp<LayerBase>& layer(previousLayers[i]);
574 if (currentLayers.indexOf( layer ) < 0) {
575 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700576 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700577 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700578 }
579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 }
582
583 commitTransaction();
584}
585
586sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
587{
588 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
589}
590
591void SurfaceFlinger::computeVisibleRegions(
592 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
593{
594 const GraphicPlane& plane(graphicPlane(0));
595 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700596 const DisplayHardware& hw(plane.displayHardware());
597 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598
599 Region aboveOpaqueLayers;
600 Region aboveCoveredLayers;
601 Region dirty;
602
603 bool secureFrameBuffer = false;
604
605 size_t i = currentLayers.size();
606 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700607 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 layer->validateVisibility(planeTransform);
609
610 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700611 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612
Mathias Agopianab028732010-03-16 16:41:46 -0700613 /*
614 * opaqueRegion: area of a surface that is fully opaque.
615 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700617
618 /*
619 * visibleRegion: area of a surface that is visible on screen
620 * and not fully transparent. This is essentially the layer's
621 * footprint minus the opaque regions above it.
622 * Areas covered by a translucent surface are considered visible.
623 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700625
626 /*
627 * coveredRegion: area of a surface that is covered by all
628 * visible regions above it (which includes the translucent areas).
629 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700631
632
633 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700634 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700636 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700638 visibleRegion.andSelf(screenRegion);
639 if (!visibleRegion.isEmpty()) {
640 // Remove the transparent area from the visible region
641 if (translucent) {
642 visibleRegion.subtractSelf(layer->transparentRegionScreen);
643 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644
Mathias Agopianab028732010-03-16 16:41:46 -0700645 // compute the opaque region
646 const int32_t layerOrientation = layer->getOrientation();
647 if (s.alpha==255 && !translucent &&
648 ((layerOrientation & Transform::ROT_INVALID) == false)) {
649 // the opaque region is the layer's footprint
650 opaqueRegion = visibleRegion;
651 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 }
653 }
654
Mathias Agopianab028732010-03-16 16:41:46 -0700655 // Clip the covered region to the visible region
656 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
657
658 // Update aboveCoveredLayers for next (lower) layer
659 aboveCoveredLayers.orSelf(visibleRegion);
660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 // subtract the opaque region covered by the layers above us
662 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663
664 // compute this layer's dirty region
665 if (layer->contentDirty) {
666 // we need to invalidate the whole region
667 dirty = visibleRegion;
668 // as well, as the old visible region
669 dirty.orSelf(layer->visibleRegionScreen);
670 layer->contentDirty = false;
671 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700672 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700673 * the exposed region consists of two components:
674 * 1) what's VISIBLE now and was COVERED before
675 * 2) what's EXPOSED now less what was EXPOSED before
676 *
677 * note that (1) is conservative, we start with the whole
678 * visible region but only keep what used to be covered by
679 * something -- which mean it may have been exposed.
680 *
681 * (2) handles areas that were not covered by anything but got
682 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700683 */
Mathias Agopianab028732010-03-16 16:41:46 -0700684 const Region newExposed = visibleRegion - coveredRegion;
685 const Region oldVisibleRegion = layer->visibleRegionScreen;
686 const Region oldCoveredRegion = layer->coveredRegionScreen;
687 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
688 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 }
690 dirty.subtractSelf(aboveOpaqueLayers);
691
692 // accumulate to the screen dirty region
693 dirtyRegion.orSelf(dirty);
694
Mathias Agopianab028732010-03-16 16:41:46 -0700695 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697
698 // Store the visible region is screen space
699 layer->setVisibleRegion(visibleRegion);
700 layer->setCoveredRegion(coveredRegion);
701
Mathias Agopian97011222009-07-28 10:57:27 -0700702 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 if (layer->isSecure() && !visibleRegion.isEmpty()) {
704 secureFrameBuffer = true;
705 }
706 }
707
Mathias Agopian97011222009-07-28 10:57:27 -0700708 // invalidate the areas where a layer was removed
709 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
710 mDirtyRegionRemovedLayer.clear();
711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712 mSecureFrameBuffer = secureFrameBuffer;
713 opaqueRegion = aboveOpaqueLayers;
714}
715
716
717void SurfaceFlinger::commitTransaction()
718{
719 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700720 mResizeTransationPending = false;
721 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722}
723
Mathias Agopian22c67842010-11-01 23:32:18 -0700724void SurfaceFlinger::setBypassLayer(const sp<LayerBase>& layer)
725{
726 // if this layer is already the bypass layer, do nothing
727 sp<Layer> cur(mBypassLayer.promote());
Mathias Agopiane24cc7a2010-12-07 21:00:25 -0800728 if (mBypassLayer == layer) {
729 if (cur != NULL) {
730 cur->updateBuffersOrientation();
731 }
Mathias Agopian22c67842010-11-01 23:32:18 -0700732 return;
Mathias Agopiane24cc7a2010-12-07 21:00:25 -0800733 }
Mathias Agopian22c67842010-11-01 23:32:18 -0700734
735 // clear the current bypass layer
736 mBypassLayer.clear();
737 if (cur != 0) {
738 cur->setBypass(false);
739 cur.clear();
740 }
741
742 // set new bypass layer
743 if (layer != 0) {
744 if (layer->setBypass(true)) {
745 mBypassLayer = static_cast<Layer*>(layer.get());
746 }
747 }
748}
749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750void SurfaceFlinger::handlePageFlip()
751{
752 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700753 LayerVector& currentLayers = const_cast<LayerVector&>(
754 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 visibleRegions |= lockPageFlip(currentLayers);
756
757 const DisplayHardware& hw = graphicPlane(0).displayHardware();
758 const Region screenRegion(hw.bounds());
759 if (visibleRegions) {
760 Region opaqueRegion;
761 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700762
763 /*
764 * rebuild the visible layer list
765 */
766 mVisibleLayersSortedByZ.clear();
767 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
768 size_t count = currentLayers.size();
769 mVisibleLayersSortedByZ.setCapacity(count);
770 for (size_t i=0 ; i<count ; i++) {
771 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
772 mVisibleLayersSortedByZ.add(currentLayers[i]);
773 }
774
Mathias Agopian22c67842010-11-01 23:32:18 -0700775#ifdef USE_COMPOSITION_BYPASS
776 sp<LayerBase> bypassLayer;
777 const size_t numVisibleLayers = mVisibleLayersSortedByZ.size();
778 if (numVisibleLayers == 1) {
779 const sp<LayerBase>& candidate(mVisibleLayersSortedByZ[0]);
780 const Region& visibleRegion(candidate->visibleRegionScreen);
781 const Region reminder(screenRegion.subtract(visibleRegion));
782 if (reminder.isEmpty()) {
783 // fullscreen candidate!
784 bypassLayer = candidate;
785 }
786 }
787 setBypassLayer(bypassLayer);
788#endif
789
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 mWormholeRegion = screenRegion.subtract(opaqueRegion);
791 mVisibleRegionsDirty = false;
792 }
793
794 unlockPageFlip(currentLayers);
795 mDirtyRegion.andSelf(screenRegion);
796}
797
798bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
799{
800 bool recomputeVisibleRegions = false;
801 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700802 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700804 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 layer->lockPageFlip(recomputeVisibleRegions);
806 }
807 return recomputeVisibleRegions;
808}
809
810void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
811{
812 const GraphicPlane& plane(graphicPlane(0));
813 const Transform& planeTransform(plane.transform());
814 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700815 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700817 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 layer->unlockPageFlip(planeTransform, mDirtyRegion);
819 }
820}
821
Mathias Agopianb8a55602009-06-26 19:06:36 -0700822
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823void SurfaceFlinger::handleRepaint()
824{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825 // compute the invalid region
826 mInvalidRegion.orSelf(mDirtyRegion);
827 if (mInvalidRegion.isEmpty()) {
828 // nothing to do
829 return;
830 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831
832 if (UNLIKELY(mDebugRegion)) {
833 debugFlashRegions();
834 }
835
Mathias Agopianb8a55602009-06-26 19:06:36 -0700836 // set the frame buffer
837 const DisplayHardware& hw(graphicPlane(0).displayHardware());
838 glMatrixMode(GL_MODELVIEW);
839 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840
841 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700842 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
843 (flags & DisplayHardware::BUFFER_PRESERVED))
844 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700845 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
846 // takes a rectangle, we must make sure to update that whole
847 // rectangle in that case
848 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700849 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700850 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 mDirtyRegion.set(mInvalidRegion.bounds());
852 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // in the BUFFER_PRESERVED case, obviously, we can update only
854 // what's needed and nothing more.
855 // NOTE: this is NOT a common case, as preserving the backbuffer
856 // is costly and usually involves copying the whole update back.
857 }
858 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700859 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700860 // We need to redraw the rectangle that will be updated
861 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700862 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700863 // rectangle instead of a region (see DisplayHardware::flip())
864 mDirtyRegion.set(mInvalidRegion.bounds());
865 } else {
866 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 mDirtyRegion.set(hw.bounds());
868 mInvalidRegion = mDirtyRegion;
869 }
870 }
871
872 // compose all surfaces
873 composeSurfaces(mDirtyRegion);
874
875 // clear the dirty regions
876 mDirtyRegion.clear();
877}
878
879void SurfaceFlinger::composeSurfaces(const Region& dirty)
880{
881 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
882 // should never happen unless the window manager has a bug
883 // draw something...
884 drawWormhole();
885 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700886 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700887 const size_t count = layers.size();
888 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian45721772010-08-12 15:03:26 -0700889 const sp<LayerBase>& layer(layers[i]);
890 const Region clip(dirty.intersect(layer->visibleRegionScreen));
891 if (!clip.isEmpty()) {
892 layer->draw(clip);
893 }
894 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895}
896
897void SurfaceFlinger::unlockClients()
898{
899 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
900 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700901 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700903 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 layer->finishPageFlip();
905 }
906}
907
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908void SurfaceFlinger::debugFlashRegions()
909{
Mathias Agopian0a917752010-06-14 21:20:00 -0700910 const DisplayHardware& hw(graphicPlane(0).displayHardware());
911 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700912
Mathias Agopian0a917752010-06-14 21:20:00 -0700913 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
914 (flags & DisplayHardware::BUFFER_PRESERVED))) {
915 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
916 mDirtyRegion.bounds() : hw.bounds());
917 composeSurfaces(repaint);
918 }
919
920 TextureManager::deactivateTextures();
921
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 glDisable(GL_BLEND);
923 glDisable(GL_DITHER);
924 glDisable(GL_SCISSOR_TEST);
925
Mathias Agopian0926f502009-05-04 14:17:04 -0700926 static int toggle = 0;
927 toggle = 1 - toggle;
928 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700929 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700930 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700931 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700932 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933
Mathias Agopian20f68782009-05-11 00:03:41 -0700934 Region::const_iterator it = mDirtyRegion.begin();
935 Region::const_iterator const end = mDirtyRegion.end();
936 while (it != end) {
937 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 GLfloat vertices[][2] = {
939 { r.left, r.top },
940 { r.left, r.bottom },
941 { r.right, r.bottom },
942 { r.right, r.top }
943 };
944 glVertexPointer(2, GL_FLOAT, 0, vertices);
945 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
946 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700947
Mathias Agopianb8a55602009-06-26 19:06:36 -0700948 if (mInvalidRegion.isEmpty()) {
949 mDirtyRegion.dump("mDirtyRegion");
950 mInvalidRegion.dump("mInvalidRegion");
951 }
952 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
954 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700955 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956
957 glEnable(GL_SCISSOR_TEST);
958 //mDirtyRegion.dump("mDirtyRegion");
959}
960
961void SurfaceFlinger::drawWormhole() const
962{
963 const Region region(mWormholeRegion.intersect(mDirtyRegion));
964 if (region.isEmpty())
965 return;
966
967 const DisplayHardware& hw(graphicPlane(0).displayHardware());
968 const int32_t width = hw.getWidth();
969 const int32_t height = hw.getHeight();
970
971 glDisable(GL_BLEND);
972 glDisable(GL_DITHER);
973
974 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700975 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700976 Region::const_iterator it = region.begin();
977 Region::const_iterator const end = region.end();
978 while (it != end) {
979 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 const GLint sy = height - (r.top + r.height());
981 glScissor(r.left, sy, r.width(), r.height());
982 glClear(GL_COLOR_BUFFER_BIT);
983 }
984 } else {
985 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
986 { width, height }, { 0, height } };
987 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
988 glVertexPointer(2, GL_SHORT, 0, vertices);
989 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
990 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -0700991#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700992 if (GLExtensions::getInstance().haveTextureExternal()) {
993 glDisable(GL_TEXTURE_EXTERNAL_OES);
994 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700995#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 glEnable(GL_TEXTURE_2D);
997 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
998 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
999 glMatrixMode(GL_TEXTURE);
1000 glLoadIdentity();
1001 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001002 Region::const_iterator it = region.begin();
1003 Region::const_iterator const end = region.end();
1004 while (it != end) {
1005 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 const GLint sy = height - (r.top + r.height());
1007 glScissor(r.left, sy, r.width(), r.height());
1008 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1009 }
1010 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1011 }
1012}
1013
1014void SurfaceFlinger::debugShowFPS() const
1015{
1016 static int mFrameCount;
1017 static int mLastFrameCount = 0;
1018 static nsecs_t mLastFpsTime = 0;
1019 static float mFps = 0;
1020 mFrameCount++;
1021 nsecs_t now = systemTime();
1022 nsecs_t diff = now - mLastFpsTime;
1023 if (diff > ms2ns(250)) {
1024 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1025 mLastFpsTime = now;
1026 mLastFrameCount = mFrameCount;
1027 }
1028 // XXX: mFPS has the value we want
1029 }
1030
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001031status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032{
1033 Mutex::Autolock _l(mStateLock);
1034 addLayer_l(layer);
1035 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1036 return NO_ERROR;
1037}
1038
Mathias Agopian96f08192010-06-02 23:28:45 -07001039status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1040{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001041 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001042 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1043}
1044
1045ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1046 const sp<LayerBaseClient>& lbc)
1047{
1048 Mutex::Autolock _l(mStateLock);
1049
1050 // attach this layer to the client
1051 ssize_t name = client->attachLayer(lbc);
1052
1053 // add this layer to the current state list
1054 addLayer_l(lbc);
1055
1056 return name;
1057}
1058
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001059status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060{
1061 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001062 status_t err = purgatorizeLayer_l(layer);
1063 if (err == NO_ERROR)
1064 setTransactionFlags(eTransactionNeeded);
1065 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066}
1067
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001068status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001070 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1071 if (lbc != 0) {
1072 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1073 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1075 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001076 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 return NO_ERROR;
1078 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001079 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080}
1081
Mathias Agopian9a112062009-04-17 19:36:26 -07001082status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1083{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001084 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001085 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001086
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001087 layerBase->onRemoved();
1088
Mathias Agopian3d579642009-06-04 18:46:21 -07001089 // it's possible that we don't find a layer, because it might
1090 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001091 // from the user because there is a race between Client::destroySurface(),
1092 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001093 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1094}
1095
Mathias Agopian96f08192010-06-02 23:28:45 -07001096status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097{
Mathias Agopian96f08192010-06-02 23:28:45 -07001098 layer->forceVisibilityTransaction();
1099 setTransactionFlags(eTraversalNeeded);
1100 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101}
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);
2116 glMatrixMode(GL_PROJECTION);
2117 glPushMatrix();
2118 glLoadIdentity();
2119 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2120 glMatrixMode(GL_MODELVIEW);
2121
2122 // redraw the screen entirely...
2123 glClearColor(0,0,0,1);
2124 glClear(GL_COLOR_BUFFER_BIT);
2125 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2126 const size_t count = layers.size();
2127 for (size_t i=0 ; i<count ; ++i) {
2128 const sp<LayerBase>& layer(layers[i]);
2129 layer->drawForSreenShot();
2130 }
2131
2132 // XXX: this is needed on tegra
2133 glScissor(0, 0, sw, sh);
2134
2135 // check for errors and return screen capture
2136 if (glGetError() != GL_NO_ERROR) {
2137 // error while rendering
2138 result = INVALID_OPERATION;
2139 } else {
2140 // allocate shared memory large enough to hold the
2141 // screen capture
2142 sp<MemoryHeapBase> base(
2143 new MemoryHeapBase(size, 0, "screen-capture") );
2144 void* const ptr = base->getBase();
2145 if (ptr) {
2146 // capture the screen with glReadPixels()
2147 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2148 if (glGetError() == GL_NO_ERROR) {
2149 *heap = base;
2150 *w = sw;
2151 *h = sh;
2152 *f = PIXEL_FORMAT_RGBA_8888;
2153 result = NO_ERROR;
2154 }
2155 } else {
2156 result = NO_MEMORY;
2157 }
2158 }
2159
2160 glEnable(GL_SCISSOR_TEST);
2161 glViewport(0, 0, hw_w, hw_h);
2162 glMatrixMode(GL_PROJECTION);
2163 glPopMatrix();
2164 glMatrixMode(GL_MODELVIEW);
2165
2166
2167 } else {
2168 result = BAD_VALUE;
2169 }
2170
2171 // release FBO resources
2172 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2173 glDeleteRenderbuffersOES(1, &tname);
2174 glDeleteFramebuffersOES(1, &name);
2175 return result;
2176}
2177
2178
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002179status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2180 sp<IMemoryHeap>* heap,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002181 uint32_t* width, uint32_t* height, PixelFormat* format,
2182 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002183{
2184 // only one display supported for now
2185 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2186 return BAD_VALUE;
2187
2188 if (!GLExtensions::getInstance().haveFramebufferObject())
2189 return INVALID_OPERATION;
2190
2191 class MessageCaptureScreen : public MessageBase {
2192 SurfaceFlinger* flinger;
2193 DisplayID dpy;
2194 sp<IMemoryHeap>* heap;
2195 uint32_t* w;
2196 uint32_t* h;
2197 PixelFormat* f;
Mathias Agopiandf85c452010-09-29 13:02:36 -07002198 uint32_t sw;
2199 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002200 status_t result;
2201 public:
2202 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002203 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
2204 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002205 : flinger(flinger), dpy(dpy),
Mathias Agopiandf85c452010-09-29 13:02:36 -07002206 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002207 {
2208 }
2209 status_t getResult() const {
2210 return result;
2211 }
2212 virtual bool handler() {
2213 Mutex::Autolock _l(flinger->mStateLock);
2214
2215 // if we have secure windows, never allow the screen capture
2216 if (flinger->mSecureFrameBuffer)
2217 return true;
2218
Mathias Agopiandf85c452010-09-29 13:02:36 -07002219 result = flinger->captureScreenImplLocked(dpy,
2220 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002221
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002222 return true;
2223 }
2224 };
2225
2226 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002227 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002228 status_t res = postMessageSync(msg);
2229 if (res == NO_ERROR) {
2230 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2231 }
2232 return res;
2233}
2234
2235// ---------------------------------------------------------------------------
2236
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002237sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2238{
2239 sp<Layer> result;
2240 Mutex::Autolock _l(mStateLock);
2241 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2242 return result;
2243}
2244
2245// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002246
Mathias Agopian96f08192010-06-02 23:28:45 -07002247Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002248 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002249{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002250}
2251
Mathias Agopian96f08192010-06-02 23:28:45 -07002252Client::~Client()
2253{
Mathias Agopian96f08192010-06-02 23:28:45 -07002254 const size_t count = mLayers.size();
2255 for (size_t i=0 ; i<count ; i++) {
2256 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2257 if (layer != 0) {
2258 mFlinger->removeLayer(layer);
2259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002260 }
2261}
2262
Mathias Agopian96f08192010-06-02 23:28:45 -07002263status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002264 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002265}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002266
Mathias Agopian96f08192010-06-02 23:28:45 -07002267ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2268{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002269 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002270 mLayers.add(name, layer);
2271 return name;
2272}
2273
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002274void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002275{
Mathias Agopian96f08192010-06-02 23:28:45 -07002276 // we do a linear search here, because this doesn't happen often
2277 const size_t count = mLayers.size();
2278 for (size_t i=0 ; i<count ; i++) {
2279 if (mLayers.valueAt(i) == layer) {
2280 mLayers.removeItemsAt(i, 1);
2281 break;
2282 }
2283 }
2284}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002285sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2286 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002287 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2288 if (layer != 0) {
2289 lbc = layer.promote();
2290 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002291 }
2292 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002293}
2294
Mathias Agopian96f08192010-06-02 23:28:45 -07002295sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002296 return 0;
2297}
2298ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2299 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002300}
Mathias Agopian96f08192010-06-02 23:28:45 -07002301sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002302 ISurfaceComposerClient::surface_data_t* params, int pid,
2303 const String8& name,
2304 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002305 uint32_t flags)
2306{
Mathias Agopian96f08192010-06-02 23:28:45 -07002307 return mFlinger->createSurface(this, pid, name, params,
2308 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309}
Mathias Agopian96f08192010-06-02 23:28:45 -07002310status_t Client::destroySurface(SurfaceID sid) {
2311 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002312}
Mathias Agopian96f08192010-06-02 23:28:45 -07002313status_t Client::setState(int32_t count, const layer_state_t* states) {
2314 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002315}
2316
2317// ---------------------------------------------------------------------------
2318
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002319UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2320 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2321{
2322 const int pgsize = getpagesize();
2323 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2324
2325 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2326 "SurfaceFlinger Client control-block");
2327
2328 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2329 if (ctrlblk) { // construct the shared structure in-place.
2330 new(ctrlblk) SharedClient;
2331 }
2332}
2333
2334UserClient::~UserClient()
2335{
2336 if (ctrlblk) {
2337 ctrlblk->~SharedClient(); // destroy our shared-structure.
2338 }
2339
2340 /*
2341 * When a UserClient dies, it's unclear what to do exactly.
2342 * We could go ahead and destroy all surfaces linked to that client
2343 * however, it wouldn't be fair to the main Client
2344 * (usually the the window-manager), which might want to re-target
2345 * the layer to another UserClient.
2346 * I think the best is to do nothing, or not much; in most cases the
2347 * WM itself will go ahead and clean things up when it detects a client of
2348 * his has died.
2349 * The remaining question is what to display? currently we keep
2350 * just keep the current buffer.
2351 */
2352}
2353
2354status_t UserClient::initCheck() const {
2355 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2356}
2357
2358void UserClient::detachLayer(const Layer* layer)
2359{
2360 int32_t name = layer->getToken();
2361 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002362 int32_t mask = 1LU<<name;
2363 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2364 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2365 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002366 }
2367}
2368
2369sp<IMemoryHeap> UserClient::getControlBlock() const {
2370 return mCblkHeap;
2371}
2372
2373ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2374{
2375 int32_t name = NAME_NOT_FOUND;
2376 sp<Layer> layer(mFlinger->getLayer(sur));
2377 if (layer == 0) return name;
2378
Mathias Agopian579b3f82010-06-08 19:54:15 -07002379 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002380 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07002381 if ((name >= 0) && (layer->getClient() == this))
2382 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002383
2384 name = 0;
2385 do {
2386 int32_t mask = 1LU<<name;
2387 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2388 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002389 status_t err = layer->setToken(
2390 const_cast<UserClient*>(this), ctrlblk, name);
2391 if (err != NO_ERROR) {
2392 // free the name
2393 android_atomic_and(~mask, &mBitmap);
2394 name = err;
2395 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002396 break;
2397 }
2398 if (++name > 31)
2399 name = NO_MEMORY;
2400 } while(name >= 0);
2401
Mathias Agopian53503a92010-06-08 15:40:56 -07002402 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2403 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002404 return name;
2405}
2406
2407sp<ISurface> UserClient::createSurface(
2408 ISurfaceComposerClient::surface_data_t* params, int pid,
2409 const String8& name,
2410 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2411 uint32_t flags) {
2412 return 0;
2413}
2414status_t UserClient::destroySurface(SurfaceID sid) {
2415 return INVALID_OPERATION;
2416}
2417status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2418 return INVALID_OPERATION;
2419}
2420
2421// ---------------------------------------------------------------------------
2422
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002423GraphicPlane::GraphicPlane()
2424 : mHw(0)
2425{
2426}
2427
2428GraphicPlane::~GraphicPlane() {
2429 delete mHw;
2430}
2431
2432bool GraphicPlane::initialized() const {
2433 return mHw ? true : false;
2434}
2435
Mathias Agopian2b92d892010-02-08 15:49:35 -08002436int GraphicPlane::getWidth() const {
2437 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002438}
2439
Mathias Agopian2b92d892010-02-08 15:49:35 -08002440int GraphicPlane::getHeight() const {
2441 return mHeight;
2442}
2443
2444void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2445{
2446 mHw = hw;
2447
2448 // initialize the display orientation transform.
2449 // it's a constant that should come from the display driver.
2450 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2451 char property[PROPERTY_VALUE_MAX];
2452 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2453 //displayOrientation
2454 switch (atoi(property)) {
2455 case 90:
2456 displayOrientation = ISurfaceComposer::eOrientation90;
2457 break;
2458 case 270:
2459 displayOrientation = ISurfaceComposer::eOrientation270;
2460 break;
2461 }
2462 }
2463
2464 const float w = hw->getWidth();
2465 const float h = hw->getHeight();
2466 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2467 &mDisplayTransform);
2468 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2469 mDisplayWidth = h;
2470 mDisplayHeight = w;
2471 } else {
2472 mDisplayWidth = w;
2473 mDisplayHeight = h;
2474 }
2475
2476 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477}
2478
2479status_t GraphicPlane::orientationToTransfrom(
2480 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002481{
2482 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 switch (orientation) {
2484 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002485 flags = Transform::ROT_0;
2486 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002487 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002488 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002489 break;
2490 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002491 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002492 break;
2493 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002494 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002495 break;
2496 default:
2497 return BAD_VALUE;
2498 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002499 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500 return NO_ERROR;
2501}
2502
2503status_t GraphicPlane::setOrientation(int orientation)
2504{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 // If the rotation can be handled in hardware, this is where
2506 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002507
2508 const DisplayHardware& hw(displayHardware());
2509 const float w = mDisplayWidth;
2510 const float h = mDisplayHeight;
2511 mWidth = int(w);
2512 mHeight = int(h);
2513
2514 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002515 GraphicPlane::orientationToTransfrom(orientation, w, h,
2516 &orientationTransform);
2517 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2518 mWidth = int(h);
2519 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002521
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002522 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002523 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002524 return NO_ERROR;
2525}
2526
2527const DisplayHardware& GraphicPlane::displayHardware() const {
2528 return *mHw;
2529}
2530
Mathias Agopian59119e62010-10-11 12:37:43 -07002531DisplayHardware& GraphicPlane::editDisplayHardware() {
2532 return *mHw;
2533}
2534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002535const Transform& GraphicPlane::transform() const {
2536 return mGlobalTransform;
2537}
2538
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002539EGLDisplay GraphicPlane::getEGLDisplay() const {
2540 return mHw->getEGLDisplay();
2541}
2542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543// ---------------------------------------------------------------------------
2544
2545}; // namespace android