blob: a6b14225f1033d7940c2c39480850d2642a209ce [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian6950e422009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070054#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Mathias Agopian627e7b52009-05-21 19:21:59 -070056/* ideally AID_GRAPHICS would be in a semi-public header
57 * or there would be a way to map a user/group name to its id
58 */
59#ifndef AID_GRAPHICS
60#define AID_GRAPHICS 1003
61#endif
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063#define DISPLAY_COUNT 1
64
65namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066// ---------------------------------------------------------------------------
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068SurfaceFlinger::SurfaceFlinger()
69 : BnSurfaceComposer(), Thread(false),
70 mTransactionFlags(0),
71 mTransactionCount(0),
Mathias Agopian9779b222009-09-07 16:32:45 -070072 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070073 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 mBootTime(systemTime()),
Mathias Agopian151e8592009-06-15 18:24:59 -070075 mHardwareTest("android.permission.HARDWARE_TEST"),
76 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopianca5edbe2010-09-24 11:26:58 -070077 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian151e8592009-06-15 18:24:59 -070078 mDump("android.permission.DUMP"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070080 mHwWorkListDirty(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070083 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070088 mDebugDisableHWC(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070089 mDebugInSwapBuffers(0),
90 mLastSwapBufferTime(0),
91 mDebugInTransaction(0),
92 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -070093 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 mConsoleSignals(0),
95 mSecureFrameBuffer(0)
96{
97 init();
98}
99
100void SurfaceFlinger::init()
101{
102 LOGI("SurfaceFlinger is starting");
103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 property_get("debug.sf.showbackground", value, "0");
109 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700111 LOGI_IF(mDebugRegion, "showupdates enabled");
112 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}
114
115SurfaceFlinger::~SurfaceFlinger()
116{
117 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118}
119
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700120sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700122 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
Mathias Agopian770492c2010-05-28 14:22:23 -0700125sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700127 sp<ISurfaceComposerClient> bclient;
128 sp<Client> client(new Client(this));
129 status_t err = client->initCheck();
130 if (err == NO_ERROR) {
131 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 return bclient;
134}
135
Mathias Agopian7623da42010-06-01 15:12:58 -0700136sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
137{
138 sp<ISurfaceComposerClient> bclient;
139 sp<UserClient> client(new UserClient(this));
140 status_t err = client->initCheck();
141 if (err == NO_ERROR) {
142 bclient = client;
143 }
144 return bclient;
145}
146
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
149{
150 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
151 const GraphicPlane& plane(mGraphicPlanes[dpy]);
152 return plane;
153}
154
155GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
156{
157 return const_cast<GraphicPlane&>(
158 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
159}
160
161void SurfaceFlinger::bootFinished()
162{
163 const nsecs_t now = systemTime();
164 const nsecs_t duration = now - mBootTime;
Andreas Hubere3c01832010-08-16 08:49:37 -0700165 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700166 mBootFinished = true;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700167 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168}
169
170void SurfaceFlinger::onFirstRef()
171{
172 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
173
174 // Wait for the main thread to be done with its initialization
175 mReadyToRunBarrier.wait();
176}
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178static inline uint16_t pack565(int r, int g, int b) {
179 return (r<<11)|(g<<5)|b;
180}
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182status_t SurfaceFlinger::readyToRun()
183{
184 LOGI( "SurfaceFlinger's main thread ready to run. "
185 "Initializing graphics H/W...");
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 // we only support one display currently
188 int dpy = 0;
189
190 {
191 // initialize the main display
192 GraphicPlane& plane(graphicPlane(dpy));
193 DisplayHardware* const hw = new DisplayHardware(this, dpy);
194 plane.setDisplayHardware(hw);
195 }
196
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700197 // create the shared control-block
198 mServerHeap = new MemoryHeapBase(4096,
199 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
200 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Hubere3c01832010-08-16 08:49:37 -0700201
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700202 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
203 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Hubere3c01832010-08-16 08:49:37 -0700204
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700205 new(mServerCblk) surface_flinger_cblk_t;
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 // initialize primary screen
208 // (other display should be initialized in the same manner, but
209 // asynchronously, as they could come and go. None of this is supported
210 // yet).
211 const GraphicPlane& plane(graphicPlane(dpy));
212 const DisplayHardware& hw = plane.displayHardware();
213 const uint32_t w = hw.getWidth();
214 const uint32_t h = hw.getHeight();
215 const uint32_t f = hw.getFormat();
216 hw.makeCurrent();
217
218 // initialize the shared control block
219 mServerCblk->connected |= 1<<dpy;
220 display_cblk_t* dcblk = mServerCblk->displays + dpy;
221 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian66c77a52010-02-08 15:49:35 -0800222 dcblk->w = plane.getWidth();
223 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 dcblk->format = f;
225 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
226 dcblk->xdpi = hw.getDpiX();
227 dcblk->ydpi = hw.getDpiY();
228 dcblk->fps = hw.getRefreshRate();
229 dcblk->density = hw.getDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700233 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 glEnableClientState(GL_VERTEX_ARRAY);
235 glEnable(GL_SCISSOR_TEST);
236 glShadeModel(GL_FLAT);
237 glDisable(GL_DITHER);
238 glDisable(GL_CULL_FACE);
239
240 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
241 const uint16_t g1 = pack565(0x17,0x2f,0x17);
242 const uint16_t textureData[4] = { g0, g1, g1, g0 };
243 glGenTextures(1, &mWormholeTexName);
244 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
245 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
246 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
247 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
248 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
250 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
251
252 glViewport(0, 0, w, h);
253 glMatrixMode(GL_PROJECTION);
254 glLoadIdentity();
255 glOrthof(0, w, h, 0, 0, 1);
256
257 LayerDim::initDimmer(this, w, h);
258
259 mReadyToRunBarrier.open();
260
261 /*
262 * We're now ready to accept clients...
263 */
264
Mathias Agopian627e7b52009-05-21 19:21:59 -0700265 // start boot animation
266 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 return NO_ERROR;
269}
270
271// ----------------------------------------------------------------------------
272#if 0
273#pragma mark -
274#pragma mark Events Handler
275#endif
276
277void SurfaceFlinger::waitForEvent()
278{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700279 while (true) {
280 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800281 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700282 if (UNLIKELY(isFrozen())) {
283 // wait 5 seconds
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700284 const nsecs_t now = systemTime();
285 if (mFreezeDisplayTime == 0) {
286 mFreezeDisplayTime = now;
287 }
288 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
289 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700290 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700291
Mathias Agopian898c4c92010-05-18 17:06:55 -0700292 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-12-01 17:23:28 -0800293
294 // see if we timed out
295 if (isFrozen()) {
296 const nsecs_t now = systemTime();
297 nsecs_t frozenTime = (now - mFreezeDisplayTime);
298 if (frozenTime >= freezeDisplayTimeout) {
299 // we timed out and are still frozen
300 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
301 mFreezeDisplay, mFreezeCount);
302 mFreezeDisplayTime = 0;
303 mFreezeCount = 0;
304 mFreezeDisplay = false;
305 }
306 }
307
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700308 if (msg != 0) {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700309 switch (msg->what) {
310 case MessageQueue::INVALIDATE:
311 // invalidate message, just return to the main loop
312 return;
313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316}
317
318void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700319 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320}
321
322void SurfaceFlinger::signal() const {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700323 // this is the IPC call
324 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325}
326
Mathias Agopian898c4c92010-05-18 17:06:55 -0700327status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
328 nsecs_t reltime, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700330 return mEventQueue.postMessage(msg, reltime, flags);
331}
332
333status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
334 nsecs_t reltime, uint32_t flags)
335{
336 status_t res = mEventQueue.postMessage(msg, reltime, flags);
337 if (res == NO_ERROR) {
338 msg->wait();
339 }
340 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341}
342
343// ----------------------------------------------------------------------------
344#if 0
345#pragma mark -
346#pragma mark Main loop
347#endif
348
349bool SurfaceFlinger::threadLoop()
350{
351 waitForEvent();
352
353 // check for transactions
354 if (UNLIKELY(mConsoleSignals)) {
355 handleConsoleEvents();
356 }
357
358 if (LIKELY(mTransactionCount == 0)) {
359 // if we're in a global transaction, don't do anything.
360 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
361 uint32_t transactionFlags = getTransactionFlags(mask);
362 if (LIKELY(transactionFlags)) {
363 handleTransaction(transactionFlags);
364 }
365 }
366
367 // post surfaces (if needed)
368 handlePageFlip();
369
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700370 if (UNLIKELY(mHwWorkListDirty)) {
371 // build the h/w work list
372 handleWorkList();
373 }
374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700376 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-09-13 22:57:58 -0700378
379 const int index = hw.getCurrentBufferIndex();
380 GraphicLog& logger(GraphicLog::getInstance());
381
382 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 handleRepaint();
384
Mathias Agopianb1a18742009-09-17 16:18:16 -0700385 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700386 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700387 hw.compositionComplete();
388
Mathias Agopian04262e92010-09-13 22:57:58 -0700389 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700390 postFramebuffer();
391
Mathias Agopiana5ab8ce2010-09-15 12:29:18 -0700392 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
Mathias Agopian04262e92010-09-13 22:57:58 -0700395 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 } else {
397 // pretend we did the post
398 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 usleep(16667); // 60 fps period
400 }
401 return true;
402}
403
404void SurfaceFlinger::postFramebuffer()
405{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 if (!mInvalidRegion.isEmpty()) {
407 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700408 const nsecs_t now = systemTime();
409 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700411 mLastSwapBufferTime = systemTime() - now;
412 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415}
416
417void SurfaceFlinger::handleConsoleEvents()
418{
419 // something to do with the console
420 const DisplayHardware& hw = graphicPlane(0).displayHardware();
421
422 int what = android_atomic_and(0, &mConsoleSignals);
423 if (what & eConsoleAcquired) {
424 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700425 // this is a temporary work-around, eventually this should be called
426 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700427 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
Mathias Agopianaab758e2010-10-11 12:37:43 -0700430 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700431 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 hw.releaseScreen();
434 }
435
436 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700437 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 hw.releaseScreen();
439 } else {
440 mDeferReleaseConsole = true;
441 }
442 }
443
444 mDirtyRegion.set(hw.bounds());
445}
446
447void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
448{
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700449 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450
Mathias Agopianff1d4102010-08-10 17:19:56 -0700451 /*
452 * Perform and commit the transaction
453 */
454
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700455 { // scope for the lock
456 Mutex::Autolock _l(mStateLock);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700457 const nsecs_t now = systemTime();
458 mDebugInTransaction = now;
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700459 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700460 mLastTransactionTime = systemTime() - now;
461 mDebugInTransaction = 0;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700462 mHwWorkListDirty = true;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700463 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700464 }
465
Mathias Agopianff1d4102010-08-10 17:19:56 -0700466 /*
467 * Clean-up all layers that went away
468 * (do this without the lock held)
469 */
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700470
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700471 const size_t count = ditchedLayers.size();
472 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianffae4fc2009-09-04 19:50:23 -0700473 if (ditchedLayers[i] != 0) {
474 //LOGD("ditching layer %p", ditchedLayers[i].get());
475 ditchedLayers[i]->ditch();
476 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700477 }
478}
479
480void SurfaceFlinger::handleTransactionLocked(
481 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
482{
483 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 const size_t count = currentLayers.size();
485
486 /*
487 * Traversal of the children
488 * (perform the transaction for each of them if needed)
489 */
490
491 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
492 if (layersNeedTransaction) {
493 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700494 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
496 if (!trFlags) continue;
497
498 const uint32_t flags = layer->doTransaction(0);
499 if (flags & Layer::eVisibleRegion)
500 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
502 }
503
504 /*
505 * Perform our own transaction if needed
506 */
507
508 if (transactionFlags & eTransactionNeeded) {
509 if (mCurrentState.orientation != mDrawingState.orientation) {
510 // the orientation has changed, recompute all visible regions
511 // and invalidate everything.
512
513 const int dpy = 0;
514 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700515 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 GraphicPlane& plane(graphicPlane(dpy));
517 plane.setOrientation(orientation);
518
519 // update the shared control block
520 const DisplayHardware& hw(plane.displayHardware());
521 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
522 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800523 dcblk->w = plane.getWidth();
524 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525
526 mVisibleRegionsDirty = true;
527 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529
530 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
531 // freezing or unfreezing the display -> trigger animation if needed
532 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800533 if (mFreezeDisplay)
534 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700537 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
538 // layers have been added
539 mVisibleRegionsDirty = true;
540 }
541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 // some layers might have been removed, so
543 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700544 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700545 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700547 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700548 const size_t count = previousLayers.size();
549 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700550 const sp<LayerBase>& layer(previousLayers[i]);
551 if (currentLayers.indexOf( layer ) < 0) {
552 // this layer is not visible anymore
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700553 ditchedLayers.add(layer);
Mathias Agopian33863dd2009-07-28 14:20:21 -0700554 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700555 }
556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559
560 commitTransaction();
561}
562
563sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
564{
565 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
566}
567
568void SurfaceFlinger::computeVisibleRegions(
569 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
570{
571 const GraphicPlane& plane(graphicPlane(0));
572 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700573 const DisplayHardware& hw(plane.displayHardware());
574 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575
576 Region aboveOpaqueLayers;
577 Region aboveCoveredLayers;
578 Region dirty;
579
580 bool secureFrameBuffer = false;
581
582 size_t i = currentLayers.size();
583 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700584 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 layer->validateVisibility(planeTransform);
586
587 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700588 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700590 /*
591 * opaqueRegion: area of a surface that is fully opaque.
592 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700594
595 /*
596 * visibleRegion: area of a surface that is visible on screen
597 * and not fully transparent. This is essentially the layer's
598 * footprint minus the opaque regions above it.
599 * Areas covered by a translucent surface are considered visible.
600 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700602
603 /*
604 * coveredRegion: area of a surface that is covered by all
605 * visible regions above it (which includes the translucent areas).
606 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700608
609
610 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700611 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 const bool translucent = layer->needsBlending();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700613 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700615 visibleRegion.andSelf(screenRegion);
616 if (!visibleRegion.isEmpty()) {
617 // Remove the transparent area from the visible region
618 if (translucent) {
619 visibleRegion.subtractSelf(layer->transparentRegionScreen);
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700622 // compute the opaque region
623 const int32_t layerOrientation = layer->getOrientation();
624 if (s.alpha==255 && !translucent &&
625 ((layerOrientation & Transform::ROT_INVALID) == false)) {
626 // the opaque region is the layer's footprint
627 opaqueRegion = visibleRegion;
628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 }
630 }
631
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700632 // Clip the covered region to the visible region
633 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
634
635 // Update aboveCoveredLayers for next (lower) layer
636 aboveCoveredLayers.orSelf(visibleRegion);
637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 // subtract the opaque region covered by the layers above us
639 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640
641 // compute this layer's dirty region
642 if (layer->contentDirty) {
643 // we need to invalidate the whole region
644 dirty = visibleRegion;
645 // as well, as the old visible region
646 dirty.orSelf(layer->visibleRegionScreen);
647 layer->contentDirty = false;
648 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700649 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700650 * the exposed region consists of two components:
651 * 1) what's VISIBLE now and was COVERED before
652 * 2) what's EXPOSED now less what was EXPOSED before
653 *
654 * note that (1) is conservative, we start with the whole
655 * visible region but only keep what used to be covered by
656 * something -- which mean it may have been exposed.
657 *
658 * (2) handles areas that were not covered by anything but got
659 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700660 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700661 const Region newExposed = visibleRegion - coveredRegion;
662 const Region oldVisibleRegion = layer->visibleRegionScreen;
663 const Region oldCoveredRegion = layer->coveredRegionScreen;
664 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
665 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667 dirty.subtractSelf(aboveOpaqueLayers);
668
669 // accumulate to the screen dirty region
670 dirtyRegion.orSelf(dirty);
671
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700672 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 // Store the visible region is screen space
676 layer->setVisibleRegion(visibleRegion);
677 layer->setCoveredRegion(coveredRegion);
678
Mathias Agopian12cedff2009-07-28 10:57:27 -0700679 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 if (layer->isSecure() && !visibleRegion.isEmpty()) {
681 secureFrameBuffer = true;
682 }
683 }
684
Mathias Agopian12cedff2009-07-28 10:57:27 -0700685 // invalidate the areas where a layer was removed
686 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
687 mDirtyRegionRemovedLayer.clear();
688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 mSecureFrameBuffer = secureFrameBuffer;
690 opaqueRegion = aboveOpaqueLayers;
691}
692
693
694void SurfaceFlinger::commitTransaction()
695{
696 mDrawingState = mCurrentState;
Mathias Agopian9779b222009-09-07 16:32:45 -0700697 mResizeTransationPending = false;
698 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699}
700
701void SurfaceFlinger::handlePageFlip()
702{
703 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700704 LayerVector& currentLayers(
705 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 visibleRegions |= lockPageFlip(currentLayers);
707
708 const DisplayHardware& hw = graphicPlane(0).displayHardware();
709 const Region screenRegion(hw.bounds());
710 if (visibleRegions) {
711 Region opaqueRegion;
712 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700713
714 /*
715 * rebuild the visible layer list
716 */
717 mVisibleLayersSortedByZ.clear();
718 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
719 size_t count = currentLayers.size();
720 mVisibleLayersSortedByZ.setCapacity(count);
721 for (size_t i=0 ; i<count ; i++) {
722 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
723 mVisibleLayersSortedByZ.add(currentLayers[i]);
724 }
725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 mWormholeRegion = screenRegion.subtract(opaqueRegion);
727 mVisibleRegionsDirty = false;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700728 mHwWorkListDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
731 unlockPageFlip(currentLayers);
732 mDirtyRegion.andSelf(screenRegion);
733}
734
735bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
736{
737 bool recomputeVisibleRegions = false;
738 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700739 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700741 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 layer->lockPageFlip(recomputeVisibleRegions);
743 }
744 return recomputeVisibleRegions;
745}
746
747void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
748{
749 const GraphicPlane& plane(graphicPlane(0));
750 const Transform& planeTransform(plane.transform());
751 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700752 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700754 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 layer->unlockPageFlip(planeTransform, mDirtyRegion);
756 }
757}
758
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700759void SurfaceFlinger::handleWorkList()
760{
761 mHwWorkListDirty = false;
762 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
763 if (hwc.initCheck() == NO_ERROR) {
764 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
765 const size_t count = currentLayers.size();
766 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700767 hwc_layer_t* const cur(hwc.getLayers());
768 for (size_t i=0 ; cur && i<count ; i++) {
769 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian6a969242010-09-22 18:58:01 -0700770 if (mDebugDisableHWC) {
771 cur[i].compositionType = HWC_FRAMEBUFFER;
772 cur[i].flags |= HWC_SKIP_LAYER;
773 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700774 }
775 }
776}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778void SurfaceFlinger::handleRepaint()
779{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700780 // compute the invalid region
781 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782
783 if (UNLIKELY(mDebugRegion)) {
784 debugFlashRegions();
785 }
786
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700787 // set the frame buffer
788 const DisplayHardware& hw(graphicPlane(0).displayHardware());
789 glMatrixMode(GL_MODELVIEW);
790 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791
792 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700793 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
794 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700795 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700796 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
797 // takes a rectangle, we must make sure to update that whole
798 // rectangle in that case
799 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700800 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700801 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 mDirtyRegion.set(mInvalidRegion.bounds());
803 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700804 // in the BUFFER_PRESERVED case, obviously, we can update only
805 // what's needed and nothing more.
806 // NOTE: this is NOT a common case, as preserving the backbuffer
807 // is costly and usually involves copying the whole update back.
808 }
809 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700810 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700811 // We need to redraw the rectangle that will be updated
812 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700813 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700814 // rectangle instead of a region (see DisplayHardware::flip())
815 mDirtyRegion.set(mInvalidRegion.bounds());
816 } else {
817 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 mDirtyRegion.set(hw.bounds());
819 mInvalidRegion = mDirtyRegion;
820 }
821 }
822
823 // compose all surfaces
824 composeSurfaces(mDirtyRegion);
825
826 // clear the dirty regions
827 mDirtyRegion.clear();
828}
829
830void SurfaceFlinger::composeSurfaces(const Region& dirty)
831{
832 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
833 // should never happen unless the window manager has a bug
834 // draw something...
835 drawWormhole();
836 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700837
838 status_t err = NO_ERROR;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700839 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700840 size_t count = layers.size();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700841
842 const DisplayHardware& hw(graphicPlane(0).displayHardware());
843 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700844 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700845
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700846 LOGE_IF(cur && hwc.getNumLayers() != count,
847 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
848 hwc.getNumLayers(), count);
849
850 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700851 if (hwc.initCheck() == NO_ERROR) {
852 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
853 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700854
855 /*
856 * update the per-frame h/w composer data for each layer
857 * and build the transparent region of the FB
858 */
859 Region transparent;
860 if (cur) {
861 for (size_t i=0 ; i<count ; i++) {
862 const sp<LayerBase>& layer(layers[i]);
863 layer->setPerFrameData(&cur[i]);
864 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
865 if (!(layer->needsBlending())) {
866 transparent.orSelf(layer->visibleRegionScreen);
867 }
868 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700869 }
870 err = hwc.prepare();
871 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
872 }
873
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700874 /*
875 * clear the area of the FB that need to be transparent
876 */
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700877 transparent.andSelf(dirty);
878 if (!transparent.isEmpty()) {
879 glClearColor(0,0,0,0);
880 Region::const_iterator it = transparent.begin();
881 Region::const_iterator const end = transparent.end();
882 const int32_t height = hw.getHeight();
883 while (it != end) {
884 const Rect& r(*it++);
885 const GLint sy = height - (r.top + r.height());
886 glScissor(r.left, sy, r.width(), r.height());
887 glClear(GL_COLOR_BUFFER_BIT);
888 }
889 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700890
891
892 /*
893 * and then, render the layers targeted at the framebuffer
894 */
895 for (size_t i=0 ; i<count ; i++) {
896 if (cur) {
Antti Hatala982f58b2010-09-09 02:32:30 -0700897 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
898 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700899 // skip layers handled by the HAL
900 continue;
901 }
902 }
Mathias Agopian6a969242010-09-22 18:58:01 -0700903
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700904 const sp<LayerBase>& layer(layers[i]);
905 const Region clip(dirty.intersect(layer->visibleRegionScreen));
906 if (!clip.isEmpty()) {
907 layer->draw(clip);
908 }
909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910}
911
912void SurfaceFlinger::unlockClients()
913{
914 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
915 const size_t count = drawingLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700916 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700918 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 layer->finishPageFlip();
920 }
921}
922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923void SurfaceFlinger::debugFlashRegions()
924{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700925 const DisplayHardware& hw(graphicPlane(0).displayHardware());
926 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700927
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700928 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
929 (flags & DisplayHardware::BUFFER_PRESERVED))) {
930 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
931 mDirtyRegion.bounds() : hw.bounds());
932 composeSurfaces(repaint);
933 }
934
935 TextureManager::deactivateTextures();
936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 glDisable(GL_BLEND);
938 glDisable(GL_DITHER);
939 glDisable(GL_SCISSOR_TEST);
940
Mathias Agopiandff8e582009-05-04 14:17:04 -0700941 static int toggle = 0;
942 toggle = 1 - toggle;
943 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700944 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700945 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700946 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700949 Region::const_iterator it = mDirtyRegion.begin();
950 Region::const_iterator const end = mDirtyRegion.end();
951 while (it != end) {
952 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 GLfloat vertices[][2] = {
954 { r.left, r.top },
955 { r.left, r.bottom },
956 { r.right, r.bottom },
957 { r.right, r.top }
958 };
959 glVertexPointer(2, GL_FLOAT, 0, vertices);
960 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
961 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700962
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700963 if (mInvalidRegion.isEmpty()) {
964 mDirtyRegion.dump("mDirtyRegion");
965 mInvalidRegion.dump("mInvalidRegion");
966 }
967 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968
969 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700970 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971
972 glEnable(GL_SCISSOR_TEST);
973 //mDirtyRegion.dump("mDirtyRegion");
974}
975
976void SurfaceFlinger::drawWormhole() const
977{
978 const Region region(mWormholeRegion.intersect(mDirtyRegion));
979 if (region.isEmpty())
980 return;
981
982 const DisplayHardware& hw(graphicPlane(0).displayHardware());
983 const int32_t width = hw.getWidth();
984 const int32_t height = hw.getHeight();
985
986 glDisable(GL_BLEND);
987 glDisable(GL_DITHER);
988
989 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700990 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700991 Region::const_iterator it = region.begin();
992 Region::const_iterator const end = region.end();
993 while (it != end) {
994 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 const GLint sy = height - (r.top + r.height());
996 glScissor(r.left, sy, r.width(), r.height());
997 glClear(GL_COLOR_BUFFER_BIT);
998 }
999 } else {
1000 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1001 { width, height }, { 0, height } };
1002 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1003 glVertexPointer(2, GL_SHORT, 0, vertices);
1004 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1005 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001006#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001007 if (GLExtensions::getInstance().haveTextureExternal()) {
1008 glDisable(GL_TEXTURE_EXTERNAL_OES);
1009 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001010#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 glEnable(GL_TEXTURE_2D);
1012 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1013 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1014 glMatrixMode(GL_TEXTURE);
1015 glLoadIdentity();
1016 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001017 Region::const_iterator it = region.begin();
1018 Region::const_iterator const end = region.end();
1019 while (it != end) {
1020 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 const GLint sy = height - (r.top + r.height());
1022 glScissor(r.left, sy, r.width(), r.height());
1023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1024 }
1025 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001026 glLoadIdentity();
1027 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029}
1030
1031void SurfaceFlinger::debugShowFPS() const
1032{
1033 static int mFrameCount;
1034 static int mLastFrameCount = 0;
1035 static nsecs_t mLastFpsTime = 0;
1036 static float mFps = 0;
1037 mFrameCount++;
1038 nsecs_t now = systemTime();
1039 nsecs_t diff = now - mLastFpsTime;
1040 if (diff > ms2ns(250)) {
1041 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1042 mLastFpsTime = now;
1043 mLastFrameCount = mFrameCount;
1044 }
1045 // XXX: mFPS has the value we want
1046 }
1047
Mathias Agopian1473f462009-04-10 14:24:30 -07001048status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049{
1050 Mutex::Autolock _l(mStateLock);
1051 addLayer_l(layer);
1052 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1053 return NO_ERROR;
1054}
1055
Mathias Agopian593c05c2010-06-02 23:28:45 -07001056status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1057{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001058 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001059 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1060}
1061
1062ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1063 const sp<LayerBaseClient>& lbc)
1064{
1065 Mutex::Autolock _l(mStateLock);
1066
1067 // attach this layer to the client
1068 ssize_t name = client->attachLayer(lbc);
1069
1070 // add this layer to the current state list
1071 addLayer_l(lbc);
1072
1073 return name;
1074}
1075
Mathias Agopian1473f462009-04-10 14:24:30 -07001076status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077{
1078 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001079 status_t err = purgatorizeLayer_l(layer);
1080 if (err == NO_ERROR)
1081 setTransactionFlags(eTransactionNeeded);
1082 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083}
1084
Mathias Agopian1473f462009-04-10 14:24:30 -07001085status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086{
Mathias Agopian7623da42010-06-01 15:12:58 -07001087 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1088 if (lbc != 0) {
1089 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1092 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001093 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 return NO_ERROR;
1095 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001096 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097}
1098
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001099status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1100{
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001101 // remove the layer from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001102 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001103
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001104 layerBase->onRemoved();
1105
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001106 // it's possible that we don't find a layer, because it might
1107 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001108 // from the user because there is a race between Client::destroySurface(),
1109 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001110 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1111}
1112
Mathias Agopian593c05c2010-06-02 23:28:45 -07001113status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001115 layer->forceVisibilityTransaction();
1116 setTransactionFlags(eTraversalNeeded);
1117 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118}
1119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1121{
1122 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1123}
1124
Mathias Agopian898c4c92010-05-18 17:06:55 -07001125uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126{
1127 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1128 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001129 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131 return old;
1132}
1133
1134void SurfaceFlinger::openGlobalTransaction()
1135{
1136 android_atomic_inc(&mTransactionCount);
1137}
1138
1139void SurfaceFlinger::closeGlobalTransaction()
1140{
1141 if (android_atomic_dec(&mTransactionCount) == 1) {
1142 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001143
Andreas Hubere3c01832010-08-16 08:49:37 -07001144 // if there is a transaction with a resize, wait for it to
Mathias Agopian9779b222009-09-07 16:32:45 -07001145 // take effect before returning.
1146 Mutex::Autolock _l(mStateLock);
1147 while (mResizeTransationPending) {
Mathias Agopian98a9c562009-09-30 14:42:13 -07001148 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1149 if (CC_UNLIKELY(err != NO_ERROR)) {
1150 // just in case something goes wrong in SF, return to the
1151 // called after a few seconds.
1152 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1153 mResizeTransationPending = false;
1154 break;
1155 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158}
1159
1160status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1161{
1162 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1163 return BAD_VALUE;
1164
1165 Mutex::Autolock _l(mStateLock);
1166 mCurrentState.freezeDisplay = 1;
1167 setTransactionFlags(eTransactionNeeded);
1168
1169 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001170 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 return NO_ERROR;
1172}
1173
1174status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1175{
1176 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1177 return BAD_VALUE;
1178
1179 Mutex::Autolock _l(mStateLock);
1180 mCurrentState.freezeDisplay = 0;
1181 setTransactionFlags(eTransactionNeeded);
1182
1183 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001184 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 return NO_ERROR;
1186}
1187
Andreas Hubere3c01832010-08-16 08:49:37 -07001188int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001189 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190{
1191 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1192 return BAD_VALUE;
1193
1194 Mutex::Autolock _l(mStateLock);
1195 if (mCurrentState.orientation != orientation) {
1196 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001197 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 mCurrentState.orientation = orientation;
1199 setTransactionFlags(eTransactionNeeded);
1200 mTransactionCV.wait(mStateLock);
1201 } else {
1202 orientation = BAD_VALUE;
1203 }
1204 }
1205 return orientation;
1206}
1207
Mathias Agopian593c05c2010-06-02 23:28:45 -07001208sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian770492c2010-05-28 14:22:23 -07001209 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1211 uint32_t flags)
1212{
Mathias Agopian1473f462009-04-10 14:24:30 -07001213 sp<LayerBaseClient> layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001215
1216 if (int32_t(w|h) < 0) {
1217 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1218 int(w), int(h));
1219 return surfaceHandle;
1220 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001223 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 switch (flags & eFXSurfaceMask) {
1225 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001226 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1227 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 break;
1229 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001230 // for now we treat Blur as Dim, until we can implement it
1231 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001233 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 break;
1235 }
1236
Mathias Agopian1473f462009-04-10 14:24:30 -07001237 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001238 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001239 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001240 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001243 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001244 params->token = token;
Mathias Agopian18b6b492009-08-19 17:46:26 -07001245 params->identity = surfaceHandle->getIdentity();
1246 params->width = w;
1247 params->height = h;
1248 params->format = format;
Mathias Agopian7623da42010-06-01 15:12:58 -07001249 if (normalLayer != 0) {
1250 Mutex::Autolock _l(mStateLock);
1251 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1252 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001253 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001254
Mathias Agopian593c05c2010-06-02 23:28:45 -07001255 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 }
1257
1258 return surfaceHandle;
1259}
1260
Mathias Agopian7623da42010-06-01 15:12:58 -07001261sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001262 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001263 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001264 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265{
1266 // initialize the surfaces
1267 switch (format) { // TODO: take h/w into account
1268 case PIXEL_FORMAT_TRANSPARENT:
1269 case PIXEL_FORMAT_TRANSLUCENT:
1270 format = PIXEL_FORMAT_RGBA_8888;
1271 break;
1272 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001273#ifdef NO_RGBX_8888
1274 format = PIXEL_FORMAT_RGB_565;
1275#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001276 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001277#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 break;
1279 }
1280
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001281#ifdef NO_RGBX_8888
1282 if (format == PIXEL_FORMAT_RGBX_8888)
1283 format = PIXEL_FORMAT_RGBA_8888;
1284#endif
1285
Mathias Agopian593c05c2010-06-02 23:28:45 -07001286 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001287 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001288 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001290 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292 return layer;
1293}
1294
Mathias Agopian7623da42010-06-01 15:12:58 -07001295sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001296 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001297 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001299 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 return layer;
1302}
1303
Mathias Agopian593c05c2010-06-02 23:28:45 -07001304status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001306 /*
1307 * called by the window manager, when a surface should be marked for
1308 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001309 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001310 * The surface is removed from the current and drawing lists, but placed
1311 * in the purgatory queue, so it's not destroyed right-away (we need
1312 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001313 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001314
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001315 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001316 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001317 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001318 if (layer != 0) {
1319 err = purgatorizeLayer_l(layer);
1320 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001321 setTransactionFlags(eTransactionNeeded);
1322 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001323 }
1324 return err;
1325}
1326
1327status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1328{
Mathias Agopian359140c2009-07-02 17:33:40 -07001329 // called by ~ISurface() when all references are gone
Andreas Hubere3c01832010-08-16 08:49:37 -07001330
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001331 class MessageDestroySurface : public MessageBase {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001332 SurfaceFlinger* flinger;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001333 sp<LayerBaseClient> layer;
1334 public:
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001335 MessageDestroySurface(
1336 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1337 : flinger(flinger), layer(layer) { }
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001338 virtual bool handler() {
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001339 sp<LayerBaseClient> l(layer);
1340 layer.clear(); // clear it outside of the lock;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001341 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian359140c2009-07-02 17:33:40 -07001342 /*
Andreas Hubere3c01832010-08-16 08:49:37 -07001343 * remove the layer from the current list -- chances are that it's
1344 * not in the list anyway, because it should have been removed
1345 * already upon request of the client (eg: window manager).
Mathias Agopian359140c2009-07-02 17:33:40 -07001346 * However, a buggy client could have not done that.
1347 * Since we know we don't have any more clients, we don't need
1348 * to use the purgatory.
1349 */
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001350 status_t err = flinger->removeLayer_l(l);
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001351 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1352 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001353 return true;
1354 }
1355 };
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001356
Mathias Agopian898c4c92010-05-18 17:06:55 -07001357 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 return NO_ERROR;
1359}
1360
1361status_t SurfaceFlinger::setClientState(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001362 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 int32_t count,
1364 const layer_state_t* states)
1365{
1366 Mutex::Autolock _l(mStateLock);
1367 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 for (int i=0 ; i<count ; i++) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001369 const layer_state_t& s(states[i]);
1370 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian1473f462009-04-10 14:24:30 -07001371 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 const uint32_t what = s.what;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 if (what & ePositionChanged) {
1374 if (layer->setPosition(s.x, s.y))
1375 flags |= eTraversalNeeded;
1376 }
1377 if (what & eLayerChanged) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001378 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 if (layer->setLayer(s.z)) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001380 mCurrentState.layersSortedByZ.removeAt(idx);
1381 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 // we need traversal (state changed)
1383 // AND transaction (list changed)
1384 flags |= eTransactionNeeded|eTraversalNeeded;
1385 }
1386 }
1387 if (what & eSizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -07001388 if (layer->setSize(s.w, s.h)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 flags |= eTraversalNeeded;
Mathias Agopian9779b222009-09-07 16:32:45 -07001390 mResizeTransationPending = true;
1391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393 if (what & eAlphaChanged) {
1394 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1395 flags |= eTraversalNeeded;
1396 }
1397 if (what & eMatrixChanged) {
1398 if (layer->setMatrix(s.matrix))
1399 flags |= eTraversalNeeded;
1400 }
1401 if (what & eTransparentRegionChanged) {
1402 if (layer->setTransparentRegionHint(s.transparentRegion))
1403 flags |= eTraversalNeeded;
1404 }
1405 if (what & eVisibilityChanged) {
1406 if (layer->setFlags(s.flags, s.mask))
1407 flags |= eTraversalNeeded;
1408 }
1409 }
1410 }
1411 if (flags) {
1412 setTransactionFlags(flags);
1413 }
1414 return NO_ERROR;
1415}
1416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417void SurfaceFlinger::screenReleased(int dpy)
1418{
1419 // this may be called by a signal handler, we can't do too much in here
1420 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1421 signalEvent();
1422}
1423
1424void SurfaceFlinger::screenAcquired(int dpy)
1425{
1426 // this may be called by a signal handler, we can't do too much in here
1427 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1428 signalEvent();
1429}
1430
1431status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1432{
Erik Gilling94720d72010-12-01 16:38:01 -08001433 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 char buffer[SIZE];
1435 String8 result;
Mathias Agopian151e8592009-06-15 18:24:59 -07001436 if (!mDump.checkCalling()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 snprintf(buffer, SIZE, "Permission Denial: "
1438 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1439 IPCThreadState::self()->getCallingPid(),
1440 IPCThreadState::self()->getCallingUid());
1441 result.append(buffer);
1442 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001443
1444 // figure out if we're stuck somewhere
1445 const nsecs_t now = systemTime();
1446 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1447 const nsecs_t inTransaction(mDebugInTransaction);
1448 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1449 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1450
1451 // Try to get the main lock, but don't insist if we can't
1452 // (this would indicate SF is stuck, but we want to be able to
1453 // print something in dumpsys).
1454 int retry = 3;
1455 while (mStateLock.tryLock()<0 && --retry>=0) {
1456 usleep(1000000);
1457 }
1458 const bool locked(retry >= 0);
1459 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001460 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001461 "SurfaceFlinger appears to be unresponsive, "
1462 "dumping anyways (no locks held)\n");
1463 result.append(buffer);
1464 }
1465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1467 const size_t count = currentLayers.size();
1468 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001469 const sp<LayerBase>& layer(currentLayers[i]);
1470 layer->dump(result, buffer, SIZE);
1471 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 s.transparentRegion.dump(result, "transparentRegion");
1473 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1474 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1475 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mWormholeRegion.dump(result, "WormholeRegion");
1478 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1479 snprintf(buffer, SIZE,
1480 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1481 mFreezeDisplay?"yes":"no", mFreezeCount,
1482 mCurrentState.orientation, hw.canDraw());
1483 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001484 snprintf(buffer, SIZE,
1485 " last eglSwapBuffers() time: %f us\n"
1486 " last transaction time : %f us\n",
1487 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1488 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001489
Mathias Agopiana8d49172009-08-26 16:36:26 -07001490 if (inSwapBuffersDuration || !locked) {
1491 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1492 inSwapBuffersDuration/1000.0);
1493 result.append(buffer);
1494 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001495
Mathias Agopiana8d49172009-08-26 16:36:26 -07001496 if (inTransactionDuration || !locked) {
1497 snprintf(buffer, SIZE, " transaction time: %f us\n",
1498 inTransactionDuration/1000.0);
1499 result.append(buffer);
1500 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001501
Mathias Agopian6a969242010-09-22 18:58:01 -07001502 HWComposer& hwc(hw.getHwComposer());
1503 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1504 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1505 mDebugDisableHWC ? "disabled" : "enabled");
1506 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -07001507 hwc.dump(result, buffer, SIZE);
Mathias Agopian6a969242010-09-22 18:58:01 -07001508
Mathias Agopian6950e422009-10-05 17:07:12 -07001509 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001510 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001511 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001512
1513 if (locked) {
1514 mStateLock.unlock();
1515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517 write(fd, result.string(), result.size());
1518 return NO_ERROR;
1519}
1520
1521status_t SurfaceFlinger::onTransact(
1522 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1523{
1524 switch (code) {
1525 case CREATE_CONNECTION:
1526 case OPEN_GLOBAL_TRANSACTION:
1527 case CLOSE_GLOBAL_TRANSACTION:
1528 case SET_ORIENTATION:
1529 case FREEZE_DISPLAY:
1530 case UNFREEZE_DISPLAY:
1531 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001532 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001533 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 {
1535 // codes that require permission check
1536 IPCThreadState* ipc = IPCThreadState::self();
1537 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001538 const int uid = ipc->getCallingUid();
Mathias Agopian151e8592009-06-15 18:24:59 -07001539 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1540 LOGE("Permission Denial: "
1541 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1542 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001544 break;
1545 }
1546 case CAPTURE_SCREEN:
1547 {
1548 // codes that require permission check
1549 IPCThreadState* ipc = IPCThreadState::self();
1550 const int pid = ipc->getCallingPid();
1551 const int uid = ipc->getCallingUid();
1552 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1553 LOGE("Permission Denial: "
1554 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1555 return PERMISSION_DENIED;
1556 }
1557 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1562 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001563 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian151e8592009-06-15 18:24:59 -07001564 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1565 IPCThreadState* ipc = IPCThreadState::self();
1566 const int pid = ipc->getCallingPid();
1567 const int uid = ipc->getCallingUid();
1568 LOGE("Permission Denial: "
1569 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 return PERMISSION_DENIED;
1571 }
1572 int n;
1573 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001574 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001575 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 return NO_ERROR;
1577 case 1002: // SHOW_UPDATES
1578 n = data.readInt32();
1579 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1580 return NO_ERROR;
1581 case 1003: // SHOW_BACKGROUND
1582 n = data.readInt32();
1583 mDebugBackground = n ? 1 : 0;
1584 return NO_ERROR;
Mathias Agopian6a969242010-09-22 18:58:01 -07001585 case 1008: // toggle use of hw composer
1586 n = data.readInt32();
1587 mDebugDisableHWC = n ? 1 : 0;
1588 mHwWorkListDirty = true;
1589 // fall-through...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 case 1004:{ // repaint everything
1591 Mutex::Autolock _l(mStateLock);
1592 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1593 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1594 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001595 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001597 case 1005:{ // force transaction
1598 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1599 return NO_ERROR;
1600 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001601 case 1006:{ // enable/disable GraphicLog
1602 int enabled = data.readInt32();
1603 GraphicLog::getInstance().setEnabled(enabled);
1604 return NO_ERROR;
1605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 case 1007: // set mFreezeCount
1607 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001608 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 return NO_ERROR;
1610 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001611 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 reply->writeInt32(0);
1613 reply->writeInt32(mDebugRegion);
1614 reply->writeInt32(mDebugBackground);
1615 return NO_ERROR;
1616 case 1013: {
1617 Mutex::Autolock _l(mStateLock);
1618 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1619 reply->writeInt32(hw.getPageFlipCount());
1620 }
1621 return NO_ERROR;
1622 }
1623 }
1624 return err;
1625}
1626
Mathias Agopianaab758e2010-10-11 12:37:43 -07001627// ---------------------------------------------------------------------------
1628
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001629status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1630 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001631{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001632 if (!GLExtensions::getInstance().haveFramebufferObject())
1633 return INVALID_OPERATION;
1634
1635 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001636 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001637 const uint32_t hw_w = hw.getWidth();
1638 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001639 GLfloat u = 1;
1640 GLfloat v = 1;
1641
1642 // make sure to clear all GL error flags
1643 while ( glGetError() != GL_NO_ERROR ) ;
1644
1645 // create a FBO
1646 GLuint name, tname;
1647 glGenTextures(1, &tname);
1648 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001649 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1650 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001651 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001652 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001653 GLint tw = (2 << (31 - clz(hw_w)));
1654 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001655 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1656 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001657 u = GLfloat(hw_w) / tw;
1658 v = GLfloat(hw_h) / th;
1659 }
1660 glGenFramebuffersOES(1, &name);
1661 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001662 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1663 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001664
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001665 // redraw the screen entirely...
1666 glClearColor(0,0,0,1);
1667 glClear(GL_COLOR_BUFFER_BIT);
1668 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1669 const size_t count = layers.size();
1670 for (size_t i=0 ; i<count ; ++i) {
1671 const sp<LayerBase>& layer(layers[i]);
1672 layer->drawForSreenShot();
1673 }
1674
1675 // back to main framebuffer
1676 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1677 glDisable(GL_SCISSOR_TEST);
1678 glDeleteFramebuffersOES(1, &name);
1679
1680 *textureName = tname;
1681 *uOut = u;
1682 *vOut = v;
1683 return NO_ERROR;
1684}
1685
1686// ---------------------------------------------------------------------------
1687
1688status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1689{
1690 status_t result = PERMISSION_DENIED;
1691
1692 if (!GLExtensions::getInstance().haveFramebufferObject())
1693 return INVALID_OPERATION;
1694
1695 // get screen geometry
1696 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1697 const uint32_t hw_w = hw.getWidth();
1698 const uint32_t hw_h = hw.getHeight();
1699 const Region screenBounds(hw.bounds());
1700
1701 GLfloat u, v;
1702 GLuint tname;
1703 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1704 if (result != NO_ERROR) {
1705 return result;
1706 }
1707
1708 GLfloat vtx[8];
1709 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1710 glEnable(GL_TEXTURE_2D);
1711 glBindTexture(GL_TEXTURE_2D, tname);
1712 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1713 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1714 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1715 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1716 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1717 glVertexPointer(2, GL_FLOAT, 0, vtx);
1718
1719 class s_curve_interpolator {
1720 const float nbFrames, s, v;
1721 public:
1722 s_curve_interpolator(int nbFrames, float s)
1723 : nbFrames(1.0f / (nbFrames-1)), s(s),
1724 v(1.0f + expf(-s + 0.5f*s)) {
1725 }
1726 float operator()(int f) {
1727 const float x = f * nbFrames;
1728 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1729 }
1730 };
1731
1732 class v_stretch {
1733 const GLfloat hw_w, hw_h;
1734 public:
1735 v_stretch(uint32_t hw_w, uint32_t hw_h)
1736 : hw_w(hw_w), hw_h(hw_h) {
1737 }
1738 void operator()(GLfloat* vtx, float v) {
1739 const GLfloat w = hw_w + (hw_w * v);
1740 const GLfloat h = hw_h - (hw_h * v);
1741 const GLfloat x = (hw_w - w) * 0.5f;
1742 const GLfloat y = (hw_h - h) * 0.5f;
1743 vtx[0] = x; vtx[1] = y;
1744 vtx[2] = x; vtx[3] = y + h;
1745 vtx[4] = x + w; vtx[5] = y + h;
1746 vtx[6] = x + w; vtx[7] = y;
1747 }
1748 };
1749
1750 class h_stretch {
1751 const GLfloat hw_w, hw_h;
1752 public:
1753 h_stretch(uint32_t hw_w, uint32_t hw_h)
1754 : hw_w(hw_w), hw_h(hw_h) {
1755 }
1756 void operator()(GLfloat* vtx, float v) {
1757 const GLfloat w = hw_w - (hw_w * v);
1758 const GLfloat h = 1.0f;
1759 const GLfloat x = (hw_w - w) * 0.5f;
1760 const GLfloat y = (hw_h - h) * 0.5f;
1761 vtx[0] = x; vtx[1] = y;
1762 vtx[2] = x; vtx[3] = y + h;
1763 vtx[4] = x + w; vtx[5] = y + h;
1764 vtx[6] = x + w; vtx[7] = y;
1765 }
1766 };
1767
1768 // the full animation is 24 frames
1769 const int nbFrames = 12;
1770 s_curve_interpolator itr(nbFrames, 7.5f);
1771 s_curve_interpolator itg(nbFrames, 8.0f);
1772 s_curve_interpolator itb(nbFrames, 8.5f);
1773
1774 v_stretch vverts(hw_w, hw_h);
1775 glEnable(GL_BLEND);
1776 glBlendFunc(GL_ONE, GL_ONE);
1777 for (int i=0 ; i<nbFrames ; i++) {
1778 float x, y, w, h;
1779 const float vr = itr(i);
1780 const float vg = itg(i);
1781 const float vb = itb(i);
1782
1783 // clear screen
1784 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001785 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001786 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001787
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001788 // draw the red plane
1789 vverts(vtx, vr);
1790 glColorMask(1,0,0,1);
1791 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001792
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001793 // draw the green plane
1794 vverts(vtx, vg);
1795 glColorMask(0,1,0,1);
1796 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001797
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001798 // draw the blue plane
1799 vverts(vtx, vb);
1800 glColorMask(0,0,1,1);
1801 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001802
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001803 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001804 glDisable(GL_TEXTURE_2D);
1805 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001806 glColor4f(vg, vg, vg, 1);
1807 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1808 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001809 }
1810
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001811 h_stretch hverts(hw_w, hw_h);
1812 glDisable(GL_BLEND);
1813 glDisable(GL_TEXTURE_2D);
1814 glColorMask(1,1,1,1);
1815 for (int i=0 ; i<nbFrames ; i++) {
1816 const float v = itg(i);
1817 hverts(vtx, v);
1818 glClear(GL_COLOR_BUFFER_BIT);
1819 glColor4f(1-v, 1-v, 1-v, 1);
1820 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1821 hw.flip(screenBounds);
1822 }
1823
1824 glColorMask(1,1,1,1);
1825 glEnable(GL_SCISSOR_TEST);
1826 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1827 glDeleteTextures(1, &tname);
1828 return NO_ERROR;
1829}
1830
1831status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1832{
1833 status_t result = PERMISSION_DENIED;
1834
1835 if (!GLExtensions::getInstance().haveFramebufferObject())
1836 return INVALID_OPERATION;
1837
1838
1839 // get screen geometry
1840 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1841 const uint32_t hw_w = hw.getWidth();
1842 const uint32_t hw_h = hw.getHeight();
1843 const Region screenBounds(hw.bounds());
1844
1845 GLfloat u, v;
1846 GLuint tname;
1847 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1848 if (result != NO_ERROR) {
1849 return result;
1850 }
1851
1852 // back to main framebuffer
1853 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1854 glDisable(GL_SCISSOR_TEST);
1855
1856 GLfloat vtx[8];
1857 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1858 glEnable(GL_TEXTURE_2D);
1859 glBindTexture(GL_TEXTURE_2D, tname);
1860 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1861 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1862 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1863 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1864 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1865 glVertexPointer(2, GL_FLOAT, 0, vtx);
1866
1867 class s_curve_interpolator {
1868 const float nbFrames, s, v;
1869 public:
1870 s_curve_interpolator(int nbFrames, float s)
1871 : nbFrames(1.0f / (nbFrames-1)), s(s),
1872 v(1.0f + expf(-s + 0.5f*s)) {
1873 }
1874 float operator()(int f) {
1875 const float x = f * nbFrames;
1876 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1877 }
1878 };
1879
1880 class v_stretch {
1881 const GLfloat hw_w, hw_h;
1882 public:
1883 v_stretch(uint32_t hw_w, uint32_t hw_h)
1884 : hw_w(hw_w), hw_h(hw_h) {
1885 }
1886 void operator()(GLfloat* vtx, float v) {
1887 const GLfloat w = hw_w + (hw_w * v);
1888 const GLfloat h = hw_h - (hw_h * v);
1889 const GLfloat x = (hw_w - w) * 0.5f;
1890 const GLfloat y = (hw_h - h) * 0.5f;
1891 vtx[0] = x; vtx[1] = y;
1892 vtx[2] = x; vtx[3] = y + h;
1893 vtx[4] = x + w; vtx[5] = y + h;
1894 vtx[6] = x + w; vtx[7] = y;
1895 }
1896 };
1897
1898 class h_stretch {
1899 const GLfloat hw_w, hw_h;
1900 public:
1901 h_stretch(uint32_t hw_w, uint32_t hw_h)
1902 : hw_w(hw_w), hw_h(hw_h) {
1903 }
1904 void operator()(GLfloat* vtx, float v) {
1905 const GLfloat w = hw_w - (hw_w * v);
1906 const GLfloat h = 1.0f;
1907 const GLfloat x = (hw_w - w) * 0.5f;
1908 const GLfloat y = (hw_h - h) * 0.5f;
1909 vtx[0] = x; vtx[1] = y;
1910 vtx[2] = x; vtx[3] = y + h;
1911 vtx[4] = x + w; vtx[5] = y + h;
1912 vtx[6] = x + w; vtx[7] = y;
1913 }
1914 };
1915
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07001916 // the full animation is 12 frames
1917 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001918 s_curve_interpolator itr(nbFrames, 7.5f);
1919 s_curve_interpolator itg(nbFrames, 8.0f);
1920 s_curve_interpolator itb(nbFrames, 8.5f);
1921
1922 h_stretch hverts(hw_w, hw_h);
1923 glDisable(GL_BLEND);
1924 glDisable(GL_TEXTURE_2D);
1925 glColorMask(1,1,1,1);
1926 for (int i=nbFrames-1 ; i>=0 ; i--) {
1927 const float v = itg(i);
1928 hverts(vtx, v);
1929 glClear(GL_COLOR_BUFFER_BIT);
1930 glColor4f(1-v, 1-v, 1-v, 1);
1931 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1932 hw.flip(screenBounds);
1933 }
1934
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07001935 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001936 v_stretch vverts(hw_w, hw_h);
1937 glEnable(GL_BLEND);
1938 glBlendFunc(GL_ONE, GL_ONE);
1939 for (int i=nbFrames-1 ; i>=0 ; i--) {
1940 float x, y, w, h;
1941 const float vr = itr(i);
1942 const float vg = itg(i);
1943 const float vb = itb(i);
1944
1945 // clear screen
1946 glColorMask(1,1,1,1);
1947 glClear(GL_COLOR_BUFFER_BIT);
1948 glEnable(GL_TEXTURE_2D);
1949
1950 // draw the red plane
1951 vverts(vtx, vr);
1952 glColorMask(1,0,0,1);
1953 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1954
1955 // draw the green plane
1956 vverts(vtx, vg);
1957 glColorMask(0,1,0,1);
1958 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1959
1960 // draw the blue plane
1961 vverts(vtx, vb);
1962 glColorMask(0,0,1,1);
1963 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1964
1965 hw.flip(screenBounds);
1966 }
1967
1968 glColorMask(1,1,1,1);
1969 glEnable(GL_SCISSOR_TEST);
1970 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001971 glDeleteTextures(1, &tname);
1972
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001973 return NO_ERROR;
1974}
1975
1976// ---------------------------------------------------------------------------
1977
Mathias Agopiand4e03f32010-10-14 14:54:06 -07001978status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001979{
1980 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1981 if (!hw.canDraw()) {
1982 // we're already off
1983 return NO_ERROR;
1984 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07001985 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
1986 electronBeamOffAnimationImplLocked();
1987 }
1988
1989 // always clear the whole screen at the end of the animation
1990 glClearColor(0,0,0,1);
1991 glDisable(GL_SCISSOR_TEST);
1992 glClear(GL_COLOR_BUFFER_BIT);
1993 glEnable(GL_SCISSOR_TEST);
1994 hw.flip( Region(hw.bounds()) );
1995
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001996 hw.setCanDraw(false);
1997 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001998}
1999
2000status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2001{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002002 class MessageTurnElectronBeamOff : public MessageBase {
2003 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002004 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002005 status_t result;
2006 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002007 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2008 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002009 }
2010 status_t getResult() const {
2011 return result;
2012 }
2013 virtual bool handler() {
2014 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002015 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002016 return true;
2017 }
2018 };
2019
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002020 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002021 status_t res = postMessageSync(msg);
2022 if (res == NO_ERROR) {
2023 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002024
2025 // work-around: when the power-manager calls us we activate the
2026 // animation. eventually, the "on" animation will be called
2027 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002028 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002029 }
2030 return res;
2031}
2032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002034
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002035status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002036{
2037 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2038 if (hw.canDraw()) {
2039 // we're already on
2040 return NO_ERROR;
2041 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002042 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2043 electronBeamOnAnimationImplLocked();
2044 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002045 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002046
2047 // make sure to redraw the whole screen when the animation is done
2048 mDirtyRegion.set(hw.bounds());
2049 signalEvent();
2050
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002051 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002052}
2053
2054status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2055{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002056 class MessageTurnElectronBeamOn : public MessageBase {
2057 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002058 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002059 status_t result;
2060 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002061 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2062 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002063 }
2064 status_t getResult() const {
2065 return result;
2066 }
2067 virtual bool handler() {
2068 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002069 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002070 return true;
2071 }
2072 };
2073
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002074 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002075 return NO_ERROR;
2076}
2077
2078// ---------------------------------------------------------------------------
2079
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002080status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2081 sp<IMemoryHeap>* heap,
2082 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002083 uint32_t sw, uint32_t sh,
2084 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002085{
2086 status_t result = PERMISSION_DENIED;
2087
2088 // only one display supported for now
2089 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2090 return BAD_VALUE;
2091
2092 if (!GLExtensions::getInstance().haveFramebufferObject())
2093 return INVALID_OPERATION;
2094
2095 // get screen geometry
2096 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2097 const uint32_t hw_w = hw.getWidth();
2098 const uint32_t hw_h = hw.getHeight();
2099
2100 if ((sw > hw_w) || (sh > hw_h))
2101 return BAD_VALUE;
2102
2103 sw = (!sw) ? hw_w : sw;
2104 sh = (!sh) ? hw_h : sh;
2105 const size_t size = sw * sh * 4;
2106
2107 // make sure to clear all GL error flags
2108 while ( glGetError() != GL_NO_ERROR ) ;
2109
2110 // create a FBO
2111 GLuint name, tname;
2112 glGenRenderbuffersOES(1, &tname);
2113 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2114 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2115 glGenFramebuffersOES(1, &name);
2116 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2117 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2118 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2119
2120 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2121 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2122
2123 // invert everything, b/c glReadPixel() below will invert the FB
2124 glViewport(0, 0, sw, sh);
2125 glMatrixMode(GL_PROJECTION);
2126 glPushMatrix();
2127 glLoadIdentity();
2128 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2129 glMatrixMode(GL_MODELVIEW);
2130
2131 // redraw the screen entirely...
2132 glClearColor(0,0,0,1);
2133 glClear(GL_COLOR_BUFFER_BIT);
2134 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2135 const size_t count = layers.size();
2136 for (size_t i=0 ; i<count ; ++i) {
2137 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002138 const uint32_t z = layer->drawingState().z;
2139 if (z >= minLayerZ && z <= maxLayerZ) {
2140 layer->drawForSreenShot();
2141 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002142 }
2143
2144 // XXX: this is needed on tegra
2145 glScissor(0, 0, sw, sh);
2146
2147 // check for errors and return screen capture
2148 if (glGetError() != GL_NO_ERROR) {
2149 // error while rendering
2150 result = INVALID_OPERATION;
2151 } else {
2152 // allocate shared memory large enough to hold the
2153 // screen capture
2154 sp<MemoryHeapBase> base(
2155 new MemoryHeapBase(size, 0, "screen-capture") );
2156 void* const ptr = base->getBase();
2157 if (ptr) {
2158 // capture the screen with glReadPixels()
2159 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2160 if (glGetError() == GL_NO_ERROR) {
2161 *heap = base;
2162 *w = sw;
2163 *h = sh;
2164 *f = PIXEL_FORMAT_RGBA_8888;
2165 result = NO_ERROR;
2166 }
2167 } else {
2168 result = NO_MEMORY;
2169 }
2170 }
2171
2172 glEnable(GL_SCISSOR_TEST);
2173 glViewport(0, 0, hw_w, hw_h);
2174 glMatrixMode(GL_PROJECTION);
2175 glPopMatrix();
2176 glMatrixMode(GL_MODELVIEW);
2177
2178
2179 } else {
2180 result = BAD_VALUE;
2181 }
2182
2183 // release FBO resources
2184 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2185 glDeleteRenderbuffersOES(1, &tname);
2186 glDeleteFramebuffersOES(1, &name);
2187 return result;
2188}
2189
2190
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002191status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2192 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002193 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002194 uint32_t sw, uint32_t sh,
2195 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002196{
2197 // only one display supported for now
2198 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2199 return BAD_VALUE;
2200
2201 if (!GLExtensions::getInstance().haveFramebufferObject())
2202 return INVALID_OPERATION;
2203
2204 class MessageCaptureScreen : public MessageBase {
2205 SurfaceFlinger* flinger;
2206 DisplayID dpy;
2207 sp<IMemoryHeap>* heap;
2208 uint32_t* w;
2209 uint32_t* h;
2210 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002211 uint32_t sw;
2212 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002213 uint32_t minLayerZ;
2214 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002215 status_t result;
2216 public:
2217 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002218 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002219 uint32_t sw, uint32_t sh,
2220 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002221 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002222 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2223 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2224 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002225 {
2226 }
2227 status_t getResult() const {
2228 return result;
2229 }
2230 virtual bool handler() {
2231 Mutex::Autolock _l(flinger->mStateLock);
2232
2233 // if we have secure windows, never allow the screen capture
2234 if (flinger->mSecureFrameBuffer)
2235 return true;
2236
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002237 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002238 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002239
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002240 return true;
2241 }
2242 };
2243
2244 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002245 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002246 status_t res = postMessageSync(msg);
2247 if (res == NO_ERROR) {
2248 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2249 }
2250 return res;
2251}
2252
2253// ---------------------------------------------------------------------------
2254
Mathias Agopian7623da42010-06-01 15:12:58 -07002255sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2256{
2257 sp<Layer> result;
2258 Mutex::Autolock _l(mStateLock);
2259 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2260 return result;
2261}
2262
2263// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264
Mathias Agopian593c05c2010-06-02 23:28:45 -07002265Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002266 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268}
2269
Mathias Agopian593c05c2010-06-02 23:28:45 -07002270Client::~Client()
2271{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002272 const size_t count = mLayers.size();
2273 for (size_t i=0 ; i<count ; i++) {
2274 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2275 if (layer != 0) {
2276 mFlinger->removeLayer(layer);
2277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 }
2279}
2280
Mathias Agopian593c05c2010-06-02 23:28:45 -07002281status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002282 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283}
Mathias Agopian1473f462009-04-10 14:24:30 -07002284
Mathias Agopian593c05c2010-06-02 23:28:45 -07002285ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2286{
Mathias Agopian7623da42010-06-01 15:12:58 -07002287 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002288 mLayers.add(name, layer);
2289 return name;
2290}
2291
Mathias Agopian7623da42010-06-01 15:12:58 -07002292void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002293{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002294 // we do a linear search here, because this doesn't happen often
2295 const size_t count = mLayers.size();
2296 for (size_t i=0 ; i<count ; i++) {
2297 if (mLayers.valueAt(i) == layer) {
2298 mLayers.removeItemsAt(i, 1);
2299 break;
2300 }
2301 }
2302}
Mathias Agopian1473f462009-04-10 14:24:30 -07002303sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2304 sp<LayerBaseClient> lbc;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002305 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2306 if (layer != 0) {
2307 lbc = layer.promote();
2308 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002309 }
2310 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311}
2312
Mathias Agopian593c05c2010-06-02 23:28:45 -07002313sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002314 return 0;
2315}
2316ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2317 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002319sp<ISurface> Client::createSurface(
Mathias Agopian7623da42010-06-01 15:12:58 -07002320 ISurfaceComposerClient::surface_data_t* params, int pid,
2321 const String8& name,
2322 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 uint32_t flags)
2324{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002325 return mFlinger->createSurface(this, pid, name, params,
2326 display, w, h, format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002328status_t Client::destroySurface(SurfaceID sid) {
2329 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002331status_t Client::setState(int32_t count, const layer_state_t* states) {
2332 return mFlinger->setClientState(this, count, states);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333}
2334
2335// ---------------------------------------------------------------------------
2336
Mathias Agopian7623da42010-06-01 15:12:58 -07002337UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2338 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2339{
2340 const int pgsize = getpagesize();
2341 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2342
2343 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2344 "SurfaceFlinger Client control-block");
2345
2346 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2347 if (ctrlblk) { // construct the shared structure in-place.
2348 new(ctrlblk) SharedClient;
2349 }
2350}
2351
2352UserClient::~UserClient()
2353{
2354 if (ctrlblk) {
2355 ctrlblk->~SharedClient(); // destroy our shared-structure.
2356 }
2357
2358 /*
2359 * When a UserClient dies, it's unclear what to do exactly.
2360 * We could go ahead and destroy all surfaces linked to that client
2361 * however, it wouldn't be fair to the main Client
2362 * (usually the the window-manager), which might want to re-target
2363 * the layer to another UserClient.
2364 * I think the best is to do nothing, or not much; in most cases the
2365 * WM itself will go ahead and clean things up when it detects a client of
2366 * his has died.
2367 * The remaining question is what to display? currently we keep
2368 * just keep the current buffer.
2369 */
2370}
2371
2372status_t UserClient::initCheck() const {
2373 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2374}
2375
2376void UserClient::detachLayer(const Layer* layer)
2377{
2378 int32_t name = layer->getToken();
2379 if (name >= 0) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002380 int32_t mask = 1LU<<name;
2381 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2382 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2383 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002384 }
2385}
2386
2387sp<IMemoryHeap> UserClient::getControlBlock() const {
2388 return mCblkHeap;
2389}
2390
2391ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2392{
2393 int32_t name = NAME_NOT_FOUND;
2394 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002395 if (layer == 0) {
2396 return name;
2397 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002398
Mathias Agopian5e140102010-06-08 19:54:15 -07002399 // if this layer already has a token, just return it
Mathias Agopian7623da42010-06-01 15:12:58 -07002400 name = layer->getToken();
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002401 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002402 return name;
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002403 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002404
2405 name = 0;
2406 do {
2407 int32_t mask = 1LU<<name;
2408 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2409 // we found and locked that name
Mathias Agopian5e140102010-06-08 19:54:15 -07002410 status_t err = layer->setToken(
2411 const_cast<UserClient*>(this), ctrlblk, name);
2412 if (err != NO_ERROR) {
2413 // free the name
2414 android_atomic_and(~mask, &mBitmap);
2415 name = err;
2416 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002417 break;
2418 }
2419 if (++name > 31)
2420 name = NO_MEMORY;
2421 } while(name >= 0);
2422
Mathias Agopian1debc662010-06-08 15:40:56 -07002423 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2424 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopian7623da42010-06-01 15:12:58 -07002425 return name;
2426}
2427
2428sp<ISurface> UserClient::createSurface(
2429 ISurfaceComposerClient::surface_data_t* params, int pid,
2430 const String8& name,
2431 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2432 uint32_t flags) {
2433 return 0;
2434}
2435status_t UserClient::destroySurface(SurfaceID sid) {
2436 return INVALID_OPERATION;
2437}
2438status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2439 return INVALID_OPERATION;
2440}
2441
2442// ---------------------------------------------------------------------------
2443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444GraphicPlane::GraphicPlane()
2445 : mHw(0)
2446{
2447}
2448
2449GraphicPlane::~GraphicPlane() {
2450 delete mHw;
2451}
2452
2453bool GraphicPlane::initialized() const {
2454 return mHw ? true : false;
2455}
2456
Mathias Agopian66c77a52010-02-08 15:49:35 -08002457int GraphicPlane::getWidth() const {
2458 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459}
2460
Mathias Agopian66c77a52010-02-08 15:49:35 -08002461int GraphicPlane::getHeight() const {
2462 return mHeight;
2463}
2464
2465void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2466{
2467 mHw = hw;
2468
2469 // initialize the display orientation transform.
2470 // it's a constant that should come from the display driver.
2471 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2472 char property[PROPERTY_VALUE_MAX];
2473 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2474 //displayOrientation
2475 switch (atoi(property)) {
2476 case 90:
2477 displayOrientation = ISurfaceComposer::eOrientation90;
2478 break;
2479 case 270:
2480 displayOrientation = ISurfaceComposer::eOrientation270;
2481 break;
2482 }
2483 }
2484
2485 const float w = hw->getWidth();
2486 const float h = hw->getHeight();
2487 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2488 &mDisplayTransform);
2489 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2490 mDisplayWidth = h;
2491 mDisplayHeight = w;
2492 } else {
2493 mDisplayWidth = w;
2494 mDisplayHeight = h;
2495 }
2496
2497 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498}
2499
2500status_t GraphicPlane::orientationToTransfrom(
2501 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002502{
2503 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 switch (orientation) {
2505 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002506 flags = Transform::ROT_0;
2507 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002509 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 break;
2511 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002512 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 break;
2514 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002515 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 break;
2517 default:
2518 return BAD_VALUE;
2519 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002520 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 return NO_ERROR;
2522}
2523
2524status_t GraphicPlane::setOrientation(int orientation)
2525{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 // If the rotation can be handled in hardware, this is where
2527 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002528
2529 const DisplayHardware& hw(displayHardware());
2530 const float w = mDisplayWidth;
2531 const float h = mDisplayHeight;
2532 mWidth = int(w);
2533 mHeight = int(h);
2534
2535 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002536 GraphicPlane::orientationToTransfrom(orientation, w, h,
2537 &orientationTransform);
2538 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2539 mWidth = int(h);
2540 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002542
Mathias Agopian3552f532009-03-27 17:58:20 -07002543 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002544 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 return NO_ERROR;
2546}
2547
2548const DisplayHardware& GraphicPlane::displayHardware() const {
2549 return *mHw;
2550}
2551
Mathias Agopianaab758e2010-10-11 12:37:43 -07002552DisplayHardware& GraphicPlane::editDisplayHardware() {
2553 return *mHw;
2554}
2555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556const Transform& GraphicPlane::transform() const {
2557 return mGlobalTransform;
2558}
2559
Mathias Agopian1473f462009-04-10 14:24:30 -07002560EGLDisplay GraphicPlane::getEGLDisplay() const {
2561 return mHw->getEGLDisplay();
2562}
2563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564// ---------------------------------------------------------------------------
2565
2566}; // namespace android