blob: 5a27fc509b7ce9292b9b96988fe3d53dc42ec263 [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 Agopian9795c422009-08-26 16:36:26 -070088 mDebugInSwapBuffers(0),
89 mLastSwapBufferTime(0),
90 mDebugInTransaction(0),
91 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070092 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mConsoleSignals(0),
94 mSecureFrameBuffer(0)
95{
96 init();
97}
98
99void SurfaceFlinger::init()
100{
101 LOGI("SurfaceFlinger is starting");
102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 property_get("debug.sf.showbackground", value, "0");
108 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
Mathias Agopian78fd5012010-04-20 14:51:04 -0700110 LOGI_IF(mDebugRegion, "showupdates enabled");
111 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112}
113
114SurfaceFlinger::~SurfaceFlinger()
115{
116 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
120{
121 return graphicPlane(0).displayHardware().getOverlayEngine();
122}
123
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700124sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700126 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
Mathias Agopian7e27f052010-05-28 14:22:23 -0700129sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130{
Mathias Agopian96f08192010-06-02 23:28:45 -0700131 sp<ISurfaceComposerClient> bclient;
132 sp<Client> client(new Client(this));
133 status_t err = client->initCheck();
134 if (err == NO_ERROR) {
135 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 return bclient;
138}
139
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700140sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
141{
142 sp<ISurfaceComposerClient> bclient;
143 sp<UserClient> client(new UserClient(this));
144 status_t err = client->initCheck();
145 if (err == NO_ERROR) {
146 bclient = client;
147 }
148 return bclient;
149}
150
151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
153{
154 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
155 const GraphicPlane& plane(mGraphicPlanes[dpy]);
156 return plane;
157}
158
159GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
160{
161 return const_cast<GraphicPlane&>(
162 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
163}
164
165void SurfaceFlinger::bootFinished()
166{
167 const nsecs_t now = systemTime();
168 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700169 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700170 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700171 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172}
173
174void SurfaceFlinger::onFirstRef()
175{
176 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
177
178 // Wait for the main thread to be done with its initialization
179 mReadyToRunBarrier.wait();
180}
181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182static inline uint16_t pack565(int r, int g, int b) {
183 return (r<<11)|(g<<5)|b;
184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186status_t SurfaceFlinger::readyToRun()
187{
188 LOGI( "SurfaceFlinger's main thread ready to run. "
189 "Initializing graphics H/W...");
190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 // we only support one display currently
192 int dpy = 0;
193
194 {
195 // initialize the main display
196 GraphicPlane& plane(graphicPlane(dpy));
197 DisplayHardware* const hw = new DisplayHardware(this, dpy);
198 plane.setDisplayHardware(hw);
199 }
200
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700201 // create the shared control-block
202 mServerHeap = new MemoryHeapBase(4096,
203 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
204 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700205
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700206 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
207 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700208
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700209 new(mServerCblk) surface_flinger_cblk_t;
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 // initialize primary screen
212 // (other display should be initialized in the same manner, but
213 // asynchronously, as they could come and go. None of this is supported
214 // yet).
215 const GraphicPlane& plane(graphicPlane(dpy));
216 const DisplayHardware& hw = plane.displayHardware();
217 const uint32_t w = hw.getWidth();
218 const uint32_t h = hw.getHeight();
219 const uint32_t f = hw.getFormat();
220 hw.makeCurrent();
221
222 // initialize the shared control block
223 mServerCblk->connected |= 1<<dpy;
224 display_cblk_t* dcblk = mServerCblk->displays + dpy;
225 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800226 dcblk->w = plane.getWidth();
227 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 dcblk->format = f;
229 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
230 dcblk->xdpi = hw.getDpiX();
231 dcblk->ydpi = hw.getDpiY();
232 dcblk->fps = hw.getRefreshRate();
233 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
235 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700237 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 glEnableClientState(GL_VERTEX_ARRAY);
239 glEnable(GL_SCISSOR_TEST);
240 glShadeModel(GL_FLAT);
241 glDisable(GL_DITHER);
242 glDisable(GL_CULL_FACE);
243
244 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
245 const uint16_t g1 = pack565(0x17,0x2f,0x17);
246 const uint16_t textureData[4] = { g0, g1, g1, g0 };
247 glGenTextures(1, &mWormholeTexName);
248 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
249 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
250 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
251 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
252 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
254 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
255
256 glViewport(0, 0, w, h);
257 glMatrixMode(GL_PROJECTION);
258 glLoadIdentity();
259 glOrthof(0, w, h, 0, 0, 1);
260
261 LayerDim::initDimmer(this, w, h);
262
263 mReadyToRunBarrier.open();
264
265 /*
266 * We're now ready to accept clients...
267 */
268
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700269 // start boot animation
270 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700271
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 return NO_ERROR;
273}
274
275// ----------------------------------------------------------------------------
276#if 0
277#pragma mark -
278#pragma mark Events Handler
279#endif
280
281void SurfaceFlinger::waitForEvent()
282{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700283 while (true) {
284 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800285 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700286 if (UNLIKELY(isFrozen())) {
287 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700288 const nsecs_t now = systemTime();
289 if (mFreezeDisplayTime == 0) {
290 mFreezeDisplayTime = now;
291 }
292 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
293 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700294 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700295
Mathias Agopianbb641242010-05-18 17:06:55 -0700296 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800297
298 // see if we timed out
299 if (isFrozen()) {
300 const nsecs_t now = systemTime();
301 nsecs_t frozenTime = (now - mFreezeDisplayTime);
302 if (frozenTime >= freezeDisplayTimeout) {
303 // we timed out and are still frozen
304 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
305 mFreezeDisplay, mFreezeCount);
306 mFreezeDisplayTime = 0;
307 mFreezeCount = 0;
308 mFreezeDisplay = false;
309 }
310 }
311
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700312 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700313 switch (msg->what) {
314 case MessageQueue::INVALIDATE:
315 // invalidate message, just return to the main loop
316 return;
317 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 }
320}
321
322void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700323 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324}
325
326void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700327 // this is the IPC call
328 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329}
330
Mathias Agopianbb641242010-05-18 17:06:55 -0700331status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
332 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333{
Mathias Agopianbb641242010-05-18 17:06:55 -0700334 return mEventQueue.postMessage(msg, reltime, flags);
335}
336
337status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
338 nsecs_t reltime, uint32_t flags)
339{
340 status_t res = mEventQueue.postMessage(msg, reltime, flags);
341 if (res == NO_ERROR) {
342 msg->wait();
343 }
344 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345}
346
347// ----------------------------------------------------------------------------
348#if 0
349#pragma mark -
350#pragma mark Main loop
351#endif
352
353bool SurfaceFlinger::threadLoop()
354{
355 waitForEvent();
356
357 // check for transactions
358 if (UNLIKELY(mConsoleSignals)) {
359 handleConsoleEvents();
360 }
361
362 if (LIKELY(mTransactionCount == 0)) {
363 // if we're in a global transaction, don't do anything.
364 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
365 uint32_t transactionFlags = getTransactionFlags(mask);
366 if (LIKELY(transactionFlags)) {
367 handleTransaction(transactionFlags);
368 }
369 }
370
371 // post surfaces (if needed)
372 handlePageFlip();
373
Mathias Agopiana350ff92010-08-10 17:14:02 -0700374 if (UNLIKELY(mHwWorkListDirty)) {
375 // build the h/w work list
376 handleWorkList();
377 }
378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700380 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700382
383 const int index = hw.getCurrentBufferIndex();
384 GraphicLog& logger(GraphicLog::getInstance());
385
386 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 handleRepaint();
388
Mathias Agopian74faca22009-09-17 16:18:16 -0700389 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700390 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700391 hw.compositionComplete();
392
Mathias Agopian35b48d12010-09-13 22:57:58 -0700393 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700394 postFramebuffer();
395
Mathias Agopianc479e182010-09-15 12:29:18 -0700396 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398
Mathias Agopian35b48d12010-09-13 22:57:58 -0700399 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 } else {
401 // pretend we did the post
402 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 usleep(16667); // 60 fps period
404 }
405 return true;
406}
407
408void SurfaceFlinger::postFramebuffer()
409{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 if (!mInvalidRegion.isEmpty()) {
411 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700412 const nsecs_t now = systemTime();
413 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700415 mLastSwapBufferTime = systemTime() - now;
416 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 }
419}
420
421void SurfaceFlinger::handleConsoleEvents()
422{
423 // something to do with the console
424 const DisplayHardware& hw = graphicPlane(0).displayHardware();
425
426 int what = android_atomic_and(0, &mConsoleSignals);
427 if (what & eConsoleAcquired) {
428 hw.acquireScreen();
429 }
430
431 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700432 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 hw.releaseScreen();
435 }
436
437 if (what & eConsoleReleased) {
438 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 hw.releaseScreen();
440 } else {
441 mDeferReleaseConsole = true;
442 }
443 }
444
445 mDirtyRegion.set(hw.bounds());
446}
447
448void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
449{
Mathias Agopian3d579642009-06-04 18:46:21 -0700450 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451
Mathias Agopian4da75192010-08-10 17:19:56 -0700452 /*
453 * Perform and commit the transaction
454 */
455
Mathias Agopian3d579642009-06-04 18:46:21 -0700456 { // scope for the lock
457 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700458 const nsecs_t now = systemTime();
459 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700460 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700461 mLastTransactionTime = systemTime() - now;
462 mDebugInTransaction = 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700463 mHwWorkListDirty = true;
Mathias Agopian4da75192010-08-10 17:19:56 -0700464 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700465 }
466
Mathias Agopian4da75192010-08-10 17:19:56 -0700467 /*
468 * Clean-up all layers that went away
469 * (do this without the lock held)
470 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700471
Mathias Agopian3d579642009-06-04 18:46:21 -0700472 const size_t count = ditchedLayers.size();
473 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700474 if (ditchedLayers[i] != 0) {
475 //LOGD("ditching layer %p", ditchedLayers[i].get());
476 ditchedLayers[i]->ditch();
477 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700478 }
479}
480
481void SurfaceFlinger::handleTransactionLocked(
482 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
483{
484 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 const size_t count = currentLayers.size();
486
487 /*
488 * Traversal of the children
489 * (perform the transaction for each of them if needed)
490 */
491
492 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
493 if (layersNeedTransaction) {
494 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700495 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
497 if (!trFlags) continue;
498
499 const uint32_t flags = layer->doTransaction(0);
500 if (flags & Layer::eVisibleRegion)
501 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 }
503 }
504
505 /*
506 * Perform our own transaction if needed
507 */
508
509 if (transactionFlags & eTransactionNeeded) {
510 if (mCurrentState.orientation != mDrawingState.orientation) {
511 // the orientation has changed, recompute all visible regions
512 // and invalidate everything.
513
514 const int dpy = 0;
515 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700516 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 GraphicPlane& plane(graphicPlane(dpy));
518 plane.setOrientation(orientation);
519
520 // update the shared control block
521 const DisplayHardware& hw(plane.displayHardware());
522 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
523 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800524 dcblk->w = plane.getWidth();
525 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526
527 mVisibleRegionsDirty = true;
528 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 }
530
531 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
532 // freezing or unfreezing the display -> trigger animation if needed
533 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800534 if (mFreezeDisplay)
535 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 }
537
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700538 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
539 // layers have been added
540 mVisibleRegionsDirty = true;
541 }
542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 // some layers might have been removed, so
544 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700546 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700548 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700549 const size_t count = previousLayers.size();
550 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700551 const sp<LayerBase>& layer(previousLayers[i]);
552 if (currentLayers.indexOf( layer ) < 0) {
553 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700554 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700555 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700556 }
557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 }
560
561 commitTransaction();
562}
563
564sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
565{
566 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
567}
568
569void SurfaceFlinger::computeVisibleRegions(
570 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
571{
572 const GraphicPlane& plane(graphicPlane(0));
573 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700574 const DisplayHardware& hw(plane.displayHardware());
575 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576
577 Region aboveOpaqueLayers;
578 Region aboveCoveredLayers;
579 Region dirty;
580
581 bool secureFrameBuffer = false;
582
583 size_t i = currentLayers.size();
584 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700585 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 layer->validateVisibility(planeTransform);
587
588 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700589 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590
Mathias Agopianab028732010-03-16 16:41:46 -0700591 /*
592 * opaqueRegion: area of a surface that is fully opaque.
593 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700595
596 /*
597 * visibleRegion: area of a surface that is visible on screen
598 * and not fully transparent. This is essentially the layer's
599 * footprint minus the opaque regions above it.
600 * Areas covered by a translucent surface are considered visible.
601 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700603
604 /*
605 * coveredRegion: area of a surface that is covered by all
606 * visible regions above it (which includes the translucent areas).
607 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700609
610
611 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700612 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700614 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700616 visibleRegion.andSelf(screenRegion);
617 if (!visibleRegion.isEmpty()) {
618 // Remove the transparent area from the visible region
619 if (translucent) {
620 visibleRegion.subtractSelf(layer->transparentRegionScreen);
621 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622
Mathias Agopianab028732010-03-16 16:41:46 -0700623 // compute the opaque region
624 const int32_t layerOrientation = layer->getOrientation();
625 if (s.alpha==255 && !translucent &&
626 ((layerOrientation & Transform::ROT_INVALID) == false)) {
627 // the opaque region is the layer's footprint
628 opaqueRegion = visibleRegion;
629 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 }
631 }
632
Mathias Agopianab028732010-03-16 16:41:46 -0700633 // Clip the covered region to the visible region
634 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
635
636 // Update aboveCoveredLayers for next (lower) layer
637 aboveCoveredLayers.orSelf(visibleRegion);
638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 // subtract the opaque region covered by the layers above us
640 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641
642 // compute this layer's dirty region
643 if (layer->contentDirty) {
644 // we need to invalidate the whole region
645 dirty = visibleRegion;
646 // as well, as the old visible region
647 dirty.orSelf(layer->visibleRegionScreen);
648 layer->contentDirty = false;
649 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700650 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700651 * the exposed region consists of two components:
652 * 1) what's VISIBLE now and was COVERED before
653 * 2) what's EXPOSED now less what was EXPOSED before
654 *
655 * note that (1) is conservative, we start with the whole
656 * visible region but only keep what used to be covered by
657 * something -- which mean it may have been exposed.
658 *
659 * (2) handles areas that were not covered by anything but got
660 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700661 */
Mathias Agopianab028732010-03-16 16:41:46 -0700662 const Region newExposed = visibleRegion - coveredRegion;
663 const Region oldVisibleRegion = layer->visibleRegionScreen;
664 const Region oldCoveredRegion = layer->coveredRegionScreen;
665 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
666 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 }
668 dirty.subtractSelf(aboveOpaqueLayers);
669
670 // accumulate to the screen dirty region
671 dirtyRegion.orSelf(dirty);
672
Mathias Agopianab028732010-03-16 16:41:46 -0700673 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700675
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 // Store the visible region is screen space
677 layer->setVisibleRegion(visibleRegion);
678 layer->setCoveredRegion(coveredRegion);
679
Mathias Agopian97011222009-07-28 10:57:27 -0700680 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 if (layer->isSecure() && !visibleRegion.isEmpty()) {
682 secureFrameBuffer = true;
683 }
684 }
685
Mathias Agopian97011222009-07-28 10:57:27 -0700686 // invalidate the areas where a layer was removed
687 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
688 mDirtyRegionRemovedLayer.clear();
689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 mSecureFrameBuffer = secureFrameBuffer;
691 opaqueRegion = aboveOpaqueLayers;
692}
693
694
695void SurfaceFlinger::commitTransaction()
696{
697 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700698 mResizeTransationPending = false;
699 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700}
701
702void SurfaceFlinger::handlePageFlip()
703{
704 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700705 LayerVector& currentLayers(
706 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 visibleRegions |= lockPageFlip(currentLayers);
708
709 const DisplayHardware& hw = graphicPlane(0).displayHardware();
710 const Region screenRegion(hw.bounds());
711 if (visibleRegions) {
712 Region opaqueRegion;
713 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700714
715 /*
716 * rebuild the visible layer list
717 */
718 mVisibleLayersSortedByZ.clear();
719 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
720 size_t count = currentLayers.size();
721 mVisibleLayersSortedByZ.setCapacity(count);
722 for (size_t i=0 ; i<count ; i++) {
723 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
724 mVisibleLayersSortedByZ.add(currentLayers[i]);
725 }
726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 mWormholeRegion = screenRegion.subtract(opaqueRegion);
728 mVisibleRegionsDirty = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700729 mHwWorkListDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 }
731
732 unlockPageFlip(currentLayers);
733 mDirtyRegion.andSelf(screenRegion);
734}
735
736bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
737{
738 bool recomputeVisibleRegions = false;
739 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700740 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700742 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 layer->lockPageFlip(recomputeVisibleRegions);
744 }
745 return recomputeVisibleRegions;
746}
747
748void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
749{
750 const GraphicPlane& plane(graphicPlane(0));
751 const Transform& planeTransform(plane.transform());
752 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700753 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700755 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 layer->unlockPageFlip(planeTransform, mDirtyRegion);
757 }
758}
759
Mathias Agopiana350ff92010-08-10 17:14:02 -0700760void SurfaceFlinger::handleWorkList()
761{
762 mHwWorkListDirty = false;
763 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
764 if (hwc.initCheck() == NO_ERROR) {
765 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
766 const size_t count = currentLayers.size();
767 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700768 hwc_layer_t* const cur(hwc.getLayers());
769 for (size_t i=0 ; cur && i<count ; i++) {
770 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700771 }
772 }
773}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700774
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775void SurfaceFlinger::handleRepaint()
776{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700777 // compute the invalid region
778 mInvalidRegion.orSelf(mDirtyRegion);
779 if (mInvalidRegion.isEmpty()) {
780 // nothing to do
781 return;
782 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783
784 if (UNLIKELY(mDebugRegion)) {
785 debugFlashRegions();
786 }
787
Mathias Agopianb8a55602009-06-26 19:06:36 -0700788 // set the frame buffer
789 const DisplayHardware& hw(graphicPlane(0).displayHardware());
790 glMatrixMode(GL_MODELVIEW);
791 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792
793 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700794 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
795 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700796 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700797 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
798 // takes a rectangle, we must make sure to update that whole
799 // rectangle in that case
800 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700801 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700802 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 mDirtyRegion.set(mInvalidRegion.bounds());
804 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700805 // in the BUFFER_PRESERVED case, obviously, we can update only
806 // what's needed and nothing more.
807 // NOTE: this is NOT a common case, as preserving the backbuffer
808 // is costly and usually involves copying the whole update back.
809 }
810 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700811 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700812 // We need to redraw the rectangle that will be updated
813 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700814 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700815 // rectangle instead of a region (see DisplayHardware::flip())
816 mDirtyRegion.set(mInvalidRegion.bounds());
817 } else {
818 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 mDirtyRegion.set(hw.bounds());
820 mInvalidRegion = mDirtyRegion;
821 }
822 }
823
824 // compose all surfaces
825 composeSurfaces(mDirtyRegion);
826
827 // clear the dirty regions
828 mDirtyRegion.clear();
829}
830
831void SurfaceFlinger::composeSurfaces(const Region& dirty)
832{
833 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
834 // should never happen unless the window manager has a bug
835 // draw something...
836 drawWormhole();
837 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700838
839 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700840 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700841 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700842
843 const DisplayHardware& hw(graphicPlane(0).displayHardware());
844 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700845 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700846
Mathias Agopian45721772010-08-12 15:03:26 -0700847 LOGE_IF(cur && hwc.getNumLayers() != count,
848 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
849 hwc.getNumLayers(), count);
850
851 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700852 if (hwc.initCheck() == NO_ERROR) {
853 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
854 }
Mathias Agopian45721772010-08-12 15:03:26 -0700855
856 /*
857 * update the per-frame h/w composer data for each layer
858 * and build the transparent region of the FB
859 */
860 Region transparent;
861 if (cur) {
862 for (size_t i=0 ; i<count ; i++) {
863 const sp<LayerBase>& layer(layers[i]);
864 layer->setPerFrameData(&cur[i]);
865 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
866 if (!(layer->needsBlending())) {
867 transparent.orSelf(layer->visibleRegionScreen);
868 }
869 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700870 }
871 err = hwc.prepare();
872 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
873 }
874
Mathias Agopian45721772010-08-12 15:03:26 -0700875 /*
876 * clear the area of the FB that need to be transparent
877 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700878 transparent.andSelf(dirty);
879 if (!transparent.isEmpty()) {
880 glClearColor(0,0,0,0);
881 Region::const_iterator it = transparent.begin();
882 Region::const_iterator const end = transparent.end();
883 const int32_t height = hw.getHeight();
884 while (it != end) {
885 const Rect& r(*it++);
886 const GLint sy = height - (r.top + r.height());
887 glScissor(r.left, sy, r.width(), r.height());
888 glClear(GL_COLOR_BUFFER_BIT);
889 }
890 }
Mathias Agopian45721772010-08-12 15:03:26 -0700891
892
893 /*
894 * and then, render the layers targeted at the framebuffer
895 */
896 for (size_t i=0 ; i<count ; i++) {
897 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700898 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
899 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700900 // skip layers handled by the HAL
901 continue;
902 }
903 }
904 const sp<LayerBase>& layer(layers[i]);
905 const Region clip(dirty.intersect(layer->visibleRegionScreen));
906 if (!clip.isEmpty()) {
907 layer->draw(clip);
908 }
909 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910}
911
912void SurfaceFlinger::unlockClients()
913{
914 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
915 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700916 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700918 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 layer->finishPageFlip();
920 }
921}
922
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923void SurfaceFlinger::debugFlashRegions()
924{
Mathias Agopian0a917752010-06-14 21:20:00 -0700925 const DisplayHardware& hw(graphicPlane(0).displayHardware());
926 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700927
Mathias Agopian0a917752010-06-14 21:20:00 -0700928 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
929 (flags & DisplayHardware::BUFFER_PRESERVED))) {
930 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
931 mDirtyRegion.bounds() : hw.bounds());
932 composeSurfaces(repaint);
933 }
934
935 TextureManager::deactivateTextures();
936
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 glDisable(GL_BLEND);
938 glDisable(GL_DITHER);
939 glDisable(GL_SCISSOR_TEST);
940
Mathias Agopian0926f502009-05-04 14:17:04 -0700941 static int toggle = 0;
942 toggle = 1 - toggle;
943 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700944 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700945 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700946 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700947 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948
Mathias Agopian20f68782009-05-11 00:03:41 -0700949 Region::const_iterator it = mDirtyRegion.begin();
950 Region::const_iterator const end = mDirtyRegion.end();
951 while (it != end) {
952 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 GLfloat vertices[][2] = {
954 { r.left, r.top },
955 { r.left, r.bottom },
956 { r.right, r.bottom },
957 { r.right, r.top }
958 };
959 glVertexPointer(2, GL_FLOAT, 0, vertices);
960 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
961 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700962
Mathias Agopianb8a55602009-06-26 19:06:36 -0700963 if (mInvalidRegion.isEmpty()) {
964 mDirtyRegion.dump("mDirtyRegion");
965 mInvalidRegion.dump("mInvalidRegion");
966 }
967 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968
969 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700970 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
972 glEnable(GL_SCISSOR_TEST);
973 //mDirtyRegion.dump("mDirtyRegion");
974}
975
976void SurfaceFlinger::drawWormhole() const
977{
978 const Region region(mWormholeRegion.intersect(mDirtyRegion));
979 if (region.isEmpty())
980 return;
981
982 const DisplayHardware& hw(graphicPlane(0).displayHardware());
983 const int32_t width = hw.getWidth();
984 const int32_t height = hw.getHeight();
985
986 glDisable(GL_BLEND);
987 glDisable(GL_DITHER);
988
989 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700990 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700991 Region::const_iterator it = region.begin();
992 Region::const_iterator const end = region.end();
993 while (it != end) {
994 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 const GLint sy = height - (r.top + r.height());
996 glScissor(r.left, sy, r.width(), r.height());
997 glClear(GL_COLOR_BUFFER_BIT);
998 }
999 } else {
1000 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1001 { width, height }, { 0, height } };
1002 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1003 glVertexPointer(2, GL_SHORT, 0, vertices);
1004 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1005 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian0a917752010-06-14 21:20:00 -07001006#if defined(GL_OES_texture_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001007 if (GLExtensions::getInstance().haveTextureExternal()) {
1008 glDisable(GL_TEXTURE_EXTERNAL_OES);
1009 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001010#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 glEnable(GL_TEXTURE_2D);
1012 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1013 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1014 glMatrixMode(GL_TEXTURE);
1015 glLoadIdentity();
1016 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001017 Region::const_iterator it = region.begin();
1018 Region::const_iterator const end = region.end();
1019 while (it != end) {
1020 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 const GLint sy = height - (r.top + r.height());
1022 glScissor(r.left, sy, r.width(), r.height());
1023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1024 }
1025 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1026 }
1027}
1028
1029void SurfaceFlinger::debugShowFPS() const
1030{
1031 static int mFrameCount;
1032 static int mLastFrameCount = 0;
1033 static nsecs_t mLastFpsTime = 0;
1034 static float mFps = 0;
1035 mFrameCount++;
1036 nsecs_t now = systemTime();
1037 nsecs_t diff = now - mLastFpsTime;
1038 if (diff > ms2ns(250)) {
1039 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1040 mLastFpsTime = now;
1041 mLastFrameCount = mFrameCount;
1042 }
1043 // XXX: mFPS has the value we want
1044 }
1045
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001046status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047{
1048 Mutex::Autolock _l(mStateLock);
1049 addLayer_l(layer);
1050 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1051 return NO_ERROR;
1052}
1053
Mathias Agopian96f08192010-06-02 23:28:45 -07001054status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1055{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001056 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001057 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1058}
1059
1060ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1061 const sp<LayerBaseClient>& lbc)
1062{
1063 Mutex::Autolock _l(mStateLock);
1064
1065 // attach this layer to the client
1066 ssize_t name = client->attachLayer(lbc);
1067
1068 // add this layer to the current state list
1069 addLayer_l(lbc);
1070
1071 return name;
1072}
1073
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075{
1076 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001077 status_t err = purgatorizeLayer_l(layer);
1078 if (err == NO_ERROR)
1079 setTransactionFlags(eTransactionNeeded);
1080 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081}
1082
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001083status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001085 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1086 if (lbc != 0) {
1087 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1088 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1090 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001091 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 return NO_ERROR;
1093 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001094 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopian9a112062009-04-17 19:36:26 -07001097status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1098{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001099 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001100 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001101
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001102 layerBase->onRemoved();
1103
Mathias Agopian3d579642009-06-04 18:46:21 -07001104 // it's possible that we don't find a layer, because it might
1105 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001106 // from the user because there is a race between Client::destroySurface(),
1107 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001108 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1109}
1110
Mathias Agopian96f08192010-06-02 23:28:45 -07001111status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112{
Mathias Agopian96f08192010-06-02 23:28:45 -07001113 layer->forceVisibilityTransaction();
1114 setTransactionFlags(eTraversalNeeded);
1115 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116}
1117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1119{
1120 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1121}
1122
Mathias Agopianbb641242010-05-18 17:06:55 -07001123uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124{
1125 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1126 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001127 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 }
1129 return old;
1130}
1131
1132void SurfaceFlinger::openGlobalTransaction()
1133{
1134 android_atomic_inc(&mTransactionCount);
1135}
1136
1137void SurfaceFlinger::closeGlobalTransaction()
1138{
1139 if (android_atomic_dec(&mTransactionCount) == 1) {
1140 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001141
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001142 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001143 // take effect before returning.
1144 Mutex::Autolock _l(mStateLock);
1145 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001146 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1147 if (CC_UNLIKELY(err != NO_ERROR)) {
1148 // just in case something goes wrong in SF, return to the
1149 // called after a few seconds.
1150 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1151 mResizeTransationPending = false;
1152 break;
1153 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001154 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155 }
1156}
1157
1158status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1159{
1160 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1161 return BAD_VALUE;
1162
1163 Mutex::Autolock _l(mStateLock);
1164 mCurrentState.freezeDisplay = 1;
1165 setTransactionFlags(eTransactionNeeded);
1166
1167 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001168 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 return NO_ERROR;
1170}
1171
1172status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1173{
1174 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1175 return BAD_VALUE;
1176
1177 Mutex::Autolock _l(mStateLock);
1178 mCurrentState.freezeDisplay = 0;
1179 setTransactionFlags(eTransactionNeeded);
1180
1181 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001182 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 return NO_ERROR;
1184}
1185
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001186int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001187 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188{
1189 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1190 return BAD_VALUE;
1191
1192 Mutex::Autolock _l(mStateLock);
1193 if (mCurrentState.orientation != orientation) {
1194 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001195 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 mCurrentState.orientation = orientation;
1197 setTransactionFlags(eTransactionNeeded);
1198 mTransactionCV.wait(mStateLock);
1199 } else {
1200 orientation = BAD_VALUE;
1201 }
1202 }
1203 return orientation;
1204}
1205
Mathias Agopian96f08192010-06-02 23:28:45 -07001206sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001207 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1209 uint32_t flags)
1210{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001211 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001213
1214 if (int32_t(w|h) < 0) {
1215 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1216 int(w), int(h));
1217 return surfaceHandle;
1218 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001221 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 switch (flags & eFXSurfaceMask) {
1223 case eFXSurfaceNormal:
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001224#if HAS_PUSH_BUFFERS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001226 layer = createPushBuffersSurface(client, d, w, h, flags);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001227 } else
1228#endif
1229 {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001230 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1231 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 }
1233 break;
1234 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001235 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 break;
1237 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001238 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 break;
1240 }
1241
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001242 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001243 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001244 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001245 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001248 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001249 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001250 params->identity = surfaceHandle->getIdentity();
1251 params->width = w;
1252 params->height = h;
1253 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001254 if (normalLayer != 0) {
1255 Mutex::Autolock _l(mStateLock);
1256 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1257 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001258 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001259
Mathias Agopian96f08192010-06-02 23:28:45 -07001260 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 }
1262
1263 return surfaceHandle;
1264}
1265
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001266sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001267 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001268 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001269 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270{
1271 // initialize the surfaces
1272 switch (format) { // TODO: take h/w into account
1273 case PIXEL_FORMAT_TRANSPARENT:
1274 case PIXEL_FORMAT_TRANSLUCENT:
1275 format = PIXEL_FORMAT_RGBA_8888;
1276 break;
1277 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001278#ifdef NO_RGBX_8888
1279 format = PIXEL_FORMAT_RGB_565;
1280#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001281 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001282#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 break;
1284 }
1285
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001286#ifdef NO_RGBX_8888
1287 if (format == PIXEL_FORMAT_RGBX_8888)
1288 format = PIXEL_FORMAT_RGBA_8888;
1289#endif
1290
Mathias Agopian96f08192010-06-02 23:28:45 -07001291 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001292 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001293 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001295 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 }
1297 return layer;
1298}
1299
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001300sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001301 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001302 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303{
Mathias Agopian96f08192010-06-02 23:28:45 -07001304 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 return layer;
1307}
1308
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001309sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001310 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001311 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312{
Mathias Agopian96f08192010-06-02 23:28:45 -07001313 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 return layer;
1316}
1317
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001318sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001319 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321{
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 return layer;
1325}
1326
Mathias Agopian96f08192010-06-02 23:28:45 -07001327status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
Mathias Agopian9a112062009-04-17 19:36:26 -07001329 /*
1330 * called by the window manager, when a surface should be marked for
1331 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001332 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001333 * The surface is removed from the current and drawing lists, but placed
1334 * in the purgatory queue, so it's not destroyed right-away (we need
1335 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001336 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001337
Mathias Agopian48d819a2009-09-10 19:41:18 -07001338 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001339 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001340 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001341 if (layer != 0) {
1342 err = purgatorizeLayer_l(layer);
1343 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001344 setTransactionFlags(eTransactionNeeded);
1345 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001346 }
1347 return err;
1348}
1349
1350status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1351{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001352 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001353
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001354 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001355 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001356 sp<LayerBaseClient> layer;
1357 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001358 MessageDestroySurface(
1359 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1360 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001361 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001362 sp<LayerBaseClient> l(layer);
1363 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001364 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001365 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001366 * remove the layer from the current list -- chances are that it's
1367 * not in the list anyway, because it should have been removed
1368 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001369 * However, a buggy client could have not done that.
1370 * Since we know we don't have any more clients, we don't need
1371 * to use the purgatory.
1372 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001373 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001374 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1375 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001376 return true;
1377 }
1378 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001379
Mathias Agopianbb641242010-05-18 17:06:55 -07001380 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 return NO_ERROR;
1382}
1383
1384status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001385 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 int32_t count,
1387 const layer_state_t* states)
1388{
1389 Mutex::Autolock _l(mStateLock);
1390 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001392 const layer_state_t& s(states[i]);
1393 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001394 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 if (what & ePositionChanged) {
1397 if (layer->setPosition(s.x, s.y))
1398 flags |= eTraversalNeeded;
1399 }
1400 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001401 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001403 mCurrentState.layersSortedByZ.removeAt(idx);
1404 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 // we need traversal (state changed)
1406 // AND transaction (list changed)
1407 flags |= eTransactionNeeded|eTraversalNeeded;
1408 }
1409 }
1410 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001411 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001413 mResizeTransationPending = true;
1414 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 }
1416 if (what & eAlphaChanged) {
1417 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1418 flags |= eTraversalNeeded;
1419 }
1420 if (what & eMatrixChanged) {
1421 if (layer->setMatrix(s.matrix))
1422 flags |= eTraversalNeeded;
1423 }
1424 if (what & eTransparentRegionChanged) {
1425 if (layer->setTransparentRegionHint(s.transparentRegion))
1426 flags |= eTraversalNeeded;
1427 }
1428 if (what & eVisibilityChanged) {
1429 if (layer->setFlags(s.flags, s.mask))
1430 flags |= eTraversalNeeded;
1431 }
1432 }
1433 }
1434 if (flags) {
1435 setTransactionFlags(flags);
1436 }
1437 return NO_ERROR;
1438}
1439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440void SurfaceFlinger::screenReleased(int dpy)
1441{
1442 // this may be called by a signal handler, we can't do too much in here
1443 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1444 signalEvent();
1445}
1446
1447void SurfaceFlinger::screenAcquired(int dpy)
1448{
1449 // this may be called by a signal handler, we can't do too much in here
1450 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1451 signalEvent();
1452}
1453
1454status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1455{
1456 const size_t SIZE = 1024;
1457 char buffer[SIZE];
1458 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001459 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 snprintf(buffer, SIZE, "Permission Denial: "
1461 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1462 IPCThreadState::self()->getCallingPid(),
1463 IPCThreadState::self()->getCallingUid());
1464 result.append(buffer);
1465 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001466
1467 // figure out if we're stuck somewhere
1468 const nsecs_t now = systemTime();
1469 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1470 const nsecs_t inTransaction(mDebugInTransaction);
1471 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1472 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1473
1474 // Try to get the main lock, but don't insist if we can't
1475 // (this would indicate SF is stuck, but we want to be able to
1476 // print something in dumpsys).
1477 int retry = 3;
1478 while (mStateLock.tryLock()<0 && --retry>=0) {
1479 usleep(1000000);
1480 }
1481 const bool locked(retry >= 0);
1482 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001483 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001484 "SurfaceFlinger appears to be unresponsive, "
1485 "dumping anyways (no locks held)\n");
1486 result.append(buffer);
1487 }
1488
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1490 const size_t count = currentLayers.size();
1491 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001492 const sp<LayerBase>& layer(currentLayers[i]);
1493 layer->dump(result, buffer, SIZE);
1494 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 s.transparentRegion.dump(result, "transparentRegion");
1496 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1497 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1498 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 mWormholeRegion.dump(result, "WormholeRegion");
1501 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1502 snprintf(buffer, SIZE,
1503 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1504 mFreezeDisplay?"yes":"no", mFreezeCount,
1505 mCurrentState.orientation, hw.canDraw());
1506 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001507 snprintf(buffer, SIZE,
1508 " last eglSwapBuffers() time: %f us\n"
1509 " last transaction time : %f us\n",
1510 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1511 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001512
Mathias Agopian9795c422009-08-26 16:36:26 -07001513 if (inSwapBuffersDuration || !locked) {
1514 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1515 inSwapBuffersDuration/1000.0);
1516 result.append(buffer);
1517 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001518
Mathias Agopian9795c422009-08-26 16:36:26 -07001519 if (inTransactionDuration || !locked) {
1520 snprintf(buffer, SIZE, " transaction time: %f us\n",
1521 inTransactionDuration/1000.0);
1522 result.append(buffer);
1523 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001524
Mathias Agopian3330b202009-10-05 17:07:12 -07001525 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001526 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001527
1528 if (locked) {
1529 mStateLock.unlock();
1530 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 }
1532 write(fd, result.string(), result.size());
1533 return NO_ERROR;
1534}
1535
1536status_t SurfaceFlinger::onTransact(
1537 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1538{
1539 switch (code) {
1540 case CREATE_CONNECTION:
1541 case OPEN_GLOBAL_TRANSACTION:
1542 case CLOSE_GLOBAL_TRANSACTION:
1543 case SET_ORIENTATION:
1544 case FREEZE_DISPLAY:
1545 case UNFREEZE_DISPLAY:
1546 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 {
1548 // codes that require permission check
1549 IPCThreadState* ipc = IPCThreadState::self();
1550 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001551 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001552 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1553 LOGE("Permission Denial: "
1554 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1555 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 }
1557 }
1558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1560 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001561 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001562 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1563 IPCThreadState* ipc = IPCThreadState::self();
1564 const int pid = ipc->getCallingPid();
1565 const int uid = ipc->getCallingUid();
1566 LOGE("Permission Denial: "
1567 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 return PERMISSION_DENIED;
1569 }
1570 int n;
1571 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001572 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001573 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 return NO_ERROR;
1575 case 1002: // SHOW_UPDATES
1576 n = data.readInt32();
1577 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1578 return NO_ERROR;
1579 case 1003: // SHOW_BACKGROUND
1580 n = data.readInt32();
1581 mDebugBackground = n ? 1 : 0;
1582 return NO_ERROR;
1583 case 1004:{ // repaint everything
1584 Mutex::Autolock _l(mStateLock);
1585 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1586 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1587 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001588 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001590 case 1005:{ // force transaction
1591 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1592 return NO_ERROR;
1593 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001594 case 1006:{ // enable/disable GraphicLog
1595 int enabled = data.readInt32();
1596 GraphicLog::getInstance().setEnabled(enabled);
1597 return NO_ERROR;
1598 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599 case 1007: // set mFreezeCount
1600 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001601 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602 return NO_ERROR;
1603 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001604 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 reply->writeInt32(0);
1606 reply->writeInt32(mDebugRegion);
1607 reply->writeInt32(mDebugBackground);
1608 return NO_ERROR;
1609 case 1013: {
1610 Mutex::Autolock _l(mStateLock);
1611 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1612 reply->writeInt32(hw.getPageFlipCount());
1613 }
1614 return NO_ERROR;
1615 }
1616 }
1617 return err;
1618}
1619
1620// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001621
1622sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1623{
1624 sp<Layer> result;
1625 Mutex::Autolock _l(mStateLock);
1626 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1627 return result;
1628}
1629
1630// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631
Mathias Agopian96f08192010-06-02 23:28:45 -07001632Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001633 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635}
1636
Mathias Agopian96f08192010-06-02 23:28:45 -07001637Client::~Client()
1638{
Mathias Agopian96f08192010-06-02 23:28:45 -07001639 const size_t count = mLayers.size();
1640 for (size_t i=0 ; i<count ; i++) {
1641 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1642 if (layer != 0) {
1643 mFlinger->removeLayer(layer);
1644 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 }
1646}
1647
Mathias Agopian96f08192010-06-02 23:28:45 -07001648status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001649 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001651
Mathias Agopian96f08192010-06-02 23:28:45 -07001652ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1653{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001654 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001655 mLayers.add(name, layer);
1656 return name;
1657}
1658
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001659void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001660{
Mathias Agopian96f08192010-06-02 23:28:45 -07001661 // we do a linear search here, because this doesn't happen often
1662 const size_t count = mLayers.size();
1663 for (size_t i=0 ; i<count ; i++) {
1664 if (mLayers.valueAt(i) == layer) {
1665 mLayers.removeItemsAt(i, 1);
1666 break;
1667 }
1668 }
1669}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001670sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1671 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001672 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1673 if (layer != 0) {
1674 lbc = layer.promote();
1675 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001676 }
1677 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678}
1679
Mathias Agopian96f08192010-06-02 23:28:45 -07001680sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001681 return 0;
1682}
1683ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1684 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685}
Mathias Agopian96f08192010-06-02 23:28:45 -07001686sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001687 ISurfaceComposerClient::surface_data_t* params, int pid,
1688 const String8& name,
1689 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 uint32_t flags)
1691{
Mathias Agopian96f08192010-06-02 23:28:45 -07001692 return mFlinger->createSurface(this, pid, name, params,
1693 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694}
Mathias Agopian96f08192010-06-02 23:28:45 -07001695status_t Client::destroySurface(SurfaceID sid) {
1696 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697}
Mathias Agopian96f08192010-06-02 23:28:45 -07001698status_t Client::setState(int32_t count, const layer_state_t* states) {
1699 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700}
1701
1702// ---------------------------------------------------------------------------
1703
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001704UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1705 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1706{
1707 const int pgsize = getpagesize();
1708 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1709
1710 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1711 "SurfaceFlinger Client control-block");
1712
1713 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1714 if (ctrlblk) { // construct the shared structure in-place.
1715 new(ctrlblk) SharedClient;
1716 }
1717}
1718
1719UserClient::~UserClient()
1720{
1721 if (ctrlblk) {
1722 ctrlblk->~SharedClient(); // destroy our shared-structure.
1723 }
1724
1725 /*
1726 * When a UserClient dies, it's unclear what to do exactly.
1727 * We could go ahead and destroy all surfaces linked to that client
1728 * however, it wouldn't be fair to the main Client
1729 * (usually the the window-manager), which might want to re-target
1730 * the layer to another UserClient.
1731 * I think the best is to do nothing, or not much; in most cases the
1732 * WM itself will go ahead and clean things up when it detects a client of
1733 * his has died.
1734 * The remaining question is what to display? currently we keep
1735 * just keep the current buffer.
1736 */
1737}
1738
1739status_t UserClient::initCheck() const {
1740 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1741}
1742
1743void UserClient::detachLayer(const Layer* layer)
1744{
1745 int32_t name = layer->getToken();
1746 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001747 int32_t mask = 1LU<<name;
1748 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1749 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1750 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001751 }
1752}
1753
1754sp<IMemoryHeap> UserClient::getControlBlock() const {
1755 return mCblkHeap;
1756}
1757
1758ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1759{
1760 int32_t name = NAME_NOT_FOUND;
1761 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001762 if (layer == 0) {
1763 return name;
1764 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001765
Mathias Agopian579b3f82010-06-08 19:54:15 -07001766 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001767 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001768 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001769 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001770 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001771
1772 name = 0;
1773 do {
1774 int32_t mask = 1LU<<name;
1775 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1776 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001777 status_t err = layer->setToken(
1778 const_cast<UserClient*>(this), ctrlblk, name);
1779 if (err != NO_ERROR) {
1780 // free the name
1781 android_atomic_and(~mask, &mBitmap);
1782 name = err;
1783 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001784 break;
1785 }
1786 if (++name > 31)
1787 name = NO_MEMORY;
1788 } while(name >= 0);
1789
Mathias Agopian53503a92010-06-08 15:40:56 -07001790 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1791 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001792 return name;
1793}
1794
1795sp<ISurface> UserClient::createSurface(
1796 ISurfaceComposerClient::surface_data_t* params, int pid,
1797 const String8& name,
1798 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1799 uint32_t flags) {
1800 return 0;
1801}
1802status_t UserClient::destroySurface(SurfaceID sid) {
1803 return INVALID_OPERATION;
1804}
1805status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1806 return INVALID_OPERATION;
1807}
1808
1809// ---------------------------------------------------------------------------
1810
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811GraphicPlane::GraphicPlane()
1812 : mHw(0)
1813{
1814}
1815
1816GraphicPlane::~GraphicPlane() {
1817 delete mHw;
1818}
1819
1820bool GraphicPlane::initialized() const {
1821 return mHw ? true : false;
1822}
1823
Mathias Agopian2b92d892010-02-08 15:49:35 -08001824int GraphicPlane::getWidth() const {
1825 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826}
1827
Mathias Agopian2b92d892010-02-08 15:49:35 -08001828int GraphicPlane::getHeight() const {
1829 return mHeight;
1830}
1831
1832void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1833{
1834 mHw = hw;
1835
1836 // initialize the display orientation transform.
1837 // it's a constant that should come from the display driver.
1838 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1839 char property[PROPERTY_VALUE_MAX];
1840 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1841 //displayOrientation
1842 switch (atoi(property)) {
1843 case 90:
1844 displayOrientation = ISurfaceComposer::eOrientation90;
1845 break;
1846 case 270:
1847 displayOrientation = ISurfaceComposer::eOrientation270;
1848 break;
1849 }
1850 }
1851
1852 const float w = hw->getWidth();
1853 const float h = hw->getHeight();
1854 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1855 &mDisplayTransform);
1856 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1857 mDisplayWidth = h;
1858 mDisplayHeight = w;
1859 } else {
1860 mDisplayWidth = w;
1861 mDisplayHeight = h;
1862 }
1863
1864 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865}
1866
1867status_t GraphicPlane::orientationToTransfrom(
1868 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001869{
1870 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 switch (orientation) {
1872 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001873 flags = Transform::ROT_0;
1874 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001876 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877 break;
1878 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001879 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 break;
1881 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001882 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 break;
1884 default:
1885 return BAD_VALUE;
1886 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001887 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 return NO_ERROR;
1889}
1890
1891status_t GraphicPlane::setOrientation(int orientation)
1892{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 // If the rotation can be handled in hardware, this is where
1894 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001895
1896 const DisplayHardware& hw(displayHardware());
1897 const float w = mDisplayWidth;
1898 const float h = mDisplayHeight;
1899 mWidth = int(w);
1900 mHeight = int(h);
1901
1902 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001903 GraphicPlane::orientationToTransfrom(orientation, w, h,
1904 &orientationTransform);
1905 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1906 mWidth = int(h);
1907 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001909
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001910 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001911 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912 return NO_ERROR;
1913}
1914
1915const DisplayHardware& GraphicPlane::displayHardware() const {
1916 return *mHw;
1917}
1918
1919const Transform& GraphicPlane::transform() const {
1920 return mGlobalTransform;
1921}
1922
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001923EGLDisplay GraphicPlane::getEGLDisplay() const {
1924 return mHw->getEGLDisplay();
1925}
1926
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927// ---------------------------------------------------------------------------
1928
1929}; // namespace android