blob: 25984408d35f2ba3986a6334d87a9c542cdec512 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include <pixelflinger/pixelflinger.h>
44#include <GLES/gl.h>
45
46#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include "Layer.h"
48#include "LayerBlur.h"
49#include "LayerBuffer.h"
50#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopiana1ecca92009-05-21 19:21:59 -070055/* ideally AID_GRAPHICS would be in a semi-public header
56 * or there would be a way to map a user/group name to its id
57 */
58#ifndef AID_GRAPHICS
59#define AID_GRAPHICS 1003
60#endif
61
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#define DISPLAY_COUNT 1
63
64namespace android {
65
66// ---------------------------------------------------------------------------
67
68void SurfaceFlinger::instantiate() {
69 defaultServiceManager()->addService(
70 String16("SurfaceFlinger"), new SurfaceFlinger());
71}
72
73void SurfaceFlinger::shutdown() {
74 // we should unregister here, but not really because
75 // when (if) the service manager goes away, all the services
76 // it has a reference to will leave too.
77}
78
79// ---------------------------------------------------------------------------
80
81SurfaceFlinger::LayerVector::LayerVector(const SurfaceFlinger::LayerVector& rhs)
82 : lookup(rhs.lookup), layers(rhs.layers)
83{
84}
85
86ssize_t SurfaceFlinger::LayerVector::indexOf(
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
89 if (guess<size() && lookup.keyAt(guess) == key)
90 return guess;
91 const ssize_t i = lookup.indexOfKey(key);
92 if (i>=0) {
93 const size_t idx = lookup.valueAt(i);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094 LOGE_IF(layers[idx]!=key,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 return idx;
98 }
99 return i;
100}
101
102ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 const sp<LayerBase>& layer,
104 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
106 size_t count = layers.size();
107 ssize_t l = 0;
108 ssize_t h = count-1;
109 ssize_t mid;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110 sp<LayerBase> const* a = layers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 while (l <= h) {
112 mid = l + (h - l)/2;
113 const int c = cmp(a+mid, &layer);
114 if (c == 0) { l = mid; break; }
115 else if (c<0) { l = mid+1; }
116 else { h = mid-1; }
117 }
118 size_t order = l;
119 while (order<count && !cmp(&layer, a+order)) {
120 order++;
121 }
122 count = lookup.size();
123 for (size_t i=0 ; i<count ; i++) {
124 if (lookup.valueAt(i) >= order) {
125 lookup.editValueAt(i)++;
126 }
127 }
128 layers.insertAt(layer, order);
129 lookup.add(layer, order);
130 return order;
131}
132
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
135 const ssize_t keyIndex = lookup.indexOfKey(layer);
136 if (keyIndex >= 0) {
137 const size_t index = lookup.valueAt(keyIndex);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 LOGE_IF(layers[index]!=layer,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 this, int(index), layers[index].get(), layer.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 layers.removeItemsAt(index);
142 lookup.removeItemsAt(keyIndex);
143 const size_t count = lookup.size();
144 for (size_t i=0 ; i<count ; i++) {
145 if (lookup.valueAt(i) >= size_t(index)) {
146 lookup.editValueAt(i)--;
147 }
148 }
149 return index;
150 }
151 return NAME_NOT_FOUND;
152}
153
154ssize_t SurfaceFlinger::LayerVector::reorder(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700155 const sp<LayerBase>& layer,
156 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
158 // XXX: it's a little lame. but oh well...
159 ssize_t err = remove(layer);
160 if (err >=0)
161 err = add(layer, cmp);
162 return err;
163}
164
165// ---------------------------------------------------------------------------
166#if 0
167#pragma mark -
168#endif
169
170SurfaceFlinger::SurfaceFlinger()
171 : BnSurfaceComposer(), Thread(false),
172 mTransactionFlags(0),
173 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700174 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -0700177 mHardwareTest("android.permission.HARDWARE_TEST"),
178 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
179 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 mVisibleRegionsDirty(false),
181 mDeferReleaseConsole(false),
182 mFreezeDisplay(false),
183 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700184 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700187 mDebugInSwapBuffers(0),
188 mLastSwapBufferTime(0),
189 mDebugInTransaction(0),
190 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700191 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 mConsoleSignals(0),
193 mSecureFrameBuffer(0)
194{
195 init();
196}
197
198void SurfaceFlinger::init()
199{
200 LOGI("SurfaceFlinger is starting");
201
202 // debugging stuff...
203 char value[PROPERTY_VALUE_MAX];
204 property_get("debug.sf.showupdates", value, "0");
205 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 property_get("debug.sf.showbackground", value, "0");
207 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
209 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
213SurfaceFlinger::~SurfaceFlinger()
214{
215 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216}
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
219{
220 return graphicPlane(0).displayHardware().getOverlayEngine();
221}
222
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700223sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
229{
230 Mutex::Autolock _l(mStateLock);
231 uint32_t token = mTokens.acquire();
232
Mathias Agopianf9d93272009-06-19 17:00:27 -0700233 sp<Client> client = new Client(token, this);
234 if (client->ctrlblk == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 mTokens.release(token);
236 return 0;
237 }
238 status_t err = mClientsMap.add(token, client);
239 if (err < 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 mTokens.release(token);
241 return 0;
242 }
243 sp<BClient> bclient =
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700244 new BClient(this, token, client->getControlBlockMemory());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 return bclient;
246}
247
248void SurfaceFlinger::destroyConnection(ClientID cid)
249{
250 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -0700251 sp<Client> client = mClientsMap.valueFor(cid);
252 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 // free all the layers this client owns
Mathias Agopian3d579642009-06-04 18:46:21 -0700254 Vector< wp<LayerBaseClient> > layers(client->getLayers());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 const size_t count = layers.size();
256 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 sp<LayerBaseClient> layer(layers[i].promote());
258 if (layer != 0) {
Mathias Agopian3d579642009-06-04 18:46:21 -0700259 purgatorizeLayer_l(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 }
262
263 // the resources associated with this client will be freed
264 // during the next transaction, after these surfaces have been
265 // properly removed from the screen
266
267 // remove this client from our ClientID->Client mapping.
268 mClientsMap.removeItem(cid);
269
270 // and add it to the list of disconnected clients
271 mDisconnectedClients.add(client);
272
273 // request a transaction
274 setTransactionFlags(eTransactionNeeded);
275 }
276}
277
278const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
279{
280 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
281 const GraphicPlane& plane(mGraphicPlanes[dpy]);
282 return plane;
283}
284
285GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
286{
287 return const_cast<GraphicPlane&>(
288 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
289}
290
291void SurfaceFlinger::bootFinished()
292{
293 const nsecs_t now = systemTime();
294 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700295 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700296 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700297 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
300void SurfaceFlinger::onFirstRef()
301{
302 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
303
304 // Wait for the main thread to be done with its initialization
305 mReadyToRunBarrier.wait();
306}
307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308static inline uint16_t pack565(int r, int g, int b) {
309 return (r<<11)|(g<<5)|b;
310}
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312status_t SurfaceFlinger::readyToRun()
313{
314 LOGI( "SurfaceFlinger's main thread ready to run. "
315 "Initializing graphics H/W...");
316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 // we only support one display currently
318 int dpy = 0;
319
320 {
321 // initialize the main display
322 GraphicPlane& plane(graphicPlane(dpy));
323 DisplayHardware* const hw = new DisplayHardware(this, dpy);
324 plane.setDisplayHardware(hw);
325 }
326
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700327 // create the shared control-block
328 mServerHeap = new MemoryHeapBase(4096,
329 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
330 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
331
332 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
333 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
334
335 new(mServerCblk) surface_flinger_cblk_t;
336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 // initialize primary screen
338 // (other display should be initialized in the same manner, but
339 // asynchronously, as they could come and go. None of this is supported
340 // yet).
341 const GraphicPlane& plane(graphicPlane(dpy));
342 const DisplayHardware& hw = plane.displayHardware();
343 const uint32_t w = hw.getWidth();
344 const uint32_t h = hw.getHeight();
345 const uint32_t f = hw.getFormat();
346 hw.makeCurrent();
347
348 // initialize the shared control block
349 mServerCblk->connected |= 1<<dpy;
350 display_cblk_t* dcblk = mServerCblk->displays + dpy;
351 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800352 dcblk->w = plane.getWidth();
353 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 dcblk->format = f;
355 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
356 dcblk->xdpi = hw.getDpiX();
357 dcblk->ydpi = hw.getDpiY();
358 dcblk->fps = hw.getRefreshRate();
359 dcblk->density = hw.getDensity();
360 asm volatile ("":::"memory");
361
362 // Initialize OpenGL|ES
363 glActiveTexture(GL_TEXTURE0);
364 glBindTexture(GL_TEXTURE_2D, 0);
365 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
366 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
367 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
368 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
369 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
370 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
371 glPixelStorei(GL_PACK_ALIGNMENT, 4);
372 glEnableClientState(GL_VERTEX_ARRAY);
373 glEnable(GL_SCISSOR_TEST);
374 glShadeModel(GL_FLAT);
375 glDisable(GL_DITHER);
376 glDisable(GL_CULL_FACE);
377
378 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
379 const uint16_t g1 = pack565(0x17,0x2f,0x17);
380 const uint16_t textureData[4] = { g0, g1, g1, g0 };
381 glGenTextures(1, &mWormholeTexName);
382 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
383 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
384 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
385 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
386 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
387 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
388 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
389
390 glViewport(0, 0, w, h);
391 glMatrixMode(GL_PROJECTION);
392 glLoadIdentity();
393 glOrthof(0, w, h, 0, 0, 1);
394
395 LayerDim::initDimmer(this, w, h);
396
397 mReadyToRunBarrier.open();
398
399 /*
400 * We're now ready to accept clients...
401 */
402
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700403 // start boot animation
404 property_set("ctl.start", "bootanim");
405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 return NO_ERROR;
407}
408
409// ----------------------------------------------------------------------------
410#if 0
411#pragma mark -
412#pragma mark Events Handler
413#endif
414
415void SurfaceFlinger::waitForEvent()
416{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700417 while (true) {
418 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800419 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700420 if (UNLIKELY(isFrozen())) {
421 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700422 const nsecs_t now = systemTime();
423 if (mFreezeDisplayTime == 0) {
424 mFreezeDisplayTime = now;
425 }
426 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
427 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700428 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700429
Mathias Agopianb6683b52009-04-28 03:17:50 -0700430 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800431
432 // see if we timed out
433 if (isFrozen()) {
434 const nsecs_t now = systemTime();
435 nsecs_t frozenTime = (now - mFreezeDisplayTime);
436 if (frozenTime >= freezeDisplayTimeout) {
437 // we timed out and are still frozen
438 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
439 mFreezeDisplay, mFreezeCount);
440 mFreezeDisplayTime = 0;
441 mFreezeCount = 0;
442 mFreezeDisplay = false;
443 }
444 }
445
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700446 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700447 switch (msg->what) {
448 case MessageQueue::INVALIDATE:
449 // invalidate message, just return to the main loop
450 return;
451 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 }
454}
455
456void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700457 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458}
459
460void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700461 // this is the IPC call
462 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463}
464
465void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
466{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700467 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468}
469
470// ----------------------------------------------------------------------------
471#if 0
472#pragma mark -
473#pragma mark Main loop
474#endif
475
476bool SurfaceFlinger::threadLoop()
477{
478 waitForEvent();
479
480 // check for transactions
481 if (UNLIKELY(mConsoleSignals)) {
482 handleConsoleEvents();
483 }
484
485 if (LIKELY(mTransactionCount == 0)) {
486 // if we're in a global transaction, don't do anything.
487 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
488 uint32_t transactionFlags = getTransactionFlags(mask);
489 if (LIKELY(transactionFlags)) {
490 handleTransaction(transactionFlags);
491 }
492 }
493
494 // post surfaces (if needed)
495 handlePageFlip();
496
497 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700498 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 // repaint the framebuffer (if needed)
500 handleRepaint();
501
Mathias Agopian74faca22009-09-17 16:18:16 -0700502 // inform the h/w that we're done compositing
503 hw.compositionComplete();
504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 // release the clients before we flip ('cause flip might block)
506 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 postFramebuffer();
509 } else {
510 // pretend we did the post
511 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 usleep(16667); // 60 fps period
513 }
514 return true;
515}
516
517void SurfaceFlinger::postFramebuffer()
518{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 if (!mInvalidRegion.isEmpty()) {
520 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700521 const nsecs_t now = systemTime();
522 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700524 mLastSwapBufferTime = systemTime() - now;
525 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 }
528}
529
530void SurfaceFlinger::handleConsoleEvents()
531{
532 // something to do with the console
533 const DisplayHardware& hw = graphicPlane(0).displayHardware();
534
535 int what = android_atomic_and(0, &mConsoleSignals);
536 if (what & eConsoleAcquired) {
537 hw.acquireScreen();
538 }
539
540 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700541 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 hw.releaseScreen();
544 }
545
546 if (what & eConsoleReleased) {
547 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 hw.releaseScreen();
549 } else {
550 mDeferReleaseConsole = true;
551 }
552 }
553
554 mDirtyRegion.set(hw.bounds());
555}
556
557void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
558{
Mathias Agopian3d579642009-06-04 18:46:21 -0700559 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560
Mathias Agopian3d579642009-06-04 18:46:21 -0700561 { // scope for the lock
562 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700563 const nsecs_t now = systemTime();
564 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700565 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700566 mLastTransactionTime = systemTime() - now;
567 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700568 }
569
570 // do this without lock held
571 const size_t count = ditchedLayers.size();
572 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700573 if (ditchedLayers[i] != 0) {
574 //LOGD("ditching layer %p", ditchedLayers[i].get());
575 ditchedLayers[i]->ditch();
576 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700577 }
578}
579
580void SurfaceFlinger::handleTransactionLocked(
581 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
582{
583 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 const size_t count = currentLayers.size();
585
586 /*
587 * Traversal of the children
588 * (perform the transaction for each of them if needed)
589 */
590
591 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
592 if (layersNeedTransaction) {
593 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700594 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
596 if (!trFlags) continue;
597
598 const uint32_t flags = layer->doTransaction(0);
599 if (flags & Layer::eVisibleRegion)
600 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 }
602 }
603
604 /*
605 * Perform our own transaction if needed
606 */
607
608 if (transactionFlags & eTransactionNeeded) {
609 if (mCurrentState.orientation != mDrawingState.orientation) {
610 // the orientation has changed, recompute all visible regions
611 // and invalidate everything.
612
613 const int dpy = 0;
614 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700615 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 GraphicPlane& plane(graphicPlane(dpy));
617 plane.setOrientation(orientation);
618
619 // update the shared control block
620 const DisplayHardware& hw(plane.displayHardware());
621 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
622 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800623 dcblk->w = plane.getWidth();
624 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625
626 mVisibleRegionsDirty = true;
627 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 }
629
630 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
631 // freezing or unfreezing the display -> trigger animation if needed
632 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800633 if (mFreezeDisplay)
634 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 }
636
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700637 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
638 // layers have been added
639 mVisibleRegionsDirty = true;
640 }
641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 // some layers might have been removed, so
643 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700645 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700647 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700648 const size_t count = previousLayers.size();
649 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700650 const sp<LayerBase>& layer(previousLayers[i]);
651 if (currentLayers.indexOf( layer ) < 0) {
652 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700653 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700654 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700655 }
656 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 }
658
659 // get rid of all resources we don't need anymore
660 // (layers and clients)
661 free_resources_l();
662 }
663
664 commitTransaction();
665}
666
667sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
668{
669 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
670}
671
672void SurfaceFlinger::computeVisibleRegions(
673 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
674{
675 const GraphicPlane& plane(graphicPlane(0));
676 const Transform& planeTransform(plane.transform());
677
678 Region aboveOpaqueLayers;
679 Region aboveCoveredLayers;
680 Region dirty;
681
682 bool secureFrameBuffer = false;
683
684 size_t i = currentLayers.size();
685 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700686 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 layer->validateVisibility(planeTransform);
688
689 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700690 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691
692 // handle hidden surfaces by setting the visible region to empty
693 Region opaqueRegion;
694 Region visibleRegion;
695 Region coveredRegion;
Mathias Agopian97011222009-07-28 10:57:27 -0700696 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700698 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 visibleRegion.set(bounds);
700 coveredRegion = visibleRegion;
701
702 // Remove the transparent area from the visible region
703 if (translucent) {
704 visibleRegion.subtractSelf(layer->transparentRegionScreen);
705 }
706
707 // compute the opaque region
708 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
709 // the opaque region is the visible region
710 opaqueRegion = visibleRegion;
711 }
712 }
713
714 // subtract the opaque region covered by the layers above us
715 visibleRegion.subtractSelf(aboveOpaqueLayers);
716 coveredRegion.andSelf(aboveCoveredLayers);
717
718 // compute this layer's dirty region
719 if (layer->contentDirty) {
720 // we need to invalidate the whole region
721 dirty = visibleRegion;
722 // as well, as the old visible region
723 dirty.orSelf(layer->visibleRegionScreen);
724 layer->contentDirty = false;
725 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700726 /* compute the exposed region:
727 * exposed = what's VISIBLE and NOT COVERED now
728 * but was COVERED before
729 */
730 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
732 dirty.subtractSelf(aboveOpaqueLayers);
733
734 // accumulate to the screen dirty region
735 dirtyRegion.orSelf(dirty);
736
Mathias Agopian62b74442009-04-14 23:02:51 -0700737 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700739 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740
741 // Store the visible region is screen space
742 layer->setVisibleRegion(visibleRegion);
743 layer->setCoveredRegion(coveredRegion);
744
Mathias Agopian97011222009-07-28 10:57:27 -0700745 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 if (layer->isSecure() && !visibleRegion.isEmpty()) {
747 secureFrameBuffer = true;
748 }
749 }
750
Mathias Agopian97011222009-07-28 10:57:27 -0700751 // invalidate the areas where a layer was removed
752 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
753 mDirtyRegionRemovedLayer.clear();
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 mSecureFrameBuffer = secureFrameBuffer;
756 opaqueRegion = aboveOpaqueLayers;
757}
758
759
760void SurfaceFlinger::commitTransaction()
761{
762 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700763 mResizeTransationPending = false;
764 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765}
766
767void SurfaceFlinger::handlePageFlip()
768{
769 bool visibleRegions = mVisibleRegionsDirty;
770 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
771 visibleRegions |= lockPageFlip(currentLayers);
772
773 const DisplayHardware& hw = graphicPlane(0).displayHardware();
774 const Region screenRegion(hw.bounds());
775 if (visibleRegions) {
776 Region opaqueRegion;
777 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
778 mWormholeRegion = screenRegion.subtract(opaqueRegion);
779 mVisibleRegionsDirty = false;
780 }
781
782 unlockPageFlip(currentLayers);
783 mDirtyRegion.andSelf(screenRegion);
784}
785
786bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
787{
788 bool recomputeVisibleRegions = false;
789 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700790 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700792 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 layer->lockPageFlip(recomputeVisibleRegions);
794 }
795 return recomputeVisibleRegions;
796}
797
798void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
799{
800 const GraphicPlane& plane(graphicPlane(0));
801 const Transform& planeTransform(plane.transform());
802 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700803 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700805 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 layer->unlockPageFlip(planeTransform, mDirtyRegion);
807 }
808}
809
Mathias Agopianb8a55602009-06-26 19:06:36 -0700810
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811void SurfaceFlinger::handleRepaint()
812{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700813 // compute the invalid region
814 mInvalidRegion.orSelf(mDirtyRegion);
815 if (mInvalidRegion.isEmpty()) {
816 // nothing to do
817 return;
818 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819
820 if (UNLIKELY(mDebugRegion)) {
821 debugFlashRegions();
822 }
823
Mathias Agopianb8a55602009-06-26 19:06:36 -0700824 // set the frame buffer
825 const DisplayHardware& hw(graphicPlane(0).displayHardware());
826 glMatrixMode(GL_MODELVIEW);
827 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828
829 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700830 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
831 (flags & DisplayHardware::BUFFER_PRESERVED))
832 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700833 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
834 // takes a rectangle, we must make sure to update that whole
835 // rectangle in that case
836 if (flags & DisplayHardware::SWAP_RECTANGLE) {
837 // FIXME: we really should be able to pass a region to
838 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 mDirtyRegion.set(mInvalidRegion.bounds());
840 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // in the BUFFER_PRESERVED case, obviously, we can update only
842 // what's needed and nothing more.
843 // NOTE: this is NOT a common case, as preserving the backbuffer
844 // is costly and usually involves copying the whole update back.
845 }
846 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700847 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 // We need to redraw the rectangle that will be updated
849 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700850 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700851 // rectangle instead of a region (see DisplayHardware::flip())
852 mDirtyRegion.set(mInvalidRegion.bounds());
853 } else {
854 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 mDirtyRegion.set(hw.bounds());
856 mInvalidRegion = mDirtyRegion;
857 }
858 }
859
860 // compose all surfaces
861 composeSurfaces(mDirtyRegion);
862
863 // clear the dirty regions
864 mDirtyRegion.clear();
865}
866
867void SurfaceFlinger::composeSurfaces(const Region& dirty)
868{
869 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
870 // should never happen unless the window manager has a bug
871 // draw something...
872 drawWormhole();
873 }
874 const SurfaceFlinger& flinger(*this);
875 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
876 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700877 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700879 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 const Region& visibleRegion(layer->visibleRegionScreen);
881 if (!visibleRegion.isEmpty()) {
882 const Region clip(dirty.intersect(visibleRegion));
883 if (!clip.isEmpty()) {
884 layer->draw(clip);
885 }
886 }
887 }
888}
889
890void SurfaceFlinger::unlockClients()
891{
892 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
893 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700894 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700896 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 layer->finishPageFlip();
898 }
899}
900
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901void SurfaceFlinger::debugFlashRegions()
902{
Mathias Agopian0926f502009-05-04 14:17:04 -0700903 const DisplayHardware& hw(graphicPlane(0).displayHardware());
904 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700905
906 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
907 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700908 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopian0926f502009-05-04 14:17:04 -0700909 mDirtyRegion.bounds() : hw.bounds());
910 composeSurfaces(repaint);
911 }
912
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913 glDisable(GL_TEXTURE_2D);
914 glDisable(GL_BLEND);
915 glDisable(GL_DITHER);
916 glDisable(GL_SCISSOR_TEST);
917
Mathias Agopian0926f502009-05-04 14:17:04 -0700918 static int toggle = 0;
919 toggle = 1 - toggle;
920 if (toggle) {
921 glColor4x(0x10000, 0, 0x10000, 0x10000);
922 } else {
923 glColor4x(0x10000, 0x10000, 0, 0x10000);
924 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925
Mathias Agopian20f68782009-05-11 00:03:41 -0700926 Region::const_iterator it = mDirtyRegion.begin();
927 Region::const_iterator const end = mDirtyRegion.end();
928 while (it != end) {
929 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930 GLfloat vertices[][2] = {
931 { r.left, r.top },
932 { r.left, r.bottom },
933 { r.right, r.bottom },
934 { r.right, r.top }
935 };
936 glVertexPointer(2, GL_FLOAT, 0, vertices);
937 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
938 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700939
Mathias Agopianb8a55602009-06-26 19:06:36 -0700940 if (mInvalidRegion.isEmpty()) {
941 mDirtyRegion.dump("mDirtyRegion");
942 mInvalidRegion.dump("mInvalidRegion");
943 }
944 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945
946 if (mDebugRegion > 1)
947 usleep(mDebugRegion * 1000);
948
949 glEnable(GL_SCISSOR_TEST);
950 //mDirtyRegion.dump("mDirtyRegion");
951}
952
953void SurfaceFlinger::drawWormhole() const
954{
955 const Region region(mWormholeRegion.intersect(mDirtyRegion));
956 if (region.isEmpty())
957 return;
958
959 const DisplayHardware& hw(graphicPlane(0).displayHardware());
960 const int32_t width = hw.getWidth();
961 const int32_t height = hw.getHeight();
962
963 glDisable(GL_BLEND);
964 glDisable(GL_DITHER);
965
966 if (LIKELY(!mDebugBackground)) {
967 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700968 Region::const_iterator it = region.begin();
969 Region::const_iterator const end = region.end();
970 while (it != end) {
971 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 const GLint sy = height - (r.top + r.height());
973 glScissor(r.left, sy, r.width(), r.height());
974 glClear(GL_COLOR_BUFFER_BIT);
975 }
976 } else {
977 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
978 { width, height }, { 0, height } };
979 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
980 glVertexPointer(2, GL_SHORT, 0, vertices);
981 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
982 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
983 glEnable(GL_TEXTURE_2D);
984 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
985 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
986 glMatrixMode(GL_TEXTURE);
987 glLoadIdentity();
988 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700989 Region::const_iterator it = region.begin();
990 Region::const_iterator const end = region.end();
991 while (it != end) {
992 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 const GLint sy = height - (r.top + r.height());
994 glScissor(r.left, sy, r.width(), r.height());
995 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
996 }
997 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
998 }
999}
1000
1001void SurfaceFlinger::debugShowFPS() const
1002{
1003 static int mFrameCount;
1004 static int mLastFrameCount = 0;
1005 static nsecs_t mLastFpsTime = 0;
1006 static float mFps = 0;
1007 mFrameCount++;
1008 nsecs_t now = systemTime();
1009 nsecs_t diff = now - mLastFpsTime;
1010 if (diff > ms2ns(250)) {
1011 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1012 mLastFpsTime = now;
1013 mLastFrameCount = mFrameCount;
1014 }
1015 // XXX: mFPS has the value we want
1016 }
1017
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001018status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019{
1020 Mutex::Autolock _l(mStateLock);
1021 addLayer_l(layer);
1022 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1023 return NO_ERROR;
1024}
1025
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001026status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027{
1028 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001029 status_t err = purgatorizeLayer_l(layer);
1030 if (err == NO_ERROR)
1031 setTransactionFlags(eTransactionNeeded);
1032 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033}
1034
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001035status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036{
1037 layer->forceVisibilityTransaction();
1038 setTransactionFlags(eTraversalNeeded);
1039 return NO_ERROR;
1040}
1041
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001042status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043{
Mathias Agopian0852e672009-09-04 19:50:23 -07001044 if (layer == 0)
1045 return BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 ssize_t i = mCurrentState.layersSortedByZ.add(
1047 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001048 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1049 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 mLayerMap.add(lbc->serverIndex(), lbc);
1051 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 return NO_ERROR;
1053}
1054
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001055status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056{
1057 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1058 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001059 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001060 sp<LayerBaseClient> layer =
1061 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001062 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 mLayerMap.removeItem(layer->serverIndex());
1064 }
1065 return NO_ERROR;
1066 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001067 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068}
1069
Mathias Agopian9a112062009-04-17 19:36:26 -07001070status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1071{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001072 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001073 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001074
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001075 layerBase->onRemoved();
1076
Mathias Agopian3d579642009-06-04 18:46:21 -07001077 // it's possible that we don't find a layer, because it might
1078 // have been destroyed already -- this is not technically an error
1079 // from the user because there is a race between BClient::destroySurface(),
1080 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001081 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1082}
1083
1084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085void SurfaceFlinger::free_resources_l()
1086{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001088 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 const size_t count = disconnectedClients.size();
1090 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001091 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 }
1094 disconnectedClients.clear();
1095}
1096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1098{
1099 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1100}
1101
1102uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1103{
1104 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1105 if ((old & flags)==0) { // wake the server up
1106 if (delay > 0) {
1107 signalDelayedEvent(delay);
1108 } else {
1109 signalEvent();
1110 }
1111 }
1112 return old;
1113}
1114
1115void SurfaceFlinger::openGlobalTransaction()
1116{
1117 android_atomic_inc(&mTransactionCount);
1118}
1119
1120void SurfaceFlinger::closeGlobalTransaction()
1121{
1122 if (android_atomic_dec(&mTransactionCount) == 1) {
1123 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001124
1125 // if there is a transaction with a resize, wait for it to
1126 // take effect before returning.
1127 Mutex::Autolock _l(mStateLock);
1128 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001129 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1130 if (CC_UNLIKELY(err != NO_ERROR)) {
1131 // just in case something goes wrong in SF, return to the
1132 // called after a few seconds.
1133 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1134 mResizeTransationPending = false;
1135 break;
1136 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 }
1139}
1140
1141status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1142{
1143 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1144 return BAD_VALUE;
1145
1146 Mutex::Autolock _l(mStateLock);
1147 mCurrentState.freezeDisplay = 1;
1148 setTransactionFlags(eTransactionNeeded);
1149
1150 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001151 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 return NO_ERROR;
1153}
1154
1155status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1156{
1157 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1158 return BAD_VALUE;
1159
1160 Mutex::Autolock _l(mStateLock);
1161 mCurrentState.freezeDisplay = 0;
1162 setTransactionFlags(eTransactionNeeded);
1163
1164 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001165 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 return NO_ERROR;
1167}
1168
Mathias Agopianc08731e2009-03-27 18:11:38 -07001169int SurfaceFlinger::setOrientation(DisplayID dpy,
1170 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171{
1172 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1173 return BAD_VALUE;
1174
1175 Mutex::Autolock _l(mStateLock);
1176 if (mCurrentState.orientation != orientation) {
1177 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001178 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 mCurrentState.orientation = orientation;
1180 setTransactionFlags(eTransactionNeeded);
1181 mTransactionCV.wait(mStateLock);
1182 } else {
1183 orientation = BAD_VALUE;
1184 }
1185 }
1186 return orientation;
1187}
1188
1189sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001190 const String8& name, ISurfaceFlingerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1192 uint32_t flags)
1193{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001194 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001196
1197 if (int32_t(w|h) < 0) {
1198 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1199 int(w), int(h));
1200 return surfaceHandle;
1201 }
1202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001204 sp<Client> client = mClientsMap.valueFor(clientId);
1205 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1207 return surfaceHandle;
1208 }
1209
1210 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001211 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1213 LOGE("createSurface() failed, generateId = %d", id);
1214 return surfaceHandle;
1215 }
1216
1217 switch (flags & eFXSurfaceMask) {
1218 case eFXSurfaceNormal:
1219 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001220 layer = createPushBuffersSurfaceLocked(client, d, id,
1221 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001223 layer = createNormalSurfaceLocked(client, d, id,
1224 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 }
1226 break;
1227 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001228 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 break;
1230 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001231 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 break;
1233 }
1234
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001235 if (layer != 0) {
Mathias Agopian285dbde2010-03-01 16:09:43 -08001236 layer->setName(name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 setTransactionFlags(eTransactionNeeded);
1238 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001239 if (surfaceHandle != 0) {
1240 params->token = surfaceHandle->getToken();
1241 params->identity = surfaceHandle->getIdentity();
1242 params->width = w;
1243 params->height = h;
1244 params->format = format;
1245 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247
1248 return surfaceHandle;
1249}
1250
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001251sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001252 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001253 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1254 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255{
1256 // initialize the surfaces
1257 switch (format) { // TODO: take h/w into account
1258 case PIXEL_FORMAT_TRANSPARENT:
1259 case PIXEL_FORMAT_TRANSLUCENT:
1260 format = PIXEL_FORMAT_RGBA_8888;
1261 break;
1262 case PIXEL_FORMAT_OPAQUE:
1263 format = PIXEL_FORMAT_RGB_565;
1264 break;
1265 }
1266
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001267 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001268 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 if (LIKELY(err == NO_ERROR)) {
1270 layer->initStates(w, h, flags);
1271 addLayer_l(layer);
1272 } else {
1273 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001274 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 }
1276 return layer;
1277}
1278
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001279sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001280 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1282{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001283 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 layer->initStates(w, h, flags);
1285 addLayer_l(layer);
1286 return layer;
1287}
1288
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001289sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001290 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1292{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001293 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 layer->initStates(w, h, flags);
1295 addLayer_l(layer);
1296 return layer;
1297}
1298
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001299sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001300 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1302{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001303 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 layer->initStates(w, h, flags);
1305 addLayer_l(layer);
1306 return layer;
1307}
1308
Mathias Agopian9a112062009-04-17 19:36:26 -07001309status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310{
Mathias Agopian9a112062009-04-17 19:36:26 -07001311 /*
1312 * called by the window manager, when a surface should be marked for
1313 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001314 *
1315 * The surface is removed from the current and drawing lists, but placed
1316 * in the purgatory queue, so it's not destroyed right-away (we need
1317 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001318 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001319
Mathias Agopian48d819a2009-09-10 19:41:18 -07001320 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001321 Mutex::Autolock _l(mStateLock);
1322 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001323 if (layer != 0) {
1324 err = purgatorizeLayer_l(layer);
1325 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001326 setTransactionFlags(eTransactionNeeded);
1327 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001328 }
1329 return err;
1330}
1331
1332status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1333{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001334 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001335
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001336 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001337 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001338 sp<LayerBaseClient> layer;
1339 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 MessageDestroySurface(
1341 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1342 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001344 sp<LayerBaseClient> l(layer);
1345 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001346 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001347 /*
1348 * remove the layer from the current list -- chances are that it's
1349 * not in the list anyway, because it should have been removed
1350 * already upon request of the client (eg: window manager).
1351 * However, a buggy client could have not done that.
1352 * Since we know we don't have any more clients, we don't need
1353 * to use the purgatory.
1354 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001355 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001356 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1357 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001358 return true;
1359 }
1360 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001361
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001362 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 return NO_ERROR;
1364}
1365
1366status_t SurfaceFlinger::setClientState(
1367 ClientID cid,
1368 int32_t count,
1369 const layer_state_t* states)
1370{
1371 Mutex::Autolock _l(mStateLock);
1372 uint32_t flags = 0;
1373 cid <<= 16;
1374 for (int i=0 ; i<count ; i++) {
1375 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001376 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 if (what & ePositionChanged) {
1380 if (layer->setPosition(s.x, s.y))
1381 flags |= eTraversalNeeded;
1382 }
1383 if (what & eLayerChanged) {
1384 if (layer->setLayer(s.z)) {
1385 mCurrentState.layersSortedByZ.reorder(
1386 layer, &Layer::compareCurrentStateZ);
1387 // we need traversal (state changed)
1388 // AND transaction (list changed)
1389 flags |= eTransactionNeeded|eTraversalNeeded;
1390 }
1391 }
1392 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001393 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001395 mResizeTransationPending = true;
1396 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 }
1398 if (what & eAlphaChanged) {
1399 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eMatrixChanged) {
1403 if (layer->setMatrix(s.matrix))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eTransparentRegionChanged) {
1407 if (layer->setTransparentRegionHint(s.transparentRegion))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eVisibilityChanged) {
1411 if (layer->setFlags(s.flags, s.mask))
1412 flags |= eTraversalNeeded;
1413 }
1414 }
1415 }
1416 if (flags) {
1417 setTransactionFlags(flags);
1418 }
1419 return NO_ERROR;
1420}
1421
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001422sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001424 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1425 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426}
1427
1428void SurfaceFlinger::screenReleased(int dpy)
1429{
1430 // this may be called by a signal handler, we can't do too much in here
1431 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1432 signalEvent();
1433}
1434
1435void SurfaceFlinger::screenAcquired(int dpy)
1436{
1437 // this may be called by a signal handler, we can't do too much in here
1438 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1439 signalEvent();
1440}
1441
1442status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1443{
1444 const size_t SIZE = 1024;
1445 char buffer[SIZE];
1446 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001447 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 snprintf(buffer, SIZE, "Permission Denial: "
1449 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1450 IPCThreadState::self()->getCallingPid(),
1451 IPCThreadState::self()->getCallingUid());
1452 result.append(buffer);
1453 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001454
1455 // figure out if we're stuck somewhere
1456 const nsecs_t now = systemTime();
1457 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1458 const nsecs_t inTransaction(mDebugInTransaction);
1459 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1460 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1461
1462 // Try to get the main lock, but don't insist if we can't
1463 // (this would indicate SF is stuck, but we want to be able to
1464 // print something in dumpsys).
1465 int retry = 3;
1466 while (mStateLock.tryLock()<0 && --retry>=0) {
1467 usleep(1000000);
1468 }
1469 const bool locked(retry >= 0);
1470 if (!locked) {
1471 snprintf(buffer, SIZE,
1472 "SurfaceFlinger appears to be unresponsive, "
1473 "dumping anyways (no locks held)\n");
1474 result.append(buffer);
1475 }
1476
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 size_t s = mClientsMap.size();
1478 char name[64];
1479 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001480 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 sprintf(name, " Client (id=0x%08x)", client->cid);
1482 client->dump(name);
1483 }
1484 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1485 const size_t count = currentLayers.size();
1486 for (size_t i=0 ; i<count ; i++) {
1487 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001488 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 const Layer::State& s = layer->drawingState();
1490 snprintf(buffer, SIZE,
1491 "+ %s %p\n"
1492 " "
1493 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
Mathias Agopian401c2572009-09-23 19:16:27 -07001494 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001496 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 s.z, layer->tx(), layer->ty(), s.w, s.h,
Mathias Agopian401c2572009-09-23 19:16:27 -07001498 layer->needsBlending(), layer->needsDithering(),
1499 layer->contentDirty,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 s.alpha, s.flags,
Mathias Agopianeda65402010-02-22 03:15:57 -08001501 s.transform[0][0], s.transform[0][1],
1502 s.transform[1][0], s.transform[1][1]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 result.append(buffer);
1504 buffer[0] = 0;
1505 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001506 sp<LayerBaseClient> lbc =
1507 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1508 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001509 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 snprintf(buffer, SIZE,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001511 " name=%s\n", lbc->getName().string());
1512 result.append(buffer);
1513 snprintf(buffer, SIZE,
1514 " id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001515 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 lbc->getIdentity());
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001517
1518 result.append(buffer);
1519 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001522 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1523 if (l != 0) {
Mathias Agopian86f73292009-09-17 01:35:28 -07001524 SharedBufferStack::Statistics stats = l->lcblk->getStats();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001525 result.append( l->lcblk->dump(" ") );
Mathias Agopian45853c92010-02-26 18:59:23 -08001526 snprintf(buffer, SIZE,
1527 " front-index=%u\n", l->getFrontBufferIndex());
1528 result.append(buffer);
1529
Mathias Agopian3330b202009-10-05 17:07:12 -07001530 sp<const GraphicBuffer> buf0(l->getBuffer(0));
1531 sp<const GraphicBuffer> buf1(l->getBuffer(1));
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001532 uint32_t w0=0, h0=0, s0=0;
1533 uint32_t w1=0, h1=0, s1=0;
1534 if (buf0 != 0) {
1535 w0 = buf0->getWidth();
1536 h0 = buf0->getHeight();
1537 s0 = buf0->getStride();
1538 }
1539 if (buf1 != 0) {
1540 w1 = buf1->getWidth();
1541 h1 = buf1->getHeight();
1542 s1 = buf1->getStride();
1543 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001544 snprintf(buffer, SIZE,
1545 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001546 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian86f73292009-09-17 01:35:28 -07001547 " freezeLock=%p, dq-q-time=%u us\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 l->pixelFormat(),
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001549 w0, h0, s0, w1, h1, s1,
Mathias Agopian86f73292009-09-17 01:35:28 -07001550 l->getFreezeLock().get(), stats.totalTime);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001551 result.append(buffer);
1552 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554 s.transparentRegion.dump(result, "transparentRegion");
1555 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1556 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1557 }
1558 mWormholeRegion.dump(result, "WormholeRegion");
1559 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1560 snprintf(buffer, SIZE,
1561 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1562 mFreezeDisplay?"yes":"no", mFreezeCount,
1563 mCurrentState.orientation, hw.canDraw());
1564 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001565 snprintf(buffer, SIZE,
1566 " last eglSwapBuffers() time: %f us\n"
1567 " last transaction time : %f us\n",
1568 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1569 result.append(buffer);
1570 if (inSwapBuffersDuration || !locked) {
1571 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1572 inSwapBuffersDuration/1000.0);
1573 result.append(buffer);
1574 }
1575 if (inTransactionDuration || !locked) {
1576 snprintf(buffer, SIZE, " transaction time: %f us\n",
1577 inTransactionDuration/1000.0);
1578 result.append(buffer);
1579 }
Mathias Agopian759fdb22009-07-02 17:33:40 -07001580 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001581 result.append(buffer);
Mathias Agopian3330b202009-10-05 17:07:12 -07001582 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001583 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001584
1585 if (locked) {
1586 mStateLock.unlock();
1587 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 }
1589 write(fd, result.string(), result.size());
1590 return NO_ERROR;
1591}
1592
1593status_t SurfaceFlinger::onTransact(
1594 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1595{
1596 switch (code) {
1597 case CREATE_CONNECTION:
1598 case OPEN_GLOBAL_TRANSACTION:
1599 case CLOSE_GLOBAL_TRANSACTION:
1600 case SET_ORIENTATION:
1601 case FREEZE_DISPLAY:
1602 case UNFREEZE_DISPLAY:
1603 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 {
1605 // codes that require permission check
1606 IPCThreadState* ipc = IPCThreadState::self();
1607 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001608 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001609 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1610 LOGE("Permission Denial: "
1611 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1612 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 }
1614 }
1615 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1617 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001618 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001619 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1620 IPCThreadState* ipc = IPCThreadState::self();
1621 const int pid = ipc->getCallingPid();
1622 const int uid = ipc->getCallingUid();
1623 LOGE("Permission Denial: "
1624 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 return PERMISSION_DENIED;
1626 }
1627 int n;
1628 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001629 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001631 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 return NO_ERROR;
1633 case 1002: // SHOW_UPDATES
1634 n = data.readInt32();
1635 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1636 return NO_ERROR;
1637 case 1003: // SHOW_BACKGROUND
1638 n = data.readInt32();
1639 mDebugBackground = n ? 1 : 0;
1640 return NO_ERROR;
1641 case 1004:{ // repaint everything
1642 Mutex::Autolock _l(mStateLock);
1643 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1644 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1645 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001646 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001648 case 1005:{ // force transaction
1649 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1650 return NO_ERROR;
1651 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 case 1007: // set mFreezeCount
1653 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001654 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 return NO_ERROR;
1656 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001657 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 reply->writeInt32(0);
1659 reply->writeInt32(mDebugRegion);
1660 reply->writeInt32(mDebugBackground);
1661 return NO_ERROR;
1662 case 1013: {
1663 Mutex::Autolock _l(mStateLock);
1664 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1665 reply->writeInt32(hw.getPageFlipCount());
1666 }
1667 return NO_ERROR;
1668 }
1669 }
1670 return err;
1671}
1672
1673// ---------------------------------------------------------------------------
1674#if 0
1675#pragma mark -
1676#endif
1677
1678Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1679 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1680{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001682 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001683
1684 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1685 "SurfaceFlinger Client control-block");
1686
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001687 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001688 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001689 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 }
1691}
1692
1693Client::~Client() {
1694 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001695 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001696 }
1697}
1698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699int32_t Client::generateId(int pid)
1700{
1701 const uint32_t i = clz( ~mBitmap );
1702 if (i >= NUM_LAYERS_MAX) {
1703 return NO_MEMORY;
1704 }
1705 mPid = pid;
1706 mInUse.add(uint8_t(i));
1707 mBitmap |= 1<<(31-i);
1708 return i;
1709}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001710
1711status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712{
1713 ssize_t idx = mInUse.indexOf(id);
1714 if (idx < 0)
1715 return NAME_NOT_FOUND;
1716 return mLayers.insertAt(layer, idx);
1717}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001718
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719void Client::free(int32_t id)
1720{
1721 ssize_t idx = mInUse.remove(uint8_t(id));
1722 if (idx >= 0) {
1723 mBitmap &= ~(1<<(31-id));
1724 mLayers.removeItemsAt(idx);
1725 }
1726}
1727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728bool Client::isValid(int32_t i) const {
1729 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1730}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001731
1732sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1733 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001735 if (idx >= 0) {
1736 lbc = mLayers[idx].promote();
1737 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1738 }
1739 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740}
1741
1742void Client::dump(const char* what)
1743{
1744}
1745
1746// ---------------------------------------------------------------------------
1747#if 0
1748#pragma mark -
1749#endif
1750
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001751BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752 : mId(cid), mFlinger(flinger), mCblk(cblk)
1753{
1754}
1755
1756BClient::~BClient() {
1757 // destroy all resources attached to this client
1758 mFlinger->destroyConnection(mId);
1759}
1760
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001761sp<IMemoryHeap> BClient::getControlBlock() const {
1762 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763}
1764
1765sp<ISurface> BClient::createSurface(
1766 ISurfaceFlingerClient::surface_data_t* params, int pid,
Mathias Agopian285dbde2010-03-01 16:09:43 -08001767 const String8& name,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001768 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1769 uint32_t flags)
1770{
Mathias Agopian285dbde2010-03-01 16:09:43 -08001771 return mFlinger->createSurface(mId, pid, name, params, display, w, h,
1772 format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773}
1774
1775status_t BClient::destroySurface(SurfaceID sid)
1776{
1777 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001778 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779}
1780
1781status_t BClient::setState(int32_t count, const layer_state_t* states)
1782{
1783 return mFlinger->setClientState(mId, count, states);
1784}
1785
1786// ---------------------------------------------------------------------------
1787
1788GraphicPlane::GraphicPlane()
1789 : mHw(0)
1790{
1791}
1792
1793GraphicPlane::~GraphicPlane() {
1794 delete mHw;
1795}
1796
1797bool GraphicPlane::initialized() const {
1798 return mHw ? true : false;
1799}
1800
Mathias Agopian2b92d892010-02-08 15:49:35 -08001801int GraphicPlane::getWidth() const {
1802 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001803}
1804
Mathias Agopian2b92d892010-02-08 15:49:35 -08001805int GraphicPlane::getHeight() const {
1806 return mHeight;
1807}
1808
1809void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1810{
1811 mHw = hw;
1812
1813 // initialize the display orientation transform.
1814 // it's a constant that should come from the display driver.
1815 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1816 char property[PROPERTY_VALUE_MAX];
1817 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1818 //displayOrientation
1819 switch (atoi(property)) {
1820 case 90:
1821 displayOrientation = ISurfaceComposer::eOrientation90;
1822 break;
1823 case 270:
1824 displayOrientation = ISurfaceComposer::eOrientation270;
1825 break;
1826 }
1827 }
1828
1829 const float w = hw->getWidth();
1830 const float h = hw->getHeight();
1831 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1832 &mDisplayTransform);
1833 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1834 mDisplayWidth = h;
1835 mDisplayHeight = w;
1836 } else {
1837 mDisplayWidth = w;
1838 mDisplayHeight = h;
1839 }
1840
1841 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842}
1843
1844status_t GraphicPlane::orientationToTransfrom(
1845 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001846{
1847 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 switch (orientation) {
1849 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001850 flags = Transform::ROT_0;
1851 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001852 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001853 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854 break;
1855 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001856 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001857 break;
1858 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001859 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001860 break;
1861 default:
1862 return BAD_VALUE;
1863 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001864 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865 return NO_ERROR;
1866}
1867
1868status_t GraphicPlane::setOrientation(int orientation)
1869{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 // If the rotation can be handled in hardware, this is where
1871 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001872
1873 const DisplayHardware& hw(displayHardware());
1874 const float w = mDisplayWidth;
1875 const float h = mDisplayHeight;
1876 mWidth = int(w);
1877 mHeight = int(h);
1878
1879 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001880 GraphicPlane::orientationToTransfrom(orientation, w, h,
1881 &orientationTransform);
1882 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1883 mWidth = int(h);
1884 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001885 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001886
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001887 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001888 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889 return NO_ERROR;
1890}
1891
1892const DisplayHardware& GraphicPlane::displayHardware() const {
1893 return *mHw;
1894}
1895
1896const Transform& GraphicPlane::transform() const {
1897 return mGlobalTransform;
1898}
1899
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001900EGLDisplay GraphicPlane::getEGLDisplay() const {
1901 return mHw->getEGLDisplay();
1902}
1903
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904// ---------------------------------------------------------------------------
1905
1906}; // namespace android