blob: 49240e8ef18db0085e386631a86bb2e3f96b567a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
50#include "LayerBlur.h"
51#include "LayerBuffer.h"
52#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopiana1ecca92009-05-21 19:21:59 -070058/* ideally AID_GRAPHICS would be in a semi-public header
59 * or there would be a way to map a user/group name to its id
60 */
61#ifndef AID_GRAPHICS
62#define AID_GRAPHICS 1003
63#endif
64
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#define DISPLAY_COUNT 1
66
67namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068// ---------------------------------------------------------------------------
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070SurfaceFlinger::SurfaceFlinger()
71 : BnSurfaceComposer(), Thread(false),
72 mTransactionFlags(0),
73 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070074 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070077 mHardwareTest("android.permission.HARDWARE_TEST"),
78 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070079 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070080 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070082 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 mDeferReleaseConsole(false),
84 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070085 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070087 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugBackground(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070090 mDebugDisableHWC(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070091 mDebugInSwapBuffers(0),
92 mLastSwapBufferTime(0),
93 mDebugInTransaction(0),
94 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070095 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mConsoleSignals(0),
97 mSecureFrameBuffer(0)
98{
99 init();
100}
101
102void SurfaceFlinger::init()
103{
104 LOGI("SurfaceFlinger is starting");
105
106 // debugging stuff...
107 char value[PROPERTY_VALUE_MAX];
108 property_get("debug.sf.showupdates", value, "0");
109 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 property_get("debug.sf.showbackground", value, "0");
111 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112
Mathias Agopian78fd5012010-04-20 14:51:04 -0700113 LOGI_IF(mDebugRegion, "showupdates enabled");
114 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115}
116
117SurfaceFlinger::~SurfaceFlinger()
118{
119 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
123{
124 return graphicPlane(0).displayHardware().getOverlayEngine();
125}
126
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700127sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700129 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
Mathias Agopian7e27f052010-05-28 14:22:23 -0700132sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133{
Mathias Agopian96f08192010-06-02 23:28:45 -0700134 sp<ISurfaceComposerClient> bclient;
135 sp<Client> client(new Client(this));
136 status_t err = client->initCheck();
137 if (err == NO_ERROR) {
138 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 return bclient;
141}
142
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700143sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
144{
145 sp<ISurfaceComposerClient> bclient;
146 sp<UserClient> client(new UserClient(this));
147 status_t err = client->initCheck();
148 if (err == NO_ERROR) {
149 bclient = client;
150 }
151 return bclient;
152}
153
154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
156{
157 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
158 const GraphicPlane& plane(mGraphicPlanes[dpy]);
159 return plane;
160}
161
162GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
163{
164 return const_cast<GraphicPlane&>(
165 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
166}
167
168void SurfaceFlinger::bootFinished()
169{
170 const nsecs_t now = systemTime();
171 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700172 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700174 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175}
176
177void SurfaceFlinger::onFirstRef()
178{
179 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
180
181 // Wait for the main thread to be done with its initialization
182 mReadyToRunBarrier.wait();
183}
184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185static inline uint16_t pack565(int r, int g, int b) {
186 return (r<<11)|(g<<5)|b;
187}
188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189status_t SurfaceFlinger::readyToRun()
190{
191 LOGI( "SurfaceFlinger's main thread ready to run. "
192 "Initializing graphics H/W...");
193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 // we only support one display currently
195 int dpy = 0;
196
197 {
198 // initialize the main display
199 GraphicPlane& plane(graphicPlane(dpy));
200 DisplayHardware* const hw = new DisplayHardware(this, dpy);
201 plane.setDisplayHardware(hw);
202 }
203
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700204 // create the shared control-block
205 mServerHeap = new MemoryHeapBase(4096,
206 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
207 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700208
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700209 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
210 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700211
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700212 new(mServerCblk) surface_flinger_cblk_t;
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 // initialize primary screen
215 // (other display should be initialized in the same manner, but
216 // asynchronously, as they could come and go. None of this is supported
217 // yet).
218 const GraphicPlane& plane(graphicPlane(dpy));
219 const DisplayHardware& hw = plane.displayHardware();
220 const uint32_t w = hw.getWidth();
221 const uint32_t h = hw.getHeight();
222 const uint32_t f = hw.getFormat();
223 hw.makeCurrent();
224
225 // initialize the shared control block
226 mServerCblk->connected |= 1<<dpy;
227 display_cblk_t* dcblk = mServerCblk->displays + dpy;
228 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800229 dcblk->w = plane.getWidth();
230 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 dcblk->format = f;
232 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
233 dcblk->xdpi = hw.getDpiX();
234 dcblk->ydpi = hw.getDpiY();
235 dcblk->fps = hw.getRefreshRate();
236 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237
238 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700240 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 glEnableClientState(GL_VERTEX_ARRAY);
242 glEnable(GL_SCISSOR_TEST);
243 glShadeModel(GL_FLAT);
244 glDisable(GL_DITHER);
245 glDisable(GL_CULL_FACE);
246
247 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
248 const uint16_t g1 = pack565(0x17,0x2f,0x17);
249 const uint16_t textureData[4] = { g0, g1, g1, g0 };
250 glGenTextures(1, &mWormholeTexName);
251 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
252 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
253 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
254 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
255 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
256 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
257 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
258
259 glViewport(0, 0, w, h);
260 glMatrixMode(GL_PROJECTION);
261 glLoadIdentity();
262 glOrthof(0, w, h, 0, 0, 1);
263
264 LayerDim::initDimmer(this, w, h);
265
266 mReadyToRunBarrier.open();
267
268 /*
269 * We're now ready to accept clients...
270 */
271
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700272 // start boot animation
273 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 return NO_ERROR;
276}
277
278// ----------------------------------------------------------------------------
279#if 0
280#pragma mark -
281#pragma mark Events Handler
282#endif
283
284void SurfaceFlinger::waitForEvent()
285{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700286 while (true) {
287 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800288 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700289 if (UNLIKELY(isFrozen())) {
290 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700291 const nsecs_t now = systemTime();
292 if (mFreezeDisplayTime == 0) {
293 mFreezeDisplayTime = now;
294 }
295 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
296 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700297 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700298
Mathias Agopianbb641242010-05-18 17:06:55 -0700299 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800300
301 // see if we timed out
302 if (isFrozen()) {
303 const nsecs_t now = systemTime();
304 nsecs_t frozenTime = (now - mFreezeDisplayTime);
305 if (frozenTime >= freezeDisplayTimeout) {
306 // we timed out and are still frozen
307 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
308 mFreezeDisplay, mFreezeCount);
309 mFreezeDisplayTime = 0;
310 mFreezeCount = 0;
311 mFreezeDisplay = false;
312 }
313 }
314
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700315 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700316 switch (msg->what) {
317 case MessageQueue::INVALIDATE:
318 // invalidate message, just return to the main loop
319 return;
320 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 }
323}
324
325void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700326 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327}
328
329void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700330 // this is the IPC call
331 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332}
333
Mathias Agopianbb641242010-05-18 17:06:55 -0700334status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
335 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336{
Mathias Agopianbb641242010-05-18 17:06:55 -0700337 return mEventQueue.postMessage(msg, reltime, flags);
338}
339
340status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
341 nsecs_t reltime, uint32_t flags)
342{
343 status_t res = mEventQueue.postMessage(msg, reltime, flags);
344 if (res == NO_ERROR) {
345 msg->wait();
346 }
347 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348}
349
350// ----------------------------------------------------------------------------
351#if 0
352#pragma mark -
353#pragma mark Main loop
354#endif
355
356bool SurfaceFlinger::threadLoop()
357{
358 waitForEvent();
359
360 // check for transactions
361 if (UNLIKELY(mConsoleSignals)) {
362 handleConsoleEvents();
363 }
364
365 if (LIKELY(mTransactionCount == 0)) {
366 // if we're in a global transaction, don't do anything.
367 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
368 uint32_t transactionFlags = getTransactionFlags(mask);
369 if (LIKELY(transactionFlags)) {
370 handleTransaction(transactionFlags);
371 }
372 }
373
374 // post surfaces (if needed)
375 handlePageFlip();
376
Mathias Agopiana350ff92010-08-10 17:14:02 -0700377 if (UNLIKELY(mHwWorkListDirty)) {
378 // build the h/w work list
379 handleWorkList();
380 }
381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700383 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700385
386 const int index = hw.getCurrentBufferIndex();
387 GraphicLog& logger(GraphicLog::getInstance());
388
389 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390 handleRepaint();
391
Mathias Agopian74faca22009-09-17 16:18:16 -0700392 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700393 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700394 hw.compositionComplete();
395
Mathias Agopian35b48d12010-09-13 22:57:58 -0700396 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700397 postFramebuffer();
398
Mathias Agopianc479e182010-09-15 12:29:18 -0700399 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401
Mathias Agopian35b48d12010-09-13 22:57:58 -0700402 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 } else {
404 // pretend we did the post
405 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 usleep(16667); // 60 fps period
407 }
408 return true;
409}
410
411void SurfaceFlinger::postFramebuffer()
412{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 if (!mInvalidRegion.isEmpty()) {
414 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700415 const nsecs_t now = systemTime();
416 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700418 mLastSwapBufferTime = systemTime() - now;
419 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 }
422}
423
424void SurfaceFlinger::handleConsoleEvents()
425{
426 // something to do with the console
427 const DisplayHardware& hw = graphicPlane(0).displayHardware();
428
429 int what = android_atomic_and(0, &mConsoleSignals);
430 if (what & eConsoleAcquired) {
431 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700432 // this is a temporary work-around, eventually this should be called
433 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700434 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 }
436
Mathias Agopian59119e62010-10-11 12:37:43 -0700437 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700438 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 hw.releaseScreen();
441 }
442
443 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700444 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 hw.releaseScreen();
446 } else {
447 mDeferReleaseConsole = true;
448 }
449 }
450
451 mDirtyRegion.set(hw.bounds());
452}
453
454void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
455{
Mathias Agopian3d579642009-06-04 18:46:21 -0700456 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457
Mathias Agopian4da75192010-08-10 17:19:56 -0700458 /*
459 * Perform and commit the transaction
460 */
461
Mathias Agopian3d579642009-06-04 18:46:21 -0700462 { // scope for the lock
463 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700464 const nsecs_t now = systemTime();
465 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700466 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700467 mLastTransactionTime = systemTime() - now;
468 mDebugInTransaction = 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700469 mHwWorkListDirty = true;
Mathias Agopian4da75192010-08-10 17:19:56 -0700470 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700471 }
472
Mathias Agopian4da75192010-08-10 17:19:56 -0700473 /*
474 * Clean-up all layers that went away
475 * (do this without the lock held)
476 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700477
Mathias Agopian3d579642009-06-04 18:46:21 -0700478 const size_t count = ditchedLayers.size();
479 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700480 if (ditchedLayers[i] != 0) {
481 //LOGD("ditching layer %p", ditchedLayers[i].get());
482 ditchedLayers[i]->ditch();
483 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700484 }
485}
486
487void SurfaceFlinger::handleTransactionLocked(
488 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
489{
490 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 const size_t count = currentLayers.size();
492
493 /*
494 * Traversal of the children
495 * (perform the transaction for each of them if needed)
496 */
497
498 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
499 if (layersNeedTransaction) {
500 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700501 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
503 if (!trFlags) continue;
504
505 const uint32_t flags = layer->doTransaction(0);
506 if (flags & Layer::eVisibleRegion)
507 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 }
509 }
510
511 /*
512 * Perform our own transaction if needed
513 */
514
515 if (transactionFlags & eTransactionNeeded) {
516 if (mCurrentState.orientation != mDrawingState.orientation) {
517 // the orientation has changed, recompute all visible regions
518 // and invalidate everything.
519
520 const int dpy = 0;
521 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700522 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 GraphicPlane& plane(graphicPlane(dpy));
524 plane.setOrientation(orientation);
525
526 // update the shared control block
527 const DisplayHardware& hw(plane.displayHardware());
528 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
529 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800530 dcblk->w = plane.getWidth();
531 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532
533 mVisibleRegionsDirty = true;
534 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 }
536
537 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
538 // freezing or unfreezing the display -> trigger animation if needed
539 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800540 if (mFreezeDisplay)
541 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 }
543
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700544 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
545 // layers have been added
546 mVisibleRegionsDirty = true;
547 }
548
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 // some layers might have been removed, so
550 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700551 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700552 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700554 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700555 const size_t count = previousLayers.size();
556 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 const sp<LayerBase>& layer(previousLayers[i]);
558 if (currentLayers.indexOf( layer ) < 0) {
559 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700560 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700561 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700562 }
563 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 }
566
567 commitTransaction();
568}
569
570sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
571{
572 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
573}
574
575void SurfaceFlinger::computeVisibleRegions(
576 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
577{
578 const GraphicPlane& plane(graphicPlane(0));
579 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700580 const DisplayHardware& hw(plane.displayHardware());
581 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582
583 Region aboveOpaqueLayers;
584 Region aboveCoveredLayers;
585 Region dirty;
586
587 bool secureFrameBuffer = false;
588
589 size_t i = currentLayers.size();
590 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 layer->validateVisibility(planeTransform);
593
594 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700595 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596
Mathias Agopianab028732010-03-16 16:41:46 -0700597 /*
598 * opaqueRegion: area of a surface that is fully opaque.
599 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700601
602 /*
603 * visibleRegion: area of a surface that is visible on screen
604 * and not fully transparent. This is essentially the layer's
605 * footprint minus the opaque regions above it.
606 * Areas covered by a translucent surface are considered visible.
607 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700609
610 /*
611 * coveredRegion: area of a surface that is covered by all
612 * visible regions above it (which includes the translucent areas).
613 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700615
616
617 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700618 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700620 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700622 visibleRegion.andSelf(screenRegion);
623 if (!visibleRegion.isEmpty()) {
624 // Remove the transparent area from the visible region
625 if (translucent) {
626 visibleRegion.subtractSelf(layer->transparentRegionScreen);
627 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628
Mathias Agopianab028732010-03-16 16:41:46 -0700629 // compute the opaque region
630 const int32_t layerOrientation = layer->getOrientation();
631 if (s.alpha==255 && !translucent &&
632 ((layerOrientation & Transform::ROT_INVALID) == false)) {
633 // the opaque region is the layer's footprint
634 opaqueRegion = visibleRegion;
635 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 }
637 }
638
Mathias Agopianab028732010-03-16 16:41:46 -0700639 // Clip the covered region to the visible region
640 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
641
642 // Update aboveCoveredLayers for next (lower) layer
643 aboveCoveredLayers.orSelf(visibleRegion);
644
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 // subtract the opaque region covered by the layers above us
646 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647
648 // compute this layer's dirty region
649 if (layer->contentDirty) {
650 // we need to invalidate the whole region
651 dirty = visibleRegion;
652 // as well, as the old visible region
653 dirty.orSelf(layer->visibleRegionScreen);
654 layer->contentDirty = false;
655 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700656 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700657 * the exposed region consists of two components:
658 * 1) what's VISIBLE now and was COVERED before
659 * 2) what's EXPOSED now less what was EXPOSED before
660 *
661 * note that (1) is conservative, we start with the whole
662 * visible region but only keep what used to be covered by
663 * something -- which mean it may have been exposed.
664 *
665 * (2) handles areas that were not covered by anything but got
666 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700667 */
Mathias Agopianab028732010-03-16 16:41:46 -0700668 const Region newExposed = visibleRegion - coveredRegion;
669 const Region oldVisibleRegion = layer->visibleRegionScreen;
670 const Region oldCoveredRegion = layer->coveredRegionScreen;
671 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
672 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 }
674 dirty.subtractSelf(aboveOpaqueLayers);
675
676 // accumulate to the screen dirty region
677 dirtyRegion.orSelf(dirty);
678
Mathias Agopianab028732010-03-16 16:41:46 -0700679 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700681
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 // Store the visible region is screen space
683 layer->setVisibleRegion(visibleRegion);
684 layer->setCoveredRegion(coveredRegion);
685
Mathias Agopian97011222009-07-28 10:57:27 -0700686 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 if (layer->isSecure() && !visibleRegion.isEmpty()) {
688 secureFrameBuffer = true;
689 }
690 }
691
Mathias Agopian97011222009-07-28 10:57:27 -0700692 // invalidate the areas where a layer was removed
693 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
694 mDirtyRegionRemovedLayer.clear();
695
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 mSecureFrameBuffer = secureFrameBuffer;
697 opaqueRegion = aboveOpaqueLayers;
698}
699
700
701void SurfaceFlinger::commitTransaction()
702{
703 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700704 mResizeTransationPending = false;
705 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706}
707
708void SurfaceFlinger::handlePageFlip()
709{
710 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700711 LayerVector& currentLayers(
712 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 visibleRegions |= lockPageFlip(currentLayers);
714
715 const DisplayHardware& hw = graphicPlane(0).displayHardware();
716 const Region screenRegion(hw.bounds());
717 if (visibleRegions) {
718 Region opaqueRegion;
719 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700720
721 /*
722 * rebuild the visible layer list
723 */
724 mVisibleLayersSortedByZ.clear();
725 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
726 size_t count = currentLayers.size();
727 mVisibleLayersSortedByZ.setCapacity(count);
728 for (size_t i=0 ; i<count ; i++) {
729 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
730 mVisibleLayersSortedByZ.add(currentLayers[i]);
731 }
732
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 mWormholeRegion = screenRegion.subtract(opaqueRegion);
734 mVisibleRegionsDirty = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700735 mHwWorkListDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736 }
737
738 unlockPageFlip(currentLayers);
739 mDirtyRegion.andSelf(screenRegion);
740}
741
742bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
743{
744 bool recomputeVisibleRegions = false;
745 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700746 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700748 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 layer->lockPageFlip(recomputeVisibleRegions);
750 }
751 return recomputeVisibleRegions;
752}
753
754void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
755{
756 const GraphicPlane& plane(graphicPlane(0));
757 const Transform& planeTransform(plane.transform());
758 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700759 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700761 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 layer->unlockPageFlip(planeTransform, mDirtyRegion);
763 }
764}
765
Mathias Agopiana350ff92010-08-10 17:14:02 -0700766void SurfaceFlinger::handleWorkList()
767{
768 mHwWorkListDirty = false;
769 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
770 if (hwc.initCheck() == NO_ERROR) {
771 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
772 const size_t count = currentLayers.size();
773 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700774 hwc_layer_t* const cur(hwc.getLayers());
775 for (size_t i=0 ; cur && i<count ; i++) {
776 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700777 if (mDebugDisableHWC) {
778 cur[i].compositionType = HWC_FRAMEBUFFER;
779 cur[i].flags |= HWC_SKIP_LAYER;
780 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700781 }
782 }
783}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700784
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785void SurfaceFlinger::handleRepaint()
786{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700787 // compute the invalid region
788 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789
790 if (UNLIKELY(mDebugRegion)) {
791 debugFlashRegions();
792 }
793
Mathias Agopianb8a55602009-06-26 19:06:36 -0700794 // set the frame buffer
795 const DisplayHardware& hw(graphicPlane(0).displayHardware());
796 glMatrixMode(GL_MODELVIEW);
797 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798
799 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700800 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
801 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700802 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700803 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
804 // takes a rectangle, we must make sure to update that whole
805 // rectangle in that case
806 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700807 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700808 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 mDirtyRegion.set(mInvalidRegion.bounds());
810 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700811 // in the BUFFER_PRESERVED case, obviously, we can update only
812 // what's needed and nothing more.
813 // NOTE: this is NOT a common case, as preserving the backbuffer
814 // is costly and usually involves copying the whole update back.
815 }
816 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700817 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700818 // We need to redraw the rectangle that will be updated
819 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700820 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700821 // rectangle instead of a region (see DisplayHardware::flip())
822 mDirtyRegion.set(mInvalidRegion.bounds());
823 } else {
824 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 mDirtyRegion.set(hw.bounds());
826 mInvalidRegion = mDirtyRegion;
827 }
828 }
829
830 // compose all surfaces
831 composeSurfaces(mDirtyRegion);
832
833 // clear the dirty regions
834 mDirtyRegion.clear();
835}
836
837void SurfaceFlinger::composeSurfaces(const Region& dirty)
838{
839 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
840 // should never happen unless the window manager has a bug
841 // draw something...
842 drawWormhole();
843 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700844
845 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700846 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700847 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700848
849 const DisplayHardware& hw(graphicPlane(0).displayHardware());
850 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700851 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700852
Mathias Agopian45721772010-08-12 15:03:26 -0700853 LOGE_IF(cur && hwc.getNumLayers() != count,
854 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
855 hwc.getNumLayers(), count);
856
857 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700858 if (hwc.initCheck() == NO_ERROR) {
859 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
860 }
Mathias Agopian45721772010-08-12 15:03:26 -0700861
862 /*
863 * update the per-frame h/w composer data for each layer
864 * and build the transparent region of the FB
865 */
866 Region transparent;
867 if (cur) {
868 for (size_t i=0 ; i<count ; i++) {
869 const sp<LayerBase>& layer(layers[i]);
870 layer->setPerFrameData(&cur[i]);
871 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
872 if (!(layer->needsBlending())) {
873 transparent.orSelf(layer->visibleRegionScreen);
874 }
875 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700876 }
877 err = hwc.prepare();
878 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
879 }
880
Mathias Agopian45721772010-08-12 15:03:26 -0700881 /*
882 * clear the area of the FB that need to be transparent
883 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700884 transparent.andSelf(dirty);
885 if (!transparent.isEmpty()) {
886 glClearColor(0,0,0,0);
887 Region::const_iterator it = transparent.begin();
888 Region::const_iterator const end = transparent.end();
889 const int32_t height = hw.getHeight();
890 while (it != end) {
891 const Rect& r(*it++);
892 const GLint sy = height - (r.top + r.height());
893 glScissor(r.left, sy, r.width(), r.height());
894 glClear(GL_COLOR_BUFFER_BIT);
895 }
896 }
Mathias Agopian45721772010-08-12 15:03:26 -0700897
898
899 /*
900 * and then, render the layers targeted at the framebuffer
901 */
902 for (size_t i=0 ; i<count ; i++) {
903 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700904 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
905 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700906 // skip layers handled by the HAL
907 continue;
908 }
909 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700910
Mathias Agopian45721772010-08-12 15:03:26 -0700911 const sp<LayerBase>& layer(layers[i]);
912 const Region clip(dirty.intersect(layer->visibleRegionScreen));
913 if (!clip.isEmpty()) {
914 layer->draw(clip);
915 }
916 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917}
918
919void SurfaceFlinger::unlockClients()
920{
921 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
922 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700923 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700925 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 layer->finishPageFlip();
927 }
928}
929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930void SurfaceFlinger::debugFlashRegions()
931{
Mathias Agopian0a917752010-06-14 21:20:00 -0700932 const DisplayHardware& hw(graphicPlane(0).displayHardware());
933 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700934
Mathias Agopian0a917752010-06-14 21:20:00 -0700935 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
936 (flags & DisplayHardware::BUFFER_PRESERVED))) {
937 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
938 mDirtyRegion.bounds() : hw.bounds());
939 composeSurfaces(repaint);
940 }
941
942 TextureManager::deactivateTextures();
943
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 glDisable(GL_BLEND);
945 glDisable(GL_DITHER);
946 glDisable(GL_SCISSOR_TEST);
947
Mathias Agopian0926f502009-05-04 14:17:04 -0700948 static int toggle = 0;
949 toggle = 1 - toggle;
950 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700951 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700952 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700953 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700954 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955
Mathias Agopian20f68782009-05-11 00:03:41 -0700956 Region::const_iterator it = mDirtyRegion.begin();
957 Region::const_iterator const end = mDirtyRegion.end();
958 while (it != end) {
959 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 GLfloat vertices[][2] = {
961 { r.left, r.top },
962 { r.left, r.bottom },
963 { r.right, r.bottom },
964 { r.right, r.top }
965 };
966 glVertexPointer(2, GL_FLOAT, 0, vertices);
967 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
968 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700969
Mathias Agopianb8a55602009-06-26 19:06:36 -0700970 if (mInvalidRegion.isEmpty()) {
971 mDirtyRegion.dump("mDirtyRegion");
972 mInvalidRegion.dump("mInvalidRegion");
973 }
974 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975
976 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700977 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978
979 glEnable(GL_SCISSOR_TEST);
980 //mDirtyRegion.dump("mDirtyRegion");
981}
982
983void SurfaceFlinger::drawWormhole() const
984{
985 const Region region(mWormholeRegion.intersect(mDirtyRegion));
986 if (region.isEmpty())
987 return;
988
989 const DisplayHardware& hw(graphicPlane(0).displayHardware());
990 const int32_t width = hw.getWidth();
991 const int32_t height = hw.getHeight();
992
993 glDisable(GL_BLEND);
994 glDisable(GL_DITHER);
995
996 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700997 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700998 Region::const_iterator it = region.begin();
999 Region::const_iterator const end = region.end();
1000 while (it != end) {
1001 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 const GLint sy = height - (r.top + r.height());
1003 glScissor(r.left, sy, r.width(), r.height());
1004 glClear(GL_COLOR_BUFFER_BIT);
1005 }
1006 } else {
1007 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1008 { width, height }, { 0, height } };
1009 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1010 glVertexPointer(2, GL_SHORT, 0, vertices);
1011 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1012 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001013#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001014 if (GLExtensions::getInstance().haveTextureExternal()) {
1015 glDisable(GL_TEXTURE_EXTERNAL_OES);
1016 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001017#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 glEnable(GL_TEXTURE_2D);
1019 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1020 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1021 glMatrixMode(GL_TEXTURE);
1022 glLoadIdentity();
1023 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001024 Region::const_iterator it = region.begin();
1025 Region::const_iterator const end = region.end();
1026 while (it != end) {
1027 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 const GLint sy = height - (r.top + r.height());
1029 glScissor(r.left, sy, r.width(), r.height());
1030 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1031 }
1032 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1033 }
1034}
1035
1036void SurfaceFlinger::debugShowFPS() const
1037{
1038 static int mFrameCount;
1039 static int mLastFrameCount = 0;
1040 static nsecs_t mLastFpsTime = 0;
1041 static float mFps = 0;
1042 mFrameCount++;
1043 nsecs_t now = systemTime();
1044 nsecs_t diff = now - mLastFpsTime;
1045 if (diff > ms2ns(250)) {
1046 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1047 mLastFpsTime = now;
1048 mLastFrameCount = mFrameCount;
1049 }
1050 // XXX: mFPS has the value we want
1051 }
1052
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001053status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054{
1055 Mutex::Autolock _l(mStateLock);
1056 addLayer_l(layer);
1057 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1058 return NO_ERROR;
1059}
1060
Mathias Agopian96f08192010-06-02 23:28:45 -07001061status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1062{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001063 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001064 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1065}
1066
1067ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1068 const sp<LayerBaseClient>& lbc)
1069{
1070 Mutex::Autolock _l(mStateLock);
1071
1072 // attach this layer to the client
1073 ssize_t name = client->attachLayer(lbc);
1074
1075 // add this layer to the current state list
1076 addLayer_l(lbc);
1077
1078 return name;
1079}
1080
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001081status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082{
1083 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001084 status_t err = purgatorizeLayer_l(layer);
1085 if (err == NO_ERROR)
1086 setTransactionFlags(eTransactionNeeded);
1087 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088}
1089
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001090status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001092 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1093 if (lbc != 0) {
1094 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1095 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1097 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001098 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 return NO_ERROR;
1100 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001101 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102}
1103
Mathias Agopian9a112062009-04-17 19:36:26 -07001104status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1105{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001106 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001107 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001108
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001109 layerBase->onRemoved();
1110
Mathias Agopian3d579642009-06-04 18:46:21 -07001111 // it's possible that we don't find a layer, because it might
1112 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001113 // from the user because there is a race between Client::destroySurface(),
1114 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001115 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1116}
1117
Mathias Agopian96f08192010-06-02 23:28:45 -07001118status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119{
Mathias Agopian96f08192010-06-02 23:28:45 -07001120 layer->forceVisibilityTransaction();
1121 setTransactionFlags(eTraversalNeeded);
1122 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123}
1124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1126{
1127 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1128}
1129
Mathias Agopianbb641242010-05-18 17:06:55 -07001130uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131{
1132 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1133 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001134 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 }
1136 return old;
1137}
1138
1139void SurfaceFlinger::openGlobalTransaction()
1140{
1141 android_atomic_inc(&mTransactionCount);
1142}
1143
1144void SurfaceFlinger::closeGlobalTransaction()
1145{
1146 if (android_atomic_dec(&mTransactionCount) == 1) {
1147 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001148
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001149 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001150 // take effect before returning.
1151 Mutex::Autolock _l(mStateLock);
1152 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001153 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1154 if (CC_UNLIKELY(err != NO_ERROR)) {
1155 // just in case something goes wrong in SF, return to the
1156 // called after a few seconds.
1157 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1158 mResizeTransationPending = false;
1159 break;
1160 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 }
1163}
1164
1165status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1166{
1167 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1168 return BAD_VALUE;
1169
1170 Mutex::Autolock _l(mStateLock);
1171 mCurrentState.freezeDisplay = 1;
1172 setTransactionFlags(eTransactionNeeded);
1173
1174 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001175 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 return NO_ERROR;
1177}
1178
1179status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1180{
1181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1182 return BAD_VALUE;
1183
1184 Mutex::Autolock _l(mStateLock);
1185 mCurrentState.freezeDisplay = 0;
1186 setTransactionFlags(eTransactionNeeded);
1187
1188 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001189 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 return NO_ERROR;
1191}
1192
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001193int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001194 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195{
1196 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1197 return BAD_VALUE;
1198
1199 Mutex::Autolock _l(mStateLock);
1200 if (mCurrentState.orientation != orientation) {
1201 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001202 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 mCurrentState.orientation = orientation;
1204 setTransactionFlags(eTransactionNeeded);
1205 mTransactionCV.wait(mStateLock);
1206 } else {
1207 orientation = BAD_VALUE;
1208 }
1209 }
1210 return orientation;
1211}
1212
Mathias Agopian96f08192010-06-02 23:28:45 -07001213sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001214 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1216 uint32_t flags)
1217{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001218 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001220
1221 if (int32_t(w|h) < 0) {
1222 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1223 int(w), int(h));
1224 return surfaceHandle;
1225 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001228 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 switch (flags & eFXSurfaceMask) {
1230 case eFXSurfaceNormal:
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001231#if HAS_PUSH_BUFFERS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001233 layer = createPushBuffersSurface(client, d, w, h, flags);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001234 } else
1235#endif
1236 {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001237 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1238 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 }
1240 break;
1241 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001242 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 break;
1244 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001245 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 break;
1247 }
1248
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001249 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001250 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001251 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001252 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001255 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001256 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001257 params->identity = surfaceHandle->getIdentity();
1258 params->width = w;
1259 params->height = h;
1260 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001261 if (normalLayer != 0) {
1262 Mutex::Autolock _l(mStateLock);
1263 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1264 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001265 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001266
Mathias Agopian96f08192010-06-02 23:28:45 -07001267 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 }
1269
1270 return surfaceHandle;
1271}
1272
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001273sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001274 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001275 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001276 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277{
1278 // initialize the surfaces
1279 switch (format) { // TODO: take h/w into account
1280 case PIXEL_FORMAT_TRANSPARENT:
1281 case PIXEL_FORMAT_TRANSLUCENT:
1282 format = PIXEL_FORMAT_RGBA_8888;
1283 break;
1284 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001285#ifdef NO_RGBX_8888
1286 format = PIXEL_FORMAT_RGB_565;
1287#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001288 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001289#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 break;
1291 }
1292
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001293#ifdef NO_RGBX_8888
1294 if (format == PIXEL_FORMAT_RGBX_8888)
1295 format = PIXEL_FORMAT_RGBA_8888;
1296#endif
1297
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001299 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001300 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001302 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 }
1304 return layer;
1305}
1306
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001307sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001308 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001309 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310{
Mathias Agopian96f08192010-06-02 23:28:45 -07001311 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 return layer;
1314}
1315
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001316sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001317 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319{
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 return layer;
1323}
1324
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001325sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001326 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001327 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
Mathias Agopian96f08192010-06-02 23:28:45 -07001329 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 return layer;
1332}
1333
Mathias Agopian96f08192010-06-02 23:28:45 -07001334status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335{
Mathias Agopian9a112062009-04-17 19:36:26 -07001336 /*
1337 * called by the window manager, when a surface should be marked for
1338 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001339 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 * The surface is removed from the current and drawing lists, but placed
1341 * in the purgatory queue, so it's not destroyed right-away (we need
1342 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001343 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001344
Mathias Agopian48d819a2009-09-10 19:41:18 -07001345 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001346 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001347 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001348 if (layer != 0) {
1349 err = purgatorizeLayer_l(layer);
1350 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001351 setTransactionFlags(eTransactionNeeded);
1352 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001353 }
1354 return err;
1355}
1356
1357status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1358{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001359 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001360
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001361 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001362 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001363 sp<LayerBaseClient> layer;
1364 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001365 MessageDestroySurface(
1366 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1367 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001368 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001369 sp<LayerBaseClient> l(layer);
1370 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001371 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001372 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001373 * remove the layer from the current list -- chances are that it's
1374 * not in the list anyway, because it should have been removed
1375 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001376 * However, a buggy client could have not done that.
1377 * Since we know we don't have any more clients, we don't need
1378 * to use the purgatory.
1379 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001380 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001381 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1382 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001383 return true;
1384 }
1385 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001386
Mathias Agopianbb641242010-05-18 17:06:55 -07001387 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 return NO_ERROR;
1389}
1390
1391status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001392 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 int32_t count,
1394 const layer_state_t* states)
1395{
1396 Mutex::Autolock _l(mStateLock);
1397 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001399 const layer_state_t& s(states[i]);
1400 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001401 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403 if (what & ePositionChanged) {
1404 if (layer->setPosition(s.x, s.y))
1405 flags |= eTraversalNeeded;
1406 }
1407 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001408 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001410 mCurrentState.layersSortedByZ.removeAt(idx);
1411 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 // we need traversal (state changed)
1413 // AND transaction (list changed)
1414 flags |= eTransactionNeeded|eTraversalNeeded;
1415 }
1416 }
1417 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001418 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001420 mResizeTransationPending = true;
1421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 }
1423 if (what & eAlphaChanged) {
1424 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1425 flags |= eTraversalNeeded;
1426 }
1427 if (what & eMatrixChanged) {
1428 if (layer->setMatrix(s.matrix))
1429 flags |= eTraversalNeeded;
1430 }
1431 if (what & eTransparentRegionChanged) {
1432 if (layer->setTransparentRegionHint(s.transparentRegion))
1433 flags |= eTraversalNeeded;
1434 }
1435 if (what & eVisibilityChanged) {
1436 if (layer->setFlags(s.flags, s.mask))
1437 flags |= eTraversalNeeded;
1438 }
1439 }
1440 }
1441 if (flags) {
1442 setTransactionFlags(flags);
1443 }
1444 return NO_ERROR;
1445}
1446
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447void SurfaceFlinger::screenReleased(int dpy)
1448{
1449 // this may be called by a signal handler, we can't do too much in here
1450 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1451 signalEvent();
1452}
1453
1454void SurfaceFlinger::screenAcquired(int dpy)
1455{
1456 // this may be called by a signal handler, we can't do too much in here
1457 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1458 signalEvent();
1459}
1460
1461status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1462{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001463 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 char buffer[SIZE];
1465 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001466 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 snprintf(buffer, SIZE, "Permission Denial: "
1468 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1469 IPCThreadState::self()->getCallingPid(),
1470 IPCThreadState::self()->getCallingUid());
1471 result.append(buffer);
1472 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001473
1474 // figure out if we're stuck somewhere
1475 const nsecs_t now = systemTime();
1476 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1477 const nsecs_t inTransaction(mDebugInTransaction);
1478 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1479 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1480
1481 // Try to get the main lock, but don't insist if we can't
1482 // (this would indicate SF is stuck, but we want to be able to
1483 // print something in dumpsys).
1484 int retry = 3;
1485 while (mStateLock.tryLock()<0 && --retry>=0) {
1486 usleep(1000000);
1487 }
1488 const bool locked(retry >= 0);
1489 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001490 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001491 "SurfaceFlinger appears to be unresponsive, "
1492 "dumping anyways (no locks held)\n");
1493 result.append(buffer);
1494 }
1495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1497 const size_t count = currentLayers.size();
1498 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001499 const sp<LayerBase>& layer(currentLayers[i]);
1500 layer->dump(result, buffer, SIZE);
1501 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 s.transparentRegion.dump(result, "transparentRegion");
1503 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1504 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1505 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 mWormholeRegion.dump(result, "WormholeRegion");
1508 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1509 snprintf(buffer, SIZE,
1510 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1511 mFreezeDisplay?"yes":"no", mFreezeCount,
1512 mCurrentState.orientation, hw.canDraw());
1513 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001514 snprintf(buffer, SIZE,
1515 " last eglSwapBuffers() time: %f us\n"
1516 " last transaction time : %f us\n",
1517 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1518 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001519
Mathias Agopian9795c422009-08-26 16:36:26 -07001520 if (inSwapBuffersDuration || !locked) {
1521 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1522 inSwapBuffersDuration/1000.0);
1523 result.append(buffer);
1524 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001525
Mathias Agopian9795c422009-08-26 16:36:26 -07001526 if (inTransactionDuration || !locked) {
1527 snprintf(buffer, SIZE, " transaction time: %f us\n",
1528 inTransactionDuration/1000.0);
1529 result.append(buffer);
1530 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001531
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001532 HWComposer& hwc(hw.getHwComposer());
1533 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1534 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1535 mDebugDisableHWC ? "disabled" : "enabled");
1536 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001537 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001538
Mathias Agopian3330b202009-10-05 17:07:12 -07001539 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001540 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001541 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001542
1543 if (locked) {
1544 mStateLock.unlock();
1545 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 }
1547 write(fd, result.string(), result.size());
1548 return NO_ERROR;
1549}
1550
1551status_t SurfaceFlinger::onTransact(
1552 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1553{
1554 switch (code) {
1555 case CREATE_CONNECTION:
1556 case OPEN_GLOBAL_TRANSACTION:
1557 case CLOSE_GLOBAL_TRANSACTION:
1558 case SET_ORIENTATION:
1559 case FREEZE_DISPLAY:
1560 case UNFREEZE_DISPLAY:
1561 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001562 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001563 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 {
1565 // codes that require permission check
1566 IPCThreadState* ipc = IPCThreadState::self();
1567 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001568 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001569 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1570 LOGE("Permission Denial: "
1571 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1572 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001574 break;
1575 }
1576 case CAPTURE_SCREEN:
1577 {
1578 // codes that require permission check
1579 IPCThreadState* ipc = IPCThreadState::self();
1580 const int pid = ipc->getCallingPid();
1581 const int uid = ipc->getCallingUid();
1582 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1583 LOGE("Permission Denial: "
1584 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1585 return PERMISSION_DENIED;
1586 }
1587 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 }
1589 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1592 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001593 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001594 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1595 IPCThreadState* ipc = IPCThreadState::self();
1596 const int pid = ipc->getCallingPid();
1597 const int uid = ipc->getCallingUid();
1598 LOGE("Permission Denial: "
1599 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 return PERMISSION_DENIED;
1601 }
1602 int n;
1603 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001604 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001605 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 return NO_ERROR;
1607 case 1002: // SHOW_UPDATES
1608 n = data.readInt32();
1609 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1610 return NO_ERROR;
1611 case 1003: // SHOW_BACKGROUND
1612 n = data.readInt32();
1613 mDebugBackground = n ? 1 : 0;
1614 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001615 case 1008: // toggle use of hw composer
1616 n = data.readInt32();
1617 mDebugDisableHWC = n ? 1 : 0;
1618 mHwWorkListDirty = true;
1619 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 case 1004:{ // repaint everything
1621 Mutex::Autolock _l(mStateLock);
1622 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1623 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1624 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001625 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001627 case 1005:{ // force transaction
1628 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1629 return NO_ERROR;
1630 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001631 case 1006:{ // enable/disable GraphicLog
1632 int enabled = data.readInt32();
1633 GraphicLog::getInstance().setEnabled(enabled);
1634 return NO_ERROR;
1635 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 case 1007: // set mFreezeCount
1637 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001638 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 return NO_ERROR;
1640 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001641 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 reply->writeInt32(0);
1643 reply->writeInt32(mDebugRegion);
1644 reply->writeInt32(mDebugBackground);
1645 return NO_ERROR;
1646 case 1013: {
1647 Mutex::Autolock _l(mStateLock);
1648 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1649 reply->writeInt32(hw.getPageFlipCount());
1650 }
1651 return NO_ERROR;
1652 }
1653 }
1654 return err;
1655}
1656
Mathias Agopian59119e62010-10-11 12:37:43 -07001657// ---------------------------------------------------------------------------
1658
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001659status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1660 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001661{
Mathias Agopian59119e62010-10-11 12:37:43 -07001662 if (!GLExtensions::getInstance().haveFramebufferObject())
1663 return INVALID_OPERATION;
1664
1665 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001666 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001667 const uint32_t hw_w = hw.getWidth();
1668 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001669 GLfloat u = 1;
1670 GLfloat v = 1;
1671
1672 // make sure to clear all GL error flags
1673 while ( glGetError() != GL_NO_ERROR ) ;
1674
1675 // create a FBO
1676 GLuint name, tname;
1677 glGenTextures(1, &tname);
1678 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001679 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1680 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001681 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001682 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001683 GLint tw = (2 << (31 - clz(hw_w)));
1684 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001685 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1686 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001687 u = GLfloat(hw_w) / tw;
1688 v = GLfloat(hw_h) / th;
1689 }
1690 glGenFramebuffersOES(1, &name);
1691 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001692 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1693 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001694
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001695 // redraw the screen entirely...
1696 glClearColor(0,0,0,1);
1697 glClear(GL_COLOR_BUFFER_BIT);
1698 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1699 const size_t count = layers.size();
1700 for (size_t i=0 ; i<count ; ++i) {
1701 const sp<LayerBase>& layer(layers[i]);
1702 layer->drawForSreenShot();
1703 }
1704
1705 // back to main framebuffer
1706 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1707 glDisable(GL_SCISSOR_TEST);
1708 glDeleteFramebuffersOES(1, &name);
1709
1710 *textureName = tname;
1711 *uOut = u;
1712 *vOut = v;
1713 return NO_ERROR;
1714}
1715
1716// ---------------------------------------------------------------------------
1717
1718status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1719{
1720 status_t result = PERMISSION_DENIED;
1721
1722 if (!GLExtensions::getInstance().haveFramebufferObject())
1723 return INVALID_OPERATION;
1724
1725 // get screen geometry
1726 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1727 const uint32_t hw_w = hw.getWidth();
1728 const uint32_t hw_h = hw.getHeight();
1729 const Region screenBounds(hw.bounds());
1730
1731 GLfloat u, v;
1732 GLuint tname;
1733 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1734 if (result != NO_ERROR) {
1735 return result;
1736 }
1737
1738 GLfloat vtx[8];
1739 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1740 glEnable(GL_TEXTURE_2D);
1741 glBindTexture(GL_TEXTURE_2D, tname);
1742 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1743 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1744 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1745 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1746 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1747 glVertexPointer(2, GL_FLOAT, 0, vtx);
1748
1749 class s_curve_interpolator {
1750 const float nbFrames, s, v;
1751 public:
1752 s_curve_interpolator(int nbFrames, float s)
1753 : nbFrames(1.0f / (nbFrames-1)), s(s),
1754 v(1.0f + expf(-s + 0.5f*s)) {
1755 }
1756 float operator()(int f) {
1757 const float x = f * nbFrames;
1758 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1759 }
1760 };
1761
1762 class v_stretch {
1763 const GLfloat hw_w, hw_h;
1764 public:
1765 v_stretch(uint32_t hw_w, uint32_t hw_h)
1766 : hw_w(hw_w), hw_h(hw_h) {
1767 }
1768 void operator()(GLfloat* vtx, float v) {
1769 const GLfloat w = hw_w + (hw_w * v);
1770 const GLfloat h = hw_h - (hw_h * v);
1771 const GLfloat x = (hw_w - w) * 0.5f;
1772 const GLfloat y = (hw_h - h) * 0.5f;
1773 vtx[0] = x; vtx[1] = y;
1774 vtx[2] = x; vtx[3] = y + h;
1775 vtx[4] = x + w; vtx[5] = y + h;
1776 vtx[6] = x + w; vtx[7] = y;
1777 }
1778 };
1779
1780 class h_stretch {
1781 const GLfloat hw_w, hw_h;
1782 public:
1783 h_stretch(uint32_t hw_w, uint32_t hw_h)
1784 : hw_w(hw_w), hw_h(hw_h) {
1785 }
1786 void operator()(GLfloat* vtx, float v) {
1787 const GLfloat w = hw_w - (hw_w * v);
1788 const GLfloat h = 1.0f;
1789 const GLfloat x = (hw_w - w) * 0.5f;
1790 const GLfloat y = (hw_h - h) * 0.5f;
1791 vtx[0] = x; vtx[1] = y;
1792 vtx[2] = x; vtx[3] = y + h;
1793 vtx[4] = x + w; vtx[5] = y + h;
1794 vtx[6] = x + w; vtx[7] = y;
1795 }
1796 };
1797
1798 // the full animation is 24 frames
1799 const int nbFrames = 12;
1800 s_curve_interpolator itr(nbFrames, 7.5f);
1801 s_curve_interpolator itg(nbFrames, 8.0f);
1802 s_curve_interpolator itb(nbFrames, 8.5f);
1803
1804 v_stretch vverts(hw_w, hw_h);
1805 glEnable(GL_BLEND);
1806 glBlendFunc(GL_ONE, GL_ONE);
1807 for (int i=0 ; i<nbFrames ; i++) {
1808 float x, y, w, h;
1809 const float vr = itr(i);
1810 const float vg = itg(i);
1811 const float vb = itb(i);
1812
1813 // clear screen
1814 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001815 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001816 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001817
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001818 // draw the red plane
1819 vverts(vtx, vr);
1820 glColorMask(1,0,0,1);
1821 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001822
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001823 // draw the green plane
1824 vverts(vtx, vg);
1825 glColorMask(0,1,0,1);
1826 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001827
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001828 // draw the blue plane
1829 vverts(vtx, vb);
1830 glColorMask(0,0,1,1);
1831 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001832
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001833 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001834 glDisable(GL_TEXTURE_2D);
1835 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001836 glColor4f(vg, vg, vg, 1);
1837 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1838 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001839 }
1840
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001841 h_stretch hverts(hw_w, hw_h);
1842 glDisable(GL_BLEND);
1843 glDisable(GL_TEXTURE_2D);
1844 glColorMask(1,1,1,1);
1845 for (int i=0 ; i<nbFrames ; i++) {
1846 const float v = itg(i);
1847 hverts(vtx, v);
1848 glClear(GL_COLOR_BUFFER_BIT);
1849 glColor4f(1-v, 1-v, 1-v, 1);
1850 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1851 hw.flip(screenBounds);
1852 }
1853
1854 glColorMask(1,1,1,1);
1855 glEnable(GL_SCISSOR_TEST);
1856 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1857 glDeleteTextures(1, &tname);
1858 return NO_ERROR;
1859}
1860
1861status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1862{
1863 status_t result = PERMISSION_DENIED;
1864
1865 if (!GLExtensions::getInstance().haveFramebufferObject())
1866 return INVALID_OPERATION;
1867
1868
1869 // get screen geometry
1870 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1871 const uint32_t hw_w = hw.getWidth();
1872 const uint32_t hw_h = hw.getHeight();
1873 const Region screenBounds(hw.bounds());
1874
1875 GLfloat u, v;
1876 GLuint tname;
1877 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1878 if (result != NO_ERROR) {
1879 return result;
1880 }
1881
1882 // back to main framebuffer
1883 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1884 glDisable(GL_SCISSOR_TEST);
1885
1886 GLfloat vtx[8];
1887 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1888 glEnable(GL_TEXTURE_2D);
1889 glBindTexture(GL_TEXTURE_2D, tname);
1890 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1891 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1892 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1893 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1894 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1895 glVertexPointer(2, GL_FLOAT, 0, vtx);
1896
1897 class s_curve_interpolator {
1898 const float nbFrames, s, v;
1899 public:
1900 s_curve_interpolator(int nbFrames, float s)
1901 : nbFrames(1.0f / (nbFrames-1)), s(s),
1902 v(1.0f + expf(-s + 0.5f*s)) {
1903 }
1904 float operator()(int f) {
1905 const float x = f * nbFrames;
1906 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1907 }
1908 };
1909
1910 class v_stretch {
1911 const GLfloat hw_w, hw_h;
1912 public:
1913 v_stretch(uint32_t hw_w, uint32_t hw_h)
1914 : hw_w(hw_w), hw_h(hw_h) {
1915 }
1916 void operator()(GLfloat* vtx, float v) {
1917 const GLfloat w = hw_w + (hw_w * v);
1918 const GLfloat h = hw_h - (hw_h * v);
1919 const GLfloat x = (hw_w - w) * 0.5f;
1920 const GLfloat y = (hw_h - h) * 0.5f;
1921 vtx[0] = x; vtx[1] = y;
1922 vtx[2] = x; vtx[3] = y + h;
1923 vtx[4] = x + w; vtx[5] = y + h;
1924 vtx[6] = x + w; vtx[7] = y;
1925 }
1926 };
1927
1928 class h_stretch {
1929 const GLfloat hw_w, hw_h;
1930 public:
1931 h_stretch(uint32_t hw_w, uint32_t hw_h)
1932 : hw_w(hw_w), hw_h(hw_h) {
1933 }
1934 void operator()(GLfloat* vtx, float v) {
1935 const GLfloat w = hw_w - (hw_w * v);
1936 const GLfloat h = 1.0f;
1937 const GLfloat x = (hw_w - w) * 0.5f;
1938 const GLfloat y = (hw_h - h) * 0.5f;
1939 vtx[0] = x; vtx[1] = y;
1940 vtx[2] = x; vtx[3] = y + h;
1941 vtx[4] = x + w; vtx[5] = y + h;
1942 vtx[6] = x + w; vtx[7] = y;
1943 }
1944 };
1945
Mathias Agopiana6546e52010-10-14 12:33:07 -07001946 // the full animation is 12 frames
1947 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001948 s_curve_interpolator itr(nbFrames, 7.5f);
1949 s_curve_interpolator itg(nbFrames, 8.0f);
1950 s_curve_interpolator itb(nbFrames, 8.5f);
1951
1952 h_stretch hverts(hw_w, hw_h);
1953 glDisable(GL_BLEND);
1954 glDisable(GL_TEXTURE_2D);
1955 glColorMask(1,1,1,1);
1956 for (int i=nbFrames-1 ; i>=0 ; i--) {
1957 const float v = itg(i);
1958 hverts(vtx, v);
1959 glClear(GL_COLOR_BUFFER_BIT);
1960 glColor4f(1-v, 1-v, 1-v, 1);
1961 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1962 hw.flip(screenBounds);
1963 }
1964
Mathias Agopiana6546e52010-10-14 12:33:07 -07001965 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001966 v_stretch vverts(hw_w, hw_h);
1967 glEnable(GL_BLEND);
1968 glBlendFunc(GL_ONE, GL_ONE);
1969 for (int i=nbFrames-1 ; i>=0 ; i--) {
1970 float x, y, w, h;
1971 const float vr = itr(i);
1972 const float vg = itg(i);
1973 const float vb = itb(i);
1974
1975 // clear screen
1976 glColorMask(1,1,1,1);
1977 glClear(GL_COLOR_BUFFER_BIT);
1978 glEnable(GL_TEXTURE_2D);
1979
1980 // draw the red plane
1981 vverts(vtx, vr);
1982 glColorMask(1,0,0,1);
1983 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1984
1985 // draw the green plane
1986 vverts(vtx, vg);
1987 glColorMask(0,1,0,1);
1988 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1989
1990 // draw the blue plane
1991 vverts(vtx, vb);
1992 glColorMask(0,0,1,1);
1993 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1994
1995 hw.flip(screenBounds);
1996 }
1997
1998 glColorMask(1,1,1,1);
1999 glEnable(GL_SCISSOR_TEST);
2000 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002001 glDeleteTextures(1, &tname);
2002
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002003 return NO_ERROR;
2004}
2005
2006// ---------------------------------------------------------------------------
2007
Mathias Agopianabd671a2010-10-14 14:54:06 -07002008status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009{
2010 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2011 if (!hw.canDraw()) {
2012 // we're already off
2013 return NO_ERROR;
2014 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002015 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2016 electronBeamOffAnimationImplLocked();
2017 }
2018
2019 // always clear the whole screen at the end of the animation
2020 glClearColor(0,0,0,1);
2021 glDisable(GL_SCISSOR_TEST);
2022 glClear(GL_COLOR_BUFFER_BIT);
2023 glEnable(GL_SCISSOR_TEST);
2024 hw.flip( Region(hw.bounds()) );
2025
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002026 hw.setCanDraw(false);
2027 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002028}
2029
2030status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2031{
Mathias Agopian59119e62010-10-11 12:37:43 -07002032 class MessageTurnElectronBeamOff : public MessageBase {
2033 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002034 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002035 status_t result;
2036 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002037 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2038 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002039 }
2040 status_t getResult() const {
2041 return result;
2042 }
2043 virtual bool handler() {
2044 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002045 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002046 return true;
2047 }
2048 };
2049
Mathias Agopianabd671a2010-10-14 14:54:06 -07002050 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002051 status_t res = postMessageSync(msg);
2052 if (res == NO_ERROR) {
2053 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054
2055 // work-around: when the power-manager calls us we activate the
2056 // animation. eventually, the "on" animation will be called
2057 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002058 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002059 }
2060 return res;
2061}
2062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002063// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002064
Mathias Agopianabd671a2010-10-14 14:54:06 -07002065status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002066{
2067 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2068 if (hw.canDraw()) {
2069 // we're already on
2070 return NO_ERROR;
2071 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002072 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2073 electronBeamOnAnimationImplLocked();
2074 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002075 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002076
2077 // make sure to redraw the whole screen when the animation is done
2078 mDirtyRegion.set(hw.bounds());
2079 signalEvent();
2080
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002081 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002082}
2083
2084status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2085{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002086 class MessageTurnElectronBeamOn : public MessageBase {
2087 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002088 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002089 status_t result;
2090 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002091 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2092 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002093 }
2094 status_t getResult() const {
2095 return result;
2096 }
2097 virtual bool handler() {
2098 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002099 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002100 return true;
2101 }
2102 };
2103
Mathias Agopianabd671a2010-10-14 14:54:06 -07002104 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002105 return NO_ERROR;
2106}
2107
2108// ---------------------------------------------------------------------------
2109
Mathias Agopian74c40c02010-09-29 13:02:36 -07002110status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2111 sp<IMemoryHeap>* heap,
2112 uint32_t* w, uint32_t* h, PixelFormat* f,
2113 uint32_t sw, uint32_t sh)
2114{
2115 status_t result = PERMISSION_DENIED;
2116
2117 // only one display supported for now
2118 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2119 return BAD_VALUE;
2120
2121 if (!GLExtensions::getInstance().haveFramebufferObject())
2122 return INVALID_OPERATION;
2123
2124 // get screen geometry
2125 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2126 const uint32_t hw_w = hw.getWidth();
2127 const uint32_t hw_h = hw.getHeight();
2128
2129 if ((sw > hw_w) || (sh > hw_h))
2130 return BAD_VALUE;
2131
2132 sw = (!sw) ? hw_w : sw;
2133 sh = (!sh) ? hw_h : sh;
2134 const size_t size = sw * sh * 4;
2135
2136 // make sure to clear all GL error flags
2137 while ( glGetError() != GL_NO_ERROR ) ;
2138
2139 // create a FBO
2140 GLuint name, tname;
2141 glGenRenderbuffersOES(1, &tname);
2142 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2143 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2144 glGenFramebuffersOES(1, &name);
2145 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2146 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2147 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2148
2149 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2150 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2151
2152 // invert everything, b/c glReadPixel() below will invert the FB
2153 glViewport(0, 0, sw, sh);
2154 glMatrixMode(GL_PROJECTION);
2155 glPushMatrix();
2156 glLoadIdentity();
2157 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2158 glMatrixMode(GL_MODELVIEW);
2159
2160 // redraw the screen entirely...
2161 glClearColor(0,0,0,1);
2162 glClear(GL_COLOR_BUFFER_BIT);
2163 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2164 const size_t count = layers.size();
2165 for (size_t i=0 ; i<count ; ++i) {
2166 const sp<LayerBase>& layer(layers[i]);
2167 layer->drawForSreenShot();
2168 }
2169
2170 // XXX: this is needed on tegra
2171 glScissor(0, 0, sw, sh);
2172
2173 // check for errors and return screen capture
2174 if (glGetError() != GL_NO_ERROR) {
2175 // error while rendering
2176 result = INVALID_OPERATION;
2177 } else {
2178 // allocate shared memory large enough to hold the
2179 // screen capture
2180 sp<MemoryHeapBase> base(
2181 new MemoryHeapBase(size, 0, "screen-capture") );
2182 void* const ptr = base->getBase();
2183 if (ptr) {
2184 // capture the screen with glReadPixels()
2185 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2186 if (glGetError() == GL_NO_ERROR) {
2187 *heap = base;
2188 *w = sw;
2189 *h = sh;
2190 *f = PIXEL_FORMAT_RGBA_8888;
2191 result = NO_ERROR;
2192 }
2193 } else {
2194 result = NO_MEMORY;
2195 }
2196 }
2197
2198 glEnable(GL_SCISSOR_TEST);
2199 glViewport(0, 0, hw_w, hw_h);
2200 glMatrixMode(GL_PROJECTION);
2201 glPopMatrix();
2202 glMatrixMode(GL_MODELVIEW);
2203
2204
2205 } else {
2206 result = BAD_VALUE;
2207 }
2208
2209 // release FBO resources
2210 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2211 glDeleteRenderbuffersOES(1, &tname);
2212 glDeleteFramebuffersOES(1, &name);
2213 return result;
2214}
2215
2216
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002217status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2218 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002219 uint32_t* width, uint32_t* height, PixelFormat* format,
2220 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002221{
2222 // only one display supported for now
2223 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2224 return BAD_VALUE;
2225
2226 if (!GLExtensions::getInstance().haveFramebufferObject())
2227 return INVALID_OPERATION;
2228
2229 class MessageCaptureScreen : public MessageBase {
2230 SurfaceFlinger* flinger;
2231 DisplayID dpy;
2232 sp<IMemoryHeap>* heap;
2233 uint32_t* w;
2234 uint32_t* h;
2235 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002236 uint32_t sw;
2237 uint32_t sh;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002238 status_t result;
2239 public:
2240 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
2242 uint32_t sw, uint32_t sh)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002243 : flinger(flinger), dpy(dpy),
Mathias Agopian74c40c02010-09-29 13:02:36 -07002244 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002245 {
2246 }
2247 status_t getResult() const {
2248 return result;
2249 }
2250 virtual bool handler() {
2251 Mutex::Autolock _l(flinger->mStateLock);
2252
2253 // if we have secure windows, never allow the screen capture
2254 if (flinger->mSecureFrameBuffer)
2255 return true;
2256
Mathias Agopian74c40c02010-09-29 13:02:36 -07002257 result = flinger->captureScreenImplLocked(dpy,
2258 heap, w, h, f, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002259
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002260 return true;
2261 }
2262 };
2263
2264 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002265 dpy, heap, width, height, format, sw, sh);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002266 status_t res = postMessageSync(msg);
2267 if (res == NO_ERROR) {
2268 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2269 }
2270 return res;
2271}
2272
2273// ---------------------------------------------------------------------------
2274
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002275sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2276{
2277 sp<Layer> result;
2278 Mutex::Autolock _l(mStateLock);
2279 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2280 return result;
2281}
2282
2283// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002284
Mathias Agopian96f08192010-06-02 23:28:45 -07002285Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002286 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002287{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002288}
2289
Mathias Agopian96f08192010-06-02 23:28:45 -07002290Client::~Client()
2291{
Mathias Agopian96f08192010-06-02 23:28:45 -07002292 const size_t count = mLayers.size();
2293 for (size_t i=0 ; i<count ; i++) {
2294 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2295 if (layer != 0) {
2296 mFlinger->removeLayer(layer);
2297 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002298 }
2299}
2300
Mathias Agopian96f08192010-06-02 23:28:45 -07002301status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002302 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002303}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002304
Mathias Agopian96f08192010-06-02 23:28:45 -07002305ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2306{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002307 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002308 mLayers.add(name, layer);
2309 return name;
2310}
2311
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002312void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002313{
Mathias Agopian96f08192010-06-02 23:28:45 -07002314 // we do a linear search here, because this doesn't happen often
2315 const size_t count = mLayers.size();
2316 for (size_t i=0 ; i<count ; i++) {
2317 if (mLayers.valueAt(i) == layer) {
2318 mLayers.removeItemsAt(i, 1);
2319 break;
2320 }
2321 }
2322}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002323sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2324 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002325 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2326 if (layer != 0) {
2327 lbc = layer.promote();
2328 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002329 }
2330 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002331}
2332
Mathias Agopian96f08192010-06-02 23:28:45 -07002333sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002334 return 0;
2335}
2336ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2337 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002338}
Mathias Agopian96f08192010-06-02 23:28:45 -07002339sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002340 ISurfaceComposerClient::surface_data_t* params, int pid,
2341 const String8& name,
2342 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002343 uint32_t flags)
2344{
Mathias Agopian96f08192010-06-02 23:28:45 -07002345 return mFlinger->createSurface(this, pid, name, params,
2346 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002347}
Mathias Agopian96f08192010-06-02 23:28:45 -07002348status_t Client::destroySurface(SurfaceID sid) {
2349 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002350}
Mathias Agopian96f08192010-06-02 23:28:45 -07002351status_t Client::setState(int32_t count, const layer_state_t* states) {
2352 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002353}
2354
2355// ---------------------------------------------------------------------------
2356
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002357UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2358 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2359{
2360 const int pgsize = getpagesize();
2361 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2362
2363 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2364 "SurfaceFlinger Client control-block");
2365
2366 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2367 if (ctrlblk) { // construct the shared structure in-place.
2368 new(ctrlblk) SharedClient;
2369 }
2370}
2371
2372UserClient::~UserClient()
2373{
2374 if (ctrlblk) {
2375 ctrlblk->~SharedClient(); // destroy our shared-structure.
2376 }
2377
2378 /*
2379 * When a UserClient dies, it's unclear what to do exactly.
2380 * We could go ahead and destroy all surfaces linked to that client
2381 * however, it wouldn't be fair to the main Client
2382 * (usually the the window-manager), which might want to re-target
2383 * the layer to another UserClient.
2384 * I think the best is to do nothing, or not much; in most cases the
2385 * WM itself will go ahead and clean things up when it detects a client of
2386 * his has died.
2387 * The remaining question is what to display? currently we keep
2388 * just keep the current buffer.
2389 */
2390}
2391
2392status_t UserClient::initCheck() const {
2393 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2394}
2395
2396void UserClient::detachLayer(const Layer* layer)
2397{
2398 int32_t name = layer->getToken();
2399 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002400 int32_t mask = 1LU<<name;
2401 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2402 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2403 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002404 }
2405}
2406
2407sp<IMemoryHeap> UserClient::getControlBlock() const {
2408 return mCblkHeap;
2409}
2410
2411ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2412{
2413 int32_t name = NAME_NOT_FOUND;
2414 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002415 if (layer == 0) {
2416 return name;
2417 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002418
Mathias Agopian579b3f82010-06-08 19:54:15 -07002419 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002420 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002421 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002422 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002423 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002424
2425 name = 0;
2426 do {
2427 int32_t mask = 1LU<<name;
2428 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2429 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002430 status_t err = layer->setToken(
2431 const_cast<UserClient*>(this), ctrlblk, name);
2432 if (err != NO_ERROR) {
2433 // free the name
2434 android_atomic_and(~mask, &mBitmap);
2435 name = err;
2436 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002437 break;
2438 }
2439 if (++name > 31)
2440 name = NO_MEMORY;
2441 } while(name >= 0);
2442
Mathias Agopian53503a92010-06-08 15:40:56 -07002443 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2444 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002445 return name;
2446}
2447
2448sp<ISurface> UserClient::createSurface(
2449 ISurfaceComposerClient::surface_data_t* params, int pid,
2450 const String8& name,
2451 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2452 uint32_t flags) {
2453 return 0;
2454}
2455status_t UserClient::destroySurface(SurfaceID sid) {
2456 return INVALID_OPERATION;
2457}
2458status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2459 return INVALID_OPERATION;
2460}
2461
2462// ---------------------------------------------------------------------------
2463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464GraphicPlane::GraphicPlane()
2465 : mHw(0)
2466{
2467}
2468
2469GraphicPlane::~GraphicPlane() {
2470 delete mHw;
2471}
2472
2473bool GraphicPlane::initialized() const {
2474 return mHw ? true : false;
2475}
2476
Mathias Agopian2b92d892010-02-08 15:49:35 -08002477int GraphicPlane::getWidth() const {
2478 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479}
2480
Mathias Agopian2b92d892010-02-08 15:49:35 -08002481int GraphicPlane::getHeight() const {
2482 return mHeight;
2483}
2484
2485void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2486{
2487 mHw = hw;
2488
2489 // initialize the display orientation transform.
2490 // it's a constant that should come from the display driver.
2491 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2492 char property[PROPERTY_VALUE_MAX];
2493 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2494 //displayOrientation
2495 switch (atoi(property)) {
2496 case 90:
2497 displayOrientation = ISurfaceComposer::eOrientation90;
2498 break;
2499 case 270:
2500 displayOrientation = ISurfaceComposer::eOrientation270;
2501 break;
2502 }
2503 }
2504
2505 const float w = hw->getWidth();
2506 const float h = hw->getHeight();
2507 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2508 &mDisplayTransform);
2509 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2510 mDisplayWidth = h;
2511 mDisplayHeight = w;
2512 } else {
2513 mDisplayWidth = w;
2514 mDisplayHeight = h;
2515 }
2516
2517 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518}
2519
2520status_t GraphicPlane::orientationToTransfrom(
2521 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002522{
2523 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002524 switch (orientation) {
2525 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002526 flags = Transform::ROT_0;
2527 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002528 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002529 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530 break;
2531 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002532 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533 break;
2534 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002535 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536 break;
2537 default:
2538 return BAD_VALUE;
2539 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002540 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002541 return NO_ERROR;
2542}
2543
2544status_t GraphicPlane::setOrientation(int orientation)
2545{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546 // If the rotation can be handled in hardware, this is where
2547 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002548
2549 const DisplayHardware& hw(displayHardware());
2550 const float w = mDisplayWidth;
2551 const float h = mDisplayHeight;
2552 mWidth = int(w);
2553 mHeight = int(h);
2554
2555 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002556 GraphicPlane::orientationToTransfrom(orientation, w, h,
2557 &orientationTransform);
2558 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2559 mWidth = int(h);
2560 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002562
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002563 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002564 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002565 return NO_ERROR;
2566}
2567
2568const DisplayHardware& GraphicPlane::displayHardware() const {
2569 return *mHw;
2570}
2571
Mathias Agopian59119e62010-10-11 12:37:43 -07002572DisplayHardware& GraphicPlane::editDisplayHardware() {
2573 return *mHw;
2574}
2575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002576const Transform& GraphicPlane::transform() const {
2577 return mGlobalTransform;
2578}
2579
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002580EGLDisplay GraphicPlane::getEGLDisplay() const {
2581 return mHw->getEGLDisplay();
2582}
2583
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002584// ---------------------------------------------------------------------------
2585
2586}; // namespace android