blob: 106af3f8bcf9c584210057eef16fb32aa74e361c [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
Mathias Agopianb8a55602009-06-26 19:06:36 -0700806
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807void SurfaceFlinger::handleRepaint()
808{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700809 // compute the invalid region
810 mInvalidRegion.orSelf(mDirtyRegion);
811 if (mInvalidRegion.isEmpty()) {
812 // nothing to do
813 return;
814 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815
816 if (UNLIKELY(mDebugRegion)) {
817 debugFlashRegions();
818 }
819
Mathias Agopianb8a55602009-06-26 19:06:36 -0700820 // set the frame buffer
821 const DisplayHardware& hw(graphicPlane(0).displayHardware());
822 glMatrixMode(GL_MODELVIEW);
823 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824
825 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700826 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
827 (flags & DisplayHardware::BUFFER_PRESERVED))
828 {
829 // we can redraw only what's dirty
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 } else {
831 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700832 // we need to redraw the rectangle that will be updated
833 // (pushed to the framebuffer).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 mDirtyRegion.set(mInvalidRegion.bounds());
835 } else {
836 // we need to redraw everything
837 mDirtyRegion.set(hw.bounds());
838 mInvalidRegion = mDirtyRegion;
839 }
840 }
841
842 // compose all surfaces
843 composeSurfaces(mDirtyRegion);
844
845 // clear the dirty regions
846 mDirtyRegion.clear();
847}
848
849void SurfaceFlinger::composeSurfaces(const Region& dirty)
850{
851 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
852 // should never happen unless the window manager has a bug
853 // draw something...
854 drawWormhole();
855 }
856 const SurfaceFlinger& flinger(*this);
857 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
858 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700859 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700861 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 const Region& visibleRegion(layer->visibleRegionScreen);
863 if (!visibleRegion.isEmpty()) {
864 const Region clip(dirty.intersect(visibleRegion));
865 if (!clip.isEmpty()) {
866 layer->draw(clip);
867 }
868 }
869 }
870}
871
872void SurfaceFlinger::unlockClients()
873{
874 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
875 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700876 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700878 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 layer->finishPageFlip();
880 }
881}
882
Mathias Agopianf9d93272009-06-19 17:00:27 -0700883void SurfaceFlinger::scheduleBroadcast(const sp<Client>& client)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884{
885 if (mLastScheduledBroadcast != client) {
886 mLastScheduledBroadcast = client;
887 mScheduledBroadcasts.add(client);
888 }
889}
890
891void SurfaceFlinger::executeScheduledBroadcasts()
892{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700893 SortedVector< wp<Client> >& list(mScheduledBroadcasts);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 size_t count = list.size();
895 while (count--) {
Mathias Agopianf9d93272009-06-19 17:00:27 -0700896 sp<Client> client = list[count].promote();
897 if (client != 0) {
898 per_client_cblk_t* const cblk = client->ctrlblk;
899 if (cblk->lock.tryLock() == NO_ERROR) {
900 cblk->cv.broadcast();
901 list.removeAt(count);
902 cblk->lock.unlock();
903 } else {
904 // schedule another round
905 LOGW("executeScheduledBroadcasts() skipped, "
906 "contention on the client. We'll try again later...");
907 signalDelayedEvent(ms2ns(4));
908 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 }
910 }
911 mLastScheduledBroadcast = 0;
912}
913
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914void SurfaceFlinger::debugFlashRegions()
915{
Mathias Agopian0926f502009-05-04 14:17:04 -0700916 const DisplayHardware& hw(graphicPlane(0).displayHardware());
917 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700918
919 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
920 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700921 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
922 mDirtyRegion.bounds() : hw.bounds());
923 composeSurfaces(repaint);
924 }
925
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 glDisable(GL_TEXTURE_2D);
927 glDisable(GL_BLEND);
928 glDisable(GL_DITHER);
929 glDisable(GL_SCISSOR_TEST);
930
Mathias Agopian0926f502009-05-04 14:17:04 -0700931 static int toggle = 0;
932 toggle = 1 - toggle;
933 if (toggle) {
934 glColor4x(0x10000, 0, 0x10000, 0x10000);
935 } else {
936 glColor4x(0x10000, 0x10000, 0, 0x10000);
937 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938
Mathias Agopian20f68782009-05-11 00:03:41 -0700939 Region::const_iterator it = mDirtyRegion.begin();
940 Region::const_iterator const end = mDirtyRegion.end();
941 while (it != end) {
942 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 GLfloat vertices[][2] = {
944 { r.left, r.top },
945 { r.left, r.bottom },
946 { r.right, r.bottom },
947 { r.right, r.top }
948 };
949 glVertexPointer(2, GL_FLOAT, 0, vertices);
950 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
951 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700952
Mathias Agopianb8a55602009-06-26 19:06:36 -0700953 if (mInvalidRegion.isEmpty()) {
954 mDirtyRegion.dump("mDirtyRegion");
955 mInvalidRegion.dump("mInvalidRegion");
956 }
957 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958
959 if (mDebugRegion > 1)
960 usleep(mDebugRegion * 1000);
961
962 glEnable(GL_SCISSOR_TEST);
963 //mDirtyRegion.dump("mDirtyRegion");
964}
965
966void SurfaceFlinger::drawWormhole() const
967{
968 const Region region(mWormholeRegion.intersect(mDirtyRegion));
969 if (region.isEmpty())
970 return;
971
972 const DisplayHardware& hw(graphicPlane(0).displayHardware());
973 const int32_t width = hw.getWidth();
974 const int32_t height = hw.getHeight();
975
976 glDisable(GL_BLEND);
977 glDisable(GL_DITHER);
978
979 if (LIKELY(!mDebugBackground)) {
980 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700981 Region::const_iterator it = region.begin();
982 Region::const_iterator const end = region.end();
983 while (it != end) {
984 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 const GLint sy = height - (r.top + r.height());
986 glScissor(r.left, sy, r.width(), r.height());
987 glClear(GL_COLOR_BUFFER_BIT);
988 }
989 } else {
990 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
991 { width, height }, { 0, height } };
992 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
993 glVertexPointer(2, GL_SHORT, 0, vertices);
994 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
995 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
996 glEnable(GL_TEXTURE_2D);
997 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
998 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
999 glMatrixMode(GL_TEXTURE);
1000 glLoadIdentity();
1001 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001002 Region::const_iterator it = region.begin();
1003 Region::const_iterator const end = region.end();
1004 while (it != end) {
1005 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 const GLint sy = height - (r.top + r.height());
1007 glScissor(r.left, sy, r.width(), r.height());
1008 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1009 }
1010 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1011 }
1012}
1013
1014void SurfaceFlinger::debugShowFPS() const
1015{
1016 static int mFrameCount;
1017 static int mLastFrameCount = 0;
1018 static nsecs_t mLastFpsTime = 0;
1019 static float mFps = 0;
1020 mFrameCount++;
1021 nsecs_t now = systemTime();
1022 nsecs_t diff = now - mLastFpsTime;
1023 if (diff > ms2ns(250)) {
1024 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1025 mLastFpsTime = now;
1026 mLastFrameCount = mFrameCount;
1027 }
1028 // XXX: mFPS has the value we want
1029 }
1030
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001031status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032{
1033 Mutex::Autolock _l(mStateLock);
1034 addLayer_l(layer);
1035 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1036 return NO_ERROR;
1037}
1038
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001039status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040{
1041 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001042 status_t err = purgatorizeLayer_l(layer);
1043 if (err == NO_ERROR)
1044 setTransactionFlags(eTransactionNeeded);
1045 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046}
1047
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001048status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049{
1050 layer->forceVisibilityTransaction();
1051 setTransactionFlags(eTraversalNeeded);
1052 return NO_ERROR;
1053}
1054
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001055status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056{
1057 ssize_t i = mCurrentState.layersSortedByZ.add(
1058 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001059 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1060 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061 mLayerMap.add(lbc->serverIndex(), lbc);
1062 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 return NO_ERROR;
1064}
1065
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067{
1068 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1069 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001070 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001071 sp<LayerBaseClient> layer =
1072 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001073 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 mLayerMap.removeItem(layer->serverIndex());
1075 }
1076 return NO_ERROR;
1077 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001078 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079}
1080
Mathias Agopian9a112062009-04-17 19:36:26 -07001081status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1082{
1083 // First add the layer to the purgatory list, which makes sure it won't
1084 // go away, then remove it from the main list (through a transaction).
1085 ssize_t err = removeLayer_l(layerBase);
1086 if (err >= 0) {
1087 mLayerPurgatory.add(layerBase);
1088 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001089 // it's possible that we don't find a layer, because it might
1090 // have been destroyed already -- this is not technically an error
1091 // from the user because there is a race between BClient::destroySurface(),
1092 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001093 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1094}
1095
1096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097void SurfaceFlinger::free_resources_l()
1098{
1099 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001100 mLayersRemoved = false;
1101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001103 SortedVector< wp<Client> >& scheduledBroadcasts(mScheduledBroadcasts);
1104 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 const size_t count = disconnectedClients.size();
1106 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001107 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 // if this client is the scheduled broadcast list,
1109 // remove it from there (and we don't need to signal it
1110 // since it is dead).
1111 int32_t index = scheduledBroadcasts.indexOf(client);
1112 if (index >= 0) {
1113 scheduledBroadcasts.removeItemsAt(index);
1114 }
1115 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 }
1117 disconnectedClients.clear();
1118}
1119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1121{
1122 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1123}
1124
1125uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1126{
1127 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1128 if ((old & flags)==0) { // wake the server up
1129 if (delay > 0) {
1130 signalDelayedEvent(delay);
1131 } else {
1132 signalEvent();
1133 }
1134 }
1135 return old;
1136}
1137
1138void SurfaceFlinger::openGlobalTransaction()
1139{
1140 android_atomic_inc(&mTransactionCount);
1141}
1142
1143void SurfaceFlinger::closeGlobalTransaction()
1144{
1145 if (android_atomic_dec(&mTransactionCount) == 1) {
1146 signalEvent();
1147 }
1148}
1149
1150status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1151{
1152 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1153 return BAD_VALUE;
1154
1155 Mutex::Autolock _l(mStateLock);
1156 mCurrentState.freezeDisplay = 1;
1157 setTransactionFlags(eTransactionNeeded);
1158
1159 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001160 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 return NO_ERROR;
1162}
1163
1164status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1165{
1166 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1167 return BAD_VALUE;
1168
1169 Mutex::Autolock _l(mStateLock);
1170 mCurrentState.freezeDisplay = 0;
1171 setTransactionFlags(eTransactionNeeded);
1172
1173 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001174 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 return NO_ERROR;
1176}
1177
Mathias Agopianc08731e2009-03-27 18:11:38 -07001178int SurfaceFlinger::setOrientation(DisplayID dpy,
1179 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180{
1181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1182 return BAD_VALUE;
1183
1184 Mutex::Autolock _l(mStateLock);
1185 if (mCurrentState.orientation != orientation) {
1186 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001187 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 mCurrentState.orientation = orientation;
1189 setTransactionFlags(eTransactionNeeded);
1190 mTransactionCV.wait(mStateLock);
1191 } else {
1192 orientation = BAD_VALUE;
1193 }
1194 }
1195 return orientation;
1196}
1197
1198sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1199 ISurfaceFlingerClient::surface_data_t* params,
1200 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1201 uint32_t flags)
1202{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001203 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 sp<LayerBaseClient::Surface> surfaceHandle;
1205 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001206 sp<Client> client = mClientsMap.valueFor(clientId);
1207 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1209 return surfaceHandle;
1210 }
1211
1212 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001213 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1215 LOGE("createSurface() failed, generateId = %d", id);
1216 return surfaceHandle;
1217 }
1218
1219 switch (flags & eFXSurfaceMask) {
1220 case eFXSurfaceNormal:
1221 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001222 layer = createPushBuffersSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 } else {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001224 layer = createNormalSurfaceLocked(client, d, id, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 }
1226 break;
1227 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001228 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 break;
1230 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001231 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 break;
1233 }
1234
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001235 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 setTransactionFlags(eTransactionNeeded);
1237 surfaceHandle = layer->getSurface();
1238 if (surfaceHandle != 0)
1239 surfaceHandle->getSurfaceData(params);
1240 }
1241
1242 return surfaceHandle;
1243}
1244
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001245sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001246 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
1248{
1249 // initialize the surfaces
1250 switch (format) { // TODO: take h/w into account
1251 case PIXEL_FORMAT_TRANSPARENT:
1252 case PIXEL_FORMAT_TRANSLUCENT:
1253 format = PIXEL_FORMAT_RGBA_8888;
1254 break;
1255 case PIXEL_FORMAT_OPAQUE:
1256 format = PIXEL_FORMAT_RGB_565;
1257 break;
1258 }
1259
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001260 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001261 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 if (LIKELY(err == NO_ERROR)) {
1263 layer->initStates(w, h, flags);
1264 addLayer_l(layer);
1265 } else {
1266 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001267 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 }
1269 return layer;
1270}
1271
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001272sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001273 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1275{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001276 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 layer->initStates(w, h, flags);
1278 addLayer_l(layer);
1279 return layer;
1280}
1281
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001282sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001283 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1285{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001286 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 layer->initStates(w, h, flags);
1288 addLayer_l(layer);
1289 return layer;
1290}
1291
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001292sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001293 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1295{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001296 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 layer->initStates(w, h, flags);
1298 addLayer_l(layer);
1299 return layer;
1300}
1301
Mathias Agopian9a112062009-04-17 19:36:26 -07001302status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303{
Mathias Agopian9a112062009-04-17 19:36:26 -07001304 /*
1305 * called by the window manager, when a surface should be marked for
1306 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001307 *
1308 * The surface is removed from the current and drawing lists, but placed
1309 * in the purgatory queue, so it's not destroyed right-away (we need
1310 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001311 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001312
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001313 Mutex::Autolock _l(mStateLock);
1314 sp<LayerBaseClient> layer = getLayerUser_l(index);
1315 status_t err = purgatorizeLayer_l(layer);
1316 if (err == NO_ERROR) {
1317 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001318 }
1319 return err;
1320}
1321
1322status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1323{
Mathias Agopian3d579642009-06-04 18:46:21 -07001324 /* called by ~ISurface() when all references are gone */
Mathias Agopian9a112062009-04-17 19:36:26 -07001325
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001326 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001327 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001328 sp<LayerBaseClient> layer;
1329 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001330 MessageDestroySurface(
1331 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1332 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001333 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001334 sp<LayerBaseClient> l(layer);
1335 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001336 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001337 // remove the layer from the current list -- chances are that it's
1338 // not in the list anyway, because it should have been removed
1339 // already upon request of the client (eg: window manager).
1340 // However, a buggy client could have not done that.
1341 // Since we know we don't have any more clients, we don't need
1342 // to use the purgatory.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001343 status_t err = flinger->removeLayer_l(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001344 if (err == NAME_NOT_FOUND) {
1345 // The surface wasn't in the current list, which means it was
1346 // removed already, which means it is in the purgatory,
1347 // and need to be removed from there.
1348 // This needs to happen from the main thread since its dtor
1349 // must run from there (b/c of OpenGL ES). Additionally, we
1350 // can't really acquire our internal lock from
1351 // destroySurface() -- see postMessage() below.
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001352 ssize_t idx = flinger->mLayerPurgatory.remove(l);
Mathias Agopian3d579642009-06-04 18:46:21 -07001353 LOGE_IF(idx < 0,
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001354 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian3d579642009-06-04 18:46:21 -07001355 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001356 return true;
1357 }
1358 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001359
1360 // It's better to not acquire our internal lock here, because it's hard
1361 // to predict that it's not going to be already taken when ~Surface()
1362 // is called.
1363
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001364 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 return NO_ERROR;
1366}
1367
1368status_t SurfaceFlinger::setClientState(
1369 ClientID cid,
1370 int32_t count,
1371 const layer_state_t* states)
1372{
1373 Mutex::Autolock _l(mStateLock);
1374 uint32_t flags = 0;
1375 cid <<= 16;
1376 for (int i=0 ; i<count ; i++) {
1377 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001378 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001379 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 if (what & ePositionChanged) {
1382 if (layer->setPosition(s.x, s.y))
1383 flags |= eTraversalNeeded;
1384 }
1385 if (what & eLayerChanged) {
1386 if (layer->setLayer(s.z)) {
1387 mCurrentState.layersSortedByZ.reorder(
1388 layer, &Layer::compareCurrentStateZ);
1389 // we need traversal (state changed)
1390 // AND transaction (list changed)
1391 flags |= eTransactionNeeded|eTraversalNeeded;
1392 }
1393 }
1394 if (what & eSizeChanged) {
1395 if (layer->setSize(s.w, s.h))
1396 flags |= eTraversalNeeded;
1397 }
1398 if (what & eAlphaChanged) {
1399 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eMatrixChanged) {
1403 if (layer->setMatrix(s.matrix))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eTransparentRegionChanged) {
1407 if (layer->setTransparentRegionHint(s.transparentRegion))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eVisibilityChanged) {
1411 if (layer->setFlags(s.flags, s.mask))
1412 flags |= eTraversalNeeded;
1413 }
1414 }
1415 }
1416 if (flags) {
1417 setTransactionFlags(flags);
1418 }
1419 return NO_ERROR;
1420}
1421
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001422sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001424 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1425 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426}
1427
1428void SurfaceFlinger::screenReleased(int dpy)
1429{
1430 // this may be called by a signal handler, we can't do too much in here
1431 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1432 signalEvent();
1433}
1434
1435void SurfaceFlinger::screenAcquired(int dpy)
1436{
1437 // this may be called by a signal handler, we can't do too much in here
1438 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1439 signalEvent();
1440}
1441
1442status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1443{
1444 const size_t SIZE = 1024;
1445 char buffer[SIZE];
1446 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001447 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 snprintf(buffer, SIZE, "Permission Denial: "
1449 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1450 IPCThreadState::self()->getCallingPid(),
1451 IPCThreadState::self()->getCallingUid());
1452 result.append(buffer);
1453 } else {
1454 Mutex::Autolock _l(mStateLock);
1455 size_t s = mClientsMap.size();
1456 char name[64];
1457 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001458 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 sprintf(name, " Client (id=0x%08x)", client->cid);
1460 client->dump(name);
1461 }
1462 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1463 const size_t count = currentLayers.size();
1464 for (size_t i=0 ; i<count ; i++) {
1465 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001466 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 const Layer::State& s = layer->drawingState();
1468 snprintf(buffer, SIZE,
1469 "+ %s %p\n"
1470 " "
1471 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1472 "needsBlending=%1d, invalidate=%1d, "
1473 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001474 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 s.z, layer->tx(), layer->ty(), s.w, s.h,
1476 layer->needsBlending(), layer->contentDirty,
1477 s.alpha, s.flags,
1478 s.transform[0], s.transform[1],
1479 s.transform[2], s.transform[3]);
1480 result.append(buffer);
1481 buffer[0] = 0;
1482 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001483 sp<LayerBaseClient> lbc =
1484 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1485 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001486 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 snprintf(buffer, SIZE,
1488 " "
1489 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001490 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 lbc->getIdentity());
1492 }
1493 result.append(buffer);
1494 buffer[0] = 0;
1495 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001496 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1497 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 const LayerBitmap& buf0(l->getBuffer(0));
1499 const LayerBitmap& buf1(l->getBuffer(1));
1500 snprintf(buffer, SIZE,
1501 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001502 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 " freezeLock=%p, swapState=0x%08x\n",
1504 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001505 buf0.getWidth(), buf0.getHeight(),
1506 buf0.getBuffer()->getStride(),
1507 buf1.getWidth(), buf1.getHeight(),
1508 buf1.getBuffer()->getStride(),
1509 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 l->lcblk->swapState);
1511 }
1512 result.append(buffer);
1513 buffer[0] = 0;
1514 s.transparentRegion.dump(result, "transparentRegion");
1515 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1516 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1517 }
1518 mWormholeRegion.dump(result, "WormholeRegion");
1519 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1520 snprintf(buffer, SIZE,
1521 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1522 mFreezeDisplay?"yes":"no", mFreezeCount,
1523 mCurrentState.orientation, hw.canDraw());
1524 result.append(buffer);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001525 snprintf(buffer, SIZE, " purgatory size: %d, client count: %d\n",
1526 mLayerPurgatory.size(), mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001527 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001528 const BufferAllocator& alloc(BufferAllocator::get());
1529 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530 }
1531 write(fd, result.string(), result.size());
1532 return NO_ERROR;
1533}
1534
1535status_t SurfaceFlinger::onTransact(
1536 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1537{
1538 switch (code) {
1539 case CREATE_CONNECTION:
1540 case OPEN_GLOBAL_TRANSACTION:
1541 case CLOSE_GLOBAL_TRANSACTION:
1542 case SET_ORIENTATION:
1543 case FREEZE_DISPLAY:
1544 case UNFREEZE_DISPLAY:
1545 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 {
1547 // codes that require permission check
1548 IPCThreadState* ipc = IPCThreadState::self();
1549 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001550 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001551 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1552 LOGE("Permission Denial: "
1553 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1554 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555 }
1556 }
1557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1559 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001560 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001561 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1562 IPCThreadState* ipc = IPCThreadState::self();
1563 const int pid = ipc->getCallingPid();
1564 const int uid = ipc->getCallingUid();
1565 LOGE("Permission Denial: "
1566 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 return PERMISSION_DENIED;
1568 }
1569 int n;
1570 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001571 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001572 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001573 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 return NO_ERROR;
1575 case 1002: // SHOW_UPDATES
1576 n = data.readInt32();
1577 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1578 return NO_ERROR;
1579 case 1003: // SHOW_BACKGROUND
1580 n = data.readInt32();
1581 mDebugBackground = n ? 1 : 0;
1582 return NO_ERROR;
1583 case 1004:{ // repaint everything
1584 Mutex::Autolock _l(mStateLock);
1585 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1586 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1587 signalEvent();
1588 }
1589 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 case 1007: // set mFreezeCount
1591 mFreezeCount = data.readInt32();
1592 return NO_ERROR;
1593 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001594 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595 reply->writeInt32(0);
1596 reply->writeInt32(mDebugRegion);
1597 reply->writeInt32(mDebugBackground);
1598 return NO_ERROR;
1599 case 1013: {
1600 Mutex::Autolock _l(mStateLock);
1601 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1602 reply->writeInt32(hw.getPageFlipCount());
1603 }
1604 return NO_ERROR;
1605 }
1606 }
1607 return err;
1608}
1609
1610// ---------------------------------------------------------------------------
1611#if 0
1612#pragma mark -
1613#endif
1614
1615Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1616 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1617{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 const int pgsize = getpagesize();
1619 const int cblksize=((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1620 mCblkHeap = new MemoryDealer(cblksize);
1621 mCblkMemory = mCblkHeap->allocate(cblksize);
1622 if (mCblkMemory != 0) {
1623 ctrlblk = static_cast<per_client_cblk_t *>(mCblkMemory->pointer());
1624 if (ctrlblk) { // construct the shared structure in-place.
1625 new(ctrlblk) per_client_cblk_t;
1626 }
1627 }
1628}
1629
1630Client::~Client() {
1631 if (ctrlblk) {
1632 const int pgsize = getpagesize();
1633 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1634 }
1635}
1636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637int32_t Client::generateId(int pid)
1638{
1639 const uint32_t i = clz( ~mBitmap );
1640 if (i >= NUM_LAYERS_MAX) {
1641 return NO_MEMORY;
1642 }
1643 mPid = pid;
1644 mInUse.add(uint8_t(i));
1645 mBitmap |= 1<<(31-i);
1646 return i;
1647}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001648
1649status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650{
1651 ssize_t idx = mInUse.indexOf(id);
1652 if (idx < 0)
1653 return NAME_NOT_FOUND;
1654 return mLayers.insertAt(layer, idx);
1655}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657void Client::free(int32_t id)
1658{
1659 ssize_t idx = mInUse.remove(uint8_t(id));
1660 if (idx >= 0) {
1661 mBitmap &= ~(1<<(31-id));
1662 mLayers.removeItemsAt(idx);
1663 }
1664}
1665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666bool Client::isValid(int32_t i) const {
1667 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1668}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001669
1670sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1671 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001673 if (idx >= 0) {
1674 lbc = mLayers[idx].promote();
1675 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1676 }
1677 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678}
1679
1680void Client::dump(const char* what)
1681{
1682}
1683
1684// ---------------------------------------------------------------------------
1685#if 0
1686#pragma mark -
1687#endif
1688
1689BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemory>& cblk)
1690 : mId(cid), mFlinger(flinger), mCblk(cblk)
1691{
1692}
1693
1694BClient::~BClient() {
1695 // destroy all resources attached to this client
1696 mFlinger->destroyConnection(mId);
1697}
1698
1699void BClient::getControlBlocks(sp<IMemory>* ctrl) const {
1700 *ctrl = mCblk;
1701}
1702
1703sp<ISurface> BClient::createSurface(
1704 ISurfaceFlingerClient::surface_data_t* params, int pid,
1705 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1706 uint32_t flags)
1707{
1708 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1709}
1710
1711status_t BClient::destroySurface(SurfaceID sid)
1712{
1713 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001714 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715}
1716
1717status_t BClient::setState(int32_t count, const layer_state_t* states)
1718{
1719 return mFlinger->setClientState(mId, count, states);
1720}
1721
1722// ---------------------------------------------------------------------------
1723
1724GraphicPlane::GraphicPlane()
1725 : mHw(0)
1726{
1727}
1728
1729GraphicPlane::~GraphicPlane() {
1730 delete mHw;
1731}
1732
1733bool GraphicPlane::initialized() const {
1734 return mHw ? true : false;
1735}
1736
1737void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1738 mHw = hw;
1739}
1740
1741void GraphicPlane::setTransform(const Transform& tr) {
1742 mTransform = tr;
1743 mGlobalTransform = mOrientationTransform * mTransform;
1744}
1745
1746status_t GraphicPlane::orientationToTransfrom(
1747 int orientation, int w, int h, Transform* tr)
1748{
1749 float a, b, c, d, x, y;
1750 switch (orientation) {
1751 case ISurfaceComposer::eOrientationDefault:
1752 a=1; b=0; c=0; d=1; x=0; y=0;
1753 break;
1754 case ISurfaceComposer::eOrientation90:
1755 a=0; b=-1; c=1; d=0; x=w; y=0;
1756 break;
1757 case ISurfaceComposer::eOrientation180:
1758 a=-1; b=0; c=0; d=-1; x=w; y=h;
1759 break;
1760 case ISurfaceComposer::eOrientation270:
1761 a=0; b=1; c=-1; d=0; x=0; y=h;
1762 break;
1763 default:
1764 return BAD_VALUE;
1765 }
1766 tr->set(a, b, c, d);
1767 tr->set(x, y);
1768 return NO_ERROR;
1769}
1770
1771status_t GraphicPlane::setOrientation(int orientation)
1772{
1773 const DisplayHardware& hw(displayHardware());
1774 const float w = hw.getWidth();
1775 const float h = hw.getHeight();
1776
1777 if (orientation == ISurfaceComposer::eOrientationDefault) {
1778 // make sure the default orientation is optimal
1779 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001780 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 mGlobalTransform = mTransform;
1782 return NO_ERROR;
1783 }
1784
1785 // If the rotation can be handled in hardware, this is where
1786 // the magic should happen.
1787 if (UNLIKELY(orientation == 42)) {
1788 float a, b, c, d, x, y;
1789 const float r = (3.14159265f / 180.0f) * 42.0f;
1790 const float si = sinf(r);
1791 const float co = cosf(r);
1792 a=co; b=-si; c=si; d=co;
1793 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1794 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1795 mOrientationTransform.set(a, b, c, d);
1796 mOrientationTransform.set(x, y);
1797 } else {
1798 GraphicPlane::orientationToTransfrom(orientation, w, h,
1799 &mOrientationTransform);
1800 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001801 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802 mGlobalTransform = mOrientationTransform * mTransform;
1803 return NO_ERROR;
1804}
1805
1806const DisplayHardware& GraphicPlane::displayHardware() const {
1807 return *mHw;
1808}
1809
1810const Transform& GraphicPlane::transform() const {
1811 return mGlobalTransform;
1812}
1813
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001814EGLDisplay GraphicPlane::getEGLDisplay() const {
1815 return mHw->getEGLDisplay();
1816}
1817
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818// ---------------------------------------------------------------------------
1819
1820}; // namespace android