blob: 5e9e06cd04478b7bba8b8c017ecad07e1f0a0e43 [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());
728 if (mBypassLayer == layer)
729 return;
730
731 // clear the current bypass layer
732 mBypassLayer.clear();
733 if (cur != 0) {
734 cur->setBypass(false);
735 cur.clear();
736 }
737
738 // set new bypass layer
739 if (layer != 0) {
740 if (layer->setBypass(true)) {
741 mBypassLayer = static_cast<Layer*>(layer.get());
742 }
743 }
744}
745
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746void SurfaceFlinger::handlePageFlip()
747{
748 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700749 LayerVector& currentLayers = const_cast<LayerVector&>(
750 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 visibleRegions |= lockPageFlip(currentLayers);
752
753 const DisplayHardware& hw = graphicPlane(0).displayHardware();
754 const Region screenRegion(hw.bounds());
755 if (visibleRegions) {
756 Region opaqueRegion;
757 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700758
759 /*
760 * rebuild the visible layer list
761 */
762 mVisibleLayersSortedByZ.clear();
763 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
764 size_t count = currentLayers.size();
765 mVisibleLayersSortedByZ.setCapacity(count);
766 for (size_t i=0 ; i<count ; i++) {
767 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
768 mVisibleLayersSortedByZ.add(currentLayers[i]);
769 }
770
Mathias Agopian22c67842010-11-01 23:32:18 -0700771#ifdef USE_COMPOSITION_BYPASS
772 sp<LayerBase> bypassLayer;
773 const size_t numVisibleLayers = mVisibleLayersSortedByZ.size();
774 if (numVisibleLayers == 1) {
775 const sp<LayerBase>& candidate(mVisibleLayersSortedByZ[0]);
776 const Region& visibleRegion(candidate->visibleRegionScreen);
777 const Region reminder(screenRegion.subtract(visibleRegion));
778 if (reminder.isEmpty()) {
779 // fullscreen candidate!
780 bypassLayer = candidate;
781 }
782 }
783 setBypassLayer(bypassLayer);
784#endif
785
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 mWormholeRegion = screenRegion.subtract(opaqueRegion);
787 mVisibleRegionsDirty = false;
788 }
789
790 unlockPageFlip(currentLayers);
791 mDirtyRegion.andSelf(screenRegion);
792}
793
794bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
795{
796 bool recomputeVisibleRegions = false;
797 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700798 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700800 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 layer->lockPageFlip(recomputeVisibleRegions);
802 }
803 return recomputeVisibleRegions;
804}
805
806void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
807{
808 const GraphicPlane& plane(graphicPlane(0));
809 const Transform& planeTransform(plane.transform());
810 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700811 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700813 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814 layer->unlockPageFlip(planeTransform, mDirtyRegion);
815 }
816}
817
Mathias Agopianb8a55602009-06-26 19:06:36 -0700818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819void SurfaceFlinger::handleRepaint()
820{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700821 // compute the invalid region
822 mInvalidRegion.orSelf(mDirtyRegion);
823 if (mInvalidRegion.isEmpty()) {
824 // nothing to do
825 return;
826 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827
828 if (UNLIKELY(mDebugRegion)) {
829 debugFlashRegions();
830 }
831
Mathias Agopianb8a55602009-06-26 19:06:36 -0700832 // set the frame buffer
833 const DisplayHardware& hw(graphicPlane(0).displayHardware());
834 glMatrixMode(GL_MODELVIEW);
835 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836
837 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700838 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
839 (flags & DisplayHardware::BUFFER_PRESERVED))
840 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
842 // takes a rectangle, we must make sure to update that whole
843 // rectangle in that case
844 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700845 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700846 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 mDirtyRegion.set(mInvalidRegion.bounds());
848 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700849 // in the BUFFER_PRESERVED case, obviously, we can update only
850 // what's needed and nothing more.
851 // NOTE: this is NOT a common case, as preserving the backbuffer
852 // is costly and usually involves copying the whole update back.
853 }
854 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700855 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // We need to redraw the rectangle that will be updated
857 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700858 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // rectangle instead of a region (see DisplayHardware::flip())
860 mDirtyRegion.set(mInvalidRegion.bounds());
861 } else {
862 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 mDirtyRegion.set(hw.bounds());
864 mInvalidRegion = mDirtyRegion;
865 }
866 }
867
868 // compose all surfaces
869 composeSurfaces(mDirtyRegion);
870
871 // clear the dirty regions
872 mDirtyRegion.clear();
873}
874
875void SurfaceFlinger::composeSurfaces(const Region& dirty)
876{
877 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
878 // should never happen unless the window manager has a bug
879 // draw something...
880 drawWormhole();
881 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700882 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700883 const size_t count = layers.size();
884 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian45721772010-08-12 15:03:26 -0700885 const sp<LayerBase>& layer(layers[i]);
886 const Region clip(dirty.intersect(layer->visibleRegionScreen));
887 if (!clip.isEmpty()) {
888 layer->draw(clip);
889 }
890 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891}
892
893void SurfaceFlinger::unlockClients()
894{
895 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
896 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700897 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700899 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900 layer->finishPageFlip();
901 }
902}
903
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904void SurfaceFlinger::debugFlashRegions()
905{
Mathias Agopian0a917752010-06-14 21:20:00 -0700906 const DisplayHardware& hw(graphicPlane(0).displayHardware());
907 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700908
Mathias Agopian0a917752010-06-14 21:20:00 -0700909 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
910 (flags & DisplayHardware::BUFFER_PRESERVED))) {
911 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
912 mDirtyRegion.bounds() : hw.bounds());
913 composeSurfaces(repaint);
914 }
915
916 TextureManager::deactivateTextures();
917
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 glDisable(GL_BLEND);
919 glDisable(GL_DITHER);
920 glDisable(GL_SCISSOR_TEST);
921
Mathias Agopian0926f502009-05-04 14:17:04 -0700922 static int toggle = 0;
923 toggle = 1 - toggle;
924 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700925 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700926 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700927 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700928 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929
Mathias Agopian20f68782009-05-11 00:03:41 -0700930 Region::const_iterator it = mDirtyRegion.begin();
931 Region::const_iterator const end = mDirtyRegion.end();
932 while (it != end) {
933 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 GLfloat vertices[][2] = {
935 { r.left, r.top },
936 { r.left, r.bottom },
937 { r.right, r.bottom },
938 { r.right, r.top }
939 };
940 glVertexPointer(2, GL_FLOAT, 0, vertices);
941 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
942 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700943
Mathias Agopianb8a55602009-06-26 19:06:36 -0700944 if (mInvalidRegion.isEmpty()) {
945 mDirtyRegion.dump("mDirtyRegion");
946 mInvalidRegion.dump("mInvalidRegion");
947 }
948 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949
950 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700951 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952
953 glEnable(GL_SCISSOR_TEST);
954 //mDirtyRegion.dump("mDirtyRegion");
955}
956
957void SurfaceFlinger::drawWormhole() const
958{
959 const Region region(mWormholeRegion.intersect(mDirtyRegion));
960 if (region.isEmpty())
961 return;
962
963 const DisplayHardware& hw(graphicPlane(0).displayHardware());
964 const int32_t width = hw.getWidth();
965 const int32_t height = hw.getHeight();
966
967 glDisable(GL_BLEND);
968 glDisable(GL_DITHER);
969
970 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700971 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700972 Region::const_iterator it = region.begin();
973 Region::const_iterator const end = region.end();
974 while (it != end) {
975 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 const GLint sy = height - (r.top + r.height());
977 glScissor(r.left, sy, r.width(), r.height());
978 glClear(GL_COLOR_BUFFER_BIT);
979 }
980 } else {
981 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
982 { width, height }, { 0, height } };
983 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
984 glVertexPointer(2, GL_SHORT, 0, vertices);
985 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
986 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -0700987#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700988 if (GLExtensions::getInstance().haveTextureExternal()) {
989 glDisable(GL_TEXTURE_EXTERNAL_OES);
990 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700991#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 glEnable(GL_TEXTURE_2D);
993 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
994 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
995 glMatrixMode(GL_TEXTURE);
996 glLoadIdentity();
997 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700998 Region::const_iterator it = region.begin();
999 Region::const_iterator const end = region.end();
1000 while (it != end) {
1001 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 const GLint sy = height - (r.top + r.height());
1003 glScissor(r.left, sy, r.width(), r.height());
1004 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1005 }
1006 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1007 }
1008}
1009
1010void SurfaceFlinger::debugShowFPS() const
1011{
1012 static int mFrameCount;
1013 static int mLastFrameCount = 0;
1014 static nsecs_t mLastFpsTime = 0;
1015 static float mFps = 0;
1016 mFrameCount++;
1017 nsecs_t now = systemTime();
1018 nsecs_t diff = now - mLastFpsTime;
1019 if (diff > ms2ns(250)) {
1020 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1021 mLastFpsTime = now;
1022 mLastFrameCount = mFrameCount;
1023 }
1024 // XXX: mFPS has the value we want
1025 }
1026
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001027status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028{
1029 Mutex::Autolock _l(mStateLock);
1030 addLayer_l(layer);
1031 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1032 return NO_ERROR;
1033}
1034
Mathias Agopian96f08192010-06-02 23:28:45 -07001035status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1036{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001037 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001038 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1039}
1040
1041ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1042 const sp<LayerBaseClient>& lbc)
1043{
1044 Mutex::Autolock _l(mStateLock);
1045
1046 // attach this layer to the client
1047 ssize_t name = client->attachLayer(lbc);
1048
1049 // add this layer to the current state list
1050 addLayer_l(lbc);
1051
1052 return name;
1053}
1054
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001055status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056{
1057 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001058 status_t err = purgatorizeLayer_l(layer);
1059 if (err == NO_ERROR)
1060 setTransactionFlags(eTransactionNeeded);
1061 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062}
1063
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001064status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001066 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1067 if (lbc != 0) {
1068 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1069 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1071 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001072 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 return NO_ERROR;
1074 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001075 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076}
1077
Mathias Agopian9a112062009-04-17 19:36:26 -07001078status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1079{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001080 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001081 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001082
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001083 layerBase->onRemoved();
1084
Mathias Agopian3d579642009-06-04 18:46:21 -07001085 // it's possible that we don't find a layer, because it might
1086 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001087 // from the user because there is a race between Client::destroySurface(),
1088 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001089 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1090}
1091
Mathias Agopian96f08192010-06-02 23:28:45 -07001092status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093{
Mathias Agopian96f08192010-06-02 23:28:45 -07001094 layer->forceVisibilityTransaction();
1095 setTransactionFlags(eTraversalNeeded);
1096 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097}
1098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1100{
1101 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1102}
1103
Mathias Agopianbb641242010-05-18 17:06:55 -07001104uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105{
1106 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1107 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001108 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 }
1110 return old;
1111}
1112
1113void SurfaceFlinger::openGlobalTransaction()
1114{
1115 android_atomic_inc(&mTransactionCount);
1116}
1117
1118void SurfaceFlinger::closeGlobalTransaction()
1119{
1120 if (android_atomic_dec(&mTransactionCount) == 1) {
1121 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001122
1123 // if there is a transaction with a resize, wait for it to
1124 // take effect before returning.
1125 Mutex::Autolock _l(mStateLock);
1126 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001127 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1128 if (CC_UNLIKELY(err != NO_ERROR)) {
1129 // just in case something goes wrong in SF, return to the
1130 // called after a few seconds.
1131 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1132 mResizeTransationPending = false;
1133 break;
1134 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 }
1137}
1138
1139status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1140{
1141 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1142 return BAD_VALUE;
1143
1144 Mutex::Autolock _l(mStateLock);
1145 mCurrentState.freezeDisplay = 1;
1146 setTransactionFlags(eTransactionNeeded);
1147
1148 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001149 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 return NO_ERROR;
1151}
1152
1153status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1154{
1155 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1156 return BAD_VALUE;
1157
1158 Mutex::Autolock _l(mStateLock);
1159 mCurrentState.freezeDisplay = 0;
1160 setTransactionFlags(eTransactionNeeded);
1161
1162 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001163 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 return NO_ERROR;
1165}
1166
Mathias Agopianc08731e2009-03-27 18:11:38 -07001167int SurfaceFlinger::setOrientation(DisplayID dpy,
1168 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169{
1170 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1171 return BAD_VALUE;
1172
1173 Mutex::Autolock _l(mStateLock);
1174 if (mCurrentState.orientation != orientation) {
1175 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001176 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 mCurrentState.orientation = orientation;
1178 setTransactionFlags(eTransactionNeeded);
1179 mTransactionCV.wait(mStateLock);
1180 } else {
1181 orientation = BAD_VALUE;
1182 }
1183 }
1184 return orientation;
1185}
1186
Mathias Agopian96f08192010-06-02 23:28:45 -07001187sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001188 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1190 uint32_t flags)
1191{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001192 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001194
1195 if (int32_t(w|h) < 0) {
1196 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1197 int(w), int(h));
1198 return surfaceHandle;
1199 }
1200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001202 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 switch (flags & eFXSurfaceMask) {
1204 case eFXSurfaceNormal:
1205 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001206 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001208 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1209 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 }
1211 break;
1212 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001213 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 break;
1215 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001216 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 break;
1218 }
1219
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001220 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001221 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001222 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001226 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001227 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001228 params->identity = surfaceHandle->getIdentity();
1229 params->width = w;
1230 params->height = h;
1231 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001232 if (normalLayer != 0) {
1233 Mutex::Autolock _l(mStateLock);
1234 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1235 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001236 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001237
Mathias Agopian96f08192010-06-02 23:28:45 -07001238 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 }
1240
1241 return surfaceHandle;
1242}
1243
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001244sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001245 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001246 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001247 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248{
1249 // initialize the surfaces
1250 switch (format) { // TODO: take h/w into account
1251 case PIXEL_FORMAT_TRANSPARENT:
1252 case PIXEL_FORMAT_TRANSLUCENT:
1253 format = PIXEL_FORMAT_RGBA_8888;
1254 break;
1255 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001256#ifdef NO_RGBX_8888
1257 format = PIXEL_FORMAT_RGB_565;
1258#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001259 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001260#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 break;
1262 }
1263
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001264#ifdef NO_RGBX_8888
1265 if (format == PIXEL_FORMAT_RGBX_8888)
1266 format = PIXEL_FORMAT_RGBA_8888;
1267#endif
1268
Mathias Agopian96f08192010-06-02 23:28:45 -07001269 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001270 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001271 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001273 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 }
1275 return layer;
1276}
1277
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001278sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001279 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001280 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281{
Mathias Agopian96f08192010-06-02 23:28:45 -07001282 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 return layer;
1285}
1286
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001287sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001288 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290{
Mathias Agopian96f08192010-06-02 23:28:45 -07001291 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 return layer;
1294}
1295
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001296sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001297 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299{
Mathias Agopian96f08192010-06-02 23:28:45 -07001300 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 return layer;
1303}
1304
Mathias Agopian96f08192010-06-02 23:28:45 -07001305status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306{
Mathias Agopian9a112062009-04-17 19:36:26 -07001307 /*
1308 * called by the window manager, when a surface should be marked for
1309 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001310 *
1311 * The surface is removed from the current and drawing lists, but placed
1312 * in the purgatory queue, so it's not destroyed right-away (we need
1313 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001314 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001315
Mathias Agopian48d819a2009-09-10 19:41:18 -07001316 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001317 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001319 if (layer != 0) {
1320 err = purgatorizeLayer_l(layer);
1321 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001322 setTransactionFlags(eTransactionNeeded);
1323 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001324 }
1325 return err;
1326}
1327
1328status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1329{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001330 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001331
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001332 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001333 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001334 sp<LayerBaseClient> layer;
1335 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001336 MessageDestroySurface(
1337 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1338 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001339 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001340 sp<LayerBaseClient> l(layer);
1341 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001342 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001343 /*
1344 * remove the layer from the current list -- chances are that it's
1345 * not in the list anyway, because it should have been removed
1346 * already upon request of the client (eg: window manager).
1347 * However, a buggy client could have not done that.
1348 * Since we know we don't have any more clients, we don't need
1349 * to use the purgatory.
1350 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001351 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001352 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1353 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001354 return true;
1355 }
1356 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001357
Mathias Agopianbb641242010-05-18 17:06:55 -07001358 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 return NO_ERROR;
1360}
1361
1362status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001363 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 int32_t count,
1365 const layer_state_t* states)
1366{
1367 Mutex::Autolock _l(mStateLock);
1368 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 const layer_state_t& s(states[i]);
1371 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001372 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 if (what & ePositionChanged) {
1375 if (layer->setPosition(s.x, s.y))
1376 flags |= eTraversalNeeded;
1377 }
1378 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001379 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001381 mCurrentState.layersSortedByZ.removeAt(idx);
1382 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 // we need traversal (state changed)
1384 // AND transaction (list changed)
1385 flags |= eTransactionNeeded|eTraversalNeeded;
1386 }
1387 }
1388 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001389 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001391 mResizeTransationPending = true;
1392 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 }
1394 if (what & eAlphaChanged) {
1395 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1396 flags |= eTraversalNeeded;
1397 }
1398 if (what & eMatrixChanged) {
1399 if (layer->setMatrix(s.matrix))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eTransparentRegionChanged) {
1403 if (layer->setTransparentRegionHint(s.transparentRegion))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eVisibilityChanged) {
1407 if (layer->setFlags(s.flags, s.mask))
1408 flags |= eTraversalNeeded;
1409 }
1410 }
1411 }
1412 if (flags) {
1413 setTransactionFlags(flags);
1414 }
1415 return NO_ERROR;
1416}
1417
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418void SurfaceFlinger::screenReleased(int dpy)
1419{
1420 // this may be called by a signal handler, we can't do too much in here
1421 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1422 signalEvent();
1423}
1424
1425void SurfaceFlinger::screenAcquired(int dpy)
1426{
1427 // this may be called by a signal handler, we can't do too much in here
1428 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1429 signalEvent();
1430}
1431
1432status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1433{
1434 const size_t SIZE = 1024;
1435 char buffer[SIZE];
1436 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001437 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 snprintf(buffer, SIZE, "Permission Denial: "
1439 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1440 IPCThreadState::self()->getCallingPid(),
1441 IPCThreadState::self()->getCallingUid());
1442 result.append(buffer);
1443 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001444
1445 // figure out if we're stuck somewhere
1446 const nsecs_t now = systemTime();
1447 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1448 const nsecs_t inTransaction(mDebugInTransaction);
1449 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1450 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1451
1452 // Try to get the main lock, but don't insist if we can't
1453 // (this would indicate SF is stuck, but we want to be able to
1454 // print something in dumpsys).
1455 int retry = 3;
1456 while (mStateLock.tryLock()<0 && --retry>=0) {
1457 usleep(1000000);
1458 }
1459 const bool locked(retry >= 0);
1460 if (!locked) {
1461 snprintf(buffer, SIZE,
1462 "SurfaceFlinger appears to be unresponsive, "
1463 "dumping anyways (no locks held)\n");
1464 result.append(buffer);
1465 }
1466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1468 const size_t count = currentLayers.size();
1469 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001470 const sp<LayerBase>& layer(currentLayers[i]);
1471 layer->dump(result, buffer, SIZE);
1472 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 s.transparentRegion.dump(result, "transparentRegion");
1474 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1475 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1476 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 mWormholeRegion.dump(result, "WormholeRegion");
1479 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1480 snprintf(buffer, SIZE,
Mathias Agopian22c67842010-11-01 23:32:18 -07001481 " display frozen: %s, freezeCount=%d, orientation=%d, bypass=%p, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 mFreezeDisplay?"yes":"no", mFreezeCount,
Mathias Agopian22c67842010-11-01 23:32:18 -07001483 mCurrentState.orientation, mBypassLayer.unsafe_get(), hw.canDraw());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001485 snprintf(buffer, SIZE,
1486 " last eglSwapBuffers() time: %f us\n"
1487 " last transaction time : %f us\n",
1488 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1489 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001490
Mathias Agopian9795c422009-08-26 16:36:26 -07001491 if (inSwapBuffersDuration || !locked) {
1492 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1493 inSwapBuffersDuration/1000.0);
1494 result.append(buffer);
1495 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001496
Mathias Agopian9795c422009-08-26 16:36:26 -07001497 if (inTransactionDuration || !locked) {
1498 snprintf(buffer, SIZE, " transaction time: %f us\n",
1499 inTransactionDuration/1000.0);
1500 result.append(buffer);
1501 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001502
Mathias Agopian3330b202009-10-05 17:07:12 -07001503 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001504 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001505
1506 if (locked) {
1507 mStateLock.unlock();
1508 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 }
1510 write(fd, result.string(), result.size());
1511 return NO_ERROR;
1512}
1513
1514status_t SurfaceFlinger::onTransact(
1515 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1516{
1517 switch (code) {
1518 case CREATE_CONNECTION:
1519 case OPEN_GLOBAL_TRANSACTION:
1520 case CLOSE_GLOBAL_TRANSACTION:
1521 case SET_ORIENTATION:
1522 case FREEZE_DISPLAY:
1523 case UNFREEZE_DISPLAY:
1524 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001525 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001526 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 {
1528 // codes that require permission check
1529 IPCThreadState* ipc = IPCThreadState::self();
1530 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001531 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001532 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1533 LOGE("Permission Denial: "
1534 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1535 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001537 break;
1538 }
1539 case CAPTURE_SCREEN:
1540 {
1541 // codes that require permission check
1542 IPCThreadState* ipc = IPCThreadState::self();
1543 const int pid = ipc->getCallingPid();
1544 const int uid = ipc->getCallingUid();
1545 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1546 LOGE("Permission Denial: "
1547 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1548 return PERMISSION_DENIED;
1549 }
1550 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551 }
1552 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1555 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001556 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001557 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1558 IPCThreadState* ipc = IPCThreadState::self();
1559 const int pid = ipc->getCallingPid();
1560 const int uid = ipc->getCallingUid();
1561 LOGE("Permission Denial: "
1562 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563 return PERMISSION_DENIED;
1564 }
1565 int n;
1566 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001567 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001568 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 return NO_ERROR;
1570 case 1002: // SHOW_UPDATES
1571 n = data.readInt32();
1572 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1573 return NO_ERROR;
1574 case 1003: // SHOW_BACKGROUND
1575 n = data.readInt32();
1576 mDebugBackground = n ? 1 : 0;
1577 return NO_ERROR;
1578 case 1004:{ // repaint everything
1579 Mutex::Autolock _l(mStateLock);
1580 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1581 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1582 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001583 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001585 case 1005:{ // force transaction
1586 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1587 return NO_ERROR;
1588 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001589 case 1006:{ // enable/disable GraphicLog
1590 int enabled = data.readInt32();
1591 GraphicLog::getInstance().setEnabled(enabled);
1592 return NO_ERROR;
1593 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 case 1007: // set mFreezeCount
1595 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001596 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 return NO_ERROR;
1598 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001599 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 reply->writeInt32(0);
1601 reply->writeInt32(mDebugRegion);
1602 reply->writeInt32(mDebugBackground);
1603 return NO_ERROR;
1604 case 1013: {
1605 Mutex::Autolock _l(mStateLock);
1606 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1607 reply->writeInt32(hw.getPageFlipCount());
1608 }
1609 return NO_ERROR;
1610 }
1611 }
1612 return err;
1613}
1614
Mathias Agopian59119e62010-10-11 12:37:43 -07001615// ---------------------------------------------------------------------------
1616
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001617status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1618 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001619{
Mathias Agopian59119e62010-10-11 12:37:43 -07001620 if (!GLExtensions::getInstance().haveFramebufferObject())
1621 return INVALID_OPERATION;
1622
1623 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001624 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001625 const uint32_t hw_w = hw.getWidth();
1626 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001627 GLfloat u = 1;
1628 GLfloat v = 1;
1629
1630 // make sure to clear all GL error flags
1631 while ( glGetError() != GL_NO_ERROR ) ;
1632
1633 // create a FBO
1634 GLuint name, tname;
1635 glGenTextures(1, &tname);
1636 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001637 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1638 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001639 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001640 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001641 GLint tw = (2 << (31 - clz(hw_w)));
1642 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001643 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1644 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001645 u = GLfloat(hw_w) / tw;
1646 v = GLfloat(hw_h) / th;
1647 }
1648 glGenFramebuffersOES(1, &name);
1649 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001650 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1651 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001652
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001653 // redraw the screen entirely...
1654 glClearColor(0,0,0,1);
1655 glClear(GL_COLOR_BUFFER_BIT);
1656 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1657 const size_t count = layers.size();
1658 for (size_t i=0 ; i<count ; ++i) {
1659 const sp<LayerBase>& layer(layers[i]);
1660 layer->drawForSreenShot();
1661 }
1662
1663 // back to main framebuffer
1664 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1665 glDisable(GL_SCISSOR_TEST);
1666 glDeleteFramebuffersOES(1, &name);
1667
1668 *textureName = tname;
1669 *uOut = u;
1670 *vOut = v;
1671 return NO_ERROR;
1672}
1673
1674// ---------------------------------------------------------------------------
1675
1676status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1677{
1678 status_t result = PERMISSION_DENIED;
1679
1680 if (!GLExtensions::getInstance().haveFramebufferObject())
1681 return INVALID_OPERATION;
1682
1683 // get screen geometry
1684 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1685 const uint32_t hw_w = hw.getWidth();
1686 const uint32_t hw_h = hw.getHeight();
1687 const Region screenBounds(hw.bounds());
1688
1689 GLfloat u, v;
1690 GLuint tname;
1691 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1692 if (result != NO_ERROR) {
1693 return result;
1694 }
1695
1696 GLfloat vtx[8];
1697 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1698 glEnable(GL_TEXTURE_2D);
1699 glBindTexture(GL_TEXTURE_2D, tname);
1700 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1701 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1702 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1703 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1704 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1705 glVertexPointer(2, GL_FLOAT, 0, vtx);
1706
1707 class s_curve_interpolator {
1708 const float nbFrames, s, v;
1709 public:
1710 s_curve_interpolator(int nbFrames, float s)
1711 : nbFrames(1.0f / (nbFrames-1)), s(s),
1712 v(1.0f + expf(-s + 0.5f*s)) {
1713 }
1714 float operator()(int f) {
1715 const float x = f * nbFrames;
1716 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1717 }
1718 };
1719
1720 class v_stretch {
1721 const GLfloat hw_w, hw_h;
1722 public:
1723 v_stretch(uint32_t hw_w, uint32_t hw_h)
1724 : hw_w(hw_w), hw_h(hw_h) {
1725 }
1726 void operator()(GLfloat* vtx, float v) {
1727 const GLfloat w = hw_w + (hw_w * v);
1728 const GLfloat h = hw_h - (hw_h * v);
1729 const GLfloat x = (hw_w - w) * 0.5f;
1730 const GLfloat y = (hw_h - h) * 0.5f;
1731 vtx[0] = x; vtx[1] = y;
1732 vtx[2] = x; vtx[3] = y + h;
1733 vtx[4] = x + w; vtx[5] = y + h;
1734 vtx[6] = x + w; vtx[7] = y;
1735 }
1736 };
1737
1738 class h_stretch {
1739 const GLfloat hw_w, hw_h;
1740 public:
1741 h_stretch(uint32_t hw_w, uint32_t hw_h)
1742 : hw_w(hw_w), hw_h(hw_h) {
1743 }
1744 void operator()(GLfloat* vtx, float v) {
1745 const GLfloat w = hw_w - (hw_w * v);
1746 const GLfloat h = 1.0f;
1747 const GLfloat x = (hw_w - w) * 0.5f;
1748 const GLfloat y = (hw_h - h) * 0.5f;
1749 vtx[0] = x; vtx[1] = y;
1750 vtx[2] = x; vtx[3] = y + h;
1751 vtx[4] = x + w; vtx[5] = y + h;
1752 vtx[6] = x + w; vtx[7] = y;
1753 }
1754 };
1755
1756 // the full animation is 24 frames
1757 const int nbFrames = 12;
1758 s_curve_interpolator itr(nbFrames, 7.5f);
1759 s_curve_interpolator itg(nbFrames, 8.0f);
1760 s_curve_interpolator itb(nbFrames, 8.5f);
1761
1762 v_stretch vverts(hw_w, hw_h);
1763 glEnable(GL_BLEND);
1764 glBlendFunc(GL_ONE, GL_ONE);
1765 for (int i=0 ; i<nbFrames ; i++) {
1766 float x, y, w, h;
1767 const float vr = itr(i);
1768 const float vg = itg(i);
1769 const float vb = itb(i);
1770
1771 // clear screen
1772 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001773 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001774 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001775
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001776 // draw the red plane
1777 vverts(vtx, vr);
1778 glColorMask(1,0,0,1);
1779 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001780
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001781 // draw the green plane
1782 vverts(vtx, vg);
1783 glColorMask(0,1,0,1);
1784 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001785
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001786 // draw the blue plane
1787 vverts(vtx, vb);
1788 glColorMask(0,0,1,1);
1789 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001790
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001791 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001792 glDisable(GL_TEXTURE_2D);
1793 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001794 glColor4f(vg, vg, vg, 1);
1795 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1796 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001797 }
1798
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001799 h_stretch hverts(hw_w, hw_h);
1800 glDisable(GL_BLEND);
1801 glDisable(GL_TEXTURE_2D);
1802 glColorMask(1,1,1,1);
1803 for (int i=0 ; i<nbFrames ; i++) {
1804 const float v = itg(i);
1805 hverts(vtx, v);
1806 glClear(GL_COLOR_BUFFER_BIT);
1807 glColor4f(1-v, 1-v, 1-v, 1);
1808 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1809 hw.flip(screenBounds);
1810 }
1811
1812 glColorMask(1,1,1,1);
1813 glEnable(GL_SCISSOR_TEST);
1814 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1815 glDeleteTextures(1, &tname);
1816 return NO_ERROR;
1817}
1818
1819status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1820{
1821 status_t result = PERMISSION_DENIED;
1822
1823 if (!GLExtensions::getInstance().haveFramebufferObject())
1824 return INVALID_OPERATION;
1825
1826
1827 // get screen geometry
1828 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1829 const uint32_t hw_w = hw.getWidth();
1830 const uint32_t hw_h = hw.getHeight();
1831 const Region screenBounds(hw.bounds());
1832
1833 GLfloat u, v;
1834 GLuint tname;
1835 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1836 if (result != NO_ERROR) {
1837 return result;
1838 }
1839
1840 // back to main framebuffer
1841 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1842 glDisable(GL_SCISSOR_TEST);
1843
1844 GLfloat vtx[8];
1845 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1846 glEnable(GL_TEXTURE_2D);
1847 glBindTexture(GL_TEXTURE_2D, tname);
1848 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1849 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1850 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1851 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1852 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1853 glVertexPointer(2, GL_FLOAT, 0, vtx);
1854
1855 class s_curve_interpolator {
1856 const float nbFrames, s, v;
1857 public:
1858 s_curve_interpolator(int nbFrames, float s)
1859 : nbFrames(1.0f / (nbFrames-1)), s(s),
1860 v(1.0f + expf(-s + 0.5f*s)) {
1861 }
1862 float operator()(int f) {
1863 const float x = f * nbFrames;
1864 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1865 }
1866 };
1867
1868 class v_stretch {
1869 const GLfloat hw_w, hw_h;
1870 public:
1871 v_stretch(uint32_t hw_w, uint32_t hw_h)
1872 : hw_w(hw_w), hw_h(hw_h) {
1873 }
1874 void operator()(GLfloat* vtx, float v) {
1875 const GLfloat w = hw_w + (hw_w * v);
1876 const GLfloat h = hw_h - (hw_h * v);
1877 const GLfloat x = (hw_w - w) * 0.5f;
1878 const GLfloat y = (hw_h - h) * 0.5f;
1879 vtx[0] = x; vtx[1] = y;
1880 vtx[2] = x; vtx[3] = y + h;
1881 vtx[4] = x + w; vtx[5] = y + h;
1882 vtx[6] = x + w; vtx[7] = y;
1883 }
1884 };
1885
1886 class h_stretch {
1887 const GLfloat hw_w, hw_h;
1888 public:
1889 h_stretch(uint32_t hw_w, uint32_t hw_h)
1890 : hw_w(hw_w), hw_h(hw_h) {
1891 }
1892 void operator()(GLfloat* vtx, float v) {
1893 const GLfloat w = hw_w - (hw_w * v);
1894 const GLfloat h = 1.0f;
1895 const GLfloat x = (hw_w - w) * 0.5f;
1896 const GLfloat y = (hw_h - h) * 0.5f;
1897 vtx[0] = x; vtx[1] = y;
1898 vtx[2] = x; vtx[3] = y + h;
1899 vtx[4] = x + w; vtx[5] = y + h;
1900 vtx[6] = x + w; vtx[7] = y;
1901 }
1902 };
1903
Mathias Agopiana6546e52010-10-14 12:33:07 -07001904 // the full animation is 12 frames
1905 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001906 s_curve_interpolator itr(nbFrames, 7.5f);
1907 s_curve_interpolator itg(nbFrames, 8.0f);
1908 s_curve_interpolator itb(nbFrames, 8.5f);
1909
1910 h_stretch hverts(hw_w, hw_h);
1911 glDisable(GL_BLEND);
1912 glDisable(GL_TEXTURE_2D);
1913 glColorMask(1,1,1,1);
1914 for (int i=nbFrames-1 ; i>=0 ; i--) {
1915 const float v = itg(i);
1916 hverts(vtx, v);
1917 glClear(GL_COLOR_BUFFER_BIT);
1918 glColor4f(1-v, 1-v, 1-v, 1);
1919 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1920 hw.flip(screenBounds);
1921 }
1922
Mathias Agopiana6546e52010-10-14 12:33:07 -07001923 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001924 v_stretch vverts(hw_w, hw_h);
1925 glEnable(GL_BLEND);
1926 glBlendFunc(GL_ONE, GL_ONE);
1927 for (int i=nbFrames-1 ; i>=0 ; i--) {
1928 float x, y, w, h;
1929 const float vr = itr(i);
1930 const float vg = itg(i);
1931 const float vb = itb(i);
1932
1933 // clear screen
1934 glColorMask(1,1,1,1);
1935 glClear(GL_COLOR_BUFFER_BIT);
1936 glEnable(GL_TEXTURE_2D);
1937
1938 // draw the red plane
1939 vverts(vtx, vr);
1940 glColorMask(1,0,0,1);
1941 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1942
1943 // draw the green plane
1944 vverts(vtx, vg);
1945 glColorMask(0,1,0,1);
1946 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1947
1948 // draw the blue plane
1949 vverts(vtx, vb);
1950 glColorMask(0,0,1,1);
1951 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1952
1953 hw.flip(screenBounds);
1954 }
1955
1956 glColorMask(1,1,1,1);
1957 glEnable(GL_SCISSOR_TEST);
1958 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07001959 glDeleteTextures(1, &tname);
1960
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001961 return NO_ERROR;
1962}
1963
1964// ---------------------------------------------------------------------------
1965
Mathias Agopianabd671a2010-10-14 14:54:06 -07001966status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001967{
1968 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1969 if (!hw.canDraw()) {
1970 // we're already off
1971 return NO_ERROR;
1972 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07001973 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
1974 electronBeamOffAnimationImplLocked();
1975 }
1976
1977 // always clear the whole screen at the end of the animation
1978 glClearColor(0,0,0,1);
1979 glDisable(GL_SCISSOR_TEST);
1980 glClear(GL_COLOR_BUFFER_BIT);
1981 glEnable(GL_SCISSOR_TEST);
1982 hw.flip( Region(hw.bounds()) );
1983
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001984 hw.setCanDraw(false);
1985 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07001986}
1987
1988status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
1989{
Mathias Agopian59119e62010-10-11 12:37:43 -07001990 class MessageTurnElectronBeamOff : public MessageBase {
1991 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07001992 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07001993 status_t result;
1994 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07001995 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
1996 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07001997 }
1998 status_t getResult() const {
1999 return result;
2000 }
2001 virtual bool handler() {
2002 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002003 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002004 return true;
2005 }
2006 };
2007
Mathias Agopianabd671a2010-10-14 14:54:06 -07002008 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002009 status_t res = postMessageSync(msg);
2010 if (res == NO_ERROR) {
2011 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002012
2013 // work-around: when the power-manager calls us we activate the
2014 // animation. eventually, the "on" animation will be called
2015 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002016 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 }
2018 return res;
2019}
2020
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002021// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002022
Mathias Agopianabd671a2010-10-14 14:54:06 -07002023status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002024{
2025 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2026 if (hw.canDraw()) {
2027 // we're already on
2028 return NO_ERROR;
2029 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002030 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2031 electronBeamOnAnimationImplLocked();
2032 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002033 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002034
2035 // make sure to redraw the whole screen when the animation is done
2036 mDirtyRegion.set(hw.bounds());
2037 signalEvent();
2038
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002039 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002040}
2041
2042status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2043{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 class MessageTurnElectronBeamOn : public MessageBase {
2045 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002046 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 status_t result;
2048 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002049 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2050 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 }
2052 status_t getResult() const {
2053 return result;
2054 }
2055 virtual bool handler() {
2056 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002057 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002058 return true;
2059 }
2060 };
2061
Mathias Agopianabd671a2010-10-14 14:54:06 -07002062 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002063 return NO_ERROR;
2064}
2065
2066// ---------------------------------------------------------------------------
2067
Mathias Agopiandf85c452010-09-29 13:02:36 -07002068status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2069 sp<IMemoryHeap>* heap,
2070 uint32_t* w, uint32_t* h, PixelFormat* f,
2071 uint32_t sw, uint32_t sh)
2072{
2073 status_t result = PERMISSION_DENIED;
2074
2075 // only one display supported for now
2076 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2077 return BAD_VALUE;
2078
2079 if (!GLExtensions::getInstance().haveFramebufferObject())
2080 return INVALID_OPERATION;
2081
2082 // get screen geometry
2083 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2084 const uint32_t hw_w = hw.getWidth();
2085 const uint32_t hw_h = hw.getHeight();
2086
2087 if ((sw > hw_w) || (sh > hw_h))
2088 return BAD_VALUE;
2089
2090 sw = (!sw) ? hw_w : sw;
2091 sh = (!sh) ? hw_h : sh;
2092 const size_t size = sw * sh * 4;
2093
2094 // make sure to clear all GL error flags
2095 while ( glGetError() != GL_NO_ERROR ) ;
2096
2097 // create a FBO
2098 GLuint name, tname;
2099 glGenRenderbuffersOES(1, &tname);
2100 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2101 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2102 glGenFramebuffersOES(1, &name);
2103 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2104 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2105 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2106
2107 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2108 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2109
2110 // invert everything, b/c glReadPixel() below will invert the FB
2111 glViewport(0, 0, sw, sh);
2112 glMatrixMode(GL_PROJECTION);
2113 glPushMatrix();
2114 glLoadIdentity();
2115 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2116 glMatrixMode(GL_MODELVIEW);
2117
2118 // redraw the screen entirely...
2119 glClearColor(0,0,0,1);
2120 glClear(GL_COLOR_BUFFER_BIT);
2121 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2122 const size_t count = layers.size();
2123 for (size_t i=0 ; i<count ; ++i) {
2124 const sp<LayerBase>& layer(layers[i]);
2125 layer->drawForSreenShot();
2126 }
2127
2128 // XXX: this is needed on tegra
2129 glScissor(0, 0, sw, sh);
2130
2131 // check for errors and return screen capture
2132 if (glGetError() != GL_NO_ERROR) {
2133 // error while rendering
2134 result = INVALID_OPERATION;
2135 } else {
2136 // allocate shared memory large enough to hold the
2137 // screen capture
2138 sp<MemoryHeapBase> base(
2139 new MemoryHeapBase(size, 0, "screen-capture") );
2140 void* const ptr = base->getBase();
2141 if (ptr) {
2142 // capture the screen with glReadPixels()
2143 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2144 if (glGetError() == GL_NO_ERROR) {
2145 *heap = base;
2146 *w = sw;
2147 *h = sh;
2148 *f = PIXEL_FORMAT_RGBA_8888;
2149 result = NO_ERROR;
2150 }
2151 } else {
2152 result = NO_MEMORY;
2153 }
2154 }
2155
2156 glEnable(GL_SCISSOR_TEST);
2157 glViewport(0, 0, hw_w, hw_h);
2158 glMatrixMode(GL_PROJECTION);
2159 glPopMatrix();
2160 glMatrixMode(GL_MODELVIEW);
2161
2162
2163 } else {
2164 result = BAD_VALUE;
2165 }
2166
2167 // release FBO resources
2168 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2169 glDeleteRenderbuffersOES(1, &tname);
2170 glDeleteFramebuffersOES(1, &name);
2171 return result;
2172}
2173
2174
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002175status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2176 sp<IMemoryHeap>* heap,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002177 uint32_t* width, uint32_t* height, PixelFormat* format,
2178 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002179{
2180 // only one display supported for now
2181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2182 return BAD_VALUE;
2183
2184 if (!GLExtensions::getInstance().haveFramebufferObject())
2185 return INVALID_OPERATION;
2186
2187 class MessageCaptureScreen : public MessageBase {
2188 SurfaceFlinger* flinger;
2189 DisplayID dpy;
2190 sp<IMemoryHeap>* heap;
2191 uint32_t* w;
2192 uint32_t* h;
2193 PixelFormat* f;
Mathias Agopiandf85c452010-09-29 13:02:36 -07002194 uint32_t sw;
2195 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002196 status_t result;
2197 public:
2198 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002199 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
2200 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002201 : flinger(flinger), dpy(dpy),
Mathias Agopiandf85c452010-09-29 13:02:36 -07002202 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002203 {
2204 }
2205 status_t getResult() const {
2206 return result;
2207 }
2208 virtual bool handler() {
2209 Mutex::Autolock _l(flinger->mStateLock);
2210
2211 // if we have secure windows, never allow the screen capture
2212 if (flinger->mSecureFrameBuffer)
2213 return true;
2214
Mathias Agopiandf85c452010-09-29 13:02:36 -07002215 result = flinger->captureScreenImplLocked(dpy,
2216 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002217
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002218 return true;
2219 }
2220 };
2221
2222 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002223 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002224 status_t res = postMessageSync(msg);
2225 if (res == NO_ERROR) {
2226 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2227 }
2228 return res;
2229}
2230
2231// ---------------------------------------------------------------------------
2232
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002233sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2234{
2235 sp<Layer> result;
2236 Mutex::Autolock _l(mStateLock);
2237 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2238 return result;
2239}
2240
2241// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002242
Mathias Agopian96f08192010-06-02 23:28:45 -07002243Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002244 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002245{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002246}
2247
Mathias Agopian96f08192010-06-02 23:28:45 -07002248Client::~Client()
2249{
Mathias Agopian96f08192010-06-02 23:28:45 -07002250 const size_t count = mLayers.size();
2251 for (size_t i=0 ; i<count ; i++) {
2252 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2253 if (layer != 0) {
2254 mFlinger->removeLayer(layer);
2255 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002256 }
2257}
2258
Mathias Agopian96f08192010-06-02 23:28:45 -07002259status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002260 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002261}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002262
Mathias Agopian96f08192010-06-02 23:28:45 -07002263ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2264{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002265 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002266 mLayers.add(name, layer);
2267 return name;
2268}
2269
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002270void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002271{
Mathias Agopian96f08192010-06-02 23:28:45 -07002272 // we do a linear search here, because this doesn't happen often
2273 const size_t count = mLayers.size();
2274 for (size_t i=0 ; i<count ; i++) {
2275 if (mLayers.valueAt(i) == layer) {
2276 mLayers.removeItemsAt(i, 1);
2277 break;
2278 }
2279 }
2280}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002281sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2282 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002283 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2284 if (layer != 0) {
2285 lbc = layer.promote();
2286 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002287 }
2288 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002289}
2290
Mathias Agopian96f08192010-06-02 23:28:45 -07002291sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002292 return 0;
2293}
2294ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2295 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002296}
Mathias Agopian96f08192010-06-02 23:28:45 -07002297sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002298 ISurfaceComposerClient::surface_data_t* params, int pid,
2299 const String8& name,
2300 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002301 uint32_t flags)
2302{
Mathias Agopian96f08192010-06-02 23:28:45 -07002303 return mFlinger->createSurface(this, pid, name, params,
2304 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002305}
Mathias Agopian96f08192010-06-02 23:28:45 -07002306status_t Client::destroySurface(SurfaceID sid) {
2307 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308}
Mathias Agopian96f08192010-06-02 23:28:45 -07002309status_t Client::setState(int32_t count, const layer_state_t* states) {
2310 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002311}
2312
2313// ---------------------------------------------------------------------------
2314
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002315UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2316 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2317{
2318 const int pgsize = getpagesize();
2319 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2320
2321 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2322 "SurfaceFlinger Client control-block");
2323
2324 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2325 if (ctrlblk) { // construct the shared structure in-place.
2326 new(ctrlblk) SharedClient;
2327 }
2328}
2329
2330UserClient::~UserClient()
2331{
2332 if (ctrlblk) {
2333 ctrlblk->~SharedClient(); // destroy our shared-structure.
2334 }
2335
2336 /*
2337 * When a UserClient dies, it's unclear what to do exactly.
2338 * We could go ahead and destroy all surfaces linked to that client
2339 * however, it wouldn't be fair to the main Client
2340 * (usually the the window-manager), which might want to re-target
2341 * the layer to another UserClient.
2342 * I think the best is to do nothing, or not much; in most cases the
2343 * WM itself will go ahead and clean things up when it detects a client of
2344 * his has died.
2345 * The remaining question is what to display? currently we keep
2346 * just keep the current buffer.
2347 */
2348}
2349
2350status_t UserClient::initCheck() const {
2351 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2352}
2353
2354void UserClient::detachLayer(const Layer* layer)
2355{
2356 int32_t name = layer->getToken();
2357 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002358 int32_t mask = 1LU<<name;
2359 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2360 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2361 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002362 }
2363}
2364
2365sp<IMemoryHeap> UserClient::getControlBlock() const {
2366 return mCblkHeap;
2367}
2368
2369ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2370{
2371 int32_t name = NAME_NOT_FOUND;
2372 sp<Layer> layer(mFlinger->getLayer(sur));
2373 if (layer == 0) return name;
2374
Mathias Agopian579b3f82010-06-08 19:54:15 -07002375 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002376 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07002377 if ((name >= 0) && (layer->getClient() == this))
2378 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002379
2380 name = 0;
2381 do {
2382 int32_t mask = 1LU<<name;
2383 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2384 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002385 status_t err = layer->setToken(
2386 const_cast<UserClient*>(this), ctrlblk, name);
2387 if (err != NO_ERROR) {
2388 // free the name
2389 android_atomic_and(~mask, &mBitmap);
2390 name = err;
2391 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002392 break;
2393 }
2394 if (++name > 31)
2395 name = NO_MEMORY;
2396 } while(name >= 0);
2397
Mathias Agopian53503a92010-06-08 15:40:56 -07002398 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2399 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002400 return name;
2401}
2402
2403sp<ISurface> UserClient::createSurface(
2404 ISurfaceComposerClient::surface_data_t* params, int pid,
2405 const String8& name,
2406 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2407 uint32_t flags) {
2408 return 0;
2409}
2410status_t UserClient::destroySurface(SurfaceID sid) {
2411 return INVALID_OPERATION;
2412}
2413status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2414 return INVALID_OPERATION;
2415}
2416
2417// ---------------------------------------------------------------------------
2418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419GraphicPlane::GraphicPlane()
2420 : mHw(0)
2421{
2422}
2423
2424GraphicPlane::~GraphicPlane() {
2425 delete mHw;
2426}
2427
2428bool GraphicPlane::initialized() const {
2429 return mHw ? true : false;
2430}
2431
Mathias Agopian2b92d892010-02-08 15:49:35 -08002432int GraphicPlane::getWidth() const {
2433 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002434}
2435
Mathias Agopian2b92d892010-02-08 15:49:35 -08002436int GraphicPlane::getHeight() const {
2437 return mHeight;
2438}
2439
2440void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2441{
2442 mHw = hw;
2443
2444 // initialize the display orientation transform.
2445 // it's a constant that should come from the display driver.
2446 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2447 char property[PROPERTY_VALUE_MAX];
2448 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2449 //displayOrientation
2450 switch (atoi(property)) {
2451 case 90:
2452 displayOrientation = ISurfaceComposer::eOrientation90;
2453 break;
2454 case 270:
2455 displayOrientation = ISurfaceComposer::eOrientation270;
2456 break;
2457 }
2458 }
2459
2460 const float w = hw->getWidth();
2461 const float h = hw->getHeight();
2462 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2463 &mDisplayTransform);
2464 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2465 mDisplayWidth = h;
2466 mDisplayHeight = w;
2467 } else {
2468 mDisplayWidth = w;
2469 mDisplayHeight = h;
2470 }
2471
2472 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473}
2474
2475status_t GraphicPlane::orientationToTransfrom(
2476 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002477{
2478 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479 switch (orientation) {
2480 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002481 flags = Transform::ROT_0;
2482 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002484 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 break;
2486 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002487 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488 break;
2489 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002490 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 break;
2492 default:
2493 return BAD_VALUE;
2494 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002495 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496 return NO_ERROR;
2497}
2498
2499status_t GraphicPlane::setOrientation(int orientation)
2500{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002501 // If the rotation can be handled in hardware, this is where
2502 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002503
2504 const DisplayHardware& hw(displayHardware());
2505 const float w = mDisplayWidth;
2506 const float h = mDisplayHeight;
2507 mWidth = int(w);
2508 mHeight = int(h);
2509
2510 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002511 GraphicPlane::orientationToTransfrom(orientation, w, h,
2512 &orientationTransform);
2513 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2514 mWidth = int(h);
2515 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002517
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002518 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002519 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 return NO_ERROR;
2521}
2522
2523const DisplayHardware& GraphicPlane::displayHardware() const {
2524 return *mHw;
2525}
2526
Mathias Agopian59119e62010-10-11 12:37:43 -07002527DisplayHardware& GraphicPlane::editDisplayHardware() {
2528 return *mHw;
2529}
2530
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002531const Transform& GraphicPlane::transform() const {
2532 return mGlobalTransform;
2533}
2534
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002535EGLDisplay GraphicPlane::getEGLDisplay() const {
2536 return mHw->getEGLDisplay();
2537}
2538
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002539// ---------------------------------------------------------------------------
2540
2541}; // namespace android