blob: 09b084e72d117d21fb2d237e0ab4346d092a51ed [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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070055#include "DisplayHardware/HWComposer.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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#define DISPLAY_COUNT 1
65
66namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067// ---------------------------------------------------------------------------
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069SurfaceFlinger::SurfaceFlinger()
70 : BnSurfaceComposer(), Thread(false),
71 mTransactionFlags(0),
72 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070073 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070076 mHardwareTest("android.permission.HARDWARE_TEST"),
77 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070078 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070079 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070081 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 mDeferReleaseConsole(false),
83 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070084 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070086 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mDebugBackground(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070089 mDebugDisableHWC(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070090 mDebugInSwapBuffers(0),
91 mLastSwapBufferTime(0),
92 mDebugInTransaction(0),
93 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070094 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mConsoleSignals(0),
96 mSecureFrameBuffer(0)
97{
98 init();
99}
100
101void SurfaceFlinger::init()
102{
103 LOGI("SurfaceFlinger is starting");
104
105 // debugging stuff...
106 char value[PROPERTY_VALUE_MAX];
107 property_get("debug.sf.showupdates", value, "0");
108 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 property_get("debug.sf.showbackground", value, "0");
110 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111
Mathias Agopian78fd5012010-04-20 14:51:04 -0700112 LOGI_IF(mDebugRegion, "showupdates enabled");
113 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114}
115
116SurfaceFlinger::~SurfaceFlinger()
117{
118 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700121sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700123 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian7e27f052010-05-28 14:22:23 -0700126sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127{
Mathias Agopian96f08192010-06-02 23:28:45 -0700128 sp<ISurfaceComposerClient> bclient;
129 sp<Client> client(new Client(this));
130 status_t err = client->initCheck();
131 if (err == NO_ERROR) {
132 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 return bclient;
135}
136
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700137sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
138{
139 sp<ISurfaceComposerClient> bclient;
140 sp<UserClient> client(new UserClient(this));
141 status_t err = client->initCheck();
142 if (err == NO_ERROR) {
143 bclient = client;
144 }
145 return bclient;
146}
147
148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
150{
151 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
152 const GraphicPlane& plane(mGraphicPlanes[dpy]);
153 return plane;
154}
155
156GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
157{
158 return const_cast<GraphicPlane&>(
159 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
160}
161
162void SurfaceFlinger::bootFinished()
163{
164 const nsecs_t now = systemTime();
165 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700166 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700167 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700168 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169}
170
171void SurfaceFlinger::onFirstRef()
172{
173 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
174
175 // Wait for the main thread to be done with its initialization
176 mReadyToRunBarrier.wait();
177}
178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179static inline uint16_t pack565(int r, int g, int b) {
180 return (r<<11)|(g<<5)|b;
181}
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183status_t SurfaceFlinger::readyToRun()
184{
185 LOGI( "SurfaceFlinger's main thread ready to run. "
186 "Initializing graphics H/W...");
187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 // we only support one display currently
189 int dpy = 0;
190
191 {
192 // initialize the main display
193 GraphicPlane& plane(graphicPlane(dpy));
194 DisplayHardware* const hw = new DisplayHardware(this, dpy);
195 plane.setDisplayHardware(hw);
196 }
197
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700198 // create the shared control-block
199 mServerHeap = new MemoryHeapBase(4096,
200 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
201 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700202
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700203 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
204 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700205
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700206 new(mServerCblk) surface_flinger_cblk_t;
207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 // initialize primary screen
209 // (other display should be initialized in the same manner, but
210 // asynchronously, as they could come and go. None of this is supported
211 // yet).
212 const GraphicPlane& plane(graphicPlane(dpy));
213 const DisplayHardware& hw = plane.displayHardware();
214 const uint32_t w = hw.getWidth();
215 const uint32_t h = hw.getHeight();
216 const uint32_t f = hw.getFormat();
217 hw.makeCurrent();
218
219 // initialize the shared control block
220 mServerCblk->connected |= 1<<dpy;
221 display_cblk_t* dcblk = mServerCblk->displays + dpy;
222 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800223 dcblk->w = plane.getWidth();
224 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 dcblk->format = f;
226 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
227 dcblk->xdpi = hw.getDpiX();
228 dcblk->ydpi = hw.getDpiY();
229 dcblk->fps = hw.getRefreshRate();
230 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231
232 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700234 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 glEnableClientState(GL_VERTEX_ARRAY);
236 glEnable(GL_SCISSOR_TEST);
237 glShadeModel(GL_FLAT);
238 glDisable(GL_DITHER);
239 glDisable(GL_CULL_FACE);
240
241 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
242 const uint16_t g1 = pack565(0x17,0x2f,0x17);
243 const uint16_t textureData[4] = { g0, g1, g1, g0 };
244 glGenTextures(1, &mWormholeTexName);
245 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
246 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
247 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
248 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
249 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
250 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
251 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
252
253 glViewport(0, 0, w, h);
254 glMatrixMode(GL_PROJECTION);
255 glLoadIdentity();
256 glOrthof(0, w, h, 0, 0, 1);
257
258 LayerDim::initDimmer(this, w, h);
259
260 mReadyToRunBarrier.open();
261
262 /*
263 * We're now ready to accept clients...
264 */
265
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700266 // start boot animation
267 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700268
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 return NO_ERROR;
270}
271
272// ----------------------------------------------------------------------------
273#if 0
274#pragma mark -
275#pragma mark Events Handler
276#endif
277
278void SurfaceFlinger::waitForEvent()
279{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700280 while (true) {
281 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800282 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700283 if (UNLIKELY(isFrozen())) {
284 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700285 const nsecs_t now = systemTime();
286 if (mFreezeDisplayTime == 0) {
287 mFreezeDisplayTime = now;
288 }
289 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
290 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700291 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700292
Mathias Agopianbb641242010-05-18 17:06:55 -0700293 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800294
295 // see if we timed out
296 if (isFrozen()) {
297 const nsecs_t now = systemTime();
298 nsecs_t frozenTime = (now - mFreezeDisplayTime);
299 if (frozenTime >= freezeDisplayTimeout) {
300 // we timed out and are still frozen
301 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
302 mFreezeDisplay, mFreezeCount);
303 mFreezeDisplayTime = 0;
304 mFreezeCount = 0;
305 mFreezeDisplay = false;
306 }
307 }
308
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700309 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700310 switch (msg->what) {
311 case MessageQueue::INVALIDATE:
312 // invalidate message, just return to the main loop
313 return;
314 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 }
317}
318
319void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700320 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
323void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700324 // this is the IPC call
325 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326}
327
Mathias Agopianbb641242010-05-18 17:06:55 -0700328status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
329 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330{
Mathias Agopianbb641242010-05-18 17:06:55 -0700331 return mEventQueue.postMessage(msg, reltime, flags);
332}
333
334status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
335 nsecs_t reltime, uint32_t flags)
336{
337 status_t res = mEventQueue.postMessage(msg, reltime, flags);
338 if (res == NO_ERROR) {
339 msg->wait();
340 }
341 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342}
343
344// ----------------------------------------------------------------------------
345#if 0
346#pragma mark -
347#pragma mark Main loop
348#endif
349
350bool SurfaceFlinger::threadLoop()
351{
352 waitForEvent();
353
354 // check for transactions
355 if (UNLIKELY(mConsoleSignals)) {
356 handleConsoleEvents();
357 }
358
359 if (LIKELY(mTransactionCount == 0)) {
360 // if we're in a global transaction, don't do anything.
361 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
362 uint32_t transactionFlags = getTransactionFlags(mask);
363 if (LIKELY(transactionFlags)) {
364 handleTransaction(transactionFlags);
365 }
366 }
367
368 // post surfaces (if needed)
369 handlePageFlip();
370
Mathias Agopiana350ff92010-08-10 17:14:02 -0700371 if (UNLIKELY(mHwWorkListDirty)) {
372 // build the h/w work list
373 handleWorkList();
374 }
375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700377 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700379
380 const int index = hw.getCurrentBufferIndex();
381 GraphicLog& logger(GraphicLog::getInstance());
382
383 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 handleRepaint();
385
Mathias Agopian74faca22009-09-17 16:18:16 -0700386 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700387 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700388 hw.compositionComplete();
389
Mathias Agopian35b48d12010-09-13 22:57:58 -0700390 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700391 postFramebuffer();
392
Mathias Agopianc479e182010-09-15 12:29:18 -0700393 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395
Mathias Agopian35b48d12010-09-13 22:57:58 -0700396 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 } else {
398 // pretend we did the post
399 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 usleep(16667); // 60 fps period
401 }
402 return true;
403}
404
405void SurfaceFlinger::postFramebuffer()
406{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 if (!mInvalidRegion.isEmpty()) {
408 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700409 const nsecs_t now = systemTime();
410 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700412 mLastSwapBufferTime = systemTime() - now;
413 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 }
416}
417
418void SurfaceFlinger::handleConsoleEvents()
419{
420 // something to do with the console
421 const DisplayHardware& hw = graphicPlane(0).displayHardware();
422
423 int what = android_atomic_and(0, &mConsoleSignals);
424 if (what & eConsoleAcquired) {
425 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700426 // this is a temporary work-around, eventually this should be called
427 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700428 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 }
430
Mathias Agopian59119e62010-10-11 12:37:43 -0700431 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700432 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 hw.releaseScreen();
435 }
436
437 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700438 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 hw.releaseScreen();
440 } else {
441 mDeferReleaseConsole = true;
442 }
443 }
444
445 mDirtyRegion.set(hw.bounds());
446}
447
448void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
449{
Mathias Agopian3d579642009-06-04 18:46:21 -0700450 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451
Mathias Agopian4da75192010-08-10 17:19:56 -0700452 /*
453 * Perform and commit the transaction
454 */
455
Mathias Agopian3d579642009-06-04 18:46:21 -0700456 { // scope for the lock
457 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700458 const nsecs_t now = systemTime();
459 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700460 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700461 mLastTransactionTime = systemTime() - now;
462 mDebugInTransaction = 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700463 mHwWorkListDirty = true;
Mathias Agopian4da75192010-08-10 17:19:56 -0700464 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700465 }
466
Mathias Agopian4da75192010-08-10 17:19:56 -0700467 /*
468 * Clean-up all layers that went away
469 * (do this without the lock held)
470 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700471
Mathias Agopian3d579642009-06-04 18:46:21 -0700472 const size_t count = ditchedLayers.size();
473 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700474 if (ditchedLayers[i] != 0) {
475 //LOGD("ditching layer %p", ditchedLayers[i].get());
476 ditchedLayers[i]->ditch();
477 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700478 }
479}
480
481void SurfaceFlinger::handleTransactionLocked(
482 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
483{
484 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 const size_t count = currentLayers.size();
486
487 /*
488 * Traversal of the children
489 * (perform the transaction for each of them if needed)
490 */
491
492 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
493 if (layersNeedTransaction) {
494 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700495 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
497 if (!trFlags) continue;
498
499 const uint32_t flags = layer->doTransaction(0);
500 if (flags & Layer::eVisibleRegion)
501 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 }
503 }
504
505 /*
506 * Perform our own transaction if needed
507 */
508
509 if (transactionFlags & eTransactionNeeded) {
510 if (mCurrentState.orientation != mDrawingState.orientation) {
511 // the orientation has changed, recompute all visible regions
512 // and invalidate everything.
513
514 const int dpy = 0;
515 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700516 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 GraphicPlane& plane(graphicPlane(dpy));
518 plane.setOrientation(orientation);
519
520 // update the shared control block
521 const DisplayHardware& hw(plane.displayHardware());
522 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
523 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800524 dcblk->w = plane.getWidth();
525 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526
527 mVisibleRegionsDirty = true;
528 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 }
530
531 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
532 // freezing or unfreezing the display -> trigger animation if needed
533 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800534 if (mFreezeDisplay)
535 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 }
537
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700538 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
539 // layers have been added
540 mVisibleRegionsDirty = true;
541 }
542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 // some layers might have been removed, so
544 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700546 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700548 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700549 const size_t count = previousLayers.size();
550 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700551 const sp<LayerBase>& layer(previousLayers[i]);
552 if (currentLayers.indexOf( layer ) < 0) {
553 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700554 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700555 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700556 }
557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 }
560
561 commitTransaction();
562}
563
564sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
565{
566 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
567}
568
569void SurfaceFlinger::computeVisibleRegions(
570 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
571{
572 const GraphicPlane& plane(graphicPlane(0));
573 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700574 const DisplayHardware& hw(plane.displayHardware());
575 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576
577 Region aboveOpaqueLayers;
578 Region aboveCoveredLayers;
579 Region dirty;
580
581 bool secureFrameBuffer = false;
582
583 size_t i = currentLayers.size();
584 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 layer->validateVisibility(planeTransform);
587
588 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700589 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590
Mathias Agopianab028732010-03-16 16:41:46 -0700591 /*
592 * opaqueRegion: area of a surface that is fully opaque.
593 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700595
596 /*
597 * visibleRegion: area of a surface that is visible on screen
598 * and not fully transparent. This is essentially the layer's
599 * footprint minus the opaque regions above it.
600 * Areas covered by a translucent surface are considered visible.
601 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700603
604 /*
605 * coveredRegion: area of a surface that is covered by all
606 * visible regions above it (which includes the translucent areas).
607 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700609
610
611 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700612 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700614 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700616 visibleRegion.andSelf(screenRegion);
617 if (!visibleRegion.isEmpty()) {
618 // Remove the transparent area from the visible region
619 if (translucent) {
620 visibleRegion.subtractSelf(layer->transparentRegionScreen);
621 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622
Mathias Agopianab028732010-03-16 16:41:46 -0700623 // compute the opaque region
624 const int32_t layerOrientation = layer->getOrientation();
625 if (s.alpha==255 && !translucent &&
626 ((layerOrientation & Transform::ROT_INVALID) == false)) {
627 // the opaque region is the layer's footprint
628 opaqueRegion = visibleRegion;
629 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 }
631 }
632
Mathias Agopianab028732010-03-16 16:41:46 -0700633 // Clip the covered region to the visible region
634 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
635
636 // Update aboveCoveredLayers for next (lower) layer
637 aboveCoveredLayers.orSelf(visibleRegion);
638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 // subtract the opaque region covered by the layers above us
640 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641
642 // compute this layer's dirty region
643 if (layer->contentDirty) {
644 // we need to invalidate the whole region
645 dirty = visibleRegion;
646 // as well, as the old visible region
647 dirty.orSelf(layer->visibleRegionScreen);
648 layer->contentDirty = false;
649 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700650 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700651 * the exposed region consists of two components:
652 * 1) what's VISIBLE now and was COVERED before
653 * 2) what's EXPOSED now less what was EXPOSED before
654 *
655 * note that (1) is conservative, we start with the whole
656 * visible region but only keep what used to be covered by
657 * something -- which mean it may have been exposed.
658 *
659 * (2) handles areas that were not covered by anything but got
660 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700661 */
Mathias Agopianab028732010-03-16 16:41:46 -0700662 const Region newExposed = visibleRegion - coveredRegion;
663 const Region oldVisibleRegion = layer->visibleRegionScreen;
664 const Region oldCoveredRegion = layer->coveredRegionScreen;
665 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
666 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 }
668 dirty.subtractSelf(aboveOpaqueLayers);
669
670 // accumulate to the screen dirty region
671 dirtyRegion.orSelf(dirty);
672
Mathias Agopianab028732010-03-16 16:41:46 -0700673 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700675
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 // Store the visible region is screen space
677 layer->setVisibleRegion(visibleRegion);
678 layer->setCoveredRegion(coveredRegion);
679
Mathias Agopian97011222009-07-28 10:57:27 -0700680 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 if (layer->isSecure() && !visibleRegion.isEmpty()) {
682 secureFrameBuffer = true;
683 }
684 }
685
Mathias Agopian97011222009-07-28 10:57:27 -0700686 // invalidate the areas where a layer was removed
687 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
688 mDirtyRegionRemovedLayer.clear();
689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 mSecureFrameBuffer = secureFrameBuffer;
691 opaqueRegion = aboveOpaqueLayers;
692}
693
694
695void SurfaceFlinger::commitTransaction()
696{
697 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700698 mResizeTransationPending = false;
699 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700}
701
702void SurfaceFlinger::handlePageFlip()
703{
704 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700705 LayerVector& currentLayers(
706 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 visibleRegions |= lockPageFlip(currentLayers);
708
709 const DisplayHardware& hw = graphicPlane(0).displayHardware();
710 const Region screenRegion(hw.bounds());
711 if (visibleRegions) {
712 Region opaqueRegion;
713 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700714
715 /*
716 * rebuild the visible layer list
717 */
718 mVisibleLayersSortedByZ.clear();
719 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
720 size_t count = currentLayers.size();
721 mVisibleLayersSortedByZ.setCapacity(count);
722 for (size_t i=0 ; i<count ; i++) {
723 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
724 mVisibleLayersSortedByZ.add(currentLayers[i]);
725 }
726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 mWormholeRegion = screenRegion.subtract(opaqueRegion);
728 mVisibleRegionsDirty = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700729 mHwWorkListDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 }
731
732 unlockPageFlip(currentLayers);
733 mDirtyRegion.andSelf(screenRegion);
734}
735
736bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
737{
738 bool recomputeVisibleRegions = false;
739 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700740 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700742 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 layer->lockPageFlip(recomputeVisibleRegions);
744 }
745 return recomputeVisibleRegions;
746}
747
748void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
749{
750 const GraphicPlane& plane(graphicPlane(0));
751 const Transform& planeTransform(plane.transform());
752 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700753 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700755 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 layer->unlockPageFlip(planeTransform, mDirtyRegion);
757 }
758}
759
Mathias Agopiana350ff92010-08-10 17:14:02 -0700760void SurfaceFlinger::handleWorkList()
761{
762 mHwWorkListDirty = false;
763 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
764 if (hwc.initCheck() == NO_ERROR) {
765 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
766 const size_t count = currentLayers.size();
767 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700768 hwc_layer_t* const cur(hwc.getLayers());
769 for (size_t i=0 ; cur && i<count ; i++) {
770 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700771 if (mDebugDisableHWC) {
772 cur[i].compositionType = HWC_FRAMEBUFFER;
773 cur[i].flags |= HWC_SKIP_LAYER;
774 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700775 }
776 }
777}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700778
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779void SurfaceFlinger::handleRepaint()
780{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700781 // compute the invalid region
782 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783
784 if (UNLIKELY(mDebugRegion)) {
785 debugFlashRegions();
786 }
787
Mathias Agopianb8a55602009-06-26 19:06:36 -0700788 // set the frame buffer
789 const DisplayHardware& hw(graphicPlane(0).displayHardware());
790 glMatrixMode(GL_MODELVIEW);
791 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792
793 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700794 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
795 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700796 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700797 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
798 // takes a rectangle, we must make sure to update that whole
799 // rectangle in that case
800 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700801 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700802 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 mDirtyRegion.set(mInvalidRegion.bounds());
804 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700805 // in the BUFFER_PRESERVED case, obviously, we can update only
806 // what's needed and nothing more.
807 // NOTE: this is NOT a common case, as preserving the backbuffer
808 // is costly and usually involves copying the whole update back.
809 }
810 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700811 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700812 // We need to redraw the rectangle that will be updated
813 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700814 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700815 // rectangle instead of a region (see DisplayHardware::flip())
816 mDirtyRegion.set(mInvalidRegion.bounds());
817 } else {
818 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 mDirtyRegion.set(hw.bounds());
820 mInvalidRegion = mDirtyRegion;
821 }
822 }
823
824 // compose all surfaces
825 composeSurfaces(mDirtyRegion);
826
827 // clear the dirty regions
828 mDirtyRegion.clear();
829}
830
831void SurfaceFlinger::composeSurfaces(const Region& dirty)
832{
833 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
834 // should never happen unless the window manager has a bug
835 // draw something...
836 drawWormhole();
837 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700838
839 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700840 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700841 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700842
843 const DisplayHardware& hw(graphicPlane(0).displayHardware());
844 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700845 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700846
Mathias Agopian45721772010-08-12 15:03:26 -0700847 LOGE_IF(cur && hwc.getNumLayers() != count,
848 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
849 hwc.getNumLayers(), count);
850
851 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700852 if (hwc.initCheck() == NO_ERROR) {
853 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
854 }
Mathias Agopian45721772010-08-12 15:03:26 -0700855
856 /*
857 * update the per-frame h/w composer data for each layer
858 * and build the transparent region of the FB
859 */
860 Region transparent;
861 if (cur) {
862 for (size_t i=0 ; i<count ; i++) {
863 const sp<LayerBase>& layer(layers[i]);
864 layer->setPerFrameData(&cur[i]);
865 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
866 if (!(layer->needsBlending())) {
867 transparent.orSelf(layer->visibleRegionScreen);
868 }
869 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700870 }
871 err = hwc.prepare();
872 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
873 }
874
Mathias Agopian45721772010-08-12 15:03:26 -0700875 /*
876 * clear the area of the FB that need to be transparent
877 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700878 transparent.andSelf(dirty);
879 if (!transparent.isEmpty()) {
880 glClearColor(0,0,0,0);
881 Region::const_iterator it = transparent.begin();
882 Region::const_iterator const end = transparent.end();
883 const int32_t height = hw.getHeight();
884 while (it != end) {
885 const Rect& r(*it++);
886 const GLint sy = height - (r.top + r.height());
887 glScissor(r.left, sy, r.width(), r.height());
888 glClear(GL_COLOR_BUFFER_BIT);
889 }
890 }
Mathias Agopian45721772010-08-12 15:03:26 -0700891
892
893 /*
894 * and then, render the layers targeted at the framebuffer
895 */
896 for (size_t i=0 ; i<count ; i++) {
897 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700898 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
899 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700900 // skip layers handled by the HAL
901 continue;
902 }
903 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700904
Mathias Agopian45721772010-08-12 15:03:26 -0700905 const sp<LayerBase>& layer(layers[i]);
906 const Region clip(dirty.intersect(layer->visibleRegionScreen));
907 if (!clip.isEmpty()) {
908 layer->draw(clip);
909 }
910 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911}
912
913void SurfaceFlinger::unlockClients()
914{
915 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
916 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700917 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700919 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 layer->finishPageFlip();
921 }
922}
923
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924void SurfaceFlinger::debugFlashRegions()
925{
Mathias Agopian0a917752010-06-14 21:20:00 -0700926 const DisplayHardware& hw(graphicPlane(0).displayHardware());
927 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700928
Mathias Agopian0a917752010-06-14 21:20:00 -0700929 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
930 (flags & DisplayHardware::BUFFER_PRESERVED))) {
931 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
932 mDirtyRegion.bounds() : hw.bounds());
933 composeSurfaces(repaint);
934 }
935
936 TextureManager::deactivateTextures();
937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 glDisable(GL_BLEND);
939 glDisable(GL_DITHER);
940 glDisable(GL_SCISSOR_TEST);
941
Mathias Agopian0926f502009-05-04 14:17:04 -0700942 static int toggle = 0;
943 toggle = 1 - toggle;
944 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700945 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700946 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700947 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700948 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949
Mathias Agopian20f68782009-05-11 00:03:41 -0700950 Region::const_iterator it = mDirtyRegion.begin();
951 Region::const_iterator const end = mDirtyRegion.end();
952 while (it != end) {
953 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954 GLfloat vertices[][2] = {
955 { r.left, r.top },
956 { r.left, r.bottom },
957 { r.right, r.bottom },
958 { r.right, r.top }
959 };
960 glVertexPointer(2, GL_FLOAT, 0, vertices);
961 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
962 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700963
Mathias Agopianb8a55602009-06-26 19:06:36 -0700964 if (mInvalidRegion.isEmpty()) {
965 mDirtyRegion.dump("mDirtyRegion");
966 mInvalidRegion.dump("mInvalidRegion");
967 }
968 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969
970 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700971 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972
973 glEnable(GL_SCISSOR_TEST);
974 //mDirtyRegion.dump("mDirtyRegion");
975}
976
977void SurfaceFlinger::drawWormhole() const
978{
979 const Region region(mWormholeRegion.intersect(mDirtyRegion));
980 if (region.isEmpty())
981 return;
982
983 const DisplayHardware& hw(graphicPlane(0).displayHardware());
984 const int32_t width = hw.getWidth();
985 const int32_t height = hw.getHeight();
986
987 glDisable(GL_BLEND);
988 glDisable(GL_DITHER);
989
990 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700991 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700992 Region::const_iterator it = region.begin();
993 Region::const_iterator const end = region.end();
994 while (it != end) {
995 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 const GLint sy = height - (r.top + r.height());
997 glScissor(r.left, sy, r.width(), r.height());
998 glClear(GL_COLOR_BUFFER_BIT);
999 }
1000 } else {
1001 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1002 { width, height }, { 0, height } };
1003 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1004 glVertexPointer(2, GL_SHORT, 0, vertices);
1005 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1006 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001007#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001008 if (GLExtensions::getInstance().haveTextureExternal()) {
1009 glDisable(GL_TEXTURE_EXTERNAL_OES);
1010 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001011#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 glEnable(GL_TEXTURE_2D);
1013 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1014 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1015 glMatrixMode(GL_TEXTURE);
1016 glLoadIdentity();
1017 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001018 Region::const_iterator it = region.begin();
1019 Region::const_iterator const end = region.end();
1020 while (it != end) {
1021 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 const GLint sy = height - (r.top + r.height());
1023 glScissor(r.left, sy, r.width(), r.height());
1024 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1025 }
1026 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1027 }
1028}
1029
1030void SurfaceFlinger::debugShowFPS() const
1031{
1032 static int mFrameCount;
1033 static int mLastFrameCount = 0;
1034 static nsecs_t mLastFpsTime = 0;
1035 static float mFps = 0;
1036 mFrameCount++;
1037 nsecs_t now = systemTime();
1038 nsecs_t diff = now - mLastFpsTime;
1039 if (diff > ms2ns(250)) {
1040 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1041 mLastFpsTime = now;
1042 mLastFrameCount = mFrameCount;
1043 }
1044 // XXX: mFPS has the value we want
1045 }
1046
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001047status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048{
1049 Mutex::Autolock _l(mStateLock);
1050 addLayer_l(layer);
1051 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1052 return NO_ERROR;
1053}
1054
Mathias Agopian96f08192010-06-02 23:28:45 -07001055status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1056{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001057 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001058 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1059}
1060
1061ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1062 const sp<LayerBaseClient>& lbc)
1063{
1064 Mutex::Autolock _l(mStateLock);
1065
1066 // attach this layer to the client
1067 ssize_t name = client->attachLayer(lbc);
1068
1069 // add this layer to the current state list
1070 addLayer_l(lbc);
1071
1072 return name;
1073}
1074
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001075status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076{
1077 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001078 status_t err = purgatorizeLayer_l(layer);
1079 if (err == NO_ERROR)
1080 setTransactionFlags(eTransactionNeeded);
1081 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082}
1083
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001084status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001086 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1087 if (lbc != 0) {
1088 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1089 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1091 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001092 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 return NO_ERROR;
1094 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001095 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096}
1097
Mathias Agopian9a112062009-04-17 19:36:26 -07001098status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1099{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001100 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001101 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001102
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001103 layerBase->onRemoved();
1104
Mathias Agopian3d579642009-06-04 18:46:21 -07001105 // it's possible that we don't find a layer, because it might
1106 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001107 // from the user because there is a race between Client::destroySurface(),
1108 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001109 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1110}
1111
Mathias Agopian96f08192010-06-02 23:28:45 -07001112status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113{
Mathias Agopian96f08192010-06-02 23:28:45 -07001114 layer->forceVisibilityTransaction();
1115 setTransactionFlags(eTraversalNeeded);
1116 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117}
1118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1120{
1121 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1122}
1123
Mathias Agopianbb641242010-05-18 17:06:55 -07001124uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125{
1126 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1127 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001128 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 }
1130 return old;
1131}
1132
1133void SurfaceFlinger::openGlobalTransaction()
1134{
1135 android_atomic_inc(&mTransactionCount);
1136}
1137
1138void SurfaceFlinger::closeGlobalTransaction()
1139{
1140 if (android_atomic_dec(&mTransactionCount) == 1) {
1141 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001142
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001143 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001144 // take effect before returning.
1145 Mutex::Autolock _l(mStateLock);
1146 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001147 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1148 if (CC_UNLIKELY(err != NO_ERROR)) {
1149 // just in case something goes wrong in SF, return to the
1150 // called after a few seconds.
1151 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1152 mResizeTransationPending = false;
1153 break;
1154 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001155 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 }
1157}
1158
1159status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1160{
1161 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1162 return BAD_VALUE;
1163
1164 Mutex::Autolock _l(mStateLock);
1165 mCurrentState.freezeDisplay = 1;
1166 setTransactionFlags(eTransactionNeeded);
1167
1168 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001169 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 return NO_ERROR;
1171}
1172
1173status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1174{
1175 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1176 return BAD_VALUE;
1177
1178 Mutex::Autolock _l(mStateLock);
1179 mCurrentState.freezeDisplay = 0;
1180 setTransactionFlags(eTransactionNeeded);
1181
1182 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001183 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 return NO_ERROR;
1185}
1186
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001187int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001188 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189{
1190 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1191 return BAD_VALUE;
1192
1193 Mutex::Autolock _l(mStateLock);
1194 if (mCurrentState.orientation != orientation) {
1195 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001196 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 mCurrentState.orientation = orientation;
1198 setTransactionFlags(eTransactionNeeded);
1199 mTransactionCV.wait(mStateLock);
1200 } else {
1201 orientation = BAD_VALUE;
1202 }
1203 }
1204 return orientation;
1205}
1206
Mathias Agopian96f08192010-06-02 23:28:45 -07001207sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001208 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1210 uint32_t flags)
1211{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001212 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001214
1215 if (int32_t(w|h) < 0) {
1216 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1217 int(w), int(h));
1218 return surfaceHandle;
1219 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001222 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 switch (flags & eFXSurfaceMask) {
1224 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001225 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1226 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 break;
1228 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001229 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230 break;
1231 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001232 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 break;
1234 }
1235
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001236 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001237 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001238 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001239 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001240
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001242 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001243 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001244 params->identity = surfaceHandle->getIdentity();
1245 params->width = w;
1246 params->height = h;
1247 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001248 if (normalLayer != 0) {
1249 Mutex::Autolock _l(mStateLock);
1250 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1251 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001252 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001253
Mathias Agopian96f08192010-06-02 23:28:45 -07001254 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 }
1256
1257 return surfaceHandle;
1258}
1259
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001260sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001261 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001262 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001263 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264{
1265 // initialize the surfaces
1266 switch (format) { // TODO: take h/w into account
1267 case PIXEL_FORMAT_TRANSPARENT:
1268 case PIXEL_FORMAT_TRANSLUCENT:
1269 format = PIXEL_FORMAT_RGBA_8888;
1270 break;
1271 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001272#ifdef NO_RGBX_8888
1273 format = PIXEL_FORMAT_RGB_565;
1274#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001275 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001276#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 break;
1278 }
1279
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001280#ifdef NO_RGBX_8888
1281 if (format == PIXEL_FORMAT_RGBX_8888)
1282 format = PIXEL_FORMAT_RGBA_8888;
1283#endif
1284
Mathias Agopian96f08192010-06-02 23:28:45 -07001285 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001286 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001287 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001289 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 }
1291 return layer;
1292}
1293
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001294sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001295 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001296 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297{
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 return layer;
1301}
1302
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001303sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001304 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001305 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306{
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 return layer;
1310}
1311
Mathias Agopian96f08192010-06-02 23:28:45 -07001312status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313{
Mathias Agopian9a112062009-04-17 19:36:26 -07001314 /*
1315 * called by the window manager, when a surface should be marked for
1316 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001317 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001318 * The surface is removed from the current and drawing lists, but placed
1319 * in the purgatory queue, so it's not destroyed right-away (we need
1320 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001321 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001322
Mathias Agopian48d819a2009-09-10 19:41:18 -07001323 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001324 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001325 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001326 if (layer != 0) {
1327 err = purgatorizeLayer_l(layer);
1328 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001329 setTransactionFlags(eTransactionNeeded);
1330 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001331 }
1332 return err;
1333}
1334
1335status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1336{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001337 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001338
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001339 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001341 sp<LayerBaseClient> layer;
1342 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001343 MessageDestroySurface(
1344 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1345 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001346 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001347 sp<LayerBaseClient> l(layer);
1348 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001349 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001350 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001351 * remove the layer from the current list -- chances are that it's
1352 * not in the list anyway, because it should have been removed
1353 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001354 * However, a buggy client could have not done that.
1355 * Since we know we don't have any more clients, we don't need
1356 * to use the purgatory.
1357 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001358 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001359 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1360 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001361 return true;
1362 }
1363 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001364
Mathias Agopianbb641242010-05-18 17:06:55 -07001365 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 return NO_ERROR;
1367}
1368
1369status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 int32_t count,
1372 const layer_state_t* states)
1373{
1374 Mutex::Autolock _l(mStateLock);
1375 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001377 const layer_state_t& s(states[i]);
1378 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001379 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 if (what & ePositionChanged) {
1382 if (layer->setPosition(s.x, s.y))
1383 flags |= eTraversalNeeded;
1384 }
1385 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001386 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001388 mCurrentState.layersSortedByZ.removeAt(idx);
1389 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 // we need traversal (state changed)
1391 // AND transaction (list changed)
1392 flags |= eTransactionNeeded|eTraversalNeeded;
1393 }
1394 }
1395 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001396 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001398 mResizeTransationPending = true;
1399 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 }
1401 if (what & eAlphaChanged) {
1402 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1403 flags |= eTraversalNeeded;
1404 }
1405 if (what & eMatrixChanged) {
1406 if (layer->setMatrix(s.matrix))
1407 flags |= eTraversalNeeded;
1408 }
1409 if (what & eTransparentRegionChanged) {
1410 if (layer->setTransparentRegionHint(s.transparentRegion))
1411 flags |= eTraversalNeeded;
1412 }
1413 if (what & eVisibilityChanged) {
1414 if (layer->setFlags(s.flags, s.mask))
1415 flags |= eTraversalNeeded;
1416 }
1417 }
1418 }
1419 if (flags) {
1420 setTransactionFlags(flags);
1421 }
1422 return NO_ERROR;
1423}
1424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425void SurfaceFlinger::screenReleased(int dpy)
1426{
1427 // this may be called by a signal handler, we can't do too much in here
1428 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1429 signalEvent();
1430}
1431
1432void SurfaceFlinger::screenAcquired(int dpy)
1433{
1434 // this may be called by a signal handler, we can't do too much in here
1435 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1436 signalEvent();
1437}
1438
1439status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1440{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001441 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 char buffer[SIZE];
1443 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001444 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 snprintf(buffer, SIZE, "Permission Denial: "
1446 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1447 IPCThreadState::self()->getCallingPid(),
1448 IPCThreadState::self()->getCallingUid());
1449 result.append(buffer);
1450 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001451
1452 // figure out if we're stuck somewhere
1453 const nsecs_t now = systemTime();
1454 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1455 const nsecs_t inTransaction(mDebugInTransaction);
1456 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1457 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1458
1459 // Try to get the main lock, but don't insist if we can't
1460 // (this would indicate SF is stuck, but we want to be able to
1461 // print something in dumpsys).
1462 int retry = 3;
1463 while (mStateLock.tryLock()<0 && --retry>=0) {
1464 usleep(1000000);
1465 }
1466 const bool locked(retry >= 0);
1467 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001468 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001469 "SurfaceFlinger appears to be unresponsive, "
1470 "dumping anyways (no locks held)\n");
1471 result.append(buffer);
1472 }
1473
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1475 const size_t count = currentLayers.size();
1476 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001477 const sp<LayerBase>& layer(currentLayers[i]);
1478 layer->dump(result, buffer, SIZE);
1479 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 s.transparentRegion.dump(result, "transparentRegion");
1481 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1482 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1483 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485 mWormholeRegion.dump(result, "WormholeRegion");
1486 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1487 snprintf(buffer, SIZE,
1488 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1489 mFreezeDisplay?"yes":"no", mFreezeCount,
1490 mCurrentState.orientation, hw.canDraw());
1491 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001492 snprintf(buffer, SIZE,
1493 " last eglSwapBuffers() time: %f us\n"
1494 " last transaction time : %f us\n",
1495 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1496 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001497
Mathias Agopian9795c422009-08-26 16:36:26 -07001498 if (inSwapBuffersDuration || !locked) {
1499 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1500 inSwapBuffersDuration/1000.0);
1501 result.append(buffer);
1502 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001503
Mathias Agopian9795c422009-08-26 16:36:26 -07001504 if (inTransactionDuration || !locked) {
1505 snprintf(buffer, SIZE, " transaction time: %f us\n",
1506 inTransactionDuration/1000.0);
1507 result.append(buffer);
1508 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001509
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001510 HWComposer& hwc(hw.getHwComposer());
1511 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1512 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1513 mDebugDisableHWC ? "disabled" : "enabled");
1514 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001515 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001516
Mathias Agopian3330b202009-10-05 17:07:12 -07001517 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001518 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001519 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001520
1521 if (locked) {
1522 mStateLock.unlock();
1523 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 }
1525 write(fd, result.string(), result.size());
1526 return NO_ERROR;
1527}
1528
1529status_t SurfaceFlinger::onTransact(
1530 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1531{
1532 switch (code) {
1533 case CREATE_CONNECTION:
1534 case OPEN_GLOBAL_TRANSACTION:
1535 case CLOSE_GLOBAL_TRANSACTION:
1536 case SET_ORIENTATION:
1537 case FREEZE_DISPLAY:
1538 case UNFREEZE_DISPLAY:
1539 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001540 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001541 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542 {
1543 // codes that require permission check
1544 IPCThreadState* ipc = IPCThreadState::self();
1545 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001546 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001547 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1548 LOGE("Permission Denial: "
1549 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1550 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001552 break;
1553 }
1554 case CAPTURE_SCREEN:
1555 {
1556 // codes that require permission check
1557 IPCThreadState* ipc = IPCThreadState::self();
1558 const int pid = ipc->getCallingPid();
1559 const int uid = ipc->getCallingUid();
1560 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1561 LOGE("Permission Denial: "
1562 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1563 return PERMISSION_DENIED;
1564 }
1565 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566 }
1567 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001568
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1570 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001571 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001572 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1573 IPCThreadState* ipc = IPCThreadState::self();
1574 const int pid = ipc->getCallingPid();
1575 const int uid = ipc->getCallingUid();
1576 LOGE("Permission Denial: "
1577 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 return PERMISSION_DENIED;
1579 }
1580 int n;
1581 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001582 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001583 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584 return NO_ERROR;
1585 case 1002: // SHOW_UPDATES
1586 n = data.readInt32();
1587 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1588 return NO_ERROR;
1589 case 1003: // SHOW_BACKGROUND
1590 n = data.readInt32();
1591 mDebugBackground = n ? 1 : 0;
1592 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001593 case 1008: // toggle use of hw composer
1594 n = data.readInt32();
1595 mDebugDisableHWC = n ? 1 : 0;
1596 mHwWorkListDirty = true;
1597 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 case 1004:{ // repaint everything
1599 Mutex::Autolock _l(mStateLock);
1600 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1601 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1602 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001603 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001605 case 1005:{ // force transaction
1606 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1607 return NO_ERROR;
1608 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001609 case 1006:{ // enable/disable GraphicLog
1610 int enabled = data.readInt32();
1611 GraphicLog::getInstance().setEnabled(enabled);
1612 return NO_ERROR;
1613 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 case 1007: // set mFreezeCount
1615 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001616 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 return NO_ERROR;
1618 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001619 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 reply->writeInt32(0);
1621 reply->writeInt32(mDebugRegion);
1622 reply->writeInt32(mDebugBackground);
1623 return NO_ERROR;
1624 case 1013: {
1625 Mutex::Autolock _l(mStateLock);
1626 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1627 reply->writeInt32(hw.getPageFlipCount());
1628 }
1629 return NO_ERROR;
1630 }
1631 }
1632 return err;
1633}
1634
Mathias Agopian59119e62010-10-11 12:37:43 -07001635// ---------------------------------------------------------------------------
1636
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001637status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1638 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001639{
Mathias Agopian59119e62010-10-11 12:37:43 -07001640 if (!GLExtensions::getInstance().haveFramebufferObject())
1641 return INVALID_OPERATION;
1642
1643 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001644 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001645 const uint32_t hw_w = hw.getWidth();
1646 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001647 GLfloat u = 1;
1648 GLfloat v = 1;
1649
1650 // make sure to clear all GL error flags
1651 while ( glGetError() != GL_NO_ERROR ) ;
1652
1653 // create a FBO
1654 GLuint name, tname;
1655 glGenTextures(1, &tname);
1656 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001657 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1658 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001659 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001660 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001661 GLint tw = (2 << (31 - clz(hw_w)));
1662 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001663 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1664 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001665 u = GLfloat(hw_w) / tw;
1666 v = GLfloat(hw_h) / th;
1667 }
1668 glGenFramebuffersOES(1, &name);
1669 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001670 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1671 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001672
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001673 // redraw the screen entirely...
1674 glClearColor(0,0,0,1);
1675 glClear(GL_COLOR_BUFFER_BIT);
1676 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1677 const size_t count = layers.size();
1678 for (size_t i=0 ; i<count ; ++i) {
1679 const sp<LayerBase>& layer(layers[i]);
1680 layer->drawForSreenShot();
1681 }
1682
1683 // back to main framebuffer
1684 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1685 glDisable(GL_SCISSOR_TEST);
1686 glDeleteFramebuffersOES(1, &name);
1687
1688 *textureName = tname;
1689 *uOut = u;
1690 *vOut = v;
1691 return NO_ERROR;
1692}
1693
1694// ---------------------------------------------------------------------------
1695
1696status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1697{
1698 status_t result = PERMISSION_DENIED;
1699
1700 if (!GLExtensions::getInstance().haveFramebufferObject())
1701 return INVALID_OPERATION;
1702
1703 // get screen geometry
1704 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1705 const uint32_t hw_w = hw.getWidth();
1706 const uint32_t hw_h = hw.getHeight();
1707 const Region screenBounds(hw.bounds());
1708
1709 GLfloat u, v;
1710 GLuint tname;
1711 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1712 if (result != NO_ERROR) {
1713 return result;
1714 }
1715
1716 GLfloat vtx[8];
1717 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1718 glEnable(GL_TEXTURE_2D);
1719 glBindTexture(GL_TEXTURE_2D, tname);
1720 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1721 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1722 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1723 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1724 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1725 glVertexPointer(2, GL_FLOAT, 0, vtx);
1726
1727 class s_curve_interpolator {
1728 const float nbFrames, s, v;
1729 public:
1730 s_curve_interpolator(int nbFrames, float s)
1731 : nbFrames(1.0f / (nbFrames-1)), s(s),
1732 v(1.0f + expf(-s + 0.5f*s)) {
1733 }
1734 float operator()(int f) {
1735 const float x = f * nbFrames;
1736 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1737 }
1738 };
1739
1740 class v_stretch {
1741 const GLfloat hw_w, hw_h;
1742 public:
1743 v_stretch(uint32_t hw_w, uint32_t hw_h)
1744 : hw_w(hw_w), hw_h(hw_h) {
1745 }
1746 void operator()(GLfloat* vtx, float v) {
1747 const GLfloat w = hw_w + (hw_w * v);
1748 const GLfloat h = hw_h - (hw_h * v);
1749 const GLfloat x = (hw_w - w) * 0.5f;
1750 const GLfloat y = (hw_h - h) * 0.5f;
1751 vtx[0] = x; vtx[1] = y;
1752 vtx[2] = x; vtx[3] = y + h;
1753 vtx[4] = x + w; vtx[5] = y + h;
1754 vtx[6] = x + w; vtx[7] = y;
1755 }
1756 };
1757
1758 class h_stretch {
1759 const GLfloat hw_w, hw_h;
1760 public:
1761 h_stretch(uint32_t hw_w, uint32_t hw_h)
1762 : hw_w(hw_w), hw_h(hw_h) {
1763 }
1764 void operator()(GLfloat* vtx, float v) {
1765 const GLfloat w = hw_w - (hw_w * v);
1766 const GLfloat h = 1.0f;
1767 const GLfloat x = (hw_w - w) * 0.5f;
1768 const GLfloat y = (hw_h - h) * 0.5f;
1769 vtx[0] = x; vtx[1] = y;
1770 vtx[2] = x; vtx[3] = y + h;
1771 vtx[4] = x + w; vtx[5] = y + h;
1772 vtx[6] = x + w; vtx[7] = y;
1773 }
1774 };
1775
1776 // the full animation is 24 frames
1777 const int nbFrames = 12;
1778 s_curve_interpolator itr(nbFrames, 7.5f);
1779 s_curve_interpolator itg(nbFrames, 8.0f);
1780 s_curve_interpolator itb(nbFrames, 8.5f);
1781
1782 v_stretch vverts(hw_w, hw_h);
1783 glEnable(GL_BLEND);
1784 glBlendFunc(GL_ONE, GL_ONE);
1785 for (int i=0 ; i<nbFrames ; i++) {
1786 float x, y, w, h;
1787 const float vr = itr(i);
1788 const float vg = itg(i);
1789 const float vb = itb(i);
1790
1791 // clear screen
1792 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001793 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001794 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001795
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001796 // draw the red plane
1797 vverts(vtx, vr);
1798 glColorMask(1,0,0,1);
1799 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001800
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001801 // draw the green plane
1802 vverts(vtx, vg);
1803 glColorMask(0,1,0,1);
1804 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001805
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001806 // draw the blue plane
1807 vverts(vtx, vb);
1808 glColorMask(0,0,1,1);
1809 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001810
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001811 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001812 glDisable(GL_TEXTURE_2D);
1813 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001814 glColor4f(vg, vg, vg, 1);
1815 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1816 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001817 }
1818
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001819 h_stretch hverts(hw_w, hw_h);
1820 glDisable(GL_BLEND);
1821 glDisable(GL_TEXTURE_2D);
1822 glColorMask(1,1,1,1);
1823 for (int i=0 ; i<nbFrames ; i++) {
1824 const float v = itg(i);
1825 hverts(vtx, v);
1826 glClear(GL_COLOR_BUFFER_BIT);
1827 glColor4f(1-v, 1-v, 1-v, 1);
1828 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1829 hw.flip(screenBounds);
1830 }
1831
1832 glColorMask(1,1,1,1);
1833 glEnable(GL_SCISSOR_TEST);
1834 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1835 glDeleteTextures(1, &tname);
1836 return NO_ERROR;
1837}
1838
1839status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1840{
1841 status_t result = PERMISSION_DENIED;
1842
1843 if (!GLExtensions::getInstance().haveFramebufferObject())
1844 return INVALID_OPERATION;
1845
1846
1847 // get screen geometry
1848 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1849 const uint32_t hw_w = hw.getWidth();
1850 const uint32_t hw_h = hw.getHeight();
1851 const Region screenBounds(hw.bounds());
1852
1853 GLfloat u, v;
1854 GLuint tname;
1855 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1856 if (result != NO_ERROR) {
1857 return result;
1858 }
1859
1860 // back to main framebuffer
1861 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1862 glDisable(GL_SCISSOR_TEST);
1863
1864 GLfloat vtx[8];
1865 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1866 glEnable(GL_TEXTURE_2D);
1867 glBindTexture(GL_TEXTURE_2D, tname);
1868 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1869 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1870 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1871 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1872 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1873 glVertexPointer(2, GL_FLOAT, 0, vtx);
1874
1875 class s_curve_interpolator {
1876 const float nbFrames, s, v;
1877 public:
1878 s_curve_interpolator(int nbFrames, float s)
1879 : nbFrames(1.0f / (nbFrames-1)), s(s),
1880 v(1.0f + expf(-s + 0.5f*s)) {
1881 }
1882 float operator()(int f) {
1883 const float x = f * nbFrames;
1884 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1885 }
1886 };
1887
1888 class v_stretch {
1889 const GLfloat hw_w, hw_h;
1890 public:
1891 v_stretch(uint32_t hw_w, uint32_t hw_h)
1892 : hw_w(hw_w), hw_h(hw_h) {
1893 }
1894 void operator()(GLfloat* vtx, float v) {
1895 const GLfloat w = hw_w + (hw_w * v);
1896 const GLfloat h = hw_h - (hw_h * v);
1897 const GLfloat x = (hw_w - w) * 0.5f;
1898 const GLfloat y = (hw_h - h) * 0.5f;
1899 vtx[0] = x; vtx[1] = y;
1900 vtx[2] = x; vtx[3] = y + h;
1901 vtx[4] = x + w; vtx[5] = y + h;
1902 vtx[6] = x + w; vtx[7] = y;
1903 }
1904 };
1905
1906 class h_stretch {
1907 const GLfloat hw_w, hw_h;
1908 public:
1909 h_stretch(uint32_t hw_w, uint32_t hw_h)
1910 : hw_w(hw_w), hw_h(hw_h) {
1911 }
1912 void operator()(GLfloat* vtx, float v) {
1913 const GLfloat w = hw_w - (hw_w * v);
1914 const GLfloat h = 1.0f;
1915 const GLfloat x = (hw_w - w) * 0.5f;
1916 const GLfloat y = (hw_h - h) * 0.5f;
1917 vtx[0] = x; vtx[1] = y;
1918 vtx[2] = x; vtx[3] = y + h;
1919 vtx[4] = x + w; vtx[5] = y + h;
1920 vtx[6] = x + w; vtx[7] = y;
1921 }
1922 };
1923
Mathias Agopiana6546e52010-10-14 12:33:07 -07001924 // the full animation is 12 frames
1925 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001926 s_curve_interpolator itr(nbFrames, 7.5f);
1927 s_curve_interpolator itg(nbFrames, 8.0f);
1928 s_curve_interpolator itb(nbFrames, 8.5f);
1929
1930 h_stretch hverts(hw_w, hw_h);
1931 glDisable(GL_BLEND);
1932 glDisable(GL_TEXTURE_2D);
1933 glColorMask(1,1,1,1);
1934 for (int i=nbFrames-1 ; i>=0 ; i--) {
1935 const float v = itg(i);
1936 hverts(vtx, v);
1937 glClear(GL_COLOR_BUFFER_BIT);
1938 glColor4f(1-v, 1-v, 1-v, 1);
1939 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1940 hw.flip(screenBounds);
1941 }
1942
Mathias Agopiana6546e52010-10-14 12:33:07 -07001943 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001944 v_stretch vverts(hw_w, hw_h);
1945 glEnable(GL_BLEND);
1946 glBlendFunc(GL_ONE, GL_ONE);
1947 for (int i=nbFrames-1 ; i>=0 ; i--) {
1948 float x, y, w, h;
1949 const float vr = itr(i);
1950 const float vg = itg(i);
1951 const float vb = itb(i);
1952
1953 // clear screen
1954 glColorMask(1,1,1,1);
1955 glClear(GL_COLOR_BUFFER_BIT);
1956 glEnable(GL_TEXTURE_2D);
1957
1958 // draw the red plane
1959 vverts(vtx, vr);
1960 glColorMask(1,0,0,1);
1961 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1962
1963 // draw the green plane
1964 vverts(vtx, vg);
1965 glColorMask(0,1,0,1);
1966 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1967
1968 // draw the blue plane
1969 vverts(vtx, vb);
1970 glColorMask(0,0,1,1);
1971 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1972
1973 hw.flip(screenBounds);
1974 }
1975
1976 glColorMask(1,1,1,1);
1977 glEnable(GL_SCISSOR_TEST);
1978 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07001979 glDeleteTextures(1, &tname);
1980
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001981 return NO_ERROR;
1982}
1983
1984// ---------------------------------------------------------------------------
1985
Mathias Agopianabd671a2010-10-14 14:54:06 -07001986status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001987{
1988 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1989 if (!hw.canDraw()) {
1990 // we're already off
1991 return NO_ERROR;
1992 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07001993 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
1994 electronBeamOffAnimationImplLocked();
1995 }
1996
1997 // always clear the whole screen at the end of the animation
1998 glClearColor(0,0,0,1);
1999 glDisable(GL_SCISSOR_TEST);
2000 glClear(GL_COLOR_BUFFER_BIT);
2001 glEnable(GL_SCISSOR_TEST);
2002 hw.flip( Region(hw.bounds()) );
2003
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002004 hw.setCanDraw(false);
2005 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002006}
2007
2008status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2009{
Mathias Agopian59119e62010-10-11 12:37:43 -07002010 class MessageTurnElectronBeamOff : public MessageBase {
2011 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002012 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002013 status_t result;
2014 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002015 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2016 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 }
2018 status_t getResult() const {
2019 return result;
2020 }
2021 virtual bool handler() {
2022 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002023 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002024 return true;
2025 }
2026 };
2027
Mathias Agopianabd671a2010-10-14 14:54:06 -07002028 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002029 status_t res = postMessageSync(msg);
2030 if (res == NO_ERROR) {
2031 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002032
2033 // work-around: when the power-manager calls us we activate the
2034 // animation. eventually, the "on" animation will be called
2035 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002036 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002037 }
2038 return res;
2039}
2040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002041// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002042
Mathias Agopianabd671a2010-10-14 14:54:06 -07002043status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044{
2045 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2046 if (hw.canDraw()) {
2047 // we're already on
2048 return NO_ERROR;
2049 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002050 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2051 electronBeamOnAnimationImplLocked();
2052 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002053 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002054
2055 // make sure to redraw the whole screen when the animation is done
2056 mDirtyRegion.set(hw.bounds());
2057 signalEvent();
2058
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002059 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002060}
2061
2062status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2063{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 class MessageTurnElectronBeamOn : public MessageBase {
2065 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002066 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002067 status_t result;
2068 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002069 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2070 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002071 }
2072 status_t getResult() const {
2073 return result;
2074 }
2075 virtual bool handler() {
2076 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002077 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002078 return true;
2079 }
2080 };
2081
Mathias Agopianabd671a2010-10-14 14:54:06 -07002082 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002083 return NO_ERROR;
2084}
2085
2086// ---------------------------------------------------------------------------
2087
Mathias Agopian74c40c02010-09-29 13:02:36 -07002088status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2089 sp<IMemoryHeap>* heap,
2090 uint32_t* w, uint32_t* h, PixelFormat* f,
2091 uint32_t sw, uint32_t sh)
2092{
2093 status_t result = PERMISSION_DENIED;
2094
2095 // only one display supported for now
2096 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2097 return BAD_VALUE;
2098
2099 if (!GLExtensions::getInstance().haveFramebufferObject())
2100 return INVALID_OPERATION;
2101
2102 // get screen geometry
2103 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2104 const uint32_t hw_w = hw.getWidth();
2105 const uint32_t hw_h = hw.getHeight();
2106
2107 if ((sw > hw_w) || (sh > hw_h))
2108 return BAD_VALUE;
2109
2110 sw = (!sw) ? hw_w : sw;
2111 sh = (!sh) ? hw_h : sh;
2112 const size_t size = sw * sh * 4;
2113
2114 // make sure to clear all GL error flags
2115 while ( glGetError() != GL_NO_ERROR ) ;
2116
2117 // create a FBO
2118 GLuint name, tname;
2119 glGenRenderbuffersOES(1, &tname);
2120 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2121 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2122 glGenFramebuffersOES(1, &name);
2123 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2124 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2125 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2126
2127 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2128 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2129
2130 // invert everything, b/c glReadPixel() below will invert the FB
2131 glViewport(0, 0, sw, sh);
2132 glMatrixMode(GL_PROJECTION);
2133 glPushMatrix();
2134 glLoadIdentity();
2135 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2136 glMatrixMode(GL_MODELVIEW);
2137
2138 // redraw the screen entirely...
2139 glClearColor(0,0,0,1);
2140 glClear(GL_COLOR_BUFFER_BIT);
2141 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2142 const size_t count = layers.size();
2143 for (size_t i=0 ; i<count ; ++i) {
2144 const sp<LayerBase>& layer(layers[i]);
2145 layer->drawForSreenShot();
2146 }
2147
2148 // XXX: this is needed on tegra
2149 glScissor(0, 0, sw, sh);
2150
2151 // check for errors and return screen capture
2152 if (glGetError() != GL_NO_ERROR) {
2153 // error while rendering
2154 result = INVALID_OPERATION;
2155 } else {
2156 // allocate shared memory large enough to hold the
2157 // screen capture
2158 sp<MemoryHeapBase> base(
2159 new MemoryHeapBase(size, 0, "screen-capture") );
2160 void* const ptr = base->getBase();
2161 if (ptr) {
2162 // capture the screen with glReadPixels()
2163 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2164 if (glGetError() == GL_NO_ERROR) {
2165 *heap = base;
2166 *w = sw;
2167 *h = sh;
2168 *f = PIXEL_FORMAT_RGBA_8888;
2169 result = NO_ERROR;
2170 }
2171 } else {
2172 result = NO_MEMORY;
2173 }
2174 }
2175
2176 glEnable(GL_SCISSOR_TEST);
2177 glViewport(0, 0, hw_w, hw_h);
2178 glMatrixMode(GL_PROJECTION);
2179 glPopMatrix();
2180 glMatrixMode(GL_MODELVIEW);
2181
2182
2183 } else {
2184 result = BAD_VALUE;
2185 }
2186
2187 // release FBO resources
2188 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2189 glDeleteRenderbuffersOES(1, &tname);
2190 glDeleteFramebuffersOES(1, &name);
2191 return result;
2192}
2193
2194
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002195status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2196 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002197 uint32_t* width, uint32_t* height, PixelFormat* format,
2198 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002199{
2200 // only one display supported for now
2201 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2202 return BAD_VALUE;
2203
2204 if (!GLExtensions::getInstance().haveFramebufferObject())
2205 return INVALID_OPERATION;
2206
2207 class MessageCaptureScreen : public MessageBase {
2208 SurfaceFlinger* flinger;
2209 DisplayID dpy;
2210 sp<IMemoryHeap>* heap;
2211 uint32_t* w;
2212 uint32_t* h;
2213 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002214 uint32_t sw;
2215 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002216 status_t result;
2217 public:
2218 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002219 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
2220 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002221 : flinger(flinger), dpy(dpy),
Mathias Agopian74c40c02010-09-29 13:02:36 -07002222 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002223 {
2224 }
2225 status_t getResult() const {
2226 return result;
2227 }
2228 virtual bool handler() {
2229 Mutex::Autolock _l(flinger->mStateLock);
2230
2231 // if we have secure windows, never allow the screen capture
2232 if (flinger->mSecureFrameBuffer)
2233 return true;
2234
Mathias Agopian74c40c02010-09-29 13:02:36 -07002235 result = flinger->captureScreenImplLocked(dpy,
2236 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002237
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002238 return true;
2239 }
2240 };
2241
2242 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002243 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002244 status_t res = postMessageSync(msg);
2245 if (res == NO_ERROR) {
2246 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2247 }
2248 return res;
2249}
2250
2251// ---------------------------------------------------------------------------
2252
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002253sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2254{
2255 sp<Layer> result;
2256 Mutex::Autolock _l(mStateLock);
2257 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2258 return result;
2259}
2260
2261// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002262
Mathias Agopian96f08192010-06-02 23:28:45 -07002263Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002264 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002265{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002266}
2267
Mathias Agopian96f08192010-06-02 23:28:45 -07002268Client::~Client()
2269{
Mathias Agopian96f08192010-06-02 23:28:45 -07002270 const size_t count = mLayers.size();
2271 for (size_t i=0 ; i<count ; i++) {
2272 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2273 if (layer != 0) {
2274 mFlinger->removeLayer(layer);
2275 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002276 }
2277}
2278
Mathias Agopian96f08192010-06-02 23:28:45 -07002279status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002280 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002281}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002282
Mathias Agopian96f08192010-06-02 23:28:45 -07002283ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2284{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002285 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002286 mLayers.add(name, layer);
2287 return name;
2288}
2289
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002290void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002291{
Mathias Agopian96f08192010-06-02 23:28:45 -07002292 // we do a linear search here, because this doesn't happen often
2293 const size_t count = mLayers.size();
2294 for (size_t i=0 ; i<count ; i++) {
2295 if (mLayers.valueAt(i) == layer) {
2296 mLayers.removeItemsAt(i, 1);
2297 break;
2298 }
2299 }
2300}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002301sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2302 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002303 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2304 if (layer != 0) {
2305 lbc = layer.promote();
2306 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002307 }
2308 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309}
2310
Mathias Agopian96f08192010-06-02 23:28:45 -07002311sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002312 return 0;
2313}
2314ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2315 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002316}
Mathias Agopian96f08192010-06-02 23:28:45 -07002317sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002318 ISurfaceComposerClient::surface_data_t* params, int pid,
2319 const String8& name,
2320 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002321 uint32_t flags)
2322{
Mathias Agopian96f08192010-06-02 23:28:45 -07002323 return mFlinger->createSurface(this, pid, name, params,
2324 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002325}
Mathias Agopian96f08192010-06-02 23:28:45 -07002326status_t Client::destroySurface(SurfaceID sid) {
2327 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002328}
Mathias Agopian96f08192010-06-02 23:28:45 -07002329status_t Client::setState(int32_t count, const layer_state_t* states) {
2330 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002331}
2332
2333// ---------------------------------------------------------------------------
2334
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002335UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2336 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2337{
2338 const int pgsize = getpagesize();
2339 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2340
2341 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2342 "SurfaceFlinger Client control-block");
2343
2344 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2345 if (ctrlblk) { // construct the shared structure in-place.
2346 new(ctrlblk) SharedClient;
2347 }
2348}
2349
2350UserClient::~UserClient()
2351{
2352 if (ctrlblk) {
2353 ctrlblk->~SharedClient(); // destroy our shared-structure.
2354 }
2355
2356 /*
2357 * When a UserClient dies, it's unclear what to do exactly.
2358 * We could go ahead and destroy all surfaces linked to that client
2359 * however, it wouldn't be fair to the main Client
2360 * (usually the the window-manager), which might want to re-target
2361 * the layer to another UserClient.
2362 * I think the best is to do nothing, or not much; in most cases the
2363 * WM itself will go ahead and clean things up when it detects a client of
2364 * his has died.
2365 * The remaining question is what to display? currently we keep
2366 * just keep the current buffer.
2367 */
2368}
2369
2370status_t UserClient::initCheck() const {
2371 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2372}
2373
2374void UserClient::detachLayer(const Layer* layer)
2375{
2376 int32_t name = layer->getToken();
2377 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002378 int32_t mask = 1LU<<name;
2379 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2380 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2381 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002382 }
2383}
2384
2385sp<IMemoryHeap> UserClient::getControlBlock() const {
2386 return mCblkHeap;
2387}
2388
2389ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2390{
2391 int32_t name = NAME_NOT_FOUND;
2392 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002393 if (layer == 0) {
2394 return name;
2395 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002396
Mathias Agopian579b3f82010-06-08 19:54:15 -07002397 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002398 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002399 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002400 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002401 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002402
2403 name = 0;
2404 do {
2405 int32_t mask = 1LU<<name;
2406 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2407 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002408 status_t err = layer->setToken(
2409 const_cast<UserClient*>(this), ctrlblk, name);
2410 if (err != NO_ERROR) {
2411 // free the name
2412 android_atomic_and(~mask, &mBitmap);
2413 name = err;
2414 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002415 break;
2416 }
2417 if (++name > 31)
2418 name = NO_MEMORY;
2419 } while(name >= 0);
2420
Mathias Agopian53503a92010-06-08 15:40:56 -07002421 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2422 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002423 return name;
2424}
2425
2426sp<ISurface> UserClient::createSurface(
2427 ISurfaceComposerClient::surface_data_t* params, int pid,
2428 const String8& name,
2429 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2430 uint32_t flags) {
2431 return 0;
2432}
2433status_t UserClient::destroySurface(SurfaceID sid) {
2434 return INVALID_OPERATION;
2435}
2436status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2437 return INVALID_OPERATION;
2438}
2439
2440// ---------------------------------------------------------------------------
2441
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002442GraphicPlane::GraphicPlane()
2443 : mHw(0)
2444{
2445}
2446
2447GraphicPlane::~GraphicPlane() {
2448 delete mHw;
2449}
2450
2451bool GraphicPlane::initialized() const {
2452 return mHw ? true : false;
2453}
2454
Mathias Agopian2b92d892010-02-08 15:49:35 -08002455int GraphicPlane::getWidth() const {
2456 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457}
2458
Mathias Agopian2b92d892010-02-08 15:49:35 -08002459int GraphicPlane::getHeight() const {
2460 return mHeight;
2461}
2462
2463void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2464{
2465 mHw = hw;
2466
2467 // initialize the display orientation transform.
2468 // it's a constant that should come from the display driver.
2469 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2470 char property[PROPERTY_VALUE_MAX];
2471 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2472 //displayOrientation
2473 switch (atoi(property)) {
2474 case 90:
2475 displayOrientation = ISurfaceComposer::eOrientation90;
2476 break;
2477 case 270:
2478 displayOrientation = ISurfaceComposer::eOrientation270;
2479 break;
2480 }
2481 }
2482
2483 const float w = hw->getWidth();
2484 const float h = hw->getHeight();
2485 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2486 &mDisplayTransform);
2487 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2488 mDisplayWidth = h;
2489 mDisplayHeight = w;
2490 } else {
2491 mDisplayWidth = w;
2492 mDisplayHeight = h;
2493 }
2494
2495 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496}
2497
2498status_t GraphicPlane::orientationToTransfrom(
2499 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002500{
2501 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002502 switch (orientation) {
2503 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002504 flags = Transform::ROT_0;
2505 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002507 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 break;
2509 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002510 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 break;
2512 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002513 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514 break;
2515 default:
2516 return BAD_VALUE;
2517 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002518 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519 return NO_ERROR;
2520}
2521
2522status_t GraphicPlane::setOrientation(int orientation)
2523{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002524 // If the rotation can be handled in hardware, this is where
2525 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002526
2527 const DisplayHardware& hw(displayHardware());
2528 const float w = mDisplayWidth;
2529 const float h = mDisplayHeight;
2530 mWidth = int(w);
2531 mHeight = int(h);
2532
2533 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002534 GraphicPlane::orientationToTransfrom(orientation, w, h,
2535 &orientationTransform);
2536 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2537 mWidth = int(h);
2538 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002539 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002540
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002541 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002542 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543 return NO_ERROR;
2544}
2545
2546const DisplayHardware& GraphicPlane::displayHardware() const {
2547 return *mHw;
2548}
2549
Mathias Agopian59119e62010-10-11 12:37:43 -07002550DisplayHardware& GraphicPlane::editDisplayHardware() {
2551 return *mHw;
2552}
2553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554const Transform& GraphicPlane::transform() const {
2555 return mGlobalTransform;
2556}
2557
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002558EGLDisplay GraphicPlane::getEGLDisplay() const {
2559 return mHw->getEGLDisplay();
2560}
2561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562// ---------------------------------------------------------------------------
2563
2564}; // namespace android