blob: 61d08aabdc20651dc605332fb0baf459489af75f [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070054#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopiana1ecca92009-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 Projectedbf3b62009-03-03 19:31:44 -080063#define DISPLAY_COUNT 1
64
65namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066// ---------------------------------------------------------------------------
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068SurfaceFlinger::SurfaceFlinger()
69 : BnSurfaceComposer(), Thread(false),
70 mTransactionFlags(0),
71 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070072 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070075 mHardwareTest("android.permission.HARDWARE_TEST"),
76 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070077 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070078 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070080 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070083 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070088 mDebugDisableHWC(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070089 mDebugInSwapBuffers(0),
90 mLastSwapBufferTime(0),
91 mDebugInTransaction(0),
92 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070093 mBootFinished(false),
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-03-03 19:31:44 -0800108 property_get("debug.sf.showbackground", value, "0");
109 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Mathias Agopian78fd5012010-04-20 14:51:04 -0700111 LOGI_IF(mDebugRegion, "showupdates enabled");
112 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
115SurfaceFlinger::~SurfaceFlinger()
116{
117 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118}
119
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700120sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700122 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123}
124
Mathias Agopian7e27f052010-05-28 14:22:23 -0700125sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126{
Mathias Agopian96f08192010-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 Projectedbf3b62009-03-03 19:31:44 -0800132 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 return bclient;
134}
135
Mathias Agopianb7e930d2010-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 Projectedbf3b62009-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 Huber8b42e8a2010-08-16 08:49:37 -0700165 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700166 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700167 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-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 Projectedbf3b62009-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 Projectedbf3b62009-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 Agopian7303c6b2009-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 Huber8b42e8a2010-08-16 08:49:37 -0700201
Mathias Agopian7303c6b2009-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 Huber8b42e8a2010-08-16 08:49:37 -0700204
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700205 new(mServerCblk) surface_flinger_cblk_t;
206
The Android Open Source Projectedbf3b62009-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 Agopian2b92d892010-02-08 15:49:35 -0800222 dcblk->w = plane.getWidth();
223 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-03-03 19:31:44 -0800230
231 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700233 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-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 Agopiana1ecca92009-05-21 19:21:59 -0700265 // start boot animation
266 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700267
The Android Open Source Projectedbf3b62009-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 Agopianf1d8e872009-04-20 19:39:12 -0700279 while (true) {
280 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800281 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700282 if (UNLIKELY(isFrozen())) {
283 // wait 5 seconds
Mathias Agopianf1d8e872009-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 Projectbcef13b2009-03-11 12:11:56 -0700290 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700291
Mathias Agopianbb641242010-05-18 17:06:55 -0700292 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-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 Agopianf1d8e872009-04-20 19:39:12 -0700308 if (msg != 0) {
Mathias Agopianf1d8e872009-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 Projectedbf3b62009-03-03 19:31:44 -0800314 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 }
316}
317
318void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700319 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320}
321
322void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700323 // this is the IPC call
324 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
Mathias Agopianbb641242010-05-18 17:06:55 -0700327status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
328 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329{
Mathias Agopianbb641242010-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 Projectedbf3b62009-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 Agopiana350ff92010-08-10 17:14:02 -0700370 if (UNLIKELY(mHwWorkListDirty)) {
371 // build the h/w work list
372 handleWorkList();
373 }
374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700376 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-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 Projectedbf3b62009-03-03 19:31:44 -0800383 handleRepaint();
384
Mathias Agopian74faca22009-09-17 16:18:16 -0700385 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700386 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700387 hw.compositionComplete();
388
Mathias Agopian35b48d12010-09-13 22:57:58 -0700389 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700390 postFramebuffer();
391
Mathias Agopianc479e182010-09-15 12:29:18 -0700392 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394
Mathias Agopian35b48d12010-09-13 22:57:58 -0700395 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396 } else {
397 // pretend we did the post
398 unlockClients();
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-03-03 19:31:44 -0800406 if (!mInvalidRegion.isEmpty()) {
407 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700408 const nsecs_t now = systemTime();
409 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700411 mLastSwapBufferTime = systemTime() - now;
412 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-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 Agopian9daa5c92010-10-12 16:05:48 -0700425 // this is a temporary work-around, eventually this should be called
426 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700427 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 }
429
Mathias Agopian59119e62010-10-11 12:37:43 -0700430 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700431 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 hw.releaseScreen();
434 }
435
436 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700437 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-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 Agopian3d579642009-06-04 18:46:21 -0700449 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450
Mathias Agopian4da75192010-08-10 17:19:56 -0700451 /*
452 * Perform and commit the transaction
453 */
454
Mathias Agopian3d579642009-06-04 18:46:21 -0700455 { // scope for the lock
456 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700457 const nsecs_t now = systemTime();
458 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700459 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700460 mLastTransactionTime = systemTime() - now;
461 mDebugInTransaction = 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700462 mHwWorkListDirty = true;
Mathias Agopian4da75192010-08-10 17:19:56 -0700463 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700464 }
465
Mathias Agopian4da75192010-08-10 17:19:56 -0700466 /*
467 * Clean-up all layers that went away
468 * (do this without the lock held)
469 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700470
Mathias Agopian3d579642009-06-04 18:46:21 -0700471 const size_t count = ditchedLayers.size();
472 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700473 if (ditchedLayers[i] != 0) {
474 //LOGD("ditching layer %p", ditchedLayers[i].get());
475 ditchedLayers[i]->ditch();
476 }
Mathias Agopian3d579642009-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 Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700494 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-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 Agopianc08731e2009-03-27 18:11:38 -0700515 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-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 Agopian2b92d892010-02-08 15:49:35 -0800523 dcblk->w = plane.getWidth();
524 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525
526 mVisibleRegionsDirty = true;
527 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-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 Agopian064e1e62010-03-01 17:51:17 -0800533 if (mFreezeDisplay)
534 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 }
536
Mathias Agopian0aa758d2009-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 Projectedbf3b62009-03-03 19:31:44 -0800542 // some layers might have been removed, so
543 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700544 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700545 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700547 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700548 const size_t count = previousLayers.size();
549 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-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 Agopian3d579642009-06-04 18:46:21 -0700553 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700554 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700555 }
556 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 }
The Android Open Source Projectedbf3b62009-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 Agopianab028732010-03-16 16:41:46 -0700573 const DisplayHardware& hw(plane.displayHardware());
574 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700584 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 layer->validateVisibility(planeTransform);
586
587 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700588 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589
Mathias Agopianab028732010-03-16 16:41:46 -0700590 /*
591 * opaqueRegion: area of a surface that is fully opaque.
592 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 Region opaqueRegion;
Mathias Agopianab028732010-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 Projectedbf3b62009-03-03 19:31:44 -0800601 Region visibleRegion;
Mathias Agopianab028732010-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 Projectedbf3b62009-03-03 19:31:44 -0800607 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700608
609
610 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700611 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700613 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 visibleRegion.set(bounds);
Mathias Agopianab028732010-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 Projectedbf3b62009-03-03 19:31:44 -0800621
Mathias Agopianab028732010-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 Projectedbf3b62009-03-03 19:31:44 -0800629 }
630 }
631
Mathias Agopianab028732010-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 Projectedbf3b62009-03-03 19:31:44 -0800638 // subtract the opaque region covered by the layers above us
639 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-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 Agopiana8d44f72009-06-28 02:54:16 -0700649 /* compute the exposed region:
Mathias Agopianab028732010-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 Agopiana8d44f72009-06-28 02:54:16 -0700660 */
Mathias Agopianab028732010-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 Projectedbf3b62009-03-03 19:31:44 -0800666 }
667 dirty.subtractSelf(aboveOpaqueLayers);
668
669 // accumulate to the screen dirty region
670 dirtyRegion.orSelf(dirty);
671
Mathias Agopianab028732010-03-16 16:41:46 -0700672 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700674
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 // Store the visible region is screen space
676 layer->setVisibleRegion(visibleRegion);
677 layer->setCoveredRegion(coveredRegion);
678
Mathias Agopian97011222009-07-28 10:57:27 -0700679 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 if (layer->isSecure() && !visibleRegion.isEmpty()) {
681 secureFrameBuffer = true;
682 }
683 }
684
Mathias Agopian97011222009-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 Projectedbf3b62009-03-03 19:31:44 -0800689 mSecureFrameBuffer = secureFrameBuffer;
690 opaqueRegion = aboveOpaqueLayers;
691}
692
693
694void SurfaceFlinger::commitTransaction()
695{
696 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700697 mResizeTransationPending = false;
698 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699}
700
701void SurfaceFlinger::handlePageFlip()
702{
703 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700704 LayerVector& currentLayers(
705 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-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 Agopian4da75192010-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 Projectedbf3b62009-03-03 19:31:44 -0800726 mWormholeRegion = screenRegion.subtract(opaqueRegion);
727 mVisibleRegionsDirty = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700728 mHwWorkListDirty = true;
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700739 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700741 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700752 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700754 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 layer->unlockPageFlip(planeTransform, mDirtyRegion);
756 }
757}
758
Mathias Agopiana350ff92010-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 Agopian45721772010-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 Agopian73d3ba92010-09-22 18:58:01 -0700770 if (mDebugDisableHWC) {
771 cur[i].compositionType = HWC_FRAMEBUFFER;
772 cur[i].flags |= HWC_SKIP_LAYER;
773 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700774 }
775 }
776}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778void SurfaceFlinger::handleRepaint()
779{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700780 // compute the invalid region
781 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782
783 if (UNLIKELY(mDebugRegion)) {
784 debugFlashRegions();
785 }
786
Mathias Agopianb8a55602009-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 Projectedbf3b62009-03-03 19:31:44 -0800791
792 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700793 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
794 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700795 {
Mathias Agopian29d06ac2009-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 Agopianb7e930d2010-06-01 15:12:58 -0700800 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700801 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 mDirtyRegion.set(mInvalidRegion.bounds());
803 } else {
Mathias Agopian29d06ac2009-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 Agopian95a666b2009-09-24 14:57:26 -0700810 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700811 // We need to redraw the rectangle that will be updated
812 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700813 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-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 Projectedbf3b62009-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 Agopiana350ff92010-08-10 17:14:02 -0700837
838 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700839 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700840 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700841
842 const DisplayHardware& hw(graphicPlane(0).displayHardware());
843 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700844 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700845
Mathias Agopian45721772010-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 Gilling24925bf2010-08-12 23:21:40 -0700851 if (hwc.initCheck() == NO_ERROR) {
852 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
853 }
Mathias Agopian45721772010-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 Agopiana350ff92010-08-10 17:14:02 -0700869 }
870 err = hwc.prepare();
871 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
872 }
873
Mathias Agopian45721772010-08-12 15:03:26 -0700874 /*
875 * clear the area of the FB that need to be transparent
876 */
Mathias Agopiana350ff92010-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 Agopian45721772010-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 Hatala586a0de2010-09-09 02:32:30 -0700897 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
898 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700899 // skip layers handled by the HAL
900 continue;
901 }
902 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700903
Mathias Agopian45721772010-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 Projectedbf3b62009-03-03 19:31:44 -0800910}
911
912void SurfaceFlinger::unlockClients()
913{
914 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
915 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700916 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700918 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 layer->finishPageFlip();
920 }
921}
922
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923void SurfaceFlinger::debugFlashRegions()
924{
Mathias Agopian0a917752010-06-14 21:20:00 -0700925 const DisplayHardware& hw(graphicPlane(0).displayHardware());
926 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700927
Mathias Agopian0a917752010-06-14 21:20:00 -0700928 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
929 (flags & DisplayHardware::BUFFER_PRESERVED))) {
930 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
931 mDirtyRegion.bounds() : hw.bounds());
932 composeSurfaces(repaint);
933 }
934
935 TextureManager::deactivateTextures();
936
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 glDisable(GL_BLEND);
938 glDisable(GL_DITHER);
939 glDisable(GL_SCISSOR_TEST);
940
Mathias Agopian0926f502009-05-04 14:17:04 -0700941 static int toggle = 0;
942 toggle = 1 - toggle;
943 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700944 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700945 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700946 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700947 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948
Mathias Agopian20f68782009-05-11 00:03:41 -0700949 Region::const_iterator it = mDirtyRegion.begin();
950 Region::const_iterator const end = mDirtyRegion.end();
951 while (it != end) {
952 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 GLfloat vertices[][2] = {
954 { r.left, r.top },
955 { r.left, r.bottom },
956 { r.right, r.bottom },
957 { r.right, r.top }
958 };
959 glVertexPointer(2, GL_FLOAT, 0, vertices);
960 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
961 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700962
Mathias Agopianb8a55602009-06-26 19:06:36 -0700963 if (mInvalidRegion.isEmpty()) {
964 mDirtyRegion.dump("mDirtyRegion");
965 mInvalidRegion.dump("mInvalidRegion");
966 }
967 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968
969 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700970 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
972 glEnable(GL_SCISSOR_TEST);
973 //mDirtyRegion.dump("mDirtyRegion");
974}
975
976void SurfaceFlinger::drawWormhole() const
977{
978 const Region region(mWormholeRegion.intersect(mDirtyRegion));
979 if (region.isEmpty())
980 return;
981
982 const DisplayHardware& hw(graphicPlane(0).displayHardware());
983 const int32_t width = hw.getWidth();
984 const int32_t height = hw.getHeight();
985
986 glDisable(GL_BLEND);
987 glDisable(GL_DITHER);
988
989 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700990 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700991 Region::const_iterator it = region.begin();
992 Region::const_iterator const end = region.end();
993 while (it != end) {
994 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 const GLint sy = height - (r.top + r.height());
996 glScissor(r.left, sy, r.width(), r.height());
997 glClear(GL_COLOR_BUFFER_BIT);
998 }
999 } else {
1000 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1001 { width, height }, { 0, height } };
1002 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1003 glVertexPointer(2, GL_SHORT, 0, vertices);
1004 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1005 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001006#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001007 if (GLExtensions::getInstance().haveTextureExternal()) {
1008 glDisable(GL_TEXTURE_EXTERNAL_OES);
1009 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001010#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 glEnable(GL_TEXTURE_2D);
1012 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1013 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1014 glMatrixMode(GL_TEXTURE);
1015 glLoadIdentity();
1016 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001017 Region::const_iterator it = region.begin();
1018 Region::const_iterator const end = region.end();
1019 while (it != end) {
1020 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 const GLint sy = height - (r.top + r.height());
1022 glScissor(r.left, sy, r.width(), r.height());
1023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1024 }
1025 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1026 }
1027}
1028
1029void SurfaceFlinger::debugShowFPS() const
1030{
1031 static int mFrameCount;
1032 static int mLastFrameCount = 0;
1033 static nsecs_t mLastFpsTime = 0;
1034 static float mFps = 0;
1035 mFrameCount++;
1036 nsecs_t now = systemTime();
1037 nsecs_t diff = now - mLastFpsTime;
1038 if (diff > ms2ns(250)) {
1039 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1040 mLastFpsTime = now;
1041 mLastFrameCount = mFrameCount;
1042 }
1043 // XXX: mFPS has the value we want
1044 }
1045
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001046status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047{
1048 Mutex::Autolock _l(mStateLock);
1049 addLayer_l(layer);
1050 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1051 return NO_ERROR;
1052}
1053
Mathias Agopian96f08192010-06-02 23:28:45 -07001054status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1055{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001056 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001057 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1058}
1059
1060ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1061 const sp<LayerBaseClient>& lbc)
1062{
1063 Mutex::Autolock _l(mStateLock);
1064
1065 // attach this layer to the client
1066 ssize_t name = client->attachLayer(lbc);
1067
1068 // add this layer to the current state list
1069 addLayer_l(lbc);
1070
1071 return name;
1072}
1073
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075{
1076 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001077 status_t err = purgatorizeLayer_l(layer);
1078 if (err == NO_ERROR)
1079 setTransactionFlags(eTransactionNeeded);
1080 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081}
1082
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001083status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001085 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1086 if (lbc != 0) {
1087 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1088 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1090 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001091 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 return NO_ERROR;
1093 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001094 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopian9a112062009-04-17 19:36:26 -07001097status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1098{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001099 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001100 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001101
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001102 layerBase->onRemoved();
1103
Mathias Agopian3d579642009-06-04 18:46:21 -07001104 // it's possible that we don't find a layer, because it might
1105 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001106 // from the user because there is a race between Client::destroySurface(),
1107 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001108 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1109}
1110
Mathias Agopian96f08192010-06-02 23:28:45 -07001111status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112{
Mathias Agopian96f08192010-06-02 23:28:45 -07001113 layer->forceVisibilityTransaction();
1114 setTransactionFlags(eTraversalNeeded);
1115 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116}
1117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1119{
1120 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1121}
1122
Mathias Agopianbb641242010-05-18 17:06:55 -07001123uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124{
1125 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1126 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001127 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 }
1129 return old;
1130}
1131
1132void SurfaceFlinger::openGlobalTransaction()
1133{
1134 android_atomic_inc(&mTransactionCount);
1135}
1136
1137void SurfaceFlinger::closeGlobalTransaction()
1138{
1139 if (android_atomic_dec(&mTransactionCount) == 1) {
1140 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001141
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001142 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001143 // take effect before returning.
1144 Mutex::Autolock _l(mStateLock);
1145 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001146 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1147 if (CC_UNLIKELY(err != NO_ERROR)) {
1148 // just in case something goes wrong in SF, return to the
1149 // called after a few seconds.
1150 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1151 mResizeTransationPending = false;
1152 break;
1153 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001154 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155 }
1156}
1157
1158status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1159{
1160 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1161 return BAD_VALUE;
1162
1163 Mutex::Autolock _l(mStateLock);
1164 mCurrentState.freezeDisplay = 1;
1165 setTransactionFlags(eTransactionNeeded);
1166
1167 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001168 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 return NO_ERROR;
1170}
1171
1172status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1173{
1174 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1175 return BAD_VALUE;
1176
1177 Mutex::Autolock _l(mStateLock);
1178 mCurrentState.freezeDisplay = 0;
1179 setTransactionFlags(eTransactionNeeded);
1180
1181 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001182 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 return NO_ERROR;
1184}
1185
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001186int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001187 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188{
1189 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1190 return BAD_VALUE;
1191
1192 Mutex::Autolock _l(mStateLock);
1193 if (mCurrentState.orientation != orientation) {
1194 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001195 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 mCurrentState.orientation = orientation;
1197 setTransactionFlags(eTransactionNeeded);
1198 mTransactionCV.wait(mStateLock);
1199 } else {
1200 orientation = BAD_VALUE;
1201 }
1202 }
1203 return orientation;
1204}
1205
Mathias Agopian96f08192010-06-02 23:28:45 -07001206sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001207 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1209 uint32_t flags)
1210{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001211 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001213
1214 if (int32_t(w|h) < 0) {
1215 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1216 int(w), int(h));
1217 return surfaceHandle;
1218 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001221 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 switch (flags & eFXSurfaceMask) {
1223 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001224 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1225 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 break;
1227 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001228 // for now we treat Blur as Dim, until we can implement it
1229 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 break;
1233 }
1234
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001235 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001236 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001237 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001238 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001241 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001242 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001243 params->identity = surfaceHandle->getIdentity();
1244 params->width = w;
1245 params->height = h;
1246 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001247 if (normalLayer != 0) {
1248 Mutex::Autolock _l(mStateLock);
1249 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1250 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001251 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001252
Mathias Agopian96f08192010-06-02 23:28:45 -07001253 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 }
1255
1256 return surfaceHandle;
1257}
1258
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001259sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001260 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001261 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001262 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263{
1264 // initialize the surfaces
1265 switch (format) { // TODO: take h/w into account
1266 case PIXEL_FORMAT_TRANSPARENT:
1267 case PIXEL_FORMAT_TRANSLUCENT:
1268 format = PIXEL_FORMAT_RGBA_8888;
1269 break;
1270 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001271#ifdef NO_RGBX_8888
1272 format = PIXEL_FORMAT_RGB_565;
1273#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001274 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001275#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 break;
1277 }
1278
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001279#ifdef NO_RGBX_8888
1280 if (format == PIXEL_FORMAT_RGBX_8888)
1281 format = PIXEL_FORMAT_RGBA_8888;
1282#endif
1283
Mathias Agopian96f08192010-06-02 23:28:45 -07001284 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001285 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001286 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001288 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 }
1290 return layer;
1291}
1292
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001293sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001294 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001295 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296{
Mathias Agopian96f08192010-06-02 23:28:45 -07001297 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 return layer;
1300}
1301
Mathias Agopian96f08192010-06-02 23:28:45 -07001302status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303{
Mathias Agopian9a112062009-04-17 19:36:26 -07001304 /*
1305 * called by the window manager, when a surface should be marked for
1306 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001307 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001308 * The surface is removed from the current and drawing lists, but placed
1309 * in the purgatory queue, so it's not destroyed right-away (we need
1310 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001311 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001312
Mathias Agopian48d819a2009-09-10 19:41:18 -07001313 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001314 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001315 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001316 if (layer != 0) {
1317 err = purgatorizeLayer_l(layer);
1318 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001319 setTransactionFlags(eTransactionNeeded);
1320 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001321 }
1322 return err;
1323}
1324
1325status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1326{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001327 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001328
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001329 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001330 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001331 sp<LayerBaseClient> layer;
1332 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001333 MessageDestroySurface(
1334 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1335 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001336 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001337 sp<LayerBaseClient> l(layer);
1338 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001339 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001340 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001341 * remove the layer from the current list -- chances are that it's
1342 * not in the list anyway, because it should have been removed
1343 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001344 * However, a buggy client could have not done that.
1345 * Since we know we don't have any more clients, we don't need
1346 * to use the purgatory.
1347 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001348 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001349 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1350 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001351 return true;
1352 }
1353 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001354
Mathias Agopianbb641242010-05-18 17:06:55 -07001355 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356 return NO_ERROR;
1357}
1358
1359status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001360 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 int32_t count,
1362 const layer_state_t* states)
1363{
1364 Mutex::Autolock _l(mStateLock);
1365 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001367 const layer_state_t& s(states[i]);
1368 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001369 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 if (what & ePositionChanged) {
1372 if (layer->setPosition(s.x, s.y))
1373 flags |= eTraversalNeeded;
1374 }
1375 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001376 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001378 mCurrentState.layersSortedByZ.removeAt(idx);
1379 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 // we need traversal (state changed)
1381 // AND transaction (list changed)
1382 flags |= eTransactionNeeded|eTraversalNeeded;
1383 }
1384 }
1385 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001386 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001388 mResizeTransationPending = true;
1389 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 }
1391 if (what & eAlphaChanged) {
1392 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1393 flags |= eTraversalNeeded;
1394 }
1395 if (what & eMatrixChanged) {
1396 if (layer->setMatrix(s.matrix))
1397 flags |= eTraversalNeeded;
1398 }
1399 if (what & eTransparentRegionChanged) {
1400 if (layer->setTransparentRegionHint(s.transparentRegion))
1401 flags |= eTraversalNeeded;
1402 }
1403 if (what & eVisibilityChanged) {
1404 if (layer->setFlags(s.flags, s.mask))
1405 flags |= eTraversalNeeded;
1406 }
1407 }
1408 }
1409 if (flags) {
1410 setTransactionFlags(flags);
1411 }
1412 return NO_ERROR;
1413}
1414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415void SurfaceFlinger::screenReleased(int dpy)
1416{
1417 // this may be called by a signal handler, we can't do too much in here
1418 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1419 signalEvent();
1420}
1421
1422void SurfaceFlinger::screenAcquired(int dpy)
1423{
1424 // this may be called by a signal handler, we can't do too much in here
1425 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1426 signalEvent();
1427}
1428
1429status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1430{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001431 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 char buffer[SIZE];
1433 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001434 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 snprintf(buffer, SIZE, "Permission Denial: "
1436 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1437 IPCThreadState::self()->getCallingPid(),
1438 IPCThreadState::self()->getCallingUid());
1439 result.append(buffer);
1440 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001441
1442 // figure out if we're stuck somewhere
1443 const nsecs_t now = systemTime();
1444 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1445 const nsecs_t inTransaction(mDebugInTransaction);
1446 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1447 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1448
1449 // Try to get the main lock, but don't insist if we can't
1450 // (this would indicate SF is stuck, but we want to be able to
1451 // print something in dumpsys).
1452 int retry = 3;
1453 while (mStateLock.tryLock()<0 && --retry>=0) {
1454 usleep(1000000);
1455 }
1456 const bool locked(retry >= 0);
1457 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001458 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001459 "SurfaceFlinger appears to be unresponsive, "
1460 "dumping anyways (no locks held)\n");
1461 result.append(buffer);
1462 }
1463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1465 const size_t count = currentLayers.size();
1466 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001467 const sp<LayerBase>& layer(currentLayers[i]);
1468 layer->dump(result, buffer, SIZE);
1469 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 s.transparentRegion.dump(result, "transparentRegion");
1471 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1472 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1473 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001474
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 mWormholeRegion.dump(result, "WormholeRegion");
1476 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1477 snprintf(buffer, SIZE,
1478 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1479 mFreezeDisplay?"yes":"no", mFreezeCount,
1480 mCurrentState.orientation, hw.canDraw());
1481 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001482 snprintf(buffer, SIZE,
1483 " last eglSwapBuffers() time: %f us\n"
1484 " last transaction time : %f us\n",
1485 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1486 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001487
Mathias Agopian9795c422009-08-26 16:36:26 -07001488 if (inSwapBuffersDuration || !locked) {
1489 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1490 inSwapBuffersDuration/1000.0);
1491 result.append(buffer);
1492 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001493
Mathias Agopian9795c422009-08-26 16:36:26 -07001494 if (inTransactionDuration || !locked) {
1495 snprintf(buffer, SIZE, " transaction time: %f us\n",
1496 inTransactionDuration/1000.0);
1497 result.append(buffer);
1498 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001499
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001500 HWComposer& hwc(hw.getHwComposer());
1501 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1502 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1503 mDebugDisableHWC ? "disabled" : "enabled");
1504 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001505 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001506
Mathias Agopian3330b202009-10-05 17:07:12 -07001507 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001508 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001509 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001510
1511 if (locked) {
1512 mStateLock.unlock();
1513 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 }
1515 write(fd, result.string(), result.size());
1516 return NO_ERROR;
1517}
1518
1519status_t SurfaceFlinger::onTransact(
1520 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1521{
1522 switch (code) {
1523 case CREATE_CONNECTION:
1524 case OPEN_GLOBAL_TRANSACTION:
1525 case CLOSE_GLOBAL_TRANSACTION:
1526 case SET_ORIENTATION:
1527 case FREEZE_DISPLAY:
1528 case UNFREEZE_DISPLAY:
1529 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001530 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001531 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 {
1533 // codes that require permission check
1534 IPCThreadState* ipc = IPCThreadState::self();
1535 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001536 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001537 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1538 LOGE("Permission Denial: "
1539 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1540 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001542 break;
1543 }
1544 case CAPTURE_SCREEN:
1545 {
1546 // codes that require permission check
1547 IPCThreadState* ipc = IPCThreadState::self();
1548 const int pid = ipc->getCallingPid();
1549 const int uid = ipc->getCallingUid();
1550 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1551 LOGE("Permission Denial: "
1552 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1553 return PERMISSION_DENIED;
1554 }
1555 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 }
1557 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001558
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1560 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001561 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001562 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1563 IPCThreadState* ipc = IPCThreadState::self();
1564 const int pid = ipc->getCallingPid();
1565 const int uid = ipc->getCallingUid();
1566 LOGE("Permission Denial: "
1567 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 return PERMISSION_DENIED;
1569 }
1570 int n;
1571 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001572 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001573 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 return NO_ERROR;
1575 case 1002: // SHOW_UPDATES
1576 n = data.readInt32();
1577 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1578 return NO_ERROR;
1579 case 1003: // SHOW_BACKGROUND
1580 n = data.readInt32();
1581 mDebugBackground = n ? 1 : 0;
1582 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001583 case 1008: // toggle use of hw composer
1584 n = data.readInt32();
1585 mDebugDisableHWC = n ? 1 : 0;
1586 mHwWorkListDirty = true;
1587 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 case 1004:{ // repaint everything
1589 Mutex::Autolock _l(mStateLock);
1590 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1591 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1592 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001593 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001595 case 1005:{ // force transaction
1596 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1597 return NO_ERROR;
1598 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001599 case 1006:{ // enable/disable GraphicLog
1600 int enabled = data.readInt32();
1601 GraphicLog::getInstance().setEnabled(enabled);
1602 return NO_ERROR;
1603 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 case 1007: // set mFreezeCount
1605 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001606 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 return NO_ERROR;
1608 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001609 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 reply->writeInt32(0);
1611 reply->writeInt32(mDebugRegion);
1612 reply->writeInt32(mDebugBackground);
1613 return NO_ERROR;
1614 case 1013: {
1615 Mutex::Autolock _l(mStateLock);
1616 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1617 reply->writeInt32(hw.getPageFlipCount());
1618 }
1619 return NO_ERROR;
1620 }
1621 }
1622 return err;
1623}
1624
Mathias Agopian59119e62010-10-11 12:37:43 -07001625// ---------------------------------------------------------------------------
1626
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001627status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1628 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001629{
Mathias Agopian59119e62010-10-11 12:37:43 -07001630 if (!GLExtensions::getInstance().haveFramebufferObject())
1631 return INVALID_OPERATION;
1632
1633 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001634 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001635 const uint32_t hw_w = hw.getWidth();
1636 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001637 GLfloat u = 1;
1638 GLfloat v = 1;
1639
1640 // make sure to clear all GL error flags
1641 while ( glGetError() != GL_NO_ERROR ) ;
1642
1643 // create a FBO
1644 GLuint name, tname;
1645 glGenTextures(1, &tname);
1646 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001647 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1648 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001649 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001650 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001651 GLint tw = (2 << (31 - clz(hw_w)));
1652 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001653 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1654 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001655 u = GLfloat(hw_w) / tw;
1656 v = GLfloat(hw_h) / th;
1657 }
1658 glGenFramebuffersOES(1, &name);
1659 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001660 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1661 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001662
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001663 // redraw the screen entirely...
1664 glClearColor(0,0,0,1);
1665 glClear(GL_COLOR_BUFFER_BIT);
1666 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1667 const size_t count = layers.size();
1668 for (size_t i=0 ; i<count ; ++i) {
1669 const sp<LayerBase>& layer(layers[i]);
1670 layer->drawForSreenShot();
1671 }
1672
1673 // back to main framebuffer
1674 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1675 glDisable(GL_SCISSOR_TEST);
1676 glDeleteFramebuffersOES(1, &name);
1677
1678 *textureName = tname;
1679 *uOut = u;
1680 *vOut = v;
1681 return NO_ERROR;
1682}
1683
1684// ---------------------------------------------------------------------------
1685
1686status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1687{
1688 status_t result = PERMISSION_DENIED;
1689
1690 if (!GLExtensions::getInstance().haveFramebufferObject())
1691 return INVALID_OPERATION;
1692
1693 // get screen geometry
1694 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1695 const uint32_t hw_w = hw.getWidth();
1696 const uint32_t hw_h = hw.getHeight();
1697 const Region screenBounds(hw.bounds());
1698
1699 GLfloat u, v;
1700 GLuint tname;
1701 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1702 if (result != NO_ERROR) {
1703 return result;
1704 }
1705
1706 GLfloat vtx[8];
1707 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1708 glEnable(GL_TEXTURE_2D);
1709 glBindTexture(GL_TEXTURE_2D, tname);
1710 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1711 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1712 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1713 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1714 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1715 glVertexPointer(2, GL_FLOAT, 0, vtx);
1716
1717 class s_curve_interpolator {
1718 const float nbFrames, s, v;
1719 public:
1720 s_curve_interpolator(int nbFrames, float s)
1721 : nbFrames(1.0f / (nbFrames-1)), s(s),
1722 v(1.0f + expf(-s + 0.5f*s)) {
1723 }
1724 float operator()(int f) {
1725 const float x = f * nbFrames;
1726 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1727 }
1728 };
1729
1730 class v_stretch {
1731 const GLfloat hw_w, hw_h;
1732 public:
1733 v_stretch(uint32_t hw_w, uint32_t hw_h)
1734 : hw_w(hw_w), hw_h(hw_h) {
1735 }
1736 void operator()(GLfloat* vtx, float v) {
1737 const GLfloat w = hw_w + (hw_w * v);
1738 const GLfloat h = hw_h - (hw_h * v);
1739 const GLfloat x = (hw_w - w) * 0.5f;
1740 const GLfloat y = (hw_h - h) * 0.5f;
1741 vtx[0] = x; vtx[1] = y;
1742 vtx[2] = x; vtx[3] = y + h;
1743 vtx[4] = x + w; vtx[5] = y + h;
1744 vtx[6] = x + w; vtx[7] = y;
1745 }
1746 };
1747
1748 class h_stretch {
1749 const GLfloat hw_w, hw_h;
1750 public:
1751 h_stretch(uint32_t hw_w, uint32_t hw_h)
1752 : hw_w(hw_w), hw_h(hw_h) {
1753 }
1754 void operator()(GLfloat* vtx, float v) {
1755 const GLfloat w = hw_w - (hw_w * v);
1756 const GLfloat h = 1.0f;
1757 const GLfloat x = (hw_w - w) * 0.5f;
1758 const GLfloat y = (hw_h - h) * 0.5f;
1759 vtx[0] = x; vtx[1] = y;
1760 vtx[2] = x; vtx[3] = y + h;
1761 vtx[4] = x + w; vtx[5] = y + h;
1762 vtx[6] = x + w; vtx[7] = y;
1763 }
1764 };
1765
1766 // the full animation is 24 frames
1767 const int nbFrames = 12;
1768 s_curve_interpolator itr(nbFrames, 7.5f);
1769 s_curve_interpolator itg(nbFrames, 8.0f);
1770 s_curve_interpolator itb(nbFrames, 8.5f);
1771
1772 v_stretch vverts(hw_w, hw_h);
1773 glEnable(GL_BLEND);
1774 glBlendFunc(GL_ONE, GL_ONE);
1775 for (int i=0 ; i<nbFrames ; i++) {
1776 float x, y, w, h;
1777 const float vr = itr(i);
1778 const float vg = itg(i);
1779 const float vb = itb(i);
1780
1781 // clear screen
1782 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001783 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001784 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001785
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001786 // draw the red plane
1787 vverts(vtx, vr);
1788 glColorMask(1,0,0,1);
1789 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001790
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001791 // draw the green plane
1792 vverts(vtx, vg);
1793 glColorMask(0,1,0,1);
1794 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001795
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001796 // draw the blue plane
1797 vverts(vtx, vb);
1798 glColorMask(0,0,1,1);
1799 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001800
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001801 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001802 glDisable(GL_TEXTURE_2D);
1803 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001804 glColor4f(vg, vg, vg, 1);
1805 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1806 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001807 }
1808
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001809 h_stretch hverts(hw_w, hw_h);
1810 glDisable(GL_BLEND);
1811 glDisable(GL_TEXTURE_2D);
1812 glColorMask(1,1,1,1);
1813 for (int i=0 ; i<nbFrames ; i++) {
1814 const float v = itg(i);
1815 hverts(vtx, v);
1816 glClear(GL_COLOR_BUFFER_BIT);
1817 glColor4f(1-v, 1-v, 1-v, 1);
1818 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1819 hw.flip(screenBounds);
1820 }
1821
1822 glColorMask(1,1,1,1);
1823 glEnable(GL_SCISSOR_TEST);
1824 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1825 glDeleteTextures(1, &tname);
1826 return NO_ERROR;
1827}
1828
1829status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1830{
1831 status_t result = PERMISSION_DENIED;
1832
1833 if (!GLExtensions::getInstance().haveFramebufferObject())
1834 return INVALID_OPERATION;
1835
1836
1837 // get screen geometry
1838 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1839 const uint32_t hw_w = hw.getWidth();
1840 const uint32_t hw_h = hw.getHeight();
1841 const Region screenBounds(hw.bounds());
1842
1843 GLfloat u, v;
1844 GLuint tname;
1845 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1846 if (result != NO_ERROR) {
1847 return result;
1848 }
1849
1850 // back to main framebuffer
1851 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1852 glDisable(GL_SCISSOR_TEST);
1853
1854 GLfloat vtx[8];
1855 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1856 glEnable(GL_TEXTURE_2D);
1857 glBindTexture(GL_TEXTURE_2D, tname);
1858 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1859 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1860 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1861 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1862 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1863 glVertexPointer(2, GL_FLOAT, 0, vtx);
1864
1865 class s_curve_interpolator {
1866 const float nbFrames, s, v;
1867 public:
1868 s_curve_interpolator(int nbFrames, float s)
1869 : nbFrames(1.0f / (nbFrames-1)), s(s),
1870 v(1.0f + expf(-s + 0.5f*s)) {
1871 }
1872 float operator()(int f) {
1873 const float x = f * nbFrames;
1874 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1875 }
1876 };
1877
1878 class v_stretch {
1879 const GLfloat hw_w, hw_h;
1880 public:
1881 v_stretch(uint32_t hw_w, uint32_t hw_h)
1882 : hw_w(hw_w), hw_h(hw_h) {
1883 }
1884 void operator()(GLfloat* vtx, float v) {
1885 const GLfloat w = hw_w + (hw_w * v);
1886 const GLfloat h = hw_h - (hw_h * v);
1887 const GLfloat x = (hw_w - w) * 0.5f;
1888 const GLfloat y = (hw_h - h) * 0.5f;
1889 vtx[0] = x; vtx[1] = y;
1890 vtx[2] = x; vtx[3] = y + h;
1891 vtx[4] = x + w; vtx[5] = y + h;
1892 vtx[6] = x + w; vtx[7] = y;
1893 }
1894 };
1895
1896 class h_stretch {
1897 const GLfloat hw_w, hw_h;
1898 public:
1899 h_stretch(uint32_t hw_w, uint32_t hw_h)
1900 : hw_w(hw_w), hw_h(hw_h) {
1901 }
1902 void operator()(GLfloat* vtx, float v) {
1903 const GLfloat w = hw_w - (hw_w * v);
1904 const GLfloat h = 1.0f;
1905 const GLfloat x = (hw_w - w) * 0.5f;
1906 const GLfloat y = (hw_h - h) * 0.5f;
1907 vtx[0] = x; vtx[1] = y;
1908 vtx[2] = x; vtx[3] = y + h;
1909 vtx[4] = x + w; vtx[5] = y + h;
1910 vtx[6] = x + w; vtx[7] = y;
1911 }
1912 };
1913
Mathias Agopiana6546e52010-10-14 12:33:07 -07001914 // the full animation is 12 frames
1915 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001916 s_curve_interpolator itr(nbFrames, 7.5f);
1917 s_curve_interpolator itg(nbFrames, 8.0f);
1918 s_curve_interpolator itb(nbFrames, 8.5f);
1919
1920 h_stretch hverts(hw_w, hw_h);
1921 glDisable(GL_BLEND);
1922 glDisable(GL_TEXTURE_2D);
1923 glColorMask(1,1,1,1);
1924 for (int i=nbFrames-1 ; i>=0 ; i--) {
1925 const float v = itg(i);
1926 hverts(vtx, v);
1927 glClear(GL_COLOR_BUFFER_BIT);
1928 glColor4f(1-v, 1-v, 1-v, 1);
1929 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1930 hw.flip(screenBounds);
1931 }
1932
Mathias Agopiana6546e52010-10-14 12:33:07 -07001933 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001934 v_stretch vverts(hw_w, hw_h);
1935 glEnable(GL_BLEND);
1936 glBlendFunc(GL_ONE, GL_ONE);
1937 for (int i=nbFrames-1 ; i>=0 ; i--) {
1938 float x, y, w, h;
1939 const float vr = itr(i);
1940 const float vg = itg(i);
1941 const float vb = itb(i);
1942
1943 // clear screen
1944 glColorMask(1,1,1,1);
1945 glClear(GL_COLOR_BUFFER_BIT);
1946 glEnable(GL_TEXTURE_2D);
1947
1948 // draw the red plane
1949 vverts(vtx, vr);
1950 glColorMask(1,0,0,1);
1951 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1952
1953 // draw the green plane
1954 vverts(vtx, vg);
1955 glColorMask(0,1,0,1);
1956 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1957
1958 // draw the blue plane
1959 vverts(vtx, vb);
1960 glColorMask(0,0,1,1);
1961 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1962
1963 hw.flip(screenBounds);
1964 }
1965
1966 glColorMask(1,1,1,1);
1967 glEnable(GL_SCISSOR_TEST);
1968 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07001969 glDeleteTextures(1, &tname);
1970
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971 return NO_ERROR;
1972}
1973
1974// ---------------------------------------------------------------------------
1975
Mathias Agopianabd671a2010-10-14 14:54:06 -07001976status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001977{
1978 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
1979 if (!hw.canDraw()) {
1980 // we're already off
1981 return NO_ERROR;
1982 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07001983 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
1984 electronBeamOffAnimationImplLocked();
1985 }
1986
1987 // always clear the whole screen at the end of the animation
1988 glClearColor(0,0,0,1);
1989 glDisable(GL_SCISSOR_TEST);
1990 glClear(GL_COLOR_BUFFER_BIT);
1991 glEnable(GL_SCISSOR_TEST);
1992 hw.flip( Region(hw.bounds()) );
1993
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001994 hw.setCanDraw(false);
1995 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07001996}
1997
1998status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
1999{
Mathias Agopian59119e62010-10-11 12:37:43 -07002000 class MessageTurnElectronBeamOff : public MessageBase {
2001 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002002 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002003 status_t result;
2004 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002005 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2006 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002007 }
2008 status_t getResult() const {
2009 return result;
2010 }
2011 virtual bool handler() {
2012 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002013 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002014 return true;
2015 }
2016 };
2017
Mathias Agopianabd671a2010-10-14 14:54:06 -07002018 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002019 status_t res = postMessageSync(msg);
2020 if (res == NO_ERROR) {
2021 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002022
2023 // work-around: when the power-manager calls us we activate the
2024 // animation. eventually, the "on" animation will be called
2025 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002026 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002027 }
2028 return res;
2029}
2030
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002031// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002032
Mathias Agopianabd671a2010-10-14 14:54:06 -07002033status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002034{
2035 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2036 if (hw.canDraw()) {
2037 // we're already on
2038 return NO_ERROR;
2039 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002040 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2041 electronBeamOnAnimationImplLocked();
2042 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002043 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002044
2045 // make sure to redraw the whole screen when the animation is done
2046 mDirtyRegion.set(hw.bounds());
2047 signalEvent();
2048
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002049 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002050}
2051
2052status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2053{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 class MessageTurnElectronBeamOn : public MessageBase {
2055 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002056 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 status_t result;
2058 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002059 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2060 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002061 }
2062 status_t getResult() const {
2063 return result;
2064 }
2065 virtual bool handler() {
2066 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002067 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002068 return true;
2069 }
2070 };
2071
Mathias Agopianabd671a2010-10-14 14:54:06 -07002072 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002073 return NO_ERROR;
2074}
2075
2076// ---------------------------------------------------------------------------
2077
Mathias Agopian74c40c02010-09-29 13:02:36 -07002078status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2079 sp<IMemoryHeap>* heap,
2080 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002081 uint32_t sw, uint32_t sh,
2082 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002083{
2084 status_t result = PERMISSION_DENIED;
2085
2086 // only one display supported for now
2087 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2088 return BAD_VALUE;
2089
2090 if (!GLExtensions::getInstance().haveFramebufferObject())
2091 return INVALID_OPERATION;
2092
2093 // get screen geometry
2094 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2095 const uint32_t hw_w = hw.getWidth();
2096 const uint32_t hw_h = hw.getHeight();
2097
2098 if ((sw > hw_w) || (sh > hw_h))
2099 return BAD_VALUE;
2100
2101 sw = (!sw) ? hw_w : sw;
2102 sh = (!sh) ? hw_h : sh;
2103 const size_t size = sw * sh * 4;
2104
2105 // make sure to clear all GL error flags
2106 while ( glGetError() != GL_NO_ERROR ) ;
2107
2108 // create a FBO
2109 GLuint name, tname;
2110 glGenRenderbuffersOES(1, &tname);
2111 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2112 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2113 glGenFramebuffersOES(1, &name);
2114 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2115 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2116 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2117
2118 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2119 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2120
2121 // invert everything, b/c glReadPixel() below will invert the FB
2122 glViewport(0, 0, sw, sh);
2123 glMatrixMode(GL_PROJECTION);
2124 glPushMatrix();
2125 glLoadIdentity();
2126 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2127 glMatrixMode(GL_MODELVIEW);
2128
2129 // redraw the screen entirely...
2130 glClearColor(0,0,0,1);
2131 glClear(GL_COLOR_BUFFER_BIT);
2132 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2133 const size_t count = layers.size();
2134 for (size_t i=0 ; i<count ; ++i) {
2135 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002136 const uint32_t z = layer->drawingState().z;
2137 if (z >= minLayerZ && z <= maxLayerZ) {
2138 layer->drawForSreenShot();
2139 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002140 }
2141
2142 // XXX: this is needed on tegra
2143 glScissor(0, 0, sw, sh);
2144
2145 // check for errors and return screen capture
2146 if (glGetError() != GL_NO_ERROR) {
2147 // error while rendering
2148 result = INVALID_OPERATION;
2149 } else {
2150 // allocate shared memory large enough to hold the
2151 // screen capture
2152 sp<MemoryHeapBase> base(
2153 new MemoryHeapBase(size, 0, "screen-capture") );
2154 void* const ptr = base->getBase();
2155 if (ptr) {
2156 // capture the screen with glReadPixels()
2157 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2158 if (glGetError() == GL_NO_ERROR) {
2159 *heap = base;
2160 *w = sw;
2161 *h = sh;
2162 *f = PIXEL_FORMAT_RGBA_8888;
2163 result = NO_ERROR;
2164 }
2165 } else {
2166 result = NO_MEMORY;
2167 }
2168 }
2169
2170 glEnable(GL_SCISSOR_TEST);
2171 glViewport(0, 0, hw_w, hw_h);
2172 glMatrixMode(GL_PROJECTION);
2173 glPopMatrix();
2174 glMatrixMode(GL_MODELVIEW);
2175
2176
2177 } else {
2178 result = BAD_VALUE;
2179 }
2180
2181 // release FBO resources
2182 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2183 glDeleteRenderbuffersOES(1, &tname);
2184 glDeleteFramebuffersOES(1, &name);
2185 return result;
2186}
2187
2188
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002189status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2190 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002191 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002192 uint32_t sw, uint32_t sh,
2193 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002194{
2195 // only one display supported for now
2196 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2197 return BAD_VALUE;
2198
2199 if (!GLExtensions::getInstance().haveFramebufferObject())
2200 return INVALID_OPERATION;
2201
2202 class MessageCaptureScreen : public MessageBase {
2203 SurfaceFlinger* flinger;
2204 DisplayID dpy;
2205 sp<IMemoryHeap>* heap;
2206 uint32_t* w;
2207 uint32_t* h;
2208 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002209 uint32_t sw;
2210 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002211 uint32_t minLayerZ;
2212 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002213 status_t result;
2214 public:
2215 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002216 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002217 uint32_t sw, uint32_t sh,
2218 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002219 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002220 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2221 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2222 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002223 {
2224 }
2225 status_t getResult() const {
2226 return result;
2227 }
2228 virtual bool handler() {
2229 Mutex::Autolock _l(flinger->mStateLock);
2230
2231 // if we have secure windows, never allow the screen capture
2232 if (flinger->mSecureFrameBuffer)
2233 return true;
2234
Mathias Agopian74c40c02010-09-29 13:02:36 -07002235 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002236 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002237
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002238 return true;
2239 }
2240 };
2241
2242 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002243 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002244 status_t res = postMessageSync(msg);
2245 if (res == NO_ERROR) {
2246 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2247 }
2248 return res;
2249}
2250
2251// ---------------------------------------------------------------------------
2252
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002253sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2254{
2255 sp<Layer> result;
2256 Mutex::Autolock _l(mStateLock);
2257 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2258 return result;
2259}
2260
2261// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002262
Mathias Agopian96f08192010-06-02 23:28:45 -07002263Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002264 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002265{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002266}
2267
Mathias Agopian96f08192010-06-02 23:28:45 -07002268Client::~Client()
2269{
Mathias Agopian96f08192010-06-02 23:28:45 -07002270 const size_t count = mLayers.size();
2271 for (size_t i=0 ; i<count ; i++) {
2272 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2273 if (layer != 0) {
2274 mFlinger->removeLayer(layer);
2275 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002276 }
2277}
2278
Mathias Agopian96f08192010-06-02 23:28:45 -07002279status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002280 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002281}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002282
Mathias Agopian96f08192010-06-02 23:28:45 -07002283ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2284{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002285 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002286 mLayers.add(name, layer);
2287 return name;
2288}
2289
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002290void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002291{
Mathias Agopian96f08192010-06-02 23:28:45 -07002292 // we do a linear search here, because this doesn't happen often
2293 const size_t count = mLayers.size();
2294 for (size_t i=0 ; i<count ; i++) {
2295 if (mLayers.valueAt(i) == layer) {
2296 mLayers.removeItemsAt(i, 1);
2297 break;
2298 }
2299 }
2300}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002301sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2302 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002303 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2304 if (layer != 0) {
2305 lbc = layer.promote();
2306 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002307 }
2308 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309}
2310
Mathias Agopian96f08192010-06-02 23:28:45 -07002311sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002312 return 0;
2313}
2314ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2315 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002316}
Mathias Agopian96f08192010-06-02 23:28:45 -07002317sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002318 ISurfaceComposerClient::surface_data_t* params, int pid,
2319 const String8& name,
2320 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002321 uint32_t flags)
2322{
Mathias Agopian96f08192010-06-02 23:28:45 -07002323 return mFlinger->createSurface(this, pid, name, params,
2324 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002325}
Mathias Agopian96f08192010-06-02 23:28:45 -07002326status_t Client::destroySurface(SurfaceID sid) {
2327 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002328}
Mathias Agopian96f08192010-06-02 23:28:45 -07002329status_t Client::setState(int32_t count, const layer_state_t* states) {
2330 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002331}
2332
2333// ---------------------------------------------------------------------------
2334
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002335UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2336 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2337{
2338 const int pgsize = getpagesize();
2339 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2340
2341 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2342 "SurfaceFlinger Client control-block");
2343
2344 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2345 if (ctrlblk) { // construct the shared structure in-place.
2346 new(ctrlblk) SharedClient;
2347 }
2348}
2349
2350UserClient::~UserClient()
2351{
2352 if (ctrlblk) {
2353 ctrlblk->~SharedClient(); // destroy our shared-structure.
2354 }
2355
2356 /*
2357 * When a UserClient dies, it's unclear what to do exactly.
2358 * We could go ahead and destroy all surfaces linked to that client
2359 * however, it wouldn't be fair to the main Client
2360 * (usually the the window-manager), which might want to re-target
2361 * the layer to another UserClient.
2362 * I think the best is to do nothing, or not much; in most cases the
2363 * WM itself will go ahead and clean things up when it detects a client of
2364 * his has died.
2365 * The remaining question is what to display? currently we keep
2366 * just keep the current buffer.
2367 */
2368}
2369
2370status_t UserClient::initCheck() const {
2371 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2372}
2373
2374void UserClient::detachLayer(const Layer* layer)
2375{
2376 int32_t name = layer->getToken();
2377 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002378 int32_t mask = 1LU<<name;
2379 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2380 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2381 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002382 }
2383}
2384
2385sp<IMemoryHeap> UserClient::getControlBlock() const {
2386 return mCblkHeap;
2387}
2388
2389ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2390{
2391 int32_t name = NAME_NOT_FOUND;
2392 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002393 if (layer == 0) {
2394 return name;
2395 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002396
Mathias Agopian579b3f82010-06-08 19:54:15 -07002397 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002398 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002399 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002400 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002401 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002402
2403 name = 0;
2404 do {
2405 int32_t mask = 1LU<<name;
2406 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2407 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002408 status_t err = layer->setToken(
2409 const_cast<UserClient*>(this), ctrlblk, name);
2410 if (err != NO_ERROR) {
2411 // free the name
2412 android_atomic_and(~mask, &mBitmap);
2413 name = err;
2414 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002415 break;
2416 }
2417 if (++name > 31)
2418 name = NO_MEMORY;
2419 } while(name >= 0);
2420
Mathias Agopian53503a92010-06-08 15:40:56 -07002421 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2422 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002423 return name;
2424}
2425
2426sp<ISurface> UserClient::createSurface(
2427 ISurfaceComposerClient::surface_data_t* params, int pid,
2428 const String8& name,
2429 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2430 uint32_t flags) {
2431 return 0;
2432}
2433status_t UserClient::destroySurface(SurfaceID sid) {
2434 return INVALID_OPERATION;
2435}
2436status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2437 return INVALID_OPERATION;
2438}
2439
2440// ---------------------------------------------------------------------------
2441
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002442GraphicPlane::GraphicPlane()
2443 : mHw(0)
2444{
2445}
2446
2447GraphicPlane::~GraphicPlane() {
2448 delete mHw;
2449}
2450
2451bool GraphicPlane::initialized() const {
2452 return mHw ? true : false;
2453}
2454
Mathias Agopian2b92d892010-02-08 15:49:35 -08002455int GraphicPlane::getWidth() const {
2456 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457}
2458
Mathias Agopian2b92d892010-02-08 15:49:35 -08002459int GraphicPlane::getHeight() const {
2460 return mHeight;
2461}
2462
2463void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2464{
2465 mHw = hw;
2466
2467 // initialize the display orientation transform.
2468 // it's a constant that should come from the display driver.
2469 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2470 char property[PROPERTY_VALUE_MAX];
2471 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2472 //displayOrientation
2473 switch (atoi(property)) {
2474 case 90:
2475 displayOrientation = ISurfaceComposer::eOrientation90;
2476 break;
2477 case 270:
2478 displayOrientation = ISurfaceComposer::eOrientation270;
2479 break;
2480 }
2481 }
2482
2483 const float w = hw->getWidth();
2484 const float h = hw->getHeight();
2485 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2486 &mDisplayTransform);
2487 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2488 mDisplayWidth = h;
2489 mDisplayHeight = w;
2490 } else {
2491 mDisplayWidth = w;
2492 mDisplayHeight = h;
2493 }
2494
2495 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496}
2497
2498status_t GraphicPlane::orientationToTransfrom(
2499 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002500{
2501 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002502 switch (orientation) {
2503 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002504 flags = Transform::ROT_0;
2505 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002507 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 break;
2509 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002510 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 break;
2512 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002513 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514 break;
2515 default:
2516 return BAD_VALUE;
2517 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002518 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519 return NO_ERROR;
2520}
2521
2522status_t GraphicPlane::setOrientation(int orientation)
2523{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002524 // If the rotation can be handled in hardware, this is where
2525 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002526
2527 const DisplayHardware& hw(displayHardware());
2528 const float w = mDisplayWidth;
2529 const float h = mDisplayHeight;
2530 mWidth = int(w);
2531 mHeight = int(h);
2532
2533 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002534 GraphicPlane::orientationToTransfrom(orientation, w, h,
2535 &orientationTransform);
2536 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2537 mWidth = int(h);
2538 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002539 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002540
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002541 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002542 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543 return NO_ERROR;
2544}
2545
2546const DisplayHardware& GraphicPlane::displayHardware() const {
2547 return *mHw;
2548}
2549
Mathias Agopian59119e62010-10-11 12:37:43 -07002550DisplayHardware& GraphicPlane::editDisplayHardware() {
2551 return *mHw;
2552}
2553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554const Transform& GraphicPlane::transform() const {
2555 return mGlobalTransform;
2556}
2557
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002558EGLDisplay GraphicPlane::getEGLDisplay() const {
2559 return mHw->getEGLDisplay();
2560}
2561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562// ---------------------------------------------------------------------------
2563
2564}; // namespace android