blob: 5a8888a26622f32f588b34ea5affbf70e0a4b39b [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
Jamie Gennis9a78c902011-01-12 18:30:40 -0800147sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
148{
149 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
150 return gba;
151}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
154{
155 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
156 const GraphicPlane& plane(mGraphicPlanes[dpy]);
157 return plane;
158}
159
160GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
161{
162 return const_cast<GraphicPlane&>(
163 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
164}
165
166void SurfaceFlinger::bootFinished()
167{
168 const nsecs_t now = systemTime();
169 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700170 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700171 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700172 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173}
174
175void SurfaceFlinger::onFirstRef()
176{
177 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
178
179 // Wait for the main thread to be done with its initialization
180 mReadyToRunBarrier.wait();
181}
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183static inline uint16_t pack565(int r, int g, int b) {
184 return (r<<11)|(g<<5)|b;
185}
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187status_t SurfaceFlinger::readyToRun()
188{
189 LOGI( "SurfaceFlinger's main thread ready to run. "
190 "Initializing graphics H/W...");
191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 // we only support one display currently
193 int dpy = 0;
194
195 {
196 // initialize the main display
197 GraphicPlane& plane(graphicPlane(dpy));
198 DisplayHardware* const hw = new DisplayHardware(this, dpy);
199 plane.setDisplayHardware(hw);
200 }
201
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700202 // create the shared control-block
203 mServerHeap = new MemoryHeapBase(4096,
204 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
205 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700206
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700207 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
208 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700209
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700210 new(mServerCblk) surface_flinger_cblk_t;
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 // initialize primary screen
213 // (other display should be initialized in the same manner, but
214 // asynchronously, as they could come and go. None of this is supported
215 // yet).
216 const GraphicPlane& plane(graphicPlane(dpy));
217 const DisplayHardware& hw = plane.displayHardware();
218 const uint32_t w = hw.getWidth();
219 const uint32_t h = hw.getHeight();
220 const uint32_t f = hw.getFormat();
221 hw.makeCurrent();
222
223 // initialize the shared control block
224 mServerCblk->connected |= 1<<dpy;
225 display_cblk_t* dcblk = mServerCblk->displays + dpy;
226 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800227 dcblk->w = plane.getWidth();
228 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 dcblk->format = f;
230 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
231 dcblk->xdpi = hw.getDpiX();
232 dcblk->ydpi = hw.getDpiY();
233 dcblk->fps = hw.getRefreshRate();
234 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700238 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 glEnableClientState(GL_VERTEX_ARRAY);
240 glEnable(GL_SCISSOR_TEST);
241 glShadeModel(GL_FLAT);
242 glDisable(GL_DITHER);
243 glDisable(GL_CULL_FACE);
244
245 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
246 const uint16_t g1 = pack565(0x17,0x2f,0x17);
247 const uint16_t textureData[4] = { g0, g1, g1, g0 };
248 glGenTextures(1, &mWormholeTexName);
249 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
250 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
251 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
252 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
253 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
255 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
256
257 glViewport(0, 0, w, h);
258 glMatrixMode(GL_PROJECTION);
259 glLoadIdentity();
260 glOrthof(0, w, h, 0, 0, 1);
261
262 LayerDim::initDimmer(this, w, h);
263
264 mReadyToRunBarrier.open();
265
266 /*
267 * We're now ready to accept clients...
268 */
269
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700270 // start boot animation
271 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 return NO_ERROR;
274}
275
276// ----------------------------------------------------------------------------
277#if 0
278#pragma mark -
279#pragma mark Events Handler
280#endif
281
282void SurfaceFlinger::waitForEvent()
283{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700284 while (true) {
285 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800286 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700287 if (UNLIKELY(isFrozen())) {
288 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700289 const nsecs_t now = systemTime();
290 if (mFreezeDisplayTime == 0) {
291 mFreezeDisplayTime = now;
292 }
293 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
294 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700295 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700296
Mathias Agopianbb641242010-05-18 17:06:55 -0700297 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800298
299 // see if we timed out
300 if (isFrozen()) {
301 const nsecs_t now = systemTime();
302 nsecs_t frozenTime = (now - mFreezeDisplayTime);
303 if (frozenTime >= freezeDisplayTimeout) {
304 // we timed out and are still frozen
305 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
306 mFreezeDisplay, mFreezeCount);
307 mFreezeDisplayTime = 0;
308 mFreezeCount = 0;
309 mFreezeDisplay = false;
310 }
311 }
312
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700313 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700314 switch (msg->what) {
315 case MessageQueue::INVALIDATE:
316 // invalidate message, just return to the main loop
317 return;
318 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 }
321}
322
323void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700324 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
326
327void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700328 // this is the IPC call
329 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Mathias Agopianbb641242010-05-18 17:06:55 -0700332status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
333 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334{
Mathias Agopianbb641242010-05-18 17:06:55 -0700335 return mEventQueue.postMessage(msg, reltime, flags);
336}
337
338status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
339 nsecs_t reltime, uint32_t flags)
340{
341 status_t res = mEventQueue.postMessage(msg, reltime, flags);
342 if (res == NO_ERROR) {
343 msg->wait();
344 }
345 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346}
347
348// ----------------------------------------------------------------------------
349#if 0
350#pragma mark -
351#pragma mark Main loop
352#endif
353
354bool SurfaceFlinger::threadLoop()
355{
356 waitForEvent();
357
358 // check for transactions
359 if (UNLIKELY(mConsoleSignals)) {
360 handleConsoleEvents();
361 }
362
363 if (LIKELY(mTransactionCount == 0)) {
364 // if we're in a global transaction, don't do anything.
365 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
366 uint32_t transactionFlags = getTransactionFlags(mask);
367 if (LIKELY(transactionFlags)) {
368 handleTransaction(transactionFlags);
369 }
370 }
371
372 // post surfaces (if needed)
373 handlePageFlip();
374
Mathias Agopiana350ff92010-08-10 17:14:02 -0700375 if (UNLIKELY(mHwWorkListDirty)) {
376 // build the h/w work list
377 handleWorkList();
378 }
379
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700381 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700383
384 const int index = hw.getCurrentBufferIndex();
385 GraphicLog& logger(GraphicLog::getInstance());
386
387 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 handleRepaint();
389
Mathias Agopian74faca22009-09-17 16:18:16 -0700390 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700391 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700392 hw.compositionComplete();
393
Mathias Agopian35b48d12010-09-13 22:57:58 -0700394 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700395 postFramebuffer();
396
Mathias Agopian35b48d12010-09-13 22:57:58 -0700397 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 } else {
399 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800400 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 usleep(16667); // 60 fps period
402 }
403 return true;
404}
405
406void SurfaceFlinger::postFramebuffer()
407{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 if (!mInvalidRegion.isEmpty()) {
409 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700410 const nsecs_t now = systemTime();
411 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700413 mLastSwapBufferTime = systemTime() - now;
414 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 }
417}
418
419void SurfaceFlinger::handleConsoleEvents()
420{
421 // something to do with the console
422 const DisplayHardware& hw = graphicPlane(0).displayHardware();
423
424 int what = android_atomic_and(0, &mConsoleSignals);
425 if (what & eConsoleAcquired) {
426 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700427 // this is a temporary work-around, eventually this should be called
428 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700429 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 }
431
Mathias Agopian59119e62010-10-11 12:37:43 -0700432 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700433 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 hw.releaseScreen();
436 }
437
438 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700439 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 hw.releaseScreen();
441 } else {
442 mDeferReleaseConsole = true;
443 }
444 }
445
446 mDirtyRegion.set(hw.bounds());
447}
448
449void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
450{
Mathias Agopian3d579642009-06-04 18:46:21 -0700451 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452
Mathias Agopian4da75192010-08-10 17:19:56 -0700453 /*
454 * Perform and commit the transaction
455 */
456
Mathias Agopian3d579642009-06-04 18:46:21 -0700457 { // scope for the lock
458 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700459 const nsecs_t now = systemTime();
460 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700461 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700462 mLastTransactionTime = systemTime() - now;
463 mDebugInTransaction = 0;
Mathias Agopianad456f92011-01-13 17:53:01 -0800464 invalidateHwcGeometry();
Mathias Agopian4da75192010-08-10 17:19:56 -0700465 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700466 }
467
Mathias Agopian4da75192010-08-10 17:19:56 -0700468 /*
469 * Clean-up all layers that went away
470 * (do this without the lock held)
471 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700472
Mathias Agopian3d579642009-06-04 18:46:21 -0700473 const size_t count = ditchedLayers.size();
474 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700475 if (ditchedLayers[i] != 0) {
476 //LOGD("ditching layer %p", ditchedLayers[i].get());
477 ditchedLayers[i]->ditch();
478 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700479 }
480}
481
482void SurfaceFlinger::handleTransactionLocked(
483 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
484{
485 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 const size_t count = currentLayers.size();
487
488 /*
489 * Traversal of the children
490 * (perform the transaction for each of them if needed)
491 */
492
493 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
494 if (layersNeedTransaction) {
495 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700496 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
498 if (!trFlags) continue;
499
500 const uint32_t flags = layer->doTransaction(0);
501 if (flags & Layer::eVisibleRegion)
502 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 }
504 }
505
506 /*
507 * Perform our own transaction if needed
508 */
509
510 if (transactionFlags & eTransactionNeeded) {
511 if (mCurrentState.orientation != mDrawingState.orientation) {
512 // the orientation has changed, recompute all visible regions
513 // and invalidate everything.
514
515 const int dpy = 0;
516 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700517 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 GraphicPlane& plane(graphicPlane(dpy));
519 plane.setOrientation(orientation);
520
521 // update the shared control block
522 const DisplayHardware& hw(plane.displayHardware());
523 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
524 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800525 dcblk->w = plane.getWidth();
526 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527
528 mVisibleRegionsDirty = true;
529 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530 }
531
532 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
533 // freezing or unfreezing the display -> trigger animation if needed
534 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800535 if (mFreezeDisplay)
536 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 }
538
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700539 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
540 // layers have been added
541 mVisibleRegionsDirty = true;
542 }
543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 // some layers might have been removed, so
545 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700547 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700549 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700550 const size_t count = previousLayers.size();
551 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700552 const sp<LayerBase>& layer(previousLayers[i]);
553 if (currentLayers.indexOf( layer ) < 0) {
554 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700555 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700556 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 }
558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 }
561
562 commitTransaction();
563}
564
565sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
566{
567 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
568}
569
570void SurfaceFlinger::computeVisibleRegions(
571 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
572{
573 const GraphicPlane& plane(graphicPlane(0));
574 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700575 const DisplayHardware& hw(plane.displayHardware());
576 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577
578 Region aboveOpaqueLayers;
579 Region aboveCoveredLayers;
580 Region dirty;
581
582 bool secureFrameBuffer = false;
583
584 size_t i = currentLayers.size();
585 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 layer->validateVisibility(planeTransform);
588
589 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700590 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591
Mathias Agopianab028732010-03-16 16:41:46 -0700592 /*
593 * opaqueRegion: area of a surface that is fully opaque.
594 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700596
597 /*
598 * visibleRegion: area of a surface that is visible on screen
599 * and not fully transparent. This is essentially the layer's
600 * footprint minus the opaque regions above it.
601 * Areas covered by a translucent surface are considered visible.
602 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700604
605 /*
606 * coveredRegion: area of a surface that is covered by all
607 * visible regions above it (which includes the translucent areas).
608 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700610
611
612 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700613 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700615 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700617 visibleRegion.andSelf(screenRegion);
618 if (!visibleRegion.isEmpty()) {
619 // Remove the transparent area from the visible region
620 if (translucent) {
621 visibleRegion.subtractSelf(layer->transparentRegionScreen);
622 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623
Mathias Agopianab028732010-03-16 16:41:46 -0700624 // compute the opaque region
625 const int32_t layerOrientation = layer->getOrientation();
626 if (s.alpha==255 && !translucent &&
627 ((layerOrientation & Transform::ROT_INVALID) == false)) {
628 // the opaque region is the layer's footprint
629 opaqueRegion = visibleRegion;
630 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 }
632 }
633
Mathias Agopianab028732010-03-16 16:41:46 -0700634 // Clip the covered region to the visible region
635 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
636
637 // Update aboveCoveredLayers for next (lower) layer
638 aboveCoveredLayers.orSelf(visibleRegion);
639
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 // subtract the opaque region covered by the layers above us
641 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642
643 // compute this layer's dirty region
644 if (layer->contentDirty) {
645 // we need to invalidate the whole region
646 dirty = visibleRegion;
647 // as well, as the old visible region
648 dirty.orSelf(layer->visibleRegionScreen);
649 layer->contentDirty = false;
650 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700651 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700652 * the exposed region consists of two components:
653 * 1) what's VISIBLE now and was COVERED before
654 * 2) what's EXPOSED now less what was EXPOSED before
655 *
656 * note that (1) is conservative, we start with the whole
657 * visible region but only keep what used to be covered by
658 * something -- which mean it may have been exposed.
659 *
660 * (2) handles areas that were not covered by anything but got
661 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700662 */
Mathias Agopianab028732010-03-16 16:41:46 -0700663 const Region newExposed = visibleRegion - coveredRegion;
664 const Region oldVisibleRegion = layer->visibleRegionScreen;
665 const Region oldCoveredRegion = layer->coveredRegionScreen;
666 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
667 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 }
669 dirty.subtractSelf(aboveOpaqueLayers);
670
671 // accumulate to the screen dirty region
672 dirtyRegion.orSelf(dirty);
673
Mathias Agopianab028732010-03-16 16:41:46 -0700674 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700676
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 // Store the visible region is screen space
678 layer->setVisibleRegion(visibleRegion);
679 layer->setCoveredRegion(coveredRegion);
680
Mathias Agopian97011222009-07-28 10:57:27 -0700681 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 if (layer->isSecure() && !visibleRegion.isEmpty()) {
683 secureFrameBuffer = true;
684 }
685 }
686
Mathias Agopian97011222009-07-28 10:57:27 -0700687 // invalidate the areas where a layer was removed
688 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
689 mDirtyRegionRemovedLayer.clear();
690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 mSecureFrameBuffer = secureFrameBuffer;
692 opaqueRegion = aboveOpaqueLayers;
693}
694
695
696void SurfaceFlinger::commitTransaction()
697{
698 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700699 mResizeTransationPending = false;
700 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701}
702
703void SurfaceFlinger::handlePageFlip()
704{
705 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700706 LayerVector& currentLayers(
707 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 visibleRegions |= lockPageFlip(currentLayers);
709
710 const DisplayHardware& hw = graphicPlane(0).displayHardware();
711 const Region screenRegion(hw.bounds());
712 if (visibleRegions) {
713 Region opaqueRegion;
714 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700715
716 /*
717 * rebuild the visible layer list
718 */
719 mVisibleLayersSortedByZ.clear();
720 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
721 size_t count = currentLayers.size();
722 mVisibleLayersSortedByZ.setCapacity(count);
723 for (size_t i=0 ; i<count ; i++) {
724 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
725 mVisibleLayersSortedByZ.add(currentLayers[i]);
726 }
727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 mWormholeRegion = screenRegion.subtract(opaqueRegion);
729 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800730 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
732
733 unlockPageFlip(currentLayers);
734 mDirtyRegion.andSelf(screenRegion);
735}
736
Mathias Agopianad456f92011-01-13 17:53:01 -0800737void SurfaceFlinger::invalidateHwcGeometry()
738{
739 mHwWorkListDirty = true;
740}
741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
743{
744 bool recomputeVisibleRegions = false;
745 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700746 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700748 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 layer->lockPageFlip(recomputeVisibleRegions);
750 }
751 return recomputeVisibleRegions;
752}
753
754void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
755{
756 const GraphicPlane& plane(graphicPlane(0));
757 const Transform& planeTransform(plane.transform());
758 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700759 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700761 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 layer->unlockPageFlip(planeTransform, mDirtyRegion);
763 }
764}
765
Mathias Agopiana350ff92010-08-10 17:14:02 -0700766void SurfaceFlinger::handleWorkList()
767{
768 mHwWorkListDirty = false;
769 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
770 if (hwc.initCheck() == NO_ERROR) {
771 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
772 const size_t count = currentLayers.size();
773 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700774 hwc_layer_t* const cur(hwc.getLayers());
775 for (size_t i=0 ; cur && i<count ; i++) {
776 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700777 if (mDebugDisableHWC) {
778 cur[i].compositionType = HWC_FRAMEBUFFER;
779 cur[i].flags |= HWC_SKIP_LAYER;
780 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700781 }
782 }
783}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700784
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785void SurfaceFlinger::handleRepaint()
786{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700787 // compute the invalid region
788 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789
790 if (UNLIKELY(mDebugRegion)) {
791 debugFlashRegions();
792 }
793
Mathias Agopianb8a55602009-06-26 19:06:36 -0700794 // set the frame buffer
795 const DisplayHardware& hw(graphicPlane(0).displayHardware());
796 glMatrixMode(GL_MODELVIEW);
797 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798
799 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700800 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
801 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700802 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700803 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
804 // takes a rectangle, we must make sure to update that whole
805 // rectangle in that case
806 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700807 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700808 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 mDirtyRegion.set(mInvalidRegion.bounds());
810 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700811 // in the BUFFER_PRESERVED case, obviously, we can update only
812 // what's needed and nothing more.
813 // NOTE: this is NOT a common case, as preserving the backbuffer
814 // is costly and usually involves copying the whole update back.
815 }
816 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700817 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700818 // We need to redraw the rectangle that will be updated
819 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700820 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700821 // rectangle instead of a region (see DisplayHardware::flip())
822 mDirtyRegion.set(mInvalidRegion.bounds());
823 } else {
824 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 mDirtyRegion.set(hw.bounds());
826 mInvalidRegion = mDirtyRegion;
827 }
828 }
829
830 // compose all surfaces
831 composeSurfaces(mDirtyRegion);
832
833 // clear the dirty regions
834 mDirtyRegion.clear();
835}
836
837void SurfaceFlinger::composeSurfaces(const Region& dirty)
838{
839 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
840 // should never happen unless the window manager has a bug
841 // draw something...
842 drawWormhole();
843 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700844
845 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700846 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700847 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700848
849 const DisplayHardware& hw(graphicPlane(0).displayHardware());
850 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700851 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700852
Mathias Agopian45721772010-08-12 15:03:26 -0700853 LOGE_IF(cur && hwc.getNumLayers() != count,
854 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
855 hwc.getNumLayers(), count);
856
857 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700858 if (hwc.initCheck() == NO_ERROR) {
859 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
860 }
Mathias Agopian45721772010-08-12 15:03:26 -0700861
862 /*
863 * update the per-frame h/w composer data for each layer
864 * and build the transparent region of the FB
865 */
866 Region transparent;
867 if (cur) {
868 for (size_t i=0 ; i<count ; i++) {
869 const sp<LayerBase>& layer(layers[i]);
870 layer->setPerFrameData(&cur[i]);
871 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
872 if (!(layer->needsBlending())) {
873 transparent.orSelf(layer->visibleRegionScreen);
874 }
875 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700876 }
877 err = hwc.prepare();
878 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
879 }
880
Mathias Agopian45721772010-08-12 15:03:26 -0700881 /*
882 * clear the area of the FB that need to be transparent
883 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700884 transparent.andSelf(dirty);
885 if (!transparent.isEmpty()) {
886 glClearColor(0,0,0,0);
887 Region::const_iterator it = transparent.begin();
888 Region::const_iterator const end = transparent.end();
889 const int32_t height = hw.getHeight();
890 while (it != end) {
891 const Rect& r(*it++);
892 const GLint sy = height - (r.top + r.height());
893 glScissor(r.left, sy, r.width(), r.height());
894 glClear(GL_COLOR_BUFFER_BIT);
895 }
896 }
Mathias Agopian45721772010-08-12 15:03:26 -0700897
898
899 /*
900 * and then, render the layers targeted at the framebuffer
901 */
902 for (size_t i=0 ; i<count ; i++) {
903 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700904 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
905 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700906 // skip layers handled by the HAL
907 continue;
908 }
909 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700910
Mathias Agopian45721772010-08-12 15:03:26 -0700911 const sp<LayerBase>& layer(layers[i]);
912 const Region clip(dirty.intersect(layer->visibleRegionScreen));
913 if (!clip.isEmpty()) {
914 layer->draw(clip);
915 }
916 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917}
918
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919void SurfaceFlinger::debugFlashRegions()
920{
Mathias Agopian0a917752010-06-14 21:20:00 -0700921 const DisplayHardware& hw(graphicPlane(0).displayHardware());
922 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700923
Mathias Agopian0a917752010-06-14 21:20:00 -0700924 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
925 (flags & DisplayHardware::BUFFER_PRESERVED))) {
926 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
927 mDirtyRegion.bounds() : hw.bounds());
928 composeSurfaces(repaint);
929 }
930
931 TextureManager::deactivateTextures();
932
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 glDisable(GL_BLEND);
934 glDisable(GL_DITHER);
935 glDisable(GL_SCISSOR_TEST);
936
Mathias Agopian0926f502009-05-04 14:17:04 -0700937 static int toggle = 0;
938 toggle = 1 - toggle;
939 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700940 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700941 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700942 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700943 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944
Mathias Agopian20f68782009-05-11 00:03:41 -0700945 Region::const_iterator it = mDirtyRegion.begin();
946 Region::const_iterator const end = mDirtyRegion.end();
947 while (it != end) {
948 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 GLfloat vertices[][2] = {
950 { r.left, r.top },
951 { r.left, r.bottom },
952 { r.right, r.bottom },
953 { r.right, r.top }
954 };
955 glVertexPointer(2, GL_FLOAT, 0, vertices);
956 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
957 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700958
Mathias Agopianb8a55602009-06-26 19:06:36 -0700959 if (mInvalidRegion.isEmpty()) {
960 mDirtyRegion.dump("mDirtyRegion");
961 mInvalidRegion.dump("mInvalidRegion");
962 }
963 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800964
965 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700966 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967
968 glEnable(GL_SCISSOR_TEST);
969 //mDirtyRegion.dump("mDirtyRegion");
970}
971
972void SurfaceFlinger::drawWormhole() const
973{
974 const Region region(mWormholeRegion.intersect(mDirtyRegion));
975 if (region.isEmpty())
976 return;
977
978 const DisplayHardware& hw(graphicPlane(0).displayHardware());
979 const int32_t width = hw.getWidth();
980 const int32_t height = hw.getHeight();
981
982 glDisable(GL_BLEND);
983 glDisable(GL_DITHER);
984
985 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700986 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700987 Region::const_iterator it = region.begin();
988 Region::const_iterator const end = region.end();
989 while (it != end) {
990 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 const GLint sy = height - (r.top + r.height());
992 glScissor(r.left, sy, r.width(), r.height());
993 glClear(GL_COLOR_BUFFER_BIT);
994 }
995 } else {
996 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
997 { width, height }, { 0, height } };
998 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
999 glVertexPointer(2, GL_SHORT, 0, vertices);
1000 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1001 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001002#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001003 if (GLExtensions::getInstance().haveTextureExternal()) {
1004 glDisable(GL_TEXTURE_EXTERNAL_OES);
1005 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001006#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 glEnable(GL_TEXTURE_2D);
1008 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1009 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1010 glMatrixMode(GL_TEXTURE);
1011 glLoadIdentity();
1012 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001013 Region::const_iterator it = region.begin();
1014 Region::const_iterator const end = region.end();
1015 while (it != end) {
1016 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 const GLint sy = height - (r.top + r.height());
1018 glScissor(r.left, sy, r.width(), r.height());
1019 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1020 }
1021 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001022 glLoadIdentity();
1023 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 }
1025}
1026
1027void SurfaceFlinger::debugShowFPS() const
1028{
1029 static int mFrameCount;
1030 static int mLastFrameCount = 0;
1031 static nsecs_t mLastFpsTime = 0;
1032 static float mFps = 0;
1033 mFrameCount++;
1034 nsecs_t now = systemTime();
1035 nsecs_t diff = now - mLastFpsTime;
1036 if (diff > ms2ns(250)) {
1037 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1038 mLastFpsTime = now;
1039 mLastFrameCount = mFrameCount;
1040 }
1041 // XXX: mFPS has the value we want
1042 }
1043
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001044status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045{
1046 Mutex::Autolock _l(mStateLock);
1047 addLayer_l(layer);
1048 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1049 return NO_ERROR;
1050}
1051
Mathias Agopian96f08192010-06-02 23:28:45 -07001052status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1053{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001054 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001055 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1056}
1057
1058ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1059 const sp<LayerBaseClient>& lbc)
1060{
1061 Mutex::Autolock _l(mStateLock);
1062
1063 // attach this layer to the client
1064 ssize_t name = client->attachLayer(lbc);
1065
1066 // add this layer to the current state list
1067 addLayer_l(lbc);
1068
1069 return name;
1070}
1071
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001072status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073{
1074 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001075 status_t err = purgatorizeLayer_l(layer);
1076 if (err == NO_ERROR)
1077 setTransactionFlags(eTransactionNeeded);
1078 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079}
1080
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001081status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001083 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1084 if (lbc != 0) {
1085 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1086 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1088 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001089 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 return NO_ERROR;
1091 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001092 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093}
1094
Mathias Agopian9a112062009-04-17 19:36:26 -07001095status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1096{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001097 // First add the layer to the purgatory list, which makes sure it won't
1098 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001099 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001100 if (err >= 0) {
1101 mLayerPurgatory.add(layerBase);
1102 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001103
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001104 layerBase->onRemoved();
1105
Mathias Agopian3d579642009-06-04 18:46:21 -07001106 // it's possible that we don't find a layer, because it might
1107 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001108 // from the user because there is a race between Client::destroySurface(),
1109 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001110 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1111}
1112
Mathias Agopian96f08192010-06-02 23:28:45 -07001113status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114{
Mathias Agopian96f08192010-06-02 23:28:45 -07001115 layer->forceVisibilityTransaction();
1116 setTransactionFlags(eTraversalNeeded);
1117 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118}
1119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1121{
1122 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1123}
1124
Mathias Agopianbb641242010-05-18 17:06:55 -07001125uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126{
1127 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1128 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001129 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 }
1131 return old;
1132}
1133
1134void SurfaceFlinger::openGlobalTransaction()
1135{
1136 android_atomic_inc(&mTransactionCount);
1137}
1138
1139void SurfaceFlinger::closeGlobalTransaction()
1140{
1141 if (android_atomic_dec(&mTransactionCount) == 1) {
1142 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001143
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001144 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001145 // take effect before returning.
1146 Mutex::Autolock _l(mStateLock);
1147 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001148 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1149 if (CC_UNLIKELY(err != NO_ERROR)) {
1150 // just in case something goes wrong in SF, return to the
1151 // called after a few seconds.
1152 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1153 mResizeTransationPending = false;
1154 break;
1155 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001156 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 }
1158}
1159
1160status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1161{
1162 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1163 return BAD_VALUE;
1164
1165 Mutex::Autolock _l(mStateLock);
1166 mCurrentState.freezeDisplay = 1;
1167 setTransactionFlags(eTransactionNeeded);
1168
1169 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001170 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 return NO_ERROR;
1172}
1173
1174status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1175{
1176 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1177 return BAD_VALUE;
1178
1179 Mutex::Autolock _l(mStateLock);
1180 mCurrentState.freezeDisplay = 0;
1181 setTransactionFlags(eTransactionNeeded);
1182
1183 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001184 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 return NO_ERROR;
1186}
1187
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001188int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001189 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190{
1191 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1192 return BAD_VALUE;
1193
1194 Mutex::Autolock _l(mStateLock);
1195 if (mCurrentState.orientation != orientation) {
1196 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001197 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 mCurrentState.orientation = orientation;
1199 setTransactionFlags(eTransactionNeeded);
1200 mTransactionCV.wait(mStateLock);
1201 } else {
1202 orientation = BAD_VALUE;
1203 }
1204 }
1205 return orientation;
1206}
1207
Mathias Agopian96f08192010-06-02 23:28:45 -07001208sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001209 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1211 uint32_t flags)
1212{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001213 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001215
1216 if (int32_t(w|h) < 0) {
1217 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1218 int(w), int(h));
1219 return surfaceHandle;
1220 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001223 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 switch (flags & eFXSurfaceMask) {
1225 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001226 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1227 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 break;
1229 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001230 // for now we treat Blur as Dim, until we can implement it
1231 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001233 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 break;
1235 }
1236
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001237 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001238 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001239 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001240 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001243 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001244 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001245 params->identity = surfaceHandle->getIdentity();
1246 params->width = w;
1247 params->height = h;
1248 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001249 if (normalLayer != 0) {
1250 Mutex::Autolock _l(mStateLock);
1251 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1252 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001253 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001254
Mathias Agopian96f08192010-06-02 23:28:45 -07001255 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 }
1257
1258 return surfaceHandle;
1259}
1260
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001261sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001262 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001263 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001264 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265{
1266 // initialize the surfaces
1267 switch (format) { // TODO: take h/w into account
1268 case PIXEL_FORMAT_TRANSPARENT:
1269 case PIXEL_FORMAT_TRANSLUCENT:
1270 format = PIXEL_FORMAT_RGBA_8888;
1271 break;
1272 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001273#ifdef NO_RGBX_8888
1274 format = PIXEL_FORMAT_RGB_565;
1275#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001276 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001277#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 break;
1279 }
1280
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001281#ifdef NO_RGBX_8888
1282 if (format == PIXEL_FORMAT_RGBX_8888)
1283 format = PIXEL_FORMAT_RGBA_8888;
1284#endif
1285
Mathias Agopian96f08192010-06-02 23:28:45 -07001286 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001287 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001288 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001290 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 }
1292 return layer;
1293}
1294
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001295sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001296 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001297 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298{
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 return layer;
1302}
1303
Mathias Agopian96f08192010-06-02 23:28:45 -07001304status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305{
Mathias Agopian9a112062009-04-17 19:36:26 -07001306 /*
1307 * called by the window manager, when a surface should be marked for
1308 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001309 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001310 * The surface is removed from the current and drawing lists, but placed
1311 * in the purgatory queue, so it's not destroyed right-away (we need
1312 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001313 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001314
Mathias Agopian48d819a2009-09-10 19:41:18 -07001315 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001316 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001317 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001318 if (layer != 0) {
1319 err = purgatorizeLayer_l(layer);
1320 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001321 setTransactionFlags(eTransactionNeeded);
1322 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001323 }
1324 return err;
1325}
1326
1327status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1328{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001329 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001330
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001331 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001332 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001333 sp<LayerBaseClient> layer;
1334 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001335 MessageDestroySurface(
1336 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1337 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001338 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001339 sp<LayerBaseClient> l(layer);
1340 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001341 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001342 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001343 * remove the layer from the current list -- chances are that it's
1344 * not in the list anyway, because it should have been removed
1345 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001346 * However, a buggy client could have not done that.
1347 * Since we know we don't have any more clients, we don't need
1348 * to use the purgatory.
1349 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001350 status_t err = flinger->removeLayer_l(l);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001351 if (err == NAME_NOT_FOUND) {
1352 // The surface wasn't in the current list, which means it was
1353 // removed already, which means it is in the purgatory,
1354 // and need to be removed from there.
1355 // This needs to happen from the main thread since its dtor
1356 // must run from there (b/c of OpenGL ES). Additionally, we
1357 // can't really acquire our internal lock from
1358 // destroySurface() -- see postMessage() below.
1359 ssize_t idx = flinger->mLayerPurgatory.remove(l);
1360 LOGE_IF(idx < 0,
1361 "layer=%p is not in the purgatory list", l.get());
1362 }
1363
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001364 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1365 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001366 return true;
1367 }
1368 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001369
Mathias Agopianbb641242010-05-18 17:06:55 -07001370 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 return NO_ERROR;
1372}
1373
1374status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001375 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 int32_t count,
1377 const layer_state_t* states)
1378{
1379 Mutex::Autolock _l(mStateLock);
1380 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001382 const layer_state_t& s(states[i]);
1383 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001384 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 if (what & ePositionChanged) {
1387 if (layer->setPosition(s.x, s.y))
1388 flags |= eTraversalNeeded;
1389 }
1390 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001391 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001393 mCurrentState.layersSortedByZ.removeAt(idx);
1394 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 // we need traversal (state changed)
1396 // AND transaction (list changed)
1397 flags |= eTransactionNeeded|eTraversalNeeded;
1398 }
1399 }
1400 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001401 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001403 mResizeTransationPending = true;
1404 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 }
1406 if (what & eAlphaChanged) {
1407 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eMatrixChanged) {
1411 if (layer->setMatrix(s.matrix))
1412 flags |= eTraversalNeeded;
1413 }
1414 if (what & eTransparentRegionChanged) {
1415 if (layer->setTransparentRegionHint(s.transparentRegion))
1416 flags |= eTraversalNeeded;
1417 }
1418 if (what & eVisibilityChanged) {
1419 if (layer->setFlags(s.flags, s.mask))
1420 flags |= eTraversalNeeded;
1421 }
1422 }
1423 }
1424 if (flags) {
1425 setTransactionFlags(flags);
1426 }
1427 return NO_ERROR;
1428}
1429
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430void SurfaceFlinger::screenReleased(int dpy)
1431{
1432 // this may be called by a signal handler, we can't do too much in here
1433 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1434 signalEvent();
1435}
1436
1437void SurfaceFlinger::screenAcquired(int dpy)
1438{
1439 // this may be called by a signal handler, we can't do too much in here
1440 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1441 signalEvent();
1442}
1443
1444status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1445{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001446 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447 char buffer[SIZE];
1448 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001449 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450 snprintf(buffer, SIZE, "Permission Denial: "
1451 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1452 IPCThreadState::self()->getCallingPid(),
1453 IPCThreadState::self()->getCallingUid());
1454 result.append(buffer);
1455 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001456
1457 // figure out if we're stuck somewhere
1458 const nsecs_t now = systemTime();
1459 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1460 const nsecs_t inTransaction(mDebugInTransaction);
1461 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1462 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1463
1464 // Try to get the main lock, but don't insist if we can't
1465 // (this would indicate SF is stuck, but we want to be able to
1466 // print something in dumpsys).
1467 int retry = 3;
1468 while (mStateLock.tryLock()<0 && --retry>=0) {
1469 usleep(1000000);
1470 }
1471 const bool locked(retry >= 0);
1472 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001473 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001474 "SurfaceFlinger appears to be unresponsive, "
1475 "dumping anyways (no locks held)\n");
1476 result.append(buffer);
1477 }
1478
Mathias Agopian48b888a2011-01-19 16:15:53 -08001479 /*
1480 * Dump the visible layer list
1481 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1483 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001484 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1485 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001487 const sp<LayerBase>& layer(currentLayers[i]);
1488 layer->dump(result, buffer, SIZE);
1489 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 s.transparentRegion.dump(result, "transparentRegion");
1491 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1492 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1493 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001494
Mathias Agopian48b888a2011-01-19 16:15:53 -08001495 /*
1496 * Dump the layers in the purgatory
1497 */
1498
1499 const size_t purgatorySize = mLayerPurgatory.size();
1500 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1501 result.append(buffer);
1502 for (size_t i=0 ; i<purgatorySize ; i++) {
1503 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1504 layer->shortDump(result, buffer, SIZE);
1505 }
1506
1507 /*
1508 * Dump SurfaceFlinger global state
1509 */
1510
1511 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1512 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 mWormholeRegion.dump(result, "WormholeRegion");
1514 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1515 snprintf(buffer, SIZE,
1516 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1517 mFreezeDisplay?"yes":"no", mFreezeCount,
1518 mCurrentState.orientation, hw.canDraw());
1519 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001520 snprintf(buffer, SIZE,
1521 " last eglSwapBuffers() time: %f us\n"
1522 " last transaction time : %f us\n",
1523 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1524 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001525
Mathias Agopian9795c422009-08-26 16:36:26 -07001526 if (inSwapBuffersDuration || !locked) {
1527 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1528 inSwapBuffersDuration/1000.0);
1529 result.append(buffer);
1530 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001531
Mathias Agopian9795c422009-08-26 16:36:26 -07001532 if (inTransactionDuration || !locked) {
1533 snprintf(buffer, SIZE, " transaction time: %f us\n",
1534 inTransactionDuration/1000.0);
1535 result.append(buffer);
1536 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001537
Mathias Agopian48b888a2011-01-19 16:15:53 -08001538 /*
1539 * Dump HWComposer state
1540 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001541 HWComposer& hwc(hw.getHwComposer());
1542 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1543 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1544 mDebugDisableHWC ? "disabled" : "enabled");
1545 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001546 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001547
Mathias Agopian48b888a2011-01-19 16:15:53 -08001548 /*
1549 * Dump gralloc state
1550 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001551 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001552 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001553 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001554
1555 if (locked) {
1556 mStateLock.unlock();
1557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 }
1559 write(fd, result.string(), result.size());
1560 return NO_ERROR;
1561}
1562
1563status_t SurfaceFlinger::onTransact(
1564 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1565{
1566 switch (code) {
1567 case CREATE_CONNECTION:
1568 case OPEN_GLOBAL_TRANSACTION:
1569 case CLOSE_GLOBAL_TRANSACTION:
1570 case SET_ORIENTATION:
1571 case FREEZE_DISPLAY:
1572 case UNFREEZE_DISPLAY:
1573 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001574 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001575 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 {
1577 // codes that require permission check
1578 IPCThreadState* ipc = IPCThreadState::self();
1579 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001580 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001581 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1582 LOGE("Permission Denial: "
1583 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1584 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001586 break;
1587 }
1588 case CAPTURE_SCREEN:
1589 {
1590 // codes that require permission check
1591 IPCThreadState* ipc = IPCThreadState::self();
1592 const int pid = ipc->getCallingPid();
1593 const int uid = ipc->getCallingUid();
1594 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1595 LOGE("Permission Denial: "
1596 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1597 return PERMISSION_DENIED;
1598 }
1599 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 }
1601 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1604 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001605 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001606 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1607 IPCThreadState* ipc = IPCThreadState::self();
1608 const int pid = ipc->getCallingPid();
1609 const int uid = ipc->getCallingUid();
1610 LOGE("Permission Denial: "
1611 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 return PERMISSION_DENIED;
1613 }
1614 int n;
1615 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001616 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001617 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 return NO_ERROR;
1619 case 1002: // SHOW_UPDATES
1620 n = data.readInt32();
1621 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1622 return NO_ERROR;
1623 case 1003: // SHOW_BACKGROUND
1624 n = data.readInt32();
1625 mDebugBackground = n ? 1 : 0;
1626 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001627 case 1008: // toggle use of hw composer
1628 n = data.readInt32();
1629 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianad456f92011-01-13 17:53:01 -08001630 invalidateHwcGeometry();
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001631 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 case 1004:{ // repaint everything
1633 Mutex::Autolock _l(mStateLock);
1634 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1635 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1636 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001637 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001639 case 1005:{ // force transaction
1640 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1641 return NO_ERROR;
1642 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001643 case 1006:{ // enable/disable GraphicLog
1644 int enabled = data.readInt32();
1645 GraphicLog::getInstance().setEnabled(enabled);
1646 return NO_ERROR;
1647 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648 case 1007: // set mFreezeCount
1649 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001650 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651 return NO_ERROR;
1652 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001653 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 reply->writeInt32(0);
1655 reply->writeInt32(mDebugRegion);
1656 reply->writeInt32(mDebugBackground);
1657 return NO_ERROR;
1658 case 1013: {
1659 Mutex::Autolock _l(mStateLock);
1660 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1661 reply->writeInt32(hw.getPageFlipCount());
1662 }
1663 return NO_ERROR;
1664 }
1665 }
1666 return err;
1667}
1668
Mathias Agopian59119e62010-10-11 12:37:43 -07001669// ---------------------------------------------------------------------------
1670
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001671status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1672 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001673{
Mathias Agopian59119e62010-10-11 12:37:43 -07001674 if (!GLExtensions::getInstance().haveFramebufferObject())
1675 return INVALID_OPERATION;
1676
1677 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001678 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001679 const uint32_t hw_w = hw.getWidth();
1680 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001681 GLfloat u = 1;
1682 GLfloat v = 1;
1683
1684 // make sure to clear all GL error flags
1685 while ( glGetError() != GL_NO_ERROR ) ;
1686
1687 // create a FBO
1688 GLuint name, tname;
1689 glGenTextures(1, &tname);
1690 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001691 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1692 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001693 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001694 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001695 GLint tw = (2 << (31 - clz(hw_w)));
1696 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001697 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1698 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001699 u = GLfloat(hw_w) / tw;
1700 v = GLfloat(hw_h) / th;
1701 }
1702 glGenFramebuffersOES(1, &name);
1703 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001704 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1705 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001706
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001707 // redraw the screen entirely...
1708 glClearColor(0,0,0,1);
1709 glClear(GL_COLOR_BUFFER_BIT);
1710 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1711 const size_t count = layers.size();
1712 for (size_t i=0 ; i<count ; ++i) {
1713 const sp<LayerBase>& layer(layers[i]);
1714 layer->drawForSreenShot();
1715 }
1716
1717 // back to main framebuffer
1718 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1719 glDisable(GL_SCISSOR_TEST);
1720 glDeleteFramebuffersOES(1, &name);
1721
1722 *textureName = tname;
1723 *uOut = u;
1724 *vOut = v;
1725 return NO_ERROR;
1726}
1727
1728// ---------------------------------------------------------------------------
1729
1730status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1731{
1732 status_t result = PERMISSION_DENIED;
1733
1734 if (!GLExtensions::getInstance().haveFramebufferObject())
1735 return INVALID_OPERATION;
1736
1737 // get screen geometry
1738 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1739 const uint32_t hw_w = hw.getWidth();
1740 const uint32_t hw_h = hw.getHeight();
1741 const Region screenBounds(hw.bounds());
1742
1743 GLfloat u, v;
1744 GLuint tname;
1745 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1746 if (result != NO_ERROR) {
1747 return result;
1748 }
1749
1750 GLfloat vtx[8];
1751 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1752 glEnable(GL_TEXTURE_2D);
1753 glBindTexture(GL_TEXTURE_2D, tname);
1754 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1755 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1756 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1757 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1758 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1759 glVertexPointer(2, GL_FLOAT, 0, vtx);
1760
1761 class s_curve_interpolator {
1762 const float nbFrames, s, v;
1763 public:
1764 s_curve_interpolator(int nbFrames, float s)
1765 : nbFrames(1.0f / (nbFrames-1)), s(s),
1766 v(1.0f + expf(-s + 0.5f*s)) {
1767 }
1768 float operator()(int f) {
1769 const float x = f * nbFrames;
1770 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1771 }
1772 };
1773
1774 class v_stretch {
1775 const GLfloat hw_w, hw_h;
1776 public:
1777 v_stretch(uint32_t hw_w, uint32_t hw_h)
1778 : hw_w(hw_w), hw_h(hw_h) {
1779 }
1780 void operator()(GLfloat* vtx, float v) {
1781 const GLfloat w = hw_w + (hw_w * v);
1782 const GLfloat h = hw_h - (hw_h * v);
1783 const GLfloat x = (hw_w - w) * 0.5f;
1784 const GLfloat y = (hw_h - h) * 0.5f;
1785 vtx[0] = x; vtx[1] = y;
1786 vtx[2] = x; vtx[3] = y + h;
1787 vtx[4] = x + w; vtx[5] = y + h;
1788 vtx[6] = x + w; vtx[7] = y;
1789 }
1790 };
1791
1792 class h_stretch {
1793 const GLfloat hw_w, hw_h;
1794 public:
1795 h_stretch(uint32_t hw_w, uint32_t hw_h)
1796 : hw_w(hw_w), hw_h(hw_h) {
1797 }
1798 void operator()(GLfloat* vtx, float v) {
1799 const GLfloat w = hw_w - (hw_w * v);
1800 const GLfloat h = 1.0f;
1801 const GLfloat x = (hw_w - w) * 0.5f;
1802 const GLfloat y = (hw_h - h) * 0.5f;
1803 vtx[0] = x; vtx[1] = y;
1804 vtx[2] = x; vtx[3] = y + h;
1805 vtx[4] = x + w; vtx[5] = y + h;
1806 vtx[6] = x + w; vtx[7] = y;
1807 }
1808 };
1809
1810 // the full animation is 24 frames
1811 const int nbFrames = 12;
1812 s_curve_interpolator itr(nbFrames, 7.5f);
1813 s_curve_interpolator itg(nbFrames, 8.0f);
1814 s_curve_interpolator itb(nbFrames, 8.5f);
1815
1816 v_stretch vverts(hw_w, hw_h);
1817 glEnable(GL_BLEND);
1818 glBlendFunc(GL_ONE, GL_ONE);
1819 for (int i=0 ; i<nbFrames ; i++) {
1820 float x, y, w, h;
1821 const float vr = itr(i);
1822 const float vg = itg(i);
1823 const float vb = itb(i);
1824
1825 // clear screen
1826 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001827 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001828 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001829
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001830 // draw the red plane
1831 vverts(vtx, vr);
1832 glColorMask(1,0,0,1);
1833 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001834
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001835 // draw the green plane
1836 vverts(vtx, vg);
1837 glColorMask(0,1,0,1);
1838 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001839
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001840 // draw the blue plane
1841 vverts(vtx, vb);
1842 glColorMask(0,0,1,1);
1843 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001844
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001845 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001846 glDisable(GL_TEXTURE_2D);
1847 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001848 glColor4f(vg, vg, vg, 1);
1849 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1850 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001851 }
1852
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001853 h_stretch hverts(hw_w, hw_h);
1854 glDisable(GL_BLEND);
1855 glDisable(GL_TEXTURE_2D);
1856 glColorMask(1,1,1,1);
1857 for (int i=0 ; i<nbFrames ; i++) {
1858 const float v = itg(i);
1859 hverts(vtx, v);
1860 glClear(GL_COLOR_BUFFER_BIT);
1861 glColor4f(1-v, 1-v, 1-v, 1);
1862 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1863 hw.flip(screenBounds);
1864 }
1865
1866 glColorMask(1,1,1,1);
1867 glEnable(GL_SCISSOR_TEST);
1868 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1869 glDeleteTextures(1, &tname);
1870 return NO_ERROR;
1871}
1872
1873status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1874{
1875 status_t result = PERMISSION_DENIED;
1876
1877 if (!GLExtensions::getInstance().haveFramebufferObject())
1878 return INVALID_OPERATION;
1879
1880
1881 // get screen geometry
1882 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1883 const uint32_t hw_w = hw.getWidth();
1884 const uint32_t hw_h = hw.getHeight();
1885 const Region screenBounds(hw.bounds());
1886
1887 GLfloat u, v;
1888 GLuint tname;
1889 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1890 if (result != NO_ERROR) {
1891 return result;
1892 }
1893
1894 // back to main framebuffer
1895 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1896 glDisable(GL_SCISSOR_TEST);
1897
1898 GLfloat vtx[8];
1899 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1900 glEnable(GL_TEXTURE_2D);
1901 glBindTexture(GL_TEXTURE_2D, tname);
1902 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1903 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1904 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1905 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1906 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1907 glVertexPointer(2, GL_FLOAT, 0, vtx);
1908
1909 class s_curve_interpolator {
1910 const float nbFrames, s, v;
1911 public:
1912 s_curve_interpolator(int nbFrames, float s)
1913 : nbFrames(1.0f / (nbFrames-1)), s(s),
1914 v(1.0f + expf(-s + 0.5f*s)) {
1915 }
1916 float operator()(int f) {
1917 const float x = f * nbFrames;
1918 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1919 }
1920 };
1921
1922 class v_stretch {
1923 const GLfloat hw_w, hw_h;
1924 public:
1925 v_stretch(uint32_t hw_w, uint32_t hw_h)
1926 : hw_w(hw_w), hw_h(hw_h) {
1927 }
1928 void operator()(GLfloat* vtx, float v) {
1929 const GLfloat w = hw_w + (hw_w * v);
1930 const GLfloat h = hw_h - (hw_h * v);
1931 const GLfloat x = (hw_w - w) * 0.5f;
1932 const GLfloat y = (hw_h - h) * 0.5f;
1933 vtx[0] = x; vtx[1] = y;
1934 vtx[2] = x; vtx[3] = y + h;
1935 vtx[4] = x + w; vtx[5] = y + h;
1936 vtx[6] = x + w; vtx[7] = y;
1937 }
1938 };
1939
1940 class h_stretch {
1941 const GLfloat hw_w, hw_h;
1942 public:
1943 h_stretch(uint32_t hw_w, uint32_t hw_h)
1944 : hw_w(hw_w), hw_h(hw_h) {
1945 }
1946 void operator()(GLfloat* vtx, float v) {
1947 const GLfloat w = hw_w - (hw_w * v);
1948 const GLfloat h = 1.0f;
1949 const GLfloat x = (hw_w - w) * 0.5f;
1950 const GLfloat y = (hw_h - h) * 0.5f;
1951 vtx[0] = x; vtx[1] = y;
1952 vtx[2] = x; vtx[3] = y + h;
1953 vtx[4] = x + w; vtx[5] = y + h;
1954 vtx[6] = x + w; vtx[7] = y;
1955 }
1956 };
1957
Mathias Agopiana6546e52010-10-14 12:33:07 -07001958 // the full animation is 12 frames
1959 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001960 s_curve_interpolator itr(nbFrames, 7.5f);
1961 s_curve_interpolator itg(nbFrames, 8.0f);
1962 s_curve_interpolator itb(nbFrames, 8.5f);
1963
1964 h_stretch hverts(hw_w, hw_h);
1965 glDisable(GL_BLEND);
1966 glDisable(GL_TEXTURE_2D);
1967 glColorMask(1,1,1,1);
1968 for (int i=nbFrames-1 ; i>=0 ; i--) {
1969 const float v = itg(i);
1970 hverts(vtx, v);
1971 glClear(GL_COLOR_BUFFER_BIT);
1972 glColor4f(1-v, 1-v, 1-v, 1);
1973 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1974 hw.flip(screenBounds);
1975 }
1976
Mathias Agopiana6546e52010-10-14 12:33:07 -07001977 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001978 v_stretch vverts(hw_w, hw_h);
1979 glEnable(GL_BLEND);
1980 glBlendFunc(GL_ONE, GL_ONE);
1981 for (int i=nbFrames-1 ; i>=0 ; i--) {
1982 float x, y, w, h;
1983 const float vr = itr(i);
1984 const float vg = itg(i);
1985 const float vb = itb(i);
1986
1987 // clear screen
1988 glColorMask(1,1,1,1);
1989 glClear(GL_COLOR_BUFFER_BIT);
1990 glEnable(GL_TEXTURE_2D);
1991
1992 // draw the red plane
1993 vverts(vtx, vr);
1994 glColorMask(1,0,0,1);
1995 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1996
1997 // draw the green plane
1998 vverts(vtx, vg);
1999 glColorMask(0,1,0,1);
2000 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2001
2002 // draw the blue plane
2003 vverts(vtx, vb);
2004 glColorMask(0,0,1,1);
2005 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2006
2007 hw.flip(screenBounds);
2008 }
2009
2010 glColorMask(1,1,1,1);
2011 glEnable(GL_SCISSOR_TEST);
2012 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002013 glDeleteTextures(1, &tname);
2014
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002015 return NO_ERROR;
2016}
2017
2018// ---------------------------------------------------------------------------
2019
Mathias Agopianabd671a2010-10-14 14:54:06 -07002020status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021{
2022 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2023 if (!hw.canDraw()) {
2024 // we're already off
2025 return NO_ERROR;
2026 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002027 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2028 electronBeamOffAnimationImplLocked();
2029 }
2030
2031 // always clear the whole screen at the end of the animation
2032 glClearColor(0,0,0,1);
2033 glDisable(GL_SCISSOR_TEST);
2034 glClear(GL_COLOR_BUFFER_BIT);
2035 glEnable(GL_SCISSOR_TEST);
2036 hw.flip( Region(hw.bounds()) );
2037
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002038 hw.setCanDraw(false);
2039 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002040}
2041
2042status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2043{
Mathias Agopian59119e62010-10-11 12:37:43 -07002044 class MessageTurnElectronBeamOff : public MessageBase {
2045 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002046 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002047 status_t result;
2048 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002049 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2050 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002051 }
2052 status_t getResult() const {
2053 return result;
2054 }
2055 virtual bool handler() {
2056 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002057 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002058 return true;
2059 }
2060 };
2061
Mathias Agopianabd671a2010-10-14 14:54:06 -07002062 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002063 status_t res = postMessageSync(msg);
2064 if (res == NO_ERROR) {
2065 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002066
2067 // work-around: when the power-manager calls us we activate the
2068 // animation. eventually, the "on" animation will be called
2069 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002070 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002071 }
2072 return res;
2073}
2074
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002075// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002076
Mathias Agopianabd671a2010-10-14 14:54:06 -07002077status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002078{
2079 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2080 if (hw.canDraw()) {
2081 // we're already on
2082 return NO_ERROR;
2083 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002084 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2085 electronBeamOnAnimationImplLocked();
2086 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002087 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002088
2089 // make sure to redraw the whole screen when the animation is done
2090 mDirtyRegion.set(hw.bounds());
2091 signalEvent();
2092
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002093 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002094}
2095
2096status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2097{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098 class MessageTurnElectronBeamOn : public MessageBase {
2099 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002100 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002101 status_t result;
2102 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002103 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2104 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002105 }
2106 status_t getResult() const {
2107 return result;
2108 }
2109 virtual bool handler() {
2110 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002111 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002112 return true;
2113 }
2114 };
2115
Mathias Agopianabd671a2010-10-14 14:54:06 -07002116 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002117 return NO_ERROR;
2118}
2119
2120// ---------------------------------------------------------------------------
2121
Mathias Agopian74c40c02010-09-29 13:02:36 -07002122status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2123 sp<IMemoryHeap>* heap,
2124 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002125 uint32_t sw, uint32_t sh,
2126 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002127{
2128 status_t result = PERMISSION_DENIED;
2129
2130 // only one display supported for now
2131 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2132 return BAD_VALUE;
2133
2134 if (!GLExtensions::getInstance().haveFramebufferObject())
2135 return INVALID_OPERATION;
2136
2137 // get screen geometry
2138 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2139 const uint32_t hw_w = hw.getWidth();
2140 const uint32_t hw_h = hw.getHeight();
2141
2142 if ((sw > hw_w) || (sh > hw_h))
2143 return BAD_VALUE;
2144
2145 sw = (!sw) ? hw_w : sw;
2146 sh = (!sh) ? hw_h : sh;
2147 const size_t size = sw * sh * 4;
2148
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002149 LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2150 sw, sh, minLayerZ, maxLayerZ);
2151
Mathias Agopian74c40c02010-09-29 13:02:36 -07002152 // make sure to clear all GL error flags
2153 while ( glGetError() != GL_NO_ERROR ) ;
2154
2155 // create a FBO
2156 GLuint name, tname;
2157 glGenRenderbuffersOES(1, &tname);
2158 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2159 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2160 glGenFramebuffersOES(1, &name);
2161 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2162 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2163 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2164
2165 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002166
2167 LOGD("screenshot: FBO created, status=0x%x", status);
2168
Mathias Agopian74c40c02010-09-29 13:02:36 -07002169 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2170
2171 // invert everything, b/c glReadPixel() below will invert the FB
2172 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002173 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002174 glMatrixMode(GL_PROJECTION);
2175 glPushMatrix();
2176 glLoadIdentity();
2177 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2178 glMatrixMode(GL_MODELVIEW);
2179
2180 // redraw the screen entirely...
2181 glClearColor(0,0,0,1);
2182 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002183
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002184 LOGD("screenshot: glClear() issued");
2185
Mathias Agopian74c40c02010-09-29 13:02:36 -07002186 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2187 const size_t count = layers.size();
2188 for (size_t i=0 ; i<count ; ++i) {
2189 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002190 const uint32_t z = layer->drawingState().z;
2191 if (z >= minLayerZ && z <= maxLayerZ) {
2192 layer->drawForSreenShot();
2193 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002194 }
2195
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002196 LOGD("screenshot: All layers rendered");
2197
Mathias Agopian74c40c02010-09-29 13:02:36 -07002198 // XXX: this is needed on tegra
2199 glScissor(0, 0, sw, sh);
2200
2201 // check for errors and return screen capture
2202 if (glGetError() != GL_NO_ERROR) {
2203 // error while rendering
2204 result = INVALID_OPERATION;
2205 } else {
2206 // allocate shared memory large enough to hold the
2207 // screen capture
2208 sp<MemoryHeapBase> base(
2209 new MemoryHeapBase(size, 0, "screen-capture") );
2210 void* const ptr = base->getBase();
2211 if (ptr) {
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002212
2213 LOGD("screenshot: about to call glReadPixels(0,0,%d,%d,...,%p)",
2214 sw, sh, ptr);
2215
Mathias Agopian74c40c02010-09-29 13:02:36 -07002216 // capture the screen with glReadPixels()
2217 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2218 if (glGetError() == GL_NO_ERROR) {
2219 *heap = base;
2220 *w = sw;
2221 *h = sh;
2222 *f = PIXEL_FORMAT_RGBA_8888;
2223 result = NO_ERROR;
2224 }
2225 } else {
2226 result = NO_MEMORY;
2227 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002228
2229 LOGD("screenshot: glReadPixels() returned %s", strerror(result));
2230
Mathias Agopian74c40c02010-09-29 13:02:36 -07002231 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002232 glEnable(GL_SCISSOR_TEST);
2233 glViewport(0, 0, hw_w, hw_h);
2234 glMatrixMode(GL_PROJECTION);
2235 glPopMatrix();
2236 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002237 } else {
2238 result = BAD_VALUE;
2239 }
2240
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002241 LOGD("screenshot: about to release FBO resources");
2242
Mathias Agopian74c40c02010-09-29 13:02:36 -07002243 // release FBO resources
2244 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2245 glDeleteRenderbuffersOES(1, &tname);
2246 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002247
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002248 LOGD("screenshot: about to call compositionComplete()");
2249
Mathias Agopiane6f09842010-12-15 14:41:59 -08002250 hw.compositionComplete();
2251
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002252 LOGD("screenshot: result = %s", strerror(result));
2253
Mathias Agopian74c40c02010-09-29 13:02:36 -07002254 return result;
2255}
2256
2257
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002258status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2259 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002260 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002261 uint32_t sw, uint32_t sh,
2262 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002263{
2264 // only one display supported for now
2265 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2266 return BAD_VALUE;
2267
2268 if (!GLExtensions::getInstance().haveFramebufferObject())
2269 return INVALID_OPERATION;
2270
2271 class MessageCaptureScreen : public MessageBase {
2272 SurfaceFlinger* flinger;
2273 DisplayID dpy;
2274 sp<IMemoryHeap>* heap;
2275 uint32_t* w;
2276 uint32_t* h;
2277 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002278 uint32_t sw;
2279 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002280 uint32_t minLayerZ;
2281 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002282 status_t result;
2283 public:
2284 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002285 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002286 uint32_t sw, uint32_t sh,
2287 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002288 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002289 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2290 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2291 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002292 {
2293 }
2294 status_t getResult() const {
2295 return result;
2296 }
2297 virtual bool handler() {
2298 Mutex::Autolock _l(flinger->mStateLock);
2299
2300 // if we have secure windows, never allow the screen capture
2301 if (flinger->mSecureFrameBuffer)
2302 return true;
2303
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002305 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002306
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002307 return true;
2308 }
2309 };
2310
2311 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002312 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002313 status_t res = postMessageSync(msg);
2314 if (res == NO_ERROR) {
2315 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2316 }
2317 return res;
2318}
2319
2320// ---------------------------------------------------------------------------
2321
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002322sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2323{
2324 sp<Layer> result;
2325 Mutex::Autolock _l(mStateLock);
2326 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2327 return result;
2328}
2329
2330// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002331
Mathias Agopian96f08192010-06-02 23:28:45 -07002332Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002333 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002334{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002335}
2336
Mathias Agopian96f08192010-06-02 23:28:45 -07002337Client::~Client()
2338{
Mathias Agopian96f08192010-06-02 23:28:45 -07002339 const size_t count = mLayers.size();
2340 for (size_t i=0 ; i<count ; i++) {
2341 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2342 if (layer != 0) {
2343 mFlinger->removeLayer(layer);
2344 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002345 }
2346}
2347
Mathias Agopian96f08192010-06-02 23:28:45 -07002348status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002349 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002350}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002351
Mathias Agopian96f08192010-06-02 23:28:45 -07002352ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2353{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002354 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002355 mLayers.add(name, layer);
2356 return name;
2357}
2358
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002359void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002360{
Mathias Agopian96f08192010-06-02 23:28:45 -07002361 // we do a linear search here, because this doesn't happen often
2362 const size_t count = mLayers.size();
2363 for (size_t i=0 ; i<count ; i++) {
2364 if (mLayers.valueAt(i) == layer) {
2365 mLayers.removeItemsAt(i, 1);
2366 break;
2367 }
2368 }
2369}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002370sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2371 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002372 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2373 if (layer != 0) {
2374 lbc = layer.promote();
2375 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002376 }
2377 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002378}
2379
Mathias Agopian96f08192010-06-02 23:28:45 -07002380sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002381 return 0;
2382}
2383ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2384 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002385}
Mathias Agopian96f08192010-06-02 23:28:45 -07002386sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002387 ISurfaceComposerClient::surface_data_t* params, int pid,
2388 const String8& name,
2389 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002390 uint32_t flags)
2391{
Mathias Agopian96f08192010-06-02 23:28:45 -07002392 return mFlinger->createSurface(this, pid, name, params,
2393 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002394}
Mathias Agopian96f08192010-06-02 23:28:45 -07002395status_t Client::destroySurface(SurfaceID sid) {
2396 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002397}
Mathias Agopian96f08192010-06-02 23:28:45 -07002398status_t Client::setState(int32_t count, const layer_state_t* states) {
2399 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002400}
2401
2402// ---------------------------------------------------------------------------
2403
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002404UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2405 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2406{
2407 const int pgsize = getpagesize();
2408 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2409
2410 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2411 "SurfaceFlinger Client control-block");
2412
2413 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2414 if (ctrlblk) { // construct the shared structure in-place.
2415 new(ctrlblk) SharedClient;
2416 }
2417}
2418
2419UserClient::~UserClient()
2420{
2421 if (ctrlblk) {
2422 ctrlblk->~SharedClient(); // destroy our shared-structure.
2423 }
2424
2425 /*
2426 * When a UserClient dies, it's unclear what to do exactly.
2427 * We could go ahead and destroy all surfaces linked to that client
2428 * however, it wouldn't be fair to the main Client
2429 * (usually the the window-manager), which might want to re-target
2430 * the layer to another UserClient.
2431 * I think the best is to do nothing, or not much; in most cases the
2432 * WM itself will go ahead and clean things up when it detects a client of
2433 * his has died.
2434 * The remaining question is what to display? currently we keep
2435 * just keep the current buffer.
2436 */
2437}
2438
2439status_t UserClient::initCheck() const {
2440 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2441}
2442
2443void UserClient::detachLayer(const Layer* layer)
2444{
2445 int32_t name = layer->getToken();
2446 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002447 int32_t mask = 1LU<<name;
2448 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2449 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2450 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002451 }
2452}
2453
2454sp<IMemoryHeap> UserClient::getControlBlock() const {
2455 return mCblkHeap;
2456}
2457
2458ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2459{
2460 int32_t name = NAME_NOT_FOUND;
2461 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002462 if (layer == 0) {
2463 return name;
2464 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002465
Mathias Agopian579b3f82010-06-08 19:54:15 -07002466 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002467 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002468 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002469 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002470 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002471
2472 name = 0;
2473 do {
2474 int32_t mask = 1LU<<name;
2475 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2476 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002477 status_t err = layer->setToken(
2478 const_cast<UserClient*>(this), ctrlblk, name);
2479 if (err != NO_ERROR) {
2480 // free the name
2481 android_atomic_and(~mask, &mBitmap);
2482 name = err;
2483 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002484 break;
2485 }
2486 if (++name > 31)
2487 name = NO_MEMORY;
2488 } while(name >= 0);
2489
Mathias Agopian53503a92010-06-08 15:40:56 -07002490 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2491 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002492 return name;
2493}
2494
2495sp<ISurface> UserClient::createSurface(
2496 ISurfaceComposerClient::surface_data_t* params, int pid,
2497 const String8& name,
2498 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2499 uint32_t flags) {
2500 return 0;
2501}
2502status_t UserClient::destroySurface(SurfaceID sid) {
2503 return INVALID_OPERATION;
2504}
2505status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2506 return INVALID_OPERATION;
2507}
2508
2509// ---------------------------------------------------------------------------
2510
Jamie Gennis9a78c902011-01-12 18:30:40 -08002511GraphicBufferAlloc::GraphicBufferAlloc() {}
2512
2513GraphicBufferAlloc::~GraphicBufferAlloc() {}
2514
2515sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2516 PixelFormat format, uint32_t usage) {
2517 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2518 status_t err = graphicBuffer->initCheck();
2519 if (err != 0) {
2520 LOGE("createGraphicBuffer: init check failed: %d", err);
2521 return 0;
2522 } else if (graphicBuffer->handle == 0) {
2523 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2524 return 0;
2525 }
2526 Mutex::Autolock _l(mLock);
2527 mBuffers.add(graphicBuffer);
2528 return graphicBuffer;
2529}
2530
2531void GraphicBufferAlloc::freeAllGraphicBuffersExcept(int bufIdx) {
2532 Mutex::Autolock _l(mLock);
2533 if (0 <= bufIdx && bufIdx < mBuffers.size()) {
2534 sp<GraphicBuffer> b(mBuffers[bufIdx]);
2535 mBuffers.clear();
2536 mBuffers.add(b);
2537 } else {
2538 mBuffers.clear();
2539 }
2540}
2541
2542// ---------------------------------------------------------------------------
2543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002544GraphicPlane::GraphicPlane()
2545 : mHw(0)
2546{
2547}
2548
2549GraphicPlane::~GraphicPlane() {
2550 delete mHw;
2551}
2552
2553bool GraphicPlane::initialized() const {
2554 return mHw ? true : false;
2555}
2556
Mathias Agopian2b92d892010-02-08 15:49:35 -08002557int GraphicPlane::getWidth() const {
2558 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559}
2560
Mathias Agopian2b92d892010-02-08 15:49:35 -08002561int GraphicPlane::getHeight() const {
2562 return mHeight;
2563}
2564
2565void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2566{
2567 mHw = hw;
2568
2569 // initialize the display orientation transform.
2570 // it's a constant that should come from the display driver.
2571 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2572 char property[PROPERTY_VALUE_MAX];
2573 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2574 //displayOrientation
2575 switch (atoi(property)) {
2576 case 90:
2577 displayOrientation = ISurfaceComposer::eOrientation90;
2578 break;
2579 case 270:
2580 displayOrientation = ISurfaceComposer::eOrientation270;
2581 break;
2582 }
2583 }
2584
2585 const float w = hw->getWidth();
2586 const float h = hw->getHeight();
2587 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2588 &mDisplayTransform);
2589 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2590 mDisplayWidth = h;
2591 mDisplayHeight = w;
2592 } else {
2593 mDisplayWidth = w;
2594 mDisplayHeight = h;
2595 }
2596
2597 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002598}
2599
2600status_t GraphicPlane::orientationToTransfrom(
2601 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002602{
2603 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002604 switch (orientation) {
2605 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002606 flags = Transform::ROT_0;
2607 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002608 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002609 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002610 break;
2611 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002612 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002613 break;
2614 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002615 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002616 break;
2617 default:
2618 return BAD_VALUE;
2619 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002620 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002621 return NO_ERROR;
2622}
2623
2624status_t GraphicPlane::setOrientation(int orientation)
2625{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626 // If the rotation can be handled in hardware, this is where
2627 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002628
2629 const DisplayHardware& hw(displayHardware());
2630 const float w = mDisplayWidth;
2631 const float h = mDisplayHeight;
2632 mWidth = int(w);
2633 mHeight = int(h);
2634
2635 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002636 GraphicPlane::orientationToTransfrom(orientation, w, h,
2637 &orientationTransform);
2638 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2639 mWidth = int(h);
2640 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002641 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002642
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002643 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002644 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002645 return NO_ERROR;
2646}
2647
2648const DisplayHardware& GraphicPlane::displayHardware() const {
2649 return *mHw;
2650}
2651
Mathias Agopian59119e62010-10-11 12:37:43 -07002652DisplayHardware& GraphicPlane::editDisplayHardware() {
2653 return *mHw;
2654}
2655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656const Transform& GraphicPlane::transform() const {
2657 return mGlobalTransform;
2658}
2659
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002660EGLDisplay GraphicPlane::getEGLDisplay() const {
2661 return mHw->getEGLDisplay();
2662}
2663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002664// ---------------------------------------------------------------------------
2665
2666}; // namespace android