blob: fff08539275b98d6d82f0b02fef93a1d61c17d84 [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
Mathias Agopian7e27f052010-05-28 14:22:23 -0700228sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229{
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 Agopianbb641242010-05-18 17:06:55 -0700429 sp<MessageBase> 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
Mathias Agopianbb641242010-05-18 17:06:55 -0700464status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
465 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466{
Mathias Agopianbb641242010-05-18 17:06:55 -0700467 return mEventQueue.postMessage(msg, reltime, flags);
468}
469
470status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
471 nsecs_t reltime, uint32_t flags)
472{
473 status_t res = mEventQueue.postMessage(msg, reltime, flags);
474 if (res == NO_ERROR) {
475 msg->wait();
476 }
477 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478}
479
480// ----------------------------------------------------------------------------
481#if 0
482#pragma mark -
483#pragma mark Main loop
484#endif
485
486bool SurfaceFlinger::threadLoop()
487{
488 waitForEvent();
489
490 // check for transactions
491 if (UNLIKELY(mConsoleSignals)) {
492 handleConsoleEvents();
493 }
494
495 if (LIKELY(mTransactionCount == 0)) {
496 // if we're in a global transaction, don't do anything.
497 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
498 uint32_t transactionFlags = getTransactionFlags(mask);
499 if (LIKELY(transactionFlags)) {
500 handleTransaction(transactionFlags);
501 }
502 }
503
504 // post surfaces (if needed)
505 handlePageFlip();
506
507 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700508 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 // repaint the framebuffer (if needed)
510 handleRepaint();
511
Mathias Agopian74faca22009-09-17 16:18:16 -0700512 // inform the h/w that we're done compositing
513 hw.compositionComplete();
514
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 // release the clients before we flip ('cause flip might block)
516 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 postFramebuffer();
519 } else {
520 // pretend we did the post
521 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 usleep(16667); // 60 fps period
523 }
524 return true;
525}
526
527void SurfaceFlinger::postFramebuffer()
528{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 if (!mInvalidRegion.isEmpty()) {
530 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700531 const nsecs_t now = systemTime();
532 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700534 mLastSwapBufferTime = systemTime() - now;
535 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 }
538}
539
540void SurfaceFlinger::handleConsoleEvents()
541{
542 // something to do with the console
543 const DisplayHardware& hw = graphicPlane(0).displayHardware();
544
545 int what = android_atomic_and(0, &mConsoleSignals);
546 if (what & eConsoleAcquired) {
547 hw.acquireScreen();
548 }
549
550 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700551 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553 hw.releaseScreen();
554 }
555
556 if (what & eConsoleReleased) {
557 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 hw.releaseScreen();
559 } else {
560 mDeferReleaseConsole = true;
561 }
562 }
563
564 mDirtyRegion.set(hw.bounds());
565}
566
567void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
568{
Mathias Agopian3d579642009-06-04 18:46:21 -0700569 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570
Mathias Agopian3d579642009-06-04 18:46:21 -0700571 { // scope for the lock
572 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700573 const nsecs_t now = systemTime();
574 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700575 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700576 mLastTransactionTime = systemTime() - now;
577 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700578 }
579
580 // do this without lock held
581 const size_t count = ditchedLayers.size();
582 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700583 if (ditchedLayers[i] != 0) {
584 //LOGD("ditching layer %p", ditchedLayers[i].get());
585 ditchedLayers[i]->ditch();
586 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700587 }
588}
589
590void SurfaceFlinger::handleTransactionLocked(
591 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
592{
593 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 const size_t count = currentLayers.size();
595
596 /*
597 * Traversal of the children
598 * (perform the transaction for each of them if needed)
599 */
600
601 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
602 if (layersNeedTransaction) {
603 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
606 if (!trFlags) continue;
607
608 const uint32_t flags = layer->doTransaction(0);
609 if (flags & Layer::eVisibleRegion)
610 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 }
612 }
613
614 /*
615 * Perform our own transaction if needed
616 */
617
618 if (transactionFlags & eTransactionNeeded) {
619 if (mCurrentState.orientation != mDrawingState.orientation) {
620 // the orientation has changed, recompute all visible regions
621 // and invalidate everything.
622
623 const int dpy = 0;
624 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700625 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 GraphicPlane& plane(graphicPlane(dpy));
627 plane.setOrientation(orientation);
628
629 // update the shared control block
630 const DisplayHardware& hw(plane.displayHardware());
631 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
632 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800633 dcblk->w = plane.getWidth();
634 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635
636 mVisibleRegionsDirty = true;
637 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 }
639
640 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
641 // freezing or unfreezing the display -> trigger animation if needed
642 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800643 if (mFreezeDisplay)
644 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 }
646
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700647 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
648 // layers have been added
649 mVisibleRegionsDirty = true;
650 }
651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 // some layers might have been removed, so
653 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700654 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700655 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700657 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700658 const size_t count = previousLayers.size();
659 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700660 const sp<LayerBase>& layer(previousLayers[i]);
661 if (currentLayers.indexOf( layer ) < 0) {
662 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700663 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700664 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700665 }
666 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 }
668
669 // get rid of all resources we don't need anymore
670 // (layers and clients)
671 free_resources_l();
672 }
673
674 commitTransaction();
675}
676
677sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
678{
679 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
680}
681
682void SurfaceFlinger::computeVisibleRegions(
683 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
684{
685 const GraphicPlane& plane(graphicPlane(0));
686 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700687 const DisplayHardware& hw(plane.displayHardware());
688 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689
690 Region aboveOpaqueLayers;
691 Region aboveCoveredLayers;
692 Region dirty;
693
694 bool secureFrameBuffer = false;
695
696 size_t i = currentLayers.size();
697 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700698 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 layer->validateVisibility(planeTransform);
700
701 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700702 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703
Mathias Agopianab028732010-03-16 16:41:46 -0700704 /*
705 * opaqueRegion: area of a surface that is fully opaque.
706 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700708
709 /*
710 * visibleRegion: area of a surface that is visible on screen
711 * and not fully transparent. This is essentially the layer's
712 * footprint minus the opaque regions above it.
713 * Areas covered by a translucent surface are considered visible.
714 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700716
717 /*
718 * coveredRegion: area of a surface that is covered by all
719 * visible regions above it (which includes the translucent areas).
720 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700722
723
724 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700725 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700727 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700729 visibleRegion.andSelf(screenRegion);
730 if (!visibleRegion.isEmpty()) {
731 // Remove the transparent area from the visible region
732 if (translucent) {
733 visibleRegion.subtractSelf(layer->transparentRegionScreen);
734 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735
Mathias Agopianab028732010-03-16 16:41:46 -0700736 // compute the opaque region
737 const int32_t layerOrientation = layer->getOrientation();
738 if (s.alpha==255 && !translucent &&
739 ((layerOrientation & Transform::ROT_INVALID) == false)) {
740 // the opaque region is the layer's footprint
741 opaqueRegion = visibleRegion;
742 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 }
744 }
745
Mathias Agopianab028732010-03-16 16:41:46 -0700746 // Clip the covered region to the visible region
747 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
748
749 // Update aboveCoveredLayers for next (lower) layer
750 aboveCoveredLayers.orSelf(visibleRegion);
751
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 // subtract the opaque region covered by the layers above us
753 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754
755 // compute this layer's dirty region
756 if (layer->contentDirty) {
757 // we need to invalidate the whole region
758 dirty = visibleRegion;
759 // as well, as the old visible region
760 dirty.orSelf(layer->visibleRegionScreen);
761 layer->contentDirty = false;
762 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700763 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700764 * the exposed region consists of two components:
765 * 1) what's VISIBLE now and was COVERED before
766 * 2) what's EXPOSED now less what was EXPOSED before
767 *
768 * note that (1) is conservative, we start with the whole
769 * visible region but only keep what used to be covered by
770 * something -- which mean it may have been exposed.
771 *
772 * (2) handles areas that were not covered by anything but got
773 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700774 */
Mathias Agopianab028732010-03-16 16:41:46 -0700775 const Region newExposed = visibleRegion - coveredRegion;
776 const Region oldVisibleRegion = layer->visibleRegionScreen;
777 const Region oldCoveredRegion = layer->coveredRegionScreen;
778 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
779 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 }
781 dirty.subtractSelf(aboveOpaqueLayers);
782
783 // accumulate to the screen dirty region
784 dirtyRegion.orSelf(dirty);
785
Mathias Agopianab028732010-03-16 16:41:46 -0700786 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788
789 // Store the visible region is screen space
790 layer->setVisibleRegion(visibleRegion);
791 layer->setCoveredRegion(coveredRegion);
792
Mathias Agopian97011222009-07-28 10:57:27 -0700793 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 if (layer->isSecure() && !visibleRegion.isEmpty()) {
795 secureFrameBuffer = true;
796 }
797 }
798
Mathias Agopian97011222009-07-28 10:57:27 -0700799 // invalidate the areas where a layer was removed
800 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
801 mDirtyRegionRemovedLayer.clear();
802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 mSecureFrameBuffer = secureFrameBuffer;
804 opaqueRegion = aboveOpaqueLayers;
805}
806
807
808void SurfaceFlinger::commitTransaction()
809{
810 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700811 mResizeTransationPending = false;
812 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813}
814
815void SurfaceFlinger::handlePageFlip()
816{
817 bool visibleRegions = mVisibleRegionsDirty;
818 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
819 visibleRegions |= lockPageFlip(currentLayers);
820
821 const DisplayHardware& hw = graphicPlane(0).displayHardware();
822 const Region screenRegion(hw.bounds());
823 if (visibleRegions) {
824 Region opaqueRegion;
825 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
826 mWormholeRegion = screenRegion.subtract(opaqueRegion);
827 mVisibleRegionsDirty = false;
828 }
829
830 unlockPageFlip(currentLayers);
831 mDirtyRegion.andSelf(screenRegion);
832}
833
834bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
835{
836 bool recomputeVisibleRegions = false;
837 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700838 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700840 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 layer->lockPageFlip(recomputeVisibleRegions);
842 }
843 return recomputeVisibleRegions;
844}
845
846void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
847{
848 const GraphicPlane& plane(graphicPlane(0));
849 const Transform& planeTransform(plane.transform());
850 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700851 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700853 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 layer->unlockPageFlip(planeTransform, mDirtyRegion);
855 }
856}
857
Mathias Agopianb8a55602009-06-26 19:06:36 -0700858
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859void SurfaceFlinger::handleRepaint()
860{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700861 // compute the invalid region
862 mInvalidRegion.orSelf(mDirtyRegion);
863 if (mInvalidRegion.isEmpty()) {
864 // nothing to do
865 return;
866 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867
868 if (UNLIKELY(mDebugRegion)) {
869 debugFlashRegions();
870 }
871
Mathias Agopianb8a55602009-06-26 19:06:36 -0700872 // set the frame buffer
873 const DisplayHardware& hw(graphicPlane(0).displayHardware());
874 glMatrixMode(GL_MODELVIEW);
875 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876
877 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700878 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
879 (flags & DisplayHardware::BUFFER_PRESERVED))
880 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700881 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
882 // takes a rectangle, we must make sure to update that whole
883 // rectangle in that case
884 if (flags & DisplayHardware::SWAP_RECTANGLE) {
885 // FIXME: we really should be able to pass a region to
886 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 mDirtyRegion.set(mInvalidRegion.bounds());
888 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700889 // in the BUFFER_PRESERVED case, obviously, we can update only
890 // what's needed and nothing more.
891 // NOTE: this is NOT a common case, as preserving the backbuffer
892 // is costly and usually involves copying the whole update back.
893 }
894 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700895 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700896 // We need to redraw the rectangle that will be updated
897 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700898 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700899 // rectangle instead of a region (see DisplayHardware::flip())
900 mDirtyRegion.set(mInvalidRegion.bounds());
901 } else {
902 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 mDirtyRegion.set(hw.bounds());
904 mInvalidRegion = mDirtyRegion;
905 }
906 }
907
908 // compose all surfaces
909 composeSurfaces(mDirtyRegion);
910
911 // clear the dirty regions
912 mDirtyRegion.clear();
913}
914
915void SurfaceFlinger::composeSurfaces(const Region& dirty)
916{
917 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
918 // should never happen unless the window manager has a bug
919 // draw something...
920 drawWormhole();
921 }
922 const SurfaceFlinger& flinger(*this);
923 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
924 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700925 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700927 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 const Region& visibleRegion(layer->visibleRegionScreen);
929 if (!visibleRegion.isEmpty()) {
930 const Region clip(dirty.intersect(visibleRegion));
931 if (!clip.isEmpty()) {
932 layer->draw(clip);
933 }
934 }
935 }
936}
937
938void SurfaceFlinger::unlockClients()
939{
940 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
941 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700942 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700944 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945 layer->finishPageFlip();
946 }
947}
948
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949void SurfaceFlinger::debugFlashRegions()
950{
Mathias Agopian0926f502009-05-04 14:17:04 -0700951 const DisplayHardware& hw(graphicPlane(0).displayHardware());
952 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700953
954 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
955 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700956 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopian0926f502009-05-04 14:17:04 -0700957 mDirtyRegion.bounds() : hw.bounds());
958 composeSurfaces(repaint);
959 }
960
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961 glDisable(GL_TEXTURE_2D);
962 glDisable(GL_BLEND);
963 glDisable(GL_DITHER);
964 glDisable(GL_SCISSOR_TEST);
965
Mathias Agopian0926f502009-05-04 14:17:04 -0700966 static int toggle = 0;
967 toggle = 1 - toggle;
968 if (toggle) {
969 glColor4x(0x10000, 0, 0x10000, 0x10000);
970 } else {
971 glColor4x(0x10000, 0x10000, 0, 0x10000);
972 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973
Mathias Agopian20f68782009-05-11 00:03:41 -0700974 Region::const_iterator it = mDirtyRegion.begin();
975 Region::const_iterator const end = mDirtyRegion.end();
976 while (it != end) {
977 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 GLfloat vertices[][2] = {
979 { r.left, r.top },
980 { r.left, r.bottom },
981 { r.right, r.bottom },
982 { r.right, r.top }
983 };
984 glVertexPointer(2, GL_FLOAT, 0, vertices);
985 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
986 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700987
Mathias Agopianb8a55602009-06-26 19:06:36 -0700988 if (mInvalidRegion.isEmpty()) {
989 mDirtyRegion.dump("mDirtyRegion");
990 mInvalidRegion.dump("mInvalidRegion");
991 }
992 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993
994 if (mDebugRegion > 1)
995 usleep(mDebugRegion * 1000);
996
997 glEnable(GL_SCISSOR_TEST);
998 //mDirtyRegion.dump("mDirtyRegion");
999}
1000
1001void SurfaceFlinger::drawWormhole() const
1002{
1003 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1004 if (region.isEmpty())
1005 return;
1006
1007 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1008 const int32_t width = hw.getWidth();
1009 const int32_t height = hw.getHeight();
1010
1011 glDisable(GL_BLEND);
1012 glDisable(GL_DITHER);
1013
1014 if (LIKELY(!mDebugBackground)) {
1015 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001016 Region::const_iterator it = region.begin();
1017 Region::const_iterator const end = region.end();
1018 while (it != end) {
1019 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 const GLint sy = height - (r.top + r.height());
1021 glScissor(r.left, sy, r.width(), r.height());
1022 glClear(GL_COLOR_BUFFER_BIT);
1023 }
1024 } else {
1025 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1026 { width, height }, { 0, height } };
1027 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1028 glVertexPointer(2, GL_SHORT, 0, vertices);
1029 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1030 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1031 glEnable(GL_TEXTURE_2D);
1032 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1033 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1034 glMatrixMode(GL_TEXTURE);
1035 glLoadIdentity();
1036 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001037 Region::const_iterator it = region.begin();
1038 Region::const_iterator const end = region.end();
1039 while (it != end) {
1040 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 const GLint sy = height - (r.top + r.height());
1042 glScissor(r.left, sy, r.width(), r.height());
1043 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1044 }
1045 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1046 }
1047}
1048
1049void SurfaceFlinger::debugShowFPS() const
1050{
1051 static int mFrameCount;
1052 static int mLastFrameCount = 0;
1053 static nsecs_t mLastFpsTime = 0;
1054 static float mFps = 0;
1055 mFrameCount++;
1056 nsecs_t now = systemTime();
1057 nsecs_t diff = now - mLastFpsTime;
1058 if (diff > ms2ns(250)) {
1059 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1060 mLastFpsTime = now;
1061 mLastFrameCount = mFrameCount;
1062 }
1063 // XXX: mFPS has the value we want
1064 }
1065
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067{
1068 Mutex::Autolock _l(mStateLock);
1069 addLayer_l(layer);
1070 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1071 return NO_ERROR;
1072}
1073
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075{
1076 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001077 status_t err = purgatorizeLayer_l(layer);
1078 if (err == NO_ERROR)
1079 setTransactionFlags(eTransactionNeeded);
1080 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081}
1082
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001083status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084{
1085 layer->forceVisibilityTransaction();
1086 setTransactionFlags(eTraversalNeeded);
1087 return NO_ERROR;
1088}
1089
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001090status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091{
1092 ssize_t i = mCurrentState.layersSortedByZ.add(
1093 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001094 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1095}
1096
1097status_t SurfaceFlinger::addClientLayer_l(const sp<LayerBaseClient>& lbc)
1098{
1099 ssize_t serverIndex = lbc->serverIndex();
1100 return mLayerMap.add(serverIndex, lbc);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101}
1102
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001103status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104{
1105 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1106 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001107 mLayersRemoved = true;
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001108 ssize_t serverIndex = layerBase->serverIndex();
1109 if (serverIndex >= 0) {
1110 mLayerMap.removeItem(serverIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 }
1112 return NO_ERROR;
1113 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001114 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115}
1116
Mathias Agopian9a112062009-04-17 19:36:26 -07001117status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1118{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001119 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001120 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001121
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001122 layerBase->onRemoved();
1123
Mathias Agopian3d579642009-06-04 18:46:21 -07001124 // it's possible that we don't find a layer, because it might
1125 // have been destroyed already -- this is not technically an error
1126 // from the user because there is a race between BClient::destroySurface(),
1127 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001128 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1129}
1130
1131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132void SurfaceFlinger::free_resources_l()
1133{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001135 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 const size_t count = disconnectedClients.size();
1137 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001138 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 }
1141 disconnectedClients.clear();
1142}
1143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1145{
1146 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1147}
1148
Mathias Agopianbb641242010-05-18 17:06:55 -07001149uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150{
1151 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1152 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001153 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154 }
1155 return old;
1156}
1157
1158void SurfaceFlinger::openGlobalTransaction()
1159{
1160 android_atomic_inc(&mTransactionCount);
1161}
1162
1163void SurfaceFlinger::closeGlobalTransaction()
1164{
1165 if (android_atomic_dec(&mTransactionCount) == 1) {
1166 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001167
1168 // if there is a transaction with a resize, wait for it to
1169 // take effect before returning.
1170 Mutex::Autolock _l(mStateLock);
1171 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001172 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1173 if (CC_UNLIKELY(err != NO_ERROR)) {
1174 // just in case something goes wrong in SF, return to the
1175 // called after a few seconds.
1176 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1177 mResizeTransationPending = false;
1178 break;
1179 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001180 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 }
1182}
1183
1184status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1185{
1186 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1187 return BAD_VALUE;
1188
1189 Mutex::Autolock _l(mStateLock);
1190 mCurrentState.freezeDisplay = 1;
1191 setTransactionFlags(eTransactionNeeded);
1192
1193 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001194 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 return NO_ERROR;
1196}
1197
1198status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1199{
1200 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1201 return BAD_VALUE;
1202
1203 Mutex::Autolock _l(mStateLock);
1204 mCurrentState.freezeDisplay = 0;
1205 setTransactionFlags(eTransactionNeeded);
1206
1207 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001208 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 return NO_ERROR;
1210}
1211
Mathias Agopianc08731e2009-03-27 18:11:38 -07001212int SurfaceFlinger::setOrientation(DisplayID dpy,
1213 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214{
1215 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1216 return BAD_VALUE;
1217
1218 Mutex::Autolock _l(mStateLock);
1219 if (mCurrentState.orientation != orientation) {
1220 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001221 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 mCurrentState.orientation = orientation;
1223 setTransactionFlags(eTransactionNeeded);
1224 mTransactionCV.wait(mStateLock);
1225 } else {
1226 orientation = BAD_VALUE;
1227 }
1228 }
1229 return orientation;
1230}
1231
1232sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001233 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1235 uint32_t flags)
1236{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001237 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001239
1240 if (int32_t(w|h) < 0) {
1241 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1242 int(w), int(h));
1243 return surfaceHandle;
1244 }
1245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001247 sp<Client> client = mClientsMap.valueFor(clientId);
1248 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1250 return surfaceHandle;
1251 }
1252
1253 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001254 int32_t id = client->generateId(pid);
Mathias Agopianbb641242010-05-18 17:06:55 -07001255 if (uint32_t(id) >= SharedBufferStack::NUM_LAYERS_MAX) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 LOGE("createSurface() failed, generateId = %d", id);
1257 return surfaceHandle;
1258 }
1259
1260 switch (flags & eFXSurfaceMask) {
1261 case eFXSurfaceNormal:
1262 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001263 layer = createPushBuffersSurfaceLocked(client, d, id,
1264 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001266 layer = createNormalSurfaceLocked(client, d, id,
1267 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 }
1269 break;
1270 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001271 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 break;
1273 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001274 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 break;
1276 }
1277
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001278 if (layer != 0) {
Mathias Agopian285dbde2010-03-01 16:09:43 -08001279 layer->setName(name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 setTransactionFlags(eTransactionNeeded);
1281 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001282 if (surfaceHandle != 0) {
1283 params->token = surfaceHandle->getToken();
1284 params->identity = surfaceHandle->getIdentity();
1285 params->width = w;
1286 params->height = h;
1287 params->format = format;
1288 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 }
1290
1291 return surfaceHandle;
1292}
1293
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001294sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001295 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001296 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1297 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298{
1299 // initialize the surfaces
1300 switch (format) { // TODO: take h/w into account
1301 case PIXEL_FORMAT_TRANSPARENT:
1302 case PIXEL_FORMAT_TRANSLUCENT:
1303 format = PIXEL_FORMAT_RGBA_8888;
1304 break;
1305 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001306 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 break;
1308 }
1309
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001310 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001311 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 if (LIKELY(err == NO_ERROR)) {
1313 layer->initStates(w, h, flags);
1314 addLayer_l(layer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001315 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 } else {
1317 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001318 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 }
1320 return layer;
1321}
1322
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001323sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001324 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1326{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001327 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 layer->initStates(w, h, flags);
1329 addLayer_l(layer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001330 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 return layer;
1332}
1333
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001334sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001335 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1337{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001338 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 layer->initStates(w, h, flags);
1340 addLayer_l(layer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001341 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 return layer;
1343}
1344
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001345sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001346 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1348{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001349 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 layer->initStates(w, h, flags);
1351 addLayer_l(layer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001352 addClientLayer_l(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 return layer;
1354}
1355
Mathias Agopian9a112062009-04-17 19:36:26 -07001356status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357{
Mathias Agopian9a112062009-04-17 19:36:26 -07001358 /*
1359 * called by the window manager, when a surface should be marked for
1360 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001361 *
1362 * The surface is removed from the current and drawing lists, but placed
1363 * in the purgatory queue, so it's not destroyed right-away (we need
1364 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001365 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001366
Mathias Agopian48d819a2009-09-10 19:41:18 -07001367 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001368 Mutex::Autolock _l(mStateLock);
1369 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001370 if (layer != 0) {
1371 err = purgatorizeLayer_l(layer);
1372 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001373 setTransactionFlags(eTransactionNeeded);
1374 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001375 }
1376 return err;
1377}
1378
1379status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1380{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001381 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001382
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001383 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001384 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001385 sp<LayerBaseClient> layer;
1386 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001387 MessageDestroySurface(
1388 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1389 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001390 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001391 sp<LayerBaseClient> l(layer);
1392 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001393 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001394 /*
1395 * remove the layer from the current list -- chances are that it's
1396 * not in the list anyway, because it should have been removed
1397 * already upon request of the client (eg: window manager).
1398 * However, a buggy client could have not done that.
1399 * Since we know we don't have any more clients, we don't need
1400 * to use the purgatory.
1401 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001402 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001403 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1404 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001405 return true;
1406 }
1407 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001408
Mathias Agopianbb641242010-05-18 17:06:55 -07001409 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410 return NO_ERROR;
1411}
1412
1413status_t SurfaceFlinger::setClientState(
1414 ClientID cid,
1415 int32_t count,
1416 const layer_state_t* states)
1417{
1418 Mutex::Autolock _l(mStateLock);
1419 uint32_t flags = 0;
1420 cid <<= 16;
1421 for (int i=0 ; i<count ; i++) {
1422 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001423 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001424 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 if (what & ePositionChanged) {
1427 if (layer->setPosition(s.x, s.y))
1428 flags |= eTraversalNeeded;
1429 }
1430 if (what & eLayerChanged) {
1431 if (layer->setLayer(s.z)) {
1432 mCurrentState.layersSortedByZ.reorder(
1433 layer, &Layer::compareCurrentStateZ);
1434 // we need traversal (state changed)
1435 // AND transaction (list changed)
1436 flags |= eTransactionNeeded|eTraversalNeeded;
1437 }
1438 }
1439 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001440 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001442 mResizeTransationPending = true;
1443 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 }
1445 if (what & eAlphaChanged) {
1446 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1447 flags |= eTraversalNeeded;
1448 }
1449 if (what & eMatrixChanged) {
1450 if (layer->setMatrix(s.matrix))
1451 flags |= eTraversalNeeded;
1452 }
1453 if (what & eTransparentRegionChanged) {
1454 if (layer->setTransparentRegionHint(s.transparentRegion))
1455 flags |= eTraversalNeeded;
1456 }
1457 if (what & eVisibilityChanged) {
1458 if (layer->setFlags(s.flags, s.mask))
1459 flags |= eTraversalNeeded;
1460 }
1461 }
1462 }
1463 if (flags) {
1464 setTransactionFlags(flags);
1465 }
1466 return NO_ERROR;
1467}
1468
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001469sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001471 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1472 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473}
1474
1475void SurfaceFlinger::screenReleased(int dpy)
1476{
1477 // this may be called by a signal handler, we can't do too much in here
1478 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1479 signalEvent();
1480}
1481
1482void SurfaceFlinger::screenAcquired(int dpy)
1483{
1484 // this may be called by a signal handler, we can't do too much in here
1485 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1486 signalEvent();
1487}
1488
1489status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1490{
1491 const size_t SIZE = 1024;
1492 char buffer[SIZE];
1493 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001494 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 snprintf(buffer, SIZE, "Permission Denial: "
1496 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1497 IPCThreadState::self()->getCallingPid(),
1498 IPCThreadState::self()->getCallingUid());
1499 result.append(buffer);
1500 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001501
1502 // figure out if we're stuck somewhere
1503 const nsecs_t now = systemTime();
1504 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1505 const nsecs_t inTransaction(mDebugInTransaction);
1506 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1507 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1508
1509 // Try to get the main lock, but don't insist if we can't
1510 // (this would indicate SF is stuck, but we want to be able to
1511 // print something in dumpsys).
1512 int retry = 3;
1513 while (mStateLock.tryLock()<0 && --retry>=0) {
1514 usleep(1000000);
1515 }
1516 const bool locked(retry >= 0);
1517 if (!locked) {
1518 snprintf(buffer, SIZE,
1519 "SurfaceFlinger appears to be unresponsive, "
1520 "dumping anyways (no locks held)\n");
1521 result.append(buffer);
1522 }
1523
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1525 const size_t count = currentLayers.size();
1526 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001527 const sp<LayerBase>& layer(currentLayers[i]);
1528 layer->dump(result, buffer, SIZE);
1529 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530 s.transparentRegion.dump(result, "transparentRegion");
1531 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1532 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1533 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 mWormholeRegion.dump(result, "WormholeRegion");
1536 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1537 snprintf(buffer, SIZE,
1538 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1539 mFreezeDisplay?"yes":"no", mFreezeCount,
1540 mCurrentState.orientation, hw.canDraw());
1541 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001542 snprintf(buffer, SIZE,
1543 " last eglSwapBuffers() time: %f us\n"
1544 " last transaction time : %f us\n",
1545 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1546 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001547
Mathias Agopian9795c422009-08-26 16:36:26 -07001548 if (inSwapBuffersDuration || !locked) {
1549 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1550 inSwapBuffersDuration/1000.0);
1551 result.append(buffer);
1552 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001553
Mathias Agopian9795c422009-08-26 16:36:26 -07001554 if (inTransactionDuration || !locked) {
1555 snprintf(buffer, SIZE, " transaction time: %f us\n",
1556 inTransactionDuration/1000.0);
1557 result.append(buffer);
1558 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001559
Mathias Agopian759fdb22009-07-02 17:33:40 -07001560 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001561 result.append(buffer);
Mathias Agopian3330b202009-10-05 17:07:12 -07001562 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001563 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001564
1565 if (locked) {
1566 mStateLock.unlock();
1567 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 }
1569 write(fd, result.string(), result.size());
1570 return NO_ERROR;
1571}
1572
1573status_t SurfaceFlinger::onTransact(
1574 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1575{
1576 switch (code) {
1577 case CREATE_CONNECTION:
1578 case OPEN_GLOBAL_TRANSACTION:
1579 case CLOSE_GLOBAL_TRANSACTION:
1580 case SET_ORIENTATION:
1581 case FREEZE_DISPLAY:
1582 case UNFREEZE_DISPLAY:
1583 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584 {
1585 // codes that require permission check
1586 IPCThreadState* ipc = IPCThreadState::self();
1587 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001588 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001589 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1590 LOGE("Permission Denial: "
1591 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1592 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593 }
1594 }
1595 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1597 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001598 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001599 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1600 IPCThreadState* ipc = IPCThreadState::self();
1601 const int pid = ipc->getCallingPid();
1602 const int uid = ipc->getCallingUid();
1603 LOGE("Permission Denial: "
1604 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 return PERMISSION_DENIED;
1606 }
1607 int n;
1608 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001609 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001611 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 return NO_ERROR;
1613 case 1002: // SHOW_UPDATES
1614 n = data.readInt32();
1615 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1616 return NO_ERROR;
1617 case 1003: // SHOW_BACKGROUND
1618 n = data.readInt32();
1619 mDebugBackground = n ? 1 : 0;
1620 return NO_ERROR;
1621 case 1004:{ // repaint everything
1622 Mutex::Autolock _l(mStateLock);
1623 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1624 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1625 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001626 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001628 case 1005:{ // force transaction
1629 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1630 return NO_ERROR;
1631 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 case 1007: // set mFreezeCount
1633 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001634 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 return NO_ERROR;
1636 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001637 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 reply->writeInt32(0);
1639 reply->writeInt32(mDebugRegion);
1640 reply->writeInt32(mDebugBackground);
1641 return NO_ERROR;
1642 case 1013: {
1643 Mutex::Autolock _l(mStateLock);
1644 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1645 reply->writeInt32(hw.getPageFlipCount());
1646 }
1647 return NO_ERROR;
1648 }
1649 }
1650 return err;
1651}
1652
1653// ---------------------------------------------------------------------------
1654#if 0
1655#pragma mark -
1656#endif
1657
1658Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1659 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1660{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001662 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001663
1664 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1665 "SurfaceFlinger Client control-block");
1666
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001667 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001668 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001669 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 }
1671}
1672
1673Client::~Client() {
1674 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001675 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 }
1677}
1678
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679int32_t Client::generateId(int pid)
1680{
1681 const uint32_t i = clz( ~mBitmap );
Mathias Agopianbb641242010-05-18 17:06:55 -07001682 if (i >= SharedBufferStack::NUM_LAYERS_MAX) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 return NO_MEMORY;
1684 }
1685 mPid = pid;
1686 mInUse.add(uint8_t(i));
1687 mBitmap |= 1<<(31-i);
1688 return i;
1689}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001690
1691status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692{
1693 ssize_t idx = mInUse.indexOf(id);
1694 if (idx < 0)
1695 return NAME_NOT_FOUND;
1696 return mLayers.insertAt(layer, idx);
1697}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699void Client::free(int32_t id)
1700{
1701 ssize_t idx = mInUse.remove(uint8_t(id));
1702 if (idx >= 0) {
1703 mBitmap &= ~(1<<(31-id));
1704 mLayers.removeItemsAt(idx);
1705 }
1706}
1707
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708bool Client::isValid(int32_t i) const {
Mathias Agopianbb641242010-05-18 17:06:55 -07001709 return (uint32_t(i)<SharedBufferStack::NUM_LAYERS_MAX) &&
1710 (mBitmap & (1<<(31-i)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001712
1713sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1714 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001716 if (idx >= 0) {
1717 lbc = mLayers[idx].promote();
1718 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1719 }
1720 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721}
1722
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723// ---------------------------------------------------------------------------
1724#if 0
1725#pragma mark -
1726#endif
1727
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001728BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 : mId(cid), mFlinger(flinger), mCblk(cblk)
1730{
1731}
1732
1733BClient::~BClient() {
1734 // destroy all resources attached to this client
1735 mFlinger->destroyConnection(mId);
1736}
1737
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001738sp<IMemoryHeap> BClient::getControlBlock() const {
1739 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740}
1741
1742sp<ISurface> BClient::createSurface(
Mathias Agopian7e27f052010-05-28 14:22:23 -07001743 ISurfaceComposerClient::surface_data_t* params, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001744 const String8& name,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1746 uint32_t flags)
1747{
Mathias Agopian285dbde2010-03-01 16:09:43 -08001748 return mFlinger->createSurface(mId, pid, name, params, display, w, h,
1749 format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750}
1751
1752status_t BClient::destroySurface(SurfaceID sid)
1753{
1754 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001755 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756}
1757
1758status_t BClient::setState(int32_t count, const layer_state_t* states)
1759{
1760 return mFlinger->setClientState(mId, count, states);
1761}
1762
1763// ---------------------------------------------------------------------------
1764
1765GraphicPlane::GraphicPlane()
1766 : mHw(0)
1767{
1768}
1769
1770GraphicPlane::~GraphicPlane() {
1771 delete mHw;
1772}
1773
1774bool GraphicPlane::initialized() const {
1775 return mHw ? true : false;
1776}
1777
Mathias Agopian2b92d892010-02-08 15:49:35 -08001778int GraphicPlane::getWidth() const {
1779 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001780}
1781
Mathias Agopian2b92d892010-02-08 15:49:35 -08001782int GraphicPlane::getHeight() const {
1783 return mHeight;
1784}
1785
1786void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1787{
1788 mHw = hw;
1789
1790 // initialize the display orientation transform.
1791 // it's a constant that should come from the display driver.
1792 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1793 char property[PROPERTY_VALUE_MAX];
1794 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1795 //displayOrientation
1796 switch (atoi(property)) {
1797 case 90:
1798 displayOrientation = ISurfaceComposer::eOrientation90;
1799 break;
1800 case 270:
1801 displayOrientation = ISurfaceComposer::eOrientation270;
1802 break;
1803 }
1804 }
1805
1806 const float w = hw->getWidth();
1807 const float h = hw->getHeight();
1808 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1809 &mDisplayTransform);
1810 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1811 mDisplayWidth = h;
1812 mDisplayHeight = w;
1813 } else {
1814 mDisplayWidth = w;
1815 mDisplayHeight = h;
1816 }
1817
1818 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819}
1820
1821status_t GraphicPlane::orientationToTransfrom(
1822 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001823{
1824 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 switch (orientation) {
1826 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001827 flags = Transform::ROT_0;
1828 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001829 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001830 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001831 break;
1832 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001833 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834 break;
1835 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001836 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001837 break;
1838 default:
1839 return BAD_VALUE;
1840 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001841 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842 return NO_ERROR;
1843}
1844
1845status_t GraphicPlane::setOrientation(int orientation)
1846{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001847 // If the rotation can be handled in hardware, this is where
1848 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001849
1850 const DisplayHardware& hw(displayHardware());
1851 const float w = mDisplayWidth;
1852 const float h = mDisplayHeight;
1853 mWidth = int(w);
1854 mHeight = int(h);
1855
1856 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001857 GraphicPlane::orientationToTransfrom(orientation, w, h,
1858 &orientationTransform);
1859 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1860 mWidth = int(h);
1861 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001862 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001863
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001864 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001865 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 return NO_ERROR;
1867}
1868
1869const DisplayHardware& GraphicPlane::displayHardware() const {
1870 return *mHw;
1871}
1872
1873const Transform& GraphicPlane::transform() const {
1874 return mGlobalTransform;
1875}
1876
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001877EGLDisplay GraphicPlane::getEGLDisplay() const {
1878 return mHw->getEGLDisplay();
1879}
1880
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881// ---------------------------------------------------------------------------
1882
1883}; // namespace android