blob: b0881a4a94035d61178829575fa73d42aadea7bb [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>
Mathias Agopian0dd593f2011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian6950e422009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070049#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070055#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Mathias Agopian7bb843c2011-04-20 14:20:59 -070057#include <private/surfaceflinger/SharedBufferStack.h>
58
Mathias Agopian627e7b52009-05-21 19:21:59 -070059/* ideally AID_GRAPHICS would be in a semi-public header
60 * or there would be a way to map a user/group name to its id
61 */
62#ifndef AID_GRAPHICS
63#define AID_GRAPHICS 1003
64#endif
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066#define DISPLAY_COUNT 1
67
68namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069// ---------------------------------------------------------------------------
70
Mathias Agopian0dd593f2011-06-27 16:05:52 -070071const String16 sHardwareTest("android.permission.HARDWARE_TEST");
72const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
73const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
74const String16 sDump("android.permission.DUMP");
75
76// ---------------------------------------------------------------------------
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078SurfaceFlinger::SurfaceFlinger()
79 : BnSurfaceComposer(), Thread(false),
80 mTransactionFlags(0),
Mathias Agopian9779b222009-09-07 16:32:45 -070081 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070082 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 mBootTime(systemTime()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070085 mHwWorkListDirty(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mDeferReleaseConsole(false),
87 mFreezeDisplay(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070090 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 mDebugBackground(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -070098 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 mConsoleSignals(0),
100 mSecureFrameBuffer(0)
101{
102 init();
103}
104
105void SurfaceFlinger::init()
106{
107 LOGI("SurfaceFlinger is starting");
108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 property_get("debug.sf.showbackground", value, "0");
114 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700116 LOGI_IF(mDebugRegion, "showupdates enabled");
117 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118}
119
120SurfaceFlinger::~SurfaceFlinger()
121{
122 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700125sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700127 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128}
129
Mathias Agopian770492c2010-05-28 14:22:23 -0700130sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700132 sp<ISurfaceComposerClient> bclient;
133 sp<Client> client(new Client(this));
134 status_t err = client->initCheck();
135 if (err == NO_ERROR) {
136 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 return bclient;
139}
140
Jamie Gennisf7acf162011-01-12 18:30:40 -0800141sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
142{
143 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
144 return gba;
145}
Mathias Agopian7623da42010-06-01 15:12:58 -0700146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
148{
149 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
150 const GraphicPlane& plane(mGraphicPlanes[dpy]);
151 return plane;
152}
153
154GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
155{
156 return const_cast<GraphicPlane&>(
157 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
158}
159
160void SurfaceFlinger::bootFinished()
161{
162 const nsecs_t now = systemTime();
163 const nsecs_t duration = now - mBootTime;
Andreas Hubere3c01832010-08-16 08:49:37 -0700164 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700165 mBootFinished = true;
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700166
167 // wait patiently for the window manager death
168 const String16 name("window");
169 sp<IBinder> window(defaultServiceManager()->getService(name));
170 if (window != 0) {
171 window->linkToDeath(this);
172 }
173
174 // stop boot animation
Mathias Agopian627e7b52009-05-21 19:21:59 -0700175 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176}
177
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700178void SurfaceFlinger::binderDied(const wp<IBinder>& who)
179{
180 // the window manager died on us. prepare its eulogy.
181
182 // unfreeze the screen in case it was... frozen
183 mFreezeDisplayTime = 0;
184 mFreezeCount = 0;
185 mFreezeDisplay = false;
186
187 // reset screen orientation
188 setOrientation(0, eOrientationDefault, 0);
189
190 // restart the boot-animation
191 property_set("ctl.start", "bootanim");
192}
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194void SurfaceFlinger::onFirstRef()
195{
196 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
197
198 // Wait for the main thread to be done with its initialization
199 mReadyToRunBarrier.wait();
200}
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202static inline uint16_t pack565(int r, int g, int b) {
203 return (r<<11)|(g<<5)|b;
204}
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206status_t SurfaceFlinger::readyToRun()
207{
208 LOGI( "SurfaceFlinger's main thread ready to run. "
209 "Initializing graphics H/W...");
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 // we only support one display currently
212 int dpy = 0;
213
214 {
215 // initialize the main display
216 GraphicPlane& plane(graphicPlane(dpy));
217 DisplayHardware* const hw = new DisplayHardware(this, dpy);
218 plane.setDisplayHardware(hw);
219 }
220
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700221 // create the shared control-block
222 mServerHeap = new MemoryHeapBase(4096,
223 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
224 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Hubere3c01832010-08-16 08:49:37 -0700225
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700226 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
227 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Hubere3c01832010-08-16 08:49:37 -0700228
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700229 new(mServerCblk) surface_flinger_cblk_t;
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 // initialize primary screen
232 // (other display should be initialized in the same manner, but
233 // asynchronously, as they could come and go. None of this is supported
234 // yet).
235 const GraphicPlane& plane(graphicPlane(dpy));
236 const DisplayHardware& hw = plane.displayHardware();
237 const uint32_t w = hw.getWidth();
238 const uint32_t h = hw.getHeight();
239 const uint32_t f = hw.getFormat();
240 hw.makeCurrent();
241
242 // initialize the shared control block
243 mServerCblk->connected |= 1<<dpy;
244 display_cblk_t* dcblk = mServerCblk->displays + dpy;
245 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian66c77a52010-02-08 15:49:35 -0800246 dcblk->w = plane.getWidth();
247 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 dcblk->format = f;
249 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
250 dcblk->xdpi = hw.getDpiX();
251 dcblk->ydpi = hw.getDpiY();
252 dcblk->fps = hw.getRefreshRate();
253 dcblk->density = hw.getDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
255 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700257 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 glEnableClientState(GL_VERTEX_ARRAY);
259 glEnable(GL_SCISSOR_TEST);
260 glShadeModel(GL_FLAT);
261 glDisable(GL_DITHER);
262 glDisable(GL_CULL_FACE);
263
264 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
265 const uint16_t g1 = pack565(0x17,0x2f,0x17);
266 const uint16_t textureData[4] = { g0, g1, g1, g0 };
267 glGenTextures(1, &mWormholeTexName);
268 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
269 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
270 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
271 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
272 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
273 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
274 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
275
276 glViewport(0, 0, w, h);
277 glMatrixMode(GL_PROJECTION);
278 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -0700279 // put the origin in the left-bottom corner
280 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 mReadyToRunBarrier.open();
283
284 /*
285 * We're now ready to accept clients...
286 */
287
Mathias Agopian627e7b52009-05-21 19:21:59 -0700288 // start boot animation
289 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 return NO_ERROR;
292}
293
294// ----------------------------------------------------------------------------
295#if 0
296#pragma mark -
297#pragma mark Events Handler
298#endif
299
300void SurfaceFlinger::waitForEvent()
301{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700302 while (true) {
303 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800304 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700305 if (UNLIKELY(isFrozen())) {
306 // wait 5 seconds
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700307 const nsecs_t now = systemTime();
308 if (mFreezeDisplayTime == 0) {
309 mFreezeDisplayTime = now;
310 }
311 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
312 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700313 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700314
Mathias Agopian898c4c92010-05-18 17:06:55 -0700315 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-12-01 17:23:28 -0800316
317 // see if we timed out
318 if (isFrozen()) {
319 const nsecs_t now = systemTime();
320 nsecs_t frozenTime = (now - mFreezeDisplayTime);
321 if (frozenTime >= freezeDisplayTimeout) {
322 // we timed out and are still frozen
323 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
324 mFreezeDisplay, mFreezeCount);
325 mFreezeDisplayTime = 0;
326 mFreezeCount = 0;
327 mFreezeDisplay = false;
328 }
329 }
330
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700331 if (msg != 0) {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700332 switch (msg->what) {
333 case MessageQueue::INVALIDATE:
334 // invalidate message, just return to the main loop
335 return;
336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339}
340
341void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700342 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343}
344
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800345bool SurfaceFlinger::authenticateSurface(const sp<ISurface>& surface) const {
346 Mutex::Autolock _l(mStateLock);
347 sp<IBinder> surfBinder(surface->asBinder());
348
349 // Check the visible layer list for the ISurface
350 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
351 size_t count = currentLayers.size();
352 for (size_t i=0 ; i<count ; i++) {
353 const sp<LayerBase>& layer(currentLayers[i]);
354 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
355 if (lbc != NULL && lbc->getSurfaceBinder() == surfBinder) {
356 return true;
357 }
358 }
359
360 // Check the layers in the purgatory. This check is here so that if a
361 // Surface gets destroyed before all the clients are done using it, the
362 // error will not be reported as "surface XYZ is not authenticated", but
363 // will instead fail later on when the client tries to use the surface,
364 // which should be reported as "surface XYZ returned an -ENODEV". The
365 // purgatorized layers are no less authentic than the visible ones, so this
366 // should not cause any harm.
367 size_t purgatorySize = mLayerPurgatory.size();
368 for (size_t i=0 ; i<purgatorySize ; i++) {
369 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
370 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
371 if (lbc != NULL && lbc->getSurfaceBinder() == surfBinder) {
372 return true;
373 }
374 }
375
376 return false;
377}
378
Mathias Agopian898c4c92010-05-18 17:06:55 -0700379status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
380 nsecs_t reltime, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700382 return mEventQueue.postMessage(msg, reltime, flags);
383}
384
385status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
386 nsecs_t reltime, uint32_t flags)
387{
388 status_t res = mEventQueue.postMessage(msg, reltime, flags);
389 if (res == NO_ERROR) {
390 msg->wait();
391 }
392 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393}
394
395// ----------------------------------------------------------------------------
396#if 0
397#pragma mark -
398#pragma mark Main loop
399#endif
400
401bool SurfaceFlinger::threadLoop()
402{
403 waitForEvent();
404
Mathias Agopian69608112011-05-19 15:38:14 -0700405 // call Layer's destructor
406 handleDestroyLayers();
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 // check for transactions
409 if (UNLIKELY(mConsoleSignals)) {
410 handleConsoleEvents();
411 }
412
Mathias Agopian439863f2011-06-28 19:09:31 -0700413 // if we're in a global transaction, don't do anything.
414 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
415 uint32_t transactionFlags = peekTransactionFlags(mask);
416 if (UNLIKELY(transactionFlags)) {
417 handleTransaction(transactionFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
419
420 // post surfaces (if needed)
421 handlePageFlip();
422
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700423 if (UNLIKELY(mHwWorkListDirty)) {
424 // build the h/w work list
425 handleWorkList();
426 }
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700429 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-09-13 22:57:58 -0700431
432 const int index = hw.getCurrentBufferIndex();
433 GraphicLog& logger(GraphicLog::getInstance());
434
435 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 handleRepaint();
437
Mathias Agopianb1a18742009-09-17 16:18:16 -0700438 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700439 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700440 hw.compositionComplete();
441
Mathias Agopian04262e92010-09-13 22:57:58 -0700442 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700443 postFramebuffer();
444
Mathias Agopian04262e92010-09-13 22:57:58 -0700445 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 } else {
447 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800448 hw.compositionComplete();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 usleep(16667); // 60 fps period
450 }
451 return true;
452}
453
454void SurfaceFlinger::postFramebuffer()
455{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 if (!mInvalidRegion.isEmpty()) {
457 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700458 const nsecs_t now = systemTime();
459 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700461 mLastSwapBufferTime = systemTime() - now;
462 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465}
466
467void SurfaceFlinger::handleConsoleEvents()
468{
469 // something to do with the console
470 const DisplayHardware& hw = graphicPlane(0).displayHardware();
471
472 int what = android_atomic_and(0, &mConsoleSignals);
473 if (what & eConsoleAcquired) {
474 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700475 // this is a temporary work-around, eventually this should be called
476 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700477 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479
Mathias Agopianaab758e2010-10-11 12:37:43 -0700480 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700481 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 hw.releaseScreen();
484 }
485
486 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700487 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 hw.releaseScreen();
489 } else {
490 mDeferReleaseConsole = true;
491 }
492 }
493
494 mDirtyRegion.set(hw.bounds());
495}
496
497void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
498{
Mathias Agopian69608112011-05-19 15:38:14 -0700499 Mutex::Autolock _l(mStateLock);
500 const nsecs_t now = systemTime();
501 mDebugInTransaction = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
Mathias Agopian69608112011-05-19 15:38:14 -0700503 // Here we're guaranteed that some transaction flags are set
504 // so we can call handleTransactionLocked() unconditionally.
505 // We call getTransactionFlags(), which will also clear the flags,
506 // with mStateLock held to guarantee that mCurrentState won't change
507 // until the transaction is committed.
Mathias Agopianff1d4102010-08-10 17:19:56 -0700508
Mathias Agopian69608112011-05-19 15:38:14 -0700509 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
510 transactionFlags = getTransactionFlags(mask);
511 handleTransactionLocked(transactionFlags);
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700512
Mathias Agopian69608112011-05-19 15:38:14 -0700513 mLastTransactionTime = systemTime() - now;
514 mDebugInTransaction = 0;
515 invalidateHwcGeometry();
516 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700517}
518
Mathias Agopian69608112011-05-19 15:38:14 -0700519void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700520{
521 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 const size_t count = currentLayers.size();
523
524 /*
525 * Traversal of the children
526 * (perform the transaction for each of them if needed)
527 */
528
529 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
530 if (layersNeedTransaction) {
531 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700532 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
534 if (!trFlags) continue;
535
536 const uint32_t flags = layer->doTransaction(0);
537 if (flags & Layer::eVisibleRegion)
538 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 }
541
542 /*
543 * Perform our own transaction if needed
544 */
545
546 if (transactionFlags & eTransactionNeeded) {
547 if (mCurrentState.orientation != mDrawingState.orientation) {
548 // the orientation has changed, recompute all visible regions
549 // and invalidate everything.
550
551 const int dpy = 0;
552 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700553 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 GraphicPlane& plane(graphicPlane(dpy));
555 plane.setOrientation(orientation);
556
557 // update the shared control block
558 const DisplayHardware& hw(plane.displayHardware());
559 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
560 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800561 dcblk->w = plane.getWidth();
562 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
564 mVisibleRegionsDirty = true;
565 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567
568 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
569 // freezing or unfreezing the display -> trigger animation if needed
570 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800571 if (mFreezeDisplay)
572 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700575 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
576 // layers have been added
577 mVisibleRegionsDirty = true;
578 }
579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 // some layers might have been removed, so
581 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700582 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700583 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700585 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700586 const size_t count = previousLayers.size();
587 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700588 const sp<LayerBase>& layer(previousLayers[i]);
589 if (currentLayers.indexOf( layer ) < 0) {
590 // this layer is not visible anymore
Mathias Agopian33863dd2009-07-28 14:20:21 -0700591 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700592 }
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596
597 commitTransaction();
598}
599
Mathias Agopian69608112011-05-19 15:38:14 -0700600void SurfaceFlinger::destroyLayer(LayerBase const* layer)
601{
602 Mutex::Autolock _l(mDestroyedLayerLock);
603 mDestroyedLayers.add(layer);
604 signalEvent();
605}
606
607void SurfaceFlinger::handleDestroyLayers()
608{
609 Vector<LayerBase const *> destroyedLayers;
610
611 { // scope for the lock
612 Mutex::Autolock _l(mDestroyedLayerLock);
613 destroyedLayers = mDestroyedLayers;
614 mDestroyedLayers.clear();
615 }
616
617 // call destructors without a lock held
618 const size_t count = destroyedLayers.size();
619 for (size_t i=0 ; i<count ; i++) {
620 //LOGD("destroying %s", destroyedLayers[i]->getName().string());
621 delete destroyedLayers[i];
622 }
623}
624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
626{
627 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
628}
629
630void SurfaceFlinger::computeVisibleRegions(
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800631 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632{
633 const GraphicPlane& plane(graphicPlane(0));
634 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700635 const DisplayHardware& hw(plane.displayHardware());
636 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
638 Region aboveOpaqueLayers;
639 Region aboveCoveredLayers;
640 Region dirty;
641
642 bool secureFrameBuffer = false;
643
644 size_t i = currentLayers.size();
645 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700646 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 layer->validateVisibility(planeTransform);
648
649 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700650 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700652 /*
653 * opaqueRegion: area of a surface that is fully opaque.
654 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700656
657 /*
658 * visibleRegion: area of a surface that is visible on screen
659 * and not fully transparent. This is essentially the layer's
660 * footprint minus the opaque regions above it.
661 * Areas covered by a translucent surface are considered visible.
662 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700664
665 /*
666 * coveredRegion: area of a surface that is covered by all
667 * visible regions above it (which includes the translucent areas).
668 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700670
671
672 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700673 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700674 const bool translucent = !layer->isOpaque();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700675 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700677 visibleRegion.andSelf(screenRegion);
678 if (!visibleRegion.isEmpty()) {
679 // Remove the transparent area from the visible region
680 if (translucent) {
681 visibleRegion.subtractSelf(layer->transparentRegionScreen);
682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700684 // compute the opaque region
685 const int32_t layerOrientation = layer->getOrientation();
686 if (s.alpha==255 && !translucent &&
687 ((layerOrientation & Transform::ROT_INVALID) == false)) {
688 // the opaque region is the layer's footprint
689 opaqueRegion = visibleRegion;
690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692 }
693
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700694 // Clip the covered region to the visible region
695 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
696
697 // Update aboveCoveredLayers for next (lower) layer
698 aboveCoveredLayers.orSelf(visibleRegion);
699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 // subtract the opaque region covered by the layers above us
701 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702
703 // compute this layer's dirty region
704 if (layer->contentDirty) {
705 // we need to invalidate the whole region
706 dirty = visibleRegion;
707 // as well, as the old visible region
708 dirty.orSelf(layer->visibleRegionScreen);
709 layer->contentDirty = false;
710 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700711 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700712 * the exposed region consists of two components:
713 * 1) what's VISIBLE now and was COVERED before
714 * 2) what's EXPOSED now less what was EXPOSED before
715 *
716 * note that (1) is conservative, we start with the whole
717 * visible region but only keep what used to be covered by
718 * something -- which mean it may have been exposed.
719 *
720 * (2) handles areas that were not covered by anything but got
721 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700722 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700723 const Region newExposed = visibleRegion - coveredRegion;
724 const Region oldVisibleRegion = layer->visibleRegionScreen;
725 const Region oldCoveredRegion = layer->coveredRegionScreen;
726 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
727 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729 dirty.subtractSelf(aboveOpaqueLayers);
730
731 // accumulate to the screen dirty region
732 dirtyRegion.orSelf(dirty);
733
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700734 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 // Store the visible region is screen space
738 layer->setVisibleRegion(visibleRegion);
739 layer->setCoveredRegion(coveredRegion);
740
Mathias Agopian12cedff2009-07-28 10:57:27 -0700741 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 if (layer->isSecure() && !visibleRegion.isEmpty()) {
743 secureFrameBuffer = true;
744 }
745 }
746
Mathias Agopian12cedff2009-07-28 10:57:27 -0700747 // invalidate the areas where a layer was removed
748 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
749 mDirtyRegionRemovedLayer.clear();
750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 mSecureFrameBuffer = secureFrameBuffer;
752 opaqueRegion = aboveOpaqueLayers;
753}
754
755
756void SurfaceFlinger::commitTransaction()
757{
758 mDrawingState = mCurrentState;
Mathias Agopian9779b222009-09-07 16:32:45 -0700759 mResizeTransationPending = false;
760 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761}
762
763void SurfaceFlinger::handlePageFlip()
764{
765 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800766 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 visibleRegions |= lockPageFlip(currentLayers);
768
769 const DisplayHardware& hw = graphicPlane(0).displayHardware();
770 const Region screenRegion(hw.bounds());
771 if (visibleRegions) {
772 Region opaqueRegion;
773 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700774
775 /*
776 * rebuild the visible layer list
777 */
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800778 const size_t count = currentLayers.size();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700779 mVisibleLayersSortedByZ.clear();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700780 mVisibleLayersSortedByZ.setCapacity(count);
781 for (size_t i=0 ; i<count ; i++) {
782 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
783 mVisibleLayersSortedByZ.add(currentLayers[i]);
784 }
785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 mWormholeRegion = screenRegion.subtract(opaqueRegion);
787 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800788 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790
791 unlockPageFlip(currentLayers);
792 mDirtyRegion.andSelf(screenRegion);
793}
794
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800795void SurfaceFlinger::invalidateHwcGeometry()
796{
797 mHwWorkListDirty = true;
798}
799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
801{
802 bool recomputeVisibleRegions = false;
803 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700804 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700806 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 layer->lockPageFlip(recomputeVisibleRegions);
808 }
809 return recomputeVisibleRegions;
810}
811
812void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
813{
814 const GraphicPlane& plane(graphicPlane(0));
815 const Transform& planeTransform(plane.transform());
816 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700817 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700819 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 layer->unlockPageFlip(planeTransform, mDirtyRegion);
821 }
822}
823
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700824void SurfaceFlinger::handleWorkList()
825{
826 mHwWorkListDirty = false;
827 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
828 if (hwc.initCheck() == NO_ERROR) {
829 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
830 const size_t count = currentLayers.size();
831 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700832 hwc_layer_t* const cur(hwc.getLayers());
833 for (size_t i=0 ; cur && i<count ; i++) {
834 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian6a969242010-09-22 18:58:01 -0700835 if (mDebugDisableHWC) {
836 cur[i].compositionType = HWC_FRAMEBUFFER;
837 cur[i].flags |= HWC_SKIP_LAYER;
838 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700839 }
840 }
841}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843void SurfaceFlinger::handleRepaint()
844{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700845 // compute the invalid region
846 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
848 if (UNLIKELY(mDebugRegion)) {
849 debugFlashRegions();
850 }
851
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700852 // set the frame buffer
853 const DisplayHardware& hw(graphicPlane(0).displayHardware());
854 glMatrixMode(GL_MODELVIEW);
855 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856
857 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700858 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
859 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700860 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700861 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
862 // takes a rectangle, we must make sure to update that whole
863 // rectangle in that case
864 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700865 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700866 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 mDirtyRegion.set(mInvalidRegion.bounds());
868 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700869 // in the BUFFER_PRESERVED case, obviously, we can update only
870 // what's needed and nothing more.
871 // NOTE: this is NOT a common case, as preserving the backbuffer
872 // is costly and usually involves copying the whole update back.
873 }
874 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700875 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700876 // We need to redraw the rectangle that will be updated
877 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700878 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700879 // rectangle instead of a region (see DisplayHardware::flip())
880 mDirtyRegion.set(mInvalidRegion.bounds());
881 } else {
882 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 mDirtyRegion.set(hw.bounds());
884 mInvalidRegion = mDirtyRegion;
885 }
886 }
887
888 // compose all surfaces
889 composeSurfaces(mDirtyRegion);
890
891 // clear the dirty regions
892 mDirtyRegion.clear();
893}
894
895void SurfaceFlinger::composeSurfaces(const Region& dirty)
896{
897 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
898 // should never happen unless the window manager has a bug
899 // draw something...
900 drawWormhole();
901 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700902
903 status_t err = NO_ERROR;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700904 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700905 size_t count = layers.size();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700906
907 const DisplayHardware& hw(graphicPlane(0).displayHardware());
908 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700909 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700910
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700911 LOGE_IF(cur && hwc.getNumLayers() != count,
912 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
913 hwc.getNumLayers(), count);
914
915 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700916 if (hwc.initCheck() == NO_ERROR) {
917 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
918 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700919
920 /*
921 * update the per-frame h/w composer data for each layer
922 * and build the transparent region of the FB
923 */
924 Region transparent;
925 if (cur) {
926 for (size_t i=0 ; i<count ; i++) {
927 const sp<LayerBase>& layer(layers[i]);
928 layer->setPerFrameData(&cur[i]);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700929 }
930 err = hwc.prepare();
931 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700932
Mathias Agopian45491692011-01-19 15:24:23 -0800933 if (err == NO_ERROR) {
934 for (size_t i=0 ; i<count ; i++) {
935 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
936 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700937 if (layer->isOpaque()) {
Mathias Agopian45491692011-01-19 15:24:23 -0800938 transparent.orSelf(layer->visibleRegionScreen);
939 }
940 }
941 }
942
943 /*
944 * clear the area of the FB that need to be transparent
945 */
946 transparent.andSelf(dirty);
947 if (!transparent.isEmpty()) {
948 glClearColor(0,0,0,0);
949 Region::const_iterator it = transparent.begin();
950 Region::const_iterator const end = transparent.end();
951 const int32_t height = hw.getHeight();
952 while (it != end) {
953 const Rect& r(*it++);
954 const GLint sy = height - (r.top + r.height());
955 glScissor(r.left, sy, r.width(), r.height());
956 glClear(GL_COLOR_BUFFER_BIT);
957 }
958 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700959 }
960 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700961
962
963 /*
964 * and then, render the layers targeted at the framebuffer
965 */
966 for (size_t i=0 ; i<count ; i++) {
967 if (cur) {
Antti Hatala982f58b2010-09-09 02:32:30 -0700968 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
969 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700970 // skip layers handled by the HAL
971 continue;
972 }
973 }
Mathias Agopian6a969242010-09-22 18:58:01 -0700974
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700975 const sp<LayerBase>& layer(layers[i]);
976 const Region clip(dirty.intersect(layer->visibleRegionScreen));
977 if (!clip.isEmpty()) {
978 layer->draw(clip);
979 }
980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981}
982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983void SurfaceFlinger::debugFlashRegions()
984{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700985 const DisplayHardware& hw(graphicPlane(0).displayHardware());
986 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700987
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700988 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
989 (flags & DisplayHardware::BUFFER_PRESERVED))) {
990 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
991 mDirtyRegion.bounds() : hw.bounds());
992 composeSurfaces(repaint);
993 }
994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 glDisable(GL_BLEND);
996 glDisable(GL_DITHER);
997 glDisable(GL_SCISSOR_TEST);
998
Mathias Agopiandff8e582009-05-04 14:17:04 -0700999 static int toggle = 0;
1000 toggle = 1 - toggle;
1001 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001002 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001003 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001004 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001007 Region::const_iterator it = mDirtyRegion.begin();
1008 Region::const_iterator const end = mDirtyRegion.end();
1009 while (it != end) {
1010 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 GLfloat vertices[][2] = {
1012 { r.left, r.top },
1013 { r.left, r.bottom },
1014 { r.right, r.bottom },
1015 { r.right, r.top }
1016 };
1017 glVertexPointer(2, GL_FLOAT, 0, vertices);
1018 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1019 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001020
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001021 if (mInvalidRegion.isEmpty()) {
1022 mDirtyRegion.dump("mDirtyRegion");
1023 mInvalidRegion.dump("mInvalidRegion");
1024 }
1025 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
1027 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001028 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
1030 glEnable(GL_SCISSOR_TEST);
1031 //mDirtyRegion.dump("mDirtyRegion");
1032}
1033
1034void SurfaceFlinger::drawWormhole() const
1035{
1036 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1037 if (region.isEmpty())
1038 return;
1039
1040 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1041 const int32_t width = hw.getWidth();
1042 const int32_t height = hw.getHeight();
1043
1044 glDisable(GL_BLEND);
1045 glDisable(GL_DITHER);
1046
1047 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001048 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001049 Region::const_iterator it = region.begin();
1050 Region::const_iterator const end = region.end();
1051 while (it != end) {
1052 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 const GLint sy = height - (r.top + r.height());
1054 glScissor(r.left, sy, r.width(), r.height());
1055 glClear(GL_COLOR_BUFFER_BIT);
1056 }
1057 } else {
1058 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1059 { width, height }, { 0, height } };
1060 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1061 glVertexPointer(2, GL_SHORT, 0, vertices);
1062 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1063 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001064#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001065 if (GLExtensions::getInstance().haveTextureExternal()) {
1066 glDisable(GL_TEXTURE_EXTERNAL_OES);
1067 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001068#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 glEnable(GL_TEXTURE_2D);
1070 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1071 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1072 glMatrixMode(GL_TEXTURE);
1073 glLoadIdentity();
1074 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001075 Region::const_iterator it = region.begin();
1076 Region::const_iterator const end = region.end();
1077 while (it != end) {
1078 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 const GLint sy = height - (r.top + r.height());
1080 glScissor(r.left, sy, r.width(), r.height());
1081 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1082 }
1083 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001084 glDisable(GL_TEXTURE_2D);
Mathias Agopian04a709e2010-12-14 18:38:36 -08001085 glLoadIdentity();
1086 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088}
1089
1090void SurfaceFlinger::debugShowFPS() const
1091{
1092 static int mFrameCount;
1093 static int mLastFrameCount = 0;
1094 static nsecs_t mLastFpsTime = 0;
1095 static float mFps = 0;
1096 mFrameCount++;
1097 nsecs_t now = systemTime();
1098 nsecs_t diff = now - mLastFpsTime;
1099 if (diff > ms2ns(250)) {
1100 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1101 mLastFpsTime = now;
1102 mLastFrameCount = mFrameCount;
1103 }
1104 // XXX: mFPS has the value we want
1105 }
1106
Mathias Agopian1473f462009-04-10 14:24:30 -07001107status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108{
1109 Mutex::Autolock _l(mStateLock);
1110 addLayer_l(layer);
1111 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1112 return NO_ERROR;
1113}
1114
Mathias Agopian593c05c2010-06-02 23:28:45 -07001115status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1116{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001117 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001118 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1119}
1120
1121ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1122 const sp<LayerBaseClient>& lbc)
1123{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001124 // attach this layer to the client
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001125 size_t name = client->attachLayer(lbc);
1126
1127 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001128
1129 // add this layer to the current state list
1130 addLayer_l(lbc);
1131
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001132 return ssize_t(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001133}
1134
Mathias Agopian1473f462009-04-10 14:24:30 -07001135status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136{
1137 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001138 status_t err = purgatorizeLayer_l(layer);
1139 if (err == NO_ERROR)
1140 setTransactionFlags(eTransactionNeeded);
1141 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142}
1143
Mathias Agopian1473f462009-04-10 14:24:30 -07001144status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145{
Mathias Agopian7623da42010-06-01 15:12:58 -07001146 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1147 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001148 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1151 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001152 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 return NO_ERROR;
1154 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001155 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156}
1157
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001158status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1159{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001160 // First add the layer to the purgatory list, which makes sure it won't
1161 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001162 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001163 if (err >= 0) {
1164 mLayerPurgatory.add(layerBase);
1165 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001166
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001167 layerBase->onRemoved();
1168
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001169 // it's possible that we don't find a layer, because it might
1170 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001171 // from the user because there is a race between Client::destroySurface(),
1172 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001173 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1174}
1175
Mathias Agopian593c05c2010-06-02 23:28:45 -07001176status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001178 layer->forceVisibilityTransaction();
1179 setTransactionFlags(eTraversalNeeded);
1180 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181}
1182
Mathias Agopian6dcb1552011-05-03 17:04:02 -07001183uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1184{
1185 return android_atomic_release_load(&mTransactionFlags);
1186}
1187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1189{
1190 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1191}
1192
Mathias Agopian898c4c92010-05-18 17:06:55 -07001193uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194{
1195 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1196 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001197 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 return old;
1200}
1201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202
Mathias Agopian439863f2011-06-28 19:09:31 -07001203void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state) {
1204 Mutex::Autolock _l(mStateLock);
Mathias Agopian9779b222009-09-07 16:32:45 -07001205
Mathias Agopian439863f2011-06-28 19:09:31 -07001206 uint32_t flags = 0;
1207 const size_t count = state.size();
1208 for (size_t i=0 ; i<count ; i++) {
1209 const ComposerState& s(state[i]);
1210 sp<Client> client( static_cast<Client *>(s.client.get()) );
1211 flags |= setClientStateLocked(client, s.state);
1212 }
1213 if (flags) {
1214 setTransactionFlags(flags);
1215 }
1216
1217 signalEvent();
1218
1219 // if there is a transaction with a resize, wait for it to
1220 // take effect before returning.
1221 while (mResizeTransationPending) {
1222 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1223 if (CC_UNLIKELY(err != NO_ERROR)) {
1224 // just in case something goes wrong in SF, return to the
1225 // called after a few seconds.
1226 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1227 mResizeTransationPending = false;
1228 break;
Mathias Agopian9779b222009-09-07 16:32:45 -07001229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
1231}
1232
1233status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1234{
1235 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1236 return BAD_VALUE;
1237
1238 Mutex::Autolock _l(mStateLock);
1239 mCurrentState.freezeDisplay = 1;
1240 setTransactionFlags(eTransactionNeeded);
1241
1242 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001243 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 return NO_ERROR;
1245}
1246
1247status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1248{
1249 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1250 return BAD_VALUE;
1251
1252 Mutex::Autolock _l(mStateLock);
1253 mCurrentState.freezeDisplay = 0;
1254 setTransactionFlags(eTransactionNeeded);
1255
1256 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001257 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 return NO_ERROR;
1259}
1260
Andreas Hubere3c01832010-08-16 08:49:37 -07001261int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001262 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263{
1264 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1265 return BAD_VALUE;
1266
1267 Mutex::Autolock _l(mStateLock);
1268 if (mCurrentState.orientation != orientation) {
1269 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001270 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 mCurrentState.orientation = orientation;
1272 setTransactionFlags(eTransactionNeeded);
1273 mTransactionCV.wait(mStateLock);
1274 } else {
1275 orientation = BAD_VALUE;
1276 }
1277 }
1278 return orientation;
1279}
1280
Mathias Agopian9638e5c2011-04-20 14:19:32 -07001281sp<ISurface> SurfaceFlinger::createSurface(
1282 ISurfaceComposerClient::surface_data_t* params,
1283 const String8& name,
1284 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1286 uint32_t flags)
1287{
Mathias Agopian1473f462009-04-10 14:24:30 -07001288 sp<LayerBaseClient> layer;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001289 sp<ISurface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001290
1291 if (int32_t(w|h) < 0) {
1292 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1293 int(w), int(h));
1294 return surfaceHandle;
1295 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001298 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 switch (flags & eFXSurfaceMask) {
1300 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001301 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1302 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 break;
1304 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001305 // for now we treat Blur as Dim, until we can implement it
1306 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001308 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 break;
1310 }
1311
Mathias Agopian1473f462009-04-10 14:24:30 -07001312 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001313 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001314 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001315 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001318 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001319 params->token = token;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001320 params->identity = layer->getIdentity();
Mathias Agopian18b6b492009-08-19 17:46:26 -07001321 params->width = w;
1322 params->height = h;
1323 params->format = format;
Mathias Agopian7623da42010-06-01 15:12:58 -07001324 if (normalLayer != 0) {
1325 Mutex::Autolock _l(mStateLock);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001326 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001327 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001328 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001329
Mathias Agopian593c05c2010-06-02 23:28:45 -07001330 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332
1333 return surfaceHandle;
1334}
1335
Mathias Agopian7623da42010-06-01 15:12:58 -07001336sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001337 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001338 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001339 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340{
1341 // initialize the surfaces
1342 switch (format) { // TODO: take h/w into account
1343 case PIXEL_FORMAT_TRANSPARENT:
1344 case PIXEL_FORMAT_TRANSLUCENT:
1345 format = PIXEL_FORMAT_RGBA_8888;
1346 break;
1347 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001348#ifdef NO_RGBX_8888
1349 format = PIXEL_FORMAT_RGB_565;
1350#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001351 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001352#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 break;
1354 }
1355
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001356#ifdef NO_RGBX_8888
1357 if (format == PIXEL_FORMAT_RGBX_8888)
1358 format = PIXEL_FORMAT_RGBA_8888;
1359#endif
1360
Mathias Agopian593c05c2010-06-02 23:28:45 -07001361 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001362 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001363 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001365 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
1367 return layer;
1368}
1369
Mathias Agopian7623da42010-06-01 15:12:58 -07001370sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001371 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001372 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001374 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 return layer;
1377}
1378
Mathias Agopian593c05c2010-06-02 23:28:45 -07001379status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001381 /*
1382 * called by the window manager, when a surface should be marked for
1383 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001384 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001385 * The surface is removed from the current and drawing lists, but placed
1386 * in the purgatory queue, so it's not destroyed right-away (we need
1387 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001388 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001389
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001390 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001391 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001392 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001393 if (layer != 0) {
1394 err = purgatorizeLayer_l(layer);
1395 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001396 setTransactionFlags(eTransactionNeeded);
1397 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001398 }
1399 return err;
1400}
1401
Mathias Agopian69608112011-05-19 15:38:14 -07001402status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001403{
Mathias Agopian359140c2009-07-02 17:33:40 -07001404 // called by ~ISurface() when all references are gone
Mathias Agopian69608112011-05-19 15:38:14 -07001405 status_t err = NO_ERROR;
1406 sp<LayerBaseClient> l(layer.promote());
1407 if (l != NULL) {
1408 Mutex::Autolock _l(mStateLock);
1409 err = removeLayer_l(l);
1410 if (err == NAME_NOT_FOUND) {
1411 // The surface wasn't in the current list, which means it was
1412 // removed already, which means it is in the purgatory,
1413 // and need to be removed from there.
1414 ssize_t idx = mLayerPurgatory.remove(l);
1415 LOGE_IF(idx < 0,
1416 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001417 }
Mathias Agopian69608112011-05-19 15:38:14 -07001418 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1419 "error removing layer=%p (%s)", l.get(), strerror(-err));
1420 }
1421 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422}
1423
Mathias Agopian439863f2011-06-28 19:09:31 -07001424uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001425 const sp<Client>& client,
Mathias Agopian439863f2011-06-28 19:09:31 -07001426 const layer_state_t& s)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 uint32_t flags = 0;
Mathias Agopian439863f2011-06-28 19:09:31 -07001429 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1430 if (layer != 0) {
1431 const uint32_t what = s.what;
1432 if (what & ePositionChanged) {
1433 if (layer->setPosition(s.x, s.y))
1434 flags |= eTraversalNeeded;
1435 }
1436 if (what & eLayerChanged) {
1437 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1438 if (layer->setLayer(s.z)) {
1439 mCurrentState.layersSortedByZ.removeAt(idx);
1440 mCurrentState.layersSortedByZ.add(layer);
1441 // we need traversal (state changed)
1442 // AND transaction (list changed)
1443 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
1445 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001446 if (what & eSizeChanged) {
1447 if (layer->setSize(s.w, s.h)) {
1448 flags |= eTraversalNeeded;
1449 mResizeTransationPending = true;
1450 }
1451 }
1452 if (what & eAlphaChanged) {
1453 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1454 flags |= eTraversalNeeded;
1455 }
1456 if (what & eMatrixChanged) {
1457 if (layer->setMatrix(s.matrix))
1458 flags |= eTraversalNeeded;
1459 }
1460 if (what & eTransparentRegionChanged) {
1461 if (layer->setTransparentRegionHint(s.transparentRegion))
1462 flags |= eTraversalNeeded;
1463 }
1464 if (what & eVisibilityChanged) {
1465 if (layer->setFlags(s.flags, s.mask))
1466 flags |= eTraversalNeeded;
1467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001469 return flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470}
1471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472void SurfaceFlinger::screenReleased(int dpy)
1473{
1474 // this may be called by a signal handler, we can't do too much in here
1475 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1476 signalEvent();
1477}
1478
1479void SurfaceFlinger::screenAcquired(int dpy)
1480{
1481 // this may be called by a signal handler, we can't do too much in here
1482 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1483 signalEvent();
1484}
1485
1486status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1487{
Erik Gilling94720d72010-12-01 16:38:01 -08001488 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 char buffer[SIZE];
1490 String8 result;
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001491
1492 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 snprintf(buffer, SIZE, "Permission Denial: "
1494 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1495 IPCThreadState::self()->getCallingPid(),
1496 IPCThreadState::self()->getCallingUid());
1497 result.append(buffer);
1498 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001499
1500 // figure out if we're stuck somewhere
1501 const nsecs_t now = systemTime();
1502 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1503 const nsecs_t inTransaction(mDebugInTransaction);
1504 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1505 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1506
1507 // Try to get the main lock, but don't insist if we can't
1508 // (this would indicate SF is stuck, but we want to be able to
1509 // print something in dumpsys).
1510 int retry = 3;
1511 while (mStateLock.tryLock()<0 && --retry>=0) {
1512 usleep(1000000);
1513 }
1514 const bool locked(retry >= 0);
1515 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001516 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001517 "SurfaceFlinger appears to be unresponsive, "
1518 "dumping anyways (no locks held)\n");
1519 result.append(buffer);
1520 }
1521
Mathias Agopian06a61e22011-01-19 16:15:53 -08001522 /*
1523 * Dump the visible layer list
1524 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1526 const size_t count = currentLayers.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001527 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1528 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001530 const sp<LayerBase>& layer(currentLayers[i]);
1531 layer->dump(result, buffer, SIZE);
1532 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 s.transparentRegion.dump(result, "transparentRegion");
1534 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1535 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1536 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001537
Mathias Agopian06a61e22011-01-19 16:15:53 -08001538 /*
1539 * Dump the layers in the purgatory
1540 */
1541
1542 const size_t purgatorySize = mLayerPurgatory.size();
1543 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1544 result.append(buffer);
1545 for (size_t i=0 ; i<purgatorySize ; i++) {
1546 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1547 layer->shortDump(result, buffer, SIZE);
1548 }
1549
1550 /*
1551 * Dump SurfaceFlinger global state
1552 */
1553
1554 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1555 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 mWormholeRegion.dump(result, "WormholeRegion");
1557 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1558 snprintf(buffer, SIZE,
1559 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1560 mFreezeDisplay?"yes":"no", mFreezeCount,
1561 mCurrentState.orientation, hw.canDraw());
1562 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001563 snprintf(buffer, SIZE,
1564 " last eglSwapBuffers() time: %f us\n"
1565 " last transaction time : %f us\n",
1566 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1567 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001568
Mathias Agopiana8d49172009-08-26 16:36:26 -07001569 if (inSwapBuffersDuration || !locked) {
1570 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1571 inSwapBuffersDuration/1000.0);
1572 result.append(buffer);
1573 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001574
Mathias Agopiana8d49172009-08-26 16:36:26 -07001575 if (inTransactionDuration || !locked) {
1576 snprintf(buffer, SIZE, " transaction time: %f us\n",
1577 inTransactionDuration/1000.0);
1578 result.append(buffer);
1579 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001580
Mathias Agopian06a61e22011-01-19 16:15:53 -08001581 /*
1582 * Dump HWComposer state
1583 */
Mathias Agopian6a969242010-09-22 18:58:01 -07001584 HWComposer& hwc(hw.getHwComposer());
1585 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1586 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1587 mDebugDisableHWC ? "disabled" : "enabled");
1588 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -07001589 hwc.dump(result, buffer, SIZE);
Mathias Agopian6a969242010-09-22 18:58:01 -07001590
Mathias Agopian06a61e22011-01-19 16:15:53 -08001591 /*
1592 * Dump gralloc state
1593 */
Mathias Agopian6950e422009-10-05 17:07:12 -07001594 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001595 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001596 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001597
1598 if (locked) {
1599 mStateLock.unlock();
1600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
1602 write(fd, result.string(), result.size());
1603 return NO_ERROR;
1604}
1605
1606status_t SurfaceFlinger::onTransact(
1607 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1608{
1609 switch (code) {
1610 case CREATE_CONNECTION:
Mathias Agopian439863f2011-06-28 19:09:31 -07001611 case SET_TRANSACTION_STATE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 case SET_ORIENTATION:
1613 case FREEZE_DISPLAY:
1614 case UNFREEZE_DISPLAY:
1615 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001616 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001617 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 {
1619 // codes that require permission check
1620 IPCThreadState* ipc = IPCThreadState::self();
1621 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001622 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001623 if ((uid != AID_GRAPHICS) &&
1624 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001625 LOGE("Permission Denial: "
1626 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1627 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001629 break;
1630 }
1631 case CAPTURE_SCREEN:
1632 {
1633 // codes that require permission check
1634 IPCThreadState* ipc = IPCThreadState::self();
1635 const int pid = ipc->getCallingPid();
1636 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001637 if ((uid != AID_GRAPHICS) &&
1638 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001639 LOGE("Permission Denial: "
1640 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1641 return PERMISSION_DENIED;
1642 }
1643 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1648 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001649 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001650 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001651 IPCThreadState* ipc = IPCThreadState::self();
1652 const int pid = ipc->getCallingPid();
1653 const int uid = ipc->getCallingUid();
1654 LOGE("Permission Denial: "
1655 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 return PERMISSION_DENIED;
1657 }
1658 int n;
1659 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001660 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001661 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 return NO_ERROR;
1663 case 1002: // SHOW_UPDATES
1664 n = data.readInt32();
1665 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1666 return NO_ERROR;
1667 case 1003: // SHOW_BACKGROUND
1668 n = data.readInt32();
1669 mDebugBackground = n ? 1 : 0;
1670 return NO_ERROR;
Mathias Agopian6a969242010-09-22 18:58:01 -07001671 case 1008: // toggle use of hw composer
1672 n = data.readInt32();
1673 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -08001674 invalidateHwcGeometry();
Mathias Agopian6a969242010-09-22 18:58:01 -07001675 // fall-through...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 case 1004:{ // repaint everything
1677 Mutex::Autolock _l(mStateLock);
1678 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1679 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1680 signalEvent();
Mathias Agopian9779b222009-09-07 16:32:45 -07001681 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001683 case 1005:{ // force transaction
1684 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1685 return NO_ERROR;
1686 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001687 case 1006:{ // enable/disable GraphicLog
1688 int enabled = data.readInt32();
1689 GraphicLog::getInstance().setEnabled(enabled);
1690 return NO_ERROR;
1691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 case 1007: // set mFreezeCount
1693 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001694 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 return NO_ERROR;
1696 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001697 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 reply->writeInt32(0);
1699 reply->writeInt32(mDebugRegion);
1700 reply->writeInt32(mDebugBackground);
1701 return NO_ERROR;
1702 case 1013: {
1703 Mutex::Autolock _l(mStateLock);
1704 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1705 reply->writeInt32(hw.getPageFlipCount());
1706 }
1707 return NO_ERROR;
1708 }
1709 }
1710 return err;
1711}
1712
Mathias Agopianaab758e2010-10-11 12:37:43 -07001713// ---------------------------------------------------------------------------
1714
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001715status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1716 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001717{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001718 if (!GLExtensions::getInstance().haveFramebufferObject())
1719 return INVALID_OPERATION;
1720
1721 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001722 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001723 const uint32_t hw_w = hw.getWidth();
1724 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001725 GLfloat u = 1;
1726 GLfloat v = 1;
1727
1728 // make sure to clear all GL error flags
1729 while ( glGetError() != GL_NO_ERROR ) ;
1730
1731 // create a FBO
1732 GLuint name, tname;
1733 glGenTextures(1, &tname);
1734 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001735 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1736 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001737 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001738 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001739 GLint tw = (2 << (31 - clz(hw_w)));
1740 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001741 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1742 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001743 u = GLfloat(hw_w) / tw;
1744 v = GLfloat(hw_h) / th;
1745 }
1746 glGenFramebuffersOES(1, &name);
1747 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001748 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1749 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001750
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001751 // redraw the screen entirely...
1752 glClearColor(0,0,0,1);
1753 glClear(GL_COLOR_BUFFER_BIT);
1754 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1755 const size_t count = layers.size();
1756 for (size_t i=0 ; i<count ; ++i) {
1757 const sp<LayerBase>& layer(layers[i]);
1758 layer->drawForSreenShot();
1759 }
1760
1761 // back to main framebuffer
1762 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1763 glDisable(GL_SCISSOR_TEST);
1764 glDeleteFramebuffersOES(1, &name);
1765
1766 *textureName = tname;
1767 *uOut = u;
1768 *vOut = v;
1769 return NO_ERROR;
1770}
1771
1772// ---------------------------------------------------------------------------
1773
1774status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1775{
1776 status_t result = PERMISSION_DENIED;
1777
1778 if (!GLExtensions::getInstance().haveFramebufferObject())
1779 return INVALID_OPERATION;
1780
1781 // get screen geometry
1782 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1783 const uint32_t hw_w = hw.getWidth();
1784 const uint32_t hw_h = hw.getHeight();
1785 const Region screenBounds(hw.bounds());
1786
1787 GLfloat u, v;
1788 GLuint tname;
1789 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1790 if (result != NO_ERROR) {
1791 return result;
1792 }
1793
1794 GLfloat vtx[8];
Mathias Agopian3a831d22011-07-07 17:30:31 -07001795 const GLfloat texCoords[4][2] = { {0,1}, {0,1-v}, {u,1-v}, {u,1} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001796 glBindTexture(GL_TEXTURE_2D, tname);
1797 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1798 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1799 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1800 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1801 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1802 glVertexPointer(2, GL_FLOAT, 0, vtx);
1803
Mathias Agopian3a831d22011-07-07 17:30:31 -07001804 /*
1805 * Texture coordinate mapping
1806 *
1807 * u
1808 * 1 +----------+---+
1809 * | | | | image is inverted
1810 * | V | | w.r.t. the texture
1811 * 1-v +----------+ | coordinates
1812 * | |
1813 * | |
1814 * | |
1815 * 0 +--------------+
1816 * 0 1
1817 *
1818 */
1819
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001820 class s_curve_interpolator {
1821 const float nbFrames, s, v;
1822 public:
1823 s_curve_interpolator(int nbFrames, float s)
1824 : nbFrames(1.0f / (nbFrames-1)), s(s),
1825 v(1.0f + expf(-s + 0.5f*s)) {
1826 }
1827 float operator()(int f) {
1828 const float x = f * nbFrames;
1829 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1830 }
1831 };
1832
1833 class v_stretch {
1834 const GLfloat hw_w, hw_h;
1835 public:
1836 v_stretch(uint32_t hw_w, uint32_t hw_h)
1837 : hw_w(hw_w), hw_h(hw_h) {
1838 }
1839 void operator()(GLfloat* vtx, float v) {
1840 const GLfloat w = hw_w + (hw_w * v);
1841 const GLfloat h = hw_h - (hw_h * v);
1842 const GLfloat x = (hw_w - w) * 0.5f;
1843 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001844 vtx[0] = x; vtx[1] = y;
1845 vtx[2] = x; vtx[3] = y + h;
1846 vtx[4] = x + w; vtx[5] = y + h;
1847 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001848 }
1849 };
1850
1851 class h_stretch {
1852 const GLfloat hw_w, hw_h;
1853 public:
1854 h_stretch(uint32_t hw_w, uint32_t hw_h)
1855 : hw_w(hw_w), hw_h(hw_h) {
1856 }
1857 void operator()(GLfloat* vtx, float v) {
1858 const GLfloat w = hw_w - (hw_w * v);
1859 const GLfloat h = 1.0f;
1860 const GLfloat x = (hw_w - w) * 0.5f;
1861 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001862 vtx[0] = x; vtx[1] = y;
1863 vtx[2] = x; vtx[3] = y + h;
1864 vtx[4] = x + w; vtx[5] = y + h;
1865 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001866 }
1867 };
1868
1869 // the full animation is 24 frames
Mathias Agopian3a831d22011-07-07 17:30:31 -07001870 char value[PROPERTY_VALUE_MAX];
1871 property_get("debug.sf.electron_frames", value, "24");
1872 int nbFrames = (atoi(value) + 1) >> 1;
1873 if (nbFrames <= 0) // just in case
1874 nbFrames = 24;
1875
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001876 s_curve_interpolator itr(nbFrames, 7.5f);
1877 s_curve_interpolator itg(nbFrames, 8.0f);
1878 s_curve_interpolator itb(nbFrames, 8.5f);
1879
1880 v_stretch vverts(hw_w, hw_h);
1881 glEnable(GL_BLEND);
1882 glBlendFunc(GL_ONE, GL_ONE);
1883 for (int i=0 ; i<nbFrames ; i++) {
1884 float x, y, w, h;
1885 const float vr = itr(i);
1886 const float vg = itg(i);
1887 const float vb = itb(i);
1888
1889 // clear screen
1890 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001891 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001892 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001893
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001894 // draw the red plane
1895 vverts(vtx, vr);
1896 glColorMask(1,0,0,1);
1897 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001898
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001899 // draw the green plane
1900 vverts(vtx, vg);
1901 glColorMask(0,1,0,1);
1902 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001903
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001904 // draw the blue plane
1905 vverts(vtx, vb);
1906 glColorMask(0,0,1,1);
1907 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001908
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001909 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001910 glDisable(GL_TEXTURE_2D);
1911 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001912 glColor4f(vg, vg, vg, 1);
1913 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1914 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001915 }
1916
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001917 h_stretch hverts(hw_w, hw_h);
1918 glDisable(GL_BLEND);
1919 glDisable(GL_TEXTURE_2D);
1920 glColorMask(1,1,1,1);
1921 for (int i=0 ; i<nbFrames ; i++) {
1922 const float v = itg(i);
1923 hverts(vtx, v);
1924 glClear(GL_COLOR_BUFFER_BIT);
1925 glColor4f(1-v, 1-v, 1-v, 1);
1926 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1927 hw.flip(screenBounds);
1928 }
1929
1930 glColorMask(1,1,1,1);
1931 glEnable(GL_SCISSOR_TEST);
1932 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1933 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001934 glDisable(GL_TEXTURE_2D);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001935 return NO_ERROR;
1936}
1937
1938status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1939{
1940 status_t result = PERMISSION_DENIED;
1941
1942 if (!GLExtensions::getInstance().haveFramebufferObject())
1943 return INVALID_OPERATION;
1944
1945
1946 // get screen geometry
1947 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1948 const uint32_t hw_w = hw.getWidth();
1949 const uint32_t hw_h = hw.getHeight();
1950 const Region screenBounds(hw.bounds());
1951
1952 GLfloat u, v;
1953 GLuint tname;
1954 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1955 if (result != NO_ERROR) {
1956 return result;
1957 }
1958
1959 // back to main framebuffer
1960 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1961 glDisable(GL_SCISSOR_TEST);
1962
1963 GLfloat vtx[8];
1964 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001965 glBindTexture(GL_TEXTURE_2D, tname);
1966 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1967 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1968 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1969 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1970 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1971 glVertexPointer(2, GL_FLOAT, 0, vtx);
1972
1973 class s_curve_interpolator {
1974 const float nbFrames, s, v;
1975 public:
1976 s_curve_interpolator(int nbFrames, float s)
1977 : nbFrames(1.0f / (nbFrames-1)), s(s),
1978 v(1.0f + expf(-s + 0.5f*s)) {
1979 }
1980 float operator()(int f) {
1981 const float x = f * nbFrames;
1982 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1983 }
1984 };
1985
1986 class v_stretch {
1987 const GLfloat hw_w, hw_h;
1988 public:
1989 v_stretch(uint32_t hw_w, uint32_t hw_h)
1990 : hw_w(hw_w), hw_h(hw_h) {
1991 }
1992 void operator()(GLfloat* vtx, float v) {
1993 const GLfloat w = hw_w + (hw_w * v);
1994 const GLfloat h = hw_h - (hw_h * v);
1995 const GLfloat x = (hw_w - w) * 0.5f;
1996 const GLfloat y = (hw_h - h) * 0.5f;
1997 vtx[0] = x; vtx[1] = y;
1998 vtx[2] = x; vtx[3] = y + h;
1999 vtx[4] = x + w; vtx[5] = y + h;
2000 vtx[6] = x + w; vtx[7] = y;
2001 }
2002 };
2003
2004 class h_stretch {
2005 const GLfloat hw_w, hw_h;
2006 public:
2007 h_stretch(uint32_t hw_w, uint32_t hw_h)
2008 : hw_w(hw_w), hw_h(hw_h) {
2009 }
2010 void operator()(GLfloat* vtx, float v) {
2011 const GLfloat w = hw_w - (hw_w * v);
2012 const GLfloat h = 1.0f;
2013 const GLfloat x = (hw_w - w) * 0.5f;
2014 const GLfloat y = (hw_h - h) * 0.5f;
2015 vtx[0] = x; vtx[1] = y;
2016 vtx[2] = x; vtx[3] = y + h;
2017 vtx[4] = x + w; vtx[5] = y + h;
2018 vtx[6] = x + w; vtx[7] = y;
2019 }
2020 };
2021
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002022 // the full animation is 12 frames
2023 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002024 s_curve_interpolator itr(nbFrames, 7.5f);
2025 s_curve_interpolator itg(nbFrames, 8.0f);
2026 s_curve_interpolator itb(nbFrames, 8.5f);
2027
2028 h_stretch hverts(hw_w, hw_h);
2029 glDisable(GL_BLEND);
2030 glDisable(GL_TEXTURE_2D);
2031 glColorMask(1,1,1,1);
2032 for (int i=nbFrames-1 ; i>=0 ; i--) {
2033 const float v = itg(i);
2034 hverts(vtx, v);
2035 glClear(GL_COLOR_BUFFER_BIT);
2036 glColor4f(1-v, 1-v, 1-v, 1);
2037 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2038 hw.flip(screenBounds);
2039 }
2040
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002041 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002042 v_stretch vverts(hw_w, hw_h);
2043 glEnable(GL_BLEND);
2044 glBlendFunc(GL_ONE, GL_ONE);
2045 for (int i=nbFrames-1 ; i>=0 ; i--) {
2046 float x, y, w, h;
2047 const float vr = itr(i);
2048 const float vg = itg(i);
2049 const float vb = itb(i);
2050
2051 // clear screen
2052 glColorMask(1,1,1,1);
2053 glClear(GL_COLOR_BUFFER_BIT);
2054 glEnable(GL_TEXTURE_2D);
2055
2056 // draw the red plane
2057 vverts(vtx, vr);
2058 glColorMask(1,0,0,1);
2059 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2060
2061 // draw the green plane
2062 vverts(vtx, vg);
2063 glColorMask(0,1,0,1);
2064 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2065
2066 // draw the blue plane
2067 vverts(vtx, vb);
2068 glColorMask(0,0,1,1);
2069 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2070
2071 hw.flip(screenBounds);
2072 }
2073
2074 glColorMask(1,1,1,1);
2075 glEnable(GL_SCISSOR_TEST);
2076 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002077 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002078 glDisable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002079
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002080 return NO_ERROR;
2081}
2082
2083// ---------------------------------------------------------------------------
2084
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002085status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002086{
2087 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2088 if (!hw.canDraw()) {
2089 // we're already off
2090 return NO_ERROR;
2091 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002092 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2093 electronBeamOffAnimationImplLocked();
2094 }
2095
2096 // always clear the whole screen at the end of the animation
2097 glClearColor(0,0,0,1);
2098 glDisable(GL_SCISSOR_TEST);
2099 glClear(GL_COLOR_BUFFER_BIT);
2100 glEnable(GL_SCISSOR_TEST);
2101 hw.flip( Region(hw.bounds()) );
2102
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002103 hw.setCanDraw(false);
2104 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002105}
2106
2107status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2108{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002109 class MessageTurnElectronBeamOff : public MessageBase {
2110 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002111 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002112 status_t result;
2113 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002114 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2115 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002116 }
2117 status_t getResult() const {
2118 return result;
2119 }
2120 virtual bool handler() {
2121 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002122 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002123 return true;
2124 }
2125 };
2126
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002127 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002128 status_t res = postMessageSync(msg);
2129 if (res == NO_ERROR) {
2130 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002131
2132 // work-around: when the power-manager calls us we activate the
2133 // animation. eventually, the "on" animation will be called
2134 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002135 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002136 }
2137 return res;
2138}
2139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002141
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002142status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002143{
2144 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2145 if (hw.canDraw()) {
2146 // we're already on
2147 return NO_ERROR;
2148 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002149 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2150 electronBeamOnAnimationImplLocked();
2151 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002152 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002153
2154 // make sure to redraw the whole screen when the animation is done
2155 mDirtyRegion.set(hw.bounds());
2156 signalEvent();
2157
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002158 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002159}
2160
2161status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2162{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002163 class MessageTurnElectronBeamOn : public MessageBase {
2164 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002165 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002166 status_t result;
2167 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002168 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2169 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002170 }
2171 status_t getResult() const {
2172 return result;
2173 }
2174 virtual bool handler() {
2175 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002176 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002177 return true;
2178 }
2179 };
2180
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002181 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002182 return NO_ERROR;
2183}
2184
2185// ---------------------------------------------------------------------------
2186
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002187status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2188 sp<IMemoryHeap>* heap,
2189 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002190 uint32_t sw, uint32_t sh,
2191 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002192{
2193 status_t result = PERMISSION_DENIED;
2194
2195 // only one display supported for now
2196 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2197 return BAD_VALUE;
2198
Jamie Gennisf72606c2011-03-09 17:05:02 -08002199 // make sure none of the layers are protected
2200 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2201 const size_t count = layers.size();
2202 for (size_t i=0 ; i<count ; ++i) {
2203 const sp<LayerBase>& layer(layers[i]);
2204 const uint32_t z = layer->drawingState().z;
2205 if (z >= minLayerZ && z <= maxLayerZ) {
2206 if (layer->isProtected()) {
2207 return INVALID_OPERATION;
2208 }
2209 }
2210 }
2211
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002212 if (!GLExtensions::getInstance().haveFramebufferObject())
2213 return INVALID_OPERATION;
2214
2215 // get screen geometry
2216 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2217 const uint32_t hw_w = hw.getWidth();
2218 const uint32_t hw_h = hw.getHeight();
2219
2220 if ((sw > hw_w) || (sh > hw_h))
2221 return BAD_VALUE;
2222
2223 sw = (!sw) ? hw_w : sw;
2224 sh = (!sh) ? hw_h : sh;
2225 const size_t size = sw * sh * 4;
2226
Mathias Agopian32ae0942011-03-02 18:45:50 -08002227 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2228 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002229
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002230 // make sure to clear all GL error flags
2231 while ( glGetError() != GL_NO_ERROR ) ;
2232
2233 // create a FBO
2234 GLuint name, tname;
2235 glGenRenderbuffersOES(1, &tname);
2236 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2237 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2238 glGenFramebuffersOES(1, &name);
2239 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2240 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2241 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2242
2243 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002244
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002245 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2246
2247 // invert everything, b/c glReadPixel() below will invert the FB
2248 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002249 glScissor(0, 0, sw, sh);
Mathias Agopian3a831d22011-07-07 17:30:31 -07002250 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002251 glMatrixMode(GL_PROJECTION);
2252 glPushMatrix();
2253 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -07002254 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002255 glMatrixMode(GL_MODELVIEW);
2256
2257 // redraw the screen entirely...
2258 glClearColor(0,0,0,1);
2259 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002260
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002261 for (size_t i=0 ; i<count ; ++i) {
2262 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002263 const uint32_t z = layer->drawingState().z;
2264 if (z >= minLayerZ && z <= maxLayerZ) {
2265 layer->drawForSreenShot();
2266 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002267 }
2268
2269 // XXX: this is needed on tegra
Mathias Agopian3a831d22011-07-07 17:30:31 -07002270 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002271 glScissor(0, 0, sw, sh);
2272
2273 // check for errors and return screen capture
2274 if (glGetError() != GL_NO_ERROR) {
2275 // error while rendering
2276 result = INVALID_OPERATION;
2277 } else {
2278 // allocate shared memory large enough to hold the
2279 // screen capture
2280 sp<MemoryHeapBase> base(
2281 new MemoryHeapBase(size, 0, "screen-capture") );
2282 void* const ptr = base->getBase();
2283 if (ptr) {
2284 // capture the screen with glReadPixels()
2285 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2286 if (glGetError() == GL_NO_ERROR) {
2287 *heap = base;
2288 *w = sw;
2289 *h = sh;
2290 *f = PIXEL_FORMAT_RGBA_8888;
2291 result = NO_ERROR;
2292 }
2293 } else {
2294 result = NO_MEMORY;
2295 }
2296 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002297 glEnable(GL_SCISSOR_TEST);
2298 glViewport(0, 0, hw_w, hw_h);
2299 glMatrixMode(GL_PROJECTION);
2300 glPopMatrix();
2301 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002302 } else {
2303 result = BAD_VALUE;
2304 }
2305
2306 // release FBO resources
2307 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2308 glDeleteRenderbuffersOES(1, &tname);
2309 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002310
2311 hw.compositionComplete();
2312
Mathias Agopian32ae0942011-03-02 18:45:50 -08002313 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002314
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002315 return result;
2316}
2317
2318
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002319status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2320 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002321 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002322 uint32_t sw, uint32_t sh,
2323 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002324{
2325 // only one display supported for now
2326 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2327 return BAD_VALUE;
2328
2329 if (!GLExtensions::getInstance().haveFramebufferObject())
2330 return INVALID_OPERATION;
2331
2332 class MessageCaptureScreen : public MessageBase {
2333 SurfaceFlinger* flinger;
2334 DisplayID dpy;
2335 sp<IMemoryHeap>* heap;
2336 uint32_t* w;
2337 uint32_t* h;
2338 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002339 uint32_t sw;
2340 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002341 uint32_t minLayerZ;
2342 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002343 status_t result;
2344 public:
2345 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002346 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002347 uint32_t sw, uint32_t sh,
2348 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002349 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002350 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2351 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2352 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002353 {
2354 }
2355 status_t getResult() const {
2356 return result;
2357 }
2358 virtual bool handler() {
2359 Mutex::Autolock _l(flinger->mStateLock);
2360
2361 // if we have secure windows, never allow the screen capture
2362 if (flinger->mSecureFrameBuffer)
2363 return true;
2364
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002365 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002366 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002367
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002368 return true;
2369 }
2370 };
2371
2372 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002373 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002374 status_t res = postMessageSync(msg);
2375 if (res == NO_ERROR) {
2376 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2377 }
2378 return res;
2379}
2380
2381// ---------------------------------------------------------------------------
2382
Mathias Agopian7623da42010-06-01 15:12:58 -07002383sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2384{
2385 sp<Layer> result;
2386 Mutex::Autolock _l(mStateLock);
2387 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2388 return result;
2389}
2390
2391// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392
Mathias Agopian593c05c2010-06-02 23:28:45 -07002393Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002394 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396}
2397
Mathias Agopian593c05c2010-06-02 23:28:45 -07002398Client::~Client()
2399{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002400 const size_t count = mLayers.size();
2401 for (size_t i=0 ; i<count ; i++) {
2402 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2403 if (layer != 0) {
2404 mFlinger->removeLayer(layer);
2405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
2407}
2408
Mathias Agopian593c05c2010-06-02 23:28:45 -07002409status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002410 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411}
Mathias Agopian1473f462009-04-10 14:24:30 -07002412
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002413size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002414{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002415 Mutex::Autolock _l(mLock);
2416 size_t name = mNameGenerator++;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002417 mLayers.add(name, layer);
2418 return name;
2419}
2420
Mathias Agopian7623da42010-06-01 15:12:58 -07002421void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002422{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002423 Mutex::Autolock _l(mLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002424 // we do a linear search here, because this doesn't happen often
2425 const size_t count = mLayers.size();
2426 for (size_t i=0 ; i<count ; i++) {
2427 if (mLayers.valueAt(i) == layer) {
2428 mLayers.removeItemsAt(i, 1);
2429 break;
2430 }
2431 }
2432}
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002433sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2434{
2435 Mutex::Autolock _l(mLock);
Mathias Agopian1473f462009-04-10 14:24:30 -07002436 sp<LayerBaseClient> lbc;
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002437 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian593c05c2010-06-02 23:28:45 -07002438 if (layer != 0) {
2439 lbc = layer.promote();
2440 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002441 }
2442 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443}
2444
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002445
2446status_t Client::onTransact(
2447 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2448{
2449 // these must be checked
2450 IPCThreadState* ipc = IPCThreadState::self();
2451 const int pid = ipc->getCallingPid();
2452 const int uid = ipc->getCallingUid();
2453 const int self_pid = getpid();
2454 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2455 // we're called from a different process, do the real check
Mathias Agopian0dd593f2011-06-27 16:05:52 -07002456 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002457 {
2458 LOGE("Permission Denial: "
2459 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2460 return PERMISSION_DENIED;
2461 }
2462 }
2463 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopian7623da42010-06-01 15:12:58 -07002464}
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002465
2466
Mathias Agopian593c05c2010-06-02 23:28:45 -07002467sp<ISurface> Client::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002468 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002469 const String8& name,
2470 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 uint32_t flags)
2472{
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002473 /*
2474 * createSurface must be called from the GL thread so that it can
2475 * have access to the GL context.
2476 */
2477
2478 class MessageCreateSurface : public MessageBase {
2479 sp<ISurface> result;
2480 SurfaceFlinger* flinger;
2481 ISurfaceComposerClient::surface_data_t* params;
2482 Client* client;
2483 const String8& name;
2484 DisplayID display;
2485 uint32_t w, h;
2486 PixelFormat format;
2487 uint32_t flags;
2488 public:
2489 MessageCreateSurface(SurfaceFlinger* flinger,
2490 ISurfaceComposerClient::surface_data_t* params,
2491 const String8& name, Client* client,
2492 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2493 uint32_t flags)
2494 : flinger(flinger), params(params), client(client), name(name),
2495 display(display), w(w), h(h), format(format), flags(flags)
2496 {
2497 }
2498 sp<ISurface> getResult() const { return result; }
2499 virtual bool handler() {
2500 result = flinger->createSurface(params, name, client,
2501 display, w, h, format, flags);
2502 return true;
2503 }
2504 };
2505
2506 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2507 params, name, this, display, w, h, format, flags);
2508 mFlinger->postMessageSync(msg);
2509 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002511status_t Client::destroySurface(SurfaceID sid) {
2512 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514
2515// ---------------------------------------------------------------------------
2516
Jamie Gennisf7acf162011-01-12 18:30:40 -08002517GraphicBufferAlloc::GraphicBufferAlloc() {}
2518
2519GraphicBufferAlloc::~GraphicBufferAlloc() {}
2520
2521sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002522 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennisf7acf162011-01-12 18:30:40 -08002523 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2524 status_t err = graphicBuffer->initCheck();
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002525 *error = err;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002526 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002527 if (err == NO_MEMORY) {
2528 GraphicBuffer::dumpAllocationsToSystemLog();
2529 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002530 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2531 "failed (%s), handle=%p",
2532 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennisf7acf162011-01-12 18:30:40 -08002533 return 0;
2534 }
Jamie Gennisf7acf162011-01-12 18:30:40 -08002535 return graphicBuffer;
2536}
2537
Jamie Gennisf7acf162011-01-12 18:30:40 -08002538// ---------------------------------------------------------------------------
2539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540GraphicPlane::GraphicPlane()
2541 : mHw(0)
2542{
2543}
2544
2545GraphicPlane::~GraphicPlane() {
2546 delete mHw;
2547}
2548
2549bool GraphicPlane::initialized() const {
2550 return mHw ? true : false;
2551}
2552
Mathias Agopian66c77a52010-02-08 15:49:35 -08002553int GraphicPlane::getWidth() const {
2554 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555}
2556
Mathias Agopian66c77a52010-02-08 15:49:35 -08002557int GraphicPlane::getHeight() const {
2558 return mHeight;
2559}
2560
2561void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2562{
2563 mHw = hw;
2564
2565 // initialize the display orientation transform.
2566 // it's a constant that should come from the display driver.
2567 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2568 char property[PROPERTY_VALUE_MAX];
2569 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2570 //displayOrientation
2571 switch (atoi(property)) {
2572 case 90:
2573 displayOrientation = ISurfaceComposer::eOrientation90;
2574 break;
2575 case 270:
2576 displayOrientation = ISurfaceComposer::eOrientation270;
2577 break;
2578 }
2579 }
2580
2581 const float w = hw->getWidth();
2582 const float h = hw->getHeight();
2583 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2584 &mDisplayTransform);
2585 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2586 mDisplayWidth = h;
2587 mDisplayHeight = w;
2588 } else {
2589 mDisplayWidth = w;
2590 mDisplayHeight = h;
2591 }
2592
2593 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594}
2595
2596status_t GraphicPlane::orientationToTransfrom(
2597 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002598{
2599 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 switch (orientation) {
2601 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002602 flags = Transform::ROT_0;
2603 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002605 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 break;
2607 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002608 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 break;
2610 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002611 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 break;
2613 default:
2614 return BAD_VALUE;
2615 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002616 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 return NO_ERROR;
2618}
2619
2620status_t GraphicPlane::setOrientation(int orientation)
2621{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 // If the rotation can be handled in hardware, this is where
2623 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002624
2625 const DisplayHardware& hw(displayHardware());
2626 const float w = mDisplayWidth;
2627 const float h = mDisplayHeight;
2628 mWidth = int(w);
2629 mHeight = int(h);
2630
2631 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002632 GraphicPlane::orientationToTransfrom(orientation, w, h,
2633 &orientationTransform);
2634 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2635 mWidth = int(h);
2636 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002638
Mathias Agopian3552f532009-03-27 17:58:20 -07002639 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002640 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 return NO_ERROR;
2642}
2643
2644const DisplayHardware& GraphicPlane::displayHardware() const {
2645 return *mHw;
2646}
2647
Mathias Agopianaab758e2010-10-11 12:37:43 -07002648DisplayHardware& GraphicPlane::editDisplayHardware() {
2649 return *mHw;
2650}
2651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652const Transform& GraphicPlane::transform() const {
2653 return mGlobalTransform;
2654}
2655
Mathias Agopian1473f462009-04-10 14:24:30 -07002656EGLDisplay GraphicPlane::getEGLDisplay() const {
2657 return mHw->getEGLDisplay();
2658}
2659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660// ---------------------------------------------------------------------------
2661
2662}; // namespace android