blob: 9a312a7369fe9ba471d5f0cb1366f6aa4846a728 [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);
1392 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1393 "error removing layer=%p (%s)", l.get(), strerror(-err));
1394 }
1395 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396}
1397
1398status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001399 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 int32_t count,
1401 const layer_state_t* states)
1402{
1403 Mutex::Autolock _l(mStateLock);
1404 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001406 const layer_state_t& s(states[i]);
1407 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001408 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410 if (what & ePositionChanged) {
1411 if (layer->setPosition(s.x, s.y))
1412 flags |= eTraversalNeeded;
1413 }
1414 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001415 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001417 mCurrentState.layersSortedByZ.removeAt(idx);
1418 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 // we need traversal (state changed)
1420 // AND transaction (list changed)
1421 flags |= eTransactionNeeded|eTraversalNeeded;
1422 }
1423 }
1424 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001425 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001427 mResizeTransationPending = true;
1428 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 }
1430 if (what & eAlphaChanged) {
1431 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1432 flags |= eTraversalNeeded;
1433 }
1434 if (what & eMatrixChanged) {
1435 if (layer->setMatrix(s.matrix))
1436 flags |= eTraversalNeeded;
1437 }
1438 if (what & eTransparentRegionChanged) {
1439 if (layer->setTransparentRegionHint(s.transparentRegion))
1440 flags |= eTraversalNeeded;
1441 }
1442 if (what & eVisibilityChanged) {
1443 if (layer->setFlags(s.flags, s.mask))
1444 flags |= eTraversalNeeded;
1445 }
1446 }
1447 }
1448 if (flags) {
1449 setTransactionFlags(flags);
1450 }
1451 return NO_ERROR;
1452}
1453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454void SurfaceFlinger::screenReleased(int dpy)
1455{
1456 // this may be called by a signal handler, we can't do too much in here
1457 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1458 signalEvent();
1459}
1460
1461void SurfaceFlinger::screenAcquired(int dpy)
1462{
1463 // this may be called by a signal handler, we can't do too much in here
1464 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1465 signalEvent();
1466}
1467
1468status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1469{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001470 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 char buffer[SIZE];
1472 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001473 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 snprintf(buffer, SIZE, "Permission Denial: "
1475 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1476 IPCThreadState::self()->getCallingPid(),
1477 IPCThreadState::self()->getCallingUid());
1478 result.append(buffer);
1479 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001480
1481 // figure out if we're stuck somewhere
1482 const nsecs_t now = systemTime();
1483 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1484 const nsecs_t inTransaction(mDebugInTransaction);
1485 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1486 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1487
1488 // Try to get the main lock, but don't insist if we can't
1489 // (this would indicate SF is stuck, but we want to be able to
1490 // print something in dumpsys).
1491 int retry = 3;
1492 while (mStateLock.tryLock()<0 && --retry>=0) {
1493 usleep(1000000);
1494 }
1495 const bool locked(retry >= 0);
1496 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001497 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001498 "SurfaceFlinger appears to be unresponsive, "
1499 "dumping anyways (no locks held)\n");
1500 result.append(buffer);
1501 }
1502
Mathias Agopian48b888a2011-01-19 16:15:53 -08001503 /*
1504 * Dump the visible layer list
1505 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1507 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001508 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1509 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001511 const sp<LayerBase>& layer(currentLayers[i]);
1512 layer->dump(result, buffer, SIZE);
1513 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 s.transparentRegion.dump(result, "transparentRegion");
1515 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1516 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1517 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001518
Mathias Agopian48b888a2011-01-19 16:15:53 -08001519 /*
1520 * Dump the layers in the purgatory
1521 */
1522
1523 const size_t purgatorySize = mLayerPurgatory.size();
1524 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1525 result.append(buffer);
1526 for (size_t i=0 ; i<purgatorySize ; i++) {
1527 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1528 layer->shortDump(result, buffer, SIZE);
1529 }
1530
1531 /*
1532 * Dump SurfaceFlinger global state
1533 */
1534
1535 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1536 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 mWormholeRegion.dump(result, "WormholeRegion");
1538 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1539 snprintf(buffer, SIZE,
1540 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1541 mFreezeDisplay?"yes":"no", mFreezeCount,
1542 mCurrentState.orientation, hw.canDraw());
1543 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001544 snprintf(buffer, SIZE,
1545 " last eglSwapBuffers() time: %f us\n"
1546 " last transaction time : %f us\n",
1547 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1548 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001549
Mathias Agopian9795c422009-08-26 16:36:26 -07001550 if (inSwapBuffersDuration || !locked) {
1551 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1552 inSwapBuffersDuration/1000.0);
1553 result.append(buffer);
1554 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001555
Mathias Agopian9795c422009-08-26 16:36:26 -07001556 if (inTransactionDuration || !locked) {
1557 snprintf(buffer, SIZE, " transaction time: %f us\n",
1558 inTransactionDuration/1000.0);
1559 result.append(buffer);
1560 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001561
Mathias Agopian48b888a2011-01-19 16:15:53 -08001562 /*
1563 * Dump HWComposer state
1564 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001565 HWComposer& hwc(hw.getHwComposer());
1566 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1567 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1568 mDebugDisableHWC ? "disabled" : "enabled");
1569 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001570 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001571
Mathias Agopian48b888a2011-01-19 16:15:53 -08001572 /*
1573 * Dump gralloc state
1574 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001575 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001576 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001577 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001578
1579 if (locked) {
1580 mStateLock.unlock();
1581 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 }
1583 write(fd, result.string(), result.size());
1584 return NO_ERROR;
1585}
1586
1587status_t SurfaceFlinger::onTransact(
1588 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1589{
1590 switch (code) {
1591 case CREATE_CONNECTION:
1592 case OPEN_GLOBAL_TRANSACTION:
1593 case CLOSE_GLOBAL_TRANSACTION:
1594 case SET_ORIENTATION:
1595 case FREEZE_DISPLAY:
1596 case UNFREEZE_DISPLAY:
1597 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001598 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001599 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 {
1601 // codes that require permission check
1602 IPCThreadState* ipc = IPCThreadState::self();
1603 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001604 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001605 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1606 LOGE("Permission Denial: "
1607 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1608 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001610 break;
1611 }
1612 case CAPTURE_SCREEN:
1613 {
1614 // codes that require permission check
1615 IPCThreadState* ipc = IPCThreadState::self();
1616 const int pid = ipc->getCallingPid();
1617 const int uid = ipc->getCallingUid();
1618 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1619 LOGE("Permission Denial: "
1620 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1621 return PERMISSION_DENIED;
1622 }
1623 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624 }
1625 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001626
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1628 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001629 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001630 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1631 IPCThreadState* ipc = IPCThreadState::self();
1632 const int pid = ipc->getCallingPid();
1633 const int uid = ipc->getCallingUid();
1634 LOGE("Permission Denial: "
1635 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 return PERMISSION_DENIED;
1637 }
1638 int n;
1639 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001640 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001641 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 return NO_ERROR;
1643 case 1002: // SHOW_UPDATES
1644 n = data.readInt32();
1645 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1646 return NO_ERROR;
1647 case 1003: // SHOW_BACKGROUND
1648 n = data.readInt32();
1649 mDebugBackground = n ? 1 : 0;
1650 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001651 case 1008: // toggle use of hw composer
1652 n = data.readInt32();
1653 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianad456f92011-01-13 17:53:01 -08001654 invalidateHwcGeometry();
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001655 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 case 1004:{ // repaint everything
1657 Mutex::Autolock _l(mStateLock);
1658 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1659 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1660 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001661 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001663 case 1005:{ // force transaction
1664 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1665 return NO_ERROR;
1666 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001667 case 1006:{ // enable/disable GraphicLog
1668 int enabled = data.readInt32();
1669 GraphicLog::getInstance().setEnabled(enabled);
1670 return NO_ERROR;
1671 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672 case 1007: // set mFreezeCount
1673 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001674 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 return NO_ERROR;
1676 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001677 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678 reply->writeInt32(0);
1679 reply->writeInt32(mDebugRegion);
1680 reply->writeInt32(mDebugBackground);
1681 return NO_ERROR;
1682 case 1013: {
1683 Mutex::Autolock _l(mStateLock);
1684 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1685 reply->writeInt32(hw.getPageFlipCount());
1686 }
1687 return NO_ERROR;
1688 }
1689 }
1690 return err;
1691}
1692
Mathias Agopian59119e62010-10-11 12:37:43 -07001693// ---------------------------------------------------------------------------
1694
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001695status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1696 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001697{
Mathias Agopian59119e62010-10-11 12:37:43 -07001698 if (!GLExtensions::getInstance().haveFramebufferObject())
1699 return INVALID_OPERATION;
1700
1701 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001702 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001703 const uint32_t hw_w = hw.getWidth();
1704 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001705 GLfloat u = 1;
1706 GLfloat v = 1;
1707
1708 // make sure to clear all GL error flags
1709 while ( glGetError() != GL_NO_ERROR ) ;
1710
1711 // create a FBO
1712 GLuint name, tname;
1713 glGenTextures(1, &tname);
1714 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001715 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1716 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001717 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001718 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001719 GLint tw = (2 << (31 - clz(hw_w)));
1720 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001721 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1722 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001723 u = GLfloat(hw_w) / tw;
1724 v = GLfloat(hw_h) / th;
1725 }
1726 glGenFramebuffersOES(1, &name);
1727 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001728 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1729 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001730
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001731 // redraw the screen entirely...
1732 glClearColor(0,0,0,1);
1733 glClear(GL_COLOR_BUFFER_BIT);
1734 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1735 const size_t count = layers.size();
1736 for (size_t i=0 ; i<count ; ++i) {
1737 const sp<LayerBase>& layer(layers[i]);
1738 layer->drawForSreenShot();
1739 }
1740
1741 // back to main framebuffer
1742 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1743 glDisable(GL_SCISSOR_TEST);
1744 glDeleteFramebuffersOES(1, &name);
1745
1746 *textureName = tname;
1747 *uOut = u;
1748 *vOut = v;
1749 return NO_ERROR;
1750}
1751
1752// ---------------------------------------------------------------------------
1753
1754status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1755{
1756 status_t result = PERMISSION_DENIED;
1757
1758 if (!GLExtensions::getInstance().haveFramebufferObject())
1759 return INVALID_OPERATION;
1760
1761 // get screen geometry
1762 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1763 const uint32_t hw_w = hw.getWidth();
1764 const uint32_t hw_h = hw.getHeight();
1765 const Region screenBounds(hw.bounds());
1766
1767 GLfloat u, v;
1768 GLuint tname;
1769 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1770 if (result != NO_ERROR) {
1771 return result;
1772 }
1773
1774 GLfloat vtx[8];
1775 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1776 glEnable(GL_TEXTURE_2D);
1777 glBindTexture(GL_TEXTURE_2D, tname);
1778 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1779 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1780 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1781 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1782 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1783 glVertexPointer(2, GL_FLOAT, 0, vtx);
1784
1785 class s_curve_interpolator {
1786 const float nbFrames, s, v;
1787 public:
1788 s_curve_interpolator(int nbFrames, float s)
1789 : nbFrames(1.0f / (nbFrames-1)), s(s),
1790 v(1.0f + expf(-s + 0.5f*s)) {
1791 }
1792 float operator()(int f) {
1793 const float x = f * nbFrames;
1794 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1795 }
1796 };
1797
1798 class v_stretch {
1799 const GLfloat hw_w, hw_h;
1800 public:
1801 v_stretch(uint32_t hw_w, uint32_t hw_h)
1802 : hw_w(hw_w), hw_h(hw_h) {
1803 }
1804 void operator()(GLfloat* vtx, float v) {
1805 const GLfloat w = hw_w + (hw_w * v);
1806 const GLfloat h = hw_h - (hw_h * v);
1807 const GLfloat x = (hw_w - w) * 0.5f;
1808 const GLfloat y = (hw_h - h) * 0.5f;
1809 vtx[0] = x; vtx[1] = y;
1810 vtx[2] = x; vtx[3] = y + h;
1811 vtx[4] = x + w; vtx[5] = y + h;
1812 vtx[6] = x + w; vtx[7] = y;
1813 }
1814 };
1815
1816 class h_stretch {
1817 const GLfloat hw_w, hw_h;
1818 public:
1819 h_stretch(uint32_t hw_w, uint32_t hw_h)
1820 : hw_w(hw_w), hw_h(hw_h) {
1821 }
1822 void operator()(GLfloat* vtx, float v) {
1823 const GLfloat w = hw_w - (hw_w * v);
1824 const GLfloat h = 1.0f;
1825 const GLfloat x = (hw_w - w) * 0.5f;
1826 const GLfloat y = (hw_h - h) * 0.5f;
1827 vtx[0] = x; vtx[1] = y;
1828 vtx[2] = x; vtx[3] = y + h;
1829 vtx[4] = x + w; vtx[5] = y + h;
1830 vtx[6] = x + w; vtx[7] = y;
1831 }
1832 };
1833
1834 // the full animation is 24 frames
1835 const int nbFrames = 12;
1836 s_curve_interpolator itr(nbFrames, 7.5f);
1837 s_curve_interpolator itg(nbFrames, 8.0f);
1838 s_curve_interpolator itb(nbFrames, 8.5f);
1839
1840 v_stretch vverts(hw_w, hw_h);
1841 glEnable(GL_BLEND);
1842 glBlendFunc(GL_ONE, GL_ONE);
1843 for (int i=0 ; i<nbFrames ; i++) {
1844 float x, y, w, h;
1845 const float vr = itr(i);
1846 const float vg = itg(i);
1847 const float vb = itb(i);
1848
1849 // clear screen
1850 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001851 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001852 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001853
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001854 // draw the red plane
1855 vverts(vtx, vr);
1856 glColorMask(1,0,0,1);
1857 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001858
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001859 // draw the green plane
1860 vverts(vtx, vg);
1861 glColorMask(0,1,0,1);
1862 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001863
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001864 // draw the blue plane
1865 vverts(vtx, vb);
1866 glColorMask(0,0,1,1);
1867 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001868
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001869 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001870 glDisable(GL_TEXTURE_2D);
1871 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001872 glColor4f(vg, vg, vg, 1);
1873 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1874 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001875 }
1876
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001877 h_stretch hverts(hw_w, hw_h);
1878 glDisable(GL_BLEND);
1879 glDisable(GL_TEXTURE_2D);
1880 glColorMask(1,1,1,1);
1881 for (int i=0 ; i<nbFrames ; i++) {
1882 const float v = itg(i);
1883 hverts(vtx, v);
1884 glClear(GL_COLOR_BUFFER_BIT);
1885 glColor4f(1-v, 1-v, 1-v, 1);
1886 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1887 hw.flip(screenBounds);
1888 }
1889
1890 glColorMask(1,1,1,1);
1891 glEnable(GL_SCISSOR_TEST);
1892 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1893 glDeleteTextures(1, &tname);
1894 return NO_ERROR;
1895}
1896
1897status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1898{
1899 status_t result = PERMISSION_DENIED;
1900
1901 if (!GLExtensions::getInstance().haveFramebufferObject())
1902 return INVALID_OPERATION;
1903
1904
1905 // get screen geometry
1906 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1907 const uint32_t hw_w = hw.getWidth();
1908 const uint32_t hw_h = hw.getHeight();
1909 const Region screenBounds(hw.bounds());
1910
1911 GLfloat u, v;
1912 GLuint tname;
1913 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1914 if (result != NO_ERROR) {
1915 return result;
1916 }
1917
1918 // back to main framebuffer
1919 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1920 glDisable(GL_SCISSOR_TEST);
1921
1922 GLfloat vtx[8];
1923 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1924 glEnable(GL_TEXTURE_2D);
1925 glBindTexture(GL_TEXTURE_2D, tname);
1926 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1927 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1928 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1929 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1930 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1931 glVertexPointer(2, GL_FLOAT, 0, vtx);
1932
1933 class s_curve_interpolator {
1934 const float nbFrames, s, v;
1935 public:
1936 s_curve_interpolator(int nbFrames, float s)
1937 : nbFrames(1.0f / (nbFrames-1)), s(s),
1938 v(1.0f + expf(-s + 0.5f*s)) {
1939 }
1940 float operator()(int f) {
1941 const float x = f * nbFrames;
1942 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1943 }
1944 };
1945
1946 class v_stretch {
1947 const GLfloat hw_w, hw_h;
1948 public:
1949 v_stretch(uint32_t hw_w, uint32_t hw_h)
1950 : hw_w(hw_w), hw_h(hw_h) {
1951 }
1952 void operator()(GLfloat* vtx, float v) {
1953 const GLfloat w = hw_w + (hw_w * v);
1954 const GLfloat h = hw_h - (hw_h * v);
1955 const GLfloat x = (hw_w - w) * 0.5f;
1956 const GLfloat y = (hw_h - h) * 0.5f;
1957 vtx[0] = x; vtx[1] = y;
1958 vtx[2] = x; vtx[3] = y + h;
1959 vtx[4] = x + w; vtx[5] = y + h;
1960 vtx[6] = x + w; vtx[7] = y;
1961 }
1962 };
1963
1964 class h_stretch {
1965 const GLfloat hw_w, hw_h;
1966 public:
1967 h_stretch(uint32_t hw_w, uint32_t hw_h)
1968 : hw_w(hw_w), hw_h(hw_h) {
1969 }
1970 void operator()(GLfloat* vtx, float v) {
1971 const GLfloat w = hw_w - (hw_w * v);
1972 const GLfloat h = 1.0f;
1973 const GLfloat x = (hw_w - w) * 0.5f;
1974 const GLfloat y = (hw_h - h) * 0.5f;
1975 vtx[0] = x; vtx[1] = y;
1976 vtx[2] = x; vtx[3] = y + h;
1977 vtx[4] = x + w; vtx[5] = y + h;
1978 vtx[6] = x + w; vtx[7] = y;
1979 }
1980 };
1981
Mathias Agopiana6546e52010-10-14 12:33:07 -07001982 // the full animation is 12 frames
1983 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001984 s_curve_interpolator itr(nbFrames, 7.5f);
1985 s_curve_interpolator itg(nbFrames, 8.0f);
1986 s_curve_interpolator itb(nbFrames, 8.5f);
1987
1988 h_stretch hverts(hw_w, hw_h);
1989 glDisable(GL_BLEND);
1990 glDisable(GL_TEXTURE_2D);
1991 glColorMask(1,1,1,1);
1992 for (int i=nbFrames-1 ; i>=0 ; i--) {
1993 const float v = itg(i);
1994 hverts(vtx, v);
1995 glClear(GL_COLOR_BUFFER_BIT);
1996 glColor4f(1-v, 1-v, 1-v, 1);
1997 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1998 hw.flip(screenBounds);
1999 }
2000
Mathias Agopiana6546e52010-10-14 12:33:07 -07002001 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002 v_stretch vverts(hw_w, hw_h);
2003 glEnable(GL_BLEND);
2004 glBlendFunc(GL_ONE, GL_ONE);
2005 for (int i=nbFrames-1 ; i>=0 ; i--) {
2006 float x, y, w, h;
2007 const float vr = itr(i);
2008 const float vg = itg(i);
2009 const float vb = itb(i);
2010
2011 // clear screen
2012 glColorMask(1,1,1,1);
2013 glClear(GL_COLOR_BUFFER_BIT);
2014 glEnable(GL_TEXTURE_2D);
2015
2016 // draw the red plane
2017 vverts(vtx, vr);
2018 glColorMask(1,0,0,1);
2019 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2020
2021 // draw the green plane
2022 vverts(vtx, vg);
2023 glColorMask(0,1,0,1);
2024 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2025
2026 // draw the blue plane
2027 vverts(vtx, vb);
2028 glColorMask(0,0,1,1);
2029 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2030
2031 hw.flip(screenBounds);
2032 }
2033
2034 glColorMask(1,1,1,1);
2035 glEnable(GL_SCISSOR_TEST);
2036 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002037 glDeleteTextures(1, &tname);
2038
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 return NO_ERROR;
2040}
2041
2042// ---------------------------------------------------------------------------
2043
Mathias Agopianabd671a2010-10-14 14:54:06 -07002044status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045{
2046 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2047 if (!hw.canDraw()) {
2048 // we're already off
2049 return NO_ERROR;
2050 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002051 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2052 electronBeamOffAnimationImplLocked();
2053 }
2054
2055 // always clear the whole screen at the end of the animation
2056 glClearColor(0,0,0,1);
2057 glDisable(GL_SCISSOR_TEST);
2058 glClear(GL_COLOR_BUFFER_BIT);
2059 glEnable(GL_SCISSOR_TEST);
2060 hw.flip( Region(hw.bounds()) );
2061
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002062 hw.setCanDraw(false);
2063 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002064}
2065
2066status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2067{
Mathias Agopian59119e62010-10-11 12:37:43 -07002068 class MessageTurnElectronBeamOff : public MessageBase {
2069 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002070 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002071 status_t result;
2072 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002073 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2074 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002075 }
2076 status_t getResult() const {
2077 return result;
2078 }
2079 virtual bool handler() {
2080 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002081 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002082 return true;
2083 }
2084 };
2085
Mathias Agopianabd671a2010-10-14 14:54:06 -07002086 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002087 status_t res = postMessageSync(msg);
2088 if (res == NO_ERROR) {
2089 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002090
2091 // work-around: when the power-manager calls us we activate the
2092 // animation. eventually, the "on" animation will be called
2093 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002094 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002095 }
2096 return res;
2097}
2098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002099// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002100
Mathias Agopianabd671a2010-10-14 14:54:06 -07002101status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002102{
2103 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2104 if (hw.canDraw()) {
2105 // we're already on
2106 return NO_ERROR;
2107 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002108 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2109 electronBeamOnAnimationImplLocked();
2110 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002111 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002112
2113 // make sure to redraw the whole screen when the animation is done
2114 mDirtyRegion.set(hw.bounds());
2115 signalEvent();
2116
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002117 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002118}
2119
2120status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2121{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002122 class MessageTurnElectronBeamOn : public MessageBase {
2123 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002124 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002125 status_t result;
2126 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002127 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2128 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002129 }
2130 status_t getResult() const {
2131 return result;
2132 }
2133 virtual bool handler() {
2134 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002135 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002136 return true;
2137 }
2138 };
2139
Mathias Agopianabd671a2010-10-14 14:54:06 -07002140 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002141 return NO_ERROR;
2142}
2143
2144// ---------------------------------------------------------------------------
2145
Mathias Agopian74c40c02010-09-29 13:02:36 -07002146status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2147 sp<IMemoryHeap>* heap,
2148 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002149 uint32_t sw, uint32_t sh,
2150 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002151{
2152 status_t result = PERMISSION_DENIED;
2153
2154 // only one display supported for now
2155 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2156 return BAD_VALUE;
2157
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08002158 // make sure none of the layers are protected
2159 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2160 const size_t count = layers.size();
2161 for (size_t i=0 ; i<count ; ++i) {
2162 const sp<LayerBase>& layer(layers[i]);
2163 const uint32_t z = layer->drawingState().z;
2164 if (z >= minLayerZ && z <= maxLayerZ) {
2165 if (layer->isProtected()) {
2166 return INVALID_OPERATION;
2167 }
2168 }
2169 }
2170
Mathias Agopian74c40c02010-09-29 13:02:36 -07002171 if (!GLExtensions::getInstance().haveFramebufferObject())
2172 return INVALID_OPERATION;
2173
2174 // get screen geometry
2175 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2176 const uint32_t hw_w = hw.getWidth();
2177 const uint32_t hw_h = hw.getHeight();
2178
2179 if ((sw > hw_w) || (sh > hw_h))
2180 return BAD_VALUE;
2181
2182 sw = (!sw) ? hw_w : sw;
2183 sh = (!sh) ? hw_h : sh;
2184 const size_t size = sw * sh * 4;
2185
Mathias Agopian1c71a472011-03-02 18:45:50 -08002186 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2187 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002188
Mathias Agopian74c40c02010-09-29 13:02:36 -07002189 // make sure to clear all GL error flags
2190 while ( glGetError() != GL_NO_ERROR ) ;
2191
2192 // create a FBO
2193 GLuint name, tname;
2194 glGenRenderbuffersOES(1, &tname);
2195 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2196 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2197 glGenFramebuffersOES(1, &name);
2198 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2199 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2200 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2201
2202 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002203
Mathias Agopian74c40c02010-09-29 13:02:36 -07002204 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2205
2206 // invert everything, b/c glReadPixel() below will invert the FB
2207 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002208 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002209 glMatrixMode(GL_PROJECTION);
2210 glPushMatrix();
2211 glLoadIdentity();
2212 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2213 glMatrixMode(GL_MODELVIEW);
2214
2215 // redraw the screen entirely...
2216 glClearColor(0,0,0,1);
2217 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002218
Mathias Agopian74c40c02010-09-29 13:02:36 -07002219 for (size_t i=0 ; i<count ; ++i) {
2220 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002221 const uint32_t z = layer->drawingState().z;
2222 if (z >= minLayerZ && z <= maxLayerZ) {
2223 layer->drawForSreenShot();
2224 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002225 }
2226
2227 // XXX: this is needed on tegra
2228 glScissor(0, 0, sw, sh);
2229
2230 // check for errors and return screen capture
2231 if (glGetError() != GL_NO_ERROR) {
2232 // error while rendering
2233 result = INVALID_OPERATION;
2234 } else {
2235 // allocate shared memory large enough to hold the
2236 // screen capture
2237 sp<MemoryHeapBase> base(
2238 new MemoryHeapBase(size, 0, "screen-capture") );
2239 void* const ptr = base->getBase();
2240 if (ptr) {
2241 // capture the screen with glReadPixels()
2242 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2243 if (glGetError() == GL_NO_ERROR) {
2244 *heap = base;
2245 *w = sw;
2246 *h = sh;
2247 *f = PIXEL_FORMAT_RGBA_8888;
2248 result = NO_ERROR;
2249 }
2250 } else {
2251 result = NO_MEMORY;
2252 }
2253 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002254 glEnable(GL_SCISSOR_TEST);
2255 glViewport(0, 0, hw_w, hw_h);
2256 glMatrixMode(GL_PROJECTION);
2257 glPopMatrix();
2258 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259 } else {
2260 result = BAD_VALUE;
2261 }
2262
2263 // release FBO resources
2264 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2265 glDeleteRenderbuffersOES(1, &tname);
2266 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002267
2268 hw.compositionComplete();
2269
Mathias Agopian1c71a472011-03-02 18:45:50 -08002270 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002271
Mathias Agopian74c40c02010-09-29 13:02:36 -07002272 return result;
2273}
2274
2275
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002276status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2277 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002278 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002279 uint32_t sw, uint32_t sh,
2280 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002281{
2282 // only one display supported for now
2283 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2284 return BAD_VALUE;
2285
2286 if (!GLExtensions::getInstance().haveFramebufferObject())
2287 return INVALID_OPERATION;
2288
2289 class MessageCaptureScreen : public MessageBase {
2290 SurfaceFlinger* flinger;
2291 DisplayID dpy;
2292 sp<IMemoryHeap>* heap;
2293 uint32_t* w;
2294 uint32_t* h;
2295 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002296 uint32_t sw;
2297 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002298 uint32_t minLayerZ;
2299 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002300 status_t result;
2301 public:
2302 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002303 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002304 uint32_t sw, uint32_t sh,
2305 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002306 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002307 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2308 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2309 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002310 {
2311 }
2312 status_t getResult() const {
2313 return result;
2314 }
2315 virtual bool handler() {
2316 Mutex::Autolock _l(flinger->mStateLock);
2317
2318 // if we have secure windows, never allow the screen capture
2319 if (flinger->mSecureFrameBuffer)
2320 return true;
2321
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002323 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002324
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002325 return true;
2326 }
2327 };
2328
2329 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002330 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002331 status_t res = postMessageSync(msg);
2332 if (res == NO_ERROR) {
2333 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2334 }
2335 return res;
2336}
2337
2338// ---------------------------------------------------------------------------
2339
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002340sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2341{
2342 sp<Layer> result;
2343 Mutex::Autolock _l(mStateLock);
2344 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2345 return result;
2346}
2347
2348// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002349
Mathias Agopian96f08192010-06-02 23:28:45 -07002350Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002351 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002352{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002353}
2354
Mathias Agopian96f08192010-06-02 23:28:45 -07002355Client::~Client()
2356{
Mathias Agopian96f08192010-06-02 23:28:45 -07002357 const size_t count = mLayers.size();
2358 for (size_t i=0 ; i<count ; i++) {
2359 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2360 if (layer != 0) {
2361 mFlinger->removeLayer(layer);
2362 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002363 }
2364}
2365
Mathias Agopian96f08192010-06-02 23:28:45 -07002366status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002367 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002368}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002369
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002370size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002371{
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002372 Mutex::Autolock _l(mLock);
2373 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002374 mLayers.add(name, layer);
2375 return name;
2376}
2377
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002378void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002379{
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002380 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002381 // we do a linear search here, because this doesn't happen often
2382 const size_t count = mLayers.size();
2383 for (size_t i=0 ; i<count ; i++) {
2384 if (mLayers.valueAt(i) == layer) {
2385 mLayers.removeItemsAt(i, 1);
2386 break;
2387 }
2388 }
2389}
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002390sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2391{
2392 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002393 sp<LayerBaseClient> lbc;
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002394 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002395 if (layer != 0) {
2396 lbc = layer.promote();
2397 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002398 }
2399 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002400}
2401
Mathias Agopian96f08192010-06-02 23:28:45 -07002402sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002403 return 0;
2404}
2405ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2406 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002407}
Mathias Agopian96f08192010-06-02 23:28:45 -07002408sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002409 ISurfaceComposerClient::surface_data_t* params, int pid,
2410 const String8& name,
2411 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002412 uint32_t flags)
2413{
Mathias Agopian96f08192010-06-02 23:28:45 -07002414 return mFlinger->createSurface(this, pid, name, params,
2415 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002416}
Mathias Agopian96f08192010-06-02 23:28:45 -07002417status_t Client::destroySurface(SurfaceID sid) {
2418 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419}
Mathias Agopian96f08192010-06-02 23:28:45 -07002420status_t Client::setState(int32_t count, const layer_state_t* states) {
2421 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002422}
2423
2424// ---------------------------------------------------------------------------
2425
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002426UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2427 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2428{
2429 const int pgsize = getpagesize();
2430 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2431
2432 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2433 "SurfaceFlinger Client control-block");
2434
2435 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2436 if (ctrlblk) { // construct the shared structure in-place.
2437 new(ctrlblk) SharedClient;
2438 }
2439}
2440
2441UserClient::~UserClient()
2442{
2443 if (ctrlblk) {
2444 ctrlblk->~SharedClient(); // destroy our shared-structure.
2445 }
2446
2447 /*
2448 * When a UserClient dies, it's unclear what to do exactly.
2449 * We could go ahead and destroy all surfaces linked to that client
2450 * however, it wouldn't be fair to the main Client
2451 * (usually the the window-manager), which might want to re-target
2452 * the layer to another UserClient.
2453 * I think the best is to do nothing, or not much; in most cases the
2454 * WM itself will go ahead and clean things up when it detects a client of
2455 * his has died.
2456 * The remaining question is what to display? currently we keep
2457 * just keep the current buffer.
2458 */
2459}
2460
2461status_t UserClient::initCheck() const {
2462 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2463}
2464
2465void UserClient::detachLayer(const Layer* layer)
2466{
2467 int32_t name = layer->getToken();
2468 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002469 int32_t mask = 1LU<<name;
2470 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2471 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2472 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002473 }
2474}
2475
2476sp<IMemoryHeap> UserClient::getControlBlock() const {
2477 return mCblkHeap;
2478}
2479
2480ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2481{
2482 int32_t name = NAME_NOT_FOUND;
2483 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002484 if (layer == 0) {
2485 return name;
2486 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002487
Mathias Agopian579b3f82010-06-08 19:54:15 -07002488 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002489 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002490 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002491 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002492 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002493
2494 name = 0;
2495 do {
2496 int32_t mask = 1LU<<name;
2497 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2498 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002499 status_t err = layer->setToken(
2500 const_cast<UserClient*>(this), ctrlblk, name);
2501 if (err != NO_ERROR) {
2502 // free the name
2503 android_atomic_and(~mask, &mBitmap);
2504 name = err;
2505 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002506 break;
2507 }
Jamie Gennis8a083922011-02-10 16:18:36 -08002508 if (++name >= SharedBufferStack::NUM_LAYERS_MAX)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002509 name = NO_MEMORY;
2510 } while(name >= 0);
2511
Mathias Agopian53503a92010-06-08 15:40:56 -07002512 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2513 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002514 return name;
2515}
2516
2517sp<ISurface> UserClient::createSurface(
2518 ISurfaceComposerClient::surface_data_t* params, int pid,
2519 const String8& name,
2520 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2521 uint32_t flags) {
2522 return 0;
2523}
2524status_t UserClient::destroySurface(SurfaceID sid) {
2525 return INVALID_OPERATION;
2526}
2527status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2528 return INVALID_OPERATION;
2529}
2530
2531// ---------------------------------------------------------------------------
2532
Jamie Gennis9a78c902011-01-12 18:30:40 -08002533GraphicBufferAlloc::GraphicBufferAlloc() {}
2534
2535GraphicBufferAlloc::~GraphicBufferAlloc() {}
2536
2537sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2538 PixelFormat format, uint32_t usage) {
2539 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2540 status_t err = graphicBuffer->initCheck();
2541 if (err != 0) {
2542 LOGE("createGraphicBuffer: init check failed: %d", err);
2543 return 0;
2544 } else if (graphicBuffer->handle == 0) {
2545 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2546 return 0;
2547 }
2548 Mutex::Autolock _l(mLock);
2549 mBuffers.add(graphicBuffer);
2550 return graphicBuffer;
2551}
2552
2553void GraphicBufferAlloc::freeAllGraphicBuffersExcept(int bufIdx) {
2554 Mutex::Autolock _l(mLock);
2555 if (0 <= bufIdx && bufIdx < mBuffers.size()) {
2556 sp<GraphicBuffer> b(mBuffers[bufIdx]);
2557 mBuffers.clear();
2558 mBuffers.add(b);
2559 } else {
2560 mBuffers.clear();
2561 }
2562}
2563
2564// ---------------------------------------------------------------------------
2565
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002566GraphicPlane::GraphicPlane()
2567 : mHw(0)
2568{
2569}
2570
2571GraphicPlane::~GraphicPlane() {
2572 delete mHw;
2573}
2574
2575bool GraphicPlane::initialized() const {
2576 return mHw ? true : false;
2577}
2578
Mathias Agopian2b92d892010-02-08 15:49:35 -08002579int GraphicPlane::getWidth() const {
2580 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581}
2582
Mathias Agopian2b92d892010-02-08 15:49:35 -08002583int GraphicPlane::getHeight() const {
2584 return mHeight;
2585}
2586
2587void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2588{
2589 mHw = hw;
2590
2591 // initialize the display orientation transform.
2592 // it's a constant that should come from the display driver.
2593 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2594 char property[PROPERTY_VALUE_MAX];
2595 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2596 //displayOrientation
2597 switch (atoi(property)) {
2598 case 90:
2599 displayOrientation = ISurfaceComposer::eOrientation90;
2600 break;
2601 case 270:
2602 displayOrientation = ISurfaceComposer::eOrientation270;
2603 break;
2604 }
2605 }
2606
2607 const float w = hw->getWidth();
2608 const float h = hw->getHeight();
2609 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2610 &mDisplayTransform);
2611 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2612 mDisplayWidth = h;
2613 mDisplayHeight = w;
2614 } else {
2615 mDisplayWidth = w;
2616 mDisplayHeight = h;
2617 }
2618
2619 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002620}
2621
2622status_t GraphicPlane::orientationToTransfrom(
2623 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002624{
2625 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626 switch (orientation) {
2627 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002628 flags = Transform::ROT_0;
2629 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002631 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002632 break;
2633 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002634 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002635 break;
2636 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002637 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002638 break;
2639 default:
2640 return BAD_VALUE;
2641 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002642 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002643 return NO_ERROR;
2644}
2645
2646status_t GraphicPlane::setOrientation(int orientation)
2647{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002648 // If the rotation can be handled in hardware, this is where
2649 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002650
2651 const DisplayHardware& hw(displayHardware());
2652 const float w = mDisplayWidth;
2653 const float h = mDisplayHeight;
2654 mWidth = int(w);
2655 mHeight = int(h);
2656
2657 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002658 GraphicPlane::orientationToTransfrom(orientation, w, h,
2659 &orientationTransform);
2660 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2661 mWidth = int(h);
2662 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002663 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002664
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002665 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002666 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002667 return NO_ERROR;
2668}
2669
2670const DisplayHardware& GraphicPlane::displayHardware() const {
2671 return *mHw;
2672}
2673
Mathias Agopian59119e62010-10-11 12:37:43 -07002674DisplayHardware& GraphicPlane::editDisplayHardware() {
2675 return *mHw;
2676}
2677
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678const Transform& GraphicPlane::transform() const {
2679 return mGlobalTransform;
2680}
2681
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002682EGLDisplay GraphicPlane::getEGLDisplay() const {
2683 return mHw->getEGLDisplay();
2684}
2685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002686// ---------------------------------------------------------------------------
2687
2688}; // namespace android