blob: 62d829b853b86db81f580ae55e515af7c07b66fb [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{
1081 ssize_t i = mCurrentState.layersSortedByZ.add(
1082 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001083 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1084}
1085
1086status_t SurfaceFlinger::addClientLayer_l(const sp<LayerBaseClient>& lbc)
1087{
1088 ssize_t serverIndex = lbc->serverIndex();
1089 return mLayerMap.add(serverIndex, lbc);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090}
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 Agopian1b5e1022010-04-20 17:55:49 -07001097 ssize_t serverIndex = layerBase->serverIndex();
1098 if (serverIndex >= 0) {
1099 mLayerMap.removeItem(serverIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 }
1101 return NO_ERROR;
1102 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001103 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104}
1105
Mathias Agopian9a112062009-04-17 19:36:26 -07001106status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1107{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001108 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001109 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001110
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001111 layerBase->onRemoved();
1112
Mathias Agopian3d579642009-06-04 18:46:21 -07001113 // it's possible that we don't find a layer, because it might
1114 // have been destroyed already -- this is not technically an error
1115 // from the user because there is a race between BClient::destroySurface(),
1116 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001117 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1118}
1119
1120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121void SurfaceFlinger::free_resources_l()
1122{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001124 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 const size_t count = disconnectedClients.size();
1126 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001127 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 }
1130 disconnectedClients.clear();
1131}
1132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1134{
1135 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1136}
1137
1138uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1139{
1140 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1141 if ((old & flags)==0) { // wake the server up
1142 if (delay > 0) {
1143 signalDelayedEvent(delay);
1144 } else {
1145 signalEvent();
1146 }
1147 }
1148 return old;
1149}
1150
1151void SurfaceFlinger::openGlobalTransaction()
1152{
1153 android_atomic_inc(&mTransactionCount);
1154}
1155
1156void SurfaceFlinger::closeGlobalTransaction()
1157{
1158 if (android_atomic_dec(&mTransactionCount) == 1) {
1159 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001160
1161 // if there is a transaction with a resize, wait for it to
1162 // take effect before returning.
1163 Mutex::Autolock _l(mStateLock);
1164 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001165 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1166 if (CC_UNLIKELY(err != NO_ERROR)) {
1167 // just in case something goes wrong in SF, return to the
1168 // called after a few seconds.
1169 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1170 mResizeTransationPending = false;
1171 break;
1172 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001173 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174 }
1175}
1176
1177status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1178{
1179 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1180 return BAD_VALUE;
1181
1182 Mutex::Autolock _l(mStateLock);
1183 mCurrentState.freezeDisplay = 1;
1184 setTransactionFlags(eTransactionNeeded);
1185
1186 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001187 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 return NO_ERROR;
1189}
1190
1191status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1192{
1193 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1194 return BAD_VALUE;
1195
1196 Mutex::Autolock _l(mStateLock);
1197 mCurrentState.freezeDisplay = 0;
1198 setTransactionFlags(eTransactionNeeded);
1199
1200 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001201 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 return NO_ERROR;
1203}
1204
Mathias Agopianc08731e2009-03-27 18:11:38 -07001205int SurfaceFlinger::setOrientation(DisplayID dpy,
1206 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207{
1208 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1209 return BAD_VALUE;
1210
1211 Mutex::Autolock _l(mStateLock);
1212 if (mCurrentState.orientation != orientation) {
1213 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001214 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 mCurrentState.orientation = orientation;
1216 setTransactionFlags(eTransactionNeeded);
1217 mTransactionCV.wait(mStateLock);
1218 } else {
1219 orientation = BAD_VALUE;
1220 }
1221 }
1222 return orientation;
1223}
1224
1225sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001226 const String8& name, ISurfaceFlingerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1228 uint32_t flags)
1229{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001230 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001232
1233 if (int32_t(w|h) < 0) {
1234 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1235 int(w), int(h));
1236 return surfaceHandle;
1237 }
1238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001240 sp<Client> client = mClientsMap.valueFor(clientId);
1241 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1243 return surfaceHandle;
1244 }
1245
1246 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001247 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1249 LOGE("createSurface() failed, generateId = %d", id);
1250 return surfaceHandle;
1251 }
1252
1253 switch (flags & eFXSurfaceMask) {
1254 case eFXSurfaceNormal:
1255 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001256 layer = createPushBuffersSurfaceLocked(client, d, id,
1257 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001259 layer = createNormalSurfaceLocked(client, d, id,
1260 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 }
1262 break;
1263 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001264 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 break;
1266 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001267 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 break;
1269 }
1270
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001271 if (layer != 0) {
Mathias Agopian285dbde2010-03-01 16:09:43 -08001272 layer->setName(name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 setTransactionFlags(eTransactionNeeded);
1274 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001275 if (surfaceHandle != 0) {
1276 params->token = surfaceHandle->getToken();
1277 params->identity = surfaceHandle->getIdentity();
1278 params->width = w;
1279 params->height = h;
1280 params->format = format;
1281 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 }
1283
1284 return surfaceHandle;
1285}
1286
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001288 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001289 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1290 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291{
1292 // initialize the surfaces
1293 switch (format) { // TODO: take h/w into account
1294 case PIXEL_FORMAT_TRANSPARENT:
1295 case PIXEL_FORMAT_TRANSLUCENT:
1296 format = PIXEL_FORMAT_RGBA_8888;
1297 break;
1298 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001299 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 break;
1301 }
1302
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001303 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001304 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 if (LIKELY(err == NO_ERROR)) {
1306 layer->initStates(w, h, flags);
1307 addLayer_l(layer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001308 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 } 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);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001323 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 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);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001334 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 return layer;
1336}
1337
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001338sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001339 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1341{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001342 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 layer->initStates(w, h, flags);
1344 addLayer_l(layer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001345 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 return layer;
1347}
1348
Mathias Agopian9a112062009-04-17 19:36:26 -07001349status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350{
Mathias Agopian9a112062009-04-17 19:36:26 -07001351 /*
1352 * called by the window manager, when a surface should be marked for
1353 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001354 *
1355 * The surface is removed from the current and drawing lists, but placed
1356 * in the purgatory queue, so it's not destroyed right-away (we need
1357 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001358 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001359
Mathias Agopian48d819a2009-09-10 19:41:18 -07001360 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001361 Mutex::Autolock _l(mStateLock);
1362 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001363 if (layer != 0) {
1364 err = purgatorizeLayer_l(layer);
1365 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001366 setTransactionFlags(eTransactionNeeded);
1367 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001368 }
1369 return err;
1370}
1371
1372status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1373{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001374 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001375
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001376 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001377 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001378 sp<LayerBaseClient> layer;
1379 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001380 MessageDestroySurface(
1381 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1382 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001383 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001384 sp<LayerBaseClient> l(layer);
1385 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001386 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001387 /*
1388 * remove the layer from the current list -- chances are that it's
1389 * not in the list anyway, because it should have been removed
1390 * already upon request of the client (eg: window manager).
1391 * However, a buggy client could have not done that.
1392 * Since we know we don't have any more clients, we don't need
1393 * to use the purgatory.
1394 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001395 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001396 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1397 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001398 return true;
1399 }
1400 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001401
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001402 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403 return NO_ERROR;
1404}
1405
1406status_t SurfaceFlinger::setClientState(
1407 ClientID cid,
1408 int32_t count,
1409 const layer_state_t* states)
1410{
1411 Mutex::Autolock _l(mStateLock);
1412 uint32_t flags = 0;
1413 cid <<= 16;
1414 for (int i=0 ; i<count ; i++) {
1415 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001416 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001417 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 if (what & ePositionChanged) {
1420 if (layer->setPosition(s.x, s.y))
1421 flags |= eTraversalNeeded;
1422 }
1423 if (what & eLayerChanged) {
1424 if (layer->setLayer(s.z)) {
1425 mCurrentState.layersSortedByZ.reorder(
1426 layer, &Layer::compareCurrentStateZ);
1427 // we need traversal (state changed)
1428 // AND transaction (list changed)
1429 flags |= eTransactionNeeded|eTraversalNeeded;
1430 }
1431 }
1432 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001433 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001435 mResizeTransationPending = true;
1436 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 }
1438 if (what & eAlphaChanged) {
1439 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1440 flags |= eTraversalNeeded;
1441 }
1442 if (what & eMatrixChanged) {
1443 if (layer->setMatrix(s.matrix))
1444 flags |= eTraversalNeeded;
1445 }
1446 if (what & eTransparentRegionChanged) {
1447 if (layer->setTransparentRegionHint(s.transparentRegion))
1448 flags |= eTraversalNeeded;
1449 }
1450 if (what & eVisibilityChanged) {
1451 if (layer->setFlags(s.flags, s.mask))
1452 flags |= eTraversalNeeded;
1453 }
1454 }
1455 }
1456 if (flags) {
1457 setTransactionFlags(flags);
1458 }
1459 return NO_ERROR;
1460}
1461
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001462sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001464 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1465 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466}
1467
1468void SurfaceFlinger::screenReleased(int dpy)
1469{
1470 // this may be called by a signal handler, we can't do too much in here
1471 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1472 signalEvent();
1473}
1474
1475void SurfaceFlinger::screenAcquired(int dpy)
1476{
1477 // this may be called by a signal handler, we can't do too much in here
1478 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1479 signalEvent();
1480}
1481
1482status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1483{
1484 const size_t SIZE = 1024;
1485 char buffer[SIZE];
1486 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001487 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 snprintf(buffer, SIZE, "Permission Denial: "
1489 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1490 IPCThreadState::self()->getCallingPid(),
1491 IPCThreadState::self()->getCallingUid());
1492 result.append(buffer);
1493 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001494
1495 // figure out if we're stuck somewhere
1496 const nsecs_t now = systemTime();
1497 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1498 const nsecs_t inTransaction(mDebugInTransaction);
1499 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1500 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1501
1502 // Try to get the main lock, but don't insist if we can't
1503 // (this would indicate SF is stuck, but we want to be able to
1504 // print something in dumpsys).
1505 int retry = 3;
1506 while (mStateLock.tryLock()<0 && --retry>=0) {
1507 usleep(1000000);
1508 }
1509 const bool locked(retry >= 0);
1510 if (!locked) {
1511 snprintf(buffer, SIZE,
1512 "SurfaceFlinger appears to be unresponsive, "
1513 "dumping anyways (no locks held)\n");
1514 result.append(buffer);
1515 }
1516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1518 const size_t count = currentLayers.size();
1519 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001520 const sp<LayerBase>& layer(currentLayers[i]);
1521 layer->dump(result, buffer, SIZE);
1522 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001523 s.transparentRegion.dump(result, "transparentRegion");
1524 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1525 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1526 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001527
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 mWormholeRegion.dump(result, "WormholeRegion");
1529 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1530 snprintf(buffer, SIZE,
1531 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1532 mFreezeDisplay?"yes":"no", mFreezeCount,
1533 mCurrentState.orientation, hw.canDraw());
1534 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001535 snprintf(buffer, SIZE,
1536 " last eglSwapBuffers() time: %f us\n"
1537 " last transaction time : %f us\n",
1538 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1539 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001540
Mathias Agopian9795c422009-08-26 16:36:26 -07001541 if (inSwapBuffersDuration || !locked) {
1542 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1543 inSwapBuffersDuration/1000.0);
1544 result.append(buffer);
1545 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001546
Mathias Agopian9795c422009-08-26 16:36:26 -07001547 if (inTransactionDuration || !locked) {
1548 snprintf(buffer, SIZE, " transaction time: %f us\n",
1549 inTransactionDuration/1000.0);
1550 result.append(buffer);
1551 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001552
Mathias Agopian759fdb22009-07-02 17:33:40 -07001553 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001554 result.append(buffer);
Mathias Agopian3330b202009-10-05 17:07:12 -07001555 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001556 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001557
1558 if (locked) {
1559 mStateLock.unlock();
1560 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 }
1562 write(fd, result.string(), result.size());
1563 return NO_ERROR;
1564}
1565
1566status_t SurfaceFlinger::onTransact(
1567 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1568{
1569 switch (code) {
1570 case CREATE_CONNECTION:
1571 case OPEN_GLOBAL_TRANSACTION:
1572 case CLOSE_GLOBAL_TRANSACTION:
1573 case SET_ORIENTATION:
1574 case FREEZE_DISPLAY:
1575 case UNFREEZE_DISPLAY:
1576 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 {
1578 // codes that require permission check
1579 IPCThreadState* ipc = IPCThreadState::self();
1580 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001581 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001582 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1583 LOGE("Permission Denial: "
1584 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1585 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 }
1587 }
1588 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1590 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001591 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001592 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1593 IPCThreadState* ipc = IPCThreadState::self();
1594 const int pid = ipc->getCallingPid();
1595 const int uid = ipc->getCallingUid();
1596 LOGE("Permission Denial: "
1597 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 return PERMISSION_DENIED;
1599 }
1600 int n;
1601 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001602 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001604 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 return NO_ERROR;
1606 case 1002: // SHOW_UPDATES
1607 n = data.readInt32();
1608 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1609 return NO_ERROR;
1610 case 1003: // SHOW_BACKGROUND
1611 n = data.readInt32();
1612 mDebugBackground = n ? 1 : 0;
1613 return NO_ERROR;
1614 case 1004:{ // repaint everything
1615 Mutex::Autolock _l(mStateLock);
1616 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1617 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1618 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001619 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001621 case 1005:{ // force transaction
1622 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1623 return NO_ERROR;
1624 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 case 1007: // set mFreezeCount
1626 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001627 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 return NO_ERROR;
1629 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001630 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631 reply->writeInt32(0);
1632 reply->writeInt32(mDebugRegion);
1633 reply->writeInt32(mDebugBackground);
1634 return NO_ERROR;
1635 case 1013: {
1636 Mutex::Autolock _l(mStateLock);
1637 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1638 reply->writeInt32(hw.getPageFlipCount());
1639 }
1640 return NO_ERROR;
1641 }
1642 }
1643 return err;
1644}
1645
1646// ---------------------------------------------------------------------------
1647#if 0
1648#pragma mark -
1649#endif
1650
1651Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1652 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1653{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001655 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001656
1657 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1658 "SurfaceFlinger Client control-block");
1659
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001660 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001661 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001662 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 }
1664}
1665
1666Client::~Client() {
1667 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001668 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 }
1670}
1671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672int32_t Client::generateId(int pid)
1673{
1674 const uint32_t i = clz( ~mBitmap );
1675 if (i >= NUM_LAYERS_MAX) {
1676 return NO_MEMORY;
1677 }
1678 mPid = pid;
1679 mInUse.add(uint8_t(i));
1680 mBitmap |= 1<<(31-i);
1681 return i;
1682}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001683
1684status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685{
1686 ssize_t idx = mInUse.indexOf(id);
1687 if (idx < 0)
1688 return NAME_NOT_FOUND;
1689 return mLayers.insertAt(layer, idx);
1690}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692void Client::free(int32_t id)
1693{
1694 ssize_t idx = mInUse.remove(uint8_t(id));
1695 if (idx >= 0) {
1696 mBitmap &= ~(1<<(31-id));
1697 mLayers.removeItemsAt(idx);
1698 }
1699}
1700
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701bool Client::isValid(int32_t i) const {
1702 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1703}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001704
1705sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1706 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001708 if (idx >= 0) {
1709 lbc = mLayers[idx].promote();
1710 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1711 }
1712 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713}
1714
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715// ---------------------------------------------------------------------------
1716#if 0
1717#pragma mark -
1718#endif
1719
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001720BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721 : mId(cid), mFlinger(flinger), mCblk(cblk)
1722{
1723}
1724
1725BClient::~BClient() {
1726 // destroy all resources attached to this client
1727 mFlinger->destroyConnection(mId);
1728}
1729
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001730sp<IMemoryHeap> BClient::getControlBlock() const {
1731 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732}
1733
1734sp<ISurface> BClient::createSurface(
1735 ISurfaceFlingerClient::surface_data_t* params, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001736 const String8& name,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1738 uint32_t flags)
1739{
Mathias Agopian285dbde2010-03-01 16:09:43 -08001740 return mFlinger->createSurface(mId, pid, name, params, display, w, h,
1741 format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742}
1743
1744status_t BClient::destroySurface(SurfaceID sid)
1745{
1746 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001747 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748}
1749
1750status_t BClient::setState(int32_t count, const layer_state_t* states)
1751{
1752 return mFlinger->setClientState(mId, count, states);
1753}
1754
1755// ---------------------------------------------------------------------------
1756
1757GraphicPlane::GraphicPlane()
1758 : mHw(0)
1759{
1760}
1761
1762GraphicPlane::~GraphicPlane() {
1763 delete mHw;
1764}
1765
1766bool GraphicPlane::initialized() const {
1767 return mHw ? true : false;
1768}
1769
Mathias Agopian2b92d892010-02-08 15:49:35 -08001770int GraphicPlane::getWidth() const {
1771 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772}
1773
Mathias Agopian2b92d892010-02-08 15:49:35 -08001774int GraphicPlane::getHeight() const {
1775 return mHeight;
1776}
1777
1778void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1779{
1780 mHw = hw;
1781
1782 // initialize the display orientation transform.
1783 // it's a constant that should come from the display driver.
1784 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1785 char property[PROPERTY_VALUE_MAX];
1786 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1787 //displayOrientation
1788 switch (atoi(property)) {
1789 case 90:
1790 displayOrientation = ISurfaceComposer::eOrientation90;
1791 break;
1792 case 270:
1793 displayOrientation = ISurfaceComposer::eOrientation270;
1794 break;
1795 }
1796 }
1797
1798 const float w = hw->getWidth();
1799 const float h = hw->getHeight();
1800 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1801 &mDisplayTransform);
1802 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1803 mDisplayWidth = h;
1804 mDisplayHeight = w;
1805 } else {
1806 mDisplayWidth = w;
1807 mDisplayHeight = h;
1808 }
1809
1810 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811}
1812
1813status_t GraphicPlane::orientationToTransfrom(
1814 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001815{
1816 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001817 switch (orientation) {
1818 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001819 flags = Transform::ROT_0;
1820 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001822 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001823 break;
1824 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001825 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826 break;
1827 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001828 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001829 break;
1830 default:
1831 return BAD_VALUE;
1832 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001833 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834 return NO_ERROR;
1835}
1836
1837status_t GraphicPlane::setOrientation(int orientation)
1838{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001839 // If the rotation can be handled in hardware, this is where
1840 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001841
1842 const DisplayHardware& hw(displayHardware());
1843 const float w = mDisplayWidth;
1844 const float h = mDisplayHeight;
1845 mWidth = int(w);
1846 mHeight = int(h);
1847
1848 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001849 GraphicPlane::orientationToTransfrom(orientation, w, h,
1850 &orientationTransform);
1851 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1852 mWidth = int(h);
1853 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001855
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001856 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001857 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858 return NO_ERROR;
1859}
1860
1861const DisplayHardware& GraphicPlane::displayHardware() const {
1862 return *mHw;
1863}
1864
1865const Transform& GraphicPlane::transform() const {
1866 return mGlobalTransform;
1867}
1868
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001869EGLDisplay GraphicPlane::getEGLDisplay() const {
1870 return mHw->getEGLDisplay();
1871}
1872
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873// ---------------------------------------------------------------------------
1874
1875}; // namespace android