blob: 8a197e256f61f4201bad74303fc86e1ccb6f4ab6 [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
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <ui/PixelFormat.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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include "Layer.h"
48#include "LayerBlur.h"
49#include "LayerBuffer.h"
50#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopiana1ecca92009-05-21 19:21:59 -070055/* ideally AID_GRAPHICS would be in a semi-public header
56 * or there would be a way to map a user/group name to its id
57 */
58#ifndef AID_GRAPHICS
59#define AID_GRAPHICS 1003
60#endif
61
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#define DISPLAY_COUNT 1
63
64namespace android {
65
66// ---------------------------------------------------------------------------
67
68void SurfaceFlinger::instantiate() {
69 defaultServiceManager()->addService(
70 String16("SurfaceFlinger"), new SurfaceFlinger());
71}
72
73void SurfaceFlinger::shutdown() {
74 // we should unregister here, but not really because
75 // when (if) the service manager goes away, all the services
76 // it has a reference to will leave too.
77}
78
79// ---------------------------------------------------------------------------
80
81SurfaceFlinger::LayerVector::LayerVector(const SurfaceFlinger::LayerVector& rhs)
82 : lookup(rhs.lookup), layers(rhs.layers)
83{
84}
85
86ssize_t SurfaceFlinger::LayerVector::indexOf(
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
89 if (guess<size() && lookup.keyAt(guess) == key)
90 return guess;
91 const ssize_t i = lookup.indexOfKey(key);
92 if (i>=0) {
93 const size_t idx = lookup.valueAt(i);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094 LOGE_IF(layers[idx]!=key,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 return idx;
98 }
99 return i;
100}
101
102ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 const sp<LayerBase>& layer,
104 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
106 size_t count = layers.size();
107 ssize_t l = 0;
108 ssize_t h = count-1;
109 ssize_t mid;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110 sp<LayerBase> const* a = layers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 while (l <= h) {
112 mid = l + (h - l)/2;
113 const int c = cmp(a+mid, &layer);
114 if (c == 0) { l = mid; break; }
115 else if (c<0) { l = mid+1; }
116 else { h = mid-1; }
117 }
118 size_t order = l;
119 while (order<count && !cmp(&layer, a+order)) {
120 order++;
121 }
122 count = lookup.size();
123 for (size_t i=0 ; i<count ; i++) {
124 if (lookup.valueAt(i) >= order) {
125 lookup.editValueAt(i)++;
126 }
127 }
128 layers.insertAt(layer, order);
129 lookup.add(layer, order);
130 return order;
131}
132
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
135 const ssize_t keyIndex = lookup.indexOfKey(layer);
136 if (keyIndex >= 0) {
137 const size_t index = lookup.valueAt(keyIndex);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 LOGE_IF(layers[index]!=layer,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 this, int(index), layers[index].get(), layer.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 layers.removeItemsAt(index);
142 lookup.removeItemsAt(keyIndex);
143 const size_t count = lookup.size();
144 for (size_t i=0 ; i<count ; i++) {
145 if (lookup.valueAt(i) >= size_t(index)) {
146 lookup.editValueAt(i)--;
147 }
148 }
149 return index;
150 }
151 return NAME_NOT_FOUND;
152}
153
154ssize_t SurfaceFlinger::LayerVector::reorder(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700155 const sp<LayerBase>& layer,
156 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
158 // XXX: it's a little lame. but oh well...
159 ssize_t err = remove(layer);
160 if (err >=0)
161 err = add(layer, cmp);
162 return err;
163}
164
165// ---------------------------------------------------------------------------
166#if 0
167#pragma mark -
168#endif
169
170SurfaceFlinger::SurfaceFlinger()
171 : BnSurfaceComposer(), Thread(false),
172 mTransactionFlags(0),
173 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700174 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -0700177 mHardwareTest("android.permission.HARDWARE_TEST"),
178 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
179 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 mVisibleRegionsDirty(false),
181 mDeferReleaseConsole(false),
182 mFreezeDisplay(false),
183 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700184 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700187 mDebugInSwapBuffers(0),
188 mLastSwapBufferTime(0),
189 mDebugInTransaction(0),
190 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700191 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 mConsoleSignals(0),
193 mSecureFrameBuffer(0)
194{
195 init();
196}
197
198void SurfaceFlinger::init()
199{
200 LOGI("SurfaceFlinger is starting");
201
202 // debugging stuff...
203 char value[PROPERTY_VALUE_MAX];
204 property_get("debug.sf.showupdates", value, "0");
205 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 property_get("debug.sf.showbackground", value, "0");
207 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
209 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
213SurfaceFlinger::~SurfaceFlinger()
214{
215 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216}
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
219{
220 return graphicPlane(0).displayHardware().getOverlayEngine();
221}
222
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700223sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
229{
230 Mutex::Autolock _l(mStateLock);
231 uint32_t token = mTokens.acquire();
232
Mathias Agopianf9d93272009-06-19 17:00:27 -0700233 sp<Client> client = new Client(token, this);
234 if (client->ctrlblk == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 mTokens.release(token);
236 return 0;
237 }
238 status_t err = mClientsMap.add(token, client);
239 if (err < 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 mTokens.release(token);
241 return 0;
242 }
243 sp<BClient> bclient =
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700244 new BClient(this, token, client->getControlBlockMemory());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 return bclient;
246}
247
248void SurfaceFlinger::destroyConnection(ClientID cid)
249{
250 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -0700251 sp<Client> client = mClientsMap.valueFor(cid);
252 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 // free all the layers this client owns
Mathias Agopian3d579642009-06-04 18:46:21 -0700254 Vector< wp<LayerBaseClient> > layers(client->getLayers());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 const size_t count = layers.size();
256 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 sp<LayerBaseClient> layer(layers[i].promote());
258 if (layer != 0) {
Mathias Agopian3d579642009-06-04 18:46:21 -0700259 purgatorizeLayer_l(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 }
262
263 // the resources associated with this client will be freed
264 // during the next transaction, after these surfaces have been
265 // properly removed from the screen
266
267 // remove this client from our ClientID->Client mapping.
268 mClientsMap.removeItem(cid);
269
270 // and add it to the list of disconnected clients
271 mDisconnectedClients.add(client);
272
273 // request a transaction
274 setTransactionFlags(eTransactionNeeded);
275 }
276}
277
278const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
279{
280 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
281 const GraphicPlane& plane(mGraphicPlanes[dpy]);
282 return plane;
283}
284
285GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
286{
287 return const_cast<GraphicPlane&>(
288 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
289}
290
291void SurfaceFlinger::bootFinished()
292{
293 const nsecs_t now = systemTime();
294 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700295 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700296 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700297 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));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800352 dcblk->w = plane.getWidth();
353 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 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;
Mathias Agopian04087722009-12-01 17:23:28 -0800419 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700420 if (UNLIKELY(isFrozen())) {
421 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700422 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 Agopian04087722009-12-01 17:23:28 -0800431
432 // see if we timed out
433 if (isFrozen()) {
434 const nsecs_t now = systemTime();
435 nsecs_t frozenTime = (now - mFreezeDisplayTime);
436 if (frozenTime >= freezeDisplayTimeout) {
437 // we timed out and are still frozen
438 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
439 mFreezeDisplay, mFreezeCount);
440 mFreezeDisplayTime = 0;
441 mFreezeCount = 0;
442 mFreezeDisplay = false;
443 }
444 }
445
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700446 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700447 switch (msg->what) {
448 case MessageQueue::INVALIDATE:
449 // invalidate message, just return to the main loop
450 return;
451 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 }
454}
455
456void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700457 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458}
459
460void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700461 // this is the IPC call
462 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463}
464
465void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
466{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700467 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468}
469
470// ----------------------------------------------------------------------------
471#if 0
472#pragma mark -
473#pragma mark Main loop
474#endif
475
476bool SurfaceFlinger::threadLoop()
477{
478 waitForEvent();
479
480 // check for transactions
481 if (UNLIKELY(mConsoleSignals)) {
482 handleConsoleEvents();
483 }
484
485 if (LIKELY(mTransactionCount == 0)) {
486 // if we're in a global transaction, don't do anything.
487 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
488 uint32_t transactionFlags = getTransactionFlags(mask);
489 if (LIKELY(transactionFlags)) {
490 handleTransaction(transactionFlags);
491 }
492 }
493
494 // post surfaces (if needed)
495 handlePageFlip();
496
497 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700498 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 // repaint the framebuffer (if needed)
500 handleRepaint();
501
Mathias Agopian74faca22009-09-17 16:18:16 -0700502 // inform the h/w that we're done compositing
503 hw.compositionComplete();
504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 // release the clients before we flip ('cause flip might block)
506 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 postFramebuffer();
509 } else {
510 // pretend we did the post
511 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 usleep(16667); // 60 fps period
513 }
514 return true;
515}
516
517void SurfaceFlinger::postFramebuffer()
518{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 if (!mInvalidRegion.isEmpty()) {
520 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700521 const nsecs_t now = systemTime();
522 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700524 mLastSwapBufferTime = systemTime() - now;
525 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 }
528}
529
530void SurfaceFlinger::handleConsoleEvents()
531{
532 // something to do with the console
533 const DisplayHardware& hw = graphicPlane(0).displayHardware();
534
535 int what = android_atomic_and(0, &mConsoleSignals);
536 if (what & eConsoleAcquired) {
537 hw.acquireScreen();
538 }
539
540 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700541 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 hw.releaseScreen();
544 }
545
546 if (what & eConsoleReleased) {
547 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 hw.releaseScreen();
549 } else {
550 mDeferReleaseConsole = true;
551 }
552 }
553
554 mDirtyRegion.set(hw.bounds());
555}
556
557void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
558{
Mathias Agopian3d579642009-06-04 18:46:21 -0700559 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560
Mathias Agopian3d579642009-06-04 18:46:21 -0700561 { // scope for the lock
562 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700563 const nsecs_t now = systemTime();
564 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700565 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700566 mLastTransactionTime = systemTime() - now;
567 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700568 }
569
570 // do this without lock held
571 const size_t count = ditchedLayers.size();
572 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700573 if (ditchedLayers[i] != 0) {
574 //LOGD("ditching layer %p", ditchedLayers[i].get());
575 ditchedLayers[i]->ditch();
576 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700577 }
578}
579
580void SurfaceFlinger::handleTransactionLocked(
581 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
582{
583 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 const size_t count = currentLayers.size();
585
586 /*
587 * Traversal of the children
588 * (perform the transaction for each of them if needed)
589 */
590
591 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
592 if (layersNeedTransaction) {
593 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700594 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
596 if (!trFlags) continue;
597
598 const uint32_t flags = layer->doTransaction(0);
599 if (flags & Layer::eVisibleRegion)
600 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 }
602 }
603
604 /*
605 * Perform our own transaction if needed
606 */
607
608 if (transactionFlags & eTransactionNeeded) {
609 if (mCurrentState.orientation != mDrawingState.orientation) {
610 // the orientation has changed, recompute all visible regions
611 // and invalidate everything.
612
613 const int dpy = 0;
614 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700615 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 GraphicPlane& plane(graphicPlane(dpy));
617 plane.setOrientation(orientation);
618
619 // update the shared control block
620 const DisplayHardware& hw(plane.displayHardware());
621 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
622 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800623 dcblk->w = plane.getWidth();
624 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625
626 mVisibleRegionsDirty = true;
627 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 }
629
630 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
631 // freezing or unfreezing the display -> trigger animation if needed
632 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800633 if (mFreezeDisplay)
634 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 }
636
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700637 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
638 // layers have been added
639 mVisibleRegionsDirty = true;
640 }
641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 // some layers might have been removed, so
643 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700645 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700647 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700648 const size_t count = previousLayers.size();
649 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700650 const sp<LayerBase>& layer(previousLayers[i]);
651 if (currentLayers.indexOf( layer ) < 0) {
652 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700653 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700654 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700655 }
656 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 }
658
659 // get rid of all resources we don't need anymore
660 // (layers and clients)
661 free_resources_l();
662 }
663
664 commitTransaction();
665}
666
667sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
668{
669 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
670}
671
672void SurfaceFlinger::computeVisibleRegions(
673 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
674{
675 const GraphicPlane& plane(graphicPlane(0));
676 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700677 const DisplayHardware& hw(plane.displayHardware());
678 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679
680 Region aboveOpaqueLayers;
681 Region aboveCoveredLayers;
682 Region dirty;
683
684 bool secureFrameBuffer = false;
685
686 size_t i = currentLayers.size();
687 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700688 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 layer->validateVisibility(planeTransform);
690
691 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700692 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693
Mathias Agopianab028732010-03-16 16:41:46 -0700694 /*
695 * opaqueRegion: area of a surface that is fully opaque.
696 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700698
699 /*
700 * visibleRegion: area of a surface that is visible on screen
701 * and not fully transparent. This is essentially the layer's
702 * footprint minus the opaque regions above it.
703 * Areas covered by a translucent surface are considered visible.
704 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700706
707 /*
708 * coveredRegion: area of a surface that is covered by all
709 * visible regions above it (which includes the translucent areas).
710 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700712
713
714 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700715 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700717 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700719 visibleRegion.andSelf(screenRegion);
720 if (!visibleRegion.isEmpty()) {
721 // Remove the transparent area from the visible region
722 if (translucent) {
723 visibleRegion.subtractSelf(layer->transparentRegionScreen);
724 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725
Mathias Agopianab028732010-03-16 16:41:46 -0700726 // compute the opaque region
727 const int32_t layerOrientation = layer->getOrientation();
728 if (s.alpha==255 && !translucent &&
729 ((layerOrientation & Transform::ROT_INVALID) == false)) {
730 // the opaque region is the layer's footprint
731 opaqueRegion = visibleRegion;
732 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 }
734 }
735
Mathias Agopianab028732010-03-16 16:41:46 -0700736 // Clip the covered region to the visible region
737 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
738
739 // Update aboveCoveredLayers for next (lower) layer
740 aboveCoveredLayers.orSelf(visibleRegion);
741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 // subtract the opaque region covered by the layers above us
743 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744
745 // compute this layer's dirty region
746 if (layer->contentDirty) {
747 // we need to invalidate the whole region
748 dirty = visibleRegion;
749 // as well, as the old visible region
750 dirty.orSelf(layer->visibleRegionScreen);
751 layer->contentDirty = false;
752 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700753 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700754 * the exposed region consists of two components:
755 * 1) what's VISIBLE now and was COVERED before
756 * 2) what's EXPOSED now less what was EXPOSED before
757 *
758 * note that (1) is conservative, we start with the whole
759 * visible region but only keep what used to be covered by
760 * something -- which mean it may have been exposed.
761 *
762 * (2) handles areas that were not covered by anything but got
763 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700764 */
Mathias Agopianab028732010-03-16 16:41:46 -0700765 const Region newExposed = visibleRegion - coveredRegion;
766 const Region oldVisibleRegion = layer->visibleRegionScreen;
767 const Region oldCoveredRegion = layer->coveredRegionScreen;
768 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
769 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 }
771 dirty.subtractSelf(aboveOpaqueLayers);
772
773 // accumulate to the screen dirty region
774 dirtyRegion.orSelf(dirty);
775
Mathias Agopianab028732010-03-16 16:41:46 -0700776 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778
779 // Store the visible region is screen space
780 layer->setVisibleRegion(visibleRegion);
781 layer->setCoveredRegion(coveredRegion);
782
Mathias Agopian97011222009-07-28 10:57:27 -0700783 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 if (layer->isSecure() && !visibleRegion.isEmpty()) {
785 secureFrameBuffer = true;
786 }
787 }
788
Mathias Agopian97011222009-07-28 10:57:27 -0700789 // invalidate the areas where a layer was removed
790 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
791 mDirtyRegionRemovedLayer.clear();
792
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 mSecureFrameBuffer = secureFrameBuffer;
794 opaqueRegion = aboveOpaqueLayers;
795}
796
797
798void SurfaceFlinger::commitTransaction()
799{
800 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700801 mResizeTransationPending = false;
802 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803}
804
805void SurfaceFlinger::handlePageFlip()
806{
807 bool visibleRegions = mVisibleRegionsDirty;
808 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
809 visibleRegions |= lockPageFlip(currentLayers);
810
811 const DisplayHardware& hw = graphicPlane(0).displayHardware();
812 const Region screenRegion(hw.bounds());
813 if (visibleRegions) {
814 Region opaqueRegion;
815 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
816 mWormholeRegion = screenRegion.subtract(opaqueRegion);
817 mVisibleRegionsDirty = false;
818 }
819
820 unlockPageFlip(currentLayers);
821 mDirtyRegion.andSelf(screenRegion);
822}
823
824bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
825{
826 bool recomputeVisibleRegions = false;
827 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700828 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700830 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 layer->lockPageFlip(recomputeVisibleRegions);
832 }
833 return recomputeVisibleRegions;
834}
835
836void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
837{
838 const GraphicPlane& plane(graphicPlane(0));
839 const Transform& planeTransform(plane.transform());
840 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700841 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700843 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844 layer->unlockPageFlip(planeTransform, mDirtyRegion);
845 }
846}
847
Mathias Agopianb8a55602009-06-26 19:06:36 -0700848
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849void SurfaceFlinger::handleRepaint()
850{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700851 // compute the invalid region
852 mInvalidRegion.orSelf(mDirtyRegion);
853 if (mInvalidRegion.isEmpty()) {
854 // nothing to do
855 return;
856 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857
858 if (UNLIKELY(mDebugRegion)) {
859 debugFlashRegions();
860 }
861
Mathias Agopianb8a55602009-06-26 19:06:36 -0700862 // set the frame buffer
863 const DisplayHardware& hw(graphicPlane(0).displayHardware());
864 glMatrixMode(GL_MODELVIEW);
865 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866
867 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700868 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
869 (flags & DisplayHardware::BUFFER_PRESERVED))
870 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700871 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
872 // takes a rectangle, we must make sure to update that whole
873 // rectangle in that case
874 if (flags & DisplayHardware::SWAP_RECTANGLE) {
875 // FIXME: we really should be able to pass a region to
876 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 mDirtyRegion.set(mInvalidRegion.bounds());
878 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700879 // in the BUFFER_PRESERVED case, obviously, we can update only
880 // what's needed and nothing more.
881 // NOTE: this is NOT a common case, as preserving the backbuffer
882 // is costly and usually involves copying the whole update back.
883 }
884 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700885 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700886 // We need to redraw the rectangle that will be updated
887 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700888 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700889 // rectangle instead of a region (see DisplayHardware::flip())
890 mDirtyRegion.set(mInvalidRegion.bounds());
891 } else {
892 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 mDirtyRegion.set(hw.bounds());
894 mInvalidRegion = mDirtyRegion;
895 }
896 }
897
898 // compose all surfaces
899 composeSurfaces(mDirtyRegion);
900
901 // clear the dirty regions
902 mDirtyRegion.clear();
903}
904
905void SurfaceFlinger::composeSurfaces(const Region& dirty)
906{
907 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
908 // should never happen unless the window manager has a bug
909 // draw something...
910 drawWormhole();
911 }
912 const SurfaceFlinger& flinger(*this);
913 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
914 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700915 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700917 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 const Region& visibleRegion(layer->visibleRegionScreen);
919 if (!visibleRegion.isEmpty()) {
920 const Region clip(dirty.intersect(visibleRegion));
921 if (!clip.isEmpty()) {
922 layer->draw(clip);
923 }
924 }
925 }
926}
927
928void SurfaceFlinger::unlockClients()
929{
930 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
931 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700932 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700934 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 layer->finishPageFlip();
936 }
937}
938
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939void SurfaceFlinger::debugFlashRegions()
940{
Mathias Agopian0926f502009-05-04 14:17:04 -0700941 const DisplayHardware& hw(graphicPlane(0).displayHardware());
942 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700943
944 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
945 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700946 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopian0926f502009-05-04 14:17:04 -0700947 mDirtyRegion.bounds() : hw.bounds());
948 composeSurfaces(repaint);
949 }
950
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 glDisable(GL_TEXTURE_2D);
952 glDisable(GL_BLEND);
953 glDisable(GL_DITHER);
954 glDisable(GL_SCISSOR_TEST);
955
Mathias Agopian0926f502009-05-04 14:17:04 -0700956 static int toggle = 0;
957 toggle = 1 - toggle;
958 if (toggle) {
959 glColor4x(0x10000, 0, 0x10000, 0x10000);
960 } else {
961 glColor4x(0x10000, 0x10000, 0, 0x10000);
962 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963
Mathias Agopian20f68782009-05-11 00:03:41 -0700964 Region::const_iterator it = mDirtyRegion.begin();
965 Region::const_iterator const end = mDirtyRegion.end();
966 while (it != end) {
967 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 GLfloat vertices[][2] = {
969 { r.left, r.top },
970 { r.left, r.bottom },
971 { r.right, r.bottom },
972 { r.right, r.top }
973 };
974 glVertexPointer(2, GL_FLOAT, 0, vertices);
975 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
976 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700977
Mathias Agopianb8a55602009-06-26 19:06:36 -0700978 if (mInvalidRegion.isEmpty()) {
979 mDirtyRegion.dump("mDirtyRegion");
980 mInvalidRegion.dump("mInvalidRegion");
981 }
982 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983
984 if (mDebugRegion > 1)
985 usleep(mDebugRegion * 1000);
986
987 glEnable(GL_SCISSOR_TEST);
988 //mDirtyRegion.dump("mDirtyRegion");
989}
990
991void SurfaceFlinger::drawWormhole() const
992{
993 const Region region(mWormholeRegion.intersect(mDirtyRegion));
994 if (region.isEmpty())
995 return;
996
997 const DisplayHardware& hw(graphicPlane(0).displayHardware());
998 const int32_t width = hw.getWidth();
999 const int32_t height = hw.getHeight();
1000
1001 glDisable(GL_BLEND);
1002 glDisable(GL_DITHER);
1003
1004 if (LIKELY(!mDebugBackground)) {
1005 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001006 Region::const_iterator it = region.begin();
1007 Region::const_iterator const end = region.end();
1008 while (it != end) {
1009 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 const GLint sy = height - (r.top + r.height());
1011 glScissor(r.left, sy, r.width(), r.height());
1012 glClear(GL_COLOR_BUFFER_BIT);
1013 }
1014 } else {
1015 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1016 { width, height }, { 0, height } };
1017 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1018 glVertexPointer(2, GL_SHORT, 0, vertices);
1019 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1020 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1021 glEnable(GL_TEXTURE_2D);
1022 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1023 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1024 glMatrixMode(GL_TEXTURE);
1025 glLoadIdentity();
1026 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001027 Region::const_iterator it = region.begin();
1028 Region::const_iterator const end = region.end();
1029 while (it != end) {
1030 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 const GLint sy = height - (r.top + r.height());
1032 glScissor(r.left, sy, r.width(), r.height());
1033 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1034 }
1035 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1036 }
1037}
1038
1039void SurfaceFlinger::debugShowFPS() const
1040{
1041 static int mFrameCount;
1042 static int mLastFrameCount = 0;
1043 static nsecs_t mLastFpsTime = 0;
1044 static float mFps = 0;
1045 mFrameCount++;
1046 nsecs_t now = systemTime();
1047 nsecs_t diff = now - mLastFpsTime;
1048 if (diff > ms2ns(250)) {
1049 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1050 mLastFpsTime = now;
1051 mLastFrameCount = mFrameCount;
1052 }
1053 // XXX: mFPS has the value we want
1054 }
1055
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001056status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057{
1058 Mutex::Autolock _l(mStateLock);
1059 addLayer_l(layer);
1060 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1061 return NO_ERROR;
1062}
1063
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001064status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065{
1066 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001067 status_t err = purgatorizeLayer_l(layer);
1068 if (err == NO_ERROR)
1069 setTransactionFlags(eTransactionNeeded);
1070 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071}
1072
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001073status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074{
1075 layer->forceVisibilityTransaction();
1076 setTransactionFlags(eTraversalNeeded);
1077 return NO_ERROR;
1078}
1079
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001080status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081{
Mathias Agopian0852e672009-09-04 19:50:23 -07001082 if (layer == 0)
1083 return BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 ssize_t i = mCurrentState.layersSortedByZ.add(
1085 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001086 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1087 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 mLayerMap.add(lbc->serverIndex(), lbc);
1089 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 return NO_ERROR;
1091}
1092
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001093status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094{
1095 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1096 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001097 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001098 sp<LayerBaseClient> layer =
1099 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001100 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 mLayerMap.removeItem(layer->serverIndex());
1102 }
1103 return NO_ERROR;
1104 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001105 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106}
1107
Mathias Agopian9a112062009-04-17 19:36:26 -07001108status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1109{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001110 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001111 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001112
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001113 layerBase->onRemoved();
1114
Mathias Agopian3d579642009-06-04 18:46:21 -07001115 // it's possible that we don't find a layer, because it might
1116 // have been destroyed already -- this is not technically an error
1117 // from the user because there is a race between BClient::destroySurface(),
1118 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001119 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1120}
1121
1122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123void SurfaceFlinger::free_resources_l()
1124{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001126 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 const size_t count = disconnectedClients.size();
1128 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001129 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131 }
1132 disconnectedClients.clear();
1133}
1134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1136{
1137 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1138}
1139
1140uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1141{
1142 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1143 if ((old & flags)==0) { // wake the server up
1144 if (delay > 0) {
1145 signalDelayedEvent(delay);
1146 } else {
1147 signalEvent();
1148 }
1149 }
1150 return old;
1151}
1152
1153void SurfaceFlinger::openGlobalTransaction()
1154{
1155 android_atomic_inc(&mTransactionCount);
1156}
1157
1158void SurfaceFlinger::closeGlobalTransaction()
1159{
1160 if (android_atomic_dec(&mTransactionCount) == 1) {
1161 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001162
1163 // if there is a transaction with a resize, wait for it to
1164 // take effect before returning.
1165 Mutex::Autolock _l(mStateLock);
1166 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001167 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1168 if (CC_UNLIKELY(err != NO_ERROR)) {
1169 // just in case something goes wrong in SF, return to the
1170 // called after a few seconds.
1171 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1172 mResizeTransationPending = false;
1173 break;
1174 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001175 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 }
1177}
1178
1179status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1180{
1181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1182 return BAD_VALUE;
1183
1184 Mutex::Autolock _l(mStateLock);
1185 mCurrentState.freezeDisplay = 1;
1186 setTransactionFlags(eTransactionNeeded);
1187
1188 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001189 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 return NO_ERROR;
1191}
1192
1193status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1194{
1195 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1196 return BAD_VALUE;
1197
1198 Mutex::Autolock _l(mStateLock);
1199 mCurrentState.freezeDisplay = 0;
1200 setTransactionFlags(eTransactionNeeded);
1201
1202 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001203 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 return NO_ERROR;
1205}
1206
Mathias Agopianc08731e2009-03-27 18:11:38 -07001207int SurfaceFlinger::setOrientation(DisplayID dpy,
1208 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209{
1210 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1211 return BAD_VALUE;
1212
1213 Mutex::Autolock _l(mStateLock);
1214 if (mCurrentState.orientation != orientation) {
1215 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001216 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 mCurrentState.orientation = orientation;
1218 setTransactionFlags(eTransactionNeeded);
1219 mTransactionCV.wait(mStateLock);
1220 } else {
1221 orientation = BAD_VALUE;
1222 }
1223 }
1224 return orientation;
1225}
1226
1227sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001228 const String8& name, ISurfaceFlingerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1230 uint32_t flags)
1231{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001232 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001234
1235 if (int32_t(w|h) < 0) {
1236 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1237 int(w), int(h));
1238 return surfaceHandle;
1239 }
1240
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001242 sp<Client> client = mClientsMap.valueFor(clientId);
1243 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1245 return surfaceHandle;
1246 }
1247
1248 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001249 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1251 LOGE("createSurface() failed, generateId = %d", id);
1252 return surfaceHandle;
1253 }
1254
1255 switch (flags & eFXSurfaceMask) {
1256 case eFXSurfaceNormal:
1257 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001258 layer = createPushBuffersSurfaceLocked(client, d, id,
1259 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001261 layer = createNormalSurfaceLocked(client, d, id,
1262 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 }
1264 break;
1265 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001266 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 break;
1268 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001269 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 break;
1271 }
1272
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001273 if (layer != 0) {
Mathias Agopian285dbde2010-03-01 16:09:43 -08001274 layer->setName(name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 setTransactionFlags(eTransactionNeeded);
1276 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001277 if (surfaceHandle != 0) {
1278 params->token = surfaceHandle->getToken();
1279 params->identity = surfaceHandle->getIdentity();
1280 params->width = w;
1281 params->height = h;
1282 params->format = format;
1283 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 }
1285
1286 return surfaceHandle;
1287}
1288
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001289sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001290 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001291 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1292 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293{
1294 // initialize the surfaces
1295 switch (format) { // TODO: take h/w into account
1296 case PIXEL_FORMAT_TRANSPARENT:
1297 case PIXEL_FORMAT_TRANSLUCENT:
1298 format = PIXEL_FORMAT_RGBA_8888;
1299 break;
1300 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001301 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 break;
1303 }
1304
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001305 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001306 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 if (LIKELY(err == NO_ERROR)) {
1308 layer->initStates(w, h, flags);
1309 addLayer_l(layer);
1310 } else {
1311 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001312 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 }
1314 return layer;
1315}
1316
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001317sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001318 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1320{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001321 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 layer->initStates(w, h, flags);
1323 addLayer_l(layer);
1324 return layer;
1325}
1326
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001327sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001328 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1330{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001331 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 layer->initStates(w, h, flags);
1333 addLayer_l(layer);
1334 return layer;
1335}
1336
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001337sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001338 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1340{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001341 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 layer->initStates(w, h, flags);
1343 addLayer_l(layer);
1344 return layer;
1345}
1346
Mathias Agopian9a112062009-04-17 19:36:26 -07001347status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348{
Mathias Agopian9a112062009-04-17 19:36:26 -07001349 /*
1350 * called by the window manager, when a surface should be marked for
1351 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001352 *
1353 * The surface is removed from the current and drawing lists, but placed
1354 * in the purgatory queue, so it's not destroyed right-away (we need
1355 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001356 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001357
Mathias Agopian48d819a2009-09-10 19:41:18 -07001358 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001359 Mutex::Autolock _l(mStateLock);
1360 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001361 if (layer != 0) {
1362 err = purgatorizeLayer_l(layer);
1363 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001364 setTransactionFlags(eTransactionNeeded);
1365 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001366 }
1367 return err;
1368}
1369
1370status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1371{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001372 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001373
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001374 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001375 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001376 sp<LayerBaseClient> layer;
1377 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001378 MessageDestroySurface(
1379 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1380 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001381 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001382 sp<LayerBaseClient> l(layer);
1383 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001384 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001385 /*
1386 * remove the layer from the current list -- chances are that it's
1387 * not in the list anyway, because it should have been removed
1388 * already upon request of the client (eg: window manager).
1389 * However, a buggy client could have not done that.
1390 * Since we know we don't have any more clients, we don't need
1391 * to use the purgatory.
1392 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001393 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001394 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1395 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001396 return true;
1397 }
1398 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001399
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001400 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 return NO_ERROR;
1402}
1403
1404status_t SurfaceFlinger::setClientState(
1405 ClientID cid,
1406 int32_t count,
1407 const layer_state_t* states)
1408{
1409 Mutex::Autolock _l(mStateLock);
1410 uint32_t flags = 0;
1411 cid <<= 16;
1412 for (int i=0 ; i<count ; i++) {
1413 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001414 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001415 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 if (what & ePositionChanged) {
1418 if (layer->setPosition(s.x, s.y))
1419 flags |= eTraversalNeeded;
1420 }
1421 if (what & eLayerChanged) {
1422 if (layer->setLayer(s.z)) {
1423 mCurrentState.layersSortedByZ.reorder(
1424 layer, &Layer::compareCurrentStateZ);
1425 // we need traversal (state changed)
1426 // AND transaction (list changed)
1427 flags |= eTransactionNeeded|eTraversalNeeded;
1428 }
1429 }
1430 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001431 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001433 mResizeTransationPending = true;
1434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 }
1436 if (what & eAlphaChanged) {
1437 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1438 flags |= eTraversalNeeded;
1439 }
1440 if (what & eMatrixChanged) {
1441 if (layer->setMatrix(s.matrix))
1442 flags |= eTraversalNeeded;
1443 }
1444 if (what & eTransparentRegionChanged) {
1445 if (layer->setTransparentRegionHint(s.transparentRegion))
1446 flags |= eTraversalNeeded;
1447 }
1448 if (what & eVisibilityChanged) {
1449 if (layer->setFlags(s.flags, s.mask))
1450 flags |= eTraversalNeeded;
1451 }
1452 }
1453 }
1454 if (flags) {
1455 setTransactionFlags(flags);
1456 }
1457 return NO_ERROR;
1458}
1459
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001460sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001462 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1463 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464}
1465
1466void SurfaceFlinger::screenReleased(int dpy)
1467{
1468 // this may be called by a signal handler, we can't do too much in here
1469 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1470 signalEvent();
1471}
1472
1473void SurfaceFlinger::screenAcquired(int dpy)
1474{
1475 // this may be called by a signal handler, we can't do too much in here
1476 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1477 signalEvent();
1478}
1479
1480status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1481{
1482 const size_t SIZE = 1024;
1483 char buffer[SIZE];
1484 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001485 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 snprintf(buffer, SIZE, "Permission Denial: "
1487 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1488 IPCThreadState::self()->getCallingPid(),
1489 IPCThreadState::self()->getCallingUid());
1490 result.append(buffer);
1491 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001492
1493 // figure out if we're stuck somewhere
1494 const nsecs_t now = systemTime();
1495 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1496 const nsecs_t inTransaction(mDebugInTransaction);
1497 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1498 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1499
1500 // Try to get the main lock, but don't insist if we can't
1501 // (this would indicate SF is stuck, but we want to be able to
1502 // print something in dumpsys).
1503 int retry = 3;
1504 while (mStateLock.tryLock()<0 && --retry>=0) {
1505 usleep(1000000);
1506 }
1507 const bool locked(retry >= 0);
1508 if (!locked) {
1509 snprintf(buffer, SIZE,
1510 "SurfaceFlinger appears to be unresponsive, "
1511 "dumping anyways (no locks held)\n");
1512 result.append(buffer);
1513 }
1514
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515 size_t s = mClientsMap.size();
1516 char name[64];
1517 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001518 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 sprintf(name, " Client (id=0x%08x)", client->cid);
1520 client->dump(name);
1521 }
1522 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1523 const size_t count = currentLayers.size();
1524 for (size_t i=0 ; i<count ; i++) {
1525 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001526 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 const Layer::State& s = layer->drawingState();
1528 snprintf(buffer, SIZE,
1529 "+ %s %p\n"
1530 " "
1531 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
Mathias Agopian401c2572009-09-23 19:16:27 -07001532 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001534 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 s.z, layer->tx(), layer->ty(), s.w, s.h,
Mathias Agopian401c2572009-09-23 19:16:27 -07001536 layer->needsBlending(), layer->needsDithering(),
1537 layer->contentDirty,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 s.alpha, s.flags,
Mathias Agopianeda65402010-02-22 03:15:57 -08001539 s.transform[0][0], s.transform[0][1],
1540 s.transform[1][0], s.transform[1][1]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 result.append(buffer);
1542 buffer[0] = 0;
1543 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001544 sp<LayerBaseClient> lbc =
1545 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1546 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001547 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 snprintf(buffer, SIZE,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001549 " name=%s\n", lbc->getName().string());
1550 result.append(buffer);
1551 snprintf(buffer, SIZE,
1552 " id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001553 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554 lbc->getIdentity());
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001555
1556 result.append(buffer);
1557 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001560 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1561 if (l != 0) {
Mathias Agopian86f73292009-09-17 01:35:28 -07001562 SharedBufferStack::Statistics stats = l->lcblk->getStats();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001563 result.append( l->lcblk->dump(" ") );
Mathias Agopian3330b202009-10-05 17:07:12 -07001564 sp<const GraphicBuffer> buf0(l->getBuffer(0));
1565 sp<const GraphicBuffer> buf1(l->getBuffer(1));
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001566 uint32_t w0=0, h0=0, s0=0;
1567 uint32_t w1=0, h1=0, s1=0;
1568 if (buf0 != 0) {
1569 w0 = buf0->getWidth();
1570 h0 = buf0->getHeight();
1571 s0 = buf0->getStride();
1572 }
1573 if (buf1 != 0) {
1574 w1 = buf1->getWidth();
1575 h1 = buf1->getHeight();
1576 s1 = buf1->getStride();
1577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 snprintf(buffer, SIZE,
1579 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001580 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian86f73292009-09-17 01:35:28 -07001581 " freezeLock=%p, dq-q-time=%u us\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 l->pixelFormat(),
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001583 w0, h0, s0, w1, h1, s1,
Mathias Agopian86f73292009-09-17 01:35:28 -07001584 l->getFreezeLock().get(), stats.totalTime);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001585 result.append(buffer);
1586 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 s.transparentRegion.dump(result, "transparentRegion");
1589 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1590 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1591 }
1592 mWormholeRegion.dump(result, "WormholeRegion");
1593 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1594 snprintf(buffer, SIZE,
1595 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1596 mFreezeDisplay?"yes":"no", mFreezeCount,
1597 mCurrentState.orientation, hw.canDraw());
1598 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001599 snprintf(buffer, SIZE,
1600 " last eglSwapBuffers() time: %f us\n"
1601 " last transaction time : %f us\n",
1602 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1603 result.append(buffer);
1604 if (inSwapBuffersDuration || !locked) {
1605 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1606 inSwapBuffersDuration/1000.0);
1607 result.append(buffer);
1608 }
1609 if (inTransactionDuration || !locked) {
1610 snprintf(buffer, SIZE, " transaction time: %f us\n",
1611 inTransactionDuration/1000.0);
1612 result.append(buffer);
1613 }
Mathias Agopian759fdb22009-07-02 17:33:40 -07001614 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001615 result.append(buffer);
Mathias Agopian3330b202009-10-05 17:07:12 -07001616 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001617 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001618
1619 if (locked) {
1620 mStateLock.unlock();
1621 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 }
1623 write(fd, result.string(), result.size());
1624 return NO_ERROR;
1625}
1626
1627status_t SurfaceFlinger::onTransact(
1628 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1629{
1630 switch (code) {
1631 case CREATE_CONNECTION:
1632 case OPEN_GLOBAL_TRANSACTION:
1633 case CLOSE_GLOBAL_TRANSACTION:
1634 case SET_ORIENTATION:
1635 case FREEZE_DISPLAY:
1636 case UNFREEZE_DISPLAY:
1637 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 {
1639 // codes that require permission check
1640 IPCThreadState* ipc = IPCThreadState::self();
1641 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001642 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001643 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1644 LOGE("Permission Denial: "
1645 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1646 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 }
1648 }
1649 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1651 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001652 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001653 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1654 IPCThreadState* ipc = IPCThreadState::self();
1655 const int pid = ipc->getCallingPid();
1656 const int uid = ipc->getCallingUid();
1657 LOGE("Permission Denial: "
1658 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659 return PERMISSION_DENIED;
1660 }
1661 int n;
1662 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001663 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001665 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 return NO_ERROR;
1667 case 1002: // SHOW_UPDATES
1668 n = data.readInt32();
1669 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1670 return NO_ERROR;
1671 case 1003: // SHOW_BACKGROUND
1672 n = data.readInt32();
1673 mDebugBackground = n ? 1 : 0;
1674 return NO_ERROR;
1675 case 1004:{ // repaint everything
1676 Mutex::Autolock _l(mStateLock);
1677 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1678 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1679 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001680 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001682 case 1005:{ // force transaction
1683 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1684 return NO_ERROR;
1685 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 case 1007: // set mFreezeCount
1687 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001688 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 return NO_ERROR;
1690 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001691 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 reply->writeInt32(0);
1693 reply->writeInt32(mDebugRegion);
1694 reply->writeInt32(mDebugBackground);
1695 return NO_ERROR;
1696 case 1013: {
1697 Mutex::Autolock _l(mStateLock);
1698 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1699 reply->writeInt32(hw.getPageFlipCount());
1700 }
1701 return NO_ERROR;
1702 }
1703 }
1704 return err;
1705}
1706
1707// ---------------------------------------------------------------------------
1708#if 0
1709#pragma mark -
1710#endif
1711
1712Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1713 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1714{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001716 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001717
1718 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1719 "SurfaceFlinger Client control-block");
1720
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001721 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001722 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001723 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724 }
1725}
1726
1727Client::~Client() {
1728 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001729 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730 }
1731}
1732
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733int32_t Client::generateId(int pid)
1734{
1735 const uint32_t i = clz( ~mBitmap );
1736 if (i >= NUM_LAYERS_MAX) {
1737 return NO_MEMORY;
1738 }
1739 mPid = pid;
1740 mInUse.add(uint8_t(i));
1741 mBitmap |= 1<<(31-i);
1742 return i;
1743}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001744
1745status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746{
1747 ssize_t idx = mInUse.indexOf(id);
1748 if (idx < 0)
1749 return NAME_NOT_FOUND;
1750 return mLayers.insertAt(layer, idx);
1751}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001752
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753void Client::free(int32_t id)
1754{
1755 ssize_t idx = mInUse.remove(uint8_t(id));
1756 if (idx >= 0) {
1757 mBitmap &= ~(1<<(31-id));
1758 mLayers.removeItemsAt(idx);
1759 }
1760}
1761
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762bool Client::isValid(int32_t i) const {
1763 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1764}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001765
1766sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1767 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001768 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001769 if (idx >= 0) {
1770 lbc = mLayers[idx].promote();
1771 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1772 }
1773 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774}
1775
1776void Client::dump(const char* what)
1777{
1778}
1779
1780// ---------------------------------------------------------------------------
1781#if 0
1782#pragma mark -
1783#endif
1784
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001785BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786 : mId(cid), mFlinger(flinger), mCblk(cblk)
1787{
1788}
1789
1790BClient::~BClient() {
1791 // destroy all resources attached to this client
1792 mFlinger->destroyConnection(mId);
1793}
1794
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001795sp<IMemoryHeap> BClient::getControlBlock() const {
1796 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797}
1798
1799sp<ISurface> BClient::createSurface(
1800 ISurfaceFlingerClient::surface_data_t* params, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001801 const String8& name,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1803 uint32_t flags)
1804{
Mathias Agopian285dbde2010-03-01 16:09:43 -08001805 return mFlinger->createSurface(mId, pid, name, params, display, w, h,
1806 format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807}
1808
1809status_t BClient::destroySurface(SurfaceID sid)
1810{
1811 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001812 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001813}
1814
1815status_t BClient::setState(int32_t count, const layer_state_t* states)
1816{
1817 return mFlinger->setClientState(mId, count, states);
1818}
1819
1820// ---------------------------------------------------------------------------
1821
1822GraphicPlane::GraphicPlane()
1823 : mHw(0)
1824{
1825}
1826
1827GraphicPlane::~GraphicPlane() {
1828 delete mHw;
1829}
1830
1831bool GraphicPlane::initialized() const {
1832 return mHw ? true : false;
1833}
1834
Mathias Agopian2b92d892010-02-08 15:49:35 -08001835int GraphicPlane::getWidth() const {
1836 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001837}
1838
Mathias Agopian2b92d892010-02-08 15:49:35 -08001839int GraphicPlane::getHeight() const {
1840 return mHeight;
1841}
1842
1843void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1844{
1845 mHw = hw;
1846
1847 // initialize the display orientation transform.
1848 // it's a constant that should come from the display driver.
1849 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1850 char property[PROPERTY_VALUE_MAX];
1851 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1852 //displayOrientation
1853 switch (atoi(property)) {
1854 case 90:
1855 displayOrientation = ISurfaceComposer::eOrientation90;
1856 break;
1857 case 270:
1858 displayOrientation = ISurfaceComposer::eOrientation270;
1859 break;
1860 }
1861 }
1862
1863 const float w = hw->getWidth();
1864 const float h = hw->getHeight();
1865 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1866 &mDisplayTransform);
1867 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1868 mDisplayWidth = h;
1869 mDisplayHeight = w;
1870 } else {
1871 mDisplayWidth = w;
1872 mDisplayHeight = h;
1873 }
1874
1875 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001876}
1877
1878status_t GraphicPlane::orientationToTransfrom(
1879 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001880{
1881 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882 switch (orientation) {
1883 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001884 flags = Transform::ROT_0;
1885 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001886 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001887 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 break;
1889 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001890 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891 break;
1892 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001893 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894 break;
1895 default:
1896 return BAD_VALUE;
1897 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001898 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899 return NO_ERROR;
1900}
1901
1902status_t GraphicPlane::setOrientation(int orientation)
1903{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904 // If the rotation can be handled in hardware, this is where
1905 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001906
1907 const DisplayHardware& hw(displayHardware());
1908 const float w = mDisplayWidth;
1909 const float h = mDisplayHeight;
1910 mWidth = int(w);
1911 mHeight = int(h);
1912
1913 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001914 GraphicPlane::orientationToTransfrom(orientation, w, h,
1915 &orientationTransform);
1916 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1917 mWidth = int(h);
1918 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001920
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001921 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001922 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001923 return NO_ERROR;
1924}
1925
1926const DisplayHardware& GraphicPlane::displayHardware() const {
1927 return *mHw;
1928}
1929
1930const Transform& GraphicPlane::transform() const {
1931 return mGlobalTransform;
1932}
1933
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001934EGLDisplay GraphicPlane::getEGLDisplay() const {
1935 return mHw->getEGLDisplay();
1936}
1937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938// ---------------------------------------------------------------------------
1939
1940}; // namespace android