blob: 2e7bbeafcf0aa53a83ed6b3249fa70c348de28d4 [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 {
728 // compute the exposed region
729 // dirty = what's visible now - what's wasn't covered before
730 // = what's visible now & what's was covered before
731 dirty = visibleRegion.intersect(layer->coveredRegionScreen);
732 }
733 dirty.subtractSelf(aboveOpaqueLayers);
734
735 // accumulate to the screen dirty region
736 dirtyRegion.orSelf(dirty);
737
Mathias Agopian62b74442009-04-14 23:02:51 -0700738 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 aboveOpaqueLayers.orSelf(opaqueRegion);
740 aboveCoveredLayers.orSelf(bounds);
741
742 // Store the visible region is screen space
743 layer->setVisibleRegion(visibleRegion);
744 layer->setCoveredRegion(coveredRegion);
745
Mathias Agopian62b74442009-04-14 23:02:51 -0700746 // If a secure layer is partially visible, lock down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 if (layer->isSecure() && !visibleRegion.isEmpty()) {
748 secureFrameBuffer = true;
749 }
750 }
751
752 mSecureFrameBuffer = secureFrameBuffer;
753 opaqueRegion = aboveOpaqueLayers;
754}
755
756
757void SurfaceFlinger::commitTransaction()
758{
759 mDrawingState = mCurrentState;
760 mTransactionCV.signal();
761}
762
763void SurfaceFlinger::handlePageFlip()
764{
765 bool visibleRegions = mVisibleRegionsDirty;
766 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
767 visibleRegions |= lockPageFlip(currentLayers);
768
769 const DisplayHardware& hw = graphicPlane(0).displayHardware();
770 const Region screenRegion(hw.bounds());
771 if (visibleRegions) {
772 Region opaqueRegion;
773 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
774 mWormholeRegion = screenRegion.subtract(opaqueRegion);
775 mVisibleRegionsDirty = false;
776 }
777
778 unlockPageFlip(currentLayers);
779 mDirtyRegion.andSelf(screenRegion);
780}
781
782bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
783{
784 bool recomputeVisibleRegions = false;
785 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700786 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700788 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 layer->lockPageFlip(recomputeVisibleRegions);
790 }
791 return recomputeVisibleRegions;
792}
793
794void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
795{
796 const GraphicPlane& plane(graphicPlane(0));
797 const Transform& planeTransform(plane.transform());
798 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700799 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700801 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 layer->unlockPageFlip(planeTransform, mDirtyRegion);
803 }
804}
805
806void SurfaceFlinger::handleRepaint()
807{
808 // set the frame buffer
809 const DisplayHardware& hw(graphicPlane(0).displayHardware());
810 glMatrixMode(GL_MODELVIEW);
811 glLoadIdentity();
812
813 if (UNLIKELY(mDebugRegion)) {
814 debugFlashRegions();
815 }
816
817 // compute the invalid region
818 mInvalidRegion.orSelf(mDirtyRegion);
819
820 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700821 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
822 (flags & DisplayHardware::BUFFER_PRESERVED))
823 {
824 // we can redraw only what's dirty
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 } else {
826 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700827 // we need to redraw the rectangle that will be updated
828 // (pushed to the framebuffer).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829 mDirtyRegion.set(mInvalidRegion.bounds());
830 } else {
831 // we need to redraw everything
832 mDirtyRegion.set(hw.bounds());
833 mInvalidRegion = mDirtyRegion;
834 }
835 }
836
837 // compose all surfaces
838 composeSurfaces(mDirtyRegion);
839
840 // clear the dirty regions
841 mDirtyRegion.clear();
842}
843
844void SurfaceFlinger::composeSurfaces(const Region& dirty)
845{
846 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
847 // should never happen unless the window manager has a bug
848 // draw something...
849 drawWormhole();
850 }
851 const SurfaceFlinger& flinger(*this);
852 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
853 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700854 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700856 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 const Region& visibleRegion(layer->visibleRegionScreen);
858 if (!visibleRegion.isEmpty()) {
859 const Region clip(dirty.intersect(visibleRegion));
860 if (!clip.isEmpty()) {
861 layer->draw(clip);
862 }
863 }
864 }
865}
866
867void SurfaceFlinger::unlockClients()
868{
869 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
870 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700871 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700873 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874 layer->finishPageFlip();
875 }
876}
877
Mathias Agopianf9d93272009-06-19 17:00:27 -0700878void SurfaceFlinger::scheduleBroadcast(const sp<Client>& client)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879{
880 if (mLastScheduledBroadcast != client) {
881 mLastScheduledBroadcast = client;
882 mScheduledBroadcasts.add(client);
883 }
884}
885
886void SurfaceFlinger::executeScheduledBroadcasts()
887{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700888 SortedVector< wp<Client> >& list(mScheduledBroadcasts);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 size_t count = list.size();
890 while (count--) {
Mathias Agopianf9d93272009-06-19 17:00:27 -0700891 sp<Client> client = list[count].promote();
892 if (client != 0) {
893 per_client_cblk_t* const cblk = client->ctrlblk;
894 if (cblk->lock.tryLock() == NO_ERROR) {
895 cblk->cv.broadcast();
896 list.removeAt(count);
897 cblk->lock.unlock();
898 } else {
899 // schedule another round
900 LOGW("executeScheduledBroadcasts() skipped, "
901 "contention on the client. We'll try again later...");
902 signalDelayedEvent(ms2ns(4));
903 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 }
905 }
906 mLastScheduledBroadcast = 0;
907}
908
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909void SurfaceFlinger::debugFlashRegions()
910{
Mathias Agopian0926f502009-05-04 14:17:04 -0700911 const DisplayHardware& hw(graphicPlane(0).displayHardware());
912 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700913
914 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
915 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700916 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
917 mDirtyRegion.bounds() : hw.bounds());
918 composeSurfaces(repaint);
919 }
920
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 glDisable(GL_TEXTURE_2D);
922 glDisable(GL_BLEND);
923 glDisable(GL_DITHER);
924 glDisable(GL_SCISSOR_TEST);
925
Mathias Agopian0926f502009-05-04 14:17:04 -0700926 static int toggle = 0;
927 toggle = 1 - toggle;
928 if (toggle) {
929 glColor4x(0x10000, 0, 0x10000, 0x10000);
930 } else {
931 glColor4x(0x10000, 0x10000, 0, 0x10000);
932 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933
Mathias Agopian20f68782009-05-11 00:03:41 -0700934 Region::const_iterator it = mDirtyRegion.begin();
935 Region::const_iterator const end = mDirtyRegion.end();
936 while (it != end) {
937 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 GLfloat vertices[][2] = {
939 { r.left, r.top },
940 { r.left, r.bottom },
941 { r.right, r.bottom },
942 { r.right, r.top }
943 };
944 glVertexPointer(2, GL_FLOAT, 0, vertices);
945 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
946 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700947
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 hw.flip(mDirtyRegion.merge(mInvalidRegion));
949 mInvalidRegion.clear();
950
951 if (mDebugRegion > 1)
952 usleep(mDebugRegion * 1000);
953
954 glEnable(GL_SCISSOR_TEST);
955 //mDirtyRegion.dump("mDirtyRegion");
956}
957
958void SurfaceFlinger::drawWormhole() const
959{
960 const Region region(mWormholeRegion.intersect(mDirtyRegion));
961 if (region.isEmpty())
962 return;
963
964 const DisplayHardware& hw(graphicPlane(0).displayHardware());
965 const int32_t width = hw.getWidth();
966 const int32_t height = hw.getHeight();
967
968 glDisable(GL_BLEND);
969 glDisable(GL_DITHER);
970
971 if (LIKELY(!mDebugBackground)) {
972 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700973 Region::const_iterator it = region.begin();
974 Region::const_iterator const end = region.end();
975 while (it != end) {
976 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 const GLint sy = height - (r.top + r.height());
978 glScissor(r.left, sy, r.width(), r.height());
979 glClear(GL_COLOR_BUFFER_BIT);
980 }
981 } else {
982 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
983 { width, height }, { 0, height } };
984 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
985 glVertexPointer(2, GL_SHORT, 0, vertices);
986 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
987 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
988 glEnable(GL_TEXTURE_2D);
989 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
990 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
991 glMatrixMode(GL_TEXTURE);
992 glLoadIdentity();
993 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700994 Region::const_iterator it = region.begin();
995 Region::const_iterator const end = region.end();
996 while (it != end) {
997 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 const GLint sy = height - (r.top + r.height());
999 glScissor(r.left, sy, r.width(), r.height());
1000 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1001 }
1002 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1003 }
1004}
1005
1006void SurfaceFlinger::debugShowFPS() const
1007{
1008 static int mFrameCount;
1009 static int mLastFrameCount = 0;
1010 static nsecs_t mLastFpsTime = 0;
1011 static float mFps = 0;
1012 mFrameCount++;
1013 nsecs_t now = systemTime();
1014 nsecs_t diff = now - mLastFpsTime;
1015 if (diff > ms2ns(250)) {
1016 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1017 mLastFpsTime = now;
1018 mLastFrameCount = mFrameCount;
1019 }
1020 // XXX: mFPS has the value we want
1021 }
1022
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001023status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024{
1025 Mutex::Autolock _l(mStateLock);
1026 addLayer_l(layer);
1027 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1028 return NO_ERROR;
1029}
1030
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001031status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032{
1033 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001034 status_t err = purgatorizeLayer_l(layer);
1035 if (err == NO_ERROR)
1036 setTransactionFlags(eTransactionNeeded);
1037 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038}
1039
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001040status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041{
1042 layer->forceVisibilityTransaction();
1043 setTransactionFlags(eTraversalNeeded);
1044 return NO_ERROR;
1045}
1046
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001047status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048{
1049 ssize_t i = mCurrentState.layersSortedByZ.add(
1050 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001051 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1052 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 mLayerMap.add(lbc->serverIndex(), lbc);
1054 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 return NO_ERROR;
1056}
1057
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001058status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059{
1060 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1061 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001062 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001063 sp<LayerBaseClient> layer =
1064 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001065 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 mLayerMap.removeItem(layer->serverIndex());
1067 }
1068 return NO_ERROR;
1069 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001070 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071}
1072
Mathias Agopian9a112062009-04-17 19:36:26 -07001073status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1074{
1075 // First add the layer to the purgatory list, which makes sure it won't
1076 // go away, then remove it from the main list (through a transaction).
1077 ssize_t err = removeLayer_l(layerBase);
1078 if (err >= 0) {
1079 mLayerPurgatory.add(layerBase);
1080 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001081 // it's possible that we don't find a layer, because it might
1082 // have been destroyed already -- this is not technically an error
1083 // from the user because there is a race between BClient::destroySurface(),
1084 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001085 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1086}
1087
1088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089void SurfaceFlinger::free_resources_l()
1090{
1091 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001092 mLayersRemoved = false;
1093
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001095 SortedVector< wp<Client> >& scheduledBroadcasts(mScheduledBroadcasts);
1096 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 const size_t count = disconnectedClients.size();
1098 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001099 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 // if this client is the scheduled broadcast list,
1101 // remove it from there (and we don't need to signal it
1102 // since it is dead).
1103 int32_t index = scheduledBroadcasts.indexOf(client);
1104 if (index >= 0) {
1105 scheduledBroadcasts.removeItemsAt(index);
1106 }
1107 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 }
1109 disconnectedClients.clear();
1110}
1111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1113{
1114 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1115}
1116
1117uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1118{
1119 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1120 if ((old & flags)==0) { // wake the server up
1121 if (delay > 0) {
1122 signalDelayedEvent(delay);
1123 } else {
1124 signalEvent();
1125 }
1126 }
1127 return old;
1128}
1129
1130void SurfaceFlinger::openGlobalTransaction()
1131{
1132 android_atomic_inc(&mTransactionCount);
1133}
1134
1135void SurfaceFlinger::closeGlobalTransaction()
1136{
1137 if (android_atomic_dec(&mTransactionCount) == 1) {
1138 signalEvent();
1139 }
1140}
1141
1142status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1143{
1144 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1145 return BAD_VALUE;
1146
1147 Mutex::Autolock _l(mStateLock);
1148 mCurrentState.freezeDisplay = 1;
1149 setTransactionFlags(eTransactionNeeded);
1150
1151 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001152 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 return NO_ERROR;
1154}
1155
1156status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1157{
1158 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1159 return BAD_VALUE;
1160
1161 Mutex::Autolock _l(mStateLock);
1162 mCurrentState.freezeDisplay = 0;
1163 setTransactionFlags(eTransactionNeeded);
1164
1165 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001166 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 return NO_ERROR;
1168}
1169
Mathias Agopianc08731e2009-03-27 18:11:38 -07001170int SurfaceFlinger::setOrientation(DisplayID dpy,
1171 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172{
1173 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1174 return BAD_VALUE;
1175
1176 Mutex::Autolock _l(mStateLock);
1177 if (mCurrentState.orientation != orientation) {
1178 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001179 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 mCurrentState.orientation = orientation;
1181 setTransactionFlags(eTransactionNeeded);
1182 mTransactionCV.wait(mStateLock);
1183 } else {
1184 orientation = BAD_VALUE;
1185 }
1186 }
1187 return orientation;
1188}
1189
1190sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1191 ISurfaceFlingerClient::surface_data_t* params,
1192 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1193 uint32_t flags)
1194{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001195 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 sp<LayerBaseClient::Surface> surfaceHandle;
1197 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001198 sp<Client> client = mClientsMap.valueFor(clientId);
1199 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1201 return surfaceHandle;
1202 }
1203
1204 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001205 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1207 LOGE("createSurface() failed, generateId = %d", id);
1208 return surfaceHandle;
1209 }
1210
1211 switch (flags & eFXSurfaceMask) {
1212 case eFXSurfaceNormal:
1213 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001214 layer = createPushBuffersSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 } else {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001216 layer = createNormalSurfaceLocked(client, d, id, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 }
1218 break;
1219 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001220 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 break;
1222 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001223 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 break;
1225 }
1226
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001227 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 setTransactionFlags(eTransactionNeeded);
1229 surfaceHandle = layer->getSurface();
1230 if (surfaceHandle != 0)
1231 surfaceHandle->getSurfaceData(params);
1232 }
1233
1234 return surfaceHandle;
1235}
1236
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001237sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001238 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
1240{
1241 // initialize the surfaces
1242 switch (format) { // TODO: take h/w into account
1243 case PIXEL_FORMAT_TRANSPARENT:
1244 case PIXEL_FORMAT_TRANSLUCENT:
1245 format = PIXEL_FORMAT_RGBA_8888;
1246 break;
1247 case PIXEL_FORMAT_OPAQUE:
1248 format = PIXEL_FORMAT_RGB_565;
1249 break;
1250 }
1251
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001252 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001253 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 if (LIKELY(err == NO_ERROR)) {
1255 layer->initStates(w, h, flags);
1256 addLayer_l(layer);
1257 } else {
1258 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001259 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 }
1261 return layer;
1262}
1263
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001264sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001265 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1267{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001268 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 layer->initStates(w, h, flags);
1270 addLayer_l(layer);
1271 return layer;
1272}
1273
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001274sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001275 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1277{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001278 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 layer->initStates(w, h, flags);
1280 addLayer_l(layer);
1281 return layer;
1282}
1283
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001284sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001285 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1287{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001288 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 layer->initStates(w, h, flags);
1290 addLayer_l(layer);
1291 return layer;
1292}
1293
Mathias Agopian9a112062009-04-17 19:36:26 -07001294status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295{
Mathias Agopian9a112062009-04-17 19:36:26 -07001296 /*
1297 * called by the window manager, when a surface should be marked for
1298 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001299 *
1300 * The surface is removed from the current and drawing lists, but placed
1301 * in the purgatory queue, so it's not destroyed right-away (we need
1302 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001303 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001304
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001305 Mutex::Autolock _l(mStateLock);
1306 sp<LayerBaseClient> layer = getLayerUser_l(index);
1307 status_t err = purgatorizeLayer_l(layer);
1308 if (err == NO_ERROR) {
1309 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001310 }
1311 return err;
1312}
1313
1314status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1315{
Mathias Agopian3d579642009-06-04 18:46:21 -07001316 /* called by ~ISurface() when all references are gone */
Mathias Agopian9a112062009-04-17 19:36:26 -07001317
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001318 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001319 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001320 sp<LayerBaseClient> layer;
1321 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001322 MessageDestroySurface(
1323 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1324 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001325 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001326 sp<LayerBaseClient> l(layer);
1327 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001328 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001329 // remove the layer from the current list -- chances are that it's
1330 // not in the list anyway, because it should have been removed
1331 // already upon request of the client (eg: window manager).
1332 // However, a buggy client could have not done that.
1333 // Since we know we don't have any more clients, we don't need
1334 // to use the purgatory.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001335 status_t err = flinger->removeLayer_l(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001336 if (err == NAME_NOT_FOUND) {
1337 // The surface wasn't in the current list, which means it was
1338 // removed already, which means it is in the purgatory,
1339 // and need to be removed from there.
1340 // This needs to happen from the main thread since its dtor
1341 // must run from there (b/c of OpenGL ES). Additionally, we
1342 // can't really acquire our internal lock from
1343 // destroySurface() -- see postMessage() below.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001344 ssize_t idx = flinger->mLayerPurgatory.remove(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001345 LOGE_IF(idx < 0,
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001346 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian3d579642009-06-04 18:46:21 -07001347 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001348 return true;
1349 }
1350 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001351
1352 // It's better to not acquire our internal lock here, because it's hard
1353 // to predict that it's not going to be already taken when ~Surface()
1354 // is called.
1355
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001356 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 return NO_ERROR;
1358}
1359
1360status_t SurfaceFlinger::setClientState(
1361 ClientID cid,
1362 int32_t count,
1363 const layer_state_t* states)
1364{
1365 Mutex::Autolock _l(mStateLock);
1366 uint32_t flags = 0;
1367 cid <<= 16;
1368 for (int i=0 ; i<count ; i++) {
1369 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001370 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001371 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 if (what & ePositionChanged) {
1374 if (layer->setPosition(s.x, s.y))
1375 flags |= eTraversalNeeded;
1376 }
1377 if (what & eLayerChanged) {
1378 if (layer->setLayer(s.z)) {
1379 mCurrentState.layersSortedByZ.reorder(
1380 layer, &Layer::compareCurrentStateZ);
1381 // we need traversal (state changed)
1382 // AND transaction (list changed)
1383 flags |= eTransactionNeeded|eTraversalNeeded;
1384 }
1385 }
1386 if (what & eSizeChanged) {
1387 if (layer->setSize(s.w, s.h))
1388 flags |= eTraversalNeeded;
1389 }
1390 if (what & eAlphaChanged) {
1391 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1392 flags |= eTraversalNeeded;
1393 }
1394 if (what & eMatrixChanged) {
1395 if (layer->setMatrix(s.matrix))
1396 flags |= eTraversalNeeded;
1397 }
1398 if (what & eTransparentRegionChanged) {
1399 if (layer->setTransparentRegionHint(s.transparentRegion))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eVisibilityChanged) {
1403 if (layer->setFlags(s.flags, s.mask))
1404 flags |= eTraversalNeeded;
1405 }
1406 }
1407 }
1408 if (flags) {
1409 setTransactionFlags(flags);
1410 }
1411 return NO_ERROR;
1412}
1413
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001414sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001416 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1417 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418}
1419
1420void SurfaceFlinger::screenReleased(int dpy)
1421{
1422 // this may be called by a signal handler, we can't do too much in here
1423 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1424 signalEvent();
1425}
1426
1427void SurfaceFlinger::screenAcquired(int dpy)
1428{
1429 // this may be called by a signal handler, we can't do too much in here
1430 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1431 signalEvent();
1432}
1433
1434status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1435{
1436 const size_t SIZE = 1024;
1437 char buffer[SIZE];
1438 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001439 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 snprintf(buffer, SIZE, "Permission Denial: "
1441 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1442 IPCThreadState::self()->getCallingPid(),
1443 IPCThreadState::self()->getCallingUid());
1444 result.append(buffer);
1445 } else {
1446 Mutex::Autolock _l(mStateLock);
1447 size_t s = mClientsMap.size();
1448 char name[64];
1449 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001450 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 sprintf(name, " Client (id=0x%08x)", client->cid);
1452 client->dump(name);
1453 }
1454 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1455 const size_t count = currentLayers.size();
1456 for (size_t i=0 ; i<count ; i++) {
1457 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001458 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 const Layer::State& s = layer->drawingState();
1460 snprintf(buffer, SIZE,
1461 "+ %s %p\n"
1462 " "
1463 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1464 "needsBlending=%1d, invalidate=%1d, "
1465 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001466 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 s.z, layer->tx(), layer->ty(), s.w, s.h,
1468 layer->needsBlending(), layer->contentDirty,
1469 s.alpha, s.flags,
1470 s.transform[0], s.transform[1],
1471 s.transform[2], s.transform[3]);
1472 result.append(buffer);
1473 buffer[0] = 0;
1474 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001475 sp<LayerBaseClient> lbc =
1476 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1477 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001478 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 snprintf(buffer, SIZE,
1480 " "
1481 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001482 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 lbc->getIdentity());
1484 }
1485 result.append(buffer);
1486 buffer[0] = 0;
1487 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001488 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1489 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 const LayerBitmap& buf0(l->getBuffer(0));
1491 const LayerBitmap& buf1(l->getBuffer(1));
1492 snprintf(buffer, SIZE,
1493 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001494 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 " freezeLock=%p, swapState=0x%08x\n",
1496 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001497 buf0.getWidth(), buf0.getHeight(),
1498 buf0.getBuffer()->getStride(),
1499 buf1.getWidth(), buf1.getHeight(),
1500 buf1.getBuffer()->getStride(),
1501 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 l->lcblk->swapState);
1503 }
1504 result.append(buffer);
1505 buffer[0] = 0;
1506 s.transparentRegion.dump(result, "transparentRegion");
1507 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1508 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1509 }
1510 mWormholeRegion.dump(result, "WormholeRegion");
1511 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1512 snprintf(buffer, SIZE,
1513 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1514 mFreezeDisplay?"yes":"no", mFreezeCount,
1515 mCurrentState.orientation, hw.canDraw());
1516 result.append(buffer);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001517 snprintf(buffer, SIZE, " purgatory size: %d, client count: %d\n",
1518 mLayerPurgatory.size(), mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001519 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001520 const BufferAllocator& alloc(BufferAllocator::get());
1521 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 }
1523 write(fd, result.string(), result.size());
1524 return NO_ERROR;
1525}
1526
1527status_t SurfaceFlinger::onTransact(
1528 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1529{
1530 switch (code) {
1531 case CREATE_CONNECTION:
1532 case OPEN_GLOBAL_TRANSACTION:
1533 case CLOSE_GLOBAL_TRANSACTION:
1534 case SET_ORIENTATION:
1535 case FREEZE_DISPLAY:
1536 case UNFREEZE_DISPLAY:
1537 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 {
1539 // codes that require permission check
1540 IPCThreadState* ipc = IPCThreadState::self();
1541 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001542 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001543 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1544 LOGE("Permission Denial: "
1545 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1546 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 }
1548 }
1549 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1551 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001552 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1553 IPCThreadState* ipc = IPCThreadState::self();
1554 const int pid = ipc->getCallingPid();
1555 const int uid = ipc->getCallingUid();
1556 LOGE("Permission Denial: "
1557 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 return PERMISSION_DENIED;
1559 }
1560 int n;
1561 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001562 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001564 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 return NO_ERROR;
1566 case 1002: // SHOW_UPDATES
1567 n = data.readInt32();
1568 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1569 return NO_ERROR;
1570 case 1003: // SHOW_BACKGROUND
1571 n = data.readInt32();
1572 mDebugBackground = n ? 1 : 0;
1573 return NO_ERROR;
1574 case 1004:{ // repaint everything
1575 Mutex::Autolock _l(mStateLock);
1576 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1577 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1578 signalEvent();
1579 }
1580 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 case 1007: // set mFreezeCount
1582 mFreezeCount = data.readInt32();
1583 return NO_ERROR;
1584 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001585 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 reply->writeInt32(0);
1587 reply->writeInt32(mDebugRegion);
1588 reply->writeInt32(mDebugBackground);
1589 return NO_ERROR;
1590 case 1013: {
1591 Mutex::Autolock _l(mStateLock);
1592 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1593 reply->writeInt32(hw.getPageFlipCount());
1594 }
1595 return NO_ERROR;
1596 }
1597 }
1598 return err;
1599}
1600
1601// ---------------------------------------------------------------------------
1602#if 0
1603#pragma mark -
1604#endif
1605
1606Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1607 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1608{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 const int pgsize = getpagesize();
1610 const int cblksize=((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1611 mCblkHeap = new MemoryDealer(cblksize);
1612 mCblkMemory = mCblkHeap->allocate(cblksize);
1613 if (mCblkMemory != 0) {
1614 ctrlblk = static_cast<per_client_cblk_t *>(mCblkMemory->pointer());
1615 if (ctrlblk) { // construct the shared structure in-place.
1616 new(ctrlblk) per_client_cblk_t;
1617 }
1618 }
1619}
1620
1621Client::~Client() {
1622 if (ctrlblk) {
1623 const int pgsize = getpagesize();
1624 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1625 }
1626}
1627
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628int32_t Client::generateId(int pid)
1629{
1630 const uint32_t i = clz( ~mBitmap );
1631 if (i >= NUM_LAYERS_MAX) {
1632 return NO_MEMORY;
1633 }
1634 mPid = pid;
1635 mInUse.add(uint8_t(i));
1636 mBitmap |= 1<<(31-i);
1637 return i;
1638}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001639
1640status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641{
1642 ssize_t idx = mInUse.indexOf(id);
1643 if (idx < 0)
1644 return NAME_NOT_FOUND;
1645 return mLayers.insertAt(layer, idx);
1646}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648void Client::free(int32_t id)
1649{
1650 ssize_t idx = mInUse.remove(uint8_t(id));
1651 if (idx >= 0) {
1652 mBitmap &= ~(1<<(31-id));
1653 mLayers.removeItemsAt(idx);
1654 }
1655}
1656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657bool Client::isValid(int32_t i) const {
1658 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1659}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001660
1661sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1662 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001664 if (idx >= 0) {
1665 lbc = mLayers[idx].promote();
1666 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1667 }
1668 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669}
1670
1671void Client::dump(const char* what)
1672{
1673}
1674
1675// ---------------------------------------------------------------------------
1676#if 0
1677#pragma mark -
1678#endif
1679
1680BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemory>& cblk)
1681 : mId(cid), mFlinger(flinger), mCblk(cblk)
1682{
1683}
1684
1685BClient::~BClient() {
1686 // destroy all resources attached to this client
1687 mFlinger->destroyConnection(mId);
1688}
1689
1690void BClient::getControlBlocks(sp<IMemory>* ctrl) const {
1691 *ctrl = mCblk;
1692}
1693
1694sp<ISurface> BClient::createSurface(
1695 ISurfaceFlingerClient::surface_data_t* params, int pid,
1696 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1697 uint32_t flags)
1698{
1699 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1700}
1701
1702status_t BClient::destroySurface(SurfaceID sid)
1703{
1704 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001705 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706}
1707
1708status_t BClient::setState(int32_t count, const layer_state_t* states)
1709{
1710 return mFlinger->setClientState(mId, count, states);
1711}
1712
1713// ---------------------------------------------------------------------------
1714
1715GraphicPlane::GraphicPlane()
1716 : mHw(0)
1717{
1718}
1719
1720GraphicPlane::~GraphicPlane() {
1721 delete mHw;
1722}
1723
1724bool GraphicPlane::initialized() const {
1725 return mHw ? true : false;
1726}
1727
1728void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1729 mHw = hw;
1730}
1731
1732void GraphicPlane::setTransform(const Transform& tr) {
1733 mTransform = tr;
1734 mGlobalTransform = mOrientationTransform * mTransform;
1735}
1736
1737status_t GraphicPlane::orientationToTransfrom(
1738 int orientation, int w, int h, Transform* tr)
1739{
1740 float a, b, c, d, x, y;
1741 switch (orientation) {
1742 case ISurfaceComposer::eOrientationDefault:
1743 a=1; b=0; c=0; d=1; x=0; y=0;
1744 break;
1745 case ISurfaceComposer::eOrientation90:
1746 a=0; b=-1; c=1; d=0; x=w; y=0;
1747 break;
1748 case ISurfaceComposer::eOrientation180:
1749 a=-1; b=0; c=0; d=-1; x=w; y=h;
1750 break;
1751 case ISurfaceComposer::eOrientation270:
1752 a=0; b=1; c=-1; d=0; x=0; y=h;
1753 break;
1754 default:
1755 return BAD_VALUE;
1756 }
1757 tr->set(a, b, c, d);
1758 tr->set(x, y);
1759 return NO_ERROR;
1760}
1761
1762status_t GraphicPlane::setOrientation(int orientation)
1763{
1764 const DisplayHardware& hw(displayHardware());
1765 const float w = hw.getWidth();
1766 const float h = hw.getHeight();
1767
1768 if (orientation == ISurfaceComposer::eOrientationDefault) {
1769 // make sure the default orientation is optimal
1770 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001771 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772 mGlobalTransform = mTransform;
1773 return NO_ERROR;
1774 }
1775
1776 // If the rotation can be handled in hardware, this is where
1777 // the magic should happen.
1778 if (UNLIKELY(orientation == 42)) {
1779 float a, b, c, d, x, y;
1780 const float r = (3.14159265f / 180.0f) * 42.0f;
1781 const float si = sinf(r);
1782 const float co = cosf(r);
1783 a=co; b=-si; c=si; d=co;
1784 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1785 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1786 mOrientationTransform.set(a, b, c, d);
1787 mOrientationTransform.set(x, y);
1788 } else {
1789 GraphicPlane::orientationToTransfrom(orientation, w, h,
1790 &mOrientationTransform);
1791 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001792 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 mGlobalTransform = mOrientationTransform * mTransform;
1794 return NO_ERROR;
1795}
1796
1797const DisplayHardware& GraphicPlane::displayHardware() const {
1798 return *mHw;
1799}
1800
1801const Transform& GraphicPlane::transform() const {
1802 return mGlobalTransform;
1803}
1804
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001805EGLDisplay GraphicPlane::getEGLDisplay() const {
1806 return mHw->getEGLDisplay();
1807}
1808
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001809// ---------------------------------------------------------------------------
1810
1811}; // namespace android