blob: 9d32547275fcb51b15641dd9a54eb49cd2d49bae [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-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 Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian6950e422009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070054#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Mathias Agopian627e7b52009-05-21 19:21:59 -070056/* ideally AID_GRAPHICS would be in a semi-public header
57 * or there would be a way to map a user/group name to its id
58 */
59#ifndef AID_GRAPHICS
60#define AID_GRAPHICS 1003
61#endif
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063#define DISPLAY_COUNT 1
64
65namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066// ---------------------------------------------------------------------------
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068SurfaceFlinger::SurfaceFlinger()
69 : BnSurfaceComposer(), Thread(false),
70 mTransactionFlags(0),
71 mTransactionCount(0),
Mathias Agopian9779b2212009-09-07 16:32:45 -070072 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070073 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 mBootTime(systemTime()),
Mathias Agopian151e8592009-06-15 18:24:59 -070075 mHardwareTest("android.permission.HARDWARE_TEST"),
76 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopianca5edbe2010-09-24 11:26:58 -070077 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian151e8592009-06-15 18:24:59 -070078 mDump("android.permission.DUMP"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070080 mHwWorkListDirty(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070083 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070088 mDebugDisableHWC(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070089 mDebugInSwapBuffers(0),
90 mLastSwapBufferTime(0),
91 mDebugInTransaction(0),
92 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -070093 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 mConsoleSignals(0),
95 mSecureFrameBuffer(0)
96{
97 init();
98}
99
100void SurfaceFlinger::init()
101{
102 LOGI("SurfaceFlinger is starting");
103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 property_get("debug.sf.showbackground", value, "0");
109 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700111 LOGI_IF(mDebugRegion, "showupdates enabled");
112 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}
114
115SurfaceFlinger::~SurfaceFlinger()
116{
117 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118}
119
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700120sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700122 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
Mathias Agopian770492c2010-05-28 14:22:23 -0700125sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700127 sp<ISurfaceComposerClient> bclient;
128 sp<Client> client(new Client(this));
129 status_t err = client->initCheck();
130 if (err == NO_ERROR) {
131 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 return bclient;
134}
135
Mathias Agopian7623da42010-06-01 15:12:58 -0700136sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
137{
138 sp<ISurfaceComposerClient> bclient;
139 sp<UserClient> client(new UserClient(this));
140 status_t err = client->initCheck();
141 if (err == NO_ERROR) {
142 bclient = client;
143 }
144 return bclient;
145}
146
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
149{
150 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
151 const GraphicPlane& plane(mGraphicPlanes[dpy]);
152 return plane;
153}
154
155GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
156{
157 return const_cast<GraphicPlane&>(
158 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
159}
160
161void SurfaceFlinger::bootFinished()
162{
163 const nsecs_t now = systemTime();
164 const nsecs_t duration = now - mBootTime;
Andreas Hubere3c01832010-08-16 08:49:37 -0700165 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700166 mBootFinished = true;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700167 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168}
169
170void SurfaceFlinger::onFirstRef()
171{
172 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
173
174 // Wait for the main thread to be done with its initialization
175 mReadyToRunBarrier.wait();
176}
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178static inline uint16_t pack565(int r, int g, int b) {
179 return (r<<11)|(g<<5)|b;
180}
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182status_t SurfaceFlinger::readyToRun()
183{
184 LOGI( "SurfaceFlinger's main thread ready to run. "
185 "Initializing graphics H/W...");
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 // we only support one display currently
188 int dpy = 0;
189
190 {
191 // initialize the main display
192 GraphicPlane& plane(graphicPlane(dpy));
193 DisplayHardware* const hw = new DisplayHardware(this, dpy);
194 plane.setDisplayHardware(hw);
195 }
196
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700197 // create the shared control-block
198 mServerHeap = new MemoryHeapBase(4096,
199 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
200 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Hubere3c01832010-08-16 08:49:37 -0700201
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700202 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
203 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Hubere3c01832010-08-16 08:49:37 -0700204
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700205 new(mServerCblk) surface_flinger_cblk_t;
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 // initialize primary screen
208 // (other display should be initialized in the same manner, but
209 // asynchronously, as they could come and go. None of this is supported
210 // yet).
211 const GraphicPlane& plane(graphicPlane(dpy));
212 const DisplayHardware& hw = plane.displayHardware();
213 const uint32_t w = hw.getWidth();
214 const uint32_t h = hw.getHeight();
215 const uint32_t f = hw.getFormat();
216 hw.makeCurrent();
217
218 // initialize the shared control block
219 mServerCblk->connected |= 1<<dpy;
220 display_cblk_t* dcblk = mServerCblk->displays + dpy;
221 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian66c77a52010-02-08 15:49:35 -0800222 dcblk->w = plane.getWidth();
223 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 dcblk->format = f;
225 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
226 dcblk->xdpi = hw.getDpiX();
227 dcblk->ydpi = hw.getDpiY();
228 dcblk->fps = hw.getRefreshRate();
229 dcblk->density = hw.getDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700233 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 glEnableClientState(GL_VERTEX_ARRAY);
235 glEnable(GL_SCISSOR_TEST);
236 glShadeModel(GL_FLAT);
237 glDisable(GL_DITHER);
238 glDisable(GL_CULL_FACE);
239
240 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
241 const uint16_t g1 = pack565(0x17,0x2f,0x17);
242 const uint16_t textureData[4] = { g0, g1, g1, g0 };
243 glGenTextures(1, &mWormholeTexName);
244 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
245 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
246 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
247 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
248 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
250 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
251
252 glViewport(0, 0, w, h);
253 glMatrixMode(GL_PROJECTION);
254 glLoadIdentity();
255 glOrthof(0, w, h, 0, 0, 1);
256
257 LayerDim::initDimmer(this, w, h);
258
259 mReadyToRunBarrier.open();
260
261 /*
262 * We're now ready to accept clients...
263 */
264
Mathias Agopian627e7b52009-05-21 19:21:59 -0700265 // start boot animation
266 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 return NO_ERROR;
269}
270
271// ----------------------------------------------------------------------------
272#if 0
273#pragma mark -
274#pragma mark Events Handler
275#endif
276
277void SurfaceFlinger::waitForEvent()
278{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700279 while (true) {
280 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800281 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700282 if (UNLIKELY(isFrozen())) {
283 // wait 5 seconds
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700284 const nsecs_t now = systemTime();
285 if (mFreezeDisplayTime == 0) {
286 mFreezeDisplayTime = now;
287 }
288 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
289 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700290 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700291
Mathias Agopian898c4c92010-05-18 17:06:55 -0700292 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-12-01 17:23:28 -0800293
294 // see if we timed out
295 if (isFrozen()) {
296 const nsecs_t now = systemTime();
297 nsecs_t frozenTime = (now - mFreezeDisplayTime);
298 if (frozenTime >= freezeDisplayTimeout) {
299 // we timed out and are still frozen
300 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
301 mFreezeDisplay, mFreezeCount);
302 mFreezeDisplayTime = 0;
303 mFreezeCount = 0;
304 mFreezeDisplay = false;
305 }
306 }
307
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700308 if (msg != 0) {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700309 switch (msg->what) {
310 case MessageQueue::INVALIDATE:
311 // invalidate message, just return to the main loop
312 return;
313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316}
317
318void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700319 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320}
321
322void SurfaceFlinger::signal() const {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700323 // this is the IPC call
324 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325}
326
Mathias Agopian898c4c92010-05-18 17:06:55 -0700327status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
328 nsecs_t reltime, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700330 return mEventQueue.postMessage(msg, reltime, flags);
331}
332
333status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
334 nsecs_t reltime, uint32_t flags)
335{
336 status_t res = mEventQueue.postMessage(msg, reltime, flags);
337 if (res == NO_ERROR) {
338 msg->wait();
339 }
340 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341}
342
343// ----------------------------------------------------------------------------
344#if 0
345#pragma mark -
346#pragma mark Main loop
347#endif
348
349bool SurfaceFlinger::threadLoop()
350{
351 waitForEvent();
352
353 // check for transactions
354 if (UNLIKELY(mConsoleSignals)) {
355 handleConsoleEvents();
356 }
357
358 if (LIKELY(mTransactionCount == 0)) {
359 // if we're in a global transaction, don't do anything.
360 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
361 uint32_t transactionFlags = getTransactionFlags(mask);
362 if (LIKELY(transactionFlags)) {
363 handleTransaction(transactionFlags);
364 }
365 }
366
367 // post surfaces (if needed)
368 handlePageFlip();
369
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700370 if (UNLIKELY(mHwWorkListDirty)) {
371 // build the h/w work list
372 handleWorkList();
373 }
374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700376 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-09-13 22:57:58 -0700378
379 const int index = hw.getCurrentBufferIndex();
380 GraphicLog& logger(GraphicLog::getInstance());
381
382 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 handleRepaint();
384
Mathias Agopianb1a18742009-09-17 16:18:16 -0700385 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700386 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700387 hw.compositionComplete();
388
Mathias Agopian04262e92010-09-13 22:57:58 -0700389 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700390 postFramebuffer();
391
Mathias Agopiana5ab8ce2010-09-15 12:29:18 -0700392 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
Mathias Agopian04262e92010-09-13 22:57:58 -0700395 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 } else {
397 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800398 hw.compositionComplete();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 usleep(16667); // 60 fps period
401 }
402 return true;
403}
404
405void SurfaceFlinger::postFramebuffer()
406{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 if (!mInvalidRegion.isEmpty()) {
408 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700409 const nsecs_t now = systemTime();
410 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700412 mLastSwapBufferTime = systemTime() - now;
413 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416}
417
418void SurfaceFlinger::handleConsoleEvents()
419{
420 // something to do with the console
421 const DisplayHardware& hw = graphicPlane(0).displayHardware();
422
423 int what = android_atomic_and(0, &mConsoleSignals);
424 if (what & eConsoleAcquired) {
425 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700426 // this is a temporary work-around, eventually this should be called
427 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700428 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 }
430
Mathias Agopianaab758e2010-10-11 12:37:43 -0700431 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700432 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 hw.releaseScreen();
435 }
436
437 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700438 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 hw.releaseScreen();
440 } else {
441 mDeferReleaseConsole = true;
442 }
443 }
444
445 mDirtyRegion.set(hw.bounds());
446}
447
448void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
449{
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700450 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451
Mathias Agopianff1d4102010-08-10 17:19:56 -0700452 /*
453 * Perform and commit the transaction
454 */
455
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700456 { // scope for the lock
457 Mutex::Autolock _l(mStateLock);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700458 const nsecs_t now = systemTime();
459 mDebugInTransaction = now;
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700460 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700461 mLastTransactionTime = systemTime() - now;
462 mDebugInTransaction = 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800463 invalidateHwcGeometry();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700464 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700465 }
466
Mathias Agopianff1d4102010-08-10 17:19:56 -0700467 /*
468 * Clean-up all layers that went away
469 * (do this without the lock held)
470 */
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700471
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700472 const size_t count = ditchedLayers.size();
473 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianffae4fc2009-09-04 19:50:23 -0700474 if (ditchedLayers[i] != 0) {
475 //LOGD("ditching layer %p", ditchedLayers[i].get());
476 ditchedLayers[i]->ditch();
477 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700478 }
479}
480
481void SurfaceFlinger::handleTransactionLocked(
482 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
483{
484 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 const size_t count = currentLayers.size();
486
487 /*
488 * Traversal of the children
489 * (perform the transaction for each of them if needed)
490 */
491
492 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
493 if (layersNeedTransaction) {
494 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700495 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
497 if (!trFlags) continue;
498
499 const uint32_t flags = layer->doTransaction(0);
500 if (flags & Layer::eVisibleRegion)
501 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503 }
504
505 /*
506 * Perform our own transaction if needed
507 */
508
509 if (transactionFlags & eTransactionNeeded) {
510 if (mCurrentState.orientation != mDrawingState.orientation) {
511 // the orientation has changed, recompute all visible regions
512 // and invalidate everything.
513
514 const int dpy = 0;
515 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700516 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 GraphicPlane& plane(graphicPlane(dpy));
518 plane.setOrientation(orientation);
519
520 // update the shared control block
521 const DisplayHardware& hw(plane.displayHardware());
522 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
523 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800524 dcblk->w = plane.getWidth();
525 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526
527 mVisibleRegionsDirty = true;
528 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
530
531 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
532 // freezing or unfreezing the display -> trigger animation if needed
533 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800534 if (mFreezeDisplay)
535 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
537
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700538 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
539 // layers have been added
540 mVisibleRegionsDirty = true;
541 }
542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 // some layers might have been removed, so
544 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700545 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700546 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700548 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700549 const size_t count = previousLayers.size();
550 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700551 const sp<LayerBase>& layer(previousLayers[i]);
552 if (currentLayers.indexOf( layer ) < 0) {
553 // this layer is not visible anymore
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700554 ditchedLayers.add(layer);
Mathias Agopian33863dd2009-07-28 14:20:21 -0700555 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700556 }
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560
561 commitTransaction();
562}
563
564sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
565{
566 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
567}
568
569void SurfaceFlinger::computeVisibleRegions(
570 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
571{
572 const GraphicPlane& plane(graphicPlane(0));
573 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700574 const DisplayHardware& hw(plane.displayHardware());
575 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576
577 Region aboveOpaqueLayers;
578 Region aboveCoveredLayers;
579 Region dirty;
580
581 bool secureFrameBuffer = false;
582
583 size_t i = currentLayers.size();
584 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700585 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 layer->validateVisibility(planeTransform);
587
588 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700589 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700591 /*
592 * opaqueRegion: area of a surface that is fully opaque.
593 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700595
596 /*
597 * visibleRegion: area of a surface that is visible on screen
598 * and not fully transparent. This is essentially the layer's
599 * footprint minus the opaque regions above it.
600 * Areas covered by a translucent surface are considered visible.
601 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700603
604 /*
605 * coveredRegion: area of a surface that is covered by all
606 * visible regions above it (which includes the translucent areas).
607 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700609
610
611 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700612 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 const bool translucent = layer->needsBlending();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700614 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700616 visibleRegion.andSelf(screenRegion);
617 if (!visibleRegion.isEmpty()) {
618 // Remove the transparent area from the visible region
619 if (translucent) {
620 visibleRegion.subtractSelf(layer->transparentRegionScreen);
621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700623 // compute the opaque region
624 const int32_t layerOrientation = layer->getOrientation();
625 if (s.alpha==255 && !translucent &&
626 ((layerOrientation & Transform::ROT_INVALID) == false)) {
627 // the opaque region is the layer's footprint
628 opaqueRegion = visibleRegion;
629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631 }
632
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700633 // Clip the covered region to the visible region
634 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
635
636 // Update aboveCoveredLayers for next (lower) layer
637 aboveCoveredLayers.orSelf(visibleRegion);
638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 // subtract the opaque region covered by the layers above us
640 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641
642 // compute this layer's dirty region
643 if (layer->contentDirty) {
644 // we need to invalidate the whole region
645 dirty = visibleRegion;
646 // as well, as the old visible region
647 dirty.orSelf(layer->visibleRegionScreen);
648 layer->contentDirty = false;
649 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700650 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700651 * the exposed region consists of two components:
652 * 1) what's VISIBLE now and was COVERED before
653 * 2) what's EXPOSED now less what was EXPOSED before
654 *
655 * note that (1) is conservative, we start with the whole
656 * visible region but only keep what used to be covered by
657 * something -- which mean it may have been exposed.
658 *
659 * (2) handles areas that were not covered by anything but got
660 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700661 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700662 const Region newExposed = visibleRegion - coveredRegion;
663 const Region oldVisibleRegion = layer->visibleRegionScreen;
664 const Region oldCoveredRegion = layer->coveredRegionScreen;
665 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
666 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668 dirty.subtractSelf(aboveOpaqueLayers);
669
670 // accumulate to the screen dirty region
671 dirtyRegion.orSelf(dirty);
672
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700673 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 // Store the visible region is screen space
677 layer->setVisibleRegion(visibleRegion);
678 layer->setCoveredRegion(coveredRegion);
679
Mathias Agopian12cedff2009-07-28 10:57:27 -0700680 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 if (layer->isSecure() && !visibleRegion.isEmpty()) {
682 secureFrameBuffer = true;
683 }
684 }
685
Mathias Agopian12cedff2009-07-28 10:57:27 -0700686 // invalidate the areas where a layer was removed
687 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
688 mDirtyRegionRemovedLayer.clear();
689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 mSecureFrameBuffer = secureFrameBuffer;
691 opaqueRegion = aboveOpaqueLayers;
692}
693
694
695void SurfaceFlinger::commitTransaction()
696{
697 mDrawingState = mCurrentState;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700698 mResizeTransationPending = false;
699 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700}
701
702void SurfaceFlinger::handlePageFlip()
703{
704 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700705 LayerVector& currentLayers(
706 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 visibleRegions |= lockPageFlip(currentLayers);
708
709 const DisplayHardware& hw = graphicPlane(0).displayHardware();
710 const Region screenRegion(hw.bounds());
711 if (visibleRegions) {
712 Region opaqueRegion;
713 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700714
715 /*
716 * rebuild the visible layer list
717 */
718 mVisibleLayersSortedByZ.clear();
719 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
720 size_t count = currentLayers.size();
721 mVisibleLayersSortedByZ.setCapacity(count);
722 for (size_t i=0 ; i<count ; i++) {
723 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
724 mVisibleLayersSortedByZ.add(currentLayers[i]);
725 }
726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 mWormholeRegion = screenRegion.subtract(opaqueRegion);
728 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800729 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 unlockPageFlip(currentLayers);
733 mDirtyRegion.andSelf(screenRegion);
734}
735
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800736void SurfaceFlinger::invalidateHwcGeometry()
737{
738 mHwWorkListDirty = true;
739}
740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
742{
743 bool recomputeVisibleRegions = false;
744 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700745 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700747 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 layer->lockPageFlip(recomputeVisibleRegions);
749 }
750 return recomputeVisibleRegions;
751}
752
753void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
754{
755 const GraphicPlane& plane(graphicPlane(0));
756 const Transform& planeTransform(plane.transform());
757 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700758 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700760 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 layer->unlockPageFlip(planeTransform, mDirtyRegion);
762 }
763}
764
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700765void SurfaceFlinger::handleWorkList()
766{
767 mHwWorkListDirty = false;
768 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
769 if (hwc.initCheck() == NO_ERROR) {
770 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
771 const size_t count = currentLayers.size();
772 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700773 hwc_layer_t* const cur(hwc.getLayers());
774 for (size_t i=0 ; cur && i<count ; i++) {
775 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian6a969242010-09-22 18:58:01 -0700776 if (mDebugDisableHWC) {
777 cur[i].compositionType = HWC_FRAMEBUFFER;
778 cur[i].flags |= HWC_SKIP_LAYER;
779 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700780 }
781 }
782}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784void SurfaceFlinger::handleRepaint()
785{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700786 // compute the invalid region
787 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
789 if (UNLIKELY(mDebugRegion)) {
790 debugFlashRegions();
791 }
792
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700793 // set the frame buffer
794 const DisplayHardware& hw(graphicPlane(0).displayHardware());
795 glMatrixMode(GL_MODELVIEW);
796 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797
798 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700799 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
800 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700801 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700802 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
803 // takes a rectangle, we must make sure to update that whole
804 // rectangle in that case
805 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700806 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700807 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 mDirtyRegion.set(mInvalidRegion.bounds());
809 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700810 // in the BUFFER_PRESERVED case, obviously, we can update only
811 // what's needed and nothing more.
812 // NOTE: this is NOT a common case, as preserving the backbuffer
813 // is costly and usually involves copying the whole update back.
814 }
815 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700816 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700817 // We need to redraw the rectangle that will be updated
818 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700819 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700820 // rectangle instead of a region (see DisplayHardware::flip())
821 mDirtyRegion.set(mInvalidRegion.bounds());
822 } else {
823 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 mDirtyRegion.set(hw.bounds());
825 mInvalidRegion = mDirtyRegion;
826 }
827 }
828
829 // compose all surfaces
830 composeSurfaces(mDirtyRegion);
831
832 // clear the dirty regions
833 mDirtyRegion.clear();
834}
835
836void SurfaceFlinger::composeSurfaces(const Region& dirty)
837{
838 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
839 // should never happen unless the window manager has a bug
840 // draw something...
841 drawWormhole();
842 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700843
844 status_t err = NO_ERROR;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700845 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700846 size_t count = layers.size();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700847
848 const DisplayHardware& hw(graphicPlane(0).displayHardware());
849 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700850 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700851
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700852 LOGE_IF(cur && hwc.getNumLayers() != count,
853 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
854 hwc.getNumLayers(), count);
855
856 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700857 if (hwc.initCheck() == NO_ERROR) {
858 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
859 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700860
861 /*
862 * update the per-frame h/w composer data for each layer
863 * and build the transparent region of the FB
864 */
865 Region transparent;
866 if (cur) {
867 for (size_t i=0 ; i<count ; i++) {
868 const sp<LayerBase>& layer(layers[i]);
869 layer->setPerFrameData(&cur[i]);
870 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
871 if (!(layer->needsBlending())) {
872 transparent.orSelf(layer->visibleRegionScreen);
873 }
874 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700875 }
876 err = hwc.prepare();
877 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
878 }
879
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700880 /*
881 * clear the area of the FB that need to be transparent
882 */
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700883 transparent.andSelf(dirty);
884 if (!transparent.isEmpty()) {
885 glClearColor(0,0,0,0);
886 Region::const_iterator it = transparent.begin();
887 Region::const_iterator const end = transparent.end();
888 const int32_t height = hw.getHeight();
889 while (it != end) {
890 const Rect& r(*it++);
891 const GLint sy = height - (r.top + r.height());
892 glScissor(r.left, sy, r.width(), r.height());
893 glClear(GL_COLOR_BUFFER_BIT);
894 }
895 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700896
897
898 /*
899 * and then, render the layers targeted at the framebuffer
900 */
901 for (size_t i=0 ; i<count ; i++) {
902 if (cur) {
Antti Hatala982f58b2010-09-09 02:32:30 -0700903 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
904 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700905 // skip layers handled by the HAL
906 continue;
907 }
908 }
Mathias Agopian6a969242010-09-22 18:58:01 -0700909
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700910 const sp<LayerBase>& layer(layers[i]);
911 const Region clip(dirty.intersect(layer->visibleRegionScreen));
912 if (!clip.isEmpty()) {
913 layer->draw(clip);
914 }
915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916}
917
918void SurfaceFlinger::unlockClients()
919{
920 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
921 const size_t count = drawingLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700922 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700924 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 layer->finishPageFlip();
926 }
927}
928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929void SurfaceFlinger::debugFlashRegions()
930{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700931 const DisplayHardware& hw(graphicPlane(0).displayHardware());
932 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700933
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700934 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
935 (flags & DisplayHardware::BUFFER_PRESERVED))) {
936 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
937 mDirtyRegion.bounds() : hw.bounds());
938 composeSurfaces(repaint);
939 }
940
941 TextureManager::deactivateTextures();
942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 glDisable(GL_BLEND);
944 glDisable(GL_DITHER);
945 glDisable(GL_SCISSOR_TEST);
946
Mathias Agopiandff8e582009-05-04 14:17:04 -0700947 static int toggle = 0;
948 toggle = 1 - toggle;
949 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700950 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700951 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700952 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700955 Region::const_iterator it = mDirtyRegion.begin();
956 Region::const_iterator const end = mDirtyRegion.end();
957 while (it != end) {
958 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 GLfloat vertices[][2] = {
960 { r.left, r.top },
961 { r.left, r.bottom },
962 { r.right, r.bottom },
963 { r.right, r.top }
964 };
965 glVertexPointer(2, GL_FLOAT, 0, vertices);
966 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
967 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700968
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700969 if (mInvalidRegion.isEmpty()) {
970 mDirtyRegion.dump("mDirtyRegion");
971 mInvalidRegion.dump("mInvalidRegion");
972 }
973 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974
975 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700976 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977
978 glEnable(GL_SCISSOR_TEST);
979 //mDirtyRegion.dump("mDirtyRegion");
980}
981
982void SurfaceFlinger::drawWormhole() const
983{
984 const Region region(mWormholeRegion.intersect(mDirtyRegion));
985 if (region.isEmpty())
986 return;
987
988 const DisplayHardware& hw(graphicPlane(0).displayHardware());
989 const int32_t width = hw.getWidth();
990 const int32_t height = hw.getHeight();
991
992 glDisable(GL_BLEND);
993 glDisable(GL_DITHER);
994
995 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700996 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700997 Region::const_iterator it = region.begin();
998 Region::const_iterator const end = region.end();
999 while (it != end) {
1000 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 const GLint sy = height - (r.top + r.height());
1002 glScissor(r.left, sy, r.width(), r.height());
1003 glClear(GL_COLOR_BUFFER_BIT);
1004 }
1005 } else {
1006 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1007 { width, height }, { 0, height } };
1008 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1009 glVertexPointer(2, GL_SHORT, 0, vertices);
1010 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1011 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001012#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001013 if (GLExtensions::getInstance().haveTextureExternal()) {
1014 glDisable(GL_TEXTURE_EXTERNAL_OES);
1015 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001016#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 glEnable(GL_TEXTURE_2D);
1018 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1019 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1020 glMatrixMode(GL_TEXTURE);
1021 glLoadIdentity();
1022 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001023 Region::const_iterator it = region.begin();
1024 Region::const_iterator const end = region.end();
1025 while (it != end) {
1026 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 const GLint sy = height - (r.top + r.height());
1028 glScissor(r.left, sy, r.width(), r.height());
1029 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1030 }
1031 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001032 glLoadIdentity();
1033 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
1035}
1036
1037void SurfaceFlinger::debugShowFPS() const
1038{
1039 static int mFrameCount;
1040 static int mLastFrameCount = 0;
1041 static nsecs_t mLastFpsTime = 0;
1042 static float mFps = 0;
1043 mFrameCount++;
1044 nsecs_t now = systemTime();
1045 nsecs_t diff = now - mLastFpsTime;
1046 if (diff > ms2ns(250)) {
1047 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1048 mLastFpsTime = now;
1049 mLastFrameCount = mFrameCount;
1050 }
1051 // XXX: mFPS has the value we want
1052 }
1053
Mathias Agopian1473f462009-04-10 14:24:30 -07001054status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055{
1056 Mutex::Autolock _l(mStateLock);
1057 addLayer_l(layer);
1058 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1059 return NO_ERROR;
1060}
1061
Mathias Agopian593c05c2010-06-02 23:28:45 -07001062status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1063{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001064 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001065 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1066}
1067
1068ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1069 const sp<LayerBaseClient>& lbc)
1070{
1071 Mutex::Autolock _l(mStateLock);
1072
1073 // attach this layer to the client
1074 ssize_t name = client->attachLayer(lbc);
1075
1076 // add this layer to the current state list
1077 addLayer_l(lbc);
1078
1079 return name;
1080}
1081
Mathias Agopian1473f462009-04-10 14:24:30 -07001082status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083{
1084 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001085 status_t err = purgatorizeLayer_l(layer);
1086 if (err == NO_ERROR)
1087 setTransactionFlags(eTransactionNeeded);
1088 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089}
1090
Mathias Agopian1473f462009-04-10 14:24:30 -07001091status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092{
Mathias Agopian7623da42010-06-01 15:12:58 -07001093 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1094 if (lbc != 0) {
1095 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1098 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001099 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 return NO_ERROR;
1101 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001102 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103}
1104
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001105status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1106{
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001107 // remove the layer from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001108 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001109
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001110 layerBase->onRemoved();
1111
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001112 // it's possible that we don't find a layer, because it might
1113 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001114 // from the user because there is a race between Client::destroySurface(),
1115 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001116 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1117}
1118
Mathias Agopian593c05c2010-06-02 23:28:45 -07001119status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001121 layer->forceVisibilityTransaction();
1122 setTransactionFlags(eTraversalNeeded);
1123 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124}
1125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1127{
1128 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1129}
1130
Mathias Agopian898c4c92010-05-18 17:06:55 -07001131uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132{
1133 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1134 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001135 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137 return old;
1138}
1139
1140void SurfaceFlinger::openGlobalTransaction()
1141{
1142 android_atomic_inc(&mTransactionCount);
1143}
1144
1145void SurfaceFlinger::closeGlobalTransaction()
1146{
1147 if (android_atomic_dec(&mTransactionCount) == 1) {
1148 signalEvent();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001149
Andreas Hubere3c01832010-08-16 08:49:37 -07001150 // if there is a transaction with a resize, wait for it to
Mathias Agopian9779b2212009-09-07 16:32:45 -07001151 // take effect before returning.
1152 Mutex::Autolock _l(mStateLock);
1153 while (mResizeTransationPending) {
Mathias Agopian98a9c562009-09-30 14:42:13 -07001154 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1155 if (CC_UNLIKELY(err != NO_ERROR)) {
1156 // just in case something goes wrong in SF, return to the
1157 // called after a few seconds.
1158 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1159 mResizeTransationPending = false;
1160 break;
1161 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164}
1165
1166status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1167{
1168 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1169 return BAD_VALUE;
1170
1171 Mutex::Autolock _l(mStateLock);
1172 mCurrentState.freezeDisplay = 1;
1173 setTransactionFlags(eTransactionNeeded);
1174
1175 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001176 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 return NO_ERROR;
1178}
1179
1180status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1181{
1182 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1183 return BAD_VALUE;
1184
1185 Mutex::Autolock _l(mStateLock);
1186 mCurrentState.freezeDisplay = 0;
1187 setTransactionFlags(eTransactionNeeded);
1188
1189 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001190 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 return NO_ERROR;
1192}
1193
Andreas Hubere3c01832010-08-16 08:49:37 -07001194int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001195 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196{
1197 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1198 return BAD_VALUE;
1199
1200 Mutex::Autolock _l(mStateLock);
1201 if (mCurrentState.orientation != orientation) {
1202 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001203 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 mCurrentState.orientation = orientation;
1205 setTransactionFlags(eTransactionNeeded);
1206 mTransactionCV.wait(mStateLock);
1207 } else {
1208 orientation = BAD_VALUE;
1209 }
1210 }
1211 return orientation;
1212}
1213
Mathias Agopian593c05c2010-06-02 23:28:45 -07001214sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian770492c2010-05-28 14:22:23 -07001215 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1217 uint32_t flags)
1218{
Mathias Agopian1473f462009-04-10 14:24:30 -07001219 sp<LayerBaseClient> layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001221
1222 if (int32_t(w|h) < 0) {
1223 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1224 int(w), int(h));
1225 return surfaceHandle;
1226 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001229 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 switch (flags & eFXSurfaceMask) {
1231 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001232 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1233 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 break;
1235 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001236 // for now we treat Blur as Dim, until we can implement it
1237 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001239 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 break;
1241 }
1242
Mathias Agopian1473f462009-04-10 14:24:30 -07001243 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001244 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001245 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001246 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001249 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001250 params->token = token;
Mathias Agopian18b6b492009-08-19 17:46:26 -07001251 params->identity = surfaceHandle->getIdentity();
1252 params->width = w;
1253 params->height = h;
1254 params->format = format;
Mathias Agopian7623da42010-06-01 15:12:58 -07001255 if (normalLayer != 0) {
1256 Mutex::Autolock _l(mStateLock);
1257 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1258 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001259 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001260
Mathias Agopian593c05c2010-06-02 23:28:45 -07001261 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263
1264 return surfaceHandle;
1265}
1266
Mathias Agopian7623da42010-06-01 15:12:58 -07001267sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001268 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001269 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001270 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271{
1272 // initialize the surfaces
1273 switch (format) { // TODO: take h/w into account
1274 case PIXEL_FORMAT_TRANSPARENT:
1275 case PIXEL_FORMAT_TRANSLUCENT:
1276 format = PIXEL_FORMAT_RGBA_8888;
1277 break;
1278 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001279#ifdef NO_RGBX_8888
1280 format = PIXEL_FORMAT_RGB_565;
1281#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001282 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001283#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 break;
1285 }
1286
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001287#ifdef NO_RGBX_8888
1288 if (format == PIXEL_FORMAT_RGBX_8888)
1289 format = PIXEL_FORMAT_RGBA_8888;
1290#endif
1291
Mathias Agopian593c05c2010-06-02 23:28:45 -07001292 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001293 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001294 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001296 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
1298 return layer;
1299}
1300
Mathias Agopian7623da42010-06-01 15:12:58 -07001301sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001302 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001303 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001305 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 return layer;
1308}
1309
Mathias Agopian593c05c2010-06-02 23:28:45 -07001310status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001312 /*
1313 * called by the window manager, when a surface should be marked for
1314 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001315 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001316 * The surface is removed from the current and drawing lists, but placed
1317 * in the purgatory queue, so it's not destroyed right-away (we need
1318 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001319 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001320
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001321 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001322 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001323 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001324 if (layer != 0) {
1325 err = purgatorizeLayer_l(layer);
1326 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001327 setTransactionFlags(eTransactionNeeded);
1328 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001329 }
1330 return err;
1331}
1332
1333status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1334{
Mathias Agopian359140c2009-07-02 17:33:40 -07001335 // called by ~ISurface() when all references are gone
Andreas Hubere3c01832010-08-16 08:49:37 -07001336
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001337 class MessageDestroySurface : public MessageBase {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001338 SurfaceFlinger* flinger;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001339 sp<LayerBaseClient> layer;
1340 public:
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001341 MessageDestroySurface(
1342 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1343 : flinger(flinger), layer(layer) { }
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001344 virtual bool handler() {
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001345 sp<LayerBaseClient> l(layer);
1346 layer.clear(); // clear it outside of the lock;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001347 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian359140c2009-07-02 17:33:40 -07001348 /*
Andreas Hubere3c01832010-08-16 08:49:37 -07001349 * remove the layer from the current list -- chances are that it's
1350 * not in the list anyway, because it should have been removed
1351 * already upon request of the client (eg: window manager).
Mathias Agopian359140c2009-07-02 17:33:40 -07001352 * However, a buggy client could have not done that.
1353 * Since we know we don't have any more clients, we don't need
1354 * to use the purgatory.
1355 */
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001356 status_t err = flinger->removeLayer_l(l);
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001357 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1358 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001359 return true;
1360 }
1361 };
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001362
Mathias Agopian898c4c92010-05-18 17:06:55 -07001363 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 return NO_ERROR;
1365}
1366
1367status_t SurfaceFlinger::setClientState(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001368 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 int32_t count,
1370 const layer_state_t* states)
1371{
1372 Mutex::Autolock _l(mStateLock);
1373 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 for (int i=0 ; i<count ; i++) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001375 const layer_state_t& s(states[i]);
1376 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian1473f462009-04-10 14:24:30 -07001377 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 const uint32_t what = s.what;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 if (what & ePositionChanged) {
1380 if (layer->setPosition(s.x, s.y))
1381 flags |= eTraversalNeeded;
1382 }
1383 if (what & eLayerChanged) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001384 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 if (layer->setLayer(s.z)) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001386 mCurrentState.layersSortedByZ.removeAt(idx);
1387 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 // we need traversal (state changed)
1389 // AND transaction (list changed)
1390 flags |= eTransactionNeeded|eTraversalNeeded;
1391 }
1392 }
1393 if (what & eSizeChanged) {
Mathias Agopian9779b2212009-09-07 16:32:45 -07001394 if (layer->setSize(s.w, s.h)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 flags |= eTraversalNeeded;
Mathias Agopian9779b2212009-09-07 16:32:45 -07001396 mResizeTransationPending = true;
1397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 if (what & eAlphaChanged) {
1400 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1401 flags |= eTraversalNeeded;
1402 }
1403 if (what & eMatrixChanged) {
1404 if (layer->setMatrix(s.matrix))
1405 flags |= eTraversalNeeded;
1406 }
1407 if (what & eTransparentRegionChanged) {
1408 if (layer->setTransparentRegionHint(s.transparentRegion))
1409 flags |= eTraversalNeeded;
1410 }
1411 if (what & eVisibilityChanged) {
1412 if (layer->setFlags(s.flags, s.mask))
1413 flags |= eTraversalNeeded;
1414 }
1415 }
1416 }
1417 if (flags) {
1418 setTransactionFlags(flags);
1419 }
1420 return NO_ERROR;
1421}
1422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423void SurfaceFlinger::screenReleased(int dpy)
1424{
1425 // this may be called by a signal handler, we can't do too much in here
1426 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1427 signalEvent();
1428}
1429
1430void SurfaceFlinger::screenAcquired(int dpy)
1431{
1432 // this may be called by a signal handler, we can't do too much in here
1433 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1434 signalEvent();
1435}
1436
1437status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1438{
Erik Gilling94720d72010-12-01 16:38:01 -08001439 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 char buffer[SIZE];
1441 String8 result;
Mathias Agopian151e8592009-06-15 18:24:59 -07001442 if (!mDump.checkCalling()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 snprintf(buffer, SIZE, "Permission Denial: "
1444 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1445 IPCThreadState::self()->getCallingPid(),
1446 IPCThreadState::self()->getCallingUid());
1447 result.append(buffer);
1448 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001449
1450 // figure out if we're stuck somewhere
1451 const nsecs_t now = systemTime();
1452 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1453 const nsecs_t inTransaction(mDebugInTransaction);
1454 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1455 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1456
1457 // Try to get the main lock, but don't insist if we can't
1458 // (this would indicate SF is stuck, but we want to be able to
1459 // print something in dumpsys).
1460 int retry = 3;
1461 while (mStateLock.tryLock()<0 && --retry>=0) {
1462 usleep(1000000);
1463 }
1464 const bool locked(retry >= 0);
1465 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001466 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001467 "SurfaceFlinger appears to be unresponsive, "
1468 "dumping anyways (no locks held)\n");
1469 result.append(buffer);
1470 }
1471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1473 const size_t count = currentLayers.size();
1474 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001475 const sp<LayerBase>& layer(currentLayers[i]);
1476 layer->dump(result, buffer, SIZE);
1477 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 s.transparentRegion.dump(result, "transparentRegion");
1479 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1480 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1481 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 mWormholeRegion.dump(result, "WormholeRegion");
1484 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1485 snprintf(buffer, SIZE,
1486 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1487 mFreezeDisplay?"yes":"no", mFreezeCount,
1488 mCurrentState.orientation, hw.canDraw());
1489 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001490 snprintf(buffer, SIZE,
1491 " last eglSwapBuffers() time: %f us\n"
1492 " last transaction time : %f us\n",
1493 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1494 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001495
Mathias Agopiana8d49172009-08-26 16:36:26 -07001496 if (inSwapBuffersDuration || !locked) {
1497 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1498 inSwapBuffersDuration/1000.0);
1499 result.append(buffer);
1500 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001501
Mathias Agopiana8d49172009-08-26 16:36:26 -07001502 if (inTransactionDuration || !locked) {
1503 snprintf(buffer, SIZE, " transaction time: %f us\n",
1504 inTransactionDuration/1000.0);
1505 result.append(buffer);
1506 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001507
Mathias Agopian6a969242010-09-22 18:58:01 -07001508 HWComposer& hwc(hw.getHwComposer());
1509 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1510 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1511 mDebugDisableHWC ? "disabled" : "enabled");
1512 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -07001513 hwc.dump(result, buffer, SIZE);
Mathias Agopian6a969242010-09-22 18:58:01 -07001514
Mathias Agopian6950e422009-10-05 17:07:12 -07001515 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001516 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001517 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001518
1519 if (locked) {
1520 mStateLock.unlock();
1521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523 write(fd, result.string(), result.size());
1524 return NO_ERROR;
1525}
1526
1527status_t SurfaceFlinger::onTransact(
1528 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1529{
1530 switch (code) {
1531 case CREATE_CONNECTION:
1532 case OPEN_GLOBAL_TRANSACTION:
1533 case CLOSE_GLOBAL_TRANSACTION:
1534 case SET_ORIENTATION:
1535 case FREEZE_DISPLAY:
1536 case UNFREEZE_DISPLAY:
1537 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001538 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001539 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 {
1541 // codes that require permission check
1542 IPCThreadState* ipc = IPCThreadState::self();
1543 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001544 const int uid = ipc->getCallingUid();
Mathias Agopian151e8592009-06-15 18:24:59 -07001545 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1546 LOGE("Permission Denial: "
1547 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1548 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001550 break;
1551 }
1552 case CAPTURE_SCREEN:
1553 {
1554 // codes that require permission check
1555 IPCThreadState* ipc = IPCThreadState::self();
1556 const int pid = ipc->getCallingPid();
1557 const int uid = ipc->getCallingUid();
1558 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1559 LOGE("Permission Denial: "
1560 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1561 return PERMISSION_DENIED;
1562 }
1563 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1568 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001569 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian151e8592009-06-15 18:24:59 -07001570 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1571 IPCThreadState* ipc = IPCThreadState::self();
1572 const int pid = ipc->getCallingPid();
1573 const int uid = ipc->getCallingUid();
1574 LOGE("Permission Denial: "
1575 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 return PERMISSION_DENIED;
1577 }
1578 int n;
1579 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001580 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001581 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 return NO_ERROR;
1583 case 1002: // SHOW_UPDATES
1584 n = data.readInt32();
1585 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1586 return NO_ERROR;
1587 case 1003: // SHOW_BACKGROUND
1588 n = data.readInt32();
1589 mDebugBackground = n ? 1 : 0;
1590 return NO_ERROR;
Mathias Agopian6a969242010-09-22 18:58:01 -07001591 case 1008: // toggle use of hw composer
1592 n = data.readInt32();
1593 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -08001594 invalidateHwcGeometry();
Mathias Agopian6a969242010-09-22 18:58:01 -07001595 // fall-through...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 case 1004:{ // repaint everything
1597 Mutex::Autolock _l(mStateLock);
1598 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1599 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1600 signalEvent();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001601 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001603 case 1005:{ // force transaction
1604 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1605 return NO_ERROR;
1606 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001607 case 1006:{ // enable/disable GraphicLog
1608 int enabled = data.readInt32();
1609 GraphicLog::getInstance().setEnabled(enabled);
1610 return NO_ERROR;
1611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 case 1007: // set mFreezeCount
1613 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001614 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 return NO_ERROR;
1616 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001617 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 reply->writeInt32(0);
1619 reply->writeInt32(mDebugRegion);
1620 reply->writeInt32(mDebugBackground);
1621 return NO_ERROR;
1622 case 1013: {
1623 Mutex::Autolock _l(mStateLock);
1624 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1625 reply->writeInt32(hw.getPageFlipCount());
1626 }
1627 return NO_ERROR;
1628 }
1629 }
1630 return err;
1631}
1632
Mathias Agopianaab758e2010-10-11 12:37:43 -07001633// ---------------------------------------------------------------------------
1634
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001635status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1636 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001637{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001638 if (!GLExtensions::getInstance().haveFramebufferObject())
1639 return INVALID_OPERATION;
1640
1641 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001642 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001643 const uint32_t hw_w = hw.getWidth();
1644 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001645 GLfloat u = 1;
1646 GLfloat v = 1;
1647
1648 // make sure to clear all GL error flags
1649 while ( glGetError() != GL_NO_ERROR ) ;
1650
1651 // create a FBO
1652 GLuint name, tname;
1653 glGenTextures(1, &tname);
1654 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001655 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1656 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001657 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001658 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001659 GLint tw = (2 << (31 - clz(hw_w)));
1660 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001661 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1662 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001663 u = GLfloat(hw_w) / tw;
1664 v = GLfloat(hw_h) / th;
1665 }
1666 glGenFramebuffersOES(1, &name);
1667 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001668 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1669 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001670
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001671 // redraw the screen entirely...
1672 glClearColor(0,0,0,1);
1673 glClear(GL_COLOR_BUFFER_BIT);
1674 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1675 const size_t count = layers.size();
1676 for (size_t i=0 ; i<count ; ++i) {
1677 const sp<LayerBase>& layer(layers[i]);
1678 layer->drawForSreenShot();
1679 }
1680
1681 // back to main framebuffer
1682 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1683 glDisable(GL_SCISSOR_TEST);
1684 glDeleteFramebuffersOES(1, &name);
1685
1686 *textureName = tname;
1687 *uOut = u;
1688 *vOut = v;
1689 return NO_ERROR;
1690}
1691
1692// ---------------------------------------------------------------------------
1693
1694status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1695{
1696 status_t result = PERMISSION_DENIED;
1697
1698 if (!GLExtensions::getInstance().haveFramebufferObject())
1699 return INVALID_OPERATION;
1700
1701 // get screen geometry
1702 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1703 const uint32_t hw_w = hw.getWidth();
1704 const uint32_t hw_h = hw.getHeight();
1705 const Region screenBounds(hw.bounds());
1706
1707 GLfloat u, v;
1708 GLuint tname;
1709 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1710 if (result != NO_ERROR) {
1711 return result;
1712 }
1713
1714 GLfloat vtx[8];
1715 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1716 glEnable(GL_TEXTURE_2D);
1717 glBindTexture(GL_TEXTURE_2D, tname);
1718 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1719 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1720 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1721 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1722 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1723 glVertexPointer(2, GL_FLOAT, 0, vtx);
1724
1725 class s_curve_interpolator {
1726 const float nbFrames, s, v;
1727 public:
1728 s_curve_interpolator(int nbFrames, float s)
1729 : nbFrames(1.0f / (nbFrames-1)), s(s),
1730 v(1.0f + expf(-s + 0.5f*s)) {
1731 }
1732 float operator()(int f) {
1733 const float x = f * nbFrames;
1734 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1735 }
1736 };
1737
1738 class v_stretch {
1739 const GLfloat hw_w, hw_h;
1740 public:
1741 v_stretch(uint32_t hw_w, uint32_t hw_h)
1742 : hw_w(hw_w), hw_h(hw_h) {
1743 }
1744 void operator()(GLfloat* vtx, float v) {
1745 const GLfloat w = hw_w + (hw_w * v);
1746 const GLfloat h = hw_h - (hw_h * v);
1747 const GLfloat x = (hw_w - w) * 0.5f;
1748 const GLfloat y = (hw_h - h) * 0.5f;
1749 vtx[0] = x; vtx[1] = y;
1750 vtx[2] = x; vtx[3] = y + h;
1751 vtx[4] = x + w; vtx[5] = y + h;
1752 vtx[6] = x + w; vtx[7] = y;
1753 }
1754 };
1755
1756 class h_stretch {
1757 const GLfloat hw_w, hw_h;
1758 public:
1759 h_stretch(uint32_t hw_w, uint32_t hw_h)
1760 : hw_w(hw_w), hw_h(hw_h) {
1761 }
1762 void operator()(GLfloat* vtx, float v) {
1763 const GLfloat w = hw_w - (hw_w * v);
1764 const GLfloat h = 1.0f;
1765 const GLfloat x = (hw_w - w) * 0.5f;
1766 const GLfloat y = (hw_h - h) * 0.5f;
1767 vtx[0] = x; vtx[1] = y;
1768 vtx[2] = x; vtx[3] = y + h;
1769 vtx[4] = x + w; vtx[5] = y + h;
1770 vtx[6] = x + w; vtx[7] = y;
1771 }
1772 };
1773
1774 // the full animation is 24 frames
1775 const int nbFrames = 12;
1776 s_curve_interpolator itr(nbFrames, 7.5f);
1777 s_curve_interpolator itg(nbFrames, 8.0f);
1778 s_curve_interpolator itb(nbFrames, 8.5f);
1779
1780 v_stretch vverts(hw_w, hw_h);
1781 glEnable(GL_BLEND);
1782 glBlendFunc(GL_ONE, GL_ONE);
1783 for (int i=0 ; i<nbFrames ; i++) {
1784 float x, y, w, h;
1785 const float vr = itr(i);
1786 const float vg = itg(i);
1787 const float vb = itb(i);
1788
1789 // clear screen
1790 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001791 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001792 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001793
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001794 // draw the red plane
1795 vverts(vtx, vr);
1796 glColorMask(1,0,0,1);
1797 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001798
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001799 // draw the green plane
1800 vverts(vtx, vg);
1801 glColorMask(0,1,0,1);
1802 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001803
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001804 // draw the blue plane
1805 vverts(vtx, vb);
1806 glColorMask(0,0,1,1);
1807 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001808
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001809 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001810 glDisable(GL_TEXTURE_2D);
1811 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001812 glColor4f(vg, vg, vg, 1);
1813 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1814 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001815 }
1816
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001817 h_stretch hverts(hw_w, hw_h);
1818 glDisable(GL_BLEND);
1819 glDisable(GL_TEXTURE_2D);
1820 glColorMask(1,1,1,1);
1821 for (int i=0 ; i<nbFrames ; i++) {
1822 const float v = itg(i);
1823 hverts(vtx, v);
1824 glClear(GL_COLOR_BUFFER_BIT);
1825 glColor4f(1-v, 1-v, 1-v, 1);
1826 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1827 hw.flip(screenBounds);
1828 }
1829
1830 glColorMask(1,1,1,1);
1831 glEnable(GL_SCISSOR_TEST);
1832 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1833 glDeleteTextures(1, &tname);
1834 return NO_ERROR;
1835}
1836
1837status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1838{
1839 status_t result = PERMISSION_DENIED;
1840
1841 if (!GLExtensions::getInstance().haveFramebufferObject())
1842 return INVALID_OPERATION;
1843
1844
1845 // get screen geometry
1846 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1847 const uint32_t hw_w = hw.getWidth();
1848 const uint32_t hw_h = hw.getHeight();
1849 const Region screenBounds(hw.bounds());
1850
1851 GLfloat u, v;
1852 GLuint tname;
1853 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1854 if (result != NO_ERROR) {
1855 return result;
1856 }
1857
1858 // back to main framebuffer
1859 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1860 glDisable(GL_SCISSOR_TEST);
1861
1862 GLfloat vtx[8];
1863 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1864 glEnable(GL_TEXTURE_2D);
1865 glBindTexture(GL_TEXTURE_2D, tname);
1866 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1867 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1868 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1869 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1870 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1871 glVertexPointer(2, GL_FLOAT, 0, vtx);
1872
1873 class s_curve_interpolator {
1874 const float nbFrames, s, v;
1875 public:
1876 s_curve_interpolator(int nbFrames, float s)
1877 : nbFrames(1.0f / (nbFrames-1)), s(s),
1878 v(1.0f + expf(-s + 0.5f*s)) {
1879 }
1880 float operator()(int f) {
1881 const float x = f * nbFrames;
1882 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1883 }
1884 };
1885
1886 class v_stretch {
1887 const GLfloat hw_w, hw_h;
1888 public:
1889 v_stretch(uint32_t hw_w, uint32_t hw_h)
1890 : hw_w(hw_w), hw_h(hw_h) {
1891 }
1892 void operator()(GLfloat* vtx, float v) {
1893 const GLfloat w = hw_w + (hw_w * v);
1894 const GLfloat h = hw_h - (hw_h * v);
1895 const GLfloat x = (hw_w - w) * 0.5f;
1896 const GLfloat y = (hw_h - h) * 0.5f;
1897 vtx[0] = x; vtx[1] = y;
1898 vtx[2] = x; vtx[3] = y + h;
1899 vtx[4] = x + w; vtx[5] = y + h;
1900 vtx[6] = x + w; vtx[7] = y;
1901 }
1902 };
1903
1904 class h_stretch {
1905 const GLfloat hw_w, hw_h;
1906 public:
1907 h_stretch(uint32_t hw_w, uint32_t hw_h)
1908 : hw_w(hw_w), hw_h(hw_h) {
1909 }
1910 void operator()(GLfloat* vtx, float v) {
1911 const GLfloat w = hw_w - (hw_w * v);
1912 const GLfloat h = 1.0f;
1913 const GLfloat x = (hw_w - w) * 0.5f;
1914 const GLfloat y = (hw_h - h) * 0.5f;
1915 vtx[0] = x; vtx[1] = y;
1916 vtx[2] = x; vtx[3] = y + h;
1917 vtx[4] = x + w; vtx[5] = y + h;
1918 vtx[6] = x + w; vtx[7] = y;
1919 }
1920 };
1921
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07001922 // the full animation is 12 frames
1923 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001924 s_curve_interpolator itr(nbFrames, 7.5f);
1925 s_curve_interpolator itg(nbFrames, 8.0f);
1926 s_curve_interpolator itb(nbFrames, 8.5f);
1927
1928 h_stretch hverts(hw_w, hw_h);
1929 glDisable(GL_BLEND);
1930 glDisable(GL_TEXTURE_2D);
1931 glColorMask(1,1,1,1);
1932 for (int i=nbFrames-1 ; i>=0 ; i--) {
1933 const float v = itg(i);
1934 hverts(vtx, v);
1935 glClear(GL_COLOR_BUFFER_BIT);
1936 glColor4f(1-v, 1-v, 1-v, 1);
1937 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1938 hw.flip(screenBounds);
1939 }
1940
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07001941 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001942 v_stretch vverts(hw_w, hw_h);
1943 glEnable(GL_BLEND);
1944 glBlendFunc(GL_ONE, GL_ONE);
1945 for (int i=nbFrames-1 ; i>=0 ; i--) {
1946 float x, y, w, h;
1947 const float vr = itr(i);
1948 const float vg = itg(i);
1949 const float vb = itb(i);
1950
1951 // clear screen
1952 glColorMask(1,1,1,1);
1953 glClear(GL_COLOR_BUFFER_BIT);
1954 glEnable(GL_TEXTURE_2D);
1955
1956 // draw the red plane
1957 vverts(vtx, vr);
1958 glColorMask(1,0,0,1);
1959 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1960
1961 // draw the green plane
1962 vverts(vtx, vg);
1963 glColorMask(0,1,0,1);
1964 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1965
1966 // draw the blue plane
1967 vverts(vtx, vb);
1968 glColorMask(0,0,1,1);
1969 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1970
1971 hw.flip(screenBounds);
1972 }
1973
1974 glColorMask(1,1,1,1);
1975 glEnable(GL_SCISSOR_TEST);
1976 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001977 glDeleteTextures(1, &tname);
1978
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001979 return NO_ERROR;
1980}
1981
1982// ---------------------------------------------------------------------------
1983
Mathias Agopiand4e03f32010-10-14 14:54:06 -07001984status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001985{
1986 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1987 if (!hw.canDraw()) {
1988 // we're already off
1989 return NO_ERROR;
1990 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07001991 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
1992 electronBeamOffAnimationImplLocked();
1993 }
1994
1995 // always clear the whole screen at the end of the animation
1996 glClearColor(0,0,0,1);
1997 glDisable(GL_SCISSOR_TEST);
1998 glClear(GL_COLOR_BUFFER_BIT);
1999 glEnable(GL_SCISSOR_TEST);
2000 hw.flip( Region(hw.bounds()) );
2001
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002002 hw.setCanDraw(false);
2003 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002004}
2005
2006status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2007{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002008 class MessageTurnElectronBeamOff : public MessageBase {
2009 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002010 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002011 status_t result;
2012 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002013 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2014 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002015 }
2016 status_t getResult() const {
2017 return result;
2018 }
2019 virtual bool handler() {
2020 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002021 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002022 return true;
2023 }
2024 };
2025
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002026 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002027 status_t res = postMessageSync(msg);
2028 if (res == NO_ERROR) {
2029 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002030
2031 // work-around: when the power-manager calls us we activate the
2032 // animation. eventually, the "on" animation will be called
2033 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002034 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002035 }
2036 return res;
2037}
2038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002040
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002041status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002042{
2043 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2044 if (hw.canDraw()) {
2045 // we're already on
2046 return NO_ERROR;
2047 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002048 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2049 electronBeamOnAnimationImplLocked();
2050 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002051 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002052
2053 // make sure to redraw the whole screen when the animation is done
2054 mDirtyRegion.set(hw.bounds());
2055 signalEvent();
2056
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002057 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002058}
2059
2060status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2061{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002062 class MessageTurnElectronBeamOn : public MessageBase {
2063 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002064 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002065 status_t result;
2066 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002067 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2068 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002069 }
2070 status_t getResult() const {
2071 return result;
2072 }
2073 virtual bool handler() {
2074 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002075 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002076 return true;
2077 }
2078 };
2079
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002080 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002081 return NO_ERROR;
2082}
2083
2084// ---------------------------------------------------------------------------
2085
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002086status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2087 sp<IMemoryHeap>* heap,
2088 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002089 uint32_t sw, uint32_t sh,
2090 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002091{
2092 status_t result = PERMISSION_DENIED;
2093
2094 // only one display supported for now
2095 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2096 return BAD_VALUE;
2097
2098 if (!GLExtensions::getInstance().haveFramebufferObject())
2099 return INVALID_OPERATION;
2100
2101 // get screen geometry
2102 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2103 const uint32_t hw_w = hw.getWidth();
2104 const uint32_t hw_h = hw.getHeight();
2105
2106 if ((sw > hw_w) || (sh > hw_h))
2107 return BAD_VALUE;
2108
2109 sw = (!sw) ? hw_w : sw;
2110 sh = (!sh) ? hw_h : sh;
2111 const size_t size = sw * sh * 4;
2112
2113 // make sure to clear all GL error flags
2114 while ( glGetError() != GL_NO_ERROR ) ;
2115
2116 // create a FBO
2117 GLuint name, tname;
2118 glGenRenderbuffersOES(1, &tname);
2119 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2120 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2121 glGenFramebuffersOES(1, &name);
2122 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2123 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2124 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2125
2126 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2127 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2128
2129 // invert everything, b/c glReadPixel() below will invert the FB
2130 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002131 glScissor(0, 0, sw, sh);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002132 glMatrixMode(GL_PROJECTION);
2133 glPushMatrix();
2134 glLoadIdentity();
2135 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2136 glMatrixMode(GL_MODELVIEW);
2137
2138 // redraw the screen entirely...
2139 glClearColor(0,0,0,1);
2140 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002141
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002142 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2143 const size_t count = layers.size();
2144 for (size_t i=0 ; i<count ; ++i) {
2145 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002146 const uint32_t z = layer->drawingState().z;
2147 if (z >= minLayerZ && z <= maxLayerZ) {
2148 layer->drawForSreenShot();
2149 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002150 }
2151
2152 // XXX: this is needed on tegra
2153 glScissor(0, 0, sw, sh);
2154
2155 // check for errors and return screen capture
2156 if (glGetError() != GL_NO_ERROR) {
2157 // error while rendering
2158 result = INVALID_OPERATION;
2159 } else {
2160 // allocate shared memory large enough to hold the
2161 // screen capture
2162 sp<MemoryHeapBase> base(
2163 new MemoryHeapBase(size, 0, "screen-capture") );
2164 void* const ptr = base->getBase();
2165 if (ptr) {
2166 // capture the screen with glReadPixels()
2167 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2168 if (glGetError() == GL_NO_ERROR) {
2169 *heap = base;
2170 *w = sw;
2171 *h = sh;
2172 *f = PIXEL_FORMAT_RGBA_8888;
2173 result = NO_ERROR;
2174 }
2175 } else {
2176 result = NO_MEMORY;
2177 }
2178 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002179 glEnable(GL_SCISSOR_TEST);
2180 glViewport(0, 0, hw_w, hw_h);
2181 glMatrixMode(GL_PROJECTION);
2182 glPopMatrix();
2183 glMatrixMode(GL_MODELVIEW);
2184
2185
2186 } else {
2187 result = BAD_VALUE;
2188 }
2189
2190 // release FBO resources
2191 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2192 glDeleteRenderbuffersOES(1, &tname);
2193 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002194
2195 hw.compositionComplete();
2196
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002197 return result;
2198}
2199
2200
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002201status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2202 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002203 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002204 uint32_t sw, uint32_t sh,
2205 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002206{
2207 // only one display supported for now
2208 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2209 return BAD_VALUE;
2210
2211 if (!GLExtensions::getInstance().haveFramebufferObject())
2212 return INVALID_OPERATION;
2213
2214 class MessageCaptureScreen : public MessageBase {
2215 SurfaceFlinger* flinger;
2216 DisplayID dpy;
2217 sp<IMemoryHeap>* heap;
2218 uint32_t* w;
2219 uint32_t* h;
2220 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002221 uint32_t sw;
2222 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002223 uint32_t minLayerZ;
2224 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002225 status_t result;
2226 public:
2227 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002228 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002229 uint32_t sw, uint32_t sh,
2230 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002231 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002232 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2233 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2234 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002235 {
2236 }
2237 status_t getResult() const {
2238 return result;
2239 }
2240 virtual bool handler() {
2241 Mutex::Autolock _l(flinger->mStateLock);
2242
2243 // if we have secure windows, never allow the screen capture
2244 if (flinger->mSecureFrameBuffer)
2245 return true;
2246
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002247 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002248 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002249
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002250 return true;
2251 }
2252 };
2253
2254 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002255 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002256 status_t res = postMessageSync(msg);
2257 if (res == NO_ERROR) {
2258 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2259 }
2260 return res;
2261}
2262
2263// ---------------------------------------------------------------------------
2264
Mathias Agopian7623da42010-06-01 15:12:58 -07002265sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2266{
2267 sp<Layer> result;
2268 Mutex::Autolock _l(mStateLock);
2269 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2270 return result;
2271}
2272
2273// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274
Jamie Gennis0a53d5c2010-12-20 11:21:07 -08002275sp<GraphicBuffer> SurfaceFlinger::createGraphicBuffer(uint32_t w, uint32_t h,
2276 PixelFormat format, uint32_t usage) const {
2277 // XXX: HACK HACK HACK!!! This should NOT be static, but it is to fix a
2278 // race between SurfaceFlinger unref'ing the buffer and the client ref'ing
2279 // it.
2280 static sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2281 status_t err = graphicBuffer->initCheck();
2282 if (err != 0) {
2283 LOGE("createGraphicBuffer: init check failed: %d", err);
2284 return 0;
2285 } else if (graphicBuffer->handle == 0) {
2286 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2287 return 0;
2288 }
2289 return graphicBuffer;
2290}
2291
2292// ---------------------------------------------------------------------------
2293
Mathias Agopian593c05c2010-06-02 23:28:45 -07002294Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002295 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297}
2298
Mathias Agopian593c05c2010-06-02 23:28:45 -07002299Client::~Client()
2300{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002301 const size_t count = mLayers.size();
2302 for (size_t i=0 ; i<count ; i++) {
2303 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2304 if (layer != 0) {
2305 mFlinger->removeLayer(layer);
2306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
2308}
2309
Mathias Agopian593c05c2010-06-02 23:28:45 -07002310status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002311 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312}
Mathias Agopian1473f462009-04-10 14:24:30 -07002313
Mathias Agopian593c05c2010-06-02 23:28:45 -07002314ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2315{
Mathias Agopian7623da42010-06-01 15:12:58 -07002316 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002317 mLayers.add(name, layer);
2318 return name;
2319}
2320
Mathias Agopian7623da42010-06-01 15:12:58 -07002321void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002322{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002323 // we do a linear search here, because this doesn't happen often
2324 const size_t count = mLayers.size();
2325 for (size_t i=0 ; i<count ; i++) {
2326 if (mLayers.valueAt(i) == layer) {
2327 mLayers.removeItemsAt(i, 1);
2328 break;
2329 }
2330 }
2331}
Mathias Agopian1473f462009-04-10 14:24:30 -07002332sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2333 sp<LayerBaseClient> lbc;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002334 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2335 if (layer != 0) {
2336 lbc = layer.promote();
2337 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002338 }
2339 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340}
2341
Mathias Agopian593c05c2010-06-02 23:28:45 -07002342sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002343 return 0;
2344}
2345ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2346 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002348sp<ISurface> Client::createSurface(
Mathias Agopian7623da42010-06-01 15:12:58 -07002349 ISurfaceComposerClient::surface_data_t* params, int pid,
2350 const String8& name,
2351 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 uint32_t flags)
2353{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002354 return mFlinger->createSurface(this, pid, name, params,
2355 display, w, h, format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002357status_t Client::destroySurface(SurfaceID sid) {
2358 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002360status_t Client::setState(int32_t count, const layer_state_t* states) {
2361 return mFlinger->setClientState(this, count, states);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362}
2363
2364// ---------------------------------------------------------------------------
2365
Mathias Agopian7623da42010-06-01 15:12:58 -07002366UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2367 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2368{
2369 const int pgsize = getpagesize();
2370 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2371
2372 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2373 "SurfaceFlinger Client control-block");
2374
2375 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2376 if (ctrlblk) { // construct the shared structure in-place.
2377 new(ctrlblk) SharedClient;
2378 }
2379}
2380
2381UserClient::~UserClient()
2382{
2383 if (ctrlblk) {
2384 ctrlblk->~SharedClient(); // destroy our shared-structure.
2385 }
2386
2387 /*
2388 * When a UserClient dies, it's unclear what to do exactly.
2389 * We could go ahead and destroy all surfaces linked to that client
2390 * however, it wouldn't be fair to the main Client
2391 * (usually the the window-manager), which might want to re-target
2392 * the layer to another UserClient.
2393 * I think the best is to do nothing, or not much; in most cases the
2394 * WM itself will go ahead and clean things up when it detects a client of
2395 * his has died.
2396 * The remaining question is what to display? currently we keep
2397 * just keep the current buffer.
2398 */
2399}
2400
2401status_t UserClient::initCheck() const {
2402 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2403}
2404
2405void UserClient::detachLayer(const Layer* layer)
2406{
2407 int32_t name = layer->getToken();
2408 if (name >= 0) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002409 int32_t mask = 1LU<<name;
2410 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2411 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2412 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002413 }
2414}
2415
2416sp<IMemoryHeap> UserClient::getControlBlock() const {
2417 return mCblkHeap;
2418}
2419
2420ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2421{
2422 int32_t name = NAME_NOT_FOUND;
2423 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002424 if (layer == 0) {
2425 return name;
2426 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002427
Mathias Agopian5e140102010-06-08 19:54:15 -07002428 // if this layer already has a token, just return it
Mathias Agopian7623da42010-06-01 15:12:58 -07002429 name = layer->getToken();
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002430 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002431 return name;
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002432 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002433
2434 name = 0;
2435 do {
2436 int32_t mask = 1LU<<name;
2437 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2438 // we found and locked that name
Mathias Agopian5e140102010-06-08 19:54:15 -07002439 status_t err = layer->setToken(
2440 const_cast<UserClient*>(this), ctrlblk, name);
2441 if (err != NO_ERROR) {
2442 // free the name
2443 android_atomic_and(~mask, &mBitmap);
2444 name = err;
2445 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002446 break;
2447 }
2448 if (++name > 31)
2449 name = NO_MEMORY;
2450 } while(name >= 0);
2451
Mathias Agopian1debc662010-06-08 15:40:56 -07002452 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2453 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopian7623da42010-06-01 15:12:58 -07002454 return name;
2455}
2456
2457sp<ISurface> UserClient::createSurface(
2458 ISurfaceComposerClient::surface_data_t* params, int pid,
2459 const String8& name,
2460 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2461 uint32_t flags) {
2462 return 0;
2463}
2464status_t UserClient::destroySurface(SurfaceID sid) {
2465 return INVALID_OPERATION;
2466}
2467status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2468 return INVALID_OPERATION;
2469}
2470
2471// ---------------------------------------------------------------------------
2472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473GraphicPlane::GraphicPlane()
2474 : mHw(0)
2475{
2476}
2477
2478GraphicPlane::~GraphicPlane() {
2479 delete mHw;
2480}
2481
2482bool GraphicPlane::initialized() const {
2483 return mHw ? true : false;
2484}
2485
Mathias Agopian66c77a52010-02-08 15:49:35 -08002486int GraphicPlane::getWidth() const {
2487 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488}
2489
Mathias Agopian66c77a52010-02-08 15:49:35 -08002490int GraphicPlane::getHeight() const {
2491 return mHeight;
2492}
2493
2494void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2495{
2496 mHw = hw;
2497
2498 // initialize the display orientation transform.
2499 // it's a constant that should come from the display driver.
2500 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2501 char property[PROPERTY_VALUE_MAX];
2502 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2503 //displayOrientation
2504 switch (atoi(property)) {
2505 case 90:
2506 displayOrientation = ISurfaceComposer::eOrientation90;
2507 break;
2508 case 270:
2509 displayOrientation = ISurfaceComposer::eOrientation270;
2510 break;
2511 }
2512 }
2513
2514 const float w = hw->getWidth();
2515 const float h = hw->getHeight();
2516 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2517 &mDisplayTransform);
2518 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2519 mDisplayWidth = h;
2520 mDisplayHeight = w;
2521 } else {
2522 mDisplayWidth = w;
2523 mDisplayHeight = h;
2524 }
2525
2526 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527}
2528
2529status_t GraphicPlane::orientationToTransfrom(
2530 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002531{
2532 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 switch (orientation) {
2534 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002535 flags = Transform::ROT_0;
2536 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002538 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 break;
2540 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002541 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 break;
2543 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002544 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 break;
2546 default:
2547 return BAD_VALUE;
2548 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002549 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 return NO_ERROR;
2551}
2552
2553status_t GraphicPlane::setOrientation(int orientation)
2554{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 // If the rotation can be handled in hardware, this is where
2556 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002557
2558 const DisplayHardware& hw(displayHardware());
2559 const float w = mDisplayWidth;
2560 const float h = mDisplayHeight;
2561 mWidth = int(w);
2562 mHeight = int(h);
2563
2564 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002565 GraphicPlane::orientationToTransfrom(orientation, w, h,
2566 &orientationTransform);
2567 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2568 mWidth = int(h);
2569 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002571
Mathias Agopian3552f532009-03-27 17:58:20 -07002572 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002573 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 return NO_ERROR;
2575}
2576
2577const DisplayHardware& GraphicPlane::displayHardware() const {
2578 return *mHw;
2579}
2580
Mathias Agopianaab758e2010-10-11 12:37:43 -07002581DisplayHardware& GraphicPlane::editDisplayHardware() {
2582 return *mHw;
2583}
2584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585const Transform& GraphicPlane::transform() const {
2586 return mGlobalTransform;
2587}
2588
Mathias Agopian1473f462009-04-10 14:24:30 -07002589EGLDisplay GraphicPlane::getEGLDisplay() const {
2590 return mHw->getEGLDisplay();
2591}
2592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593// ---------------------------------------------------------------------------
2594
2595}; // namespace android