blob: ed75d8ef4e03cd891be88e58645d91994f9708ea [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
Mathias Agopian78fd5012010-04-20 14:51:04 -0700209 LOGI_IF(mDebugRegion, "showupdates enabled");
210 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();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360
361 // Initialize OpenGL|ES
362 glActiveTexture(GL_TEXTURE0);
363 glBindTexture(GL_TEXTURE_2D, 0);
364 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
365 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
366 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
367 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
368 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
369 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
370 glPixelStorei(GL_PACK_ALIGNMENT, 4);
371 glEnableClientState(GL_VERTEX_ARRAY);
372 glEnable(GL_SCISSOR_TEST);
373 glShadeModel(GL_FLAT);
374 glDisable(GL_DITHER);
375 glDisable(GL_CULL_FACE);
376
377 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
378 const uint16_t g1 = pack565(0x17,0x2f,0x17);
379 const uint16_t textureData[4] = { g0, g1, g1, g0 };
380 glGenTextures(1, &mWormholeTexName);
381 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
382 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
383 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
384 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
385 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
386 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
387 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
388
389 glViewport(0, 0, w, h);
390 glMatrixMode(GL_PROJECTION);
391 glLoadIdentity();
392 glOrthof(0, w, h, 0, 0, 1);
393
394 LayerDim::initDimmer(this, w, h);
395
396 mReadyToRunBarrier.open();
397
398 /*
399 * We're now ready to accept clients...
400 */
401
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700402 // start boot animation
403 property_set("ctl.start", "bootanim");
404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405 return NO_ERROR;
406}
407
408// ----------------------------------------------------------------------------
409#if 0
410#pragma mark -
411#pragma mark Events Handler
412#endif
413
414void SurfaceFlinger::waitForEvent()
415{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700416 while (true) {
417 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800418 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700419 if (UNLIKELY(isFrozen())) {
420 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700421 const nsecs_t now = systemTime();
422 if (mFreezeDisplayTime == 0) {
423 mFreezeDisplayTime = now;
424 }
425 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
426 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700427 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700428
Mathias Agopianb6683b52009-04-28 03:17:50 -0700429 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800430
431 // see if we timed out
432 if (isFrozen()) {
433 const nsecs_t now = systemTime();
434 nsecs_t frozenTime = (now - mFreezeDisplayTime);
435 if (frozenTime >= freezeDisplayTimeout) {
436 // we timed out and are still frozen
437 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
438 mFreezeDisplay, mFreezeCount);
439 mFreezeDisplayTime = 0;
440 mFreezeCount = 0;
441 mFreezeDisplay = false;
442 }
443 }
444
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700445 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700446 switch (msg->what) {
447 case MessageQueue::INVALIDATE:
448 // invalidate message, just return to the main loop
449 return;
450 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452 }
453}
454
455void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700456 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457}
458
459void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700460 // this is the IPC call
461 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462}
463
464void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
465{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700466 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467}
468
469// ----------------------------------------------------------------------------
470#if 0
471#pragma mark -
472#pragma mark Main loop
473#endif
474
475bool SurfaceFlinger::threadLoop()
476{
477 waitForEvent();
478
479 // check for transactions
480 if (UNLIKELY(mConsoleSignals)) {
481 handleConsoleEvents();
482 }
483
484 if (LIKELY(mTransactionCount == 0)) {
485 // if we're in a global transaction, don't do anything.
486 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
487 uint32_t transactionFlags = getTransactionFlags(mask);
488 if (LIKELY(transactionFlags)) {
489 handleTransaction(transactionFlags);
490 }
491 }
492
493 // post surfaces (if needed)
494 handlePageFlip();
495
496 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700497 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 // repaint the framebuffer (if needed)
499 handleRepaint();
500
Mathias Agopian74faca22009-09-17 16:18:16 -0700501 // inform the h/w that we're done compositing
502 hw.compositionComplete();
503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504 // release the clients before we flip ('cause flip might block)
505 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 postFramebuffer();
508 } else {
509 // pretend we did the post
510 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 usleep(16667); // 60 fps period
512 }
513 return true;
514}
515
516void SurfaceFlinger::postFramebuffer()
517{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 if (!mInvalidRegion.isEmpty()) {
519 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700520 const nsecs_t now = systemTime();
521 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700523 mLastSwapBufferTime = systemTime() - now;
524 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 }
527}
528
529void SurfaceFlinger::handleConsoleEvents()
530{
531 // something to do with the console
532 const DisplayHardware& hw = graphicPlane(0).displayHardware();
533
534 int what = android_atomic_and(0, &mConsoleSignals);
535 if (what & eConsoleAcquired) {
536 hw.acquireScreen();
537 }
538
539 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700540 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 hw.releaseScreen();
543 }
544
545 if (what & eConsoleReleased) {
546 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 hw.releaseScreen();
548 } else {
549 mDeferReleaseConsole = true;
550 }
551 }
552
553 mDirtyRegion.set(hw.bounds());
554}
555
556void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
557{
Mathias Agopian3d579642009-06-04 18:46:21 -0700558 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559
Mathias Agopian3d579642009-06-04 18:46:21 -0700560 { // scope for the lock
561 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700562 const nsecs_t now = systemTime();
563 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700564 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700565 mLastTransactionTime = systemTime() - now;
566 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700567 }
568
569 // do this without lock held
570 const size_t count = ditchedLayers.size();
571 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700572 if (ditchedLayers[i] != 0) {
573 //LOGD("ditching layer %p", ditchedLayers[i].get());
574 ditchedLayers[i]->ditch();
575 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700576 }
577}
578
579void SurfaceFlinger::handleTransactionLocked(
580 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
581{
582 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 const size_t count = currentLayers.size();
584
585 /*
586 * Traversal of the children
587 * (perform the transaction for each of them if needed)
588 */
589
590 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
591 if (layersNeedTransaction) {
592 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
595 if (!trFlags) continue;
596
597 const uint32_t flags = layer->doTransaction(0);
598 if (flags & Layer::eVisibleRegion)
599 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 }
601 }
602
603 /*
604 * Perform our own transaction if needed
605 */
606
607 if (transactionFlags & eTransactionNeeded) {
608 if (mCurrentState.orientation != mDrawingState.orientation) {
609 // the orientation has changed, recompute all visible regions
610 // and invalidate everything.
611
612 const int dpy = 0;
613 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700614 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 GraphicPlane& plane(graphicPlane(dpy));
616 plane.setOrientation(orientation);
617
618 // update the shared control block
619 const DisplayHardware& hw(plane.displayHardware());
620 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
621 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800622 dcblk->w = plane.getWidth();
623 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624
625 mVisibleRegionsDirty = true;
626 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 }
628
629 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
630 // freezing or unfreezing the display -> trigger animation if needed
631 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800632 if (mFreezeDisplay)
633 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 }
635
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700636 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
637 // layers have been added
638 mVisibleRegionsDirty = true;
639 }
640
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 // some layers might have been removed, so
642 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700643 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700644 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700646 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700647 const size_t count = previousLayers.size();
648 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700649 const sp<LayerBase>& layer(previousLayers[i]);
650 if (currentLayers.indexOf( layer ) < 0) {
651 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700652 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700653 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700654 }
655 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 }
657
658 // get rid of all resources we don't need anymore
659 // (layers and clients)
660 free_resources_l();
661 }
662
663 commitTransaction();
664}
665
666sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
667{
668 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
669}
670
671void SurfaceFlinger::computeVisibleRegions(
672 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
673{
674 const GraphicPlane& plane(graphicPlane(0));
675 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700676 const DisplayHardware& hw(plane.displayHardware());
677 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678
679 Region aboveOpaqueLayers;
680 Region aboveCoveredLayers;
681 Region dirty;
682
683 bool secureFrameBuffer = false;
684
685 size_t i = currentLayers.size();
686 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700687 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 layer->validateVisibility(planeTransform);
689
690 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700691 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
Mathias Agopianab028732010-03-16 16:41:46 -0700693 /*
694 * opaqueRegion: area of a surface that is fully opaque.
695 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700697
698 /*
699 * visibleRegion: area of a surface that is visible on screen
700 * and not fully transparent. This is essentially the layer's
701 * footprint minus the opaque regions above it.
702 * Areas covered by a translucent surface are considered visible.
703 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700705
706 /*
707 * coveredRegion: area of a surface that is covered by all
708 * visible regions above it (which includes the translucent areas).
709 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700711
712
713 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700714 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700716 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700718 visibleRegion.andSelf(screenRegion);
719 if (!visibleRegion.isEmpty()) {
720 // Remove the transparent area from the visible region
721 if (translucent) {
722 visibleRegion.subtractSelf(layer->transparentRegionScreen);
723 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724
Mathias Agopianab028732010-03-16 16:41:46 -0700725 // compute the opaque region
726 const int32_t layerOrientation = layer->getOrientation();
727 if (s.alpha==255 && !translucent &&
728 ((layerOrientation & Transform::ROT_INVALID) == false)) {
729 // the opaque region is the layer's footprint
730 opaqueRegion = visibleRegion;
731 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 }
733 }
734
Mathias Agopianab028732010-03-16 16:41:46 -0700735 // Clip the covered region to the visible region
736 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
737
738 // Update aboveCoveredLayers for next (lower) layer
739 aboveCoveredLayers.orSelf(visibleRegion);
740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 // subtract the opaque region covered by the layers above us
742 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743
744 // compute this layer's dirty region
745 if (layer->contentDirty) {
746 // we need to invalidate the whole region
747 dirty = visibleRegion;
748 // as well, as the old visible region
749 dirty.orSelf(layer->visibleRegionScreen);
750 layer->contentDirty = false;
751 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700752 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700753 * the exposed region consists of two components:
754 * 1) what's VISIBLE now and was COVERED before
755 * 2) what's EXPOSED now less what was EXPOSED before
756 *
757 * note that (1) is conservative, we start with the whole
758 * visible region but only keep what used to be covered by
759 * something -- which mean it may have been exposed.
760 *
761 * (2) handles areas that were not covered by anything but got
762 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700763 */
Mathias Agopianab028732010-03-16 16:41:46 -0700764 const Region newExposed = visibleRegion - coveredRegion;
765 const Region oldVisibleRegion = layer->visibleRegionScreen;
766 const Region oldCoveredRegion = layer->coveredRegionScreen;
767 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
768 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769 }
770 dirty.subtractSelf(aboveOpaqueLayers);
771
772 // accumulate to the screen dirty region
773 dirtyRegion.orSelf(dirty);
774
Mathias Agopianab028732010-03-16 16:41:46 -0700775 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777
778 // Store the visible region is screen space
779 layer->setVisibleRegion(visibleRegion);
780 layer->setCoveredRegion(coveredRegion);
781
Mathias Agopian97011222009-07-28 10:57:27 -0700782 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783 if (layer->isSecure() && !visibleRegion.isEmpty()) {
784 secureFrameBuffer = true;
785 }
786 }
787
Mathias Agopian97011222009-07-28 10:57:27 -0700788 // invalidate the areas where a layer was removed
789 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
790 mDirtyRegionRemovedLayer.clear();
791
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 mSecureFrameBuffer = secureFrameBuffer;
793 opaqueRegion = aboveOpaqueLayers;
794}
795
796
797void SurfaceFlinger::commitTransaction()
798{
799 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700800 mResizeTransationPending = false;
801 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802}
803
804void SurfaceFlinger::handlePageFlip()
805{
806 bool visibleRegions = mVisibleRegionsDirty;
807 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
808 visibleRegions |= lockPageFlip(currentLayers);
809
810 const DisplayHardware& hw = graphicPlane(0).displayHardware();
811 const Region screenRegion(hw.bounds());
812 if (visibleRegions) {
813 Region opaqueRegion;
814 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
815 mWormholeRegion = screenRegion.subtract(opaqueRegion);
816 mVisibleRegionsDirty = false;
817 }
818
819 unlockPageFlip(currentLayers);
820 mDirtyRegion.andSelf(screenRegion);
821}
822
823bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
824{
825 bool recomputeVisibleRegions = false;
826 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700827 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700829 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 layer->lockPageFlip(recomputeVisibleRegions);
831 }
832 return recomputeVisibleRegions;
833}
834
835void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
836{
837 const GraphicPlane& plane(graphicPlane(0));
838 const Transform& planeTransform(plane.transform());
839 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700840 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700842 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 layer->unlockPageFlip(planeTransform, mDirtyRegion);
844 }
845}
846
Mathias Agopianb8a55602009-06-26 19:06:36 -0700847
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848void SurfaceFlinger::handleRepaint()
849{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700850 // compute the invalid region
851 mInvalidRegion.orSelf(mDirtyRegion);
852 if (mInvalidRegion.isEmpty()) {
853 // nothing to do
854 return;
855 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856
857 if (UNLIKELY(mDebugRegion)) {
858 debugFlashRegions();
859 }
860
Mathias Agopianb8a55602009-06-26 19:06:36 -0700861 // set the frame buffer
862 const DisplayHardware& hw(graphicPlane(0).displayHardware());
863 glMatrixMode(GL_MODELVIEW);
864 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865
866 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700867 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
868 (flags & DisplayHardware::BUFFER_PRESERVED))
869 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700870 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
871 // takes a rectangle, we must make sure to update that whole
872 // rectangle in that case
873 if (flags & DisplayHardware::SWAP_RECTANGLE) {
874 // FIXME: we really should be able to pass a region to
875 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 mDirtyRegion.set(mInvalidRegion.bounds());
877 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700878 // in the BUFFER_PRESERVED case, obviously, we can update only
879 // what's needed and nothing more.
880 // NOTE: this is NOT a common case, as preserving the backbuffer
881 // is costly and usually involves copying the whole update back.
882 }
883 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700884 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700885 // We need to redraw the rectangle that will be updated
886 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700887 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700888 // rectangle instead of a region (see DisplayHardware::flip())
889 mDirtyRegion.set(mInvalidRegion.bounds());
890 } else {
891 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 mDirtyRegion.set(hw.bounds());
893 mInvalidRegion = mDirtyRegion;
894 }
895 }
896
897 // compose all surfaces
898 composeSurfaces(mDirtyRegion);
899
900 // clear the dirty regions
901 mDirtyRegion.clear();
902}
903
904void SurfaceFlinger::composeSurfaces(const Region& dirty)
905{
906 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
907 // should never happen unless the window manager has a bug
908 // draw something...
909 drawWormhole();
910 }
911 const SurfaceFlinger& flinger(*this);
912 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
913 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700914 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700916 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 const Region& visibleRegion(layer->visibleRegionScreen);
918 if (!visibleRegion.isEmpty()) {
919 const Region clip(dirty.intersect(visibleRegion));
920 if (!clip.isEmpty()) {
921 layer->draw(clip);
922 }
923 }
924 }
925}
926
927void SurfaceFlinger::unlockClients()
928{
929 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
930 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700931 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700933 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 layer->finishPageFlip();
935 }
936}
937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938void SurfaceFlinger::debugFlashRegions()
939{
Mathias Agopian0926f502009-05-04 14:17:04 -0700940 const DisplayHardware& hw(graphicPlane(0).displayHardware());
941 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700942
943 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
944 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700945 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopian0926f502009-05-04 14:17:04 -0700946 mDirtyRegion.bounds() : hw.bounds());
947 composeSurfaces(repaint);
948 }
949
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 glDisable(GL_TEXTURE_2D);
951 glDisable(GL_BLEND);
952 glDisable(GL_DITHER);
953 glDisable(GL_SCISSOR_TEST);
954
Mathias Agopian0926f502009-05-04 14:17:04 -0700955 static int toggle = 0;
956 toggle = 1 - toggle;
957 if (toggle) {
958 glColor4x(0x10000, 0, 0x10000, 0x10000);
959 } else {
960 glColor4x(0x10000, 0x10000, 0, 0x10000);
961 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962
Mathias Agopian20f68782009-05-11 00:03:41 -0700963 Region::const_iterator it = mDirtyRegion.begin();
964 Region::const_iterator const end = mDirtyRegion.end();
965 while (it != end) {
966 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967 GLfloat vertices[][2] = {
968 { r.left, r.top },
969 { r.left, r.bottom },
970 { r.right, r.bottom },
971 { r.right, r.top }
972 };
973 glVertexPointer(2, GL_FLOAT, 0, vertices);
974 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
975 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700976
Mathias Agopianb8a55602009-06-26 19:06:36 -0700977 if (mInvalidRegion.isEmpty()) {
978 mDirtyRegion.dump("mDirtyRegion");
979 mInvalidRegion.dump("mInvalidRegion");
980 }
981 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982
983 if (mDebugRegion > 1)
984 usleep(mDebugRegion * 1000);
985
986 glEnable(GL_SCISSOR_TEST);
987 //mDirtyRegion.dump("mDirtyRegion");
988}
989
990void SurfaceFlinger::drawWormhole() const
991{
992 const Region region(mWormholeRegion.intersect(mDirtyRegion));
993 if (region.isEmpty())
994 return;
995
996 const DisplayHardware& hw(graphicPlane(0).displayHardware());
997 const int32_t width = hw.getWidth();
998 const int32_t height = hw.getHeight();
999
1000 glDisable(GL_BLEND);
1001 glDisable(GL_DITHER);
1002
1003 if (LIKELY(!mDebugBackground)) {
1004 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001005 Region::const_iterator it = region.begin();
1006 Region::const_iterator const end = region.end();
1007 while (it != end) {
1008 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 const GLint sy = height - (r.top + r.height());
1010 glScissor(r.left, sy, r.width(), r.height());
1011 glClear(GL_COLOR_BUFFER_BIT);
1012 }
1013 } else {
1014 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1015 { width, height }, { 0, height } };
1016 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1017 glVertexPointer(2, GL_SHORT, 0, vertices);
1018 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1019 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1020 glEnable(GL_TEXTURE_2D);
1021 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1022 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1023 glMatrixMode(GL_TEXTURE);
1024 glLoadIdentity();
1025 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001026 Region::const_iterator it = region.begin();
1027 Region::const_iterator const end = region.end();
1028 while (it != end) {
1029 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 const GLint sy = height - (r.top + r.height());
1031 glScissor(r.left, sy, r.width(), r.height());
1032 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1033 }
1034 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1035 }
1036}
1037
1038void SurfaceFlinger::debugShowFPS() const
1039{
1040 static int mFrameCount;
1041 static int mLastFrameCount = 0;
1042 static nsecs_t mLastFpsTime = 0;
1043 static float mFps = 0;
1044 mFrameCount++;
1045 nsecs_t now = systemTime();
1046 nsecs_t diff = now - mLastFpsTime;
1047 if (diff > ms2ns(250)) {
1048 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1049 mLastFpsTime = now;
1050 mLastFrameCount = mFrameCount;
1051 }
1052 // XXX: mFPS has the value we want
1053 }
1054
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001055status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056{
1057 Mutex::Autolock _l(mStateLock);
1058 addLayer_l(layer);
1059 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1060 return NO_ERROR;
1061}
1062
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001063status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064{
1065 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001066 status_t err = purgatorizeLayer_l(layer);
1067 if (err == NO_ERROR)
1068 setTransactionFlags(eTransactionNeeded);
1069 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070}
1071
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001072status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073{
1074 layer->forceVisibilityTransaction();
1075 setTransactionFlags(eTraversalNeeded);
1076 return NO_ERROR;
1077}
1078
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001079status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080{
Mathias Agopian0852e672009-09-04 19:50:23 -07001081 if (layer == 0)
1082 return BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 ssize_t i = mCurrentState.layersSortedByZ.add(
1084 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001085 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1086 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 mLayerMap.add(lbc->serverIndex(), lbc);
1088 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 return NO_ERROR;
1090}
1091
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001092status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093{
1094 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1095 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001096 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001097 sp<LayerBaseClient> layer =
1098 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001099 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 mLayerMap.removeItem(layer->serverIndex());
1101 }
1102 return NO_ERROR;
1103 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001104 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105}
1106
Mathias Agopian9a112062009-04-17 19:36:26 -07001107status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1108{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001109 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001110 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001111
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001112 layerBase->onRemoved();
1113
Mathias Agopian3d579642009-06-04 18:46:21 -07001114 // it's possible that we don't find a layer, because it might
1115 // have been destroyed already -- this is not technically an error
1116 // from the user because there is a race between BClient::destroySurface(),
1117 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001118 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1119}
1120
1121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122void SurfaceFlinger::free_resources_l()
1123{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001125 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 const size_t count = disconnectedClients.size();
1127 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001128 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 }
1131 disconnectedClients.clear();
1132}
1133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1135{
1136 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1137}
1138
1139uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1140{
1141 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1142 if ((old & flags)==0) { // wake the server up
1143 if (delay > 0) {
1144 signalDelayedEvent(delay);
1145 } else {
1146 signalEvent();
1147 }
1148 }
1149 return old;
1150}
1151
1152void SurfaceFlinger::openGlobalTransaction()
1153{
1154 android_atomic_inc(&mTransactionCount);
1155}
1156
1157void SurfaceFlinger::closeGlobalTransaction()
1158{
1159 if (android_atomic_dec(&mTransactionCount) == 1) {
1160 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001161
1162 // if there is a transaction with a resize, wait for it to
1163 // take effect before returning.
1164 Mutex::Autolock _l(mStateLock);
1165 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001166 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1167 if (CC_UNLIKELY(err != NO_ERROR)) {
1168 // just in case something goes wrong in SF, return to the
1169 // called after a few seconds.
1170 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1171 mResizeTransationPending = false;
1172 break;
1173 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001174 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 }
1176}
1177
1178status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1179{
1180 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1181 return BAD_VALUE;
1182
1183 Mutex::Autolock _l(mStateLock);
1184 mCurrentState.freezeDisplay = 1;
1185 setTransactionFlags(eTransactionNeeded);
1186
1187 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001188 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 return NO_ERROR;
1190}
1191
1192status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1193{
1194 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1195 return BAD_VALUE;
1196
1197 Mutex::Autolock _l(mStateLock);
1198 mCurrentState.freezeDisplay = 0;
1199 setTransactionFlags(eTransactionNeeded);
1200
1201 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001202 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 return NO_ERROR;
1204}
1205
Mathias Agopianc08731e2009-03-27 18:11:38 -07001206int SurfaceFlinger::setOrientation(DisplayID dpy,
1207 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208{
1209 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1210 return BAD_VALUE;
1211
1212 Mutex::Autolock _l(mStateLock);
1213 if (mCurrentState.orientation != orientation) {
1214 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001215 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216 mCurrentState.orientation = orientation;
1217 setTransactionFlags(eTransactionNeeded);
1218 mTransactionCV.wait(mStateLock);
1219 } else {
1220 orientation = BAD_VALUE;
1221 }
1222 }
1223 return orientation;
1224}
1225
1226sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001227 const String8& name, ISurfaceFlingerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1229 uint32_t flags)
1230{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001231 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001233
1234 if (int32_t(w|h) < 0) {
1235 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1236 int(w), int(h));
1237 return surfaceHandle;
1238 }
1239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001241 sp<Client> client = mClientsMap.valueFor(clientId);
1242 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1244 return surfaceHandle;
1245 }
1246
1247 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001248 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1250 LOGE("createSurface() failed, generateId = %d", id);
1251 return surfaceHandle;
1252 }
1253
1254 switch (flags & eFXSurfaceMask) {
1255 case eFXSurfaceNormal:
1256 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001257 layer = createPushBuffersSurfaceLocked(client, d, id,
1258 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001260 layer = createNormalSurfaceLocked(client, d, id,
1261 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 }
1263 break;
1264 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001265 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 break;
1267 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001268 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 break;
1270 }
1271
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001272 if (layer != 0) {
Mathias Agopian285dbde2010-03-01 16:09:43 -08001273 layer->setName(name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 setTransactionFlags(eTransactionNeeded);
1275 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001276 if (surfaceHandle != 0) {
1277 params->token = surfaceHandle->getToken();
1278 params->identity = surfaceHandle->getIdentity();
1279 params->width = w;
1280 params->height = h;
1281 params->format = format;
1282 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 }
1284
1285 return surfaceHandle;
1286}
1287
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001288sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001289 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001290 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1291 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292{
1293 // initialize the surfaces
1294 switch (format) { // TODO: take h/w into account
1295 case PIXEL_FORMAT_TRANSPARENT:
1296 case PIXEL_FORMAT_TRANSLUCENT:
1297 format = PIXEL_FORMAT_RGBA_8888;
1298 break;
1299 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001300 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 break;
1302 }
1303
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001304 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001305 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 if (LIKELY(err == NO_ERROR)) {
1307 layer->initStates(w, h, flags);
1308 addLayer_l(layer);
1309 } else {
1310 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001311 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 }
1313 return layer;
1314}
1315
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001316sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001317 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1319{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001320 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 layer->initStates(w, h, flags);
1322 addLayer_l(layer);
1323 return layer;
1324}
1325
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001326sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001327 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1329{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001330 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 layer->initStates(w, h, flags);
1332 addLayer_l(layer);
1333 return layer;
1334}
1335
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001336sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001337 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1339{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001340 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001341 layer->initStates(w, h, flags);
1342 addLayer_l(layer);
1343 return layer;
1344}
1345
Mathias Agopian9a112062009-04-17 19:36:26 -07001346status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347{
Mathias Agopian9a112062009-04-17 19:36:26 -07001348 /*
1349 * called by the window manager, when a surface should be marked for
1350 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001351 *
1352 * The surface is removed from the current and drawing lists, but placed
1353 * in the purgatory queue, so it's not destroyed right-away (we need
1354 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001355 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001356
Mathias Agopian48d819a2009-09-10 19:41:18 -07001357 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001358 Mutex::Autolock _l(mStateLock);
1359 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001360 if (layer != 0) {
1361 err = purgatorizeLayer_l(layer);
1362 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001363 setTransactionFlags(eTransactionNeeded);
1364 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001365 }
1366 return err;
1367}
1368
1369status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1370{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001371 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001372
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001373 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001374 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001375 sp<LayerBaseClient> layer;
1376 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001377 MessageDestroySurface(
1378 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1379 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001380 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001381 sp<LayerBaseClient> l(layer);
1382 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001383 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001384 /*
1385 * remove the layer from the current list -- chances are that it's
1386 * not in the list anyway, because it should have been removed
1387 * already upon request of the client (eg: window manager).
1388 * However, a buggy client could have not done that.
1389 * Since we know we don't have any more clients, we don't need
1390 * to use the purgatory.
1391 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001392 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001393 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1394 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001395 return true;
1396 }
1397 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001398
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001399 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 return NO_ERROR;
1401}
1402
1403status_t SurfaceFlinger::setClientState(
1404 ClientID cid,
1405 int32_t count,
1406 const layer_state_t* states)
1407{
1408 Mutex::Autolock _l(mStateLock);
1409 uint32_t flags = 0;
1410 cid <<= 16;
1411 for (int i=0 ; i<count ; i++) {
1412 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001413 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001414 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 if (what & ePositionChanged) {
1417 if (layer->setPosition(s.x, s.y))
1418 flags |= eTraversalNeeded;
1419 }
1420 if (what & eLayerChanged) {
1421 if (layer->setLayer(s.z)) {
1422 mCurrentState.layersSortedByZ.reorder(
1423 layer, &Layer::compareCurrentStateZ);
1424 // we need traversal (state changed)
1425 // AND transaction (list changed)
1426 flags |= eTransactionNeeded|eTraversalNeeded;
1427 }
1428 }
1429 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001430 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001432 mResizeTransationPending = true;
1433 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 }
1435 if (what & eAlphaChanged) {
1436 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1437 flags |= eTraversalNeeded;
1438 }
1439 if (what & eMatrixChanged) {
1440 if (layer->setMatrix(s.matrix))
1441 flags |= eTraversalNeeded;
1442 }
1443 if (what & eTransparentRegionChanged) {
1444 if (layer->setTransparentRegionHint(s.transparentRegion))
1445 flags |= eTraversalNeeded;
1446 }
1447 if (what & eVisibilityChanged) {
1448 if (layer->setFlags(s.flags, s.mask))
1449 flags |= eTraversalNeeded;
1450 }
1451 }
1452 }
1453 if (flags) {
1454 setTransactionFlags(flags);
1455 }
1456 return NO_ERROR;
1457}
1458
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001459sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001461 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1462 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463}
1464
1465void SurfaceFlinger::screenReleased(int dpy)
1466{
1467 // this may be called by a signal handler, we can't do too much in here
1468 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1469 signalEvent();
1470}
1471
1472void SurfaceFlinger::screenAcquired(int dpy)
1473{
1474 // this may be called by a signal handler, we can't do too much in here
1475 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1476 signalEvent();
1477}
1478
1479status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1480{
1481 const size_t SIZE = 1024;
1482 char buffer[SIZE];
1483 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001484 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485 snprintf(buffer, SIZE, "Permission Denial: "
1486 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1487 IPCThreadState::self()->getCallingPid(),
1488 IPCThreadState::self()->getCallingUid());
1489 result.append(buffer);
1490 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001491
1492 // figure out if we're stuck somewhere
1493 const nsecs_t now = systemTime();
1494 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1495 const nsecs_t inTransaction(mDebugInTransaction);
1496 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1497 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1498
1499 // Try to get the main lock, but don't insist if we can't
1500 // (this would indicate SF is stuck, but we want to be able to
1501 // print something in dumpsys).
1502 int retry = 3;
1503 while (mStateLock.tryLock()<0 && --retry>=0) {
1504 usleep(1000000);
1505 }
1506 const bool locked(retry >= 0);
1507 if (!locked) {
1508 snprintf(buffer, SIZE,
1509 "SurfaceFlinger appears to be unresponsive, "
1510 "dumping anyways (no locks held)\n");
1511 result.append(buffer);
1512 }
1513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 size_t s = mClientsMap.size();
1515 char name[64];
1516 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001517 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 sprintf(name, " Client (id=0x%08x)", client->cid);
1519 client->dump(name);
1520 }
1521 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1522 const size_t count = currentLayers.size();
1523 for (size_t i=0 ; i<count ; i++) {
1524 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001525 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526 const Layer::State& s = layer->drawingState();
1527 snprintf(buffer, SIZE,
1528 "+ %s %p\n"
1529 " "
1530 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
Mathias Agopian401c2572009-09-23 19:16:27 -07001531 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001533 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534 s.z, layer->tx(), layer->ty(), s.w, s.h,
Mathias Agopian401c2572009-09-23 19:16:27 -07001535 layer->needsBlending(), layer->needsDithering(),
1536 layer->contentDirty,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 s.alpha, s.flags,
Mathias Agopianeda65402010-02-22 03:15:57 -08001538 s.transform[0][0], s.transform[0][1],
1539 s.transform[1][0], s.transform[1][1]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540 result.append(buffer);
1541 buffer[0] = 0;
1542 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001543 sp<LayerBaseClient> lbc =
1544 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1545 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001546 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 snprintf(buffer, SIZE,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001548 " name=%s\n", lbc->getName().string());
1549 result.append(buffer);
1550 snprintf(buffer, SIZE,
1551 " id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001552 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 lbc->getIdentity());
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001554
1555 result.append(buffer);
1556 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001559 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1560 if (l != 0) {
Mathias Agopian86f73292009-09-17 01:35:28 -07001561 SharedBufferStack::Statistics stats = l->lcblk->getStats();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001562 result.append( l->lcblk->dump(" ") );
Mathias Agopian3330b202009-10-05 17:07:12 -07001563 sp<const GraphicBuffer> buf0(l->getBuffer(0));
1564 sp<const GraphicBuffer> buf1(l->getBuffer(1));
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001565 uint32_t w0=0, h0=0, s0=0;
1566 uint32_t w1=0, h1=0, s1=0;
1567 if (buf0 != 0) {
1568 w0 = buf0->getWidth();
1569 h0 = buf0->getHeight();
1570 s0 = buf0->getStride();
1571 }
1572 if (buf1 != 0) {
1573 w1 = buf1->getWidth();
1574 h1 = buf1->getHeight();
1575 s1 = buf1->getStride();
1576 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 snprintf(buffer, SIZE,
1578 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001579 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian86f73292009-09-17 01:35:28 -07001580 " freezeLock=%p, dq-q-time=%u us\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 l->pixelFormat(),
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001582 w0, h0, s0, w1, h1, s1,
Mathias Agopian86f73292009-09-17 01:35:28 -07001583 l->getFreezeLock().get(), stats.totalTime);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001584 result.append(buffer);
1585 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 s.transparentRegion.dump(result, "transparentRegion");
1588 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1589 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1590 }
1591 mWormholeRegion.dump(result, "WormholeRegion");
1592 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1593 snprintf(buffer, SIZE,
1594 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1595 mFreezeDisplay?"yes":"no", mFreezeCount,
1596 mCurrentState.orientation, hw.canDraw());
1597 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001598 snprintf(buffer, SIZE,
1599 " last eglSwapBuffers() time: %f us\n"
1600 " last transaction time : %f us\n",
1601 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1602 result.append(buffer);
1603 if (inSwapBuffersDuration || !locked) {
1604 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1605 inSwapBuffersDuration/1000.0);
1606 result.append(buffer);
1607 }
1608 if (inTransactionDuration || !locked) {
1609 snprintf(buffer, SIZE, " transaction time: %f us\n",
1610 inTransactionDuration/1000.0);
1611 result.append(buffer);
1612 }
Mathias Agopian759fdb22009-07-02 17:33:40 -07001613 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001614 result.append(buffer);
Mathias Agopian3330b202009-10-05 17:07:12 -07001615 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001616 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001617
1618 if (locked) {
1619 mStateLock.unlock();
1620 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621 }
1622 write(fd, result.string(), result.size());
1623 return NO_ERROR;
1624}
1625
1626status_t SurfaceFlinger::onTransact(
1627 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1628{
1629 switch (code) {
1630 case CREATE_CONNECTION:
1631 case OPEN_GLOBAL_TRANSACTION:
1632 case CLOSE_GLOBAL_TRANSACTION:
1633 case SET_ORIENTATION:
1634 case FREEZE_DISPLAY:
1635 case UNFREEZE_DISPLAY:
1636 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637 {
1638 // codes that require permission check
1639 IPCThreadState* ipc = IPCThreadState::self();
1640 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001641 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001642 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1643 LOGE("Permission Denial: "
1644 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1645 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 }
1647 }
1648 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1650 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001651 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001652 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1653 IPCThreadState* ipc = IPCThreadState::self();
1654 const int pid = ipc->getCallingPid();
1655 const int uid = ipc->getCallingUid();
1656 LOGE("Permission Denial: "
1657 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 return PERMISSION_DENIED;
1659 }
1660 int n;
1661 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001662 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001664 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665 return NO_ERROR;
1666 case 1002: // SHOW_UPDATES
1667 n = data.readInt32();
1668 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1669 return NO_ERROR;
1670 case 1003: // SHOW_BACKGROUND
1671 n = data.readInt32();
1672 mDebugBackground = n ? 1 : 0;
1673 return NO_ERROR;
1674 case 1004:{ // repaint everything
1675 Mutex::Autolock _l(mStateLock);
1676 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1677 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1678 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001679 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001681 case 1005:{ // force transaction
1682 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1683 return NO_ERROR;
1684 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 case 1007: // set mFreezeCount
1686 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001687 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688 return NO_ERROR;
1689 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001690 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 reply->writeInt32(0);
1692 reply->writeInt32(mDebugRegion);
1693 reply->writeInt32(mDebugBackground);
1694 return NO_ERROR;
1695 case 1013: {
1696 Mutex::Autolock _l(mStateLock);
1697 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1698 reply->writeInt32(hw.getPageFlipCount());
1699 }
1700 return NO_ERROR;
1701 }
1702 }
1703 return err;
1704}
1705
1706// ---------------------------------------------------------------------------
1707#if 0
1708#pragma mark -
1709#endif
1710
1711Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1712 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1713{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001715 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001716
1717 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1718 "SurfaceFlinger Client control-block");
1719
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001720 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001721 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001722 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723 }
1724}
1725
1726Client::~Client() {
1727 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001728 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 }
1730}
1731
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732int32_t Client::generateId(int pid)
1733{
1734 const uint32_t i = clz( ~mBitmap );
1735 if (i >= NUM_LAYERS_MAX) {
1736 return NO_MEMORY;
1737 }
1738 mPid = pid;
1739 mInUse.add(uint8_t(i));
1740 mBitmap |= 1<<(31-i);
1741 return i;
1742}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001743
1744status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745{
1746 ssize_t idx = mInUse.indexOf(id);
1747 if (idx < 0)
1748 return NAME_NOT_FOUND;
1749 return mLayers.insertAt(layer, idx);
1750}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001751
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752void Client::free(int32_t id)
1753{
1754 ssize_t idx = mInUse.remove(uint8_t(id));
1755 if (idx >= 0) {
1756 mBitmap &= ~(1<<(31-id));
1757 mLayers.removeItemsAt(idx);
1758 }
1759}
1760
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001761bool Client::isValid(int32_t i) const {
1762 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1763}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001764
1765sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1766 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001768 if (idx >= 0) {
1769 lbc = mLayers[idx].promote();
1770 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1771 }
1772 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773}
1774
1775void Client::dump(const char* what)
1776{
1777}
1778
1779// ---------------------------------------------------------------------------
1780#if 0
1781#pragma mark -
1782#endif
1783
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001784BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785 : mId(cid), mFlinger(flinger), mCblk(cblk)
1786{
1787}
1788
1789BClient::~BClient() {
1790 // destroy all resources attached to this client
1791 mFlinger->destroyConnection(mId);
1792}
1793
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001794sp<IMemoryHeap> BClient::getControlBlock() const {
1795 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796}
1797
1798sp<ISurface> BClient::createSurface(
1799 ISurfaceFlingerClient::surface_data_t* params, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001800 const String8& name,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1802 uint32_t flags)
1803{
Mathias Agopian285dbde2010-03-01 16:09:43 -08001804 return mFlinger->createSurface(mId, pid, name, params, display, w, h,
1805 format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001806}
1807
1808status_t BClient::destroySurface(SurfaceID sid)
1809{
1810 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001811 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001812}
1813
1814status_t BClient::setState(int32_t count, const layer_state_t* states)
1815{
1816 return mFlinger->setClientState(mId, count, states);
1817}
1818
1819// ---------------------------------------------------------------------------
1820
1821GraphicPlane::GraphicPlane()
1822 : mHw(0)
1823{
1824}
1825
1826GraphicPlane::~GraphicPlane() {
1827 delete mHw;
1828}
1829
1830bool GraphicPlane::initialized() const {
1831 return mHw ? true : false;
1832}
1833
Mathias Agopian2b92d892010-02-08 15:49:35 -08001834int GraphicPlane::getWidth() const {
1835 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836}
1837
Mathias Agopian2b92d892010-02-08 15:49:35 -08001838int GraphicPlane::getHeight() const {
1839 return mHeight;
1840}
1841
1842void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1843{
1844 mHw = hw;
1845
1846 // initialize the display orientation transform.
1847 // it's a constant that should come from the display driver.
1848 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1849 char property[PROPERTY_VALUE_MAX];
1850 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1851 //displayOrientation
1852 switch (atoi(property)) {
1853 case 90:
1854 displayOrientation = ISurfaceComposer::eOrientation90;
1855 break;
1856 case 270:
1857 displayOrientation = ISurfaceComposer::eOrientation270;
1858 break;
1859 }
1860 }
1861
1862 const float w = hw->getWidth();
1863 const float h = hw->getHeight();
1864 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1865 &mDisplayTransform);
1866 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1867 mDisplayWidth = h;
1868 mDisplayHeight = w;
1869 } else {
1870 mDisplayWidth = w;
1871 mDisplayHeight = h;
1872 }
1873
1874 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875}
1876
1877status_t GraphicPlane::orientationToTransfrom(
1878 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001879{
1880 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 switch (orientation) {
1882 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001883 flags = Transform::ROT_0;
1884 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001885 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001886 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 break;
1888 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001889 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890 break;
1891 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001892 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 break;
1894 default:
1895 return BAD_VALUE;
1896 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001897 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001898 return NO_ERROR;
1899}
1900
1901status_t GraphicPlane::setOrientation(int orientation)
1902{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001903 // If the rotation can be handled in hardware, this is where
1904 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001905
1906 const DisplayHardware& hw(displayHardware());
1907 const float w = mDisplayWidth;
1908 const float h = mDisplayHeight;
1909 mWidth = int(w);
1910 mHeight = int(h);
1911
1912 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001913 GraphicPlane::orientationToTransfrom(orientation, w, h,
1914 &orientationTransform);
1915 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1916 mWidth = int(h);
1917 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001919
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001920 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001921 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 return NO_ERROR;
1923}
1924
1925const DisplayHardware& GraphicPlane::displayHardware() const {
1926 return *mHw;
1927}
1928
1929const Transform& GraphicPlane::transform() const {
1930 return mGlobalTransform;
1931}
1932
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001933EGLDisplay GraphicPlane::getEGLDisplay() const {
1934 return mHw->getEGLDisplay();
1935}
1936
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937// ---------------------------------------------------------------------------
1938
1939}; // namespace android