blob: 06c86ddef66e5de8276c1b67ab1cfec61dbbe6a5 [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"
Mathias Agopiana350ff92010-08-10 17:14:02 -070056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopiana1ecca92009-05-21 19:21:59 -070058/* ideally AID_GRAPHICS would be in a semi-public header
59 * or there would be a way to map a user/group name to its id
60 */
61#ifndef AID_GRAPHICS
62#define AID_GRAPHICS 1003
63#endif
64
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#define DISPLAY_COUNT 1
66
67namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068// ---------------------------------------------------------------------------
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070SurfaceFlinger::SurfaceFlinger()
71 : BnSurfaceComposer(), Thread(false),
72 mTransactionFlags(0),
73 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070074 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070077 mHardwareTest("android.permission.HARDWARE_TEST"),
78 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
79 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070081 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 mDeferReleaseConsole(false),
83 mFreezeDisplay(false),
84 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070088 mDebugDisableHWC(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070089 mDebugInSwapBuffers(0),
90 mLastSwapBufferTime(0),
91 mDebugInTransaction(0),
92 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070093 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 mConsoleSignals(0),
95 mSecureFrameBuffer(0)
96{
97 init();
98}
99
100void SurfaceFlinger::init()
101{
102 LOGI("SurfaceFlinger is starting");
103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 property_get("debug.sf.showbackground", value, "0");
109 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Mathias Agopian78fd5012010-04-20 14:51:04 -0700111 LOGI_IF(mDebugRegion, "showupdates enabled");
112 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
115SurfaceFlinger::~SurfaceFlinger()
116{
117 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118}
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
121{
122 return graphicPlane(0).displayHardware().getOverlayEngine();
123}
124
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700125sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700127 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128}
129
Mathias Agopian7e27f052010-05-28 14:22:23 -0700130sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131{
Mathias Agopian96f08192010-06-02 23:28:45 -0700132 sp<ISurfaceComposerClient> bclient;
133 sp<Client> client(new Client(this));
134 status_t err = client->initCheck();
135 if (err == NO_ERROR) {
136 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 return bclient;
139}
140
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700141sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
142{
143 sp<ISurfaceComposerClient> bclient;
144 sp<UserClient> client(new UserClient(this));
145 status_t err = client->initCheck();
146 if (err == NO_ERROR) {
147 bclient = client;
148 }
149 return bclient;
150}
151
152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
154{
155 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
156 const GraphicPlane& plane(mGraphicPlanes[dpy]);
157 return plane;
158}
159
160GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
161{
162 return const_cast<GraphicPlane&>(
163 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
164}
165
166void SurfaceFlinger::bootFinished()
167{
168 const nsecs_t now = systemTime();
169 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700170 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700171 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700172 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173}
174
175void SurfaceFlinger::onFirstRef()
176{
177 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
178
179 // Wait for the main thread to be done with its initialization
180 mReadyToRunBarrier.wait();
181}
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183static inline uint16_t pack565(int r, int g, int b) {
184 return (r<<11)|(g<<5)|b;
185}
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187status_t SurfaceFlinger::readyToRun()
188{
189 LOGI( "SurfaceFlinger's main thread ready to run. "
190 "Initializing graphics H/W...");
191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 // we only support one display currently
193 int dpy = 0;
194
195 {
196 // initialize the main display
197 GraphicPlane& plane(graphicPlane(dpy));
198 DisplayHardware* const hw = new DisplayHardware(this, dpy);
199 plane.setDisplayHardware(hw);
200 }
201
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700202 // create the shared control-block
203 mServerHeap = new MemoryHeapBase(4096,
204 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
205 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700206
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700207 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
208 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700209
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700210 new(mServerCblk) surface_flinger_cblk_t;
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 // initialize primary screen
213 // (other display should be initialized in the same manner, but
214 // asynchronously, as they could come and go. None of this is supported
215 // yet).
216 const GraphicPlane& plane(graphicPlane(dpy));
217 const DisplayHardware& hw = plane.displayHardware();
218 const uint32_t w = hw.getWidth();
219 const uint32_t h = hw.getHeight();
220 const uint32_t f = hw.getFormat();
221 hw.makeCurrent();
222
223 // initialize the shared control block
224 mServerCblk->connected |= 1<<dpy;
225 display_cblk_t* dcblk = mServerCblk->displays + dpy;
226 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800227 dcblk->w = plane.getWidth();
228 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 dcblk->format = f;
230 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
231 dcblk->xdpi = hw.getDpiX();
232 dcblk->ydpi = hw.getDpiY();
233 dcblk->fps = hw.getRefreshRate();
234 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700238 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 glEnableClientState(GL_VERTEX_ARRAY);
240 glEnable(GL_SCISSOR_TEST);
241 glShadeModel(GL_FLAT);
242 glDisable(GL_DITHER);
243 glDisable(GL_CULL_FACE);
244
245 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
246 const uint16_t g1 = pack565(0x17,0x2f,0x17);
247 const uint16_t textureData[4] = { g0, g1, g1, g0 };
248 glGenTextures(1, &mWormholeTexName);
249 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
250 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
251 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
252 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
253 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
255 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
256
257 glViewport(0, 0, w, h);
258 glMatrixMode(GL_PROJECTION);
259 glLoadIdentity();
260 glOrthof(0, w, h, 0, 0, 1);
261
262 LayerDim::initDimmer(this, w, h);
263
264 mReadyToRunBarrier.open();
265
266 /*
267 * We're now ready to accept clients...
268 */
269
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700270 // start boot animation
271 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 return NO_ERROR;
274}
275
276// ----------------------------------------------------------------------------
277#if 0
278#pragma mark -
279#pragma mark Events Handler
280#endif
281
282void SurfaceFlinger::waitForEvent()
283{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700284 while (true) {
285 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800286 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700287 if (UNLIKELY(isFrozen())) {
288 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700289 const nsecs_t now = systemTime();
290 if (mFreezeDisplayTime == 0) {
291 mFreezeDisplayTime = now;
292 }
293 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
294 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700295 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700296
Mathias Agopianbb641242010-05-18 17:06:55 -0700297 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800298
299 // see if we timed out
300 if (isFrozen()) {
301 const nsecs_t now = systemTime();
302 nsecs_t frozenTime = (now - mFreezeDisplayTime);
303 if (frozenTime >= freezeDisplayTimeout) {
304 // we timed out and are still frozen
305 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
306 mFreezeDisplay, mFreezeCount);
307 mFreezeDisplayTime = 0;
308 mFreezeCount = 0;
309 mFreezeDisplay = false;
310 }
311 }
312
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700313 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700314 switch (msg->what) {
315 case MessageQueue::INVALIDATE:
316 // invalidate message, just return to the main loop
317 return;
318 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 }
321}
322
323void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700324 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
327void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700328 // this is the IPC call
329 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Mathias Agopianbb641242010-05-18 17:06:55 -0700332status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
333 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334{
Mathias Agopianbb641242010-05-18 17:06:55 -0700335 return mEventQueue.postMessage(msg, reltime, flags);
336}
337
338status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
339 nsecs_t reltime, uint32_t flags)
340{
341 status_t res = mEventQueue.postMessage(msg, reltime, flags);
342 if (res == NO_ERROR) {
343 msg->wait();
344 }
345 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346}
347
348// ----------------------------------------------------------------------------
349#if 0
350#pragma mark -
351#pragma mark Main loop
352#endif
353
354bool SurfaceFlinger::threadLoop()
355{
356 waitForEvent();
357
358 // check for transactions
359 if (UNLIKELY(mConsoleSignals)) {
360 handleConsoleEvents();
361 }
362
363 if (LIKELY(mTransactionCount == 0)) {
364 // if we're in a global transaction, don't do anything.
365 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
366 uint32_t transactionFlags = getTransactionFlags(mask);
367 if (LIKELY(transactionFlags)) {
368 handleTransaction(transactionFlags);
369 }
370 }
371
372 // post surfaces (if needed)
373 handlePageFlip();
374
Mathias Agopiana350ff92010-08-10 17:14:02 -0700375 if (UNLIKELY(mHwWorkListDirty)) {
376 // build the h/w work list
377 handleWorkList();
378 }
379
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700381 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700383
384 const int index = hw.getCurrentBufferIndex();
385 GraphicLog& logger(GraphicLog::getInstance());
386
387 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 handleRepaint();
389
Mathias Agopian74faca22009-09-17 16:18:16 -0700390 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700391 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700392 hw.compositionComplete();
393
Mathias Agopian35b48d12010-09-13 22:57:58 -0700394 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700395 postFramebuffer();
396
Mathias Agopianc479e182010-09-15 12:29:18 -0700397 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399
Mathias Agopian35b48d12010-09-13 22:57:58 -0700400 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 } else {
402 // pretend we did the post
403 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 usleep(16667); // 60 fps period
405 }
406 return true;
407}
408
409void SurfaceFlinger::postFramebuffer()
410{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411 if (!mInvalidRegion.isEmpty()) {
412 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700413 const nsecs_t now = systemTime();
414 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700416 mLastSwapBufferTime = systemTime() - now;
417 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 }
420}
421
422void SurfaceFlinger::handleConsoleEvents()
423{
424 // something to do with the console
425 const DisplayHardware& hw = graphicPlane(0).displayHardware();
426
427 int what = android_atomic_and(0, &mConsoleSignals);
428 if (what & eConsoleAcquired) {
429 hw.acquireScreen();
430 }
431
432 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700433 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 hw.releaseScreen();
436 }
437
438 if (what & eConsoleReleased) {
439 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 hw.releaseScreen();
441 } else {
442 mDeferReleaseConsole = true;
443 }
444 }
445
446 mDirtyRegion.set(hw.bounds());
447}
448
449void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
450{
Mathias Agopian3d579642009-06-04 18:46:21 -0700451 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452
Mathias Agopian4da75192010-08-10 17:19:56 -0700453 /*
454 * Perform and commit the transaction
455 */
456
Mathias Agopian3d579642009-06-04 18:46:21 -0700457 { // scope for the lock
458 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700459 const nsecs_t now = systemTime();
460 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700461 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700462 mLastTransactionTime = systemTime() - now;
463 mDebugInTransaction = 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700464 mHwWorkListDirty = true;
Mathias Agopian4da75192010-08-10 17:19:56 -0700465 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700466 }
467
Mathias Agopian4da75192010-08-10 17:19:56 -0700468 /*
469 * Clean-up all layers that went away
470 * (do this without the lock held)
471 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700472
Mathias Agopian3d579642009-06-04 18:46:21 -0700473 const size_t count = ditchedLayers.size();
474 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700475 if (ditchedLayers[i] != 0) {
476 //LOGD("ditching layer %p", ditchedLayers[i].get());
477 ditchedLayers[i]->ditch();
478 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700479 }
480}
481
482void SurfaceFlinger::handleTransactionLocked(
483 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
484{
485 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 const size_t count = currentLayers.size();
487
488 /*
489 * Traversal of the children
490 * (perform the transaction for each of them if needed)
491 */
492
493 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
494 if (layersNeedTransaction) {
495 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700496 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
498 if (!trFlags) continue;
499
500 const uint32_t flags = layer->doTransaction(0);
501 if (flags & Layer::eVisibleRegion)
502 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 }
504 }
505
506 /*
507 * Perform our own transaction if needed
508 */
509
510 if (transactionFlags & eTransactionNeeded) {
511 if (mCurrentState.orientation != mDrawingState.orientation) {
512 // the orientation has changed, recompute all visible regions
513 // and invalidate everything.
514
515 const int dpy = 0;
516 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700517 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 GraphicPlane& plane(graphicPlane(dpy));
519 plane.setOrientation(orientation);
520
521 // update the shared control block
522 const DisplayHardware& hw(plane.displayHardware());
523 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
524 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800525 dcblk->w = plane.getWidth();
526 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527
528 mVisibleRegionsDirty = true;
529 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530 }
531
532 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
533 // freezing or unfreezing the display -> trigger animation if needed
534 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800535 if (mFreezeDisplay)
536 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 }
538
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700539 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
540 // layers have been added
541 mVisibleRegionsDirty = true;
542 }
543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 // some layers might have been removed, so
545 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700547 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700549 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700550 const size_t count = previousLayers.size();
551 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700552 const sp<LayerBase>& layer(previousLayers[i]);
553 if (currentLayers.indexOf( layer ) < 0) {
554 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700555 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700556 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 }
558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 }
561
562 commitTransaction();
563}
564
565sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
566{
567 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
568}
569
570void SurfaceFlinger::computeVisibleRegions(
571 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
572{
573 const GraphicPlane& plane(graphicPlane(0));
574 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700575 const DisplayHardware& hw(plane.displayHardware());
576 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577
578 Region aboveOpaqueLayers;
579 Region aboveCoveredLayers;
580 Region dirty;
581
582 bool secureFrameBuffer = false;
583
584 size_t i = currentLayers.size();
585 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 layer->validateVisibility(planeTransform);
588
589 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700590 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591
Mathias Agopianab028732010-03-16 16:41:46 -0700592 /*
593 * opaqueRegion: area of a surface that is fully opaque.
594 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700596
597 /*
598 * visibleRegion: area of a surface that is visible on screen
599 * and not fully transparent. This is essentially the layer's
600 * footprint minus the opaque regions above it.
601 * Areas covered by a translucent surface are considered visible.
602 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700604
605 /*
606 * coveredRegion: area of a surface that is covered by all
607 * visible regions above it (which includes the translucent areas).
608 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700610
611
612 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700613 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700615 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700617 visibleRegion.andSelf(screenRegion);
618 if (!visibleRegion.isEmpty()) {
619 // Remove the transparent area from the visible region
620 if (translucent) {
621 visibleRegion.subtractSelf(layer->transparentRegionScreen);
622 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623
Mathias Agopianab028732010-03-16 16:41:46 -0700624 // compute the opaque region
625 const int32_t layerOrientation = layer->getOrientation();
626 if (s.alpha==255 && !translucent &&
627 ((layerOrientation & Transform::ROT_INVALID) == false)) {
628 // the opaque region is the layer's footprint
629 opaqueRegion = visibleRegion;
630 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 }
632 }
633
Mathias Agopianab028732010-03-16 16:41:46 -0700634 // Clip the covered region to the visible region
635 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
636
637 // Update aboveCoveredLayers for next (lower) layer
638 aboveCoveredLayers.orSelf(visibleRegion);
639
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 // subtract the opaque region covered by the layers above us
641 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642
643 // compute this layer's dirty region
644 if (layer->contentDirty) {
645 // we need to invalidate the whole region
646 dirty = visibleRegion;
647 // as well, as the old visible region
648 dirty.orSelf(layer->visibleRegionScreen);
649 layer->contentDirty = false;
650 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700651 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700652 * the exposed region consists of two components:
653 * 1) what's VISIBLE now and was COVERED before
654 * 2) what's EXPOSED now less what was EXPOSED before
655 *
656 * note that (1) is conservative, we start with the whole
657 * visible region but only keep what used to be covered by
658 * something -- which mean it may have been exposed.
659 *
660 * (2) handles areas that were not covered by anything but got
661 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700662 */
Mathias Agopianab028732010-03-16 16:41:46 -0700663 const Region newExposed = visibleRegion - coveredRegion;
664 const Region oldVisibleRegion = layer->visibleRegionScreen;
665 const Region oldCoveredRegion = layer->coveredRegionScreen;
666 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
667 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 }
669 dirty.subtractSelf(aboveOpaqueLayers);
670
671 // accumulate to the screen dirty region
672 dirtyRegion.orSelf(dirty);
673
Mathias Agopianab028732010-03-16 16:41:46 -0700674 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700676
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 // Store the visible region is screen space
678 layer->setVisibleRegion(visibleRegion);
679 layer->setCoveredRegion(coveredRegion);
680
Mathias Agopian97011222009-07-28 10:57:27 -0700681 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 if (layer->isSecure() && !visibleRegion.isEmpty()) {
683 secureFrameBuffer = true;
684 }
685 }
686
Mathias Agopian97011222009-07-28 10:57:27 -0700687 // invalidate the areas where a layer was removed
688 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
689 mDirtyRegionRemovedLayer.clear();
690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 mSecureFrameBuffer = secureFrameBuffer;
692 opaqueRegion = aboveOpaqueLayers;
693}
694
695
696void SurfaceFlinger::commitTransaction()
697{
698 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700699 mResizeTransationPending = false;
700 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701}
702
703void SurfaceFlinger::handlePageFlip()
704{
705 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700706 LayerVector& currentLayers(
707 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 visibleRegions |= lockPageFlip(currentLayers);
709
710 const DisplayHardware& hw = graphicPlane(0).displayHardware();
711 const Region screenRegion(hw.bounds());
712 if (visibleRegions) {
713 Region opaqueRegion;
714 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700715
716 /*
717 * rebuild the visible layer list
718 */
719 mVisibleLayersSortedByZ.clear();
720 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
721 size_t count = currentLayers.size();
722 mVisibleLayersSortedByZ.setCapacity(count);
723 for (size_t i=0 ; i<count ; i++) {
724 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
725 mVisibleLayersSortedByZ.add(currentLayers[i]);
726 }
727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 mWormholeRegion = screenRegion.subtract(opaqueRegion);
729 mVisibleRegionsDirty = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700730 mHwWorkListDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
732
733 unlockPageFlip(currentLayers);
734 mDirtyRegion.andSelf(screenRegion);
735}
736
737bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
738{
739 bool recomputeVisibleRegions = false;
740 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700741 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700743 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 layer->lockPageFlip(recomputeVisibleRegions);
745 }
746 return recomputeVisibleRegions;
747}
748
749void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
750{
751 const GraphicPlane& plane(graphicPlane(0));
752 const Transform& planeTransform(plane.transform());
753 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700754 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700756 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 layer->unlockPageFlip(planeTransform, mDirtyRegion);
758 }
759}
760
Mathias Agopiana350ff92010-08-10 17:14:02 -0700761void SurfaceFlinger::handleWorkList()
762{
763 mHwWorkListDirty = false;
764 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
765 if (hwc.initCheck() == NO_ERROR) {
766 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
767 const size_t count = currentLayers.size();
768 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700769 hwc_layer_t* const cur(hwc.getLayers());
770 for (size_t i=0 ; cur && i<count ; i++) {
771 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700772 if (mDebugDisableHWC) {
773 cur[i].compositionType = HWC_FRAMEBUFFER;
774 cur[i].flags |= HWC_SKIP_LAYER;
775 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700776 }
777 }
778}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700779
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780void SurfaceFlinger::handleRepaint()
781{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700782 // compute the invalid region
783 mInvalidRegion.orSelf(mDirtyRegion);
784 if (mInvalidRegion.isEmpty()) {
785 // nothing to do
786 return;
787 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788
789 if (UNLIKELY(mDebugRegion)) {
790 debugFlashRegions();
791 }
792
Mathias Agopianb8a55602009-06-26 19:06:36 -0700793 // set the frame buffer
794 const DisplayHardware& hw(graphicPlane(0).displayHardware());
795 glMatrixMode(GL_MODELVIEW);
796 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800797
798 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700799 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
800 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700801 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700802 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
803 // takes a rectangle, we must make sure to update that whole
804 // rectangle in that case
805 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700806 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700807 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 mDirtyRegion.set(mInvalidRegion.bounds());
809 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700810 // in the BUFFER_PRESERVED case, obviously, we can update only
811 // what's needed and nothing more.
812 // NOTE: this is NOT a common case, as preserving the backbuffer
813 // is costly and usually involves copying the whole update back.
814 }
815 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700816 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700817 // We need to redraw the rectangle that will be updated
818 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700819 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700820 // rectangle instead of a region (see DisplayHardware::flip())
821 mDirtyRegion.set(mInvalidRegion.bounds());
822 } else {
823 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824 mDirtyRegion.set(hw.bounds());
825 mInvalidRegion = mDirtyRegion;
826 }
827 }
828
829 // compose all surfaces
830 composeSurfaces(mDirtyRegion);
831
832 // clear the dirty regions
833 mDirtyRegion.clear();
834}
835
836void SurfaceFlinger::composeSurfaces(const Region& dirty)
837{
838 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
839 // should never happen unless the window manager has a bug
840 // draw something...
841 drawWormhole();
842 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700843
844 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700845 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700846 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700847
848 const DisplayHardware& hw(graphicPlane(0).displayHardware());
849 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700850 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700851
Mathias Agopian45721772010-08-12 15:03:26 -0700852 LOGE_IF(cur && hwc.getNumLayers() != count,
853 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
854 hwc.getNumLayers(), count);
855
856 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700857 if (hwc.initCheck() == NO_ERROR) {
858 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
859 }
Mathias Agopian45721772010-08-12 15:03:26 -0700860
861 /*
862 * update the per-frame h/w composer data for each layer
863 * and build the transparent region of the FB
864 */
865 Region transparent;
866 if (cur) {
867 for (size_t i=0 ; i<count ; i++) {
868 const sp<LayerBase>& layer(layers[i]);
869 layer->setPerFrameData(&cur[i]);
870 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
871 if (!(layer->needsBlending())) {
872 transparent.orSelf(layer->visibleRegionScreen);
873 }
874 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700875 }
876 err = hwc.prepare();
877 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
878 }
879
Mathias Agopian45721772010-08-12 15:03:26 -0700880 /*
881 * clear the area of the FB that need to be transparent
882 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700883 transparent.andSelf(dirty);
884 if (!transparent.isEmpty()) {
885 glClearColor(0,0,0,0);
886 Region::const_iterator it = transparent.begin();
887 Region::const_iterator const end = transparent.end();
888 const int32_t height = hw.getHeight();
889 while (it != end) {
890 const Rect& r(*it++);
891 const GLint sy = height - (r.top + r.height());
892 glScissor(r.left, sy, r.width(), r.height());
893 glClear(GL_COLOR_BUFFER_BIT);
894 }
895 }
Mathias Agopian45721772010-08-12 15:03:26 -0700896
897
898 /*
899 * and then, render the layers targeted at the framebuffer
900 */
901 for (size_t i=0 ; i<count ; i++) {
902 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700903 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
904 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700905 // skip layers handled by the HAL
906 continue;
907 }
908 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700909
Mathias Agopian45721772010-08-12 15:03:26 -0700910 const sp<LayerBase>& layer(layers[i]);
911 const Region clip(dirty.intersect(layer->visibleRegionScreen));
912 if (!clip.isEmpty()) {
913 layer->draw(clip);
914 }
915 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916}
917
918void SurfaceFlinger::unlockClients()
919{
920 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
921 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700922 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700924 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 layer->finishPageFlip();
926 }
927}
928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929void SurfaceFlinger::debugFlashRegions()
930{
Mathias Agopian0a917752010-06-14 21:20:00 -0700931 const DisplayHardware& hw(graphicPlane(0).displayHardware());
932 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700933
Mathias Agopian0a917752010-06-14 21:20:00 -0700934 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
935 (flags & DisplayHardware::BUFFER_PRESERVED))) {
936 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
937 mDirtyRegion.bounds() : hw.bounds());
938 composeSurfaces(repaint);
939 }
940
941 TextureManager::deactivateTextures();
942
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 glDisable(GL_BLEND);
944 glDisable(GL_DITHER);
945 glDisable(GL_SCISSOR_TEST);
946
Mathias Agopian0926f502009-05-04 14:17:04 -0700947 static int toggle = 0;
948 toggle = 1 - toggle;
949 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700950 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700951 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700952 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700953 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954
Mathias Agopian20f68782009-05-11 00:03:41 -0700955 Region::const_iterator it = mDirtyRegion.begin();
956 Region::const_iterator const end = mDirtyRegion.end();
957 while (it != end) {
958 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 GLfloat vertices[][2] = {
960 { r.left, r.top },
961 { r.left, r.bottom },
962 { r.right, r.bottom },
963 { r.right, r.top }
964 };
965 glVertexPointer(2, GL_FLOAT, 0, vertices);
966 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
967 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700968
Mathias Agopianb8a55602009-06-26 19:06:36 -0700969 if (mInvalidRegion.isEmpty()) {
970 mDirtyRegion.dump("mDirtyRegion");
971 mInvalidRegion.dump("mInvalidRegion");
972 }
973 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974
975 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700976 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977
978 glEnable(GL_SCISSOR_TEST);
979 //mDirtyRegion.dump("mDirtyRegion");
980}
981
982void SurfaceFlinger::drawWormhole() const
983{
984 const Region region(mWormholeRegion.intersect(mDirtyRegion));
985 if (region.isEmpty())
986 return;
987
988 const DisplayHardware& hw(graphicPlane(0).displayHardware());
989 const int32_t width = hw.getWidth();
990 const int32_t height = hw.getHeight();
991
992 glDisable(GL_BLEND);
993 glDisable(GL_DITHER);
994
995 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700996 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700997 Region::const_iterator it = region.begin();
998 Region::const_iterator const end = region.end();
999 while (it != end) {
1000 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 const GLint sy = height - (r.top + r.height());
1002 glScissor(r.left, sy, r.width(), r.height());
1003 glClear(GL_COLOR_BUFFER_BIT);
1004 }
1005 } else {
1006 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1007 { width, height }, { 0, height } };
1008 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1009 glVertexPointer(2, GL_SHORT, 0, vertices);
1010 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1011 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001012#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001013 if (GLExtensions::getInstance().haveTextureExternal()) {
1014 glDisable(GL_TEXTURE_EXTERNAL_OES);
1015 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001016#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 glEnable(GL_TEXTURE_2D);
1018 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1019 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1020 glMatrixMode(GL_TEXTURE);
1021 glLoadIdentity();
1022 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001023 Region::const_iterator it = region.begin();
1024 Region::const_iterator const end = region.end();
1025 while (it != end) {
1026 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 const GLint sy = height - (r.top + r.height());
1028 glScissor(r.left, sy, r.width(), r.height());
1029 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1030 }
1031 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1032 }
1033}
1034
1035void SurfaceFlinger::debugShowFPS() const
1036{
1037 static int mFrameCount;
1038 static int mLastFrameCount = 0;
1039 static nsecs_t mLastFpsTime = 0;
1040 static float mFps = 0;
1041 mFrameCount++;
1042 nsecs_t now = systemTime();
1043 nsecs_t diff = now - mLastFpsTime;
1044 if (diff > ms2ns(250)) {
1045 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1046 mLastFpsTime = now;
1047 mLastFrameCount = mFrameCount;
1048 }
1049 // XXX: mFPS has the value we want
1050 }
1051
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001052status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053{
1054 Mutex::Autolock _l(mStateLock);
1055 addLayer_l(layer);
1056 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1057 return NO_ERROR;
1058}
1059
Mathias Agopian96f08192010-06-02 23:28:45 -07001060status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1061{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001062 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001063 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1064}
1065
1066ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1067 const sp<LayerBaseClient>& lbc)
1068{
1069 Mutex::Autolock _l(mStateLock);
1070
1071 // attach this layer to the client
1072 ssize_t name = client->attachLayer(lbc);
1073
1074 // add this layer to the current state list
1075 addLayer_l(lbc);
1076
1077 return name;
1078}
1079
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001080status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081{
1082 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001083 status_t err = purgatorizeLayer_l(layer);
1084 if (err == NO_ERROR)
1085 setTransactionFlags(eTransactionNeeded);
1086 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087}
1088
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001089status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001091 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1092 if (lbc != 0) {
1093 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1094 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1096 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001097 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 return NO_ERROR;
1099 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001100 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101}
1102
Mathias Agopian9a112062009-04-17 19:36:26 -07001103status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1104{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001105 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001106 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001107
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001108 layerBase->onRemoved();
1109
Mathias Agopian3d579642009-06-04 18:46:21 -07001110 // it's possible that we don't find a layer, because it might
1111 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001112 // from the user because there is a race between Client::destroySurface(),
1113 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001114 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1115}
1116
Mathias Agopian96f08192010-06-02 23:28:45 -07001117status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118{
Mathias Agopian96f08192010-06-02 23:28:45 -07001119 layer->forceVisibilityTransaction();
1120 setTransactionFlags(eTraversalNeeded);
1121 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122}
1123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1125{
1126 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1127}
1128
Mathias Agopianbb641242010-05-18 17:06:55 -07001129uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130{
1131 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1132 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001133 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 }
1135 return old;
1136}
1137
1138void SurfaceFlinger::openGlobalTransaction()
1139{
1140 android_atomic_inc(&mTransactionCount);
1141}
1142
1143void SurfaceFlinger::closeGlobalTransaction()
1144{
1145 if (android_atomic_dec(&mTransactionCount) == 1) {
1146 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001147
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001148 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001149 // take effect before returning.
1150 Mutex::Autolock _l(mStateLock);
1151 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001152 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1153 if (CC_UNLIKELY(err != NO_ERROR)) {
1154 // just in case something goes wrong in SF, return to the
1155 // called after a few seconds.
1156 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1157 mResizeTransationPending = false;
1158 break;
1159 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 }
1162}
1163
1164status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1165{
1166 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1167 return BAD_VALUE;
1168
1169 Mutex::Autolock _l(mStateLock);
1170 mCurrentState.freezeDisplay = 1;
1171 setTransactionFlags(eTransactionNeeded);
1172
1173 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001174 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 return NO_ERROR;
1176}
1177
1178status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1179{
1180 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1181 return BAD_VALUE;
1182
1183 Mutex::Autolock _l(mStateLock);
1184 mCurrentState.freezeDisplay = 0;
1185 setTransactionFlags(eTransactionNeeded);
1186
1187 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001188 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 return NO_ERROR;
1190}
1191
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001192int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001193 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194{
1195 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1196 return BAD_VALUE;
1197
1198 Mutex::Autolock _l(mStateLock);
1199 if (mCurrentState.orientation != orientation) {
1200 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001201 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 mCurrentState.orientation = orientation;
1203 setTransactionFlags(eTransactionNeeded);
1204 mTransactionCV.wait(mStateLock);
1205 } else {
1206 orientation = BAD_VALUE;
1207 }
1208 }
1209 return orientation;
1210}
1211
Mathias Agopian96f08192010-06-02 23:28:45 -07001212sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001213 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1215 uint32_t flags)
1216{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001217 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001219
1220 if (int32_t(w|h) < 0) {
1221 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1222 int(w), int(h));
1223 return surfaceHandle;
1224 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001227 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 switch (flags & eFXSurfaceMask) {
1229 case eFXSurfaceNormal:
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001230#if HAS_PUSH_BUFFERS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001232 layer = createPushBuffersSurface(client, d, w, h, flags);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001233 } else
1234#endif
1235 {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001236 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1237 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 }
1239 break;
1240 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001241 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 break;
1243 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001244 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 break;
1246 }
1247
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001248 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001249 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001250 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001251 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001254 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001255 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001256 params->identity = surfaceHandle->getIdentity();
1257 params->width = w;
1258 params->height = h;
1259 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001260 if (normalLayer != 0) {
1261 Mutex::Autolock _l(mStateLock);
1262 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1263 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001264 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001265
Mathias Agopian96f08192010-06-02 23:28:45 -07001266 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 }
1268
1269 return surfaceHandle;
1270}
1271
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001272sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001273 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001274 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001275 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276{
1277 // initialize the surfaces
1278 switch (format) { // TODO: take h/w into account
1279 case PIXEL_FORMAT_TRANSPARENT:
1280 case PIXEL_FORMAT_TRANSLUCENT:
1281 format = PIXEL_FORMAT_RGBA_8888;
1282 break;
1283 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001284#ifdef NO_RGBX_8888
1285 format = PIXEL_FORMAT_RGB_565;
1286#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001287 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001288#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 break;
1290 }
1291
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001292#ifdef NO_RGBX_8888
1293 if (format == PIXEL_FORMAT_RGBX_8888)
1294 format = PIXEL_FORMAT_RGBA_8888;
1295#endif
1296
Mathias Agopian96f08192010-06-02 23:28:45 -07001297 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001298 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001301 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 }
1303 return layer;
1304}
1305
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001306sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001307 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001308 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309{
Mathias Agopian96f08192010-06-02 23:28:45 -07001310 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 return layer;
1313}
1314
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001315sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001316 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001317 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
Mathias Agopian96f08192010-06-02 23:28:45 -07001319 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 return layer;
1322}
1323
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001324sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001325 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001326 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327{
Mathias Agopian96f08192010-06-02 23:28:45 -07001328 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 return layer;
1331}
1332
Mathias Agopian96f08192010-06-02 23:28:45 -07001333status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334{
Mathias Agopian9a112062009-04-17 19:36:26 -07001335 /*
1336 * called by the window manager, when a surface should be marked for
1337 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001338 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001339 * The surface is removed from the current and drawing lists, but placed
1340 * in the purgatory queue, so it's not destroyed right-away (we need
1341 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001342 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001343
Mathias Agopian48d819a2009-09-10 19:41:18 -07001344 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001345 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001346 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001347 if (layer != 0) {
1348 err = purgatorizeLayer_l(layer);
1349 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001350 setTransactionFlags(eTransactionNeeded);
1351 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001352 }
1353 return err;
1354}
1355
1356status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1357{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001358 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001359
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001360 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001361 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001362 sp<LayerBaseClient> layer;
1363 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001364 MessageDestroySurface(
1365 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1366 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001367 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001368 sp<LayerBaseClient> l(layer);
1369 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001370 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001371 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001372 * remove the layer from the current list -- chances are that it's
1373 * not in the list anyway, because it should have been removed
1374 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001375 * However, a buggy client could have not done that.
1376 * Since we know we don't have any more clients, we don't need
1377 * to use the purgatory.
1378 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001379 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001380 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1381 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001382 return true;
1383 }
1384 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001385
Mathias Agopianbb641242010-05-18 17:06:55 -07001386 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 return NO_ERROR;
1388}
1389
1390status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001391 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 int32_t count,
1393 const layer_state_t* states)
1394{
1395 Mutex::Autolock _l(mStateLock);
1396 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001398 const layer_state_t& s(states[i]);
1399 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001400 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 if (what & ePositionChanged) {
1403 if (layer->setPosition(s.x, s.y))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001407 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001409 mCurrentState.layersSortedByZ.removeAt(idx);
1410 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 // we need traversal (state changed)
1412 // AND transaction (list changed)
1413 flags |= eTransactionNeeded|eTraversalNeeded;
1414 }
1415 }
1416 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001417 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001419 mResizeTransationPending = true;
1420 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 }
1422 if (what & eAlphaChanged) {
1423 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1424 flags |= eTraversalNeeded;
1425 }
1426 if (what & eMatrixChanged) {
1427 if (layer->setMatrix(s.matrix))
1428 flags |= eTraversalNeeded;
1429 }
1430 if (what & eTransparentRegionChanged) {
1431 if (layer->setTransparentRegionHint(s.transparentRegion))
1432 flags |= eTraversalNeeded;
1433 }
1434 if (what & eVisibilityChanged) {
1435 if (layer->setFlags(s.flags, s.mask))
1436 flags |= eTraversalNeeded;
1437 }
1438 }
1439 }
1440 if (flags) {
1441 setTransactionFlags(flags);
1442 }
1443 return NO_ERROR;
1444}
1445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446void SurfaceFlinger::screenReleased(int dpy)
1447{
1448 // this may be called by a signal handler, we can't do too much in here
1449 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1450 signalEvent();
1451}
1452
1453void SurfaceFlinger::screenAcquired(int dpy)
1454{
1455 // this may be called by a signal handler, we can't do too much in here
1456 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1457 signalEvent();
1458}
1459
1460status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1461{
1462 const size_t SIZE = 1024;
1463 char buffer[SIZE];
1464 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001465 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 snprintf(buffer, SIZE, "Permission Denial: "
1467 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1468 IPCThreadState::self()->getCallingPid(),
1469 IPCThreadState::self()->getCallingUid());
1470 result.append(buffer);
1471 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001472
1473 // figure out if we're stuck somewhere
1474 const nsecs_t now = systemTime();
1475 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1476 const nsecs_t inTransaction(mDebugInTransaction);
1477 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1478 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1479
1480 // Try to get the main lock, but don't insist if we can't
1481 // (this would indicate SF is stuck, but we want to be able to
1482 // print something in dumpsys).
1483 int retry = 3;
1484 while (mStateLock.tryLock()<0 && --retry>=0) {
1485 usleep(1000000);
1486 }
1487 const bool locked(retry >= 0);
1488 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001489 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001490 "SurfaceFlinger appears to be unresponsive, "
1491 "dumping anyways (no locks held)\n");
1492 result.append(buffer);
1493 }
1494
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1496 const size_t count = currentLayers.size();
1497 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001498 const sp<LayerBase>& layer(currentLayers[i]);
1499 layer->dump(result, buffer, SIZE);
1500 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 s.transparentRegion.dump(result, "transparentRegion");
1502 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1503 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1504 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001505
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 mWormholeRegion.dump(result, "WormholeRegion");
1507 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1508 snprintf(buffer, SIZE,
1509 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1510 mFreezeDisplay?"yes":"no", mFreezeCount,
1511 mCurrentState.orientation, hw.canDraw());
1512 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001513 snprintf(buffer, SIZE,
1514 " last eglSwapBuffers() time: %f us\n"
1515 " last transaction time : %f us\n",
1516 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1517 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001518
Mathias Agopian9795c422009-08-26 16:36:26 -07001519 if (inSwapBuffersDuration || !locked) {
1520 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1521 inSwapBuffersDuration/1000.0);
1522 result.append(buffer);
1523 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001524
Mathias Agopian9795c422009-08-26 16:36:26 -07001525 if (inTransactionDuration || !locked) {
1526 snprintf(buffer, SIZE, " transaction time: %f us\n",
1527 inTransactionDuration/1000.0);
1528 result.append(buffer);
1529 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001530
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001531 HWComposer& hwc(hw.getHwComposer());
1532 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1533 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1534 mDebugDisableHWC ? "disabled" : "enabled");
1535 result.append(buffer);
1536
Mathias Agopian3330b202009-10-05 17:07:12 -07001537 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001538 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001539
1540 if (locked) {
1541 mStateLock.unlock();
1542 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 }
1544 write(fd, result.string(), result.size());
1545 return NO_ERROR;
1546}
1547
1548status_t SurfaceFlinger::onTransact(
1549 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1550{
1551 switch (code) {
1552 case CREATE_CONNECTION:
1553 case OPEN_GLOBAL_TRANSACTION:
1554 case CLOSE_GLOBAL_TRANSACTION:
1555 case SET_ORIENTATION:
1556 case FREEZE_DISPLAY:
1557 case UNFREEZE_DISPLAY:
1558 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 {
1560 // codes that require permission check
1561 IPCThreadState* ipc = IPCThreadState::self();
1562 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001563 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001564 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1565 LOGE("Permission Denial: "
1566 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1567 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 }
1569 }
1570 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1572 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001573 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001574 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1575 IPCThreadState* ipc = IPCThreadState::self();
1576 const int pid = ipc->getCallingPid();
1577 const int uid = ipc->getCallingUid();
1578 LOGE("Permission Denial: "
1579 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 return PERMISSION_DENIED;
1581 }
1582 int n;
1583 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001584 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001585 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 return NO_ERROR;
1587 case 1002: // SHOW_UPDATES
1588 n = data.readInt32();
1589 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1590 return NO_ERROR;
1591 case 1003: // SHOW_BACKGROUND
1592 n = data.readInt32();
1593 mDebugBackground = n ? 1 : 0;
1594 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001595 case 1008: // toggle use of hw composer
1596 n = data.readInt32();
1597 mDebugDisableHWC = n ? 1 : 0;
1598 mHwWorkListDirty = true;
1599 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 case 1004:{ // repaint everything
1601 Mutex::Autolock _l(mStateLock);
1602 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1603 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1604 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001605 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001607 case 1005:{ // force transaction
1608 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1609 return NO_ERROR;
1610 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001611 case 1006:{ // enable/disable GraphicLog
1612 int enabled = data.readInt32();
1613 GraphicLog::getInstance().setEnabled(enabled);
1614 return NO_ERROR;
1615 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 case 1007: // set mFreezeCount
1617 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001618 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 return NO_ERROR;
1620 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001621 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 reply->writeInt32(0);
1623 reply->writeInt32(mDebugRegion);
1624 reply->writeInt32(mDebugBackground);
1625 return NO_ERROR;
1626 case 1013: {
1627 Mutex::Autolock _l(mStateLock);
1628 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1629 reply->writeInt32(hw.getPageFlipCount());
1630 }
1631 return NO_ERROR;
1632 }
1633 }
1634 return err;
1635}
1636
1637// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001638
1639sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1640{
1641 sp<Layer> result;
1642 Mutex::Autolock _l(mStateLock);
1643 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1644 return result;
1645}
1646
1647// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648
Mathias Agopian96f08192010-06-02 23:28:45 -07001649Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001650 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652}
1653
Mathias Agopian96f08192010-06-02 23:28:45 -07001654Client::~Client()
1655{
Mathias Agopian96f08192010-06-02 23:28:45 -07001656 const size_t count = mLayers.size();
1657 for (size_t i=0 ; i<count ; i++) {
1658 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1659 if (layer != 0) {
1660 mFlinger->removeLayer(layer);
1661 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 }
1663}
1664
Mathias Agopian96f08192010-06-02 23:28:45 -07001665status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001666 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001668
Mathias Agopian96f08192010-06-02 23:28:45 -07001669ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1670{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001671 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001672 mLayers.add(name, layer);
1673 return name;
1674}
1675
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001676void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001677{
Mathias Agopian96f08192010-06-02 23:28:45 -07001678 // we do a linear search here, because this doesn't happen often
1679 const size_t count = mLayers.size();
1680 for (size_t i=0 ; i<count ; i++) {
1681 if (mLayers.valueAt(i) == layer) {
1682 mLayers.removeItemsAt(i, 1);
1683 break;
1684 }
1685 }
1686}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001687sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1688 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001689 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1690 if (layer != 0) {
1691 lbc = layer.promote();
1692 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001693 }
1694 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695}
1696
Mathias Agopian96f08192010-06-02 23:28:45 -07001697sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001698 return 0;
1699}
1700ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1701 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702}
Mathias Agopian96f08192010-06-02 23:28:45 -07001703sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001704 ISurfaceComposerClient::surface_data_t* params, int pid,
1705 const String8& name,
1706 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 uint32_t flags)
1708{
Mathias Agopian96f08192010-06-02 23:28:45 -07001709 return mFlinger->createSurface(this, pid, name, params,
1710 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711}
Mathias Agopian96f08192010-06-02 23:28:45 -07001712status_t Client::destroySurface(SurfaceID sid) {
1713 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714}
Mathias Agopian96f08192010-06-02 23:28:45 -07001715status_t Client::setState(int32_t count, const layer_state_t* states) {
1716 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717}
1718
1719// ---------------------------------------------------------------------------
1720
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001721UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1722 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1723{
1724 const int pgsize = getpagesize();
1725 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1726
1727 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1728 "SurfaceFlinger Client control-block");
1729
1730 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1731 if (ctrlblk) { // construct the shared structure in-place.
1732 new(ctrlblk) SharedClient;
1733 }
1734}
1735
1736UserClient::~UserClient()
1737{
1738 if (ctrlblk) {
1739 ctrlblk->~SharedClient(); // destroy our shared-structure.
1740 }
1741
1742 /*
1743 * When a UserClient dies, it's unclear what to do exactly.
1744 * We could go ahead and destroy all surfaces linked to that client
1745 * however, it wouldn't be fair to the main Client
1746 * (usually the the window-manager), which might want to re-target
1747 * the layer to another UserClient.
1748 * I think the best is to do nothing, or not much; in most cases the
1749 * WM itself will go ahead and clean things up when it detects a client of
1750 * his has died.
1751 * The remaining question is what to display? currently we keep
1752 * just keep the current buffer.
1753 */
1754}
1755
1756status_t UserClient::initCheck() const {
1757 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1758}
1759
1760void UserClient::detachLayer(const Layer* layer)
1761{
1762 int32_t name = layer->getToken();
1763 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001764 int32_t mask = 1LU<<name;
1765 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1766 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1767 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001768 }
1769}
1770
1771sp<IMemoryHeap> UserClient::getControlBlock() const {
1772 return mCblkHeap;
1773}
1774
1775ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1776{
1777 int32_t name = NAME_NOT_FOUND;
1778 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001779 if (layer == 0) {
1780 return name;
1781 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001782
Mathias Agopian579b3f82010-06-08 19:54:15 -07001783 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001784 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001785 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001786 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001787 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001788
1789 name = 0;
1790 do {
1791 int32_t mask = 1LU<<name;
1792 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1793 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001794 status_t err = layer->setToken(
1795 const_cast<UserClient*>(this), ctrlblk, name);
1796 if (err != NO_ERROR) {
1797 // free the name
1798 android_atomic_and(~mask, &mBitmap);
1799 name = err;
1800 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001801 break;
1802 }
1803 if (++name > 31)
1804 name = NO_MEMORY;
1805 } while(name >= 0);
1806
Mathias Agopian53503a92010-06-08 15:40:56 -07001807 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1808 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001809 return name;
1810}
1811
1812sp<ISurface> UserClient::createSurface(
1813 ISurfaceComposerClient::surface_data_t* params, int pid,
1814 const String8& name,
1815 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1816 uint32_t flags) {
1817 return 0;
1818}
1819status_t UserClient::destroySurface(SurfaceID sid) {
1820 return INVALID_OPERATION;
1821}
1822status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1823 return INVALID_OPERATION;
1824}
1825
1826// ---------------------------------------------------------------------------
1827
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828GraphicPlane::GraphicPlane()
1829 : mHw(0)
1830{
1831}
1832
1833GraphicPlane::~GraphicPlane() {
1834 delete mHw;
1835}
1836
1837bool GraphicPlane::initialized() const {
1838 return mHw ? true : false;
1839}
1840
Mathias Agopian2b92d892010-02-08 15:49:35 -08001841int GraphicPlane::getWidth() const {
1842 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001843}
1844
Mathias Agopian2b92d892010-02-08 15:49:35 -08001845int GraphicPlane::getHeight() const {
1846 return mHeight;
1847}
1848
1849void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1850{
1851 mHw = hw;
1852
1853 // initialize the display orientation transform.
1854 // it's a constant that should come from the display driver.
1855 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1856 char property[PROPERTY_VALUE_MAX];
1857 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1858 //displayOrientation
1859 switch (atoi(property)) {
1860 case 90:
1861 displayOrientation = ISurfaceComposer::eOrientation90;
1862 break;
1863 case 270:
1864 displayOrientation = ISurfaceComposer::eOrientation270;
1865 break;
1866 }
1867 }
1868
1869 const float w = hw->getWidth();
1870 const float h = hw->getHeight();
1871 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1872 &mDisplayTransform);
1873 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1874 mDisplayWidth = h;
1875 mDisplayHeight = w;
1876 } else {
1877 mDisplayWidth = w;
1878 mDisplayHeight = h;
1879 }
1880
1881 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882}
1883
1884status_t GraphicPlane::orientationToTransfrom(
1885 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001886{
1887 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 switch (orientation) {
1889 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001890 flags = Transform::ROT_0;
1891 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001892 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001893 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894 break;
1895 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001896 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 break;
1898 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001899 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900 break;
1901 default:
1902 return BAD_VALUE;
1903 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001904 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 return NO_ERROR;
1906}
1907
1908status_t GraphicPlane::setOrientation(int orientation)
1909{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910 // If the rotation can be handled in hardware, this is where
1911 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001912
1913 const DisplayHardware& hw(displayHardware());
1914 const float w = mDisplayWidth;
1915 const float h = mDisplayHeight;
1916 mWidth = int(w);
1917 mHeight = int(h);
1918
1919 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001920 GraphicPlane::orientationToTransfrom(orientation, w, h,
1921 &orientationTransform);
1922 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1923 mWidth = int(h);
1924 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001926
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001927 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001928 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929 return NO_ERROR;
1930}
1931
1932const DisplayHardware& GraphicPlane::displayHardware() const {
1933 return *mHw;
1934}
1935
1936const Transform& GraphicPlane::transform() const {
1937 return mGlobalTransform;
1938}
1939
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001940EGLDisplay GraphicPlane::getEGLDisplay() const {
1941 return mHw->getEGLDisplay();
1942}
1943
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944// ---------------------------------------------------------------------------
1945
1946}; // namespace android