blob: a975480ce208e7f83a205f2588e751aa9c912592 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
50#include "LayerBlur.h"
51#include "LayerBuffer.h"
52#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopiana1ecca92009-05-21 19:21:59 -070057/* ideally AID_GRAPHICS would be in a semi-public header
58 * or there would be a way to map a user/group name to its id
59 */
60#ifndef AID_GRAPHICS
61#define AID_GRAPHICS 1003
62#endif
63
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),
81 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
Mathias Agopian9daa5c92010-10-12 16:05:48 -070083 mElectronBeamAnimation(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070088 mDebugInSwapBuffers(0),
89 mLastSwapBufferTime(0),
90 mDebugInTransaction(0),
91 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070092 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mConsoleSignals(0),
94 mSecureFrameBuffer(0)
95{
96 init();
97}
98
99void SurfaceFlinger::init()
100{
101 LOGI("SurfaceFlinger is starting");
102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 property_get("debug.sf.showbackground", value, "0");
108 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
Mathias Agopian78fd5012010-04-20 14:51:04 -0700110 LOGI_IF(mDebugRegion, "showupdates enabled");
111 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112}
113
114SurfaceFlinger::~SurfaceFlinger()
115{
116 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
120{
121 return graphicPlane(0).displayHardware().getOverlayEngine();
122}
123
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700124sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700126 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
Mathias Agopian7e27f052010-05-28 14:22:23 -0700129sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130{
Mathias Agopian96f08192010-06-02 23:28:45 -0700131 sp<ISurfaceComposerClient> bclient;
132 sp<Client> client(new Client(this));
133 status_t err = client->initCheck();
134 if (err == NO_ERROR) {
135 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 return bclient;
138}
139
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700140sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
141{
142 sp<ISurfaceComposerClient> bclient;
143 sp<UserClient> client(new UserClient(this));
144 status_t err = client->initCheck();
145 if (err == NO_ERROR) {
146 bclient = client;
147 }
148 return bclient;
149}
150
151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
153{
154 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
155 const GraphicPlane& plane(mGraphicPlanes[dpy]);
156 return plane;
157}
158
159GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
160{
161 return const_cast<GraphicPlane&>(
162 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
163}
164
165void SurfaceFlinger::bootFinished()
166{
167 const nsecs_t now = systemTime();
168 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700169 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700170 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700171 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172}
173
174void SurfaceFlinger::onFirstRef()
175{
176 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
177
178 // Wait for the main thread to be done with its initialization
179 mReadyToRunBarrier.wait();
180}
181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182static inline uint16_t pack565(int r, int g, int b) {
183 return (r<<11)|(g<<5)|b;
184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186status_t SurfaceFlinger::readyToRun()
187{
188 LOGI( "SurfaceFlinger's main thread ready to run. "
189 "Initializing graphics H/W...");
190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 // we only support one display currently
192 int dpy = 0;
193
194 {
195 // initialize the main display
196 GraphicPlane& plane(graphicPlane(dpy));
197 DisplayHardware* const hw = new DisplayHardware(this, dpy);
198 plane.setDisplayHardware(hw);
199 }
200
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700201 // create the shared control-block
202 mServerHeap = new MemoryHeapBase(4096,
203 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
204 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
205
206 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
207 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
208
209 new(mServerCblk) surface_flinger_cblk_t;
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 // initialize primary screen
212 // (other display should be initialized in the same manner, but
213 // asynchronously, as they could come and go. None of this is supported
214 // yet).
215 const GraphicPlane& plane(graphicPlane(dpy));
216 const DisplayHardware& hw = plane.displayHardware();
217 const uint32_t w = hw.getWidth();
218 const uint32_t h = hw.getHeight();
219 const uint32_t f = hw.getFormat();
220 hw.makeCurrent();
221
222 // initialize the shared control block
223 mServerCblk->connected |= 1<<dpy;
224 display_cblk_t* dcblk = mServerCblk->displays + dpy;
225 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800226 dcblk->w = plane.getWidth();
227 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 dcblk->format = f;
229 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
230 dcblk->xdpi = hw.getDpiX();
231 dcblk->ydpi = hw.getDpiY();
232 dcblk->fps = hw.getRefreshRate();
233 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
235 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
237 glPixelStorei(GL_PACK_ALIGNMENT, 4);
238 glEnableClientState(GL_VERTEX_ARRAY);
239 glEnable(GL_SCISSOR_TEST);
240 glShadeModel(GL_FLAT);
241 glDisable(GL_DITHER);
242 glDisable(GL_CULL_FACE);
243
244 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
245 const uint16_t g1 = pack565(0x17,0x2f,0x17);
246 const uint16_t textureData[4] = { g0, g1, g1, g0 };
247 glGenTextures(1, &mWormholeTexName);
248 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
249 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
250 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
251 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
252 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
254 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
255
256 glViewport(0, 0, w, h);
257 glMatrixMode(GL_PROJECTION);
258 glLoadIdentity();
259 glOrthof(0, w, h, 0, 0, 1);
260
261 LayerDim::initDimmer(this, w, h);
262
263 mReadyToRunBarrier.open();
264
265 /*
266 * We're now ready to accept clients...
267 */
268
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700269 // start boot animation
270 property_set("ctl.start", "bootanim");
271
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 return NO_ERROR;
273}
274
275// ----------------------------------------------------------------------------
276#if 0
277#pragma mark -
278#pragma mark Events Handler
279#endif
280
281void SurfaceFlinger::waitForEvent()
282{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700283 while (true) {
284 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800285 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700286 if (UNLIKELY(isFrozen())) {
287 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700288 const nsecs_t now = systemTime();
289 if (mFreezeDisplayTime == 0) {
290 mFreezeDisplayTime = now;
291 }
292 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
293 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700294 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700295
Mathias Agopianbb641242010-05-18 17:06:55 -0700296 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800297
298 // see if we timed out
299 if (isFrozen()) {
300 const nsecs_t now = systemTime();
301 nsecs_t frozenTime = (now - mFreezeDisplayTime);
302 if (frozenTime >= freezeDisplayTimeout) {
303 // we timed out and are still frozen
304 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
305 mFreezeDisplay, mFreezeCount);
306 mFreezeDisplayTime = 0;
307 mFreezeCount = 0;
308 mFreezeDisplay = false;
309 }
310 }
311
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700312 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700313 switch (msg->what) {
314 case MessageQueue::INVALIDATE:
315 // invalidate message, just return to the main loop
316 return;
317 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 }
320}
321
322void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700323 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324}
325
326void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700327 // this is the IPC call
328 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329}
330
Mathias Agopianbb641242010-05-18 17:06:55 -0700331status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
332 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333{
Mathias Agopianbb641242010-05-18 17:06:55 -0700334 return mEventQueue.postMessage(msg, reltime, flags);
335}
336
337status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
338 nsecs_t reltime, uint32_t flags)
339{
340 status_t res = mEventQueue.postMessage(msg, reltime, flags);
341 if (res == NO_ERROR) {
342 msg->wait();
343 }
344 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345}
346
347// ----------------------------------------------------------------------------
348#if 0
349#pragma mark -
350#pragma mark Main loop
351#endif
352
353bool SurfaceFlinger::threadLoop()
354{
355 waitForEvent();
356
357 // check for transactions
358 if (UNLIKELY(mConsoleSignals)) {
359 handleConsoleEvents();
360 }
361
362 if (LIKELY(mTransactionCount == 0)) {
363 // if we're in a global transaction, don't do anything.
364 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
365 uint32_t transactionFlags = getTransactionFlags(mask);
366 if (LIKELY(transactionFlags)) {
367 handleTransaction(transactionFlags);
368 }
369 }
370
371 // post surfaces (if needed)
372 handlePageFlip();
373
374 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700375 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700377
378 const int index = hw.getCurrentBufferIndex();
379 GraphicLog& logger(GraphicLog::getInstance());
380
381 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 handleRepaint();
383
Mathias Agopian74faca22009-09-17 16:18:16 -0700384 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700385 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700386 hw.compositionComplete();
387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 // release the clients before we flip ('cause flip might block)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700389 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391
Mathias Agopian35b48d12010-09-13 22:57:58 -0700392 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 postFramebuffer();
Mathias Agopian35b48d12010-09-13 22:57:58 -0700394
395 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396 } else {
397 // pretend we did the post
398 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 usleep(16667); // 60 fps period
400 }
401 return true;
402}
403
404void SurfaceFlinger::postFramebuffer()
405{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 if (!mInvalidRegion.isEmpty()) {
407 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700408 const nsecs_t now = systemTime();
409 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700411 mLastSwapBufferTime = systemTime() - now;
412 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 }
415}
416
417void SurfaceFlinger::handleConsoleEvents()
418{
419 // something to do with the console
420 const DisplayHardware& hw = graphicPlane(0).displayHardware();
421
422 int what = android_atomic_and(0, &mConsoleSignals);
423 if (what & eConsoleAcquired) {
424 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700425 // this is a temporary work-around, eventually this should be called
426 // by the power-manager
427 if (mElectronBeamAnimation)
428 SurfaceFlinger::turnElectronBeamOn(0);
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 Agopian4da75192010-08-10 17:19:56 -0700463 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700464 }
465
Mathias Agopian4da75192010-08-10 17:19:56 -0700466 /*
467 * Clean-up all layers that went away
468 * (do this without the lock held)
469 */
Mathias Agopian3d579642009-06-04 18:46:21 -0700470 const size_t count = ditchedLayers.size();
471 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700472 if (ditchedLayers[i] != 0) {
473 //LOGD("ditching layer %p", ditchedLayers[i].get());
474 ditchedLayers[i]->ditch();
475 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700476 }
477}
478
479void SurfaceFlinger::handleTransactionLocked(
480 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
481{
482 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 const size_t count = currentLayers.size();
484
485 /*
486 * Traversal of the children
487 * (perform the transaction for each of them if needed)
488 */
489
490 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
491 if (layersNeedTransaction) {
492 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700493 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
495 if (!trFlags) continue;
496
497 const uint32_t flags = layer->doTransaction(0);
498 if (flags & Layer::eVisibleRegion)
499 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 }
501 }
502
503 /*
504 * Perform our own transaction if needed
505 */
506
507 if (transactionFlags & eTransactionNeeded) {
508 if (mCurrentState.orientation != mDrawingState.orientation) {
509 // the orientation has changed, recompute all visible regions
510 // and invalidate everything.
511
512 const int dpy = 0;
513 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700514 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 GraphicPlane& plane(graphicPlane(dpy));
516 plane.setOrientation(orientation);
517
518 // update the shared control block
519 const DisplayHardware& hw(plane.displayHardware());
520 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
521 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800522 dcblk->w = plane.getWidth();
523 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524
525 mVisibleRegionsDirty = true;
526 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 }
528
529 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
530 // freezing or unfreezing the display -> trigger animation if needed
531 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800532 if (mFreezeDisplay)
533 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534 }
535
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700536 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
537 // layers have been added
538 mVisibleRegionsDirty = true;
539 }
540
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 // some layers might have been removed, so
542 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700544 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700546 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700547 const size_t count = previousLayers.size();
548 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700549 const sp<LayerBase>& layer(previousLayers[i]);
550 if (currentLayers.indexOf( layer ) < 0) {
551 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700552 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700553 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700554 }
555 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 }
558
559 commitTransaction();
560}
561
562sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
563{
564 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
565}
566
567void SurfaceFlinger::computeVisibleRegions(
568 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
569{
570 const GraphicPlane& plane(graphicPlane(0));
571 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700572 const DisplayHardware& hw(plane.displayHardware());
573 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574
575 Region aboveOpaqueLayers;
576 Region aboveCoveredLayers;
577 Region dirty;
578
579 bool secureFrameBuffer = false;
580
581 size_t i = currentLayers.size();
582 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700583 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 layer->validateVisibility(planeTransform);
585
586 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700587 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588
Mathias Agopianab028732010-03-16 16:41:46 -0700589 /*
590 * opaqueRegion: area of a surface that is fully opaque.
591 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700593
594 /*
595 * visibleRegion: area of a surface that is visible on screen
596 * and not fully transparent. This is essentially the layer's
597 * footprint minus the opaque regions above it.
598 * Areas covered by a translucent surface are considered visible.
599 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700601
602 /*
603 * coveredRegion: area of a surface that is covered by all
604 * visible regions above it (which includes the translucent areas).
605 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700607
608
609 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700610 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700612 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700614 visibleRegion.andSelf(screenRegion);
615 if (!visibleRegion.isEmpty()) {
616 // Remove the transparent area from the visible region
617 if (translucent) {
618 visibleRegion.subtractSelf(layer->transparentRegionScreen);
619 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620
Mathias Agopianab028732010-03-16 16:41:46 -0700621 // compute the opaque region
622 const int32_t layerOrientation = layer->getOrientation();
623 if (s.alpha==255 && !translucent &&
624 ((layerOrientation & Transform::ROT_INVALID) == false)) {
625 // the opaque region is the layer's footprint
626 opaqueRegion = visibleRegion;
627 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 }
629 }
630
Mathias Agopianab028732010-03-16 16:41:46 -0700631 // Clip the covered region to the visible region
632 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
633
634 // Update aboveCoveredLayers for next (lower) layer
635 aboveCoveredLayers.orSelf(visibleRegion);
636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 // subtract the opaque region covered by the layers above us
638 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639
640 // compute this layer's dirty region
641 if (layer->contentDirty) {
642 // we need to invalidate the whole region
643 dirty = visibleRegion;
644 // as well, as the old visible region
645 dirty.orSelf(layer->visibleRegionScreen);
646 layer->contentDirty = false;
647 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700648 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700649 * the exposed region consists of two components:
650 * 1) what's VISIBLE now and was COVERED before
651 * 2) what's EXPOSED now less what was EXPOSED before
652 *
653 * note that (1) is conservative, we start with the whole
654 * visible region but only keep what used to be covered by
655 * something -- which mean it may have been exposed.
656 *
657 * (2) handles areas that were not covered by anything but got
658 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700659 */
Mathias Agopianab028732010-03-16 16:41:46 -0700660 const Region newExposed = visibleRegion - coveredRegion;
661 const Region oldVisibleRegion = layer->visibleRegionScreen;
662 const Region oldCoveredRegion = layer->coveredRegionScreen;
663 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
664 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665 }
666 dirty.subtractSelf(aboveOpaqueLayers);
667
668 // accumulate to the screen dirty region
669 dirtyRegion.orSelf(dirty);
670
Mathias Agopianab028732010-03-16 16:41:46 -0700671 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673
674 // Store the visible region is screen space
675 layer->setVisibleRegion(visibleRegion);
676 layer->setCoveredRegion(coveredRegion);
677
Mathias Agopian97011222009-07-28 10:57:27 -0700678 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679 if (layer->isSecure() && !visibleRegion.isEmpty()) {
680 secureFrameBuffer = true;
681 }
682 }
683
Mathias Agopian97011222009-07-28 10:57:27 -0700684 // invalidate the areas where a layer was removed
685 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
686 mDirtyRegionRemovedLayer.clear();
687
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 mSecureFrameBuffer = secureFrameBuffer;
689 opaqueRegion = aboveOpaqueLayers;
690}
691
692
693void SurfaceFlinger::commitTransaction()
694{
695 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700696 mResizeTransationPending = false;
697 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698}
699
700void SurfaceFlinger::handlePageFlip()
701{
702 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700703 LayerVector& currentLayers = const_cast<LayerVector&>(
704 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 visibleRegions |= lockPageFlip(currentLayers);
706
707 const DisplayHardware& hw = graphicPlane(0).displayHardware();
708 const Region screenRegion(hw.bounds());
709 if (visibleRegions) {
710 Region opaqueRegion;
711 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700712
713 /*
714 * rebuild the visible layer list
715 */
716 mVisibleLayersSortedByZ.clear();
717 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
718 size_t count = currentLayers.size();
719 mVisibleLayersSortedByZ.setCapacity(count);
720 for (size_t i=0 ; i<count ; i++) {
721 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
722 mVisibleLayersSortedByZ.add(currentLayers[i]);
723 }
724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 mWormholeRegion = screenRegion.subtract(opaqueRegion);
726 mVisibleRegionsDirty = false;
727 }
728
729 unlockPageFlip(currentLayers);
730 mDirtyRegion.andSelf(screenRegion);
731}
732
733bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
734{
735 bool recomputeVisibleRegions = false;
736 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700737 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700739 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 layer->lockPageFlip(recomputeVisibleRegions);
741 }
742 return recomputeVisibleRegions;
743}
744
745void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
746{
747 const GraphicPlane& plane(graphicPlane(0));
748 const Transform& planeTransform(plane.transform());
749 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700750 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700752 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753 layer->unlockPageFlip(planeTransform, mDirtyRegion);
754 }
755}
756
Mathias Agopianb8a55602009-06-26 19:06:36 -0700757
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758void SurfaceFlinger::handleRepaint()
759{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700760 // compute the invalid region
761 mInvalidRegion.orSelf(mDirtyRegion);
762 if (mInvalidRegion.isEmpty()) {
763 // nothing to do
764 return;
765 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766
767 if (UNLIKELY(mDebugRegion)) {
768 debugFlashRegions();
769 }
770
Mathias Agopianb8a55602009-06-26 19:06:36 -0700771 // set the frame buffer
772 const DisplayHardware& hw(graphicPlane(0).displayHardware());
773 glMatrixMode(GL_MODELVIEW);
774 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775
776 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700777 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
778 (flags & DisplayHardware::BUFFER_PRESERVED))
779 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700780 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
781 // takes a rectangle, we must make sure to update that whole
782 // rectangle in that case
783 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700784 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700785 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 mDirtyRegion.set(mInvalidRegion.bounds());
787 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700788 // in the BUFFER_PRESERVED case, obviously, we can update only
789 // what's needed and nothing more.
790 // NOTE: this is NOT a common case, as preserving the backbuffer
791 // is costly and usually involves copying the whole update back.
792 }
793 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700794 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700795 // We need to redraw the rectangle that will be updated
796 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700797 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700798 // rectangle instead of a region (see DisplayHardware::flip())
799 mDirtyRegion.set(mInvalidRegion.bounds());
800 } else {
801 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 mDirtyRegion.set(hw.bounds());
803 mInvalidRegion = mDirtyRegion;
804 }
805 }
806
807 // compose all surfaces
808 composeSurfaces(mDirtyRegion);
809
810 // clear the dirty regions
811 mDirtyRegion.clear();
812}
813
814void SurfaceFlinger::composeSurfaces(const Region& dirty)
815{
816 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
817 // should never happen unless the window manager has a bug
818 // draw something...
819 drawWormhole();
820 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700821 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700822 const size_t count = layers.size();
823 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian45721772010-08-12 15:03:26 -0700824 const sp<LayerBase>& layer(layers[i]);
825 const Region clip(dirty.intersect(layer->visibleRegionScreen));
826 if (!clip.isEmpty()) {
827 layer->draw(clip);
828 }
829 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830}
831
832void SurfaceFlinger::unlockClients()
833{
834 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
835 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700836 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700838 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 layer->finishPageFlip();
840 }
841}
842
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843void SurfaceFlinger::debugFlashRegions()
844{
Mathias Agopian0a917752010-06-14 21:20:00 -0700845 const DisplayHardware& hw(graphicPlane(0).displayHardware());
846 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700847
Mathias Agopian0a917752010-06-14 21:20:00 -0700848 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
849 (flags & DisplayHardware::BUFFER_PRESERVED))) {
850 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
851 mDirtyRegion.bounds() : hw.bounds());
852 composeSurfaces(repaint);
853 }
854
855 TextureManager::deactivateTextures();
856
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 glDisable(GL_BLEND);
858 glDisable(GL_DITHER);
859 glDisable(GL_SCISSOR_TEST);
860
Mathias Agopian0926f502009-05-04 14:17:04 -0700861 static int toggle = 0;
862 toggle = 1 - toggle;
863 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700864 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700865 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700866 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700867 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868
Mathias Agopian20f68782009-05-11 00:03:41 -0700869 Region::const_iterator it = mDirtyRegion.begin();
870 Region::const_iterator const end = mDirtyRegion.end();
871 while (it != end) {
872 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 GLfloat vertices[][2] = {
874 { r.left, r.top },
875 { r.left, r.bottom },
876 { r.right, r.bottom },
877 { r.right, r.top }
878 };
879 glVertexPointer(2, GL_FLOAT, 0, vertices);
880 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
881 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700882
Mathias Agopianb8a55602009-06-26 19:06:36 -0700883 if (mInvalidRegion.isEmpty()) {
884 mDirtyRegion.dump("mDirtyRegion");
885 mInvalidRegion.dump("mInvalidRegion");
886 }
887 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888
889 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700890 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891
892 glEnable(GL_SCISSOR_TEST);
893 //mDirtyRegion.dump("mDirtyRegion");
894}
895
896void SurfaceFlinger::drawWormhole() const
897{
898 const Region region(mWormholeRegion.intersect(mDirtyRegion));
899 if (region.isEmpty())
900 return;
901
902 const DisplayHardware& hw(graphicPlane(0).displayHardware());
903 const int32_t width = hw.getWidth();
904 const int32_t height = hw.getHeight();
905
906 glDisable(GL_BLEND);
907 glDisable(GL_DITHER);
908
909 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700910 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700911 Region::const_iterator it = region.begin();
912 Region::const_iterator const end = region.end();
913 while (it != end) {
914 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 const GLint sy = height - (r.top + r.height());
916 glScissor(r.left, sy, r.width(), r.height());
917 glClear(GL_COLOR_BUFFER_BIT);
918 }
919 } else {
920 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
921 { width, height }, { 0, height } };
922 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
923 glVertexPointer(2, GL_SHORT, 0, vertices);
924 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
925 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -0700926#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700927 if (GLExtensions::getInstance().haveTextureExternal()) {
928 glDisable(GL_TEXTURE_EXTERNAL_OES);
929 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700930#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 glEnable(GL_TEXTURE_2D);
932 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
933 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
934 glMatrixMode(GL_TEXTURE);
935 glLoadIdentity();
936 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700937 Region::const_iterator it = region.begin();
938 Region::const_iterator const end = region.end();
939 while (it != end) {
940 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 const GLint sy = height - (r.top + r.height());
942 glScissor(r.left, sy, r.width(), r.height());
943 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
944 }
945 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
946 }
947}
948
949void SurfaceFlinger::debugShowFPS() const
950{
951 static int mFrameCount;
952 static int mLastFrameCount = 0;
953 static nsecs_t mLastFpsTime = 0;
954 static float mFps = 0;
955 mFrameCount++;
956 nsecs_t now = systemTime();
957 nsecs_t diff = now - mLastFpsTime;
958 if (diff > ms2ns(250)) {
959 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
960 mLastFpsTime = now;
961 mLastFrameCount = mFrameCount;
962 }
963 // XXX: mFPS has the value we want
964 }
965
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700966status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967{
968 Mutex::Autolock _l(mStateLock);
969 addLayer_l(layer);
970 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
971 return NO_ERROR;
972}
973
Mathias Agopian96f08192010-06-02 23:28:45 -0700974status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
975{
Mathias Agopianf6679fc2010-08-10 18:09:09 -0700976 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -0700977 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
978}
979
980ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
981 const sp<LayerBaseClient>& lbc)
982{
983 Mutex::Autolock _l(mStateLock);
984
985 // attach this layer to the client
986 ssize_t name = client->attachLayer(lbc);
987
988 // add this layer to the current state list
989 addLayer_l(lbc);
990
991 return name;
992}
993
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700994status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995{
996 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -0700997 status_t err = purgatorizeLayer_l(layer);
998 if (err == NO_ERROR)
999 setTransactionFlags(eTransactionNeeded);
1000 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001}
1002
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001003status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001005 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1006 if (lbc != 0) {
1007 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1008 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1010 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001011 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 return NO_ERROR;
1013 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001014 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015}
1016
Mathias Agopian9a112062009-04-17 19:36:26 -07001017status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1018{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001019 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001020 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001021
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001022 layerBase->onRemoved();
1023
Mathias Agopian3d579642009-06-04 18:46:21 -07001024 // it's possible that we don't find a layer, because it might
1025 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001026 // from the user because there is a race between Client::destroySurface(),
1027 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001028 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1029}
1030
Mathias Agopian96f08192010-06-02 23:28:45 -07001031status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032{
Mathias Agopian96f08192010-06-02 23:28:45 -07001033 layer->forceVisibilityTransaction();
1034 setTransactionFlags(eTraversalNeeded);
1035 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036}
1037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1039{
1040 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1041}
1042
Mathias Agopianbb641242010-05-18 17:06:55 -07001043uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044{
1045 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1046 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001047 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 }
1049 return old;
1050}
1051
1052void SurfaceFlinger::openGlobalTransaction()
1053{
1054 android_atomic_inc(&mTransactionCount);
1055}
1056
1057void SurfaceFlinger::closeGlobalTransaction()
1058{
1059 if (android_atomic_dec(&mTransactionCount) == 1) {
1060 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001061
1062 // if there is a transaction with a resize, wait for it to
1063 // take effect before returning.
1064 Mutex::Autolock _l(mStateLock);
1065 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001066 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1067 if (CC_UNLIKELY(err != NO_ERROR)) {
1068 // just in case something goes wrong in SF, return to the
1069 // called after a few seconds.
1070 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1071 mResizeTransationPending = false;
1072 break;
1073 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001074 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 }
1076}
1077
1078status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1079{
1080 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1081 return BAD_VALUE;
1082
1083 Mutex::Autolock _l(mStateLock);
1084 mCurrentState.freezeDisplay = 1;
1085 setTransactionFlags(eTransactionNeeded);
1086
1087 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001088 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 return NO_ERROR;
1090}
1091
1092status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1093{
1094 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1095 return BAD_VALUE;
1096
1097 Mutex::Autolock _l(mStateLock);
1098 mCurrentState.freezeDisplay = 0;
1099 setTransactionFlags(eTransactionNeeded);
1100
1101 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001102 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 return NO_ERROR;
1104}
1105
Mathias Agopianc08731e2009-03-27 18:11:38 -07001106int SurfaceFlinger::setOrientation(DisplayID dpy,
1107 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108{
1109 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1110 return BAD_VALUE;
1111
1112 Mutex::Autolock _l(mStateLock);
1113 if (mCurrentState.orientation != orientation) {
1114 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001115 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 mCurrentState.orientation = orientation;
1117 setTransactionFlags(eTransactionNeeded);
1118 mTransactionCV.wait(mStateLock);
1119 } else {
1120 orientation = BAD_VALUE;
1121 }
1122 }
1123 return orientation;
1124}
1125
Mathias Agopian96f08192010-06-02 23:28:45 -07001126sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001127 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1129 uint32_t flags)
1130{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001131 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001133
1134 if (int32_t(w|h) < 0) {
1135 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1136 int(w), int(h));
1137 return surfaceHandle;
1138 }
1139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001141 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 switch (flags & eFXSurfaceMask) {
1143 case eFXSurfaceNormal:
1144 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001145 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001147 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1148 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 }
1150 break;
1151 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001152 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 break;
1154 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001155 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 break;
1157 }
1158
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001159 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001160 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001161 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001162 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001163
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001165 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001166 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001167 params->identity = surfaceHandle->getIdentity();
1168 params->width = w;
1169 params->height = h;
1170 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001171 if (normalLayer != 0) {
1172 Mutex::Autolock _l(mStateLock);
1173 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1174 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001175 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001176
Mathias Agopian96f08192010-06-02 23:28:45 -07001177 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 }
1179
1180 return surfaceHandle;
1181}
1182
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001183sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001184 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001185 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001186 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187{
1188 // initialize the surfaces
1189 switch (format) { // TODO: take h/w into account
1190 case PIXEL_FORMAT_TRANSPARENT:
1191 case PIXEL_FORMAT_TRANSLUCENT:
1192 format = PIXEL_FORMAT_RGBA_8888;
1193 break;
1194 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001195#ifdef NO_RGBX_8888
1196 format = PIXEL_FORMAT_RGB_565;
1197#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001198 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001199#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 break;
1201 }
1202
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001203#ifdef NO_RGBX_8888
1204 if (format == PIXEL_FORMAT_RGBX_8888)
1205 format = PIXEL_FORMAT_RGBA_8888;
1206#endif
1207
Mathias Agopian96f08192010-06-02 23:28:45 -07001208 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001209 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001212 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 }
1214 return layer;
1215}
1216
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001217sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001218 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001219 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220{
Mathias Agopian96f08192010-06-02 23:28:45 -07001221 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 return layer;
1224}
1225
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001226sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001227 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001228 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229{
Mathias Agopian96f08192010-06-02 23:28:45 -07001230 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 return layer;
1233}
1234
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001235sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001236 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001237 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238{
Mathias Agopian96f08192010-06-02 23:28:45 -07001239 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 return layer;
1242}
1243
Mathias Agopian96f08192010-06-02 23:28:45 -07001244status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245{
Mathias Agopian9a112062009-04-17 19:36:26 -07001246 /*
1247 * called by the window manager, when a surface should be marked for
1248 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001249 *
1250 * The surface is removed from the current and drawing lists, but placed
1251 * in the purgatory queue, so it's not destroyed right-away (we need
1252 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001253 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001254
Mathias Agopian48d819a2009-09-10 19:41:18 -07001255 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001256 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001257 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001258 if (layer != 0) {
1259 err = purgatorizeLayer_l(layer);
1260 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001261 setTransactionFlags(eTransactionNeeded);
1262 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001263 }
1264 return err;
1265}
1266
1267status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1268{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001269 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001270
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001271 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001272 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001273 sp<LayerBaseClient> layer;
1274 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001275 MessageDestroySurface(
1276 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1277 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001278 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001279 sp<LayerBaseClient> l(layer);
1280 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001281 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001282 /*
1283 * remove the layer from the current list -- chances are that it's
1284 * not in the list anyway, because it should have been removed
1285 * already upon request of the client (eg: window manager).
1286 * However, a buggy client could have not done that.
1287 * Since we know we don't have any more clients, we don't need
1288 * to use the purgatory.
1289 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001290 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001291 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1292 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001293 return true;
1294 }
1295 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001296
Mathias Agopianbb641242010-05-18 17:06:55 -07001297 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 return NO_ERROR;
1299}
1300
1301status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001302 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 int32_t count,
1304 const layer_state_t* states)
1305{
1306 Mutex::Autolock _l(mStateLock);
1307 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001309 const layer_state_t& s(states[i]);
1310 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001311 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 if (what & ePositionChanged) {
1314 if (layer->setPosition(s.x, s.y))
1315 flags |= eTraversalNeeded;
1316 }
1317 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001318 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001320 mCurrentState.layersSortedByZ.removeAt(idx);
1321 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 // we need traversal (state changed)
1323 // AND transaction (list changed)
1324 flags |= eTransactionNeeded|eTraversalNeeded;
1325 }
1326 }
1327 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001328 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001330 mResizeTransationPending = true;
1331 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 }
1333 if (what & eAlphaChanged) {
1334 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1335 flags |= eTraversalNeeded;
1336 }
1337 if (what & eMatrixChanged) {
1338 if (layer->setMatrix(s.matrix))
1339 flags |= eTraversalNeeded;
1340 }
1341 if (what & eTransparentRegionChanged) {
1342 if (layer->setTransparentRegionHint(s.transparentRegion))
1343 flags |= eTraversalNeeded;
1344 }
1345 if (what & eVisibilityChanged) {
1346 if (layer->setFlags(s.flags, s.mask))
1347 flags |= eTraversalNeeded;
1348 }
1349 }
1350 }
1351 if (flags) {
1352 setTransactionFlags(flags);
1353 }
1354 return NO_ERROR;
1355}
1356
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357void SurfaceFlinger::screenReleased(int dpy)
1358{
1359 // this may be called by a signal handler, we can't do too much in here
1360 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1361 signalEvent();
1362}
1363
1364void SurfaceFlinger::screenAcquired(int dpy)
1365{
1366 // this may be called by a signal handler, we can't do too much in here
1367 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1368 signalEvent();
1369}
1370
1371status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1372{
1373 const size_t SIZE = 1024;
1374 char buffer[SIZE];
1375 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001376 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 snprintf(buffer, SIZE, "Permission Denial: "
1378 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1379 IPCThreadState::self()->getCallingPid(),
1380 IPCThreadState::self()->getCallingUid());
1381 result.append(buffer);
1382 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001383
1384 // figure out if we're stuck somewhere
1385 const nsecs_t now = systemTime();
1386 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1387 const nsecs_t inTransaction(mDebugInTransaction);
1388 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1389 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1390
1391 // Try to get the main lock, but don't insist if we can't
1392 // (this would indicate SF is stuck, but we want to be able to
1393 // print something in dumpsys).
1394 int retry = 3;
1395 while (mStateLock.tryLock()<0 && --retry>=0) {
1396 usleep(1000000);
1397 }
1398 const bool locked(retry >= 0);
1399 if (!locked) {
1400 snprintf(buffer, SIZE,
1401 "SurfaceFlinger appears to be unresponsive, "
1402 "dumping anyways (no locks held)\n");
1403 result.append(buffer);
1404 }
1405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1407 const size_t count = currentLayers.size();
1408 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001409 const sp<LayerBase>& layer(currentLayers[i]);
1410 layer->dump(result, buffer, SIZE);
1411 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 s.transparentRegion.dump(result, "transparentRegion");
1413 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1414 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1415 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 mWormholeRegion.dump(result, "WormholeRegion");
1418 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1419 snprintf(buffer, SIZE,
1420 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1421 mFreezeDisplay?"yes":"no", mFreezeCount,
1422 mCurrentState.orientation, hw.canDraw());
1423 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001424 snprintf(buffer, SIZE,
1425 " last eglSwapBuffers() time: %f us\n"
1426 " last transaction time : %f us\n",
1427 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1428 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001429
Mathias Agopian9795c422009-08-26 16:36:26 -07001430 if (inSwapBuffersDuration || !locked) {
1431 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1432 inSwapBuffersDuration/1000.0);
1433 result.append(buffer);
1434 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001435
Mathias Agopian9795c422009-08-26 16:36:26 -07001436 if (inTransactionDuration || !locked) {
1437 snprintf(buffer, SIZE, " transaction time: %f us\n",
1438 inTransactionDuration/1000.0);
1439 result.append(buffer);
1440 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001441
Mathias Agopian3330b202009-10-05 17:07:12 -07001442 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001443 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001444
1445 if (locked) {
1446 mStateLock.unlock();
1447 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 }
1449 write(fd, result.string(), result.size());
1450 return NO_ERROR;
1451}
1452
1453status_t SurfaceFlinger::onTransact(
1454 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1455{
1456 switch (code) {
1457 case CREATE_CONNECTION:
1458 case OPEN_GLOBAL_TRANSACTION:
1459 case CLOSE_GLOBAL_TRANSACTION:
1460 case SET_ORIENTATION:
1461 case FREEZE_DISPLAY:
1462 case UNFREEZE_DISPLAY:
1463 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001464 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001465 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 {
1467 // codes that require permission check
1468 IPCThreadState* ipc = IPCThreadState::self();
1469 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001470 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001471 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1472 LOGE("Permission Denial: "
1473 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1474 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001476 break;
1477 }
1478 case CAPTURE_SCREEN:
1479 {
1480 // codes that require permission check
1481 IPCThreadState* ipc = IPCThreadState::self();
1482 const int pid = ipc->getCallingPid();
1483 const int uid = ipc->getCallingUid();
1484 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1485 LOGE("Permission Denial: "
1486 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1487 return PERMISSION_DENIED;
1488 }
1489 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 }
1491 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001492
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1494 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001495 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001496 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1497 IPCThreadState* ipc = IPCThreadState::self();
1498 const int pid = ipc->getCallingPid();
1499 const int uid = ipc->getCallingUid();
1500 LOGE("Permission Denial: "
1501 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 return PERMISSION_DENIED;
1503 }
1504 int n;
1505 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001506 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001507 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 return NO_ERROR;
1509 case 1002: // SHOW_UPDATES
1510 n = data.readInt32();
1511 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1512 return NO_ERROR;
1513 case 1003: // SHOW_BACKGROUND
1514 n = data.readInt32();
1515 mDebugBackground = n ? 1 : 0;
1516 return NO_ERROR;
1517 case 1004:{ // repaint everything
1518 Mutex::Autolock _l(mStateLock);
1519 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1520 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1521 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001522 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001523 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001524 case 1005:{ // force transaction
1525 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1526 return NO_ERROR;
1527 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001528 case 1006:{ // enable/disable GraphicLog
1529 int enabled = data.readInt32();
1530 GraphicLog::getInstance().setEnabled(enabled);
1531 return NO_ERROR;
1532 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 case 1007: // set mFreezeCount
1534 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001535 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536 return NO_ERROR;
1537 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001538 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001539 reply->writeInt32(0);
1540 reply->writeInt32(mDebugRegion);
1541 reply->writeInt32(mDebugBackground);
1542 return NO_ERROR;
1543 case 1013: {
1544 Mutex::Autolock _l(mStateLock);
1545 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1546 reply->writeInt32(hw.getPageFlipCount());
1547 }
1548 return NO_ERROR;
1549 }
1550 }
1551 return err;
1552}
1553
Mathias Agopian59119e62010-10-11 12:37:43 -07001554// ---------------------------------------------------------------------------
1555
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001556status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1557 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001558{
Mathias Agopian59119e62010-10-11 12:37:43 -07001559 if (!GLExtensions::getInstance().haveFramebufferObject())
1560 return INVALID_OPERATION;
1561
1562 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001563 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001564 const uint32_t hw_w = hw.getWidth();
1565 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001566 GLfloat u = 1;
1567 GLfloat v = 1;
1568
1569 // make sure to clear all GL error flags
1570 while ( glGetError() != GL_NO_ERROR ) ;
1571
1572 // create a FBO
1573 GLuint name, tname;
1574 glGenTextures(1, &tname);
1575 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001576 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1577 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001578 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001579 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001580 GLint tw = (2 << (31 - clz(hw_w)));
1581 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001582 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1583 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001584 u = GLfloat(hw_w) / tw;
1585 v = GLfloat(hw_h) / th;
1586 }
1587 glGenFramebuffersOES(1, &name);
1588 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001589 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1590 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001591
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001592 // redraw the screen entirely...
1593 glClearColor(0,0,0,1);
1594 glClear(GL_COLOR_BUFFER_BIT);
1595 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1596 const size_t count = layers.size();
1597 for (size_t i=0 ; i<count ; ++i) {
1598 const sp<LayerBase>& layer(layers[i]);
1599 layer->drawForSreenShot();
1600 }
1601
1602 // back to main framebuffer
1603 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1604 glDisable(GL_SCISSOR_TEST);
1605 glDeleteFramebuffersOES(1, &name);
1606
1607 *textureName = tname;
1608 *uOut = u;
1609 *vOut = v;
1610 return NO_ERROR;
1611}
1612
1613// ---------------------------------------------------------------------------
1614
1615status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1616{
1617 status_t result = PERMISSION_DENIED;
1618
1619 if (!GLExtensions::getInstance().haveFramebufferObject())
1620 return INVALID_OPERATION;
1621
1622 // get screen geometry
1623 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1624 const uint32_t hw_w = hw.getWidth();
1625 const uint32_t hw_h = hw.getHeight();
1626 const Region screenBounds(hw.bounds());
1627
1628 GLfloat u, v;
1629 GLuint tname;
1630 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1631 if (result != NO_ERROR) {
1632 return result;
1633 }
1634
1635 GLfloat vtx[8];
1636 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1637 glEnable(GL_TEXTURE_2D);
1638 glBindTexture(GL_TEXTURE_2D, tname);
1639 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1640 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1641 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1642 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1643 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1644 glVertexPointer(2, GL_FLOAT, 0, vtx);
1645
1646 class s_curve_interpolator {
1647 const float nbFrames, s, v;
1648 public:
1649 s_curve_interpolator(int nbFrames, float s)
1650 : nbFrames(1.0f / (nbFrames-1)), s(s),
1651 v(1.0f + expf(-s + 0.5f*s)) {
1652 }
1653 float operator()(int f) {
1654 const float x = f * nbFrames;
1655 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1656 }
1657 };
1658
1659 class v_stretch {
1660 const GLfloat hw_w, hw_h;
1661 public:
1662 v_stretch(uint32_t hw_w, uint32_t hw_h)
1663 : hw_w(hw_w), hw_h(hw_h) {
1664 }
1665 void operator()(GLfloat* vtx, float v) {
1666 const GLfloat w = hw_w + (hw_w * v);
1667 const GLfloat h = hw_h - (hw_h * v);
1668 const GLfloat x = (hw_w - w) * 0.5f;
1669 const GLfloat y = (hw_h - h) * 0.5f;
1670 vtx[0] = x; vtx[1] = y;
1671 vtx[2] = x; vtx[3] = y + h;
1672 vtx[4] = x + w; vtx[5] = y + h;
1673 vtx[6] = x + w; vtx[7] = y;
1674 }
1675 };
1676
1677 class h_stretch {
1678 const GLfloat hw_w, hw_h;
1679 public:
1680 h_stretch(uint32_t hw_w, uint32_t hw_h)
1681 : hw_w(hw_w), hw_h(hw_h) {
1682 }
1683 void operator()(GLfloat* vtx, float v) {
1684 const GLfloat w = hw_w - (hw_w * v);
1685 const GLfloat h = 1.0f;
1686 const GLfloat x = (hw_w - w) * 0.5f;
1687 const GLfloat y = (hw_h - h) * 0.5f;
1688 vtx[0] = x; vtx[1] = y;
1689 vtx[2] = x; vtx[3] = y + h;
1690 vtx[4] = x + w; vtx[5] = y + h;
1691 vtx[6] = x + w; vtx[7] = y;
1692 }
1693 };
1694
1695 // the full animation is 24 frames
1696 const int nbFrames = 12;
1697 s_curve_interpolator itr(nbFrames, 7.5f);
1698 s_curve_interpolator itg(nbFrames, 8.0f);
1699 s_curve_interpolator itb(nbFrames, 8.5f);
1700
1701 v_stretch vverts(hw_w, hw_h);
1702 glEnable(GL_BLEND);
1703 glBlendFunc(GL_ONE, GL_ONE);
1704 for (int i=0 ; i<nbFrames ; i++) {
1705 float x, y, w, h;
1706 const float vr = itr(i);
1707 const float vg = itg(i);
1708 const float vb = itb(i);
1709
1710 // clear screen
1711 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001712 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001713 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001714
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001715 // draw the red plane
1716 vverts(vtx, vr);
1717 glColorMask(1,0,0,1);
1718 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001719
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001720 // draw the green plane
1721 vverts(vtx, vg);
1722 glColorMask(0,1,0,1);
1723 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001724
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001725 // draw the blue plane
1726 vverts(vtx, vb);
1727 glColorMask(0,0,1,1);
1728 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001729
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001730 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001731 glDisable(GL_TEXTURE_2D);
1732 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001733 glColor4f(vg, vg, vg, 1);
1734 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1735 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001736 }
1737
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001738 h_stretch hverts(hw_w, hw_h);
1739 glDisable(GL_BLEND);
1740 glDisable(GL_TEXTURE_2D);
1741 glColorMask(1,1,1,1);
1742 for (int i=0 ; i<nbFrames ; i++) {
1743 const float v = itg(i);
1744 hverts(vtx, v);
1745 glClear(GL_COLOR_BUFFER_BIT);
1746 glColor4f(1-v, 1-v, 1-v, 1);
1747 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1748 hw.flip(screenBounds);
1749 }
1750
1751 glColorMask(1,1,1,1);
1752 glEnable(GL_SCISSOR_TEST);
1753 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1754 glDeleteTextures(1, &tname);
1755 return NO_ERROR;
1756}
1757
1758status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1759{
1760 status_t result = PERMISSION_DENIED;
1761
1762 if (!GLExtensions::getInstance().haveFramebufferObject())
1763 return INVALID_OPERATION;
1764
1765
1766 // get screen geometry
1767 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1768 const uint32_t hw_w = hw.getWidth();
1769 const uint32_t hw_h = hw.getHeight();
1770 const Region screenBounds(hw.bounds());
1771
1772 GLfloat u, v;
1773 GLuint tname;
1774 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1775 if (result != NO_ERROR) {
1776 return result;
1777 }
1778
1779 // back to main framebuffer
1780 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1781 glDisable(GL_SCISSOR_TEST);
1782
1783 GLfloat vtx[8];
1784 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1785 glEnable(GL_TEXTURE_2D);
1786 glBindTexture(GL_TEXTURE_2D, tname);
1787 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1788 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1789 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1790 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1791 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1792 glVertexPointer(2, GL_FLOAT, 0, vtx);
1793
1794 class s_curve_interpolator {
1795 const float nbFrames, s, v;
1796 public:
1797 s_curve_interpolator(int nbFrames, float s)
1798 : nbFrames(1.0f / (nbFrames-1)), s(s),
1799 v(1.0f + expf(-s + 0.5f*s)) {
1800 }
1801 float operator()(int f) {
1802 const float x = f * nbFrames;
1803 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1804 }
1805 };
1806
1807 class v_stretch {
1808 const GLfloat hw_w, hw_h;
1809 public:
1810 v_stretch(uint32_t hw_w, uint32_t hw_h)
1811 : hw_w(hw_w), hw_h(hw_h) {
1812 }
1813 void operator()(GLfloat* vtx, float v) {
1814 const GLfloat w = hw_w + (hw_w * v);
1815 const GLfloat h = hw_h - (hw_h * v);
1816 const GLfloat x = (hw_w - w) * 0.5f;
1817 const GLfloat y = (hw_h - h) * 0.5f;
1818 vtx[0] = x; vtx[1] = y;
1819 vtx[2] = x; vtx[3] = y + h;
1820 vtx[4] = x + w; vtx[5] = y + h;
1821 vtx[6] = x + w; vtx[7] = y;
1822 }
1823 };
1824
1825 class h_stretch {
1826 const GLfloat hw_w, hw_h;
1827 public:
1828 h_stretch(uint32_t hw_w, uint32_t hw_h)
1829 : hw_w(hw_w), hw_h(hw_h) {
1830 }
1831 void operator()(GLfloat* vtx, float v) {
1832 const GLfloat w = hw_w - (hw_w * v);
1833 const GLfloat h = 1.0f;
1834 const GLfloat x = (hw_w - w) * 0.5f;
1835 const GLfloat y = (hw_h - h) * 0.5f;
1836 vtx[0] = x; vtx[1] = y;
1837 vtx[2] = x; vtx[3] = y + h;
1838 vtx[4] = x + w; vtx[5] = y + h;
1839 vtx[6] = x + w; vtx[7] = y;
1840 }
1841 };
1842
1843 // the full animation is 24 frames
1844 const int nbFrames = 12;
1845 s_curve_interpolator itr(nbFrames, 7.5f);
1846 s_curve_interpolator itg(nbFrames, 8.0f);
1847 s_curve_interpolator itb(nbFrames, 8.5f);
1848
1849 h_stretch hverts(hw_w, hw_h);
1850 glDisable(GL_BLEND);
1851 glDisable(GL_TEXTURE_2D);
1852 glColorMask(1,1,1,1);
1853 for (int i=nbFrames-1 ; i>=0 ; i--) {
1854 const float v = itg(i);
1855 hverts(vtx, v);
1856 glClear(GL_COLOR_BUFFER_BIT);
1857 glColor4f(1-v, 1-v, 1-v, 1);
1858 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1859 hw.flip(screenBounds);
1860 }
1861
1862 v_stretch vverts(hw_w, hw_h);
1863 glEnable(GL_BLEND);
1864 glBlendFunc(GL_ONE, GL_ONE);
1865 for (int i=nbFrames-1 ; i>=0 ; i--) {
1866 float x, y, w, h;
1867 const float vr = itr(i);
1868 const float vg = itg(i);
1869 const float vb = itb(i);
1870
1871 // clear screen
1872 glColorMask(1,1,1,1);
1873 glClear(GL_COLOR_BUFFER_BIT);
1874 glEnable(GL_TEXTURE_2D);
1875
1876 // draw the red plane
1877 vverts(vtx, vr);
1878 glColorMask(1,0,0,1);
1879 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1880
1881 // draw the green plane
1882 vverts(vtx, vg);
1883 glColorMask(0,1,0,1);
1884 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1885
1886 // draw the blue plane
1887 vverts(vtx, vb);
1888 glColorMask(0,0,1,1);
1889 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1890
1891 hw.flip(screenBounds);
1892 }
1893
1894 glColorMask(1,1,1,1);
1895 glEnable(GL_SCISSOR_TEST);
1896 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07001897 glDeleteTextures(1, &tname);
1898
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001899 return NO_ERROR;
1900}
1901
1902// ---------------------------------------------------------------------------
1903
1904status_t SurfaceFlinger::turnElectronBeamOffImplLocked()
1905{
1906 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1907 if (!hw.canDraw()) {
1908 // we're already off
1909 return NO_ERROR;
1910 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001911 electronBeamOffAnimationImplLocked();
1912 hw.setCanDraw(false);
1913 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07001914}
1915
1916status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
1917{
Mathias Agopian59119e62010-10-11 12:37:43 -07001918 class MessageTurnElectronBeamOff : public MessageBase {
1919 SurfaceFlinger* flinger;
1920 status_t result;
1921 public:
1922 MessageTurnElectronBeamOff(SurfaceFlinger* flinger)
1923 : flinger(flinger), result(PERMISSION_DENIED) {
1924 }
1925 status_t getResult() const {
1926 return result;
1927 }
1928 virtual bool handler() {
1929 Mutex::Autolock _l(flinger->mStateLock);
1930 result = flinger->turnElectronBeamOffImplLocked();
1931 return true;
1932 }
1933 };
1934
1935 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this);
1936 status_t res = postMessageSync(msg);
1937 if (res == NO_ERROR) {
1938 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001939
1940 // work-around: when the power-manager calls us we activate the
1941 // animation. eventually, the "on" animation will be called
1942 // by the power-manager itself
1943 mElectronBeamAnimation = true;
Mathias Agopian59119e62010-10-11 12:37:43 -07001944 }
1945 return res;
1946}
1947
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001948// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001949
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001950status_t SurfaceFlinger::turnElectronBeamOnImplLocked()
1951{
1952 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1953 if (hw.canDraw()) {
1954 // we're already on
1955 return NO_ERROR;
1956 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001957 electronBeamOnAnimationImplLocked();
1958 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07001959
1960 // make sure to redraw the whole screen when the animation is done
1961 mDirtyRegion.set(hw.bounds());
1962 signalEvent();
1963
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001964 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001965}
1966
1967status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
1968{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001969 class MessageTurnElectronBeamOn : public MessageBase {
1970 SurfaceFlinger* flinger;
1971 status_t result;
1972 public:
1973 MessageTurnElectronBeamOn(SurfaceFlinger* flinger)
1974 : flinger(flinger), result(PERMISSION_DENIED) {
1975 }
1976 status_t getResult() const {
1977 return result;
1978 }
1979 virtual bool handler() {
1980 Mutex::Autolock _l(flinger->mStateLock);
1981 result = flinger->turnElectronBeamOnImplLocked();
1982 return true;
1983 }
1984 };
1985
1986 postMessageAsync( new MessageTurnElectronBeamOn(this) );
1987 return NO_ERROR;
1988}
1989
1990// ---------------------------------------------------------------------------
1991
Mathias Agopiandf85c452010-09-29 13:02:36 -07001992status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
1993 sp<IMemoryHeap>* heap,
1994 uint32_t* w, uint32_t* h, PixelFormat* f,
1995 uint32_t sw, uint32_t sh)
1996{
1997 status_t result = PERMISSION_DENIED;
1998
1999 // only one display supported for now
2000 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2001 return BAD_VALUE;
2002
2003 if (!GLExtensions::getInstance().haveFramebufferObject())
2004 return INVALID_OPERATION;
2005
2006 // get screen geometry
2007 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2008 const uint32_t hw_w = hw.getWidth();
2009 const uint32_t hw_h = hw.getHeight();
2010
2011 if ((sw > hw_w) || (sh > hw_h))
2012 return BAD_VALUE;
2013
2014 sw = (!sw) ? hw_w : sw;
2015 sh = (!sh) ? hw_h : sh;
2016 const size_t size = sw * sh * 4;
2017
2018 // make sure to clear all GL error flags
2019 while ( glGetError() != GL_NO_ERROR ) ;
2020
2021 // create a FBO
2022 GLuint name, tname;
2023 glGenRenderbuffersOES(1, &tname);
2024 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2025 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2026 glGenFramebuffersOES(1, &name);
2027 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2028 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2029 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2030
2031 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2032 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2033
2034 // invert everything, b/c glReadPixel() below will invert the FB
2035 glViewport(0, 0, sw, sh);
2036 glMatrixMode(GL_PROJECTION);
2037 glPushMatrix();
2038 glLoadIdentity();
2039 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2040 glMatrixMode(GL_MODELVIEW);
2041
2042 // redraw the screen entirely...
2043 glClearColor(0,0,0,1);
2044 glClear(GL_COLOR_BUFFER_BIT);
2045 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2046 const size_t count = layers.size();
2047 for (size_t i=0 ; i<count ; ++i) {
2048 const sp<LayerBase>& layer(layers[i]);
2049 layer->drawForSreenShot();
2050 }
2051
2052 // XXX: this is needed on tegra
2053 glScissor(0, 0, sw, sh);
2054
2055 // check for errors and return screen capture
2056 if (glGetError() != GL_NO_ERROR) {
2057 // error while rendering
2058 result = INVALID_OPERATION;
2059 } else {
2060 // allocate shared memory large enough to hold the
2061 // screen capture
2062 sp<MemoryHeapBase> base(
2063 new MemoryHeapBase(size, 0, "screen-capture") );
2064 void* const ptr = base->getBase();
2065 if (ptr) {
2066 // capture the screen with glReadPixels()
2067 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2068 if (glGetError() == GL_NO_ERROR) {
2069 *heap = base;
2070 *w = sw;
2071 *h = sh;
2072 *f = PIXEL_FORMAT_RGBA_8888;
2073 result = NO_ERROR;
2074 }
2075 } else {
2076 result = NO_MEMORY;
2077 }
2078 }
2079
2080 glEnable(GL_SCISSOR_TEST);
2081 glViewport(0, 0, hw_w, hw_h);
2082 glMatrixMode(GL_PROJECTION);
2083 glPopMatrix();
2084 glMatrixMode(GL_MODELVIEW);
2085
2086
2087 } else {
2088 result = BAD_VALUE;
2089 }
2090
2091 // release FBO resources
2092 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2093 glDeleteRenderbuffersOES(1, &tname);
2094 glDeleteFramebuffersOES(1, &name);
2095 return result;
2096}
2097
2098
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002099status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2100 sp<IMemoryHeap>* heap,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002101 uint32_t* width, uint32_t* height, PixelFormat* format,
2102 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002103{
2104 // only one display supported for now
2105 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2106 return BAD_VALUE;
2107
2108 if (!GLExtensions::getInstance().haveFramebufferObject())
2109 return INVALID_OPERATION;
2110
2111 class MessageCaptureScreen : public MessageBase {
2112 SurfaceFlinger* flinger;
2113 DisplayID dpy;
2114 sp<IMemoryHeap>* heap;
2115 uint32_t* w;
2116 uint32_t* h;
2117 PixelFormat* f;
Mathias Agopiandf85c452010-09-29 13:02:36 -07002118 uint32_t sw;
2119 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002120 status_t result;
2121 public:
2122 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002123 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
2124 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002125 : flinger(flinger), dpy(dpy),
Mathias Agopiandf85c452010-09-29 13:02:36 -07002126 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002127 {
2128 }
2129 status_t getResult() const {
2130 return result;
2131 }
2132 virtual bool handler() {
2133 Mutex::Autolock _l(flinger->mStateLock);
2134
2135 // if we have secure windows, never allow the screen capture
2136 if (flinger->mSecureFrameBuffer)
2137 return true;
2138
Mathias Agopiandf85c452010-09-29 13:02:36 -07002139 result = flinger->captureScreenImplLocked(dpy,
2140 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002141
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002142 return true;
2143 }
2144 };
2145
2146 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopiandf85c452010-09-29 13:02:36 -07002147 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002148 status_t res = postMessageSync(msg);
2149 if (res == NO_ERROR) {
2150 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2151 }
2152 return res;
2153}
2154
2155// ---------------------------------------------------------------------------
2156
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002157sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2158{
2159 sp<Layer> result;
2160 Mutex::Autolock _l(mStateLock);
2161 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2162 return result;
2163}
2164
2165// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002166
Mathias Agopian96f08192010-06-02 23:28:45 -07002167Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002168 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002169{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002170}
2171
Mathias Agopian96f08192010-06-02 23:28:45 -07002172Client::~Client()
2173{
Mathias Agopian96f08192010-06-02 23:28:45 -07002174 const size_t count = mLayers.size();
2175 for (size_t i=0 ; i<count ; i++) {
2176 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2177 if (layer != 0) {
2178 mFlinger->removeLayer(layer);
2179 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002180 }
2181}
2182
Mathias Agopian96f08192010-06-02 23:28:45 -07002183status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002184 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002185}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002186
Mathias Agopian96f08192010-06-02 23:28:45 -07002187ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2188{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002189 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002190 mLayers.add(name, layer);
2191 return name;
2192}
2193
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002194void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002195{
Mathias Agopian96f08192010-06-02 23:28:45 -07002196 // we do a linear search here, because this doesn't happen often
2197 const size_t count = mLayers.size();
2198 for (size_t i=0 ; i<count ; i++) {
2199 if (mLayers.valueAt(i) == layer) {
2200 mLayers.removeItemsAt(i, 1);
2201 break;
2202 }
2203 }
2204}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002205sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2206 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002207 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2208 if (layer != 0) {
2209 lbc = layer.promote();
2210 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002211 }
2212 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002213}
2214
Mathias Agopian96f08192010-06-02 23:28:45 -07002215sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002216 return 0;
2217}
2218ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2219 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002220}
Mathias Agopian96f08192010-06-02 23:28:45 -07002221sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002222 ISurfaceComposerClient::surface_data_t* params, int pid,
2223 const String8& name,
2224 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225 uint32_t flags)
2226{
Mathias Agopian96f08192010-06-02 23:28:45 -07002227 return mFlinger->createSurface(this, pid, name, params,
2228 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002229}
Mathias Agopian96f08192010-06-02 23:28:45 -07002230status_t Client::destroySurface(SurfaceID sid) {
2231 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002232}
Mathias Agopian96f08192010-06-02 23:28:45 -07002233status_t Client::setState(int32_t count, const layer_state_t* states) {
2234 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235}
2236
2237// ---------------------------------------------------------------------------
2238
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002239UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2240 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2241{
2242 const int pgsize = getpagesize();
2243 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2244
2245 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2246 "SurfaceFlinger Client control-block");
2247
2248 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2249 if (ctrlblk) { // construct the shared structure in-place.
2250 new(ctrlblk) SharedClient;
2251 }
2252}
2253
2254UserClient::~UserClient()
2255{
2256 if (ctrlblk) {
2257 ctrlblk->~SharedClient(); // destroy our shared-structure.
2258 }
2259
2260 /*
2261 * When a UserClient dies, it's unclear what to do exactly.
2262 * We could go ahead and destroy all surfaces linked to that client
2263 * however, it wouldn't be fair to the main Client
2264 * (usually the the window-manager), which might want to re-target
2265 * the layer to another UserClient.
2266 * I think the best is to do nothing, or not much; in most cases the
2267 * WM itself will go ahead and clean things up when it detects a client of
2268 * his has died.
2269 * The remaining question is what to display? currently we keep
2270 * just keep the current buffer.
2271 */
2272}
2273
2274status_t UserClient::initCheck() const {
2275 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2276}
2277
2278void UserClient::detachLayer(const Layer* layer)
2279{
2280 int32_t name = layer->getToken();
2281 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002282 int32_t mask = 1LU<<name;
2283 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2284 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2285 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002286 }
2287}
2288
2289sp<IMemoryHeap> UserClient::getControlBlock() const {
2290 return mCblkHeap;
2291}
2292
2293ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2294{
2295 int32_t name = NAME_NOT_FOUND;
2296 sp<Layer> layer(mFlinger->getLayer(sur));
2297 if (layer == 0) return name;
2298
Mathias Agopian579b3f82010-06-08 19:54:15 -07002299 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002300 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07002301 if ((name >= 0) && (layer->getClient() == this))
2302 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002303
2304 name = 0;
2305 do {
2306 int32_t mask = 1LU<<name;
2307 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2308 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002309 status_t err = layer->setToken(
2310 const_cast<UserClient*>(this), ctrlblk, name);
2311 if (err != NO_ERROR) {
2312 // free the name
2313 android_atomic_and(~mask, &mBitmap);
2314 name = err;
2315 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002316 break;
2317 }
2318 if (++name > 31)
2319 name = NO_MEMORY;
2320 } while(name >= 0);
2321
Mathias Agopian53503a92010-06-08 15:40:56 -07002322 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2323 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002324 return name;
2325}
2326
2327sp<ISurface> UserClient::createSurface(
2328 ISurfaceComposerClient::surface_data_t* params, int pid,
2329 const String8& name,
2330 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2331 uint32_t flags) {
2332 return 0;
2333}
2334status_t UserClient::destroySurface(SurfaceID sid) {
2335 return INVALID_OPERATION;
2336}
2337status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2338 return INVALID_OPERATION;
2339}
2340
2341// ---------------------------------------------------------------------------
2342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002343GraphicPlane::GraphicPlane()
2344 : mHw(0)
2345{
2346}
2347
2348GraphicPlane::~GraphicPlane() {
2349 delete mHw;
2350}
2351
2352bool GraphicPlane::initialized() const {
2353 return mHw ? true : false;
2354}
2355
Mathias Agopian2b92d892010-02-08 15:49:35 -08002356int GraphicPlane::getWidth() const {
2357 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002358}
2359
Mathias Agopian2b92d892010-02-08 15:49:35 -08002360int GraphicPlane::getHeight() const {
2361 return mHeight;
2362}
2363
2364void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2365{
2366 mHw = hw;
2367
2368 // initialize the display orientation transform.
2369 // it's a constant that should come from the display driver.
2370 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2371 char property[PROPERTY_VALUE_MAX];
2372 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2373 //displayOrientation
2374 switch (atoi(property)) {
2375 case 90:
2376 displayOrientation = ISurfaceComposer::eOrientation90;
2377 break;
2378 case 270:
2379 displayOrientation = ISurfaceComposer::eOrientation270;
2380 break;
2381 }
2382 }
2383
2384 const float w = hw->getWidth();
2385 const float h = hw->getHeight();
2386 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2387 &mDisplayTransform);
2388 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2389 mDisplayWidth = h;
2390 mDisplayHeight = w;
2391 } else {
2392 mDisplayWidth = w;
2393 mDisplayHeight = h;
2394 }
2395
2396 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002397}
2398
2399status_t GraphicPlane::orientationToTransfrom(
2400 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002401{
2402 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002403 switch (orientation) {
2404 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002405 flags = Transform::ROT_0;
2406 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002407 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002408 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002409 break;
2410 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002411 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002412 break;
2413 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002414 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002415 break;
2416 default:
2417 return BAD_VALUE;
2418 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002419 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002420 return NO_ERROR;
2421}
2422
2423status_t GraphicPlane::setOrientation(int orientation)
2424{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002425 // If the rotation can be handled in hardware, this is where
2426 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002427
2428 const DisplayHardware& hw(displayHardware());
2429 const float w = mDisplayWidth;
2430 const float h = mDisplayHeight;
2431 mWidth = int(w);
2432 mHeight = int(h);
2433
2434 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002435 GraphicPlane::orientationToTransfrom(orientation, w, h,
2436 &orientationTransform);
2437 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2438 mWidth = int(h);
2439 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002440 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002441
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002442 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002443 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444 return NO_ERROR;
2445}
2446
2447const DisplayHardware& GraphicPlane::displayHardware() const {
2448 return *mHw;
2449}
2450
Mathias Agopian59119e62010-10-11 12:37:43 -07002451DisplayHardware& GraphicPlane::editDisplayHardware() {
2452 return *mHw;
2453}
2454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002455const Transform& GraphicPlane::transform() const {
2456 return mGlobalTransform;
2457}
2458
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002459EGLDisplay GraphicPlane::getEGLDisplay() const {
2460 return mHw->getEGLDisplay();
2461}
2462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002463// ---------------------------------------------------------------------------
2464
2465}; // namespace android