blob: 3824024a3475cb8434d5fc31ca71d1590f671007 [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 Agopian1c97d2e2009-08-19 17:46:26 -07001242 layer = createPushBuffersSurfaceLocked(client, d, id,
1243 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001245 layer = createNormalSurfaceLocked(client, d, id,
1246 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 }
1248 break;
1249 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001250 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 break;
1252 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001253 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 break;
1255 }
1256
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001257 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 setTransactionFlags(eTransactionNeeded);
1259 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001260 if (surfaceHandle != 0) {
1261 params->token = surfaceHandle->getToken();
1262 params->identity = surfaceHandle->getIdentity();
1263 params->width = w;
1264 params->height = h;
1265 params->format = format;
1266 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 }
1268
1269 return surfaceHandle;
1270}
1271
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001272sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001273 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001274 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1275 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276{
1277 // initialize the surfaces
1278 switch (format) { // TODO: take h/w into account
1279 case PIXEL_FORMAT_TRANSPARENT:
1280 case PIXEL_FORMAT_TRANSLUCENT:
1281 format = PIXEL_FORMAT_RGBA_8888;
1282 break;
1283 case PIXEL_FORMAT_OPAQUE:
1284 format = PIXEL_FORMAT_RGB_565;
1285 break;
1286 }
1287
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001288 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001289 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 if (LIKELY(err == NO_ERROR)) {
1291 layer->initStates(w, h, flags);
1292 addLayer_l(layer);
1293 } else {
1294 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001295 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 }
1297 return layer;
1298}
1299
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001300sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001301 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1303{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001304 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 layer->initStates(w, h, flags);
1306 addLayer_l(layer);
1307 return layer;
1308}
1309
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001310sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001311 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1313{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001314 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 layer->initStates(w, h, flags);
1316 addLayer_l(layer);
1317 return layer;
1318}
1319
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001320sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001321 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1323{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001324 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 layer->initStates(w, h, flags);
1326 addLayer_l(layer);
1327 return layer;
1328}
1329
Mathias Agopian9a112062009-04-17 19:36:26 -07001330status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331{
Mathias Agopian9a112062009-04-17 19:36:26 -07001332 /*
1333 * called by the window manager, when a surface should be marked for
1334 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001335 *
1336 * The surface is removed from the current and drawing lists, but placed
1337 * in the purgatory queue, so it's not destroyed right-away (we need
1338 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001339 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001340
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001341 Mutex::Autolock _l(mStateLock);
1342 sp<LayerBaseClient> layer = getLayerUser_l(index);
1343 status_t err = purgatorizeLayer_l(layer);
1344 if (err == NO_ERROR) {
1345 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001346 }
1347 return err;
1348}
1349
1350status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1351{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001352 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001353
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001354 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001355 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001356 sp<LayerBaseClient> layer;
1357 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001358 MessageDestroySurface(
1359 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1360 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001361 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001362 sp<LayerBaseClient> l(layer);
1363 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001364 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001365 /*
1366 * remove the layer from the current list -- chances are that it's
1367 * not in the list anyway, because it should have been removed
1368 * already upon request of the client (eg: window manager).
1369 * However, a buggy client could have not done that.
1370 * Since we know we don't have any more clients, we don't need
1371 * to use the purgatory.
1372 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001373 status_t err = flinger->removeLayer_l(l);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001374 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1375 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001376 return true;
1377 }
1378 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001379
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001380 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 return NO_ERROR;
1382}
1383
1384status_t SurfaceFlinger::setClientState(
1385 ClientID cid,
1386 int32_t count,
1387 const layer_state_t* states)
1388{
1389 Mutex::Autolock _l(mStateLock);
1390 uint32_t flags = 0;
1391 cid <<= 16;
1392 for (int i=0 ; i<count ; i++) {
1393 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001394 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001395 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 if (what & ePositionChanged) {
1398 if (layer->setPosition(s.x, s.y))
1399 flags |= eTraversalNeeded;
1400 }
1401 if (what & eLayerChanged) {
1402 if (layer->setLayer(s.z)) {
1403 mCurrentState.layersSortedByZ.reorder(
1404 layer, &Layer::compareCurrentStateZ);
1405 // we need traversal (state changed)
1406 // AND transaction (list changed)
1407 flags |= eTransactionNeeded|eTraversalNeeded;
1408 }
1409 }
1410 if (what & eSizeChanged) {
1411 if (layer->setSize(s.w, s.h))
1412 flags |= eTraversalNeeded;
1413 }
1414 if (what & eAlphaChanged) {
1415 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1416 flags |= eTraversalNeeded;
1417 }
1418 if (what & eMatrixChanged) {
1419 if (layer->setMatrix(s.matrix))
1420 flags |= eTraversalNeeded;
1421 }
1422 if (what & eTransparentRegionChanged) {
1423 if (layer->setTransparentRegionHint(s.transparentRegion))
1424 flags |= eTraversalNeeded;
1425 }
1426 if (what & eVisibilityChanged) {
1427 if (layer->setFlags(s.flags, s.mask))
1428 flags |= eTraversalNeeded;
1429 }
1430 }
1431 }
1432 if (flags) {
1433 setTransactionFlags(flags);
1434 }
1435 return NO_ERROR;
1436}
1437
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001438sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001440 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1441 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442}
1443
1444void SurfaceFlinger::screenReleased(int dpy)
1445{
1446 // this may be called by a signal handler, we can't do too much in here
1447 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1448 signalEvent();
1449}
1450
1451void SurfaceFlinger::screenAcquired(int dpy)
1452{
1453 // this may be called by a signal handler, we can't do too much in here
1454 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1455 signalEvent();
1456}
1457
1458status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1459{
1460 const size_t SIZE = 1024;
1461 char buffer[SIZE];
1462 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001463 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 snprintf(buffer, SIZE, "Permission Denial: "
1465 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1466 IPCThreadState::self()->getCallingPid(),
1467 IPCThreadState::self()->getCallingUid());
1468 result.append(buffer);
1469 } else {
1470 Mutex::Autolock _l(mStateLock);
1471 size_t s = mClientsMap.size();
1472 char name[64];
1473 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001474 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 sprintf(name, " Client (id=0x%08x)", client->cid);
1476 client->dump(name);
1477 }
1478 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1479 const size_t count = currentLayers.size();
1480 for (size_t i=0 ; i<count ; i++) {
1481 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001482 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 const Layer::State& s = layer->drawingState();
1484 snprintf(buffer, SIZE,
1485 "+ %s %p\n"
1486 " "
1487 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1488 "needsBlending=%1d, invalidate=%1d, "
1489 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001490 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 s.z, layer->tx(), layer->ty(), s.w, s.h,
1492 layer->needsBlending(), layer->contentDirty,
1493 s.alpha, s.flags,
1494 s.transform[0], s.transform[1],
1495 s.transform[2], s.transform[3]);
1496 result.append(buffer);
1497 buffer[0] = 0;
1498 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001499 sp<LayerBaseClient> lbc =
1500 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1501 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001502 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 snprintf(buffer, SIZE,
1504 " "
1505 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001506 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 lbc->getIdentity());
1508 }
1509 result.append(buffer);
1510 buffer[0] = 0;
1511 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001512 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1513 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 const LayerBitmap& buf0(l->getBuffer(0));
1515 const LayerBitmap& buf1(l->getBuffer(1));
1516 snprintf(buffer, SIZE,
1517 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001518 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 " freezeLock=%p, swapState=0x%08x\n",
1520 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001521 buf0.getWidth(), buf0.getHeight(),
1522 buf0.getBuffer()->getStride(),
1523 buf1.getWidth(), buf1.getHeight(),
1524 buf1.getBuffer()->getStride(),
1525 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526 l->lcblk->swapState);
1527 }
1528 result.append(buffer);
1529 buffer[0] = 0;
1530 s.transparentRegion.dump(result, "transparentRegion");
1531 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1532 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1533 }
1534 mWormholeRegion.dump(result, "WormholeRegion");
1535 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1536 snprintf(buffer, SIZE,
1537 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1538 mFreezeDisplay?"yes":"no", mFreezeCount,
1539 mCurrentState.orientation, hw.canDraw());
1540 result.append(buffer);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001541 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001542 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001543 const BufferAllocator& alloc(BufferAllocator::get());
1544 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 }
1546 write(fd, result.string(), result.size());
1547 return NO_ERROR;
1548}
1549
1550status_t SurfaceFlinger::onTransact(
1551 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1552{
1553 switch (code) {
1554 case CREATE_CONNECTION:
1555 case OPEN_GLOBAL_TRANSACTION:
1556 case CLOSE_GLOBAL_TRANSACTION:
1557 case SET_ORIENTATION:
1558 case FREEZE_DISPLAY:
1559 case UNFREEZE_DISPLAY:
1560 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 {
1562 // codes that require permission check
1563 IPCThreadState* ipc = IPCThreadState::self();
1564 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001565 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001566 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1567 LOGE("Permission Denial: "
1568 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1569 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 }
1571 }
1572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1574 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001575 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001576 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1577 IPCThreadState* ipc = IPCThreadState::self();
1578 const int pid = ipc->getCallingPid();
1579 const int uid = ipc->getCallingUid();
1580 LOGE("Permission Denial: "
1581 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 return PERMISSION_DENIED;
1583 }
1584 int n;
1585 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001586 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001588 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 return NO_ERROR;
1590 case 1002: // SHOW_UPDATES
1591 n = data.readInt32();
1592 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1593 return NO_ERROR;
1594 case 1003: // SHOW_BACKGROUND
1595 n = data.readInt32();
1596 mDebugBackground = n ? 1 : 0;
1597 return NO_ERROR;
1598 case 1004:{ // repaint everything
1599 Mutex::Autolock _l(mStateLock);
1600 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1601 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1602 signalEvent();
1603 }
1604 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 case 1007: // set mFreezeCount
1606 mFreezeCount = data.readInt32();
1607 return NO_ERROR;
1608 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001609 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 reply->writeInt32(0);
1611 reply->writeInt32(mDebugRegion);
1612 reply->writeInt32(mDebugBackground);
1613 return NO_ERROR;
1614 case 1013: {
1615 Mutex::Autolock _l(mStateLock);
1616 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1617 reply->writeInt32(hw.getPageFlipCount());
1618 }
1619 return NO_ERROR;
1620 }
1621 }
1622 return err;
1623}
1624
1625// ---------------------------------------------------------------------------
1626#if 0
1627#pragma mark -
1628#endif
1629
1630Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1631 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1632{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 const int pgsize = getpagesize();
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001634 const int cblksize = ((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1635
1636 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1637 "SurfaceFlinger Client control-block");
1638
1639 ctrlblk = static_cast<per_client_cblk_t *>(mCblkHeap->getBase());
1640 if (ctrlblk) { // construct the shared structure in-place.
1641 new(ctrlblk) per_client_cblk_t;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 }
1643}
1644
1645Client::~Client() {
1646 if (ctrlblk) {
1647 const int pgsize = getpagesize();
1648 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1649 }
1650}
1651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652int32_t Client::generateId(int pid)
1653{
1654 const uint32_t i = clz( ~mBitmap );
1655 if (i >= NUM_LAYERS_MAX) {
1656 return NO_MEMORY;
1657 }
1658 mPid = pid;
1659 mInUse.add(uint8_t(i));
1660 mBitmap |= 1<<(31-i);
1661 return i;
1662}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001663
1664status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665{
1666 ssize_t idx = mInUse.indexOf(id);
1667 if (idx < 0)
1668 return NAME_NOT_FOUND;
1669 return mLayers.insertAt(layer, idx);
1670}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672void Client::free(int32_t id)
1673{
1674 ssize_t idx = mInUse.remove(uint8_t(id));
1675 if (idx >= 0) {
1676 mBitmap &= ~(1<<(31-id));
1677 mLayers.removeItemsAt(idx);
1678 }
1679}
1680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681bool Client::isValid(int32_t i) const {
1682 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1683}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001684
1685sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1686 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001688 if (idx >= 0) {
1689 lbc = mLayers[idx].promote();
1690 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1691 }
1692 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693}
1694
1695void Client::dump(const char* what)
1696{
1697}
1698
1699// ---------------------------------------------------------------------------
1700#if 0
1701#pragma mark -
1702#endif
1703
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001704BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 : mId(cid), mFlinger(flinger), mCblk(cblk)
1706{
1707}
1708
1709BClient::~BClient() {
1710 // destroy all resources attached to this client
1711 mFlinger->destroyConnection(mId);
1712}
1713
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001714sp<IMemoryHeap> BClient::getControlBlock() const {
1715 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716}
1717
1718sp<ISurface> BClient::createSurface(
1719 ISurfaceFlingerClient::surface_data_t* params, int pid,
1720 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1721 uint32_t flags)
1722{
1723 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1724}
1725
1726status_t BClient::destroySurface(SurfaceID sid)
1727{
1728 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001729 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730}
1731
1732status_t BClient::setState(int32_t count, const layer_state_t* states)
1733{
1734 return mFlinger->setClientState(mId, count, states);
1735}
1736
1737// ---------------------------------------------------------------------------
1738
1739GraphicPlane::GraphicPlane()
1740 : mHw(0)
1741{
1742}
1743
1744GraphicPlane::~GraphicPlane() {
1745 delete mHw;
1746}
1747
1748bool GraphicPlane::initialized() const {
1749 return mHw ? true : false;
1750}
1751
1752void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1753 mHw = hw;
1754}
1755
1756void GraphicPlane::setTransform(const Transform& tr) {
1757 mTransform = tr;
1758 mGlobalTransform = mOrientationTransform * mTransform;
1759}
1760
1761status_t GraphicPlane::orientationToTransfrom(
1762 int orientation, int w, int h, Transform* tr)
1763{
1764 float a, b, c, d, x, y;
1765 switch (orientation) {
1766 case ISurfaceComposer::eOrientationDefault:
1767 a=1; b=0; c=0; d=1; x=0; y=0;
1768 break;
1769 case ISurfaceComposer::eOrientation90:
1770 a=0; b=-1; c=1; d=0; x=w; y=0;
1771 break;
1772 case ISurfaceComposer::eOrientation180:
1773 a=-1; b=0; c=0; d=-1; x=w; y=h;
1774 break;
1775 case ISurfaceComposer::eOrientation270:
1776 a=0; b=1; c=-1; d=0; x=0; y=h;
1777 break;
1778 default:
1779 return BAD_VALUE;
1780 }
1781 tr->set(a, b, c, d);
1782 tr->set(x, y);
1783 return NO_ERROR;
1784}
1785
1786status_t GraphicPlane::setOrientation(int orientation)
1787{
1788 const DisplayHardware& hw(displayHardware());
1789 const float w = hw.getWidth();
1790 const float h = hw.getHeight();
1791
1792 if (orientation == ISurfaceComposer::eOrientationDefault) {
1793 // make sure the default orientation is optimal
1794 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001795 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796 mGlobalTransform = mTransform;
1797 return NO_ERROR;
1798 }
1799
1800 // If the rotation can be handled in hardware, this is where
1801 // the magic should happen.
1802 if (UNLIKELY(orientation == 42)) {
1803 float a, b, c, d, x, y;
1804 const float r = (3.14159265f / 180.0f) * 42.0f;
1805 const float si = sinf(r);
1806 const float co = cosf(r);
1807 a=co; b=-si; c=si; d=co;
1808 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1809 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1810 mOrientationTransform.set(a, b, c, d);
1811 mOrientationTransform.set(x, y);
1812 } else {
1813 GraphicPlane::orientationToTransfrom(orientation, w, h,
1814 &mOrientationTransform);
1815 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001816 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001817 mGlobalTransform = mOrientationTransform * mTransform;
1818 return NO_ERROR;
1819}
1820
1821const DisplayHardware& GraphicPlane::displayHardware() const {
1822 return *mHw;
1823}
1824
1825const Transform& GraphicPlane::transform() const {
1826 return mGlobalTransform;
1827}
1828
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001829EGLDisplay GraphicPlane::getEGLDisplay() const {
1830 return mHw->getEGLDisplay();
1831}
1832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833// ---------------------------------------------------------------------------
1834
1835}; // namespace android