blob: 17cff553f963568fb2d05fb22165143a7074f324 [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 {
830 // we can redraw only what's dirty
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 } else {
832 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700833 // we need to redraw the rectangle that will be updated
834 // (pushed to the framebuffer).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835 mDirtyRegion.set(mInvalidRegion.bounds());
836 } else {
837 // we need to redraw everything
838 mDirtyRegion.set(hw.bounds());
839 mInvalidRegion = mDirtyRegion;
840 }
841 }
842
843 // compose all surfaces
844 composeSurfaces(mDirtyRegion);
845
846 // clear the dirty regions
847 mDirtyRegion.clear();
848}
849
850void SurfaceFlinger::composeSurfaces(const Region& dirty)
851{
852 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
853 // should never happen unless the window manager has a bug
854 // draw something...
855 drawWormhole();
856 }
857 const SurfaceFlinger& flinger(*this);
858 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
859 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700860 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700862 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 const Region& visibleRegion(layer->visibleRegionScreen);
864 if (!visibleRegion.isEmpty()) {
865 const Region clip(dirty.intersect(visibleRegion));
866 if (!clip.isEmpty()) {
867 layer->draw(clip);
868 }
869 }
870 }
871}
872
873void SurfaceFlinger::unlockClients()
874{
875 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
876 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700877 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700879 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 layer->finishPageFlip();
881 }
882}
883
Mathias Agopianf9d93272009-06-19 17:00:27 -0700884void SurfaceFlinger::scheduleBroadcast(const sp<Client>& client)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885{
886 if (mLastScheduledBroadcast != client) {
887 mLastScheduledBroadcast = client;
888 mScheduledBroadcasts.add(client);
889 }
890}
891
892void SurfaceFlinger::executeScheduledBroadcasts()
893{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700894 SortedVector< wp<Client> >& list(mScheduledBroadcasts);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 size_t count = list.size();
896 while (count--) {
Mathias Agopianf9d93272009-06-19 17:00:27 -0700897 sp<Client> client = list[count].promote();
898 if (client != 0) {
899 per_client_cblk_t* const cblk = client->ctrlblk;
900 if (cblk->lock.tryLock() == NO_ERROR) {
901 cblk->cv.broadcast();
902 list.removeAt(count);
903 cblk->lock.unlock();
904 } else {
905 // schedule another round
906 LOGW("executeScheduledBroadcasts() skipped, "
907 "contention on the client. We'll try again later...");
908 signalDelayedEvent(ms2ns(4));
909 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 }
911 }
912 mLastScheduledBroadcast = 0;
913}
914
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915void SurfaceFlinger::debugFlashRegions()
916{
Mathias Agopian0926f502009-05-04 14:17:04 -0700917 const DisplayHardware& hw(graphicPlane(0).displayHardware());
918 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700919
920 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
921 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700922 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
923 mDirtyRegion.bounds() : hw.bounds());
924 composeSurfaces(repaint);
925 }
926
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 glDisable(GL_TEXTURE_2D);
928 glDisable(GL_BLEND);
929 glDisable(GL_DITHER);
930 glDisable(GL_SCISSOR_TEST);
931
Mathias Agopian0926f502009-05-04 14:17:04 -0700932 static int toggle = 0;
933 toggle = 1 - toggle;
934 if (toggle) {
935 glColor4x(0x10000, 0, 0x10000, 0x10000);
936 } else {
937 glColor4x(0x10000, 0x10000, 0, 0x10000);
938 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939
Mathias Agopian20f68782009-05-11 00:03:41 -0700940 Region::const_iterator it = mDirtyRegion.begin();
941 Region::const_iterator const end = mDirtyRegion.end();
942 while (it != end) {
943 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 GLfloat vertices[][2] = {
945 { r.left, r.top },
946 { r.left, r.bottom },
947 { r.right, r.bottom },
948 { r.right, r.top }
949 };
950 glVertexPointer(2, GL_FLOAT, 0, vertices);
951 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
952 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700953
Mathias Agopianb8a55602009-06-26 19:06:36 -0700954 if (mInvalidRegion.isEmpty()) {
955 mDirtyRegion.dump("mDirtyRegion");
956 mInvalidRegion.dump("mInvalidRegion");
957 }
958 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959
960 if (mDebugRegion > 1)
961 usleep(mDebugRegion * 1000);
962
963 glEnable(GL_SCISSOR_TEST);
964 //mDirtyRegion.dump("mDirtyRegion");
965}
966
967void SurfaceFlinger::drawWormhole() const
968{
969 const Region region(mWormholeRegion.intersect(mDirtyRegion));
970 if (region.isEmpty())
971 return;
972
973 const DisplayHardware& hw(graphicPlane(0).displayHardware());
974 const int32_t width = hw.getWidth();
975 const int32_t height = hw.getHeight();
976
977 glDisable(GL_BLEND);
978 glDisable(GL_DITHER);
979
980 if (LIKELY(!mDebugBackground)) {
981 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700982 Region::const_iterator it = region.begin();
983 Region::const_iterator const end = region.end();
984 while (it != end) {
985 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986 const GLint sy = height - (r.top + r.height());
987 glScissor(r.left, sy, r.width(), r.height());
988 glClear(GL_COLOR_BUFFER_BIT);
989 }
990 } else {
991 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
992 { width, height }, { 0, height } };
993 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
994 glVertexPointer(2, GL_SHORT, 0, vertices);
995 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
996 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
997 glEnable(GL_TEXTURE_2D);
998 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
999 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1000 glMatrixMode(GL_TEXTURE);
1001 glLoadIdentity();
1002 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001003 Region::const_iterator it = region.begin();
1004 Region::const_iterator const end = region.end();
1005 while (it != end) {
1006 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 const GLint sy = height - (r.top + r.height());
1008 glScissor(r.left, sy, r.width(), r.height());
1009 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1010 }
1011 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1012 }
1013}
1014
1015void SurfaceFlinger::debugShowFPS() const
1016{
1017 static int mFrameCount;
1018 static int mLastFrameCount = 0;
1019 static nsecs_t mLastFpsTime = 0;
1020 static float mFps = 0;
1021 mFrameCount++;
1022 nsecs_t now = systemTime();
1023 nsecs_t diff = now - mLastFpsTime;
1024 if (diff > ms2ns(250)) {
1025 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1026 mLastFpsTime = now;
1027 mLastFrameCount = mFrameCount;
1028 }
1029 // XXX: mFPS has the value we want
1030 }
1031
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001032status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033{
1034 Mutex::Autolock _l(mStateLock);
1035 addLayer_l(layer);
1036 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1037 return NO_ERROR;
1038}
1039
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001040status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041{
1042 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001043 status_t err = purgatorizeLayer_l(layer);
1044 if (err == NO_ERROR)
1045 setTransactionFlags(eTransactionNeeded);
1046 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047}
1048
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001049status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050{
1051 layer->forceVisibilityTransaction();
1052 setTransactionFlags(eTraversalNeeded);
1053 return NO_ERROR;
1054}
1055
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001056status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057{
1058 ssize_t i = mCurrentState.layersSortedByZ.add(
1059 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001060 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1061 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 mLayerMap.add(lbc->serverIndex(), lbc);
1063 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 return NO_ERROR;
1065}
1066
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001067status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068{
1069 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1070 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001072 sp<LayerBaseClient> layer =
1073 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 mLayerMap.removeItem(layer->serverIndex());
1076 }
1077 return NO_ERROR;
1078 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001079 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080}
1081
Mathias Agopian9a112062009-04-17 19:36:26 -07001082status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1083{
1084 // First add the layer to the purgatory list, which makes sure it won't
1085 // go away, then remove it from the main list (through a transaction).
1086 ssize_t err = removeLayer_l(layerBase);
1087 if (err >= 0) {
1088 mLayerPurgatory.add(layerBase);
1089 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001090 // it's possible that we don't find a layer, because it might
1091 // have been destroyed already -- this is not technically an error
1092 // from the user because there is a race between BClient::destroySurface(),
1093 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001094 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1095}
1096
1097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098void SurfaceFlinger::free_resources_l()
1099{
1100 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001101 mLayersRemoved = false;
1102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001104 SortedVector< wp<Client> >& scheduledBroadcasts(mScheduledBroadcasts);
1105 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 const size_t count = disconnectedClients.size();
1107 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001108 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 // if this client is the scheduled broadcast list,
1110 // remove it from there (and we don't need to signal it
1111 // since it is dead).
1112 int32_t index = scheduledBroadcasts.indexOf(client);
1113 if (index >= 0) {
1114 scheduledBroadcasts.removeItemsAt(index);
1115 }
1116 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 }
1118 disconnectedClients.clear();
1119}
1120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1122{
1123 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1124}
1125
1126uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1127{
1128 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1129 if ((old & flags)==0) { // wake the server up
1130 if (delay > 0) {
1131 signalDelayedEvent(delay);
1132 } else {
1133 signalEvent();
1134 }
1135 }
1136 return old;
1137}
1138
1139void SurfaceFlinger::openGlobalTransaction()
1140{
1141 android_atomic_inc(&mTransactionCount);
1142}
1143
1144void SurfaceFlinger::closeGlobalTransaction()
1145{
1146 if (android_atomic_dec(&mTransactionCount) == 1) {
1147 signalEvent();
1148 }
1149}
1150
1151status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1152{
1153 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1154 return BAD_VALUE;
1155
1156 Mutex::Autolock _l(mStateLock);
1157 mCurrentState.freezeDisplay = 1;
1158 setTransactionFlags(eTransactionNeeded);
1159
1160 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001161 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 return NO_ERROR;
1163}
1164
1165status_t SurfaceFlinger::unfreezeDisplay(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 = 0;
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
Mathias Agopianc08731e2009-03-27 18:11:38 -07001179int SurfaceFlinger::setOrientation(DisplayID dpy,
1180 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181{
1182 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1183 return BAD_VALUE;
1184
1185 Mutex::Autolock _l(mStateLock);
1186 if (mCurrentState.orientation != orientation) {
1187 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001188 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 mCurrentState.orientation = orientation;
1190 setTransactionFlags(eTransactionNeeded);
1191 mTransactionCV.wait(mStateLock);
1192 } else {
1193 orientation = BAD_VALUE;
1194 }
1195 }
1196 return orientation;
1197}
1198
1199sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1200 ISurfaceFlingerClient::surface_data_t* params,
1201 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1202 uint32_t flags)
1203{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001204 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 sp<LayerBaseClient::Surface> surfaceHandle;
1206 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001207 sp<Client> client = mClientsMap.valueFor(clientId);
1208 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1210 return surfaceHandle;
1211 }
1212
1213 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001214 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1216 LOGE("createSurface() failed, generateId = %d", id);
1217 return surfaceHandle;
1218 }
1219
1220 switch (flags & eFXSurfaceMask) {
1221 case eFXSurfaceNormal:
1222 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001223 layer = createPushBuffersSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 } else {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001225 layer = createNormalSurfaceLocked(client, d, id, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 }
1227 break;
1228 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001229 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230 break;
1231 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001232 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 break;
1234 }
1235
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001236 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 setTransactionFlags(eTransactionNeeded);
1238 surfaceHandle = layer->getSurface();
1239 if (surfaceHandle != 0)
1240 surfaceHandle->getSurfaceData(params);
1241 }
1242
1243 return surfaceHandle;
1244}
1245
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001246sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001247 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
1249{
1250 // initialize the surfaces
1251 switch (format) { // TODO: take h/w into account
1252 case PIXEL_FORMAT_TRANSPARENT:
1253 case PIXEL_FORMAT_TRANSLUCENT:
1254 format = PIXEL_FORMAT_RGBA_8888;
1255 break;
1256 case PIXEL_FORMAT_OPAQUE:
1257 format = PIXEL_FORMAT_RGB_565;
1258 break;
1259 }
1260
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001261 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001262 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 if (LIKELY(err == NO_ERROR)) {
1264 layer->initStates(w, h, flags);
1265 addLayer_l(layer);
1266 } else {
1267 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001268 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 }
1270 return layer;
1271}
1272
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001273sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001274 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1276{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001277 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 layer->initStates(w, h, flags);
1279 addLayer_l(layer);
1280 return layer;
1281}
1282
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001283sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001284 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1286{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 layer->initStates(w, h, flags);
1289 addLayer_l(layer);
1290 return layer;
1291}
1292
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001293sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001294 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1296{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001297 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 layer->initStates(w, h, flags);
1299 addLayer_l(layer);
1300 return layer;
1301}
1302
Mathias Agopian9a112062009-04-17 19:36:26 -07001303status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304{
Mathias Agopian9a112062009-04-17 19:36:26 -07001305 /*
1306 * called by the window manager, when a surface should be marked for
1307 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001308 *
1309 * The surface is removed from the current and drawing lists, but placed
1310 * in the purgatory queue, so it's not destroyed right-away (we need
1311 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001312 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001313
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001314 Mutex::Autolock _l(mStateLock);
1315 sp<LayerBaseClient> layer = getLayerUser_l(index);
1316 status_t err = purgatorizeLayer_l(layer);
1317 if (err == NO_ERROR) {
1318 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001319 }
1320 return err;
1321}
1322
1323status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1324{
Mathias Agopian3d579642009-06-04 18:46:21 -07001325 /* called by ~ISurface() when all references are gone */
Mathias Agopian9a112062009-04-17 19:36:26 -07001326
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001327 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001328 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001329 sp<LayerBaseClient> layer;
1330 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001331 MessageDestroySurface(
1332 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1333 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001334 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001335 sp<LayerBaseClient> l(layer);
1336 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001337 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001338 // remove the layer from the current list -- chances are that it's
1339 // not in the list anyway, because it should have been removed
1340 // already upon request of the client (eg: window manager).
1341 // However, a buggy client could have not done that.
1342 // Since we know we don't have any more clients, we don't need
1343 // to use the purgatory.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001344 status_t err = flinger->removeLayer_l(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001345 if (err == NAME_NOT_FOUND) {
1346 // The surface wasn't in the current list, which means it was
1347 // removed already, which means it is in the purgatory,
1348 // and need to be removed from there.
1349 // This needs to happen from the main thread since its dtor
1350 // must run from there (b/c of OpenGL ES). Additionally, we
1351 // can't really acquire our internal lock from
1352 // destroySurface() -- see postMessage() below.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001353 ssize_t idx = flinger->mLayerPurgatory.remove(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001354 LOGE_IF(idx < 0,
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001355 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian3d579642009-06-04 18:46:21 -07001356 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001357 return true;
1358 }
1359 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001360
1361 // It's better to not acquire our internal lock here, because it's hard
1362 // to predict that it's not going to be already taken when ~Surface()
1363 // is called.
1364
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001365 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 return NO_ERROR;
1367}
1368
1369status_t SurfaceFlinger::setClientState(
1370 ClientID cid,
1371 int32_t count,
1372 const layer_state_t* states)
1373{
1374 Mutex::Autolock _l(mStateLock);
1375 uint32_t flags = 0;
1376 cid <<= 16;
1377 for (int i=0 ; i<count ; i++) {
1378 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001379 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001380 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 if (what & ePositionChanged) {
1383 if (layer->setPosition(s.x, s.y))
1384 flags |= eTraversalNeeded;
1385 }
1386 if (what & eLayerChanged) {
1387 if (layer->setLayer(s.z)) {
1388 mCurrentState.layersSortedByZ.reorder(
1389 layer, &Layer::compareCurrentStateZ);
1390 // we need traversal (state changed)
1391 // AND transaction (list changed)
1392 flags |= eTransactionNeeded|eTraversalNeeded;
1393 }
1394 }
1395 if (what & eSizeChanged) {
1396 if (layer->setSize(s.w, s.h))
1397 flags |= eTraversalNeeded;
1398 }
1399 if (what & eAlphaChanged) {
1400 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1401 flags |= eTraversalNeeded;
1402 }
1403 if (what & eMatrixChanged) {
1404 if (layer->setMatrix(s.matrix))
1405 flags |= eTraversalNeeded;
1406 }
1407 if (what & eTransparentRegionChanged) {
1408 if (layer->setTransparentRegionHint(s.transparentRegion))
1409 flags |= eTraversalNeeded;
1410 }
1411 if (what & eVisibilityChanged) {
1412 if (layer->setFlags(s.flags, s.mask))
1413 flags |= eTraversalNeeded;
1414 }
1415 }
1416 }
1417 if (flags) {
1418 setTransactionFlags(flags);
1419 }
1420 return NO_ERROR;
1421}
1422
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001423sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001425 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1426 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427}
1428
1429void SurfaceFlinger::screenReleased(int dpy)
1430{
1431 // this may be called by a signal handler, we can't do too much in here
1432 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1433 signalEvent();
1434}
1435
1436void SurfaceFlinger::screenAcquired(int dpy)
1437{
1438 // this may be called by a signal handler, we can't do too much in here
1439 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1440 signalEvent();
1441}
1442
1443status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1444{
1445 const size_t SIZE = 1024;
1446 char buffer[SIZE];
1447 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001448 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 snprintf(buffer, SIZE, "Permission Denial: "
1450 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1451 IPCThreadState::self()->getCallingPid(),
1452 IPCThreadState::self()->getCallingUid());
1453 result.append(buffer);
1454 } else {
1455 Mutex::Autolock _l(mStateLock);
1456 size_t s = mClientsMap.size();
1457 char name[64];
1458 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001459 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 sprintf(name, " Client (id=0x%08x)", client->cid);
1461 client->dump(name);
1462 }
1463 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1464 const size_t count = currentLayers.size();
1465 for (size_t i=0 ; i<count ; i++) {
1466 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001467 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468 const Layer::State& s = layer->drawingState();
1469 snprintf(buffer, SIZE,
1470 "+ %s %p\n"
1471 " "
1472 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1473 "needsBlending=%1d, invalidate=%1d, "
1474 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001475 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 s.z, layer->tx(), layer->ty(), s.w, s.h,
1477 layer->needsBlending(), layer->contentDirty,
1478 s.alpha, s.flags,
1479 s.transform[0], s.transform[1],
1480 s.transform[2], s.transform[3]);
1481 result.append(buffer);
1482 buffer[0] = 0;
1483 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001484 sp<LayerBaseClient> lbc =
1485 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1486 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001487 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 snprintf(buffer, SIZE,
1489 " "
1490 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001491 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 lbc->getIdentity());
1493 }
1494 result.append(buffer);
1495 buffer[0] = 0;
1496 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001497 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1498 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499 const LayerBitmap& buf0(l->getBuffer(0));
1500 const LayerBitmap& buf1(l->getBuffer(1));
1501 snprintf(buffer, SIZE,
1502 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001503 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504 " freezeLock=%p, swapState=0x%08x\n",
1505 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001506 buf0.getWidth(), buf0.getHeight(),
1507 buf0.getBuffer()->getStride(),
1508 buf1.getWidth(), buf1.getHeight(),
1509 buf1.getBuffer()->getStride(),
1510 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511 l->lcblk->swapState);
1512 }
1513 result.append(buffer);
1514 buffer[0] = 0;
1515 s.transparentRegion.dump(result, "transparentRegion");
1516 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1517 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1518 }
1519 mWormholeRegion.dump(result, "WormholeRegion");
1520 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1521 snprintf(buffer, SIZE,
1522 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1523 mFreezeDisplay?"yes":"no", mFreezeCount,
1524 mCurrentState.orientation, hw.canDraw());
1525 result.append(buffer);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001526 snprintf(buffer, SIZE, " purgatory size: %d, client count: %d\n",
1527 mLayerPurgatory.size(), mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001528 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001529 const BufferAllocator& alloc(BufferAllocator::get());
1530 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 }
1532 write(fd, result.string(), result.size());
1533 return NO_ERROR;
1534}
1535
1536status_t SurfaceFlinger::onTransact(
1537 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1538{
1539 switch (code) {
1540 case CREATE_CONNECTION:
1541 case OPEN_GLOBAL_TRANSACTION:
1542 case CLOSE_GLOBAL_TRANSACTION:
1543 case SET_ORIENTATION:
1544 case FREEZE_DISPLAY:
1545 case UNFREEZE_DISPLAY:
1546 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 {
1548 // codes that require permission check
1549 IPCThreadState* ipc = IPCThreadState::self();
1550 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001551 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001552 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1553 LOGE("Permission Denial: "
1554 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1555 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 }
1557 }
1558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1560 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001561 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001562 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1563 IPCThreadState* ipc = IPCThreadState::self();
1564 const int pid = ipc->getCallingPid();
1565 const int uid = ipc->getCallingUid();
1566 LOGE("Permission Denial: "
1567 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 return PERMISSION_DENIED;
1569 }
1570 int n;
1571 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001572 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001574 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575 return NO_ERROR;
1576 case 1002: // SHOW_UPDATES
1577 n = data.readInt32();
1578 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1579 return NO_ERROR;
1580 case 1003: // SHOW_BACKGROUND
1581 n = data.readInt32();
1582 mDebugBackground = n ? 1 : 0;
1583 return NO_ERROR;
1584 case 1004:{ // repaint everything
1585 Mutex::Autolock _l(mStateLock);
1586 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1587 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1588 signalEvent();
1589 }
1590 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 case 1007: // set mFreezeCount
1592 mFreezeCount = data.readInt32();
1593 return NO_ERROR;
1594 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001595 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596 reply->writeInt32(0);
1597 reply->writeInt32(mDebugRegion);
1598 reply->writeInt32(mDebugBackground);
1599 return NO_ERROR;
1600 case 1013: {
1601 Mutex::Autolock _l(mStateLock);
1602 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1603 reply->writeInt32(hw.getPageFlipCount());
1604 }
1605 return NO_ERROR;
1606 }
1607 }
1608 return err;
1609}
1610
1611// ---------------------------------------------------------------------------
1612#if 0
1613#pragma mark -
1614#endif
1615
1616Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1617 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1618{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 const int pgsize = getpagesize();
1620 const int cblksize=((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1621 mCblkHeap = new MemoryDealer(cblksize);
1622 mCblkMemory = mCblkHeap->allocate(cblksize);
1623 if (mCblkMemory != 0) {
1624 ctrlblk = static_cast<per_client_cblk_t *>(mCblkMemory->pointer());
1625 if (ctrlblk) { // construct the shared structure in-place.
1626 new(ctrlblk) per_client_cblk_t;
1627 }
1628 }
1629}
1630
1631Client::~Client() {
1632 if (ctrlblk) {
1633 const int pgsize = getpagesize();
1634 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1635 }
1636}
1637
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638int32_t Client::generateId(int pid)
1639{
1640 const uint32_t i = clz( ~mBitmap );
1641 if (i >= NUM_LAYERS_MAX) {
1642 return NO_MEMORY;
1643 }
1644 mPid = pid;
1645 mInUse.add(uint8_t(i));
1646 mBitmap |= 1<<(31-i);
1647 return i;
1648}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001649
1650status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651{
1652 ssize_t idx = mInUse.indexOf(id);
1653 if (idx < 0)
1654 return NAME_NOT_FOUND;
1655 return mLayers.insertAt(layer, idx);
1656}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001657
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658void Client::free(int32_t id)
1659{
1660 ssize_t idx = mInUse.remove(uint8_t(id));
1661 if (idx >= 0) {
1662 mBitmap &= ~(1<<(31-id));
1663 mLayers.removeItemsAt(idx);
1664 }
1665}
1666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667bool Client::isValid(int32_t i) const {
1668 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1669}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001670
1671sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1672 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001674 if (idx >= 0) {
1675 lbc = mLayers[idx].promote();
1676 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1677 }
1678 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679}
1680
1681void Client::dump(const char* what)
1682{
1683}
1684
1685// ---------------------------------------------------------------------------
1686#if 0
1687#pragma mark -
1688#endif
1689
1690BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemory>& cblk)
1691 : mId(cid), mFlinger(flinger), mCblk(cblk)
1692{
1693}
1694
1695BClient::~BClient() {
1696 // destroy all resources attached to this client
1697 mFlinger->destroyConnection(mId);
1698}
1699
1700void BClient::getControlBlocks(sp<IMemory>* ctrl) const {
1701 *ctrl = mCblk;
1702}
1703
1704sp<ISurface> BClient::createSurface(
1705 ISurfaceFlingerClient::surface_data_t* params, int pid,
1706 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1707 uint32_t flags)
1708{
1709 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1710}
1711
1712status_t BClient::destroySurface(SurfaceID sid)
1713{
1714 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001715 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716}
1717
1718status_t BClient::setState(int32_t count, const layer_state_t* states)
1719{
1720 return mFlinger->setClientState(mId, count, states);
1721}
1722
1723// ---------------------------------------------------------------------------
1724
1725GraphicPlane::GraphicPlane()
1726 : mHw(0)
1727{
1728}
1729
1730GraphicPlane::~GraphicPlane() {
1731 delete mHw;
1732}
1733
1734bool GraphicPlane::initialized() const {
1735 return mHw ? true : false;
1736}
1737
1738void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1739 mHw = hw;
1740}
1741
1742void GraphicPlane::setTransform(const Transform& tr) {
1743 mTransform = tr;
1744 mGlobalTransform = mOrientationTransform * mTransform;
1745}
1746
1747status_t GraphicPlane::orientationToTransfrom(
1748 int orientation, int w, int h, Transform* tr)
1749{
1750 float a, b, c, d, x, y;
1751 switch (orientation) {
1752 case ISurfaceComposer::eOrientationDefault:
1753 a=1; b=0; c=0; d=1; x=0; y=0;
1754 break;
1755 case ISurfaceComposer::eOrientation90:
1756 a=0; b=-1; c=1; d=0; x=w; y=0;
1757 break;
1758 case ISurfaceComposer::eOrientation180:
1759 a=-1; b=0; c=0; d=-1; x=w; y=h;
1760 break;
1761 case ISurfaceComposer::eOrientation270:
1762 a=0; b=1; c=-1; d=0; x=0; y=h;
1763 break;
1764 default:
1765 return BAD_VALUE;
1766 }
1767 tr->set(a, b, c, d);
1768 tr->set(x, y);
1769 return NO_ERROR;
1770}
1771
1772status_t GraphicPlane::setOrientation(int orientation)
1773{
1774 const DisplayHardware& hw(displayHardware());
1775 const float w = hw.getWidth();
1776 const float h = hw.getHeight();
1777
1778 if (orientation == ISurfaceComposer::eOrientationDefault) {
1779 // make sure the default orientation is optimal
1780 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001781 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782 mGlobalTransform = mTransform;
1783 return NO_ERROR;
1784 }
1785
1786 // If the rotation can be handled in hardware, this is where
1787 // the magic should happen.
1788 if (UNLIKELY(orientation == 42)) {
1789 float a, b, c, d, x, y;
1790 const float r = (3.14159265f / 180.0f) * 42.0f;
1791 const float si = sinf(r);
1792 const float co = cosf(r);
1793 a=co; b=-si; c=si; d=co;
1794 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1795 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1796 mOrientationTransform.set(a, b, c, d);
1797 mOrientationTransform.set(x, y);
1798 } else {
1799 GraphicPlane::orientationToTransfrom(orientation, w, h,
1800 &mOrientationTransform);
1801 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001802 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001803 mGlobalTransform = mOrientationTransform * mTransform;
1804 return NO_ERROR;
1805}
1806
1807const DisplayHardware& GraphicPlane::displayHardware() const {
1808 return *mHw;
1809}
1810
1811const Transform& GraphicPlane::transform() const {
1812 return mGlobalTransform;
1813}
1814
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001815EGLDisplay GraphicPlane::getEGLDisplay() const {
1816 return mHw->getEGLDisplay();
1817}
1818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819// ---------------------------------------------------------------------------
1820
1821}; // namespace android