blob: e74e0009232d2be40d4888538413315cafd9bf02 [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>
34#include <binder/MemoryDealer.h>
35#include <binder/MemoryBase.h>
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
40#include <ui/PixelFormat.h>
41#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include <pixelflinger/pixelflinger.h>
44#include <GLES/gl.h>
45
46#include "clz.h"
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
49#include "LayerBlur.h"
50#include "LayerBuffer.h"
51#include "LayerDim.h"
52#include "LayerBitmap.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 {
67
68// ---------------------------------------------------------------------------
69
70void SurfaceFlinger::instantiate() {
71 defaultServiceManager()->addService(
72 String16("SurfaceFlinger"), new SurfaceFlinger());
73}
74
75void SurfaceFlinger::shutdown() {
76 // we should unregister here, but not really because
77 // when (if) the service manager goes away, all the services
78 // it has a reference to will leave too.
79}
80
81// ---------------------------------------------------------------------------
82
83SurfaceFlinger::LayerVector::LayerVector(const SurfaceFlinger::LayerVector& rhs)
84 : lookup(rhs.lookup), layers(rhs.layers)
85{
86}
87
88ssize_t SurfaceFlinger::LayerVector::indexOf(
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090{
91 if (guess<size() && lookup.keyAt(guess) == key)
92 return guess;
93 const ssize_t i = lookup.indexOfKey(key);
94 if (i>=0) {
95 const size_t idx = lookup.valueAt(i);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 LOGE_IF(layers[idx]!=key,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 return idx;
100 }
101 return i;
102}
103
104ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105 const sp<LayerBase>& layer,
106 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107{
108 size_t count = layers.size();
109 ssize_t l = 0;
110 ssize_t h = count-1;
111 ssize_t mid;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112 sp<LayerBase> const* a = layers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 while (l <= h) {
114 mid = l + (h - l)/2;
115 const int c = cmp(a+mid, &layer);
116 if (c == 0) { l = mid; break; }
117 else if (c<0) { l = mid+1; }
118 else { h = mid-1; }
119 }
120 size_t order = l;
121 while (order<count && !cmp(&layer, a+order)) {
122 order++;
123 }
124 count = lookup.size();
125 for (size_t i=0 ; i<count ; i++) {
126 if (lookup.valueAt(i) >= order) {
127 lookup.editValueAt(i)++;
128 }
129 }
130 layers.insertAt(layer, order);
131 lookup.add(layer, order);
132 return order;
133}
134
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136{
137 const ssize_t keyIndex = lookup.indexOfKey(layer);
138 if (keyIndex >= 0) {
139 const size_t index = lookup.valueAt(keyIndex);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 LOGE_IF(layers[index]!=layer,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142 this, int(index), layers[index].get(), layer.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 layers.removeItemsAt(index);
144 lookup.removeItemsAt(keyIndex);
145 const size_t count = lookup.size();
146 for (size_t i=0 ; i<count ; i++) {
147 if (lookup.valueAt(i) >= size_t(index)) {
148 lookup.editValueAt(i)--;
149 }
150 }
151 return index;
152 }
153 return NAME_NOT_FOUND;
154}
155
156ssize_t SurfaceFlinger::LayerVector::reorder(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157 const sp<LayerBase>& layer,
158 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
160 // XXX: it's a little lame. but oh well...
161 ssize_t err = remove(layer);
162 if (err >=0)
163 err = add(layer, cmp);
164 return err;
165}
166
167// ---------------------------------------------------------------------------
168#if 0
169#pragma mark -
170#endif
171
172SurfaceFlinger::SurfaceFlinger()
173 : BnSurfaceComposer(), Thread(false),
174 mTransactionFlags(0),
175 mTransactionCount(0),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700176 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -0700178 mHardwareTest("android.permission.HARDWARE_TEST"),
179 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
180 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 mLastScheduledBroadcast(NULL),
182 mVisibleRegionsDirty(false),
183 mDeferReleaseConsole(false),
184 mFreezeDisplay(false),
185 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700186 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 mDebugBackground(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 mConsoleSignals(0),
190 mSecureFrameBuffer(0)
191{
192 init();
193}
194
195void SurfaceFlinger::init()
196{
197 LOGI("SurfaceFlinger is starting");
198
199 // debugging stuff...
200 char value[PROPERTY_VALUE_MAX];
201 property_get("debug.sf.showupdates", value, "0");
202 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 property_get("debug.sf.showbackground", value, "0");
204 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205
206 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
210SurfaceFlinger::~SurfaceFlinger()
211{
212 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213}
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
216{
217 return graphicPlane(0).displayHardware().getOverlayEngine();
218}
219
220sp<IMemory> SurfaceFlinger::getCblk() const
221{
222 return mServerCblkMemory;
223}
224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
226{
227 Mutex::Autolock _l(mStateLock);
228 uint32_t token = mTokens.acquire();
229
Mathias Agopianf9d93272009-06-19 17:00:27 -0700230 sp<Client> client = new Client(token, this);
231 if (client->ctrlblk == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 mTokens.release(token);
233 return 0;
234 }
235 status_t err = mClientsMap.add(token, client);
236 if (err < 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 mTokens.release(token);
238 return 0;
239 }
240 sp<BClient> bclient =
241 new BClient(this, token, client->controlBlockMemory());
242 return bclient;
243}
244
245void SurfaceFlinger::destroyConnection(ClientID cid)
246{
247 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -0700248 sp<Client> client = mClientsMap.valueFor(cid);
249 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 // free all the layers this client owns
Mathias Agopian3d579642009-06-04 18:46:21 -0700251 Vector< wp<LayerBaseClient> > layers(client->getLayers());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 const size_t count = layers.size();
253 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254 sp<LayerBaseClient> layer(layers[i].promote());
255 if (layer != 0) {
Mathias Agopian3d579642009-06-04 18:46:21 -0700256 purgatorizeLayer_l(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 }
259
260 // the resources associated with this client will be freed
261 // during the next transaction, after these surfaces have been
262 // properly removed from the screen
263
264 // remove this client from our ClientID->Client mapping.
265 mClientsMap.removeItem(cid);
266
267 // and add it to the list of disconnected clients
268 mDisconnectedClients.add(client);
269
270 // request a transaction
271 setTransactionFlags(eTransactionNeeded);
272 }
273}
274
275const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
276{
277 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
278 const GraphicPlane& plane(mGraphicPlanes[dpy]);
279 return plane;
280}
281
282GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
283{
284 return const_cast<GraphicPlane&>(
285 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
286}
287
288void SurfaceFlinger::bootFinished()
289{
290 const nsecs_t now = systemTime();
291 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700292 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
293 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294}
295
296void SurfaceFlinger::onFirstRef()
297{
298 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
299
300 // Wait for the main thread to be done with its initialization
301 mReadyToRunBarrier.wait();
302}
303
304
305static inline uint16_t pack565(int r, int g, int b) {
306 return (r<<11)|(g<<5)|b;
307}
308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309status_t SurfaceFlinger::readyToRun()
310{
311 LOGI( "SurfaceFlinger's main thread ready to run. "
312 "Initializing graphics H/W...");
313
314 // create the shared control-block
315 mServerHeap = new MemoryDealer(4096, MemoryDealer::READ_ONLY);
316 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
317
318 mServerCblkMemory = mServerHeap->allocate(4096);
319 LOGE_IF(mServerCblkMemory==0, "can't create shared control block");
320
321 mServerCblk = static_cast<surface_flinger_cblk_t *>(mServerCblkMemory->pointer());
322 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
323 new(mServerCblk) surface_flinger_cblk_t;
324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 // we only support one display currently
326 int dpy = 0;
327
328 {
329 // initialize the main display
330 GraphicPlane& plane(graphicPlane(dpy));
331 DisplayHardware* const hw = new DisplayHardware(this, dpy);
332 plane.setDisplayHardware(hw);
333 }
334
335 // initialize primary screen
336 // (other display should be initialized in the same manner, but
337 // asynchronously, as they could come and go. None of this is supported
338 // yet).
339 const GraphicPlane& plane(graphicPlane(dpy));
340 const DisplayHardware& hw = plane.displayHardware();
341 const uint32_t w = hw.getWidth();
342 const uint32_t h = hw.getHeight();
343 const uint32_t f = hw.getFormat();
344 hw.makeCurrent();
345
346 // initialize the shared control block
347 mServerCblk->connected |= 1<<dpy;
348 display_cblk_t* dcblk = mServerCblk->displays + dpy;
349 memset(dcblk, 0, sizeof(display_cblk_t));
350 dcblk->w = w;
351 dcblk->h = h;
352 dcblk->format = f;
353 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
354 dcblk->xdpi = hw.getDpiX();
355 dcblk->ydpi = hw.getDpiY();
356 dcblk->fps = hw.getRefreshRate();
357 dcblk->density = hw.getDensity();
358 asm volatile ("":::"memory");
359
360 // Initialize OpenGL|ES
361 glActiveTexture(GL_TEXTURE0);
362 glBindTexture(GL_TEXTURE_2D, 0);
363 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
364 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
365 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
366 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
367 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
368 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
369 glPixelStorei(GL_PACK_ALIGNMENT, 4);
370 glEnableClientState(GL_VERTEX_ARRAY);
371 glEnable(GL_SCISSOR_TEST);
372 glShadeModel(GL_FLAT);
373 glDisable(GL_DITHER);
374 glDisable(GL_CULL_FACE);
375
376 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
377 const uint16_t g1 = pack565(0x17,0x2f,0x17);
378 const uint16_t textureData[4] = { g0, g1, g1, g0 };
379 glGenTextures(1, &mWormholeTexName);
380 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
381 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
382 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
383 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
384 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
385 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
386 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
387
388 glViewport(0, 0, w, h);
389 glMatrixMode(GL_PROJECTION);
390 glLoadIdentity();
391 glOrthof(0, w, h, 0, 0, 1);
392
393 LayerDim::initDimmer(this, w, h);
394
395 mReadyToRunBarrier.open();
396
397 /*
398 * We're now ready to accept clients...
399 */
400
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700401 // start boot animation
402 property_set("ctl.start", "bootanim");
403
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 return NO_ERROR;
405}
406
407// ----------------------------------------------------------------------------
408#if 0
409#pragma mark -
410#pragma mark Events Handler
411#endif
412
413void SurfaceFlinger::waitForEvent()
414{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700415 while (true) {
416 nsecs_t timeout = -1;
417 if (UNLIKELY(isFrozen())) {
418 // wait 5 seconds
419 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
420 const nsecs_t now = systemTime();
421 if (mFreezeDisplayTime == 0) {
422 mFreezeDisplayTime = now;
423 }
424 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
425 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700426 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700427
Mathias Agopianb6683b52009-04-28 03:17:50 -0700428 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700429 if (msg != 0) {
430 mFreezeDisplayTime = 0;
431 switch (msg->what) {
432 case MessageQueue::INVALIDATE:
433 // invalidate message, just return to the main loop
434 return;
435 }
436 } else {
437 // we timed out
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 if (isFrozen()) {
439 // we timed out and are still frozen
440 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
441 mFreezeDisplay, mFreezeCount);
442 mFreezeCount = 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700443 mFreezeDisplay = false;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700444 return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 }
446 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 }
448}
449
450void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700451 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452}
453
454void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700455 // this is the IPC call
456 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457}
458
459void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
460{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700461 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462}
463
464// ----------------------------------------------------------------------------
465#if 0
466#pragma mark -
467#pragma mark Main loop
468#endif
469
470bool SurfaceFlinger::threadLoop()
471{
472 waitForEvent();
473
474 // check for transactions
475 if (UNLIKELY(mConsoleSignals)) {
476 handleConsoleEvents();
477 }
478
479 if (LIKELY(mTransactionCount == 0)) {
480 // if we're in a global transaction, don't do anything.
481 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
482 uint32_t transactionFlags = getTransactionFlags(mask);
483 if (LIKELY(transactionFlags)) {
484 handleTransaction(transactionFlags);
485 }
486 }
487
488 // post surfaces (if needed)
489 handlePageFlip();
490
491 const DisplayHardware& hw(graphicPlane(0).displayHardware());
492 if (LIKELY(hw.canDraw())) {
493 // repaint the framebuffer (if needed)
494 handleRepaint();
495
496 // release the clients before we flip ('cause flip might block)
497 unlockClients();
498 executeScheduledBroadcasts();
499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 postFramebuffer();
501 } else {
502 // pretend we did the post
503 unlockClients();
504 executeScheduledBroadcasts();
505 usleep(16667); // 60 fps period
506 }
507 return true;
508}
509
510void SurfaceFlinger::postFramebuffer()
511{
Mathias Agopian0926f502009-05-04 14:17:04 -0700512 if (isFrozen()) {
513 // we are not allowed to draw, but pause a bit to make sure
514 // apps don't end up using the whole CPU, if they depend on
515 // surfaceflinger for synchronization.
516 usleep(8333); // 8.3ms ~ 120fps
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 return;
518 }
519
520 if (!mInvalidRegion.isEmpty()) {
521 const DisplayHardware& hw(graphicPlane(0).displayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 }
525}
526
527void SurfaceFlinger::handleConsoleEvents()
528{
529 // something to do with the console
530 const DisplayHardware& hw = graphicPlane(0).displayHardware();
531
532 int what = android_atomic_and(0, &mConsoleSignals);
533 if (what & eConsoleAcquired) {
534 hw.acquireScreen();
535 }
536
537 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700538 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 hw.releaseScreen();
541 }
542
543 if (what & eConsoleReleased) {
544 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 hw.releaseScreen();
546 } else {
547 mDeferReleaseConsole = true;
548 }
549 }
550
551 mDirtyRegion.set(hw.bounds());
552}
553
554void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
555{
Mathias Agopian3d579642009-06-04 18:46:21 -0700556 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557
Mathias Agopian3d579642009-06-04 18:46:21 -0700558 { // scope for the lock
559 Mutex::Autolock _l(mStateLock);
560 handleTransactionLocked(transactionFlags, ditchedLayers);
561 }
562
563 // do this without lock held
564 const size_t count = ditchedLayers.size();
565 for (size_t i=0 ; i<count ; i++) {
566 //LOGD("ditching layer %p", ditchedLayers[i].get());
567 ditchedLayers[i]->ditch();
568 }
569}
570
571void SurfaceFlinger::handleTransactionLocked(
572 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
573{
574 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 const size_t count = currentLayers.size();
576
577 /*
578 * Traversal of the children
579 * (perform the transaction for each of them if needed)
580 */
581
582 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
583 if (layersNeedTransaction) {
584 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
587 if (!trFlags) continue;
588
589 const uint32_t flags = layer->doTransaction(0);
590 if (flags & Layer::eVisibleRegion)
591 mVisibleRegionsDirty = true;
592
593 if (flags & Layer::eRestartTransaction) {
594 // restart the transaction, but back-off a little
595 layer->setTransactionFlags(eTransactionNeeded);
596 setTransactionFlags(eTraversalNeeded, ms2ns(8));
597 }
598 }
599 }
600
601 /*
602 * Perform our own transaction if needed
603 */
604
605 if (transactionFlags & eTransactionNeeded) {
606 if (mCurrentState.orientation != mDrawingState.orientation) {
607 // the orientation has changed, recompute all visible regions
608 // and invalidate everything.
609
610 const int dpy = 0;
611 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700612 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 GraphicPlane& plane(graphicPlane(dpy));
614 plane.setOrientation(orientation);
615
616 // update the shared control block
617 const DisplayHardware& hw(plane.displayHardware());
618 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
619 dcblk->orientation = orientation;
620 if (orientation & eOrientationSwapMask) {
621 // 90 or 270 degrees orientation
622 dcblk->w = hw.getHeight();
623 dcblk->h = hw.getWidth();
624 } else {
625 dcblk->w = hw.getWidth();
626 dcblk->h = hw.getHeight();
627 }
628
629 mVisibleRegionsDirty = true;
630 mDirtyRegion.set(hw.bounds());
Mathias Agopianc08731e2009-03-27 18:11:38 -0700631 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 }
633
634 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
635 // freezing or unfreezing the display -> trigger animation if needed
636 mFreezeDisplay = mCurrentState.freezeDisplay;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 }
638
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700639 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
640 // layers have been added
641 mVisibleRegionsDirty = true;
642 }
643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 // some layers might have been removed, so
645 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700646 if (mLayersRemoved) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700648 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700649 const size_t count = previousLayers.size();
650 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700651 const sp<LayerBase>& layer(previousLayers[i]);
652 if (currentLayers.indexOf( layer ) < 0) {
653 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700654 ditchedLayers.add(layer);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700655 }
656 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 }
658
659 // get rid of all resources we don't need anymore
660 // (layers and clients)
661 free_resources_l();
662 }
663
664 commitTransaction();
665}
666
667sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
668{
669 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
670}
671
672void SurfaceFlinger::computeVisibleRegions(
673 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
674{
675 const GraphicPlane& plane(graphicPlane(0));
676 const Transform& planeTransform(plane.transform());
677
678 Region aboveOpaqueLayers;
679 Region aboveCoveredLayers;
680 Region dirty;
681
682 bool secureFrameBuffer = false;
683
684 size_t i = currentLayers.size();
685 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700686 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 layer->validateVisibility(planeTransform);
688
689 // start with the whole surface at its current location
690 const Layer::State& s = layer->drawingState();
691 const Rect bounds(layer->visibleBounds());
692
693 // handle hidden surfaces by setting the visible region to empty
694 Region opaqueRegion;
695 Region visibleRegion;
696 Region coveredRegion;
697 if (UNLIKELY((s.flags & ISurfaceComposer::eLayerHidden) || !s.alpha)) {
698 visibleRegion.clear();
699 } else {
700 const bool translucent = layer->needsBlending();
701 visibleRegion.set(bounds);
702 coveredRegion = visibleRegion;
703
704 // Remove the transparent area from the visible region
705 if (translucent) {
706 visibleRegion.subtractSelf(layer->transparentRegionScreen);
707 }
708
709 // compute the opaque region
710 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
711 // the opaque region is the visible region
712 opaqueRegion = visibleRegion;
713 }
714 }
715
716 // subtract the opaque region covered by the layers above us
717 visibleRegion.subtractSelf(aboveOpaqueLayers);
718 coveredRegion.andSelf(aboveCoveredLayers);
719
720 // compute this layer's dirty region
721 if (layer->contentDirty) {
722 // we need to invalidate the whole region
723 dirty = visibleRegion;
724 // as well, as the old visible region
725 dirty.orSelf(layer->visibleRegionScreen);
726 layer->contentDirty = false;
727 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700728 /* compute the exposed region:
729 * exposed = what's VISIBLE and NOT COVERED now
730 * but was COVERED before
731 */
732 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 }
734 dirty.subtractSelf(aboveOpaqueLayers);
735
736 // accumulate to the screen dirty region
737 dirtyRegion.orSelf(dirty);
738
Mathias Agopian62b74442009-04-14 23:02:51 -0700739 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700741 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742
743 // Store the visible region is screen space
744 layer->setVisibleRegion(visibleRegion);
745 layer->setCoveredRegion(coveredRegion);
746
Mathias Agopian62b74442009-04-14 23:02:51 -0700747 // If a secure layer is partially visible, lock down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748 if (layer->isSecure() && !visibleRegion.isEmpty()) {
749 secureFrameBuffer = true;
750 }
751 }
752
753 mSecureFrameBuffer = secureFrameBuffer;
754 opaqueRegion = aboveOpaqueLayers;
755}
756
757
758void SurfaceFlinger::commitTransaction()
759{
760 mDrawingState = mCurrentState;
761 mTransactionCV.signal();
762}
763
764void SurfaceFlinger::handlePageFlip()
765{
766 bool visibleRegions = mVisibleRegionsDirty;
767 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
768 visibleRegions |= lockPageFlip(currentLayers);
769
770 const DisplayHardware& hw = graphicPlane(0).displayHardware();
771 const Region screenRegion(hw.bounds());
772 if (visibleRegions) {
773 Region opaqueRegion;
774 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
775 mWormholeRegion = screenRegion.subtract(opaqueRegion);
776 mVisibleRegionsDirty = false;
777 }
778
779 unlockPageFlip(currentLayers);
780 mDirtyRegion.andSelf(screenRegion);
781}
782
783bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
784{
785 bool recomputeVisibleRegions = false;
786 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700787 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700789 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 layer->lockPageFlip(recomputeVisibleRegions);
791 }
792 return recomputeVisibleRegions;
793}
794
795void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
796{
797 const GraphicPlane& plane(graphicPlane(0));
798 const Transform& planeTransform(plane.transform());
799 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700800 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700802 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 layer->unlockPageFlip(planeTransform, mDirtyRegion);
804 }
805}
806
Mathias Agopianb8a55602009-06-26 19:06:36 -0700807
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808void SurfaceFlinger::handleRepaint()
809{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700810 // compute the invalid region
811 mInvalidRegion.orSelf(mDirtyRegion);
812 if (mInvalidRegion.isEmpty()) {
813 // nothing to do
814 return;
815 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816
817 if (UNLIKELY(mDebugRegion)) {
818 debugFlashRegions();
819 }
820
Mathias Agopianb8a55602009-06-26 19:06:36 -0700821 // set the frame buffer
822 const DisplayHardware& hw(graphicPlane(0).displayHardware());
823 glMatrixMode(GL_MODELVIEW);
824 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825
826 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700827 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
828 (flags & DisplayHardware::BUFFER_PRESERVED))
829 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700830 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
831 // takes a rectangle, we must make sure to update that whole
832 // rectangle in that case
833 if (flags & DisplayHardware::SWAP_RECTANGLE) {
834 // FIXME: we really should be able to pass a region to
835 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836 mDirtyRegion.set(mInvalidRegion.bounds());
837 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 // in the BUFFER_PRESERVED case, obviously, we can update only
839 // what's needed and nothing more.
840 // NOTE: this is NOT a common case, as preserving the backbuffer
841 // is costly and usually involves copying the whole update back.
842 }
843 } else {
844 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
845 // We need to redraw the rectangle that will be updated
846 // (pushed to the framebuffer).
847 // This is needed because UPDATE_ON_DEMAND only takes one
848 // rectangle instead of a region (see DisplayHardware::flip())
849 mDirtyRegion.set(mInvalidRegion.bounds());
850 } else {
851 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 mDirtyRegion.set(hw.bounds());
853 mInvalidRegion = mDirtyRegion;
854 }
855 }
856
857 // compose all surfaces
858 composeSurfaces(mDirtyRegion);
859
860 // clear the dirty regions
861 mDirtyRegion.clear();
862}
863
864void SurfaceFlinger::composeSurfaces(const Region& dirty)
865{
866 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
867 // should never happen unless the window manager has a bug
868 // draw something...
869 drawWormhole();
870 }
871 const SurfaceFlinger& flinger(*this);
872 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
873 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700874 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700876 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 const Region& visibleRegion(layer->visibleRegionScreen);
878 if (!visibleRegion.isEmpty()) {
879 const Region clip(dirty.intersect(visibleRegion));
880 if (!clip.isEmpty()) {
881 layer->draw(clip);
882 }
883 }
884 }
885}
886
887void SurfaceFlinger::unlockClients()
888{
889 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
890 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700891 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700893 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 layer->finishPageFlip();
895 }
896}
897
Mathias Agopianf9d93272009-06-19 17:00:27 -0700898void SurfaceFlinger::scheduleBroadcast(const sp<Client>& client)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899{
900 if (mLastScheduledBroadcast != client) {
901 mLastScheduledBroadcast = client;
902 mScheduledBroadcasts.add(client);
903 }
904}
905
906void SurfaceFlinger::executeScheduledBroadcasts()
907{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700908 SortedVector< wp<Client> >& list(mScheduledBroadcasts);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 size_t count = list.size();
910 while (count--) {
Mathias Agopianf9d93272009-06-19 17:00:27 -0700911 sp<Client> client = list[count].promote();
912 if (client != 0) {
913 per_client_cblk_t* const cblk = client->ctrlblk;
914 if (cblk->lock.tryLock() == NO_ERROR) {
915 cblk->cv.broadcast();
916 list.removeAt(count);
917 cblk->lock.unlock();
918 } else {
919 // schedule another round
920 LOGW("executeScheduledBroadcasts() skipped, "
921 "contention on the client. We'll try again later...");
922 signalDelayedEvent(ms2ns(4));
923 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 }
925 }
926 mLastScheduledBroadcast = 0;
927}
928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929void SurfaceFlinger::debugFlashRegions()
930{
Mathias Agopian0926f502009-05-04 14:17:04 -0700931 const DisplayHardware& hw(graphicPlane(0).displayHardware());
932 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700933
934 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
935 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700936 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
937 mDirtyRegion.bounds() : hw.bounds());
938 composeSurfaces(repaint);
939 }
940
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 glDisable(GL_TEXTURE_2D);
942 glDisable(GL_BLEND);
943 glDisable(GL_DITHER);
944 glDisable(GL_SCISSOR_TEST);
945
Mathias Agopian0926f502009-05-04 14:17:04 -0700946 static int toggle = 0;
947 toggle = 1 - toggle;
948 if (toggle) {
949 glColor4x(0x10000, 0, 0x10000, 0x10000);
950 } else {
951 glColor4x(0x10000, 0x10000, 0, 0x10000);
952 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
Mathias Agopian20f68782009-05-11 00:03:41 -0700954 Region::const_iterator it = mDirtyRegion.begin();
955 Region::const_iterator const end = mDirtyRegion.end();
956 while (it != end) {
957 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 GLfloat vertices[][2] = {
959 { r.left, r.top },
960 { r.left, r.bottom },
961 { r.right, r.bottom },
962 { r.right, r.top }
963 };
964 glVertexPointer(2, GL_FLOAT, 0, vertices);
965 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
966 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700967
Mathias Agopianb8a55602009-06-26 19:06:36 -0700968 if (mInvalidRegion.isEmpty()) {
969 mDirtyRegion.dump("mDirtyRegion");
970 mInvalidRegion.dump("mInvalidRegion");
971 }
972 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973
974 if (mDebugRegion > 1)
975 usleep(mDebugRegion * 1000);
976
977 glEnable(GL_SCISSOR_TEST);
978 //mDirtyRegion.dump("mDirtyRegion");
979}
980
981void SurfaceFlinger::drawWormhole() const
982{
983 const Region region(mWormholeRegion.intersect(mDirtyRegion));
984 if (region.isEmpty())
985 return;
986
987 const DisplayHardware& hw(graphicPlane(0).displayHardware());
988 const int32_t width = hw.getWidth();
989 const int32_t height = hw.getHeight();
990
991 glDisable(GL_BLEND);
992 glDisable(GL_DITHER);
993
994 if (LIKELY(!mDebugBackground)) {
995 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700996 Region::const_iterator it = region.begin();
997 Region::const_iterator const end = region.end();
998 while (it != end) {
999 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 const GLint sy = height - (r.top + r.height());
1001 glScissor(r.left, sy, r.width(), r.height());
1002 glClear(GL_COLOR_BUFFER_BIT);
1003 }
1004 } else {
1005 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1006 { width, height }, { 0, height } };
1007 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1008 glVertexPointer(2, GL_SHORT, 0, vertices);
1009 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1010 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1011 glEnable(GL_TEXTURE_2D);
1012 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1013 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1014 glMatrixMode(GL_TEXTURE);
1015 glLoadIdentity();
1016 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001017 Region::const_iterator it = region.begin();
1018 Region::const_iterator const end = region.end();
1019 while (it != end) {
1020 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 const GLint sy = height - (r.top + r.height());
1022 glScissor(r.left, sy, r.width(), r.height());
1023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1024 }
1025 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1026 }
1027}
1028
1029void SurfaceFlinger::debugShowFPS() const
1030{
1031 static int mFrameCount;
1032 static int mLastFrameCount = 0;
1033 static nsecs_t mLastFpsTime = 0;
1034 static float mFps = 0;
1035 mFrameCount++;
1036 nsecs_t now = systemTime();
1037 nsecs_t diff = now - mLastFpsTime;
1038 if (diff > ms2ns(250)) {
1039 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1040 mLastFpsTime = now;
1041 mLastFrameCount = mFrameCount;
1042 }
1043 // XXX: mFPS has the value we want
1044 }
1045
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001046status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047{
1048 Mutex::Autolock _l(mStateLock);
1049 addLayer_l(layer);
1050 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1051 return NO_ERROR;
1052}
1053
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001054status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055{
1056 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001057 status_t err = purgatorizeLayer_l(layer);
1058 if (err == NO_ERROR)
1059 setTransactionFlags(eTransactionNeeded);
1060 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061}
1062
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001063status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064{
1065 layer->forceVisibilityTransaction();
1066 setTransactionFlags(eTraversalNeeded);
1067 return NO_ERROR;
1068}
1069
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001070status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071{
1072 ssize_t i = mCurrentState.layersSortedByZ.add(
1073 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1075 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 mLayerMap.add(lbc->serverIndex(), lbc);
1077 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 return NO_ERROR;
1079}
1080
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001081status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082{
1083 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1084 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001085 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001086 sp<LayerBaseClient> layer =
1087 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001088 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 mLayerMap.removeItem(layer->serverIndex());
1090 }
1091 return NO_ERROR;
1092 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001093 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094}
1095
Mathias Agopian9a112062009-04-17 19:36:26 -07001096status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1097{
1098 // First add the layer to the purgatory list, which makes sure it won't
1099 // go away, then remove it from the main list (through a transaction).
1100 ssize_t err = removeLayer_l(layerBase);
1101 if (err >= 0) {
1102 mLayerPurgatory.add(layerBase);
1103 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001104 // it's possible that we don't find a layer, because it might
1105 // have been destroyed already -- this is not technically an error
1106 // from the user because there is a race between BClient::destroySurface(),
1107 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001108 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1109}
1110
1111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112void SurfaceFlinger::free_resources_l()
1113{
1114 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001115 mLayersRemoved = false;
1116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001118 SortedVector< wp<Client> >& scheduledBroadcasts(mScheduledBroadcasts);
1119 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 const size_t count = disconnectedClients.size();
1121 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001122 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 // if this client is the scheduled broadcast list,
1124 // remove it from there (and we don't need to signal it
1125 // since it is dead).
1126 int32_t index = scheduledBroadcasts.indexOf(client);
1127 if (index >= 0) {
1128 scheduledBroadcasts.removeItemsAt(index);
1129 }
1130 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131 }
1132 disconnectedClients.clear();
1133}
1134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1136{
1137 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1138}
1139
1140uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1141{
1142 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1143 if ((old & flags)==0) { // wake the server up
1144 if (delay > 0) {
1145 signalDelayedEvent(delay);
1146 } else {
1147 signalEvent();
1148 }
1149 }
1150 return old;
1151}
1152
1153void SurfaceFlinger::openGlobalTransaction()
1154{
1155 android_atomic_inc(&mTransactionCount);
1156}
1157
1158void SurfaceFlinger::closeGlobalTransaction()
1159{
1160 if (android_atomic_dec(&mTransactionCount) == 1) {
1161 signalEvent();
1162 }
1163}
1164
1165status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1166{
1167 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1168 return BAD_VALUE;
1169
1170 Mutex::Autolock _l(mStateLock);
1171 mCurrentState.freezeDisplay = 1;
1172 setTransactionFlags(eTransactionNeeded);
1173
1174 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001175 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 return NO_ERROR;
1177}
1178
1179status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1180{
1181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1182 return BAD_VALUE;
1183
1184 Mutex::Autolock _l(mStateLock);
1185 mCurrentState.freezeDisplay = 0;
1186 setTransactionFlags(eTransactionNeeded);
1187
1188 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001189 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 return NO_ERROR;
1191}
1192
Mathias Agopianc08731e2009-03-27 18:11:38 -07001193int SurfaceFlinger::setOrientation(DisplayID dpy,
1194 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195{
1196 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1197 return BAD_VALUE;
1198
1199 Mutex::Autolock _l(mStateLock);
1200 if (mCurrentState.orientation != orientation) {
1201 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001202 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 mCurrentState.orientation = orientation;
1204 setTransactionFlags(eTransactionNeeded);
1205 mTransactionCV.wait(mStateLock);
1206 } else {
1207 orientation = BAD_VALUE;
1208 }
1209 }
1210 return orientation;
1211}
1212
1213sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1214 ISurfaceFlingerClient::surface_data_t* params,
1215 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1216 uint32_t flags)
1217{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001218 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 sp<LayerBaseClient::Surface> surfaceHandle;
1220 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001221 sp<Client> client = mClientsMap.valueFor(clientId);
1222 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1224 return surfaceHandle;
1225 }
1226
1227 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001228 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1230 LOGE("createSurface() failed, generateId = %d", id);
1231 return surfaceHandle;
1232 }
1233
1234 switch (flags & eFXSurfaceMask) {
1235 case eFXSurfaceNormal:
1236 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001237 layer = createPushBuffersSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 } else {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001239 layer = createNormalSurfaceLocked(client, d, id, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 }
1241 break;
1242 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001243 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 break;
1245 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001246 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 break;
1248 }
1249
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001250 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 setTransactionFlags(eTransactionNeeded);
1252 surfaceHandle = layer->getSurface();
1253 if (surfaceHandle != 0)
1254 surfaceHandle->getSurfaceData(params);
1255 }
1256
1257 return surfaceHandle;
1258}
1259
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001260sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001261 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
1263{
1264 // initialize the surfaces
1265 switch (format) { // TODO: take h/w into account
1266 case PIXEL_FORMAT_TRANSPARENT:
1267 case PIXEL_FORMAT_TRANSLUCENT:
1268 format = PIXEL_FORMAT_RGBA_8888;
1269 break;
1270 case PIXEL_FORMAT_OPAQUE:
1271 format = PIXEL_FORMAT_RGB_565;
1272 break;
1273 }
1274
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001275 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001276 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 if (LIKELY(err == NO_ERROR)) {
1278 layer->initStates(w, h, flags);
1279 addLayer_l(layer);
1280 } else {
1281 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001282 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 }
1284 return layer;
1285}
1286
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001288 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1290{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001291 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 layer->initStates(w, h, flags);
1293 addLayer_l(layer);
1294 return layer;
1295}
1296
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001297sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001298 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1300{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001301 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 layer->initStates(w, h, flags);
1303 addLayer_l(layer);
1304 return layer;
1305}
1306
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001307sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001308 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1310{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001311 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 layer->initStates(w, h, flags);
1313 addLayer_l(layer);
1314 return layer;
1315}
1316
Mathias Agopian9a112062009-04-17 19:36:26 -07001317status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
Mathias Agopian9a112062009-04-17 19:36:26 -07001319 /*
1320 * called by the window manager, when a surface should be marked for
1321 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001322 *
1323 * The surface is removed from the current and drawing lists, but placed
1324 * in the purgatory queue, so it's not destroyed right-away (we need
1325 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001326 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001327
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001328 Mutex::Autolock _l(mStateLock);
1329 sp<LayerBaseClient> layer = getLayerUser_l(index);
1330 status_t err = purgatorizeLayer_l(layer);
1331 if (err == NO_ERROR) {
1332 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001333 }
1334 return err;
1335}
1336
1337status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1338{
Mathias Agopian3d579642009-06-04 18:46:21 -07001339 /* called by ~ISurface() when all references are gone */
Mathias Agopian9a112062009-04-17 19:36:26 -07001340
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001341 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001342 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 sp<LayerBaseClient> layer;
1344 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001345 MessageDestroySurface(
1346 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1347 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001348 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001349 sp<LayerBaseClient> l(layer);
1350 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001351 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001352 // remove the layer from the current list -- chances are that it's
1353 // not in the list anyway, because it should have been removed
1354 // already upon request of the client (eg: window manager).
1355 // However, a buggy client could have not done that.
1356 // Since we know we don't have any more clients, we don't need
1357 // to use the purgatory.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001358 status_t err = flinger->removeLayer_l(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001359 if (err == NAME_NOT_FOUND) {
1360 // The surface wasn't in the current list, which means it was
1361 // removed already, which means it is in the purgatory,
1362 // and need to be removed from there.
1363 // This needs to happen from the main thread since its dtor
1364 // must run from there (b/c of OpenGL ES). Additionally, we
1365 // can't really acquire our internal lock from
1366 // destroySurface() -- see postMessage() below.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001367 ssize_t idx = flinger->mLayerPurgatory.remove(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001368 LOGE_IF(idx < 0,
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001369 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian3d579642009-06-04 18:46:21 -07001370 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001371 return true;
1372 }
1373 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001374
1375 // It's better to not acquire our internal lock here, because it's hard
1376 // to predict that it's not going to be already taken when ~Surface()
1377 // is called.
1378
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001379 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 return NO_ERROR;
1381}
1382
1383status_t SurfaceFlinger::setClientState(
1384 ClientID cid,
1385 int32_t count,
1386 const layer_state_t* states)
1387{
1388 Mutex::Autolock _l(mStateLock);
1389 uint32_t flags = 0;
1390 cid <<= 16;
1391 for (int i=0 ; i<count ; i++) {
1392 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001393 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001394 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 if (what & ePositionChanged) {
1397 if (layer->setPosition(s.x, s.y))
1398 flags |= eTraversalNeeded;
1399 }
1400 if (what & eLayerChanged) {
1401 if (layer->setLayer(s.z)) {
1402 mCurrentState.layersSortedByZ.reorder(
1403 layer, &Layer::compareCurrentStateZ);
1404 // we need traversal (state changed)
1405 // AND transaction (list changed)
1406 flags |= eTransactionNeeded|eTraversalNeeded;
1407 }
1408 }
1409 if (what & eSizeChanged) {
1410 if (layer->setSize(s.w, s.h))
1411 flags |= eTraversalNeeded;
1412 }
1413 if (what & eAlphaChanged) {
1414 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1415 flags |= eTraversalNeeded;
1416 }
1417 if (what & eMatrixChanged) {
1418 if (layer->setMatrix(s.matrix))
1419 flags |= eTraversalNeeded;
1420 }
1421 if (what & eTransparentRegionChanged) {
1422 if (layer->setTransparentRegionHint(s.transparentRegion))
1423 flags |= eTraversalNeeded;
1424 }
1425 if (what & eVisibilityChanged) {
1426 if (layer->setFlags(s.flags, s.mask))
1427 flags |= eTraversalNeeded;
1428 }
1429 }
1430 }
1431 if (flags) {
1432 setTransactionFlags(flags);
1433 }
1434 return NO_ERROR;
1435}
1436
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001437sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001439 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1440 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441}
1442
1443void SurfaceFlinger::screenReleased(int dpy)
1444{
1445 // this may be called by a signal handler, we can't do too much in here
1446 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1447 signalEvent();
1448}
1449
1450void SurfaceFlinger::screenAcquired(int dpy)
1451{
1452 // this may be called by a signal handler, we can't do too much in here
1453 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1454 signalEvent();
1455}
1456
1457status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1458{
1459 const size_t SIZE = 1024;
1460 char buffer[SIZE];
1461 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001462 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 snprintf(buffer, SIZE, "Permission Denial: "
1464 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1465 IPCThreadState::self()->getCallingPid(),
1466 IPCThreadState::self()->getCallingUid());
1467 result.append(buffer);
1468 } else {
1469 Mutex::Autolock _l(mStateLock);
1470 size_t s = mClientsMap.size();
1471 char name[64];
1472 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001473 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 sprintf(name, " Client (id=0x%08x)", client->cid);
1475 client->dump(name);
1476 }
1477 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1478 const size_t count = currentLayers.size();
1479 for (size_t i=0 ; i<count ; i++) {
1480 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001481 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 const Layer::State& s = layer->drawingState();
1483 snprintf(buffer, SIZE,
1484 "+ %s %p\n"
1485 " "
1486 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1487 "needsBlending=%1d, invalidate=%1d, "
1488 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001489 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 s.z, layer->tx(), layer->ty(), s.w, s.h,
1491 layer->needsBlending(), layer->contentDirty,
1492 s.alpha, s.flags,
1493 s.transform[0], s.transform[1],
1494 s.transform[2], s.transform[3]);
1495 result.append(buffer);
1496 buffer[0] = 0;
1497 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001498 sp<LayerBaseClient> lbc =
1499 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1500 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001501 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 snprintf(buffer, SIZE,
1503 " "
1504 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001505 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 lbc->getIdentity());
1507 }
1508 result.append(buffer);
1509 buffer[0] = 0;
1510 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001511 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1512 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 const LayerBitmap& buf0(l->getBuffer(0));
1514 const LayerBitmap& buf1(l->getBuffer(1));
1515 snprintf(buffer, SIZE,
1516 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001517 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 " freezeLock=%p, swapState=0x%08x\n",
1519 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001520 buf0.getWidth(), buf0.getHeight(),
1521 buf0.getBuffer()->getStride(),
1522 buf1.getWidth(), buf1.getHeight(),
1523 buf1.getBuffer()->getStride(),
1524 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 l->lcblk->swapState);
1526 }
1527 result.append(buffer);
1528 buffer[0] = 0;
1529 s.transparentRegion.dump(result, "transparentRegion");
1530 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1531 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1532 }
1533 mWormholeRegion.dump(result, "WormholeRegion");
1534 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1535 snprintf(buffer, SIZE,
1536 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1537 mFreezeDisplay?"yes":"no", mFreezeCount,
1538 mCurrentState.orientation, hw.canDraw());
1539 result.append(buffer);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001540 snprintf(buffer, SIZE, " purgatory size: %d, client count: %d\n",
1541 mLayerPurgatory.size(), mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001542 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001543 const BufferAllocator& alloc(BufferAllocator::get());
1544 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 }
1546 write(fd, result.string(), result.size());
1547 return NO_ERROR;
1548}
1549
1550status_t SurfaceFlinger::onTransact(
1551 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1552{
1553 switch (code) {
1554 case CREATE_CONNECTION:
1555 case OPEN_GLOBAL_TRANSACTION:
1556 case CLOSE_GLOBAL_TRANSACTION:
1557 case SET_ORIENTATION:
1558 case FREEZE_DISPLAY:
1559 case UNFREEZE_DISPLAY:
1560 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 {
1562 // codes that require permission check
1563 IPCThreadState* ipc = IPCThreadState::self();
1564 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001565 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001566 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1567 LOGE("Permission Denial: "
1568 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1569 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 }
1571 }
1572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1574 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001575 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001576 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1577 IPCThreadState* ipc = IPCThreadState::self();
1578 const int pid = ipc->getCallingPid();
1579 const int uid = ipc->getCallingUid();
1580 LOGE("Permission Denial: "
1581 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 return PERMISSION_DENIED;
1583 }
1584 int n;
1585 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001586 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001588 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 return NO_ERROR;
1590 case 1002: // SHOW_UPDATES
1591 n = data.readInt32();
1592 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1593 return NO_ERROR;
1594 case 1003: // SHOW_BACKGROUND
1595 n = data.readInt32();
1596 mDebugBackground = n ? 1 : 0;
1597 return NO_ERROR;
1598 case 1004:{ // repaint everything
1599 Mutex::Autolock _l(mStateLock);
1600 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1601 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1602 signalEvent();
1603 }
1604 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 case 1007: // set mFreezeCount
1606 mFreezeCount = data.readInt32();
1607 return NO_ERROR;
1608 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001609 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 reply->writeInt32(0);
1611 reply->writeInt32(mDebugRegion);
1612 reply->writeInt32(mDebugBackground);
1613 return NO_ERROR;
1614 case 1013: {
1615 Mutex::Autolock _l(mStateLock);
1616 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1617 reply->writeInt32(hw.getPageFlipCount());
1618 }
1619 return NO_ERROR;
1620 }
1621 }
1622 return err;
1623}
1624
1625// ---------------------------------------------------------------------------
1626#if 0
1627#pragma mark -
1628#endif
1629
1630Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1631 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1632{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 const int pgsize = getpagesize();
1634 const int cblksize=((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1635 mCblkHeap = new MemoryDealer(cblksize);
1636 mCblkMemory = mCblkHeap->allocate(cblksize);
1637 if (mCblkMemory != 0) {
1638 ctrlblk = static_cast<per_client_cblk_t *>(mCblkMemory->pointer());
1639 if (ctrlblk) { // construct the shared structure in-place.
1640 new(ctrlblk) per_client_cblk_t;
1641 }
1642 }
1643}
1644
1645Client::~Client() {
1646 if (ctrlblk) {
1647 const int pgsize = getpagesize();
1648 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1649 }
1650}
1651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652int32_t Client::generateId(int pid)
1653{
1654 const uint32_t i = clz( ~mBitmap );
1655 if (i >= NUM_LAYERS_MAX) {
1656 return NO_MEMORY;
1657 }
1658 mPid = pid;
1659 mInUse.add(uint8_t(i));
1660 mBitmap |= 1<<(31-i);
1661 return i;
1662}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001663
1664status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665{
1666 ssize_t idx = mInUse.indexOf(id);
1667 if (idx < 0)
1668 return NAME_NOT_FOUND;
1669 return mLayers.insertAt(layer, idx);
1670}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672void Client::free(int32_t id)
1673{
1674 ssize_t idx = mInUse.remove(uint8_t(id));
1675 if (idx >= 0) {
1676 mBitmap &= ~(1<<(31-id));
1677 mLayers.removeItemsAt(idx);
1678 }
1679}
1680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681bool Client::isValid(int32_t i) const {
1682 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1683}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001684
1685sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1686 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001688 if (idx >= 0) {
1689 lbc = mLayers[idx].promote();
1690 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1691 }
1692 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693}
1694
1695void Client::dump(const char* what)
1696{
1697}
1698
1699// ---------------------------------------------------------------------------
1700#if 0
1701#pragma mark -
1702#endif
1703
1704BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemory>& cblk)
1705 : mId(cid), mFlinger(flinger), mCblk(cblk)
1706{
1707}
1708
1709BClient::~BClient() {
1710 // destroy all resources attached to this client
1711 mFlinger->destroyConnection(mId);
1712}
1713
1714void BClient::getControlBlocks(sp<IMemory>* ctrl) const {
1715 *ctrl = mCblk;
1716}
1717
1718sp<ISurface> BClient::createSurface(
1719 ISurfaceFlingerClient::surface_data_t* params, int pid,
1720 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1721 uint32_t flags)
1722{
1723 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1724}
1725
1726status_t BClient::destroySurface(SurfaceID sid)
1727{
1728 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001729 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730}
1731
1732status_t BClient::setState(int32_t count, const layer_state_t* states)
1733{
1734 return mFlinger->setClientState(mId, count, states);
1735}
1736
1737// ---------------------------------------------------------------------------
1738
1739GraphicPlane::GraphicPlane()
1740 : mHw(0)
1741{
1742}
1743
1744GraphicPlane::~GraphicPlane() {
1745 delete mHw;
1746}
1747
1748bool GraphicPlane::initialized() const {
1749 return mHw ? true : false;
1750}
1751
1752void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1753 mHw = hw;
1754}
1755
1756void GraphicPlane::setTransform(const Transform& tr) {
1757 mTransform = tr;
1758 mGlobalTransform = mOrientationTransform * mTransform;
1759}
1760
1761status_t GraphicPlane::orientationToTransfrom(
1762 int orientation, int w, int h, Transform* tr)
1763{
1764 float a, b, c, d, x, y;
1765 switch (orientation) {
1766 case ISurfaceComposer::eOrientationDefault:
1767 a=1; b=0; c=0; d=1; x=0; y=0;
1768 break;
1769 case ISurfaceComposer::eOrientation90:
1770 a=0; b=-1; c=1; d=0; x=w; y=0;
1771 break;
1772 case ISurfaceComposer::eOrientation180:
1773 a=-1; b=0; c=0; d=-1; x=w; y=h;
1774 break;
1775 case ISurfaceComposer::eOrientation270:
1776 a=0; b=1; c=-1; d=0; x=0; y=h;
1777 break;
1778 default:
1779 return BAD_VALUE;
1780 }
1781 tr->set(a, b, c, d);
1782 tr->set(x, y);
1783 return NO_ERROR;
1784}
1785
1786status_t GraphicPlane::setOrientation(int orientation)
1787{
1788 const DisplayHardware& hw(displayHardware());
1789 const float w = hw.getWidth();
1790 const float h = hw.getHeight();
1791
1792 if (orientation == ISurfaceComposer::eOrientationDefault) {
1793 // make sure the default orientation is optimal
1794 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001795 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796 mGlobalTransform = mTransform;
1797 return NO_ERROR;
1798 }
1799
1800 // If the rotation can be handled in hardware, this is where
1801 // the magic should happen.
1802 if (UNLIKELY(orientation == 42)) {
1803 float a, b, c, d, x, y;
1804 const float r = (3.14159265f / 180.0f) * 42.0f;
1805 const float si = sinf(r);
1806 const float co = cosf(r);
1807 a=co; b=-si; c=si; d=co;
1808 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1809 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1810 mOrientationTransform.set(a, b, c, d);
1811 mOrientationTransform.set(x, y);
1812 } else {
1813 GraphicPlane::orientationToTransfrom(orientation, w, h,
1814 &mOrientationTransform);
1815 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001816 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001817 mGlobalTransform = mOrientationTransform * mTransform;
1818 return NO_ERROR;
1819}
1820
1821const DisplayHardware& GraphicPlane::displayHardware() const {
1822 return *mHw;
1823}
1824
1825const Transform& GraphicPlane::transform() const {
1826 return mGlobalTransform;
1827}
1828
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001829EGLDisplay GraphicPlane::getEGLDisplay() const {
1830 return mHw->getEGLDisplay();
1831}
1832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833// ---------------------------------------------------------------------------
1834
1835}; // namespace android