blob: 006bb1076ff2b3ee1a7a681ead0ce16e67d64c72 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include <pixelflinger/pixelflinger.h>
44#include <GLES/gl.h>
45
46#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070047#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
49#include "LayerBlur.h"
50#include "LayerBuffer.h"
51#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070055#include "DisplayHardware/HWComposer.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),
Mathias Agopiana350ff92010-08-10 17:14:02 -070080 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 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
Mathias Agopiana350ff92010-08-10 17:14:02 -0700373 if (UNLIKELY(mHwWorkListDirty)) {
374 // build the h/w work list
375 handleWorkList();
376 }
377
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700379 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 // repaint the framebuffer (if needed)
381 handleRepaint();
382
Mathias Agopian74faca22009-09-17 16:18:16 -0700383 // inform the h/w that we're done compositing
384 hw.compositionComplete();
385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 // release the clients before we flip ('cause flip might block)
387 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 postFramebuffer();
390 } else {
391 // pretend we did the post
392 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 usleep(16667); // 60 fps period
394 }
395 return true;
396}
397
398void SurfaceFlinger::postFramebuffer()
399{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 if (!mInvalidRegion.isEmpty()) {
401 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700402 const nsecs_t now = systemTime();
403 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700405 mLastSwapBufferTime = systemTime() - now;
406 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 }
409}
410
411void SurfaceFlinger::handleConsoleEvents()
412{
413 // something to do with the console
414 const DisplayHardware& hw = graphicPlane(0).displayHardware();
415
416 int what = android_atomic_and(0, &mConsoleSignals);
417 if (what & eConsoleAcquired) {
418 hw.acquireScreen();
419 }
420
421 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700422 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 hw.releaseScreen();
425 }
426
427 if (what & eConsoleReleased) {
428 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 hw.releaseScreen();
430 } else {
431 mDeferReleaseConsole = true;
432 }
433 }
434
435 mDirtyRegion.set(hw.bounds());
436}
437
438void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
439{
Mathias Agopian3d579642009-06-04 18:46:21 -0700440 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441
Mathias Agopian4da75192010-08-10 17:19:56 -0700442 /*
443 * Perform and commit the transaction
444 */
445
Mathias Agopian3d579642009-06-04 18:46:21 -0700446 { // scope for the lock
447 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700448 const nsecs_t now = systemTime();
449 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700450 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700451 mLastTransactionTime = systemTime() - now;
452 mDebugInTransaction = 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700453 mHwWorkListDirty = true;
Mathias Agopian4da75192010-08-10 17:19:56 -0700454 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700455 }
456
Mathias Agopian4da75192010-08-10 17:19:56 -0700457 /*
458 * Clean-up all layers that went away
459 * (do this without the lock held)
460 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700461
Mathias Agopian3d579642009-06-04 18:46:21 -0700462 const size_t count = ditchedLayers.size();
463 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700464 if (ditchedLayers[i] != 0) {
465 //LOGD("ditching layer %p", ditchedLayers[i].get());
466 ditchedLayers[i]->ditch();
467 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700468 }
469}
470
471void SurfaceFlinger::handleTransactionLocked(
472 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
473{
474 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 const size_t count = currentLayers.size();
476
477 /*
478 * Traversal of the children
479 * (perform the transaction for each of them if needed)
480 */
481
482 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
483 if (layersNeedTransaction) {
484 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700485 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
487 if (!trFlags) continue;
488
489 const uint32_t flags = layer->doTransaction(0);
490 if (flags & Layer::eVisibleRegion)
491 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 }
493 }
494
495 /*
496 * Perform our own transaction if needed
497 */
498
499 if (transactionFlags & eTransactionNeeded) {
500 if (mCurrentState.orientation != mDrawingState.orientation) {
501 // the orientation has changed, recompute all visible regions
502 // and invalidate everything.
503
504 const int dpy = 0;
505 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700506 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 GraphicPlane& plane(graphicPlane(dpy));
508 plane.setOrientation(orientation);
509
510 // update the shared control block
511 const DisplayHardware& hw(plane.displayHardware());
512 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
513 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800514 dcblk->w = plane.getWidth();
515 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516
517 mVisibleRegionsDirty = true;
518 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 }
520
521 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
522 // freezing or unfreezing the display -> trigger animation if needed
523 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800524 if (mFreezeDisplay)
525 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 }
527
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700528 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
529 // layers have been added
530 mVisibleRegionsDirty = true;
531 }
532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 // some layers might have been removed, so
534 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700535 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700536 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700538 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700539 const size_t count = previousLayers.size();
540 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700541 const sp<LayerBase>& layer(previousLayers[i]);
542 if (currentLayers.indexOf( layer ) < 0) {
543 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700544 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700545 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700546 }
547 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 }
550
551 commitTransaction();
552}
553
554sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
555{
556 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
557}
558
559void SurfaceFlinger::computeVisibleRegions(
560 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
561{
562 const GraphicPlane& plane(graphicPlane(0));
563 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700564 const DisplayHardware& hw(plane.displayHardware());
565 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566
567 Region aboveOpaqueLayers;
568 Region aboveCoveredLayers;
569 Region dirty;
570
571 bool secureFrameBuffer = false;
572
573 size_t i = currentLayers.size();
574 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700575 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 layer->validateVisibility(planeTransform);
577
578 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700579 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580
Mathias Agopianab028732010-03-16 16:41:46 -0700581 /*
582 * opaqueRegion: area of a surface that is fully opaque.
583 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700585
586 /*
587 * visibleRegion: area of a surface that is visible on screen
588 * and not fully transparent. This is essentially the layer's
589 * footprint minus the opaque regions above it.
590 * Areas covered by a translucent surface are considered visible.
591 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700593
594 /*
595 * coveredRegion: area of a surface that is covered by all
596 * visible regions above it (which includes the translucent areas).
597 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700599
600
601 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700602 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700604 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700606 visibleRegion.andSelf(screenRegion);
607 if (!visibleRegion.isEmpty()) {
608 // Remove the transparent area from the visible region
609 if (translucent) {
610 visibleRegion.subtractSelf(layer->transparentRegionScreen);
611 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612
Mathias Agopianab028732010-03-16 16:41:46 -0700613 // compute the opaque region
614 const int32_t layerOrientation = layer->getOrientation();
615 if (s.alpha==255 && !translucent &&
616 ((layerOrientation & Transform::ROT_INVALID) == false)) {
617 // the opaque region is the layer's footprint
618 opaqueRegion = visibleRegion;
619 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 }
621 }
622
Mathias Agopianab028732010-03-16 16:41:46 -0700623 // Clip the covered region to the visible region
624 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
625
626 // Update aboveCoveredLayers for next (lower) layer
627 aboveCoveredLayers.orSelf(visibleRegion);
628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 // subtract the opaque region covered by the layers above us
630 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631
632 // compute this layer's dirty region
633 if (layer->contentDirty) {
634 // we need to invalidate the whole region
635 dirty = visibleRegion;
636 // as well, as the old visible region
637 dirty.orSelf(layer->visibleRegionScreen);
638 layer->contentDirty = false;
639 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700640 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700641 * the exposed region consists of two components:
642 * 1) what's VISIBLE now and was COVERED before
643 * 2) what's EXPOSED now less what was EXPOSED before
644 *
645 * note that (1) is conservative, we start with the whole
646 * visible region but only keep what used to be covered by
647 * something -- which mean it may have been exposed.
648 *
649 * (2) handles areas that were not covered by anything but got
650 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700651 */
Mathias Agopianab028732010-03-16 16:41:46 -0700652 const Region newExposed = visibleRegion - coveredRegion;
653 const Region oldVisibleRegion = layer->visibleRegionScreen;
654 const Region oldCoveredRegion = layer->coveredRegionScreen;
655 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
656 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 }
658 dirty.subtractSelf(aboveOpaqueLayers);
659
660 // accumulate to the screen dirty region
661 dirtyRegion.orSelf(dirty);
662
Mathias Agopianab028732010-03-16 16:41:46 -0700663 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
666 // Store the visible region is screen space
667 layer->setVisibleRegion(visibleRegion);
668 layer->setCoveredRegion(coveredRegion);
669
Mathias Agopian97011222009-07-28 10:57:27 -0700670 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 if (layer->isSecure() && !visibleRegion.isEmpty()) {
672 secureFrameBuffer = true;
673 }
674 }
675
Mathias Agopian97011222009-07-28 10:57:27 -0700676 // invalidate the areas where a layer was removed
677 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
678 mDirtyRegionRemovedLayer.clear();
679
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 mSecureFrameBuffer = secureFrameBuffer;
681 opaqueRegion = aboveOpaqueLayers;
682}
683
684
685void SurfaceFlinger::commitTransaction()
686{
687 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700688 mResizeTransationPending = false;
689 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690}
691
692void SurfaceFlinger::handlePageFlip()
693{
694 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700695 LayerVector& currentLayers(
696 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 visibleRegions |= lockPageFlip(currentLayers);
698
699 const DisplayHardware& hw = graphicPlane(0).displayHardware();
700 const Region screenRegion(hw.bounds());
701 if (visibleRegions) {
702 Region opaqueRegion;
703 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700704
705 /*
706 * rebuild the visible layer list
707 */
708 mVisibleLayersSortedByZ.clear();
709 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
710 size_t count = currentLayers.size();
711 mVisibleLayersSortedByZ.setCapacity(count);
712 for (size_t i=0 ; i<count ; i++) {
713 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
714 mVisibleLayersSortedByZ.add(currentLayers[i]);
715 }
716
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717 mWormholeRegion = screenRegion.subtract(opaqueRegion);
718 mVisibleRegionsDirty = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700719 mHwWorkListDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 }
721
722 unlockPageFlip(currentLayers);
723 mDirtyRegion.andSelf(screenRegion);
724}
725
726bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
727{
728 bool recomputeVisibleRegions = false;
729 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700730 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700732 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 layer->lockPageFlip(recomputeVisibleRegions);
734 }
735 return recomputeVisibleRegions;
736}
737
738void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
739{
740 const GraphicPlane& plane(graphicPlane(0));
741 const Transform& planeTransform(plane.transform());
742 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700743 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700745 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 layer->unlockPageFlip(planeTransform, mDirtyRegion);
747 }
748}
749
Mathias Agopiana350ff92010-08-10 17:14:02 -0700750void SurfaceFlinger::handleWorkList()
751{
752 mHwWorkListDirty = false;
753 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
754 if (hwc.initCheck() == NO_ERROR) {
755 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
756 const size_t count = currentLayers.size();
757 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700758 hwc_layer_t* const cur(hwc.getLayers());
759 for (size_t i=0 ; cur && i<count ; i++) {
760 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700761 }
762 }
763}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765void SurfaceFlinger::handleRepaint()
766{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700767 // compute the invalid region
768 mInvalidRegion.orSelf(mDirtyRegion);
769 if (mInvalidRegion.isEmpty()) {
770 // nothing to do
771 return;
772 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773
774 if (UNLIKELY(mDebugRegion)) {
775 debugFlashRegions();
776 }
777
Mathias Agopianb8a55602009-06-26 19:06:36 -0700778 // set the frame buffer
779 const DisplayHardware& hw(graphicPlane(0).displayHardware());
780 glMatrixMode(GL_MODELVIEW);
781 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782
783 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700784 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
785 (flags & DisplayHardware::BUFFER_PRESERVED))
786 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700787 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
788 // takes a rectangle, we must make sure to update that whole
789 // rectangle in that case
790 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700791 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700792 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 mDirtyRegion.set(mInvalidRegion.bounds());
794 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700795 // in the BUFFER_PRESERVED case, obviously, we can update only
796 // what's needed and nothing more.
797 // NOTE: this is NOT a common case, as preserving the backbuffer
798 // is costly and usually involves copying the whole update back.
799 }
800 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700801 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700802 // We need to redraw the rectangle that will be updated
803 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700804 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700805 // rectangle instead of a region (see DisplayHardware::flip())
806 mDirtyRegion.set(mInvalidRegion.bounds());
807 } else {
808 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 mDirtyRegion.set(hw.bounds());
810 mInvalidRegion = mDirtyRegion;
811 }
812 }
813
814 // compose all surfaces
815 composeSurfaces(mDirtyRegion);
816
817 // clear the dirty regions
818 mDirtyRegion.clear();
819}
820
821void SurfaceFlinger::composeSurfaces(const Region& dirty)
822{
823 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
824 // should never happen unless the window manager has a bug
825 // draw something...
826 drawWormhole();
827 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700828
829 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700830 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700831 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700832
833 const DisplayHardware& hw(graphicPlane(0).displayHardware());
834 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700835 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700836
Mathias Agopian45721772010-08-12 15:03:26 -0700837 LOGE_IF(cur && hwc.getNumLayers() != count,
838 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
839 hwc.getNumLayers(), count);
840
841 // just to be extra-safe, use the smallest count
842 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
843
844 /*
845 * update the per-frame h/w composer data for each layer
846 * and build the transparent region of the FB
847 */
848 Region transparent;
849 if (cur) {
850 for (size_t i=0 ; i<count ; i++) {
851 const sp<LayerBase>& layer(layers[i]);
852 layer->setPerFrameData(&cur[i]);
853 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
854 if (!(layer->needsBlending())) {
855 transparent.orSelf(layer->visibleRegionScreen);
856 }
857 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700858 }
859 err = hwc.prepare();
860 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
861 }
862
Mathias Agopian45721772010-08-12 15:03:26 -0700863 /*
864 * clear the area of the FB that need to be transparent
865 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700866 transparent.andSelf(dirty);
867 if (!transparent.isEmpty()) {
868 glClearColor(0,0,0,0);
869 Region::const_iterator it = transparent.begin();
870 Region::const_iterator const end = transparent.end();
871 const int32_t height = hw.getHeight();
872 while (it != end) {
873 const Rect& r(*it++);
874 const GLint sy = height - (r.top + r.height());
875 glScissor(r.left, sy, r.width(), r.height());
876 glClear(GL_COLOR_BUFFER_BIT);
877 }
878 }
Mathias Agopian45721772010-08-12 15:03:26 -0700879
880
881 /*
882 * and then, render the layers targeted at the framebuffer
883 */
884 for (size_t i=0 ; i<count ; i++) {
885 if (cur) {
886 if (!(cur[i].compositionType == HWC_FRAMEBUFFER) ||
887 cur[i].flags & HWC_SKIP_LAYER) {
888 // skip layers handled by the HAL
889 continue;
890 }
891 }
892 const sp<LayerBase>& layer(layers[i]);
893 const Region clip(dirty.intersect(layer->visibleRegionScreen));
894 if (!clip.isEmpty()) {
895 layer->draw(clip);
896 }
897 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898}
899
900void SurfaceFlinger::unlockClients()
901{
902 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
903 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700904 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700906 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 layer->finishPageFlip();
908 }
909}
910
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911void SurfaceFlinger::debugFlashRegions()
912{
Mathias Agopian0a917752010-06-14 21:20:00 -0700913 const DisplayHardware& hw(graphicPlane(0).displayHardware());
914 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700915
Mathias Agopian0a917752010-06-14 21:20:00 -0700916 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
917 (flags & DisplayHardware::BUFFER_PRESERVED))) {
918 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
919 mDirtyRegion.bounds() : hw.bounds());
920 composeSurfaces(repaint);
921 }
922
923 TextureManager::deactivateTextures();
924
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 glDisable(GL_BLEND);
926 glDisable(GL_DITHER);
927 glDisable(GL_SCISSOR_TEST);
928
Mathias Agopian0926f502009-05-04 14:17:04 -0700929 static int toggle = 0;
930 toggle = 1 - toggle;
931 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700932 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700933 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700934 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700935 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936
Mathias Agopian20f68782009-05-11 00:03:41 -0700937 Region::const_iterator it = mDirtyRegion.begin();
938 Region::const_iterator const end = mDirtyRegion.end();
939 while (it != end) {
940 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 GLfloat vertices[][2] = {
942 { r.left, r.top },
943 { r.left, r.bottom },
944 { r.right, r.bottom },
945 { r.right, r.top }
946 };
947 glVertexPointer(2, GL_FLOAT, 0, vertices);
948 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
949 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700950
Mathias Agopianb8a55602009-06-26 19:06:36 -0700951 if (mInvalidRegion.isEmpty()) {
952 mDirtyRegion.dump("mDirtyRegion");
953 mInvalidRegion.dump("mInvalidRegion");
954 }
955 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956
957 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700958 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959
960 glEnable(GL_SCISSOR_TEST);
961 //mDirtyRegion.dump("mDirtyRegion");
962}
963
964void SurfaceFlinger::drawWormhole() const
965{
966 const Region region(mWormholeRegion.intersect(mDirtyRegion));
967 if (region.isEmpty())
968 return;
969
970 const DisplayHardware& hw(graphicPlane(0).displayHardware());
971 const int32_t width = hw.getWidth();
972 const int32_t height = hw.getHeight();
973
974 glDisable(GL_BLEND);
975 glDisable(GL_DITHER);
976
977 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700978 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700979 Region::const_iterator it = region.begin();
980 Region::const_iterator const end = region.end();
981 while (it != end) {
982 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 const GLint sy = height - (r.top + r.height());
984 glScissor(r.left, sy, r.width(), r.height());
985 glClear(GL_COLOR_BUFFER_BIT);
986 }
987 } else {
988 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
989 { width, height }, { 0, height } };
990 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
991 glVertexPointer(2, GL_SHORT, 0, vertices);
992 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
993 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian0a917752010-06-14 21:20:00 -0700994#if defined(GL_OES_texture_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700995 if (GLExtensions::getInstance().haveTextureExternal()) {
996 glDisable(GL_TEXTURE_EXTERNAL_OES);
997 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700998#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 glEnable(GL_TEXTURE_2D);
1000 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1001 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1002 glMatrixMode(GL_TEXTURE);
1003 glLoadIdentity();
1004 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001005 Region::const_iterator it = region.begin();
1006 Region::const_iterator const end = region.end();
1007 while (it != end) {
1008 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 const GLint sy = height - (r.top + r.height());
1010 glScissor(r.left, sy, r.width(), r.height());
1011 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1012 }
1013 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1014 }
1015}
1016
1017void SurfaceFlinger::debugShowFPS() const
1018{
1019 static int mFrameCount;
1020 static int mLastFrameCount = 0;
1021 static nsecs_t mLastFpsTime = 0;
1022 static float mFps = 0;
1023 mFrameCount++;
1024 nsecs_t now = systemTime();
1025 nsecs_t diff = now - mLastFpsTime;
1026 if (diff > ms2ns(250)) {
1027 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1028 mLastFpsTime = now;
1029 mLastFrameCount = mFrameCount;
1030 }
1031 // XXX: mFPS has the value we want
1032 }
1033
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001034status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035{
1036 Mutex::Autolock _l(mStateLock);
1037 addLayer_l(layer);
1038 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1039 return NO_ERROR;
1040}
1041
Mathias Agopian96f08192010-06-02 23:28:45 -07001042status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1043{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001044 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001045 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1046}
1047
1048ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1049 const sp<LayerBaseClient>& lbc)
1050{
1051 Mutex::Autolock _l(mStateLock);
1052
1053 // attach this layer to the client
1054 ssize_t name = client->attachLayer(lbc);
1055
1056 // add this layer to the current state list
1057 addLayer_l(lbc);
1058
1059 return name;
1060}
1061
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001062status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063{
1064 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001065 status_t err = purgatorizeLayer_l(layer);
1066 if (err == NO_ERROR)
1067 setTransactionFlags(eTransactionNeeded);
1068 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001073 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1074 if (lbc != 0) {
1075 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1076 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1078 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001079 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 return NO_ERROR;
1081 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001082 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083}
1084
Mathias Agopian9a112062009-04-17 19:36:26 -07001085status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1086{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001087 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001088 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001089
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001090 layerBase->onRemoved();
1091
Mathias Agopian3d579642009-06-04 18:46:21 -07001092 // it's possible that we don't find a layer, because it might
1093 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001094 // from the user because there is a race between Client::destroySurface(),
1095 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001096 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1097}
1098
Mathias Agopian96f08192010-06-02 23:28:45 -07001099status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100{
Mathias Agopian96f08192010-06-02 23:28:45 -07001101 layer->forceVisibilityTransaction();
1102 setTransactionFlags(eTraversalNeeded);
1103 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104}
1105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1107{
1108 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1109}
1110
Mathias Agopianbb641242010-05-18 17:06:55 -07001111uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112{
1113 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1114 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001115 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 }
1117 return old;
1118}
1119
1120void SurfaceFlinger::openGlobalTransaction()
1121{
1122 android_atomic_inc(&mTransactionCount);
1123}
1124
1125void SurfaceFlinger::closeGlobalTransaction()
1126{
1127 if (android_atomic_dec(&mTransactionCount) == 1) {
1128 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001129
1130 // if there is a transaction with a resize, wait for it to
1131 // take effect before returning.
1132 Mutex::Autolock _l(mStateLock);
1133 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001134 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1135 if (CC_UNLIKELY(err != NO_ERROR)) {
1136 // just in case something goes wrong in SF, return to the
1137 // called after a few seconds.
1138 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1139 mResizeTransationPending = false;
1140 break;
1141 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001142 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 }
1144}
1145
1146status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1147{
1148 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1149 return BAD_VALUE;
1150
1151 Mutex::Autolock _l(mStateLock);
1152 mCurrentState.freezeDisplay = 1;
1153 setTransactionFlags(eTransactionNeeded);
1154
1155 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001156 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 return NO_ERROR;
1158}
1159
1160status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1161{
1162 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1163 return BAD_VALUE;
1164
1165 Mutex::Autolock _l(mStateLock);
1166 mCurrentState.freezeDisplay = 0;
1167 setTransactionFlags(eTransactionNeeded);
1168
1169 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001170 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 return NO_ERROR;
1172}
1173
Mathias Agopianc08731e2009-03-27 18:11:38 -07001174int SurfaceFlinger::setOrientation(DisplayID dpy,
1175 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176{
1177 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1178 return BAD_VALUE;
1179
1180 Mutex::Autolock _l(mStateLock);
1181 if (mCurrentState.orientation != orientation) {
1182 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001183 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 mCurrentState.orientation = orientation;
1185 setTransactionFlags(eTransactionNeeded);
1186 mTransactionCV.wait(mStateLock);
1187 } else {
1188 orientation = BAD_VALUE;
1189 }
1190 }
1191 return orientation;
1192}
1193
Mathias Agopian96f08192010-06-02 23:28:45 -07001194sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001195 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1197 uint32_t flags)
1198{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001199 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001201
1202 if (int32_t(w|h) < 0) {
1203 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1204 int(w), int(h));
1205 return surfaceHandle;
1206 }
1207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001209 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 switch (flags & eFXSurfaceMask) {
1211 case eFXSurfaceNormal:
1212 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001213 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001215 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1216 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 }
1218 break;
1219 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001220 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 break;
1222 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 break;
1225 }
1226
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001227 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001228 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001229 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001230 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001233 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001234 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001235 params->identity = surfaceHandle->getIdentity();
1236 params->width = w;
1237 params->height = h;
1238 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001239 if (normalLayer != 0) {
1240 Mutex::Autolock _l(mStateLock);
1241 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1242 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001243 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001244
Mathias Agopian96f08192010-06-02 23:28:45 -07001245 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247
1248 return surfaceHandle;
1249}
1250
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001251sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001252 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001253 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001254 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255{
1256 // initialize the surfaces
1257 switch (format) { // TODO: take h/w into account
1258 case PIXEL_FORMAT_TRANSPARENT:
1259 case PIXEL_FORMAT_TRANSLUCENT:
1260 format = PIXEL_FORMAT_RGBA_8888;
1261 break;
1262 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001263#ifdef NO_RGBX_8888
1264 format = PIXEL_FORMAT_RGB_565;
1265#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001266 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001267#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 break;
1269 }
1270
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001271#ifdef NO_RGBX_8888
1272 if (format == PIXEL_FORMAT_RGBX_8888)
1273 format = PIXEL_FORMAT_RGBA_8888;
1274#endif
1275
Mathias Agopian96f08192010-06-02 23:28:45 -07001276 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001277 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001278 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001280 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 }
1282 return layer;
1283}
1284
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001285sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001286 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001287 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288{
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 return layer;
1292}
1293
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001294sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001295 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001296 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297{
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 return layer;
1301}
1302
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001303sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001304 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001305 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306{
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 return layer;
1310}
1311
Mathias Agopian96f08192010-06-02 23:28:45 -07001312status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313{
Mathias Agopian9a112062009-04-17 19:36:26 -07001314 /*
1315 * called by the window manager, when a surface should be marked for
1316 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001317 *
1318 * The surface is removed from the current and drawing lists, but placed
1319 * in the purgatory queue, so it's not destroyed right-away (we need
1320 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001321 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001322
Mathias Agopian48d819a2009-09-10 19:41:18 -07001323 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001324 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001325 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001326 if (layer != 0) {
1327 err = purgatorizeLayer_l(layer);
1328 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001329 setTransactionFlags(eTransactionNeeded);
1330 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001331 }
1332 return err;
1333}
1334
1335status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1336{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001337 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001338
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001339 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001341 sp<LayerBaseClient> layer;
1342 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001343 MessageDestroySurface(
1344 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1345 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001346 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001347 sp<LayerBaseClient> l(layer);
1348 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001349 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001350 /*
1351 * remove the layer from the current list -- chances are that it's
1352 * not in the list anyway, because it should have been removed
1353 * already upon request of the client (eg: window manager).
1354 * However, a buggy client could have not done that.
1355 * Since we know we don't have any more clients, we don't need
1356 * to use the purgatory.
1357 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001358 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001359 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1360 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001361 return true;
1362 }
1363 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001364
Mathias Agopianbb641242010-05-18 17:06:55 -07001365 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 return NO_ERROR;
1367}
1368
1369status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 int32_t count,
1372 const layer_state_t* states)
1373{
1374 Mutex::Autolock _l(mStateLock);
1375 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001377 const layer_state_t& s(states[i]);
1378 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001379 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 if (what & ePositionChanged) {
1382 if (layer->setPosition(s.x, s.y))
1383 flags |= eTraversalNeeded;
1384 }
1385 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001386 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001388 mCurrentState.layersSortedByZ.removeAt(idx);
1389 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 // we need traversal (state changed)
1391 // AND transaction (list changed)
1392 flags |= eTransactionNeeded|eTraversalNeeded;
1393 }
1394 }
1395 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001396 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001398 mResizeTransationPending = true;
1399 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 }
1401 if (what & eAlphaChanged) {
1402 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1403 flags |= eTraversalNeeded;
1404 }
1405 if (what & eMatrixChanged) {
1406 if (layer->setMatrix(s.matrix))
1407 flags |= eTraversalNeeded;
1408 }
1409 if (what & eTransparentRegionChanged) {
1410 if (layer->setTransparentRegionHint(s.transparentRegion))
1411 flags |= eTraversalNeeded;
1412 }
1413 if (what & eVisibilityChanged) {
1414 if (layer->setFlags(s.flags, s.mask))
1415 flags |= eTraversalNeeded;
1416 }
1417 }
1418 }
1419 if (flags) {
1420 setTransactionFlags(flags);
1421 }
1422 return NO_ERROR;
1423}
1424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425void SurfaceFlinger::screenReleased(int dpy)
1426{
1427 // this may be called by a signal handler, we can't do too much in here
1428 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1429 signalEvent();
1430}
1431
1432void SurfaceFlinger::screenAcquired(int dpy)
1433{
1434 // this may be called by a signal handler, we can't do too much in here
1435 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1436 signalEvent();
1437}
1438
1439status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1440{
1441 const size_t SIZE = 1024;
1442 char buffer[SIZE];
1443 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001444 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 snprintf(buffer, SIZE, "Permission Denial: "
1446 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1447 IPCThreadState::self()->getCallingPid(),
1448 IPCThreadState::self()->getCallingUid());
1449 result.append(buffer);
1450 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001451
1452 // figure out if we're stuck somewhere
1453 const nsecs_t now = systemTime();
1454 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1455 const nsecs_t inTransaction(mDebugInTransaction);
1456 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1457 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1458
1459 // Try to get the main lock, but don't insist if we can't
1460 // (this would indicate SF is stuck, but we want to be able to
1461 // print something in dumpsys).
1462 int retry = 3;
1463 while (mStateLock.tryLock()<0 && --retry>=0) {
1464 usleep(1000000);
1465 }
1466 const bool locked(retry >= 0);
1467 if (!locked) {
1468 snprintf(buffer, SIZE,
1469 "SurfaceFlinger appears to be unresponsive, "
1470 "dumping anyways (no locks held)\n");
1471 result.append(buffer);
1472 }
1473
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1475 const size_t count = currentLayers.size();
1476 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001477 const sp<LayerBase>& layer(currentLayers[i]);
1478 layer->dump(result, buffer, SIZE);
1479 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 s.transparentRegion.dump(result, "transparentRegion");
1481 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1482 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1483 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485 mWormholeRegion.dump(result, "WormholeRegion");
1486 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1487 snprintf(buffer, SIZE,
1488 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1489 mFreezeDisplay?"yes":"no", mFreezeCount,
1490 mCurrentState.orientation, hw.canDraw());
1491 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001492 snprintf(buffer, SIZE,
1493 " last eglSwapBuffers() time: %f us\n"
1494 " last transaction time : %f us\n",
1495 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1496 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001497
Mathias Agopian9795c422009-08-26 16:36:26 -07001498 if (inSwapBuffersDuration || !locked) {
1499 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1500 inSwapBuffersDuration/1000.0);
1501 result.append(buffer);
1502 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001503
Mathias Agopian9795c422009-08-26 16:36:26 -07001504 if (inTransactionDuration || !locked) {
1505 snprintf(buffer, SIZE, " transaction time: %f us\n",
1506 inTransactionDuration/1000.0);
1507 result.append(buffer);
1508 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001509
Mathias Agopian3330b202009-10-05 17:07:12 -07001510 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001511 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001512
1513 if (locked) {
1514 mStateLock.unlock();
1515 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 }
1517 write(fd, result.string(), result.size());
1518 return NO_ERROR;
1519}
1520
1521status_t SurfaceFlinger::onTransact(
1522 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1523{
1524 switch (code) {
1525 case CREATE_CONNECTION:
1526 case OPEN_GLOBAL_TRANSACTION:
1527 case CLOSE_GLOBAL_TRANSACTION:
1528 case SET_ORIENTATION:
1529 case FREEZE_DISPLAY:
1530 case UNFREEZE_DISPLAY:
1531 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 {
1533 // codes that require permission check
1534 IPCThreadState* ipc = IPCThreadState::self();
1535 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001536 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001537 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1538 LOGE("Permission Denial: "
1539 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1540 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 }
1542 }
1543 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001544 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1545 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001546 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001547 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1548 IPCThreadState* ipc = IPCThreadState::self();
1549 const int pid = ipc->getCallingPid();
1550 const int uid = ipc->getCallingUid();
1551 LOGE("Permission Denial: "
1552 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 return PERMISSION_DENIED;
1554 }
1555 int n;
1556 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001557 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001559 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560 return NO_ERROR;
1561 case 1002: // SHOW_UPDATES
1562 n = data.readInt32();
1563 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1564 return NO_ERROR;
1565 case 1003: // SHOW_BACKGROUND
1566 n = data.readInt32();
1567 mDebugBackground = n ? 1 : 0;
1568 return NO_ERROR;
1569 case 1004:{ // repaint everything
1570 Mutex::Autolock _l(mStateLock);
1571 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1572 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1573 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001574 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001576 case 1005:{ // force transaction
1577 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1578 return NO_ERROR;
1579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 case 1007: // set mFreezeCount
1581 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001582 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 return NO_ERROR;
1584 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001585 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 reply->writeInt32(0);
1587 reply->writeInt32(mDebugRegion);
1588 reply->writeInt32(mDebugBackground);
1589 return NO_ERROR;
1590 case 1013: {
1591 Mutex::Autolock _l(mStateLock);
1592 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1593 reply->writeInt32(hw.getPageFlipCount());
1594 }
1595 return NO_ERROR;
1596 }
1597 }
1598 return err;
1599}
1600
1601// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001602
1603sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1604{
1605 sp<Layer> result;
1606 Mutex::Autolock _l(mStateLock);
1607 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1608 return result;
1609}
1610
1611// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612
Mathias Agopian96f08192010-06-02 23:28:45 -07001613Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001614 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616}
1617
Mathias Agopian96f08192010-06-02 23:28:45 -07001618Client::~Client()
1619{
Mathias Agopian96f08192010-06-02 23:28:45 -07001620 const size_t count = mLayers.size();
1621 for (size_t i=0 ; i<count ; i++) {
1622 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1623 if (layer != 0) {
1624 mFlinger->removeLayer(layer);
1625 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626 }
1627}
1628
Mathias Agopian96f08192010-06-02 23:28:45 -07001629status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001630 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001632
Mathias Agopian96f08192010-06-02 23:28:45 -07001633ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1634{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001635 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001636 mLayers.add(name, layer);
1637 return name;
1638}
1639
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001640void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001641{
Mathias Agopian96f08192010-06-02 23:28:45 -07001642 // we do a linear search here, because this doesn't happen often
1643 const size_t count = mLayers.size();
1644 for (size_t i=0 ; i<count ; i++) {
1645 if (mLayers.valueAt(i) == layer) {
1646 mLayers.removeItemsAt(i, 1);
1647 break;
1648 }
1649 }
1650}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001651sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1652 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001653 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1654 if (layer != 0) {
1655 lbc = layer.promote();
1656 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001657 }
1658 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659}
1660
Mathias Agopian96f08192010-06-02 23:28:45 -07001661sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001662 return 0;
1663}
1664ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1665 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666}
Mathias Agopian96f08192010-06-02 23:28:45 -07001667sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001668 ISurfaceComposerClient::surface_data_t* params, int pid,
1669 const String8& name,
1670 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 uint32_t flags)
1672{
Mathias Agopian96f08192010-06-02 23:28:45 -07001673 return mFlinger->createSurface(this, pid, name, params,
1674 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675}
Mathias Agopian96f08192010-06-02 23:28:45 -07001676status_t Client::destroySurface(SurfaceID sid) {
1677 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678}
Mathias Agopian96f08192010-06-02 23:28:45 -07001679status_t Client::setState(int32_t count, const layer_state_t* states) {
1680 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681}
1682
1683// ---------------------------------------------------------------------------
1684
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001685UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1686 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1687{
1688 const int pgsize = getpagesize();
1689 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1690
1691 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1692 "SurfaceFlinger Client control-block");
1693
1694 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1695 if (ctrlblk) { // construct the shared structure in-place.
1696 new(ctrlblk) SharedClient;
1697 }
1698}
1699
1700UserClient::~UserClient()
1701{
1702 if (ctrlblk) {
1703 ctrlblk->~SharedClient(); // destroy our shared-structure.
1704 }
1705
1706 /*
1707 * When a UserClient dies, it's unclear what to do exactly.
1708 * We could go ahead and destroy all surfaces linked to that client
1709 * however, it wouldn't be fair to the main Client
1710 * (usually the the window-manager), which might want to re-target
1711 * the layer to another UserClient.
1712 * I think the best is to do nothing, or not much; in most cases the
1713 * WM itself will go ahead and clean things up when it detects a client of
1714 * his has died.
1715 * The remaining question is what to display? currently we keep
1716 * just keep the current buffer.
1717 */
1718}
1719
1720status_t UserClient::initCheck() const {
1721 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1722}
1723
1724void UserClient::detachLayer(const Layer* layer)
1725{
1726 int32_t name = layer->getToken();
1727 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001728 int32_t mask = 1LU<<name;
1729 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1730 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1731 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001732 }
1733}
1734
1735sp<IMemoryHeap> UserClient::getControlBlock() const {
1736 return mCblkHeap;
1737}
1738
1739ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1740{
1741 int32_t name = NAME_NOT_FOUND;
1742 sp<Layer> layer(mFlinger->getLayer(sur));
1743 if (layer == 0) return name;
1744
Mathias Agopian579b3f82010-06-08 19:54:15 -07001745 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001746 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07001747 if ((name >= 0) && (layer->getClient() == this))
1748 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001749
1750 name = 0;
1751 do {
1752 int32_t mask = 1LU<<name;
1753 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1754 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001755 status_t err = layer->setToken(
1756 const_cast<UserClient*>(this), ctrlblk, name);
1757 if (err != NO_ERROR) {
1758 // free the name
1759 android_atomic_and(~mask, &mBitmap);
1760 name = err;
1761 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001762 break;
1763 }
1764 if (++name > 31)
1765 name = NO_MEMORY;
1766 } while(name >= 0);
1767
Mathias Agopian53503a92010-06-08 15:40:56 -07001768 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1769 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001770 return name;
1771}
1772
1773sp<ISurface> UserClient::createSurface(
1774 ISurfaceComposerClient::surface_data_t* params, int pid,
1775 const String8& name,
1776 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1777 uint32_t flags) {
1778 return 0;
1779}
1780status_t UserClient::destroySurface(SurfaceID sid) {
1781 return INVALID_OPERATION;
1782}
1783status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1784 return INVALID_OPERATION;
1785}
1786
1787// ---------------------------------------------------------------------------
1788
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001789GraphicPlane::GraphicPlane()
1790 : mHw(0)
1791{
1792}
1793
1794GraphicPlane::~GraphicPlane() {
1795 delete mHw;
1796}
1797
1798bool GraphicPlane::initialized() const {
1799 return mHw ? true : false;
1800}
1801
Mathias Agopian2b92d892010-02-08 15:49:35 -08001802int GraphicPlane::getWidth() const {
1803 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001804}
1805
Mathias Agopian2b92d892010-02-08 15:49:35 -08001806int GraphicPlane::getHeight() const {
1807 return mHeight;
1808}
1809
1810void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1811{
1812 mHw = hw;
1813
1814 // initialize the display orientation transform.
1815 // it's a constant that should come from the display driver.
1816 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1817 char property[PROPERTY_VALUE_MAX];
1818 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1819 //displayOrientation
1820 switch (atoi(property)) {
1821 case 90:
1822 displayOrientation = ISurfaceComposer::eOrientation90;
1823 break;
1824 case 270:
1825 displayOrientation = ISurfaceComposer::eOrientation270;
1826 break;
1827 }
1828 }
1829
1830 const float w = hw->getWidth();
1831 const float h = hw->getHeight();
1832 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1833 &mDisplayTransform);
1834 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1835 mDisplayWidth = h;
1836 mDisplayHeight = w;
1837 } else {
1838 mDisplayWidth = w;
1839 mDisplayHeight = h;
1840 }
1841
1842 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001843}
1844
1845status_t GraphicPlane::orientationToTransfrom(
1846 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001847{
1848 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849 switch (orientation) {
1850 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001851 flags = Transform::ROT_0;
1852 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001854 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855 break;
1856 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001857 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858 break;
1859 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001860 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001861 break;
1862 default:
1863 return BAD_VALUE;
1864 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001865 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 return NO_ERROR;
1867}
1868
1869status_t GraphicPlane::setOrientation(int orientation)
1870{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 // If the rotation can be handled in hardware, this is where
1872 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001873
1874 const DisplayHardware& hw(displayHardware());
1875 const float w = mDisplayWidth;
1876 const float h = mDisplayHeight;
1877 mWidth = int(w);
1878 mHeight = int(h);
1879
1880 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001881 GraphicPlane::orientationToTransfrom(orientation, w, h,
1882 &orientationTransform);
1883 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1884 mWidth = int(h);
1885 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001886 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001887
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001888 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001889 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890 return NO_ERROR;
1891}
1892
1893const DisplayHardware& GraphicPlane::displayHardware() const {
1894 return *mHw;
1895}
1896
1897const Transform& GraphicPlane::transform() const {
1898 return mGlobalTransform;
1899}
1900
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001901EGLDisplay GraphicPlane::getEGLDisplay() const {
1902 return mHw->getEGLDisplay();
1903}
1904
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905// ---------------------------------------------------------------------------
1906
1907}; // namespace android