blob: 2f3a144ec1f11e198f5f3a536e9ec9a07f9dc695 [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
390 // check for transactions
391 if (UNLIKELY(mConsoleSignals)) {
392 handleConsoleEvents();
393 }
394
395 if (LIKELY(mTransactionCount == 0)) {
396 // if we're in a global transaction, don't do anything.
397 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
398 uint32_t transactionFlags = getTransactionFlags(mask);
399 if (LIKELY(transactionFlags)) {
400 handleTransaction(transactionFlags);
401 }
402 }
403
404 // post surfaces (if needed)
405 handlePageFlip();
406
Mathias Agopiana350ff92010-08-10 17:14:02 -0700407 if (UNLIKELY(mHwWorkListDirty)) {
408 // build the h/w work list
409 handleWorkList();
410 }
411
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700413 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700415
416 const int index = hw.getCurrentBufferIndex();
417 GraphicLog& logger(GraphicLog::getInstance());
418
419 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420 handleRepaint();
421
Mathias Agopian74faca22009-09-17 16:18:16 -0700422 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700423 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700424 hw.compositionComplete();
425
Mathias Agopian35b48d12010-09-13 22:57:58 -0700426 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700427 postFramebuffer();
428
Mathias Agopian35b48d12010-09-13 22:57:58 -0700429 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 } else {
431 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800432 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 usleep(16667); // 60 fps period
434 }
435 return true;
436}
437
438void SurfaceFlinger::postFramebuffer()
439{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 if (!mInvalidRegion.isEmpty()) {
441 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700442 const nsecs_t now = systemTime();
443 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700445 mLastSwapBufferTime = systemTime() - now;
446 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448 }
449}
450
451void SurfaceFlinger::handleConsoleEvents()
452{
453 // something to do with the console
454 const DisplayHardware& hw = graphicPlane(0).displayHardware();
455
456 int what = android_atomic_and(0, &mConsoleSignals);
457 if (what & eConsoleAcquired) {
458 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700459 // this is a temporary work-around, eventually this should be called
460 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700461 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 }
463
Mathias Agopian59119e62010-10-11 12:37:43 -0700464 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700465 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 hw.releaseScreen();
468 }
469
470 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700471 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472 hw.releaseScreen();
473 } else {
474 mDeferReleaseConsole = true;
475 }
476 }
477
478 mDirtyRegion.set(hw.bounds());
479}
480
481void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
482{
Mathias Agopian3d579642009-06-04 18:46:21 -0700483 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484
Mathias Agopian4da75192010-08-10 17:19:56 -0700485 /*
486 * Perform and commit the transaction
487 */
488
Mathias Agopian3d579642009-06-04 18:46:21 -0700489 { // scope for the lock
490 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700491 const nsecs_t now = systemTime();
492 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700493 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700494 mLastTransactionTime = systemTime() - now;
495 mDebugInTransaction = 0;
Mathias Agopianad456f92011-01-13 17:53:01 -0800496 invalidateHwcGeometry();
Mathias Agopian4da75192010-08-10 17:19:56 -0700497 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700498 }
499
Mathias Agopian4da75192010-08-10 17:19:56 -0700500 /*
501 * Clean-up all layers that went away
502 * (do this without the lock held)
503 */
Mathias Agopiana350ff92010-08-10 17:14:02 -0700504
Mathias Agopian3d579642009-06-04 18:46:21 -0700505 const size_t count = ditchedLayers.size();
506 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700507 if (ditchedLayers[i] != 0) {
508 //LOGD("ditching layer %p", ditchedLayers[i].get());
509 ditchedLayers[i]->ditch();
510 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700511 }
512}
513
514void SurfaceFlinger::handleTransactionLocked(
515 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
516{
517 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 const size_t count = currentLayers.size();
519
520 /*
521 * Traversal of the children
522 * (perform the transaction for each of them if needed)
523 */
524
525 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
526 if (layersNeedTransaction) {
527 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700528 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
530 if (!trFlags) continue;
531
532 const uint32_t flags = layer->doTransaction(0);
533 if (flags & Layer::eVisibleRegion)
534 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 }
536 }
537
538 /*
539 * Perform our own transaction if needed
540 */
541
542 if (transactionFlags & eTransactionNeeded) {
543 if (mCurrentState.orientation != mDrawingState.orientation) {
544 // the orientation has changed, recompute all visible regions
545 // and invalidate everything.
546
547 const int dpy = 0;
548 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700549 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 GraphicPlane& plane(graphicPlane(dpy));
551 plane.setOrientation(orientation);
552
553 // update the shared control block
554 const DisplayHardware& hw(plane.displayHardware());
555 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
556 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800557 dcblk->w = plane.getWidth();
558 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559
560 mVisibleRegionsDirty = true;
561 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 }
563
564 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
565 // freezing or unfreezing the display -> trigger animation if needed
566 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800567 if (mFreezeDisplay)
568 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 }
570
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700571 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
572 // layers have been added
573 mVisibleRegionsDirty = true;
574 }
575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 // some layers might have been removed, so
577 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700579 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700581 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700582 const size_t count = previousLayers.size();
583 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700584 const sp<LayerBase>& layer(previousLayers[i]);
585 if (currentLayers.indexOf( layer ) < 0) {
586 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700587 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700588 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700589 }
590 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 }
593
594 commitTransaction();
595}
596
597sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
598{
599 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
600}
601
602void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800603 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604{
605 const GraphicPlane& plane(graphicPlane(0));
606 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700607 const DisplayHardware& hw(plane.displayHardware());
608 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609
610 Region aboveOpaqueLayers;
611 Region aboveCoveredLayers;
612 Region dirty;
613
614 bool secureFrameBuffer = false;
615
616 size_t i = currentLayers.size();
617 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700618 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 layer->validateVisibility(planeTransform);
620
621 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700622 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623
Mathias Agopianab028732010-03-16 16:41:46 -0700624 /*
625 * opaqueRegion: area of a surface that is fully opaque.
626 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700628
629 /*
630 * visibleRegion: area of a surface that is visible on screen
631 * and not fully transparent. This is essentially the layer's
632 * footprint minus the opaque regions above it.
633 * Areas covered by a translucent surface are considered visible.
634 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700636
637 /*
638 * coveredRegion: area of a surface that is covered by all
639 * visible regions above it (which includes the translucent areas).
640 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700642
643
644 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700645 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700647 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700649 visibleRegion.andSelf(screenRegion);
650 if (!visibleRegion.isEmpty()) {
651 // Remove the transparent area from the visible region
652 if (translucent) {
653 visibleRegion.subtractSelf(layer->transparentRegionScreen);
654 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655
Mathias Agopianab028732010-03-16 16:41:46 -0700656 // compute the opaque region
657 const int32_t layerOrientation = layer->getOrientation();
658 if (s.alpha==255 && !translucent &&
659 ((layerOrientation & Transform::ROT_INVALID) == false)) {
660 // the opaque region is the layer's footprint
661 opaqueRegion = visibleRegion;
662 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 }
664 }
665
Mathias Agopianab028732010-03-16 16:41:46 -0700666 // Clip the covered region to the visible region
667 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
668
669 // Update aboveCoveredLayers for next (lower) layer
670 aboveCoveredLayers.orSelf(visibleRegion);
671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 // subtract the opaque region covered by the layers above us
673 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674
675 // compute this layer's dirty region
676 if (layer->contentDirty) {
677 // we need to invalidate the whole region
678 dirty = visibleRegion;
679 // as well, as the old visible region
680 dirty.orSelf(layer->visibleRegionScreen);
681 layer->contentDirty = false;
682 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700683 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700684 * the exposed region consists of two components:
685 * 1) what's VISIBLE now and was COVERED before
686 * 2) what's EXPOSED now less what was EXPOSED before
687 *
688 * note that (1) is conservative, we start with the whole
689 * visible region but only keep what used to be covered by
690 * something -- which mean it may have been exposed.
691 *
692 * (2) handles areas that were not covered by anything but got
693 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700694 */
Mathias Agopianab028732010-03-16 16:41:46 -0700695 const Region newExposed = visibleRegion - coveredRegion;
696 const Region oldVisibleRegion = layer->visibleRegionScreen;
697 const Region oldCoveredRegion = layer->coveredRegionScreen;
698 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
699 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 }
701 dirty.subtractSelf(aboveOpaqueLayers);
702
703 // accumulate to the screen dirty region
704 dirtyRegion.orSelf(dirty);
705
Mathias Agopianab028732010-03-16 16:41:46 -0700706 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 // Store the visible region is screen space
710 layer->setVisibleRegion(visibleRegion);
711 layer->setCoveredRegion(coveredRegion);
712
Mathias Agopian97011222009-07-28 10:57:27 -0700713 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 if (layer->isSecure() && !visibleRegion.isEmpty()) {
715 secureFrameBuffer = true;
716 }
717 }
718
Mathias Agopian97011222009-07-28 10:57:27 -0700719 // invalidate the areas where a layer was removed
720 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
721 mDirtyRegionRemovedLayer.clear();
722
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 mSecureFrameBuffer = secureFrameBuffer;
724 opaqueRegion = aboveOpaqueLayers;
725}
726
727
728void SurfaceFlinger::commitTransaction()
729{
730 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700731 mResizeTransationPending = false;
732 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733}
734
735void SurfaceFlinger::handlePageFlip()
736{
737 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800738 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 visibleRegions |= lockPageFlip(currentLayers);
740
741 const DisplayHardware& hw = graphicPlane(0).displayHardware();
742 const Region screenRegion(hw.bounds());
743 if (visibleRegions) {
744 Region opaqueRegion;
745 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700746
747 /*
748 * rebuild the visible layer list
749 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800750 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700751 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700752 mVisibleLayersSortedByZ.setCapacity(count);
753 for (size_t i=0 ; i<count ; i++) {
754 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
755 mVisibleLayersSortedByZ.add(currentLayers[i]);
756 }
757
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 mWormholeRegion = screenRegion.subtract(opaqueRegion);
759 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800760 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 }
762
763 unlockPageFlip(currentLayers);
764 mDirtyRegion.andSelf(screenRegion);
765}
766
Mathias Agopianad456f92011-01-13 17:53:01 -0800767void SurfaceFlinger::invalidateHwcGeometry()
768{
769 mHwWorkListDirty = true;
770}
771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
773{
774 bool recomputeVisibleRegions = false;
775 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700776 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700778 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 layer->lockPageFlip(recomputeVisibleRegions);
780 }
781 return recomputeVisibleRegions;
782}
783
784void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
785{
786 const GraphicPlane& plane(graphicPlane(0));
787 const Transform& planeTransform(plane.transform());
788 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700789 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700791 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 layer->unlockPageFlip(planeTransform, mDirtyRegion);
793 }
794}
795
Mathias Agopiana350ff92010-08-10 17:14:02 -0700796void SurfaceFlinger::handleWorkList()
797{
798 mHwWorkListDirty = false;
799 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
800 if (hwc.initCheck() == NO_ERROR) {
801 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
802 const size_t count = currentLayers.size();
803 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700804 hwc_layer_t* const cur(hwc.getLayers());
805 for (size_t i=0 ; cur && i<count ; i++) {
806 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700807 if (mDebugDisableHWC) {
808 cur[i].compositionType = HWC_FRAMEBUFFER;
809 cur[i].flags |= HWC_SKIP_LAYER;
810 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700811 }
812 }
813}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700814
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815void SurfaceFlinger::handleRepaint()
816{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700817 // compute the invalid region
818 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819
820 if (UNLIKELY(mDebugRegion)) {
821 debugFlashRegions();
822 }
823
Mathias Agopianb8a55602009-06-26 19:06:36 -0700824 // set the frame buffer
825 const DisplayHardware& hw(graphicPlane(0).displayHardware());
826 glMatrixMode(GL_MODELVIEW);
827 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828
829 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700830 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
831 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700832 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700833 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
834 // takes a rectangle, we must make sure to update that whole
835 // rectangle in that case
836 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700837 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 mDirtyRegion.set(mInvalidRegion.bounds());
840 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // in the BUFFER_PRESERVED case, obviously, we can update only
842 // what's needed and nothing more.
843 // NOTE: this is NOT a common case, as preserving the backbuffer
844 // is costly and usually involves copying the whole update back.
845 }
846 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700847 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 // We need to redraw the rectangle that will be updated
849 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700850 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700851 // rectangle instead of a region (see DisplayHardware::flip())
852 mDirtyRegion.set(mInvalidRegion.bounds());
853 } else {
854 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 mDirtyRegion.set(hw.bounds());
856 mInvalidRegion = mDirtyRegion;
857 }
858 }
859
860 // compose all surfaces
861 composeSurfaces(mDirtyRegion);
862
863 // clear the dirty regions
864 mDirtyRegion.clear();
865}
866
867void SurfaceFlinger::composeSurfaces(const Region& dirty)
868{
869 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
870 // should never happen unless the window manager has a bug
871 // draw something...
872 drawWormhole();
873 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700874
875 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700876 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700877 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700878
879 const DisplayHardware& hw(graphicPlane(0).displayHardware());
880 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700881 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700882
Mathias Agopian45721772010-08-12 15:03:26 -0700883 LOGE_IF(cur && hwc.getNumLayers() != count,
884 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
885 hwc.getNumLayers(), count);
886
887 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700888 if (hwc.initCheck() == NO_ERROR) {
889 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
890 }
Mathias Agopian45721772010-08-12 15:03:26 -0700891
892 /*
893 * update the per-frame h/w composer data for each layer
894 * and build the transparent region of the FB
895 */
896 Region transparent;
897 if (cur) {
898 for (size_t i=0 ; i<count ; i++) {
899 const sp<LayerBase>& layer(layers[i]);
900 layer->setPerFrameData(&cur[i]);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700901 }
902 err = hwc.prepare();
903 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700904
Mathias Agopianf20a3242011-01-19 15:24:23 -0800905 if (err == NO_ERROR) {
906 for (size_t i=0 ; i<count ; i++) {
907 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
908 const sp<LayerBase>& layer(layers[i]);
909 if (!(layer->needsBlending())) {
910 transparent.orSelf(layer->visibleRegionScreen);
911 }
912 }
913 }
914
915 /*
916 * clear the area of the FB that need to be transparent
917 */
918 transparent.andSelf(dirty);
919 if (!transparent.isEmpty()) {
920 glClearColor(0,0,0,0);
921 Region::const_iterator it = transparent.begin();
922 Region::const_iterator const end = transparent.end();
923 const int32_t height = hw.getHeight();
924 while (it != end) {
925 const Rect& r(*it++);
926 const GLint sy = height - (r.top + r.height());
927 glScissor(r.left, sy, r.width(), r.height());
928 glClear(GL_COLOR_BUFFER_BIT);
929 }
930 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700931 }
932 }
Mathias Agopian45721772010-08-12 15:03:26 -0700933
934
935 /*
936 * and then, render the layers targeted at the framebuffer
937 */
938 for (size_t i=0 ; i<count ; i++) {
939 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700940 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
941 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700942 // skip layers handled by the HAL
943 continue;
944 }
945 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700946
Mathias Agopian45721772010-08-12 15:03:26 -0700947 const sp<LayerBase>& layer(layers[i]);
948 const Region clip(dirty.intersect(layer->visibleRegionScreen));
949 if (!clip.isEmpty()) {
950 layer->draw(clip);
951 }
952 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953}
954
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955void SurfaceFlinger::debugFlashRegions()
956{
Mathias Agopian0a917752010-06-14 21:20:00 -0700957 const DisplayHardware& hw(graphicPlane(0).displayHardware());
958 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700959
Mathias Agopian0a917752010-06-14 21:20:00 -0700960 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
961 (flags & DisplayHardware::BUFFER_PRESERVED))) {
962 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
963 mDirtyRegion.bounds() : hw.bounds());
964 composeSurfaces(repaint);
965 }
966
967 TextureManager::deactivateTextures();
968
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 glDisable(GL_BLEND);
970 glDisable(GL_DITHER);
971 glDisable(GL_SCISSOR_TEST);
972
Mathias Agopian0926f502009-05-04 14:17:04 -0700973 static int toggle = 0;
974 toggle = 1 - toggle;
975 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700976 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700977 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700978 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700979 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980
Mathias Agopian20f68782009-05-11 00:03:41 -0700981 Region::const_iterator it = mDirtyRegion.begin();
982 Region::const_iterator const end = mDirtyRegion.end();
983 while (it != end) {
984 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 GLfloat vertices[][2] = {
986 { r.left, r.top },
987 { r.left, r.bottom },
988 { r.right, r.bottom },
989 { r.right, r.top }
990 };
991 glVertexPointer(2, GL_FLOAT, 0, vertices);
992 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
993 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700994
Mathias Agopianb8a55602009-06-26 19:06:36 -0700995 if (mInvalidRegion.isEmpty()) {
996 mDirtyRegion.dump("mDirtyRegion");
997 mInvalidRegion.dump("mInvalidRegion");
998 }
999 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000
1001 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001002 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003
1004 glEnable(GL_SCISSOR_TEST);
1005 //mDirtyRegion.dump("mDirtyRegion");
1006}
1007
1008void SurfaceFlinger::drawWormhole() const
1009{
1010 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1011 if (region.isEmpty())
1012 return;
1013
1014 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1015 const int32_t width = hw.getWidth();
1016 const int32_t height = hw.getHeight();
1017
1018 glDisable(GL_BLEND);
1019 glDisable(GL_DITHER);
1020
1021 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001022 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001023 Region::const_iterator it = region.begin();
1024 Region::const_iterator const end = region.end();
1025 while (it != end) {
1026 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 const GLint sy = height - (r.top + r.height());
1028 glScissor(r.left, sy, r.width(), r.height());
1029 glClear(GL_COLOR_BUFFER_BIT);
1030 }
1031 } else {
1032 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1033 { width, height }, { 0, height } };
1034 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1035 glVertexPointer(2, GL_SHORT, 0, vertices);
1036 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1037 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001038#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001039 if (GLExtensions::getInstance().haveTextureExternal()) {
1040 glDisable(GL_TEXTURE_EXTERNAL_OES);
1041 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001042#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 glEnable(GL_TEXTURE_2D);
1044 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1045 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1046 glMatrixMode(GL_TEXTURE);
1047 glLoadIdentity();
1048 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001049 Region::const_iterator it = region.begin();
1050 Region::const_iterator const end = region.end();
1051 while (it != end) {
1052 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 const GLint sy = height - (r.top + r.height());
1054 glScissor(r.left, sy, r.width(), r.height());
1055 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1056 }
1057 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001058 glLoadIdentity();
1059 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 }
1061}
1062
1063void SurfaceFlinger::debugShowFPS() const
1064{
1065 static int mFrameCount;
1066 static int mLastFrameCount = 0;
1067 static nsecs_t mLastFpsTime = 0;
1068 static float mFps = 0;
1069 mFrameCount++;
1070 nsecs_t now = systemTime();
1071 nsecs_t diff = now - mLastFpsTime;
1072 if (diff > ms2ns(250)) {
1073 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1074 mLastFpsTime = now;
1075 mLastFrameCount = mFrameCount;
1076 }
1077 // XXX: mFPS has the value we want
1078 }
1079
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001080status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081{
1082 Mutex::Autolock _l(mStateLock);
1083 addLayer_l(layer);
1084 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1085 return NO_ERROR;
1086}
1087
Mathias Agopian96f08192010-06-02 23:28:45 -07001088status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1089{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001090 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001091 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1092}
1093
1094ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1095 const sp<LayerBaseClient>& lbc)
1096{
1097 Mutex::Autolock _l(mStateLock);
1098
1099 // attach this layer to the client
1100 ssize_t name = client->attachLayer(lbc);
1101
1102 // add this layer to the current state list
1103 addLayer_l(lbc);
1104
1105 return name;
1106}
1107
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001108status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109{
1110 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001111 status_t err = purgatorizeLayer_l(layer);
1112 if (err == NO_ERROR)
1113 setTransactionFlags(eTransactionNeeded);
1114 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115}
1116
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001117status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001119 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1120 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001121 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001122 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1124 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001125 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 return NO_ERROR;
1127 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001128 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129}
1130
Mathias Agopian9a112062009-04-17 19:36:26 -07001131status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1132{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001133 // First add the layer to the purgatory list, which makes sure it won't
1134 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001135 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001136 if (err >= 0) {
1137 mLayerPurgatory.add(layerBase);
1138 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001139
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001140 layerBase->onRemoved();
1141
Mathias Agopian3d579642009-06-04 18:46:21 -07001142 // it's possible that we don't find a layer, because it might
1143 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001144 // from the user because there is a race between Client::destroySurface(),
1145 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001146 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1147}
1148
Mathias Agopian96f08192010-06-02 23:28:45 -07001149status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150{
Mathias Agopian96f08192010-06-02 23:28:45 -07001151 layer->forceVisibilityTransaction();
1152 setTransactionFlags(eTraversalNeeded);
1153 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154}
1155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1157{
1158 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1159}
1160
Mathias Agopianbb641242010-05-18 17:06:55 -07001161uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162{
1163 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1164 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001165 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 }
1167 return old;
1168}
1169
1170void SurfaceFlinger::openGlobalTransaction()
1171{
1172 android_atomic_inc(&mTransactionCount);
1173}
1174
1175void SurfaceFlinger::closeGlobalTransaction()
1176{
1177 if (android_atomic_dec(&mTransactionCount) == 1) {
1178 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001179
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001180 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001181 // take effect before returning.
1182 Mutex::Autolock _l(mStateLock);
1183 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001184 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1185 if (CC_UNLIKELY(err != NO_ERROR)) {
1186 // just in case something goes wrong in SF, return to the
1187 // called after a few seconds.
1188 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1189 mResizeTransationPending = false;
1190 break;
1191 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001192 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 }
1194}
1195
1196status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1197{
1198 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1199 return BAD_VALUE;
1200
1201 Mutex::Autolock _l(mStateLock);
1202 mCurrentState.freezeDisplay = 1;
1203 setTransactionFlags(eTransactionNeeded);
1204
1205 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001206 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 return NO_ERROR;
1208}
1209
1210status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1211{
1212 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1213 return BAD_VALUE;
1214
1215 Mutex::Autolock _l(mStateLock);
1216 mCurrentState.freezeDisplay = 0;
1217 setTransactionFlags(eTransactionNeeded);
1218
1219 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001220 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 return NO_ERROR;
1222}
1223
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001224int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001225 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226{
1227 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1228 return BAD_VALUE;
1229
1230 Mutex::Autolock _l(mStateLock);
1231 if (mCurrentState.orientation != orientation) {
1232 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001233 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 mCurrentState.orientation = orientation;
1235 setTransactionFlags(eTransactionNeeded);
1236 mTransactionCV.wait(mStateLock);
1237 } else {
1238 orientation = BAD_VALUE;
1239 }
1240 }
1241 return orientation;
1242}
1243
Mathias Agopian96f08192010-06-02 23:28:45 -07001244sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001245 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1247 uint32_t flags)
1248{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001249 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001251
1252 if (int32_t(w|h) < 0) {
1253 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1254 int(w), int(h));
1255 return surfaceHandle;
1256 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001259 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 switch (flags & eFXSurfaceMask) {
1261 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001262 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1263 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 break;
1265 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001266 // for now we treat Blur as Dim, until we can implement it
1267 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001269 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 break;
1271 }
1272
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001273 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001274 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001275 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001276 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001279 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001280 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001281 params->identity = surfaceHandle->getIdentity();
1282 params->width = w;
1283 params->height = h;
1284 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001285 if (normalLayer != 0) {
1286 Mutex::Autolock _l(mStateLock);
1287 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1288 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001289 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001290
Mathias Agopian96f08192010-06-02 23:28:45 -07001291 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 }
1293
1294 return surfaceHandle;
1295}
1296
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001297sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001298 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001300 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301{
1302 // initialize the surfaces
1303 switch (format) { // TODO: take h/w into account
1304 case PIXEL_FORMAT_TRANSPARENT:
1305 case PIXEL_FORMAT_TRANSLUCENT:
1306 format = PIXEL_FORMAT_RGBA_8888;
1307 break;
1308 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001309#ifdef NO_RGBX_8888
1310 format = PIXEL_FORMAT_RGB_565;
1311#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001312 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001313#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 break;
1315 }
1316
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001317#ifdef NO_RGBX_8888
1318 if (format == PIXEL_FORMAT_RGBX_8888)
1319 format = PIXEL_FORMAT_RGBA_8888;
1320#endif
1321
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001323 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001324 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001326 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 }
1328 return layer;
1329}
1330
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001331sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001332 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001333 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334{
Mathias Agopian96f08192010-06-02 23:28:45 -07001335 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 return layer;
1338}
1339
Mathias Agopian96f08192010-06-02 23:28:45 -07001340status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001341{
Mathias Agopian9a112062009-04-17 19:36:26 -07001342 /*
1343 * called by the window manager, when a surface should be marked for
1344 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001345 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001346 * The surface is removed from the current and drawing lists, but placed
1347 * in the purgatory queue, so it's not destroyed right-away (we need
1348 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001349 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001350
Mathias Agopian48d819a2009-09-10 19:41:18 -07001351 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001352 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001353 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001354 if (layer != 0) {
1355 err = purgatorizeLayer_l(layer);
1356 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001357 setTransactionFlags(eTransactionNeeded);
1358 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001359 }
1360 return err;
1361}
1362
1363status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1364{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001365 // called by ~ISurface() when all references are gone
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001366
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001367 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001368 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001369 sp<LayerBaseClient> layer;
1370 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001371 MessageDestroySurface(
1372 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1373 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001374 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001375 sp<LayerBaseClient> l(layer);
1376 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001377 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001378 /*
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001379 * remove the layer from the current list -- chances are that it's
1380 * not in the list anyway, because it should have been removed
1381 * already upon request of the client (eg: window manager).
Mathias Agopian759fdb22009-07-02 17:33:40 -07001382 * However, a buggy client could have not done that.
1383 * Since we know we don't have any more clients, we don't need
1384 * to use the purgatory.
1385 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001386 status_t err = flinger->removeLayer_l(l);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001387 if (err == NAME_NOT_FOUND) {
1388 // The surface wasn't in the current list, which means it was
1389 // removed already, which means it is in the purgatory,
1390 // and need to be removed from there.
1391 // This needs to happen from the main thread since its dtor
1392 // must run from there (b/c of OpenGL ES). Additionally, we
1393 // can't really acquire our internal lock from
1394 // destroySurface() -- see postMessage() below.
1395 ssize_t idx = flinger->mLayerPurgatory.remove(l);
1396 LOGE_IF(idx < 0,
1397 "layer=%p is not in the purgatory list", l.get());
1398 }
1399
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001400 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1401 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001402 return true;
1403 }
1404 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001405
Mathias Agopianbb641242010-05-18 17:06:55 -07001406 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 return NO_ERROR;
1408}
1409
1410status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001411 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 int32_t count,
1413 const layer_state_t* states)
1414{
1415 Mutex::Autolock _l(mStateLock);
1416 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001418 const layer_state_t& s(states[i]);
1419 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001420 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 if (what & ePositionChanged) {
1423 if (layer->setPosition(s.x, s.y))
1424 flags |= eTraversalNeeded;
1425 }
1426 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001427 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001429 mCurrentState.layersSortedByZ.removeAt(idx);
1430 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 // we need traversal (state changed)
1432 // AND transaction (list changed)
1433 flags |= eTransactionNeeded|eTraversalNeeded;
1434 }
1435 }
1436 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001437 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001439 mResizeTransationPending = true;
1440 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 }
1442 if (what & eAlphaChanged) {
1443 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1444 flags |= eTraversalNeeded;
1445 }
1446 if (what & eMatrixChanged) {
1447 if (layer->setMatrix(s.matrix))
1448 flags |= eTraversalNeeded;
1449 }
1450 if (what & eTransparentRegionChanged) {
1451 if (layer->setTransparentRegionHint(s.transparentRegion))
1452 flags |= eTraversalNeeded;
1453 }
1454 if (what & eVisibilityChanged) {
1455 if (layer->setFlags(s.flags, s.mask))
1456 flags |= eTraversalNeeded;
1457 }
1458 }
1459 }
1460 if (flags) {
1461 setTransactionFlags(flags);
1462 }
1463 return NO_ERROR;
1464}
1465
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466void SurfaceFlinger::screenReleased(int dpy)
1467{
1468 // this may be called by a signal handler, we can't do too much in here
1469 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1470 signalEvent();
1471}
1472
1473void SurfaceFlinger::screenAcquired(int dpy)
1474{
1475 // this may be called by a signal handler, we can't do too much in here
1476 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1477 signalEvent();
1478}
1479
1480status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1481{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001482 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 char buffer[SIZE];
1484 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001485 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 snprintf(buffer, SIZE, "Permission Denial: "
1487 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1488 IPCThreadState::self()->getCallingPid(),
1489 IPCThreadState::self()->getCallingUid());
1490 result.append(buffer);
1491 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001492
1493 // figure out if we're stuck somewhere
1494 const nsecs_t now = systemTime();
1495 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1496 const nsecs_t inTransaction(mDebugInTransaction);
1497 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1498 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1499
1500 // Try to get the main lock, but don't insist if we can't
1501 // (this would indicate SF is stuck, but we want to be able to
1502 // print something in dumpsys).
1503 int retry = 3;
1504 while (mStateLock.tryLock()<0 && --retry>=0) {
1505 usleep(1000000);
1506 }
1507 const bool locked(retry >= 0);
1508 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001509 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001510 "SurfaceFlinger appears to be unresponsive, "
1511 "dumping anyways (no locks held)\n");
1512 result.append(buffer);
1513 }
1514
Mathias Agopian48b888a2011-01-19 16:15:53 -08001515 /*
1516 * Dump the visible layer list
1517 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1519 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001520 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1521 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001523 const sp<LayerBase>& layer(currentLayers[i]);
1524 layer->dump(result, buffer, SIZE);
1525 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526 s.transparentRegion.dump(result, "transparentRegion");
1527 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1528 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1529 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001530
Mathias Agopian48b888a2011-01-19 16:15:53 -08001531 /*
1532 * Dump the layers in the purgatory
1533 */
1534
1535 const size_t purgatorySize = mLayerPurgatory.size();
1536 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1537 result.append(buffer);
1538 for (size_t i=0 ; i<purgatorySize ; i++) {
1539 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1540 layer->shortDump(result, buffer, SIZE);
1541 }
1542
1543 /*
1544 * Dump SurfaceFlinger global state
1545 */
1546
1547 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1548 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549 mWormholeRegion.dump(result, "WormholeRegion");
1550 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1551 snprintf(buffer, SIZE,
1552 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1553 mFreezeDisplay?"yes":"no", mFreezeCount,
1554 mCurrentState.orientation, hw.canDraw());
1555 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001556 snprintf(buffer, SIZE,
1557 " last eglSwapBuffers() time: %f us\n"
1558 " last transaction time : %f us\n",
1559 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1560 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001561
Mathias Agopian9795c422009-08-26 16:36:26 -07001562 if (inSwapBuffersDuration || !locked) {
1563 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1564 inSwapBuffersDuration/1000.0);
1565 result.append(buffer);
1566 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001567
Mathias Agopian9795c422009-08-26 16:36:26 -07001568 if (inTransactionDuration || !locked) {
1569 snprintf(buffer, SIZE, " transaction time: %f us\n",
1570 inTransactionDuration/1000.0);
1571 result.append(buffer);
1572 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001573
Mathias Agopian48b888a2011-01-19 16:15:53 -08001574 /*
1575 * Dump HWComposer state
1576 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001577 HWComposer& hwc(hw.getHwComposer());
1578 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1579 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1580 mDebugDisableHWC ? "disabled" : "enabled");
1581 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001582 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001583
Mathias Agopian48b888a2011-01-19 16:15:53 -08001584 /*
1585 * Dump gralloc state
1586 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001587 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001588 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001589 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001590
1591 if (locked) {
1592 mStateLock.unlock();
1593 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 }
1595 write(fd, result.string(), result.size());
1596 return NO_ERROR;
1597}
1598
1599status_t SurfaceFlinger::onTransact(
1600 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1601{
1602 switch (code) {
1603 case CREATE_CONNECTION:
1604 case OPEN_GLOBAL_TRANSACTION:
1605 case CLOSE_GLOBAL_TRANSACTION:
1606 case SET_ORIENTATION:
1607 case FREEZE_DISPLAY:
1608 case UNFREEZE_DISPLAY:
1609 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001610 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001611 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 {
1613 // codes that require permission check
1614 IPCThreadState* ipc = IPCThreadState::self();
1615 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001616 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001617 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1618 LOGE("Permission Denial: "
1619 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1620 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001622 break;
1623 }
1624 case CAPTURE_SCREEN:
1625 {
1626 // codes that require permission check
1627 IPCThreadState* ipc = IPCThreadState::self();
1628 const int pid = ipc->getCallingPid();
1629 const int uid = ipc->getCallingUid();
1630 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1631 LOGE("Permission Denial: "
1632 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1633 return PERMISSION_DENIED;
1634 }
1635 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 }
1637 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1640 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001641 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001642 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1643 IPCThreadState* ipc = IPCThreadState::self();
1644 const int pid = ipc->getCallingPid();
1645 const int uid = ipc->getCallingUid();
1646 LOGE("Permission Denial: "
1647 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648 return PERMISSION_DENIED;
1649 }
1650 int n;
1651 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001652 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001653 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 return NO_ERROR;
1655 case 1002: // SHOW_UPDATES
1656 n = data.readInt32();
1657 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1658 return NO_ERROR;
1659 case 1003: // SHOW_BACKGROUND
1660 n = data.readInt32();
1661 mDebugBackground = n ? 1 : 0;
1662 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001663 case 1008: // toggle use of hw composer
1664 n = data.readInt32();
1665 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianad456f92011-01-13 17:53:01 -08001666 invalidateHwcGeometry();
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001667 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668 case 1004:{ // repaint everything
1669 Mutex::Autolock _l(mStateLock);
1670 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1671 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1672 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001673 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001675 case 1005:{ // force transaction
1676 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1677 return NO_ERROR;
1678 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001679 case 1006:{ // enable/disable GraphicLog
1680 int enabled = data.readInt32();
1681 GraphicLog::getInstance().setEnabled(enabled);
1682 return NO_ERROR;
1683 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684 case 1007: // set mFreezeCount
1685 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001686 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 return NO_ERROR;
1688 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001689 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 reply->writeInt32(0);
1691 reply->writeInt32(mDebugRegion);
1692 reply->writeInt32(mDebugBackground);
1693 return NO_ERROR;
1694 case 1013: {
1695 Mutex::Autolock _l(mStateLock);
1696 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1697 reply->writeInt32(hw.getPageFlipCount());
1698 }
1699 return NO_ERROR;
1700 }
1701 }
1702 return err;
1703}
1704
Mathias Agopian59119e62010-10-11 12:37:43 -07001705// ---------------------------------------------------------------------------
1706
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001707status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1708 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001709{
Mathias Agopian59119e62010-10-11 12:37:43 -07001710 if (!GLExtensions::getInstance().haveFramebufferObject())
1711 return INVALID_OPERATION;
1712
1713 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001714 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001715 const uint32_t hw_w = hw.getWidth();
1716 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001717 GLfloat u = 1;
1718 GLfloat v = 1;
1719
1720 // make sure to clear all GL error flags
1721 while ( glGetError() != GL_NO_ERROR ) ;
1722
1723 // create a FBO
1724 GLuint name, tname;
1725 glGenTextures(1, &tname);
1726 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001727 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1728 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001729 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001730 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001731 GLint tw = (2 << (31 - clz(hw_w)));
1732 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001733 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1734 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001735 u = GLfloat(hw_w) / tw;
1736 v = GLfloat(hw_h) / th;
1737 }
1738 glGenFramebuffersOES(1, &name);
1739 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001740 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1741 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001742
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001743 // redraw the screen entirely...
1744 glClearColor(0,0,0,1);
1745 glClear(GL_COLOR_BUFFER_BIT);
1746 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1747 const size_t count = layers.size();
1748 for (size_t i=0 ; i<count ; ++i) {
1749 const sp<LayerBase>& layer(layers[i]);
1750 layer->drawForSreenShot();
1751 }
1752
1753 // back to main framebuffer
1754 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1755 glDisable(GL_SCISSOR_TEST);
1756 glDeleteFramebuffersOES(1, &name);
1757
1758 *textureName = tname;
1759 *uOut = u;
1760 *vOut = v;
1761 return NO_ERROR;
1762}
1763
1764// ---------------------------------------------------------------------------
1765
1766status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1767{
1768 status_t result = PERMISSION_DENIED;
1769
1770 if (!GLExtensions::getInstance().haveFramebufferObject())
1771 return INVALID_OPERATION;
1772
1773 // get screen geometry
1774 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1775 const uint32_t hw_w = hw.getWidth();
1776 const uint32_t hw_h = hw.getHeight();
1777 const Region screenBounds(hw.bounds());
1778
1779 GLfloat u, v;
1780 GLuint tname;
1781 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1782 if (result != NO_ERROR) {
1783 return result;
1784 }
1785
1786 GLfloat vtx[8];
1787 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1788 glEnable(GL_TEXTURE_2D);
1789 glBindTexture(GL_TEXTURE_2D, tname);
1790 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1791 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1792 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1793 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1794 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1795 glVertexPointer(2, GL_FLOAT, 0, vtx);
1796
1797 class s_curve_interpolator {
1798 const float nbFrames, s, v;
1799 public:
1800 s_curve_interpolator(int nbFrames, float s)
1801 : nbFrames(1.0f / (nbFrames-1)), s(s),
1802 v(1.0f + expf(-s + 0.5f*s)) {
1803 }
1804 float operator()(int f) {
1805 const float x = f * nbFrames;
1806 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1807 }
1808 };
1809
1810 class v_stretch {
1811 const GLfloat hw_w, hw_h;
1812 public:
1813 v_stretch(uint32_t hw_w, uint32_t hw_h)
1814 : hw_w(hw_w), hw_h(hw_h) {
1815 }
1816 void operator()(GLfloat* vtx, float v) {
1817 const GLfloat w = hw_w + (hw_w * v);
1818 const GLfloat h = hw_h - (hw_h * v);
1819 const GLfloat x = (hw_w - w) * 0.5f;
1820 const GLfloat y = (hw_h - h) * 0.5f;
1821 vtx[0] = x; vtx[1] = y;
1822 vtx[2] = x; vtx[3] = y + h;
1823 vtx[4] = x + w; vtx[5] = y + h;
1824 vtx[6] = x + w; vtx[7] = y;
1825 }
1826 };
1827
1828 class h_stretch {
1829 const GLfloat hw_w, hw_h;
1830 public:
1831 h_stretch(uint32_t hw_w, uint32_t hw_h)
1832 : hw_w(hw_w), hw_h(hw_h) {
1833 }
1834 void operator()(GLfloat* vtx, float v) {
1835 const GLfloat w = hw_w - (hw_w * v);
1836 const GLfloat h = 1.0f;
1837 const GLfloat x = (hw_w - w) * 0.5f;
1838 const GLfloat y = (hw_h - h) * 0.5f;
1839 vtx[0] = x; vtx[1] = y;
1840 vtx[2] = x; vtx[3] = y + h;
1841 vtx[4] = x + w; vtx[5] = y + h;
1842 vtx[6] = x + w; vtx[7] = y;
1843 }
1844 };
1845
1846 // the full animation is 24 frames
1847 const int nbFrames = 12;
1848 s_curve_interpolator itr(nbFrames, 7.5f);
1849 s_curve_interpolator itg(nbFrames, 8.0f);
1850 s_curve_interpolator itb(nbFrames, 8.5f);
1851
1852 v_stretch vverts(hw_w, hw_h);
1853 glEnable(GL_BLEND);
1854 glBlendFunc(GL_ONE, GL_ONE);
1855 for (int i=0 ; i<nbFrames ; i++) {
1856 float x, y, w, h;
1857 const float vr = itr(i);
1858 const float vg = itg(i);
1859 const float vb = itb(i);
1860
1861 // clear screen
1862 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001863 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001864 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001865
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001866 // draw the red plane
1867 vverts(vtx, vr);
1868 glColorMask(1,0,0,1);
1869 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001870
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001871 // draw the green plane
1872 vverts(vtx, vg);
1873 glColorMask(0,1,0,1);
1874 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001875
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001876 // draw the blue plane
1877 vverts(vtx, vb);
1878 glColorMask(0,0,1,1);
1879 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001880
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001881 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001882 glDisable(GL_TEXTURE_2D);
1883 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001884 glColor4f(vg, vg, vg, 1);
1885 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1886 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001887 }
1888
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001889 h_stretch hverts(hw_w, hw_h);
1890 glDisable(GL_BLEND);
1891 glDisable(GL_TEXTURE_2D);
1892 glColorMask(1,1,1,1);
1893 for (int i=0 ; i<nbFrames ; i++) {
1894 const float v = itg(i);
1895 hverts(vtx, v);
1896 glClear(GL_COLOR_BUFFER_BIT);
1897 glColor4f(1-v, 1-v, 1-v, 1);
1898 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1899 hw.flip(screenBounds);
1900 }
1901
1902 glColorMask(1,1,1,1);
1903 glEnable(GL_SCISSOR_TEST);
1904 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1905 glDeleteTextures(1, &tname);
1906 return NO_ERROR;
1907}
1908
1909status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1910{
1911 status_t result = PERMISSION_DENIED;
1912
1913 if (!GLExtensions::getInstance().haveFramebufferObject())
1914 return INVALID_OPERATION;
1915
1916
1917 // get screen geometry
1918 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1919 const uint32_t hw_w = hw.getWidth();
1920 const uint32_t hw_h = hw.getHeight();
1921 const Region screenBounds(hw.bounds());
1922
1923 GLfloat u, v;
1924 GLuint tname;
1925 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1926 if (result != NO_ERROR) {
1927 return result;
1928 }
1929
1930 // back to main framebuffer
1931 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1932 glDisable(GL_SCISSOR_TEST);
1933
1934 GLfloat vtx[8];
1935 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1936 glEnable(GL_TEXTURE_2D);
1937 glBindTexture(GL_TEXTURE_2D, tname);
1938 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1939 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1940 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1941 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1942 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1943 glVertexPointer(2, GL_FLOAT, 0, vtx);
1944
1945 class s_curve_interpolator {
1946 const float nbFrames, s, v;
1947 public:
1948 s_curve_interpolator(int nbFrames, float s)
1949 : nbFrames(1.0f / (nbFrames-1)), s(s),
1950 v(1.0f + expf(-s + 0.5f*s)) {
1951 }
1952 float operator()(int f) {
1953 const float x = f * nbFrames;
1954 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1955 }
1956 };
1957
1958 class v_stretch {
1959 const GLfloat hw_w, hw_h;
1960 public:
1961 v_stretch(uint32_t hw_w, uint32_t hw_h)
1962 : hw_w(hw_w), hw_h(hw_h) {
1963 }
1964 void operator()(GLfloat* vtx, float v) {
1965 const GLfloat w = hw_w + (hw_w * v);
1966 const GLfloat h = hw_h - (hw_h * v);
1967 const GLfloat x = (hw_w - w) * 0.5f;
1968 const GLfloat y = (hw_h - h) * 0.5f;
1969 vtx[0] = x; vtx[1] = y;
1970 vtx[2] = x; vtx[3] = y + h;
1971 vtx[4] = x + w; vtx[5] = y + h;
1972 vtx[6] = x + w; vtx[7] = y;
1973 }
1974 };
1975
1976 class h_stretch {
1977 const GLfloat hw_w, hw_h;
1978 public:
1979 h_stretch(uint32_t hw_w, uint32_t hw_h)
1980 : hw_w(hw_w), hw_h(hw_h) {
1981 }
1982 void operator()(GLfloat* vtx, float v) {
1983 const GLfloat w = hw_w - (hw_w * v);
1984 const GLfloat h = 1.0f;
1985 const GLfloat x = (hw_w - w) * 0.5f;
1986 const GLfloat y = (hw_h - h) * 0.5f;
1987 vtx[0] = x; vtx[1] = y;
1988 vtx[2] = x; vtx[3] = y + h;
1989 vtx[4] = x + w; vtx[5] = y + h;
1990 vtx[6] = x + w; vtx[7] = y;
1991 }
1992 };
1993
Mathias Agopiana6546e52010-10-14 12:33:07 -07001994 // the full animation is 12 frames
1995 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001996 s_curve_interpolator itr(nbFrames, 7.5f);
1997 s_curve_interpolator itg(nbFrames, 8.0f);
1998 s_curve_interpolator itb(nbFrames, 8.5f);
1999
2000 h_stretch hverts(hw_w, hw_h);
2001 glDisable(GL_BLEND);
2002 glDisable(GL_TEXTURE_2D);
2003 glColorMask(1,1,1,1);
2004 for (int i=nbFrames-1 ; i>=0 ; i--) {
2005 const float v = itg(i);
2006 hverts(vtx, v);
2007 glClear(GL_COLOR_BUFFER_BIT);
2008 glColor4f(1-v, 1-v, 1-v, 1);
2009 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2010 hw.flip(screenBounds);
2011 }
2012
Mathias Agopiana6546e52010-10-14 12:33:07 -07002013 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002014 v_stretch vverts(hw_w, hw_h);
2015 glEnable(GL_BLEND);
2016 glBlendFunc(GL_ONE, GL_ONE);
2017 for (int i=nbFrames-1 ; i>=0 ; i--) {
2018 float x, y, w, h;
2019 const float vr = itr(i);
2020 const float vg = itg(i);
2021 const float vb = itb(i);
2022
2023 // clear screen
2024 glColorMask(1,1,1,1);
2025 glClear(GL_COLOR_BUFFER_BIT);
2026 glEnable(GL_TEXTURE_2D);
2027
2028 // draw the red plane
2029 vverts(vtx, vr);
2030 glColorMask(1,0,0,1);
2031 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2032
2033 // draw the green plane
2034 vverts(vtx, vg);
2035 glColorMask(0,1,0,1);
2036 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2037
2038 // draw the blue plane
2039 vverts(vtx, vb);
2040 glColorMask(0,0,1,1);
2041 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2042
2043 hw.flip(screenBounds);
2044 }
2045
2046 glColorMask(1,1,1,1);
2047 glEnable(GL_SCISSOR_TEST);
2048 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002049 glDeleteTextures(1, &tname);
2050
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 return NO_ERROR;
2052}
2053
2054// ---------------------------------------------------------------------------
2055
Mathias Agopianabd671a2010-10-14 14:54:06 -07002056status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057{
2058 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2059 if (!hw.canDraw()) {
2060 // we're already off
2061 return NO_ERROR;
2062 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002063 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2064 electronBeamOffAnimationImplLocked();
2065 }
2066
2067 // always clear the whole screen at the end of the animation
2068 glClearColor(0,0,0,1);
2069 glDisable(GL_SCISSOR_TEST);
2070 glClear(GL_COLOR_BUFFER_BIT);
2071 glEnable(GL_SCISSOR_TEST);
2072 hw.flip( Region(hw.bounds()) );
2073
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002074 hw.setCanDraw(false);
2075 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002076}
2077
2078status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2079{
Mathias Agopian59119e62010-10-11 12:37:43 -07002080 class MessageTurnElectronBeamOff : public MessageBase {
2081 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002082 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002083 status_t result;
2084 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002085 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2086 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002087 }
2088 status_t getResult() const {
2089 return result;
2090 }
2091 virtual bool handler() {
2092 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002093 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002094 return true;
2095 }
2096 };
2097
Mathias Agopianabd671a2010-10-14 14:54:06 -07002098 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002099 status_t res = postMessageSync(msg);
2100 if (res == NO_ERROR) {
2101 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002102
2103 // work-around: when the power-manager calls us we activate the
2104 // animation. eventually, the "on" animation will be called
2105 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002106 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002107 }
2108 return res;
2109}
2110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002112
Mathias Agopianabd671a2010-10-14 14:54:06 -07002113status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002114{
2115 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2116 if (hw.canDraw()) {
2117 // we're already on
2118 return NO_ERROR;
2119 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002120 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2121 electronBeamOnAnimationImplLocked();
2122 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002123 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002124
2125 // make sure to redraw the whole screen when the animation is done
2126 mDirtyRegion.set(hw.bounds());
2127 signalEvent();
2128
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002129 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002130}
2131
2132status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2133{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002134 class MessageTurnElectronBeamOn : public MessageBase {
2135 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002136 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002137 status_t result;
2138 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002139 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2140 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002141 }
2142 status_t getResult() const {
2143 return result;
2144 }
2145 virtual bool handler() {
2146 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002147 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002148 return true;
2149 }
2150 };
2151
Mathias Agopianabd671a2010-10-14 14:54:06 -07002152 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002153 return NO_ERROR;
2154}
2155
2156// ---------------------------------------------------------------------------
2157
Mathias Agopian74c40c02010-09-29 13:02:36 -07002158status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2159 sp<IMemoryHeap>* heap,
2160 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002161 uint32_t sw, uint32_t sh,
2162 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002163{
2164 status_t result = PERMISSION_DENIED;
2165
2166 // only one display supported for now
2167 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2168 return BAD_VALUE;
2169
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08002170 // make sure none of the layers are protected
2171 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2172 const size_t count = layers.size();
2173 for (size_t i=0 ; i<count ; ++i) {
2174 const sp<LayerBase>& layer(layers[i]);
2175 const uint32_t z = layer->drawingState().z;
2176 if (z >= minLayerZ && z <= maxLayerZ) {
2177 if (layer->isProtected()) {
2178 return INVALID_OPERATION;
2179 }
2180 }
2181 }
2182
Mathias Agopian74c40c02010-09-29 13:02:36 -07002183 if (!GLExtensions::getInstance().haveFramebufferObject())
2184 return INVALID_OPERATION;
2185
2186 // get screen geometry
2187 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2188 const uint32_t hw_w = hw.getWidth();
2189 const uint32_t hw_h = hw.getHeight();
2190
2191 if ((sw > hw_w) || (sh > hw_h))
2192 return BAD_VALUE;
2193
2194 sw = (!sw) ? hw_w : sw;
2195 sh = (!sh) ? hw_h : sh;
2196 const size_t size = sw * sh * 4;
2197
Mathias Agopian1c71a472011-03-02 18:45:50 -08002198 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2199 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002200
Mathias Agopian74c40c02010-09-29 13:02:36 -07002201 // make sure to clear all GL error flags
2202 while ( glGetError() != GL_NO_ERROR ) ;
2203
2204 // create a FBO
2205 GLuint name, tname;
2206 glGenRenderbuffersOES(1, &tname);
2207 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2208 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2209 glGenFramebuffersOES(1, &name);
2210 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2211 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2212 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2213
2214 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002215
Mathias Agopian74c40c02010-09-29 13:02:36 -07002216 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2217
2218 // invert everything, b/c glReadPixel() below will invert the FB
2219 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002220 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002221 glMatrixMode(GL_PROJECTION);
2222 glPushMatrix();
2223 glLoadIdentity();
2224 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2225 glMatrixMode(GL_MODELVIEW);
2226
2227 // redraw the screen entirely...
2228 glClearColor(0,0,0,1);
2229 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002230
Mathias Agopian74c40c02010-09-29 13:02:36 -07002231 for (size_t i=0 ; i<count ; ++i) {
2232 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002233 const uint32_t z = layer->drawingState().z;
2234 if (z >= minLayerZ && z <= maxLayerZ) {
2235 layer->drawForSreenShot();
2236 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002237 }
2238
2239 // XXX: this is needed on tegra
2240 glScissor(0, 0, sw, sh);
2241
2242 // check for errors and return screen capture
2243 if (glGetError() != GL_NO_ERROR) {
2244 // error while rendering
2245 result = INVALID_OPERATION;
2246 } else {
2247 // allocate shared memory large enough to hold the
2248 // screen capture
2249 sp<MemoryHeapBase> base(
2250 new MemoryHeapBase(size, 0, "screen-capture") );
2251 void* const ptr = base->getBase();
2252 if (ptr) {
2253 // capture the screen with glReadPixels()
2254 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2255 if (glGetError() == GL_NO_ERROR) {
2256 *heap = base;
2257 *w = sw;
2258 *h = sh;
2259 *f = PIXEL_FORMAT_RGBA_8888;
2260 result = NO_ERROR;
2261 }
2262 } else {
2263 result = NO_MEMORY;
2264 }
2265 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002266 glEnable(GL_SCISSOR_TEST);
2267 glViewport(0, 0, hw_w, hw_h);
2268 glMatrixMode(GL_PROJECTION);
2269 glPopMatrix();
2270 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002271 } else {
2272 result = BAD_VALUE;
2273 }
2274
2275 // release FBO resources
2276 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2277 glDeleteRenderbuffersOES(1, &tname);
2278 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002279
2280 hw.compositionComplete();
2281
Mathias Agopian1c71a472011-03-02 18:45:50 -08002282 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002283
Mathias Agopian74c40c02010-09-29 13:02:36 -07002284 return result;
2285}
2286
2287
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002288status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2289 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002290 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002291 uint32_t sw, uint32_t sh,
2292 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002293{
2294 // only one display supported for now
2295 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2296 return BAD_VALUE;
2297
2298 if (!GLExtensions::getInstance().haveFramebufferObject())
2299 return INVALID_OPERATION;
2300
2301 class MessageCaptureScreen : public MessageBase {
2302 SurfaceFlinger* flinger;
2303 DisplayID dpy;
2304 sp<IMemoryHeap>* heap;
2305 uint32_t* w;
2306 uint32_t* h;
2307 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002308 uint32_t sw;
2309 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002310 uint32_t minLayerZ;
2311 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002312 status_t result;
2313 public:
2314 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002315 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002316 uint32_t sw, uint32_t sh,
2317 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002318 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002319 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2320 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2321 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002322 {
2323 }
2324 status_t getResult() const {
2325 return result;
2326 }
2327 virtual bool handler() {
2328 Mutex::Autolock _l(flinger->mStateLock);
2329
2330 // if we have secure windows, never allow the screen capture
2331 if (flinger->mSecureFrameBuffer)
2332 return true;
2333
Mathias Agopian74c40c02010-09-29 13:02:36 -07002334 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002335 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002336
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002337 return true;
2338 }
2339 };
2340
2341 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002342 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002343 status_t res = postMessageSync(msg);
2344 if (res == NO_ERROR) {
2345 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2346 }
2347 return res;
2348}
2349
2350// ---------------------------------------------------------------------------
2351
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002352sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2353{
2354 sp<Layer> result;
2355 Mutex::Autolock _l(mStateLock);
2356 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2357 return result;
2358}
2359
2360// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002361
Mathias Agopian96f08192010-06-02 23:28:45 -07002362Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002363 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002364{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002365}
2366
Mathias Agopian96f08192010-06-02 23:28:45 -07002367Client::~Client()
2368{
Mathias Agopian96f08192010-06-02 23:28:45 -07002369 const size_t count = mLayers.size();
2370 for (size_t i=0 ; i<count ; i++) {
2371 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2372 if (layer != 0) {
2373 mFlinger->removeLayer(layer);
2374 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002375 }
2376}
2377
Mathias Agopian96f08192010-06-02 23:28:45 -07002378status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002379 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002380}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002381
Mathias Agopian96f08192010-06-02 23:28:45 -07002382ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
2383{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002384 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07002385 mLayers.add(name, layer);
2386 return name;
2387}
2388
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002389void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002390{
Mathias Agopian96f08192010-06-02 23:28:45 -07002391 // we do a linear search here, because this doesn't happen often
2392 const size_t count = mLayers.size();
2393 for (size_t i=0 ; i<count ; i++) {
2394 if (mLayers.valueAt(i) == layer) {
2395 mLayers.removeItemsAt(i, 1);
2396 break;
2397 }
2398 }
2399}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002400sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
2401 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07002402 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
2403 if (layer != 0) {
2404 lbc = layer.promote();
2405 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002406 }
2407 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002408}
2409
Mathias Agopian96f08192010-06-02 23:28:45 -07002410sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002411 return 0;
2412}
2413ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2414 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002415}
Mathias Agopian96f08192010-06-02 23:28:45 -07002416sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002417 ISurfaceComposerClient::surface_data_t* params, int pid,
2418 const String8& name,
2419 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002420 uint32_t flags)
2421{
Mathias Agopian96f08192010-06-02 23:28:45 -07002422 return mFlinger->createSurface(this, pid, name, params,
2423 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002424}
Mathias Agopian96f08192010-06-02 23:28:45 -07002425status_t Client::destroySurface(SurfaceID sid) {
2426 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002427}
Mathias Agopian96f08192010-06-02 23:28:45 -07002428status_t Client::setState(int32_t count, const layer_state_t* states) {
2429 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002430}
2431
2432// ---------------------------------------------------------------------------
2433
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002434UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2435 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2436{
2437 const int pgsize = getpagesize();
2438 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2439
2440 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2441 "SurfaceFlinger Client control-block");
2442
2443 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2444 if (ctrlblk) { // construct the shared structure in-place.
2445 new(ctrlblk) SharedClient;
2446 }
2447}
2448
2449UserClient::~UserClient()
2450{
2451 if (ctrlblk) {
2452 ctrlblk->~SharedClient(); // destroy our shared-structure.
2453 }
2454
2455 /*
2456 * When a UserClient dies, it's unclear what to do exactly.
2457 * We could go ahead and destroy all surfaces linked to that client
2458 * however, it wouldn't be fair to the main Client
2459 * (usually the the window-manager), which might want to re-target
2460 * the layer to another UserClient.
2461 * I think the best is to do nothing, or not much; in most cases the
2462 * WM itself will go ahead and clean things up when it detects a client of
2463 * his has died.
2464 * The remaining question is what to display? currently we keep
2465 * just keep the current buffer.
2466 */
2467}
2468
2469status_t UserClient::initCheck() const {
2470 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2471}
2472
2473void UserClient::detachLayer(const Layer* layer)
2474{
2475 int32_t name = layer->getToken();
2476 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002477 int32_t mask = 1LU<<name;
2478 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2479 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2480 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002481 }
2482}
2483
2484sp<IMemoryHeap> UserClient::getControlBlock() const {
2485 return mCblkHeap;
2486}
2487
2488ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2489{
2490 int32_t name = NAME_NOT_FOUND;
2491 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002492 if (layer == 0) {
2493 return name;
2494 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002495
Mathias Agopian579b3f82010-06-08 19:54:15 -07002496 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002497 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002498 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002499 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002500 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002501
2502 name = 0;
2503 do {
2504 int32_t mask = 1LU<<name;
2505 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2506 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002507 status_t err = layer->setToken(
2508 const_cast<UserClient*>(this), ctrlblk, name);
2509 if (err != NO_ERROR) {
2510 // free the name
2511 android_atomic_and(~mask, &mBitmap);
2512 name = err;
2513 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002514 break;
2515 }
Mathias Agopian1bbafb92011-03-11 16:54:47 -08002516 if (++name >= int32_t(SharedBufferStack::NUM_LAYERS_MAX))
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002517 name = NO_MEMORY;
2518 } while(name >= 0);
2519
Mathias Agopian53503a92010-06-08 15:40:56 -07002520 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2521 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002522 return name;
2523}
2524
2525sp<ISurface> UserClient::createSurface(
2526 ISurfaceComposerClient::surface_data_t* params, int pid,
2527 const String8& name,
2528 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2529 uint32_t flags) {
2530 return 0;
2531}
2532status_t UserClient::destroySurface(SurfaceID sid) {
2533 return INVALID_OPERATION;
2534}
2535status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2536 return INVALID_OPERATION;
2537}
2538
2539// ---------------------------------------------------------------------------
2540
Jamie Gennis9a78c902011-01-12 18:30:40 -08002541GraphicBufferAlloc::GraphicBufferAlloc() {}
2542
2543GraphicBufferAlloc::~GraphicBufferAlloc() {}
2544
2545sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2546 PixelFormat format, uint32_t usage) {
2547 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2548 status_t err = graphicBuffer->initCheck();
2549 if (err != 0) {
2550 LOGE("createGraphicBuffer: init check failed: %d", err);
2551 return 0;
2552 } else if (graphicBuffer->handle == 0) {
2553 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2554 return 0;
2555 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002556 return graphicBuffer;
2557}
2558
Jamie Gennis9a78c902011-01-12 18:30:40 -08002559// ---------------------------------------------------------------------------
2560
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561GraphicPlane::GraphicPlane()
2562 : mHw(0)
2563{
2564}
2565
2566GraphicPlane::~GraphicPlane() {
2567 delete mHw;
2568}
2569
2570bool GraphicPlane::initialized() const {
2571 return mHw ? true : false;
2572}
2573
Mathias Agopian2b92d892010-02-08 15:49:35 -08002574int GraphicPlane::getWidth() const {
2575 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002576}
2577
Mathias Agopian2b92d892010-02-08 15:49:35 -08002578int GraphicPlane::getHeight() const {
2579 return mHeight;
2580}
2581
2582void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2583{
2584 mHw = hw;
2585
2586 // initialize the display orientation transform.
2587 // it's a constant that should come from the display driver.
2588 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2589 char property[PROPERTY_VALUE_MAX];
2590 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2591 //displayOrientation
2592 switch (atoi(property)) {
2593 case 90:
2594 displayOrientation = ISurfaceComposer::eOrientation90;
2595 break;
2596 case 270:
2597 displayOrientation = ISurfaceComposer::eOrientation270;
2598 break;
2599 }
2600 }
2601
2602 const float w = hw->getWidth();
2603 const float h = hw->getHeight();
2604 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2605 &mDisplayTransform);
2606 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2607 mDisplayWidth = h;
2608 mDisplayHeight = w;
2609 } else {
2610 mDisplayWidth = w;
2611 mDisplayHeight = h;
2612 }
2613
2614 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615}
2616
2617status_t GraphicPlane::orientationToTransfrom(
2618 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002619{
2620 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002621 switch (orientation) {
2622 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002623 flags = Transform::ROT_0;
2624 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002625 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002626 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627 break;
2628 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002629 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630 break;
2631 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002632 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633 break;
2634 default:
2635 return BAD_VALUE;
2636 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002637 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002638 return NO_ERROR;
2639}
2640
2641status_t GraphicPlane::setOrientation(int orientation)
2642{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002643 // If the rotation can be handled in hardware, this is where
2644 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002645
2646 const DisplayHardware& hw(displayHardware());
2647 const float w = mDisplayWidth;
2648 const float h = mDisplayHeight;
2649 mWidth = int(w);
2650 mHeight = int(h);
2651
2652 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002653 GraphicPlane::orientationToTransfrom(orientation, w, h,
2654 &orientationTransform);
2655 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2656 mWidth = int(h);
2657 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002658 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002659
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002660 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002661 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002662 return NO_ERROR;
2663}
2664
2665const DisplayHardware& GraphicPlane::displayHardware() const {
2666 return *mHw;
2667}
2668
Mathias Agopian59119e62010-10-11 12:37:43 -07002669DisplayHardware& GraphicPlane::editDisplayHardware() {
2670 return *mHw;
2671}
2672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673const Transform& GraphicPlane::transform() const {
2674 return mGlobalTransform;
2675}
2676
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002677EGLDisplay GraphicPlane::getEGLDisplay() const {
2678 return mHw->getEGLDisplay();
2679}
2680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002681// ---------------------------------------------------------------------------
2682
2683}; // namespace android