blob: 8685f9951f926d93c9e236fccb2a24b27034ff8d [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-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 Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080042
43#include <pixelflinger/pixelflinger.h>
44#include <GLES/gl.h>
45
46#include "clz.h"
Mathias Agopian9779b222009-09-07 16:32:45 -070047#include "Buffer.h"
Mathias Agopian1473f462009-04-10 14:24:30 -070048#include "BufferAllocator.h"
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Mathias Agopian627e7b52009-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 Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -070089 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -070096 LOGE_IF(layers[idx]!=key,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian1473f462009-04-10 14:24:30 -070098 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 return idx;
100 }
101 return i;
102}
103
104ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian1473f462009-04-10 14:24:30 -0700105 const sp<LayerBase>& layer,
106 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700112 sp<LayerBase> const* a = layers.array();
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700135ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700140 LOGE_IF(layers[index]!=layer,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian1473f462009-04-10 14:24:30 -0700142 this, int(index), layers[index].get(), layer.get());
The Android Open Source Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700157 const sp<LayerBase>& layer,
158 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Project9066cfe2009-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 Agopian9779b222009-09-07 16:32:45 -0700176 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -0700177 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 mBootTime(systemTime()),
Mathias Agopian151e8592009-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 Project9066cfe2009-03-03 19:31:44 -0800182 mVisibleRegionsDirty(false),
183 mDeferReleaseConsole(false),
184 mFreezeDisplay(false),
185 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700186 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 mDebugBackground(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -0700189 mDebugInSwapBuffers(0),
190 mLastSwapBufferTime(0),
191 mDebugInTransaction(0),
192 mLastTransactionTime(0),
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800207 property_get("debug.sf.showbackground", value, "0");
208 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
210 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212}
213
214SurfaceFlinger::~SurfaceFlinger()
215{
216 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217}
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
220{
221 return graphicPlane(0).displayHardware().getOverlayEngine();
222}
223
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700224sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700226 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227}
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
230{
231 Mutex::Autolock _l(mStateLock);
232 uint32_t token = mTokens.acquire();
233
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700234 sp<Client> client = new Client(token, this);
235 if (client->ctrlblk == 0) {
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800241 mTokens.release(token);
242 return 0;
243 }
244 sp<BClient> bclient =
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700245 new BClient(this, token, client->getControlBlockMemory());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 return bclient;
247}
248
249void SurfaceFlinger::destroyConnection(ClientID cid)
250{
251 Mutex::Autolock _l(mStateLock);
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700252 sp<Client> client = mClientsMap.valueFor(cid);
253 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 // free all the layers this client owns
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700255 Vector< wp<LayerBaseClient> > layers(client->getLayers());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 const size_t count = layers.size();
257 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700258 sp<LayerBaseClient> layer(layers[i].promote());
259 if (layer != 0) {
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700260 purgatorizeLayer_l(layer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700261 }
The Android Open Source Project9066cfe2009-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 Agopian627e7b52009-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 Project9066cfe2009-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 Project9066cfe2009-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 Project9066cfe2009-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 Project9066cfe2009-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 Agopiand763b5d2009-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 Project9066cfe2009-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 Agopian627e7b52009-05-21 19:21:59 -0700403 // start boot animation
404 property_set("ctl.start", "bootanim");
405
The Android Open Source Project9066cfe2009-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 Agopian6ead5d92009-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 Projectc39a6e02009-03-11 12:11:56 -0700428 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700429
Mathias Agopianbdbe6022009-04-28 03:17:50 -0700430 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopian6ead5d92009-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 Project9066cfe2009-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 Projectc39a6e02009-03-11 12:11:56 -0700445 mFreezeDisplay = false;
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700446 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450}
451
452void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700453 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454}
455
456void SurfaceFlinger::signal() const {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700457 // this is the IPC call
458 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459}
460
461void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
462{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700463 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 postFramebuffer();
502 } else {
503 // pretend we did the post
504 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 usleep(16667); // 60 fps period
506 }
507 return true;
508}
509
510void SurfaceFlinger::postFramebuffer()
511{
Mathias Agopiandff8e582009-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 Project9066cfe2009-03-03 19:31:44 -0800517 return;
518 }
519
520 if (!mInvalidRegion.isEmpty()) {
521 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700522 const nsecs_t now = systemTime();
523 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700525 mLastSwapBufferTime = systemTime() - now;
526 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-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 Agopianed81f222009-04-14 23:02:51 -0700542 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 hw.releaseScreen();
545 }
546
547 if (what & eConsoleReleased) {
548 if (hw.canDraw()) {
The Android Open Source Project9066cfe2009-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 Agopian2d5ee252009-06-04 18:46:21 -0700560 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700562 { // scope for the lock
563 Mutex::Autolock _l(mStateLock);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700564 const nsecs_t now = systemTime();
565 mDebugInTransaction = now;
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700566 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700567 mLastTransactionTime = systemTime() - now;
568 mDebugInTransaction = 0;
Mathias Agopian2d5ee252009-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 Agopianffae4fc2009-09-04 19:50:23 -0700574 if (ditchedLayers[i] != 0) {
575 //LOGD("ditching layer %p", ditchedLayers[i].get());
576 ditchedLayers[i]->ditch();
577 }
Mathias Agopian2d5ee252009-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 Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -0700595 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-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 Agopianeb0c86e2009-03-27 18:11:38 -0700622 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-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 Agopianeb0c86e2009-03-27 18:11:38 -0700641 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800647 }
648
Mathias Agopiana3aa6c92009-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 Project9066cfe2009-03-03 19:31:44 -0800654 // some layers might have been removed, so
655 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700656 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700657 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700659 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700660 const size_t count = previousLayers.size();
661 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700662 const sp<LayerBase>& layer(previousLayers[i]);
663 if (currentLayers.indexOf( layer ) < 0) {
664 // this layer is not visible anymore
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700665 ditchedLayers.add(layer);
Mathias Agopian33863dd2009-07-28 14:20:21 -0700666 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700667 }
668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670
671 // get rid of all resources we don't need anymore
672 // (layers and clients)
673 free_resources_l();
674 }
675
676 commitTransaction();
677}
678
679sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
680{
681 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
682}
683
684void SurfaceFlinger::computeVisibleRegions(
685 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
686{
687 const GraphicPlane& plane(graphicPlane(0));
688 const Transform& planeTransform(plane.transform());
689
690 Region aboveOpaqueLayers;
691 Region aboveCoveredLayers;
692 Region dirty;
693
694 bool secureFrameBuffer = false;
695
696 size_t i = currentLayers.size();
697 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700698 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 layer->validateVisibility(planeTransform);
700
701 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700702 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703
704 // handle hidden surfaces by setting the visible region to empty
705 Region opaqueRegion;
706 Region visibleRegion;
707 Region coveredRegion;
Mathias Agopian12cedff2009-07-28 10:57:27 -0700708 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 const bool translucent = layer->needsBlending();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700710 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 visibleRegion.set(bounds);
712 coveredRegion = visibleRegion;
713
714 // Remove the transparent area from the visible region
715 if (translucent) {
716 visibleRegion.subtractSelf(layer->transparentRegionScreen);
717 }
718
719 // compute the opaque region
720 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
721 // the opaque region is the visible region
722 opaqueRegion = visibleRegion;
723 }
724 }
725
726 // subtract the opaque region covered by the layers above us
727 visibleRegion.subtractSelf(aboveOpaqueLayers);
728 coveredRegion.andSelf(aboveCoveredLayers);
729
730 // compute this layer's dirty region
731 if (layer->contentDirty) {
732 // we need to invalidate the whole region
733 dirty = visibleRegion;
734 // as well, as the old visible region
735 dirty.orSelf(layer->visibleRegionScreen);
736 layer->contentDirty = false;
737 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700738 /* compute the exposed region:
739 * exposed = what's VISIBLE and NOT COVERED now
740 * but was COVERED before
741 */
742 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 dirty.subtractSelf(aboveOpaqueLayers);
745
746 // accumulate to the screen dirty region
747 dirtyRegion.orSelf(dirty);
748
Mathias Agopianed81f222009-04-14 23:02:51 -0700749 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700751 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
753 // Store the visible region is screen space
754 layer->setVisibleRegion(visibleRegion);
755 layer->setCoveredRegion(coveredRegion);
756
Mathias Agopian12cedff2009-07-28 10:57:27 -0700757 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 if (layer->isSecure() && !visibleRegion.isEmpty()) {
759 secureFrameBuffer = true;
760 }
761 }
762
Mathias Agopian12cedff2009-07-28 10:57:27 -0700763 // invalidate the areas where a layer was removed
764 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
765 mDirtyRegionRemovedLayer.clear();
766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 mSecureFrameBuffer = secureFrameBuffer;
768 opaqueRegion = aboveOpaqueLayers;
769}
770
771
772void SurfaceFlinger::commitTransaction()
773{
774 mDrawingState = mCurrentState;
Mathias Agopian9779b222009-09-07 16:32:45 -0700775 mResizeTransationPending = false;
776 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777}
778
779void SurfaceFlinger::handlePageFlip()
780{
781 bool visibleRegions = mVisibleRegionsDirty;
782 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
783 visibleRegions |= lockPageFlip(currentLayers);
784
785 const DisplayHardware& hw = graphicPlane(0).displayHardware();
786 const Region screenRegion(hw.bounds());
787 if (visibleRegions) {
788 Region opaqueRegion;
789 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
790 mWormholeRegion = screenRegion.subtract(opaqueRegion);
791 mVisibleRegionsDirty = false;
792 }
793
794 unlockPageFlip(currentLayers);
795 mDirtyRegion.andSelf(screenRegion);
796}
797
798bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
799{
800 bool recomputeVisibleRegions = false;
801 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700802 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700804 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 layer->lockPageFlip(recomputeVisibleRegions);
806 }
807 return recomputeVisibleRegions;
808}
809
810void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
811{
812 const GraphicPlane& plane(graphicPlane(0));
813 const Transform& planeTransform(plane.transform());
814 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700815 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700817 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 layer->unlockPageFlip(planeTransform, mDirtyRegion);
819 }
820}
821
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823void SurfaceFlinger::handleRepaint()
824{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700825 // compute the invalid region
826 mInvalidRegion.orSelf(mDirtyRegion);
827 if (mInvalidRegion.isEmpty()) {
828 // nothing to do
829 return;
830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831
832 if (UNLIKELY(mDebugRegion)) {
833 debugFlashRegions();
834 }
835
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700836 // set the frame buffer
837 const DisplayHardware& hw(graphicPlane(0).displayHardware());
838 glMatrixMode(GL_MODELVIEW);
839 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840
841 uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700842 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
843 (flags & DisplayHardware::BUFFER_PRESERVED))
844 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700845 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
846 // takes a rectangle, we must make sure to update that whole
847 // rectangle in that case
848 if (flags & DisplayHardware::SWAP_RECTANGLE) {
849 // FIXME: we really should be able to pass a region to
850 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 mDirtyRegion.set(mInvalidRegion.bounds());
852 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700853 // in the BUFFER_PRESERVED case, obviously, we can update only
854 // what's needed and nothing more.
855 // NOTE: this is NOT a common case, as preserving the backbuffer
856 // is costly and usually involves copying the whole update back.
857 }
858 } else {
859 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
860 // We need to redraw the rectangle that will be updated
861 // (pushed to the framebuffer).
862 // This is needed because UPDATE_ON_DEMAND only takes one
863 // rectangle instead of a region (see DisplayHardware::flip())
864 mDirtyRegion.set(mInvalidRegion.bounds());
865 } else {
866 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 mDirtyRegion.set(hw.bounds());
868 mInvalidRegion = mDirtyRegion;
869 }
870 }
871
872 // compose all surfaces
873 composeSurfaces(mDirtyRegion);
874
875 // clear the dirty regions
876 mDirtyRegion.clear();
877}
878
879void SurfaceFlinger::composeSurfaces(const Region& dirty)
880{
881 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
882 // should never happen unless the window manager has a bug
883 // draw something...
884 drawWormhole();
885 }
886 const SurfaceFlinger& flinger(*this);
887 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
888 const size_t count = drawingLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700889 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700891 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 const Region& visibleRegion(layer->visibleRegionScreen);
893 if (!visibleRegion.isEmpty()) {
894 const Region clip(dirty.intersect(visibleRegion));
895 if (!clip.isEmpty()) {
896 layer->draw(clip);
897 }
898 }
899 }
900}
901
902void SurfaceFlinger::unlockClients()
903{
904 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
905 const size_t count = drawingLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700906 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700908 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 layer->finishPageFlip();
910 }
911}
912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913void SurfaceFlinger::debugFlashRegions()
914{
Mathias Agopiandff8e582009-05-04 14:17:04 -0700915 const DisplayHardware& hw(graphicPlane(0).displayHardware());
916 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700917
918 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
919 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopiandff8e582009-05-04 14:17:04 -0700920 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
921 mDirtyRegion.bounds() : hw.bounds());
922 composeSurfaces(repaint);
923 }
924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 glDisable(GL_TEXTURE_2D);
926 glDisable(GL_BLEND);
927 glDisable(GL_DITHER);
928 glDisable(GL_SCISSOR_TEST);
929
Mathias Agopiandff8e582009-05-04 14:17:04 -0700930 static int toggle = 0;
931 toggle = 1 - toggle;
932 if (toggle) {
933 glColor4x(0x10000, 0, 0x10000, 0x10000);
934 } else {
935 glColor4x(0x10000, 0x10000, 0, 0x10000);
936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700938 Region::const_iterator it = mDirtyRegion.begin();
939 Region::const_iterator const end = mDirtyRegion.end();
940 while (it != end) {
941 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 GLfloat vertices[][2] = {
943 { r.left, r.top },
944 { r.left, r.bottom },
945 { r.right, r.bottom },
946 { r.right, r.top }
947 };
948 glVertexPointer(2, GL_FLOAT, 0, vertices);
949 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
950 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700951
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700952 if (mInvalidRegion.isEmpty()) {
953 mDirtyRegion.dump("mDirtyRegion");
954 mInvalidRegion.dump("mInvalidRegion");
955 }
956 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957
958 if (mDebugRegion > 1)
959 usleep(mDebugRegion * 1000);
960
961 glEnable(GL_SCISSOR_TEST);
962 //mDirtyRegion.dump("mDirtyRegion");
963}
964
965void SurfaceFlinger::drawWormhole() const
966{
967 const Region region(mWormholeRegion.intersect(mDirtyRegion));
968 if (region.isEmpty())
969 return;
970
971 const DisplayHardware& hw(graphicPlane(0).displayHardware());
972 const int32_t width = hw.getWidth();
973 const int32_t height = hw.getHeight();
974
975 glDisable(GL_BLEND);
976 glDisable(GL_DITHER);
977
978 if (LIKELY(!mDebugBackground)) {
979 glClearColorx(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700980 Region::const_iterator it = region.begin();
981 Region::const_iterator const end = region.end();
982 while (it != end) {
983 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 const GLint sy = height - (r.top + r.height());
985 glScissor(r.left, sy, r.width(), r.height());
986 glClear(GL_COLOR_BUFFER_BIT);
987 }
988 } else {
989 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
990 { width, height }, { 0, height } };
991 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
992 glVertexPointer(2, GL_SHORT, 0, vertices);
993 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
994 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
995 glEnable(GL_TEXTURE_2D);
996 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
997 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
998 glMatrixMode(GL_TEXTURE);
999 glLoadIdentity();
1000 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001001 Region::const_iterator it = region.begin();
1002 Region::const_iterator const end = region.end();
1003 while (it != end) {
1004 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 const GLint sy = height - (r.top + r.height());
1006 glScissor(r.left, sy, r.width(), r.height());
1007 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1008 }
1009 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1010 }
1011}
1012
1013void SurfaceFlinger::debugShowFPS() const
1014{
1015 static int mFrameCount;
1016 static int mLastFrameCount = 0;
1017 static nsecs_t mLastFpsTime = 0;
1018 static float mFps = 0;
1019 mFrameCount++;
1020 nsecs_t now = systemTime();
1021 nsecs_t diff = now - mLastFpsTime;
1022 if (diff > ms2ns(250)) {
1023 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1024 mLastFpsTime = now;
1025 mLastFrameCount = mFrameCount;
1026 }
1027 // XXX: mFPS has the value we want
1028 }
1029
Mathias Agopian1473f462009-04-10 14:24:30 -07001030status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031{
1032 Mutex::Autolock _l(mStateLock);
1033 addLayer_l(layer);
1034 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1035 return NO_ERROR;
1036}
1037
Mathias Agopian1473f462009-04-10 14:24:30 -07001038status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039{
1040 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001041 status_t err = purgatorizeLayer_l(layer);
1042 if (err == NO_ERROR)
1043 setTransactionFlags(eTransactionNeeded);
1044 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045}
1046
Mathias Agopian1473f462009-04-10 14:24:30 -07001047status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048{
1049 layer->forceVisibilityTransaction();
1050 setTransactionFlags(eTraversalNeeded);
1051 return NO_ERROR;
1052}
1053
Mathias Agopian1473f462009-04-10 14:24:30 -07001054status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055{
Mathias Agopianffae4fc2009-09-04 19:50:23 -07001056 if (layer == 0)
1057 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 ssize_t i = mCurrentState.layersSortedByZ.add(
1059 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian1473f462009-04-10 14:24:30 -07001060 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1061 if (lbc != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 mLayerMap.add(lbc->serverIndex(), lbc);
1063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 return NO_ERROR;
1065}
1066
Mathias Agopian1473f462009-04-10 14:24:30 -07001067status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068{
1069 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1070 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001071 mLayersRemoved = true;
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001072 sp<LayerBaseClient> layer =
1073 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001074 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 mLayerMap.removeItem(layer->serverIndex());
1076 }
1077 return NO_ERROR;
1078 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001079 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080}
1081
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001082status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1083{
Mathias Agopian359140c2009-07-02 17:33:40 -07001084 // remove the layer from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001085 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian359140c2009-07-02 17:33:40 -07001086
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001087 // it's possible that we don't find a layer, because it might
1088 // have been destroyed already -- this is not technically an error
1089 // from the user because there is a race between BClient::destroySurface(),
1090 // ~BClient() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001091 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1092}
1093
1094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095void SurfaceFlinger::free_resources_l()
1096{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 // free resources associated with disconnected clients
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001098 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 const size_t count = disconnectedClients.size();
1100 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001101 sp<Client> client = disconnectedClients[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 mTokens.release(client->cid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 }
1104 disconnectedClients.clear();
1105}
1106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1108{
1109 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1110}
1111
1112uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1113{
1114 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1115 if ((old & flags)==0) { // wake the server up
1116 if (delay > 0) {
1117 signalDelayedEvent(delay);
1118 } else {
1119 signalEvent();
1120 }
1121 }
1122 return old;
1123}
1124
1125void SurfaceFlinger::openGlobalTransaction()
1126{
1127 android_atomic_inc(&mTransactionCount);
1128}
1129
1130void SurfaceFlinger::closeGlobalTransaction()
1131{
1132 if (android_atomic_dec(&mTransactionCount) == 1) {
1133 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001134
1135 // if there is a transaction with a resize, wait for it to
1136 // take effect before returning.
1137 Mutex::Autolock _l(mStateLock);
1138 while (mResizeTransationPending) {
1139 mTransactionCV.wait(mStateLock);
1140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142}
1143
1144status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1145{
1146 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1147 return BAD_VALUE;
1148
1149 Mutex::Autolock _l(mStateLock);
1150 mCurrentState.freezeDisplay = 1;
1151 setTransactionFlags(eTransactionNeeded);
1152
1153 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001154 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 return NO_ERROR;
1156}
1157
1158status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1159{
1160 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1161 return BAD_VALUE;
1162
1163 Mutex::Autolock _l(mStateLock);
1164 mCurrentState.freezeDisplay = 0;
1165 setTransactionFlags(eTransactionNeeded);
1166
1167 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001168 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 return NO_ERROR;
1170}
1171
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001172int SurfaceFlinger::setOrientation(DisplayID dpy,
1173 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174{
1175 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1176 return BAD_VALUE;
1177
1178 Mutex::Autolock _l(mStateLock);
1179 if (mCurrentState.orientation != orientation) {
1180 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001181 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 mCurrentState.orientation = orientation;
1183 setTransactionFlags(eTransactionNeeded);
1184 mTransactionCV.wait(mStateLock);
1185 } else {
1186 orientation = BAD_VALUE;
1187 }
1188 }
1189 return orientation;
1190}
1191
1192sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1193 ISurfaceFlingerClient::surface_data_t* params,
1194 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1195 uint32_t flags)
1196{
Mathias Agopian1473f462009-04-10 14:24:30 -07001197 sp<LayerBaseClient> layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001199
1200 if (int32_t(w|h) < 0) {
1201 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1202 int(w), int(h));
1203 return surfaceHandle;
1204 }
1205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 Mutex::Autolock _l(mStateLock);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001207 sp<Client> client = mClientsMap.valueFor(clientId);
1208 if (UNLIKELY(client == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1210 return surfaceHandle;
1211 }
1212
1213 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001214 int32_t id = client->generateId(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1216 LOGE("createSurface() failed, generateId = %d", id);
1217 return surfaceHandle;
1218 }
1219
1220 switch (flags & eFXSurfaceMask) {
1221 case eFXSurfaceNormal:
1222 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian18b6b492009-08-19 17:46:26 -07001223 layer = createPushBuffersSurfaceLocked(client, d, id,
1224 w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 } else {
Mathias Agopian18b6b492009-08-19 17:46:26 -07001226 layer = createNormalSurfaceLocked(client, d, id,
1227 w, h, flags, format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 break;
1230 case eFXSurfaceBlur:
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001231 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 break;
1233 case eFXSurfaceDim:
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001234 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 break;
1236 }
1237
Mathias Agopian1473f462009-04-10 14:24:30 -07001238 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 setTransactionFlags(eTransactionNeeded);
1240 surfaceHandle = layer->getSurface();
Mathias Agopian18b6b492009-08-19 17:46:26 -07001241 if (surfaceHandle != 0) {
1242 params->token = surfaceHandle->getToken();
1243 params->identity = surfaceHandle->getIdentity();
1244 params->width = w;
1245 params->height = h;
1246 params->format = format;
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249
1250 return surfaceHandle;
1251}
1252
Mathias Agopian1473f462009-04-10 14:24:30 -07001253sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001254 const sp<Client>& client, DisplayID display,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001255 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1256 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257{
1258 // initialize the surfaces
1259 switch (format) { // TODO: take h/w into account
1260 case PIXEL_FORMAT_TRANSPARENT:
1261 case PIXEL_FORMAT_TRANSLUCENT:
1262 format = PIXEL_FORMAT_RGBA_8888;
1263 break;
1264 case PIXEL_FORMAT_OPAQUE:
1265 format = PIXEL_FORMAT_RGB_565;
1266 break;
1267 }
1268
Mathias Agopian1473f462009-04-10 14:24:30 -07001269 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001270 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 if (LIKELY(err == NO_ERROR)) {
1272 layer->initStates(w, h, flags);
1273 addLayer_l(layer);
1274 } else {
1275 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001276 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278 return layer;
1279}
1280
Mathias Agopian1473f462009-04-10 14:24:30 -07001281sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001282 const sp<Client>& client, DisplayID display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1284{
Mathias Agopian1473f462009-04-10 14:24:30 -07001285 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 layer->initStates(w, h, flags);
1287 addLayer_l(layer);
1288 return layer;
1289}
1290
Mathias Agopian1473f462009-04-10 14:24:30 -07001291sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001292 const sp<Client>& client, DisplayID display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1294{
Mathias Agopian1473f462009-04-10 14:24:30 -07001295 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 layer->initStates(w, h, flags);
1297 addLayer_l(layer);
1298 return layer;
1299}
1300
Mathias Agopian1473f462009-04-10 14:24:30 -07001301sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001302 const sp<Client>& client, DisplayID display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1304{
Mathias Agopian1473f462009-04-10 14:24:30 -07001305 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 layer->initStates(w, h, flags);
1307 addLayer_l(layer);
1308 return layer;
1309}
1310
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001311status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001313 /*
1314 * called by the window manager, when a surface should be marked for
1315 * destruction.
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001316 *
1317 * The surface is removed from the current and drawing lists, but placed
1318 * in the purgatory queue, so it's not destroyed right-away (we need
1319 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001320 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001321
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001322 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001323 Mutex::Autolock _l(mStateLock);
1324 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001325 if (layer != 0) {
1326 err = purgatorizeLayer_l(layer);
1327 if (err == NO_ERROR) {
1328 layer->onRemoved();
1329 setTransactionFlags(eTransactionNeeded);
1330 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001331 }
1332 return err;
1333}
1334
1335status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1336{
Mathias Agopian359140c2009-07-02 17:33:40 -07001337 // called by ~ISurface() when all references are gone
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001338
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001339 class MessageDestroySurface : public MessageBase {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001340 SurfaceFlinger* flinger;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001341 sp<LayerBaseClient> layer;
1342 public:
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001343 MessageDestroySurface(
1344 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1345 : flinger(flinger), layer(layer) { }
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001346 virtual bool handler() {
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001347 sp<LayerBaseClient> l(layer);
1348 layer.clear(); // clear it outside of the lock;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001349 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian359140c2009-07-02 17:33:40 -07001350 /*
1351 * remove the layer from the current list -- chances are that it's
1352 * not in the list anyway, because it should have been removed
1353 * already upon request of the client (eg: window manager).
1354 * However, a buggy client could have not done that.
1355 * Since we know we don't have any more clients, we don't need
1356 * to use the purgatory.
1357 */
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001358 status_t err = flinger->removeLayer_l(l);
Mathias Agopian359140c2009-07-02 17:33:40 -07001359 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1360 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001361 return true;
1362 }
1363 };
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001364
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001365 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 return NO_ERROR;
1367}
1368
1369status_t SurfaceFlinger::setClientState(
1370 ClientID cid,
1371 int32_t count,
1372 const layer_state_t* states)
1373{
1374 Mutex::Autolock _l(mStateLock);
1375 uint32_t flags = 0;
1376 cid <<= 16;
1377 for (int i=0 ; i<count ; i++) {
1378 const layer_state_t& s = states[i];
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001379 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian1473f462009-04-10 14:24:30 -07001380 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 const uint32_t what = s.what;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 if (what & ePositionChanged) {
1383 if (layer->setPosition(s.x, s.y))
1384 flags |= eTraversalNeeded;
1385 }
1386 if (what & eLayerChanged) {
1387 if (layer->setLayer(s.z)) {
1388 mCurrentState.layersSortedByZ.reorder(
1389 layer, &Layer::compareCurrentStateZ);
1390 // we need traversal (state changed)
1391 // AND transaction (list changed)
1392 flags |= eTransactionNeeded|eTraversalNeeded;
1393 }
1394 }
1395 if (what & eSizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -07001396 if (layer->setSize(s.w, s.h)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 flags |= eTraversalNeeded;
Mathias Agopian9779b222009-09-07 16:32:45 -07001398 mResizeTransationPending = true;
1399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
1401 if (what & eAlphaChanged) {
1402 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1403 flags |= eTraversalNeeded;
1404 }
1405 if (what & eMatrixChanged) {
1406 if (layer->setMatrix(s.matrix))
1407 flags |= eTraversalNeeded;
1408 }
1409 if (what & eTransparentRegionChanged) {
1410 if (layer->setTransparentRegionHint(s.transparentRegion))
1411 flags |= eTraversalNeeded;
1412 }
1413 if (what & eVisibilityChanged) {
1414 if (layer->setFlags(s.flags, s.mask))
1415 flags |= eTraversalNeeded;
1416 }
1417 }
1418 }
1419 if (flags) {
1420 setTransactionFlags(flags);
1421 }
1422 return NO_ERROR;
1423}
1424
Mathias Agopian1473f462009-04-10 14:24:30 -07001425sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426{
Mathias Agopian1473f462009-04-10 14:24:30 -07001427 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1428 return layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429}
1430
1431void SurfaceFlinger::screenReleased(int dpy)
1432{
1433 // this may be called by a signal handler, we can't do too much in here
1434 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1435 signalEvent();
1436}
1437
1438void SurfaceFlinger::screenAcquired(int dpy)
1439{
1440 // this may be called by a signal handler, we can't do too much in here
1441 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1442 signalEvent();
1443}
1444
1445status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1446{
1447 const size_t SIZE = 1024;
1448 char buffer[SIZE];
1449 String8 result;
Mathias Agopian151e8592009-06-15 18:24:59 -07001450 if (!mDump.checkCalling()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 snprintf(buffer, SIZE, "Permission Denial: "
1452 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1453 IPCThreadState::self()->getCallingPid(),
1454 IPCThreadState::self()->getCallingUid());
1455 result.append(buffer);
1456 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001457
1458 // figure out if we're stuck somewhere
1459 const nsecs_t now = systemTime();
1460 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1461 const nsecs_t inTransaction(mDebugInTransaction);
1462 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1463 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1464
1465 // Try to get the main lock, but don't insist if we can't
1466 // (this would indicate SF is stuck, but we want to be able to
1467 // print something in dumpsys).
1468 int retry = 3;
1469 while (mStateLock.tryLock()<0 && --retry>=0) {
1470 usleep(1000000);
1471 }
1472 const bool locked(retry >= 0);
1473 if (!locked) {
1474 snprintf(buffer, SIZE,
1475 "SurfaceFlinger appears to be unresponsive, "
1476 "dumping anyways (no locks held)\n");
1477 result.append(buffer);
1478 }
1479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 size_t s = mClientsMap.size();
1481 char name[64];
1482 for (size_t i=0 ; i<s ; i++) {
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001483 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 sprintf(name, " Client (id=0x%08x)", client->cid);
1485 client->dump(name);
1486 }
1487 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1488 const size_t count = currentLayers.size();
1489 for (size_t i=0 ; i<count ; i++) {
1490 /*** LayerBase ***/
Mathias Agopian1473f462009-04-10 14:24:30 -07001491 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 const Layer::State& s = layer->drawingState();
1493 snprintf(buffer, SIZE,
1494 "+ %s %p\n"
1495 " "
1496 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1497 "needsBlending=%1d, invalidate=%1d, "
1498 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian1473f462009-04-10 14:24:30 -07001499 layer->getTypeID(), layer.get(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 s.z, layer->tx(), layer->ty(), s.w, s.h,
1501 layer->needsBlending(), layer->contentDirty,
1502 s.alpha, s.flags,
1503 s.transform[0], s.transform[1],
1504 s.transform[2], s.transform[3]);
1505 result.append(buffer);
1506 buffer[0] = 0;
1507 /*** LayerBaseClient ***/
Mathias Agopian1473f462009-04-10 14:24:30 -07001508 sp<LayerBaseClient> lbc =
1509 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1510 if (lbc != 0) {
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001511 sp<Client> client(lbc->client.promote());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 snprintf(buffer, SIZE,
1513 " "
1514 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001515 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 lbc->getIdentity());
Mathias Agopian9779b222009-09-07 16:32:45 -07001517
1518 result.append(buffer);
1519 buffer[0] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 /*** Layer ***/
Mathias Agopian1473f462009-04-10 14:24:30 -07001522 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1523 if (l != 0) {
Mathias Agopian9779b222009-09-07 16:32:45 -07001524 result.append( l->lcblk->dump(" ") );
1525 sp<const Buffer> buf0(l->getBuffer(0));
1526 sp<const Buffer> buf1(l->getBuffer(1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 snprintf(buffer, SIZE,
1528 " "
Mathias Agopian1473f462009-04-10 14:24:30 -07001529 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian9779b222009-09-07 16:32:45 -07001530 " freezeLock=%p\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 l->pixelFormat(),
Mathias Agopian9779b222009-09-07 16:32:45 -07001532 buf0->getWidth(), buf0->getHeight(), buf0->getStride(),
1533 buf1->getWidth(), buf1->getHeight(), buf1->getStride(),
1534 l->getFreezeLock().get());
1535 result.append(buffer);
1536 buffer[0] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 s.transparentRegion.dump(result, "transparentRegion");
1539 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1540 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1541 }
1542 mWormholeRegion.dump(result, "WormholeRegion");
1543 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1544 snprintf(buffer, SIZE,
1545 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1546 mFreezeDisplay?"yes":"no", mFreezeCount,
1547 mCurrentState.orientation, hw.canDraw());
1548 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001549 snprintf(buffer, SIZE,
1550 " last eglSwapBuffers() time: %f us\n"
1551 " last transaction time : %f us\n",
1552 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1553 result.append(buffer);
1554 if (inSwapBuffersDuration || !locked) {
1555 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1556 inSwapBuffersDuration/1000.0);
1557 result.append(buffer);
1558 }
1559 if (inTransactionDuration || !locked) {
1560 snprintf(buffer, SIZE, " transaction time: %f us\n",
1561 inTransactionDuration/1000.0);
1562 result.append(buffer);
1563 }
Mathias Agopian359140c2009-07-02 17:33:40 -07001564 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001565 result.append(buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -07001566 const BufferAllocator& alloc(BufferAllocator::get());
1567 alloc.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001568
1569 if (locked) {
1570 mStateLock.unlock();
1571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573 write(fd, result.string(), result.size());
1574 return NO_ERROR;
1575}
1576
1577status_t SurfaceFlinger::onTransact(
1578 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1579{
1580 switch (code) {
1581 case CREATE_CONNECTION:
1582 case OPEN_GLOBAL_TRANSACTION:
1583 case CLOSE_GLOBAL_TRANSACTION:
1584 case SET_ORIENTATION:
1585 case FREEZE_DISPLAY:
1586 case UNFREEZE_DISPLAY:
1587 case BOOT_FINISHED:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 {
1589 // codes that require permission check
1590 IPCThreadState* ipc = IPCThreadState::self();
1591 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001592 const int uid = ipc->getCallingUid();
Mathias Agopian151e8592009-06-15 18:24:59 -07001593 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1594 LOGE("Permission Denial: "
1595 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1596 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598 }
1599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1601 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001602 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian151e8592009-06-15 18:24:59 -07001603 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1604 IPCThreadState* ipc = IPCThreadState::self();
1605 const int pid = ipc->getCallingPid();
1606 const int uid = ipc->getCallingUid();
1607 LOGE("Permission Denial: "
1608 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 return PERMISSION_DENIED;
1610 }
1611 int n;
1612 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001613 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 return NO_ERROR;
Mathias Agopianef07dda2009-04-21 18:28:33 -07001615 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 return NO_ERROR;
1617 case 1002: // SHOW_UPDATES
1618 n = data.readInt32();
1619 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1620 return NO_ERROR;
1621 case 1003: // SHOW_BACKGROUND
1622 n = data.readInt32();
1623 mDebugBackground = n ? 1 : 0;
1624 return NO_ERROR;
1625 case 1004:{ // repaint everything
1626 Mutex::Autolock _l(mStateLock);
1627 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1628 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1629 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001630 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001632 case 1005:{ // force transaction
1633 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1634 return NO_ERROR;
1635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 case 1007: // set mFreezeCount
1637 mFreezeCount = data.readInt32();
1638 return NO_ERROR;
1639 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001640 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 reply->writeInt32(0);
1642 reply->writeInt32(mDebugRegion);
1643 reply->writeInt32(mDebugBackground);
1644 return NO_ERROR;
1645 case 1013: {
1646 Mutex::Autolock _l(mStateLock);
1647 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1648 reply->writeInt32(hw.getPageFlipCount());
1649 }
1650 return NO_ERROR;
1651 }
1652 }
1653 return err;
1654}
1655
1656// ---------------------------------------------------------------------------
1657#if 0
1658#pragma mark -
1659#endif
1660
1661Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1662 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1663{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 const int pgsize = getpagesize();
Mathias Agopian9779b222009-09-07 16:32:45 -07001665 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001666
1667 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1668 "SurfaceFlinger Client control-block");
1669
Mathias Agopian9779b222009-09-07 16:32:45 -07001670 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001671 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopian9779b222009-09-07 16:32:45 -07001672 new(ctrlblk) SharedClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674}
1675
1676Client::~Client() {
1677 if (ctrlblk) {
Mathias Agopian9779b222009-09-07 16:32:45 -07001678 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
1680}
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682int32_t Client::generateId(int pid)
1683{
1684 const uint32_t i = clz( ~mBitmap );
1685 if (i >= NUM_LAYERS_MAX) {
1686 return NO_MEMORY;
1687 }
1688 mPid = pid;
1689 mInUse.add(uint8_t(i));
1690 mBitmap |= 1<<(31-i);
1691 return i;
1692}
Mathias Agopian1473f462009-04-10 14:24:30 -07001693
1694status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695{
1696 ssize_t idx = mInUse.indexOf(id);
1697 if (idx < 0)
1698 return NAME_NOT_FOUND;
1699 return mLayers.insertAt(layer, idx);
1700}
Mathias Agopian1473f462009-04-10 14:24:30 -07001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702void Client::free(int32_t id)
1703{
1704 ssize_t idx = mInUse.remove(uint8_t(id));
1705 if (idx >= 0) {
1706 mBitmap &= ~(1<<(31-id));
1707 mLayers.removeItemsAt(idx);
1708 }
1709}
1710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711bool Client::isValid(int32_t i) const {
1712 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1713}
Mathias Agopian1473f462009-04-10 14:24:30 -07001714
1715sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1716 sp<LayerBaseClient> lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07001718 if (idx >= 0) {
1719 lbc = mLayers[idx].promote();
1720 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1721 }
1722 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723}
1724
1725void Client::dump(const char* what)
1726{
1727}
1728
1729// ---------------------------------------------------------------------------
1730#if 0
1731#pragma mark -
1732#endif
1733
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001734BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 : mId(cid), mFlinger(flinger), mCblk(cblk)
1736{
1737}
1738
1739BClient::~BClient() {
1740 // destroy all resources attached to this client
1741 mFlinger->destroyConnection(mId);
1742}
1743
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001744sp<IMemoryHeap> BClient::getControlBlock() const {
1745 return mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746}
1747
1748sp<ISurface> BClient::createSurface(
1749 ISurfaceFlingerClient::surface_data_t* params, int pid,
1750 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1751 uint32_t flags)
1752{
1753 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1754}
1755
1756status_t BClient::destroySurface(SurfaceID sid)
1757{
1758 sid |= (mId << 16); // add the client-part to id
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001759 return mFlinger->removeSurface(sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760}
1761
1762status_t BClient::setState(int32_t count, const layer_state_t* states)
1763{
1764 return mFlinger->setClientState(mId, count, states);
1765}
1766
1767// ---------------------------------------------------------------------------
1768
1769GraphicPlane::GraphicPlane()
1770 : mHw(0)
1771{
1772}
1773
1774GraphicPlane::~GraphicPlane() {
1775 delete mHw;
1776}
1777
1778bool GraphicPlane::initialized() const {
1779 return mHw ? true : false;
1780}
1781
1782void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1783 mHw = hw;
1784}
1785
1786void GraphicPlane::setTransform(const Transform& tr) {
1787 mTransform = tr;
1788 mGlobalTransform = mOrientationTransform * mTransform;
1789}
1790
1791status_t GraphicPlane::orientationToTransfrom(
1792 int orientation, int w, int h, Transform* tr)
1793{
1794 float a, b, c, d, x, y;
1795 switch (orientation) {
1796 case ISurfaceComposer::eOrientationDefault:
1797 a=1; b=0; c=0; d=1; x=0; y=0;
1798 break;
1799 case ISurfaceComposer::eOrientation90:
1800 a=0; b=-1; c=1; d=0; x=w; y=0;
1801 break;
1802 case ISurfaceComposer::eOrientation180:
1803 a=-1; b=0; c=0; d=-1; x=w; y=h;
1804 break;
1805 case ISurfaceComposer::eOrientation270:
1806 a=0; b=1; c=-1; d=0; x=0; y=h;
1807 break;
1808 default:
1809 return BAD_VALUE;
1810 }
1811 tr->set(a, b, c, d);
1812 tr->set(x, y);
1813 return NO_ERROR;
1814}
1815
1816status_t GraphicPlane::setOrientation(int orientation)
1817{
1818 const DisplayHardware& hw(displayHardware());
1819 const float w = hw.getWidth();
1820 const float h = hw.getHeight();
1821
1822 if (orientation == ISurfaceComposer::eOrientationDefault) {
1823 // make sure the default orientation is optimal
1824 mOrientationTransform.reset();
Mathias Agopian3552f532009-03-27 17:58:20 -07001825 mOrientation = orientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 mGlobalTransform = mTransform;
1827 return NO_ERROR;
1828 }
1829
1830 // If the rotation can be handled in hardware, this is where
1831 // the magic should happen.
1832 if (UNLIKELY(orientation == 42)) {
1833 float a, b, c, d, x, y;
1834 const float r = (3.14159265f / 180.0f) * 42.0f;
1835 const float si = sinf(r);
1836 const float co = cosf(r);
1837 a=co; b=-si; c=si; d=co;
1838 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1839 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1840 mOrientationTransform.set(a, b, c, d);
1841 mOrientationTransform.set(x, y);
1842 } else {
1843 GraphicPlane::orientationToTransfrom(orientation, w, h,
1844 &mOrientationTransform);
1845 }
Mathias Agopian3552f532009-03-27 17:58:20 -07001846 mOrientation = orientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 mGlobalTransform = mOrientationTransform * mTransform;
1848 return NO_ERROR;
1849}
1850
1851const DisplayHardware& GraphicPlane::displayHardware() const {
1852 return *mHw;
1853}
1854
1855const Transform& GraphicPlane::transform() const {
1856 return mGlobalTransform;
1857}
1858
Mathias Agopian1473f462009-04-10 14:24:30 -07001859EGLDisplay GraphicPlane::getEGLDisplay() const {
1860 return mHw->getEGLDisplay();
1861}
1862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863// ---------------------------------------------------------------------------
1864
1865}; // namespace android