blob: dc241d4e760ac045dae01ce261116f2a55eaa92e [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>
Mathias Agopian35b48d12010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
50#include "LayerBlur.h"
51#include "LayerBuffer.h"
52#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopiana1ecca92009-05-21 19:21:59 -070057/* ideally AID_GRAPHICS would be in a semi-public header
58 * or there would be a way to map a user/group name to its id
59 */
60#ifndef AID_GRAPHICS
61#define AID_GRAPHICS 1003
62#endif
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#define DISPLAY_COUNT 1
65
66namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067// ---------------------------------------------------------------------------
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069SurfaceFlinger::SurfaceFlinger()
70 : BnSurfaceComposer(), Thread(false),
71 mTransactionFlags(0),
72 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070073 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070076 mHardwareTest("android.permission.HARDWARE_TEST"),
77 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
78 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 mVisibleRegionsDirty(false),
80 mDeferReleaseConsole(false),
81 mFreezeDisplay(false),
82 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070083 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070086 mDebugInSwapBuffers(0),
87 mLastSwapBufferTime(0),
88 mDebugInTransaction(0),
89 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070090 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mConsoleSignals(0),
92 mSecureFrameBuffer(0)
93{
94 init();
95}
96
97void SurfaceFlinger::init()
98{
99 LOGI("SurfaceFlinger is starting");
100
101 // debugging stuff...
102 char value[PROPERTY_VALUE_MAX];
103 property_get("debug.sf.showupdates", value, "0");
104 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showbackground", value, "0");
106 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
Mathias Agopian78fd5012010-04-20 14:51:04 -0700108 LOGI_IF(mDebugRegion, "showupdates enabled");
109 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110}
111
112SurfaceFlinger::~SurfaceFlinger()
113{
114 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115}
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
118{
119 return graphicPlane(0).displayHardware().getOverlayEngine();
120}
121
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700122sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700124 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125}
126
Mathias Agopian7e27f052010-05-28 14:22:23 -0700127sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128{
Mathias Agopian96f08192010-06-02 23:28:45 -0700129 sp<ISurfaceComposerClient> bclient;
130 sp<Client> client(new Client(this));
131 status_t err = client->initCheck();
132 if (err == NO_ERROR) {
133 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 return bclient;
136}
137
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700138sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
139{
140 sp<ISurfaceComposerClient> bclient;
141 sp<UserClient> client(new UserClient(this));
142 status_t err = client->initCheck();
143 if (err == NO_ERROR) {
144 bclient = client;
145 }
146 return bclient;
147}
148
149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
151{
152 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
153 const GraphicPlane& plane(mGraphicPlanes[dpy]);
154 return plane;
155}
156
157GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
158{
159 return const_cast<GraphicPlane&>(
160 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
161}
162
163void SurfaceFlinger::bootFinished()
164{
165 const nsecs_t now = systemTime();
166 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700167 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700168 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700169 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170}
171
172void SurfaceFlinger::onFirstRef()
173{
174 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
175
176 // Wait for the main thread to be done with its initialization
177 mReadyToRunBarrier.wait();
178}
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180static inline uint16_t pack565(int r, int g, int b) {
181 return (r<<11)|(g<<5)|b;
182}
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184status_t SurfaceFlinger::readyToRun()
185{
186 LOGI( "SurfaceFlinger's main thread ready to run. "
187 "Initializing graphics H/W...");
188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 // we only support one display currently
190 int dpy = 0;
191
192 {
193 // initialize the main display
194 GraphicPlane& plane(graphicPlane(dpy));
195 DisplayHardware* const hw = new DisplayHardware(this, dpy);
196 plane.setDisplayHardware(hw);
197 }
198
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700199 // create the shared control-block
200 mServerHeap = new MemoryHeapBase(4096,
201 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
202 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
203
204 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
205 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
206
207 new(mServerCblk) surface_flinger_cblk_t;
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 // initialize primary screen
210 // (other display should be initialized in the same manner, but
211 // asynchronously, as they could come and go. None of this is supported
212 // yet).
213 const GraphicPlane& plane(graphicPlane(dpy));
214 const DisplayHardware& hw = plane.displayHardware();
215 const uint32_t w = hw.getWidth();
216 const uint32_t h = hw.getHeight();
217 const uint32_t f = hw.getFormat();
218 hw.makeCurrent();
219
220 // initialize the shared control block
221 mServerCblk->connected |= 1<<dpy;
222 display_cblk_t* dcblk = mServerCblk->displays + dpy;
223 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800224 dcblk->w = plane.getWidth();
225 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 dcblk->format = f;
227 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
228 dcblk->xdpi = hw.getDpiX();
229 dcblk->ydpi = hw.getDpiY();
230 dcblk->fps = hw.getRefreshRate();
231 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232
233 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
235 glPixelStorei(GL_PACK_ALIGNMENT, 4);
236 glEnableClientState(GL_VERTEX_ARRAY);
237 glEnable(GL_SCISSOR_TEST);
238 glShadeModel(GL_FLAT);
239 glDisable(GL_DITHER);
240 glDisable(GL_CULL_FACE);
241
242 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
243 const uint16_t g1 = pack565(0x17,0x2f,0x17);
244 const uint16_t textureData[4] = { g0, g1, g1, g0 };
245 glGenTextures(1, &mWormholeTexName);
246 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
247 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
248 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
249 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
250 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
251 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
252 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
253
254 glViewport(0, 0, w, h);
255 glMatrixMode(GL_PROJECTION);
256 glLoadIdentity();
257 glOrthof(0, w, h, 0, 0, 1);
258
259 LayerDim::initDimmer(this, w, h);
260
261 mReadyToRunBarrier.open();
262
263 /*
264 * We're now ready to accept clients...
265 */
266
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700267 // start boot animation
268 property_set("ctl.start", "bootanim");
269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 return NO_ERROR;
271}
272
273// ----------------------------------------------------------------------------
274#if 0
275#pragma mark -
276#pragma mark Events Handler
277#endif
278
279void SurfaceFlinger::waitForEvent()
280{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700281 while (true) {
282 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800283 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700284 if (UNLIKELY(isFrozen())) {
285 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700286 const nsecs_t now = systemTime();
287 if (mFreezeDisplayTime == 0) {
288 mFreezeDisplayTime = now;
289 }
290 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
291 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700292 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700293
Mathias Agopianbb641242010-05-18 17:06:55 -0700294 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800295
296 // see if we timed out
297 if (isFrozen()) {
298 const nsecs_t now = systemTime();
299 nsecs_t frozenTime = (now - mFreezeDisplayTime);
300 if (frozenTime >= freezeDisplayTimeout) {
301 // we timed out and are still frozen
302 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
303 mFreezeDisplay, mFreezeCount);
304 mFreezeDisplayTime = 0;
305 mFreezeCount = 0;
306 mFreezeDisplay = false;
307 }
308 }
309
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700310 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700311 switch (msg->what) {
312 case MessageQueue::INVALIDATE:
313 // invalidate message, just return to the main loop
314 return;
315 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 }
318}
319
320void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700321 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322}
323
324void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700325 // this is the IPC call
326 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327}
328
Mathias Agopianbb641242010-05-18 17:06:55 -0700329status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
330 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331{
Mathias Agopianbb641242010-05-18 17:06:55 -0700332 return mEventQueue.postMessage(msg, reltime, flags);
333}
334
335status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
336 nsecs_t reltime, uint32_t flags)
337{
338 status_t res = mEventQueue.postMessage(msg, reltime, flags);
339 if (res == NO_ERROR) {
340 msg->wait();
341 }
342 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343}
344
345// ----------------------------------------------------------------------------
346#if 0
347#pragma mark -
348#pragma mark Main loop
349#endif
350
351bool SurfaceFlinger::threadLoop()
352{
353 waitForEvent();
354
355 // check for transactions
356 if (UNLIKELY(mConsoleSignals)) {
357 handleConsoleEvents();
358 }
359
360 if (LIKELY(mTransactionCount == 0)) {
361 // if we're in a global transaction, don't do anything.
362 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
363 uint32_t transactionFlags = getTransactionFlags(mask);
364 if (LIKELY(transactionFlags)) {
365 handleTransaction(transactionFlags);
366 }
367 }
368
369 // post surfaces (if needed)
370 handlePageFlip();
371
372 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700373 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700375
376 const int index = hw.getCurrentBufferIndex();
377 GraphicLog& logger(GraphicLog::getInstance());
378
379 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 handleRepaint();
381
Mathias Agopian74faca22009-09-17 16:18:16 -0700382 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700383 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700384 hw.compositionComplete();
385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 // release the clients before we flip ('cause flip might block)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700387 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389
Mathias Agopian35b48d12010-09-13 22:57:58 -0700390 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 postFramebuffer();
Mathias Agopian35b48d12010-09-13 22:57:58 -0700392
393 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 } else {
395 // pretend we did the post
396 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 usleep(16667); // 60 fps period
398 }
399 return true;
400}
401
402void SurfaceFlinger::postFramebuffer()
403{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 if (!mInvalidRegion.isEmpty()) {
405 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700406 const nsecs_t now = systemTime();
407 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700409 mLastSwapBufferTime = systemTime() - now;
410 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 }
413}
414
415void SurfaceFlinger::handleConsoleEvents()
416{
417 // something to do with the console
418 const DisplayHardware& hw = graphicPlane(0).displayHardware();
419
420 int what = android_atomic_and(0, &mConsoleSignals);
421 if (what & eConsoleAcquired) {
422 hw.acquireScreen();
423 }
424
425 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700426 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 hw.releaseScreen();
429 }
430
431 if (what & eConsoleReleased) {
432 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 hw.releaseScreen();
434 } else {
435 mDeferReleaseConsole = true;
436 }
437 }
438
439 mDirtyRegion.set(hw.bounds());
440}
441
442void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
443{
Mathias Agopian3d579642009-06-04 18:46:21 -0700444 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445
Mathias Agopian4da75192010-08-10 17:19:56 -0700446 /*
447 * Perform and commit the transaction
448 */
449
Mathias Agopian3d579642009-06-04 18:46:21 -0700450 { // scope for the lock
451 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700452 const nsecs_t now = systemTime();
453 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700454 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700455 mLastTransactionTime = systemTime() - now;
456 mDebugInTransaction = 0;
Mathias Agopian4da75192010-08-10 17:19:56 -0700457 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700458 }
459
Mathias Agopian4da75192010-08-10 17:19:56 -0700460 /*
461 * Clean-up all layers that went away
462 * (do this without the lock held)
463 */
Mathias Agopian3d579642009-06-04 18:46:21 -0700464 const size_t count = ditchedLayers.size();
465 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700466 if (ditchedLayers[i] != 0) {
467 //LOGD("ditching layer %p", ditchedLayers[i].get());
468 ditchedLayers[i]->ditch();
469 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700470 }
471}
472
473void SurfaceFlinger::handleTransactionLocked(
474 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
475{
476 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477 const size_t count = currentLayers.size();
478
479 /*
480 * Traversal of the children
481 * (perform the transaction for each of them if needed)
482 */
483
484 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
485 if (layersNeedTransaction) {
486 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700487 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
489 if (!trFlags) continue;
490
491 const uint32_t flags = layer->doTransaction(0);
492 if (flags & Layer::eVisibleRegion)
493 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 }
495 }
496
497 /*
498 * Perform our own transaction if needed
499 */
500
501 if (transactionFlags & eTransactionNeeded) {
502 if (mCurrentState.orientation != mDrawingState.orientation) {
503 // the orientation has changed, recompute all visible regions
504 // and invalidate everything.
505
506 const int dpy = 0;
507 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700508 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 GraphicPlane& plane(graphicPlane(dpy));
510 plane.setOrientation(orientation);
511
512 // update the shared control block
513 const DisplayHardware& hw(plane.displayHardware());
514 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
515 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800516 dcblk->w = plane.getWidth();
517 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518
519 mVisibleRegionsDirty = true;
520 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521 }
522
523 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
524 // freezing or unfreezing the display -> trigger animation if needed
525 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800526 if (mFreezeDisplay)
527 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528 }
529
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700530 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
531 // layers have been added
532 mVisibleRegionsDirty = true;
533 }
534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 // some layers might have been removed, so
536 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700538 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700540 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700541 const size_t count = previousLayers.size();
542 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700543 const sp<LayerBase>& layer(previousLayers[i]);
544 if (currentLayers.indexOf( layer ) < 0) {
545 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700546 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700547 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700548 }
549 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 }
552
553 commitTransaction();
554}
555
556sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
557{
558 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
559}
560
561void SurfaceFlinger::computeVisibleRegions(
562 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
563{
564 const GraphicPlane& plane(graphicPlane(0));
565 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700566 const DisplayHardware& hw(plane.displayHardware());
567 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568
569 Region aboveOpaqueLayers;
570 Region aboveCoveredLayers;
571 Region dirty;
572
573 bool secureFrameBuffer = false;
574
575 size_t i = currentLayers.size();
576 while (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 layer->validateVisibility(planeTransform);
579
580 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700581 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582
Mathias Agopianab028732010-03-16 16:41:46 -0700583 /*
584 * opaqueRegion: area of a surface that is fully opaque.
585 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700587
588 /*
589 * visibleRegion: area of a surface that is visible on screen
590 * and not fully transparent. This is essentially the layer's
591 * footprint minus the opaque regions above it.
592 * Areas covered by a translucent surface are considered visible.
593 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700595
596 /*
597 * coveredRegion: area of a surface that is covered by all
598 * visible regions above it (which includes the translucent areas).
599 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700601
602
603 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700604 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700606 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700608 visibleRegion.andSelf(screenRegion);
609 if (!visibleRegion.isEmpty()) {
610 // Remove the transparent area from the visible region
611 if (translucent) {
612 visibleRegion.subtractSelf(layer->transparentRegionScreen);
613 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614
Mathias Agopianab028732010-03-16 16:41:46 -0700615 // compute the opaque region
616 const int32_t layerOrientation = layer->getOrientation();
617 if (s.alpha==255 && !translucent &&
618 ((layerOrientation & Transform::ROT_INVALID) == false)) {
619 // the opaque region is the layer's footprint
620 opaqueRegion = visibleRegion;
621 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 }
623 }
624
Mathias Agopianab028732010-03-16 16:41:46 -0700625 // Clip the covered region to the visible region
626 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
627
628 // Update aboveCoveredLayers for next (lower) layer
629 aboveCoveredLayers.orSelf(visibleRegion);
630
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 // subtract the opaque region covered by the layers above us
632 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633
634 // compute this layer's dirty region
635 if (layer->contentDirty) {
636 // we need to invalidate the whole region
637 dirty = visibleRegion;
638 // as well, as the old visible region
639 dirty.orSelf(layer->visibleRegionScreen);
640 layer->contentDirty = false;
641 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700642 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700643 * the exposed region consists of two components:
644 * 1) what's VISIBLE now and was COVERED before
645 * 2) what's EXPOSED now less what was EXPOSED before
646 *
647 * note that (1) is conservative, we start with the whole
648 * visible region but only keep what used to be covered by
649 * something -- which mean it may have been exposed.
650 *
651 * (2) handles areas that were not covered by anything but got
652 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700653 */
Mathias Agopianab028732010-03-16 16:41:46 -0700654 const Region newExposed = visibleRegion - coveredRegion;
655 const Region oldVisibleRegion = layer->visibleRegionScreen;
656 const Region oldCoveredRegion = layer->coveredRegionScreen;
657 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
658 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 }
660 dirty.subtractSelf(aboveOpaqueLayers);
661
662 // accumulate to the screen dirty region
663 dirtyRegion.orSelf(dirty);
664
Mathias Agopianab028732010-03-16 16:41:46 -0700665 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667
668 // Store the visible region is screen space
669 layer->setVisibleRegion(visibleRegion);
670 layer->setCoveredRegion(coveredRegion);
671
Mathias Agopian97011222009-07-28 10:57:27 -0700672 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 if (layer->isSecure() && !visibleRegion.isEmpty()) {
674 secureFrameBuffer = true;
675 }
676 }
677
Mathias Agopian97011222009-07-28 10:57:27 -0700678 // invalidate the areas where a layer was removed
679 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
680 mDirtyRegionRemovedLayer.clear();
681
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 mSecureFrameBuffer = secureFrameBuffer;
683 opaqueRegion = aboveOpaqueLayers;
684}
685
686
687void SurfaceFlinger::commitTransaction()
688{
689 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700690 mResizeTransationPending = false;
691 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692}
693
694void SurfaceFlinger::handlePageFlip()
695{
696 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700697 LayerVector& currentLayers = const_cast<LayerVector&>(
698 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 visibleRegions |= lockPageFlip(currentLayers);
700
701 const DisplayHardware& hw = graphicPlane(0).displayHardware();
702 const Region screenRegion(hw.bounds());
703 if (visibleRegions) {
704 Region opaqueRegion;
705 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700706
707 /*
708 * rebuild the visible layer list
709 */
710 mVisibleLayersSortedByZ.clear();
711 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
712 size_t count = currentLayers.size();
713 mVisibleLayersSortedByZ.setCapacity(count);
714 for (size_t i=0 ; i<count ; i++) {
715 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
716 mVisibleLayersSortedByZ.add(currentLayers[i]);
717 }
718
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 mWormholeRegion = screenRegion.subtract(opaqueRegion);
720 mVisibleRegionsDirty = false;
721 }
722
723 unlockPageFlip(currentLayers);
724 mDirtyRegion.andSelf(screenRegion);
725}
726
727bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
728{
729 bool recomputeVisibleRegions = false;
730 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700731 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700733 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 layer->lockPageFlip(recomputeVisibleRegions);
735 }
736 return recomputeVisibleRegions;
737}
738
739void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
740{
741 const GraphicPlane& plane(graphicPlane(0));
742 const Transform& planeTransform(plane.transform());
743 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700744 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700746 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 layer->unlockPageFlip(planeTransform, mDirtyRegion);
748 }
749}
750
Mathias Agopianb8a55602009-06-26 19:06:36 -0700751
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752void SurfaceFlinger::handleRepaint()
753{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700754 // compute the invalid region
755 mInvalidRegion.orSelf(mDirtyRegion);
756 if (mInvalidRegion.isEmpty()) {
757 // nothing to do
758 return;
759 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760
761 if (UNLIKELY(mDebugRegion)) {
762 debugFlashRegions();
763 }
764
Mathias Agopianb8a55602009-06-26 19:06:36 -0700765 // set the frame buffer
766 const DisplayHardware& hw(graphicPlane(0).displayHardware());
767 glMatrixMode(GL_MODELVIEW);
768 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769
770 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700771 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
772 (flags & DisplayHardware::BUFFER_PRESERVED))
773 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700774 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
775 // takes a rectangle, we must make sure to update that whole
776 // rectangle in that case
777 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700778 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700779 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 mDirtyRegion.set(mInvalidRegion.bounds());
781 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700782 // in the BUFFER_PRESERVED case, obviously, we can update only
783 // what's needed and nothing more.
784 // NOTE: this is NOT a common case, as preserving the backbuffer
785 // is costly and usually involves copying the whole update back.
786 }
787 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700788 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700789 // We need to redraw the rectangle that will be updated
790 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700791 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700792 // rectangle instead of a region (see DisplayHardware::flip())
793 mDirtyRegion.set(mInvalidRegion.bounds());
794 } else {
795 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 mDirtyRegion.set(hw.bounds());
797 mInvalidRegion = mDirtyRegion;
798 }
799 }
800
801 // compose all surfaces
802 composeSurfaces(mDirtyRegion);
803
804 // clear the dirty regions
805 mDirtyRegion.clear();
806}
807
808void SurfaceFlinger::composeSurfaces(const Region& dirty)
809{
810 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
811 // should never happen unless the window manager has a bug
812 // draw something...
813 drawWormhole();
814 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700815 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700816 const size_t count = layers.size();
817 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian45721772010-08-12 15:03:26 -0700818 const sp<LayerBase>& layer(layers[i]);
819 const Region clip(dirty.intersect(layer->visibleRegionScreen));
820 if (!clip.isEmpty()) {
821 layer->draw(clip);
822 }
823 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824}
825
826void SurfaceFlinger::unlockClients()
827{
828 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
829 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700830 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700832 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833 layer->finishPageFlip();
834 }
835}
836
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837void SurfaceFlinger::debugFlashRegions()
838{
Mathias Agopian0a917752010-06-14 21:20:00 -0700839 const DisplayHardware& hw(graphicPlane(0).displayHardware());
840 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700841
Mathias Agopian0a917752010-06-14 21:20:00 -0700842 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
843 (flags & DisplayHardware::BUFFER_PRESERVED))) {
844 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
845 mDirtyRegion.bounds() : hw.bounds());
846 composeSurfaces(repaint);
847 }
848
849 TextureManager::deactivateTextures();
850
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 glDisable(GL_BLEND);
852 glDisable(GL_DITHER);
853 glDisable(GL_SCISSOR_TEST);
854
Mathias Agopian0926f502009-05-04 14:17:04 -0700855 static int toggle = 0;
856 toggle = 1 - toggle;
857 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700858 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700859 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700860 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700861 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862
Mathias Agopian20f68782009-05-11 00:03:41 -0700863 Region::const_iterator it = mDirtyRegion.begin();
864 Region::const_iterator const end = mDirtyRegion.end();
865 while (it != end) {
866 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 GLfloat vertices[][2] = {
868 { r.left, r.top },
869 { r.left, r.bottom },
870 { r.right, r.bottom },
871 { r.right, r.top }
872 };
873 glVertexPointer(2, GL_FLOAT, 0, vertices);
874 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
875 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700876
Mathias Agopianb8a55602009-06-26 19:06:36 -0700877 if (mInvalidRegion.isEmpty()) {
878 mDirtyRegion.dump("mDirtyRegion");
879 mInvalidRegion.dump("mInvalidRegion");
880 }
881 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882
883 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700884 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885
886 glEnable(GL_SCISSOR_TEST);
887 //mDirtyRegion.dump("mDirtyRegion");
888}
889
890void SurfaceFlinger::drawWormhole() const
891{
892 const Region region(mWormholeRegion.intersect(mDirtyRegion));
893 if (region.isEmpty())
894 return;
895
896 const DisplayHardware& hw(graphicPlane(0).displayHardware());
897 const int32_t width = hw.getWidth();
898 const int32_t height = hw.getHeight();
899
900 glDisable(GL_BLEND);
901 glDisable(GL_DITHER);
902
903 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700904 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700905 Region::const_iterator it = region.begin();
906 Region::const_iterator const end = region.end();
907 while (it != end) {
908 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 const GLint sy = height - (r.top + r.height());
910 glScissor(r.left, sy, r.width(), r.height());
911 glClear(GL_COLOR_BUFFER_BIT);
912 }
913 } else {
914 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
915 { width, height }, { 0, height } };
916 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
917 glVertexPointer(2, GL_SHORT, 0, vertices);
918 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
919 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -0700920#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700921 if (GLExtensions::getInstance().haveTextureExternal()) {
922 glDisable(GL_TEXTURE_EXTERNAL_OES);
923 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700924#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 glEnable(GL_TEXTURE_2D);
926 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
927 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
928 glMatrixMode(GL_TEXTURE);
929 glLoadIdentity();
930 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700931 Region::const_iterator it = region.begin();
932 Region::const_iterator const end = region.end();
933 while (it != end) {
934 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 const GLint sy = height - (r.top + r.height());
936 glScissor(r.left, sy, r.width(), r.height());
937 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
938 }
939 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
940 }
941}
942
943void SurfaceFlinger::debugShowFPS() const
944{
945 static int mFrameCount;
946 static int mLastFrameCount = 0;
947 static nsecs_t mLastFpsTime = 0;
948 static float mFps = 0;
949 mFrameCount++;
950 nsecs_t now = systemTime();
951 nsecs_t diff = now - mLastFpsTime;
952 if (diff > ms2ns(250)) {
953 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
954 mLastFpsTime = now;
955 mLastFrameCount = mFrameCount;
956 }
957 // XXX: mFPS has the value we want
958 }
959
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700960status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961{
962 Mutex::Autolock _l(mStateLock);
963 addLayer_l(layer);
964 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
965 return NO_ERROR;
966}
967
Mathias Agopian96f08192010-06-02 23:28:45 -0700968status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
969{
Mathias Agopianf6679fc2010-08-10 18:09:09 -0700970 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -0700971 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
972}
973
974ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
975 const sp<LayerBaseClient>& lbc)
976{
977 Mutex::Autolock _l(mStateLock);
978
979 // attach this layer to the client
980 ssize_t name = client->attachLayer(lbc);
981
982 // add this layer to the current state list
983 addLayer_l(lbc);
984
985 return name;
986}
987
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700988status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989{
990 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -0700991 status_t err = purgatorizeLayer_l(layer);
992 if (err == NO_ERROR)
993 setTransactionFlags(eTransactionNeeded);
994 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995}
996
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700997status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700999 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1000 if (lbc != 0) {
1001 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1002 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1004 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001005 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 return NO_ERROR;
1007 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001008 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009}
1010
Mathias Agopian9a112062009-04-17 19:36:26 -07001011status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1012{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001013 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001014 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001015
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001016 layerBase->onRemoved();
1017
Mathias Agopian3d579642009-06-04 18:46:21 -07001018 // it's possible that we don't find a layer, because it might
1019 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001020 // from the user because there is a race between Client::destroySurface(),
1021 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001022 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1023}
1024
Mathias Agopian96f08192010-06-02 23:28:45 -07001025status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026{
Mathias Agopian96f08192010-06-02 23:28:45 -07001027 layer->forceVisibilityTransaction();
1028 setTransactionFlags(eTraversalNeeded);
1029 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030}
1031
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1033{
1034 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1035}
1036
Mathias Agopianbb641242010-05-18 17:06:55 -07001037uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038{
1039 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1040 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001041 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 }
1043 return old;
1044}
1045
1046void SurfaceFlinger::openGlobalTransaction()
1047{
1048 android_atomic_inc(&mTransactionCount);
1049}
1050
1051void SurfaceFlinger::closeGlobalTransaction()
1052{
1053 if (android_atomic_dec(&mTransactionCount) == 1) {
1054 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001055
1056 // if there is a transaction with a resize, wait for it to
1057 // take effect before returning.
1058 Mutex::Autolock _l(mStateLock);
1059 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001060 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1061 if (CC_UNLIKELY(err != NO_ERROR)) {
1062 // just in case something goes wrong in SF, return to the
1063 // called after a few seconds.
1064 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1065 mResizeTransationPending = false;
1066 break;
1067 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001068 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 }
1070}
1071
1072status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1073{
1074 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1075 return BAD_VALUE;
1076
1077 Mutex::Autolock _l(mStateLock);
1078 mCurrentState.freezeDisplay = 1;
1079 setTransactionFlags(eTransactionNeeded);
1080
1081 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001082 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 return NO_ERROR;
1084}
1085
1086status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1087{
1088 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1089 return BAD_VALUE;
1090
1091 Mutex::Autolock _l(mStateLock);
1092 mCurrentState.freezeDisplay = 0;
1093 setTransactionFlags(eTransactionNeeded);
1094
1095 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001096 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 return NO_ERROR;
1098}
1099
Mathias Agopianc08731e2009-03-27 18:11:38 -07001100int SurfaceFlinger::setOrientation(DisplayID dpy,
1101 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102{
1103 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1104 return BAD_VALUE;
1105
1106 Mutex::Autolock _l(mStateLock);
1107 if (mCurrentState.orientation != orientation) {
1108 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001109 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 mCurrentState.orientation = orientation;
1111 setTransactionFlags(eTransactionNeeded);
1112 mTransactionCV.wait(mStateLock);
1113 } else {
1114 orientation = BAD_VALUE;
1115 }
1116 }
1117 return orientation;
1118}
1119
Mathias Agopian96f08192010-06-02 23:28:45 -07001120sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001121 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1123 uint32_t flags)
1124{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001125 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001127
1128 if (int32_t(w|h) < 0) {
1129 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1130 int(w), int(h));
1131 return surfaceHandle;
1132 }
1133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001135 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 switch (flags & eFXSurfaceMask) {
1137 case eFXSurfaceNormal:
1138 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001139 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001141 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1142 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 }
1144 break;
1145 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001146 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 break;
1148 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001149 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 break;
1151 }
1152
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001153 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001154 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001155 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001156 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001159 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001160 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001161 params->identity = surfaceHandle->getIdentity();
1162 params->width = w;
1163 params->height = h;
1164 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001165 if (normalLayer != 0) {
1166 Mutex::Autolock _l(mStateLock);
1167 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1168 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001169 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001170
Mathias Agopian96f08192010-06-02 23:28:45 -07001171 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172 }
1173
1174 return surfaceHandle;
1175}
1176
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001177sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001178 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001179 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001180 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181{
1182 // initialize the surfaces
1183 switch (format) { // TODO: take h/w into account
1184 case PIXEL_FORMAT_TRANSPARENT:
1185 case PIXEL_FORMAT_TRANSLUCENT:
1186 format = PIXEL_FORMAT_RGBA_8888;
1187 break;
1188 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001189#ifdef NO_RGBX_8888
1190 format = PIXEL_FORMAT_RGB_565;
1191#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001192 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001193#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 break;
1195 }
1196
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001197#ifdef NO_RGBX_8888
1198 if (format == PIXEL_FORMAT_RGBX_8888)
1199 format = PIXEL_FORMAT_RGBA_8888;
1200#endif
1201
Mathias Agopian96f08192010-06-02 23:28:45 -07001202 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001203 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001204 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001206 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 }
1208 return layer;
1209}
1210
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001211sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001212 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001213 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214{
Mathias Agopian96f08192010-06-02 23:28:45 -07001215 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 return layer;
1218}
1219
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001220sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001221 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001222 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223{
Mathias Agopian96f08192010-06-02 23:28:45 -07001224 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 return layer;
1227}
1228
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001229sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001230 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232{
Mathias Agopian96f08192010-06-02 23:28:45 -07001233 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 return layer;
1236}
1237
Mathias Agopian96f08192010-06-02 23:28:45 -07001238status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239{
Mathias Agopian9a112062009-04-17 19:36:26 -07001240 /*
1241 * called by the window manager, when a surface should be marked for
1242 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001243 *
1244 * The surface is removed from the current and drawing lists, but placed
1245 * in the purgatory queue, so it's not destroyed right-away (we need
1246 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001247 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001248
Mathias Agopian48d819a2009-09-10 19:41:18 -07001249 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001250 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001251 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001252 if (layer != 0) {
1253 err = purgatorizeLayer_l(layer);
1254 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001255 setTransactionFlags(eTransactionNeeded);
1256 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001257 }
1258 return err;
1259}
1260
1261status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1262{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001263 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001264
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001265 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001266 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001267 sp<LayerBaseClient> layer;
1268 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001269 MessageDestroySurface(
1270 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1271 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001272 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001273 sp<LayerBaseClient> l(layer);
1274 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001275 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001276 /*
1277 * remove the layer from the current list -- chances are that it's
1278 * not in the list anyway, because it should have been removed
1279 * already upon request of the client (eg: window manager).
1280 * However, a buggy client could have not done that.
1281 * Since we know we don't have any more clients, we don't need
1282 * to use the purgatory.
1283 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001284 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001285 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1286 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001287 return true;
1288 }
1289 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001290
Mathias Agopianbb641242010-05-18 17:06:55 -07001291 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 return NO_ERROR;
1293}
1294
1295status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001296 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 int32_t count,
1298 const layer_state_t* states)
1299{
1300 Mutex::Autolock _l(mStateLock);
1301 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001303 const layer_state_t& s(states[i]);
1304 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001305 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 if (what & ePositionChanged) {
1308 if (layer->setPosition(s.x, s.y))
1309 flags |= eTraversalNeeded;
1310 }
1311 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001312 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001314 mCurrentState.layersSortedByZ.removeAt(idx);
1315 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 // we need traversal (state changed)
1317 // AND transaction (list changed)
1318 flags |= eTransactionNeeded|eTraversalNeeded;
1319 }
1320 }
1321 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001322 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001324 mResizeTransationPending = true;
1325 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326 }
1327 if (what & eAlphaChanged) {
1328 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1329 flags |= eTraversalNeeded;
1330 }
1331 if (what & eMatrixChanged) {
1332 if (layer->setMatrix(s.matrix))
1333 flags |= eTraversalNeeded;
1334 }
1335 if (what & eTransparentRegionChanged) {
1336 if (layer->setTransparentRegionHint(s.transparentRegion))
1337 flags |= eTraversalNeeded;
1338 }
1339 if (what & eVisibilityChanged) {
1340 if (layer->setFlags(s.flags, s.mask))
1341 flags |= eTraversalNeeded;
1342 }
1343 }
1344 }
1345 if (flags) {
1346 setTransactionFlags(flags);
1347 }
1348 return NO_ERROR;
1349}
1350
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351void SurfaceFlinger::screenReleased(int dpy)
1352{
1353 // this may be called by a signal handler, we can't do too much in here
1354 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1355 signalEvent();
1356}
1357
1358void SurfaceFlinger::screenAcquired(int dpy)
1359{
1360 // this may be called by a signal handler, we can't do too much in here
1361 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1362 signalEvent();
1363}
1364
1365status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1366{
1367 const size_t SIZE = 1024;
1368 char buffer[SIZE];
1369 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001370 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 snprintf(buffer, SIZE, "Permission Denial: "
1372 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1373 IPCThreadState::self()->getCallingPid(),
1374 IPCThreadState::self()->getCallingUid());
1375 result.append(buffer);
1376 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001377
1378 // figure out if we're stuck somewhere
1379 const nsecs_t now = systemTime();
1380 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1381 const nsecs_t inTransaction(mDebugInTransaction);
1382 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1383 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1384
1385 // Try to get the main lock, but don't insist if we can't
1386 // (this would indicate SF is stuck, but we want to be able to
1387 // print something in dumpsys).
1388 int retry = 3;
1389 while (mStateLock.tryLock()<0 && --retry>=0) {
1390 usleep(1000000);
1391 }
1392 const bool locked(retry >= 0);
1393 if (!locked) {
1394 snprintf(buffer, SIZE,
1395 "SurfaceFlinger appears to be unresponsive, "
1396 "dumping anyways (no locks held)\n");
1397 result.append(buffer);
1398 }
1399
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1401 const size_t count = currentLayers.size();
1402 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001403 const sp<LayerBase>& layer(currentLayers[i]);
1404 layer->dump(result, buffer, SIZE);
1405 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 s.transparentRegion.dump(result, "transparentRegion");
1407 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1408 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1409 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001410
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 mWormholeRegion.dump(result, "WormholeRegion");
1412 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1413 snprintf(buffer, SIZE,
1414 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1415 mFreezeDisplay?"yes":"no", mFreezeCount,
1416 mCurrentState.orientation, hw.canDraw());
1417 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001418 snprintf(buffer, SIZE,
1419 " last eglSwapBuffers() time: %f us\n"
1420 " last transaction time : %f us\n",
1421 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1422 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001423
Mathias Agopian9795c422009-08-26 16:36:26 -07001424 if (inSwapBuffersDuration || !locked) {
1425 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1426 inSwapBuffersDuration/1000.0);
1427 result.append(buffer);
1428 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001429
Mathias Agopian9795c422009-08-26 16:36:26 -07001430 if (inTransactionDuration || !locked) {
1431 snprintf(buffer, SIZE, " transaction time: %f us\n",
1432 inTransactionDuration/1000.0);
1433 result.append(buffer);
1434 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001435
Mathias Agopian3330b202009-10-05 17:07:12 -07001436 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001437 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001438
1439 if (locked) {
1440 mStateLock.unlock();
1441 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 }
1443 write(fd, result.string(), result.size());
1444 return NO_ERROR;
1445}
1446
1447status_t SurfaceFlinger::onTransact(
1448 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1449{
1450 switch (code) {
1451 case CREATE_CONNECTION:
1452 case OPEN_GLOBAL_TRANSACTION:
1453 case CLOSE_GLOBAL_TRANSACTION:
1454 case SET_ORIENTATION:
1455 case FREEZE_DISPLAY:
1456 case UNFREEZE_DISPLAY:
1457 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 {
1459 // codes that require permission check
1460 IPCThreadState* ipc = IPCThreadState::self();
1461 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001462 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001463 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1464 LOGE("Permission Denial: "
1465 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1466 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 }
1468 }
1469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1471 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001472 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001473 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1474 IPCThreadState* ipc = IPCThreadState::self();
1475 const int pid = ipc->getCallingPid();
1476 const int uid = ipc->getCallingUid();
1477 LOGE("Permission Denial: "
1478 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 return PERMISSION_DENIED;
1480 }
1481 int n;
1482 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001483 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001484 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485 return NO_ERROR;
1486 case 1002: // SHOW_UPDATES
1487 n = data.readInt32();
1488 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1489 return NO_ERROR;
1490 case 1003: // SHOW_BACKGROUND
1491 n = data.readInt32();
1492 mDebugBackground = n ? 1 : 0;
1493 return NO_ERROR;
1494 case 1004:{ // repaint everything
1495 Mutex::Autolock _l(mStateLock);
1496 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1497 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1498 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001499 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001501 case 1005:{ // force transaction
1502 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1503 return NO_ERROR;
1504 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001505 case 1006:{ // enable/disable GraphicLog
1506 int enabled = data.readInt32();
1507 GraphicLog::getInstance().setEnabled(enabled);
1508 return NO_ERROR;
1509 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 case 1007: // set mFreezeCount
1511 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001512 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 return NO_ERROR;
1514 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001515 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 reply->writeInt32(0);
1517 reply->writeInt32(mDebugRegion);
1518 reply->writeInt32(mDebugBackground);
1519 return NO_ERROR;
1520 case 1013: {
1521 Mutex::Autolock _l(mStateLock);
1522 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1523 reply->writeInt32(hw.getPageFlipCount());
1524 }
1525 return NO_ERROR;
1526 }
1527 }
1528 return err;
1529}
1530
1531// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001532
1533sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1534{
1535 sp<Layer> result;
1536 Mutex::Autolock _l(mStateLock);
1537 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1538 return result;
1539}
1540
1541// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542
Mathias Agopian96f08192010-06-02 23:28:45 -07001543Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001544 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546}
1547
Mathias Agopian96f08192010-06-02 23:28:45 -07001548Client::~Client()
1549{
Mathias Agopian96f08192010-06-02 23:28:45 -07001550 const size_t count = mLayers.size();
1551 for (size_t i=0 ; i<count ; i++) {
1552 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1553 if (layer != 0) {
1554 mFlinger->removeLayer(layer);
1555 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 }
1557}
1558
Mathias Agopian96f08192010-06-02 23:28:45 -07001559status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001560 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001562
Mathias Agopian96f08192010-06-02 23:28:45 -07001563ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1564{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001565 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001566 mLayers.add(name, layer);
1567 return name;
1568}
1569
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001570void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001571{
Mathias Agopian96f08192010-06-02 23:28:45 -07001572 // we do a linear search here, because this doesn't happen often
1573 const size_t count = mLayers.size();
1574 for (size_t i=0 ; i<count ; i++) {
1575 if (mLayers.valueAt(i) == layer) {
1576 mLayers.removeItemsAt(i, 1);
1577 break;
1578 }
1579 }
1580}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001581sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1582 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001583 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1584 if (layer != 0) {
1585 lbc = layer.promote();
1586 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001587 }
1588 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589}
1590
Mathias Agopian96f08192010-06-02 23:28:45 -07001591sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001592 return 0;
1593}
1594ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1595 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596}
Mathias Agopian96f08192010-06-02 23:28:45 -07001597sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001598 ISurfaceComposerClient::surface_data_t* params, int pid,
1599 const String8& name,
1600 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601 uint32_t flags)
1602{
Mathias Agopian96f08192010-06-02 23:28:45 -07001603 return mFlinger->createSurface(this, pid, name, params,
1604 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605}
Mathias Agopian96f08192010-06-02 23:28:45 -07001606status_t Client::destroySurface(SurfaceID sid) {
1607 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608}
Mathias Agopian96f08192010-06-02 23:28:45 -07001609status_t Client::setState(int32_t count, const layer_state_t* states) {
1610 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611}
1612
1613// ---------------------------------------------------------------------------
1614
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001615UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1616 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1617{
1618 const int pgsize = getpagesize();
1619 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1620
1621 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1622 "SurfaceFlinger Client control-block");
1623
1624 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1625 if (ctrlblk) { // construct the shared structure in-place.
1626 new(ctrlblk) SharedClient;
1627 }
1628}
1629
1630UserClient::~UserClient()
1631{
1632 if (ctrlblk) {
1633 ctrlblk->~SharedClient(); // destroy our shared-structure.
1634 }
1635
1636 /*
1637 * When a UserClient dies, it's unclear what to do exactly.
1638 * We could go ahead and destroy all surfaces linked to that client
1639 * however, it wouldn't be fair to the main Client
1640 * (usually the the window-manager), which might want to re-target
1641 * the layer to another UserClient.
1642 * I think the best is to do nothing, or not much; in most cases the
1643 * WM itself will go ahead and clean things up when it detects a client of
1644 * his has died.
1645 * The remaining question is what to display? currently we keep
1646 * just keep the current buffer.
1647 */
1648}
1649
1650status_t UserClient::initCheck() const {
1651 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1652}
1653
1654void UserClient::detachLayer(const Layer* layer)
1655{
1656 int32_t name = layer->getToken();
1657 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001658 int32_t mask = 1LU<<name;
1659 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1660 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1661 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001662 }
1663}
1664
1665sp<IMemoryHeap> UserClient::getControlBlock() const {
1666 return mCblkHeap;
1667}
1668
1669ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1670{
1671 int32_t name = NAME_NOT_FOUND;
1672 sp<Layer> layer(mFlinger->getLayer(sur));
1673 if (layer == 0) return name;
1674
Mathias Agopian579b3f82010-06-08 19:54:15 -07001675 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001676 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07001677 if ((name >= 0) && (layer->getClient() == this))
1678 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001679
1680 name = 0;
1681 do {
1682 int32_t mask = 1LU<<name;
1683 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1684 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001685 status_t err = layer->setToken(
1686 const_cast<UserClient*>(this), ctrlblk, name);
1687 if (err != NO_ERROR) {
1688 // free the name
1689 android_atomic_and(~mask, &mBitmap);
1690 name = err;
1691 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001692 break;
1693 }
1694 if (++name > 31)
1695 name = NO_MEMORY;
1696 } while(name >= 0);
1697
Mathias Agopian53503a92010-06-08 15:40:56 -07001698 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1699 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001700 return name;
1701}
1702
1703sp<ISurface> UserClient::createSurface(
1704 ISurfaceComposerClient::surface_data_t* params, int pid,
1705 const String8& name,
1706 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1707 uint32_t flags) {
1708 return 0;
1709}
1710status_t UserClient::destroySurface(SurfaceID sid) {
1711 return INVALID_OPERATION;
1712}
1713status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1714 return INVALID_OPERATION;
1715}
1716
1717// ---------------------------------------------------------------------------
1718
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719GraphicPlane::GraphicPlane()
1720 : mHw(0)
1721{
1722}
1723
1724GraphicPlane::~GraphicPlane() {
1725 delete mHw;
1726}
1727
1728bool GraphicPlane::initialized() const {
1729 return mHw ? true : false;
1730}
1731
Mathias Agopian2b92d892010-02-08 15:49:35 -08001732int GraphicPlane::getWidth() const {
1733 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734}
1735
Mathias Agopian2b92d892010-02-08 15:49:35 -08001736int GraphicPlane::getHeight() const {
1737 return mHeight;
1738}
1739
1740void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1741{
1742 mHw = hw;
1743
1744 // initialize the display orientation transform.
1745 // it's a constant that should come from the display driver.
1746 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1747 char property[PROPERTY_VALUE_MAX];
1748 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1749 //displayOrientation
1750 switch (atoi(property)) {
1751 case 90:
1752 displayOrientation = ISurfaceComposer::eOrientation90;
1753 break;
1754 case 270:
1755 displayOrientation = ISurfaceComposer::eOrientation270;
1756 break;
1757 }
1758 }
1759
1760 const float w = hw->getWidth();
1761 const float h = hw->getHeight();
1762 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1763 &mDisplayTransform);
1764 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1765 mDisplayWidth = h;
1766 mDisplayHeight = w;
1767 } else {
1768 mDisplayWidth = w;
1769 mDisplayHeight = h;
1770 }
1771
1772 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773}
1774
1775status_t GraphicPlane::orientationToTransfrom(
1776 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001777{
1778 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779 switch (orientation) {
1780 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001781 flags = Transform::ROT_0;
1782 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001784 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785 break;
1786 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001787 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 break;
1789 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001790 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001791 break;
1792 default:
1793 return BAD_VALUE;
1794 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001795 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796 return NO_ERROR;
1797}
1798
1799status_t GraphicPlane::setOrientation(int orientation)
1800{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 // If the rotation can be handled in hardware, this is where
1802 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001803
1804 const DisplayHardware& hw(displayHardware());
1805 const float w = mDisplayWidth;
1806 const float h = mDisplayHeight;
1807 mWidth = int(w);
1808 mHeight = int(h);
1809
1810 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001811 GraphicPlane::orientationToTransfrom(orientation, w, h,
1812 &orientationTransform);
1813 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1814 mWidth = int(h);
1815 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001817
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001818 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001819 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001820 return NO_ERROR;
1821}
1822
1823const DisplayHardware& GraphicPlane::displayHardware() const {
1824 return *mHw;
1825}
1826
1827const Transform& GraphicPlane::transform() const {
1828 return mGlobalTransform;
1829}
1830
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001831EGLDisplay GraphicPlane::getEGLDisplay() const {
1832 return mHw->getEGLDisplay();
1833}
1834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835// ---------------------------------------------------------------------------
1836
1837}; // namespace android