blob: b368db6e2e3be4c9c420be1aeb798542e12fee5a [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 Agopiancbb288b2009-09-07 16:32:45 -070047#include "Buffer.h"
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
50#include "LayerBlur.h"
51#include "LayerBuffer.h"
52#include "LayerDim.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 Agopiancbb288b2009-09-07 16:32:45 -0700176 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -0700179 mHardwareTest("android.permission.HARDWARE_TEST"),
180 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
181 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 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),
Mathias Agopian9795c422009-08-26 16:36:26 -0700189 mDebugInSwapBuffers(0),
190 mLastSwapBufferTime(0),
191 mDebugInTransaction(0),
192 mLastTransactionTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 mConsoleSignals(0),
194 mSecureFrameBuffer(0)
195{
196 init();
197}
198
199void SurfaceFlinger::init()
200{
201 LOGI("SurfaceFlinger is starting");
202
203 // debugging stuff...
204 char value[PROPERTY_VALUE_MAX];
205 property_get("debug.sf.showupdates", value, "0");
206 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 property_get("debug.sf.showbackground", value, "0");
208 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209
210 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
214SurfaceFlinger::~SurfaceFlinger()
215{
216 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217}
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
220{
221 return graphicPlane(0).displayHardware().getOverlayEngine();
222}
223
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700224sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700226 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227}
228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
230{
231 Mutex::Autolock _l(mStateLock);
232 uint32_t token = mTokens.acquire();
233
Mathias Agopianf9d93272009-06-19 17:00:27 -0700234 sp<Client> client = new Client(token, this);
235 if (client->ctrlblk == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 mTokens.release(token);
237 return 0;
238 }
239 status_t err = mClientsMap.add(token, client);
240 if (err < 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 mTokens.release(token);
242 return 0;
243 }
244 sp<BClient> bclient =
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700245 new BClient(this, token, client->getControlBlockMemory());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 return bclient;
247}
248
249void SurfaceFlinger::destroyConnection(ClientID cid)
250{
251 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -0700252 sp<Client> client = mClientsMap.valueFor(cid);
253 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 // free all the layers this client owns
Mathias Agopian3d579642009-06-04 18:46:21 -0700255 Vector< wp<LayerBaseClient> > layers(client->getLayers());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 const size_t count = layers.size();
257 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700258 sp<LayerBaseClient> layer(layers[i].promote());
259 if (layer != 0) {
Mathias Agopian3d579642009-06-04 18:46:21 -0700260 purgatorizeLayer_l(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700261 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 }
263
264 // the resources associated with this client will be freed
265 // during the next transaction, after these surfaces have been
266 // properly removed from the screen
267
268 // remove this client from our ClientID->Client mapping.
269 mClientsMap.removeItem(cid);
270
271 // and add it to the list of disconnected clients
272 mDisconnectedClients.add(client);
273
274 // request a transaction
275 setTransactionFlags(eTransactionNeeded);
276 }
277}
278
279const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
280{
281 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
282 const GraphicPlane& plane(mGraphicPlanes[dpy]);
283 return plane;
284}
285
286GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
287{
288 return const_cast<GraphicPlane&>(
289 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
290}
291
292void SurfaceFlinger::bootFinished()
293{
294 const nsecs_t now = systemTime();
295 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700296 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
297 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
300void SurfaceFlinger::onFirstRef()
301{
302 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
303
304 // Wait for the main thread to be done with its initialization
305 mReadyToRunBarrier.wait();
306}
307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308static inline uint16_t pack565(int r, int g, int b) {
309 return (r<<11)|(g<<5)|b;
310}
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312status_t SurfaceFlinger::readyToRun()
313{
314 LOGI( "SurfaceFlinger's main thread ready to run. "
315 "Initializing graphics H/W...");
316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 // we only support one display currently
318 int dpy = 0;
319
320 {
321 // initialize the main display
322 GraphicPlane& plane(graphicPlane(dpy));
323 DisplayHardware* const hw = new DisplayHardware(this, dpy);
324 plane.setDisplayHardware(hw);
325 }
326
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700327 // create the shared control-block
328 mServerHeap = new MemoryHeapBase(4096,
329 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
330 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
331
332 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
333 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
334
335 new(mServerCblk) surface_flinger_cblk_t;
336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 // initialize primary screen
338 // (other display should be initialized in the same manner, but
339 // asynchronously, as they could come and go. None of this is supported
340 // yet).
341 const GraphicPlane& plane(graphicPlane(dpy));
342 const DisplayHardware& hw = plane.displayHardware();
343 const uint32_t w = hw.getWidth();
344 const uint32_t h = hw.getHeight();
345 const uint32_t f = hw.getFormat();
346 hw.makeCurrent();
347
348 // initialize the shared control block
349 mServerCblk->connected |= 1<<dpy;
350 display_cblk_t* dcblk = mServerCblk->displays + dpy;
351 memset(dcblk, 0, sizeof(display_cblk_t));
352 dcblk->w = w;
353 dcblk->h = h;
354 dcblk->format = f;
355 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
356 dcblk->xdpi = hw.getDpiX();
357 dcblk->ydpi = hw.getDpiY();
358 dcblk->fps = hw.getRefreshRate();
359 dcblk->density = hw.getDensity();
360 asm volatile ("":::"memory");
361
362 // Initialize OpenGL|ES
363 glActiveTexture(GL_TEXTURE0);
364 glBindTexture(GL_TEXTURE_2D, 0);
365 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
366 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
367 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
368 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
369 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
370 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
371 glPixelStorei(GL_PACK_ALIGNMENT, 4);
372 glEnableClientState(GL_VERTEX_ARRAY);
373 glEnable(GL_SCISSOR_TEST);
374 glShadeModel(GL_FLAT);
375 glDisable(GL_DITHER);
376 glDisable(GL_CULL_FACE);
377
378 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
379 const uint16_t g1 = pack565(0x17,0x2f,0x17);
380 const uint16_t textureData[4] = { g0, g1, g1, g0 };
381 glGenTextures(1, &mWormholeTexName);
382 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
383 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
384 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
385 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
386 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
387 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
388 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
389
390 glViewport(0, 0, w, h);
391 glMatrixMode(GL_PROJECTION);
392 glLoadIdentity();
393 glOrthof(0, w, h, 0, 0, 1);
394
395 LayerDim::initDimmer(this, w, h);
396
397 mReadyToRunBarrier.open();
398
399 /*
400 * We're now ready to accept clients...
401 */
402
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700403 // start boot animation
404 property_set("ctl.start", "bootanim");
405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 return NO_ERROR;
407}
408
409// ----------------------------------------------------------------------------
410#if 0
411#pragma mark -
412#pragma mark Events Handler
413#endif
414
415void SurfaceFlinger::waitForEvent()
416{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700417 while (true) {
418 nsecs_t timeout = -1;
419 if (UNLIKELY(isFrozen())) {
420 // wait 5 seconds
421 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
422 const nsecs_t now = systemTime();
423 if (mFreezeDisplayTime == 0) {
424 mFreezeDisplayTime = now;
425 }
426 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
427 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700428 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700429
Mathias Agopianb6683b52009-04-28 03:17:50 -0700430 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700431 if (msg != 0) {
432 mFreezeDisplayTime = 0;
433 switch (msg->what) {
434 case MessageQueue::INVALIDATE:
435 // invalidate message, just return to the main loop
436 return;
437 }
438 } else {
439 // we timed out
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 if (isFrozen()) {
441 // we timed out and are still frozen
442 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
443 mFreezeDisplay, mFreezeCount);
444 mFreezeCount = 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700445 mFreezeDisplay = false;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700446 return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 }
448 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 }
450}
451
452void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700453 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454}
455
456void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700457 // this is the IPC call
458 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459}
460
461void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
462{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700463 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464}
465
466// ----------------------------------------------------------------------------
467#if 0
468#pragma mark -
469#pragma mark Main loop
470#endif
471
472bool SurfaceFlinger::threadLoop()
473{
474 waitForEvent();
475
476 // check for transactions
477 if (UNLIKELY(mConsoleSignals)) {
478 handleConsoleEvents();
479 }
480
481 if (LIKELY(mTransactionCount == 0)) {
482 // if we're in a global transaction, don't do anything.
483 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
484 uint32_t transactionFlags = getTransactionFlags(mask);
485 if (LIKELY(transactionFlags)) {
486 handleTransaction(transactionFlags);
487 }
488 }
489
490 // post surfaces (if needed)
491 handlePageFlip();
492
493 const DisplayHardware& hw(graphicPlane(0).displayHardware());
494 if (LIKELY(hw.canDraw())) {
495 // repaint the framebuffer (if needed)
496 handleRepaint();
497
498 // release the clients before we flip ('cause flip might block)
499 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 postFramebuffer();
502 } else {
503 // pretend we did the post
504 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 usleep(16667); // 60 fps period
506 }
507 return true;
508}
509
510void SurfaceFlinger::postFramebuffer()
511{
Mathias Agopian0926f502009-05-04 14:17:04 -0700512 if (isFrozen()) {
513 // we are not allowed to draw, but pause a bit to make sure
514 // apps don't end up using the whole CPU, if they depend on
515 // surfaceflinger for synchronization.
516 usleep(8333); // 8.3ms ~ 120fps
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 return;
518 }
519
520 if (!mInvalidRegion.isEmpty()) {
521 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700522 const nsecs_t now = systemTime();
523 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700525 mLastSwapBufferTime = systemTime() - now;
526 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528 }
529}
530
531void SurfaceFlinger::handleConsoleEvents()
532{
533 // something to do with the console
534 const DisplayHardware& hw = graphicPlane(0).displayHardware();
535
536 int what = android_atomic_and(0, &mConsoleSignals);
537 if (what & eConsoleAcquired) {
538 hw.acquireScreen();
539 }
540
541 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700542 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 hw.releaseScreen();
545 }
546
547 if (what & eConsoleReleased) {
548 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 hw.releaseScreen();
550 } else {
551 mDeferReleaseConsole = true;
552 }
553 }
554
555 mDirtyRegion.set(hw.bounds());
556}
557
558void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
559{
Mathias Agopian3d579642009-06-04 18:46:21 -0700560 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561
Mathias Agopian3d579642009-06-04 18:46:21 -0700562 { // scope for the lock
563 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700564 const nsecs_t now = systemTime();
565 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700566 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700567 mLastTransactionTime = systemTime() - now;
568 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700569 }
570
571 // do this without lock held
572 const size_t count = ditchedLayers.size();
573 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700574 if (ditchedLayers[i] != 0) {
575 //LOGD("ditching layer %p", ditchedLayers[i].get());
576 ditchedLayers[i]->ditch();
577 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700578 }
579}
580
581void SurfaceFlinger::handleTransactionLocked(
582 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
583{
584 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 const size_t count = currentLayers.size();
586
587 /*
588 * Traversal of the children
589 * (perform the transaction for each of them if needed)
590 */
591
592 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
593 if (layersNeedTransaction) {
594 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
597 if (!trFlags) continue;
598
599 const uint32_t flags = layer->doTransaction(0);
600 if (flags & Layer::eVisibleRegion)
601 mVisibleRegionsDirty = true;
602
603 if (flags & Layer::eRestartTransaction) {
604 // restart the transaction, but back-off a little
605 layer->setTransactionFlags(eTransactionNeeded);
606 setTransactionFlags(eTraversalNeeded, ms2ns(8));
607 }
608 }
609 }
610
611 /*
612 * Perform our own transaction if needed
613 */
614
615 if (transactionFlags & eTransactionNeeded) {
616 if (mCurrentState.orientation != mDrawingState.orientation) {
617 // the orientation has changed, recompute all visible regions
618 // and invalidate everything.
619
620 const int dpy = 0;
621 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700622 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 GraphicPlane& plane(graphicPlane(dpy));
624 plane.setOrientation(orientation);
625
626 // update the shared control block
627 const DisplayHardware& hw(plane.displayHardware());
628 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
629 dcblk->orientation = orientation;
630 if (orientation & eOrientationSwapMask) {
631 // 90 or 270 degrees orientation
632 dcblk->w = hw.getHeight();
633 dcblk->h = hw.getWidth();
634 } else {
635 dcblk->w = hw.getWidth();
636 dcblk->h = hw.getHeight();
637 }
638
639 mVisibleRegionsDirty = true;
640 mDirtyRegion.set(hw.bounds());
Mathias Agopianc08731e2009-03-27 18:11:38 -0700641 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 }
643
644 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
645 // freezing or unfreezing the display -> trigger animation if needed
646 mFreezeDisplay = mCurrentState.freezeDisplay;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 }
648
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700649 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
650 // layers have been added
651 mVisibleRegionsDirty = true;
652 }
653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 // some layers might have been removed, so
655 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700656 if (mLayersRemoved) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700658 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700659 const size_t count = previousLayers.size();
660 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700661 const sp<LayerBase>& layer(previousLayers[i]);
662 if (currentLayers.indexOf( layer ) < 0) {
663 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700664 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700665 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700666 }
667 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 }
669
670 // get rid of all resources we don't need anymore
671 // (layers and clients)
672 free_resources_l();
673 }
674
675 commitTransaction();
676}
677
678sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
679{
680 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
681}
682
683void SurfaceFlinger::computeVisibleRegions(
684 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
685{
686 const GraphicPlane& plane(graphicPlane(0));
687 const Transform& planeTransform(plane.transform());
688
689 Region aboveOpaqueLayers;
690 Region aboveCoveredLayers;
691 Region dirty;
692
693 bool secureFrameBuffer = false;
694
695 size_t i = currentLayers.size();
696 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700697 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 layer->validateVisibility(planeTransform);
699
700 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700701 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702
703 // handle hidden surfaces by setting the visible region to empty
704 Region opaqueRegion;
705 Region visibleRegion;
706 Region coveredRegion;
Mathias Agopian97011222009-07-28 10:57:27 -0700707 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700709 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 visibleRegion.set(bounds);
711 coveredRegion = visibleRegion;
712
713 // Remove the transparent area from the visible region
714 if (translucent) {
715 visibleRegion.subtractSelf(layer->transparentRegionScreen);
716 }
717
718 // compute the opaque region
719 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
720 // the opaque region is the visible region
721 opaqueRegion = visibleRegion;
722 }
723 }
724
725 // subtract the opaque region covered by the layers above us
726 visibleRegion.subtractSelf(aboveOpaqueLayers);
727 coveredRegion.andSelf(aboveCoveredLayers);
728
729 // compute this layer's dirty region
730 if (layer->contentDirty) {
731 // we need to invalidate the whole region
732 dirty = visibleRegion;
733 // as well, as the old visible region
734 dirty.orSelf(layer->visibleRegionScreen);
735 layer->contentDirty = false;
736 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700737 /* compute the exposed region:
738 * exposed = what's VISIBLE and NOT COVERED now
739 * but was COVERED before
740 */
741 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 }
743 dirty.subtractSelf(aboveOpaqueLayers);
744
745 // accumulate to the screen dirty region
746 dirtyRegion.orSelf(dirty);
747
Mathias Agopian62b74442009-04-14 23:02:51 -0700748 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700750 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751
752 // Store the visible region is screen space
753 layer->setVisibleRegion(visibleRegion);
754 layer->setCoveredRegion(coveredRegion);
755
Mathias Agopian97011222009-07-28 10:57:27 -0700756 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 if (layer->isSecure() && !visibleRegion.isEmpty()) {
758 secureFrameBuffer = true;
759 }
760 }
761
Mathias Agopian97011222009-07-28 10:57:27 -0700762 // invalidate the areas where a layer was removed
763 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
764 mDirtyRegionRemovedLayer.clear();
765
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 mSecureFrameBuffer = secureFrameBuffer;
767 opaqueRegion = aboveOpaqueLayers;
768}
769
770
771void SurfaceFlinger::commitTransaction()
772{
773 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700774 mResizeTransationPending = false;
775 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776}
777
778void SurfaceFlinger::handlePageFlip()
779{
780 bool visibleRegions = mVisibleRegionsDirty;
781 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
782 visibleRegions |= lockPageFlip(currentLayers);
783
784 const DisplayHardware& hw = graphicPlane(0).displayHardware();
785 const Region screenRegion(hw.bounds());
786 if (visibleRegions) {
787 Region opaqueRegion;
788 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
789 mWormholeRegion = screenRegion.subtract(opaqueRegion);
790 mVisibleRegionsDirty = false;
791 }
792
793 unlockPageFlip(currentLayers);
794 mDirtyRegion.andSelf(screenRegion);
795}
796
797bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
798{
799 bool recomputeVisibleRegions = false;
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->lockPageFlip(recomputeVisibleRegions);
805 }
806 return recomputeVisibleRegions;
807}
808
809void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
810{
811 const GraphicPlane& plane(graphicPlane(0));
812 const Transform& planeTransform(plane.transform());
813 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700814 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700816 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 layer->unlockPageFlip(planeTransform, mDirtyRegion);
818 }
819}
820
Mathias Agopianb8a55602009-06-26 19:06:36 -0700821
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822void SurfaceFlinger::handleRepaint()
823{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700824 // compute the invalid region
825 mInvalidRegion.orSelf(mDirtyRegion);
826 if (mInvalidRegion.isEmpty()) {
827 // nothing to do
828 return;
829 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
831 if (UNLIKELY(mDebugRegion)) {
832 debugFlashRegions();
833 }
834
Mathias Agopianb8a55602009-06-26 19:06:36 -0700835 // set the frame buffer
836 const DisplayHardware& hw(graphicPlane(0).displayHardware());
837 glMatrixMode(GL_MODELVIEW);
838 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839
840 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700841 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
842 (flags & DisplayHardware::BUFFER_PRESERVED))
843 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700844 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
845 // takes a rectangle, we must make sure to update that whole
846 // rectangle in that case
847 if (flags & DisplayHardware::SWAP_RECTANGLE) {
848 // FIXME: we really should be able to pass a region to
849 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 mDirtyRegion.set(mInvalidRegion.bounds());
851 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700852 // in the BUFFER_PRESERVED case, obviously, we can update only
853 // what's needed and nothing more.
854 // NOTE: this is NOT a common case, as preserving the backbuffer
855 // is costly and usually involves copying the whole update back.
856 }
857 } else {
858 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
859 // We need to redraw the rectangle that will be updated
860 // (pushed to the framebuffer).
861 // This is needed because UPDATE_ON_DEMAND only takes one
862 // rectangle instead of a region (see DisplayHardware::flip())
863 mDirtyRegion.set(mInvalidRegion.bounds());
864 } else {
865 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866 mDirtyRegion.set(hw.bounds());
867 mInvalidRegion = mDirtyRegion;
868 }
869 }
870
871 // compose all surfaces
872 composeSurfaces(mDirtyRegion);
873
874 // clear the dirty regions
875 mDirtyRegion.clear();
876}
877
878void SurfaceFlinger::composeSurfaces(const Region& dirty)
879{
880 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
881 // should never happen unless the window manager has a bug
882 // draw something...
883 drawWormhole();
884 }
885 const SurfaceFlinger& flinger(*this);
886 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
887 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700888 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700890 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 const Region& visibleRegion(layer->visibleRegionScreen);
892 if (!visibleRegion.isEmpty()) {
893 const Region clip(dirty.intersect(visibleRegion));
894 if (!clip.isEmpty()) {
895 layer->draw(clip);
896 }
897 }
898 }
899}
900
901void SurfaceFlinger::unlockClients()
902{
903 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
904 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700905 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700907 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 layer->finishPageFlip();
909 }
910}
911
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912void SurfaceFlinger::debugFlashRegions()
913{
Mathias Agopian0926f502009-05-04 14:17:04 -0700914 const DisplayHardware& hw(graphicPlane(0).displayHardware());
915 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700916
917 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
918 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700919 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
920 mDirtyRegion.bounds() : hw.bounds());
921 composeSurfaces(repaint);
922 }
923
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 glDisable(GL_TEXTURE_2D);
925 glDisable(GL_BLEND);
926 glDisable(GL_DITHER);
927 glDisable(GL_SCISSOR_TEST);
928
Mathias Agopian0926f502009-05-04 14:17:04 -0700929 static int toggle = 0;
930 toggle = 1 - toggle;
931 if (toggle) {
932 glColor4x(0x10000, 0, 0x10000, 0x10000);
933 } else {
934 glColor4x(0x10000, 0x10000, 0, 0x10000);
935 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936
Mathias Agopian20f68782009-05-11 00:03:41 -0700937 Region::const_iterator it = mDirtyRegion.begin();
938 Region::const_iterator const end = mDirtyRegion.end();
939 while (it != end) {
940 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 GLfloat vertices[][2] = {
942 { r.left, r.top },
943 { r.left, r.bottom },
944 { r.right, r.bottom },
945 { r.right, r.top }
946 };
947 glVertexPointer(2, GL_FLOAT, 0, vertices);
948 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
949 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700950
Mathias Agopianb8a55602009-06-26 19:06:36 -0700951 if (mInvalidRegion.isEmpty()) {
952 mDirtyRegion.dump("mDirtyRegion");
953 mInvalidRegion.dump("mInvalidRegion");
954 }
955 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956
957 if (mDebugRegion > 1)
958 usleep(mDebugRegion * 1000);
959
960 glEnable(GL_SCISSOR_TEST);
961 //mDirtyRegion.dump("mDirtyRegion");
962}
963
964void SurfaceFlinger::drawWormhole() const
965{
966 const Region region(mWormholeRegion.intersect(mDirtyRegion));
967 if (region.isEmpty())
968 return;
969
970 const DisplayHardware& hw(graphicPlane(0).displayHardware());
971 const int32_t width = hw.getWidth();
972 const int32_t height = hw.getHeight();
973
974 glDisable(GL_BLEND);
975 glDisable(GL_DITHER);
976
977 if (LIKELY(!mDebugBackground)) {
978 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700979 Region::const_iterator it = region.begin();
980 Region::const_iterator const end = region.end();
981 while (it != end) {
982 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 const GLint sy = height - (r.top + r.height());
984 glScissor(r.left, sy, r.width(), r.height());
985 glClear(GL_COLOR_BUFFER_BIT);
986 }
987 } else {
988 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
989 { width, height }, { 0, height } };
990 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
991 glVertexPointer(2, GL_SHORT, 0, vertices);
992 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
993 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
994 glEnable(GL_TEXTURE_2D);
995 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
996 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
997 glMatrixMode(GL_TEXTURE);
998 glLoadIdentity();
999 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001000 Region::const_iterator it = region.begin();
1001 Region::const_iterator const end = region.end();
1002 while (it != end) {
1003 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 const GLint sy = height - (r.top + r.height());
1005 glScissor(r.left, sy, r.width(), r.height());
1006 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1007 }
1008 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1009 }
1010}
1011
1012void SurfaceFlinger::debugShowFPS() const
1013{
1014 static int mFrameCount;
1015 static int mLastFrameCount = 0;
1016 static nsecs_t mLastFpsTime = 0;
1017 static float mFps = 0;
1018 mFrameCount++;
1019 nsecs_t now = systemTime();
1020 nsecs_t diff = now - mLastFpsTime;
1021 if (diff > ms2ns(250)) {
1022 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1023 mLastFpsTime = now;
1024 mLastFrameCount = mFrameCount;
1025 }
1026 // XXX: mFPS has the value we want
1027 }
1028
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001029status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030{
1031 Mutex::Autolock _l(mStateLock);
1032 addLayer_l(layer);
1033 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1034 return NO_ERROR;
1035}
1036
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001037status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038{
1039 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001040 status_t err = purgatorizeLayer_l(layer);
1041 if (err == NO_ERROR)
1042 setTransactionFlags(eTransactionNeeded);
1043 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044}
1045
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001046status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047{
1048 layer->forceVisibilityTransaction();
1049 setTransactionFlags(eTraversalNeeded);
1050 return NO_ERROR;
1051}
1052
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001053status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054{
Mathias Agopian0852e672009-09-04 19:50:23 -07001055 if (layer == 0)
1056 return BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 ssize_t i = mCurrentState.layersSortedByZ.add(
1058 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001059 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1060 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061 mLayerMap.add(lbc->serverIndex(), lbc);
1062 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 return NO_ERROR;
1064}
1065
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067{
1068 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1069 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001070 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001071 sp<LayerBaseClient> layer =
1072 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001073 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 mLayerMap.removeItem(layer->serverIndex());
1075 }
1076 return NO_ERROR;
1077 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001078 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079}
1080
Mathias Agopian9a112062009-04-17 19:36:26 -07001081status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1082{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001083 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001084 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001085
Mathias Agopian3d579642009-06-04 18:46:21 -07001086 // it's possible that we don't find a layer, because it might
1087 // have been destroyed already -- this is not technically an error
1088 // from the user because there is a race between BClient::destroySurface(),
1089 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001090 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1091}
1092
1093
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094void SurfaceFlinger::free_resources_l()
1095{
1096 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001097 mLayersRemoved = false;
1098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001100 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 const size_t count = disconnectedClients.size();
1102 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001103 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 }
1106 disconnectedClients.clear();
1107}
1108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1110{
1111 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1112}
1113
1114uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1115{
1116 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1117 if ((old & flags)==0) { // wake the server up
1118 if (delay > 0) {
1119 signalDelayedEvent(delay);
1120 } else {
1121 signalEvent();
1122 }
1123 }
1124 return old;
1125}
1126
1127void SurfaceFlinger::openGlobalTransaction()
1128{
1129 android_atomic_inc(&mTransactionCount);
1130}
1131
1132void SurfaceFlinger::closeGlobalTransaction()
1133{
1134 if (android_atomic_dec(&mTransactionCount) == 1) {
1135 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001136
1137 // if there is a transaction with a resize, wait for it to
1138 // take effect before returning.
1139 Mutex::Autolock _l(mStateLock);
1140 while (mResizeTransationPending) {
1141 mTransactionCV.wait(mStateLock);
1142 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 }
1144}
1145
1146status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1147{
1148 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1149 return BAD_VALUE;
1150
1151 Mutex::Autolock _l(mStateLock);
1152 mCurrentState.freezeDisplay = 1;
1153 setTransactionFlags(eTransactionNeeded);
1154
1155 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001156 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 return NO_ERROR;
1158}
1159
1160status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1161{
1162 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1163 return BAD_VALUE;
1164
1165 Mutex::Autolock _l(mStateLock);
1166 mCurrentState.freezeDisplay = 0;
1167 setTransactionFlags(eTransactionNeeded);
1168
1169 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001170 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 return NO_ERROR;
1172}
1173
Mathias Agopianc08731e2009-03-27 18:11:38 -07001174int SurfaceFlinger::setOrientation(DisplayID dpy,
1175 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176{
1177 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1178 return BAD_VALUE;
1179
1180 Mutex::Autolock _l(mStateLock);
1181 if (mCurrentState.orientation != orientation) {
1182 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001183 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 mCurrentState.orientation = orientation;
1185 setTransactionFlags(eTransactionNeeded);
1186 mTransactionCV.wait(mStateLock);
1187 } else {
1188 orientation = BAD_VALUE;
1189 }
1190 }
1191 return orientation;
1192}
1193
1194sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1195 ISurfaceFlingerClient::surface_data_t* params,
1196 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1197 uint32_t flags)
1198{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001199 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001201
1202 if (int32_t(w|h) < 0) {
1203 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1204 int(w), int(h));
1205 return surfaceHandle;
1206 }
1207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001209 sp<Client> client = mClientsMap.valueFor(clientId);
1210 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1212 return surfaceHandle;
1213 }
1214
1215 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001216 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1218 LOGE("createSurface() failed, generateId = %d", id);
1219 return surfaceHandle;
1220 }
1221
1222 switch (flags & eFXSurfaceMask) {
1223 case eFXSurfaceNormal:
1224 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001225 layer = createPushBuffersSurfaceLocked(client, d, id,
1226 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001228 layer = createNormalSurfaceLocked(client, d, id,
1229 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230 }
1231 break;
1232 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001233 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 break;
1235 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001236 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 break;
1238 }
1239
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001240 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 setTransactionFlags(eTransactionNeeded);
1242 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001243 if (surfaceHandle != 0) {
1244 params->token = surfaceHandle->getToken();
1245 params->identity = surfaceHandle->getIdentity();
1246 params->width = w;
1247 params->height = h;
1248 params->format = format;
1249 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 }
1251
1252 return surfaceHandle;
1253}
1254
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001255sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001256 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001257 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1258 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259{
1260 // initialize the surfaces
1261 switch (format) { // TODO: take h/w into account
1262 case PIXEL_FORMAT_TRANSPARENT:
1263 case PIXEL_FORMAT_TRANSLUCENT:
1264 format = PIXEL_FORMAT_RGBA_8888;
1265 break;
1266 case PIXEL_FORMAT_OPAQUE:
1267 format = PIXEL_FORMAT_RGB_565;
1268 break;
1269 }
1270
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001271 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001272 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 if (LIKELY(err == NO_ERROR)) {
1274 layer->initStates(w, h, flags);
1275 addLayer_l(layer);
1276 } else {
1277 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001278 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 }
1280 return layer;
1281}
1282
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001283sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001284 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1286{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 layer->initStates(w, h, flags);
1289 addLayer_l(layer);
1290 return layer;
1291}
1292
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001293sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001294 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1296{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001297 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 layer->initStates(w, h, flags);
1299 addLayer_l(layer);
1300 return layer;
1301}
1302
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001303sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001304 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1306{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001307 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 layer->initStates(w, h, flags);
1309 addLayer_l(layer);
1310 return layer;
1311}
1312
Mathias Agopian9a112062009-04-17 19:36:26 -07001313status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314{
Mathias Agopian9a112062009-04-17 19:36:26 -07001315 /*
1316 * called by the window manager, when a surface should be marked for
1317 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001318 *
1319 * The surface is removed from the current and drawing lists, but placed
1320 * in the purgatory queue, so it's not destroyed right-away (we need
1321 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001322 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001323
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001324 Mutex::Autolock _l(mStateLock);
1325 sp<LayerBaseClient> layer = getLayerUser_l(index);
1326 status_t err = purgatorizeLayer_l(layer);
1327 if (err == NO_ERROR) {
1328 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001329 }
1330 return err;
1331}
1332
1333status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1334{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001335 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001336
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001337 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001338 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001339 sp<LayerBaseClient> layer;
1340 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001341 MessageDestroySurface(
1342 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1343 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001344 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001345 sp<LayerBaseClient> l(layer);
1346 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001347 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001348 /*
1349 * remove the layer from the current list -- chances are that it's
1350 * not in the list anyway, because it should have been removed
1351 * already upon request of the client (eg: window manager).
1352 * However, a buggy client could have not done that.
1353 * Since we know we don't have any more clients, we don't need
1354 * to use the purgatory.
1355 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001356 status_t err = flinger->removeLayer_l(l);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001357 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1358 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001359 return true;
1360 }
1361 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001362
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001363 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 return NO_ERROR;
1365}
1366
1367status_t SurfaceFlinger::setClientState(
1368 ClientID cid,
1369 int32_t count,
1370 const layer_state_t* states)
1371{
1372 Mutex::Autolock _l(mStateLock);
1373 uint32_t flags = 0;
1374 cid <<= 16;
1375 for (int i=0 ; i<count ; i++) {
1376 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001377 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001378 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 if (what & ePositionChanged) {
1381 if (layer->setPosition(s.x, s.y))
1382 flags |= eTraversalNeeded;
1383 }
1384 if (what & eLayerChanged) {
1385 if (layer->setLayer(s.z)) {
1386 mCurrentState.layersSortedByZ.reorder(
1387 layer, &Layer::compareCurrentStateZ);
1388 // we need traversal (state changed)
1389 // AND transaction (list changed)
1390 flags |= eTransactionNeeded|eTraversalNeeded;
1391 }
1392 }
1393 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001394 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001396 mResizeTransationPending = true;
1397 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 }
1399 if (what & eAlphaChanged) {
1400 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1401 flags |= eTraversalNeeded;
1402 }
1403 if (what & eMatrixChanged) {
1404 if (layer->setMatrix(s.matrix))
1405 flags |= eTraversalNeeded;
1406 }
1407 if (what & eTransparentRegionChanged) {
1408 if (layer->setTransparentRegionHint(s.transparentRegion))
1409 flags |= eTraversalNeeded;
1410 }
1411 if (what & eVisibilityChanged) {
1412 if (layer->setFlags(s.flags, s.mask))
1413 flags |= eTraversalNeeded;
1414 }
1415 }
1416 }
1417 if (flags) {
1418 setTransactionFlags(flags);
1419 }
1420 return NO_ERROR;
1421}
1422
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001423sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001425 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1426 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427}
1428
1429void SurfaceFlinger::screenReleased(int dpy)
1430{
1431 // this may be called by a signal handler, we can't do too much in here
1432 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1433 signalEvent();
1434}
1435
1436void SurfaceFlinger::screenAcquired(int dpy)
1437{
1438 // this may be called by a signal handler, we can't do too much in here
1439 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1440 signalEvent();
1441}
1442
1443status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1444{
1445 const size_t SIZE = 1024;
1446 char buffer[SIZE];
1447 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001448 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 snprintf(buffer, SIZE, "Permission Denial: "
1450 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1451 IPCThreadState::self()->getCallingPid(),
1452 IPCThreadState::self()->getCallingUid());
1453 result.append(buffer);
1454 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001455
1456 // figure out if we're stuck somewhere
1457 const nsecs_t now = systemTime();
1458 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1459 const nsecs_t inTransaction(mDebugInTransaction);
1460 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1461 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1462
1463 // Try to get the main lock, but don't insist if we can't
1464 // (this would indicate SF is stuck, but we want to be able to
1465 // print something in dumpsys).
1466 int retry = 3;
1467 while (mStateLock.tryLock()<0 && --retry>=0) {
1468 usleep(1000000);
1469 }
1470 const bool locked(retry >= 0);
1471 if (!locked) {
1472 snprintf(buffer, SIZE,
1473 "SurfaceFlinger appears to be unresponsive, "
1474 "dumping anyways (no locks held)\n");
1475 result.append(buffer);
1476 }
1477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 size_t s = mClientsMap.size();
1479 char name[64];
1480 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001481 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 sprintf(name, " Client (id=0x%08x)", client->cid);
1483 client->dump(name);
1484 }
1485 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1486 const size_t count = currentLayers.size();
1487 for (size_t i=0 ; i<count ; i++) {
1488 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001489 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 const Layer::State& s = layer->drawingState();
1491 snprintf(buffer, SIZE,
1492 "+ %s %p\n"
1493 " "
1494 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1495 "needsBlending=%1d, invalidate=%1d, "
1496 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001497 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 s.z, layer->tx(), layer->ty(), s.w, s.h,
1499 layer->needsBlending(), layer->contentDirty,
1500 s.alpha, s.flags,
1501 s.transform[0], s.transform[1],
1502 s.transform[2], s.transform[3]);
1503 result.append(buffer);
1504 buffer[0] = 0;
1505 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001506 sp<LayerBaseClient> lbc =
1507 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1508 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001509 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 snprintf(buffer, SIZE,
1511 " "
1512 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001513 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 lbc->getIdentity());
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001515
1516 result.append(buffer);
1517 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001520 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1521 if (l != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001522 result.append( l->lcblk->dump(" ") );
1523 sp<const Buffer> buf0(l->getBuffer(0));
1524 sp<const Buffer> buf1(l->getBuffer(1));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 snprintf(buffer, SIZE,
1526 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001527 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001528 " freezeLock=%p\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529 l->pixelFormat(),
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001530 buf0->getWidth(), buf0->getHeight(), buf0->getStride(),
1531 buf1->getWidth(), buf1->getHeight(), buf1->getStride(),
1532 l->getFreezeLock().get());
1533 result.append(buffer);
1534 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536 s.transparentRegion.dump(result, "transparentRegion");
1537 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1538 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1539 }
1540 mWormholeRegion.dump(result, "WormholeRegion");
1541 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1542 snprintf(buffer, SIZE,
1543 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1544 mFreezeDisplay?"yes":"no", mFreezeCount,
1545 mCurrentState.orientation, hw.canDraw());
1546 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001547 snprintf(buffer, SIZE,
1548 " last eglSwapBuffers() time: %f us\n"
1549 " last transaction time : %f us\n",
1550 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1551 result.append(buffer);
1552 if (inSwapBuffersDuration || !locked) {
1553 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1554 inSwapBuffersDuration/1000.0);
1555 result.append(buffer);
1556 }
1557 if (inTransactionDuration || !locked) {
1558 snprintf(buffer, SIZE, " transaction time: %f us\n",
1559 inTransactionDuration/1000.0);
1560 result.append(buffer);
1561 }
Mathias Agopian759fdb22009-07-02 17:33:40 -07001562 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001563 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001564 const BufferAllocator& alloc(BufferAllocator::get());
1565 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001566
1567 if (locked) {
1568 mStateLock.unlock();
1569 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 }
1571 write(fd, result.string(), result.size());
1572 return NO_ERROR;
1573}
1574
1575status_t SurfaceFlinger::onTransact(
1576 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1577{
1578 switch (code) {
1579 case CREATE_CONNECTION:
1580 case OPEN_GLOBAL_TRANSACTION:
1581 case CLOSE_GLOBAL_TRANSACTION:
1582 case SET_ORIENTATION:
1583 case FREEZE_DISPLAY:
1584 case UNFREEZE_DISPLAY:
1585 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 {
1587 // codes that require permission check
1588 IPCThreadState* ipc = IPCThreadState::self();
1589 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001590 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001591 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1592 LOGE("Permission Denial: "
1593 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1594 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595 }
1596 }
1597 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1599 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001600 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001601 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1602 IPCThreadState* ipc = IPCThreadState::self();
1603 const int pid = ipc->getCallingPid();
1604 const int uid = ipc->getCallingUid();
1605 LOGE("Permission Denial: "
1606 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 return PERMISSION_DENIED;
1608 }
1609 int n;
1610 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001611 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001613 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 return NO_ERROR;
1615 case 1002: // SHOW_UPDATES
1616 n = data.readInt32();
1617 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1618 return NO_ERROR;
1619 case 1003: // SHOW_BACKGROUND
1620 n = data.readInt32();
1621 mDebugBackground = n ? 1 : 0;
1622 return NO_ERROR;
1623 case 1004:{ // repaint everything
1624 Mutex::Autolock _l(mStateLock);
1625 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1626 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1627 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001628 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001630 case 1005:{ // force transaction
1631 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1632 return NO_ERROR;
1633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634 case 1007: // set mFreezeCount
1635 mFreezeCount = data.readInt32();
1636 return NO_ERROR;
1637 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001638 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 reply->writeInt32(0);
1640 reply->writeInt32(mDebugRegion);
1641 reply->writeInt32(mDebugBackground);
1642 return NO_ERROR;
1643 case 1013: {
1644 Mutex::Autolock _l(mStateLock);
1645 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1646 reply->writeInt32(hw.getPageFlipCount());
1647 }
1648 return NO_ERROR;
1649 }
1650 }
1651 return err;
1652}
1653
1654// ---------------------------------------------------------------------------
1655#if 0
1656#pragma mark -
1657#endif
1658
1659Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1660 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1661{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001663 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001664
1665 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1666 "SurfaceFlinger Client control-block");
1667
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001668 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001669 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001670 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 }
1672}
1673
1674Client::~Client() {
1675 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001676 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 }
1678}
1679
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680int32_t Client::generateId(int pid)
1681{
1682 const uint32_t i = clz( ~mBitmap );
1683 if (i >= NUM_LAYERS_MAX) {
1684 return NO_MEMORY;
1685 }
1686 mPid = pid;
1687 mInUse.add(uint8_t(i));
1688 mBitmap |= 1<<(31-i);
1689 return i;
1690}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001691
1692status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693{
1694 ssize_t idx = mInUse.indexOf(id);
1695 if (idx < 0)
1696 return NAME_NOT_FOUND;
1697 return mLayers.insertAt(layer, idx);
1698}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001699
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700void Client::free(int32_t id)
1701{
1702 ssize_t idx = mInUse.remove(uint8_t(id));
1703 if (idx >= 0) {
1704 mBitmap &= ~(1<<(31-id));
1705 mLayers.removeItemsAt(idx);
1706 }
1707}
1708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709bool Client::isValid(int32_t i) const {
1710 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1711}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001712
1713sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1714 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001716 if (idx >= 0) {
1717 lbc = mLayers[idx].promote();
1718 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1719 }
1720 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721}
1722
1723void Client::dump(const char* what)
1724{
1725}
1726
1727// ---------------------------------------------------------------------------
1728#if 0
1729#pragma mark -
1730#endif
1731
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001732BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 : mId(cid), mFlinger(flinger), mCblk(cblk)
1734{
1735}
1736
1737BClient::~BClient() {
1738 // destroy all resources attached to this client
1739 mFlinger->destroyConnection(mId);
1740}
1741
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001742sp<IMemoryHeap> BClient::getControlBlock() const {
1743 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744}
1745
1746sp<ISurface> BClient::createSurface(
1747 ISurfaceFlingerClient::surface_data_t* params, int pid,
1748 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1749 uint32_t flags)
1750{
1751 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1752}
1753
1754status_t BClient::destroySurface(SurfaceID sid)
1755{
1756 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001757 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758}
1759
1760status_t BClient::setState(int32_t count, const layer_state_t* states)
1761{
1762 return mFlinger->setClientState(mId, count, states);
1763}
1764
1765// ---------------------------------------------------------------------------
1766
1767GraphicPlane::GraphicPlane()
1768 : mHw(0)
1769{
1770}
1771
1772GraphicPlane::~GraphicPlane() {
1773 delete mHw;
1774}
1775
1776bool GraphicPlane::initialized() const {
1777 return mHw ? true : false;
1778}
1779
1780void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1781 mHw = hw;
1782}
1783
1784void GraphicPlane::setTransform(const Transform& tr) {
1785 mTransform = tr;
1786 mGlobalTransform = mOrientationTransform * mTransform;
1787}
1788
1789status_t GraphicPlane::orientationToTransfrom(
1790 int orientation, int w, int h, Transform* tr)
1791{
1792 float a, b, c, d, x, y;
1793 switch (orientation) {
1794 case ISurfaceComposer::eOrientationDefault:
1795 a=1; b=0; c=0; d=1; x=0; y=0;
1796 break;
1797 case ISurfaceComposer::eOrientation90:
1798 a=0; b=-1; c=1; d=0; x=w; y=0;
1799 break;
1800 case ISurfaceComposer::eOrientation180:
1801 a=-1; b=0; c=0; d=-1; x=w; y=h;
1802 break;
1803 case ISurfaceComposer::eOrientation270:
1804 a=0; b=1; c=-1; d=0; x=0; y=h;
1805 break;
1806 default:
1807 return BAD_VALUE;
1808 }
1809 tr->set(a, b, c, d);
1810 tr->set(x, y);
1811 return NO_ERROR;
1812}
1813
1814status_t GraphicPlane::setOrientation(int orientation)
1815{
1816 const DisplayHardware& hw(displayHardware());
1817 const float w = hw.getWidth();
1818 const float h = hw.getHeight();
1819
1820 if (orientation == ISurfaceComposer::eOrientationDefault) {
1821 // make sure the default orientation is optimal
1822 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001823 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001824 mGlobalTransform = mTransform;
1825 return NO_ERROR;
1826 }
1827
1828 // If the rotation can be handled in hardware, this is where
1829 // the magic should happen.
1830 if (UNLIKELY(orientation == 42)) {
1831 float a, b, c, d, x, y;
1832 const float r = (3.14159265f / 180.0f) * 42.0f;
1833 const float si = sinf(r);
1834 const float co = cosf(r);
1835 a=co; b=-si; c=si; d=co;
1836 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1837 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1838 mOrientationTransform.set(a, b, c, d);
1839 mOrientationTransform.set(x, y);
1840 } else {
1841 GraphicPlane::orientationToTransfrom(orientation, w, h,
1842 &mOrientationTransform);
1843 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001844 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001845 mGlobalTransform = mOrientationTransform * mTransform;
1846 return NO_ERROR;
1847}
1848
1849const DisplayHardware& GraphicPlane::displayHardware() const {
1850 return *mHw;
1851}
1852
1853const Transform& GraphicPlane::transform() const {
1854 return mGlobalTransform;
1855}
1856
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001857EGLDisplay GraphicPlane::getEGLDisplay() const {
1858 return mHw->getEGLDisplay();
1859}
1860
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001861// ---------------------------------------------------------------------------
1862
1863}; // namespace android