blob: 49268588b5b2ef77b8acb3e73b6d5e4834bc3d6f [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{
Mathias Agopian96f08192010-06-02 23:28:45 -0700230 sp<ISurfaceComposerClient> bclient;
231 sp<Client> client(new Client(this));
232 status_t err = client->initCheck();
233 if (err == NO_ERROR) {
234 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 return bclient;
237}
238
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700239sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
240{
241 sp<ISurfaceComposerClient> bclient;
242 sp<UserClient> client(new UserClient(this));
243 status_t err = client->initCheck();
244 if (err == NO_ERROR) {
245 bclient = client;
246 }
247 return bclient;
248}
249
250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
252{
253 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
254 const GraphicPlane& plane(mGraphicPlanes[dpy]);
255 return plane;
256}
257
258GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
259{
260 return const_cast<GraphicPlane&>(
261 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
262}
263
264void SurfaceFlinger::bootFinished()
265{
266 const nsecs_t now = systemTime();
267 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700268 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700269 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700270 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271}
272
273void SurfaceFlinger::onFirstRef()
274{
275 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
276
277 // Wait for the main thread to be done with its initialization
278 mReadyToRunBarrier.wait();
279}
280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281static inline uint16_t pack565(int r, int g, int b) {
282 return (r<<11)|(g<<5)|b;
283}
284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285status_t SurfaceFlinger::readyToRun()
286{
287 LOGI( "SurfaceFlinger's main thread ready to run. "
288 "Initializing graphics H/W...");
289
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 // we only support one display currently
291 int dpy = 0;
292
293 {
294 // initialize the main display
295 GraphicPlane& plane(graphicPlane(dpy));
296 DisplayHardware* const hw = new DisplayHardware(this, dpy);
297 plane.setDisplayHardware(hw);
298 }
299
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700300 // create the shared control-block
301 mServerHeap = new MemoryHeapBase(4096,
302 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
303 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
304
305 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
306 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
307
308 new(mServerCblk) surface_flinger_cblk_t;
309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 // initialize primary screen
311 // (other display should be initialized in the same manner, but
312 // asynchronously, as they could come and go. None of this is supported
313 // yet).
314 const GraphicPlane& plane(graphicPlane(dpy));
315 const DisplayHardware& hw = plane.displayHardware();
316 const uint32_t w = hw.getWidth();
317 const uint32_t h = hw.getHeight();
318 const uint32_t f = hw.getFormat();
319 hw.makeCurrent();
320
321 // initialize the shared control block
322 mServerCblk->connected |= 1<<dpy;
323 display_cblk_t* dcblk = mServerCblk->displays + dpy;
324 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800325 dcblk->w = plane.getWidth();
326 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 dcblk->format = f;
328 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
329 dcblk->xdpi = hw.getDpiX();
330 dcblk->ydpi = hw.getDpiY();
331 dcblk->fps = hw.getRefreshRate();
332 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333
334 // Initialize OpenGL|ES
335 glActiveTexture(GL_TEXTURE0);
336 glBindTexture(GL_TEXTURE_2D, 0);
337 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
338 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
339 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
340 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
341 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
342 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
343 glPixelStorei(GL_PACK_ALIGNMENT, 4);
344 glEnableClientState(GL_VERTEX_ARRAY);
345 glEnable(GL_SCISSOR_TEST);
346 glShadeModel(GL_FLAT);
347 glDisable(GL_DITHER);
348 glDisable(GL_CULL_FACE);
349
350 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
351 const uint16_t g1 = pack565(0x17,0x2f,0x17);
352 const uint16_t textureData[4] = { g0, g1, g1, g0 };
353 glGenTextures(1, &mWormholeTexName);
354 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
355 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
356 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
357 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
359 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
360 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
361
362 glViewport(0, 0, w, h);
363 glMatrixMode(GL_PROJECTION);
364 glLoadIdentity();
365 glOrthof(0, w, h, 0, 0, 1);
366
367 LayerDim::initDimmer(this, w, h);
368
369 mReadyToRunBarrier.open();
370
371 /*
372 * We're now ready to accept clients...
373 */
374
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700375 // start boot animation
376 property_set("ctl.start", "bootanim");
377
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 return NO_ERROR;
379}
380
381// ----------------------------------------------------------------------------
382#if 0
383#pragma mark -
384#pragma mark Events Handler
385#endif
386
387void SurfaceFlinger::waitForEvent()
388{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700389 while (true) {
390 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800391 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700392 if (UNLIKELY(isFrozen())) {
393 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700394 const nsecs_t now = systemTime();
395 if (mFreezeDisplayTime == 0) {
396 mFreezeDisplayTime = now;
397 }
398 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
399 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700400 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700401
Mathias Agopianbb641242010-05-18 17:06:55 -0700402 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800403
404 // see if we timed out
405 if (isFrozen()) {
406 const nsecs_t now = systemTime();
407 nsecs_t frozenTime = (now - mFreezeDisplayTime);
408 if (frozenTime >= freezeDisplayTimeout) {
409 // we timed out and are still frozen
410 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
411 mFreezeDisplay, mFreezeCount);
412 mFreezeDisplayTime = 0;
413 mFreezeCount = 0;
414 mFreezeDisplay = false;
415 }
416 }
417
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700418 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700419 switch (msg->what) {
420 case MessageQueue::INVALIDATE:
421 // invalidate message, just return to the main loop
422 return;
423 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 }
426}
427
428void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700429 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430}
431
432void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700433 // this is the IPC call
434 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435}
436
Mathias Agopianbb641242010-05-18 17:06:55 -0700437status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
438 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439{
Mathias Agopianbb641242010-05-18 17:06:55 -0700440 return mEventQueue.postMessage(msg, reltime, flags);
441}
442
443status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
444 nsecs_t reltime, uint32_t flags)
445{
446 status_t res = mEventQueue.postMessage(msg, reltime, flags);
447 if (res == NO_ERROR) {
448 msg->wait();
449 }
450 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451}
452
453// ----------------------------------------------------------------------------
454#if 0
455#pragma mark -
456#pragma mark Main loop
457#endif
458
459bool SurfaceFlinger::threadLoop()
460{
461 waitForEvent();
462
463 // check for transactions
464 if (UNLIKELY(mConsoleSignals)) {
465 handleConsoleEvents();
466 }
467
468 if (LIKELY(mTransactionCount == 0)) {
469 // if we're in a global transaction, don't do anything.
470 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
471 uint32_t transactionFlags = getTransactionFlags(mask);
472 if (LIKELY(transactionFlags)) {
473 handleTransaction(transactionFlags);
474 }
475 }
476
477 // post surfaces (if needed)
478 handlePageFlip();
479
480 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700481 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 // repaint the framebuffer (if needed)
483 handleRepaint();
484
Mathias Agopian74faca22009-09-17 16:18:16 -0700485 // inform the h/w that we're done compositing
486 hw.compositionComplete();
487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 // release the clients before we flip ('cause flip might block)
489 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 postFramebuffer();
492 } else {
493 // pretend we did the post
494 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 usleep(16667); // 60 fps period
496 }
497 return true;
498}
499
500void SurfaceFlinger::postFramebuffer()
501{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 if (!mInvalidRegion.isEmpty()) {
503 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700504 const nsecs_t now = systemTime();
505 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700507 mLastSwapBufferTime = systemTime() - now;
508 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 }
511}
512
513void SurfaceFlinger::handleConsoleEvents()
514{
515 // something to do with the console
516 const DisplayHardware& hw = graphicPlane(0).displayHardware();
517
518 int what = android_atomic_and(0, &mConsoleSignals);
519 if (what & eConsoleAcquired) {
520 hw.acquireScreen();
521 }
522
523 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700524 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 hw.releaseScreen();
527 }
528
529 if (what & eConsoleReleased) {
530 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 hw.releaseScreen();
532 } else {
533 mDeferReleaseConsole = true;
534 }
535 }
536
537 mDirtyRegion.set(hw.bounds());
538}
539
540void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
541{
Mathias Agopian3d579642009-06-04 18:46:21 -0700542 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543
Mathias Agopian3d579642009-06-04 18:46:21 -0700544 { // scope for the lock
545 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700546 const nsecs_t now = systemTime();
547 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700548 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700549 mLastTransactionTime = systemTime() - now;
550 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700551 }
552
553 // do this without lock held
554 const size_t count = ditchedLayers.size();
555 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700556 if (ditchedLayers[i] != 0) {
557 //LOGD("ditching layer %p", ditchedLayers[i].get());
558 ditchedLayers[i]->ditch();
559 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700560 }
561}
562
563void SurfaceFlinger::handleTransactionLocked(
564 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
565{
566 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 const size_t count = currentLayers.size();
568
569 /*
570 * Traversal of the children
571 * (perform the transaction for each of them if needed)
572 */
573
574 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
575 if (layersNeedTransaction) {
576 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
579 if (!trFlags) continue;
580
581 const uint32_t flags = layer->doTransaction(0);
582 if (flags & Layer::eVisibleRegion)
583 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 }
585 }
586
587 /*
588 * Perform our own transaction if needed
589 */
590
591 if (transactionFlags & eTransactionNeeded) {
592 if (mCurrentState.orientation != mDrawingState.orientation) {
593 // the orientation has changed, recompute all visible regions
594 // and invalidate everything.
595
596 const int dpy = 0;
597 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700598 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 GraphicPlane& plane(graphicPlane(dpy));
600 plane.setOrientation(orientation);
601
602 // update the shared control block
603 const DisplayHardware& hw(plane.displayHardware());
604 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
605 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800606 dcblk->w = plane.getWidth();
607 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608
609 mVisibleRegionsDirty = true;
610 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 }
612
613 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
614 // freezing or unfreezing the display -> trigger animation if needed
615 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800616 if (mFreezeDisplay)
617 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 }
619
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700620 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
621 // layers have been added
622 mVisibleRegionsDirty = true;
623 }
624
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 // some layers might have been removed, so
626 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700628 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700630 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700631 const size_t count = previousLayers.size();
632 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700633 const sp<LayerBase>& layer(previousLayers[i]);
634 if (currentLayers.indexOf( layer ) < 0) {
635 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700636 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700637 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700638 }
639 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 }
642
643 commitTransaction();
644}
645
646sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
647{
648 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
649}
650
651void SurfaceFlinger::computeVisibleRegions(
652 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
653{
654 const GraphicPlane& plane(graphicPlane(0));
655 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700656 const DisplayHardware& hw(plane.displayHardware());
657 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658
659 Region aboveOpaqueLayers;
660 Region aboveCoveredLayers;
661 Region dirty;
662
663 bool secureFrameBuffer = false;
664
665 size_t i = currentLayers.size();
666 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700667 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 layer->validateVisibility(planeTransform);
669
670 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700671 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672
Mathias Agopianab028732010-03-16 16:41:46 -0700673 /*
674 * opaqueRegion: area of a surface that is fully opaque.
675 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700677
678 /*
679 * visibleRegion: area of a surface that is visible on screen
680 * and not fully transparent. This is essentially the layer's
681 * footprint minus the opaque regions above it.
682 * Areas covered by a translucent surface are considered visible.
683 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700685
686 /*
687 * coveredRegion: area of a surface that is covered by all
688 * visible regions above it (which includes the translucent areas).
689 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700691
692
693 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700694 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700696 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700698 visibleRegion.andSelf(screenRegion);
699 if (!visibleRegion.isEmpty()) {
700 // Remove the transparent area from the visible region
701 if (translucent) {
702 visibleRegion.subtractSelf(layer->transparentRegionScreen);
703 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704
Mathias Agopianab028732010-03-16 16:41:46 -0700705 // compute the opaque region
706 const int32_t layerOrientation = layer->getOrientation();
707 if (s.alpha==255 && !translucent &&
708 ((layerOrientation & Transform::ROT_INVALID) == false)) {
709 // the opaque region is the layer's footprint
710 opaqueRegion = visibleRegion;
711 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712 }
713 }
714
Mathias Agopianab028732010-03-16 16:41:46 -0700715 // Clip the covered region to the visible region
716 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
717
718 // Update aboveCoveredLayers for next (lower) layer
719 aboveCoveredLayers.orSelf(visibleRegion);
720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 // subtract the opaque region covered by the layers above us
722 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723
724 // compute this layer's dirty region
725 if (layer->contentDirty) {
726 // we need to invalidate the whole region
727 dirty = visibleRegion;
728 // as well, as the old visible region
729 dirty.orSelf(layer->visibleRegionScreen);
730 layer->contentDirty = false;
731 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700732 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700733 * the exposed region consists of two components:
734 * 1) what's VISIBLE now and was COVERED before
735 * 2) what's EXPOSED now less what was EXPOSED before
736 *
737 * note that (1) is conservative, we start with the whole
738 * visible region but only keep what used to be covered by
739 * something -- which mean it may have been exposed.
740 *
741 * (2) handles areas that were not covered by anything but got
742 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700743 */
Mathias Agopianab028732010-03-16 16:41:46 -0700744 const Region newExposed = visibleRegion - coveredRegion;
745 const Region oldVisibleRegion = layer->visibleRegionScreen;
746 const Region oldCoveredRegion = layer->coveredRegionScreen;
747 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
748 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 }
750 dirty.subtractSelf(aboveOpaqueLayers);
751
752 // accumulate to the screen dirty region
753 dirtyRegion.orSelf(dirty);
754
Mathias Agopianab028732010-03-16 16:41:46 -0700755 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757
758 // Store the visible region is screen space
759 layer->setVisibleRegion(visibleRegion);
760 layer->setCoveredRegion(coveredRegion);
761
Mathias Agopian97011222009-07-28 10:57:27 -0700762 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763 if (layer->isSecure() && !visibleRegion.isEmpty()) {
764 secureFrameBuffer = true;
765 }
766 }
767
Mathias Agopian97011222009-07-28 10:57:27 -0700768 // invalidate the areas where a layer was removed
769 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
770 mDirtyRegionRemovedLayer.clear();
771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 mSecureFrameBuffer = secureFrameBuffer;
773 opaqueRegion = aboveOpaqueLayers;
774}
775
776
777void SurfaceFlinger::commitTransaction()
778{
779 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700780 mResizeTransationPending = false;
781 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782}
783
784void SurfaceFlinger::handlePageFlip()
785{
786 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700787 LayerVector& currentLayers = const_cast<LayerVector&>(
788 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 visibleRegions |= lockPageFlip(currentLayers);
790
791 const DisplayHardware& hw = graphicPlane(0).displayHardware();
792 const Region screenRegion(hw.bounds());
793 if (visibleRegions) {
794 Region opaqueRegion;
795 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
796 mWormholeRegion = screenRegion.subtract(opaqueRegion);
797 mVisibleRegionsDirty = false;
798 }
799
800 unlockPageFlip(currentLayers);
801 mDirtyRegion.andSelf(screenRegion);
802}
803
804bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
805{
806 bool recomputeVisibleRegions = false;
807 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700808 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700810 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 layer->lockPageFlip(recomputeVisibleRegions);
812 }
813 return recomputeVisibleRegions;
814}
815
816void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
817{
818 const GraphicPlane& plane(graphicPlane(0));
819 const Transform& planeTransform(plane.transform());
820 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700821 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700823 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824 layer->unlockPageFlip(planeTransform, mDirtyRegion);
825 }
826}
827
Mathias Agopianb8a55602009-06-26 19:06:36 -0700828
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829void SurfaceFlinger::handleRepaint()
830{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700831 // compute the invalid region
832 mInvalidRegion.orSelf(mDirtyRegion);
833 if (mInvalidRegion.isEmpty()) {
834 // nothing to do
835 return;
836 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837
838 if (UNLIKELY(mDebugRegion)) {
839 debugFlashRegions();
840 }
841
Mathias Agopianb8a55602009-06-26 19:06:36 -0700842 // set the frame buffer
843 const DisplayHardware& hw(graphicPlane(0).displayHardware());
844 glMatrixMode(GL_MODELVIEW);
845 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846
847 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700848 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
849 (flags & DisplayHardware::BUFFER_PRESERVED))
850 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700851 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
852 // takes a rectangle, we must make sure to update that whole
853 // rectangle in that case
854 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700855 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 mDirtyRegion.set(mInvalidRegion.bounds());
858 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // in the BUFFER_PRESERVED case, obviously, we can update only
860 // what's needed and nothing more.
861 // NOTE: this is NOT a common case, as preserving the backbuffer
862 // is costly and usually involves copying the whole update back.
863 }
864 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700865 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700866 // We need to redraw the rectangle that will be updated
867 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700868 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700869 // rectangle instead of a region (see DisplayHardware::flip())
870 mDirtyRegion.set(mInvalidRegion.bounds());
871 } else {
872 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 mDirtyRegion.set(hw.bounds());
874 mInvalidRegion = mDirtyRegion;
875 }
876 }
877
878 // compose all surfaces
879 composeSurfaces(mDirtyRegion);
880
881 // clear the dirty regions
882 mDirtyRegion.clear();
883}
884
885void SurfaceFlinger::composeSurfaces(const Region& dirty)
886{
887 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
888 // should never happen unless the window manager has a bug
889 // draw something...
890 drawWormhole();
891 }
892 const SurfaceFlinger& flinger(*this);
893 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
894 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700895 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700897 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 const Region& visibleRegion(layer->visibleRegionScreen);
899 if (!visibleRegion.isEmpty()) {
900 const Region clip(dirty.intersect(visibleRegion));
901 if (!clip.isEmpty()) {
902 layer->draw(clip);
903 }
904 }
905 }
906}
907
908void SurfaceFlinger::unlockClients()
909{
910 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
911 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700912 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700914 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 layer->finishPageFlip();
916 }
917}
918
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919void SurfaceFlinger::debugFlashRegions()
920{
Mathias Agopian0926f502009-05-04 14:17:04 -0700921 const DisplayHardware& hw(graphicPlane(0).displayHardware());
922 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700923
924 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
925 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700926 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopian0926f502009-05-04 14:17:04 -0700927 mDirtyRegion.bounds() : hw.bounds());
928 composeSurfaces(repaint);
929 }
930
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 glDisable(GL_TEXTURE_2D);
932 glDisable(GL_BLEND);
933 glDisable(GL_DITHER);
934 glDisable(GL_SCISSOR_TEST);
935
Mathias Agopian0926f502009-05-04 14:17:04 -0700936 static int toggle = 0;
937 toggle = 1 - toggle;
938 if (toggle) {
939 glColor4x(0x10000, 0, 0x10000, 0x10000);
940 } else {
941 glColor4x(0x10000, 0x10000, 0, 0x10000);
942 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943
Mathias Agopian20f68782009-05-11 00:03:41 -0700944 Region::const_iterator it = mDirtyRegion.begin();
945 Region::const_iterator const end = mDirtyRegion.end();
946 while (it != end) {
947 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 GLfloat vertices[][2] = {
949 { r.left, r.top },
950 { r.left, r.bottom },
951 { r.right, r.bottom },
952 { r.right, r.top }
953 };
954 glVertexPointer(2, GL_FLOAT, 0, vertices);
955 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
956 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700957
Mathias Agopianb8a55602009-06-26 19:06:36 -0700958 if (mInvalidRegion.isEmpty()) {
959 mDirtyRegion.dump("mDirtyRegion");
960 mInvalidRegion.dump("mInvalidRegion");
961 }
962 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963
964 if (mDebugRegion > 1)
965 usleep(mDebugRegion * 1000);
966
967 glEnable(GL_SCISSOR_TEST);
968 //mDirtyRegion.dump("mDirtyRegion");
969}
970
971void SurfaceFlinger::drawWormhole() const
972{
973 const Region region(mWormholeRegion.intersect(mDirtyRegion));
974 if (region.isEmpty())
975 return;
976
977 const DisplayHardware& hw(graphicPlane(0).displayHardware());
978 const int32_t width = hw.getWidth();
979 const int32_t height = hw.getHeight();
980
981 glDisable(GL_BLEND);
982 glDisable(GL_DITHER);
983
984 if (LIKELY(!mDebugBackground)) {
985 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700986 Region::const_iterator it = region.begin();
987 Region::const_iterator const end = region.end();
988 while (it != end) {
989 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 const GLint sy = height - (r.top + r.height());
991 glScissor(r.left, sy, r.width(), r.height());
992 glClear(GL_COLOR_BUFFER_BIT);
993 }
994 } else {
995 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
996 { width, height }, { 0, height } };
997 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
998 glVertexPointer(2, GL_SHORT, 0, vertices);
999 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1000 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1001 glEnable(GL_TEXTURE_2D);
1002 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1003 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1004 glMatrixMode(GL_TEXTURE);
1005 glLoadIdentity();
1006 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001007 Region::const_iterator it = region.begin();
1008 Region::const_iterator const end = region.end();
1009 while (it != end) {
1010 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 const GLint sy = height - (r.top + r.height());
1012 glScissor(r.left, sy, r.width(), r.height());
1013 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1014 }
1015 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1016 }
1017}
1018
1019void SurfaceFlinger::debugShowFPS() const
1020{
1021 static int mFrameCount;
1022 static int mLastFrameCount = 0;
1023 static nsecs_t mLastFpsTime = 0;
1024 static float mFps = 0;
1025 mFrameCount++;
1026 nsecs_t now = systemTime();
1027 nsecs_t diff = now - mLastFpsTime;
1028 if (diff > ms2ns(250)) {
1029 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1030 mLastFpsTime = now;
1031 mLastFrameCount = mFrameCount;
1032 }
1033 // XXX: mFPS has the value we want
1034 }
1035
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001036status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037{
1038 Mutex::Autolock _l(mStateLock);
1039 addLayer_l(layer);
1040 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1041 return NO_ERROR;
1042}
1043
Mathias Agopian96f08192010-06-02 23:28:45 -07001044status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1045{
1046 ssize_t i = mCurrentState.layersSortedByZ.add(
1047 layer, &LayerBase::compareCurrentStateZ);
1048 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1049}
1050
1051ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1052 const sp<LayerBaseClient>& lbc)
1053{
1054 Mutex::Autolock _l(mStateLock);
1055
1056 // attach this layer to the client
1057 ssize_t name = client->attachLayer(lbc);
1058
1059 // add this layer to the current state list
1060 addLayer_l(lbc);
1061
1062 return name;
1063}
1064
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001065status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066{
1067 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001068 status_t err = purgatorizeLayer_l(layer);
1069 if (err == NO_ERROR)
1070 setTransactionFlags(eTransactionNeeded);
1071 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072}
1073
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001076 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1077 if (lbc != 0) {
1078 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1079 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1081 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001082 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 return NO_ERROR;
1084 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001085 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086}
1087
Mathias Agopian9a112062009-04-17 19:36:26 -07001088status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1089{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001090 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001091 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001092
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001093 layerBase->onRemoved();
1094
Mathias Agopian3d579642009-06-04 18:46:21 -07001095 // it's possible that we don't find a layer, because it might
1096 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001097 // from the user because there is a race between Client::destroySurface(),
1098 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001099 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1100}
1101
Mathias Agopian96f08192010-06-02 23:28:45 -07001102status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103{
Mathias Agopian96f08192010-06-02 23:28:45 -07001104 layer->forceVisibilityTransaction();
1105 setTransactionFlags(eTraversalNeeded);
1106 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107}
1108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1110{
1111 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1112}
1113
Mathias Agopianbb641242010-05-18 17:06:55 -07001114uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115{
1116 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1117 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001118 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 }
1120 return old;
1121}
1122
1123void SurfaceFlinger::openGlobalTransaction()
1124{
1125 android_atomic_inc(&mTransactionCount);
1126}
1127
1128void SurfaceFlinger::closeGlobalTransaction()
1129{
1130 if (android_atomic_dec(&mTransactionCount) == 1) {
1131 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001132
1133 // if there is a transaction with a resize, wait for it to
1134 // take effect before returning.
1135 Mutex::Autolock _l(mStateLock);
1136 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001137 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1138 if (CC_UNLIKELY(err != NO_ERROR)) {
1139 // just in case something goes wrong in SF, return to the
1140 // called after a few seconds.
1141 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1142 mResizeTransationPending = false;
1143 break;
1144 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 }
1147}
1148
1149status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1150{
1151 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1152 return BAD_VALUE;
1153
1154 Mutex::Autolock _l(mStateLock);
1155 mCurrentState.freezeDisplay = 1;
1156 setTransactionFlags(eTransactionNeeded);
1157
1158 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001159 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 return NO_ERROR;
1161}
1162
1163status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1164{
1165 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1166 return BAD_VALUE;
1167
1168 Mutex::Autolock _l(mStateLock);
1169 mCurrentState.freezeDisplay = 0;
1170 setTransactionFlags(eTransactionNeeded);
1171
1172 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001173 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174 return NO_ERROR;
1175}
1176
Mathias Agopianc08731e2009-03-27 18:11:38 -07001177int SurfaceFlinger::setOrientation(DisplayID dpy,
1178 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179{
1180 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1181 return BAD_VALUE;
1182
1183 Mutex::Autolock _l(mStateLock);
1184 if (mCurrentState.orientation != orientation) {
1185 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001186 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 mCurrentState.orientation = orientation;
1188 setTransactionFlags(eTransactionNeeded);
1189 mTransactionCV.wait(mStateLock);
1190 } else {
1191 orientation = BAD_VALUE;
1192 }
1193 }
1194 return orientation;
1195}
1196
Mathias Agopian96f08192010-06-02 23:28:45 -07001197sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001198 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1200 uint32_t flags)
1201{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001202 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001204
1205 if (int32_t(w|h) < 0) {
1206 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1207 int(w), int(h));
1208 return surfaceHandle;
1209 }
1210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 switch (flags & eFXSurfaceMask) {
1214 case eFXSurfaceNormal:
1215 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001216 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001218 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1219 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 }
1221 break;
1222 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 break;
1225 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001226 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 break;
1228 }
1229
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001230 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001232 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001233 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001236 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001237 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001238 params->identity = surfaceHandle->getIdentity();
1239 params->width = w;
1240 params->height = h;
1241 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001242 if (normalLayer != 0) {
1243 Mutex::Autolock _l(mStateLock);
1244 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1245 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001246 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001247
Mathias Agopian96f08192010-06-02 23:28:45 -07001248 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 }
1250
1251 return surfaceHandle;
1252}
1253
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001254sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001255 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001256 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001257 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258{
1259 // initialize the surfaces
1260 switch (format) { // TODO: take h/w into account
1261 case PIXEL_FORMAT_TRANSPARENT:
1262 case PIXEL_FORMAT_TRANSLUCENT:
1263 format = PIXEL_FORMAT_RGBA_8888;
1264 break;
1265 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001266 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 break;
1268 }
1269
Mathias Agopian96f08192010-06-02 23:28:45 -07001270 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001271 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001272 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 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 Agopianb7e930d2010-06-01 15:12:58 -07001279sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001280 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001281 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282{
Mathias Agopian96f08192010-06-02 23:28:45 -07001283 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 return layer;
1286}
1287
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001288sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001289 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001290 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291{
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 return layer;
1295}
1296
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001297sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001298 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300{
Mathias Agopian96f08192010-06-02 23:28:45 -07001301 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 return layer;
1304}
1305
Mathias Agopian96f08192010-06-02 23:28:45 -07001306status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307{
Mathias Agopian9a112062009-04-17 19:36:26 -07001308 /*
1309 * called by the window manager, when a surface should be marked for
1310 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001311 *
1312 * The surface is removed from the current and drawing lists, but placed
1313 * in the purgatory queue, so it's not destroyed right-away (we need
1314 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001315 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001316
Mathias Agopian48d819a2009-09-10 19:41:18 -07001317 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001318 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001319 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001320 if (layer != 0) {
1321 err = purgatorizeLayer_l(layer);
1322 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001323 setTransactionFlags(eTransactionNeeded);
1324 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001325 }
1326 return err;
1327}
1328
1329status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1330{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001331 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001332
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001333 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001334 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001335 sp<LayerBaseClient> layer;
1336 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001337 MessageDestroySurface(
1338 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1339 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001340 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001341 sp<LayerBaseClient> l(layer);
1342 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001344 /*
1345 * remove the layer from the current list -- chances are that it's
1346 * not in the list anyway, because it should have been removed
1347 * already upon request of the client (eg: window manager).
1348 * However, a buggy client could have not done that.
1349 * Since we know we don't have any more clients, we don't need
1350 * to use the purgatory.
1351 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001352 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001353 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1354 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001355 return true;
1356 }
1357 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001358
Mathias Agopianbb641242010-05-18 17:06:55 -07001359 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 return NO_ERROR;
1361}
1362
1363status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001364 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 int32_t count,
1366 const layer_state_t* states)
1367{
1368 Mutex::Autolock _l(mStateLock);
1369 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001371 const layer_state_t& s(states[i]);
1372 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001373 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375 if (what & ePositionChanged) {
1376 if (layer->setPosition(s.x, s.y))
1377 flags |= eTraversalNeeded;
1378 }
1379 if (what & eLayerChanged) {
1380 if (layer->setLayer(s.z)) {
1381 mCurrentState.layersSortedByZ.reorder(
1382 layer, &Layer::compareCurrentStateZ);
1383 // we need traversal (state changed)
1384 // AND transaction (list changed)
1385 flags |= eTransactionNeeded|eTraversalNeeded;
1386 }
1387 }
1388 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001389 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001391 mResizeTransationPending = true;
1392 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 }
1394 if (what & eAlphaChanged) {
1395 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1396 flags |= eTraversalNeeded;
1397 }
1398 if (what & eMatrixChanged) {
1399 if (layer->setMatrix(s.matrix))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eTransparentRegionChanged) {
1403 if (layer->setTransparentRegionHint(s.transparentRegion))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eVisibilityChanged) {
1407 if (layer->setFlags(s.flags, s.mask))
1408 flags |= eTraversalNeeded;
1409 }
1410 }
1411 }
1412 if (flags) {
1413 setTransactionFlags(flags);
1414 }
1415 return NO_ERROR;
1416}
1417
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418void SurfaceFlinger::screenReleased(int dpy)
1419{
1420 // this may be called by a signal handler, we can't do too much in here
1421 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1422 signalEvent();
1423}
1424
1425void SurfaceFlinger::screenAcquired(int dpy)
1426{
1427 // this may be called by a signal handler, we can't do too much in here
1428 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1429 signalEvent();
1430}
1431
1432status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1433{
1434 const size_t SIZE = 1024;
1435 char buffer[SIZE];
1436 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001437 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 snprintf(buffer, SIZE, "Permission Denial: "
1439 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1440 IPCThreadState::self()->getCallingPid(),
1441 IPCThreadState::self()->getCallingUid());
1442 result.append(buffer);
1443 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001444
1445 // figure out if we're stuck somewhere
1446 const nsecs_t now = systemTime();
1447 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1448 const nsecs_t inTransaction(mDebugInTransaction);
1449 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1450 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1451
1452 // Try to get the main lock, but don't insist if we can't
1453 // (this would indicate SF is stuck, but we want to be able to
1454 // print something in dumpsys).
1455 int retry = 3;
1456 while (mStateLock.tryLock()<0 && --retry>=0) {
1457 usleep(1000000);
1458 }
1459 const bool locked(retry >= 0);
1460 if (!locked) {
1461 snprintf(buffer, SIZE,
1462 "SurfaceFlinger appears to be unresponsive, "
1463 "dumping anyways (no locks held)\n");
1464 result.append(buffer);
1465 }
1466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1468 const size_t count = currentLayers.size();
1469 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001470 const sp<LayerBase>& layer(currentLayers[i]);
1471 layer->dump(result, buffer, SIZE);
1472 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 s.transparentRegion.dump(result, "transparentRegion");
1474 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1475 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1476 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 mWormholeRegion.dump(result, "WormholeRegion");
1479 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1480 snprintf(buffer, SIZE,
1481 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1482 mFreezeDisplay?"yes":"no", mFreezeCount,
1483 mCurrentState.orientation, hw.canDraw());
1484 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001485 snprintf(buffer, SIZE,
1486 " last eglSwapBuffers() time: %f us\n"
1487 " last transaction time : %f us\n",
1488 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1489 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001490
Mathias Agopian9795c422009-08-26 16:36:26 -07001491 if (inSwapBuffersDuration || !locked) {
1492 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1493 inSwapBuffersDuration/1000.0);
1494 result.append(buffer);
1495 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001496
Mathias Agopian9795c422009-08-26 16:36:26 -07001497 if (inTransactionDuration || !locked) {
1498 snprintf(buffer, SIZE, " transaction time: %f us\n",
1499 inTransactionDuration/1000.0);
1500 result.append(buffer);
1501 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001502
Mathias Agopian3330b202009-10-05 17:07:12 -07001503 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001504 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001505
1506 if (locked) {
1507 mStateLock.unlock();
1508 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 }
1510 write(fd, result.string(), result.size());
1511 return NO_ERROR;
1512}
1513
1514status_t SurfaceFlinger::onTransact(
1515 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1516{
1517 switch (code) {
1518 case CREATE_CONNECTION:
1519 case OPEN_GLOBAL_TRANSACTION:
1520 case CLOSE_GLOBAL_TRANSACTION:
1521 case SET_ORIENTATION:
1522 case FREEZE_DISPLAY:
1523 case UNFREEZE_DISPLAY:
1524 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 {
1526 // codes that require permission check
1527 IPCThreadState* ipc = IPCThreadState::self();
1528 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001529 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001530 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1531 LOGE("Permission Denial: "
1532 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1533 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534 }
1535 }
1536 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1538 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001539 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001540 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1541 IPCThreadState* ipc = IPCThreadState::self();
1542 const int pid = ipc->getCallingPid();
1543 const int uid = ipc->getCallingUid();
1544 LOGE("Permission Denial: "
1545 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 return PERMISSION_DENIED;
1547 }
1548 int n;
1549 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001550 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001552 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 return NO_ERROR;
1554 case 1002: // SHOW_UPDATES
1555 n = data.readInt32();
1556 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1557 return NO_ERROR;
1558 case 1003: // SHOW_BACKGROUND
1559 n = data.readInt32();
1560 mDebugBackground = n ? 1 : 0;
1561 return NO_ERROR;
1562 case 1004:{ // repaint everything
1563 Mutex::Autolock _l(mStateLock);
1564 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1565 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1566 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001567 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001569 case 1005:{ // force transaction
1570 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1571 return NO_ERROR;
1572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 case 1007: // set mFreezeCount
1574 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001575 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 return NO_ERROR;
1577 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001578 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579 reply->writeInt32(0);
1580 reply->writeInt32(mDebugRegion);
1581 reply->writeInt32(mDebugBackground);
1582 return NO_ERROR;
1583 case 1013: {
1584 Mutex::Autolock _l(mStateLock);
1585 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1586 reply->writeInt32(hw.getPageFlipCount());
1587 }
1588 return NO_ERROR;
1589 }
1590 }
1591 return err;
1592}
1593
1594// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001595
1596sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1597{
1598 sp<Layer> result;
1599 Mutex::Autolock _l(mStateLock);
1600 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1601 return result;
1602}
1603
1604// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605
Mathias Agopian96f08192010-06-02 23:28:45 -07001606Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001607 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609}
1610
Mathias Agopian96f08192010-06-02 23:28:45 -07001611Client::~Client()
1612{
Mathias Agopian96f08192010-06-02 23:28:45 -07001613 const size_t count = mLayers.size();
1614 for (size_t i=0 ; i<count ; i++) {
1615 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1616 if (layer != 0) {
1617 mFlinger->removeLayer(layer);
1618 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 }
1620}
1621
Mathias Agopian96f08192010-06-02 23:28:45 -07001622status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001623 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001625
Mathias Agopian96f08192010-06-02 23:28:45 -07001626ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1627{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001628 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001629 mLayers.add(name, layer);
1630 return name;
1631}
1632
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001633void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001634{
Mathias Agopian96f08192010-06-02 23:28:45 -07001635 // we do a linear search here, because this doesn't happen often
1636 const size_t count = mLayers.size();
1637 for (size_t i=0 ; i<count ; i++) {
1638 if (mLayers.valueAt(i) == layer) {
1639 mLayers.removeItemsAt(i, 1);
1640 break;
1641 }
1642 }
1643}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001644sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1645 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001646 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1647 if (layer != 0) {
1648 lbc = layer.promote();
1649 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001650 }
1651 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652}
1653
Mathias Agopian96f08192010-06-02 23:28:45 -07001654sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001655 return 0;
1656}
1657ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1658 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659}
Mathias Agopian96f08192010-06-02 23:28:45 -07001660sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001661 ISurfaceComposerClient::surface_data_t* params, int pid,
1662 const String8& name,
1663 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 uint32_t flags)
1665{
Mathias Agopian96f08192010-06-02 23:28:45 -07001666 return mFlinger->createSurface(this, pid, name, params,
1667 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668}
Mathias Agopian96f08192010-06-02 23:28:45 -07001669status_t Client::destroySurface(SurfaceID sid) {
1670 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671}
Mathias Agopian96f08192010-06-02 23:28:45 -07001672status_t Client::setState(int32_t count, const layer_state_t* states) {
1673 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674}
1675
1676// ---------------------------------------------------------------------------
1677
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001678UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1679 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1680{
1681 const int pgsize = getpagesize();
1682 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1683
1684 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1685 "SurfaceFlinger Client control-block");
1686
1687 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1688 if (ctrlblk) { // construct the shared structure in-place.
1689 new(ctrlblk) SharedClient;
1690 }
1691}
1692
1693UserClient::~UserClient()
1694{
1695 if (ctrlblk) {
1696 ctrlblk->~SharedClient(); // destroy our shared-structure.
1697 }
1698
1699 /*
1700 * When a UserClient dies, it's unclear what to do exactly.
1701 * We could go ahead and destroy all surfaces linked to that client
1702 * however, it wouldn't be fair to the main Client
1703 * (usually the the window-manager), which might want to re-target
1704 * the layer to another UserClient.
1705 * I think the best is to do nothing, or not much; in most cases the
1706 * WM itself will go ahead and clean things up when it detects a client of
1707 * his has died.
1708 * The remaining question is what to display? currently we keep
1709 * just keep the current buffer.
1710 */
1711}
1712
1713status_t UserClient::initCheck() const {
1714 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1715}
1716
1717void UserClient::detachLayer(const Layer* layer)
1718{
1719 int32_t name = layer->getToken();
1720 if (name >= 0) {
1721 android_atomic_and(~(1LU<<name), &mBitmap);
1722 }
1723}
1724
1725sp<IMemoryHeap> UserClient::getControlBlock() const {
1726 return mCblkHeap;
1727}
1728
1729ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1730{
1731 int32_t name = NAME_NOT_FOUND;
1732 sp<Layer> layer(mFlinger->getLayer(sur));
1733 if (layer == 0) return name;
1734
1735 // this layer already has a token, just return it
1736 // FIXME: we should check that this token is for the same client
1737 name = layer->getToken();
1738 if (name >= 0) return name;
1739
1740 name = 0;
1741 do {
1742 int32_t mask = 1LU<<name;
1743 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1744 // we found and locked that name
1745 layer->setToken(const_cast<UserClient*>(this), ctrlblk, name);
1746 break;
1747 }
1748 if (++name > 31)
1749 name = NO_MEMORY;
1750 } while(name >= 0);
1751
Mathias Agopian53503a92010-06-08 15:40:56 -07001752 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1753 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001754 return name;
1755}
1756
1757sp<ISurface> UserClient::createSurface(
1758 ISurfaceComposerClient::surface_data_t* params, int pid,
1759 const String8& name,
1760 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1761 uint32_t flags) {
1762 return 0;
1763}
1764status_t UserClient::destroySurface(SurfaceID sid) {
1765 return INVALID_OPERATION;
1766}
1767status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1768 return INVALID_OPERATION;
1769}
1770
1771// ---------------------------------------------------------------------------
1772
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773GraphicPlane::GraphicPlane()
1774 : mHw(0)
1775{
1776}
1777
1778GraphicPlane::~GraphicPlane() {
1779 delete mHw;
1780}
1781
1782bool GraphicPlane::initialized() const {
1783 return mHw ? true : false;
1784}
1785
Mathias Agopian2b92d892010-02-08 15:49:35 -08001786int GraphicPlane::getWidth() const {
1787 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788}
1789
Mathias Agopian2b92d892010-02-08 15:49:35 -08001790int GraphicPlane::getHeight() const {
1791 return mHeight;
1792}
1793
1794void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1795{
1796 mHw = hw;
1797
1798 // initialize the display orientation transform.
1799 // it's a constant that should come from the display driver.
1800 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1801 char property[PROPERTY_VALUE_MAX];
1802 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1803 //displayOrientation
1804 switch (atoi(property)) {
1805 case 90:
1806 displayOrientation = ISurfaceComposer::eOrientation90;
1807 break;
1808 case 270:
1809 displayOrientation = ISurfaceComposer::eOrientation270;
1810 break;
1811 }
1812 }
1813
1814 const float w = hw->getWidth();
1815 const float h = hw->getHeight();
1816 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1817 &mDisplayTransform);
1818 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1819 mDisplayWidth = h;
1820 mDisplayHeight = w;
1821 } else {
1822 mDisplayWidth = w;
1823 mDisplayHeight = h;
1824 }
1825
1826 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001827}
1828
1829status_t GraphicPlane::orientationToTransfrom(
1830 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001831{
1832 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833 switch (orientation) {
1834 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001835 flags = Transform::ROT_0;
1836 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001837 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001838 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001839 break;
1840 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001841 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842 break;
1843 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001844 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001845 break;
1846 default:
1847 return BAD_VALUE;
1848 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001849 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001850 return NO_ERROR;
1851}
1852
1853status_t GraphicPlane::setOrientation(int orientation)
1854{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855 // If the rotation can be handled in hardware, this is where
1856 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001857
1858 const DisplayHardware& hw(displayHardware());
1859 const float w = mDisplayWidth;
1860 const float h = mDisplayHeight;
1861 mWidth = int(w);
1862 mHeight = int(h);
1863
1864 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001865 GraphicPlane::orientationToTransfrom(orientation, w, h,
1866 &orientationTransform);
1867 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1868 mWidth = int(h);
1869 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001871
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001872 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001873 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874 return NO_ERROR;
1875}
1876
1877const DisplayHardware& GraphicPlane::displayHardware() const {
1878 return *mHw;
1879}
1880
1881const Transform& GraphicPlane::transform() const {
1882 return mGlobalTransform;
1883}
1884
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001885EGLDisplay GraphicPlane::getEGLDisplay() const {
1886 return mHw->getEGLDisplay();
1887}
1888
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889// ---------------------------------------------------------------------------
1890
1891}; // namespace android