blob: e6bdfd1419ff2d150920fe4646ea6d5601a0ea26 [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"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070079 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070080 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070082 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 mDeferReleaseConsole(false),
84 mFreezeDisplay(false),
85 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070086 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mDebugBackground(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070089 mDebugDisableHWC(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070090 mDebugInSwapBuffers(0),
91 mLastSwapBufferTime(0),
92 mDebugInTransaction(0),
93 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070094 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mConsoleSignals(0),
96 mSecureFrameBuffer(0)
97{
98 init();
99}
100
101void SurfaceFlinger::init()
102{
103 LOGI("SurfaceFlinger is starting");
104
105 // debugging stuff...
106 char value[PROPERTY_VALUE_MAX];
107 property_get("debug.sf.showupdates", value, "0");
108 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 property_get("debug.sf.showbackground", value, "0");
110 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111
Mathias Agopian78fd5012010-04-20 14:51:04 -0700112 LOGI_IF(mDebugRegion, "showupdates enabled");
113 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114}
115
116SurfaceFlinger::~SurfaceFlinger()
117{
118 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
122{
123 return graphicPlane(0).displayHardware().getOverlayEngine();
124}
125
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700126sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700128 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
Mathias Agopian7e27f052010-05-28 14:22:23 -0700131sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132{
Mathias Agopian96f08192010-06-02 23:28:45 -0700133 sp<ISurfaceComposerClient> bclient;
134 sp<Client> client(new Client(this));
135 status_t err = client->initCheck();
136 if (err == NO_ERROR) {
137 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 return bclient;
140}
141
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700142sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
143{
144 sp<ISurfaceComposerClient> bclient;
145 sp<UserClient> client(new UserClient(this));
146 status_t err = client->initCheck();
147 if (err == NO_ERROR) {
148 bclient = client;
149 }
150 return bclient;
151}
152
153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
155{
156 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
157 const GraphicPlane& plane(mGraphicPlanes[dpy]);
158 return plane;
159}
160
161GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
162{
163 return const_cast<GraphicPlane&>(
164 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
165}
166
167void SurfaceFlinger::bootFinished()
168{
169 const nsecs_t now = systemTime();
170 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700171 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700172 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700173 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
176void SurfaceFlinger::onFirstRef()
177{
178 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
179
180 // Wait for the main thread to be done with its initialization
181 mReadyToRunBarrier.wait();
182}
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184static inline uint16_t pack565(int r, int g, int b) {
185 return (r<<11)|(g<<5)|b;
186}
187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188status_t SurfaceFlinger::readyToRun()
189{
190 LOGI( "SurfaceFlinger's main thread ready to run. "
191 "Initializing graphics H/W...");
192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 // we only support one display currently
194 int dpy = 0;
195
196 {
197 // initialize the main display
198 GraphicPlane& plane(graphicPlane(dpy));
199 DisplayHardware* const hw = new DisplayHardware(this, dpy);
200 plane.setDisplayHardware(hw);
201 }
202
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700203 // create the shared control-block
204 mServerHeap = new MemoryHeapBase(4096,
205 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
206 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700207
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700208 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
209 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700210
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700211 new(mServerCblk) surface_flinger_cblk_t;
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 // initialize primary screen
214 // (other display should be initialized in the same manner, but
215 // asynchronously, as they could come and go. None of this is supported
216 // yet).
217 const GraphicPlane& plane(graphicPlane(dpy));
218 const DisplayHardware& hw = plane.displayHardware();
219 const uint32_t w = hw.getWidth();
220 const uint32_t h = hw.getHeight();
221 const uint32_t f = hw.getFormat();
222 hw.makeCurrent();
223
224 // initialize the shared control block
225 mServerCblk->connected |= 1<<dpy;
226 display_cblk_t* dcblk = mServerCblk->displays + dpy;
227 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800228 dcblk->w = plane.getWidth();
229 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 dcblk->format = f;
231 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
232 dcblk->xdpi = hw.getDpiX();
233 dcblk->ydpi = hw.getDpiY();
234 dcblk->fps = hw.getRefreshRate();
235 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236
237 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700239 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 glEnableClientState(GL_VERTEX_ARRAY);
241 glEnable(GL_SCISSOR_TEST);
242 glShadeModel(GL_FLAT);
243 glDisable(GL_DITHER);
244 glDisable(GL_CULL_FACE);
245
246 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
247 const uint16_t g1 = pack565(0x17,0x2f,0x17);
248 const uint16_t textureData[4] = { g0, g1, g1, g0 };
249 glGenTextures(1, &mWormholeTexName);
250 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
251 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
252 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
253 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
254 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
255 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
256 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
257
258 glViewport(0, 0, w, h);
259 glMatrixMode(GL_PROJECTION);
260 glLoadIdentity();
261 glOrthof(0, w, h, 0, 0, 1);
262
263 LayerDim::initDimmer(this, w, h);
264
265 mReadyToRunBarrier.open();
266
267 /*
268 * We're now ready to accept clients...
269 */
270
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700271 // start boot animation
272 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 return NO_ERROR;
275}
276
277// ----------------------------------------------------------------------------
278#if 0
279#pragma mark -
280#pragma mark Events Handler
281#endif
282
283void SurfaceFlinger::waitForEvent()
284{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700285 while (true) {
286 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800287 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700288 if (UNLIKELY(isFrozen())) {
289 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700290 const nsecs_t now = systemTime();
291 if (mFreezeDisplayTime == 0) {
292 mFreezeDisplayTime = now;
293 }
294 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
295 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700296 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700297
Mathias Agopianbb641242010-05-18 17:06:55 -0700298 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800299
300 // see if we timed out
301 if (isFrozen()) {
302 const nsecs_t now = systemTime();
303 nsecs_t frozenTime = (now - mFreezeDisplayTime);
304 if (frozenTime >= freezeDisplayTimeout) {
305 // we timed out and are still frozen
306 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
307 mFreezeDisplay, mFreezeCount);
308 mFreezeDisplayTime = 0;
309 mFreezeCount = 0;
310 mFreezeDisplay = false;
311 }
312 }
313
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700314 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700315 switch (msg->what) {
316 case MessageQueue::INVALIDATE:
317 // invalidate message, just return to the main loop
318 return;
319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 }
322}
323
324void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700325 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326}
327
328void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700329 // this is the IPC call
330 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331}
332
Mathias Agopianbb641242010-05-18 17:06:55 -0700333status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
334 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335{
Mathias Agopianbb641242010-05-18 17:06:55 -0700336 return mEventQueue.postMessage(msg, reltime, flags);
337}
338
339status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
340 nsecs_t reltime, uint32_t flags)
341{
342 status_t res = mEventQueue.postMessage(msg, reltime, flags);
343 if (res == NO_ERROR) {
344 msg->wait();
345 }
346 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347}
348
349// ----------------------------------------------------------------------------
350#if 0
351#pragma mark -
352#pragma mark Main loop
353#endif
354
355bool SurfaceFlinger::threadLoop()
356{
357 waitForEvent();
358
359 // check for transactions
360 if (UNLIKELY(mConsoleSignals)) {
361 handleConsoleEvents();
362 }
363
364 if (LIKELY(mTransactionCount == 0)) {
365 // if we're in a global transaction, don't do anything.
366 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
367 uint32_t transactionFlags = getTransactionFlags(mask);
368 if (LIKELY(transactionFlags)) {
369 handleTransaction(transactionFlags);
370 }
371 }
372
373 // post surfaces (if needed)
374 handlePageFlip();
375
Mathias Agopiana350ff92010-08-10 17:14:02 -0700376 if (UNLIKELY(mHwWorkListDirty)) {
377 // build the h/w work list
378 handleWorkList();
379 }
380
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700382 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700384
385 const int index = hw.getCurrentBufferIndex();
386 GraphicLog& logger(GraphicLog::getInstance());
387
388 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 handleRepaint();
390
Mathias Agopian74faca22009-09-17 16:18:16 -0700391 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700392 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700393 hw.compositionComplete();
394
Mathias Agopian35b48d12010-09-13 22:57:58 -0700395 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700396 postFramebuffer();
397
Mathias Agopianc479e182010-09-15 12:29:18 -0700398 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400
Mathias Agopian35b48d12010-09-13 22:57:58 -0700401 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 } else {
403 // pretend we did the post
404 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405 usleep(16667); // 60 fps period
406 }
407 return true;
408}
409
410void SurfaceFlinger::postFramebuffer()
411{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 if (!mInvalidRegion.isEmpty()) {
413 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700414 const nsecs_t now = systemTime();
415 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700417 mLastSwapBufferTime = systemTime() - now;
418 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420 }
421}
422
423void SurfaceFlinger::handleConsoleEvents()
424{
425 // something to do with the console
426 const DisplayHardware& hw = graphicPlane(0).displayHardware();
427
428 int what = android_atomic_and(0, &mConsoleSignals);
429 if (what & eConsoleAcquired) {
430 hw.acquireScreen();
431 }
432
433 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700434 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 hw.releaseScreen();
437 }
438
439 if (what & eConsoleReleased) {
440 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 hw.releaseScreen();
442 } else {
443 mDeferReleaseConsole = true;
444 }
445 }
446
447 mDirtyRegion.set(hw.bounds());
448}
449
450void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
451{
Mathias Agopian3d579642009-06-04 18:46:21 -0700452 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453
Mathias Agopian4da75192010-08-10 17:19:56 -0700454 /*
455 * Perform and commit the transaction
456 */
457
Mathias Agopian3d579642009-06-04 18:46:21 -0700458 { // scope for the lock
459 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700460 const nsecs_t now = systemTime();
461 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700462 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700463 mLastTransactionTime = systemTime() - now;
464 mDebugInTransaction = 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700465 mHwWorkListDirty = true;
Mathias Agopian4da75192010-08-10 17:19:56 -0700466 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700467 }
468
Mathias Agopian4da75192010-08-10 17:19:56 -0700469 /*
470 * Clean-up all layers that went away
471 * (do this without the lock held)
472 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700473
Mathias Agopian3d579642009-06-04 18:46:21 -0700474 const size_t count = ditchedLayers.size();
475 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700476 if (ditchedLayers[i] != 0) {
477 //LOGD("ditching layer %p", ditchedLayers[i].get());
478 ditchedLayers[i]->ditch();
479 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700480 }
481}
482
483void SurfaceFlinger::handleTransactionLocked(
484 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
485{
486 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 const size_t count = currentLayers.size();
488
489 /*
490 * Traversal of the children
491 * (perform the transaction for each of them if needed)
492 */
493
494 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
495 if (layersNeedTransaction) {
496 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700497 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
499 if (!trFlags) continue;
500
501 const uint32_t flags = layer->doTransaction(0);
502 if (flags & Layer::eVisibleRegion)
503 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504 }
505 }
506
507 /*
508 * Perform our own transaction if needed
509 */
510
511 if (transactionFlags & eTransactionNeeded) {
512 if (mCurrentState.orientation != mDrawingState.orientation) {
513 // the orientation has changed, recompute all visible regions
514 // and invalidate everything.
515
516 const int dpy = 0;
517 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700518 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 GraphicPlane& plane(graphicPlane(dpy));
520 plane.setOrientation(orientation);
521
522 // update the shared control block
523 const DisplayHardware& hw(plane.displayHardware());
524 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
525 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800526 dcblk->w = plane.getWidth();
527 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528
529 mVisibleRegionsDirty = true;
530 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 }
532
533 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
534 // freezing or unfreezing the display -> trigger animation if needed
535 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800536 if (mFreezeDisplay)
537 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 }
539
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700540 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
541 // layers have been added
542 mVisibleRegionsDirty = true;
543 }
544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 // some layers might have been removed, so
546 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700548 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700550 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700551 const size_t count = previousLayers.size();
552 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700553 const sp<LayerBase>& layer(previousLayers[i]);
554 if (currentLayers.indexOf( layer ) < 0) {
555 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700556 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700557 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700558 }
559 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 }
562
563 commitTransaction();
564}
565
566sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
567{
568 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
569}
570
571void SurfaceFlinger::computeVisibleRegions(
572 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
573{
574 const GraphicPlane& plane(graphicPlane(0));
575 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700576 const DisplayHardware& hw(plane.displayHardware());
577 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578
579 Region aboveOpaqueLayers;
580 Region aboveCoveredLayers;
581 Region dirty;
582
583 bool secureFrameBuffer = false;
584
585 size_t i = currentLayers.size();
586 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700587 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 layer->validateVisibility(planeTransform);
589
590 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700591 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592
Mathias Agopianab028732010-03-16 16:41:46 -0700593 /*
594 * opaqueRegion: area of a surface that is fully opaque.
595 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700597
598 /*
599 * visibleRegion: area of a surface that is visible on screen
600 * and not fully transparent. This is essentially the layer's
601 * footprint minus the opaque regions above it.
602 * Areas covered by a translucent surface are considered visible.
603 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700605
606 /*
607 * coveredRegion: area of a surface that is covered by all
608 * visible regions above it (which includes the translucent areas).
609 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700611
612
613 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700614 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700616 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700618 visibleRegion.andSelf(screenRegion);
619 if (!visibleRegion.isEmpty()) {
620 // Remove the transparent area from the visible region
621 if (translucent) {
622 visibleRegion.subtractSelf(layer->transparentRegionScreen);
623 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624
Mathias Agopianab028732010-03-16 16:41:46 -0700625 // compute the opaque region
626 const int32_t layerOrientation = layer->getOrientation();
627 if (s.alpha==255 && !translucent &&
628 ((layerOrientation & Transform::ROT_INVALID) == false)) {
629 // the opaque region is the layer's footprint
630 opaqueRegion = visibleRegion;
631 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 }
633 }
634
Mathias Agopianab028732010-03-16 16:41:46 -0700635 // Clip the covered region to the visible region
636 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
637
638 // Update aboveCoveredLayers for next (lower) layer
639 aboveCoveredLayers.orSelf(visibleRegion);
640
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 // subtract the opaque region covered by the layers above us
642 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
644 // compute this layer's dirty region
645 if (layer->contentDirty) {
646 // we need to invalidate the whole region
647 dirty = visibleRegion;
648 // as well, as the old visible region
649 dirty.orSelf(layer->visibleRegionScreen);
650 layer->contentDirty = false;
651 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700652 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700653 * the exposed region consists of two components:
654 * 1) what's VISIBLE now and was COVERED before
655 * 2) what's EXPOSED now less what was EXPOSED before
656 *
657 * note that (1) is conservative, we start with the whole
658 * visible region but only keep what used to be covered by
659 * something -- which mean it may have been exposed.
660 *
661 * (2) handles areas that were not covered by anything but got
662 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700663 */
Mathias Agopianab028732010-03-16 16:41:46 -0700664 const Region newExposed = visibleRegion - coveredRegion;
665 const Region oldVisibleRegion = layer->visibleRegionScreen;
666 const Region oldCoveredRegion = layer->coveredRegionScreen;
667 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
668 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 }
670 dirty.subtractSelf(aboveOpaqueLayers);
671
672 // accumulate to the screen dirty region
673 dirtyRegion.orSelf(dirty);
674
Mathias Agopianab028732010-03-16 16:41:46 -0700675 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700677
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 // Store the visible region is screen space
679 layer->setVisibleRegion(visibleRegion);
680 layer->setCoveredRegion(coveredRegion);
681
Mathias Agopian97011222009-07-28 10:57:27 -0700682 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 if (layer->isSecure() && !visibleRegion.isEmpty()) {
684 secureFrameBuffer = true;
685 }
686 }
687
Mathias Agopian97011222009-07-28 10:57:27 -0700688 // invalidate the areas where a layer was removed
689 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
690 mDirtyRegionRemovedLayer.clear();
691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 mSecureFrameBuffer = secureFrameBuffer;
693 opaqueRegion = aboveOpaqueLayers;
694}
695
696
697void SurfaceFlinger::commitTransaction()
698{
699 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700700 mResizeTransationPending = false;
701 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702}
703
704void SurfaceFlinger::handlePageFlip()
705{
706 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700707 LayerVector& currentLayers(
708 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 visibleRegions |= lockPageFlip(currentLayers);
710
711 const DisplayHardware& hw = graphicPlane(0).displayHardware();
712 const Region screenRegion(hw.bounds());
713 if (visibleRegions) {
714 Region opaqueRegion;
715 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700716
717 /*
718 * rebuild the visible layer list
719 */
720 mVisibleLayersSortedByZ.clear();
721 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
722 size_t count = currentLayers.size();
723 mVisibleLayersSortedByZ.setCapacity(count);
724 for (size_t i=0 ; i<count ; i++) {
725 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
726 mVisibleLayersSortedByZ.add(currentLayers[i]);
727 }
728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729 mWormholeRegion = screenRegion.subtract(opaqueRegion);
730 mVisibleRegionsDirty = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700731 mHwWorkListDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 }
733
734 unlockPageFlip(currentLayers);
735 mDirtyRegion.andSelf(screenRegion);
736}
737
738bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
739{
740 bool recomputeVisibleRegions = false;
741 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700742 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700744 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 layer->lockPageFlip(recomputeVisibleRegions);
746 }
747 return recomputeVisibleRegions;
748}
749
750void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
751{
752 const GraphicPlane& plane(graphicPlane(0));
753 const Transform& planeTransform(plane.transform());
754 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700755 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700757 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 layer->unlockPageFlip(planeTransform, mDirtyRegion);
759 }
760}
761
Mathias Agopiana350ff92010-08-10 17:14:02 -0700762void SurfaceFlinger::handleWorkList()
763{
764 mHwWorkListDirty = false;
765 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
766 if (hwc.initCheck() == NO_ERROR) {
767 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
768 const size_t count = currentLayers.size();
769 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700770 hwc_layer_t* const cur(hwc.getLayers());
771 for (size_t i=0 ; cur && i<count ; i++) {
772 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700773 if (mDebugDisableHWC) {
774 cur[i].compositionType = HWC_FRAMEBUFFER;
775 cur[i].flags |= HWC_SKIP_LAYER;
776 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700777 }
778 }
779}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700780
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781void SurfaceFlinger::handleRepaint()
782{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700783 // compute the invalid region
784 mInvalidRegion.orSelf(mDirtyRegion);
785 if (mInvalidRegion.isEmpty()) {
786 // nothing to do
787 return;
788 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789
790 if (UNLIKELY(mDebugRegion)) {
791 debugFlashRegions();
792 }
793
Mathias Agopianb8a55602009-06-26 19:06:36 -0700794 // set the frame buffer
795 const DisplayHardware& hw(graphicPlane(0).displayHardware());
796 glMatrixMode(GL_MODELVIEW);
797 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798
799 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700800 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
801 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700802 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700803 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
804 // takes a rectangle, we must make sure to update that whole
805 // rectangle in that case
806 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700807 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700808 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 mDirtyRegion.set(mInvalidRegion.bounds());
810 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700811 // in the BUFFER_PRESERVED case, obviously, we can update only
812 // what's needed and nothing more.
813 // NOTE: this is NOT a common case, as preserving the backbuffer
814 // is costly and usually involves copying the whole update back.
815 }
816 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700817 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700818 // We need to redraw the rectangle that will be updated
819 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700820 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700821 // rectangle instead of a region (see DisplayHardware::flip())
822 mDirtyRegion.set(mInvalidRegion.bounds());
823 } else {
824 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 mDirtyRegion.set(hw.bounds());
826 mInvalidRegion = mDirtyRegion;
827 }
828 }
829
830 // compose all surfaces
831 composeSurfaces(mDirtyRegion);
832
833 // clear the dirty regions
834 mDirtyRegion.clear();
835}
836
837void SurfaceFlinger::composeSurfaces(const Region& dirty)
838{
839 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
840 // should never happen unless the window manager has a bug
841 // draw something...
842 drawWormhole();
843 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700844
845 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700846 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700847 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700848
849 const DisplayHardware& hw(graphicPlane(0).displayHardware());
850 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700851 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700852
Mathias Agopian45721772010-08-12 15:03:26 -0700853 LOGE_IF(cur && hwc.getNumLayers() != count,
854 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
855 hwc.getNumLayers(), count);
856
857 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700858 if (hwc.initCheck() == NO_ERROR) {
859 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
860 }
Mathias Agopian45721772010-08-12 15:03:26 -0700861
862 /*
863 * update the per-frame h/w composer data for each layer
864 * and build the transparent region of the FB
865 */
866 Region transparent;
867 if (cur) {
868 for (size_t i=0 ; i<count ; i++) {
869 const sp<LayerBase>& layer(layers[i]);
870 layer->setPerFrameData(&cur[i]);
871 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
872 if (!(layer->needsBlending())) {
873 transparent.orSelf(layer->visibleRegionScreen);
874 }
875 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700876 }
877 err = hwc.prepare();
878 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
879 }
880
Mathias Agopian45721772010-08-12 15:03:26 -0700881 /*
882 * clear the area of the FB that need to be transparent
883 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700884 transparent.andSelf(dirty);
885 if (!transparent.isEmpty()) {
886 glClearColor(0,0,0,0);
887 Region::const_iterator it = transparent.begin();
888 Region::const_iterator const end = transparent.end();
889 const int32_t height = hw.getHeight();
890 while (it != end) {
891 const Rect& r(*it++);
892 const GLint sy = height - (r.top + r.height());
893 glScissor(r.left, sy, r.width(), r.height());
894 glClear(GL_COLOR_BUFFER_BIT);
895 }
896 }
Mathias Agopian45721772010-08-12 15:03:26 -0700897
898
899 /*
900 * and then, render the layers targeted at the framebuffer
901 */
902 for (size_t i=0 ; i<count ; i++) {
903 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700904 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
905 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700906 // skip layers handled by the HAL
907 continue;
908 }
909 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700910
Mathias Agopian45721772010-08-12 15:03:26 -0700911 const sp<LayerBase>& layer(layers[i]);
912 const Region clip(dirty.intersect(layer->visibleRegionScreen));
913 if (!clip.isEmpty()) {
914 layer->draw(clip);
915 }
916 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917}
918
919void SurfaceFlinger::unlockClients()
920{
921 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
922 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700923 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700925 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 layer->finishPageFlip();
927 }
928}
929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930void SurfaceFlinger::debugFlashRegions()
931{
Mathias Agopian0a917752010-06-14 21:20:00 -0700932 const DisplayHardware& hw(graphicPlane(0).displayHardware());
933 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700934
Mathias Agopian0a917752010-06-14 21:20:00 -0700935 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
936 (flags & DisplayHardware::BUFFER_PRESERVED))) {
937 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
938 mDirtyRegion.bounds() : hw.bounds());
939 composeSurfaces(repaint);
940 }
941
942 TextureManager::deactivateTextures();
943
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 glDisable(GL_BLEND);
945 glDisable(GL_DITHER);
946 glDisable(GL_SCISSOR_TEST);
947
Mathias Agopian0926f502009-05-04 14:17:04 -0700948 static int toggle = 0;
949 toggle = 1 - toggle;
950 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700951 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700952 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700953 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700954 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955
Mathias Agopian20f68782009-05-11 00:03:41 -0700956 Region::const_iterator it = mDirtyRegion.begin();
957 Region::const_iterator const end = mDirtyRegion.end();
958 while (it != end) {
959 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 GLfloat vertices[][2] = {
961 { r.left, r.top },
962 { r.left, r.bottom },
963 { r.right, r.bottom },
964 { r.right, r.top }
965 };
966 glVertexPointer(2, GL_FLOAT, 0, vertices);
967 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
968 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700969
Mathias Agopianb8a55602009-06-26 19:06:36 -0700970 if (mInvalidRegion.isEmpty()) {
971 mDirtyRegion.dump("mDirtyRegion");
972 mInvalidRegion.dump("mInvalidRegion");
973 }
974 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975
976 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700977 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978
979 glEnable(GL_SCISSOR_TEST);
980 //mDirtyRegion.dump("mDirtyRegion");
981}
982
983void SurfaceFlinger::drawWormhole() const
984{
985 const Region region(mWormholeRegion.intersect(mDirtyRegion));
986 if (region.isEmpty())
987 return;
988
989 const DisplayHardware& hw(graphicPlane(0).displayHardware());
990 const int32_t width = hw.getWidth();
991 const int32_t height = hw.getHeight();
992
993 glDisable(GL_BLEND);
994 glDisable(GL_DITHER);
995
996 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700997 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700998 Region::const_iterator it = region.begin();
999 Region::const_iterator const end = region.end();
1000 while (it != end) {
1001 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 const GLint sy = height - (r.top + r.height());
1003 glScissor(r.left, sy, r.width(), r.height());
1004 glClear(GL_COLOR_BUFFER_BIT);
1005 }
1006 } else {
1007 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1008 { width, height }, { 0, height } };
1009 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1010 glVertexPointer(2, GL_SHORT, 0, vertices);
1011 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1012 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001013#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001014 if (GLExtensions::getInstance().haveTextureExternal()) {
1015 glDisable(GL_TEXTURE_EXTERNAL_OES);
1016 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001017#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 glEnable(GL_TEXTURE_2D);
1019 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1020 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1021 glMatrixMode(GL_TEXTURE);
1022 glLoadIdentity();
1023 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001024 Region::const_iterator it = region.begin();
1025 Region::const_iterator const end = region.end();
1026 while (it != end) {
1027 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 const GLint sy = height - (r.top + r.height());
1029 glScissor(r.left, sy, r.width(), r.height());
1030 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1031 }
1032 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1033 }
1034}
1035
1036void SurfaceFlinger::debugShowFPS() const
1037{
1038 static int mFrameCount;
1039 static int mLastFrameCount = 0;
1040 static nsecs_t mLastFpsTime = 0;
1041 static float mFps = 0;
1042 mFrameCount++;
1043 nsecs_t now = systemTime();
1044 nsecs_t diff = now - mLastFpsTime;
1045 if (diff > ms2ns(250)) {
1046 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1047 mLastFpsTime = now;
1048 mLastFrameCount = mFrameCount;
1049 }
1050 // XXX: mFPS has the value we want
1051 }
1052
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001053status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054{
1055 Mutex::Autolock _l(mStateLock);
1056 addLayer_l(layer);
1057 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1058 return NO_ERROR;
1059}
1060
Mathias Agopian96f08192010-06-02 23:28:45 -07001061status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1062{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001063 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001064 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1065}
1066
1067ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1068 const sp<LayerBaseClient>& lbc)
1069{
1070 Mutex::Autolock _l(mStateLock);
1071
1072 // attach this layer to the client
1073 ssize_t name = client->attachLayer(lbc);
1074
1075 // add this layer to the current state list
1076 addLayer_l(lbc);
1077
1078 return name;
1079}
1080
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001081status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082{
1083 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001084 status_t err = purgatorizeLayer_l(layer);
1085 if (err == NO_ERROR)
1086 setTransactionFlags(eTransactionNeeded);
1087 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088}
1089
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001090status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001092 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1093 if (lbc != 0) {
1094 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1095 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1097 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001098 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 return NO_ERROR;
1100 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001101 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102}
1103
Mathias Agopian9a112062009-04-17 19:36:26 -07001104status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1105{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001106 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001107 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001108
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001109 layerBase->onRemoved();
1110
Mathias Agopian3d579642009-06-04 18:46:21 -07001111 // it's possible that we don't find a layer, because it might
1112 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001113 // from the user because there is a race between Client::destroySurface(),
1114 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001115 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1116}
1117
Mathias Agopian96f08192010-06-02 23:28:45 -07001118status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119{
Mathias Agopian96f08192010-06-02 23:28:45 -07001120 layer->forceVisibilityTransaction();
1121 setTransactionFlags(eTraversalNeeded);
1122 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123}
1124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1126{
1127 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1128}
1129
Mathias Agopianbb641242010-05-18 17:06:55 -07001130uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131{
1132 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1133 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001134 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 }
1136 return old;
1137}
1138
1139void SurfaceFlinger::openGlobalTransaction()
1140{
1141 android_atomic_inc(&mTransactionCount);
1142}
1143
1144void SurfaceFlinger::closeGlobalTransaction()
1145{
1146 if (android_atomic_dec(&mTransactionCount) == 1) {
1147 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001148
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001149 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001150 // take effect before returning.
1151 Mutex::Autolock _l(mStateLock);
1152 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001153 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1154 if (CC_UNLIKELY(err != NO_ERROR)) {
1155 // just in case something goes wrong in SF, return to the
1156 // called after a few seconds.
1157 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1158 mResizeTransationPending = false;
1159 break;
1160 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 }
1163}
1164
1165status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1166{
1167 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1168 return BAD_VALUE;
1169
1170 Mutex::Autolock _l(mStateLock);
1171 mCurrentState.freezeDisplay = 1;
1172 setTransactionFlags(eTransactionNeeded);
1173
1174 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001175 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 return NO_ERROR;
1177}
1178
1179status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1180{
1181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1182 return BAD_VALUE;
1183
1184 Mutex::Autolock _l(mStateLock);
1185 mCurrentState.freezeDisplay = 0;
1186 setTransactionFlags(eTransactionNeeded);
1187
1188 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001189 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 return NO_ERROR;
1191}
1192
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001193int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001194 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195{
1196 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1197 return BAD_VALUE;
1198
1199 Mutex::Autolock _l(mStateLock);
1200 if (mCurrentState.orientation != orientation) {
1201 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001202 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 mCurrentState.orientation = orientation;
1204 setTransactionFlags(eTransactionNeeded);
1205 mTransactionCV.wait(mStateLock);
1206 } else {
1207 orientation = BAD_VALUE;
1208 }
1209 }
1210 return orientation;
1211}
1212
Mathias Agopian96f08192010-06-02 23:28:45 -07001213sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001214 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1216 uint32_t flags)
1217{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001218 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001220
1221 if (int32_t(w|h) < 0) {
1222 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1223 int(w), int(h));
1224 return surfaceHandle;
1225 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001228 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 switch (flags & eFXSurfaceMask) {
1230 case eFXSurfaceNormal:
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001231#if HAS_PUSH_BUFFERS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001233 layer = createPushBuffersSurface(client, d, w, h, flags);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001234 } else
1235#endif
1236 {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001237 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1238 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 }
1240 break;
1241 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001242 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 break;
1244 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001245 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 break;
1247 }
1248
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001249 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001250 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001251 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001252 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001255 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001256 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001257 params->identity = surfaceHandle->getIdentity();
1258 params->width = w;
1259 params->height = h;
1260 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001261 if (normalLayer != 0) {
1262 Mutex::Autolock _l(mStateLock);
1263 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1264 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001265 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001266
Mathias Agopian96f08192010-06-02 23:28:45 -07001267 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 }
1269
1270 return surfaceHandle;
1271}
1272
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001273sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001274 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001275 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001276 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277{
1278 // initialize the surfaces
1279 switch (format) { // TODO: take h/w into account
1280 case PIXEL_FORMAT_TRANSPARENT:
1281 case PIXEL_FORMAT_TRANSLUCENT:
1282 format = PIXEL_FORMAT_RGBA_8888;
1283 break;
1284 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001285#ifdef NO_RGBX_8888
1286 format = PIXEL_FORMAT_RGB_565;
1287#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001288 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001289#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 break;
1291 }
1292
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001293#ifdef NO_RGBX_8888
1294 if (format == PIXEL_FORMAT_RGBX_8888)
1295 format = PIXEL_FORMAT_RGBA_8888;
1296#endif
1297
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001299 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001300 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001302 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 }
1304 return layer;
1305}
1306
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001307sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001308 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001309 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310{
Mathias Agopian96f08192010-06-02 23:28:45 -07001311 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 return layer;
1314}
1315
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001316sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001317 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319{
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 return layer;
1323}
1324
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001325sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001326 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001327 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
Mathias Agopian96f08192010-06-02 23:28:45 -07001329 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 return layer;
1332}
1333
Mathias Agopian96f08192010-06-02 23:28:45 -07001334status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335{
Mathias Agopian9a112062009-04-17 19:36:26 -07001336 /*
1337 * called by the window manager, when a surface should be marked for
1338 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001339 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 * The surface is removed from the current and drawing lists, but placed
1341 * in the purgatory queue, so it's not destroyed right-away (we need
1342 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001343 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001344
Mathias Agopian48d819a2009-09-10 19:41:18 -07001345 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001346 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001347 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001348 if (layer != 0) {
1349 err = purgatorizeLayer_l(layer);
1350 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001351 setTransactionFlags(eTransactionNeeded);
1352 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001353 }
1354 return err;
1355}
1356
1357status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1358{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001359 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001360
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001361 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001362 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001363 sp<LayerBaseClient> layer;
1364 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001365 MessageDestroySurface(
1366 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1367 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001368 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001369 sp<LayerBaseClient> l(layer);
1370 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001371 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001372 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001373 * remove the layer from the current list -- chances are that it's
1374 * not in the list anyway, because it should have been removed
1375 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001376 * However, a buggy client could have not done that.
1377 * Since we know we don't have any more clients, we don't need
1378 * to use the purgatory.
1379 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001380 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001381 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1382 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001383 return true;
1384 }
1385 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001386
Mathias Agopianbb641242010-05-18 17:06:55 -07001387 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 return NO_ERROR;
1389}
1390
1391status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001392 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 int32_t count,
1394 const layer_state_t* states)
1395{
1396 Mutex::Autolock _l(mStateLock);
1397 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001399 const layer_state_t& s(states[i]);
1400 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001401 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403 if (what & ePositionChanged) {
1404 if (layer->setPosition(s.x, s.y))
1405 flags |= eTraversalNeeded;
1406 }
1407 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001408 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001410 mCurrentState.layersSortedByZ.removeAt(idx);
1411 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 // we need traversal (state changed)
1413 // AND transaction (list changed)
1414 flags |= eTransactionNeeded|eTraversalNeeded;
1415 }
1416 }
1417 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001418 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001420 mResizeTransationPending = true;
1421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 }
1423 if (what & eAlphaChanged) {
1424 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1425 flags |= eTraversalNeeded;
1426 }
1427 if (what & eMatrixChanged) {
1428 if (layer->setMatrix(s.matrix))
1429 flags |= eTraversalNeeded;
1430 }
1431 if (what & eTransparentRegionChanged) {
1432 if (layer->setTransparentRegionHint(s.transparentRegion))
1433 flags |= eTraversalNeeded;
1434 }
1435 if (what & eVisibilityChanged) {
1436 if (layer->setFlags(s.flags, s.mask))
1437 flags |= eTraversalNeeded;
1438 }
1439 }
1440 }
1441 if (flags) {
1442 setTransactionFlags(flags);
1443 }
1444 return NO_ERROR;
1445}
1446
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447void SurfaceFlinger::screenReleased(int dpy)
1448{
1449 // this may be called by a signal handler, we can't do too much in here
1450 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1451 signalEvent();
1452}
1453
1454void SurfaceFlinger::screenAcquired(int dpy)
1455{
1456 // this may be called by a signal handler, we can't do too much in here
1457 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1458 signalEvent();
1459}
1460
1461status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1462{
1463 const size_t SIZE = 1024;
1464 char buffer[SIZE];
1465 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001466 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 snprintf(buffer, SIZE, "Permission Denial: "
1468 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1469 IPCThreadState::self()->getCallingPid(),
1470 IPCThreadState::self()->getCallingUid());
1471 result.append(buffer);
1472 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001473
1474 // figure out if we're stuck somewhere
1475 const nsecs_t now = systemTime();
1476 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1477 const nsecs_t inTransaction(mDebugInTransaction);
1478 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1479 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1480
1481 // Try to get the main lock, but don't insist if we can't
1482 // (this would indicate SF is stuck, but we want to be able to
1483 // print something in dumpsys).
1484 int retry = 3;
1485 while (mStateLock.tryLock()<0 && --retry>=0) {
1486 usleep(1000000);
1487 }
1488 const bool locked(retry >= 0);
1489 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001490 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001491 "SurfaceFlinger appears to be unresponsive, "
1492 "dumping anyways (no locks held)\n");
1493 result.append(buffer);
1494 }
1495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1497 const size_t count = currentLayers.size();
1498 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001499 const sp<LayerBase>& layer(currentLayers[i]);
1500 layer->dump(result, buffer, SIZE);
1501 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 s.transparentRegion.dump(result, "transparentRegion");
1503 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1504 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1505 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 mWormholeRegion.dump(result, "WormholeRegion");
1508 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1509 snprintf(buffer, SIZE,
1510 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1511 mFreezeDisplay?"yes":"no", mFreezeCount,
1512 mCurrentState.orientation, hw.canDraw());
1513 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001514 snprintf(buffer, SIZE,
1515 " last eglSwapBuffers() time: %f us\n"
1516 " last transaction time : %f us\n",
1517 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1518 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001519
Mathias Agopian9795c422009-08-26 16:36:26 -07001520 if (inSwapBuffersDuration || !locked) {
1521 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1522 inSwapBuffersDuration/1000.0);
1523 result.append(buffer);
1524 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001525
Mathias Agopian9795c422009-08-26 16:36:26 -07001526 if (inTransactionDuration || !locked) {
1527 snprintf(buffer, SIZE, " transaction time: %f us\n",
1528 inTransactionDuration/1000.0);
1529 result.append(buffer);
1530 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001531
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001532 HWComposer& hwc(hw.getHwComposer());
1533 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1534 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1535 mDebugDisableHWC ? "disabled" : "enabled");
1536 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001537 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001538
Mathias Agopian3330b202009-10-05 17:07:12 -07001539 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001540 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001541
1542 if (locked) {
1543 mStateLock.unlock();
1544 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 }
1546 write(fd, result.string(), result.size());
1547 return NO_ERROR;
1548}
1549
1550status_t SurfaceFlinger::onTransact(
1551 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1552{
1553 switch (code) {
1554 case CREATE_CONNECTION:
1555 case OPEN_GLOBAL_TRANSACTION:
1556 case CLOSE_GLOBAL_TRANSACTION:
1557 case SET_ORIENTATION:
1558 case FREEZE_DISPLAY:
1559 case UNFREEZE_DISPLAY:
1560 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 {
1562 // codes that require permission check
1563 IPCThreadState* ipc = IPCThreadState::self();
1564 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001565 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001566 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1567 LOGE("Permission Denial: "
1568 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1569 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001571 break;
1572 }
1573 case CAPTURE_SCREEN:
1574 {
1575 // codes that require permission check
1576 IPCThreadState* ipc = IPCThreadState::self();
1577 const int pid = ipc->getCallingPid();
1578 const int uid = ipc->getCallingUid();
1579 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1580 LOGE("Permission Denial: "
1581 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1582 return PERMISSION_DENIED;
1583 }
1584 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585 }
1586 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001587
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1589 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001590 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001591 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1592 IPCThreadState* ipc = IPCThreadState::self();
1593 const int pid = ipc->getCallingPid();
1594 const int uid = ipc->getCallingUid();
1595 LOGE("Permission Denial: "
1596 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 return PERMISSION_DENIED;
1598 }
1599 int n;
1600 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001601 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001602 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 return NO_ERROR;
1604 case 1002: // SHOW_UPDATES
1605 n = data.readInt32();
1606 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1607 return NO_ERROR;
1608 case 1003: // SHOW_BACKGROUND
1609 n = data.readInt32();
1610 mDebugBackground = n ? 1 : 0;
1611 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001612 case 1008: // toggle use of hw composer
1613 n = data.readInt32();
1614 mDebugDisableHWC = n ? 1 : 0;
1615 mHwWorkListDirty = true;
1616 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 case 1004:{ // repaint everything
1618 Mutex::Autolock _l(mStateLock);
1619 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1620 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1621 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001622 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001624 case 1005:{ // force transaction
1625 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1626 return NO_ERROR;
1627 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001628 case 1006:{ // enable/disable GraphicLog
1629 int enabled = data.readInt32();
1630 GraphicLog::getInstance().setEnabled(enabled);
1631 return NO_ERROR;
1632 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 case 1007: // set mFreezeCount
1634 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001635 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 return NO_ERROR;
1637 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001638 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 reply->writeInt32(0);
1640 reply->writeInt32(mDebugRegion);
1641 reply->writeInt32(mDebugBackground);
1642 return NO_ERROR;
1643 case 1013: {
1644 Mutex::Autolock _l(mStateLock);
1645 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1646 reply->writeInt32(hw.getPageFlipCount());
1647 }
1648 return NO_ERROR;
1649 }
1650 }
1651 return err;
1652}
1653
1654// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001655
Mathias Agopian74c40c02010-09-29 13:02:36 -07001656status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
1657 sp<IMemoryHeap>* heap,
1658 uint32_t* w, uint32_t* h, PixelFormat* f,
1659 uint32_t sw, uint32_t sh)
1660{
1661 status_t result = PERMISSION_DENIED;
1662
1663 // only one display supported for now
1664 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1665 return BAD_VALUE;
1666
1667 if (!GLExtensions::getInstance().haveFramebufferObject())
1668 return INVALID_OPERATION;
1669
1670 // get screen geometry
1671 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
1672 const uint32_t hw_w = hw.getWidth();
1673 const uint32_t hw_h = hw.getHeight();
1674
1675 if ((sw > hw_w) || (sh > hw_h))
1676 return BAD_VALUE;
1677
1678 sw = (!sw) ? hw_w : sw;
1679 sh = (!sh) ? hw_h : sh;
1680 const size_t size = sw * sh * 4;
1681
1682 // make sure to clear all GL error flags
1683 while ( glGetError() != GL_NO_ERROR ) ;
1684
1685 // create a FBO
1686 GLuint name, tname;
1687 glGenRenderbuffersOES(1, &tname);
1688 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
1689 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
1690 glGenFramebuffersOES(1, &name);
1691 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
1692 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
1693 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
1694
1695 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
1696 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
1697
1698 // invert everything, b/c glReadPixel() below will invert the FB
1699 glViewport(0, 0, sw, sh);
1700 glMatrixMode(GL_PROJECTION);
1701 glPushMatrix();
1702 glLoadIdentity();
1703 glOrthof(0, hw_w, 0, hw_h, 0, 1);
1704 glMatrixMode(GL_MODELVIEW);
1705
1706 // redraw the screen entirely...
1707 glClearColor(0,0,0,1);
1708 glClear(GL_COLOR_BUFFER_BIT);
1709 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1710 const size_t count = layers.size();
1711 for (size_t i=0 ; i<count ; ++i) {
1712 const sp<LayerBase>& layer(layers[i]);
1713 layer->drawForSreenShot();
1714 }
1715
1716 // XXX: this is needed on tegra
1717 glScissor(0, 0, sw, sh);
1718
1719 // check for errors and return screen capture
1720 if (glGetError() != GL_NO_ERROR) {
1721 // error while rendering
1722 result = INVALID_OPERATION;
1723 } else {
1724 // allocate shared memory large enough to hold the
1725 // screen capture
1726 sp<MemoryHeapBase> base(
1727 new MemoryHeapBase(size, 0, "screen-capture") );
1728 void* const ptr = base->getBase();
1729 if (ptr) {
1730 // capture the screen with glReadPixels()
1731 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
1732 if (glGetError() == GL_NO_ERROR) {
1733 *heap = base;
1734 *w = sw;
1735 *h = sh;
1736 *f = PIXEL_FORMAT_RGBA_8888;
1737 result = NO_ERROR;
1738 }
1739 } else {
1740 result = NO_MEMORY;
1741 }
1742 }
1743
1744 glEnable(GL_SCISSOR_TEST);
1745 glViewport(0, 0, hw_w, hw_h);
1746 glMatrixMode(GL_PROJECTION);
1747 glPopMatrix();
1748 glMatrixMode(GL_MODELVIEW);
1749
1750
1751 } else {
1752 result = BAD_VALUE;
1753 }
1754
1755 // release FBO resources
1756 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1757 glDeleteRenderbuffersOES(1, &tname);
1758 glDeleteFramebuffersOES(1, &name);
1759 return result;
1760}
1761
1762
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001763status_t SurfaceFlinger::captureScreen(DisplayID dpy,
1764 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07001765 uint32_t* width, uint32_t* height, PixelFormat* format,
1766 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001767{
1768 // only one display supported for now
1769 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1770 return BAD_VALUE;
1771
1772 if (!GLExtensions::getInstance().haveFramebufferObject())
1773 return INVALID_OPERATION;
1774
1775 class MessageCaptureScreen : public MessageBase {
1776 SurfaceFlinger* flinger;
1777 DisplayID dpy;
1778 sp<IMemoryHeap>* heap;
1779 uint32_t* w;
1780 uint32_t* h;
1781 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001782 uint32_t sw;
1783 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001784 status_t result;
1785 public:
1786 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07001787 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
1788 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001789 : flinger(flinger), dpy(dpy),
Mathias Agopian74c40c02010-09-29 13:02:36 -07001790 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001791 {
1792 }
1793 status_t getResult() const {
1794 return result;
1795 }
1796 virtual bool handler() {
1797 Mutex::Autolock _l(flinger->mStateLock);
1798
1799 // if we have secure windows, never allow the screen capture
1800 if (flinger->mSecureFrameBuffer)
1801 return true;
1802
Mathias Agopian74c40c02010-09-29 13:02:36 -07001803 result = flinger->captureScreenImplLocked(dpy,
1804 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001805
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001806 return true;
1807 }
1808 };
1809
1810 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian74c40c02010-09-29 13:02:36 -07001811 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001812 status_t res = postMessageSync(msg);
1813 if (res == NO_ERROR) {
1814 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
1815 }
1816 return res;
1817}
1818
1819// ---------------------------------------------------------------------------
1820
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001821sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1822{
1823 sp<Layer> result;
1824 Mutex::Autolock _l(mStateLock);
1825 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1826 return result;
1827}
1828
1829// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001830
Mathias Agopian96f08192010-06-02 23:28:45 -07001831Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001832 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834}
1835
Mathias Agopian96f08192010-06-02 23:28:45 -07001836Client::~Client()
1837{
Mathias Agopian96f08192010-06-02 23:28:45 -07001838 const size_t count = mLayers.size();
1839 for (size_t i=0 ; i<count ; i++) {
1840 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1841 if (layer != 0) {
1842 mFlinger->removeLayer(layer);
1843 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001844 }
1845}
1846
Mathias Agopian96f08192010-06-02 23:28:45 -07001847status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001848 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001850
Mathias Agopian96f08192010-06-02 23:28:45 -07001851ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1852{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001853 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001854 mLayers.add(name, layer);
1855 return name;
1856}
1857
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001858void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001859{
Mathias Agopian96f08192010-06-02 23:28:45 -07001860 // we do a linear search here, because this doesn't happen often
1861 const size_t count = mLayers.size();
1862 for (size_t i=0 ; i<count ; i++) {
1863 if (mLayers.valueAt(i) == layer) {
1864 mLayers.removeItemsAt(i, 1);
1865 break;
1866 }
1867 }
1868}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001869sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1870 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001871 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1872 if (layer != 0) {
1873 lbc = layer.promote();
1874 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001875 }
1876 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877}
1878
Mathias Agopian96f08192010-06-02 23:28:45 -07001879sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001880 return 0;
1881}
1882ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1883 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884}
Mathias Agopian96f08192010-06-02 23:28:45 -07001885sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001886 ISurfaceComposerClient::surface_data_t* params, int pid,
1887 const String8& name,
1888 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889 uint32_t flags)
1890{
Mathias Agopian96f08192010-06-02 23:28:45 -07001891 return mFlinger->createSurface(this, pid, name, params,
1892 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893}
Mathias Agopian96f08192010-06-02 23:28:45 -07001894status_t Client::destroySurface(SurfaceID sid) {
1895 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896}
Mathias Agopian96f08192010-06-02 23:28:45 -07001897status_t Client::setState(int32_t count, const layer_state_t* states) {
1898 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899}
1900
1901// ---------------------------------------------------------------------------
1902
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001903UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1904 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1905{
1906 const int pgsize = getpagesize();
1907 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1908
1909 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1910 "SurfaceFlinger Client control-block");
1911
1912 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1913 if (ctrlblk) { // construct the shared structure in-place.
1914 new(ctrlblk) SharedClient;
1915 }
1916}
1917
1918UserClient::~UserClient()
1919{
1920 if (ctrlblk) {
1921 ctrlblk->~SharedClient(); // destroy our shared-structure.
1922 }
1923
1924 /*
1925 * When a UserClient dies, it's unclear what to do exactly.
1926 * We could go ahead and destroy all surfaces linked to that client
1927 * however, it wouldn't be fair to the main Client
1928 * (usually the the window-manager), which might want to re-target
1929 * the layer to another UserClient.
1930 * I think the best is to do nothing, or not much; in most cases the
1931 * WM itself will go ahead and clean things up when it detects a client of
1932 * his has died.
1933 * The remaining question is what to display? currently we keep
1934 * just keep the current buffer.
1935 */
1936}
1937
1938status_t UserClient::initCheck() const {
1939 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1940}
1941
1942void UserClient::detachLayer(const Layer* layer)
1943{
1944 int32_t name = layer->getToken();
1945 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001946 int32_t mask = 1LU<<name;
1947 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1948 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1949 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001950 }
1951}
1952
1953sp<IMemoryHeap> UserClient::getControlBlock() const {
1954 return mCblkHeap;
1955}
1956
1957ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1958{
1959 int32_t name = NAME_NOT_FOUND;
1960 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001961 if (layer == 0) {
1962 return name;
1963 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001964
Mathias Agopian579b3f82010-06-08 19:54:15 -07001965 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001966 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001967 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001968 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07001969 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001970
1971 name = 0;
1972 do {
1973 int32_t mask = 1LU<<name;
1974 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1975 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001976 status_t err = layer->setToken(
1977 const_cast<UserClient*>(this), ctrlblk, name);
1978 if (err != NO_ERROR) {
1979 // free the name
1980 android_atomic_and(~mask, &mBitmap);
1981 name = err;
1982 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001983 break;
1984 }
1985 if (++name > 31)
1986 name = NO_MEMORY;
1987 } while(name >= 0);
1988
Mathias Agopian53503a92010-06-08 15:40:56 -07001989 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1990 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001991 return name;
1992}
1993
1994sp<ISurface> UserClient::createSurface(
1995 ISurfaceComposerClient::surface_data_t* params, int pid,
1996 const String8& name,
1997 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1998 uint32_t flags) {
1999 return 0;
2000}
2001status_t UserClient::destroySurface(SurfaceID sid) {
2002 return INVALID_OPERATION;
2003}
2004status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2005 return INVALID_OPERATION;
2006}
2007
2008// ---------------------------------------------------------------------------
2009
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002010GraphicPlane::GraphicPlane()
2011 : mHw(0)
2012{
2013}
2014
2015GraphicPlane::~GraphicPlane() {
2016 delete mHw;
2017}
2018
2019bool GraphicPlane::initialized() const {
2020 return mHw ? true : false;
2021}
2022
Mathias Agopian2b92d892010-02-08 15:49:35 -08002023int GraphicPlane::getWidth() const {
2024 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025}
2026
Mathias Agopian2b92d892010-02-08 15:49:35 -08002027int GraphicPlane::getHeight() const {
2028 return mHeight;
2029}
2030
2031void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2032{
2033 mHw = hw;
2034
2035 // initialize the display orientation transform.
2036 // it's a constant that should come from the display driver.
2037 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2038 char property[PROPERTY_VALUE_MAX];
2039 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2040 //displayOrientation
2041 switch (atoi(property)) {
2042 case 90:
2043 displayOrientation = ISurfaceComposer::eOrientation90;
2044 break;
2045 case 270:
2046 displayOrientation = ISurfaceComposer::eOrientation270;
2047 break;
2048 }
2049 }
2050
2051 const float w = hw->getWidth();
2052 const float h = hw->getHeight();
2053 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2054 &mDisplayTransform);
2055 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2056 mDisplayWidth = h;
2057 mDisplayHeight = w;
2058 } else {
2059 mDisplayWidth = w;
2060 mDisplayHeight = h;
2061 }
2062
2063 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002064}
2065
2066status_t GraphicPlane::orientationToTransfrom(
2067 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002068{
2069 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002070 switch (orientation) {
2071 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002072 flags = Transform::ROT_0;
2073 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002075 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002076 break;
2077 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002078 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079 break;
2080 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002081 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082 break;
2083 default:
2084 return BAD_VALUE;
2085 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002086 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002087 return NO_ERROR;
2088}
2089
2090status_t GraphicPlane::setOrientation(int orientation)
2091{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002092 // If the rotation can be handled in hardware, this is where
2093 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002094
2095 const DisplayHardware& hw(displayHardware());
2096 const float w = mDisplayWidth;
2097 const float h = mDisplayHeight;
2098 mWidth = int(w);
2099 mHeight = int(h);
2100
2101 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002102 GraphicPlane::orientationToTransfrom(orientation, w, h,
2103 &orientationTransform);
2104 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2105 mWidth = int(h);
2106 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002108
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002109 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002110 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111 return NO_ERROR;
2112}
2113
2114const DisplayHardware& GraphicPlane::displayHardware() const {
2115 return *mHw;
2116}
2117
2118const Transform& GraphicPlane::transform() const {
2119 return mGlobalTransform;
2120}
2121
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002122EGLDisplay GraphicPlane::getEGLDisplay() const {
2123 return mHw->getEGLDisplay();
2124}
2125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002126// ---------------------------------------------------------------------------
2127
2128}; // namespace android