blob: 102899c562b222494c391091ad46e5a395008ca1 [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 Agopian5d7126b2009-07-28 14:20:21 -0700653 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700654 }
655 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 }
657
658 // get rid of all resources we don't need anymore
659 // (layers and clients)
660 free_resources_l();
661 }
662
663 commitTransaction();
664}
665
666sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
667{
668 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
669}
670
671void SurfaceFlinger::computeVisibleRegions(
672 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
673{
674 const GraphicPlane& plane(graphicPlane(0));
675 const Transform& planeTransform(plane.transform());
676
677 Region aboveOpaqueLayers;
678 Region aboveCoveredLayers;
679 Region dirty;
680
681 bool secureFrameBuffer = false;
682
683 size_t i = currentLayers.size();
684 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700685 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 layer->validateVisibility(planeTransform);
687
688 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700689 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690
691 // handle hidden surfaces by setting the visible region to empty
692 Region opaqueRegion;
693 Region visibleRegion;
694 Region coveredRegion;
Mathias Agopian97011222009-07-28 10:57:27 -0700695 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700697 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 visibleRegion.set(bounds);
699 coveredRegion = visibleRegion;
700
701 // Remove the transparent area from the visible region
702 if (translucent) {
703 visibleRegion.subtractSelf(layer->transparentRegionScreen);
704 }
705
706 // compute the opaque region
707 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
708 // the opaque region is the visible region
709 opaqueRegion = visibleRegion;
710 }
711 }
712
713 // subtract the opaque region covered by the layers above us
714 visibleRegion.subtractSelf(aboveOpaqueLayers);
715 coveredRegion.andSelf(aboveCoveredLayers);
716
717 // compute this layer's dirty region
718 if (layer->contentDirty) {
719 // we need to invalidate the whole region
720 dirty = visibleRegion;
721 // as well, as the old visible region
722 dirty.orSelf(layer->visibleRegionScreen);
723 layer->contentDirty = false;
724 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700725 /* compute the exposed region:
726 * exposed = what's VISIBLE and NOT COVERED now
727 * but was COVERED before
728 */
729 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 }
731 dirty.subtractSelf(aboveOpaqueLayers);
732
733 // accumulate to the screen dirty region
734 dirtyRegion.orSelf(dirty);
735
Mathias Agopian62b74442009-04-14 23:02:51 -0700736 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700738 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739
740 // Store the visible region is screen space
741 layer->setVisibleRegion(visibleRegion);
742 layer->setCoveredRegion(coveredRegion);
743
Mathias Agopian97011222009-07-28 10:57:27 -0700744 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 if (layer->isSecure() && !visibleRegion.isEmpty()) {
746 secureFrameBuffer = true;
747 }
748 }
749
Mathias Agopian97011222009-07-28 10:57:27 -0700750 // invalidate the areas where a layer was removed
751 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
752 mDirtyRegionRemovedLayer.clear();
753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 mSecureFrameBuffer = secureFrameBuffer;
755 opaqueRegion = aboveOpaqueLayers;
756}
757
758
759void SurfaceFlinger::commitTransaction()
760{
761 mDrawingState = mCurrentState;
762 mTransactionCV.signal();
763}
764
765void SurfaceFlinger::handlePageFlip()
766{
767 bool visibleRegions = mVisibleRegionsDirty;
768 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
769 visibleRegions |= lockPageFlip(currentLayers);
770
771 const DisplayHardware& hw = graphicPlane(0).displayHardware();
772 const Region screenRegion(hw.bounds());
773 if (visibleRegions) {
774 Region opaqueRegion;
775 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
776 mWormholeRegion = screenRegion.subtract(opaqueRegion);
777 mVisibleRegionsDirty = false;
778 }
779
780 unlockPageFlip(currentLayers);
781 mDirtyRegion.andSelf(screenRegion);
782}
783
784bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
785{
786 bool recomputeVisibleRegions = false;
787 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700788 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791 layer->lockPageFlip(recomputeVisibleRegions);
792 }
793 return recomputeVisibleRegions;
794}
795
796void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
797{
798 const GraphicPlane& plane(graphicPlane(0));
799 const Transform& planeTransform(plane.transform());
800 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700801 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700803 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 layer->unlockPageFlip(planeTransform, mDirtyRegion);
805 }
806}
807
Mathias Agopianb8a55602009-06-26 19:06:36 -0700808
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809void SurfaceFlinger::handleRepaint()
810{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700811 // compute the invalid region
812 mInvalidRegion.orSelf(mDirtyRegion);
813 if (mInvalidRegion.isEmpty()) {
814 // nothing to do
815 return;
816 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817
818 if (UNLIKELY(mDebugRegion)) {
819 debugFlashRegions();
820 }
821
Mathias Agopianb8a55602009-06-26 19:06:36 -0700822 // set the frame buffer
823 const DisplayHardware& hw(graphicPlane(0).displayHardware());
824 glMatrixMode(GL_MODELVIEW);
825 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826
827 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700828 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
829 (flags & DisplayHardware::BUFFER_PRESERVED))
830 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700831 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
832 // takes a rectangle, we must make sure to update that whole
833 // rectangle in that case
834 if (flags & DisplayHardware::SWAP_RECTANGLE) {
835 // FIXME: we really should be able to pass a region to
836 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 mDirtyRegion.set(mInvalidRegion.bounds());
838 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700839 // in the BUFFER_PRESERVED case, obviously, we can update only
840 // what's needed and nothing more.
841 // NOTE: this is NOT a common case, as preserving the backbuffer
842 // is costly and usually involves copying the whole update back.
843 }
844 } else {
845 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
846 // We need to redraw the rectangle that will be updated
847 // (pushed to the framebuffer).
848 // This is needed because UPDATE_ON_DEMAND only takes one
849 // rectangle instead of a region (see DisplayHardware::flip())
850 mDirtyRegion.set(mInvalidRegion.bounds());
851 } else {
852 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853 mDirtyRegion.set(hw.bounds());
854 mInvalidRegion = mDirtyRegion;
855 }
856 }
857
858 // compose all surfaces
859 composeSurfaces(mDirtyRegion);
860
861 // clear the dirty regions
862 mDirtyRegion.clear();
863}
864
865void SurfaceFlinger::composeSurfaces(const Region& dirty)
866{
867 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
868 // should never happen unless the window manager has a bug
869 // draw something...
870 drawWormhole();
871 }
872 const SurfaceFlinger& flinger(*this);
873 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
874 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700875 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700877 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 const Region& visibleRegion(layer->visibleRegionScreen);
879 if (!visibleRegion.isEmpty()) {
880 const Region clip(dirty.intersect(visibleRegion));
881 if (!clip.isEmpty()) {
882 layer->draw(clip);
883 }
884 }
885 }
886}
887
888void SurfaceFlinger::unlockClients()
889{
890 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
891 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700892 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700894 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 layer->finishPageFlip();
896 }
897}
898
Mathias Agopianf9d93272009-06-19 17:00:27 -0700899void SurfaceFlinger::scheduleBroadcast(const sp<Client>& client)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900{
901 if (mLastScheduledBroadcast != client) {
902 mLastScheduledBroadcast = client;
903 mScheduledBroadcasts.add(client);
904 }
905}
906
907void SurfaceFlinger::executeScheduledBroadcasts()
908{
Mathias Agopianf9d93272009-06-19 17:00:27 -0700909 SortedVector< wp<Client> >& list(mScheduledBroadcasts);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 size_t count = list.size();
911 while (count--) {
Mathias Agopianf9d93272009-06-19 17:00:27 -0700912 sp<Client> client = list[count].promote();
913 if (client != 0) {
914 per_client_cblk_t* const cblk = client->ctrlblk;
915 if (cblk->lock.tryLock() == NO_ERROR) {
916 cblk->cv.broadcast();
917 list.removeAt(count);
918 cblk->lock.unlock();
919 } else {
920 // schedule another round
921 LOGW("executeScheduledBroadcasts() skipped, "
922 "contention on the client. We'll try again later...");
923 signalDelayedEvent(ms2ns(4));
924 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 }
926 }
927 mLastScheduledBroadcast = 0;
928}
929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930void SurfaceFlinger::debugFlashRegions()
931{
Mathias Agopian0926f502009-05-04 14:17:04 -0700932 const DisplayHardware& hw(graphicPlane(0).displayHardware());
933 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700934
935 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
936 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700937 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
938 mDirtyRegion.bounds() : hw.bounds());
939 composeSurfaces(repaint);
940 }
941
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 glDisable(GL_TEXTURE_2D);
943 glDisable(GL_BLEND);
944 glDisable(GL_DITHER);
945 glDisable(GL_SCISSOR_TEST);
946
Mathias Agopian0926f502009-05-04 14:17:04 -0700947 static int toggle = 0;
948 toggle = 1 - toggle;
949 if (toggle) {
950 glColor4x(0x10000, 0, 0x10000, 0x10000);
951 } else {
952 glColor4x(0x10000, 0x10000, 0, 0x10000);
953 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954
Mathias Agopian20f68782009-05-11 00:03:41 -0700955 Region::const_iterator it = mDirtyRegion.begin();
956 Region::const_iterator const end = mDirtyRegion.end();
957 while (it != end) {
958 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 GLfloat vertices[][2] = {
960 { r.left, r.top },
961 { r.left, r.bottom },
962 { r.right, r.bottom },
963 { r.right, r.top }
964 };
965 glVertexPointer(2, GL_FLOAT, 0, vertices);
966 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
967 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700968
Mathias Agopianb8a55602009-06-26 19:06:36 -0700969 if (mInvalidRegion.isEmpty()) {
970 mDirtyRegion.dump("mDirtyRegion");
971 mInvalidRegion.dump("mInvalidRegion");
972 }
973 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974
975 if (mDebugRegion > 1)
976 usleep(mDebugRegion * 1000);
977
978 glEnable(GL_SCISSOR_TEST);
979 //mDirtyRegion.dump("mDirtyRegion");
980}
981
982void SurfaceFlinger::drawWormhole() const
983{
984 const Region region(mWormholeRegion.intersect(mDirtyRegion));
985 if (region.isEmpty())
986 return;
987
988 const DisplayHardware& hw(graphicPlane(0).displayHardware());
989 const int32_t width = hw.getWidth();
990 const int32_t height = hw.getHeight();
991
992 glDisable(GL_BLEND);
993 glDisable(GL_DITHER);
994
995 if (LIKELY(!mDebugBackground)) {
996 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700997 Region::const_iterator it = region.begin();
998 Region::const_iterator const end = region.end();
999 while (it != end) {
1000 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 const GLint sy = height - (r.top + r.height());
1002 glScissor(r.left, sy, r.width(), r.height());
1003 glClear(GL_COLOR_BUFFER_BIT);
1004 }
1005 } else {
1006 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1007 { width, height }, { 0, height } };
1008 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1009 glVertexPointer(2, GL_SHORT, 0, vertices);
1010 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1011 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1012 glEnable(GL_TEXTURE_2D);
1013 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1014 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1015 glMatrixMode(GL_TEXTURE);
1016 glLoadIdentity();
1017 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001018 Region::const_iterator it = region.begin();
1019 Region::const_iterator const end = region.end();
1020 while (it != end) {
1021 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 const GLint sy = height - (r.top + r.height());
1023 glScissor(r.left, sy, r.width(), r.height());
1024 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1025 }
1026 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1027 }
1028}
1029
1030void SurfaceFlinger::debugShowFPS() const
1031{
1032 static int mFrameCount;
1033 static int mLastFrameCount = 0;
1034 static nsecs_t mLastFpsTime = 0;
1035 static float mFps = 0;
1036 mFrameCount++;
1037 nsecs_t now = systemTime();
1038 nsecs_t diff = now - mLastFpsTime;
1039 if (diff > ms2ns(250)) {
1040 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1041 mLastFpsTime = now;
1042 mLastFrameCount = mFrameCount;
1043 }
1044 // XXX: mFPS has the value we want
1045 }
1046
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001047status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048{
1049 Mutex::Autolock _l(mStateLock);
1050 addLayer_l(layer);
1051 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1052 return NO_ERROR;
1053}
1054
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001055status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056{
1057 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001058 status_t err = purgatorizeLayer_l(layer);
1059 if (err == NO_ERROR)
1060 setTransactionFlags(eTransactionNeeded);
1061 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062}
1063
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001064status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065{
1066 layer->forceVisibilityTransaction();
1067 setTransactionFlags(eTraversalNeeded);
1068 return NO_ERROR;
1069}
1070
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
1073 ssize_t i = mCurrentState.layersSortedByZ.add(
1074 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001075 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1076 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 mLayerMap.add(lbc->serverIndex(), lbc);
1078 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 return NO_ERROR;
1080}
1081
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001082status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083{
1084 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1085 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001086 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001087 sp<LayerBaseClient> layer =
1088 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001089 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 mLayerMap.removeItem(layer->serverIndex());
1091 }
1092 return NO_ERROR;
1093 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001094 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopian9a112062009-04-17 19:36:26 -07001097status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1098{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001099 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001100 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001101
Mathias Agopian3d579642009-06-04 18:46:21 -07001102 // it's possible that we don't find a layer, because it might
1103 // have been destroyed already -- this is not technically an error
1104 // from the user because there is a race between BClient::destroySurface(),
1105 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001106 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1107}
1108
1109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110void SurfaceFlinger::free_resources_l()
1111{
1112 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001113 mLayersRemoved = false;
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001116 SortedVector< wp<Client> >& scheduledBroadcasts(mScheduledBroadcasts);
1117 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 const size_t count = disconnectedClients.size();
1119 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001120 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 // if this client is the scheduled broadcast list,
1122 // remove it from there (and we don't need to signal it
1123 // since it is dead).
1124 int32_t index = scheduledBroadcasts.indexOf(client);
1125 if (index >= 0) {
1126 scheduledBroadcasts.removeItemsAt(index);
1127 }
1128 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 }
1130 disconnectedClients.clear();
1131}
1132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1134{
1135 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1136}
1137
1138uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1139{
1140 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1141 if ((old & flags)==0) { // wake the server up
1142 if (delay > 0) {
1143 signalDelayedEvent(delay);
1144 } else {
1145 signalEvent();
1146 }
1147 }
1148 return old;
1149}
1150
1151void SurfaceFlinger::openGlobalTransaction()
1152{
1153 android_atomic_inc(&mTransactionCount);
1154}
1155
1156void SurfaceFlinger::closeGlobalTransaction()
1157{
1158 if (android_atomic_dec(&mTransactionCount) == 1) {
1159 signalEvent();
1160 }
1161}
1162
1163status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1164{
1165 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1166 return BAD_VALUE;
1167
1168 Mutex::Autolock _l(mStateLock);
1169 mCurrentState.freezeDisplay = 1;
1170 setTransactionFlags(eTransactionNeeded);
1171
1172 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001173 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174 return NO_ERROR;
1175}
1176
1177status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1178{
1179 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1180 return BAD_VALUE;
1181
1182 Mutex::Autolock _l(mStateLock);
1183 mCurrentState.freezeDisplay = 0;
1184 setTransactionFlags(eTransactionNeeded);
1185
1186 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001187 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 return NO_ERROR;
1189}
1190
Mathias Agopianc08731e2009-03-27 18:11:38 -07001191int SurfaceFlinger::setOrientation(DisplayID dpy,
1192 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193{
1194 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1195 return BAD_VALUE;
1196
1197 Mutex::Autolock _l(mStateLock);
1198 if (mCurrentState.orientation != orientation) {
1199 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001200 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 mCurrentState.orientation = orientation;
1202 setTransactionFlags(eTransactionNeeded);
1203 mTransactionCV.wait(mStateLock);
1204 } else {
1205 orientation = BAD_VALUE;
1206 }
1207 }
1208 return orientation;
1209}
1210
1211sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1212 ISurfaceFlingerClient::surface_data_t* params,
1213 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1214 uint32_t flags)
1215{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001216 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001218
1219 if (int32_t(w|h) < 0) {
1220 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1221 int(w), int(h));
1222 return surfaceHandle;
1223 }
1224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001226 sp<Client> client = mClientsMap.valueFor(clientId);
1227 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1229 return surfaceHandle;
1230 }
1231
1232 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001233 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1235 LOGE("createSurface() failed, generateId = %d", id);
1236 return surfaceHandle;
1237 }
1238
1239 switch (flags & eFXSurfaceMask) {
1240 case eFXSurfaceNormal:
1241 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001242 layer = createPushBuffersSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 } else {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001244 layer = createNormalSurfaceLocked(client, d, id, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 }
1246 break;
1247 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001248 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 break;
1250 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001251 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 break;
1253 }
1254
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001255 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 setTransactionFlags(eTransactionNeeded);
1257 surfaceHandle = layer->getSurface();
1258 if (surfaceHandle != 0)
1259 surfaceHandle->getSurfaceData(params);
1260 }
1261
1262 return surfaceHandle;
1263}
1264
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001265sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001266 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
1268{
1269 // initialize the surfaces
1270 switch (format) { // TODO: take h/w into account
1271 case PIXEL_FORMAT_TRANSPARENT:
1272 case PIXEL_FORMAT_TRANSLUCENT:
1273 format = PIXEL_FORMAT_RGBA_8888;
1274 break;
1275 case PIXEL_FORMAT_OPAQUE:
1276 format = PIXEL_FORMAT_RGB_565;
1277 break;
1278 }
1279
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001280 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001281 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 if (LIKELY(err == NO_ERROR)) {
1283 layer->initStates(w, h, flags);
1284 addLayer_l(layer);
1285 } else {
1286 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 }
1289 return layer;
1290}
1291
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001292sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
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<LayerBlur> layer = new LayerBlur(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::createDimSurfaceLocked(
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<LayerDim> layer = new LayerDim(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 Agopian076b1cc2009-04-10 14:24:30 -07001312sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001313 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1315{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001316 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 layer->initStates(w, h, flags);
1318 addLayer_l(layer);
1319 return layer;
1320}
1321
Mathias Agopian9a112062009-04-17 19:36:26 -07001322status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323{
Mathias Agopian9a112062009-04-17 19:36:26 -07001324 /*
1325 * called by the window manager, when a surface should be marked for
1326 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001327 *
1328 * The surface is removed from the current and drawing lists, but placed
1329 * in the purgatory queue, so it's not destroyed right-away (we need
1330 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001331 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001332
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001333 Mutex::Autolock _l(mStateLock);
1334 sp<LayerBaseClient> layer = getLayerUser_l(index);
1335 status_t err = purgatorizeLayer_l(layer);
1336 if (err == NO_ERROR) {
1337 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001338 }
1339 return err;
1340}
1341
1342status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1343{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001344 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001345
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001346 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001347 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001348 sp<LayerBaseClient> layer;
1349 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001350 MessageDestroySurface(
1351 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1352 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001353 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001354 sp<LayerBaseClient> l(layer);
1355 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001356 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001357 /*
1358 * remove the layer from the current list -- chances are that it's
1359 * not in the list anyway, because it should have been removed
1360 * already upon request of the client (eg: window manager).
1361 * However, a buggy client could have not done that.
1362 * Since we know we don't have any more clients, we don't need
1363 * to use the purgatory.
1364 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001365 status_t err = flinger->removeLayer_l(l);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001366 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1367 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001368 return true;
1369 }
1370 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001371
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001372 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 return NO_ERROR;
1374}
1375
1376status_t SurfaceFlinger::setClientState(
1377 ClientID cid,
1378 int32_t count,
1379 const layer_state_t* states)
1380{
1381 Mutex::Autolock _l(mStateLock);
1382 uint32_t flags = 0;
1383 cid <<= 16;
1384 for (int i=0 ; i<count ; i++) {
1385 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001386 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001387 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 if (what & ePositionChanged) {
1390 if (layer->setPosition(s.x, s.y))
1391 flags |= eTraversalNeeded;
1392 }
1393 if (what & eLayerChanged) {
1394 if (layer->setLayer(s.z)) {
1395 mCurrentState.layersSortedByZ.reorder(
1396 layer, &Layer::compareCurrentStateZ);
1397 // we need traversal (state changed)
1398 // AND transaction (list changed)
1399 flags |= eTransactionNeeded|eTraversalNeeded;
1400 }
1401 }
1402 if (what & eSizeChanged) {
1403 if (layer->setSize(s.w, s.h))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eAlphaChanged) {
1407 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eMatrixChanged) {
1411 if (layer->setMatrix(s.matrix))
1412 flags |= eTraversalNeeded;
1413 }
1414 if (what & eTransparentRegionChanged) {
1415 if (layer->setTransparentRegionHint(s.transparentRegion))
1416 flags |= eTraversalNeeded;
1417 }
1418 if (what & eVisibilityChanged) {
1419 if (layer->setFlags(s.flags, s.mask))
1420 flags |= eTraversalNeeded;
1421 }
1422 }
1423 }
1424 if (flags) {
1425 setTransactionFlags(flags);
1426 }
1427 return NO_ERROR;
1428}
1429
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001430sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001432 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1433 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434}
1435
1436void SurfaceFlinger::screenReleased(int dpy)
1437{
1438 // this may be called by a signal handler, we can't do too much in here
1439 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1440 signalEvent();
1441}
1442
1443void SurfaceFlinger::screenAcquired(int dpy)
1444{
1445 // this may be called by a signal handler, we can't do too much in here
1446 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1447 signalEvent();
1448}
1449
1450status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1451{
1452 const size_t SIZE = 1024;
1453 char buffer[SIZE];
1454 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001455 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 snprintf(buffer, SIZE, "Permission Denial: "
1457 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1458 IPCThreadState::self()->getCallingPid(),
1459 IPCThreadState::self()->getCallingUid());
1460 result.append(buffer);
1461 } else {
1462 Mutex::Autolock _l(mStateLock);
1463 size_t s = mClientsMap.size();
1464 char name[64];
1465 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001466 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 sprintf(name, " Client (id=0x%08x)", client->cid);
1468 client->dump(name);
1469 }
1470 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1471 const size_t count = currentLayers.size();
1472 for (size_t i=0 ; i<count ; i++) {
1473 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001474 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 const Layer::State& s = layer->drawingState();
1476 snprintf(buffer, SIZE,
1477 "+ %s %p\n"
1478 " "
1479 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1480 "needsBlending=%1d, invalidate=%1d, "
1481 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001482 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 s.z, layer->tx(), layer->ty(), s.w, s.h,
1484 layer->needsBlending(), layer->contentDirty,
1485 s.alpha, s.flags,
1486 s.transform[0], s.transform[1],
1487 s.transform[2], s.transform[3]);
1488 result.append(buffer);
1489 buffer[0] = 0;
1490 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001491 sp<LayerBaseClient> lbc =
1492 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1493 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001494 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 snprintf(buffer, SIZE,
1496 " "
1497 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001498 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499 lbc->getIdentity());
1500 }
1501 result.append(buffer);
1502 buffer[0] = 0;
1503 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001504 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1505 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 const LayerBitmap& buf0(l->getBuffer(0));
1507 const LayerBitmap& buf1(l->getBuffer(1));
1508 snprintf(buffer, SIZE,
1509 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001510 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511 " freezeLock=%p, swapState=0x%08x\n",
1512 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001513 buf0.getWidth(), buf0.getHeight(),
1514 buf0.getBuffer()->getStride(),
1515 buf1.getWidth(), buf1.getHeight(),
1516 buf1.getBuffer()->getStride(),
1517 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 l->lcblk->swapState);
1519 }
1520 result.append(buffer);
1521 buffer[0] = 0;
1522 s.transparentRegion.dump(result, "transparentRegion");
1523 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1524 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1525 }
1526 mWormholeRegion.dump(result, "WormholeRegion");
1527 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1528 snprintf(buffer, SIZE,
1529 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1530 mFreezeDisplay?"yes":"no", mFreezeCount,
1531 mCurrentState.orientation, hw.canDraw());
1532 result.append(buffer);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001533 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001534 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001535 const BufferAllocator& alloc(BufferAllocator::get());
1536 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 }
1538 write(fd, result.string(), result.size());
1539 return NO_ERROR;
1540}
1541
1542status_t SurfaceFlinger::onTransact(
1543 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1544{
1545 switch (code) {
1546 case CREATE_CONNECTION:
1547 case OPEN_GLOBAL_TRANSACTION:
1548 case CLOSE_GLOBAL_TRANSACTION:
1549 case SET_ORIENTATION:
1550 case FREEZE_DISPLAY:
1551 case UNFREEZE_DISPLAY:
1552 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 {
1554 // codes that require permission check
1555 IPCThreadState* ipc = IPCThreadState::self();
1556 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001557 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001558 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1559 LOGE("Permission Denial: "
1560 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1561 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562 }
1563 }
1564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1566 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001567 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001568 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1569 IPCThreadState* ipc = IPCThreadState::self();
1570 const int pid = ipc->getCallingPid();
1571 const int uid = ipc->getCallingUid();
1572 LOGE("Permission Denial: "
1573 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 return PERMISSION_DENIED;
1575 }
1576 int n;
1577 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001578 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001580 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 return NO_ERROR;
1582 case 1002: // SHOW_UPDATES
1583 n = data.readInt32();
1584 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1585 return NO_ERROR;
1586 case 1003: // SHOW_BACKGROUND
1587 n = data.readInt32();
1588 mDebugBackground = n ? 1 : 0;
1589 return NO_ERROR;
1590 case 1004:{ // repaint everything
1591 Mutex::Autolock _l(mStateLock);
1592 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1593 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1594 signalEvent();
1595 }
1596 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 case 1007: // set mFreezeCount
1598 mFreezeCount = data.readInt32();
1599 return NO_ERROR;
1600 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001601 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602 reply->writeInt32(0);
1603 reply->writeInt32(mDebugRegion);
1604 reply->writeInt32(mDebugBackground);
1605 return NO_ERROR;
1606 case 1013: {
1607 Mutex::Autolock _l(mStateLock);
1608 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1609 reply->writeInt32(hw.getPageFlipCount());
1610 }
1611 return NO_ERROR;
1612 }
1613 }
1614 return err;
1615}
1616
1617// ---------------------------------------------------------------------------
1618#if 0
1619#pragma mark -
1620#endif
1621
1622Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1623 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1624{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 const int pgsize = getpagesize();
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001626 const int cblksize = ((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1627
1628 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1629 "SurfaceFlinger Client control-block");
1630
1631 ctrlblk = static_cast<per_client_cblk_t *>(mCblkHeap->getBase());
1632 if (ctrlblk) { // construct the shared structure in-place.
1633 new(ctrlblk) per_client_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634 }
1635}
1636
1637Client::~Client() {
1638 if (ctrlblk) {
1639 const int pgsize = getpagesize();
1640 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1641 }
1642}
1643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644int32_t Client::generateId(int pid)
1645{
1646 const uint32_t i = clz( ~mBitmap );
1647 if (i >= NUM_LAYERS_MAX) {
1648 return NO_MEMORY;
1649 }
1650 mPid = pid;
1651 mInUse.add(uint8_t(i));
1652 mBitmap |= 1<<(31-i);
1653 return i;
1654}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001655
1656status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657{
1658 ssize_t idx = mInUse.indexOf(id);
1659 if (idx < 0)
1660 return NAME_NOT_FOUND;
1661 return mLayers.insertAt(layer, idx);
1662}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664void Client::free(int32_t id)
1665{
1666 ssize_t idx = mInUse.remove(uint8_t(id));
1667 if (idx >= 0) {
1668 mBitmap &= ~(1<<(31-id));
1669 mLayers.removeItemsAt(idx);
1670 }
1671}
1672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673bool Client::isValid(int32_t i) const {
1674 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1675}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001676
1677sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1678 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001680 if (idx >= 0) {
1681 lbc = mLayers[idx].promote();
1682 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1683 }
1684 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685}
1686
1687void Client::dump(const char* what)
1688{
1689}
1690
1691// ---------------------------------------------------------------------------
1692#if 0
1693#pragma mark -
1694#endif
1695
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001696BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 : mId(cid), mFlinger(flinger), mCblk(cblk)
1698{
1699}
1700
1701BClient::~BClient() {
1702 // destroy all resources attached to this client
1703 mFlinger->destroyConnection(mId);
1704}
1705
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001706sp<IMemoryHeap> BClient::getControlBlock() const {
1707 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708}
1709
1710sp<ISurface> BClient::createSurface(
1711 ISurfaceFlingerClient::surface_data_t* params, int pid,
1712 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1713 uint32_t flags)
1714{
1715 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1716}
1717
1718status_t BClient::destroySurface(SurfaceID sid)
1719{
1720 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001721 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722}
1723
1724status_t BClient::setState(int32_t count, const layer_state_t* states)
1725{
1726 return mFlinger->setClientState(mId, count, states);
1727}
1728
1729// ---------------------------------------------------------------------------
1730
1731GraphicPlane::GraphicPlane()
1732 : mHw(0)
1733{
1734}
1735
1736GraphicPlane::~GraphicPlane() {
1737 delete mHw;
1738}
1739
1740bool GraphicPlane::initialized() const {
1741 return mHw ? true : false;
1742}
1743
1744void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1745 mHw = hw;
1746}
1747
1748void GraphicPlane::setTransform(const Transform& tr) {
1749 mTransform = tr;
1750 mGlobalTransform = mOrientationTransform * mTransform;
1751}
1752
1753status_t GraphicPlane::orientationToTransfrom(
1754 int orientation, int w, int h, Transform* tr)
1755{
1756 float a, b, c, d, x, y;
1757 switch (orientation) {
1758 case ISurfaceComposer::eOrientationDefault:
1759 a=1; b=0; c=0; d=1; x=0; y=0;
1760 break;
1761 case ISurfaceComposer::eOrientation90:
1762 a=0; b=-1; c=1; d=0; x=w; y=0;
1763 break;
1764 case ISurfaceComposer::eOrientation180:
1765 a=-1; b=0; c=0; d=-1; x=w; y=h;
1766 break;
1767 case ISurfaceComposer::eOrientation270:
1768 a=0; b=1; c=-1; d=0; x=0; y=h;
1769 break;
1770 default:
1771 return BAD_VALUE;
1772 }
1773 tr->set(a, b, c, d);
1774 tr->set(x, y);
1775 return NO_ERROR;
1776}
1777
1778status_t GraphicPlane::setOrientation(int orientation)
1779{
1780 const DisplayHardware& hw(displayHardware());
1781 const float w = hw.getWidth();
1782 const float h = hw.getHeight();
1783
1784 if (orientation == ISurfaceComposer::eOrientationDefault) {
1785 // make sure the default orientation is optimal
1786 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001787 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 mGlobalTransform = mTransform;
1789 return NO_ERROR;
1790 }
1791
1792 // If the rotation can be handled in hardware, this is where
1793 // the magic should happen.
1794 if (UNLIKELY(orientation == 42)) {
1795 float a, b, c, d, x, y;
1796 const float r = (3.14159265f / 180.0f) * 42.0f;
1797 const float si = sinf(r);
1798 const float co = cosf(r);
1799 a=co; b=-si; c=si; d=co;
1800 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1801 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1802 mOrientationTransform.set(a, b, c, d);
1803 mOrientationTransform.set(x, y);
1804 } else {
1805 GraphicPlane::orientationToTransfrom(orientation, w, h,
1806 &mOrientationTransform);
1807 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001808 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001809 mGlobalTransform = mOrientationTransform * mTransform;
1810 return NO_ERROR;
1811}
1812
1813const DisplayHardware& GraphicPlane::displayHardware() const {
1814 return *mHw;
1815}
1816
1817const Transform& GraphicPlane::transform() const {
1818 return mGlobalTransform;
1819}
1820
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001821EGLDisplay GraphicPlane::getEGLDisplay() const {
1822 return mHw->getEGLDisplay();
1823}
1824
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825// ---------------------------------------------------------------------------
1826
1827}; // namespace android