blob: a7b58576924c233d3b4e7977723def55db30157e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070041#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070048#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070054#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopiana1ecca92009-05-21 19:21:59 -070056/* ideally AID_GRAPHICS would be in a semi-public header
57 * or there would be a way to map a user/group name to its id
58 */
59#ifndef AID_GRAPHICS
60#define AID_GRAPHICS 1003
61#endif
62
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#define DISPLAY_COUNT 1
64
65namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066// ---------------------------------------------------------------------------
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068SurfaceFlinger::SurfaceFlinger()
69 : BnSurfaceComposer(), Thread(false),
70 mTransactionFlags(0),
71 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070072 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -070075 mHardwareTest("android.permission.HARDWARE_TEST"),
76 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
Mathias Agopian1b0b30d2010-09-24 11:26:58 -070077 mReadFramebuffer("android.permission.READ_FRAME_BUFFER"),
Mathias Agopian375f5632009-06-15 18:24:59 -070078 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070080 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 mDeferReleaseConsole(false),
82 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070083 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070085 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mDebugBackground(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070088 mDebugDisableHWC(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070089 mDebugInSwapBuffers(0),
90 mLastSwapBufferTime(0),
91 mDebugInTransaction(0),
92 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070093 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 mConsoleSignals(0),
95 mSecureFrameBuffer(0)
96{
97 init();
98}
99
100void SurfaceFlinger::init()
101{
102 LOGI("SurfaceFlinger is starting");
103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 property_get("debug.sf.showbackground", value, "0");
109 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Mathias Agopian78fd5012010-04-20 14:51:04 -0700111 LOGI_IF(mDebugRegion, "showupdates enabled");
112 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
115SurfaceFlinger::~SurfaceFlinger()
116{
117 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118}
119
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700120sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700122 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123}
124
Mathias Agopian7e27f052010-05-28 14:22:23 -0700125sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126{
Mathias Agopian96f08192010-06-02 23:28:45 -0700127 sp<ISurfaceComposerClient> bclient;
128 sp<Client> client(new Client(this));
129 status_t err = client->initCheck();
130 if (err == NO_ERROR) {
131 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 return bclient;
134}
135
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700136sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
137{
138 sp<ISurfaceComposerClient> bclient;
139 sp<UserClient> client(new UserClient(this));
140 status_t err = client->initCheck();
141 if (err == NO_ERROR) {
142 bclient = client;
143 }
144 return bclient;
145}
146
Jamie Gennis9a78c902011-01-12 18:30:40 -0800147sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
148{
149 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
150 return gba;
151}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
154{
155 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
156 const GraphicPlane& plane(mGraphicPlanes[dpy]);
157 return plane;
158}
159
160GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
161{
162 return const_cast<GraphicPlane&>(
163 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
164}
165
166void SurfaceFlinger::bootFinished()
167{
168 const nsecs_t now = systemTime();
169 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700170 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700171 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700172 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173}
174
175void SurfaceFlinger::onFirstRef()
176{
177 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
178
179 // Wait for the main thread to be done with its initialization
180 mReadyToRunBarrier.wait();
181}
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183static inline uint16_t pack565(int r, int g, int b) {
184 return (r<<11)|(g<<5)|b;
185}
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187status_t SurfaceFlinger::readyToRun()
188{
189 LOGI( "SurfaceFlinger's main thread ready to run. "
190 "Initializing graphics H/W...");
191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 // we only support one display currently
193 int dpy = 0;
194
195 {
196 // initialize the main display
197 GraphicPlane& plane(graphicPlane(dpy));
198 DisplayHardware* const hw = new DisplayHardware(this, dpy);
199 plane.setDisplayHardware(hw);
200 }
201
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700202 // create the shared control-block
203 mServerHeap = new MemoryHeapBase(4096,
204 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
205 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700206
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700207 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
208 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700209
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700210 new(mServerCblk) surface_flinger_cblk_t;
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 // initialize primary screen
213 // (other display should be initialized in the same manner, but
214 // asynchronously, as they could come and go. None of this is supported
215 // yet).
216 const GraphicPlane& plane(graphicPlane(dpy));
217 const DisplayHardware& hw = plane.displayHardware();
218 const uint32_t w = hw.getWidth();
219 const uint32_t h = hw.getHeight();
220 const uint32_t f = hw.getFormat();
221 hw.makeCurrent();
222
223 // initialize the shared control block
224 mServerCblk->connected |= 1<<dpy;
225 display_cblk_t* dcblk = mServerCblk->displays + dpy;
226 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800227 dcblk->w = plane.getWidth();
228 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 dcblk->format = f;
230 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
231 dcblk->xdpi = hw.getDpiX();
232 dcblk->ydpi = hw.getDpiY();
233 dcblk->fps = hw.getRefreshRate();
234 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700238 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 glEnableClientState(GL_VERTEX_ARRAY);
240 glEnable(GL_SCISSOR_TEST);
241 glShadeModel(GL_FLAT);
242 glDisable(GL_DITHER);
243 glDisable(GL_CULL_FACE);
244
245 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
246 const uint16_t g1 = pack565(0x17,0x2f,0x17);
247 const uint16_t textureData[4] = { g0, g1, g1, g0 };
248 glGenTextures(1, &mWormholeTexName);
249 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
250 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
251 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
252 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
253 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
255 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
256
257 glViewport(0, 0, w, h);
258 glMatrixMode(GL_PROJECTION);
259 glLoadIdentity();
260 glOrthof(0, w, h, 0, 0, 1);
261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 mReadyToRunBarrier.open();
263
264 /*
265 * We're now ready to accept clients...
266 */
267
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700268 // start boot animation
269 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 return NO_ERROR;
272}
273
274// ----------------------------------------------------------------------------
275#if 0
276#pragma mark -
277#pragma mark Events Handler
278#endif
279
280void SurfaceFlinger::waitForEvent()
281{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700282 while (true) {
283 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800284 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700285 if (UNLIKELY(isFrozen())) {
286 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700287 const nsecs_t now = systemTime();
288 if (mFreezeDisplayTime == 0) {
289 mFreezeDisplayTime = now;
290 }
291 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
292 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700293 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700294
Mathias Agopianbb641242010-05-18 17:06:55 -0700295 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800296
297 // see if we timed out
298 if (isFrozen()) {
299 const nsecs_t now = systemTime();
300 nsecs_t frozenTime = (now - mFreezeDisplayTime);
301 if (frozenTime >= freezeDisplayTimeout) {
302 // we timed out and are still frozen
303 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
304 mFreezeDisplay, mFreezeCount);
305 mFreezeDisplayTime = 0;
306 mFreezeCount = 0;
307 mFreezeDisplay = false;
308 }
309 }
310
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700311 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700312 switch (msg->what) {
313 case MessageQueue::INVALIDATE:
314 // invalidate message, just return to the main loop
315 return;
316 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
319}
320
321void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700322 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323}
324
325void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700326 // this is the IPC call
327 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
Jamie Gennis134f0422011-03-08 12:18:54 -0800330bool SurfaceFlinger::authenticateSurface(const sp<ISurface>& surface) const {
331 Mutex::Autolock _l(mStateLock);
332 sp<IBinder> surfBinder(surface->asBinder());
333
334 // Check the visible layer list for the ISurface
335 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
336 size_t count = currentLayers.size();
337 for (size_t i=0 ; i<count ; i++) {
338 const sp<LayerBase>& layer(currentLayers[i]);
339 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
340 if (lbc != NULL && lbc->getSurfaceBinder() == surfBinder) {
341 return true;
342 }
343 }
344
345 // Check the layers in the purgatory. This check is here so that if a
346 // Surface gets destroyed before all the clients are done using it, the
347 // error will not be reported as "surface XYZ is not authenticated", but
348 // will instead fail later on when the client tries to use the surface,
349 // which should be reported as "surface XYZ returned an -ENODEV". The
350 // purgatorized layers are no less authentic than the visible ones, so this
351 // should not cause any harm.
352 size_t purgatorySize = mLayerPurgatory.size();
353 for (size_t i=0 ; i<purgatorySize ; i++) {
354 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
355 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
356 if (lbc != NULL && lbc->getSurfaceBinder() == surfBinder) {
357 return true;
358 }
359 }
360
361 return false;
362}
363
Mathias Agopianbb641242010-05-18 17:06:55 -0700364status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
365 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366{
Mathias Agopianbb641242010-05-18 17:06:55 -0700367 return mEventQueue.postMessage(msg, reltime, flags);
368}
369
370status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
371 nsecs_t reltime, uint32_t flags)
372{
373 status_t res = mEventQueue.postMessage(msg, reltime, flags);
374 if (res == NO_ERROR) {
375 msg->wait();
376 }
377 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378}
379
380// ----------------------------------------------------------------------------
381#if 0
382#pragma mark -
383#pragma mark Main loop
384#endif
385
386bool SurfaceFlinger::threadLoop()
387{
388 waitForEvent();
389
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700390 // call Layer's destructor
391 handleDestroyLayers();
392
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 // check for transactions
394 if (UNLIKELY(mConsoleSignals)) {
395 handleConsoleEvents();
396 }
397
398 if (LIKELY(mTransactionCount == 0)) {
399 // if we're in a global transaction, don't do anything.
400 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700401 uint32_t transactionFlags = peekTransactionFlags(mask);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 if (LIKELY(transactionFlags)) {
403 handleTransaction(transactionFlags);
404 }
405 }
406
407 // post surfaces (if needed)
408 handlePageFlip();
409
Mathias Agopiana350ff92010-08-10 17:14:02 -0700410 if (UNLIKELY(mHwWorkListDirty)) {
411 // build the h/w work list
412 handleWorkList();
413 }
414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700416 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700418
419 const int index = hw.getCurrentBufferIndex();
420 GraphicLog& logger(GraphicLog::getInstance());
421
422 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 handleRepaint();
424
Mathias Agopian74faca22009-09-17 16:18:16 -0700425 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700426 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700427 hw.compositionComplete();
428
Mathias Agopian35b48d12010-09-13 22:57:58 -0700429 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700430 postFramebuffer();
431
Mathias Agopian35b48d12010-09-13 22:57:58 -0700432 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 } else {
434 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800435 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 usleep(16667); // 60 fps period
437 }
438 return true;
439}
440
441void SurfaceFlinger::postFramebuffer()
442{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 if (!mInvalidRegion.isEmpty()) {
444 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700445 const nsecs_t now = systemTime();
446 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700448 mLastSwapBufferTime = systemTime() - now;
449 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 }
452}
453
454void SurfaceFlinger::handleConsoleEvents()
455{
456 // something to do with the console
457 const DisplayHardware& hw = graphicPlane(0).displayHardware();
458
459 int what = android_atomic_and(0, &mConsoleSignals);
460 if (what & eConsoleAcquired) {
461 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700462 // this is a temporary work-around, eventually this should be called
463 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700464 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465 }
466
Mathias Agopian59119e62010-10-11 12:37:43 -0700467 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700468 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470 hw.releaseScreen();
471 }
472
473 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700474 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 hw.releaseScreen();
476 } else {
477 mDeferReleaseConsole = true;
478 }
479 }
480
481 mDirtyRegion.set(hw.bounds());
482}
483
484void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
485{
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700486 Mutex::Autolock _l(mStateLock);
487 const nsecs_t now = systemTime();
488 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700490 // Here we're guaranteed that some transaction flags are set
491 // so we can call handleTransactionLocked() unconditionally.
492 // We call getTransactionFlags(), which will also clear the flags,
493 // with mStateLock held to guarantee that mCurrentState won't change
494 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700495
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700496 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
497 transactionFlags = getTransactionFlags(mask);
498 handleTransactionLocked(transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700499
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700500 mLastTransactionTime = systemTime() - now;
501 mDebugInTransaction = 0;
502 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700503}
504
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700505void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700506{
507 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 const size_t count = currentLayers.size();
509
510 /*
511 * Traversal of the children
512 * (perform the transaction for each of them if needed)
513 */
514
515 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
516 if (layersNeedTransaction) {
517 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700518 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
520 if (!trFlags) continue;
521
522 const uint32_t flags = layer->doTransaction(0);
523 if (flags & Layer::eVisibleRegion)
524 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525 }
526 }
527
528 /*
529 * Perform our own transaction if needed
530 */
531
532 if (transactionFlags & eTransactionNeeded) {
533 if (mCurrentState.orientation != mDrawingState.orientation) {
534 // the orientation has changed, recompute all visible regions
535 // and invalidate everything.
536
537 const int dpy = 0;
538 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700539 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 GraphicPlane& plane(graphicPlane(dpy));
541 plane.setOrientation(orientation);
542
543 // update the shared control block
544 const DisplayHardware& hw(plane.displayHardware());
545 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
546 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800547 dcblk->w = plane.getWidth();
548 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549
550 mVisibleRegionsDirty = true;
551 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 }
553
554 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
555 // freezing or unfreezing the display -> trigger animation if needed
556 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800557 if (mFreezeDisplay)
558 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 }
560
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700561 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
562 // layers have been added
563 mVisibleRegionsDirty = true;
564 }
565
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 // some layers might have been removed, so
567 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700568 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700569 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700571 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700572 const size_t count = previousLayers.size();
573 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700574 const sp<LayerBase>& layer(previousLayers[i]);
575 if (currentLayers.indexOf( layer ) < 0) {
576 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700577 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700578 }
579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 }
582
583 commitTransaction();
584}
585
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700586void SurfaceFlinger::destroyLayer(LayerBase const* layer)
587{
588 Mutex::Autolock _l(mDestroyedLayerLock);
589 mDestroyedLayers.add(layer);
590 signalEvent();
591}
592
593void SurfaceFlinger::handleDestroyLayers()
594{
595 Vector<LayerBase const *> destroyedLayers;
596
597 { // scope for the lock
598 Mutex::Autolock _l(mDestroyedLayerLock);
599 destroyedLayers = mDestroyedLayers;
600 mDestroyedLayers.clear();
601 }
602
603 // call destructors without a lock held
604 const size_t count = destroyedLayers.size();
605 for (size_t i=0 ; i<count ; i++) {
606 //LOGD("destroying %s", destroyedLayers[i]->getName().string());
607 delete destroyedLayers[i];
608 }
609}
610
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
612{
613 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
614}
615
616void SurfaceFlinger::computeVisibleRegions(
617 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
618{
619 const GraphicPlane& plane(graphicPlane(0));
620 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700621 const DisplayHardware& hw(plane.displayHardware());
622 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623
624 Region aboveOpaqueLayers;
625 Region aboveCoveredLayers;
626 Region dirty;
627
628 bool secureFrameBuffer = false;
629
630 size_t i = currentLayers.size();
631 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700632 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 layer->validateVisibility(planeTransform);
634
635 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700636 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637
Mathias Agopianab028732010-03-16 16:41:46 -0700638 /*
639 * opaqueRegion: area of a surface that is fully opaque.
640 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700642
643 /*
644 * visibleRegion: area of a surface that is visible on screen
645 * and not fully transparent. This is essentially the layer's
646 * footprint minus the opaque regions above it.
647 * Areas covered by a translucent surface are considered visible.
648 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700650
651 /*
652 * coveredRegion: area of a surface that is covered by all
653 * visible regions above it (which includes the translucent areas).
654 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700656
657
658 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700659 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700661 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700663 visibleRegion.andSelf(screenRegion);
664 if (!visibleRegion.isEmpty()) {
665 // Remove the transparent area from the visible region
666 if (translucent) {
667 visibleRegion.subtractSelf(layer->transparentRegionScreen);
668 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669
Mathias Agopianab028732010-03-16 16:41:46 -0700670 // compute the opaque region
671 const int32_t layerOrientation = layer->getOrientation();
672 if (s.alpha==255 && !translucent &&
673 ((layerOrientation & Transform::ROT_INVALID) == false)) {
674 // the opaque region is the layer's footprint
675 opaqueRegion = visibleRegion;
676 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 }
678 }
679
Mathias Agopianab028732010-03-16 16:41:46 -0700680 // Clip the covered region to the visible region
681 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
682
683 // Update aboveCoveredLayers for next (lower) layer
684 aboveCoveredLayers.orSelf(visibleRegion);
685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 // subtract the opaque region covered by the layers above us
687 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688
689 // compute this layer's dirty region
690 if (layer->contentDirty) {
691 // we need to invalidate the whole region
692 dirty = visibleRegion;
693 // as well, as the old visible region
694 dirty.orSelf(layer->visibleRegionScreen);
695 layer->contentDirty = false;
696 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700697 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700698 * the exposed region consists of two components:
699 * 1) what's VISIBLE now and was COVERED before
700 * 2) what's EXPOSED now less what was EXPOSED before
701 *
702 * note that (1) is conservative, we start with the whole
703 * visible region but only keep what used to be covered by
704 * something -- which mean it may have been exposed.
705 *
706 * (2) handles areas that were not covered by anything but got
707 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700708 */
Mathias Agopianab028732010-03-16 16:41:46 -0700709 const Region newExposed = visibleRegion - coveredRegion;
710 const Region oldVisibleRegion = layer->visibleRegionScreen;
711 const Region oldCoveredRegion = layer->coveredRegionScreen;
712 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
713 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 }
715 dirty.subtractSelf(aboveOpaqueLayers);
716
717 // accumulate to the screen dirty region
718 dirtyRegion.orSelf(dirty);
719
Mathias Agopianab028732010-03-16 16:41:46 -0700720 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700722
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 // Store the visible region is screen space
724 layer->setVisibleRegion(visibleRegion);
725 layer->setCoveredRegion(coveredRegion);
726
Mathias Agopian97011222009-07-28 10:57:27 -0700727 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 if (layer->isSecure() && !visibleRegion.isEmpty()) {
729 secureFrameBuffer = true;
730 }
731 }
732
Mathias Agopian97011222009-07-28 10:57:27 -0700733 // invalidate the areas where a layer was removed
734 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
735 mDirtyRegionRemovedLayer.clear();
736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 mSecureFrameBuffer = secureFrameBuffer;
738 opaqueRegion = aboveOpaqueLayers;
739}
740
741
742void SurfaceFlinger::commitTransaction()
743{
744 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700745 mResizeTransationPending = false;
746 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
749void SurfaceFlinger::handlePageFlip()
750{
751 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700752 LayerVector& currentLayers(
753 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 visibleRegions |= lockPageFlip(currentLayers);
755
756 const DisplayHardware& hw = graphicPlane(0).displayHardware();
757 const Region screenRegion(hw.bounds());
758 if (visibleRegions) {
759 Region opaqueRegion;
760 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700761
762 /*
763 * rebuild the visible layer list
764 */
765 mVisibleLayersSortedByZ.clear();
766 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
767 size_t count = currentLayers.size();
768 mVisibleLayersSortedByZ.setCapacity(count);
769 for (size_t i=0 ; i<count ; i++) {
770 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
771 mVisibleLayersSortedByZ.add(currentLayers[i]);
772 }
773
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 mWormholeRegion = screenRegion.subtract(opaqueRegion);
775 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800776 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 }
778
779 unlockPageFlip(currentLayers);
780 mDirtyRegion.andSelf(screenRegion);
781}
782
Mathias Agopianad456f92011-01-13 17:53:01 -0800783void SurfaceFlinger::invalidateHwcGeometry()
784{
785 mHwWorkListDirty = true;
786}
787
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
789{
790 bool recomputeVisibleRegions = false;
791 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700792 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700794 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795 layer->lockPageFlip(recomputeVisibleRegions);
796 }
797 return recomputeVisibleRegions;
798}
799
800void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
801{
802 const GraphicPlane& plane(graphicPlane(0));
803 const Transform& planeTransform(plane.transform());
804 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700805 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700807 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 layer->unlockPageFlip(planeTransform, mDirtyRegion);
809 }
810}
811
Mathias Agopiana350ff92010-08-10 17:14:02 -0700812void SurfaceFlinger::handleWorkList()
813{
814 mHwWorkListDirty = false;
815 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
816 if (hwc.initCheck() == NO_ERROR) {
817 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
818 const size_t count = currentLayers.size();
819 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700820 hwc_layer_t* const cur(hwc.getLayers());
821 for (size_t i=0 ; cur && i<count ; i++) {
822 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700823 if (mDebugDisableHWC) {
824 cur[i].compositionType = HWC_FRAMEBUFFER;
825 cur[i].flags |= HWC_SKIP_LAYER;
826 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700827 }
828 }
829}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831void SurfaceFlinger::handleRepaint()
832{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700833 // compute the invalid region
834 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835
836 if (UNLIKELY(mDebugRegion)) {
837 debugFlashRegions();
838 }
839
Mathias Agopianb8a55602009-06-26 19:06:36 -0700840 // set the frame buffer
841 const DisplayHardware& hw(graphicPlane(0).displayHardware());
842 glMatrixMode(GL_MODELVIEW);
843 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844
845 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700846 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
847 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700848 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700849 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
850 // takes a rectangle, we must make sure to update that whole
851 // rectangle in that case
852 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700853 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700854 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 mDirtyRegion.set(mInvalidRegion.bounds());
856 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700857 // in the BUFFER_PRESERVED case, obviously, we can update only
858 // what's needed and nothing more.
859 // NOTE: this is NOT a common case, as preserving the backbuffer
860 // is costly and usually involves copying the whole update back.
861 }
862 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700863 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700864 // We need to redraw the rectangle that will be updated
865 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700866 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700867 // rectangle instead of a region (see DisplayHardware::flip())
868 mDirtyRegion.set(mInvalidRegion.bounds());
869 } else {
870 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 mDirtyRegion.set(hw.bounds());
872 mInvalidRegion = mDirtyRegion;
873 }
874 }
875
876 // compose all surfaces
877 composeSurfaces(mDirtyRegion);
878
879 // clear the dirty regions
880 mDirtyRegion.clear();
881}
882
883void SurfaceFlinger::composeSurfaces(const Region& dirty)
884{
885 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
886 // should never happen unless the window manager has a bug
887 // draw something...
888 drawWormhole();
889 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700890
891 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700892 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700893 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700894
895 const DisplayHardware& hw(graphicPlane(0).displayHardware());
896 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700897 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700898
Mathias Agopian45721772010-08-12 15:03:26 -0700899 LOGE_IF(cur && hwc.getNumLayers() != count,
900 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
901 hwc.getNumLayers(), count);
902
903 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700904 if (hwc.initCheck() == NO_ERROR) {
905 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
906 }
Mathias Agopian45721772010-08-12 15:03:26 -0700907
908 /*
909 * update the per-frame h/w composer data for each layer
910 * and build the transparent region of the FB
911 */
912 Region transparent;
913 if (cur) {
914 for (size_t i=0 ; i<count ; i++) {
915 const sp<LayerBase>& layer(layers[i]);
916 layer->setPerFrameData(&cur[i]);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700917 }
918 err = hwc.prepare();
919 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700920
Mathias Agopianf20a3242011-01-19 15:24:23 -0800921 if (err == NO_ERROR) {
922 for (size_t i=0 ; i<count ; i++) {
923 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
924 const sp<LayerBase>& layer(layers[i]);
925 if (!(layer->needsBlending())) {
926 transparent.orSelf(layer->visibleRegionScreen);
927 }
928 }
929 }
930
931 /*
932 * clear the area of the FB that need to be transparent
933 */
934 transparent.andSelf(dirty);
935 if (!transparent.isEmpty()) {
936 glClearColor(0,0,0,0);
937 Region::const_iterator it = transparent.begin();
938 Region::const_iterator const end = transparent.end();
939 const int32_t height = hw.getHeight();
940 while (it != end) {
941 const Rect& r(*it++);
942 const GLint sy = height - (r.top + r.height());
943 glScissor(r.left, sy, r.width(), r.height());
944 glClear(GL_COLOR_BUFFER_BIT);
945 }
946 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700947 }
948 }
Mathias Agopian45721772010-08-12 15:03:26 -0700949
950
951 /*
952 * and then, render the layers targeted at the framebuffer
953 */
954 for (size_t i=0 ; i<count ; i++) {
955 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700956 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
957 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700958 // skip layers handled by the HAL
959 continue;
960 }
961 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700962
Mathias Agopian45721772010-08-12 15:03:26 -0700963 const sp<LayerBase>& layer(layers[i]);
964 const Region clip(dirty.intersect(layer->visibleRegionScreen));
965 if (!clip.isEmpty()) {
966 layer->draw(clip);
967 }
968 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969}
970
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971void SurfaceFlinger::debugFlashRegions()
972{
Mathias Agopian0a917752010-06-14 21:20:00 -0700973 const DisplayHardware& hw(graphicPlane(0).displayHardware());
974 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700975
Mathias Agopian0a917752010-06-14 21:20:00 -0700976 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
977 (flags & DisplayHardware::BUFFER_PRESERVED))) {
978 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
979 mDirtyRegion.bounds() : hw.bounds());
980 composeSurfaces(repaint);
981 }
982
983 TextureManager::deactivateTextures();
984
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 glDisable(GL_BLEND);
986 glDisable(GL_DITHER);
987 glDisable(GL_SCISSOR_TEST);
988
Mathias Agopian0926f502009-05-04 14:17:04 -0700989 static int toggle = 0;
990 toggle = 1 - toggle;
991 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700992 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700993 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700994 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700995 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996
Mathias Agopian20f68782009-05-11 00:03:41 -0700997 Region::const_iterator it = mDirtyRegion.begin();
998 Region::const_iterator const end = mDirtyRegion.end();
999 while (it != end) {
1000 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 GLfloat vertices[][2] = {
1002 { r.left, r.top },
1003 { r.left, r.bottom },
1004 { r.right, r.bottom },
1005 { r.right, r.top }
1006 };
1007 glVertexPointer(2, GL_FLOAT, 0, vertices);
1008 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1009 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001010
Mathias Agopianb8a55602009-06-26 19:06:36 -07001011 if (mInvalidRegion.isEmpty()) {
1012 mDirtyRegion.dump("mDirtyRegion");
1013 mInvalidRegion.dump("mInvalidRegion");
1014 }
1015 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016
1017 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001018 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019
1020 glEnable(GL_SCISSOR_TEST);
1021 //mDirtyRegion.dump("mDirtyRegion");
1022}
1023
1024void SurfaceFlinger::drawWormhole() const
1025{
1026 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1027 if (region.isEmpty())
1028 return;
1029
1030 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1031 const int32_t width = hw.getWidth();
1032 const int32_t height = hw.getHeight();
1033
1034 glDisable(GL_BLEND);
1035 glDisable(GL_DITHER);
1036
1037 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001038 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001039 Region::const_iterator it = region.begin();
1040 Region::const_iterator const end = region.end();
1041 while (it != end) {
1042 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 const GLint sy = height - (r.top + r.height());
1044 glScissor(r.left, sy, r.width(), r.height());
1045 glClear(GL_COLOR_BUFFER_BIT);
1046 }
1047 } else {
1048 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1049 { width, height }, { 0, height } };
1050 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1051 glVertexPointer(2, GL_SHORT, 0, vertices);
1052 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1053 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001054#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001055 if (GLExtensions::getInstance().haveTextureExternal()) {
1056 glDisable(GL_TEXTURE_EXTERNAL_OES);
1057 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001058#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 glEnable(GL_TEXTURE_2D);
1060 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1061 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1062 glMatrixMode(GL_TEXTURE);
1063 glLoadIdentity();
1064 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001065 Region::const_iterator it = region.begin();
1066 Region::const_iterator const end = region.end();
1067 while (it != end) {
1068 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 const GLint sy = height - (r.top + r.height());
1070 glScissor(r.left, sy, r.width(), r.height());
1071 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1072 }
1073 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001074 glLoadIdentity();
1075 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 }
1077}
1078
1079void SurfaceFlinger::debugShowFPS() const
1080{
1081 static int mFrameCount;
1082 static int mLastFrameCount = 0;
1083 static nsecs_t mLastFpsTime = 0;
1084 static float mFps = 0;
1085 mFrameCount++;
1086 nsecs_t now = systemTime();
1087 nsecs_t diff = now - mLastFpsTime;
1088 if (diff > ms2ns(250)) {
1089 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1090 mLastFpsTime = now;
1091 mLastFrameCount = mFrameCount;
1092 }
1093 // XXX: mFPS has the value we want
1094 }
1095
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001096status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097{
1098 Mutex::Autolock _l(mStateLock);
1099 addLayer_l(layer);
1100 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1101 return NO_ERROR;
1102}
1103
Mathias Agopian96f08192010-06-02 23:28:45 -07001104status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1105{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001106 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001107 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1108}
1109
1110ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1111 const sp<LayerBaseClient>& lbc)
1112{
Mathias Agopian96f08192010-06-02 23:28:45 -07001113 // attach this layer to the client
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001114 size_t name = client->attachLayer(lbc);
1115
1116 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001117
1118 // add this layer to the current state list
1119 addLayer_l(lbc);
1120
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001121 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001122}
1123
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001124status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125{
1126 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001127 status_t err = purgatorizeLayer_l(layer);
1128 if (err == NO_ERROR)
1129 setTransactionFlags(eTransactionNeeded);
1130 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131}
1132
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001133status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001135 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1136 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001137 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1140 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001141 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 return NO_ERROR;
1143 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001144 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145}
1146
Mathias Agopian9a112062009-04-17 19:36:26 -07001147status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1148{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001149 // First add the layer to the purgatory list, which makes sure it won't
1150 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001151 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001152 if (err >= 0) {
1153 mLayerPurgatory.add(layerBase);
1154 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001155
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001156 layerBase->onRemoved();
1157
Mathias Agopian3d579642009-06-04 18:46:21 -07001158 // it's possible that we don't find a layer, because it might
1159 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001160 // from the user because there is a race between Client::destroySurface(),
1161 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001162 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1163}
1164
Mathias Agopian96f08192010-06-02 23:28:45 -07001165status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166{
Mathias Agopian96f08192010-06-02 23:28:45 -07001167 layer->forceVisibilityTransaction();
1168 setTransactionFlags(eTraversalNeeded);
1169 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170}
1171
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001172uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1173{
1174 return android_atomic_release_load(&mTransactionFlags);
1175}
1176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1178{
1179 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1180}
1181
Mathias Agopianbb641242010-05-18 17:06:55 -07001182uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183{
1184 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1185 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001186 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 }
1188 return old;
1189}
1190
1191void SurfaceFlinger::openGlobalTransaction()
1192{
1193 android_atomic_inc(&mTransactionCount);
1194}
1195
1196void SurfaceFlinger::closeGlobalTransaction()
1197{
1198 if (android_atomic_dec(&mTransactionCount) == 1) {
1199 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001200
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001201 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001202 // take effect before returning.
1203 Mutex::Autolock _l(mStateLock);
1204 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001205 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1206 if (CC_UNLIKELY(err != NO_ERROR)) {
1207 // just in case something goes wrong in SF, return to the
1208 // called after a few seconds.
1209 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1210 mResizeTransationPending = false;
1211 break;
1212 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 }
1215}
1216
1217status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1218{
1219 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1220 return BAD_VALUE;
1221
1222 Mutex::Autolock _l(mStateLock);
1223 mCurrentState.freezeDisplay = 1;
1224 setTransactionFlags(eTransactionNeeded);
1225
1226 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001227 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 return NO_ERROR;
1229}
1230
1231status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1232{
1233 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1234 return BAD_VALUE;
1235
1236 Mutex::Autolock _l(mStateLock);
1237 mCurrentState.freezeDisplay = 0;
1238 setTransactionFlags(eTransactionNeeded);
1239
1240 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001241 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 return NO_ERROR;
1243}
1244
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001245int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001246 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247{
1248 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1249 return BAD_VALUE;
1250
1251 Mutex::Autolock _l(mStateLock);
1252 if (mCurrentState.orientation != orientation) {
1253 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001254 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 mCurrentState.orientation = orientation;
1256 setTransactionFlags(eTransactionNeeded);
1257 mTransactionCV.wait(mStateLock);
1258 } else {
1259 orientation = BAD_VALUE;
1260 }
1261 }
1262 return orientation;
1263}
1264
Mathias Agopian96f08192010-06-02 23:28:45 -07001265sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001266 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1268 uint32_t flags)
1269{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001270 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001272
1273 if (int32_t(w|h) < 0) {
1274 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1275 int(w), int(h));
1276 return surfaceHandle;
1277 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001280 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 switch (flags & eFXSurfaceMask) {
1282 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001283 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1284 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 break;
1286 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001287 // for now we treat Blur as Dim, until we can implement it
1288 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001290 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 break;
1292 }
1293
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001294 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001295 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001296 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001297 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001300 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001301 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001302 params->identity = surfaceHandle->getIdentity();
1303 params->width = w;
1304 params->height = h;
1305 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001306 if (normalLayer != 0) {
1307 Mutex::Autolock _l(mStateLock);
1308 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1309 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001310 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001311
Mathias Agopian96f08192010-06-02 23:28:45 -07001312 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 }
1314
1315 return surfaceHandle;
1316}
1317
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001318sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001319 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001321 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322{
1323 // initialize the surfaces
1324 switch (format) { // TODO: take h/w into account
1325 case PIXEL_FORMAT_TRANSPARENT:
1326 case PIXEL_FORMAT_TRANSLUCENT:
1327 format = PIXEL_FORMAT_RGBA_8888;
1328 break;
1329 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001330#ifdef NO_RGBX_8888
1331 format = PIXEL_FORMAT_RGB_565;
1332#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001333 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001334#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 break;
1336 }
1337
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001338#ifdef NO_RGBX_8888
1339 if (format == PIXEL_FORMAT_RGBX_8888)
1340 format = PIXEL_FORMAT_RGBA_8888;
1341#endif
1342
Mathias Agopian96f08192010-06-02 23:28:45 -07001343 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001344 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001345 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001347 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 }
1349 return layer;
1350}
1351
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001352sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001353 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001354 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355{
Mathias Agopian96f08192010-06-02 23:28:45 -07001356 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 return layer;
1359}
1360
Mathias Agopian96f08192010-06-02 23:28:45 -07001361status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362{
Mathias Agopian9a112062009-04-17 19:36:26 -07001363 /*
1364 * called by the window manager, when a surface should be marked for
1365 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001366 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001367 * The surface is removed from the current and drawing lists, but placed
1368 * in the purgatory queue, so it's not destroyed right-away (we need
1369 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001370 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001371
Mathias Agopian48d819a2009-09-10 19:41:18 -07001372 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001373 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001374 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001375 if (layer != 0) {
1376 err = purgatorizeLayer_l(layer);
1377 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001378 setTransactionFlags(eTransactionNeeded);
1379 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001380 }
1381 return err;
1382}
1383
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001384status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001385{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001386 // called by ~ISurface() when all references are gone
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001387 status_t err = NO_ERROR;
1388 sp<LayerBaseClient> l(layer.promote());
1389 if (l != NULL) {
1390 Mutex::Autolock _l(mStateLock);
1391 err = removeLayer_l(l);
Mathias Agopian36ca5e72011-06-07 14:52:22 -07001392 if (err == NAME_NOT_FOUND) {
1393 // The surface wasn't in the current list, which means it was
1394 // removed already, which means it is in the purgatory,
1395 // and need to be removed from there.
1396 ssize_t idx = mLayerPurgatory.remove(l);
1397 LOGE_IF(idx < 0,
1398 "layer=%p is not in the purgatory list", l.get());
1399 }
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001400 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1401 "error removing layer=%p (%s)", l.get(), strerror(-err));
1402 }
1403 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404}
1405
1406status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001407 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 int32_t count,
1409 const layer_state_t* states)
1410{
1411 Mutex::Autolock _l(mStateLock);
1412 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001414 const layer_state_t& s(states[i]);
1415 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001416 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 if (what & ePositionChanged) {
1419 if (layer->setPosition(s.x, s.y))
1420 flags |= eTraversalNeeded;
1421 }
1422 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001423 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001425 mCurrentState.layersSortedByZ.removeAt(idx);
1426 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 // we need traversal (state changed)
1428 // AND transaction (list changed)
1429 flags |= eTransactionNeeded|eTraversalNeeded;
1430 }
1431 }
1432 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001433 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001435 mResizeTransationPending = true;
1436 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 }
1438 if (what & eAlphaChanged) {
1439 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1440 flags |= eTraversalNeeded;
1441 }
1442 if (what & eMatrixChanged) {
1443 if (layer->setMatrix(s.matrix))
1444 flags |= eTraversalNeeded;
1445 }
1446 if (what & eTransparentRegionChanged) {
1447 if (layer->setTransparentRegionHint(s.transparentRegion))
1448 flags |= eTraversalNeeded;
1449 }
1450 if (what & eVisibilityChanged) {
1451 if (layer->setFlags(s.flags, s.mask))
1452 flags |= eTraversalNeeded;
1453 }
1454 }
1455 }
1456 if (flags) {
1457 setTransactionFlags(flags);
1458 }
1459 return NO_ERROR;
1460}
1461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462void SurfaceFlinger::screenReleased(int dpy)
1463{
1464 // this may be called by a signal handler, we can't do too much in here
1465 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1466 signalEvent();
1467}
1468
1469void SurfaceFlinger::screenAcquired(int dpy)
1470{
1471 // this may be called by a signal handler, we can't do too much in here
1472 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1473 signalEvent();
1474}
1475
1476status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1477{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001478 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 char buffer[SIZE];
1480 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001481 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 snprintf(buffer, SIZE, "Permission Denial: "
1483 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1484 IPCThreadState::self()->getCallingPid(),
1485 IPCThreadState::self()->getCallingUid());
1486 result.append(buffer);
1487 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001488
1489 // figure out if we're stuck somewhere
1490 const nsecs_t now = systemTime();
1491 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1492 const nsecs_t inTransaction(mDebugInTransaction);
1493 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1494 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1495
1496 // Try to get the main lock, but don't insist if we can't
1497 // (this would indicate SF is stuck, but we want to be able to
1498 // print something in dumpsys).
1499 int retry = 3;
1500 while (mStateLock.tryLock()<0 && --retry>=0) {
1501 usleep(1000000);
1502 }
1503 const bool locked(retry >= 0);
1504 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001505 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001506 "SurfaceFlinger appears to be unresponsive, "
1507 "dumping anyways (no locks held)\n");
1508 result.append(buffer);
1509 }
1510
Mathias Agopian48b888a2011-01-19 16:15:53 -08001511 /*
1512 * Dump the visible layer list
1513 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1515 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001516 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1517 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001519 const sp<LayerBase>& layer(currentLayers[i]);
1520 layer->dump(result, buffer, SIZE);
1521 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 s.transparentRegion.dump(result, "transparentRegion");
1523 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1524 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1525 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001526
Mathias Agopian48b888a2011-01-19 16:15:53 -08001527 /*
1528 * Dump the layers in the purgatory
1529 */
1530
1531 const size_t purgatorySize = mLayerPurgatory.size();
1532 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1533 result.append(buffer);
1534 for (size_t i=0 ; i<purgatorySize ; i++) {
1535 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1536 layer->shortDump(result, buffer, SIZE);
1537 }
1538
1539 /*
1540 * Dump SurfaceFlinger global state
1541 */
1542
1543 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1544 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 mWormholeRegion.dump(result, "WormholeRegion");
1546 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1547 snprintf(buffer, SIZE,
1548 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1549 mFreezeDisplay?"yes":"no", mFreezeCount,
1550 mCurrentState.orientation, hw.canDraw());
1551 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001552 snprintf(buffer, SIZE,
1553 " last eglSwapBuffers() time: %f us\n"
1554 " last transaction time : %f us\n",
1555 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1556 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001557
Mathias Agopian9795c422009-08-26 16:36:26 -07001558 if (inSwapBuffersDuration || !locked) {
1559 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1560 inSwapBuffersDuration/1000.0);
1561 result.append(buffer);
1562 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001563
Mathias Agopian9795c422009-08-26 16:36:26 -07001564 if (inTransactionDuration || !locked) {
1565 snprintf(buffer, SIZE, " transaction time: %f us\n",
1566 inTransactionDuration/1000.0);
1567 result.append(buffer);
1568 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001569
Mathias Agopian48b888a2011-01-19 16:15:53 -08001570 /*
1571 * Dump HWComposer state
1572 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001573 HWComposer& hwc(hw.getHwComposer());
1574 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1575 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1576 mDebugDisableHWC ? "disabled" : "enabled");
1577 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001578 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001579
Mathias Agopian48b888a2011-01-19 16:15:53 -08001580 /*
1581 * Dump gralloc state
1582 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001583 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001584 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001585 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001586
1587 if (locked) {
1588 mStateLock.unlock();
1589 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 }
1591 write(fd, result.string(), result.size());
1592 return NO_ERROR;
1593}
1594
1595status_t SurfaceFlinger::onTransact(
1596 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1597{
1598 switch (code) {
1599 case CREATE_CONNECTION:
1600 case OPEN_GLOBAL_TRANSACTION:
1601 case CLOSE_GLOBAL_TRANSACTION:
1602 case SET_ORIENTATION:
1603 case FREEZE_DISPLAY:
1604 case UNFREEZE_DISPLAY:
1605 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001606 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001607 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608 {
1609 // codes that require permission check
1610 IPCThreadState* ipc = IPCThreadState::self();
1611 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001612 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001613 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1614 LOGE("Permission Denial: "
1615 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1616 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001618 break;
1619 }
1620 case CAPTURE_SCREEN:
1621 {
1622 // codes that require permission check
1623 IPCThreadState* ipc = IPCThreadState::self();
1624 const int pid = ipc->getCallingPid();
1625 const int uid = ipc->getCallingUid();
1626 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1627 LOGE("Permission Denial: "
1628 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1629 return PERMISSION_DENIED;
1630 }
1631 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 }
1633 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001634
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1636 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001637 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001638 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1639 IPCThreadState* ipc = IPCThreadState::self();
1640 const int pid = ipc->getCallingPid();
1641 const int uid = ipc->getCallingUid();
1642 LOGE("Permission Denial: "
1643 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 return PERMISSION_DENIED;
1645 }
1646 int n;
1647 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001648 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001649 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 return NO_ERROR;
1651 case 1002: // SHOW_UPDATES
1652 n = data.readInt32();
1653 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1654 return NO_ERROR;
1655 case 1003: // SHOW_BACKGROUND
1656 n = data.readInt32();
1657 mDebugBackground = n ? 1 : 0;
1658 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001659 case 1008: // toggle use of hw composer
1660 n = data.readInt32();
1661 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianad456f92011-01-13 17:53:01 -08001662 invalidateHwcGeometry();
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001663 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 case 1004:{ // repaint everything
1665 Mutex::Autolock _l(mStateLock);
1666 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1667 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1668 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001669 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001671 case 1005:{ // force transaction
1672 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1673 return NO_ERROR;
1674 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001675 case 1006:{ // enable/disable GraphicLog
1676 int enabled = data.readInt32();
1677 GraphicLog::getInstance().setEnabled(enabled);
1678 return NO_ERROR;
1679 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 case 1007: // set mFreezeCount
1681 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001682 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 return NO_ERROR;
1684 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001685 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 reply->writeInt32(0);
1687 reply->writeInt32(mDebugRegion);
1688 reply->writeInt32(mDebugBackground);
1689 return NO_ERROR;
1690 case 1013: {
1691 Mutex::Autolock _l(mStateLock);
1692 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1693 reply->writeInt32(hw.getPageFlipCount());
1694 }
1695 return NO_ERROR;
1696 }
1697 }
1698 return err;
1699}
1700
Mathias Agopian59119e62010-10-11 12:37:43 -07001701// ---------------------------------------------------------------------------
1702
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001703status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1704 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001705{
Mathias Agopian59119e62010-10-11 12:37:43 -07001706 if (!GLExtensions::getInstance().haveFramebufferObject())
1707 return INVALID_OPERATION;
1708
1709 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001710 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001711 const uint32_t hw_w = hw.getWidth();
1712 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001713 GLfloat u = 1;
1714 GLfloat v = 1;
1715
1716 // make sure to clear all GL error flags
1717 while ( glGetError() != GL_NO_ERROR ) ;
1718
1719 // create a FBO
1720 GLuint name, tname;
1721 glGenTextures(1, &tname);
1722 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001723 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1724 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001725 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001726 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001727 GLint tw = (2 << (31 - clz(hw_w)));
1728 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001729 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1730 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001731 u = GLfloat(hw_w) / tw;
1732 v = GLfloat(hw_h) / th;
1733 }
1734 glGenFramebuffersOES(1, &name);
1735 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001736 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1737 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001738
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001739 // redraw the screen entirely...
1740 glClearColor(0,0,0,1);
1741 glClear(GL_COLOR_BUFFER_BIT);
1742 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1743 const size_t count = layers.size();
1744 for (size_t i=0 ; i<count ; ++i) {
1745 const sp<LayerBase>& layer(layers[i]);
1746 layer->drawForSreenShot();
1747 }
1748
1749 // back to main framebuffer
1750 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1751 glDisable(GL_SCISSOR_TEST);
1752 glDeleteFramebuffersOES(1, &name);
1753
1754 *textureName = tname;
1755 *uOut = u;
1756 *vOut = v;
1757 return NO_ERROR;
1758}
1759
1760// ---------------------------------------------------------------------------
1761
1762status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1763{
1764 status_t result = PERMISSION_DENIED;
1765
1766 if (!GLExtensions::getInstance().haveFramebufferObject())
1767 return INVALID_OPERATION;
1768
1769 // get screen geometry
1770 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1771 const uint32_t hw_w = hw.getWidth();
1772 const uint32_t hw_h = hw.getHeight();
1773 const Region screenBounds(hw.bounds());
1774
1775 GLfloat u, v;
1776 GLuint tname;
1777 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1778 if (result != NO_ERROR) {
1779 return result;
1780 }
1781
1782 GLfloat vtx[8];
1783 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1784 glEnable(GL_TEXTURE_2D);
1785 glBindTexture(GL_TEXTURE_2D, tname);
1786 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1787 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1788 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1789 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1790 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1791 glVertexPointer(2, GL_FLOAT, 0, vtx);
1792
1793 class s_curve_interpolator {
1794 const float nbFrames, s, v;
1795 public:
1796 s_curve_interpolator(int nbFrames, float s)
1797 : nbFrames(1.0f / (nbFrames-1)), s(s),
1798 v(1.0f + expf(-s + 0.5f*s)) {
1799 }
1800 float operator()(int f) {
1801 const float x = f * nbFrames;
1802 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1803 }
1804 };
1805
1806 class v_stretch {
1807 const GLfloat hw_w, hw_h;
1808 public:
1809 v_stretch(uint32_t hw_w, uint32_t hw_h)
1810 : hw_w(hw_w), hw_h(hw_h) {
1811 }
1812 void operator()(GLfloat* vtx, float v) {
1813 const GLfloat w = hw_w + (hw_w * v);
1814 const GLfloat h = hw_h - (hw_h * v);
1815 const GLfloat x = (hw_w - w) * 0.5f;
1816 const GLfloat y = (hw_h - h) * 0.5f;
1817 vtx[0] = x; vtx[1] = y;
1818 vtx[2] = x; vtx[3] = y + h;
1819 vtx[4] = x + w; vtx[5] = y + h;
1820 vtx[6] = x + w; vtx[7] = y;
1821 }
1822 };
1823
1824 class h_stretch {
1825 const GLfloat hw_w, hw_h;
1826 public:
1827 h_stretch(uint32_t hw_w, uint32_t hw_h)
1828 : hw_w(hw_w), hw_h(hw_h) {
1829 }
1830 void operator()(GLfloat* vtx, float v) {
1831 const GLfloat w = hw_w - (hw_w * v);
1832 const GLfloat h = 1.0f;
1833 const GLfloat x = (hw_w - w) * 0.5f;
1834 const GLfloat y = (hw_h - h) * 0.5f;
1835 vtx[0] = x; vtx[1] = y;
1836 vtx[2] = x; vtx[3] = y + h;
1837 vtx[4] = x + w; vtx[5] = y + h;
1838 vtx[6] = x + w; vtx[7] = y;
1839 }
1840 };
1841
1842 // the full animation is 24 frames
1843 const int nbFrames = 12;
1844 s_curve_interpolator itr(nbFrames, 7.5f);
1845 s_curve_interpolator itg(nbFrames, 8.0f);
1846 s_curve_interpolator itb(nbFrames, 8.5f);
1847
1848 v_stretch vverts(hw_w, hw_h);
1849 glEnable(GL_BLEND);
1850 glBlendFunc(GL_ONE, GL_ONE);
1851 for (int i=0 ; i<nbFrames ; i++) {
1852 float x, y, w, h;
1853 const float vr = itr(i);
1854 const float vg = itg(i);
1855 const float vb = itb(i);
1856
1857 // clear screen
1858 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001859 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001860 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001861
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001862 // draw the red plane
1863 vverts(vtx, vr);
1864 glColorMask(1,0,0,1);
1865 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001866
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001867 // draw the green plane
1868 vverts(vtx, vg);
1869 glColorMask(0,1,0,1);
1870 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001871
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001872 // draw the blue plane
1873 vverts(vtx, vb);
1874 glColorMask(0,0,1,1);
1875 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001876
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001877 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001878 glDisable(GL_TEXTURE_2D);
1879 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001880 glColor4f(vg, vg, vg, 1);
1881 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1882 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001883 }
1884
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001885 h_stretch hverts(hw_w, hw_h);
1886 glDisable(GL_BLEND);
1887 glDisable(GL_TEXTURE_2D);
1888 glColorMask(1,1,1,1);
1889 for (int i=0 ; i<nbFrames ; i++) {
1890 const float v = itg(i);
1891 hverts(vtx, v);
1892 glClear(GL_COLOR_BUFFER_BIT);
1893 glColor4f(1-v, 1-v, 1-v, 1);
1894 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1895 hw.flip(screenBounds);
1896 }
1897
1898 glColorMask(1,1,1,1);
1899 glEnable(GL_SCISSOR_TEST);
1900 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1901 glDeleteTextures(1, &tname);
1902 return NO_ERROR;
1903}
1904
1905status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1906{
1907 status_t result = PERMISSION_DENIED;
1908
1909 if (!GLExtensions::getInstance().haveFramebufferObject())
1910 return INVALID_OPERATION;
1911
1912
1913 // get screen geometry
1914 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1915 const uint32_t hw_w = hw.getWidth();
1916 const uint32_t hw_h = hw.getHeight();
1917 const Region screenBounds(hw.bounds());
1918
1919 GLfloat u, v;
1920 GLuint tname;
1921 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1922 if (result != NO_ERROR) {
1923 return result;
1924 }
1925
1926 // back to main framebuffer
1927 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1928 glDisable(GL_SCISSOR_TEST);
1929
1930 GLfloat vtx[8];
1931 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1932 glEnable(GL_TEXTURE_2D);
1933 glBindTexture(GL_TEXTURE_2D, tname);
1934 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1935 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1936 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1937 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1938 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1939 glVertexPointer(2, GL_FLOAT, 0, vtx);
1940
1941 class s_curve_interpolator {
1942 const float nbFrames, s, v;
1943 public:
1944 s_curve_interpolator(int nbFrames, float s)
1945 : nbFrames(1.0f / (nbFrames-1)), s(s),
1946 v(1.0f + expf(-s + 0.5f*s)) {
1947 }
1948 float operator()(int f) {
1949 const float x = f * nbFrames;
1950 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1951 }
1952 };
1953
1954 class v_stretch {
1955 const GLfloat hw_w, hw_h;
1956 public:
1957 v_stretch(uint32_t hw_w, uint32_t hw_h)
1958 : hw_w(hw_w), hw_h(hw_h) {
1959 }
1960 void operator()(GLfloat* vtx, float v) {
1961 const GLfloat w = hw_w + (hw_w * v);
1962 const GLfloat h = hw_h - (hw_h * v);
1963 const GLfloat x = (hw_w - w) * 0.5f;
1964 const GLfloat y = (hw_h - h) * 0.5f;
1965 vtx[0] = x; vtx[1] = y;
1966 vtx[2] = x; vtx[3] = y + h;
1967 vtx[4] = x + w; vtx[5] = y + h;
1968 vtx[6] = x + w; vtx[7] = y;
1969 }
1970 };
1971
1972 class h_stretch {
1973 const GLfloat hw_w, hw_h;
1974 public:
1975 h_stretch(uint32_t hw_w, uint32_t hw_h)
1976 : hw_w(hw_w), hw_h(hw_h) {
1977 }
1978 void operator()(GLfloat* vtx, float v) {
1979 const GLfloat w = hw_w - (hw_w * v);
1980 const GLfloat h = 1.0f;
1981 const GLfloat x = (hw_w - w) * 0.5f;
1982 const GLfloat y = (hw_h - h) * 0.5f;
1983 vtx[0] = x; vtx[1] = y;
1984 vtx[2] = x; vtx[3] = y + h;
1985 vtx[4] = x + w; vtx[5] = y + h;
1986 vtx[6] = x + w; vtx[7] = y;
1987 }
1988 };
1989
Mathias Agopiana6546e52010-10-14 12:33:07 -07001990 // the full animation is 12 frames
1991 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001992 s_curve_interpolator itr(nbFrames, 7.5f);
1993 s_curve_interpolator itg(nbFrames, 8.0f);
1994 s_curve_interpolator itb(nbFrames, 8.5f);
1995
1996 h_stretch hverts(hw_w, hw_h);
1997 glDisable(GL_BLEND);
1998 glDisable(GL_TEXTURE_2D);
1999 glColorMask(1,1,1,1);
2000 for (int i=nbFrames-1 ; i>=0 ; i--) {
2001 const float v = itg(i);
2002 hverts(vtx, v);
2003 glClear(GL_COLOR_BUFFER_BIT);
2004 glColor4f(1-v, 1-v, 1-v, 1);
2005 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2006 hw.flip(screenBounds);
2007 }
2008
Mathias Agopiana6546e52010-10-14 12:33:07 -07002009 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002010 v_stretch vverts(hw_w, hw_h);
2011 glEnable(GL_BLEND);
2012 glBlendFunc(GL_ONE, GL_ONE);
2013 for (int i=nbFrames-1 ; i>=0 ; i--) {
2014 float x, y, w, h;
2015 const float vr = itr(i);
2016 const float vg = itg(i);
2017 const float vb = itb(i);
2018
2019 // clear screen
2020 glColorMask(1,1,1,1);
2021 glClear(GL_COLOR_BUFFER_BIT);
2022 glEnable(GL_TEXTURE_2D);
2023
2024 // draw the red plane
2025 vverts(vtx, vr);
2026 glColorMask(1,0,0,1);
2027 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2028
2029 // draw the green plane
2030 vverts(vtx, vg);
2031 glColorMask(0,1,0,1);
2032 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2033
2034 // draw the blue plane
2035 vverts(vtx, vb);
2036 glColorMask(0,0,1,1);
2037 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2038
2039 hw.flip(screenBounds);
2040 }
2041
2042 glColorMask(1,1,1,1);
2043 glEnable(GL_SCISSOR_TEST);
2044 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002045 glDeleteTextures(1, &tname);
2046
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 return NO_ERROR;
2048}
2049
2050// ---------------------------------------------------------------------------
2051
Mathias Agopianabd671a2010-10-14 14:54:06 -07002052status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002053{
2054 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2055 if (!hw.canDraw()) {
2056 // we're already off
2057 return NO_ERROR;
2058 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002059 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2060 electronBeamOffAnimationImplLocked();
2061 }
2062
2063 // always clear the whole screen at the end of the animation
2064 glClearColor(0,0,0,1);
2065 glDisable(GL_SCISSOR_TEST);
2066 glClear(GL_COLOR_BUFFER_BIT);
2067 glEnable(GL_SCISSOR_TEST);
2068 hw.flip( Region(hw.bounds()) );
2069
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002070 hw.setCanDraw(false);
2071 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002072}
2073
2074status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2075{
Mathias Agopian59119e62010-10-11 12:37:43 -07002076 class MessageTurnElectronBeamOff : public MessageBase {
2077 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002078 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002079 status_t result;
2080 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002081 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2082 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002083 }
2084 status_t getResult() const {
2085 return result;
2086 }
2087 virtual bool handler() {
2088 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002089 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002090 return true;
2091 }
2092 };
2093
Mathias Agopianabd671a2010-10-14 14:54:06 -07002094 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002095 status_t res = postMessageSync(msg);
2096 if (res == NO_ERROR) {
2097 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098
2099 // work-around: when the power-manager calls us we activate the
2100 // animation. eventually, the "on" animation will be called
2101 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002102 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002103 }
2104 return res;
2105}
2106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002108
Mathias Agopianabd671a2010-10-14 14:54:06 -07002109status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002110{
2111 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2112 if (hw.canDraw()) {
2113 // we're already on
2114 return NO_ERROR;
2115 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002116 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2117 electronBeamOnAnimationImplLocked();
2118 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002119 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002120
2121 // make sure to redraw the whole screen when the animation is done
2122 mDirtyRegion.set(hw.bounds());
2123 signalEvent();
2124
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002125 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002126}
2127
2128status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2129{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002130 class MessageTurnElectronBeamOn : public MessageBase {
2131 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002132 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002133 status_t result;
2134 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002135 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2136 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002137 }
2138 status_t getResult() const {
2139 return result;
2140 }
2141 virtual bool handler() {
2142 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002143 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002144 return true;
2145 }
2146 };
2147
Mathias Agopianabd671a2010-10-14 14:54:06 -07002148 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002149 return NO_ERROR;
2150}
2151
2152// ---------------------------------------------------------------------------
2153
Mathias Agopian74c40c02010-09-29 13:02:36 -07002154status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2155 sp<IMemoryHeap>* heap,
2156 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002157 uint32_t sw, uint32_t sh,
2158 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002159{
2160 status_t result = PERMISSION_DENIED;
2161
2162 // only one display supported for now
2163 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2164 return BAD_VALUE;
2165
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08002166 // make sure none of the layers are protected
2167 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2168 const size_t count = layers.size();
2169 for (size_t i=0 ; i<count ; ++i) {
2170 const sp<LayerBase>& layer(layers[i]);
2171 const uint32_t z = layer->drawingState().z;
2172 if (z >= minLayerZ && z <= maxLayerZ) {
2173 if (layer->isProtected()) {
2174 return INVALID_OPERATION;
2175 }
2176 }
2177 }
2178
Mathias Agopian74c40c02010-09-29 13:02:36 -07002179 if (!GLExtensions::getInstance().haveFramebufferObject())
2180 return INVALID_OPERATION;
2181
2182 // get screen geometry
2183 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2184 const uint32_t hw_w = hw.getWidth();
2185 const uint32_t hw_h = hw.getHeight();
2186
2187 if ((sw > hw_w) || (sh > hw_h))
2188 return BAD_VALUE;
2189
2190 sw = (!sw) ? hw_w : sw;
2191 sh = (!sh) ? hw_h : sh;
2192 const size_t size = sw * sh * 4;
2193
Mathias Agopian1c71a472011-03-02 18:45:50 -08002194 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2195 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002196
Mathias Agopian74c40c02010-09-29 13:02:36 -07002197 // make sure to clear all GL error flags
2198 while ( glGetError() != GL_NO_ERROR ) ;
2199
2200 // create a FBO
2201 GLuint name, tname;
2202 glGenRenderbuffersOES(1, &tname);
2203 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2204 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2205 glGenFramebuffersOES(1, &name);
2206 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2207 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2208 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2209
2210 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002211
Mathias Agopian74c40c02010-09-29 13:02:36 -07002212 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2213
2214 // invert everything, b/c glReadPixel() below will invert the FB
2215 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002216 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217 glMatrixMode(GL_PROJECTION);
2218 glPushMatrix();
2219 glLoadIdentity();
2220 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2221 glMatrixMode(GL_MODELVIEW);
2222
2223 // redraw the screen entirely...
2224 glClearColor(0,0,0,1);
2225 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002226
Mathias Agopian74c40c02010-09-29 13:02:36 -07002227 for (size_t i=0 ; i<count ; ++i) {
2228 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002229 const uint32_t z = layer->drawingState().z;
2230 if (z >= minLayerZ && z <= maxLayerZ) {
2231 layer->drawForSreenShot();
2232 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002233 }
2234
2235 // XXX: this is needed on tegra
2236 glScissor(0, 0, sw, sh);
2237
2238 // check for errors and return screen capture
2239 if (glGetError() != GL_NO_ERROR) {
2240 // error while rendering
2241 result = INVALID_OPERATION;
2242 } else {
2243 // allocate shared memory large enough to hold the
2244 // screen capture
2245 sp<MemoryHeapBase> base(
2246 new MemoryHeapBase(size, 0, "screen-capture") );
2247 void* const ptr = base->getBase();
2248 if (ptr) {
2249 // capture the screen with glReadPixels()
2250 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2251 if (glGetError() == GL_NO_ERROR) {
2252 *heap = base;
2253 *w = sw;
2254 *h = sh;
2255 *f = PIXEL_FORMAT_RGBA_8888;
2256 result = NO_ERROR;
2257 }
2258 } else {
2259 result = NO_MEMORY;
2260 }
2261 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002262 glEnable(GL_SCISSOR_TEST);
2263 glViewport(0, 0, hw_w, hw_h);
2264 glMatrixMode(GL_PROJECTION);
2265 glPopMatrix();
2266 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002267 } else {
2268 result = BAD_VALUE;
2269 }
2270
2271 // release FBO resources
2272 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2273 glDeleteRenderbuffersOES(1, &tname);
2274 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002275
2276 hw.compositionComplete();
2277
Mathias Agopian1c71a472011-03-02 18:45:50 -08002278 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002279
Mathias Agopian74c40c02010-09-29 13:02:36 -07002280 return result;
2281}
2282
2283
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002284status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2285 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002286 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002287 uint32_t sw, uint32_t sh,
2288 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002289{
2290 // only one display supported for now
2291 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2292 return BAD_VALUE;
2293
2294 if (!GLExtensions::getInstance().haveFramebufferObject())
2295 return INVALID_OPERATION;
2296
2297 class MessageCaptureScreen : public MessageBase {
2298 SurfaceFlinger* flinger;
2299 DisplayID dpy;
2300 sp<IMemoryHeap>* heap;
2301 uint32_t* w;
2302 uint32_t* h;
2303 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304 uint32_t sw;
2305 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002306 uint32_t minLayerZ;
2307 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002308 status_t result;
2309 public:
2310 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002311 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002312 uint32_t sw, uint32_t sh,
2313 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002314 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002315 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2316 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2317 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002318 {
2319 }
2320 status_t getResult() const {
2321 return result;
2322 }
2323 virtual bool handler() {
2324 Mutex::Autolock _l(flinger->mStateLock);
2325
2326 // if we have secure windows, never allow the screen capture
2327 if (flinger->mSecureFrameBuffer)
2328 return true;
2329
Mathias Agopian74c40c02010-09-29 13:02:36 -07002330 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002331 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002332
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002333 return true;
2334 }
2335 };
2336
2337 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002338 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002339 status_t res = postMessageSync(msg);
2340 if (res == NO_ERROR) {
2341 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2342 }
2343 return res;
2344}
2345
2346// ---------------------------------------------------------------------------
2347
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002348sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2349{
2350 sp<Layer> result;
2351 Mutex::Autolock _l(mStateLock);
2352 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2353 return result;
2354}
2355
2356// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002357
Mathias Agopian96f08192010-06-02 23:28:45 -07002358Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002359 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002360{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002361}
2362
Mathias Agopian96f08192010-06-02 23:28:45 -07002363Client::~Client()
2364{
Mathias Agopian96f08192010-06-02 23:28:45 -07002365 const size_t count = mLayers.size();
2366 for (size_t i=0 ; i<count ; i++) {
2367 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2368 if (layer != 0) {
2369 mFlinger->removeLayer(layer);
2370 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002371 }
2372}
2373
Mathias Agopian96f08192010-06-02 23:28:45 -07002374status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002375 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002376}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002377
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002378size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002379{
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002380 Mutex::Autolock _l(mLock);
2381 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002382 mLayers.add(name, layer);
2383 return name;
2384}
2385
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002386void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002387{
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002388 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002389 // we do a linear search here, because this doesn't happen often
2390 const size_t count = mLayers.size();
2391 for (size_t i=0 ; i<count ; i++) {
2392 if (mLayers.valueAt(i) == layer) {
2393 mLayers.removeItemsAt(i, 1);
2394 break;
2395 }
2396 }
2397}
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002398sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2399{
2400 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002401 sp<LayerBaseClient> lbc;
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002402 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002403 if (layer != 0) {
2404 lbc = layer.promote();
2405 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002406 }
2407 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002408}
2409
Mathias Agopian96f08192010-06-02 23:28:45 -07002410sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002411 return 0;
2412}
2413ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2414 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002415}
Mathias Agopian96f08192010-06-02 23:28:45 -07002416sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002417 ISurfaceComposerClient::surface_data_t* params, int pid,
2418 const String8& name,
2419 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002420 uint32_t flags)
2421{
Mathias Agopian96f08192010-06-02 23:28:45 -07002422 return mFlinger->createSurface(this, pid, name, params,
2423 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002424}
Mathias Agopian96f08192010-06-02 23:28:45 -07002425status_t Client::destroySurface(SurfaceID sid) {
2426 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002427}
Mathias Agopian96f08192010-06-02 23:28:45 -07002428status_t Client::setState(int32_t count, const layer_state_t* states) {
2429 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002430}
2431
2432// ---------------------------------------------------------------------------
2433
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002434UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2435 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2436{
2437 const int pgsize = getpagesize();
2438 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2439
2440 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2441 "SurfaceFlinger Client control-block");
2442
2443 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2444 if (ctrlblk) { // construct the shared structure in-place.
2445 new(ctrlblk) SharedClient;
2446 }
2447}
2448
2449UserClient::~UserClient()
2450{
2451 if (ctrlblk) {
2452 ctrlblk->~SharedClient(); // destroy our shared-structure.
2453 }
2454
2455 /*
2456 * When a UserClient dies, it's unclear what to do exactly.
2457 * We could go ahead and destroy all surfaces linked to that client
2458 * however, it wouldn't be fair to the main Client
2459 * (usually the the window-manager), which might want to re-target
2460 * the layer to another UserClient.
2461 * I think the best is to do nothing, or not much; in most cases the
2462 * WM itself will go ahead and clean things up when it detects a client of
2463 * his has died.
2464 * The remaining question is what to display? currently we keep
2465 * just keep the current buffer.
2466 */
2467}
2468
2469status_t UserClient::initCheck() const {
2470 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2471}
2472
2473void UserClient::detachLayer(const Layer* layer)
2474{
2475 int32_t name = layer->getToken();
2476 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002477 int32_t mask = 1LU<<name;
2478 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2479 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2480 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002481 }
2482}
2483
2484sp<IMemoryHeap> UserClient::getControlBlock() const {
2485 return mCblkHeap;
2486}
2487
2488ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2489{
2490 int32_t name = NAME_NOT_FOUND;
2491 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002492 if (layer == 0) {
2493 return name;
2494 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002495
Mathias Agopian579b3f82010-06-08 19:54:15 -07002496 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002497 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002498 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002499 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002500 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002501
2502 name = 0;
2503 do {
2504 int32_t mask = 1LU<<name;
2505 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2506 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002507 status_t err = layer->setToken(
2508 const_cast<UserClient*>(this), ctrlblk, name);
2509 if (err != NO_ERROR) {
2510 // free the name
2511 android_atomic_and(~mask, &mBitmap);
2512 name = err;
2513 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002514 break;
2515 }
Jamie Gennis8a083922011-02-10 16:18:36 -08002516 if (++name >= SharedBufferStack::NUM_LAYERS_MAX)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002517 name = NO_MEMORY;
2518 } while(name >= 0);
2519
Mathias Agopian53503a92010-06-08 15:40:56 -07002520 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2521 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002522 return name;
2523}
2524
2525sp<ISurface> UserClient::createSurface(
2526 ISurfaceComposerClient::surface_data_t* params, int pid,
2527 const String8& name,
2528 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2529 uint32_t flags) {
2530 return 0;
2531}
2532status_t UserClient::destroySurface(SurfaceID sid) {
2533 return INVALID_OPERATION;
2534}
2535status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2536 return INVALID_OPERATION;
2537}
2538
2539// ---------------------------------------------------------------------------
2540
Jamie Gennis9a78c902011-01-12 18:30:40 -08002541GraphicBufferAlloc::GraphicBufferAlloc() {}
2542
2543GraphicBufferAlloc::~GraphicBufferAlloc() {}
2544
2545sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2546 PixelFormat format, uint32_t usage) {
2547 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2548 status_t err = graphicBuffer->initCheck();
2549 if (err != 0) {
2550 LOGE("createGraphicBuffer: init check failed: %d", err);
2551 return 0;
2552 } else if (graphicBuffer->handle == 0) {
2553 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2554 return 0;
2555 }
2556 Mutex::Autolock _l(mLock);
2557 mBuffers.add(graphicBuffer);
2558 return graphicBuffer;
2559}
2560
2561void GraphicBufferAlloc::freeAllGraphicBuffersExcept(int bufIdx) {
2562 Mutex::Autolock _l(mLock);
2563 if (0 <= bufIdx && bufIdx < mBuffers.size()) {
2564 sp<GraphicBuffer> b(mBuffers[bufIdx]);
2565 mBuffers.clear();
2566 mBuffers.add(b);
2567 } else {
2568 mBuffers.clear();
2569 }
2570}
2571
2572// ---------------------------------------------------------------------------
2573
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002574GraphicPlane::GraphicPlane()
2575 : mHw(0)
2576{
2577}
2578
2579GraphicPlane::~GraphicPlane() {
2580 delete mHw;
2581}
2582
2583bool GraphicPlane::initialized() const {
2584 return mHw ? true : false;
2585}
2586
Mathias Agopian2b92d892010-02-08 15:49:35 -08002587int GraphicPlane::getWidth() const {
2588 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002589}
2590
Mathias Agopian2b92d892010-02-08 15:49:35 -08002591int GraphicPlane::getHeight() const {
2592 return mHeight;
2593}
2594
2595void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2596{
2597 mHw = hw;
2598
2599 // initialize the display orientation transform.
2600 // it's a constant that should come from the display driver.
2601 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2602 char property[PROPERTY_VALUE_MAX];
2603 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2604 //displayOrientation
2605 switch (atoi(property)) {
2606 case 90:
2607 displayOrientation = ISurfaceComposer::eOrientation90;
2608 break;
2609 case 270:
2610 displayOrientation = ISurfaceComposer::eOrientation270;
2611 break;
2612 }
2613 }
2614
2615 const float w = hw->getWidth();
2616 const float h = hw->getHeight();
2617 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2618 &mDisplayTransform);
2619 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2620 mDisplayWidth = h;
2621 mDisplayHeight = w;
2622 } else {
2623 mDisplayWidth = w;
2624 mDisplayHeight = h;
2625 }
2626
2627 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628}
2629
2630status_t GraphicPlane::orientationToTransfrom(
2631 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002632{
2633 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634 switch (orientation) {
2635 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002636 flags = Transform::ROT_0;
2637 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002638 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002639 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002640 break;
2641 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002642 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002643 break;
2644 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002645 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002646 break;
2647 default:
2648 return BAD_VALUE;
2649 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002650 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651 return NO_ERROR;
2652}
2653
2654status_t GraphicPlane::setOrientation(int orientation)
2655{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656 // If the rotation can be handled in hardware, this is where
2657 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002658
2659 const DisplayHardware& hw(displayHardware());
2660 const float w = mDisplayWidth;
2661 const float h = mDisplayHeight;
2662 mWidth = int(w);
2663 mHeight = int(h);
2664
2665 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002666 GraphicPlane::orientationToTransfrom(orientation, w, h,
2667 &orientationTransform);
2668 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2669 mWidth = int(h);
2670 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002671 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002672
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002673 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002674 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002675 return NO_ERROR;
2676}
2677
2678const DisplayHardware& GraphicPlane::displayHardware() const {
2679 return *mHw;
2680}
2681
Mathias Agopian59119e62010-10-11 12:37:43 -07002682DisplayHardware& GraphicPlane::editDisplayHardware() {
2683 return *mHw;
2684}
2685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002686const Transform& GraphicPlane::transform() const {
2687 return mGlobalTransform;
2688}
2689
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002690EGLDisplay GraphicPlane::getEGLDisplay() const {
2691 return mHw->getEGLDisplay();
2692}
2693
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002694// ---------------------------------------------------------------------------
2695
2696}; // namespace android