blob: f5835c90a52448db7f6308dd26eca514a923f20b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian6950e422009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070054#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Mathias Agopian627e7b52009-05-21 19:21:59 -070056/* ideally AID_GRAPHICS would be in a semi-public header
57 * or there would be a way to map a user/group name to its id
58 */
59#ifndef AID_GRAPHICS
60#define AID_GRAPHICS 1003
61#endif
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063#define DISPLAY_COUNT 1
64
65namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066// ---------------------------------------------------------------------------
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068SurfaceFlinger::SurfaceFlinger()
69 : BnSurfaceComposer(), Thread(false),
70 mTransactionFlags(0),
71 mTransactionCount(0),
Mathias Agopian9779b2212009-09-07 16:32:45 -070072 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070073 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 mBootTime(systemTime()),
Mathias Agopian151e8592009-06-15 18:24:59 -070075 mHardwareTest("android.permission.HARDWARE_TEST"),
76 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopianca5edbe2010-09-24 11:26:58 -070077 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian151e8592009-06-15 18:24:59 -070078 mDump("android.permission.DUMP"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070080 mHwWorkListDirty(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070083 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070088 mDebugDisableHWC(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070089 mDebugInSwapBuffers(0),
90 mLastSwapBufferTime(0),
91 mDebugInTransaction(0),
92 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -070093 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 mConsoleSignals(0),
95 mSecureFrameBuffer(0)
96{
97 init();
98}
99
100void SurfaceFlinger::init()
101{
102 LOGI("SurfaceFlinger is starting");
103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 property_get("debug.sf.showbackground", value, "0");
109 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700111 LOGI_IF(mDebugRegion, "showupdates enabled");
112 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}
114
115SurfaceFlinger::~SurfaceFlinger()
116{
117 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118}
119
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700120sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700122 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
Mathias Agopian770492c2010-05-28 14:22:23 -0700125sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700127 sp<ISurfaceComposerClient> bclient;
128 sp<Client> client(new Client(this));
129 status_t err = client->initCheck();
130 if (err == NO_ERROR) {
131 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 return bclient;
134}
135
Mathias Agopian7623da42010-06-01 15:12:58 -0700136sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
137{
138 sp<ISurfaceComposerClient> bclient;
139 sp<UserClient> client(new UserClient(this));
140 status_t err = client->initCheck();
141 if (err == NO_ERROR) {
142 bclient = client;
143 }
144 return bclient;
145}
146
Jamie Gennisf7acf162011-01-12 18:30:40 -0800147sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
148{
149 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
150 return gba;
151}
Mathias Agopian7623da42010-06-01 15:12:58 -0700152
The Android Open Source Project9066cfe2009-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 Hubere3c01832010-08-16 08:49:37 -0700170 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700171 mBootFinished = true;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700172 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Project9066cfe2009-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 Project9066cfe2009-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 Agopiand763b5d2009-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 Hubere3c01832010-08-16 08:49:37 -0700206
Mathias Agopiand763b5d2009-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 Hubere3c01832010-08-16 08:49:37 -0700209
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700210 new(mServerCblk) surface_flinger_cblk_t;
211
The Android Open Source Project9066cfe2009-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 Agopian66c77a52010-02-08 15:49:35 -0800227 dcblk->w = plane.getWidth();
228 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800235
236 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700238 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-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 Agopian627e7b52009-05-21 19:21:59 -0700270 // start boot animation
271 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700272
The Android Open Source Project9066cfe2009-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 Agopian6ead5d92009-04-20 19:39:12 -0700284 while (true) {
285 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800286 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700287 if (UNLIKELY(isFrozen())) {
288 // wait 5 seconds
Mathias Agopian6ead5d92009-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 Projectc39a6e02009-03-11 12:11:56 -0700295 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700296
Mathias Agopian898c4c92010-05-18 17:06:55 -0700297 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-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 Agopian6ead5d92009-04-20 19:39:12 -0700313 if (msg != 0) {
Mathias Agopian6ead5d92009-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 Project9066cfe2009-03-03 19:31:44 -0800319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321}
322
323void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700324 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325}
326
327void SurfaceFlinger::signal() const {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700328 // this is the IPC call
329 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330}
331
Mathias Agopian898c4c92010-05-18 17:06:55 -0700332status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
333 nsecs_t reltime, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334{
Mathias Agopian898c4c92010-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 Project9066cfe2009-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 Agopiane0d5f5b2010-08-10 17:14:02 -0700375 if (UNLIKELY(mHwWorkListDirty)) {
376 // build the h/w work list
377 handleWorkList();
378 }
379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700381 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-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 Project9066cfe2009-03-03 19:31:44 -0800388 handleRepaint();
389
Mathias Agopianb1a18742009-09-17 16:18:16 -0700390 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700391 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700392 hw.compositionComplete();
393
Mathias Agopian04262e92010-09-13 22:57:58 -0700394 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700395 postFramebuffer();
396
Mathias Agopiana5ab8ce2010-09-15 12:29:18 -0700397 logger.log(GraphicLog::SF_UNLOCK_CLIENTS, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Mathias Agopian04262e92010-09-13 22:57:58 -0700400 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 } else {
402 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800403 hw.compositionComplete();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 usleep(16667); // 60 fps period
406 }
407 return true;
408}
409
410void SurfaceFlinger::postFramebuffer()
411{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 if (!mInvalidRegion.isEmpty()) {
413 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700414 const nsecs_t now = systemTime();
415 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700417 mLastSwapBufferTime = systemTime() - now;
418 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421}
422
423void SurfaceFlinger::handleConsoleEvents()
424{
425 // something to do with the console
426 const DisplayHardware& hw = graphicPlane(0).displayHardware();
427
428 int what = android_atomic_and(0, &mConsoleSignals);
429 if (what & eConsoleAcquired) {
430 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700431 // this is a temporary work-around, eventually this should be called
432 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700433 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
435
Mathias Agopianaab758e2010-10-11 12:37:43 -0700436 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700437 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 hw.releaseScreen();
440 }
441
442 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700443 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 hw.releaseScreen();
445 } else {
446 mDeferReleaseConsole = true;
447 }
448 }
449
450 mDirtyRegion.set(hw.bounds());
451}
452
453void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
454{
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700455 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456
Mathias Agopianff1d4102010-08-10 17:19:56 -0700457 /*
458 * Perform and commit the transaction
459 */
460
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700461 { // scope for the lock
462 Mutex::Autolock _l(mStateLock);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700463 const nsecs_t now = systemTime();
464 mDebugInTransaction = now;
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700465 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700466 mLastTransactionTime = systemTime() - now;
467 mDebugInTransaction = 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800468 invalidateHwcGeometry();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700469 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700470 }
471
Mathias Agopianff1d4102010-08-10 17:19:56 -0700472 /*
473 * Clean-up all layers that went away
474 * (do this without the lock held)
475 */
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700476
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700477 const size_t count = ditchedLayers.size();
478 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianffae4fc2009-09-04 19:50:23 -0700479 if (ditchedLayers[i] != 0) {
480 //LOGD("ditching layer %p", ditchedLayers[i].get());
481 ditchedLayers[i]->ditch();
482 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700483 }
484}
485
486void SurfaceFlinger::handleTransactionLocked(
487 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
488{
489 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 const size_t count = currentLayers.size();
491
492 /*
493 * Traversal of the children
494 * (perform the transaction for each of them if needed)
495 */
496
497 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
498 if (layersNeedTransaction) {
499 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700500 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
502 if (!trFlags) continue;
503
504 const uint32_t flags = layer->doTransaction(0);
505 if (flags & Layer::eVisibleRegion)
506 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508 }
509
510 /*
511 * Perform our own transaction if needed
512 */
513
514 if (transactionFlags & eTransactionNeeded) {
515 if (mCurrentState.orientation != mDrawingState.orientation) {
516 // the orientation has changed, recompute all visible regions
517 // and invalidate everything.
518
519 const int dpy = 0;
520 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700521 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 GraphicPlane& plane(graphicPlane(dpy));
523 plane.setOrientation(orientation);
524
525 // update the shared control block
526 const DisplayHardware& hw(plane.displayHardware());
527 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
528 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800529 dcblk->w = plane.getWidth();
530 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531
532 mVisibleRegionsDirty = true;
533 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535
536 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
537 // freezing or unfreezing the display -> trigger animation if needed
538 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800539 if (mFreezeDisplay)
540 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
542
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700543 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
544 // layers have been added
545 mVisibleRegionsDirty = true;
546 }
547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 // some layers might have been removed, so
549 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700550 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700551 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700553 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700554 const size_t count = previousLayers.size();
555 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700556 const sp<LayerBase>& layer(previousLayers[i]);
557 if (currentLayers.indexOf( layer ) < 0) {
558 // this layer is not visible anymore
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700559 ditchedLayers.add(layer);
Mathias Agopian33863dd2009-07-28 14:20:21 -0700560 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700561 }
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
566 commitTransaction();
567}
568
569sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
570{
571 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
572}
573
574void SurfaceFlinger::computeVisibleRegions(
575 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
576{
577 const GraphicPlane& plane(graphicPlane(0));
578 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700579 const DisplayHardware& hw(plane.displayHardware());
580 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581
582 Region aboveOpaqueLayers;
583 Region aboveCoveredLayers;
584 Region dirty;
585
586 bool secureFrameBuffer = false;
587
588 size_t i = currentLayers.size();
589 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700590 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 layer->validateVisibility(planeTransform);
592
593 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700594 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700596 /*
597 * opaqueRegion: area of a surface that is fully opaque.
598 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700600
601 /*
602 * visibleRegion: area of a surface that is visible on screen
603 * and not fully transparent. This is essentially the layer's
604 * footprint minus the opaque regions above it.
605 * Areas covered by a translucent surface are considered visible.
606 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700608
609 /*
610 * coveredRegion: area of a surface that is covered by all
611 * visible regions above it (which includes the translucent areas).
612 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700614
615
616 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700617 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 const bool translucent = layer->needsBlending();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700619 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700621 visibleRegion.andSelf(screenRegion);
622 if (!visibleRegion.isEmpty()) {
623 // Remove the transparent area from the visible region
624 if (translucent) {
625 visibleRegion.subtractSelf(layer->transparentRegionScreen);
626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700628 // compute the opaque region
629 const int32_t layerOrientation = layer->getOrientation();
630 if (s.alpha==255 && !translucent &&
631 ((layerOrientation & Transform::ROT_INVALID) == false)) {
632 // the opaque region is the layer's footprint
633 opaqueRegion = visibleRegion;
634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636 }
637
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700638 // Clip the covered region to the visible region
639 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
640
641 // Update aboveCoveredLayers for next (lower) layer
642 aboveCoveredLayers.orSelf(visibleRegion);
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 // subtract the opaque region covered by the layers above us
645 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646
647 // compute this layer's dirty region
648 if (layer->contentDirty) {
649 // we need to invalidate the whole region
650 dirty = visibleRegion;
651 // as well, as the old visible region
652 dirty.orSelf(layer->visibleRegionScreen);
653 layer->contentDirty = false;
654 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700655 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700656 * the exposed region consists of two components:
657 * 1) what's VISIBLE now and was COVERED before
658 * 2) what's EXPOSED now less what was EXPOSED before
659 *
660 * note that (1) is conservative, we start with the whole
661 * visible region but only keep what used to be covered by
662 * something -- which mean it may have been exposed.
663 *
664 * (2) handles areas that were not covered by anything but got
665 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700666 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700667 const Region newExposed = visibleRegion - coveredRegion;
668 const Region oldVisibleRegion = layer->visibleRegionScreen;
669 const Region oldCoveredRegion = layer->coveredRegionScreen;
670 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
671 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673 dirty.subtractSelf(aboveOpaqueLayers);
674
675 // accumulate to the screen dirty region
676 dirtyRegion.orSelf(dirty);
677
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700678 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 // Store the visible region is screen space
682 layer->setVisibleRegion(visibleRegion);
683 layer->setCoveredRegion(coveredRegion);
684
Mathias Agopian12cedff2009-07-28 10:57:27 -0700685 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 if (layer->isSecure() && !visibleRegion.isEmpty()) {
687 secureFrameBuffer = true;
688 }
689 }
690
Mathias Agopian12cedff2009-07-28 10:57:27 -0700691 // invalidate the areas where a layer was removed
692 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
693 mDirtyRegionRemovedLayer.clear();
694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 mSecureFrameBuffer = secureFrameBuffer;
696 opaqueRegion = aboveOpaqueLayers;
697}
698
699
700void SurfaceFlinger::commitTransaction()
701{
702 mDrawingState = mCurrentState;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700703 mResizeTransationPending = false;
704 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705}
706
707void SurfaceFlinger::handlePageFlip()
708{
709 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700710 LayerVector& currentLayers(
711 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 visibleRegions |= lockPageFlip(currentLayers);
713
714 const DisplayHardware& hw = graphicPlane(0).displayHardware();
715 const Region screenRegion(hw.bounds());
716 if (visibleRegions) {
717 Region opaqueRegion;
718 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700719
720 /*
721 * rebuild the visible layer list
722 */
723 mVisibleLayersSortedByZ.clear();
724 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
725 size_t count = currentLayers.size();
726 mVisibleLayersSortedByZ.setCapacity(count);
727 for (size_t i=0 ; i<count ; i++) {
728 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
729 mVisibleLayersSortedByZ.add(currentLayers[i]);
730 }
731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 mWormholeRegion = screenRegion.subtract(opaqueRegion);
733 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800734 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736
737 unlockPageFlip(currentLayers);
738 mDirtyRegion.andSelf(screenRegion);
739}
740
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800741void SurfaceFlinger::invalidateHwcGeometry()
742{
743 mHwWorkListDirty = true;
744}
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
747{
748 bool recomputeVisibleRegions = false;
749 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700750 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700752 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 layer->lockPageFlip(recomputeVisibleRegions);
754 }
755 return recomputeVisibleRegions;
756}
757
758void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
759{
760 const GraphicPlane& plane(graphicPlane(0));
761 const Transform& planeTransform(plane.transform());
762 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700763 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700765 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 layer->unlockPageFlip(planeTransform, mDirtyRegion);
767 }
768}
769
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700770void SurfaceFlinger::handleWorkList()
771{
772 mHwWorkListDirty = false;
773 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
774 if (hwc.initCheck() == NO_ERROR) {
775 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
776 const size_t count = currentLayers.size();
777 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700778 hwc_layer_t* const cur(hwc.getLayers());
779 for (size_t i=0 ; cur && i<count ; i++) {
780 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian6a969242010-09-22 18:58:01 -0700781 if (mDebugDisableHWC) {
782 cur[i].compositionType = HWC_FRAMEBUFFER;
783 cur[i].flags |= HWC_SKIP_LAYER;
784 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700785 }
786 }
787}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789void SurfaceFlinger::handleRepaint()
790{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700791 // compute the invalid region
792 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793
794 if (UNLIKELY(mDebugRegion)) {
795 debugFlashRegions();
796 }
797
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700798 // set the frame buffer
799 const DisplayHardware& hw(graphicPlane(0).displayHardware());
800 glMatrixMode(GL_MODELVIEW);
801 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802
803 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700804 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
805 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700806 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700807 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
808 // takes a rectangle, we must make sure to update that whole
809 // rectangle in that case
810 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700811 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700812 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 mDirtyRegion.set(mInvalidRegion.bounds());
814 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700815 // in the BUFFER_PRESERVED case, obviously, we can update only
816 // what's needed and nothing more.
817 // NOTE: this is NOT a common case, as preserving the backbuffer
818 // is costly and usually involves copying the whole update back.
819 }
820 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700821 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700822 // We need to redraw the rectangle that will be updated
823 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700824 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700825 // rectangle instead of a region (see DisplayHardware::flip())
826 mDirtyRegion.set(mInvalidRegion.bounds());
827 } else {
828 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 mDirtyRegion.set(hw.bounds());
830 mInvalidRegion = mDirtyRegion;
831 }
832 }
833
834 // compose all surfaces
835 composeSurfaces(mDirtyRegion);
836
837 // clear the dirty regions
838 mDirtyRegion.clear();
839}
840
841void SurfaceFlinger::composeSurfaces(const Region& dirty)
842{
843 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
844 // should never happen unless the window manager has a bug
845 // draw something...
846 drawWormhole();
847 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700848
849 status_t err = NO_ERROR;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700850 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700851 size_t count = layers.size();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700852
853 const DisplayHardware& hw(graphicPlane(0).displayHardware());
854 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700855 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700856
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700857 LOGE_IF(cur && hwc.getNumLayers() != count,
858 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
859 hwc.getNumLayers(), count);
860
861 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700862 if (hwc.initCheck() == NO_ERROR) {
863 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
864 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700865
866 /*
867 * update the per-frame h/w composer data for each layer
868 * and build the transparent region of the FB
869 */
870 Region transparent;
871 if (cur) {
872 for (size_t i=0 ; i<count ; i++) {
873 const sp<LayerBase>& layer(layers[i]);
874 layer->setPerFrameData(&cur[i]);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700875 }
876 err = hwc.prepare();
877 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700878
Mathias Agopian45491692011-01-19 15:24:23 -0800879 if (err == NO_ERROR) {
880 for (size_t i=0 ; i<count ; i++) {
881 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
882 const sp<LayerBase>& layer(layers[i]);
883 if (!(layer->needsBlending())) {
884 transparent.orSelf(layer->visibleRegionScreen);
885 }
886 }
887 }
888
889 /*
890 * clear the area of the FB that need to be transparent
891 */
892 transparent.andSelf(dirty);
893 if (!transparent.isEmpty()) {
894 glClearColor(0,0,0,0);
895 Region::const_iterator it = transparent.begin();
896 Region::const_iterator const end = transparent.end();
897 const int32_t height = hw.getHeight();
898 while (it != end) {
899 const Rect& r(*it++);
900 const GLint sy = height - (r.top + r.height());
901 glScissor(r.left, sy, r.width(), r.height());
902 glClear(GL_COLOR_BUFFER_BIT);
903 }
904 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700905 }
906 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700907
908
909 /*
910 * and then, render the layers targeted at the framebuffer
911 */
912 for (size_t i=0 ; i<count ; i++) {
913 if (cur) {
Antti Hatala982f58b2010-09-09 02:32:30 -0700914 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
915 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700916 // skip layers handled by the HAL
917 continue;
918 }
919 }
Mathias Agopian6a969242010-09-22 18:58:01 -0700920
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700921 const sp<LayerBase>& layer(layers[i]);
922 const Region clip(dirty.intersect(layer->visibleRegionScreen));
923 if (!clip.isEmpty()) {
924 layer->draw(clip);
925 }
926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927}
928
929void SurfaceFlinger::unlockClients()
930{
931 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
932 const size_t count = drawingLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700933 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700935 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 layer->finishPageFlip();
937 }
938}
939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940void SurfaceFlinger::debugFlashRegions()
941{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700942 const DisplayHardware& hw(graphicPlane(0).displayHardware());
943 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700944
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700945 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
946 (flags & DisplayHardware::BUFFER_PRESERVED))) {
947 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
948 mDirtyRegion.bounds() : hw.bounds());
949 composeSurfaces(repaint);
950 }
951
952 TextureManager::deactivateTextures();
953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 glDisable(GL_BLEND);
955 glDisable(GL_DITHER);
956 glDisable(GL_SCISSOR_TEST);
957
Mathias Agopiandff8e582009-05-04 14:17:04 -0700958 static int toggle = 0;
959 toggle = 1 - toggle;
960 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700961 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700962 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700963 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700966 Region::const_iterator it = mDirtyRegion.begin();
967 Region::const_iterator const end = mDirtyRegion.end();
968 while (it != end) {
969 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 GLfloat vertices[][2] = {
971 { r.left, r.top },
972 { r.left, r.bottom },
973 { r.right, r.bottom },
974 { r.right, r.top }
975 };
976 glVertexPointer(2, GL_FLOAT, 0, vertices);
977 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
978 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700979
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700980 if (mInvalidRegion.isEmpty()) {
981 mDirtyRegion.dump("mDirtyRegion");
982 mInvalidRegion.dump("mInvalidRegion");
983 }
984 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985
986 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700987 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988
989 glEnable(GL_SCISSOR_TEST);
990 //mDirtyRegion.dump("mDirtyRegion");
991}
992
993void SurfaceFlinger::drawWormhole() const
994{
995 const Region region(mWormholeRegion.intersect(mDirtyRegion));
996 if (region.isEmpty())
997 return;
998
999 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1000 const int32_t width = hw.getWidth();
1001 const int32_t height = hw.getHeight();
1002
1003 glDisable(GL_BLEND);
1004 glDisable(GL_DITHER);
1005
1006 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001007 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001008 Region::const_iterator it = region.begin();
1009 Region::const_iterator const end = region.end();
1010 while (it != end) {
1011 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 const GLint sy = height - (r.top + r.height());
1013 glScissor(r.left, sy, r.width(), r.height());
1014 glClear(GL_COLOR_BUFFER_BIT);
1015 }
1016 } else {
1017 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1018 { width, height }, { 0, height } };
1019 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1020 glVertexPointer(2, GL_SHORT, 0, vertices);
1021 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1022 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001023#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001024 if (GLExtensions::getInstance().haveTextureExternal()) {
1025 glDisable(GL_TEXTURE_EXTERNAL_OES);
1026 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001027#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 glEnable(GL_TEXTURE_2D);
1029 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1030 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1031 glMatrixMode(GL_TEXTURE);
1032 glLoadIdentity();
1033 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001034 Region::const_iterator it = region.begin();
1035 Region::const_iterator const end = region.end();
1036 while (it != end) {
1037 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 const GLint sy = height - (r.top + r.height());
1039 glScissor(r.left, sy, r.width(), r.height());
1040 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1041 }
1042 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001043 glLoadIdentity();
1044 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046}
1047
1048void SurfaceFlinger::debugShowFPS() const
1049{
1050 static int mFrameCount;
1051 static int mLastFrameCount = 0;
1052 static nsecs_t mLastFpsTime = 0;
1053 static float mFps = 0;
1054 mFrameCount++;
1055 nsecs_t now = systemTime();
1056 nsecs_t diff = now - mLastFpsTime;
1057 if (diff > ms2ns(250)) {
1058 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1059 mLastFpsTime = now;
1060 mLastFrameCount = mFrameCount;
1061 }
1062 // XXX: mFPS has the value we want
1063 }
1064
Mathias Agopian1473f462009-04-10 14:24:30 -07001065status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066{
1067 Mutex::Autolock _l(mStateLock);
1068 addLayer_l(layer);
1069 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1070 return NO_ERROR;
1071}
1072
Mathias Agopian593c05c2010-06-02 23:28:45 -07001073status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1074{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001075 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001076 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1077}
1078
1079ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1080 const sp<LayerBaseClient>& lbc)
1081{
1082 Mutex::Autolock _l(mStateLock);
1083
1084 // attach this layer to the client
1085 ssize_t name = client->attachLayer(lbc);
1086
1087 // add this layer to the current state list
1088 addLayer_l(lbc);
1089
1090 return name;
1091}
1092
Mathias Agopian1473f462009-04-10 14:24:30 -07001093status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094{
1095 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001096 status_t err = purgatorizeLayer_l(layer);
1097 if (err == NO_ERROR)
1098 setTransactionFlags(eTransactionNeeded);
1099 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100}
1101
Mathias Agopian1473f462009-04-10 14:24:30 -07001102status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103{
Mathias Agopian7623da42010-06-01 15:12:58 -07001104 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1105 if (lbc != 0) {
1106 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1109 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001110 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 return NO_ERROR;
1112 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001113 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114}
1115
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001116status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1117{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001118 // First add the layer to the purgatory list, which makes sure it won't
1119 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001120 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001121 if (err >= 0) {
1122 mLayerPurgatory.add(layerBase);
1123 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001124
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001125 layerBase->onRemoved();
1126
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001127 // it's possible that we don't find a layer, because it might
1128 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001129 // from the user because there is a race between Client::destroySurface(),
1130 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001131 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1132}
1133
Mathias Agopian593c05c2010-06-02 23:28:45 -07001134status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001136 layer->forceVisibilityTransaction();
1137 setTransactionFlags(eTraversalNeeded);
1138 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139}
1140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1142{
1143 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1144}
1145
Mathias Agopian898c4c92010-05-18 17:06:55 -07001146uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147{
1148 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1149 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001150 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 return old;
1153}
1154
1155void SurfaceFlinger::openGlobalTransaction()
1156{
1157 android_atomic_inc(&mTransactionCount);
1158}
1159
1160void SurfaceFlinger::closeGlobalTransaction()
1161{
1162 if (android_atomic_dec(&mTransactionCount) == 1) {
1163 signalEvent();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001164
Andreas Hubere3c01832010-08-16 08:49:37 -07001165 // if there is a transaction with a resize, wait for it to
Mathias Agopian9779b2212009-09-07 16:32:45 -07001166 // take effect before returning.
1167 Mutex::Autolock _l(mStateLock);
1168 while (mResizeTransationPending) {
Mathias Agopian98a9c562009-09-30 14:42:13 -07001169 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1170 if (CC_UNLIKELY(err != NO_ERROR)) {
1171 // just in case something goes wrong in SF, return to the
1172 // called after a few seconds.
1173 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1174 mResizeTransationPending = false;
1175 break;
1176 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179}
1180
1181status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1182{
1183 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1184 return BAD_VALUE;
1185
1186 Mutex::Autolock _l(mStateLock);
1187 mCurrentState.freezeDisplay = 1;
1188 setTransactionFlags(eTransactionNeeded);
1189
1190 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001191 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 return NO_ERROR;
1193}
1194
1195status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1196{
1197 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1198 return BAD_VALUE;
1199
1200 Mutex::Autolock _l(mStateLock);
1201 mCurrentState.freezeDisplay = 0;
1202 setTransactionFlags(eTransactionNeeded);
1203
1204 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001205 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 return NO_ERROR;
1207}
1208
Andreas Hubere3c01832010-08-16 08:49:37 -07001209int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001210 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211{
1212 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1213 return BAD_VALUE;
1214
1215 Mutex::Autolock _l(mStateLock);
1216 if (mCurrentState.orientation != orientation) {
1217 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001218 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 mCurrentState.orientation = orientation;
1220 setTransactionFlags(eTransactionNeeded);
1221 mTransactionCV.wait(mStateLock);
1222 } else {
1223 orientation = BAD_VALUE;
1224 }
1225 }
1226 return orientation;
1227}
1228
Mathias Agopian593c05c2010-06-02 23:28:45 -07001229sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian770492c2010-05-28 14:22:23 -07001230 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1232 uint32_t flags)
1233{
Mathias Agopian1473f462009-04-10 14:24:30 -07001234 sp<LayerBaseClient> layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001236
1237 if (int32_t(w|h) < 0) {
1238 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1239 int(w), int(h));
1240 return surfaceHandle;
1241 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001244 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 switch (flags & eFXSurfaceMask) {
1246 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001247 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1248 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 break;
1250 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001251 // for now we treat Blur as Dim, until we can implement it
1252 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001254 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 break;
1256 }
1257
Mathias Agopian1473f462009-04-10 14:24:30 -07001258 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001259 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001260 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001261 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001264 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001265 params->token = token;
Mathias Agopian18b6b492009-08-19 17:46:26 -07001266 params->identity = surfaceHandle->getIdentity();
1267 params->width = w;
1268 params->height = h;
1269 params->format = format;
Mathias Agopian7623da42010-06-01 15:12:58 -07001270 if (normalLayer != 0) {
1271 Mutex::Autolock _l(mStateLock);
1272 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1273 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001274 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001275
Mathias Agopian593c05c2010-06-02 23:28:45 -07001276 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278
1279 return surfaceHandle;
1280}
1281
Mathias Agopian7623da42010-06-01 15:12:58 -07001282sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001283 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001284 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001285 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286{
1287 // initialize the surfaces
1288 switch (format) { // TODO: take h/w into account
1289 case PIXEL_FORMAT_TRANSPARENT:
1290 case PIXEL_FORMAT_TRANSLUCENT:
1291 format = PIXEL_FORMAT_RGBA_8888;
1292 break;
1293 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001294#ifdef NO_RGBX_8888
1295 format = PIXEL_FORMAT_RGB_565;
1296#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001297 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001298#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 break;
1300 }
1301
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001302#ifdef NO_RGBX_8888
1303 if (format == PIXEL_FORMAT_RGBX_8888)
1304 format = PIXEL_FORMAT_RGBA_8888;
1305#endif
1306
Mathias Agopian593c05c2010-06-02 23:28:45 -07001307 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001308 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001309 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001311 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313 return layer;
1314}
1315
Mathias Agopian7623da42010-06-01 15:12:58 -07001316sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001317 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001318 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001320 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 return layer;
1323}
1324
Mathias Agopian593c05c2010-06-02 23:28:45 -07001325status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001327 /*
1328 * called by the window manager, when a surface should be marked for
1329 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001330 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001331 * The surface is removed from the current and drawing lists, but placed
1332 * in the purgatory queue, so it's not destroyed right-away (we need
1333 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001334 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001335
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001336 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001337 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001338 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001339 if (layer != 0) {
1340 err = purgatorizeLayer_l(layer);
1341 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001342 setTransactionFlags(eTransactionNeeded);
1343 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001344 }
1345 return err;
1346}
1347
1348status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1349{
Mathias Agopian359140c2009-07-02 17:33:40 -07001350 // called by ~ISurface() when all references are gone
Andreas Hubere3c01832010-08-16 08:49:37 -07001351
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001352 class MessageDestroySurface : public MessageBase {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001353 SurfaceFlinger* flinger;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001354 sp<LayerBaseClient> layer;
1355 public:
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001356 MessageDestroySurface(
1357 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1358 : flinger(flinger), layer(layer) { }
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001359 virtual bool handler() {
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001360 sp<LayerBaseClient> l(layer);
1361 layer.clear(); // clear it outside of the lock;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001362 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian359140c2009-07-02 17:33:40 -07001363 /*
Andreas Hubere3c01832010-08-16 08:49:37 -07001364 * remove the layer from the current list -- chances are that it's
1365 * not in the list anyway, because it should have been removed
1366 * already upon request of the client (eg: window manager).
Mathias Agopian359140c2009-07-02 17:33:40 -07001367 * However, a buggy client could have not done that.
1368 * Since we know we don't have any more clients, we don't need
1369 * to use the purgatory.
1370 */
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001371 status_t err = flinger->removeLayer_l(l);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001372 if (err == NAME_NOT_FOUND) {
1373 // The surface wasn't in the current list, which means it was
1374 // removed already, which means it is in the purgatory,
1375 // and need to be removed from there.
1376 // This needs to happen from the main thread since its dtor
1377 // must run from there (b/c of OpenGL ES). Additionally, we
1378 // can't really acquire our internal lock from
1379 // destroySurface() -- see postMessage() below.
1380 ssize_t idx = flinger->mLayerPurgatory.remove(l);
1381 LOGE_IF(idx < 0,
1382 "layer=%p is not in the purgatory list", l.get());
1383 }
1384
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001385 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1386 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001387 return true;
1388 }
1389 };
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001390
Mathias Agopian898c4c92010-05-18 17:06:55 -07001391 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 return NO_ERROR;
1393}
1394
1395status_t SurfaceFlinger::setClientState(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001396 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 int32_t count,
1398 const layer_state_t* states)
1399{
1400 Mutex::Autolock _l(mStateLock);
1401 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 for (int i=0 ; i<count ; i++) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001403 const layer_state_t& s(states[i]);
1404 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian1473f462009-04-10 14:24:30 -07001405 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 const uint32_t what = s.what;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 if (what & ePositionChanged) {
1408 if (layer->setPosition(s.x, s.y))
1409 flags |= eTraversalNeeded;
1410 }
1411 if (what & eLayerChanged) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001412 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 if (layer->setLayer(s.z)) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001414 mCurrentState.layersSortedByZ.removeAt(idx);
1415 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 // we need traversal (state changed)
1417 // AND transaction (list changed)
1418 flags |= eTransactionNeeded|eTraversalNeeded;
1419 }
1420 }
1421 if (what & eSizeChanged) {
Mathias Agopian9779b2212009-09-07 16:32:45 -07001422 if (layer->setSize(s.w, s.h)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 flags |= eTraversalNeeded;
Mathias Agopian9779b2212009-09-07 16:32:45 -07001424 mResizeTransationPending = true;
1425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427 if (what & eAlphaChanged) {
1428 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1429 flags |= eTraversalNeeded;
1430 }
1431 if (what & eMatrixChanged) {
1432 if (layer->setMatrix(s.matrix))
1433 flags |= eTraversalNeeded;
1434 }
1435 if (what & eTransparentRegionChanged) {
1436 if (layer->setTransparentRegionHint(s.transparentRegion))
1437 flags |= eTraversalNeeded;
1438 }
1439 if (what & eVisibilityChanged) {
1440 if (layer->setFlags(s.flags, s.mask))
1441 flags |= eTraversalNeeded;
1442 }
1443 }
1444 }
1445 if (flags) {
1446 setTransactionFlags(flags);
1447 }
1448 return NO_ERROR;
1449}
1450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451void SurfaceFlinger::screenReleased(int dpy)
1452{
1453 // this may be called by a signal handler, we can't do too much in here
1454 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1455 signalEvent();
1456}
1457
1458void SurfaceFlinger::screenAcquired(int dpy)
1459{
1460 // this may be called by a signal handler, we can't do too much in here
1461 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1462 signalEvent();
1463}
1464
1465status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1466{
Erik Gilling94720d72010-12-01 16:38:01 -08001467 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 char buffer[SIZE];
1469 String8 result;
Mathias Agopian151e8592009-06-15 18:24:59 -07001470 if (!mDump.checkCalling()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 snprintf(buffer, SIZE, "Permission Denial: "
1472 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1473 IPCThreadState::self()->getCallingPid(),
1474 IPCThreadState::self()->getCallingUid());
1475 result.append(buffer);
1476 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001477
1478 // figure out if we're stuck somewhere
1479 const nsecs_t now = systemTime();
1480 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1481 const nsecs_t inTransaction(mDebugInTransaction);
1482 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1483 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1484
1485 // Try to get the main lock, but don't insist if we can't
1486 // (this would indicate SF is stuck, but we want to be able to
1487 // print something in dumpsys).
1488 int retry = 3;
1489 while (mStateLock.tryLock()<0 && --retry>=0) {
1490 usleep(1000000);
1491 }
1492 const bool locked(retry >= 0);
1493 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001494 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001495 "SurfaceFlinger appears to be unresponsive, "
1496 "dumping anyways (no locks held)\n");
1497 result.append(buffer);
1498 }
1499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1501 const size_t count = currentLayers.size();
1502 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001503 const sp<LayerBase>& layer(currentLayers[i]);
1504 layer->dump(result, buffer, SIZE);
1505 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 s.transparentRegion.dump(result, "transparentRegion");
1507 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1508 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1509 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 mWormholeRegion.dump(result, "WormholeRegion");
1512 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1513 snprintf(buffer, SIZE,
1514 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1515 mFreezeDisplay?"yes":"no", mFreezeCount,
1516 mCurrentState.orientation, hw.canDraw());
1517 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001518 snprintf(buffer, SIZE,
1519 " last eglSwapBuffers() time: %f us\n"
1520 " last transaction time : %f us\n",
1521 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1522 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001523
Mathias Agopiana8d49172009-08-26 16:36:26 -07001524 if (inSwapBuffersDuration || !locked) {
1525 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1526 inSwapBuffersDuration/1000.0);
1527 result.append(buffer);
1528 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001529
Mathias Agopiana8d49172009-08-26 16:36:26 -07001530 if (inTransactionDuration || !locked) {
1531 snprintf(buffer, SIZE, " transaction time: %f us\n",
1532 inTransactionDuration/1000.0);
1533 result.append(buffer);
1534 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001535
Mathias Agopian6a969242010-09-22 18:58:01 -07001536 HWComposer& hwc(hw.getHwComposer());
1537 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1538 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1539 mDebugDisableHWC ? "disabled" : "enabled");
1540 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -07001541 hwc.dump(result, buffer, SIZE);
Mathias Agopian6a969242010-09-22 18:58:01 -07001542
Mathias Agopian6950e422009-10-05 17:07:12 -07001543 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001544 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001545 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001546
1547 if (locked) {
1548 mStateLock.unlock();
1549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
1551 write(fd, result.string(), result.size());
1552 return NO_ERROR;
1553}
1554
1555status_t SurfaceFlinger::onTransact(
1556 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1557{
1558 switch (code) {
1559 case CREATE_CONNECTION:
1560 case OPEN_GLOBAL_TRANSACTION:
1561 case CLOSE_GLOBAL_TRANSACTION:
1562 case SET_ORIENTATION:
1563 case FREEZE_DISPLAY:
1564 case UNFREEZE_DISPLAY:
1565 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001566 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001567 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 {
1569 // codes that require permission check
1570 IPCThreadState* ipc = IPCThreadState::self();
1571 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001572 const int uid = ipc->getCallingUid();
Mathias Agopian151e8592009-06-15 18:24:59 -07001573 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1574 LOGE("Permission Denial: "
1575 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1576 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001578 break;
1579 }
1580 case CAPTURE_SCREEN:
1581 {
1582 // codes that require permission check
1583 IPCThreadState* ipc = IPCThreadState::self();
1584 const int pid = ipc->getCallingPid();
1585 const int uid = ipc->getCallingUid();
1586 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1587 LOGE("Permission Denial: "
1588 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1589 return PERMISSION_DENIED;
1590 }
1591 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
1593 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1596 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001597 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian151e8592009-06-15 18:24:59 -07001598 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1599 IPCThreadState* ipc = IPCThreadState::self();
1600 const int pid = ipc->getCallingPid();
1601 const int uid = ipc->getCallingUid();
1602 LOGE("Permission Denial: "
1603 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 return PERMISSION_DENIED;
1605 }
1606 int n;
1607 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001608 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001609 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 return NO_ERROR;
1611 case 1002: // SHOW_UPDATES
1612 n = data.readInt32();
1613 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1614 return NO_ERROR;
1615 case 1003: // SHOW_BACKGROUND
1616 n = data.readInt32();
1617 mDebugBackground = n ? 1 : 0;
1618 return NO_ERROR;
Mathias Agopian6a969242010-09-22 18:58:01 -07001619 case 1008: // toggle use of hw composer
1620 n = data.readInt32();
1621 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -08001622 invalidateHwcGeometry();
Mathias Agopian6a969242010-09-22 18:58:01 -07001623 // fall-through...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 case 1004:{ // repaint everything
1625 Mutex::Autolock _l(mStateLock);
1626 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1627 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1628 signalEvent();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001629 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001631 case 1005:{ // force transaction
1632 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1633 return NO_ERROR;
1634 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001635 case 1006:{ // enable/disable GraphicLog
1636 int enabled = data.readInt32();
1637 GraphicLog::getInstance().setEnabled(enabled);
1638 return NO_ERROR;
1639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 case 1007: // set mFreezeCount
1641 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001642 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 return NO_ERROR;
1644 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001645 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 reply->writeInt32(0);
1647 reply->writeInt32(mDebugRegion);
1648 reply->writeInt32(mDebugBackground);
1649 return NO_ERROR;
1650 case 1013: {
1651 Mutex::Autolock _l(mStateLock);
1652 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1653 reply->writeInt32(hw.getPageFlipCount());
1654 }
1655 return NO_ERROR;
1656 }
1657 }
1658 return err;
1659}
1660
Mathias Agopianaab758e2010-10-11 12:37:43 -07001661// ---------------------------------------------------------------------------
1662
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001663status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1664 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001665{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001666 if (!GLExtensions::getInstance().haveFramebufferObject())
1667 return INVALID_OPERATION;
1668
1669 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001670 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001671 const uint32_t hw_w = hw.getWidth();
1672 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001673 GLfloat u = 1;
1674 GLfloat v = 1;
1675
1676 // make sure to clear all GL error flags
1677 while ( glGetError() != GL_NO_ERROR ) ;
1678
1679 // create a FBO
1680 GLuint name, tname;
1681 glGenTextures(1, &tname);
1682 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001683 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1684 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001685 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001686 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001687 GLint tw = (2 << (31 - clz(hw_w)));
1688 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001689 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1690 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001691 u = GLfloat(hw_w) / tw;
1692 v = GLfloat(hw_h) / th;
1693 }
1694 glGenFramebuffersOES(1, &name);
1695 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001696 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1697 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001698
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001699 // redraw the screen entirely...
1700 glClearColor(0,0,0,1);
1701 glClear(GL_COLOR_BUFFER_BIT);
1702 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1703 const size_t count = layers.size();
1704 for (size_t i=0 ; i<count ; ++i) {
1705 const sp<LayerBase>& layer(layers[i]);
1706 layer->drawForSreenShot();
1707 }
1708
1709 // back to main framebuffer
1710 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1711 glDisable(GL_SCISSOR_TEST);
1712 glDeleteFramebuffersOES(1, &name);
1713
1714 *textureName = tname;
1715 *uOut = u;
1716 *vOut = v;
1717 return NO_ERROR;
1718}
1719
1720// ---------------------------------------------------------------------------
1721
1722status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1723{
1724 status_t result = PERMISSION_DENIED;
1725
1726 if (!GLExtensions::getInstance().haveFramebufferObject())
1727 return INVALID_OPERATION;
1728
1729 // get screen geometry
1730 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1731 const uint32_t hw_w = hw.getWidth();
1732 const uint32_t hw_h = hw.getHeight();
1733 const Region screenBounds(hw.bounds());
1734
1735 GLfloat u, v;
1736 GLuint tname;
1737 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1738 if (result != NO_ERROR) {
1739 return result;
1740 }
1741
1742 GLfloat vtx[8];
1743 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1744 glEnable(GL_TEXTURE_2D);
1745 glBindTexture(GL_TEXTURE_2D, tname);
1746 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1747 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1748 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1749 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1750 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1751 glVertexPointer(2, GL_FLOAT, 0, vtx);
1752
1753 class s_curve_interpolator {
1754 const float nbFrames, s, v;
1755 public:
1756 s_curve_interpolator(int nbFrames, float s)
1757 : nbFrames(1.0f / (nbFrames-1)), s(s),
1758 v(1.0f + expf(-s + 0.5f*s)) {
1759 }
1760 float operator()(int f) {
1761 const float x = f * nbFrames;
1762 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1763 }
1764 };
1765
1766 class v_stretch {
1767 const GLfloat hw_w, hw_h;
1768 public:
1769 v_stretch(uint32_t hw_w, uint32_t hw_h)
1770 : hw_w(hw_w), hw_h(hw_h) {
1771 }
1772 void operator()(GLfloat* vtx, float v) {
1773 const GLfloat w = hw_w + (hw_w * v);
1774 const GLfloat h = hw_h - (hw_h * v);
1775 const GLfloat x = (hw_w - w) * 0.5f;
1776 const GLfloat y = (hw_h - h) * 0.5f;
1777 vtx[0] = x; vtx[1] = y;
1778 vtx[2] = x; vtx[3] = y + h;
1779 vtx[4] = x + w; vtx[5] = y + h;
1780 vtx[6] = x + w; vtx[7] = y;
1781 }
1782 };
1783
1784 class h_stretch {
1785 const GLfloat hw_w, hw_h;
1786 public:
1787 h_stretch(uint32_t hw_w, uint32_t hw_h)
1788 : hw_w(hw_w), hw_h(hw_h) {
1789 }
1790 void operator()(GLfloat* vtx, float v) {
1791 const GLfloat w = hw_w - (hw_w * v);
1792 const GLfloat h = 1.0f;
1793 const GLfloat x = (hw_w - w) * 0.5f;
1794 const GLfloat y = (hw_h - h) * 0.5f;
1795 vtx[0] = x; vtx[1] = y;
1796 vtx[2] = x; vtx[3] = y + h;
1797 vtx[4] = x + w; vtx[5] = y + h;
1798 vtx[6] = x + w; vtx[7] = y;
1799 }
1800 };
1801
1802 // the full animation is 24 frames
1803 const int nbFrames = 12;
1804 s_curve_interpolator itr(nbFrames, 7.5f);
1805 s_curve_interpolator itg(nbFrames, 8.0f);
1806 s_curve_interpolator itb(nbFrames, 8.5f);
1807
1808 v_stretch vverts(hw_w, hw_h);
1809 glEnable(GL_BLEND);
1810 glBlendFunc(GL_ONE, GL_ONE);
1811 for (int i=0 ; i<nbFrames ; i++) {
1812 float x, y, w, h;
1813 const float vr = itr(i);
1814 const float vg = itg(i);
1815 const float vb = itb(i);
1816
1817 // clear screen
1818 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001819 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001820 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001821
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001822 // draw the red plane
1823 vverts(vtx, vr);
1824 glColorMask(1,0,0,1);
1825 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001826
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001827 // draw the green plane
1828 vverts(vtx, vg);
1829 glColorMask(0,1,0,1);
1830 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001831
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001832 // draw the blue plane
1833 vverts(vtx, vb);
1834 glColorMask(0,0,1,1);
1835 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001836
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001837 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001838 glDisable(GL_TEXTURE_2D);
1839 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001840 glColor4f(vg, vg, vg, 1);
1841 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1842 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001843 }
1844
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001845 h_stretch hverts(hw_w, hw_h);
1846 glDisable(GL_BLEND);
1847 glDisable(GL_TEXTURE_2D);
1848 glColorMask(1,1,1,1);
1849 for (int i=0 ; i<nbFrames ; i++) {
1850 const float v = itg(i);
1851 hverts(vtx, v);
1852 glClear(GL_COLOR_BUFFER_BIT);
1853 glColor4f(1-v, 1-v, 1-v, 1);
1854 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1855 hw.flip(screenBounds);
1856 }
1857
1858 glColorMask(1,1,1,1);
1859 glEnable(GL_SCISSOR_TEST);
1860 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1861 glDeleteTextures(1, &tname);
1862 return NO_ERROR;
1863}
1864
1865status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1866{
1867 status_t result = PERMISSION_DENIED;
1868
1869 if (!GLExtensions::getInstance().haveFramebufferObject())
1870 return INVALID_OPERATION;
1871
1872
1873 // get screen geometry
1874 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1875 const uint32_t hw_w = hw.getWidth();
1876 const uint32_t hw_h = hw.getHeight();
1877 const Region screenBounds(hw.bounds());
1878
1879 GLfloat u, v;
1880 GLuint tname;
1881 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1882 if (result != NO_ERROR) {
1883 return result;
1884 }
1885
1886 // back to main framebuffer
1887 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1888 glDisable(GL_SCISSOR_TEST);
1889
1890 GLfloat vtx[8];
1891 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1892 glEnable(GL_TEXTURE_2D);
1893 glBindTexture(GL_TEXTURE_2D, tname);
1894 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1895 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1896 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1897 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1898 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1899 glVertexPointer(2, GL_FLOAT, 0, vtx);
1900
1901 class s_curve_interpolator {
1902 const float nbFrames, s, v;
1903 public:
1904 s_curve_interpolator(int nbFrames, float s)
1905 : nbFrames(1.0f / (nbFrames-1)), s(s),
1906 v(1.0f + expf(-s + 0.5f*s)) {
1907 }
1908 float operator()(int f) {
1909 const float x = f * nbFrames;
1910 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1911 }
1912 };
1913
1914 class v_stretch {
1915 const GLfloat hw_w, hw_h;
1916 public:
1917 v_stretch(uint32_t hw_w, uint32_t hw_h)
1918 : hw_w(hw_w), hw_h(hw_h) {
1919 }
1920 void operator()(GLfloat* vtx, float v) {
1921 const GLfloat w = hw_w + (hw_w * v);
1922 const GLfloat h = hw_h - (hw_h * v);
1923 const GLfloat x = (hw_w - w) * 0.5f;
1924 const GLfloat y = (hw_h - h) * 0.5f;
1925 vtx[0] = x; vtx[1] = y;
1926 vtx[2] = x; vtx[3] = y + h;
1927 vtx[4] = x + w; vtx[5] = y + h;
1928 vtx[6] = x + w; vtx[7] = y;
1929 }
1930 };
1931
1932 class h_stretch {
1933 const GLfloat hw_w, hw_h;
1934 public:
1935 h_stretch(uint32_t hw_w, uint32_t hw_h)
1936 : hw_w(hw_w), hw_h(hw_h) {
1937 }
1938 void operator()(GLfloat* vtx, float v) {
1939 const GLfloat w = hw_w - (hw_w * v);
1940 const GLfloat h = 1.0f;
1941 const GLfloat x = (hw_w - w) * 0.5f;
1942 const GLfloat y = (hw_h - h) * 0.5f;
1943 vtx[0] = x; vtx[1] = y;
1944 vtx[2] = x; vtx[3] = y + h;
1945 vtx[4] = x + w; vtx[5] = y + h;
1946 vtx[6] = x + w; vtx[7] = y;
1947 }
1948 };
1949
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07001950 // the full animation is 12 frames
1951 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001952 s_curve_interpolator itr(nbFrames, 7.5f);
1953 s_curve_interpolator itg(nbFrames, 8.0f);
1954 s_curve_interpolator itb(nbFrames, 8.5f);
1955
1956 h_stretch hverts(hw_w, hw_h);
1957 glDisable(GL_BLEND);
1958 glDisable(GL_TEXTURE_2D);
1959 glColorMask(1,1,1,1);
1960 for (int i=nbFrames-1 ; i>=0 ; i--) {
1961 const float v = itg(i);
1962 hverts(vtx, v);
1963 glClear(GL_COLOR_BUFFER_BIT);
1964 glColor4f(1-v, 1-v, 1-v, 1);
1965 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1966 hw.flip(screenBounds);
1967 }
1968
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07001969 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001970 v_stretch vverts(hw_w, hw_h);
1971 glEnable(GL_BLEND);
1972 glBlendFunc(GL_ONE, GL_ONE);
1973 for (int i=nbFrames-1 ; i>=0 ; i--) {
1974 float x, y, w, h;
1975 const float vr = itr(i);
1976 const float vg = itg(i);
1977 const float vb = itb(i);
1978
1979 // clear screen
1980 glColorMask(1,1,1,1);
1981 glClear(GL_COLOR_BUFFER_BIT);
1982 glEnable(GL_TEXTURE_2D);
1983
1984 // draw the red plane
1985 vverts(vtx, vr);
1986 glColorMask(1,0,0,1);
1987 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1988
1989 // draw the green plane
1990 vverts(vtx, vg);
1991 glColorMask(0,1,0,1);
1992 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1993
1994 // draw the blue plane
1995 vverts(vtx, vb);
1996 glColorMask(0,0,1,1);
1997 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1998
1999 hw.flip(screenBounds);
2000 }
2001
2002 glColorMask(1,1,1,1);
2003 glEnable(GL_SCISSOR_TEST);
2004 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002005 glDeleteTextures(1, &tname);
2006
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002007 return NO_ERROR;
2008}
2009
2010// ---------------------------------------------------------------------------
2011
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002012status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002013{
2014 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2015 if (!hw.canDraw()) {
2016 // we're already off
2017 return NO_ERROR;
2018 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002019 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2020 electronBeamOffAnimationImplLocked();
2021 }
2022
2023 // always clear the whole screen at the end of the animation
2024 glClearColor(0,0,0,1);
2025 glDisable(GL_SCISSOR_TEST);
2026 glClear(GL_COLOR_BUFFER_BIT);
2027 glEnable(GL_SCISSOR_TEST);
2028 hw.flip( Region(hw.bounds()) );
2029
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002030 hw.setCanDraw(false);
2031 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002032}
2033
2034status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2035{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002036 class MessageTurnElectronBeamOff : public MessageBase {
2037 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002038 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002039 status_t result;
2040 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002041 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2042 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002043 }
2044 status_t getResult() const {
2045 return result;
2046 }
2047 virtual bool handler() {
2048 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002049 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002050 return true;
2051 }
2052 };
2053
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002054 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002055 status_t res = postMessageSync(msg);
2056 if (res == NO_ERROR) {
2057 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002058
2059 // work-around: when the power-manager calls us we activate the
2060 // animation. eventually, the "on" animation will be called
2061 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002062 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002063 }
2064 return res;
2065}
2066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002068
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002069status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002070{
2071 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2072 if (hw.canDraw()) {
2073 // we're already on
2074 return NO_ERROR;
2075 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002076 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2077 electronBeamOnAnimationImplLocked();
2078 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002079 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002080
2081 // make sure to redraw the whole screen when the animation is done
2082 mDirtyRegion.set(hw.bounds());
2083 signalEvent();
2084
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002085 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002086}
2087
2088status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2089{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002090 class MessageTurnElectronBeamOn : public MessageBase {
2091 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002092 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002093 status_t result;
2094 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002095 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2096 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002097 }
2098 status_t getResult() const {
2099 return result;
2100 }
2101 virtual bool handler() {
2102 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002103 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002104 return true;
2105 }
2106 };
2107
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002108 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002109 return NO_ERROR;
2110}
2111
2112// ---------------------------------------------------------------------------
2113
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002114status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2115 sp<IMemoryHeap>* heap,
2116 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002117 uint32_t sw, uint32_t sh,
2118 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002119{
2120 status_t result = PERMISSION_DENIED;
2121
2122 // only one display supported for now
2123 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2124 return BAD_VALUE;
2125
2126 if (!GLExtensions::getInstance().haveFramebufferObject())
2127 return INVALID_OPERATION;
2128
2129 // get screen geometry
2130 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2131 const uint32_t hw_w = hw.getWidth();
2132 const uint32_t hw_h = hw.getHeight();
2133
2134 if ((sw > hw_w) || (sh > hw_h))
2135 return BAD_VALUE;
2136
2137 sw = (!sw) ? hw_w : sw;
2138 sh = (!sh) ? hw_h : sh;
2139 const size_t size = sw * sh * 4;
2140
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002141 LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2142 sw, sh, minLayerZ, maxLayerZ);
2143
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002144 // make sure to clear all GL error flags
2145 while ( glGetError() != GL_NO_ERROR ) ;
2146
2147 // create a FBO
2148 GLuint name, tname;
2149 glGenRenderbuffersOES(1, &tname);
2150 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2151 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2152 glGenFramebuffersOES(1, &name);
2153 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2154 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2155 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2156
2157 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002158
2159 LOGD("screenshot: FBO created, status=0x%x", status);
2160
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002161 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2162
2163 // invert everything, b/c glReadPixel() below will invert the FB
2164 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002165 glScissor(0, 0, sw, sh);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002166 glMatrixMode(GL_PROJECTION);
2167 glPushMatrix();
2168 glLoadIdentity();
2169 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2170 glMatrixMode(GL_MODELVIEW);
2171
2172 // redraw the screen entirely...
2173 glClearColor(0,0,0,1);
2174 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002175
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002176 LOGD("screenshot: glClear() issued");
2177
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002178 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2179 const size_t count = layers.size();
2180 for (size_t i=0 ; i<count ; ++i) {
2181 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002182 const uint32_t z = layer->drawingState().z;
2183 if (z >= minLayerZ && z <= maxLayerZ) {
2184 layer->drawForSreenShot();
2185 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002186 }
2187
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002188 LOGD("screenshot: All layers rendered");
2189
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002190 // XXX: this is needed on tegra
2191 glScissor(0, 0, sw, sh);
2192
2193 // check for errors and return screen capture
2194 if (glGetError() != GL_NO_ERROR) {
2195 // error while rendering
2196 result = INVALID_OPERATION;
2197 } else {
2198 // allocate shared memory large enough to hold the
2199 // screen capture
2200 sp<MemoryHeapBase> base(
2201 new MemoryHeapBase(size, 0, "screen-capture") );
2202 void* const ptr = base->getBase();
2203 if (ptr) {
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002204
2205 LOGD("screenshot: about to call glReadPixels(0,0,%d,%d,...,%p)",
2206 sw, sh, ptr);
2207
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002208 // capture the screen with glReadPixels()
2209 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2210 if (glGetError() == GL_NO_ERROR) {
2211 *heap = base;
2212 *w = sw;
2213 *h = sh;
2214 *f = PIXEL_FORMAT_RGBA_8888;
2215 result = NO_ERROR;
2216 }
2217 } else {
2218 result = NO_MEMORY;
2219 }
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002220
2221 LOGD("screenshot: glReadPixels() returned %s", strerror(result));
2222
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002223 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002224 glEnable(GL_SCISSOR_TEST);
2225 glViewport(0, 0, hw_w, hw_h);
2226 glMatrixMode(GL_PROJECTION);
2227 glPopMatrix();
2228 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002229 } else {
2230 result = BAD_VALUE;
2231 }
2232
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002233 LOGD("screenshot: about to release FBO resources");
2234
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002235 // release FBO resources
2236 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2237 glDeleteRenderbuffersOES(1, &tname);
2238 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002239
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002240 LOGD("screenshot: about to call compositionComplete()");
2241
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002242 hw.compositionComplete();
2243
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002244 LOGD("screenshot: result = %s", strerror(result));
2245
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002246 return result;
2247}
2248
2249
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002250status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2251 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002252 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002253 uint32_t sw, uint32_t sh,
2254 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002255{
2256 // only one display supported for now
2257 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2258 return BAD_VALUE;
2259
2260 if (!GLExtensions::getInstance().haveFramebufferObject())
2261 return INVALID_OPERATION;
2262
2263 class MessageCaptureScreen : public MessageBase {
2264 SurfaceFlinger* flinger;
2265 DisplayID dpy;
2266 sp<IMemoryHeap>* heap;
2267 uint32_t* w;
2268 uint32_t* h;
2269 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002270 uint32_t sw;
2271 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002272 uint32_t minLayerZ;
2273 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002274 status_t result;
2275 public:
2276 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002277 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002278 uint32_t sw, uint32_t sh,
2279 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002280 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002281 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2282 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2283 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002284 {
2285 }
2286 status_t getResult() const {
2287 return result;
2288 }
2289 virtual bool handler() {
2290 Mutex::Autolock _l(flinger->mStateLock);
2291
2292 // if we have secure windows, never allow the screen capture
2293 if (flinger->mSecureFrameBuffer)
2294 return true;
2295
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002296 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002297 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002298
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002299 return true;
2300 }
2301 };
2302
2303 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002304 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002305 status_t res = postMessageSync(msg);
2306 if (res == NO_ERROR) {
2307 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2308 }
2309 return res;
2310}
2311
2312// ---------------------------------------------------------------------------
2313
Mathias Agopian7623da42010-06-01 15:12:58 -07002314sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2315{
2316 sp<Layer> result;
2317 Mutex::Autolock _l(mStateLock);
2318 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2319 return result;
2320}
2321
2322// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323
Mathias Agopian593c05c2010-06-02 23:28:45 -07002324Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002325 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327}
2328
Mathias Agopian593c05c2010-06-02 23:28:45 -07002329Client::~Client()
2330{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002331 const size_t count = mLayers.size();
2332 for (size_t i=0 ; i<count ; i++) {
2333 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2334 if (layer != 0) {
2335 mFlinger->removeLayer(layer);
2336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 }
2338}
2339
Mathias Agopian593c05c2010-06-02 23:28:45 -07002340status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002341 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342}
Mathias Agopian1473f462009-04-10 14:24:30 -07002343
Mathias Agopian593c05c2010-06-02 23:28:45 -07002344ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2345{
Mathias Agopian7623da42010-06-01 15:12:58 -07002346 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002347 mLayers.add(name, layer);
2348 return name;
2349}
2350
Mathias Agopian7623da42010-06-01 15:12:58 -07002351void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002352{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002353 // we do a linear search here, because this doesn't happen often
2354 const size_t count = mLayers.size();
2355 for (size_t i=0 ; i<count ; i++) {
2356 if (mLayers.valueAt(i) == layer) {
2357 mLayers.removeItemsAt(i, 1);
2358 break;
2359 }
2360 }
2361}
Mathias Agopian1473f462009-04-10 14:24:30 -07002362sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2363 sp<LayerBaseClient> lbc;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002364 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2365 if (layer != 0) {
2366 lbc = layer.promote();
2367 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002368 }
2369 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370}
2371
Mathias Agopian593c05c2010-06-02 23:28:45 -07002372sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002373 return 0;
2374}
2375ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2376 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002378sp<ISurface> Client::createSurface(
Mathias Agopian7623da42010-06-01 15:12:58 -07002379 ISurfaceComposerClient::surface_data_t* params, int pid,
2380 const String8& name,
2381 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 uint32_t flags)
2383{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002384 return mFlinger->createSurface(this, pid, name, params,
2385 display, w, h, format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002387status_t Client::destroySurface(SurfaceID sid) {
2388 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002390status_t Client::setState(int32_t count, const layer_state_t* states) {
2391 return mFlinger->setClientState(this, count, states);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392}
2393
2394// ---------------------------------------------------------------------------
2395
Mathias Agopian7623da42010-06-01 15:12:58 -07002396UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2397 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2398{
2399 const int pgsize = getpagesize();
2400 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2401
2402 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2403 "SurfaceFlinger Client control-block");
2404
2405 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2406 if (ctrlblk) { // construct the shared structure in-place.
2407 new(ctrlblk) SharedClient;
2408 }
2409}
2410
2411UserClient::~UserClient()
2412{
2413 if (ctrlblk) {
2414 ctrlblk->~SharedClient(); // destroy our shared-structure.
2415 }
2416
2417 /*
2418 * When a UserClient dies, it's unclear what to do exactly.
2419 * We could go ahead and destroy all surfaces linked to that client
2420 * however, it wouldn't be fair to the main Client
2421 * (usually the the window-manager), which might want to re-target
2422 * the layer to another UserClient.
2423 * I think the best is to do nothing, or not much; in most cases the
2424 * WM itself will go ahead and clean things up when it detects a client of
2425 * his has died.
2426 * The remaining question is what to display? currently we keep
2427 * just keep the current buffer.
2428 */
2429}
2430
2431status_t UserClient::initCheck() const {
2432 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2433}
2434
2435void UserClient::detachLayer(const Layer* layer)
2436{
2437 int32_t name = layer->getToken();
2438 if (name >= 0) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002439 int32_t mask = 1LU<<name;
2440 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2441 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2442 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002443 }
2444}
2445
2446sp<IMemoryHeap> UserClient::getControlBlock() const {
2447 return mCblkHeap;
2448}
2449
2450ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2451{
2452 int32_t name = NAME_NOT_FOUND;
2453 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002454 if (layer == 0) {
2455 return name;
2456 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002457
Mathias Agopian5e140102010-06-08 19:54:15 -07002458 // if this layer already has a token, just return it
Mathias Agopian7623da42010-06-01 15:12:58 -07002459 name = layer->getToken();
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002460 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002461 return name;
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002462 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002463
2464 name = 0;
2465 do {
2466 int32_t mask = 1LU<<name;
2467 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2468 // we found and locked that name
Mathias Agopian5e140102010-06-08 19:54:15 -07002469 status_t err = layer->setToken(
2470 const_cast<UserClient*>(this), ctrlblk, name);
2471 if (err != NO_ERROR) {
2472 // free the name
2473 android_atomic_and(~mask, &mBitmap);
2474 name = err;
2475 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002476 break;
2477 }
2478 if (++name > 31)
2479 name = NO_MEMORY;
2480 } while(name >= 0);
2481
Mathias Agopian1debc662010-06-08 15:40:56 -07002482 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2483 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopian7623da42010-06-01 15:12:58 -07002484 return name;
2485}
2486
2487sp<ISurface> UserClient::createSurface(
2488 ISurfaceComposerClient::surface_data_t* params, int pid,
2489 const String8& name,
2490 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2491 uint32_t flags) {
2492 return 0;
2493}
2494status_t UserClient::destroySurface(SurfaceID sid) {
2495 return INVALID_OPERATION;
2496}
2497status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2498 return INVALID_OPERATION;
2499}
2500
2501// ---------------------------------------------------------------------------
2502
Jamie Gennisf7acf162011-01-12 18:30:40 -08002503GraphicBufferAlloc::GraphicBufferAlloc() {}
2504
2505GraphicBufferAlloc::~GraphicBufferAlloc() {}
2506
2507sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2508 PixelFormat format, uint32_t usage) {
2509 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2510 status_t err = graphicBuffer->initCheck();
2511 if (err != 0) {
2512 LOGE("createGraphicBuffer: init check failed: %d", err);
2513 return 0;
2514 } else if (graphicBuffer->handle == 0) {
2515 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2516 return 0;
2517 }
2518 Mutex::Autolock _l(mLock);
2519 mBuffers.add(graphicBuffer);
2520 return graphicBuffer;
2521}
2522
2523void GraphicBufferAlloc::freeAllGraphicBuffersExcept(int bufIdx) {
2524 Mutex::Autolock _l(mLock);
2525 if (0 <= bufIdx && bufIdx < mBuffers.size()) {
2526 sp<GraphicBuffer> b(mBuffers[bufIdx]);
2527 mBuffers.clear();
2528 mBuffers.add(b);
2529 } else {
2530 mBuffers.clear();
2531 }
2532}
2533
2534// ---------------------------------------------------------------------------
2535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536GraphicPlane::GraphicPlane()
2537 : mHw(0)
2538{
2539}
2540
2541GraphicPlane::~GraphicPlane() {
2542 delete mHw;
2543}
2544
2545bool GraphicPlane::initialized() const {
2546 return mHw ? true : false;
2547}
2548
Mathias Agopian66c77a52010-02-08 15:49:35 -08002549int GraphicPlane::getWidth() const {
2550 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551}
2552
Mathias Agopian66c77a52010-02-08 15:49:35 -08002553int GraphicPlane::getHeight() const {
2554 return mHeight;
2555}
2556
2557void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2558{
2559 mHw = hw;
2560
2561 // initialize the display orientation transform.
2562 // it's a constant that should come from the display driver.
2563 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2564 char property[PROPERTY_VALUE_MAX];
2565 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2566 //displayOrientation
2567 switch (atoi(property)) {
2568 case 90:
2569 displayOrientation = ISurfaceComposer::eOrientation90;
2570 break;
2571 case 270:
2572 displayOrientation = ISurfaceComposer::eOrientation270;
2573 break;
2574 }
2575 }
2576
2577 const float w = hw->getWidth();
2578 const float h = hw->getHeight();
2579 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2580 &mDisplayTransform);
2581 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2582 mDisplayWidth = h;
2583 mDisplayHeight = w;
2584 } else {
2585 mDisplayWidth = w;
2586 mDisplayHeight = h;
2587 }
2588
2589 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590}
2591
2592status_t GraphicPlane::orientationToTransfrom(
2593 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002594{
2595 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 switch (orientation) {
2597 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002598 flags = Transform::ROT_0;
2599 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002601 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 break;
2603 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002604 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 break;
2606 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002607 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 break;
2609 default:
2610 return BAD_VALUE;
2611 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002612 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 return NO_ERROR;
2614}
2615
2616status_t GraphicPlane::setOrientation(int orientation)
2617{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 // If the rotation can be handled in hardware, this is where
2619 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002620
2621 const DisplayHardware& hw(displayHardware());
2622 const float w = mDisplayWidth;
2623 const float h = mDisplayHeight;
2624 mWidth = int(w);
2625 mHeight = int(h);
2626
2627 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002628 GraphicPlane::orientationToTransfrom(orientation, w, h,
2629 &orientationTransform);
2630 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2631 mWidth = int(h);
2632 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002634
Mathias Agopian3552f532009-03-27 17:58:20 -07002635 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002636 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 return NO_ERROR;
2638}
2639
2640const DisplayHardware& GraphicPlane::displayHardware() const {
2641 return *mHw;
2642}
2643
Mathias Agopianaab758e2010-10-11 12:37:43 -07002644DisplayHardware& GraphicPlane::editDisplayHardware() {
2645 return *mHw;
2646}
2647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648const Transform& GraphicPlane::transform() const {
2649 return mGlobalTransform;
2650}
2651
Mathias Agopian1473f462009-04-10 14:24:30 -07002652EGLDisplay GraphicPlane::getEGLDisplay() const {
2653 return mHw->getEGLDisplay();
2654}
2655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656// ---------------------------------------------------------------------------
2657
2658}; // namespace android