blob: 2b06f6fa42bf27f8be40377e9876c9d1234a7738 [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"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070078 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070079 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 mVisibleRegionsDirty(false),
81 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
83 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070084 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070087 mDebugInSwapBuffers(0),
88 mLastSwapBufferTime(0),
89 mDebugInTransaction(0),
90 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070091 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mConsoleSignals(0),
93 mSecureFrameBuffer(0)
94{
95 init();
96}
97
98void SurfaceFlinger::init()
99{
100 LOGI("SurfaceFlinger is starting");
101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
104 property_get("debug.sf.showupdates", value, "0");
105 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 property_get("debug.sf.showbackground", value, "0");
107 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
Mathias Agopian78fd5012010-04-20 14:51:04 -0700109 LOGI_IF(mDebugRegion, "showupdates enabled");
110 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111}
112
113SurfaceFlinger::~SurfaceFlinger()
114{
115 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
119{
120 return graphicPlane(0).displayHardware().getOverlayEngine();
121}
122
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700123sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700125 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126}
127
Mathias Agopian7e27f052010-05-28 14:22:23 -0700128sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129{
Mathias Agopian96f08192010-06-02 23:28:45 -0700130 sp<ISurfaceComposerClient> bclient;
131 sp<Client> client(new Client(this));
132 status_t err = client->initCheck();
133 if (err == NO_ERROR) {
134 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 return bclient;
137}
138
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700139sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
140{
141 sp<ISurfaceComposerClient> bclient;
142 sp<UserClient> client(new UserClient(this));
143 status_t err = client->initCheck();
144 if (err == NO_ERROR) {
145 bclient = client;
146 }
147 return bclient;
148}
149
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
152{
153 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
154 const GraphicPlane& plane(mGraphicPlanes[dpy]);
155 return plane;
156}
157
158GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
159{
160 return const_cast<GraphicPlane&>(
161 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
162}
163
164void SurfaceFlinger::bootFinished()
165{
166 const nsecs_t now = systemTime();
167 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700168 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700169 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700170 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171}
172
173void SurfaceFlinger::onFirstRef()
174{
175 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
176
177 // Wait for the main thread to be done with its initialization
178 mReadyToRunBarrier.wait();
179}
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181static inline uint16_t pack565(int r, int g, int b) {
182 return (r<<11)|(g<<5)|b;
183}
184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185status_t SurfaceFlinger::readyToRun()
186{
187 LOGI( "SurfaceFlinger's main thread ready to run. "
188 "Initializing graphics H/W...");
189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 // we only support one display currently
191 int dpy = 0;
192
193 {
194 // initialize the main display
195 GraphicPlane& plane(graphicPlane(dpy));
196 DisplayHardware* const hw = new DisplayHardware(this, dpy);
197 plane.setDisplayHardware(hw);
198 }
199
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700200 // create the shared control-block
201 mServerHeap = new MemoryHeapBase(4096,
202 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
203 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
204
205 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
206 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
207
208 new(mServerCblk) surface_flinger_cblk_t;
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 // initialize primary screen
211 // (other display should be initialized in the same manner, but
212 // asynchronously, as they could come and go. None of this is supported
213 // yet).
214 const GraphicPlane& plane(graphicPlane(dpy));
215 const DisplayHardware& hw = plane.displayHardware();
216 const uint32_t w = hw.getWidth();
217 const uint32_t h = hw.getHeight();
218 const uint32_t f = hw.getFormat();
219 hw.makeCurrent();
220
221 // initialize the shared control block
222 mServerCblk->connected |= 1<<dpy;
223 display_cblk_t* dcblk = mServerCblk->displays + dpy;
224 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800225 dcblk->w = plane.getWidth();
226 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 dcblk->format = f;
228 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
229 dcblk->xdpi = hw.getDpiX();
230 dcblk->ydpi = hw.getDpiY();
231 dcblk->fps = hw.getRefreshRate();
232 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
234 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
236 glPixelStorei(GL_PACK_ALIGNMENT, 4);
237 glEnableClientState(GL_VERTEX_ARRAY);
238 glEnable(GL_SCISSOR_TEST);
239 glShadeModel(GL_FLAT);
240 glDisable(GL_DITHER);
241 glDisable(GL_CULL_FACE);
242
243 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
244 const uint16_t g1 = pack565(0x17,0x2f,0x17);
245 const uint16_t textureData[4] = { g0, g1, g1, g0 };
246 glGenTextures(1, &mWormholeTexName);
247 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
248 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
249 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
250 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
251 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
252 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
253 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
254
255 glViewport(0, 0, w, h);
256 glMatrixMode(GL_PROJECTION);
257 glLoadIdentity();
258 glOrthof(0, w, h, 0, 0, 1);
259
260 LayerDim::initDimmer(this, w, h);
261
262 mReadyToRunBarrier.open();
263
264 /*
265 * We're now ready to accept clients...
266 */
267
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700268 // start boot animation
269 property_set("ctl.start", "bootanim");
270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 return NO_ERROR;
272}
273
274// ----------------------------------------------------------------------------
275#if 0
276#pragma mark -
277#pragma mark Events Handler
278#endif
279
280void SurfaceFlinger::waitForEvent()
281{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700282 while (true) {
283 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800284 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700285 if (UNLIKELY(isFrozen())) {
286 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700287 const nsecs_t now = systemTime();
288 if (mFreezeDisplayTime == 0) {
289 mFreezeDisplayTime = now;
290 }
291 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
292 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700293 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700294
Mathias Agopianbb641242010-05-18 17:06:55 -0700295 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800296
297 // see if we timed out
298 if (isFrozen()) {
299 const nsecs_t now = systemTime();
300 nsecs_t frozenTime = (now - mFreezeDisplayTime);
301 if (frozenTime >= freezeDisplayTimeout) {
302 // we timed out and are still frozen
303 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
304 mFreezeDisplay, mFreezeCount);
305 mFreezeDisplayTime = 0;
306 mFreezeCount = 0;
307 mFreezeDisplay = false;
308 }
309 }
310
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700311 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700312 switch (msg->what) {
313 case MessageQueue::INVALIDATE:
314 // invalidate message, just return to the main loop
315 return;
316 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
319}
320
321void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700322 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323}
324
325void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700326 // this is the IPC call
327 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
Mathias Agopianbb641242010-05-18 17:06:55 -0700330status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
331 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332{
Mathias Agopianbb641242010-05-18 17:06:55 -0700333 return mEventQueue.postMessage(msg, reltime, flags);
334}
335
336status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
337 nsecs_t reltime, uint32_t flags)
338{
339 status_t res = mEventQueue.postMessage(msg, reltime, flags);
340 if (res == NO_ERROR) {
341 msg->wait();
342 }
343 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344}
345
346// ----------------------------------------------------------------------------
347#if 0
348#pragma mark -
349#pragma mark Main loop
350#endif
351
352bool SurfaceFlinger::threadLoop()
353{
354 waitForEvent();
355
356 // check for transactions
357 if (UNLIKELY(mConsoleSignals)) {
358 handleConsoleEvents();
359 }
360
361 if (LIKELY(mTransactionCount == 0)) {
362 // if we're in a global transaction, don't do anything.
363 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
364 uint32_t transactionFlags = getTransactionFlags(mask);
365 if (LIKELY(transactionFlags)) {
366 handleTransaction(transactionFlags);
367 }
368 }
369
370 // post surfaces (if needed)
371 handlePageFlip();
372
373 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700374 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700376
377 const int index = hw.getCurrentBufferIndex();
378 GraphicLog& logger(GraphicLog::getInstance());
379
380 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 handleRepaint();
382
Mathias Agopian74faca22009-09-17 16:18:16 -0700383 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700384 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700385 hw.compositionComplete();
386
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 // release the clients before we flip ('cause flip might block)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700388 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390
Mathias Agopian35b48d12010-09-13 22:57:58 -0700391 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 postFramebuffer();
Mathias Agopian35b48d12010-09-13 22:57:58 -0700393
394 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 } else {
396 // pretend we did the post
397 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 usleep(16667); // 60 fps period
399 }
400 return true;
401}
402
403void SurfaceFlinger::postFramebuffer()
404{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405 if (!mInvalidRegion.isEmpty()) {
406 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700407 const nsecs_t now = systemTime();
408 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700410 mLastSwapBufferTime = systemTime() - now;
411 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 }
414}
415
416void SurfaceFlinger::handleConsoleEvents()
417{
418 // something to do with the console
419 const DisplayHardware& hw = graphicPlane(0).displayHardware();
420
421 int what = android_atomic_and(0, &mConsoleSignals);
422 if (what & eConsoleAcquired) {
423 hw.acquireScreen();
424 }
425
426 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700427 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 hw.releaseScreen();
430 }
431
432 if (what & eConsoleReleased) {
433 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 hw.releaseScreen();
435 } else {
436 mDeferReleaseConsole = true;
437 }
438 }
439
440 mDirtyRegion.set(hw.bounds());
441}
442
443void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
444{
Mathias Agopian3d579642009-06-04 18:46:21 -0700445 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446
Mathias Agopian4da75192010-08-10 17:19:56 -0700447 /*
448 * Perform and commit the transaction
449 */
450
Mathias Agopian3d579642009-06-04 18:46:21 -0700451 { // scope for the lock
452 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700453 const nsecs_t now = systemTime();
454 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700455 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700456 mLastTransactionTime = systemTime() - now;
457 mDebugInTransaction = 0;
Mathias Agopian4da75192010-08-10 17:19:56 -0700458 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700459 }
460
Mathias Agopian4da75192010-08-10 17:19:56 -0700461 /*
462 * Clean-up all layers that went away
463 * (do this without the lock held)
464 */
Mathias Agopian3d579642009-06-04 18:46:21 -0700465 const size_t count = ditchedLayers.size();
466 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700467 if (ditchedLayers[i] != 0) {
468 //LOGD("ditching layer %p", ditchedLayers[i].get());
469 ditchedLayers[i]->ditch();
470 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700471 }
472}
473
474void SurfaceFlinger::handleTransactionLocked(
475 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
476{
477 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 const size_t count = currentLayers.size();
479
480 /*
481 * Traversal of the children
482 * (perform the transaction for each of them if needed)
483 */
484
485 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
486 if (layersNeedTransaction) {
487 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700488 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
490 if (!trFlags) continue;
491
492 const uint32_t flags = layer->doTransaction(0);
493 if (flags & Layer::eVisibleRegion)
494 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 }
496 }
497
498 /*
499 * Perform our own transaction if needed
500 */
501
502 if (transactionFlags & eTransactionNeeded) {
503 if (mCurrentState.orientation != mDrawingState.orientation) {
504 // the orientation has changed, recompute all visible regions
505 // and invalidate everything.
506
507 const int dpy = 0;
508 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700509 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 GraphicPlane& plane(graphicPlane(dpy));
511 plane.setOrientation(orientation);
512
513 // update the shared control block
514 const DisplayHardware& hw(plane.displayHardware());
515 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
516 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800517 dcblk->w = plane.getWidth();
518 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519
520 mVisibleRegionsDirty = true;
521 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 }
523
524 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
525 // freezing or unfreezing the display -> trigger animation if needed
526 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800527 if (mFreezeDisplay)
528 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 }
530
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700531 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
532 // layers have been added
533 mVisibleRegionsDirty = true;
534 }
535
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 // some layers might have been removed, so
537 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700538 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700539 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700541 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700542 const size_t count = previousLayers.size();
543 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700544 const sp<LayerBase>& layer(previousLayers[i]);
545 if (currentLayers.indexOf( layer ) < 0) {
546 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700547 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700548 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700549 }
550 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 }
553
554 commitTransaction();
555}
556
557sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
558{
559 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
560}
561
562void SurfaceFlinger::computeVisibleRegions(
563 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
564{
565 const GraphicPlane& plane(graphicPlane(0));
566 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700567 const DisplayHardware& hw(plane.displayHardware());
568 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569
570 Region aboveOpaqueLayers;
571 Region aboveCoveredLayers;
572 Region dirty;
573
574 bool secureFrameBuffer = false;
575
576 size_t i = currentLayers.size();
577 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 layer->validateVisibility(planeTransform);
580
581 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700582 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583
Mathias Agopianab028732010-03-16 16:41:46 -0700584 /*
585 * opaqueRegion: area of a surface that is fully opaque.
586 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700588
589 /*
590 * visibleRegion: area of a surface that is visible on screen
591 * and not fully transparent. This is essentially the layer's
592 * footprint minus the opaque regions above it.
593 * Areas covered by a translucent surface are considered visible.
594 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700596
597 /*
598 * coveredRegion: area of a surface that is covered by all
599 * visible regions above it (which includes the translucent areas).
600 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700602
603
604 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700605 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700607 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700609 visibleRegion.andSelf(screenRegion);
610 if (!visibleRegion.isEmpty()) {
611 // Remove the transparent area from the visible region
612 if (translucent) {
613 visibleRegion.subtractSelf(layer->transparentRegionScreen);
614 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615
Mathias Agopianab028732010-03-16 16:41:46 -0700616 // compute the opaque region
617 const int32_t layerOrientation = layer->getOrientation();
618 if (s.alpha==255 && !translucent &&
619 ((layerOrientation & Transform::ROT_INVALID) == false)) {
620 // the opaque region is the layer's footprint
621 opaqueRegion = visibleRegion;
622 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 }
624 }
625
Mathias Agopianab028732010-03-16 16:41:46 -0700626 // Clip the covered region to the visible region
627 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
628
629 // Update aboveCoveredLayers for next (lower) layer
630 aboveCoveredLayers.orSelf(visibleRegion);
631
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 // subtract the opaque region covered by the layers above us
633 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634
635 // compute this layer's dirty region
636 if (layer->contentDirty) {
637 // we need to invalidate the whole region
638 dirty = visibleRegion;
639 // as well, as the old visible region
640 dirty.orSelf(layer->visibleRegionScreen);
641 layer->contentDirty = false;
642 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700643 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700644 * the exposed region consists of two components:
645 * 1) what's VISIBLE now and was COVERED before
646 * 2) what's EXPOSED now less what was EXPOSED before
647 *
648 * note that (1) is conservative, we start with the whole
649 * visible region but only keep what used to be covered by
650 * something -- which mean it may have been exposed.
651 *
652 * (2) handles areas that were not covered by anything but got
653 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700654 */
Mathias Agopianab028732010-03-16 16:41:46 -0700655 const Region newExposed = visibleRegion - coveredRegion;
656 const Region oldVisibleRegion = layer->visibleRegionScreen;
657 const Region oldCoveredRegion = layer->coveredRegionScreen;
658 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
659 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 }
661 dirty.subtractSelf(aboveOpaqueLayers);
662
663 // accumulate to the screen dirty region
664 dirtyRegion.orSelf(dirty);
665
Mathias Agopianab028732010-03-16 16:41:46 -0700666 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668
669 // Store the visible region is screen space
670 layer->setVisibleRegion(visibleRegion);
671 layer->setCoveredRegion(coveredRegion);
672
Mathias Agopian97011222009-07-28 10:57:27 -0700673 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 if (layer->isSecure() && !visibleRegion.isEmpty()) {
675 secureFrameBuffer = true;
676 }
677 }
678
Mathias Agopian97011222009-07-28 10:57:27 -0700679 // invalidate the areas where a layer was removed
680 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
681 mDirtyRegionRemovedLayer.clear();
682
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 mSecureFrameBuffer = secureFrameBuffer;
684 opaqueRegion = aboveOpaqueLayers;
685}
686
687
688void SurfaceFlinger::commitTransaction()
689{
690 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700691 mResizeTransationPending = false;
692 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693}
694
695void SurfaceFlinger::handlePageFlip()
696{
697 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700698 LayerVector& currentLayers = const_cast<LayerVector&>(
699 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 visibleRegions |= lockPageFlip(currentLayers);
701
702 const DisplayHardware& hw = graphicPlane(0).displayHardware();
703 const Region screenRegion(hw.bounds());
704 if (visibleRegions) {
705 Region opaqueRegion;
706 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700707
708 /*
709 * rebuild the visible layer list
710 */
711 mVisibleLayersSortedByZ.clear();
712 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
713 size_t count = currentLayers.size();
714 mVisibleLayersSortedByZ.setCapacity(count);
715 for (size_t i=0 ; i<count ; i++) {
716 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
717 mVisibleLayersSortedByZ.add(currentLayers[i]);
718 }
719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 mWormholeRegion = screenRegion.subtract(opaqueRegion);
721 mVisibleRegionsDirty = false;
722 }
723
724 unlockPageFlip(currentLayers);
725 mDirtyRegion.andSelf(screenRegion);
726}
727
728bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
729{
730 bool recomputeVisibleRegions = false;
731 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700732 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700734 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 layer->lockPageFlip(recomputeVisibleRegions);
736 }
737 return recomputeVisibleRegions;
738}
739
740void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
741{
742 const GraphicPlane& plane(graphicPlane(0));
743 const Transform& planeTransform(plane.transform());
744 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700745 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700747 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748 layer->unlockPageFlip(planeTransform, mDirtyRegion);
749 }
750}
751
Mathias Agopianb8a55602009-06-26 19:06:36 -0700752
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753void SurfaceFlinger::handleRepaint()
754{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700755 // compute the invalid region
756 mInvalidRegion.orSelf(mDirtyRegion);
757 if (mInvalidRegion.isEmpty()) {
758 // nothing to do
759 return;
760 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761
762 if (UNLIKELY(mDebugRegion)) {
763 debugFlashRegions();
764 }
765
Mathias Agopianb8a55602009-06-26 19:06:36 -0700766 // set the frame buffer
767 const DisplayHardware& hw(graphicPlane(0).displayHardware());
768 glMatrixMode(GL_MODELVIEW);
769 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770
771 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700772 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
773 (flags & DisplayHardware::BUFFER_PRESERVED))
774 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700775 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
776 // takes a rectangle, we must make sure to update that whole
777 // rectangle in that case
778 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700779 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700780 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 mDirtyRegion.set(mInvalidRegion.bounds());
782 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700783 // in the BUFFER_PRESERVED case, obviously, we can update only
784 // what's needed and nothing more.
785 // NOTE: this is NOT a common case, as preserving the backbuffer
786 // is costly and usually involves copying the whole update back.
787 }
788 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700789 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700790 // We need to redraw the rectangle that will be updated
791 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700792 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700793 // rectangle instead of a region (see DisplayHardware::flip())
794 mDirtyRegion.set(mInvalidRegion.bounds());
795 } else {
796 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800797 mDirtyRegion.set(hw.bounds());
798 mInvalidRegion = mDirtyRegion;
799 }
800 }
801
802 // compose all surfaces
803 composeSurfaces(mDirtyRegion);
804
805 // clear the dirty regions
806 mDirtyRegion.clear();
807}
808
809void SurfaceFlinger::composeSurfaces(const Region& dirty)
810{
811 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
812 // should never happen unless the window manager has a bug
813 // draw something...
814 drawWormhole();
815 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700816 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian000ca8f2010-08-17 20:19:23 -0700817 const size_t count = layers.size();
818 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian45721772010-08-12 15:03:26 -0700819 const sp<LayerBase>& layer(layers[i]);
820 const Region clip(dirty.intersect(layer->visibleRegionScreen));
821 if (!clip.isEmpty()) {
822 layer->draw(clip);
823 }
824 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825}
826
827void SurfaceFlinger::unlockClients()
828{
829 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
830 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700831 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700833 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 layer->finishPageFlip();
835 }
836}
837
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838void SurfaceFlinger::debugFlashRegions()
839{
Mathias Agopian0a917752010-06-14 21:20:00 -0700840 const DisplayHardware& hw(graphicPlane(0).displayHardware());
841 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700842
Mathias Agopian0a917752010-06-14 21:20:00 -0700843 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
844 (flags & DisplayHardware::BUFFER_PRESERVED))) {
845 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
846 mDirtyRegion.bounds() : hw.bounds());
847 composeSurfaces(repaint);
848 }
849
850 TextureManager::deactivateTextures();
851
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 glDisable(GL_BLEND);
853 glDisable(GL_DITHER);
854 glDisable(GL_SCISSOR_TEST);
855
Mathias Agopian0926f502009-05-04 14:17:04 -0700856 static int toggle = 0;
857 toggle = 1 - toggle;
858 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700859 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700860 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700861 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700862 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863
Mathias Agopian20f68782009-05-11 00:03:41 -0700864 Region::const_iterator it = mDirtyRegion.begin();
865 Region::const_iterator const end = mDirtyRegion.end();
866 while (it != end) {
867 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868 GLfloat vertices[][2] = {
869 { r.left, r.top },
870 { r.left, r.bottom },
871 { r.right, r.bottom },
872 { r.right, r.top }
873 };
874 glVertexPointer(2, GL_FLOAT, 0, vertices);
875 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
876 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700877
Mathias Agopianb8a55602009-06-26 19:06:36 -0700878 if (mInvalidRegion.isEmpty()) {
879 mDirtyRegion.dump("mDirtyRegion");
880 mInvalidRegion.dump("mInvalidRegion");
881 }
882 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883
884 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700885 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886
887 glEnable(GL_SCISSOR_TEST);
888 //mDirtyRegion.dump("mDirtyRegion");
889}
890
891void SurfaceFlinger::drawWormhole() const
892{
893 const Region region(mWormholeRegion.intersect(mDirtyRegion));
894 if (region.isEmpty())
895 return;
896
897 const DisplayHardware& hw(graphicPlane(0).displayHardware());
898 const int32_t width = hw.getWidth();
899 const int32_t height = hw.getHeight();
900
901 glDisable(GL_BLEND);
902 glDisable(GL_DITHER);
903
904 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700905 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700906 Region::const_iterator it = region.begin();
907 Region::const_iterator const end = region.end();
908 while (it != end) {
909 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 const GLint sy = height - (r.top + r.height());
911 glScissor(r.left, sy, r.width(), r.height());
912 glClear(GL_COLOR_BUFFER_BIT);
913 }
914 } else {
915 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
916 { width, height }, { 0, height } };
917 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
918 glVertexPointer(2, GL_SHORT, 0, vertices);
919 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
920 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -0700921#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700922 if (GLExtensions::getInstance().haveTextureExternal()) {
923 glDisable(GL_TEXTURE_EXTERNAL_OES);
924 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700925#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 glEnable(GL_TEXTURE_2D);
927 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
928 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
929 glMatrixMode(GL_TEXTURE);
930 glLoadIdentity();
931 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700932 Region::const_iterator it = region.begin();
933 Region::const_iterator const end = region.end();
934 while (it != end) {
935 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 const GLint sy = height - (r.top + r.height());
937 glScissor(r.left, sy, r.width(), r.height());
938 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
939 }
940 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
941 }
942}
943
944void SurfaceFlinger::debugShowFPS() const
945{
946 static int mFrameCount;
947 static int mLastFrameCount = 0;
948 static nsecs_t mLastFpsTime = 0;
949 static float mFps = 0;
950 mFrameCount++;
951 nsecs_t now = systemTime();
952 nsecs_t diff = now - mLastFpsTime;
953 if (diff > ms2ns(250)) {
954 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
955 mLastFpsTime = now;
956 mLastFrameCount = mFrameCount;
957 }
958 // XXX: mFPS has the value we want
959 }
960
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700961status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962{
963 Mutex::Autolock _l(mStateLock);
964 addLayer_l(layer);
965 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
966 return NO_ERROR;
967}
968
Mathias Agopian96f08192010-06-02 23:28:45 -0700969status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
970{
Mathias Agopianf6679fc2010-08-10 18:09:09 -0700971 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -0700972 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
973}
974
975ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
976 const sp<LayerBaseClient>& lbc)
977{
978 Mutex::Autolock _l(mStateLock);
979
980 // attach this layer to the client
981 ssize_t name = client->attachLayer(lbc);
982
983 // add this layer to the current state list
984 addLayer_l(lbc);
985
986 return name;
987}
988
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700989status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990{
991 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -0700992 status_t err = purgatorizeLayer_l(layer);
993 if (err == NO_ERROR)
994 setTransactionFlags(eTransactionNeeded);
995 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996}
997
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700998status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001000 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1001 if (lbc != 0) {
1002 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1003 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1005 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001006 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 return NO_ERROR;
1008 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001009 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010}
1011
Mathias Agopian9a112062009-04-17 19:36:26 -07001012status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1013{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001014 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001015 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001016
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001017 layerBase->onRemoved();
1018
Mathias Agopian3d579642009-06-04 18:46:21 -07001019 // it's possible that we don't find a layer, because it might
1020 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001021 // from the user because there is a race between Client::destroySurface(),
1022 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001023 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1024}
1025
Mathias Agopian96f08192010-06-02 23:28:45 -07001026status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027{
Mathias Agopian96f08192010-06-02 23:28:45 -07001028 layer->forceVisibilityTransaction();
1029 setTransactionFlags(eTraversalNeeded);
1030 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031}
1032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1034{
1035 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1036}
1037
Mathias Agopianbb641242010-05-18 17:06:55 -07001038uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039{
1040 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1041 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001042 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 }
1044 return old;
1045}
1046
1047void SurfaceFlinger::openGlobalTransaction()
1048{
1049 android_atomic_inc(&mTransactionCount);
1050}
1051
1052void SurfaceFlinger::closeGlobalTransaction()
1053{
1054 if (android_atomic_dec(&mTransactionCount) == 1) {
1055 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001056
1057 // if there is a transaction with a resize, wait for it to
1058 // take effect before returning.
1059 Mutex::Autolock _l(mStateLock);
1060 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001061 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1062 if (CC_UNLIKELY(err != NO_ERROR)) {
1063 // just in case something goes wrong in SF, return to the
1064 // called after a few seconds.
1065 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1066 mResizeTransationPending = false;
1067 break;
1068 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001069 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 }
1071}
1072
1073status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1074{
1075 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1076 return BAD_VALUE;
1077
1078 Mutex::Autolock _l(mStateLock);
1079 mCurrentState.freezeDisplay = 1;
1080 setTransactionFlags(eTransactionNeeded);
1081
1082 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001083 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 return NO_ERROR;
1085}
1086
1087status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1088{
1089 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1090 return BAD_VALUE;
1091
1092 Mutex::Autolock _l(mStateLock);
1093 mCurrentState.freezeDisplay = 0;
1094 setTransactionFlags(eTransactionNeeded);
1095
1096 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001097 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 return NO_ERROR;
1099}
1100
Mathias Agopianc08731e2009-03-27 18:11:38 -07001101int SurfaceFlinger::setOrientation(DisplayID dpy,
1102 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103{
1104 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1105 return BAD_VALUE;
1106
1107 Mutex::Autolock _l(mStateLock);
1108 if (mCurrentState.orientation != orientation) {
1109 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001110 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 mCurrentState.orientation = orientation;
1112 setTransactionFlags(eTransactionNeeded);
1113 mTransactionCV.wait(mStateLock);
1114 } else {
1115 orientation = BAD_VALUE;
1116 }
1117 }
1118 return orientation;
1119}
1120
Mathias Agopian96f08192010-06-02 23:28:45 -07001121sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001122 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1124 uint32_t flags)
1125{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001126 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001128
1129 if (int32_t(w|h) < 0) {
1130 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1131 int(w), int(h));
1132 return surfaceHandle;
1133 }
1134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001136 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 switch (flags & eFXSurfaceMask) {
1138 case eFXSurfaceNormal:
1139 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001140 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001142 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1143 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144 }
1145 break;
1146 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001147 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 break;
1149 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001150 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151 break;
1152 }
1153
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001154 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001155 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001156 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001157 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001160 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001161 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001162 params->identity = surfaceHandle->getIdentity();
1163 params->width = w;
1164 params->height = h;
1165 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001166 if (normalLayer != 0) {
1167 Mutex::Autolock _l(mStateLock);
1168 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1169 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001170 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001171
Mathias Agopian96f08192010-06-02 23:28:45 -07001172 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 }
1174
1175 return surfaceHandle;
1176}
1177
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001178sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001179 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001180 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001181 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182{
1183 // initialize the surfaces
1184 switch (format) { // TODO: take h/w into account
1185 case PIXEL_FORMAT_TRANSPARENT:
1186 case PIXEL_FORMAT_TRANSLUCENT:
1187 format = PIXEL_FORMAT_RGBA_8888;
1188 break;
1189 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001190#ifdef NO_RGBX_8888
1191 format = PIXEL_FORMAT_RGB_565;
1192#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001193 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001194#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 break;
1196 }
1197
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001198#ifdef NO_RGBX_8888
1199 if (format == PIXEL_FORMAT_RGBX_8888)
1200 format = PIXEL_FORMAT_RGBA_8888;
1201#endif
1202
Mathias Agopian96f08192010-06-02 23:28:45 -07001203 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001204 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001205 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001207 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 }
1209 return layer;
1210}
1211
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001213 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001214 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215{
Mathias Agopian96f08192010-06-02 23:28:45 -07001216 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 return layer;
1219}
1220
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001221sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001222 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224{
Mathias Agopian96f08192010-06-02 23:28:45 -07001225 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 return layer;
1228}
1229
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001230sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001231 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001232 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233{
Mathias Agopian96f08192010-06-02 23:28:45 -07001234 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 return layer;
1237}
1238
Mathias Agopian96f08192010-06-02 23:28:45 -07001239status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240{
Mathias Agopian9a112062009-04-17 19:36:26 -07001241 /*
1242 * called by the window manager, when a surface should be marked for
1243 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001244 *
1245 * The surface is removed from the current and drawing lists, but placed
1246 * in the purgatory queue, so it's not destroyed right-away (we need
1247 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001248 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001249
Mathias Agopian48d819a2009-09-10 19:41:18 -07001250 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001251 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001252 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001253 if (layer != 0) {
1254 err = purgatorizeLayer_l(layer);
1255 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001256 setTransactionFlags(eTransactionNeeded);
1257 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001258 }
1259 return err;
1260}
1261
1262status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1263{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001264 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001265
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001266 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001267 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001268 sp<LayerBaseClient> layer;
1269 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001270 MessageDestroySurface(
1271 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1272 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001273 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001274 sp<LayerBaseClient> l(layer);
1275 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001276 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001277 /*
1278 * remove the layer from the current list -- chances are that it's
1279 * not in the list anyway, because it should have been removed
1280 * already upon request of the client (eg: window manager).
1281 * However, a buggy client could have not done that.
1282 * Since we know we don't have any more clients, we don't need
1283 * to use the purgatory.
1284 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001285 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001286 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1287 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001288 return true;
1289 }
1290 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001291
Mathias Agopianbb641242010-05-18 17:06:55 -07001292 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 return NO_ERROR;
1294}
1295
1296status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001297 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 int32_t count,
1299 const layer_state_t* states)
1300{
1301 Mutex::Autolock _l(mStateLock);
1302 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001304 const layer_state_t& s(states[i]);
1305 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001306 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 if (what & ePositionChanged) {
1309 if (layer->setPosition(s.x, s.y))
1310 flags |= eTraversalNeeded;
1311 }
1312 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001313 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001315 mCurrentState.layersSortedByZ.removeAt(idx);
1316 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 // we need traversal (state changed)
1318 // AND transaction (list changed)
1319 flags |= eTransactionNeeded|eTraversalNeeded;
1320 }
1321 }
1322 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001323 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001325 mResizeTransationPending = true;
1326 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 }
1328 if (what & eAlphaChanged) {
1329 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1330 flags |= eTraversalNeeded;
1331 }
1332 if (what & eMatrixChanged) {
1333 if (layer->setMatrix(s.matrix))
1334 flags |= eTraversalNeeded;
1335 }
1336 if (what & eTransparentRegionChanged) {
1337 if (layer->setTransparentRegionHint(s.transparentRegion))
1338 flags |= eTraversalNeeded;
1339 }
1340 if (what & eVisibilityChanged) {
1341 if (layer->setFlags(s.flags, s.mask))
1342 flags |= eTraversalNeeded;
1343 }
1344 }
1345 }
1346 if (flags) {
1347 setTransactionFlags(flags);
1348 }
1349 return NO_ERROR;
1350}
1351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352void SurfaceFlinger::screenReleased(int dpy)
1353{
1354 // this may be called by a signal handler, we can't do too much in here
1355 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1356 signalEvent();
1357}
1358
1359void SurfaceFlinger::screenAcquired(int dpy)
1360{
1361 // this may be called by a signal handler, we can't do too much in here
1362 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1363 signalEvent();
1364}
1365
1366status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1367{
1368 const size_t SIZE = 1024;
1369 char buffer[SIZE];
1370 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001371 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 snprintf(buffer, SIZE, "Permission Denial: "
1373 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1374 IPCThreadState::self()->getCallingPid(),
1375 IPCThreadState::self()->getCallingUid());
1376 result.append(buffer);
1377 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001378
1379 // figure out if we're stuck somewhere
1380 const nsecs_t now = systemTime();
1381 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1382 const nsecs_t inTransaction(mDebugInTransaction);
1383 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1384 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1385
1386 // Try to get the main lock, but don't insist if we can't
1387 // (this would indicate SF is stuck, but we want to be able to
1388 // print something in dumpsys).
1389 int retry = 3;
1390 while (mStateLock.tryLock()<0 && --retry>=0) {
1391 usleep(1000000);
1392 }
1393 const bool locked(retry >= 0);
1394 if (!locked) {
1395 snprintf(buffer, SIZE,
1396 "SurfaceFlinger appears to be unresponsive, "
1397 "dumping anyways (no locks held)\n");
1398 result.append(buffer);
1399 }
1400
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1402 const size_t count = currentLayers.size();
1403 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001404 const sp<LayerBase>& layer(currentLayers[i]);
1405 layer->dump(result, buffer, SIZE);
1406 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 s.transparentRegion.dump(result, "transparentRegion");
1408 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1409 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1410 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001411
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 mWormholeRegion.dump(result, "WormholeRegion");
1413 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1414 snprintf(buffer, SIZE,
1415 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1416 mFreezeDisplay?"yes":"no", mFreezeCount,
1417 mCurrentState.orientation, hw.canDraw());
1418 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001419 snprintf(buffer, SIZE,
1420 " last eglSwapBuffers() time: %f us\n"
1421 " last transaction time : %f us\n",
1422 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1423 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001424
Mathias Agopian9795c422009-08-26 16:36:26 -07001425 if (inSwapBuffersDuration || !locked) {
1426 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1427 inSwapBuffersDuration/1000.0);
1428 result.append(buffer);
1429 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001430
Mathias Agopian9795c422009-08-26 16:36:26 -07001431 if (inTransactionDuration || !locked) {
1432 snprintf(buffer, SIZE, " transaction time: %f us\n",
1433 inTransactionDuration/1000.0);
1434 result.append(buffer);
1435 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001436
Mathias Agopian3330b202009-10-05 17:07:12 -07001437 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001438 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001439
1440 if (locked) {
1441 mStateLock.unlock();
1442 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444 write(fd, result.string(), result.size());
1445 return NO_ERROR;
1446}
1447
1448status_t SurfaceFlinger::onTransact(
1449 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1450{
1451 switch (code) {
1452 case CREATE_CONNECTION:
1453 case OPEN_GLOBAL_TRANSACTION:
1454 case CLOSE_GLOBAL_TRANSACTION:
1455 case SET_ORIENTATION:
1456 case FREEZE_DISPLAY:
1457 case UNFREEZE_DISPLAY:
1458 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 {
1460 // codes that require permission check
1461 IPCThreadState* ipc = IPCThreadState::self();
1462 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001463 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001464 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1465 LOGE("Permission Denial: "
1466 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1467 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001469 break;
1470 }
1471 case CAPTURE_SCREEN:
1472 {
1473 // codes that require permission check
1474 IPCThreadState* ipc = IPCThreadState::self();
1475 const int pid = ipc->getCallingPid();
1476 const int uid = ipc->getCallingUid();
1477 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1478 LOGE("Permission Denial: "
1479 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1480 return PERMISSION_DENIED;
1481 }
1482 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 }
1484 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001485
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1487 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001488 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001489 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1490 IPCThreadState* ipc = IPCThreadState::self();
1491 const int pid = ipc->getCallingPid();
1492 const int uid = ipc->getCallingUid();
1493 LOGE("Permission Denial: "
1494 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 return PERMISSION_DENIED;
1496 }
1497 int n;
1498 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001499 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001500 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 return NO_ERROR;
1502 case 1002: // SHOW_UPDATES
1503 n = data.readInt32();
1504 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1505 return NO_ERROR;
1506 case 1003: // SHOW_BACKGROUND
1507 n = data.readInt32();
1508 mDebugBackground = n ? 1 : 0;
1509 return NO_ERROR;
1510 case 1004:{ // repaint everything
1511 Mutex::Autolock _l(mStateLock);
1512 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1513 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1514 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001515 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001517 case 1005:{ // force transaction
1518 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1519 return NO_ERROR;
1520 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001521 case 1006:{ // enable/disable GraphicLog
1522 int enabled = data.readInt32();
1523 GraphicLog::getInstance().setEnabled(enabled);
1524 return NO_ERROR;
1525 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526 case 1007: // set mFreezeCount
1527 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001528 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529 return NO_ERROR;
1530 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001531 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 reply->writeInt32(0);
1533 reply->writeInt32(mDebugRegion);
1534 reply->writeInt32(mDebugBackground);
1535 return NO_ERROR;
1536 case 1013: {
1537 Mutex::Autolock _l(mStateLock);
1538 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1539 reply->writeInt32(hw.getPageFlipCount());
1540 }
1541 return NO_ERROR;
1542 }
1543 }
1544 return err;
1545}
1546
1547// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001548
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001549status_t SurfaceFlinger::captureScreen(DisplayID dpy,
1550 sp<IMemoryHeap>* heap,
1551 uint32_t* width, uint32_t* height, PixelFormat* format)
1552{
1553 // only one display supported for now
1554 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1555 return BAD_VALUE;
1556
1557 if (!GLExtensions::getInstance().haveFramebufferObject())
1558 return INVALID_OPERATION;
1559
1560 class MessageCaptureScreen : public MessageBase {
1561 SurfaceFlinger* flinger;
1562 DisplayID dpy;
1563 sp<IMemoryHeap>* heap;
1564 uint32_t* w;
1565 uint32_t* h;
1566 PixelFormat* f;
1567 status_t result;
1568 public:
1569 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
1570 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f)
1571 : flinger(flinger), dpy(dpy),
1572 heap(heap), w(w), h(h), f(f), result(PERMISSION_DENIED)
1573 {
1574 }
1575 status_t getResult() const {
1576 return result;
1577 }
1578 virtual bool handler() {
1579 Mutex::Autolock _l(flinger->mStateLock);
1580
1581 // if we have secure windows, never allow the screen capture
1582 if (flinger->mSecureFrameBuffer)
1583 return true;
1584
1585 // make sure to clear all GL error flags
1586 while ( glGetError() != GL_NO_ERROR ) ;
1587
1588 // get screen geometry
1589 const DisplayHardware& hw(flinger->graphicPlane(dpy).displayHardware());
1590 const uint32_t sw = hw.getWidth();
1591 const uint32_t sh = hw.getHeight();
1592 const Region screenBounds(hw.bounds());
1593 const size_t size = sw * sh * 4;
1594
1595 // create a FBO
1596 GLuint name, tname;
1597 glGenRenderbuffersOES(1, &tname);
1598 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
1599 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
1600 glGenFramebuffersOES(1, &name);
1601 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
1602 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
1603 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
1604
1605 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
1606 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
1607
1608 // invert everything, b/c glReadPixel() below will invert the FB
1609 glMatrixMode(GL_PROJECTION);
1610 glPushMatrix();
1611 glLoadIdentity();
1612 glOrthof(0, sw, 0, sh, 0, 1);
1613 glMatrixMode(GL_MODELVIEW);
1614
1615 // redraw the screen entirely...
1616 glClearColor(0,0,0,1);
1617 glClear(GL_COLOR_BUFFER_BIT);
1618 const Vector< sp<LayerBase> >& layers(
1619 flinger->mVisibleLayersSortedByZ);
1620 const size_t count = layers.size();
1621 for (size_t i=0 ; i<count ; ++i) {
1622 const sp<LayerBase>& layer(layers[i]);
1623 if (!strcmp(layer->getTypeId(), "LayerBuffer")) {
1624 // we cannot render LayerBuffer because it doens't
1625 // use OpenGL, and won't show-up in the FBO.
1626 continue;
1627 }
1628 layer->draw(screenBounds);
1629 }
1630
1631 glMatrixMode(GL_PROJECTION);
1632 glPopMatrix();
1633 glMatrixMode(GL_MODELVIEW);
1634
1635 // check for errors and return screen capture
1636 if (glGetError() != GL_NO_ERROR) {
1637 // error while rendering
1638 result = INVALID_OPERATION;
1639 } else {
1640 // allocate shared memory large enough to hold the
1641 // screen capture
1642 sp<MemoryHeapBase> base(
1643 new MemoryHeapBase(size, 0, "screen-capture") );
1644 void* const ptr = base->getBase();
1645 if (ptr) {
1646 // capture the screen with glReadPixels()
1647 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
1648 if (glGetError() == GL_NO_ERROR) {
1649 *heap = base;
1650 *w = sw;
1651 *h = sh;
1652 *f = PIXEL_FORMAT_RGBA_8888;
1653 result = NO_ERROR;
1654 }
1655 } else {
1656 result = NO_MEMORY;
1657 }
1658 }
1659 } else {
1660 result = BAD_VALUE;
1661 }
1662
1663 // release FBO resources
1664 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1665 glDeleteRenderbuffersOES(1, &tname);
1666 glDeleteFramebuffersOES(1, &name);
1667 return true;
1668 }
1669 };
1670
1671 sp<MessageBase> msg = new MessageCaptureScreen(this,
1672 dpy, heap, width, height, format);
1673 status_t res = postMessageSync(msg);
1674 if (res == NO_ERROR) {
1675 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
1676 }
1677 return res;
1678}
1679
1680// ---------------------------------------------------------------------------
1681
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001682sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1683{
1684 sp<Layer> result;
1685 Mutex::Autolock _l(mStateLock);
1686 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1687 return result;
1688}
1689
1690// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691
Mathias Agopian96f08192010-06-02 23:28:45 -07001692Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001693 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695}
1696
Mathias Agopian96f08192010-06-02 23:28:45 -07001697Client::~Client()
1698{
Mathias Agopian96f08192010-06-02 23:28:45 -07001699 const size_t count = mLayers.size();
1700 for (size_t i=0 ; i<count ; i++) {
1701 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1702 if (layer != 0) {
1703 mFlinger->removeLayer(layer);
1704 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 }
1706}
1707
Mathias Agopian96f08192010-06-02 23:28:45 -07001708status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001709 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001711
Mathias Agopian96f08192010-06-02 23:28:45 -07001712ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1713{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001714 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001715 mLayers.add(name, layer);
1716 return name;
1717}
1718
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001719void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001720{
Mathias Agopian96f08192010-06-02 23:28:45 -07001721 // we do a linear search here, because this doesn't happen often
1722 const size_t count = mLayers.size();
1723 for (size_t i=0 ; i<count ; i++) {
1724 if (mLayers.valueAt(i) == layer) {
1725 mLayers.removeItemsAt(i, 1);
1726 break;
1727 }
1728 }
1729}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001730sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1731 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001732 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1733 if (layer != 0) {
1734 lbc = layer.promote();
1735 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001736 }
1737 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738}
1739
Mathias Agopian96f08192010-06-02 23:28:45 -07001740sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001741 return 0;
1742}
1743ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1744 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745}
Mathias Agopian96f08192010-06-02 23:28:45 -07001746sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001747 ISurfaceComposerClient::surface_data_t* params, int pid,
1748 const String8& name,
1749 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 uint32_t flags)
1751{
Mathias Agopian96f08192010-06-02 23:28:45 -07001752 return mFlinger->createSurface(this, pid, name, params,
1753 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754}
Mathias Agopian96f08192010-06-02 23:28:45 -07001755status_t Client::destroySurface(SurfaceID sid) {
1756 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757}
Mathias Agopian96f08192010-06-02 23:28:45 -07001758status_t Client::setState(int32_t count, const layer_state_t* states) {
1759 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760}
1761
1762// ---------------------------------------------------------------------------
1763
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001764UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1765 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1766{
1767 const int pgsize = getpagesize();
1768 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1769
1770 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1771 "SurfaceFlinger Client control-block");
1772
1773 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1774 if (ctrlblk) { // construct the shared structure in-place.
1775 new(ctrlblk) SharedClient;
1776 }
1777}
1778
1779UserClient::~UserClient()
1780{
1781 if (ctrlblk) {
1782 ctrlblk->~SharedClient(); // destroy our shared-structure.
1783 }
1784
1785 /*
1786 * When a UserClient dies, it's unclear what to do exactly.
1787 * We could go ahead and destroy all surfaces linked to that client
1788 * however, it wouldn't be fair to the main Client
1789 * (usually the the window-manager), which might want to re-target
1790 * the layer to another UserClient.
1791 * I think the best is to do nothing, or not much; in most cases the
1792 * WM itself will go ahead and clean things up when it detects a client of
1793 * his has died.
1794 * The remaining question is what to display? currently we keep
1795 * just keep the current buffer.
1796 */
1797}
1798
1799status_t UserClient::initCheck() const {
1800 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1801}
1802
1803void UserClient::detachLayer(const Layer* layer)
1804{
1805 int32_t name = layer->getToken();
1806 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001807 int32_t mask = 1LU<<name;
1808 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1809 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1810 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001811 }
1812}
1813
1814sp<IMemoryHeap> UserClient::getControlBlock() const {
1815 return mCblkHeap;
1816}
1817
1818ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1819{
1820 int32_t name = NAME_NOT_FOUND;
1821 sp<Layer> layer(mFlinger->getLayer(sur));
1822 if (layer == 0) return name;
1823
Mathias Agopian579b3f82010-06-08 19:54:15 -07001824 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001825 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07001826 if ((name >= 0) && (layer->getClient() == this))
1827 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001828
1829 name = 0;
1830 do {
1831 int32_t mask = 1LU<<name;
1832 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1833 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001834 status_t err = layer->setToken(
1835 const_cast<UserClient*>(this), ctrlblk, name);
1836 if (err != NO_ERROR) {
1837 // free the name
1838 android_atomic_and(~mask, &mBitmap);
1839 name = err;
1840 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001841 break;
1842 }
1843 if (++name > 31)
1844 name = NO_MEMORY;
1845 } while(name >= 0);
1846
Mathias Agopian53503a92010-06-08 15:40:56 -07001847 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1848 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001849 return name;
1850}
1851
1852sp<ISurface> UserClient::createSurface(
1853 ISurfaceComposerClient::surface_data_t* params, int pid,
1854 const String8& name,
1855 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1856 uint32_t flags) {
1857 return 0;
1858}
1859status_t UserClient::destroySurface(SurfaceID sid) {
1860 return INVALID_OPERATION;
1861}
1862status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1863 return INVALID_OPERATION;
1864}
1865
1866// ---------------------------------------------------------------------------
1867
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001868GraphicPlane::GraphicPlane()
1869 : mHw(0)
1870{
1871}
1872
1873GraphicPlane::~GraphicPlane() {
1874 delete mHw;
1875}
1876
1877bool GraphicPlane::initialized() const {
1878 return mHw ? true : false;
1879}
1880
Mathias Agopian2b92d892010-02-08 15:49:35 -08001881int GraphicPlane::getWidth() const {
1882 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883}
1884
Mathias Agopian2b92d892010-02-08 15:49:35 -08001885int GraphicPlane::getHeight() const {
1886 return mHeight;
1887}
1888
1889void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1890{
1891 mHw = hw;
1892
1893 // initialize the display orientation transform.
1894 // it's a constant that should come from the display driver.
1895 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1896 char property[PROPERTY_VALUE_MAX];
1897 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1898 //displayOrientation
1899 switch (atoi(property)) {
1900 case 90:
1901 displayOrientation = ISurfaceComposer::eOrientation90;
1902 break;
1903 case 270:
1904 displayOrientation = ISurfaceComposer::eOrientation270;
1905 break;
1906 }
1907 }
1908
1909 const float w = hw->getWidth();
1910 const float h = hw->getHeight();
1911 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1912 &mDisplayTransform);
1913 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1914 mDisplayWidth = h;
1915 mDisplayHeight = w;
1916 } else {
1917 mDisplayWidth = w;
1918 mDisplayHeight = h;
1919 }
1920
1921 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922}
1923
1924status_t GraphicPlane::orientationToTransfrom(
1925 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001926{
1927 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928 switch (orientation) {
1929 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001930 flags = Transform::ROT_0;
1931 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001932 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001933 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001934 break;
1935 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001936 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937 break;
1938 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001939 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001940 break;
1941 default:
1942 return BAD_VALUE;
1943 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001944 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945 return NO_ERROR;
1946}
1947
1948status_t GraphicPlane::setOrientation(int orientation)
1949{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950 // If the rotation can be handled in hardware, this is where
1951 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001952
1953 const DisplayHardware& hw(displayHardware());
1954 const float w = mDisplayWidth;
1955 const float h = mDisplayHeight;
1956 mWidth = int(w);
1957 mHeight = int(h);
1958
1959 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001960 GraphicPlane::orientationToTransfrom(orientation, w, h,
1961 &orientationTransform);
1962 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1963 mWidth = int(h);
1964 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001966
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001967 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001968 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001969 return NO_ERROR;
1970}
1971
1972const DisplayHardware& GraphicPlane::displayHardware() const {
1973 return *mHw;
1974}
1975
1976const Transform& GraphicPlane::transform() const {
1977 return mGlobalTransform;
1978}
1979
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001980EGLDisplay GraphicPlane::getEGLDisplay() const {
1981 return mHw->getEGLDisplay();
1982}
1983
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984// ---------------------------------------------------------------------------
1985
1986}; // namespace android