blob: a9fa1ef424437829e24e0dbc5bbaa7ffdcb4a882 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian6950e422009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070054#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Mathias Agopian627e7b52009-05-21 19:21:59 -070056/* ideally AID_GRAPHICS would be in a semi-public header
57 * or there would be a way to map a user/group name to its id
58 */
59#ifndef AID_GRAPHICS
60#define AID_GRAPHICS 1003
61#endif
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063#define DISPLAY_COUNT 1
64
65namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066// ---------------------------------------------------------------------------
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068SurfaceFlinger::SurfaceFlinger()
69 : BnSurfaceComposer(), Thread(false),
70 mTransactionFlags(0),
71 mTransactionCount(0),
Mathias Agopian9779b222009-09-07 16:32:45 -070072 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070073 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 mBootTime(systemTime()),
Mathias Agopian151e8592009-06-15 18:24:59 -070075 mHardwareTest("android.permission.HARDWARE_TEST"),
76 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopianca5edbe2010-09-24 11:26:58 -070077 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian151e8592009-06-15 18:24:59 -070078 mDump("android.permission.DUMP"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070080 mHwWorkListDirty(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070083 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070088 mDebugDisableHWC(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070089 mDebugInSwapBuffers(0),
90 mLastSwapBufferTime(0),
91 mDebugInTransaction(0),
92 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -070093 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 mConsoleSignals(0),
95 mSecureFrameBuffer(0)
96{
97 init();
98}
99
100void SurfaceFlinger::init()
101{
102 LOGI("SurfaceFlinger is starting");
103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 property_get("debug.sf.showbackground", value, "0");
109 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700111 LOGI_IF(mDebugRegion, "showupdates enabled");
112 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}
114
115SurfaceFlinger::~SurfaceFlinger()
116{
117 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118}
119
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700120sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700122 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
Mathias Agopian770492c2010-05-28 14:22:23 -0700125sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700127 sp<ISurfaceComposerClient> bclient;
128 sp<Client> client(new Client(this));
129 status_t err = client->initCheck();
130 if (err == NO_ERROR) {
131 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 return bclient;
134}
135
Mathias Agopian7623da42010-06-01 15:12:58 -0700136sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
137{
138 sp<ISurfaceComposerClient> bclient;
139 sp<UserClient> client(new UserClient(this));
140 status_t err = client->initCheck();
141 if (err == NO_ERROR) {
142 bclient = client;
143 }
144 return bclient;
145}
146
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 mReadyToRunBarrier.open();
263
264 /*
265 * We're now ready to accept clients...
266 */
267
Mathias Agopian627e7b52009-05-21 19:21:59 -0700268 // start boot animation
269 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 return NO_ERROR;
272}
273
274// ----------------------------------------------------------------------------
275#if 0
276#pragma mark -
277#pragma mark Events Handler
278#endif
279
280void SurfaceFlinger::waitForEvent()
281{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700282 while (true) {
283 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800284 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700285 if (UNLIKELY(isFrozen())) {
286 // wait 5 seconds
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700287 const nsecs_t now = systemTime();
288 if (mFreezeDisplayTime == 0) {
289 mFreezeDisplayTime = now;
290 }
291 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
292 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700293 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700294
Mathias Agopian898c4c92010-05-18 17:06:55 -0700295 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-12-01 17:23:28 -0800296
297 // see if we timed out
298 if (isFrozen()) {
299 const nsecs_t now = systemTime();
300 nsecs_t frozenTime = (now - mFreezeDisplayTime);
301 if (frozenTime >= freezeDisplayTimeout) {
302 // we timed out and are still frozen
303 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
304 mFreezeDisplay, mFreezeCount);
305 mFreezeDisplayTime = 0;
306 mFreezeCount = 0;
307 mFreezeDisplay = false;
308 }
309 }
310
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700311 if (msg != 0) {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700312 switch (msg->what) {
313 case MessageQueue::INVALIDATE:
314 // invalidate message, just return to the main loop
315 return;
316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319}
320
321void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700322 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323}
324
325void SurfaceFlinger::signal() const {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700326 // this is the IPC call
327 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328}
329
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800330bool SurfaceFlinger::authenticateSurface(const sp<ISurface>& surface) const {
331 Mutex::Autolock _l(mStateLock);
332 sp<IBinder> surfBinder(surface->asBinder());
333
334 // Check the visible layer list for the ISurface
335 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
336 size_t count = currentLayers.size();
337 for (size_t i=0 ; i<count ; i++) {
338 const sp<LayerBase>& layer(currentLayers[i]);
339 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
340 if (lbc != NULL && lbc->getSurfaceBinder() == surfBinder) {
341 return true;
342 }
343 }
344
345 // Check the layers in the purgatory. This check is here so that if a
346 // Surface gets destroyed before all the clients are done using it, the
347 // error will not be reported as "surface XYZ is not authenticated", but
348 // will instead fail later on when the client tries to use the surface,
349 // which should be reported as "surface XYZ returned an -ENODEV". The
350 // purgatorized layers are no less authentic than the visible ones, so this
351 // should not cause any harm.
352 size_t purgatorySize = mLayerPurgatory.size();
353 for (size_t i=0 ; i<purgatorySize ; i++) {
354 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
355 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
356 if (lbc != NULL && lbc->getSurfaceBinder() == surfBinder) {
357 return true;
358 }
359 }
360
361 return false;
362}
363
Mathias Agopian898c4c92010-05-18 17:06:55 -0700364status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
365 nsecs_t reltime, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700367 return mEventQueue.postMessage(msg, reltime, flags);
368}
369
370status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
371 nsecs_t reltime, uint32_t flags)
372{
373 status_t res = mEventQueue.postMessage(msg, reltime, flags);
374 if (res == NO_ERROR) {
375 msg->wait();
376 }
377 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378}
379
380// ----------------------------------------------------------------------------
381#if 0
382#pragma mark -
383#pragma mark Main loop
384#endif
385
386bool SurfaceFlinger::threadLoop()
387{
388 waitForEvent();
389
390 // check for transactions
391 if (UNLIKELY(mConsoleSignals)) {
392 handleConsoleEvents();
393 }
394
395 if (LIKELY(mTransactionCount == 0)) {
396 // if we're in a global transaction, don't do anything.
397 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
398 uint32_t transactionFlags = getTransactionFlags(mask);
399 if (LIKELY(transactionFlags)) {
400 handleTransaction(transactionFlags);
401 }
402 }
403
404 // post surfaces (if needed)
405 handlePageFlip();
406
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700407 if (UNLIKELY(mHwWorkListDirty)) {
408 // build the h/w work list
409 handleWorkList();
410 }
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700413 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-09-13 22:57:58 -0700415
416 const int index = hw.getCurrentBufferIndex();
417 GraphicLog& logger(GraphicLog::getInstance());
418
419 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 handleRepaint();
421
Mathias Agopianb1a18742009-09-17 16:18:16 -0700422 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700423 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700424 hw.compositionComplete();
425
Mathias Agopian04262e92010-09-13 22:57:58 -0700426 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700427 postFramebuffer();
428
Mathias Agopian04262e92010-09-13 22:57:58 -0700429 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 } else {
431 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800432 hw.compositionComplete();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 usleep(16667); // 60 fps period
434 }
435 return true;
436}
437
438void SurfaceFlinger::postFramebuffer()
439{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 if (!mInvalidRegion.isEmpty()) {
441 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700442 const nsecs_t now = systemTime();
443 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700445 mLastSwapBufferTime = systemTime() - now;
446 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449}
450
451void SurfaceFlinger::handleConsoleEvents()
452{
453 // something to do with the console
454 const DisplayHardware& hw = graphicPlane(0).displayHardware();
455
456 int what = android_atomic_and(0, &mConsoleSignals);
457 if (what & eConsoleAcquired) {
458 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700459 // this is a temporary work-around, eventually this should be called
460 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700461 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463
Mathias Agopianaab758e2010-10-11 12:37:43 -0700464 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700465 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 hw.releaseScreen();
468 }
469
470 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700471 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 hw.releaseScreen();
473 } else {
474 mDeferReleaseConsole = true;
475 }
476 }
477
478 mDirtyRegion.set(hw.bounds());
479}
480
481void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
482{
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700483 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484
Mathias Agopianff1d4102010-08-10 17:19:56 -0700485 /*
486 * Perform and commit the transaction
487 */
488
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700489 { // scope for the lock
490 Mutex::Autolock _l(mStateLock);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700491 const nsecs_t now = systemTime();
492 mDebugInTransaction = now;
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700493 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700494 mLastTransactionTime = systemTime() - now;
495 mDebugInTransaction = 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800496 invalidateHwcGeometry();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700497 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700498 }
499
Mathias Agopianff1d4102010-08-10 17:19:56 -0700500 /*
501 * Clean-up all layers that went away
502 * (do this without the lock held)
503 */
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700504
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700505 const size_t count = ditchedLayers.size();
506 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianffae4fc2009-09-04 19:50:23 -0700507 if (ditchedLayers[i] != 0) {
508 //LOGD("ditching layer %p", ditchedLayers[i].get());
509 ditchedLayers[i]->ditch();
510 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700511 }
512}
513
514void SurfaceFlinger::handleTransactionLocked(
515 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
516{
517 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 const size_t count = currentLayers.size();
519
520 /*
521 * Traversal of the children
522 * (perform the transaction for each of them if needed)
523 */
524
525 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
526 if (layersNeedTransaction) {
527 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700528 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
530 if (!trFlags) continue;
531
532 const uint32_t flags = layer->doTransaction(0);
533 if (flags & Layer::eVisibleRegion)
534 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536 }
537
538 /*
539 * Perform our own transaction if needed
540 */
541
542 if (transactionFlags & eTransactionNeeded) {
543 if (mCurrentState.orientation != mDrawingState.orientation) {
544 // the orientation has changed, recompute all visible regions
545 // and invalidate everything.
546
547 const int dpy = 0;
548 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700549 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 GraphicPlane& plane(graphicPlane(dpy));
551 plane.setOrientation(orientation);
552
553 // update the shared control block
554 const DisplayHardware& hw(plane.displayHardware());
555 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
556 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800557 dcblk->w = plane.getWidth();
558 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
560 mVisibleRegionsDirty = true;
561 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563
564 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
565 // freezing or unfreezing the display -> trigger animation if needed
566 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800567 if (mFreezeDisplay)
568 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700571 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
572 // layers have been added
573 mVisibleRegionsDirty = true;
574 }
575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 // some layers might have been removed, so
577 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700578 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700579 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700581 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700582 const size_t count = previousLayers.size();
583 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700584 const sp<LayerBase>& layer(previousLayers[i]);
585 if (currentLayers.indexOf( layer ) < 0) {
586 // this layer is not visible anymore
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700587 ditchedLayers.add(layer);
Mathias Agopian33863dd2009-07-28 14:20:21 -0700588 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700589 }
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593
594 commitTransaction();
595}
596
597sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
598{
599 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
600}
601
602void SurfaceFlinger::computeVisibleRegions(
603 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
604{
605 const GraphicPlane& plane(graphicPlane(0));
606 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700607 const DisplayHardware& hw(plane.displayHardware());
608 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 Region aboveOpaqueLayers;
611 Region aboveCoveredLayers;
612 Region dirty;
613
614 bool secureFrameBuffer = false;
615
616 size_t i = currentLayers.size();
617 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700618 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 layer->validateVisibility(planeTransform);
620
621 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700622 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700624 /*
625 * opaqueRegion: area of a surface that is fully opaque.
626 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700628
629 /*
630 * visibleRegion: area of a surface that is visible on screen
631 * and not fully transparent. This is essentially the layer's
632 * footprint minus the opaque regions above it.
633 * Areas covered by a translucent surface are considered visible.
634 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700636
637 /*
638 * coveredRegion: area of a surface that is covered by all
639 * visible regions above it (which includes the translucent areas).
640 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700642
643
644 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700645 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 const bool translucent = layer->needsBlending();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700647 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700649 visibleRegion.andSelf(screenRegion);
650 if (!visibleRegion.isEmpty()) {
651 // Remove the transparent area from the visible region
652 if (translucent) {
653 visibleRegion.subtractSelf(layer->transparentRegionScreen);
654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700656 // compute the opaque region
657 const int32_t layerOrientation = layer->getOrientation();
658 if (s.alpha==255 && !translucent &&
659 ((layerOrientation & Transform::ROT_INVALID) == false)) {
660 // the opaque region is the layer's footprint
661 opaqueRegion = visibleRegion;
662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664 }
665
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700666 // Clip the covered region to the visible region
667 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
668
669 // Update aboveCoveredLayers for next (lower) layer
670 aboveCoveredLayers.orSelf(visibleRegion);
671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 // subtract the opaque region covered by the layers above us
673 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
675 // compute this layer's dirty region
676 if (layer->contentDirty) {
677 // we need to invalidate the whole region
678 dirty = visibleRegion;
679 // as well, as the old visible region
680 dirty.orSelf(layer->visibleRegionScreen);
681 layer->contentDirty = false;
682 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700683 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700684 * the exposed region consists of two components:
685 * 1) what's VISIBLE now and was COVERED before
686 * 2) what's EXPOSED now less what was EXPOSED before
687 *
688 * note that (1) is conservative, we start with the whole
689 * visible region but only keep what used to be covered by
690 * something -- which mean it may have been exposed.
691 *
692 * (2) handles areas that were not covered by anything but got
693 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700694 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700695 const Region newExposed = visibleRegion - coveredRegion;
696 const Region oldVisibleRegion = layer->visibleRegionScreen;
697 const Region oldCoveredRegion = layer->coveredRegionScreen;
698 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
699 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701 dirty.subtractSelf(aboveOpaqueLayers);
702
703 // accumulate to the screen dirty region
704 dirtyRegion.orSelf(dirty);
705
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700706 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 // Store the visible region is screen space
710 layer->setVisibleRegion(visibleRegion);
711 layer->setCoveredRegion(coveredRegion);
712
Mathias Agopian12cedff2009-07-28 10:57:27 -0700713 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 if (layer->isSecure() && !visibleRegion.isEmpty()) {
715 secureFrameBuffer = true;
716 }
717 }
718
Mathias Agopian12cedff2009-07-28 10:57:27 -0700719 // invalidate the areas where a layer was removed
720 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
721 mDirtyRegionRemovedLayer.clear();
722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 mSecureFrameBuffer = secureFrameBuffer;
724 opaqueRegion = aboveOpaqueLayers;
725}
726
727
728void SurfaceFlinger::commitTransaction()
729{
730 mDrawingState = mCurrentState;
Mathias Agopian9779b222009-09-07 16:32:45 -0700731 mResizeTransationPending = false;
732 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733}
734
735void SurfaceFlinger::handlePageFlip()
736{
737 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700738 LayerVector& currentLayers(
739 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 visibleRegions |= lockPageFlip(currentLayers);
741
742 const DisplayHardware& hw = graphicPlane(0).displayHardware();
743 const Region screenRegion(hw.bounds());
744 if (visibleRegions) {
745 Region opaqueRegion;
746 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700747
748 /*
749 * rebuild the visible layer list
750 */
751 mVisibleLayersSortedByZ.clear();
752 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
753 size_t count = currentLayers.size();
754 mVisibleLayersSortedByZ.setCapacity(count);
755 for (size_t i=0 ; i<count ; i++) {
756 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
757 mVisibleLayersSortedByZ.add(currentLayers[i]);
758 }
759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 mWormholeRegion = screenRegion.subtract(opaqueRegion);
761 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800762 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764
765 unlockPageFlip(currentLayers);
766 mDirtyRegion.andSelf(screenRegion);
767}
768
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800769void SurfaceFlinger::invalidateHwcGeometry()
770{
771 mHwWorkListDirty = true;
772}
773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
775{
776 bool recomputeVisibleRegions = false;
777 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700778 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700780 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 layer->lockPageFlip(recomputeVisibleRegions);
782 }
783 return recomputeVisibleRegions;
784}
785
786void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
787{
788 const GraphicPlane& plane(graphicPlane(0));
789 const Transform& planeTransform(plane.transform());
790 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700791 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700793 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 layer->unlockPageFlip(planeTransform, mDirtyRegion);
795 }
796}
797
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700798void SurfaceFlinger::handleWorkList()
799{
800 mHwWorkListDirty = false;
801 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
802 if (hwc.initCheck() == NO_ERROR) {
803 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
804 const size_t count = currentLayers.size();
805 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700806 hwc_layer_t* const cur(hwc.getLayers());
807 for (size_t i=0 ; cur && i<count ; i++) {
808 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian6a969242010-09-22 18:58:01 -0700809 if (mDebugDisableHWC) {
810 cur[i].compositionType = HWC_FRAMEBUFFER;
811 cur[i].flags |= HWC_SKIP_LAYER;
812 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700813 }
814 }
815}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817void SurfaceFlinger::handleRepaint()
818{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700819 // compute the invalid region
820 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821
822 if (UNLIKELY(mDebugRegion)) {
823 debugFlashRegions();
824 }
825
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700826 // set the frame buffer
827 const DisplayHardware& hw(graphicPlane(0).displayHardware());
828 glMatrixMode(GL_MODELVIEW);
829 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830
831 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700832 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
833 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700834 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700835 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
836 // takes a rectangle, we must make sure to update that whole
837 // rectangle in that case
838 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700839 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700840 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 mDirtyRegion.set(mInvalidRegion.bounds());
842 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700843 // in the BUFFER_PRESERVED case, obviously, we can update only
844 // what's needed and nothing more.
845 // NOTE: this is NOT a common case, as preserving the backbuffer
846 // is costly and usually involves copying the whole update back.
847 }
848 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700849 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700850 // We need to redraw the rectangle that will be updated
851 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700852 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700853 // rectangle instead of a region (see DisplayHardware::flip())
854 mDirtyRegion.set(mInvalidRegion.bounds());
855 } else {
856 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 mDirtyRegion.set(hw.bounds());
858 mInvalidRegion = mDirtyRegion;
859 }
860 }
861
862 // compose all surfaces
863 composeSurfaces(mDirtyRegion);
864
865 // clear the dirty regions
866 mDirtyRegion.clear();
867}
868
869void SurfaceFlinger::composeSurfaces(const Region& dirty)
870{
871 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
872 // should never happen unless the window manager has a bug
873 // draw something...
874 drawWormhole();
875 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700876
877 status_t err = NO_ERROR;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700878 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700879 size_t count = layers.size();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700880
881 const DisplayHardware& hw(graphicPlane(0).displayHardware());
882 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700883 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700884
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700885 LOGE_IF(cur && hwc.getNumLayers() != count,
886 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
887 hwc.getNumLayers(), count);
888
889 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700890 if (hwc.initCheck() == NO_ERROR) {
891 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
892 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700893
894 /*
895 * update the per-frame h/w composer data for each layer
896 * and build the transparent region of the FB
897 */
898 Region transparent;
899 if (cur) {
900 for (size_t i=0 ; i<count ; i++) {
901 const sp<LayerBase>& layer(layers[i]);
902 layer->setPerFrameData(&cur[i]);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700903 }
904 err = hwc.prepare();
905 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700906
Mathias Agopian45491692011-01-19 15:24:23 -0800907 if (err == NO_ERROR) {
908 for (size_t i=0 ; i<count ; i++) {
909 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
910 const sp<LayerBase>& layer(layers[i]);
911 if (!(layer->needsBlending())) {
912 transparent.orSelf(layer->visibleRegionScreen);
913 }
914 }
915 }
916
917 /*
918 * clear the area of the FB that need to be transparent
919 */
920 transparent.andSelf(dirty);
921 if (!transparent.isEmpty()) {
922 glClearColor(0,0,0,0);
923 Region::const_iterator it = transparent.begin();
924 Region::const_iterator const end = transparent.end();
925 const int32_t height = hw.getHeight();
926 while (it != end) {
927 const Rect& r(*it++);
928 const GLint sy = height - (r.top + r.height());
929 glScissor(r.left, sy, r.width(), r.height());
930 glClear(GL_COLOR_BUFFER_BIT);
931 }
932 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700933 }
934 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700935
936
937 /*
938 * and then, render the layers targeted at the framebuffer
939 */
940 for (size_t i=0 ; i<count ; i++) {
941 if (cur) {
Antti Hatala982f58b2010-09-09 02:32:30 -0700942 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
943 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700944 // skip layers handled by the HAL
945 continue;
946 }
947 }
Mathias Agopian6a969242010-09-22 18:58:01 -0700948
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700949 const sp<LayerBase>& layer(layers[i]);
950 const Region clip(dirty.intersect(layer->visibleRegionScreen));
951 if (!clip.isEmpty()) {
952 layer->draw(clip);
953 }
954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955}
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957void SurfaceFlinger::debugFlashRegions()
958{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700959 const DisplayHardware& hw(graphicPlane(0).displayHardware());
960 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700961
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700962 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
963 (flags & DisplayHardware::BUFFER_PRESERVED))) {
964 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
965 mDirtyRegion.bounds() : hw.bounds());
966 composeSurfaces(repaint);
967 }
968
969 TextureManager::deactivateTextures();
970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 glDisable(GL_BLEND);
972 glDisable(GL_DITHER);
973 glDisable(GL_SCISSOR_TEST);
974
Mathias Agopiandff8e582009-05-04 14:17:04 -0700975 static int toggle = 0;
976 toggle = 1 - toggle;
977 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700978 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700979 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700980 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700983 Region::const_iterator it = mDirtyRegion.begin();
984 Region::const_iterator const end = mDirtyRegion.end();
985 while (it != end) {
986 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 GLfloat vertices[][2] = {
988 { r.left, r.top },
989 { r.left, r.bottom },
990 { r.right, r.bottom },
991 { r.right, r.top }
992 };
993 glVertexPointer(2, GL_FLOAT, 0, vertices);
994 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
995 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700996
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700997 if (mInvalidRegion.isEmpty()) {
998 mDirtyRegion.dump("mDirtyRegion");
999 mInvalidRegion.dump("mInvalidRegion");
1000 }
1001 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002
1003 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001004 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005
1006 glEnable(GL_SCISSOR_TEST);
1007 //mDirtyRegion.dump("mDirtyRegion");
1008}
1009
1010void SurfaceFlinger::drawWormhole() const
1011{
1012 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1013 if (region.isEmpty())
1014 return;
1015
1016 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1017 const int32_t width = hw.getWidth();
1018 const int32_t height = hw.getHeight();
1019
1020 glDisable(GL_BLEND);
1021 glDisable(GL_DITHER);
1022
1023 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001024 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001025 Region::const_iterator it = region.begin();
1026 Region::const_iterator const end = region.end();
1027 while (it != end) {
1028 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 const GLint sy = height - (r.top + r.height());
1030 glScissor(r.left, sy, r.width(), r.height());
1031 glClear(GL_COLOR_BUFFER_BIT);
1032 }
1033 } else {
1034 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1035 { width, height }, { 0, height } };
1036 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1037 glVertexPointer(2, GL_SHORT, 0, vertices);
1038 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1039 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001040#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001041 if (GLExtensions::getInstance().haveTextureExternal()) {
1042 glDisable(GL_TEXTURE_EXTERNAL_OES);
1043 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001044#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 glEnable(GL_TEXTURE_2D);
1046 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1047 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1048 glMatrixMode(GL_TEXTURE);
1049 glLoadIdentity();
1050 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001051 Region::const_iterator it = region.begin();
1052 Region::const_iterator const end = region.end();
1053 while (it != end) {
1054 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 const GLint sy = height - (r.top + r.height());
1056 glScissor(r.left, sy, r.width(), r.height());
1057 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1058 }
1059 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001060 glLoadIdentity();
1061 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063}
1064
1065void SurfaceFlinger::debugShowFPS() const
1066{
1067 static int mFrameCount;
1068 static int mLastFrameCount = 0;
1069 static nsecs_t mLastFpsTime = 0;
1070 static float mFps = 0;
1071 mFrameCount++;
1072 nsecs_t now = systemTime();
1073 nsecs_t diff = now - mLastFpsTime;
1074 if (diff > ms2ns(250)) {
1075 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1076 mLastFpsTime = now;
1077 mLastFrameCount = mFrameCount;
1078 }
1079 // XXX: mFPS has the value we want
1080 }
1081
Mathias Agopian1473f462009-04-10 14:24:30 -07001082status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083{
1084 Mutex::Autolock _l(mStateLock);
1085 addLayer_l(layer);
1086 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1087 return NO_ERROR;
1088}
1089
Mathias Agopian593c05c2010-06-02 23:28:45 -07001090status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1091{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001092 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001093 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1094}
1095
1096ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1097 const sp<LayerBaseClient>& lbc)
1098{
1099 Mutex::Autolock _l(mStateLock);
1100
1101 // attach this layer to the client
1102 ssize_t name = client->attachLayer(lbc);
1103
1104 // add this layer to the current state list
1105 addLayer_l(lbc);
1106
1107 return name;
1108}
1109
Mathias Agopian1473f462009-04-10 14:24:30 -07001110status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111{
1112 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001113 status_t err = purgatorizeLayer_l(layer);
1114 if (err == NO_ERROR)
1115 setTransactionFlags(eTransactionNeeded);
1116 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117}
1118
Mathias Agopian1473f462009-04-10 14:24:30 -07001119status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120{
Mathias Agopian7623da42010-06-01 15:12:58 -07001121 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1122 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001123 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1126 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001127 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 return NO_ERROR;
1129 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001130 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131}
1132
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001133status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1134{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001135 // First add the layer to the purgatory list, which makes sure it won't
1136 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001137 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001138 if (err >= 0) {
1139 mLayerPurgatory.add(layerBase);
1140 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001141
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001142 layerBase->onRemoved();
1143
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001144 // it's possible that we don't find a layer, because it might
1145 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001146 // from the user because there is a race between Client::destroySurface(),
1147 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001148 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1149}
1150
Mathias Agopian593c05c2010-06-02 23:28:45 -07001151status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001153 layer->forceVisibilityTransaction();
1154 setTransactionFlags(eTraversalNeeded);
1155 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156}
1157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1159{
1160 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1161}
1162
Mathias Agopian898c4c92010-05-18 17:06:55 -07001163uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164{
1165 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1166 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001167 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 return old;
1170}
1171
1172void SurfaceFlinger::openGlobalTransaction()
1173{
1174 android_atomic_inc(&mTransactionCount);
1175}
1176
1177void SurfaceFlinger::closeGlobalTransaction()
1178{
1179 if (android_atomic_dec(&mTransactionCount) == 1) {
1180 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001181
Andreas Hubere3c01832010-08-16 08:49:37 -07001182 // if there is a transaction with a resize, wait for it to
Mathias Agopian9779b222009-09-07 16:32:45 -07001183 // take effect before returning.
1184 Mutex::Autolock _l(mStateLock);
1185 while (mResizeTransationPending) {
Mathias Agopian98a9c562009-09-30 14:42:13 -07001186 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1187 if (CC_UNLIKELY(err != NO_ERROR)) {
1188 // just in case something goes wrong in SF, return to the
1189 // called after a few seconds.
1190 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1191 mResizeTransationPending = false;
1192 break;
1193 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 }
1196}
1197
1198status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1199{
1200 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1201 return BAD_VALUE;
1202
1203 Mutex::Autolock _l(mStateLock);
1204 mCurrentState.freezeDisplay = 1;
1205 setTransactionFlags(eTransactionNeeded);
1206
1207 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001208 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 return NO_ERROR;
1210}
1211
1212status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1213{
1214 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1215 return BAD_VALUE;
1216
1217 Mutex::Autolock _l(mStateLock);
1218 mCurrentState.freezeDisplay = 0;
1219 setTransactionFlags(eTransactionNeeded);
1220
1221 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001222 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 return NO_ERROR;
1224}
1225
Andreas Hubere3c01832010-08-16 08:49:37 -07001226int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001227 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228{
1229 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1230 return BAD_VALUE;
1231
1232 Mutex::Autolock _l(mStateLock);
1233 if (mCurrentState.orientation != orientation) {
1234 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001235 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 mCurrentState.orientation = orientation;
1237 setTransactionFlags(eTransactionNeeded);
1238 mTransactionCV.wait(mStateLock);
1239 } else {
1240 orientation = BAD_VALUE;
1241 }
1242 }
1243 return orientation;
1244}
1245
Mathias Agopian593c05c2010-06-02 23:28:45 -07001246sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian770492c2010-05-28 14:22:23 -07001247 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1249 uint32_t flags)
1250{
Mathias Agopian1473f462009-04-10 14:24:30 -07001251 sp<LayerBaseClient> layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001253
1254 if (int32_t(w|h) < 0) {
1255 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1256 int(w), int(h));
1257 return surfaceHandle;
1258 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001261 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 switch (flags & eFXSurfaceMask) {
1263 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001264 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1265 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 break;
1267 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001268 // for now we treat Blur as Dim, until we can implement it
1269 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001271 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 break;
1273 }
1274
Mathias Agopian1473f462009-04-10 14:24:30 -07001275 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001276 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001277 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001278 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001281 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001282 params->token = token;
Mathias Agopian18b6b492009-08-19 17:46:26 -07001283 params->identity = surfaceHandle->getIdentity();
1284 params->width = w;
1285 params->height = h;
1286 params->format = format;
Mathias Agopian7623da42010-06-01 15:12:58 -07001287 if (normalLayer != 0) {
1288 Mutex::Autolock _l(mStateLock);
1289 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1290 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001291 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001292
Mathias Agopian593c05c2010-06-02 23:28:45 -07001293 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
1295
1296 return surfaceHandle;
1297}
1298
Mathias Agopian7623da42010-06-01 15:12:58 -07001299sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001300 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001301 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001302 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303{
1304 // initialize the surfaces
1305 switch (format) { // TODO: take h/w into account
1306 case PIXEL_FORMAT_TRANSPARENT:
1307 case PIXEL_FORMAT_TRANSLUCENT:
1308 format = PIXEL_FORMAT_RGBA_8888;
1309 break;
1310 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001311#ifdef NO_RGBX_8888
1312 format = PIXEL_FORMAT_RGB_565;
1313#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001314 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001315#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 break;
1317 }
1318
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001319#ifdef NO_RGBX_8888
1320 if (format == PIXEL_FORMAT_RGBX_8888)
1321 format = PIXEL_FORMAT_RGBA_8888;
1322#endif
1323
Mathias Agopian593c05c2010-06-02 23:28:45 -07001324 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001325 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001326 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001328 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330 return layer;
1331}
1332
Mathias Agopian7623da42010-06-01 15:12:58 -07001333sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001334 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001335 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001337 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 return layer;
1340}
1341
Mathias Agopian593c05c2010-06-02 23:28:45 -07001342status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001344 /*
1345 * called by the window manager, when a surface should be marked for
1346 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001347 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001348 * The surface is removed from the current and drawing lists, but placed
1349 * in the purgatory queue, so it's not destroyed right-away (we need
1350 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001351 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001352
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001353 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001354 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001355 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001356 if (layer != 0) {
1357 err = purgatorizeLayer_l(layer);
1358 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001359 setTransactionFlags(eTransactionNeeded);
1360 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001361 }
1362 return err;
1363}
1364
1365status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1366{
Mathias Agopian359140c2009-07-02 17:33:40 -07001367 // called by ~ISurface() when all references are gone
Andreas Hubere3c01832010-08-16 08:49:37 -07001368
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001369 class MessageDestroySurface : public MessageBase {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001370 SurfaceFlinger* flinger;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001371 sp<LayerBaseClient> layer;
1372 public:
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001373 MessageDestroySurface(
1374 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1375 : flinger(flinger), layer(layer) { }
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001376 virtual bool handler() {
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001377 sp<LayerBaseClient> l(layer);
1378 layer.clear(); // clear it outside of the lock;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001379 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian359140c2009-07-02 17:33:40 -07001380 /*
Andreas Hubere3c01832010-08-16 08:49:37 -07001381 * remove the layer from the current list -- chances are that it's
1382 * not in the list anyway, because it should have been removed
1383 * already upon request of the client (eg: window manager).
Mathias Agopian359140c2009-07-02 17:33:40 -07001384 * However, a buggy client could have not done that.
1385 * Since we know we don't have any more clients, we don't need
1386 * to use the purgatory.
1387 */
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001388 status_t err = flinger->removeLayer_l(l);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001389 if (err == NAME_NOT_FOUND) {
1390 // The surface wasn't in the current list, which means it was
1391 // removed already, which means it is in the purgatory,
1392 // and need to be removed from there.
1393 // This needs to happen from the main thread since its dtor
1394 // must run from there (b/c of OpenGL ES). Additionally, we
1395 // can't really acquire our internal lock from
1396 // destroySurface() -- see postMessage() below.
1397 ssize_t idx = flinger->mLayerPurgatory.remove(l);
1398 LOGE_IF(idx < 0,
1399 "layer=%p is not in the purgatory list", l.get());
1400 }
1401
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001402 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1403 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001404 return true;
1405 }
1406 };
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001407
Mathias Agopian898c4c92010-05-18 17:06:55 -07001408 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 return NO_ERROR;
1410}
1411
1412status_t SurfaceFlinger::setClientState(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001413 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 int32_t count,
1415 const layer_state_t* states)
1416{
1417 Mutex::Autolock _l(mStateLock);
1418 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 for (int i=0 ; i<count ; i++) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001420 const layer_state_t& s(states[i]);
1421 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian1473f462009-04-10 14:24:30 -07001422 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 const uint32_t what = s.what;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 if (what & ePositionChanged) {
1425 if (layer->setPosition(s.x, s.y))
1426 flags |= eTraversalNeeded;
1427 }
1428 if (what & eLayerChanged) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001429 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 if (layer->setLayer(s.z)) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001431 mCurrentState.layersSortedByZ.removeAt(idx);
1432 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 // we need traversal (state changed)
1434 // AND transaction (list changed)
1435 flags |= eTransactionNeeded|eTraversalNeeded;
1436 }
1437 }
1438 if (what & eSizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -07001439 if (layer->setSize(s.w, s.h)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 flags |= eTraversalNeeded;
Mathias Agopian9779b222009-09-07 16:32:45 -07001441 mResizeTransationPending = true;
1442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
1444 if (what & eAlphaChanged) {
1445 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1446 flags |= eTraversalNeeded;
1447 }
1448 if (what & eMatrixChanged) {
1449 if (layer->setMatrix(s.matrix))
1450 flags |= eTraversalNeeded;
1451 }
1452 if (what & eTransparentRegionChanged) {
1453 if (layer->setTransparentRegionHint(s.transparentRegion))
1454 flags |= eTraversalNeeded;
1455 }
1456 if (what & eVisibilityChanged) {
1457 if (layer->setFlags(s.flags, s.mask))
1458 flags |= eTraversalNeeded;
1459 }
1460 }
1461 }
1462 if (flags) {
1463 setTransactionFlags(flags);
1464 }
1465 return NO_ERROR;
1466}
1467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468void SurfaceFlinger::screenReleased(int dpy)
1469{
1470 // this may be called by a signal handler, we can't do too much in here
1471 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1472 signalEvent();
1473}
1474
1475void SurfaceFlinger::screenAcquired(int dpy)
1476{
1477 // this may be called by a signal handler, we can't do too much in here
1478 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1479 signalEvent();
1480}
1481
1482status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1483{
Erik Gilling94720d72010-12-01 16:38:01 -08001484 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 char buffer[SIZE];
1486 String8 result;
Mathias Agopian151e8592009-06-15 18:24:59 -07001487 if (!mDump.checkCalling()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 snprintf(buffer, SIZE, "Permission Denial: "
1489 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1490 IPCThreadState::self()->getCallingPid(),
1491 IPCThreadState::self()->getCallingUid());
1492 result.append(buffer);
1493 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001494
1495 // figure out if we're stuck somewhere
1496 const nsecs_t now = systemTime();
1497 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1498 const nsecs_t inTransaction(mDebugInTransaction);
1499 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1500 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1501
1502 // Try to get the main lock, but don't insist if we can't
1503 // (this would indicate SF is stuck, but we want to be able to
1504 // print something in dumpsys).
1505 int retry = 3;
1506 while (mStateLock.tryLock()<0 && --retry>=0) {
1507 usleep(1000000);
1508 }
1509 const bool locked(retry >= 0);
1510 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001511 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001512 "SurfaceFlinger appears to be unresponsive, "
1513 "dumping anyways (no locks held)\n");
1514 result.append(buffer);
1515 }
1516
Mathias Agopian06a61e22011-01-19 16:15:53 -08001517 /*
1518 * Dump the visible layer list
1519 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1521 const size_t count = currentLayers.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001522 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1523 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001525 const sp<LayerBase>& layer(currentLayers[i]);
1526 layer->dump(result, buffer, SIZE);
1527 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 s.transparentRegion.dump(result, "transparentRegion");
1529 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1530 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1531 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001532
Mathias Agopian06a61e22011-01-19 16:15:53 -08001533 /*
1534 * Dump the layers in the purgatory
1535 */
1536
1537 const size_t purgatorySize = mLayerPurgatory.size();
1538 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1539 result.append(buffer);
1540 for (size_t i=0 ; i<purgatorySize ; i++) {
1541 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1542 layer->shortDump(result, buffer, SIZE);
1543 }
1544
1545 /*
1546 * Dump SurfaceFlinger global state
1547 */
1548
1549 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1550 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 mWormholeRegion.dump(result, "WormholeRegion");
1552 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1553 snprintf(buffer, SIZE,
1554 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1555 mFreezeDisplay?"yes":"no", mFreezeCount,
1556 mCurrentState.orientation, hw.canDraw());
1557 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001558 snprintf(buffer, SIZE,
1559 " last eglSwapBuffers() time: %f us\n"
1560 " last transaction time : %f us\n",
1561 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1562 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001563
Mathias Agopiana8d49172009-08-26 16:36:26 -07001564 if (inSwapBuffersDuration || !locked) {
1565 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1566 inSwapBuffersDuration/1000.0);
1567 result.append(buffer);
1568 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001569
Mathias Agopiana8d49172009-08-26 16:36:26 -07001570 if (inTransactionDuration || !locked) {
1571 snprintf(buffer, SIZE, " transaction time: %f us\n",
1572 inTransactionDuration/1000.0);
1573 result.append(buffer);
1574 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001575
Mathias Agopian06a61e22011-01-19 16:15:53 -08001576 /*
1577 * Dump HWComposer state
1578 */
Mathias Agopian6a969242010-09-22 18:58:01 -07001579 HWComposer& hwc(hw.getHwComposer());
1580 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1581 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1582 mDebugDisableHWC ? "disabled" : "enabled");
1583 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -07001584 hwc.dump(result, buffer, SIZE);
Mathias Agopian6a969242010-09-22 18:58:01 -07001585
Mathias Agopian06a61e22011-01-19 16:15:53 -08001586 /*
1587 * Dump gralloc state
1588 */
Mathias Agopian6950e422009-10-05 17:07:12 -07001589 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001590 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001591 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001592
1593 if (locked) {
1594 mStateLock.unlock();
1595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
1597 write(fd, result.string(), result.size());
1598 return NO_ERROR;
1599}
1600
1601status_t SurfaceFlinger::onTransact(
1602 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1603{
1604 switch (code) {
1605 case CREATE_CONNECTION:
1606 case OPEN_GLOBAL_TRANSACTION:
1607 case CLOSE_GLOBAL_TRANSACTION:
1608 case SET_ORIENTATION:
1609 case FREEZE_DISPLAY:
1610 case UNFREEZE_DISPLAY:
1611 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001612 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001613 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 {
1615 // codes that require permission check
1616 IPCThreadState* ipc = IPCThreadState::self();
1617 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001618 const int uid = ipc->getCallingUid();
Mathias Agopian151e8592009-06-15 18:24:59 -07001619 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1620 LOGE("Permission Denial: "
1621 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1622 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001624 break;
1625 }
1626 case CAPTURE_SCREEN:
1627 {
1628 // codes that require permission check
1629 IPCThreadState* ipc = IPCThreadState::self();
1630 const int pid = ipc->getCallingPid();
1631 const int uid = ipc->getCallingUid();
1632 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1633 LOGE("Permission Denial: "
1634 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1635 return PERMISSION_DENIED;
1636 }
1637 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
1639 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1642 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001643 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian151e8592009-06-15 18:24:59 -07001644 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1645 IPCThreadState* ipc = IPCThreadState::self();
1646 const int pid = ipc->getCallingPid();
1647 const int uid = ipc->getCallingUid();
1648 LOGE("Permission Denial: "
1649 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 return PERMISSION_DENIED;
1651 }
1652 int n;
1653 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001654 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001655 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 return NO_ERROR;
1657 case 1002: // SHOW_UPDATES
1658 n = data.readInt32();
1659 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1660 return NO_ERROR;
1661 case 1003: // SHOW_BACKGROUND
1662 n = data.readInt32();
1663 mDebugBackground = n ? 1 : 0;
1664 return NO_ERROR;
Mathias Agopian6a969242010-09-22 18:58:01 -07001665 case 1008: // toggle use of hw composer
1666 n = data.readInt32();
1667 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -08001668 invalidateHwcGeometry();
Mathias Agopian6a969242010-09-22 18:58:01 -07001669 // fall-through...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 case 1004:{ // repaint everything
1671 Mutex::Autolock _l(mStateLock);
1672 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1673 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1674 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001675 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001677 case 1005:{ // force transaction
1678 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1679 return NO_ERROR;
1680 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001681 case 1006:{ // enable/disable GraphicLog
1682 int enabled = data.readInt32();
1683 GraphicLog::getInstance().setEnabled(enabled);
1684 return NO_ERROR;
1685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 case 1007: // set mFreezeCount
1687 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001688 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 return NO_ERROR;
1690 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001691 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 reply->writeInt32(0);
1693 reply->writeInt32(mDebugRegion);
1694 reply->writeInt32(mDebugBackground);
1695 return NO_ERROR;
1696 case 1013: {
1697 Mutex::Autolock _l(mStateLock);
1698 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1699 reply->writeInt32(hw.getPageFlipCount());
1700 }
1701 return NO_ERROR;
1702 }
1703 }
1704 return err;
1705}
1706
Mathias Agopianaab758e2010-10-11 12:37:43 -07001707// ---------------------------------------------------------------------------
1708
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001709status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1710 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001711{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001712 if (!GLExtensions::getInstance().haveFramebufferObject())
1713 return INVALID_OPERATION;
1714
1715 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001716 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001717 const uint32_t hw_w = hw.getWidth();
1718 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001719 GLfloat u = 1;
1720 GLfloat v = 1;
1721
1722 // make sure to clear all GL error flags
1723 while ( glGetError() != GL_NO_ERROR ) ;
1724
1725 // create a FBO
1726 GLuint name, tname;
1727 glGenTextures(1, &tname);
1728 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001729 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1730 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001731 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001732 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001733 GLint tw = (2 << (31 - clz(hw_w)));
1734 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001735 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1736 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001737 u = GLfloat(hw_w) / tw;
1738 v = GLfloat(hw_h) / th;
1739 }
1740 glGenFramebuffersOES(1, &name);
1741 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001742 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1743 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001744
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001745 // redraw the screen entirely...
1746 glClearColor(0,0,0,1);
1747 glClear(GL_COLOR_BUFFER_BIT);
1748 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1749 const size_t count = layers.size();
1750 for (size_t i=0 ; i<count ; ++i) {
1751 const sp<LayerBase>& layer(layers[i]);
1752 layer->drawForSreenShot();
1753 }
1754
1755 // back to main framebuffer
1756 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1757 glDisable(GL_SCISSOR_TEST);
1758 glDeleteFramebuffersOES(1, &name);
1759
1760 *textureName = tname;
1761 *uOut = u;
1762 *vOut = v;
1763 return NO_ERROR;
1764}
1765
1766// ---------------------------------------------------------------------------
1767
1768status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1769{
1770 status_t result = PERMISSION_DENIED;
1771
1772 if (!GLExtensions::getInstance().haveFramebufferObject())
1773 return INVALID_OPERATION;
1774
1775 // get screen geometry
1776 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1777 const uint32_t hw_w = hw.getWidth();
1778 const uint32_t hw_h = hw.getHeight();
1779 const Region screenBounds(hw.bounds());
1780
1781 GLfloat u, v;
1782 GLuint tname;
1783 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1784 if (result != NO_ERROR) {
1785 return result;
1786 }
1787
1788 GLfloat vtx[8];
1789 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1790 glEnable(GL_TEXTURE_2D);
1791 glBindTexture(GL_TEXTURE_2D, tname);
1792 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1793 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1794 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1795 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1796 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1797 glVertexPointer(2, GL_FLOAT, 0, vtx);
1798
1799 class s_curve_interpolator {
1800 const float nbFrames, s, v;
1801 public:
1802 s_curve_interpolator(int nbFrames, float s)
1803 : nbFrames(1.0f / (nbFrames-1)), s(s),
1804 v(1.0f + expf(-s + 0.5f*s)) {
1805 }
1806 float operator()(int f) {
1807 const float x = f * nbFrames;
1808 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1809 }
1810 };
1811
1812 class v_stretch {
1813 const GLfloat hw_w, hw_h;
1814 public:
1815 v_stretch(uint32_t hw_w, uint32_t hw_h)
1816 : hw_w(hw_w), hw_h(hw_h) {
1817 }
1818 void operator()(GLfloat* vtx, float v) {
1819 const GLfloat w = hw_w + (hw_w * v);
1820 const GLfloat h = hw_h - (hw_h * v);
1821 const GLfloat x = (hw_w - w) * 0.5f;
1822 const GLfloat y = (hw_h - h) * 0.5f;
1823 vtx[0] = x; vtx[1] = y;
1824 vtx[2] = x; vtx[3] = y + h;
1825 vtx[4] = x + w; vtx[5] = y + h;
1826 vtx[6] = x + w; vtx[7] = y;
1827 }
1828 };
1829
1830 class h_stretch {
1831 const GLfloat hw_w, hw_h;
1832 public:
1833 h_stretch(uint32_t hw_w, uint32_t hw_h)
1834 : hw_w(hw_w), hw_h(hw_h) {
1835 }
1836 void operator()(GLfloat* vtx, float v) {
1837 const GLfloat w = hw_w - (hw_w * v);
1838 const GLfloat h = 1.0f;
1839 const GLfloat x = (hw_w - w) * 0.5f;
1840 const GLfloat y = (hw_h - h) * 0.5f;
1841 vtx[0] = x; vtx[1] = y;
1842 vtx[2] = x; vtx[3] = y + h;
1843 vtx[4] = x + w; vtx[5] = y + h;
1844 vtx[6] = x + w; vtx[7] = y;
1845 }
1846 };
1847
1848 // the full animation is 24 frames
1849 const int nbFrames = 12;
1850 s_curve_interpolator itr(nbFrames, 7.5f);
1851 s_curve_interpolator itg(nbFrames, 8.0f);
1852 s_curve_interpolator itb(nbFrames, 8.5f);
1853
1854 v_stretch vverts(hw_w, hw_h);
1855 glEnable(GL_BLEND);
1856 glBlendFunc(GL_ONE, GL_ONE);
1857 for (int i=0 ; i<nbFrames ; i++) {
1858 float x, y, w, h;
1859 const float vr = itr(i);
1860 const float vg = itg(i);
1861 const float vb = itb(i);
1862
1863 // clear screen
1864 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001865 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001866 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001867
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001868 // draw the red plane
1869 vverts(vtx, vr);
1870 glColorMask(1,0,0,1);
1871 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001872
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001873 // draw the green plane
1874 vverts(vtx, vg);
1875 glColorMask(0,1,0,1);
1876 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001877
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001878 // draw the blue plane
1879 vverts(vtx, vb);
1880 glColorMask(0,0,1,1);
1881 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001882
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001883 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001884 glDisable(GL_TEXTURE_2D);
1885 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001886 glColor4f(vg, vg, vg, 1);
1887 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1888 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001889 }
1890
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001891 h_stretch hverts(hw_w, hw_h);
1892 glDisable(GL_BLEND);
1893 glDisable(GL_TEXTURE_2D);
1894 glColorMask(1,1,1,1);
1895 for (int i=0 ; i<nbFrames ; i++) {
1896 const float v = itg(i);
1897 hverts(vtx, v);
1898 glClear(GL_COLOR_BUFFER_BIT);
1899 glColor4f(1-v, 1-v, 1-v, 1);
1900 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1901 hw.flip(screenBounds);
1902 }
1903
1904 glColorMask(1,1,1,1);
1905 glEnable(GL_SCISSOR_TEST);
1906 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1907 glDeleteTextures(1, &tname);
1908 return NO_ERROR;
1909}
1910
1911status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1912{
1913 status_t result = PERMISSION_DENIED;
1914
1915 if (!GLExtensions::getInstance().haveFramebufferObject())
1916 return INVALID_OPERATION;
1917
1918
1919 // get screen geometry
1920 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1921 const uint32_t hw_w = hw.getWidth();
1922 const uint32_t hw_h = hw.getHeight();
1923 const Region screenBounds(hw.bounds());
1924
1925 GLfloat u, v;
1926 GLuint tname;
1927 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1928 if (result != NO_ERROR) {
1929 return result;
1930 }
1931
1932 // back to main framebuffer
1933 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1934 glDisable(GL_SCISSOR_TEST);
1935
1936 GLfloat vtx[8];
1937 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1938 glEnable(GL_TEXTURE_2D);
1939 glBindTexture(GL_TEXTURE_2D, tname);
1940 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1941 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1942 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1943 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1944 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1945 glVertexPointer(2, GL_FLOAT, 0, vtx);
1946
1947 class s_curve_interpolator {
1948 const float nbFrames, s, v;
1949 public:
1950 s_curve_interpolator(int nbFrames, float s)
1951 : nbFrames(1.0f / (nbFrames-1)), s(s),
1952 v(1.0f + expf(-s + 0.5f*s)) {
1953 }
1954 float operator()(int f) {
1955 const float x = f * nbFrames;
1956 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1957 }
1958 };
1959
1960 class v_stretch {
1961 const GLfloat hw_w, hw_h;
1962 public:
1963 v_stretch(uint32_t hw_w, uint32_t hw_h)
1964 : hw_w(hw_w), hw_h(hw_h) {
1965 }
1966 void operator()(GLfloat* vtx, float v) {
1967 const GLfloat w = hw_w + (hw_w * v);
1968 const GLfloat h = hw_h - (hw_h * v);
1969 const GLfloat x = (hw_w - w) * 0.5f;
1970 const GLfloat y = (hw_h - h) * 0.5f;
1971 vtx[0] = x; vtx[1] = y;
1972 vtx[2] = x; vtx[3] = y + h;
1973 vtx[4] = x + w; vtx[5] = y + h;
1974 vtx[6] = x + w; vtx[7] = y;
1975 }
1976 };
1977
1978 class h_stretch {
1979 const GLfloat hw_w, hw_h;
1980 public:
1981 h_stretch(uint32_t hw_w, uint32_t hw_h)
1982 : hw_w(hw_w), hw_h(hw_h) {
1983 }
1984 void operator()(GLfloat* vtx, float v) {
1985 const GLfloat w = hw_w - (hw_w * v);
1986 const GLfloat h = 1.0f;
1987 const GLfloat x = (hw_w - w) * 0.5f;
1988 const GLfloat y = (hw_h - h) * 0.5f;
1989 vtx[0] = x; vtx[1] = y;
1990 vtx[2] = x; vtx[3] = y + h;
1991 vtx[4] = x + w; vtx[5] = y + h;
1992 vtx[6] = x + w; vtx[7] = y;
1993 }
1994 };
1995
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07001996 // the full animation is 12 frames
1997 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001998 s_curve_interpolator itr(nbFrames, 7.5f);
1999 s_curve_interpolator itg(nbFrames, 8.0f);
2000 s_curve_interpolator itb(nbFrames, 8.5f);
2001
2002 h_stretch hverts(hw_w, hw_h);
2003 glDisable(GL_BLEND);
2004 glDisable(GL_TEXTURE_2D);
2005 glColorMask(1,1,1,1);
2006 for (int i=nbFrames-1 ; i>=0 ; i--) {
2007 const float v = itg(i);
2008 hverts(vtx, v);
2009 glClear(GL_COLOR_BUFFER_BIT);
2010 glColor4f(1-v, 1-v, 1-v, 1);
2011 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2012 hw.flip(screenBounds);
2013 }
2014
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002015 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002016 v_stretch vverts(hw_w, hw_h);
2017 glEnable(GL_BLEND);
2018 glBlendFunc(GL_ONE, GL_ONE);
2019 for (int i=nbFrames-1 ; i>=0 ; i--) {
2020 float x, y, w, h;
2021 const float vr = itr(i);
2022 const float vg = itg(i);
2023 const float vb = itb(i);
2024
2025 // clear screen
2026 glColorMask(1,1,1,1);
2027 glClear(GL_COLOR_BUFFER_BIT);
2028 glEnable(GL_TEXTURE_2D);
2029
2030 // draw the red plane
2031 vverts(vtx, vr);
2032 glColorMask(1,0,0,1);
2033 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2034
2035 // draw the green plane
2036 vverts(vtx, vg);
2037 glColorMask(0,1,0,1);
2038 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2039
2040 // draw the blue plane
2041 vverts(vtx, vb);
2042 glColorMask(0,0,1,1);
2043 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2044
2045 hw.flip(screenBounds);
2046 }
2047
2048 glColorMask(1,1,1,1);
2049 glEnable(GL_SCISSOR_TEST);
2050 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002051 glDeleteTextures(1, &tname);
2052
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002053 return NO_ERROR;
2054}
2055
2056// ---------------------------------------------------------------------------
2057
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002058status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002059{
2060 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2061 if (!hw.canDraw()) {
2062 // we're already off
2063 return NO_ERROR;
2064 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002065 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2066 electronBeamOffAnimationImplLocked();
2067 }
2068
2069 // always clear the whole screen at the end of the animation
2070 glClearColor(0,0,0,1);
2071 glDisable(GL_SCISSOR_TEST);
2072 glClear(GL_COLOR_BUFFER_BIT);
2073 glEnable(GL_SCISSOR_TEST);
2074 hw.flip( Region(hw.bounds()) );
2075
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002076 hw.setCanDraw(false);
2077 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002078}
2079
2080status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2081{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002082 class MessageTurnElectronBeamOff : public MessageBase {
2083 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002084 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002085 status_t result;
2086 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002087 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2088 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002089 }
2090 status_t getResult() const {
2091 return result;
2092 }
2093 virtual bool handler() {
2094 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002095 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002096 return true;
2097 }
2098 };
2099
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002100 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002101 status_t res = postMessageSync(msg);
2102 if (res == NO_ERROR) {
2103 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002104
2105 // work-around: when the power-manager calls us we activate the
2106 // animation. eventually, the "on" animation will be called
2107 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002108 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002109 }
2110 return res;
2111}
2112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002114
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002115status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002116{
2117 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2118 if (hw.canDraw()) {
2119 // we're already on
2120 return NO_ERROR;
2121 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002122 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2123 electronBeamOnAnimationImplLocked();
2124 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002125 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002126
2127 // make sure to redraw the whole screen when the animation is done
2128 mDirtyRegion.set(hw.bounds());
2129 signalEvent();
2130
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002131 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002132}
2133
2134status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2135{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002136 class MessageTurnElectronBeamOn : public MessageBase {
2137 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002138 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002139 status_t result;
2140 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002141 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2142 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002143 }
2144 status_t getResult() const {
2145 return result;
2146 }
2147 virtual bool handler() {
2148 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002149 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002150 return true;
2151 }
2152 };
2153
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002154 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002155 return NO_ERROR;
2156}
2157
2158// ---------------------------------------------------------------------------
2159
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002160status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2161 sp<IMemoryHeap>* heap,
2162 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002163 uint32_t sw, uint32_t sh,
2164 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002165{
2166 status_t result = PERMISSION_DENIED;
2167
2168 // only one display supported for now
2169 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2170 return BAD_VALUE;
2171
Jamie Gennisf72606c2011-03-09 17:05:02 -08002172 // make sure none of the layers are protected
2173 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2174 const size_t count = layers.size();
2175 for (size_t i=0 ; i<count ; ++i) {
2176 const sp<LayerBase>& layer(layers[i]);
2177 const uint32_t z = layer->drawingState().z;
2178 if (z >= minLayerZ && z <= maxLayerZ) {
2179 if (layer->isProtected()) {
2180 return INVALID_OPERATION;
2181 }
2182 }
2183 }
2184
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002185 if (!GLExtensions::getInstance().haveFramebufferObject())
2186 return INVALID_OPERATION;
2187
2188 // get screen geometry
2189 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2190 const uint32_t hw_w = hw.getWidth();
2191 const uint32_t hw_h = hw.getHeight();
2192
2193 if ((sw > hw_w) || (sh > hw_h))
2194 return BAD_VALUE;
2195
2196 sw = (!sw) ? hw_w : sw;
2197 sh = (!sh) ? hw_h : sh;
2198 const size_t size = sw * sh * 4;
2199
Mathias Agopian32ae0942011-03-02 18:45:50 -08002200 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2201 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002202
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002203 // make sure to clear all GL error flags
2204 while ( glGetError() != GL_NO_ERROR ) ;
2205
2206 // create a FBO
2207 GLuint name, tname;
2208 glGenRenderbuffersOES(1, &tname);
2209 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2210 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2211 glGenFramebuffersOES(1, &name);
2212 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2213 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2214 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2215
2216 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002217
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002218 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2219
2220 // invert everything, b/c glReadPixel() below will invert the FB
2221 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002222 glScissor(0, 0, sw, sh);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002223 glMatrixMode(GL_PROJECTION);
2224 glPushMatrix();
2225 glLoadIdentity();
2226 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2227 glMatrixMode(GL_MODELVIEW);
2228
2229 // redraw the screen entirely...
2230 glClearColor(0,0,0,1);
2231 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002232
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002233 for (size_t i=0 ; i<count ; ++i) {
2234 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002235 const uint32_t z = layer->drawingState().z;
2236 if (z >= minLayerZ && z <= maxLayerZ) {
2237 layer->drawForSreenShot();
2238 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002239 }
2240
2241 // XXX: this is needed on tegra
2242 glScissor(0, 0, sw, sh);
2243
2244 // check for errors and return screen capture
2245 if (glGetError() != GL_NO_ERROR) {
2246 // error while rendering
2247 result = INVALID_OPERATION;
2248 } else {
2249 // allocate shared memory large enough to hold the
2250 // screen capture
2251 sp<MemoryHeapBase> base(
2252 new MemoryHeapBase(size, 0, "screen-capture") );
2253 void* const ptr = base->getBase();
2254 if (ptr) {
2255 // capture the screen with glReadPixels()
2256 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2257 if (glGetError() == GL_NO_ERROR) {
2258 *heap = base;
2259 *w = sw;
2260 *h = sh;
2261 *f = PIXEL_FORMAT_RGBA_8888;
2262 result = NO_ERROR;
2263 }
2264 } else {
2265 result = NO_MEMORY;
2266 }
2267 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002268 glEnable(GL_SCISSOR_TEST);
2269 glViewport(0, 0, hw_w, hw_h);
2270 glMatrixMode(GL_PROJECTION);
2271 glPopMatrix();
2272 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002273 } else {
2274 result = BAD_VALUE;
2275 }
2276
2277 // release FBO resources
2278 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2279 glDeleteRenderbuffersOES(1, &tname);
2280 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002281
2282 hw.compositionComplete();
2283
Mathias Agopian32ae0942011-03-02 18:45:50 -08002284 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002285
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002286 return result;
2287}
2288
2289
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002290status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2291 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002292 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002293 uint32_t sw, uint32_t sh,
2294 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002295{
2296 // only one display supported for now
2297 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2298 return BAD_VALUE;
2299
2300 if (!GLExtensions::getInstance().haveFramebufferObject())
2301 return INVALID_OPERATION;
2302
2303 class MessageCaptureScreen : public MessageBase {
2304 SurfaceFlinger* flinger;
2305 DisplayID dpy;
2306 sp<IMemoryHeap>* heap;
2307 uint32_t* w;
2308 uint32_t* h;
2309 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002310 uint32_t sw;
2311 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002312 uint32_t minLayerZ;
2313 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002314 status_t result;
2315 public:
2316 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002317 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002318 uint32_t sw, uint32_t sh,
2319 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002320 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002321 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2322 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2323 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002324 {
2325 }
2326 status_t getResult() const {
2327 return result;
2328 }
2329 virtual bool handler() {
2330 Mutex::Autolock _l(flinger->mStateLock);
2331
2332 // if we have secure windows, never allow the screen capture
2333 if (flinger->mSecureFrameBuffer)
2334 return true;
2335
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002336 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002337 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002338
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002339 return true;
2340 }
2341 };
2342
2343 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002344 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002345 status_t res = postMessageSync(msg);
2346 if (res == NO_ERROR) {
2347 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2348 }
2349 return res;
2350}
2351
2352// ---------------------------------------------------------------------------
2353
Mathias Agopian7623da42010-06-01 15:12:58 -07002354sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2355{
2356 sp<Layer> result;
2357 Mutex::Autolock _l(mStateLock);
2358 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2359 return result;
2360}
2361
2362// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363
Mathias Agopian593c05c2010-06-02 23:28:45 -07002364Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002365 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367}
2368
Mathias Agopian593c05c2010-06-02 23:28:45 -07002369Client::~Client()
2370{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002371 const size_t count = mLayers.size();
2372 for (size_t i=0 ; i<count ; i++) {
2373 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2374 if (layer != 0) {
2375 mFlinger->removeLayer(layer);
2376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 }
2378}
2379
Mathias Agopian593c05c2010-06-02 23:28:45 -07002380status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002381 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382}
Mathias Agopian1473f462009-04-10 14:24:30 -07002383
Mathias Agopian593c05c2010-06-02 23:28:45 -07002384ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2385{
Mathias Agopian7623da42010-06-01 15:12:58 -07002386 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002387 mLayers.add(name, layer);
2388 return name;
2389}
2390
Mathias Agopian7623da42010-06-01 15:12:58 -07002391void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002392{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002393 // we do a linear search here, because this doesn't happen often
2394 const size_t count = mLayers.size();
2395 for (size_t i=0 ; i<count ; i++) {
2396 if (mLayers.valueAt(i) == layer) {
2397 mLayers.removeItemsAt(i, 1);
2398 break;
2399 }
2400 }
2401}
Mathias Agopian1473f462009-04-10 14:24:30 -07002402sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2403 sp<LayerBaseClient> lbc;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002404 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2405 if (layer != 0) {
2406 lbc = layer.promote();
2407 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002408 }
2409 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410}
2411
Mathias Agopian593c05c2010-06-02 23:28:45 -07002412sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002413 return 0;
2414}
2415ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2416 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002418sp<ISurface> Client::createSurface(
Mathias Agopian7623da42010-06-01 15:12:58 -07002419 ISurfaceComposerClient::surface_data_t* params, int pid,
2420 const String8& name,
2421 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 uint32_t flags)
2423{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002424 return mFlinger->createSurface(this, pid, name, params,
2425 display, w, h, format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002427status_t Client::destroySurface(SurfaceID sid) {
2428 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002430status_t Client::setState(int32_t count, const layer_state_t* states) {
2431 return mFlinger->setClientState(this, count, states);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432}
2433
2434// ---------------------------------------------------------------------------
2435
Mathias Agopian7623da42010-06-01 15:12:58 -07002436UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2437 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2438{
2439 const int pgsize = getpagesize();
2440 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2441
2442 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2443 "SurfaceFlinger Client control-block");
2444
2445 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2446 if (ctrlblk) { // construct the shared structure in-place.
2447 new(ctrlblk) SharedClient;
2448 }
2449}
2450
2451UserClient::~UserClient()
2452{
2453 if (ctrlblk) {
2454 ctrlblk->~SharedClient(); // destroy our shared-structure.
2455 }
2456
2457 /*
2458 * When a UserClient dies, it's unclear what to do exactly.
2459 * We could go ahead and destroy all surfaces linked to that client
2460 * however, it wouldn't be fair to the main Client
2461 * (usually the the window-manager), which might want to re-target
2462 * the layer to another UserClient.
2463 * I think the best is to do nothing, or not much; in most cases the
2464 * WM itself will go ahead and clean things up when it detects a client of
2465 * his has died.
2466 * The remaining question is what to display? currently we keep
2467 * just keep the current buffer.
2468 */
2469}
2470
2471status_t UserClient::initCheck() const {
2472 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2473}
2474
2475void UserClient::detachLayer(const Layer* layer)
2476{
2477 int32_t name = layer->getToken();
2478 if (name >= 0) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002479 int32_t mask = 1LU<<name;
2480 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2481 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2482 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002483 }
2484}
2485
2486sp<IMemoryHeap> UserClient::getControlBlock() const {
2487 return mCblkHeap;
2488}
2489
2490ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2491{
2492 int32_t name = NAME_NOT_FOUND;
2493 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002494 if (layer == 0) {
2495 return name;
2496 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002497
Mathias Agopian5e140102010-06-08 19:54:15 -07002498 // if this layer already has a token, just return it
Mathias Agopian7623da42010-06-01 15:12:58 -07002499 name = layer->getToken();
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002500 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002501 return name;
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002502 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002503
2504 name = 0;
2505 do {
2506 int32_t mask = 1LU<<name;
2507 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2508 // we found and locked that name
Mathias Agopian5e140102010-06-08 19:54:15 -07002509 status_t err = layer->setToken(
2510 const_cast<UserClient*>(this), ctrlblk, name);
2511 if (err != NO_ERROR) {
2512 // free the name
2513 android_atomic_and(~mask, &mBitmap);
2514 name = err;
2515 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002516 break;
2517 }
Jamie Gennisc86d9042011-02-10 16:18:36 -08002518 if (++name >= SharedBufferStack::NUM_LAYERS_MAX)
Mathias Agopian7623da42010-06-01 15:12:58 -07002519 name = NO_MEMORY;
2520 } while(name >= 0);
2521
Mathias Agopian1debc662010-06-08 15:40:56 -07002522 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2523 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopian7623da42010-06-01 15:12:58 -07002524 return name;
2525}
2526
2527sp<ISurface> UserClient::createSurface(
2528 ISurfaceComposerClient::surface_data_t* params, int pid,
2529 const String8& name,
2530 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2531 uint32_t flags) {
2532 return 0;
2533}
2534status_t UserClient::destroySurface(SurfaceID sid) {
2535 return INVALID_OPERATION;
2536}
2537status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2538 return INVALID_OPERATION;
2539}
2540
2541// ---------------------------------------------------------------------------
2542
Jamie Gennisf7acf162011-01-12 18:30:40 -08002543GraphicBufferAlloc::GraphicBufferAlloc() {}
2544
2545GraphicBufferAlloc::~GraphicBufferAlloc() {}
2546
2547sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2548 PixelFormat format, uint32_t usage) {
2549 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2550 status_t err = graphicBuffer->initCheck();
2551 if (err != 0) {
2552 LOGE("createGraphicBuffer: init check failed: %d", err);
2553 return 0;
2554 } else if (graphicBuffer->handle == 0) {
2555 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2556 return 0;
2557 }
2558 Mutex::Autolock _l(mLock);
2559 mBuffers.add(graphicBuffer);
2560 return graphicBuffer;
2561}
2562
2563void GraphicBufferAlloc::freeAllGraphicBuffersExcept(int bufIdx) {
2564 Mutex::Autolock _l(mLock);
2565 if (0 <= bufIdx && bufIdx < mBuffers.size()) {
2566 sp<GraphicBuffer> b(mBuffers[bufIdx]);
2567 mBuffers.clear();
2568 mBuffers.add(b);
2569 } else {
2570 mBuffers.clear();
2571 }
2572}
2573
2574// ---------------------------------------------------------------------------
2575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576GraphicPlane::GraphicPlane()
2577 : mHw(0)
2578{
2579}
2580
2581GraphicPlane::~GraphicPlane() {
2582 delete mHw;
2583}
2584
2585bool GraphicPlane::initialized() const {
2586 return mHw ? true : false;
2587}
2588
Mathias Agopian66c77a52010-02-08 15:49:35 -08002589int GraphicPlane::getWidth() const {
2590 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591}
2592
Mathias Agopian66c77a52010-02-08 15:49:35 -08002593int GraphicPlane::getHeight() const {
2594 return mHeight;
2595}
2596
2597void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2598{
2599 mHw = hw;
2600
2601 // initialize the display orientation transform.
2602 // it's a constant that should come from the display driver.
2603 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2604 char property[PROPERTY_VALUE_MAX];
2605 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2606 //displayOrientation
2607 switch (atoi(property)) {
2608 case 90:
2609 displayOrientation = ISurfaceComposer::eOrientation90;
2610 break;
2611 case 270:
2612 displayOrientation = ISurfaceComposer::eOrientation270;
2613 break;
2614 }
2615 }
2616
2617 const float w = hw->getWidth();
2618 const float h = hw->getHeight();
2619 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2620 &mDisplayTransform);
2621 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2622 mDisplayWidth = h;
2623 mDisplayHeight = w;
2624 } else {
2625 mDisplayWidth = w;
2626 mDisplayHeight = h;
2627 }
2628
2629 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630}
2631
2632status_t GraphicPlane::orientationToTransfrom(
2633 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002634{
2635 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 switch (orientation) {
2637 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002638 flags = Transform::ROT_0;
2639 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002641 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 break;
2643 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002644 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 break;
2646 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002647 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 break;
2649 default:
2650 return BAD_VALUE;
2651 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002652 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 return NO_ERROR;
2654}
2655
2656status_t GraphicPlane::setOrientation(int orientation)
2657{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 // If the rotation can be handled in hardware, this is where
2659 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002660
2661 const DisplayHardware& hw(displayHardware());
2662 const float w = mDisplayWidth;
2663 const float h = mDisplayHeight;
2664 mWidth = int(w);
2665 mHeight = int(h);
2666
2667 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002668 GraphicPlane::orientationToTransfrom(orientation, w, h,
2669 &orientationTransform);
2670 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2671 mWidth = int(h);
2672 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002674
Mathias Agopian3552f532009-03-27 17:58:20 -07002675 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002676 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 return NO_ERROR;
2678}
2679
2680const DisplayHardware& GraphicPlane::displayHardware() const {
2681 return *mHw;
2682}
2683
Mathias Agopianaab758e2010-10-11 12:37:43 -07002684DisplayHardware& GraphicPlane::editDisplayHardware() {
2685 return *mHw;
2686}
2687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688const Transform& GraphicPlane::transform() const {
2689 return mGlobalTransform;
2690}
2691
Mathias Agopian1473f462009-04-10 14:24:30 -07002692EGLDisplay GraphicPlane::getEGLDisplay() const {
2693 return mHw->getEGLDisplay();
2694}
2695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696// ---------------------------------------------------------------------------
2697
2698}; // namespace android