blob: e8f0328b3a45bd9a8d6995d9366d788673b0128d [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;
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700398 uint32_t transactionFlags = peekTransactionFlags(mask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 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 Agopian6dcb1552011-05-03 17:04:02 -0700493
494 // Here we're guaranteed that some transaction flags are set
495 // so we can call handleTransactionLocked() unconditionally.
496 // We call getTransactionFlags(), which will also clear the flags,
497 // with mStateLock held to guarantee that mCurrentState won't change
498 // until the transaction is commited.
499
500 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
501 transactionFlags = getTransactionFlags(mask);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700502 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700503
Mathias Agopiana8d49172009-08-26 16:36:26 -0700504 mLastTransactionTime = systemTime() - now;
505 mDebugInTransaction = 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800506 invalidateHwcGeometry();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700507 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700508 }
509
Mathias Agopianff1d4102010-08-10 17:19:56 -0700510 /*
511 * Clean-up all layers that went away
512 * (do this without the lock held)
513 */
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700514
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700515 const size_t count = ditchedLayers.size();
516 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianffae4fc2009-09-04 19:50:23 -0700517 if (ditchedLayers[i] != 0) {
518 //LOGD("ditching layer %p", ditchedLayers[i].get());
519 ditchedLayers[i]->ditch();
520 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700521 }
522}
523
524void SurfaceFlinger::handleTransactionLocked(
525 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
526{
527 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 const size_t count = currentLayers.size();
529
530 /*
531 * Traversal of the children
532 * (perform the transaction for each of them if needed)
533 */
534
535 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
536 if (layersNeedTransaction) {
537 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700538 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
540 if (!trFlags) continue;
541
542 const uint32_t flags = layer->doTransaction(0);
543 if (flags & Layer::eVisibleRegion)
544 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
546 }
547
548 /*
549 * Perform our own transaction if needed
550 */
551
552 if (transactionFlags & eTransactionNeeded) {
553 if (mCurrentState.orientation != mDrawingState.orientation) {
554 // the orientation has changed, recompute all visible regions
555 // and invalidate everything.
556
557 const int dpy = 0;
558 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700559 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 GraphicPlane& plane(graphicPlane(dpy));
561 plane.setOrientation(orientation);
562
563 // update the shared control block
564 const DisplayHardware& hw(plane.displayHardware());
565 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
566 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800567 dcblk->w = plane.getWidth();
568 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
570 mVisibleRegionsDirty = true;
571 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573
574 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
575 // freezing or unfreezing the display -> trigger animation if needed
576 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800577 if (mFreezeDisplay)
578 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700581 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
582 // layers have been added
583 mVisibleRegionsDirty = true;
584 }
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 // some layers might have been removed, so
587 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700588 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700589 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700591 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700592 const size_t count = previousLayers.size();
593 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700594 const sp<LayerBase>& layer(previousLayers[i]);
595 if (currentLayers.indexOf( layer ) < 0) {
596 // this layer is not visible anymore
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700597 ditchedLayers.add(layer);
Mathias Agopian33863dd2009-07-28 14:20:21 -0700598 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700599 }
600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603
604 commitTransaction();
605}
606
607sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
608{
609 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
610}
611
612void SurfaceFlinger::computeVisibleRegions(
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800613 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614{
615 const GraphicPlane& plane(graphicPlane(0));
616 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700617 const DisplayHardware& hw(plane.displayHardware());
618 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
620 Region aboveOpaqueLayers;
621 Region aboveCoveredLayers;
622 Region dirty;
623
624 bool secureFrameBuffer = false;
625
626 size_t i = currentLayers.size();
627 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700628 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 layer->validateVisibility(planeTransform);
630
631 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700632 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700634 /*
635 * opaqueRegion: area of a surface that is fully opaque.
636 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700638
639 /*
640 * visibleRegion: area of a surface that is visible on screen
641 * and not fully transparent. This is essentially the layer's
642 * footprint minus the opaque regions above it.
643 * Areas covered by a translucent surface are considered visible.
644 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700646
647 /*
648 * coveredRegion: area of a surface that is covered by all
649 * visible regions above it (which includes the translucent areas).
650 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700652
653
654 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700655 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 const bool translucent = layer->needsBlending();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700657 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700659 visibleRegion.andSelf(screenRegion);
660 if (!visibleRegion.isEmpty()) {
661 // Remove the transparent area from the visible region
662 if (translucent) {
663 visibleRegion.subtractSelf(layer->transparentRegionScreen);
664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700666 // compute the opaque region
667 const int32_t layerOrientation = layer->getOrientation();
668 if (s.alpha==255 && !translucent &&
669 ((layerOrientation & Transform::ROT_INVALID) == false)) {
670 // the opaque region is the layer's footprint
671 opaqueRegion = visibleRegion;
672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674 }
675
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700676 // Clip the covered region to the visible region
677 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
678
679 // Update aboveCoveredLayers for next (lower) layer
680 aboveCoveredLayers.orSelf(visibleRegion);
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 // subtract the opaque region covered by the layers above us
683 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684
685 // compute this layer's dirty region
686 if (layer->contentDirty) {
687 // we need to invalidate the whole region
688 dirty = visibleRegion;
689 // as well, as the old visible region
690 dirty.orSelf(layer->visibleRegionScreen);
691 layer->contentDirty = false;
692 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700693 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700694 * the exposed region consists of two components:
695 * 1) what's VISIBLE now and was COVERED before
696 * 2) what's EXPOSED now less what was EXPOSED before
697 *
698 * note that (1) is conservative, we start with the whole
699 * visible region but only keep what used to be covered by
700 * something -- which mean it may have been exposed.
701 *
702 * (2) handles areas that were not covered by anything but got
703 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700704 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700705 const Region newExposed = visibleRegion - coveredRegion;
706 const Region oldVisibleRegion = layer->visibleRegionScreen;
707 const Region oldCoveredRegion = layer->coveredRegionScreen;
708 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
709 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 dirty.subtractSelf(aboveOpaqueLayers);
712
713 // accumulate to the screen dirty region
714 dirtyRegion.orSelf(dirty);
715
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700716 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 // Store the visible region is screen space
720 layer->setVisibleRegion(visibleRegion);
721 layer->setCoveredRegion(coveredRegion);
722
Mathias Agopian12cedff2009-07-28 10:57:27 -0700723 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 if (layer->isSecure() && !visibleRegion.isEmpty()) {
725 secureFrameBuffer = true;
726 }
727 }
728
Mathias Agopian12cedff2009-07-28 10:57:27 -0700729 // invalidate the areas where a layer was removed
730 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
731 mDirtyRegionRemovedLayer.clear();
732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 mSecureFrameBuffer = secureFrameBuffer;
734 opaqueRegion = aboveOpaqueLayers;
735}
736
737
738void SurfaceFlinger::commitTransaction()
739{
740 mDrawingState = mCurrentState;
Mathias Agopian9779b222009-09-07 16:32:45 -0700741 mResizeTransationPending = false;
742 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743}
744
745void SurfaceFlinger::handlePageFlip()
746{
747 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800748 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 visibleRegions |= lockPageFlip(currentLayers);
750
751 const DisplayHardware& hw = graphicPlane(0).displayHardware();
752 const Region screenRegion(hw.bounds());
753 if (visibleRegions) {
754 Region opaqueRegion;
755 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700756
757 /*
758 * rebuild the visible layer list
759 */
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800760 const size_t count = currentLayers.size();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700761 mVisibleLayersSortedByZ.clear();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700762 mVisibleLayersSortedByZ.setCapacity(count);
763 for (size_t i=0 ; i<count ; i++) {
764 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
765 mVisibleLayersSortedByZ.add(currentLayers[i]);
766 }
767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 mWormholeRegion = screenRegion.subtract(opaqueRegion);
769 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800770 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772
773 unlockPageFlip(currentLayers);
774 mDirtyRegion.andSelf(screenRegion);
775}
776
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800777void SurfaceFlinger::invalidateHwcGeometry()
778{
779 mHwWorkListDirty = true;
780}
781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
783{
784 bool recomputeVisibleRegions = false;
785 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700786 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700788 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 layer->lockPageFlip(recomputeVisibleRegions);
790 }
791 return recomputeVisibleRegions;
792}
793
794void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
795{
796 const GraphicPlane& plane(graphicPlane(0));
797 const Transform& planeTransform(plane.transform());
798 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700799 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700801 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 layer->unlockPageFlip(planeTransform, mDirtyRegion);
803 }
804}
805
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700806void SurfaceFlinger::handleWorkList()
807{
808 mHwWorkListDirty = false;
809 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
810 if (hwc.initCheck() == NO_ERROR) {
811 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
812 const size_t count = currentLayers.size();
813 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700814 hwc_layer_t* const cur(hwc.getLayers());
815 for (size_t i=0 ; cur && i<count ; i++) {
816 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian6a969242010-09-22 18:58:01 -0700817 if (mDebugDisableHWC) {
818 cur[i].compositionType = HWC_FRAMEBUFFER;
819 cur[i].flags |= HWC_SKIP_LAYER;
820 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700821 }
822 }
823}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825void SurfaceFlinger::handleRepaint()
826{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700827 // compute the invalid region
828 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
830 if (UNLIKELY(mDebugRegion)) {
831 debugFlashRegions();
832 }
833
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700834 // set the frame buffer
835 const DisplayHardware& hw(graphicPlane(0).displayHardware());
836 glMatrixMode(GL_MODELVIEW);
837 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838
839 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700840 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
841 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700842 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700843 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
844 // takes a rectangle, we must make sure to update that whole
845 // rectangle in that case
846 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700847 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700848 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 mDirtyRegion.set(mInvalidRegion.bounds());
850 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700851 // in the BUFFER_PRESERVED case, obviously, we can update only
852 // what's needed and nothing more.
853 // NOTE: this is NOT a common case, as preserving the backbuffer
854 // is costly and usually involves copying the whole update back.
855 }
856 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700857 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700858 // We need to redraw the rectangle that will be updated
859 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700860 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700861 // rectangle instead of a region (see DisplayHardware::flip())
862 mDirtyRegion.set(mInvalidRegion.bounds());
863 } else {
864 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 mDirtyRegion.set(hw.bounds());
866 mInvalidRegion = mDirtyRegion;
867 }
868 }
869
870 // compose all surfaces
871 composeSurfaces(mDirtyRegion);
872
873 // clear the dirty regions
874 mDirtyRegion.clear();
875}
876
877void SurfaceFlinger::composeSurfaces(const Region& dirty)
878{
879 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
880 // should never happen unless the window manager has a bug
881 // draw something...
882 drawWormhole();
883 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700884
885 status_t err = NO_ERROR;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700886 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700887 size_t count = layers.size();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700888
889 const DisplayHardware& hw(graphicPlane(0).displayHardware());
890 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700891 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700892
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700893 LOGE_IF(cur && hwc.getNumLayers() != count,
894 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
895 hwc.getNumLayers(), count);
896
897 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700898 if (hwc.initCheck() == NO_ERROR) {
899 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
900 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700901
902 /*
903 * update the per-frame h/w composer data for each layer
904 * and build the transparent region of the FB
905 */
906 Region transparent;
907 if (cur) {
908 for (size_t i=0 ; i<count ; i++) {
909 const sp<LayerBase>& layer(layers[i]);
910 layer->setPerFrameData(&cur[i]);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700911 }
912 err = hwc.prepare();
913 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700914
Mathias Agopian45491692011-01-19 15:24:23 -0800915 if (err == NO_ERROR) {
916 for (size_t i=0 ; i<count ; i++) {
917 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
918 const sp<LayerBase>& layer(layers[i]);
919 if (!(layer->needsBlending())) {
920 transparent.orSelf(layer->visibleRegionScreen);
921 }
922 }
923 }
924
925 /*
926 * clear the area of the FB that need to be transparent
927 */
928 transparent.andSelf(dirty);
929 if (!transparent.isEmpty()) {
930 glClearColor(0,0,0,0);
931 Region::const_iterator it = transparent.begin();
932 Region::const_iterator const end = transparent.end();
933 const int32_t height = hw.getHeight();
934 while (it != end) {
935 const Rect& r(*it++);
936 const GLint sy = height - (r.top + r.height());
937 glScissor(r.left, sy, r.width(), r.height());
938 glClear(GL_COLOR_BUFFER_BIT);
939 }
940 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700941 }
942 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700943
944
945 /*
946 * and then, render the layers targeted at the framebuffer
947 */
948 for (size_t i=0 ; i<count ; i++) {
949 if (cur) {
Antti Hatala982f58b2010-09-09 02:32:30 -0700950 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
951 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700952 // skip layers handled by the HAL
953 continue;
954 }
955 }
Mathias Agopian6a969242010-09-22 18:58:01 -0700956
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700957 const sp<LayerBase>& layer(layers[i]);
958 const Region clip(dirty.intersect(layer->visibleRegionScreen));
959 if (!clip.isEmpty()) {
960 layer->draw(clip);
961 }
962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963}
964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965void SurfaceFlinger::debugFlashRegions()
966{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700967 const DisplayHardware& hw(graphicPlane(0).displayHardware());
968 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700969
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700970 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
971 (flags & DisplayHardware::BUFFER_PRESERVED))) {
972 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
973 mDirtyRegion.bounds() : hw.bounds());
974 composeSurfaces(repaint);
975 }
976
977 TextureManager::deactivateTextures();
978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 glDisable(GL_BLEND);
980 glDisable(GL_DITHER);
981 glDisable(GL_SCISSOR_TEST);
982
Mathias Agopiandff8e582009-05-04 14:17:04 -0700983 static int toggle = 0;
984 toggle = 1 - toggle;
985 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700986 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700987 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700988 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700991 Region::const_iterator it = mDirtyRegion.begin();
992 Region::const_iterator const end = mDirtyRegion.end();
993 while (it != end) {
994 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 GLfloat vertices[][2] = {
996 { r.left, r.top },
997 { r.left, r.bottom },
998 { r.right, r.bottom },
999 { r.right, r.top }
1000 };
1001 glVertexPointer(2, GL_FLOAT, 0, vertices);
1002 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1003 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001004
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001005 if (mInvalidRegion.isEmpty()) {
1006 mDirtyRegion.dump("mDirtyRegion");
1007 mInvalidRegion.dump("mInvalidRegion");
1008 }
1009 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010
1011 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001012 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013
1014 glEnable(GL_SCISSOR_TEST);
1015 //mDirtyRegion.dump("mDirtyRegion");
1016}
1017
1018void SurfaceFlinger::drawWormhole() const
1019{
1020 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1021 if (region.isEmpty())
1022 return;
1023
1024 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1025 const int32_t width = hw.getWidth();
1026 const int32_t height = hw.getHeight();
1027
1028 glDisable(GL_BLEND);
1029 glDisable(GL_DITHER);
1030
1031 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001032 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001033 Region::const_iterator it = region.begin();
1034 Region::const_iterator const end = region.end();
1035 while (it != end) {
1036 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 const GLint sy = height - (r.top + r.height());
1038 glScissor(r.left, sy, r.width(), r.height());
1039 glClear(GL_COLOR_BUFFER_BIT);
1040 }
1041 } else {
1042 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1043 { width, height }, { 0, height } };
1044 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1045 glVertexPointer(2, GL_SHORT, 0, vertices);
1046 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1047 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001048#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001049 if (GLExtensions::getInstance().haveTextureExternal()) {
1050 glDisable(GL_TEXTURE_EXTERNAL_OES);
1051 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001052#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 glEnable(GL_TEXTURE_2D);
1054 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1055 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1056 glMatrixMode(GL_TEXTURE);
1057 glLoadIdentity();
1058 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001059 Region::const_iterator it = region.begin();
1060 Region::const_iterator const end = region.end();
1061 while (it != end) {
1062 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 const GLint sy = height - (r.top + r.height());
1064 glScissor(r.left, sy, r.width(), r.height());
1065 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1066 }
1067 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001068 glLoadIdentity();
1069 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071}
1072
1073void SurfaceFlinger::debugShowFPS() const
1074{
1075 static int mFrameCount;
1076 static int mLastFrameCount = 0;
1077 static nsecs_t mLastFpsTime = 0;
1078 static float mFps = 0;
1079 mFrameCount++;
1080 nsecs_t now = systemTime();
1081 nsecs_t diff = now - mLastFpsTime;
1082 if (diff > ms2ns(250)) {
1083 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1084 mLastFpsTime = now;
1085 mLastFrameCount = mFrameCount;
1086 }
1087 // XXX: mFPS has the value we want
1088 }
1089
Mathias Agopian1473f462009-04-10 14:24:30 -07001090status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091{
1092 Mutex::Autolock _l(mStateLock);
1093 addLayer_l(layer);
1094 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1095 return NO_ERROR;
1096}
1097
Mathias Agopian593c05c2010-06-02 23:28:45 -07001098status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1099{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001100 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001101 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1102}
1103
1104ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1105 const sp<LayerBaseClient>& lbc)
1106{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001107 // attach this layer to the client
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001108 size_t name = client->attachLayer(lbc);
1109
1110 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001111
1112 // add this layer to the current state list
1113 addLayer_l(lbc);
1114
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001115 return ssize_t(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001116}
1117
Mathias Agopian1473f462009-04-10 14:24:30 -07001118status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119{
1120 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001121 status_t err = purgatorizeLayer_l(layer);
1122 if (err == NO_ERROR)
1123 setTransactionFlags(eTransactionNeeded);
1124 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125}
1126
Mathias Agopian1473f462009-04-10 14:24:30 -07001127status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128{
Mathias Agopian7623da42010-06-01 15:12:58 -07001129 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1130 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001131 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1134 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001135 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 return NO_ERROR;
1137 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001138 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139}
1140
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001141status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1142{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001143 // First add the layer to the purgatory list, which makes sure it won't
1144 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001145 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001146 if (err >= 0) {
1147 mLayerPurgatory.add(layerBase);
1148 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001149
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001150 layerBase->onRemoved();
1151
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001152 // it's possible that we don't find a layer, because it might
1153 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001154 // from the user because there is a race between Client::destroySurface(),
1155 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001156 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1157}
1158
Mathias Agopian593c05c2010-06-02 23:28:45 -07001159status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001161 layer->forceVisibilityTransaction();
1162 setTransactionFlags(eTraversalNeeded);
1163 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164}
1165
Mathias Agopian6dcb1552011-05-03 17:04:02 -07001166uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1167{
1168 return android_atomic_release_load(&mTransactionFlags);
1169}
1170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1172{
1173 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1174}
1175
Mathias Agopian898c4c92010-05-18 17:06:55 -07001176uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177{
1178 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1179 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001180 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182 return old;
1183}
1184
1185void SurfaceFlinger::openGlobalTransaction()
1186{
1187 android_atomic_inc(&mTransactionCount);
1188}
1189
1190void SurfaceFlinger::closeGlobalTransaction()
1191{
1192 if (android_atomic_dec(&mTransactionCount) == 1) {
1193 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001194
Andreas Hubere3c01832010-08-16 08:49:37 -07001195 // if there is a transaction with a resize, wait for it to
Mathias Agopian9779b222009-09-07 16:32:45 -07001196 // take effect before returning.
1197 Mutex::Autolock _l(mStateLock);
1198 while (mResizeTransationPending) {
Mathias Agopian98a9c562009-09-30 14:42:13 -07001199 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1200 if (CC_UNLIKELY(err != NO_ERROR)) {
1201 // just in case something goes wrong in SF, return to the
1202 // called after a few seconds.
1203 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1204 mResizeTransationPending = false;
1205 break;
1206 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
1209}
1210
1211status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1212{
1213 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1214 return BAD_VALUE;
1215
1216 Mutex::Autolock _l(mStateLock);
1217 mCurrentState.freezeDisplay = 1;
1218 setTransactionFlags(eTransactionNeeded);
1219
1220 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001221 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 return NO_ERROR;
1223}
1224
1225status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1226{
1227 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1228 return BAD_VALUE;
1229
1230 Mutex::Autolock _l(mStateLock);
1231 mCurrentState.freezeDisplay = 0;
1232 setTransactionFlags(eTransactionNeeded);
1233
1234 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001235 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 return NO_ERROR;
1237}
1238
Andreas Hubere3c01832010-08-16 08:49:37 -07001239int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001240 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241{
1242 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1243 return BAD_VALUE;
1244
1245 Mutex::Autolock _l(mStateLock);
1246 if (mCurrentState.orientation != orientation) {
1247 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001248 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 mCurrentState.orientation = orientation;
1250 setTransactionFlags(eTransactionNeeded);
1251 mTransactionCV.wait(mStateLock);
1252 } else {
1253 orientation = BAD_VALUE;
1254 }
1255 }
1256 return orientation;
1257}
1258
Mathias Agopian9638e5c2011-04-20 14:19:32 -07001259sp<ISurface> SurfaceFlinger::createSurface(
1260 ISurfaceComposerClient::surface_data_t* params,
1261 const String8& name,
1262 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1264 uint32_t flags)
1265{
Mathias Agopian1473f462009-04-10 14:24:30 -07001266 sp<LayerBaseClient> layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001268
1269 if (int32_t(w|h) < 0) {
1270 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1271 int(w), int(h));
1272 return surfaceHandle;
1273 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001276 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 switch (flags & eFXSurfaceMask) {
1278 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001279 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1280 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 break;
1282 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001283 // for now we treat Blur as Dim, until we can implement it
1284 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001286 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 break;
1288 }
1289
Mathias Agopian1473f462009-04-10 14:24:30 -07001290 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001291 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001292 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001293 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001296 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001297 params->token = token;
Mathias Agopian18b6b492009-08-19 17:46:26 -07001298 params->identity = surfaceHandle->getIdentity();
1299 params->width = w;
1300 params->height = h;
1301 params->format = format;
Mathias Agopian7623da42010-06-01 15:12:58 -07001302 if (normalLayer != 0) {
1303 Mutex::Autolock _l(mStateLock);
1304 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1305 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001306 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001307
Mathias Agopian593c05c2010-06-02 23:28:45 -07001308 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310
1311 return surfaceHandle;
1312}
1313
Mathias Agopian7623da42010-06-01 15:12:58 -07001314sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001315 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001316 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001317 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318{
1319 // initialize the surfaces
1320 switch (format) { // TODO: take h/w into account
1321 case PIXEL_FORMAT_TRANSPARENT:
1322 case PIXEL_FORMAT_TRANSLUCENT:
1323 format = PIXEL_FORMAT_RGBA_8888;
1324 break;
1325 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001326#ifdef NO_RGBX_8888
1327 format = PIXEL_FORMAT_RGB_565;
1328#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001329 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001330#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 break;
1332 }
1333
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001334#ifdef NO_RGBX_8888
1335 if (format == PIXEL_FORMAT_RGBX_8888)
1336 format = PIXEL_FORMAT_RGBA_8888;
1337#endif
1338
Mathias Agopian593c05c2010-06-02 23:28:45 -07001339 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001340 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001341 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001343 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 }
1345 return layer;
1346}
1347
Mathias Agopian7623da42010-06-01 15:12:58 -07001348sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001349 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001350 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001352 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 return layer;
1355}
1356
Mathias Agopian593c05c2010-06-02 23:28:45 -07001357status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001359 /*
1360 * called by the window manager, when a surface should be marked for
1361 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001362 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001363 * The surface is removed from the current and drawing lists, but placed
1364 * in the purgatory queue, so it's not destroyed right-away (we need
1365 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001366 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001367
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001368 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001369 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001370 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001371 if (layer != 0) {
1372 err = purgatorizeLayer_l(layer);
1373 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001374 setTransactionFlags(eTransactionNeeded);
1375 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001376 }
1377 return err;
1378}
1379
1380status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1381{
Mathias Agopian359140c2009-07-02 17:33:40 -07001382 // called by ~ISurface() when all references are gone
Andreas Hubere3c01832010-08-16 08:49:37 -07001383
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001384 class MessageDestroySurface : public MessageBase {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001385 SurfaceFlinger* flinger;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001386 sp<LayerBaseClient> layer;
1387 public:
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001388 MessageDestroySurface(
1389 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1390 : flinger(flinger), layer(layer) { }
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001391 virtual bool handler() {
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001392 sp<LayerBaseClient> l(layer);
1393 layer.clear(); // clear it outside of the lock;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001394 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian359140c2009-07-02 17:33:40 -07001395 /*
Andreas Hubere3c01832010-08-16 08:49:37 -07001396 * remove the layer from the current list -- chances are that it's
1397 * not in the list anyway, because it should have been removed
1398 * already upon request of the client (eg: window manager).
Mathias Agopian359140c2009-07-02 17:33:40 -07001399 * However, a buggy client could have not done that.
1400 * Since we know we don't have any more clients, we don't need
1401 * to use the purgatory.
1402 */
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001403 status_t err = flinger->removeLayer_l(l);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001404 if (err == NAME_NOT_FOUND) {
1405 // The surface wasn't in the current list, which means it was
1406 // removed already, which means it is in the purgatory,
1407 // and need to be removed from there.
1408 // This needs to happen from the main thread since its dtor
1409 // must run from there (b/c of OpenGL ES). Additionally, we
1410 // can't really acquire our internal lock from
1411 // destroySurface() -- see postMessage() below.
1412 ssize_t idx = flinger->mLayerPurgatory.remove(l);
1413 LOGE_IF(idx < 0,
1414 "layer=%p is not in the purgatory list", l.get());
1415 }
1416
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001417 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1418 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001419 return true;
1420 }
1421 };
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001422
Mathias Agopian898c4c92010-05-18 17:06:55 -07001423 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 return NO_ERROR;
1425}
1426
1427status_t SurfaceFlinger::setClientState(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001428 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 int32_t count,
1430 const layer_state_t* states)
1431{
1432 Mutex::Autolock _l(mStateLock);
1433 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 for (int i=0 ; i<count ; i++) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001435 const layer_state_t& s(states[i]);
1436 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian1473f462009-04-10 14:24:30 -07001437 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 const uint32_t what = s.what;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 if (what & ePositionChanged) {
1440 if (layer->setPosition(s.x, s.y))
1441 flags |= eTraversalNeeded;
1442 }
1443 if (what & eLayerChanged) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001444 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 if (layer->setLayer(s.z)) {
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001446 mCurrentState.layersSortedByZ.removeAt(idx);
1447 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 // we need traversal (state changed)
1449 // AND transaction (list changed)
1450 flags |= eTransactionNeeded|eTraversalNeeded;
1451 }
1452 }
1453 if (what & eSizeChanged) {
Mathias Agopian9779b222009-09-07 16:32:45 -07001454 if (layer->setSize(s.w, s.h)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 flags |= eTraversalNeeded;
Mathias Agopian9779b222009-09-07 16:32:45 -07001456 mResizeTransationPending = true;
1457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
1459 if (what & eAlphaChanged) {
1460 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1461 flags |= eTraversalNeeded;
1462 }
1463 if (what & eMatrixChanged) {
1464 if (layer->setMatrix(s.matrix))
1465 flags |= eTraversalNeeded;
1466 }
1467 if (what & eTransparentRegionChanged) {
1468 if (layer->setTransparentRegionHint(s.transparentRegion))
1469 flags |= eTraversalNeeded;
1470 }
1471 if (what & eVisibilityChanged) {
1472 if (layer->setFlags(s.flags, s.mask))
1473 flags |= eTraversalNeeded;
1474 }
1475 }
1476 }
1477 if (flags) {
1478 setTransactionFlags(flags);
1479 }
1480 return NO_ERROR;
1481}
1482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483void SurfaceFlinger::screenReleased(int dpy)
1484{
1485 // this may be called by a signal handler, we can't do too much in here
1486 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1487 signalEvent();
1488}
1489
1490void SurfaceFlinger::screenAcquired(int dpy)
1491{
1492 // this may be called by a signal handler, we can't do too much in here
1493 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1494 signalEvent();
1495}
1496
1497status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1498{
Erik Gilling94720d72010-12-01 16:38:01 -08001499 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 char buffer[SIZE];
1501 String8 result;
Mathias Agopian151e8592009-06-15 18:24:59 -07001502 if (!mDump.checkCalling()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 snprintf(buffer, SIZE, "Permission Denial: "
1504 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1505 IPCThreadState::self()->getCallingPid(),
1506 IPCThreadState::self()->getCallingUid());
1507 result.append(buffer);
1508 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001509
1510 // figure out if we're stuck somewhere
1511 const nsecs_t now = systemTime();
1512 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1513 const nsecs_t inTransaction(mDebugInTransaction);
1514 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1515 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1516
1517 // Try to get the main lock, but don't insist if we can't
1518 // (this would indicate SF is stuck, but we want to be able to
1519 // print something in dumpsys).
1520 int retry = 3;
1521 while (mStateLock.tryLock()<0 && --retry>=0) {
1522 usleep(1000000);
1523 }
1524 const bool locked(retry >= 0);
1525 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001526 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001527 "SurfaceFlinger appears to be unresponsive, "
1528 "dumping anyways (no locks held)\n");
1529 result.append(buffer);
1530 }
1531
Mathias Agopian06a61e22011-01-19 16:15:53 -08001532 /*
1533 * Dump the visible layer list
1534 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1536 const size_t count = currentLayers.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001537 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1538 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001540 const sp<LayerBase>& layer(currentLayers[i]);
1541 layer->dump(result, buffer, SIZE);
1542 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 s.transparentRegion.dump(result, "transparentRegion");
1544 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1545 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1546 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001547
Mathias Agopian06a61e22011-01-19 16:15:53 -08001548 /*
1549 * Dump the layers in the purgatory
1550 */
1551
1552 const size_t purgatorySize = mLayerPurgatory.size();
1553 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1554 result.append(buffer);
1555 for (size_t i=0 ; i<purgatorySize ; i++) {
1556 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1557 layer->shortDump(result, buffer, SIZE);
1558 }
1559
1560 /*
1561 * Dump SurfaceFlinger global state
1562 */
1563
1564 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1565 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 mWormholeRegion.dump(result, "WormholeRegion");
1567 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1568 snprintf(buffer, SIZE,
1569 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1570 mFreezeDisplay?"yes":"no", mFreezeCount,
1571 mCurrentState.orientation, hw.canDraw());
1572 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001573 snprintf(buffer, SIZE,
1574 " last eglSwapBuffers() time: %f us\n"
1575 " last transaction time : %f us\n",
1576 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1577 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001578
Mathias Agopiana8d49172009-08-26 16:36:26 -07001579 if (inSwapBuffersDuration || !locked) {
1580 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1581 inSwapBuffersDuration/1000.0);
1582 result.append(buffer);
1583 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001584
Mathias Agopiana8d49172009-08-26 16:36:26 -07001585 if (inTransactionDuration || !locked) {
1586 snprintf(buffer, SIZE, " transaction time: %f us\n",
1587 inTransactionDuration/1000.0);
1588 result.append(buffer);
1589 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001590
Mathias Agopian06a61e22011-01-19 16:15:53 -08001591 /*
1592 * Dump HWComposer state
1593 */
Mathias Agopian6a969242010-09-22 18:58:01 -07001594 HWComposer& hwc(hw.getHwComposer());
1595 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1596 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1597 mDebugDisableHWC ? "disabled" : "enabled");
1598 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -07001599 hwc.dump(result, buffer, SIZE);
Mathias Agopian6a969242010-09-22 18:58:01 -07001600
Mathias Agopian06a61e22011-01-19 16:15:53 -08001601 /*
1602 * Dump gralloc state
1603 */
Mathias Agopian6950e422009-10-05 17:07:12 -07001604 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001605 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001606 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001607
1608 if (locked) {
1609 mStateLock.unlock();
1610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612 write(fd, result.string(), result.size());
1613 return NO_ERROR;
1614}
1615
1616status_t SurfaceFlinger::onTransact(
1617 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1618{
1619 switch (code) {
1620 case CREATE_CONNECTION:
1621 case OPEN_GLOBAL_TRANSACTION:
1622 case CLOSE_GLOBAL_TRANSACTION:
1623 case SET_ORIENTATION:
1624 case FREEZE_DISPLAY:
1625 case UNFREEZE_DISPLAY:
1626 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001627 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001628 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 {
1630 // codes that require permission check
1631 IPCThreadState* ipc = IPCThreadState::self();
1632 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001633 const int uid = ipc->getCallingUid();
Mathias Agopian151e8592009-06-15 18:24:59 -07001634 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1635 LOGE("Permission Denial: "
1636 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1637 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001639 break;
1640 }
1641 case CAPTURE_SCREEN:
1642 {
1643 // codes that require permission check
1644 IPCThreadState* ipc = IPCThreadState::self();
1645 const int pid = ipc->getCallingPid();
1646 const int uid = ipc->getCallingUid();
1647 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1648 LOGE("Permission Denial: "
1649 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1650 return PERMISSION_DENIED;
1651 }
1652 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
1654 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1657 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001658 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian151e8592009-06-15 18:24:59 -07001659 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1660 IPCThreadState* ipc = IPCThreadState::self();
1661 const int pid = ipc->getCallingPid();
1662 const int uid = ipc->getCallingUid();
1663 LOGE("Permission Denial: "
1664 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 return PERMISSION_DENIED;
1666 }
1667 int n;
1668 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001669 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001670 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 return NO_ERROR;
1672 case 1002: // SHOW_UPDATES
1673 n = data.readInt32();
1674 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1675 return NO_ERROR;
1676 case 1003: // SHOW_BACKGROUND
1677 n = data.readInt32();
1678 mDebugBackground = n ? 1 : 0;
1679 return NO_ERROR;
Mathias Agopian6a969242010-09-22 18:58:01 -07001680 case 1008: // toggle use of hw composer
1681 n = data.readInt32();
1682 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -08001683 invalidateHwcGeometry();
Mathias Agopian6a969242010-09-22 18:58:01 -07001684 // fall-through...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 case 1004:{ // repaint everything
1686 Mutex::Autolock _l(mStateLock);
1687 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1688 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1689 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001690 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001692 case 1005:{ // force transaction
1693 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1694 return NO_ERROR;
1695 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001696 case 1006:{ // enable/disable GraphicLog
1697 int enabled = data.readInt32();
1698 GraphicLog::getInstance().setEnabled(enabled);
1699 return NO_ERROR;
1700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 case 1007: // set mFreezeCount
1702 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001703 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 return NO_ERROR;
1705 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001706 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 reply->writeInt32(0);
1708 reply->writeInt32(mDebugRegion);
1709 reply->writeInt32(mDebugBackground);
1710 return NO_ERROR;
1711 case 1013: {
1712 Mutex::Autolock _l(mStateLock);
1713 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1714 reply->writeInt32(hw.getPageFlipCount());
1715 }
1716 return NO_ERROR;
1717 }
1718 }
1719 return err;
1720}
1721
Mathias Agopianaab758e2010-10-11 12:37:43 -07001722// ---------------------------------------------------------------------------
1723
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001724status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1725 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001726{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001727 if (!GLExtensions::getInstance().haveFramebufferObject())
1728 return INVALID_OPERATION;
1729
1730 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001731 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001732 const uint32_t hw_w = hw.getWidth();
1733 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001734 GLfloat u = 1;
1735 GLfloat v = 1;
1736
1737 // make sure to clear all GL error flags
1738 while ( glGetError() != GL_NO_ERROR ) ;
1739
1740 // create a FBO
1741 GLuint name, tname;
1742 glGenTextures(1, &tname);
1743 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001744 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1745 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001746 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001747 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001748 GLint tw = (2 << (31 - clz(hw_w)));
1749 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001750 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1751 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001752 u = GLfloat(hw_w) / tw;
1753 v = GLfloat(hw_h) / th;
1754 }
1755 glGenFramebuffersOES(1, &name);
1756 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001757 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1758 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001759
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001760 // redraw the screen entirely...
1761 glClearColor(0,0,0,1);
1762 glClear(GL_COLOR_BUFFER_BIT);
1763 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1764 const size_t count = layers.size();
1765 for (size_t i=0 ; i<count ; ++i) {
1766 const sp<LayerBase>& layer(layers[i]);
1767 layer->drawForSreenShot();
1768 }
1769
1770 // back to main framebuffer
1771 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1772 glDisable(GL_SCISSOR_TEST);
1773 glDeleteFramebuffersOES(1, &name);
1774
1775 *textureName = tname;
1776 *uOut = u;
1777 *vOut = v;
1778 return NO_ERROR;
1779}
1780
1781// ---------------------------------------------------------------------------
1782
1783status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1784{
1785 status_t result = PERMISSION_DENIED;
1786
1787 if (!GLExtensions::getInstance().haveFramebufferObject())
1788 return INVALID_OPERATION;
1789
1790 // get screen geometry
1791 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1792 const uint32_t hw_w = hw.getWidth();
1793 const uint32_t hw_h = hw.getHeight();
1794 const Region screenBounds(hw.bounds());
1795
1796 GLfloat u, v;
1797 GLuint tname;
1798 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1799 if (result != NO_ERROR) {
1800 return result;
1801 }
1802
1803 GLfloat vtx[8];
1804 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1805 glEnable(GL_TEXTURE_2D);
1806 glBindTexture(GL_TEXTURE_2D, tname);
1807 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1808 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1809 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1810 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1811 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1812 glVertexPointer(2, GL_FLOAT, 0, vtx);
1813
1814 class s_curve_interpolator {
1815 const float nbFrames, s, v;
1816 public:
1817 s_curve_interpolator(int nbFrames, float s)
1818 : nbFrames(1.0f / (nbFrames-1)), s(s),
1819 v(1.0f + expf(-s + 0.5f*s)) {
1820 }
1821 float operator()(int f) {
1822 const float x = f * nbFrames;
1823 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1824 }
1825 };
1826
1827 class v_stretch {
1828 const GLfloat hw_w, hw_h;
1829 public:
1830 v_stretch(uint32_t hw_w, uint32_t hw_h)
1831 : hw_w(hw_w), hw_h(hw_h) {
1832 }
1833 void operator()(GLfloat* vtx, float v) {
1834 const GLfloat w = hw_w + (hw_w * v);
1835 const GLfloat h = hw_h - (hw_h * v);
1836 const GLfloat x = (hw_w - w) * 0.5f;
1837 const GLfloat y = (hw_h - h) * 0.5f;
1838 vtx[0] = x; vtx[1] = y;
1839 vtx[2] = x; vtx[3] = y + h;
1840 vtx[4] = x + w; vtx[5] = y + h;
1841 vtx[6] = x + w; vtx[7] = y;
1842 }
1843 };
1844
1845 class h_stretch {
1846 const GLfloat hw_w, hw_h;
1847 public:
1848 h_stretch(uint32_t hw_w, uint32_t hw_h)
1849 : hw_w(hw_w), hw_h(hw_h) {
1850 }
1851 void operator()(GLfloat* vtx, float v) {
1852 const GLfloat w = hw_w - (hw_w * v);
1853 const GLfloat h = 1.0f;
1854 const GLfloat x = (hw_w - w) * 0.5f;
1855 const GLfloat y = (hw_h - h) * 0.5f;
1856 vtx[0] = x; vtx[1] = y;
1857 vtx[2] = x; vtx[3] = y + h;
1858 vtx[4] = x + w; vtx[5] = y + h;
1859 vtx[6] = x + w; vtx[7] = y;
1860 }
1861 };
1862
1863 // the full animation is 24 frames
1864 const int nbFrames = 12;
1865 s_curve_interpolator itr(nbFrames, 7.5f);
1866 s_curve_interpolator itg(nbFrames, 8.0f);
1867 s_curve_interpolator itb(nbFrames, 8.5f);
1868
1869 v_stretch vverts(hw_w, hw_h);
1870 glEnable(GL_BLEND);
1871 glBlendFunc(GL_ONE, GL_ONE);
1872 for (int i=0 ; i<nbFrames ; i++) {
1873 float x, y, w, h;
1874 const float vr = itr(i);
1875 const float vg = itg(i);
1876 const float vb = itb(i);
1877
1878 // clear screen
1879 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001880 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001881 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001882
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001883 // draw the red plane
1884 vverts(vtx, vr);
1885 glColorMask(1,0,0,1);
1886 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001887
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001888 // draw the green plane
1889 vverts(vtx, vg);
1890 glColorMask(0,1,0,1);
1891 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001892
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001893 // draw the blue plane
1894 vverts(vtx, vb);
1895 glColorMask(0,0,1,1);
1896 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001897
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001898 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001899 glDisable(GL_TEXTURE_2D);
1900 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001901 glColor4f(vg, vg, vg, 1);
1902 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1903 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001904 }
1905
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001906 h_stretch hverts(hw_w, hw_h);
1907 glDisable(GL_BLEND);
1908 glDisable(GL_TEXTURE_2D);
1909 glColorMask(1,1,1,1);
1910 for (int i=0 ; i<nbFrames ; i++) {
1911 const float v = itg(i);
1912 hverts(vtx, v);
1913 glClear(GL_COLOR_BUFFER_BIT);
1914 glColor4f(1-v, 1-v, 1-v, 1);
1915 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1916 hw.flip(screenBounds);
1917 }
1918
1919 glColorMask(1,1,1,1);
1920 glEnable(GL_SCISSOR_TEST);
1921 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1922 glDeleteTextures(1, &tname);
1923 return NO_ERROR;
1924}
1925
1926status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1927{
1928 status_t result = PERMISSION_DENIED;
1929
1930 if (!GLExtensions::getInstance().haveFramebufferObject())
1931 return INVALID_OPERATION;
1932
1933
1934 // get screen geometry
1935 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1936 const uint32_t hw_w = hw.getWidth();
1937 const uint32_t hw_h = hw.getHeight();
1938 const Region screenBounds(hw.bounds());
1939
1940 GLfloat u, v;
1941 GLuint tname;
1942 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1943 if (result != NO_ERROR) {
1944 return result;
1945 }
1946
1947 // back to main framebuffer
1948 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1949 glDisable(GL_SCISSOR_TEST);
1950
1951 GLfloat vtx[8];
1952 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1953 glEnable(GL_TEXTURE_2D);
1954 glBindTexture(GL_TEXTURE_2D, tname);
1955 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1956 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1957 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1958 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1959 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1960 glVertexPointer(2, GL_FLOAT, 0, vtx);
1961
1962 class s_curve_interpolator {
1963 const float nbFrames, s, v;
1964 public:
1965 s_curve_interpolator(int nbFrames, float s)
1966 : nbFrames(1.0f / (nbFrames-1)), s(s),
1967 v(1.0f + expf(-s + 0.5f*s)) {
1968 }
1969 float operator()(int f) {
1970 const float x = f * nbFrames;
1971 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1972 }
1973 };
1974
1975 class v_stretch {
1976 const GLfloat hw_w, hw_h;
1977 public:
1978 v_stretch(uint32_t hw_w, uint32_t hw_h)
1979 : hw_w(hw_w), hw_h(hw_h) {
1980 }
1981 void operator()(GLfloat* vtx, float v) {
1982 const GLfloat w = hw_w + (hw_w * v);
1983 const GLfloat h = hw_h - (hw_h * v);
1984 const GLfloat x = (hw_w - w) * 0.5f;
1985 const GLfloat y = (hw_h - h) * 0.5f;
1986 vtx[0] = x; vtx[1] = y;
1987 vtx[2] = x; vtx[3] = y + h;
1988 vtx[4] = x + w; vtx[5] = y + h;
1989 vtx[6] = x + w; vtx[7] = y;
1990 }
1991 };
1992
1993 class h_stretch {
1994 const GLfloat hw_w, hw_h;
1995 public:
1996 h_stretch(uint32_t hw_w, uint32_t hw_h)
1997 : hw_w(hw_w), hw_h(hw_h) {
1998 }
1999 void operator()(GLfloat* vtx, float v) {
2000 const GLfloat w = hw_w - (hw_w * v);
2001 const GLfloat h = 1.0f;
2002 const GLfloat x = (hw_w - w) * 0.5f;
2003 const GLfloat y = (hw_h - h) * 0.5f;
2004 vtx[0] = x; vtx[1] = y;
2005 vtx[2] = x; vtx[3] = y + h;
2006 vtx[4] = x + w; vtx[5] = y + h;
2007 vtx[6] = x + w; vtx[7] = y;
2008 }
2009 };
2010
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002011 // the full animation is 12 frames
2012 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002013 s_curve_interpolator itr(nbFrames, 7.5f);
2014 s_curve_interpolator itg(nbFrames, 8.0f);
2015 s_curve_interpolator itb(nbFrames, 8.5f);
2016
2017 h_stretch hverts(hw_w, hw_h);
2018 glDisable(GL_BLEND);
2019 glDisable(GL_TEXTURE_2D);
2020 glColorMask(1,1,1,1);
2021 for (int i=nbFrames-1 ; i>=0 ; i--) {
2022 const float v = itg(i);
2023 hverts(vtx, v);
2024 glClear(GL_COLOR_BUFFER_BIT);
2025 glColor4f(1-v, 1-v, 1-v, 1);
2026 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2027 hw.flip(screenBounds);
2028 }
2029
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002030 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002031 v_stretch vverts(hw_w, hw_h);
2032 glEnable(GL_BLEND);
2033 glBlendFunc(GL_ONE, GL_ONE);
2034 for (int i=nbFrames-1 ; i>=0 ; i--) {
2035 float x, y, w, h;
2036 const float vr = itr(i);
2037 const float vg = itg(i);
2038 const float vb = itb(i);
2039
2040 // clear screen
2041 glColorMask(1,1,1,1);
2042 glClear(GL_COLOR_BUFFER_BIT);
2043 glEnable(GL_TEXTURE_2D);
2044
2045 // draw the red plane
2046 vverts(vtx, vr);
2047 glColorMask(1,0,0,1);
2048 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2049
2050 // draw the green plane
2051 vverts(vtx, vg);
2052 glColorMask(0,1,0,1);
2053 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2054
2055 // draw the blue plane
2056 vverts(vtx, vb);
2057 glColorMask(0,0,1,1);
2058 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2059
2060 hw.flip(screenBounds);
2061 }
2062
2063 glColorMask(1,1,1,1);
2064 glEnable(GL_SCISSOR_TEST);
2065 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002066 glDeleteTextures(1, &tname);
2067
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002068 return NO_ERROR;
2069}
2070
2071// ---------------------------------------------------------------------------
2072
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002073status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002074{
2075 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2076 if (!hw.canDraw()) {
2077 // we're already off
2078 return NO_ERROR;
2079 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002080 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2081 electronBeamOffAnimationImplLocked();
2082 }
2083
2084 // always clear the whole screen at the end of the animation
2085 glClearColor(0,0,0,1);
2086 glDisable(GL_SCISSOR_TEST);
2087 glClear(GL_COLOR_BUFFER_BIT);
2088 glEnable(GL_SCISSOR_TEST);
2089 hw.flip( Region(hw.bounds()) );
2090
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002091 hw.setCanDraw(false);
2092 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002093}
2094
2095status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2096{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002097 class MessageTurnElectronBeamOff : public MessageBase {
2098 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002099 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002100 status_t result;
2101 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002102 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2103 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002104 }
2105 status_t getResult() const {
2106 return result;
2107 }
2108 virtual bool handler() {
2109 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002110 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002111 return true;
2112 }
2113 };
2114
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002115 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002116 status_t res = postMessageSync(msg);
2117 if (res == NO_ERROR) {
2118 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002119
2120 // work-around: when the power-manager calls us we activate the
2121 // animation. eventually, the "on" animation will be called
2122 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002123 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002124 }
2125 return res;
2126}
2127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002129
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002130status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002131{
2132 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2133 if (hw.canDraw()) {
2134 // we're already on
2135 return NO_ERROR;
2136 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002137 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2138 electronBeamOnAnimationImplLocked();
2139 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002140 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002141
2142 // make sure to redraw the whole screen when the animation is done
2143 mDirtyRegion.set(hw.bounds());
2144 signalEvent();
2145
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002146 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002147}
2148
2149status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2150{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002151 class MessageTurnElectronBeamOn : public MessageBase {
2152 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002153 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002154 status_t result;
2155 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002156 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2157 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002158 }
2159 status_t getResult() const {
2160 return result;
2161 }
2162 virtual bool handler() {
2163 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002164 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002165 return true;
2166 }
2167 };
2168
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002169 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002170 return NO_ERROR;
2171}
2172
2173// ---------------------------------------------------------------------------
2174
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002175status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2176 sp<IMemoryHeap>* heap,
2177 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002178 uint32_t sw, uint32_t sh,
2179 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002180{
2181 status_t result = PERMISSION_DENIED;
2182
2183 // only one display supported for now
2184 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2185 return BAD_VALUE;
2186
Jamie Gennisf72606c2011-03-09 17:05:02 -08002187 // make sure none of the layers are protected
2188 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2189 const size_t count = layers.size();
2190 for (size_t i=0 ; i<count ; ++i) {
2191 const sp<LayerBase>& layer(layers[i]);
2192 const uint32_t z = layer->drawingState().z;
2193 if (z >= minLayerZ && z <= maxLayerZ) {
2194 if (layer->isProtected()) {
2195 return INVALID_OPERATION;
2196 }
2197 }
2198 }
2199
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002200 if (!GLExtensions::getInstance().haveFramebufferObject())
2201 return INVALID_OPERATION;
2202
2203 // get screen geometry
2204 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2205 const uint32_t hw_w = hw.getWidth();
2206 const uint32_t hw_h = hw.getHeight();
2207
2208 if ((sw > hw_w) || (sh > hw_h))
2209 return BAD_VALUE;
2210
2211 sw = (!sw) ? hw_w : sw;
2212 sh = (!sh) ? hw_h : sh;
2213 const size_t size = sw * sh * 4;
2214
Mathias Agopian32ae0942011-03-02 18:45:50 -08002215 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2216 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002217
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002218 // make sure to clear all GL error flags
2219 while ( glGetError() != GL_NO_ERROR ) ;
2220
2221 // create a FBO
2222 GLuint name, tname;
2223 glGenRenderbuffersOES(1, &tname);
2224 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2225 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2226 glGenFramebuffersOES(1, &name);
2227 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2228 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2229 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2230
2231 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002232
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002233 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2234
2235 // invert everything, b/c glReadPixel() below will invert the FB
2236 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002237 glScissor(0, 0, sw, sh);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002238 glMatrixMode(GL_PROJECTION);
2239 glPushMatrix();
2240 glLoadIdentity();
2241 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2242 glMatrixMode(GL_MODELVIEW);
2243
2244 // redraw the screen entirely...
2245 glClearColor(0,0,0,1);
2246 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002247
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002248 for (size_t i=0 ; i<count ; ++i) {
2249 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002250 const uint32_t z = layer->drawingState().z;
2251 if (z >= minLayerZ && z <= maxLayerZ) {
2252 layer->drawForSreenShot();
2253 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002254 }
2255
2256 // XXX: this is needed on tegra
2257 glScissor(0, 0, sw, sh);
2258
2259 // check for errors and return screen capture
2260 if (glGetError() != GL_NO_ERROR) {
2261 // error while rendering
2262 result = INVALID_OPERATION;
2263 } else {
2264 // allocate shared memory large enough to hold the
2265 // screen capture
2266 sp<MemoryHeapBase> base(
2267 new MemoryHeapBase(size, 0, "screen-capture") );
2268 void* const ptr = base->getBase();
2269 if (ptr) {
2270 // capture the screen with glReadPixels()
2271 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2272 if (glGetError() == GL_NO_ERROR) {
2273 *heap = base;
2274 *w = sw;
2275 *h = sh;
2276 *f = PIXEL_FORMAT_RGBA_8888;
2277 result = NO_ERROR;
2278 }
2279 } else {
2280 result = NO_MEMORY;
2281 }
2282 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002283 glEnable(GL_SCISSOR_TEST);
2284 glViewport(0, 0, hw_w, hw_h);
2285 glMatrixMode(GL_PROJECTION);
2286 glPopMatrix();
2287 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002288 } else {
2289 result = BAD_VALUE;
2290 }
2291
2292 // release FBO resources
2293 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2294 glDeleteRenderbuffersOES(1, &tname);
2295 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002296
2297 hw.compositionComplete();
2298
Mathias Agopian32ae0942011-03-02 18:45:50 -08002299 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002300
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002301 return result;
2302}
2303
2304
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002305status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2306 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002307 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002308 uint32_t sw, uint32_t sh,
2309 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002310{
2311 // only one display supported for now
2312 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2313 return BAD_VALUE;
2314
2315 if (!GLExtensions::getInstance().haveFramebufferObject())
2316 return INVALID_OPERATION;
2317
2318 class MessageCaptureScreen : public MessageBase {
2319 SurfaceFlinger* flinger;
2320 DisplayID dpy;
2321 sp<IMemoryHeap>* heap;
2322 uint32_t* w;
2323 uint32_t* h;
2324 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002325 uint32_t sw;
2326 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002327 uint32_t minLayerZ;
2328 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002329 status_t result;
2330 public:
2331 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002332 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002333 uint32_t sw, uint32_t sh,
2334 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002335 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002336 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2337 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2338 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002339 {
2340 }
2341 status_t getResult() const {
2342 return result;
2343 }
2344 virtual bool handler() {
2345 Mutex::Autolock _l(flinger->mStateLock);
2346
2347 // if we have secure windows, never allow the screen capture
2348 if (flinger->mSecureFrameBuffer)
2349 return true;
2350
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002351 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002352 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002353
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002354 return true;
2355 }
2356 };
2357
2358 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002359 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002360 status_t res = postMessageSync(msg);
2361 if (res == NO_ERROR) {
2362 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2363 }
2364 return res;
2365}
2366
2367// ---------------------------------------------------------------------------
2368
Mathias Agopian7623da42010-06-01 15:12:58 -07002369sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2370{
2371 sp<Layer> result;
2372 Mutex::Autolock _l(mStateLock);
2373 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2374 return result;
2375}
2376
2377// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378
Mathias Agopian593c05c2010-06-02 23:28:45 -07002379Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002380 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382}
2383
Mathias Agopian593c05c2010-06-02 23:28:45 -07002384Client::~Client()
2385{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002386 const size_t count = mLayers.size();
2387 for (size_t i=0 ; i<count ; i++) {
2388 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2389 if (layer != 0) {
2390 mFlinger->removeLayer(layer);
2391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
2393}
2394
Mathias Agopian593c05c2010-06-02 23:28:45 -07002395status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002396 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397}
Mathias Agopian1473f462009-04-10 14:24:30 -07002398
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002399size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002400{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002401 Mutex::Autolock _l(mLock);
2402 size_t name = mNameGenerator++;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002403 mLayers.add(name, layer);
2404 return name;
2405}
2406
Mathias Agopian7623da42010-06-01 15:12:58 -07002407void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002408{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002409 Mutex::Autolock _l(mLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002410 // we do a linear search here, because this doesn't happen often
2411 const size_t count = mLayers.size();
2412 for (size_t i=0 ; i<count ; i++) {
2413 if (mLayers.valueAt(i) == layer) {
2414 mLayers.removeItemsAt(i, 1);
2415 break;
2416 }
2417 }
2418}
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002419sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2420{
2421 Mutex::Autolock _l(mLock);
Mathias Agopian1473f462009-04-10 14:24:30 -07002422 sp<LayerBaseClient> lbc;
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002423 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian593c05c2010-06-02 23:28:45 -07002424 if (layer != 0) {
2425 lbc = layer.promote();
2426 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002427 }
2428 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429}
2430
Mathias Agopian593c05c2010-06-02 23:28:45 -07002431sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002432 return 0;
2433}
2434ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2435 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002437sp<ISurface> Client::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002438 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002439 const String8& name,
2440 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 uint32_t flags)
2442{
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002443 return mFlinger->createSurface(params, name, this,
Mathias Agopian593c05c2010-06-02 23:28:45 -07002444 display, w, h, format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002446status_t Client::destroySurface(SurfaceID sid) {
2447 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002449status_t Client::setState(int32_t count, const layer_state_t* states) {
2450 return mFlinger->setClientState(this, count, states);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451}
2452
2453// ---------------------------------------------------------------------------
2454
Mathias Agopian7623da42010-06-01 15:12:58 -07002455UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2456 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2457{
2458 const int pgsize = getpagesize();
2459 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2460
2461 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2462 "SurfaceFlinger Client control-block");
2463
2464 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2465 if (ctrlblk) { // construct the shared structure in-place.
2466 new(ctrlblk) SharedClient;
2467 }
2468}
2469
2470UserClient::~UserClient()
2471{
2472 if (ctrlblk) {
2473 ctrlblk->~SharedClient(); // destroy our shared-structure.
2474 }
2475
2476 /*
2477 * When a UserClient dies, it's unclear what to do exactly.
2478 * We could go ahead and destroy all surfaces linked to that client
2479 * however, it wouldn't be fair to the main Client
2480 * (usually the the window-manager), which might want to re-target
2481 * the layer to another UserClient.
2482 * I think the best is to do nothing, or not much; in most cases the
2483 * WM itself will go ahead and clean things up when it detects a client of
2484 * his has died.
2485 * The remaining question is what to display? currently we keep
2486 * just keep the current buffer.
2487 */
2488}
2489
2490status_t UserClient::initCheck() const {
2491 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2492}
2493
2494void UserClient::detachLayer(const Layer* layer)
2495{
2496 int32_t name = layer->getToken();
2497 if (name >= 0) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002498 int32_t mask = 1LU<<name;
2499 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2500 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2501 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002502 }
2503}
2504
2505sp<IMemoryHeap> UserClient::getControlBlock() const {
2506 return mCblkHeap;
2507}
2508
2509ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2510{
2511 int32_t name = NAME_NOT_FOUND;
2512 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002513 if (layer == 0) {
2514 return name;
2515 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002516
Mathias Agopian5e140102010-06-08 19:54:15 -07002517 // if this layer already has a token, just return it
Mathias Agopian7623da42010-06-01 15:12:58 -07002518 name = layer->getToken();
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002519 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian5e140102010-06-08 19:54:15 -07002520 return name;
Jamie Gennis85cfdd02010-08-10 16:37:53 -07002521 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002522
2523 name = 0;
2524 do {
2525 int32_t mask = 1LU<<name;
2526 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2527 // we found and locked that name
Mathias Agopian5e140102010-06-08 19:54:15 -07002528 status_t err = layer->setToken(
2529 const_cast<UserClient*>(this), ctrlblk, name);
2530 if (err != NO_ERROR) {
2531 // free the name
2532 android_atomic_and(~mask, &mBitmap);
2533 name = err;
2534 }
Mathias Agopian7623da42010-06-01 15:12:58 -07002535 break;
2536 }
Mathias Agopianf0ff9062011-03-11 16:54:47 -08002537 if (++name >= int32_t(SharedBufferStack::NUM_LAYERS_MAX))
Mathias Agopian7623da42010-06-01 15:12:58 -07002538 name = NO_MEMORY;
2539 } while(name >= 0);
2540
Mathias Agopian1debc662010-06-08 15:40:56 -07002541 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2542 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopian7623da42010-06-01 15:12:58 -07002543 return name;
2544}
2545
2546sp<ISurface> UserClient::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002547 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002548 const String8& name,
2549 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2550 uint32_t flags) {
2551 return 0;
2552}
2553status_t UserClient::destroySurface(SurfaceID sid) {
2554 return INVALID_OPERATION;
2555}
2556status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2557 return INVALID_OPERATION;
2558}
2559
2560// ---------------------------------------------------------------------------
2561
Jamie Gennisf7acf162011-01-12 18:30:40 -08002562GraphicBufferAlloc::GraphicBufferAlloc() {}
2563
2564GraphicBufferAlloc::~GraphicBufferAlloc() {}
2565
2566sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2567 PixelFormat format, uint32_t usage) {
2568 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2569 status_t err = graphicBuffer->initCheck();
2570 if (err != 0) {
2571 LOGE("createGraphicBuffer: init check failed: %d", err);
2572 return 0;
2573 } else if (graphicBuffer->handle == 0) {
2574 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2575 return 0;
2576 }
Jamie Gennisf7acf162011-01-12 18:30:40 -08002577 return graphicBuffer;
2578}
2579
Jamie Gennisf7acf162011-01-12 18:30:40 -08002580// ---------------------------------------------------------------------------
2581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582GraphicPlane::GraphicPlane()
2583 : mHw(0)
2584{
2585}
2586
2587GraphicPlane::~GraphicPlane() {
2588 delete mHw;
2589}
2590
2591bool GraphicPlane::initialized() const {
2592 return mHw ? true : false;
2593}
2594
Mathias Agopian66c77a52010-02-08 15:49:35 -08002595int GraphicPlane::getWidth() const {
2596 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597}
2598
Mathias Agopian66c77a52010-02-08 15:49:35 -08002599int GraphicPlane::getHeight() const {
2600 return mHeight;
2601}
2602
2603void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2604{
2605 mHw = hw;
2606
2607 // initialize the display orientation transform.
2608 // it's a constant that should come from the display driver.
2609 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2610 char property[PROPERTY_VALUE_MAX];
2611 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2612 //displayOrientation
2613 switch (atoi(property)) {
2614 case 90:
2615 displayOrientation = ISurfaceComposer::eOrientation90;
2616 break;
2617 case 270:
2618 displayOrientation = ISurfaceComposer::eOrientation270;
2619 break;
2620 }
2621 }
2622
2623 const float w = hw->getWidth();
2624 const float h = hw->getHeight();
2625 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2626 &mDisplayTransform);
2627 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2628 mDisplayWidth = h;
2629 mDisplayHeight = w;
2630 } else {
2631 mDisplayWidth = w;
2632 mDisplayHeight = h;
2633 }
2634
2635 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636}
2637
2638status_t GraphicPlane::orientationToTransfrom(
2639 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002640{
2641 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 switch (orientation) {
2643 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002644 flags = Transform::ROT_0;
2645 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002647 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 break;
2649 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002650 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 break;
2652 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002653 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 break;
2655 default:
2656 return BAD_VALUE;
2657 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002658 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 return NO_ERROR;
2660}
2661
2662status_t GraphicPlane::setOrientation(int orientation)
2663{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 // If the rotation can be handled in hardware, this is where
2665 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002666
2667 const DisplayHardware& hw(displayHardware());
2668 const float w = mDisplayWidth;
2669 const float h = mDisplayHeight;
2670 mWidth = int(w);
2671 mHeight = int(h);
2672
2673 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002674 GraphicPlane::orientationToTransfrom(orientation, w, h,
2675 &orientationTransform);
2676 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2677 mWidth = int(h);
2678 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002680
Mathias Agopian3552f532009-03-27 17:58:20 -07002681 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002682 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 return NO_ERROR;
2684}
2685
2686const DisplayHardware& GraphicPlane::displayHardware() const {
2687 return *mHw;
2688}
2689
Mathias Agopianaab758e2010-10-11 12:37:43 -07002690DisplayHardware& GraphicPlane::editDisplayHardware() {
2691 return *mHw;
2692}
2693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694const Transform& GraphicPlane::transform() const {
2695 return mGlobalTransform;
2696}
2697
Mathias Agopian1473f462009-04-10 14:24:30 -07002698EGLDisplay GraphicPlane::getEGLDisplay() const {
2699 return mHw->getEGLDisplay();
2700}
2701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702// ---------------------------------------------------------------------------
2703
2704}; // namespace android