blob: 5ac1cfd932c992850fdfbb27b33dbac1e34a5190 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
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
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700220sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700222 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223}
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 =
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700241 new BClient(this, token, client->getControlBlockMemory());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304static inline uint16_t pack565(int r, int g, int b) {
305 return (r<<11)|(g<<5)|b;
306}
307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308status_t SurfaceFlinger::readyToRun()
309{
310 LOGI( "SurfaceFlinger's main thread ready to run. "
311 "Initializing graphics H/W...");
312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 // we only support one display currently
314 int dpy = 0;
315
316 {
317 // initialize the main display
318 GraphicPlane& plane(graphicPlane(dpy));
319 DisplayHardware* const hw = new DisplayHardware(this, dpy);
320 plane.setDisplayHardware(hw);
321 }
322
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700323 // create the shared control-block
324 mServerHeap = new MemoryHeapBase(4096,
325 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
326 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
327
328 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
329 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
330
331 new(mServerCblk) surface_flinger_cblk_t;
332
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 // initialize primary screen
334 // (other display should be initialized in the same manner, but
335 // asynchronously, as they could come and go. None of this is supported
336 // yet).
337 const GraphicPlane& plane(graphicPlane(dpy));
338 const DisplayHardware& hw = plane.displayHardware();
339 const uint32_t w = hw.getWidth();
340 const uint32_t h = hw.getHeight();
341 const uint32_t f = hw.getFormat();
342 hw.makeCurrent();
343
344 // initialize the shared control block
345 mServerCblk->connected |= 1<<dpy;
346 display_cblk_t* dcblk = mServerCblk->displays + dpy;
347 memset(dcblk, 0, sizeof(display_cblk_t));
348 dcblk->w = w;
349 dcblk->h = h;
350 dcblk->format = f;
351 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
352 dcblk->xdpi = hw.getDpiX();
353 dcblk->ydpi = hw.getDpiY();
354 dcblk->fps = hw.getRefreshRate();
355 dcblk->density = hw.getDensity();
356 asm volatile ("":::"memory");
357
358 // Initialize OpenGL|ES
359 glActiveTexture(GL_TEXTURE0);
360 glBindTexture(GL_TEXTURE_2D, 0);
361 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
362 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
363 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
364 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
365 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
366 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
367 glPixelStorei(GL_PACK_ALIGNMENT, 4);
368 glEnableClientState(GL_VERTEX_ARRAY);
369 glEnable(GL_SCISSOR_TEST);
370 glShadeModel(GL_FLAT);
371 glDisable(GL_DITHER);
372 glDisable(GL_CULL_FACE);
373
374 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
375 const uint16_t g1 = pack565(0x17,0x2f,0x17);
376 const uint16_t textureData[4] = { g0, g1, g1, g0 };
377 glGenTextures(1, &mWormholeTexName);
378 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
379 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
380 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
381 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
382 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
383 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
384 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
385
386 glViewport(0, 0, w, h);
387 glMatrixMode(GL_PROJECTION);
388 glLoadIdentity();
389 glOrthof(0, w, h, 0, 0, 1);
390
391 LayerDim::initDimmer(this, w, h);
392
393 mReadyToRunBarrier.open();
394
395 /*
396 * We're now ready to accept clients...
397 */
398
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700399 // start boot animation
400 property_set("ctl.start", "bootanim");
401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 return NO_ERROR;
403}
404
405// ----------------------------------------------------------------------------
406#if 0
407#pragma mark -
408#pragma mark Events Handler
409#endif
410
411void SurfaceFlinger::waitForEvent()
412{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700413 while (true) {
414 nsecs_t timeout = -1;
415 if (UNLIKELY(isFrozen())) {
416 // wait 5 seconds
417 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
418 const nsecs_t now = systemTime();
419 if (mFreezeDisplayTime == 0) {
420 mFreezeDisplayTime = now;
421 }
422 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
423 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700424 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700425
Mathias Agopianb6683b52009-04-28 03:17:50 -0700426 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700427 if (msg != 0) {
428 mFreezeDisplayTime = 0;
429 switch (msg->what) {
430 case MessageQueue::INVALIDATE:
431 // invalidate message, just return to the main loop
432 return;
433 }
434 } else {
435 // we timed out
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 if (isFrozen()) {
437 // we timed out and are still frozen
438 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
439 mFreezeDisplay, mFreezeCount);
440 mFreezeCount = 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700441 mFreezeDisplay = false;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700442 return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 }
444 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 }
446}
447
448void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700449 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450}
451
452void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700453 // this is the IPC call
454 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455}
456
457void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
458{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700459 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460}
461
462// ----------------------------------------------------------------------------
463#if 0
464#pragma mark -
465#pragma mark Main loop
466#endif
467
468bool SurfaceFlinger::threadLoop()
469{
470 waitForEvent();
471
472 // check for transactions
473 if (UNLIKELY(mConsoleSignals)) {
474 handleConsoleEvents();
475 }
476
477 if (LIKELY(mTransactionCount == 0)) {
478 // if we're in a global transaction, don't do anything.
479 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
480 uint32_t transactionFlags = getTransactionFlags(mask);
481 if (LIKELY(transactionFlags)) {
482 handleTransaction(transactionFlags);
483 }
484 }
485
486 // post surfaces (if needed)
487 handlePageFlip();
488
489 const DisplayHardware& hw(graphicPlane(0).displayHardware());
490 if (LIKELY(hw.canDraw())) {
491 // repaint the framebuffer (if needed)
492 handleRepaint();
493
494 // release the clients before we flip ('cause flip might block)
495 unlockClients();
496 executeScheduledBroadcasts();
497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 postFramebuffer();
499 } else {
500 // pretend we did the post
501 unlockClients();
502 executeScheduledBroadcasts();
503 usleep(16667); // 60 fps period
504 }
505 return true;
506}
507
508void SurfaceFlinger::postFramebuffer()
509{
Mathias Agopian0926f502009-05-04 14:17:04 -0700510 if (isFrozen()) {
511 // we are not allowed to draw, but pause a bit to make sure
512 // apps don't end up using the whole CPU, if they depend on
513 // surfaceflinger for synchronization.
514 usleep(8333); // 8.3ms ~ 120fps
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 return;
516 }
517
518 if (!mInvalidRegion.isEmpty()) {
519 const DisplayHardware& hw(graphicPlane(0).displayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 }
523}
524
525void SurfaceFlinger::handleConsoleEvents()
526{
527 // something to do with the console
528 const DisplayHardware& hw = graphicPlane(0).displayHardware();
529
530 int what = android_atomic_and(0, &mConsoleSignals);
531 if (what & eConsoleAcquired) {
532 hw.acquireScreen();
533 }
534
535 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700536 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 hw.releaseScreen();
539 }
540
541 if (what & eConsoleReleased) {
542 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 hw.releaseScreen();
544 } else {
545 mDeferReleaseConsole = true;
546 }
547 }
548
549 mDirtyRegion.set(hw.bounds());
550}
551
552void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
553{
Mathias Agopian3d579642009-06-04 18:46:21 -0700554 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555
Mathias Agopian3d579642009-06-04 18:46:21 -0700556 { // scope for the lock
557 Mutex::Autolock _l(mStateLock);
558 handleTransactionLocked(transactionFlags, ditchedLayers);
559 }
560
561 // do this without lock held
562 const size_t count = ditchedLayers.size();
563 for (size_t i=0 ; i<count ; i++) {
564 //LOGD("ditching layer %p", ditchedLayers[i].get());
565 ditchedLayers[i]->ditch();
566 }
567}
568
569void SurfaceFlinger::handleTransactionLocked(
570 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
571{
572 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 const size_t count = currentLayers.size();
574
575 /*
576 * Traversal of the children
577 * (perform the transaction for each of them if needed)
578 */
579
580 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
581 if (layersNeedTransaction) {
582 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700583 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
585 if (!trFlags) continue;
586
587 const uint32_t flags = layer->doTransaction(0);
588 if (flags & Layer::eVisibleRegion)
589 mVisibleRegionsDirty = true;
590
591 if (flags & Layer::eRestartTransaction) {
592 // restart the transaction, but back-off a little
593 layer->setTransactionFlags(eTransactionNeeded);
594 setTransactionFlags(eTraversalNeeded, ms2ns(8));
595 }
596 }
597 }
598
599 /*
600 * Perform our own transaction if needed
601 */
602
603 if (transactionFlags & eTransactionNeeded) {
604 if (mCurrentState.orientation != mDrawingState.orientation) {
605 // the orientation has changed, recompute all visible regions
606 // and invalidate everything.
607
608 const int dpy = 0;
609 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700610 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 GraphicPlane& plane(graphicPlane(dpy));
612 plane.setOrientation(orientation);
613
614 // update the shared control block
615 const DisplayHardware& hw(plane.displayHardware());
616 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
617 dcblk->orientation = orientation;
618 if (orientation & eOrientationSwapMask) {
619 // 90 or 270 degrees orientation
620 dcblk->w = hw.getHeight();
621 dcblk->h = hw.getWidth();
622 } else {
623 dcblk->w = hw.getWidth();
624 dcblk->h = hw.getHeight();
625 }
626
627 mVisibleRegionsDirty = true;
628 mDirtyRegion.set(hw.bounds());
Mathias Agopianc08731e2009-03-27 18:11:38 -0700629 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 }
631
632 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
633 // freezing or unfreezing the display -> trigger animation if needed
634 mFreezeDisplay = mCurrentState.freezeDisplay;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 }
636
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700637 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
638 // layers have been added
639 mVisibleRegionsDirty = true;
640 }
641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 // some layers might have been removed, so
643 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644 if (mLayersRemoved) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700646 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700647 const size_t count = previousLayers.size();
648 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700649 const sp<LayerBase>& layer(previousLayers[i]);
650 if (currentLayers.indexOf( layer ) < 0) {
651 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700652 ditchedLayers.add(layer);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700653 }
654 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 }
656
657 // get rid of all resources we don't need anymore
658 // (layers and clients)
659 free_resources_l();
660 }
661
662 commitTransaction();
663}
664
665sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
666{
667 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
668}
669
670void SurfaceFlinger::computeVisibleRegions(
671 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
672{
673 const GraphicPlane& plane(graphicPlane(0));
674 const Transform& planeTransform(plane.transform());
675
676 Region aboveOpaqueLayers;
677 Region aboveCoveredLayers;
678 Region dirty;
679
680 bool secureFrameBuffer = false;
681
682 size_t i = currentLayers.size();
683 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700684 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 layer->validateVisibility(planeTransform);
686
687 // start with the whole surface at its current location
688 const Layer::State& s = layer->drawingState();
689 const Rect bounds(layer->visibleBounds());
690
691 // handle hidden surfaces by setting the visible region to empty
692 Region opaqueRegion;
693 Region visibleRegion;
694 Region coveredRegion;
695 if (UNLIKELY((s.flags & ISurfaceComposer::eLayerHidden) || !s.alpha)) {
696 visibleRegion.clear();
697 } else {
698 const bool translucent = layer->needsBlending();
699 visibleRegion.set(bounds);
700 coveredRegion = visibleRegion;
701
702 // Remove the transparent area from the visible region
703 if (translucent) {
704 visibleRegion.subtractSelf(layer->transparentRegionScreen);
705 }
706
707 // compute the opaque region
708 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
709 // the opaque region is the visible region
710 opaqueRegion = visibleRegion;
711 }
712 }
713
714 // subtract the opaque region covered by the layers above us
715 visibleRegion.subtractSelf(aboveOpaqueLayers);
716 coveredRegion.andSelf(aboveCoveredLayers);
717
718 // compute this layer's dirty region
719 if (layer->contentDirty) {
720 // we need to invalidate the whole region
721 dirty = visibleRegion;
722 // as well, as the old visible region
723 dirty.orSelf(layer->visibleRegionScreen);
724 layer->contentDirty = false;
725 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700726 /* compute the exposed region:
727 * exposed = what's VISIBLE and NOT COVERED now
728 * but was COVERED before
729 */
730 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
732 dirty.subtractSelf(aboveOpaqueLayers);
733
734 // accumulate to the screen dirty region
735 dirtyRegion.orSelf(dirty);
736
Mathias Agopian62b74442009-04-14 23:02:51 -0700737 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700739 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740
741 // Store the visible region is screen space
742 layer->setVisibleRegion(visibleRegion);
743 layer->setCoveredRegion(coveredRegion);
744
Mathias Agopian62b74442009-04-14 23:02:51 -0700745 // If a secure layer is partially visible, lock down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 if (layer->isSecure() && !visibleRegion.isEmpty()) {
747 secureFrameBuffer = true;
748 }
749 }
750
751 mSecureFrameBuffer = secureFrameBuffer;
752 opaqueRegion = aboveOpaqueLayers;
753}
754
755
756void SurfaceFlinger::commitTransaction()
757{
758 mDrawingState = mCurrentState;
759 mTransactionCV.signal();
760}
761
762void SurfaceFlinger::handlePageFlip()
763{
764 bool visibleRegions = mVisibleRegionsDirty;
765 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
766 visibleRegions |= lockPageFlip(currentLayers);
767
768 const DisplayHardware& hw = graphicPlane(0).displayHardware();
769 const Region screenRegion(hw.bounds());
770 if (visibleRegions) {
771 Region opaqueRegion;
772 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
773 mWormholeRegion = screenRegion.subtract(opaqueRegion);
774 mVisibleRegionsDirty = false;
775 }
776
777 unlockPageFlip(currentLayers);
778 mDirtyRegion.andSelf(screenRegion);
779}
780
781bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
782{
783 bool recomputeVisibleRegions = false;
784 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700785 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700787 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 layer->lockPageFlip(recomputeVisibleRegions);
789 }
790 return recomputeVisibleRegions;
791}
792
793void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
794{
795 const GraphicPlane& plane(graphicPlane(0));
796 const Transform& planeTransform(plane.transform());
797 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700798 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700800 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 layer->unlockPageFlip(planeTransform, mDirtyRegion);
802 }
803}
804
Mathias Agopianb8a55602009-06-26 19:06:36 -0700805
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806void SurfaceFlinger::handleRepaint()
807{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700808 // compute the invalid region
809 mInvalidRegion.orSelf(mDirtyRegion);
810 if (mInvalidRegion.isEmpty()) {
811 // nothing to do
812 return;
813 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814
815 if (UNLIKELY(mDebugRegion)) {
816 debugFlashRegions();
817 }
818
Mathias Agopianb8a55602009-06-26 19:06:36 -0700819 // set the frame buffer
820 const DisplayHardware& hw(graphicPlane(0).displayHardware());
821 glMatrixMode(GL_MODELVIEW);
822 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823
824 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700825 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
826 (flags & DisplayHardware::BUFFER_PRESERVED))
827 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700828 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
829 // takes a rectangle, we must make sure to update that whole
830 // rectangle in that case
831 if (flags & DisplayHardware::SWAP_RECTANGLE) {
832 // FIXME: we really should be able to pass a region to
833 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 mDirtyRegion.set(mInvalidRegion.bounds());
835 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 // in the BUFFER_PRESERVED case, obviously, we can update only
837 // what's needed and nothing more.
838 // NOTE: this is NOT a common case, as preserving the backbuffer
839 // is costly and usually involves copying the whole update back.
840 }
841 } else {
842 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
843 // We need to redraw the rectangle that will be updated
844 // (pushed to the framebuffer).
845 // This is needed because UPDATE_ON_DEMAND only takes one
846 // rectangle instead of a region (see DisplayHardware::flip())
847 mDirtyRegion.set(mInvalidRegion.bounds());
848 } else {
849 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 mDirtyRegion.set(hw.bounds());
851 mInvalidRegion = mDirtyRegion;
852 }
853 }
854
855 // compose all surfaces
856 composeSurfaces(mDirtyRegion);
857
858 // clear the dirty regions
859 mDirtyRegion.clear();
860}
861
862void SurfaceFlinger::composeSurfaces(const Region& dirty)
863{
864 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
865 // should never happen unless the window manager has a bug
866 // draw something...
867 drawWormhole();
868 }
869 const SurfaceFlinger& flinger(*this);
870 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
871 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700872 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700874 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 const Region& visibleRegion(layer->visibleRegionScreen);
876 if (!visibleRegion.isEmpty()) {
877 const Region clip(dirty.intersect(visibleRegion));
878 if (!clip.isEmpty()) {
879 layer->draw(clip);
880 }
881 }
882 }
883}
884
885void SurfaceFlinger::unlockClients()
886{
887 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
888 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700889 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700891 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 layer->finishPageFlip();
893 }
894}
895
Mathias Agopianf9d93272009-06-19 17:00:27 -0700896void SurfaceFlinger::scheduleBroadcast(const sp<Client>& client)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897{
898 if (mLastScheduledBroadcast != client) {
899 mLastScheduledBroadcast = client;
900 mScheduledBroadcasts.add(client);
901 }
902}
903
904void SurfaceFlinger::executeScheduledBroadcasts()
905{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700906 SortedVector< wp<Client> >& list(mScheduledBroadcasts);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 size_t count = list.size();
908 while (count--) {
Mathias Agopianf9d93272009-06-19 17:00:27 -0700909 sp<Client> client = list[count].promote();
910 if (client != 0) {
911 per_client_cblk_t* const cblk = client->ctrlblk;
912 if (cblk->lock.tryLock() == NO_ERROR) {
913 cblk->cv.broadcast();
914 list.removeAt(count);
915 cblk->lock.unlock();
916 } else {
917 // schedule another round
918 LOGW("executeScheduledBroadcasts() skipped, "
919 "contention on the client. We'll try again later...");
920 signalDelayedEvent(ms2ns(4));
921 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 }
923 }
924 mLastScheduledBroadcast = 0;
925}
926
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927void SurfaceFlinger::debugFlashRegions()
928{
Mathias Agopian0926f502009-05-04 14:17:04 -0700929 const DisplayHardware& hw(graphicPlane(0).displayHardware());
930 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700931
932 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
933 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700934 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
935 mDirtyRegion.bounds() : hw.bounds());
936 composeSurfaces(repaint);
937 }
938
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 glDisable(GL_TEXTURE_2D);
940 glDisable(GL_BLEND);
941 glDisable(GL_DITHER);
942 glDisable(GL_SCISSOR_TEST);
943
Mathias Agopian0926f502009-05-04 14:17:04 -0700944 static int toggle = 0;
945 toggle = 1 - toggle;
946 if (toggle) {
947 glColor4x(0x10000, 0, 0x10000, 0x10000);
948 } else {
949 glColor4x(0x10000, 0x10000, 0, 0x10000);
950 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951
Mathias Agopian20f68782009-05-11 00:03:41 -0700952 Region::const_iterator it = mDirtyRegion.begin();
953 Region::const_iterator const end = mDirtyRegion.end();
954 while (it != end) {
955 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956 GLfloat vertices[][2] = {
957 { r.left, r.top },
958 { r.left, r.bottom },
959 { r.right, r.bottom },
960 { r.right, r.top }
961 };
962 glVertexPointer(2, GL_FLOAT, 0, vertices);
963 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
964 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700965
Mathias Agopianb8a55602009-06-26 19:06:36 -0700966 if (mInvalidRegion.isEmpty()) {
967 mDirtyRegion.dump("mDirtyRegion");
968 mInvalidRegion.dump("mInvalidRegion");
969 }
970 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
972 if (mDebugRegion > 1)
973 usleep(mDebugRegion * 1000);
974
975 glEnable(GL_SCISSOR_TEST);
976 //mDirtyRegion.dump("mDirtyRegion");
977}
978
979void SurfaceFlinger::drawWormhole() const
980{
981 const Region region(mWormholeRegion.intersect(mDirtyRegion));
982 if (region.isEmpty())
983 return;
984
985 const DisplayHardware& hw(graphicPlane(0).displayHardware());
986 const int32_t width = hw.getWidth();
987 const int32_t height = hw.getHeight();
988
989 glDisable(GL_BLEND);
990 glDisable(GL_DITHER);
991
992 if (LIKELY(!mDebugBackground)) {
993 glClearColorx(0,0,0,0);
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 glClear(GL_COLOR_BUFFER_BIT);
1001 }
1002 } else {
1003 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1004 { width, height }, { 0, height } };
1005 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1006 glVertexPointer(2, GL_SHORT, 0, vertices);
1007 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1008 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1009 glEnable(GL_TEXTURE_2D);
1010 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1011 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1012 glMatrixMode(GL_TEXTURE);
1013 glLoadIdentity();
1014 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001015 Region::const_iterator it = region.begin();
1016 Region::const_iterator const end = region.end();
1017 while (it != end) {
1018 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 const GLint sy = height - (r.top + r.height());
1020 glScissor(r.left, sy, r.width(), r.height());
1021 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1022 }
1023 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1024 }
1025}
1026
1027void SurfaceFlinger::debugShowFPS() const
1028{
1029 static int mFrameCount;
1030 static int mLastFrameCount = 0;
1031 static nsecs_t mLastFpsTime = 0;
1032 static float mFps = 0;
1033 mFrameCount++;
1034 nsecs_t now = systemTime();
1035 nsecs_t diff = now - mLastFpsTime;
1036 if (diff > ms2ns(250)) {
1037 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1038 mLastFpsTime = now;
1039 mLastFrameCount = mFrameCount;
1040 }
1041 // XXX: mFPS has the value we want
1042 }
1043
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001044status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045{
1046 Mutex::Autolock _l(mStateLock);
1047 addLayer_l(layer);
1048 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1049 return NO_ERROR;
1050}
1051
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001052status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053{
1054 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001055 status_t err = purgatorizeLayer_l(layer);
1056 if (err == NO_ERROR)
1057 setTransactionFlags(eTransactionNeeded);
1058 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059}
1060
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001061status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062{
1063 layer->forceVisibilityTransaction();
1064 setTransactionFlags(eTraversalNeeded);
1065 return NO_ERROR;
1066}
1067
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001068status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069{
1070 ssize_t i = mCurrentState.layersSortedByZ.add(
1071 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001072 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1073 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 mLayerMap.add(lbc->serverIndex(), lbc);
1075 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 return NO_ERROR;
1077}
1078
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001079status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080{
1081 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1082 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001083 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001084 sp<LayerBaseClient> layer =
1085 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001086 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 mLayerMap.removeItem(layer->serverIndex());
1088 }
1089 return NO_ERROR;
1090 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001091 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092}
1093
Mathias Agopian9a112062009-04-17 19:36:26 -07001094status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1095{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001096 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001097 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001098
Mathias Agopian3d579642009-06-04 18:46:21 -07001099 // it's possible that we don't find a layer, because it might
1100 // have been destroyed already -- this is not technically an error
1101 // from the user because there is a race between BClient::destroySurface(),
1102 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001103 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1104}
1105
1106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107void SurfaceFlinger::free_resources_l()
1108{
1109 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001110 mLayersRemoved = false;
1111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001113 SortedVector< wp<Client> >& scheduledBroadcasts(mScheduledBroadcasts);
1114 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 const size_t count = disconnectedClients.size();
1116 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001117 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 // if this client is the scheduled broadcast list,
1119 // remove it from there (and we don't need to signal it
1120 // since it is dead).
1121 int32_t index = scheduledBroadcasts.indexOf(client);
1122 if (index >= 0) {
1123 scheduledBroadcasts.removeItemsAt(index);
1124 }
1125 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 }
1127 disconnectedClients.clear();
1128}
1129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1131{
1132 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1133}
1134
1135uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1136{
1137 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1138 if ((old & flags)==0) { // wake the server up
1139 if (delay > 0) {
1140 signalDelayedEvent(delay);
1141 } else {
1142 signalEvent();
1143 }
1144 }
1145 return old;
1146}
1147
1148void SurfaceFlinger::openGlobalTransaction()
1149{
1150 android_atomic_inc(&mTransactionCount);
1151}
1152
1153void SurfaceFlinger::closeGlobalTransaction()
1154{
1155 if (android_atomic_dec(&mTransactionCount) == 1) {
1156 signalEvent();
1157 }
1158}
1159
1160status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1161{
1162 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1163 return BAD_VALUE;
1164
1165 Mutex::Autolock _l(mStateLock);
1166 mCurrentState.freezeDisplay = 1;
1167 setTransactionFlags(eTransactionNeeded);
1168
1169 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001170 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 return NO_ERROR;
1172}
1173
1174status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1175{
1176 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1177 return BAD_VALUE;
1178
1179 Mutex::Autolock _l(mStateLock);
1180 mCurrentState.freezeDisplay = 0;
1181 setTransactionFlags(eTransactionNeeded);
1182
1183 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001184 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 return NO_ERROR;
1186}
1187
Mathias Agopianc08731e2009-03-27 18:11:38 -07001188int SurfaceFlinger::setOrientation(DisplayID dpy,
1189 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190{
1191 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1192 return BAD_VALUE;
1193
1194 Mutex::Autolock _l(mStateLock);
1195 if (mCurrentState.orientation != orientation) {
1196 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001197 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 mCurrentState.orientation = orientation;
1199 setTransactionFlags(eTransactionNeeded);
1200 mTransactionCV.wait(mStateLock);
1201 } else {
1202 orientation = BAD_VALUE;
1203 }
1204 }
1205 return orientation;
1206}
1207
1208sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1209 ISurfaceFlingerClient::surface_data_t* params,
1210 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1211 uint32_t flags)
1212{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001213 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 sp<LayerBaseClient::Surface> surfaceHandle;
1215 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001216 sp<Client> client = mClientsMap.valueFor(clientId);
1217 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1219 return surfaceHandle;
1220 }
1221
1222 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001223 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1225 LOGE("createSurface() failed, generateId = %d", id);
1226 return surfaceHandle;
1227 }
1228
1229 switch (flags & eFXSurfaceMask) {
1230 case eFXSurfaceNormal:
1231 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001232 layer = createPushBuffersSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 } else {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001234 layer = createNormalSurfaceLocked(client, d, id, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 }
1236 break;
1237 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001238 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 break;
1240 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001241 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 break;
1243 }
1244
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001245 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 setTransactionFlags(eTransactionNeeded);
1247 surfaceHandle = layer->getSurface();
1248 if (surfaceHandle != 0)
1249 surfaceHandle->getSurfaceData(params);
1250 }
1251
1252 return surfaceHandle;
1253}
1254
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001255sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001256 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
1258{
1259 // initialize the surfaces
1260 switch (format) { // TODO: take h/w into account
1261 case PIXEL_FORMAT_TRANSPARENT:
1262 case PIXEL_FORMAT_TRANSLUCENT:
1263 format = PIXEL_FORMAT_RGBA_8888;
1264 break;
1265 case PIXEL_FORMAT_OPAQUE:
1266 format = PIXEL_FORMAT_RGB_565;
1267 break;
1268 }
1269
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001270 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001271 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 if (LIKELY(err == NO_ERROR)) {
1273 layer->initStates(w, h, flags);
1274 addLayer_l(layer);
1275 } else {
1276 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001277 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 }
1279 return layer;
1280}
1281
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001282sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
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<LayerBlur> layer = new LayerBlur(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::createDimSurfaceLocked(
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<LayerDim> layer = new LayerDim(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 Agopian076b1cc2009-04-10 14:24:30 -07001302sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001303 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1305{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001306 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 layer->initStates(w, h, flags);
1308 addLayer_l(layer);
1309 return layer;
1310}
1311
Mathias Agopian9a112062009-04-17 19:36:26 -07001312status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313{
Mathias Agopian9a112062009-04-17 19:36:26 -07001314 /*
1315 * called by the window manager, when a surface should be marked for
1316 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001317 *
1318 * The surface is removed from the current and drawing lists, but placed
1319 * in the purgatory queue, so it's not destroyed right-away (we need
1320 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001321 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001322
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001323 Mutex::Autolock _l(mStateLock);
1324 sp<LayerBaseClient> layer = getLayerUser_l(index);
1325 status_t err = purgatorizeLayer_l(layer);
1326 if (err == NO_ERROR) {
1327 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001328 }
1329 return err;
1330}
1331
1332status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1333{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001334 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001335
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001336 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001337 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001338 sp<LayerBaseClient> layer;
1339 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 MessageDestroySurface(
1341 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1342 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001344 sp<LayerBaseClient> l(layer);
1345 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001346 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001347 /*
1348 * remove the layer from the current list -- chances are that it's
1349 * not in the list anyway, because it should have been removed
1350 * already upon request of the client (eg: window manager).
1351 * However, a buggy client could have not done that.
1352 * Since we know we don't have any more clients, we don't need
1353 * to use the purgatory.
1354 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001355 status_t err = flinger->removeLayer_l(l);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001356 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1357 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001358 return true;
1359 }
1360 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001361
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001362 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 return NO_ERROR;
1364}
1365
1366status_t SurfaceFlinger::setClientState(
1367 ClientID cid,
1368 int32_t count,
1369 const layer_state_t* states)
1370{
1371 Mutex::Autolock _l(mStateLock);
1372 uint32_t flags = 0;
1373 cid <<= 16;
1374 for (int i=0 ; i<count ; i++) {
1375 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001376 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 if (what & ePositionChanged) {
1380 if (layer->setPosition(s.x, s.y))
1381 flags |= eTraversalNeeded;
1382 }
1383 if (what & eLayerChanged) {
1384 if (layer->setLayer(s.z)) {
1385 mCurrentState.layersSortedByZ.reorder(
1386 layer, &Layer::compareCurrentStateZ);
1387 // we need traversal (state changed)
1388 // AND transaction (list changed)
1389 flags |= eTransactionNeeded|eTraversalNeeded;
1390 }
1391 }
1392 if (what & eSizeChanged) {
1393 if (layer->setSize(s.w, s.h))
1394 flags |= eTraversalNeeded;
1395 }
1396 if (what & eAlphaChanged) {
1397 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1398 flags |= eTraversalNeeded;
1399 }
1400 if (what & eMatrixChanged) {
1401 if (layer->setMatrix(s.matrix))
1402 flags |= eTraversalNeeded;
1403 }
1404 if (what & eTransparentRegionChanged) {
1405 if (layer->setTransparentRegionHint(s.transparentRegion))
1406 flags |= eTraversalNeeded;
1407 }
1408 if (what & eVisibilityChanged) {
1409 if (layer->setFlags(s.flags, s.mask))
1410 flags |= eTraversalNeeded;
1411 }
1412 }
1413 }
1414 if (flags) {
1415 setTransactionFlags(flags);
1416 }
1417 return NO_ERROR;
1418}
1419
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001420sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001422 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1423 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424}
1425
1426void SurfaceFlinger::screenReleased(int dpy)
1427{
1428 // this may be called by a signal handler, we can't do too much in here
1429 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1430 signalEvent();
1431}
1432
1433void SurfaceFlinger::screenAcquired(int dpy)
1434{
1435 // this may be called by a signal handler, we can't do too much in here
1436 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1437 signalEvent();
1438}
1439
1440status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1441{
1442 const size_t SIZE = 1024;
1443 char buffer[SIZE];
1444 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001445 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446 snprintf(buffer, SIZE, "Permission Denial: "
1447 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1448 IPCThreadState::self()->getCallingPid(),
1449 IPCThreadState::self()->getCallingUid());
1450 result.append(buffer);
1451 } else {
1452 Mutex::Autolock _l(mStateLock);
1453 size_t s = mClientsMap.size();
1454 char name[64];
1455 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001456 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457 sprintf(name, " Client (id=0x%08x)", client->cid);
1458 client->dump(name);
1459 }
1460 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1461 const size_t count = currentLayers.size();
1462 for (size_t i=0 ; i<count ; i++) {
1463 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001464 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465 const Layer::State& s = layer->drawingState();
1466 snprintf(buffer, SIZE,
1467 "+ %s %p\n"
1468 " "
1469 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1470 "needsBlending=%1d, invalidate=%1d, "
1471 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001472 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 s.z, layer->tx(), layer->ty(), s.w, s.h,
1474 layer->needsBlending(), layer->contentDirty,
1475 s.alpha, s.flags,
1476 s.transform[0], s.transform[1],
1477 s.transform[2], s.transform[3]);
1478 result.append(buffer);
1479 buffer[0] = 0;
1480 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001481 sp<LayerBaseClient> lbc =
1482 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1483 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001484 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485 snprintf(buffer, SIZE,
1486 " "
1487 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001488 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 lbc->getIdentity());
1490 }
1491 result.append(buffer);
1492 buffer[0] = 0;
1493 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001494 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1495 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 const LayerBitmap& buf0(l->getBuffer(0));
1497 const LayerBitmap& buf1(l->getBuffer(1));
1498 snprintf(buffer, SIZE,
1499 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001500 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 " freezeLock=%p, swapState=0x%08x\n",
1502 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001503 buf0.getWidth(), buf0.getHeight(),
1504 buf0.getBuffer()->getStride(),
1505 buf1.getWidth(), buf1.getHeight(),
1506 buf1.getBuffer()->getStride(),
1507 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 l->lcblk->swapState);
1509 }
1510 result.append(buffer);
1511 buffer[0] = 0;
1512 s.transparentRegion.dump(result, "transparentRegion");
1513 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1514 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1515 }
1516 mWormholeRegion.dump(result, "WormholeRegion");
1517 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1518 snprintf(buffer, SIZE,
1519 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1520 mFreezeDisplay?"yes":"no", mFreezeCount,
1521 mCurrentState.orientation, hw.canDraw());
1522 result.append(buffer);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001523 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001524 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001525 const BufferAllocator& alloc(BufferAllocator::get());
1526 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 }
1528 write(fd, result.string(), result.size());
1529 return NO_ERROR;
1530}
1531
1532status_t SurfaceFlinger::onTransact(
1533 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1534{
1535 switch (code) {
1536 case CREATE_CONNECTION:
1537 case OPEN_GLOBAL_TRANSACTION:
1538 case CLOSE_GLOBAL_TRANSACTION:
1539 case SET_ORIENTATION:
1540 case FREEZE_DISPLAY:
1541 case UNFREEZE_DISPLAY:
1542 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 {
1544 // codes that require permission check
1545 IPCThreadState* ipc = IPCThreadState::self();
1546 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001547 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001548 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1549 LOGE("Permission Denial: "
1550 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1551 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552 }
1553 }
1554 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1556 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001557 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001558 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1559 IPCThreadState* ipc = IPCThreadState::self();
1560 const int pid = ipc->getCallingPid();
1561 const int uid = ipc->getCallingUid();
1562 LOGE("Permission Denial: "
1563 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 return PERMISSION_DENIED;
1565 }
1566 int n;
1567 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001568 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001570 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571 return NO_ERROR;
1572 case 1002: // SHOW_UPDATES
1573 n = data.readInt32();
1574 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1575 return NO_ERROR;
1576 case 1003: // SHOW_BACKGROUND
1577 n = data.readInt32();
1578 mDebugBackground = n ? 1 : 0;
1579 return NO_ERROR;
1580 case 1004:{ // repaint everything
1581 Mutex::Autolock _l(mStateLock);
1582 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1583 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1584 signalEvent();
1585 }
1586 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 case 1007: // set mFreezeCount
1588 mFreezeCount = data.readInt32();
1589 return NO_ERROR;
1590 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001591 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592 reply->writeInt32(0);
1593 reply->writeInt32(mDebugRegion);
1594 reply->writeInt32(mDebugBackground);
1595 return NO_ERROR;
1596 case 1013: {
1597 Mutex::Autolock _l(mStateLock);
1598 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1599 reply->writeInt32(hw.getPageFlipCount());
1600 }
1601 return NO_ERROR;
1602 }
1603 }
1604 return err;
1605}
1606
1607// ---------------------------------------------------------------------------
1608#if 0
1609#pragma mark -
1610#endif
1611
1612Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1613 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1614{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615 const int pgsize = getpagesize();
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001616 const int cblksize = ((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1617
1618 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1619 "SurfaceFlinger Client control-block");
1620
1621 ctrlblk = static_cast<per_client_cblk_t *>(mCblkHeap->getBase());
1622 if (ctrlblk) { // construct the shared structure in-place.
1623 new(ctrlblk) per_client_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624 }
1625}
1626
1627Client::~Client() {
1628 if (ctrlblk) {
1629 const int pgsize = getpagesize();
1630 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1631 }
1632}
1633
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634int32_t Client::generateId(int pid)
1635{
1636 const uint32_t i = clz( ~mBitmap );
1637 if (i >= NUM_LAYERS_MAX) {
1638 return NO_MEMORY;
1639 }
1640 mPid = pid;
1641 mInUse.add(uint8_t(i));
1642 mBitmap |= 1<<(31-i);
1643 return i;
1644}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001645
1646status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647{
1648 ssize_t idx = mInUse.indexOf(id);
1649 if (idx < 0)
1650 return NAME_NOT_FOUND;
1651 return mLayers.insertAt(layer, idx);
1652}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654void Client::free(int32_t id)
1655{
1656 ssize_t idx = mInUse.remove(uint8_t(id));
1657 if (idx >= 0) {
1658 mBitmap &= ~(1<<(31-id));
1659 mLayers.removeItemsAt(idx);
1660 }
1661}
1662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663bool Client::isValid(int32_t i) const {
1664 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1665}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001666
1667sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1668 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001670 if (idx >= 0) {
1671 lbc = mLayers[idx].promote();
1672 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1673 }
1674 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675}
1676
1677void Client::dump(const char* what)
1678{
1679}
1680
1681// ---------------------------------------------------------------------------
1682#if 0
1683#pragma mark -
1684#endif
1685
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001686BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 : mId(cid), mFlinger(flinger), mCblk(cblk)
1688{
1689}
1690
1691BClient::~BClient() {
1692 // destroy all resources attached to this client
1693 mFlinger->destroyConnection(mId);
1694}
1695
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001696sp<IMemoryHeap> BClient::getControlBlock() const {
1697 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698}
1699
1700sp<ISurface> BClient::createSurface(
1701 ISurfaceFlingerClient::surface_data_t* params, int pid,
1702 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1703 uint32_t flags)
1704{
1705 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1706}
1707
1708status_t BClient::destroySurface(SurfaceID sid)
1709{
1710 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001711 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712}
1713
1714status_t BClient::setState(int32_t count, const layer_state_t* states)
1715{
1716 return mFlinger->setClientState(mId, count, states);
1717}
1718
1719// ---------------------------------------------------------------------------
1720
1721GraphicPlane::GraphicPlane()
1722 : mHw(0)
1723{
1724}
1725
1726GraphicPlane::~GraphicPlane() {
1727 delete mHw;
1728}
1729
1730bool GraphicPlane::initialized() const {
1731 return mHw ? true : false;
1732}
1733
1734void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1735 mHw = hw;
1736}
1737
1738void GraphicPlane::setTransform(const Transform& tr) {
1739 mTransform = tr;
1740 mGlobalTransform = mOrientationTransform * mTransform;
1741}
1742
1743status_t GraphicPlane::orientationToTransfrom(
1744 int orientation, int w, int h, Transform* tr)
1745{
1746 float a, b, c, d, x, y;
1747 switch (orientation) {
1748 case ISurfaceComposer::eOrientationDefault:
1749 a=1; b=0; c=0; d=1; x=0; y=0;
1750 break;
1751 case ISurfaceComposer::eOrientation90:
1752 a=0; b=-1; c=1; d=0; x=w; y=0;
1753 break;
1754 case ISurfaceComposer::eOrientation180:
1755 a=-1; b=0; c=0; d=-1; x=w; y=h;
1756 break;
1757 case ISurfaceComposer::eOrientation270:
1758 a=0; b=1; c=-1; d=0; x=0; y=h;
1759 break;
1760 default:
1761 return BAD_VALUE;
1762 }
1763 tr->set(a, b, c, d);
1764 tr->set(x, y);
1765 return NO_ERROR;
1766}
1767
1768status_t GraphicPlane::setOrientation(int orientation)
1769{
1770 const DisplayHardware& hw(displayHardware());
1771 const float w = hw.getWidth();
1772 const float h = hw.getHeight();
1773
1774 if (orientation == ISurfaceComposer::eOrientationDefault) {
1775 // make sure the default orientation is optimal
1776 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001777 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778 mGlobalTransform = mTransform;
1779 return NO_ERROR;
1780 }
1781
1782 // If the rotation can be handled in hardware, this is where
1783 // the magic should happen.
1784 if (UNLIKELY(orientation == 42)) {
1785 float a, b, c, d, x, y;
1786 const float r = (3.14159265f / 180.0f) * 42.0f;
1787 const float si = sinf(r);
1788 const float co = cosf(r);
1789 a=co; b=-si; c=si; d=co;
1790 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1791 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1792 mOrientationTransform.set(a, b, c, d);
1793 mOrientationTransform.set(x, y);
1794 } else {
1795 GraphicPlane::orientationToTransfrom(orientation, w, h,
1796 &mOrientationTransform);
1797 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001798 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 mGlobalTransform = mOrientationTransform * mTransform;
1800 return NO_ERROR;
1801}
1802
1803const DisplayHardware& GraphicPlane::displayHardware() const {
1804 return *mHw;
1805}
1806
1807const Transform& GraphicPlane::transform() const {
1808 return mGlobalTransform;
1809}
1810
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001811EGLDisplay GraphicPlane::getEGLDisplay() const {
1812 return mHw->getEGLDisplay();
1813}
1814
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001815// ---------------------------------------------------------------------------
1816
1817}; // namespace android