blob: 33cb9a41a617513e3a9207816a689855fdcc80eb [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;
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700398 uint32_t transactionFlags = peekTransactionFlags(mask);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 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 Agopianf3d939c2011-06-06 09:55:15 -0700483 Mutex::Autolock _l(mStateLock);
484 const nsecs_t now = systemTime();
485 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700487 // Here we're guaranteed that some transaction flags are set
488 // so we can call handleTransactionLocked() unconditionally.
489 // We call getTransactionFlags(), which will also clear the flags,
490 // with mStateLock held to guarantee that mCurrentState won't change
491 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700492
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700493 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
494 transactionFlags = getTransactionFlags(mask);
495 handleTransactionLocked(transactionFlags);
Mathias Agopian3d579642009-06-04 18:46:21 -0700496
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700497 mLastTransactionTime = systemTime() - now;
498 mDebugInTransaction = 0;
499 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700500}
501
Mathias Agopianf3d939c2011-06-06 09:55:15 -0700502void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700503{
504 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 const size_t count = currentLayers.size();
506
507 /*
508 * Traversal of the children
509 * (perform the transaction for each of them if needed)
510 */
511
512 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
513 if (layersNeedTransaction) {
514 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
517 if (!trFlags) continue;
518
519 const uint32_t flags = layer->doTransaction(0);
520 if (flags & Layer::eVisibleRegion)
521 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 }
523 }
524
525 /*
526 * Perform our own transaction if needed
527 */
528
529 if (transactionFlags & eTransactionNeeded) {
530 if (mCurrentState.orientation != mDrawingState.orientation) {
531 // the orientation has changed, recompute all visible regions
532 // and invalidate everything.
533
534 const int dpy = 0;
535 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700536 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 GraphicPlane& plane(graphicPlane(dpy));
538 plane.setOrientation(orientation);
539
540 // update the shared control block
541 const DisplayHardware& hw(plane.displayHardware());
542 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
543 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800544 dcblk->w = plane.getWidth();
545 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546
547 mVisibleRegionsDirty = true;
548 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 }
550
551 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
552 // freezing or unfreezing the display -> trigger animation if needed
553 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800554 if (mFreezeDisplay)
555 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 }
557
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700558 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
559 // layers have been added
560 mVisibleRegionsDirty = true;
561 }
562
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 // some layers might have been removed, so
564 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700566 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700568 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700569 const size_t count = previousLayers.size();
570 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700571 const sp<LayerBase>& layer(previousLayers[i]);
572 if (currentLayers.indexOf( layer ) < 0) {
573 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700574 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700575 }
576 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 }
579
580 commitTransaction();
581}
582
583sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
584{
585 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
586}
587
588void SurfaceFlinger::computeVisibleRegions(
589 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
590{
591 const GraphicPlane& plane(graphicPlane(0));
592 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700593 const DisplayHardware& hw(plane.displayHardware());
594 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595
596 Region aboveOpaqueLayers;
597 Region aboveCoveredLayers;
598 Region dirty;
599
600 bool secureFrameBuffer = false;
601
602 size_t i = currentLayers.size();
603 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 layer->validateVisibility(planeTransform);
606
607 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700608 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609
Mathias Agopianab028732010-03-16 16:41:46 -0700610 /*
611 * opaqueRegion: area of a surface that is fully opaque.
612 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700614
615 /*
616 * visibleRegion: area of a surface that is visible on screen
617 * and not fully transparent. This is essentially the layer's
618 * footprint minus the opaque regions above it.
619 * Areas covered by a translucent surface are considered visible.
620 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700622
623 /*
624 * coveredRegion: area of a surface that is covered by all
625 * visible regions above it (which includes the translucent areas).
626 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700628
629
630 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700631 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700633 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700635 visibleRegion.andSelf(screenRegion);
636 if (!visibleRegion.isEmpty()) {
637 // Remove the transparent area from the visible region
638 if (translucent) {
639 visibleRegion.subtractSelf(layer->transparentRegionScreen);
640 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641
Mathias Agopianab028732010-03-16 16:41:46 -0700642 // compute the opaque region
643 const int32_t layerOrientation = layer->getOrientation();
644 if (s.alpha==255 && !translucent &&
645 ((layerOrientation & Transform::ROT_INVALID) == false)) {
646 // the opaque region is the layer's footprint
647 opaqueRegion = visibleRegion;
648 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649 }
650 }
651
Mathias Agopianab028732010-03-16 16:41:46 -0700652 // Clip the covered region to the visible region
653 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
654
655 // Update aboveCoveredLayers for next (lower) layer
656 aboveCoveredLayers.orSelf(visibleRegion);
657
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 // subtract the opaque region covered by the layers above us
659 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660
661 // compute this layer's dirty region
662 if (layer->contentDirty) {
663 // we need to invalidate the whole region
664 dirty = visibleRegion;
665 // as well, as the old visible region
666 dirty.orSelf(layer->visibleRegionScreen);
667 layer->contentDirty = false;
668 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700669 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700670 * the exposed region consists of two components:
671 * 1) what's VISIBLE now and was COVERED before
672 * 2) what's EXPOSED now less what was EXPOSED before
673 *
674 * note that (1) is conservative, we start with the whole
675 * visible region but only keep what used to be covered by
676 * something -- which mean it may have been exposed.
677 *
678 * (2) handles areas that were not covered by anything but got
679 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700680 */
Mathias Agopianab028732010-03-16 16:41:46 -0700681 const Region newExposed = visibleRegion - coveredRegion;
682 const Region oldVisibleRegion = layer->visibleRegionScreen;
683 const Region oldCoveredRegion = layer->coveredRegionScreen;
684 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
685 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 }
687 dirty.subtractSelf(aboveOpaqueLayers);
688
689 // accumulate to the screen dirty region
690 dirtyRegion.orSelf(dirty);
691
Mathias Agopianab028732010-03-16 16:41:46 -0700692 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 // Store the visible region is screen space
696 layer->setVisibleRegion(visibleRegion);
697 layer->setCoveredRegion(coveredRegion);
698
Mathias Agopian97011222009-07-28 10:57:27 -0700699 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 if (layer->isSecure() && !visibleRegion.isEmpty()) {
701 secureFrameBuffer = true;
702 }
703 }
704
Mathias Agopian97011222009-07-28 10:57:27 -0700705 // invalidate the areas where a layer was removed
706 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
707 mDirtyRegionRemovedLayer.clear();
708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 mSecureFrameBuffer = secureFrameBuffer;
710 opaqueRegion = aboveOpaqueLayers;
711}
712
713
714void SurfaceFlinger::commitTransaction()
715{
716 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700717 mResizeTransationPending = false;
718 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719}
720
721void SurfaceFlinger::handlePageFlip()
722{
723 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700724 LayerVector& currentLayers(
725 const_cast<LayerVector&>(mDrawingState.layersSortedByZ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 visibleRegions |= lockPageFlip(currentLayers);
727
728 const DisplayHardware& hw = graphicPlane(0).displayHardware();
729 const Region screenRegion(hw.bounds());
730 if (visibleRegions) {
731 Region opaqueRegion;
732 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700733
734 /*
735 * rebuild the visible layer list
736 */
737 mVisibleLayersSortedByZ.clear();
738 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
739 size_t count = currentLayers.size();
740 mVisibleLayersSortedByZ.setCapacity(count);
741 for (size_t i=0 ; i<count ; i++) {
742 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
743 mVisibleLayersSortedByZ.add(currentLayers[i]);
744 }
745
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 mWormholeRegion = screenRegion.subtract(opaqueRegion);
747 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800748 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 }
750
751 unlockPageFlip(currentLayers);
752 mDirtyRegion.andSelf(screenRegion);
753}
754
Mathias Agopianad456f92011-01-13 17:53:01 -0800755void SurfaceFlinger::invalidateHwcGeometry()
756{
757 mHwWorkListDirty = true;
758}
759
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
761{
762 bool recomputeVisibleRegions = false;
763 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700764 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700766 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767 layer->lockPageFlip(recomputeVisibleRegions);
768 }
769 return recomputeVisibleRegions;
770}
771
772void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
773{
774 const GraphicPlane& plane(graphicPlane(0));
775 const Transform& planeTransform(plane.transform());
776 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700777 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700779 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 layer->unlockPageFlip(planeTransform, mDirtyRegion);
781 }
782}
783
Mathias Agopiana350ff92010-08-10 17:14:02 -0700784void SurfaceFlinger::handleWorkList()
785{
786 mHwWorkListDirty = false;
787 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
788 if (hwc.initCheck() == NO_ERROR) {
789 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
790 const size_t count = currentLayers.size();
791 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700792 hwc_layer_t* const cur(hwc.getLayers());
793 for (size_t i=0 ; cur && i<count ; i++) {
794 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700795 if (mDebugDisableHWC) {
796 cur[i].compositionType = HWC_FRAMEBUFFER;
797 cur[i].flags |= HWC_SKIP_LAYER;
798 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700799 }
800 }
801}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803void SurfaceFlinger::handleRepaint()
804{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700805 // compute the invalid region
806 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807
808 if (UNLIKELY(mDebugRegion)) {
809 debugFlashRegions();
810 }
811
Mathias Agopianb8a55602009-06-26 19:06:36 -0700812 // set the frame buffer
813 const DisplayHardware& hw(graphicPlane(0).displayHardware());
814 glMatrixMode(GL_MODELVIEW);
815 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816
817 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700818 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
819 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700820 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700821 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
822 // takes a rectangle, we must make sure to update that whole
823 // rectangle in that case
824 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700825 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700826 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 mDirtyRegion.set(mInvalidRegion.bounds());
828 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700829 // in the BUFFER_PRESERVED case, obviously, we can update only
830 // what's needed and nothing more.
831 // NOTE: this is NOT a common case, as preserving the backbuffer
832 // is costly and usually involves copying the whole update back.
833 }
834 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700835 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 // We need to redraw the rectangle that will be updated
837 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700838 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700839 // rectangle instead of a region (see DisplayHardware::flip())
840 mDirtyRegion.set(mInvalidRegion.bounds());
841 } else {
842 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 mDirtyRegion.set(hw.bounds());
844 mInvalidRegion = mDirtyRegion;
845 }
846 }
847
848 // compose all surfaces
849 composeSurfaces(mDirtyRegion);
850
851 // clear the dirty regions
852 mDirtyRegion.clear();
853}
854
855void SurfaceFlinger::composeSurfaces(const Region& dirty)
856{
857 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
858 // should never happen unless the window manager has a bug
859 // draw something...
860 drawWormhole();
861 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700862
863 status_t err = NO_ERROR;
Mathias Agopian4da75192010-08-10 17:19:56 -0700864 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopian45721772010-08-12 15:03:26 -0700865 size_t count = layers.size();
Mathias Agopiana350ff92010-08-10 17:14:02 -0700866
867 const DisplayHardware& hw(graphicPlane(0).displayHardware());
868 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700869 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700870
Mathias Agopian45721772010-08-12 15:03:26 -0700871 LOGE_IF(cur && hwc.getNumLayers() != count,
872 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
873 hwc.getNumLayers(), count);
874
875 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700876 if (hwc.initCheck() == NO_ERROR) {
877 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
878 }
Mathias Agopian45721772010-08-12 15:03:26 -0700879
880 /*
881 * update the per-frame h/w composer data for each layer
882 * and build the transparent region of the FB
883 */
884 Region transparent;
885 if (cur) {
886 for (size_t i=0 ; i<count ; i++) {
887 const sp<LayerBase>& layer(layers[i]);
888 layer->setPerFrameData(&cur[i]);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700889 }
890 err = hwc.prepare();
891 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700892
Mathias Agopianf20a3242011-01-19 15:24:23 -0800893 if (err == NO_ERROR) {
894 for (size_t i=0 ; i<count ; i++) {
895 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
896 const sp<LayerBase>& layer(layers[i]);
897 if (!(layer->needsBlending())) {
898 transparent.orSelf(layer->visibleRegionScreen);
899 }
900 }
901 }
902
903 /*
904 * clear the area of the FB that need to be transparent
905 */
906 transparent.andSelf(dirty);
907 if (!transparent.isEmpty()) {
908 glClearColor(0,0,0,0);
909 Region::const_iterator it = transparent.begin();
910 Region::const_iterator const end = transparent.end();
911 const int32_t height = hw.getHeight();
912 while (it != end) {
913 const Rect& r(*it++);
914 const GLint sy = height - (r.top + r.height());
915 glScissor(r.left, sy, r.width(), r.height());
916 glClear(GL_COLOR_BUFFER_BIT);
917 }
918 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700919 }
920 }
Mathias Agopian45721772010-08-12 15:03:26 -0700921
922
923 /*
924 * and then, render the layers targeted at the framebuffer
925 */
926 for (size_t i=0 ; i<count ; i++) {
927 if (cur) {
Antti Hatala586a0de2010-09-09 02:32:30 -0700928 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
929 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopian45721772010-08-12 15:03:26 -0700930 // skip layers handled by the HAL
931 continue;
932 }
933 }
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700934
Mathias Agopian45721772010-08-12 15:03:26 -0700935 const sp<LayerBase>& layer(layers[i]);
936 const Region clip(dirty.intersect(layer->visibleRegionScreen));
937 if (!clip.isEmpty()) {
938 layer->draw(clip);
939 }
940 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941}
942
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943void SurfaceFlinger::debugFlashRegions()
944{
Mathias Agopian0a917752010-06-14 21:20:00 -0700945 const DisplayHardware& hw(graphicPlane(0).displayHardware());
946 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700947
Mathias Agopian0a917752010-06-14 21:20:00 -0700948 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
949 (flags & DisplayHardware::BUFFER_PRESERVED))) {
950 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
951 mDirtyRegion.bounds() : hw.bounds());
952 composeSurfaces(repaint);
953 }
954
955 TextureManager::deactivateTextures();
956
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957 glDisable(GL_BLEND);
958 glDisable(GL_DITHER);
959 glDisable(GL_SCISSOR_TEST);
960
Mathias Agopian0926f502009-05-04 14:17:04 -0700961 static int toggle = 0;
962 toggle = 1 - toggle;
963 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700964 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700965 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700966 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700967 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968
Mathias Agopian20f68782009-05-11 00:03:41 -0700969 Region::const_iterator it = mDirtyRegion.begin();
970 Region::const_iterator const end = mDirtyRegion.end();
971 while (it != end) {
972 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 GLfloat vertices[][2] = {
974 { r.left, r.top },
975 { r.left, r.bottom },
976 { r.right, r.bottom },
977 { r.right, r.top }
978 };
979 glVertexPointer(2, GL_FLOAT, 0, vertices);
980 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
981 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700982
Mathias Agopianb8a55602009-06-26 19:06:36 -0700983 if (mInvalidRegion.isEmpty()) {
984 mDirtyRegion.dump("mDirtyRegion");
985 mInvalidRegion.dump("mInvalidRegion");
986 }
987 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988
989 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700990 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991
992 glEnable(GL_SCISSOR_TEST);
993 //mDirtyRegion.dump("mDirtyRegion");
994}
995
996void SurfaceFlinger::drawWormhole() const
997{
998 const Region region(mWormholeRegion.intersect(mDirtyRegion));
999 if (region.isEmpty())
1000 return;
1001
1002 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1003 const int32_t width = hw.getWidth();
1004 const int32_t height = hw.getHeight();
1005
1006 glDisable(GL_BLEND);
1007 glDisable(GL_DITHER);
1008
1009 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001010 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001011 Region::const_iterator it = region.begin();
1012 Region::const_iterator const end = region.end();
1013 while (it != end) {
1014 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 const GLint sy = height - (r.top + r.height());
1016 glScissor(r.left, sy, r.width(), r.height());
1017 glClear(GL_COLOR_BUFFER_BIT);
1018 }
1019 } else {
1020 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1021 { width, height }, { 0, height } };
1022 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1023 glVertexPointer(2, GL_SHORT, 0, vertices);
1024 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1025 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001026#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001027 if (GLExtensions::getInstance().haveTextureExternal()) {
1028 glDisable(GL_TEXTURE_EXTERNAL_OES);
1029 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001030#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 glEnable(GL_TEXTURE_2D);
1032 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1033 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1034 glMatrixMode(GL_TEXTURE);
1035 glLoadIdentity();
1036 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001037 Region::const_iterator it = region.begin();
1038 Region::const_iterator const end = region.end();
1039 while (it != end) {
1040 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 const GLint sy = height - (r.top + r.height());
1042 glScissor(r.left, sy, r.width(), r.height());
1043 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1044 }
1045 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001046 glLoadIdentity();
1047 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 }
1049}
1050
1051void SurfaceFlinger::debugShowFPS() const
1052{
1053 static int mFrameCount;
1054 static int mLastFrameCount = 0;
1055 static nsecs_t mLastFpsTime = 0;
1056 static float mFps = 0;
1057 mFrameCount++;
1058 nsecs_t now = systemTime();
1059 nsecs_t diff = now - mLastFpsTime;
1060 if (diff > ms2ns(250)) {
1061 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1062 mLastFpsTime = now;
1063 mLastFrameCount = mFrameCount;
1064 }
1065 // XXX: mFPS has the value we want
1066 }
1067
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001068status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069{
1070 Mutex::Autolock _l(mStateLock);
1071 addLayer_l(layer);
1072 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1073 return NO_ERROR;
1074}
1075
Mathias Agopian96f08192010-06-02 23:28:45 -07001076status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1077{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001078 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001079 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1080}
1081
1082ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1083 const sp<LayerBaseClient>& lbc)
1084{
Mathias Agopian96f08192010-06-02 23:28:45 -07001085 // attach this layer to the client
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001086 size_t name = client->attachLayer(lbc);
1087
1088 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001089
1090 // add this layer to the current state list
1091 addLayer_l(lbc);
1092
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001093 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001094}
1095
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001096status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097{
1098 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001099 status_t err = purgatorizeLayer_l(layer);
1100 if (err == NO_ERROR)
1101 setTransactionFlags(eTransactionNeeded);
1102 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103}
1104
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001105status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001107 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1108 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001109 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001110 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1112 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001113 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 return NO_ERROR;
1115 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001116 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117}
1118
Mathias Agopian9a112062009-04-17 19:36:26 -07001119status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1120{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001121 // First add the layer to the purgatory list, which makes sure it won't
1122 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001123 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001124 if (err >= 0) {
1125 mLayerPurgatory.add(layerBase);
1126 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001127
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001128 layerBase->onRemoved();
1129
Mathias Agopian3d579642009-06-04 18:46:21 -07001130 // it's possible that we don't find a layer, because it might
1131 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001132 // from the user because there is a race between Client::destroySurface(),
1133 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001134 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1135}
1136
Mathias Agopian96f08192010-06-02 23:28:45 -07001137status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138{
Mathias Agopian96f08192010-06-02 23:28:45 -07001139 layer->forceVisibilityTransaction();
1140 setTransactionFlags(eTraversalNeeded);
1141 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142}
1143
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001144uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1145{
1146 return android_atomic_release_load(&mTransactionFlags);
1147}
1148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1150{
1151 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1152}
1153
Mathias Agopianbb641242010-05-18 17:06:55 -07001154uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155{
1156 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1157 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001158 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 }
1160 return old;
1161}
1162
1163void SurfaceFlinger::openGlobalTransaction()
1164{
1165 android_atomic_inc(&mTransactionCount);
1166}
1167
1168void SurfaceFlinger::closeGlobalTransaction()
1169{
1170 if (android_atomic_dec(&mTransactionCount) == 1) {
1171 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001172
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001173 // if there is a transaction with a resize, wait for it to
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001174 // take effect before returning.
1175 Mutex::Autolock _l(mStateLock);
1176 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001177 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1178 if (CC_UNLIKELY(err != NO_ERROR)) {
1179 // just in case something goes wrong in SF, return to the
1180 // called after a few seconds.
1181 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1182 mResizeTransationPending = false;
1183 break;
1184 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001185 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 }
1187}
1188
1189status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1190{
1191 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1192 return BAD_VALUE;
1193
1194 Mutex::Autolock _l(mStateLock);
1195 mCurrentState.freezeDisplay = 1;
1196 setTransactionFlags(eTransactionNeeded);
1197
1198 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001199 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 return NO_ERROR;
1201}
1202
1203status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1204{
1205 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1206 return BAD_VALUE;
1207
1208 Mutex::Autolock _l(mStateLock);
1209 mCurrentState.freezeDisplay = 0;
1210 setTransactionFlags(eTransactionNeeded);
1211
1212 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001213 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 return NO_ERROR;
1215}
1216
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001217int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001218 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219{
1220 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1221 return BAD_VALUE;
1222
1223 Mutex::Autolock _l(mStateLock);
1224 if (mCurrentState.orientation != orientation) {
1225 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001226 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 mCurrentState.orientation = orientation;
1228 setTransactionFlags(eTransactionNeeded);
1229 mTransactionCV.wait(mStateLock);
1230 } else {
1231 orientation = BAD_VALUE;
1232 }
1233 }
1234 return orientation;
1235}
1236
Mathias Agopian96f08192010-06-02 23:28:45 -07001237sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001238 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1240 uint32_t flags)
1241{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001242 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001244
1245 if (int32_t(w|h) < 0) {
1246 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1247 int(w), int(h));
1248 return surfaceHandle;
1249 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001252 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 switch (flags & eFXSurfaceMask) {
1254 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001255 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1256 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257 break;
1258 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001259 // for now we treat Blur as Dim, until we can implement it
1260 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001262 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 break;
1264 }
1265
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001266 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001267 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001268 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001269 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001272 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001273 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001274 params->identity = surfaceHandle->getIdentity();
1275 params->width = w;
1276 params->height = h;
1277 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001278 if (normalLayer != 0) {
1279 Mutex::Autolock _l(mStateLock);
1280 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1281 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001282 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001283
Mathias Agopian96f08192010-06-02 23:28:45 -07001284 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 }
1286
1287 return surfaceHandle;
1288}
1289
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001290sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001291 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001293 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294{
1295 // initialize the surfaces
1296 switch (format) { // TODO: take h/w into account
1297 case PIXEL_FORMAT_TRANSPARENT:
1298 case PIXEL_FORMAT_TRANSLUCENT:
1299 format = PIXEL_FORMAT_RGBA_8888;
1300 break;
1301 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001302#ifdef NO_RGBX_8888
1303 format = PIXEL_FORMAT_RGB_565;
1304#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001305 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001306#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 break;
1308 }
1309
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001310#ifdef NO_RGBX_8888
1311 if (format == PIXEL_FORMAT_RGBX_8888)
1312 format = PIXEL_FORMAT_RGBA_8888;
1313#endif
1314
Mathias Agopian96f08192010-06-02 23:28:45 -07001315 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001316 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001317 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001319 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 }
1321 return layer;
1322}
1323
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001324sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001325 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001326 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327{
Mathias Agopian96f08192010-06-02 23:28:45 -07001328 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 return layer;
1331}
1332
Mathias Agopian96f08192010-06-02 23:28:45 -07001333status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334{
Mathias Agopian9a112062009-04-17 19:36:26 -07001335 /*
1336 * called by the window manager, when a surface should be marked for
1337 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001338 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001339 * The surface is removed from the current and drawing lists, but placed
1340 * in the purgatory queue, so it's not destroyed right-away (we need
1341 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001342 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001343
Mathias Agopian48d819a2009-09-10 19:41:18 -07001344 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001345 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001346 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001347 if (layer != 0) {
1348 err = purgatorizeLayer_l(layer);
1349 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001350 setTransactionFlags(eTransactionNeeded);
1351 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001352 }
1353 return err;
1354}
1355
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001356status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001357{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001358 // called by ~ISurface() when all references are gone
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001359 status_t err = NO_ERROR;
1360 sp<LayerBaseClient> l(layer.promote());
1361 if (l != NULL) {
1362 Mutex::Autolock _l(mStateLock);
1363 err = removeLayer_l(l);
Mathias Agopian36ca5e72011-06-07 14:52:22 -07001364 if (err == NAME_NOT_FOUND) {
1365 // The surface wasn't in the current list, which means it was
1366 // removed already, which means it is in the purgatory,
1367 // and need to be removed from there.
1368 ssize_t idx = mLayerPurgatory.remove(l);
1369 LOGE_IF(idx < 0,
1370 "layer=%p is not in the purgatory list", l.get());
1371 }
Mathias Agopianf3d939c2011-06-06 09:55:15 -07001372 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1373 "error removing layer=%p (%s)", l.get(), strerror(-err));
1374 }
1375 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376}
1377
1378status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001379 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 int32_t count,
1381 const layer_state_t* states)
1382{
1383 Mutex::Autolock _l(mStateLock);
1384 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001386 const layer_state_t& s(states[i]);
1387 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001388 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 if (what & ePositionChanged) {
1391 if (layer->setPosition(s.x, s.y))
1392 flags |= eTraversalNeeded;
1393 }
1394 if (what & eLayerChanged) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001395 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 if (layer->setLayer(s.z)) {
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001397 mCurrentState.layersSortedByZ.removeAt(idx);
1398 mCurrentState.layersSortedByZ.add(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399 // we need traversal (state changed)
1400 // AND transaction (list changed)
1401 flags |= eTransactionNeeded|eTraversalNeeded;
1402 }
1403 }
1404 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001405 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001407 mResizeTransationPending = true;
1408 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 }
1410 if (what & eAlphaChanged) {
1411 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1412 flags |= eTraversalNeeded;
1413 }
1414 if (what & eMatrixChanged) {
1415 if (layer->setMatrix(s.matrix))
1416 flags |= eTraversalNeeded;
1417 }
1418 if (what & eTransparentRegionChanged) {
1419 if (layer->setTransparentRegionHint(s.transparentRegion))
1420 flags |= eTraversalNeeded;
1421 }
1422 if (what & eVisibilityChanged) {
1423 if (layer->setFlags(s.flags, s.mask))
1424 flags |= eTraversalNeeded;
1425 }
1426 }
1427 }
1428 if (flags) {
1429 setTransactionFlags(flags);
1430 }
1431 return NO_ERROR;
1432}
1433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434void SurfaceFlinger::screenReleased(int dpy)
1435{
1436 // this may be called by a signal handler, we can't do too much in here
1437 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1438 signalEvent();
1439}
1440
1441void SurfaceFlinger::screenAcquired(int dpy)
1442{
1443 // this may be called by a signal handler, we can't do too much in here
1444 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1445 signalEvent();
1446}
1447
1448status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1449{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001450 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 char buffer[SIZE];
1452 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001453 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 snprintf(buffer, SIZE, "Permission Denial: "
1455 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1456 IPCThreadState::self()->getCallingPid(),
1457 IPCThreadState::self()->getCallingUid());
1458 result.append(buffer);
1459 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001460
1461 // figure out if we're stuck somewhere
1462 const nsecs_t now = systemTime();
1463 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1464 const nsecs_t inTransaction(mDebugInTransaction);
1465 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1466 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1467
1468 // Try to get the main lock, but don't insist if we can't
1469 // (this would indicate SF is stuck, but we want to be able to
1470 // print something in dumpsys).
1471 int retry = 3;
1472 while (mStateLock.tryLock()<0 && --retry>=0) {
1473 usleep(1000000);
1474 }
1475 const bool locked(retry >= 0);
1476 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001477 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001478 "SurfaceFlinger appears to be unresponsive, "
1479 "dumping anyways (no locks held)\n");
1480 result.append(buffer);
1481 }
1482
Mathias Agopian48b888a2011-01-19 16:15:53 -08001483 /*
1484 * Dump the visible layer list
1485 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1487 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001488 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1489 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001491 const sp<LayerBase>& layer(currentLayers[i]);
1492 layer->dump(result, buffer, SIZE);
1493 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 s.transparentRegion.dump(result, "transparentRegion");
1495 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1496 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1497 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001498
Mathias Agopian48b888a2011-01-19 16:15:53 -08001499 /*
1500 * Dump the layers in the purgatory
1501 */
1502
1503 const size_t purgatorySize = mLayerPurgatory.size();
1504 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1505 result.append(buffer);
1506 for (size_t i=0 ; i<purgatorySize ; i++) {
1507 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1508 layer->shortDump(result, buffer, SIZE);
1509 }
1510
1511 /*
1512 * Dump SurfaceFlinger global state
1513 */
1514
1515 snprintf(buffer, SIZE, "SurfaceFlinger global state\n");
1516 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517 mWormholeRegion.dump(result, "WormholeRegion");
1518 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1519 snprintf(buffer, SIZE,
1520 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1521 mFreezeDisplay?"yes":"no", mFreezeCount,
1522 mCurrentState.orientation, hw.canDraw());
1523 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001524 snprintf(buffer, SIZE,
1525 " last eglSwapBuffers() time: %f us\n"
1526 " last transaction time : %f us\n",
1527 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1528 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001529
Mathias Agopian9795c422009-08-26 16:36:26 -07001530 if (inSwapBuffersDuration || !locked) {
1531 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1532 inSwapBuffersDuration/1000.0);
1533 result.append(buffer);
1534 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001535
Mathias Agopian9795c422009-08-26 16:36:26 -07001536 if (inTransactionDuration || !locked) {
1537 snprintf(buffer, SIZE, " transaction time: %f us\n",
1538 inTransactionDuration/1000.0);
1539 result.append(buffer);
1540 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001541
Mathias Agopian48b888a2011-01-19 16:15:53 -08001542 /*
1543 * Dump HWComposer state
1544 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001545 HWComposer& hwc(hw.getHwComposer());
1546 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1547 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1548 mDebugDisableHWC ? "disabled" : "enabled");
1549 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001550 hwc.dump(result, buffer, SIZE);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001551
Mathias Agopian48b888a2011-01-19 16:15:53 -08001552 /*
1553 * Dump gralloc state
1554 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001555 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001556 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001557 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001558
1559 if (locked) {
1560 mStateLock.unlock();
1561 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562 }
1563 write(fd, result.string(), result.size());
1564 return NO_ERROR;
1565}
1566
1567status_t SurfaceFlinger::onTransact(
1568 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1569{
1570 switch (code) {
1571 case CREATE_CONNECTION:
1572 case OPEN_GLOBAL_TRANSACTION:
1573 case CLOSE_GLOBAL_TRANSACTION:
1574 case SET_ORIENTATION:
1575 case FREEZE_DISPLAY:
1576 case UNFREEZE_DISPLAY:
1577 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001578 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001579 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 {
1581 // codes that require permission check
1582 IPCThreadState* ipc = IPCThreadState::self();
1583 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001584 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001585 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1586 LOGE("Permission Denial: "
1587 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1588 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001590 break;
1591 }
1592 case CAPTURE_SCREEN:
1593 {
1594 // codes that require permission check
1595 IPCThreadState* ipc = IPCThreadState::self();
1596 const int pid = ipc->getCallingPid();
1597 const int uid = ipc->getCallingUid();
1598 if ((uid != AID_GRAPHICS) && !mReadFramebuffer.check(pid, uid)) {
1599 LOGE("Permission Denial: "
1600 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1601 return PERMISSION_DENIED;
1602 }
1603 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 }
1605 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001606
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1608 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001609 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001610 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1611 IPCThreadState* ipc = IPCThreadState::self();
1612 const int pid = ipc->getCallingPid();
1613 const int uid = ipc->getCallingUid();
1614 LOGE("Permission Denial: "
1615 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 return PERMISSION_DENIED;
1617 }
1618 int n;
1619 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001620 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001621 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 return NO_ERROR;
1623 case 1002: // SHOW_UPDATES
1624 n = data.readInt32();
1625 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1626 return NO_ERROR;
1627 case 1003: // SHOW_BACKGROUND
1628 n = data.readInt32();
1629 mDebugBackground = n ? 1 : 0;
1630 return NO_ERROR;
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001631 case 1008: // toggle use of hw composer
1632 n = data.readInt32();
1633 mDebugDisableHWC = n ? 1 : 0;
Mathias Agopianad456f92011-01-13 17:53:01 -08001634 invalidateHwcGeometry();
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001635 // fall-through...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 case 1004:{ // repaint everything
1637 Mutex::Autolock _l(mStateLock);
1638 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1639 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1640 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001641 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001643 case 1005:{ // force transaction
1644 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1645 return NO_ERROR;
1646 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001647 case 1006:{ // enable/disable GraphicLog
1648 int enabled = data.readInt32();
1649 GraphicLog::getInstance().setEnabled(enabled);
1650 return NO_ERROR;
1651 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 case 1007: // set mFreezeCount
1653 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001654 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 return NO_ERROR;
1656 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001657 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 reply->writeInt32(0);
1659 reply->writeInt32(mDebugRegion);
1660 reply->writeInt32(mDebugBackground);
1661 return NO_ERROR;
1662 case 1013: {
1663 Mutex::Autolock _l(mStateLock);
1664 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1665 reply->writeInt32(hw.getPageFlipCount());
1666 }
1667 return NO_ERROR;
1668 }
1669 }
1670 return err;
1671}
1672
Mathias Agopian59119e62010-10-11 12:37:43 -07001673// ---------------------------------------------------------------------------
1674
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001675status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1676 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001677{
Mathias Agopian59119e62010-10-11 12:37:43 -07001678 if (!GLExtensions::getInstance().haveFramebufferObject())
1679 return INVALID_OPERATION;
1680
1681 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001682 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001683 const uint32_t hw_w = hw.getWidth();
1684 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001685 GLfloat u = 1;
1686 GLfloat v = 1;
1687
1688 // make sure to clear all GL error flags
1689 while ( glGetError() != GL_NO_ERROR ) ;
1690
1691 // create a FBO
1692 GLuint name, tname;
1693 glGenTextures(1, &tname);
1694 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001695 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1696 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001697 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001698 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001699 GLint tw = (2 << (31 - clz(hw_w)));
1700 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001701 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1702 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001703 u = GLfloat(hw_w) / tw;
1704 v = GLfloat(hw_h) / th;
1705 }
1706 glGenFramebuffersOES(1, &name);
1707 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001708 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1709 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001710
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001711 // redraw the screen entirely...
1712 glClearColor(0,0,0,1);
1713 glClear(GL_COLOR_BUFFER_BIT);
1714 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1715 const size_t count = layers.size();
1716 for (size_t i=0 ; i<count ; ++i) {
1717 const sp<LayerBase>& layer(layers[i]);
1718 layer->drawForSreenShot();
1719 }
1720
1721 // back to main framebuffer
1722 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1723 glDisable(GL_SCISSOR_TEST);
1724 glDeleteFramebuffersOES(1, &name);
1725
1726 *textureName = tname;
1727 *uOut = u;
1728 *vOut = v;
1729 return NO_ERROR;
1730}
1731
1732// ---------------------------------------------------------------------------
1733
1734status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1735{
1736 status_t result = PERMISSION_DENIED;
1737
1738 if (!GLExtensions::getInstance().haveFramebufferObject())
1739 return INVALID_OPERATION;
1740
1741 // get screen geometry
1742 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1743 const uint32_t hw_w = hw.getWidth();
1744 const uint32_t hw_h = hw.getHeight();
1745 const Region screenBounds(hw.bounds());
1746
1747 GLfloat u, v;
1748 GLuint tname;
1749 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1750 if (result != NO_ERROR) {
1751 return result;
1752 }
1753
1754 GLfloat vtx[8];
1755 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1756 glEnable(GL_TEXTURE_2D);
1757 glBindTexture(GL_TEXTURE_2D, tname);
1758 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1759 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1760 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1761 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1762 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1763 glVertexPointer(2, GL_FLOAT, 0, vtx);
1764
1765 class s_curve_interpolator {
1766 const float nbFrames, s, v;
1767 public:
1768 s_curve_interpolator(int nbFrames, float s)
1769 : nbFrames(1.0f / (nbFrames-1)), s(s),
1770 v(1.0f + expf(-s + 0.5f*s)) {
1771 }
1772 float operator()(int f) {
1773 const float x = f * nbFrames;
1774 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1775 }
1776 };
1777
1778 class v_stretch {
1779 const GLfloat hw_w, hw_h;
1780 public:
1781 v_stretch(uint32_t hw_w, uint32_t hw_h)
1782 : hw_w(hw_w), hw_h(hw_h) {
1783 }
1784 void operator()(GLfloat* vtx, float v) {
1785 const GLfloat w = hw_w + (hw_w * v);
1786 const GLfloat h = hw_h - (hw_h * v);
1787 const GLfloat x = (hw_w - w) * 0.5f;
1788 const GLfloat y = (hw_h - h) * 0.5f;
1789 vtx[0] = x; vtx[1] = y;
1790 vtx[2] = x; vtx[3] = y + h;
1791 vtx[4] = x + w; vtx[5] = y + h;
1792 vtx[6] = x + w; vtx[7] = y;
1793 }
1794 };
1795
1796 class h_stretch {
1797 const GLfloat hw_w, hw_h;
1798 public:
1799 h_stretch(uint32_t hw_w, uint32_t hw_h)
1800 : hw_w(hw_w), hw_h(hw_h) {
1801 }
1802 void operator()(GLfloat* vtx, float v) {
1803 const GLfloat w = hw_w - (hw_w * v);
1804 const GLfloat h = 1.0f;
1805 const GLfloat x = (hw_w - w) * 0.5f;
1806 const GLfloat y = (hw_h - h) * 0.5f;
1807 vtx[0] = x; vtx[1] = y;
1808 vtx[2] = x; vtx[3] = y + h;
1809 vtx[4] = x + w; vtx[5] = y + h;
1810 vtx[6] = x + w; vtx[7] = y;
1811 }
1812 };
1813
1814 // the full animation is 24 frames
1815 const int nbFrames = 12;
1816 s_curve_interpolator itr(nbFrames, 7.5f);
1817 s_curve_interpolator itg(nbFrames, 8.0f);
1818 s_curve_interpolator itb(nbFrames, 8.5f);
1819
1820 v_stretch vverts(hw_w, hw_h);
1821 glEnable(GL_BLEND);
1822 glBlendFunc(GL_ONE, GL_ONE);
1823 for (int i=0 ; i<nbFrames ; i++) {
1824 float x, y, w, h;
1825 const float vr = itr(i);
1826 const float vg = itg(i);
1827 const float vb = itb(i);
1828
1829 // clear screen
1830 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001831 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001832 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001833
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001834 // draw the red plane
1835 vverts(vtx, vr);
1836 glColorMask(1,0,0,1);
1837 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001838
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001839 // draw the green plane
1840 vverts(vtx, vg);
1841 glColorMask(0,1,0,1);
1842 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001843
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001844 // draw the blue plane
1845 vverts(vtx, vb);
1846 glColorMask(0,0,1,1);
1847 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001848
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001849 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001850 glDisable(GL_TEXTURE_2D);
1851 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001852 glColor4f(vg, vg, vg, 1);
1853 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1854 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001855 }
1856
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001857 h_stretch hverts(hw_w, hw_h);
1858 glDisable(GL_BLEND);
1859 glDisable(GL_TEXTURE_2D);
1860 glColorMask(1,1,1,1);
1861 for (int i=0 ; i<nbFrames ; i++) {
1862 const float v = itg(i);
1863 hverts(vtx, v);
1864 glClear(GL_COLOR_BUFFER_BIT);
1865 glColor4f(1-v, 1-v, 1-v, 1);
1866 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1867 hw.flip(screenBounds);
1868 }
1869
1870 glColorMask(1,1,1,1);
1871 glEnable(GL_SCISSOR_TEST);
1872 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1873 glDeleteTextures(1, &tname);
1874 return NO_ERROR;
1875}
1876
1877status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1878{
1879 status_t result = PERMISSION_DENIED;
1880
1881 if (!GLExtensions::getInstance().haveFramebufferObject())
1882 return INVALID_OPERATION;
1883
1884
1885 // get screen geometry
1886 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1887 const uint32_t hw_w = hw.getWidth();
1888 const uint32_t hw_h = hw.getHeight();
1889 const Region screenBounds(hw.bounds());
1890
1891 GLfloat u, v;
1892 GLuint tname;
1893 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1894 if (result != NO_ERROR) {
1895 return result;
1896 }
1897
1898 // back to main framebuffer
1899 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1900 glDisable(GL_SCISSOR_TEST);
1901
1902 GLfloat vtx[8];
1903 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
1904 glEnable(GL_TEXTURE_2D);
1905 glBindTexture(GL_TEXTURE_2D, tname);
1906 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1907 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1908 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1909 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1910 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1911 glVertexPointer(2, GL_FLOAT, 0, vtx);
1912
1913 class s_curve_interpolator {
1914 const float nbFrames, s, v;
1915 public:
1916 s_curve_interpolator(int nbFrames, float s)
1917 : nbFrames(1.0f / (nbFrames-1)), s(s),
1918 v(1.0f + expf(-s + 0.5f*s)) {
1919 }
1920 float operator()(int f) {
1921 const float x = f * nbFrames;
1922 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1923 }
1924 };
1925
1926 class v_stretch {
1927 const GLfloat hw_w, hw_h;
1928 public:
1929 v_stretch(uint32_t hw_w, uint32_t hw_h)
1930 : hw_w(hw_w), hw_h(hw_h) {
1931 }
1932 void operator()(GLfloat* vtx, float v) {
1933 const GLfloat w = hw_w + (hw_w * v);
1934 const GLfloat h = hw_h - (hw_h * v);
1935 const GLfloat x = (hw_w - w) * 0.5f;
1936 const GLfloat y = (hw_h - h) * 0.5f;
1937 vtx[0] = x; vtx[1] = y;
1938 vtx[2] = x; vtx[3] = y + h;
1939 vtx[4] = x + w; vtx[5] = y + h;
1940 vtx[6] = x + w; vtx[7] = y;
1941 }
1942 };
1943
1944 class h_stretch {
1945 const GLfloat hw_w, hw_h;
1946 public:
1947 h_stretch(uint32_t hw_w, uint32_t hw_h)
1948 : hw_w(hw_w), hw_h(hw_h) {
1949 }
1950 void operator()(GLfloat* vtx, float v) {
1951 const GLfloat w = hw_w - (hw_w * v);
1952 const GLfloat h = 1.0f;
1953 const GLfloat x = (hw_w - w) * 0.5f;
1954 const GLfloat y = (hw_h - h) * 0.5f;
1955 vtx[0] = x; vtx[1] = y;
1956 vtx[2] = x; vtx[3] = y + h;
1957 vtx[4] = x + w; vtx[5] = y + h;
1958 vtx[6] = x + w; vtx[7] = y;
1959 }
1960 };
1961
Mathias Agopiana6546e52010-10-14 12:33:07 -07001962 // the full animation is 12 frames
1963 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001964 s_curve_interpolator itr(nbFrames, 7.5f);
1965 s_curve_interpolator itg(nbFrames, 8.0f);
1966 s_curve_interpolator itb(nbFrames, 8.5f);
1967
1968 h_stretch hverts(hw_w, hw_h);
1969 glDisable(GL_BLEND);
1970 glDisable(GL_TEXTURE_2D);
1971 glColorMask(1,1,1,1);
1972 for (int i=nbFrames-1 ; i>=0 ; i--) {
1973 const float v = itg(i);
1974 hverts(vtx, v);
1975 glClear(GL_COLOR_BUFFER_BIT);
1976 glColor4f(1-v, 1-v, 1-v, 1);
1977 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1978 hw.flip(screenBounds);
1979 }
1980
Mathias Agopiana6546e52010-10-14 12:33:07 -07001981 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001982 v_stretch vverts(hw_w, hw_h);
1983 glEnable(GL_BLEND);
1984 glBlendFunc(GL_ONE, GL_ONE);
1985 for (int i=nbFrames-1 ; i>=0 ; i--) {
1986 float x, y, w, h;
1987 const float vr = itr(i);
1988 const float vg = itg(i);
1989 const float vb = itb(i);
1990
1991 // clear screen
1992 glColorMask(1,1,1,1);
1993 glClear(GL_COLOR_BUFFER_BIT);
1994 glEnable(GL_TEXTURE_2D);
1995
1996 // draw the red plane
1997 vverts(vtx, vr);
1998 glColorMask(1,0,0,1);
1999 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2000
2001 // draw the green plane
2002 vverts(vtx, vg);
2003 glColorMask(0,1,0,1);
2004 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2005
2006 // draw the blue plane
2007 vverts(vtx, vb);
2008 glColorMask(0,0,1,1);
2009 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2010
2011 hw.flip(screenBounds);
2012 }
2013
2014 glColorMask(1,1,1,1);
2015 glEnable(GL_SCISSOR_TEST);
2016 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 glDeleteTextures(1, &tname);
2018
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002019 return NO_ERROR;
2020}
2021
2022// ---------------------------------------------------------------------------
2023
Mathias Agopianabd671a2010-10-14 14:54:06 -07002024status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002025{
2026 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2027 if (!hw.canDraw()) {
2028 // we're already off
2029 return NO_ERROR;
2030 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002031 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2032 electronBeamOffAnimationImplLocked();
2033 }
2034
2035 // always clear the whole screen at the end of the animation
2036 glClearColor(0,0,0,1);
2037 glDisable(GL_SCISSOR_TEST);
2038 glClear(GL_COLOR_BUFFER_BIT);
2039 glEnable(GL_SCISSOR_TEST);
2040 hw.flip( Region(hw.bounds()) );
2041
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002042 hw.setCanDraw(false);
2043 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002044}
2045
2046status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2047{
Mathias Agopian59119e62010-10-11 12:37:43 -07002048 class MessageTurnElectronBeamOff : public MessageBase {
2049 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002050 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002051 status_t result;
2052 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002053 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2054 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002055 }
2056 status_t getResult() const {
2057 return result;
2058 }
2059 virtual bool handler() {
2060 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002061 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002062 return true;
2063 }
2064 };
2065
Mathias Agopianabd671a2010-10-14 14:54:06 -07002066 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002067 status_t res = postMessageSync(msg);
2068 if (res == NO_ERROR) {
2069 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002070
2071 // work-around: when the power-manager calls us we activate the
2072 // animation. eventually, the "on" animation will be called
2073 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002074 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002075 }
2076 return res;
2077}
2078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002080
Mathias Agopianabd671a2010-10-14 14:54:06 -07002081status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002082{
2083 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2084 if (hw.canDraw()) {
2085 // we're already on
2086 return NO_ERROR;
2087 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002088 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2089 electronBeamOnAnimationImplLocked();
2090 }
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002091 hw.setCanDraw(true);
Mathias Agopiana7f03732010-10-14 12:46:24 -07002092
2093 // make sure to redraw the whole screen when the animation is done
2094 mDirtyRegion.set(hw.bounds());
2095 signalEvent();
2096
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002097 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098}
2099
2100status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2101{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002102 class MessageTurnElectronBeamOn : public MessageBase {
2103 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002104 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002105 status_t result;
2106 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002107 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2108 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002109 }
2110 status_t getResult() const {
2111 return result;
2112 }
2113 virtual bool handler() {
2114 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002115 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002116 return true;
2117 }
2118 };
2119
Mathias Agopianabd671a2010-10-14 14:54:06 -07002120 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002121 return NO_ERROR;
2122}
2123
2124// ---------------------------------------------------------------------------
2125
Mathias Agopian74c40c02010-09-29 13:02:36 -07002126status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2127 sp<IMemoryHeap>* heap,
2128 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002129 uint32_t sw, uint32_t sh,
2130 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002131{
2132 status_t result = PERMISSION_DENIED;
2133
2134 // only one display supported for now
2135 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2136 return BAD_VALUE;
2137
Jamie Gennis7a4d0df2011-03-09 17:05:02 -08002138 // make sure none of the layers are protected
2139 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2140 const size_t count = layers.size();
2141 for (size_t i=0 ; i<count ; ++i) {
2142 const sp<LayerBase>& layer(layers[i]);
2143 const uint32_t z = layer->drawingState().z;
2144 if (z >= minLayerZ && z <= maxLayerZ) {
2145 if (layer->isProtected()) {
2146 return INVALID_OPERATION;
2147 }
2148 }
2149 }
2150
Mathias Agopian74c40c02010-09-29 13:02:36 -07002151 if (!GLExtensions::getInstance().haveFramebufferObject())
2152 return INVALID_OPERATION;
2153
2154 // get screen geometry
2155 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2156 const uint32_t hw_w = hw.getWidth();
2157 const uint32_t hw_h = hw.getHeight();
2158
2159 if ((sw > hw_w) || (sh > hw_h))
2160 return BAD_VALUE;
2161
2162 sw = (!sw) ? hw_w : sw;
2163 sh = (!sh) ? hw_h : sh;
2164 const size_t size = sw * sh * 4;
2165
Mathias Agopian1c71a472011-03-02 18:45:50 -08002166 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2167 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002168
Mathias Agopian74c40c02010-09-29 13:02:36 -07002169 // make sure to clear all GL error flags
2170 while ( glGetError() != GL_NO_ERROR ) ;
2171
2172 // create a FBO
2173 GLuint name, tname;
2174 glGenRenderbuffersOES(1, &tname);
2175 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2176 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2177 glGenFramebuffersOES(1, &name);
2178 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2179 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2180 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2181
2182 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002183
Mathias Agopian74c40c02010-09-29 13:02:36 -07002184 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2185
2186 // invert everything, b/c glReadPixel() below will invert the FB
2187 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002188 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002189 glMatrixMode(GL_PROJECTION);
2190 glPushMatrix();
2191 glLoadIdentity();
2192 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2193 glMatrixMode(GL_MODELVIEW);
2194
2195 // redraw the screen entirely...
2196 glClearColor(0,0,0,1);
2197 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002198
Mathias Agopian74c40c02010-09-29 13:02:36 -07002199 for (size_t i=0 ; i<count ; ++i) {
2200 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002201 const uint32_t z = layer->drawingState().z;
2202 if (z >= minLayerZ && z <= maxLayerZ) {
2203 layer->drawForSreenShot();
2204 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002205 }
2206
2207 // XXX: this is needed on tegra
2208 glScissor(0, 0, sw, sh);
2209
2210 // check for errors and return screen capture
2211 if (glGetError() != GL_NO_ERROR) {
2212 // error while rendering
2213 result = INVALID_OPERATION;
2214 } else {
2215 // allocate shared memory large enough to hold the
2216 // screen capture
2217 sp<MemoryHeapBase> base(
2218 new MemoryHeapBase(size, 0, "screen-capture") );
2219 void* const ptr = base->getBase();
2220 if (ptr) {
2221 // capture the screen with glReadPixels()
2222 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2223 if (glGetError() == GL_NO_ERROR) {
2224 *heap = base;
2225 *w = sw;
2226 *h = sh;
2227 *f = PIXEL_FORMAT_RGBA_8888;
2228 result = NO_ERROR;
2229 }
2230 } else {
2231 result = NO_MEMORY;
2232 }
2233 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002234 glEnable(GL_SCISSOR_TEST);
2235 glViewport(0, 0, hw_w, hw_h);
2236 glMatrixMode(GL_PROJECTION);
2237 glPopMatrix();
2238 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002239 } else {
2240 result = BAD_VALUE;
2241 }
2242
2243 // release FBO resources
2244 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2245 glDeleteRenderbuffersOES(1, &tname);
2246 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002247
2248 hw.compositionComplete();
2249
Mathias Agopian1c71a472011-03-02 18:45:50 -08002250 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002251
Mathias Agopian74c40c02010-09-29 13:02:36 -07002252 return result;
2253}
2254
2255
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002256status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2257 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002258 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002259 uint32_t sw, uint32_t sh,
2260 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002261{
2262 // only one display supported for now
2263 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2264 return BAD_VALUE;
2265
2266 if (!GLExtensions::getInstance().haveFramebufferObject())
2267 return INVALID_OPERATION;
2268
2269 class MessageCaptureScreen : public MessageBase {
2270 SurfaceFlinger* flinger;
2271 DisplayID dpy;
2272 sp<IMemoryHeap>* heap;
2273 uint32_t* w;
2274 uint32_t* h;
2275 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002276 uint32_t sw;
2277 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002278 uint32_t minLayerZ;
2279 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002280 status_t result;
2281 public:
2282 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002283 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002284 uint32_t sw, uint32_t sh,
2285 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002286 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002287 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2288 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2289 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002290 {
2291 }
2292 status_t getResult() const {
2293 return result;
2294 }
2295 virtual bool handler() {
2296 Mutex::Autolock _l(flinger->mStateLock);
2297
2298 // if we have secure windows, never allow the screen capture
2299 if (flinger->mSecureFrameBuffer)
2300 return true;
2301
Mathias Agopian74c40c02010-09-29 13:02:36 -07002302 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002303 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002304
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002305 return true;
2306 }
2307 };
2308
2309 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002310 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002311 status_t res = postMessageSync(msg);
2312 if (res == NO_ERROR) {
2313 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2314 }
2315 return res;
2316}
2317
2318// ---------------------------------------------------------------------------
2319
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002320sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2321{
2322 sp<Layer> result;
2323 Mutex::Autolock _l(mStateLock);
2324 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2325 return result;
2326}
2327
2328// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002329
Mathias Agopian96f08192010-06-02 23:28:45 -07002330Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002331 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002332{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002333}
2334
Mathias Agopian96f08192010-06-02 23:28:45 -07002335Client::~Client()
2336{
Mathias Agopian96f08192010-06-02 23:28:45 -07002337 const size_t count = mLayers.size();
2338 for (size_t i=0 ; i<count ; i++) {
2339 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2340 if (layer != 0) {
2341 mFlinger->removeLayer(layer);
2342 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002343 }
2344}
2345
Mathias Agopian96f08192010-06-02 23:28:45 -07002346status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002347 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002348}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002349
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002350size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002351{
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002352 Mutex::Autolock _l(mLock);
2353 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002354 mLayers.add(name, layer);
2355 return name;
2356}
2357
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002358void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002359{
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002360 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002361 // we do a linear search here, because this doesn't happen often
2362 const size_t count = mLayers.size();
2363 for (size_t i=0 ; i<count ; i++) {
2364 if (mLayers.valueAt(i) == layer) {
2365 mLayers.removeItemsAt(i, 1);
2366 break;
2367 }
2368 }
2369}
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002370sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2371{
2372 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002373 sp<LayerBaseClient> lbc;
Mathias Agopianf3d939c2011-06-06 09:55:15 -07002374 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002375 if (layer != 0) {
2376 lbc = layer.promote();
2377 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002378 }
2379 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002380}
2381
Mathias Agopian96f08192010-06-02 23:28:45 -07002382sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002383 return 0;
2384}
2385ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
2386 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002387}
Mathias Agopian96f08192010-06-02 23:28:45 -07002388sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002389 ISurfaceComposerClient::surface_data_t* params, int pid,
2390 const String8& name,
2391 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002392 uint32_t flags)
2393{
Mathias Agopian96f08192010-06-02 23:28:45 -07002394 return mFlinger->createSurface(this, pid, name, params,
2395 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002396}
Mathias Agopian96f08192010-06-02 23:28:45 -07002397status_t Client::destroySurface(SurfaceID sid) {
2398 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002399}
Mathias Agopian96f08192010-06-02 23:28:45 -07002400status_t Client::setState(int32_t count, const layer_state_t* states) {
2401 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002402}
2403
2404// ---------------------------------------------------------------------------
2405
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002406UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
2407 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
2408{
2409 const int pgsize = getpagesize();
2410 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
2411
2412 mCblkHeap = new MemoryHeapBase(cblksize, 0,
2413 "SurfaceFlinger Client control-block");
2414
2415 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
2416 if (ctrlblk) { // construct the shared structure in-place.
2417 new(ctrlblk) SharedClient;
2418 }
2419}
2420
2421UserClient::~UserClient()
2422{
2423 if (ctrlblk) {
2424 ctrlblk->~SharedClient(); // destroy our shared-structure.
2425 }
2426
2427 /*
2428 * When a UserClient dies, it's unclear what to do exactly.
2429 * We could go ahead and destroy all surfaces linked to that client
2430 * however, it wouldn't be fair to the main Client
2431 * (usually the the window-manager), which might want to re-target
2432 * the layer to another UserClient.
2433 * I think the best is to do nothing, or not much; in most cases the
2434 * WM itself will go ahead and clean things up when it detects a client of
2435 * his has died.
2436 * The remaining question is what to display? currently we keep
2437 * just keep the current buffer.
2438 */
2439}
2440
2441status_t UserClient::initCheck() const {
2442 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
2443}
2444
2445void UserClient::detachLayer(const Layer* layer)
2446{
2447 int32_t name = layer->getToken();
2448 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002449 int32_t mask = 1LU<<name;
2450 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
2451 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
2452 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002453 }
2454}
2455
2456sp<IMemoryHeap> UserClient::getControlBlock() const {
2457 return mCblkHeap;
2458}
2459
2460ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
2461{
2462 int32_t name = NAME_NOT_FOUND;
2463 sp<Layer> layer(mFlinger->getLayer(sur));
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002464 if (layer == 0) {
2465 return name;
2466 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002467
Mathias Agopian579b3f82010-06-08 19:54:15 -07002468 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002469 name = layer->getToken();
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002470 if ((name >= 0) && (layer->getClient() == this)) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07002471 return name;
Jamie Gennis89c2dd22010-08-10 16:37:53 -07002472 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002473
2474 name = 0;
2475 do {
2476 int32_t mask = 1LU<<name;
2477 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
2478 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07002479 status_t err = layer->setToken(
2480 const_cast<UserClient*>(this), ctrlblk, name);
2481 if (err != NO_ERROR) {
2482 // free the name
2483 android_atomic_and(~mask, &mBitmap);
2484 name = err;
2485 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002486 break;
2487 }
Jamie Gennis8a083922011-02-10 16:18:36 -08002488 if (++name >= SharedBufferStack::NUM_LAYERS_MAX)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002489 name = NO_MEMORY;
2490 } while(name >= 0);
2491
Mathias Agopian53503a92010-06-08 15:40:56 -07002492 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
2493 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002494 return name;
2495}
2496
2497sp<ISurface> UserClient::createSurface(
2498 ISurfaceComposerClient::surface_data_t* params, int pid,
2499 const String8& name,
2500 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2501 uint32_t flags) {
2502 return 0;
2503}
2504status_t UserClient::destroySurface(SurfaceID sid) {
2505 return INVALID_OPERATION;
2506}
2507status_t UserClient::setState(int32_t count, const layer_state_t* states) {
2508 return INVALID_OPERATION;
2509}
2510
2511// ---------------------------------------------------------------------------
2512
Jamie Gennis9a78c902011-01-12 18:30:40 -08002513GraphicBufferAlloc::GraphicBufferAlloc() {}
2514
2515GraphicBufferAlloc::~GraphicBufferAlloc() {}
2516
2517sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
2518 PixelFormat format, uint32_t usage) {
2519 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2520 status_t err = graphicBuffer->initCheck();
2521 if (err != 0) {
2522 LOGE("createGraphicBuffer: init check failed: %d", err);
2523 return 0;
2524 } else if (graphicBuffer->handle == 0) {
2525 LOGE("createGraphicBuffer: unable to create GraphicBuffer");
2526 return 0;
2527 }
2528 Mutex::Autolock _l(mLock);
2529 mBuffers.add(graphicBuffer);
2530 return graphicBuffer;
2531}
2532
2533void GraphicBufferAlloc::freeAllGraphicBuffersExcept(int bufIdx) {
2534 Mutex::Autolock _l(mLock);
2535 if (0 <= bufIdx && bufIdx < mBuffers.size()) {
2536 sp<GraphicBuffer> b(mBuffers[bufIdx]);
2537 mBuffers.clear();
2538 mBuffers.add(b);
2539 } else {
2540 mBuffers.clear();
2541 }
2542}
2543
2544// ---------------------------------------------------------------------------
2545
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546GraphicPlane::GraphicPlane()
2547 : mHw(0)
2548{
2549}
2550
2551GraphicPlane::~GraphicPlane() {
2552 delete mHw;
2553}
2554
2555bool GraphicPlane::initialized() const {
2556 return mHw ? true : false;
2557}
2558
Mathias Agopian2b92d892010-02-08 15:49:35 -08002559int GraphicPlane::getWidth() const {
2560 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561}
2562
Mathias Agopian2b92d892010-02-08 15:49:35 -08002563int GraphicPlane::getHeight() const {
2564 return mHeight;
2565}
2566
2567void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2568{
2569 mHw = hw;
2570
2571 // initialize the display orientation transform.
2572 // it's a constant that should come from the display driver.
2573 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2574 char property[PROPERTY_VALUE_MAX];
2575 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2576 //displayOrientation
2577 switch (atoi(property)) {
2578 case 90:
2579 displayOrientation = ISurfaceComposer::eOrientation90;
2580 break;
2581 case 270:
2582 displayOrientation = ISurfaceComposer::eOrientation270;
2583 break;
2584 }
2585 }
2586
2587 const float w = hw->getWidth();
2588 const float h = hw->getHeight();
2589 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2590 &mDisplayTransform);
2591 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2592 mDisplayWidth = h;
2593 mDisplayHeight = w;
2594 } else {
2595 mDisplayWidth = w;
2596 mDisplayHeight = h;
2597 }
2598
2599 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002600}
2601
2602status_t GraphicPlane::orientationToTransfrom(
2603 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002604{
2605 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002606 switch (orientation) {
2607 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002608 flags = Transform::ROT_0;
2609 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002610 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002611 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002612 break;
2613 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002614 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615 break;
2616 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002617 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618 break;
2619 default:
2620 return BAD_VALUE;
2621 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002622 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623 return NO_ERROR;
2624}
2625
2626status_t GraphicPlane::setOrientation(int orientation)
2627{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628 // If the rotation can be handled in hardware, this is where
2629 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002630
2631 const DisplayHardware& hw(displayHardware());
2632 const float w = mDisplayWidth;
2633 const float h = mDisplayHeight;
2634 mWidth = int(w);
2635 mHeight = int(h);
2636
2637 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002638 GraphicPlane::orientationToTransfrom(orientation, w, h,
2639 &orientationTransform);
2640 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2641 mWidth = int(h);
2642 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002643 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002644
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002645 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002646 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002647 return NO_ERROR;
2648}
2649
2650const DisplayHardware& GraphicPlane::displayHardware() const {
2651 return *mHw;
2652}
2653
Mathias Agopian59119e62010-10-11 12:37:43 -07002654DisplayHardware& GraphicPlane::editDisplayHardware() {
2655 return *mHw;
2656}
2657
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002658const Transform& GraphicPlane::transform() const {
2659 return mGlobalTransform;
2660}
2661
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002662EGLDisplay GraphicPlane::getEGLDisplay() const {
2663 return mHw->getEGLDisplay();
2664}
2665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002666// ---------------------------------------------------------------------------
2667
2668}; // namespace android