blob: 96a541184edb8cd950fc4d61e4a0f2889af4505e [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43#include <pixelflinger/pixelflinger.h>
44#include <GLES/gl.h>
45
46#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include "Layer.h"
48#include "LayerBlur.h"
49#include "LayerBuffer.h"
50#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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopiana1ecca92009-05-21 19:21:59 -070055/* ideally AID_GRAPHICS would be in a semi-public header
56 * or there would be a way to map a user/group name to its id
57 */
58#ifndef AID_GRAPHICS
59#define AID_GRAPHICS 1003
60#endif
61
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#define DISPLAY_COUNT 1
63
64namespace android {
65
66// ---------------------------------------------------------------------------
67
68void SurfaceFlinger::instantiate() {
69 defaultServiceManager()->addService(
70 String16("SurfaceFlinger"), new SurfaceFlinger());
71}
72
73void SurfaceFlinger::shutdown() {
74 // we should unregister here, but not really because
75 // when (if) the service manager goes away, all the services
76 // it has a reference to will leave too.
77}
78
79// ---------------------------------------------------------------------------
80
81SurfaceFlinger::LayerVector::LayerVector(const SurfaceFlinger::LayerVector& rhs)
82 : lookup(rhs.lookup), layers(rhs.layers)
83{
84}
85
86ssize_t SurfaceFlinger::LayerVector::indexOf(
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
89 if (guess<size() && lookup.keyAt(guess) == key)
90 return guess;
91 const ssize_t i = lookup.indexOfKey(key);
92 if (i>=0) {
93 const size_t idx = lookup.valueAt(i);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094 LOGE_IF(layers[idx]!=key,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 return idx;
98 }
99 return i;
100}
101
102ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 const sp<LayerBase>& layer,
104 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
106 size_t count = layers.size();
107 ssize_t l = 0;
108 ssize_t h = count-1;
109 ssize_t mid;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110 sp<LayerBase> const* a = layers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 while (l <= h) {
112 mid = l + (h - l)/2;
113 const int c = cmp(a+mid, &layer);
114 if (c == 0) { l = mid; break; }
115 else if (c<0) { l = mid+1; }
116 else { h = mid-1; }
117 }
118 size_t order = l;
119 while (order<count && !cmp(&layer, a+order)) {
120 order++;
121 }
122 count = lookup.size();
123 for (size_t i=0 ; i<count ; i++) {
124 if (lookup.valueAt(i) >= order) {
125 lookup.editValueAt(i)++;
126 }
127 }
128 layers.insertAt(layer, order);
129 lookup.add(layer, order);
130 return order;
131}
132
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
135 const ssize_t keyIndex = lookup.indexOfKey(layer);
136 if (keyIndex >= 0) {
137 const size_t index = lookup.valueAt(keyIndex);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 LOGE_IF(layers[index]!=layer,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 this, int(index), layers[index].get(), layer.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 layers.removeItemsAt(index);
142 lookup.removeItemsAt(keyIndex);
143 const size_t count = lookup.size();
144 for (size_t i=0 ; i<count ; i++) {
145 if (lookup.valueAt(i) >= size_t(index)) {
146 lookup.editValueAt(i)--;
147 }
148 }
149 return index;
150 }
151 return NAME_NOT_FOUND;
152}
153
154ssize_t SurfaceFlinger::LayerVector::reorder(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700155 const sp<LayerBase>& layer,
156 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
158 // XXX: it's a little lame. but oh well...
159 ssize_t err = remove(layer);
160 if (err >=0)
161 err = add(layer, cmp);
162 return err;
163}
164
165// ---------------------------------------------------------------------------
166#if 0
167#pragma mark -
168#endif
169
170SurfaceFlinger::SurfaceFlinger()
171 : BnSurfaceComposer(), Thread(false),
172 mTransactionFlags(0),
173 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700174 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -0700177 mHardwareTest("android.permission.HARDWARE_TEST"),
178 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
179 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 mVisibleRegionsDirty(false),
181 mDeferReleaseConsole(false),
182 mFreezeDisplay(false),
183 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700184 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700187 mDebugInSwapBuffers(0),
188 mLastSwapBufferTime(0),
189 mDebugInTransaction(0),
190 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700191 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 mConsoleSignals(0),
193 mSecureFrameBuffer(0)
194{
195 init();
196}
197
198void SurfaceFlinger::init()
199{
200 LOGI("SurfaceFlinger is starting");
201
202 // debugging stuff...
203 char value[PROPERTY_VALUE_MAX];
204 property_get("debug.sf.showupdates", value, "0");
205 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 property_get("debug.sf.showbackground", value, "0");
207 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
Mathias Agopian78fd5012010-04-20 14:51:04 -0700209 LOGI_IF(mDebugRegion, "showupdates enabled");
210 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
213SurfaceFlinger::~SurfaceFlinger()
214{
215 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216}
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
219{
220 return graphicPlane(0).displayHardware().getOverlayEngine();
221}
222
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700223sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
Mathias Agopian7e27f052010-05-28 14:22:23 -0700228sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229{
Mathias Agopian96f08192010-06-02 23:28:45 -0700230 sp<ISurfaceComposerClient> bclient;
231 sp<Client> client(new Client(this));
232 status_t err = client->initCheck();
233 if (err == NO_ERROR) {
234 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 return bclient;
237}
238
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700239sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
240{
241 sp<ISurfaceComposerClient> bclient;
242 sp<UserClient> client(new UserClient(this));
243 status_t err = client->initCheck();
244 if (err == NO_ERROR) {
245 bclient = client;
246 }
247 return bclient;
248}
249
250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
252{
253 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
254 const GraphicPlane& plane(mGraphicPlanes[dpy]);
255 return plane;
256}
257
258GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
259{
260 return const_cast<GraphicPlane&>(
261 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
262}
263
264void SurfaceFlinger::bootFinished()
265{
266 const nsecs_t now = systemTime();
267 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700268 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700269 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700270 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271}
272
273void SurfaceFlinger::onFirstRef()
274{
275 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
276
277 // Wait for the main thread to be done with its initialization
278 mReadyToRunBarrier.wait();
279}
280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281static inline uint16_t pack565(int r, int g, int b) {
282 return (r<<11)|(g<<5)|b;
283}
284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285status_t SurfaceFlinger::readyToRun()
286{
287 LOGI( "SurfaceFlinger's main thread ready to run. "
288 "Initializing graphics H/W...");
289
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 // we only support one display currently
291 int dpy = 0;
292
293 {
294 // initialize the main display
295 GraphicPlane& plane(graphicPlane(dpy));
296 DisplayHardware* const hw = new DisplayHardware(this, dpy);
297 plane.setDisplayHardware(hw);
298 }
299
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700300 // create the shared control-block
301 mServerHeap = new MemoryHeapBase(4096,
302 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
303 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
304
305 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
306 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
307
308 new(mServerCblk) surface_flinger_cblk_t;
309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 // initialize primary screen
311 // (other display should be initialized in the same manner, but
312 // asynchronously, as they could come and go. None of this is supported
313 // yet).
314 const GraphicPlane& plane(graphicPlane(dpy));
315 const DisplayHardware& hw = plane.displayHardware();
316 const uint32_t w = hw.getWidth();
317 const uint32_t h = hw.getHeight();
318 const uint32_t f = hw.getFormat();
319 hw.makeCurrent();
320
321 // initialize the shared control block
322 mServerCblk->connected |= 1<<dpy;
323 display_cblk_t* dcblk = mServerCblk->displays + dpy;
324 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800325 dcblk->w = plane.getWidth();
326 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 dcblk->format = f;
328 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
329 dcblk->xdpi = hw.getDpiX();
330 dcblk->ydpi = hw.getDpiY();
331 dcblk->fps = hw.getRefreshRate();
332 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333
334 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
336 glPixelStorei(GL_PACK_ALIGNMENT, 4);
337 glEnableClientState(GL_VERTEX_ARRAY);
338 glEnable(GL_SCISSOR_TEST);
339 glShadeModel(GL_FLAT);
340 glDisable(GL_DITHER);
341 glDisable(GL_CULL_FACE);
342
343 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
344 const uint16_t g1 = pack565(0x17,0x2f,0x17);
345 const uint16_t textureData[4] = { g0, g1, g1, g0 };
346 glGenTextures(1, &mWormholeTexName);
347 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
350 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
352 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
353 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
354
355 glViewport(0, 0, w, h);
356 glMatrixMode(GL_PROJECTION);
357 glLoadIdentity();
358 glOrthof(0, w, h, 0, 0, 1);
359
360 LayerDim::initDimmer(this, w, h);
361
362 mReadyToRunBarrier.open();
363
364 /*
365 * We're now ready to accept clients...
366 */
367
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700368 // start boot animation
369 property_set("ctl.start", "bootanim");
370
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 return NO_ERROR;
372}
373
374// ----------------------------------------------------------------------------
375#if 0
376#pragma mark -
377#pragma mark Events Handler
378#endif
379
380void SurfaceFlinger::waitForEvent()
381{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700382 while (true) {
383 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800384 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700385 if (UNLIKELY(isFrozen())) {
386 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700387 const nsecs_t now = systemTime();
388 if (mFreezeDisplayTime == 0) {
389 mFreezeDisplayTime = now;
390 }
391 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
392 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700393 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700394
Mathias Agopianbb641242010-05-18 17:06:55 -0700395 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800396
397 // see if we timed out
398 if (isFrozen()) {
399 const nsecs_t now = systemTime();
400 nsecs_t frozenTime = (now - mFreezeDisplayTime);
401 if (frozenTime >= freezeDisplayTimeout) {
402 // we timed out and are still frozen
403 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
404 mFreezeDisplay, mFreezeCount);
405 mFreezeDisplayTime = 0;
406 mFreezeCount = 0;
407 mFreezeDisplay = false;
408 }
409 }
410
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700411 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700412 switch (msg->what) {
413 case MessageQueue::INVALIDATE:
414 // invalidate message, just return to the main loop
415 return;
416 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 }
419}
420
421void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700422 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423}
424
425void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700426 // this is the IPC call
427 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428}
429
Mathias Agopianbb641242010-05-18 17:06:55 -0700430status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
431 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432{
Mathias Agopianbb641242010-05-18 17:06:55 -0700433 return mEventQueue.postMessage(msg, reltime, flags);
434}
435
436status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
437 nsecs_t reltime, uint32_t flags)
438{
439 status_t res = mEventQueue.postMessage(msg, reltime, flags);
440 if (res == NO_ERROR) {
441 msg->wait();
442 }
443 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444}
445
446// ----------------------------------------------------------------------------
447#if 0
448#pragma mark -
449#pragma mark Main loop
450#endif
451
452bool SurfaceFlinger::threadLoop()
453{
454 waitForEvent();
455
456 // check for transactions
457 if (UNLIKELY(mConsoleSignals)) {
458 handleConsoleEvents();
459 }
460
461 if (LIKELY(mTransactionCount == 0)) {
462 // if we're in a global transaction, don't do anything.
463 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
464 uint32_t transactionFlags = getTransactionFlags(mask);
465 if (LIKELY(transactionFlags)) {
466 handleTransaction(transactionFlags);
467 }
468 }
469
470 // post surfaces (if needed)
471 handlePageFlip();
472
473 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700474 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 // repaint the framebuffer (if needed)
476 handleRepaint();
477
Mathias Agopian74faca22009-09-17 16:18:16 -0700478 // inform the h/w that we're done compositing
479 hw.compositionComplete();
480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 // release the clients before we flip ('cause flip might block)
482 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484 postFramebuffer();
485 } else {
486 // pretend we did the post
487 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 usleep(16667); // 60 fps period
489 }
490 return true;
491}
492
493void SurfaceFlinger::postFramebuffer()
494{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 if (!mInvalidRegion.isEmpty()) {
496 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700497 const nsecs_t now = systemTime();
498 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700500 mLastSwapBufferTime = systemTime() - now;
501 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 }
504}
505
506void SurfaceFlinger::handleConsoleEvents()
507{
508 // something to do with the console
509 const DisplayHardware& hw = graphicPlane(0).displayHardware();
510
511 int what = android_atomic_and(0, &mConsoleSignals);
512 if (what & eConsoleAcquired) {
513 hw.acquireScreen();
514 }
515
516 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700517 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 hw.releaseScreen();
520 }
521
522 if (what & eConsoleReleased) {
523 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 hw.releaseScreen();
525 } else {
526 mDeferReleaseConsole = true;
527 }
528 }
529
530 mDirtyRegion.set(hw.bounds());
531}
532
533void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
534{
Mathias Agopian3d579642009-06-04 18:46:21 -0700535 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536
Mathias Agopian3d579642009-06-04 18:46:21 -0700537 { // scope for the lock
538 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700539 const nsecs_t now = systemTime();
540 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700541 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700542 mLastTransactionTime = systemTime() - now;
543 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700544 }
545
546 // do this without lock held
547 const size_t count = ditchedLayers.size();
548 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700549 if (ditchedLayers[i] != 0) {
550 //LOGD("ditching layer %p", ditchedLayers[i].get());
551 ditchedLayers[i]->ditch();
552 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700553 }
554}
555
556void SurfaceFlinger::handleTransactionLocked(
557 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
558{
559 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 const size_t count = currentLayers.size();
561
562 /*
563 * Traversal of the children
564 * (perform the transaction for each of them if needed)
565 */
566
567 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
568 if (layersNeedTransaction) {
569 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700570 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
572 if (!trFlags) continue;
573
574 const uint32_t flags = layer->doTransaction(0);
575 if (flags & Layer::eVisibleRegion)
576 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
578 }
579
580 /*
581 * Perform our own transaction if needed
582 */
583
584 if (transactionFlags & eTransactionNeeded) {
585 if (mCurrentState.orientation != mDrawingState.orientation) {
586 // the orientation has changed, recompute all visible regions
587 // and invalidate everything.
588
589 const int dpy = 0;
590 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700591 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 GraphicPlane& plane(graphicPlane(dpy));
593 plane.setOrientation(orientation);
594
595 // update the shared control block
596 const DisplayHardware& hw(plane.displayHardware());
597 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
598 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800599 dcblk->w = plane.getWidth();
600 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601
602 mVisibleRegionsDirty = true;
603 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 }
605
606 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
607 // freezing or unfreezing the display -> trigger animation if needed
608 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800609 if (mFreezeDisplay)
610 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 }
612
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700613 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
614 // layers have been added
615 mVisibleRegionsDirty = true;
616 }
617
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 // some layers might have been removed, so
619 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700621 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700623 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700624 const size_t count = previousLayers.size();
625 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700626 const sp<LayerBase>& layer(previousLayers[i]);
627 if (currentLayers.indexOf( layer ) < 0) {
628 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700629 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700630 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700631 }
632 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 }
635
636 commitTransaction();
637}
638
639sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
640{
641 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
642}
643
644void SurfaceFlinger::computeVisibleRegions(
645 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
646{
647 const GraphicPlane& plane(graphicPlane(0));
648 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700649 const DisplayHardware& hw(plane.displayHardware());
650 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651
652 Region aboveOpaqueLayers;
653 Region aboveCoveredLayers;
654 Region dirty;
655
656 bool secureFrameBuffer = false;
657
658 size_t i = currentLayers.size();
659 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700660 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 layer->validateVisibility(planeTransform);
662
663 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700664 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
Mathias Agopianab028732010-03-16 16:41:46 -0700666 /*
667 * opaqueRegion: area of a surface that is fully opaque.
668 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700670
671 /*
672 * visibleRegion: area of a surface that is visible on screen
673 * and not fully transparent. This is essentially the layer's
674 * footprint minus the opaque regions above it.
675 * Areas covered by a translucent surface are considered visible.
676 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700678
679 /*
680 * coveredRegion: area of a surface that is covered by all
681 * visible regions above it (which includes the translucent areas).
682 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700684
685
686 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700687 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700689 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700691 visibleRegion.andSelf(screenRegion);
692 if (!visibleRegion.isEmpty()) {
693 // Remove the transparent area from the visible region
694 if (translucent) {
695 visibleRegion.subtractSelf(layer->transparentRegionScreen);
696 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697
Mathias Agopianab028732010-03-16 16:41:46 -0700698 // compute the opaque region
699 const int32_t layerOrientation = layer->getOrientation();
700 if (s.alpha==255 && !translucent &&
701 ((layerOrientation & Transform::ROT_INVALID) == false)) {
702 // the opaque region is the layer's footprint
703 opaqueRegion = visibleRegion;
704 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 }
706 }
707
Mathias Agopianab028732010-03-16 16:41:46 -0700708 // Clip the covered region to the visible region
709 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
710
711 // Update aboveCoveredLayers for next (lower) layer
712 aboveCoveredLayers.orSelf(visibleRegion);
713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 // subtract the opaque region covered by the layers above us
715 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716
717 // compute this layer's dirty region
718 if (layer->contentDirty) {
719 // we need to invalidate the whole region
720 dirty = visibleRegion;
721 // as well, as the old visible region
722 dirty.orSelf(layer->visibleRegionScreen);
723 layer->contentDirty = false;
724 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700725 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700726 * the exposed region consists of two components:
727 * 1) what's VISIBLE now and was COVERED before
728 * 2) what's EXPOSED now less what was EXPOSED before
729 *
730 * note that (1) is conservative, we start with the whole
731 * visible region but only keep what used to be covered by
732 * something -- which mean it may have been exposed.
733 *
734 * (2) handles areas that were not covered by anything but got
735 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700736 */
Mathias Agopianab028732010-03-16 16:41:46 -0700737 const Region newExposed = visibleRegion - coveredRegion;
738 const Region oldVisibleRegion = layer->visibleRegionScreen;
739 const Region oldCoveredRegion = layer->coveredRegionScreen;
740 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
741 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 }
743 dirty.subtractSelf(aboveOpaqueLayers);
744
745 // accumulate to the screen dirty region
746 dirtyRegion.orSelf(dirty);
747
Mathias Agopianab028732010-03-16 16:41:46 -0700748 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750
751 // Store the visible region is screen space
752 layer->setVisibleRegion(visibleRegion);
753 layer->setCoveredRegion(coveredRegion);
754
Mathias Agopian97011222009-07-28 10:57:27 -0700755 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 if (layer->isSecure() && !visibleRegion.isEmpty()) {
757 secureFrameBuffer = true;
758 }
759 }
760
Mathias Agopian97011222009-07-28 10:57:27 -0700761 // invalidate the areas where a layer was removed
762 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
763 mDirtyRegionRemovedLayer.clear();
764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 mSecureFrameBuffer = secureFrameBuffer;
766 opaqueRegion = aboveOpaqueLayers;
767}
768
769
770void SurfaceFlinger::commitTransaction()
771{
772 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700773 mResizeTransationPending = false;
774 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775}
776
777void SurfaceFlinger::handlePageFlip()
778{
779 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700780 LayerVector& currentLayers = const_cast<LayerVector&>(
781 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 visibleRegions |= lockPageFlip(currentLayers);
783
784 const DisplayHardware& hw = graphicPlane(0).displayHardware();
785 const Region screenRegion(hw.bounds());
786 if (visibleRegions) {
787 Region opaqueRegion;
788 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
789 mWormholeRegion = screenRegion.subtract(opaqueRegion);
790 mVisibleRegionsDirty = false;
791 }
792
793 unlockPageFlip(currentLayers);
794 mDirtyRegion.andSelf(screenRegion);
795}
796
797bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
798{
799 bool recomputeVisibleRegions = false;
800 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700801 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700803 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 layer->lockPageFlip(recomputeVisibleRegions);
805 }
806 return recomputeVisibleRegions;
807}
808
809void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
810{
811 const GraphicPlane& plane(graphicPlane(0));
812 const Transform& planeTransform(plane.transform());
813 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700814 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700816 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 layer->unlockPageFlip(planeTransform, mDirtyRegion);
818 }
819}
820
Mathias Agopianb8a55602009-06-26 19:06:36 -0700821
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822void SurfaceFlinger::handleRepaint()
823{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700824 // compute the invalid region
825 mInvalidRegion.orSelf(mDirtyRegion);
826 if (mInvalidRegion.isEmpty()) {
827 // nothing to do
828 return;
829 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
831 if (UNLIKELY(mDebugRegion)) {
832 debugFlashRegions();
833 }
834
Mathias Agopianb8a55602009-06-26 19:06:36 -0700835 // set the frame buffer
836 const DisplayHardware& hw(graphicPlane(0).displayHardware());
837 glMatrixMode(GL_MODELVIEW);
838 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839
840 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700841 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
842 (flags & DisplayHardware::BUFFER_PRESERVED))
843 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700844 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
845 // takes a rectangle, we must make sure to update that whole
846 // rectangle in that case
847 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700848 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700849 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 mDirtyRegion.set(mInvalidRegion.bounds());
851 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700852 // in the BUFFER_PRESERVED case, obviously, we can update only
853 // what's needed and nothing more.
854 // NOTE: this is NOT a common case, as preserving the backbuffer
855 // is costly and usually involves copying the whole update back.
856 }
857 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700858 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // We need to redraw the rectangle that will be updated
860 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700861 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700862 // rectangle instead of a region (see DisplayHardware::flip())
863 mDirtyRegion.set(mInvalidRegion.bounds());
864 } else {
865 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866 mDirtyRegion.set(hw.bounds());
867 mInvalidRegion = mDirtyRegion;
868 }
869 }
870
871 // compose all surfaces
872 composeSurfaces(mDirtyRegion);
873
874 // clear the dirty regions
875 mDirtyRegion.clear();
876}
877
878void SurfaceFlinger::composeSurfaces(const Region& dirty)
879{
880 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
881 // should never happen unless the window manager has a bug
882 // draw something...
883 drawWormhole();
884 }
885 const SurfaceFlinger& flinger(*this);
886 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
887 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700888 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700890 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 const Region& visibleRegion(layer->visibleRegionScreen);
892 if (!visibleRegion.isEmpty()) {
893 const Region clip(dirty.intersect(visibleRegion));
894 if (!clip.isEmpty()) {
895 layer->draw(clip);
896 }
897 }
898 }
899}
900
901void SurfaceFlinger::unlockClients()
902{
903 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
904 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700905 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700907 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 layer->finishPageFlip();
909 }
910}
911
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912void SurfaceFlinger::debugFlashRegions()
913{
Mathias Agopian0a917752010-06-14 21:20:00 -0700914 const DisplayHardware& hw(graphicPlane(0).displayHardware());
915 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700916
Mathias Agopian0a917752010-06-14 21:20:00 -0700917 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
918 (flags & DisplayHardware::BUFFER_PRESERVED))) {
919 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
920 mDirtyRegion.bounds() : hw.bounds());
921 composeSurfaces(repaint);
922 }
923
924 TextureManager::deactivateTextures();
925
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 glDisable(GL_BLEND);
927 glDisable(GL_DITHER);
928 glDisable(GL_SCISSOR_TEST);
929
Mathias Agopian0926f502009-05-04 14:17:04 -0700930 static int toggle = 0;
931 toggle = 1 - toggle;
932 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700933 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700934 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700935 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700936 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937
Mathias Agopian20f68782009-05-11 00:03:41 -0700938 Region::const_iterator it = mDirtyRegion.begin();
939 Region::const_iterator const end = mDirtyRegion.end();
940 while (it != end) {
941 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 GLfloat vertices[][2] = {
943 { r.left, r.top },
944 { r.left, r.bottom },
945 { r.right, r.bottom },
946 { r.right, r.top }
947 };
948 glVertexPointer(2, GL_FLOAT, 0, vertices);
949 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
950 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700951
Mathias Agopianb8a55602009-06-26 19:06:36 -0700952 if (mInvalidRegion.isEmpty()) {
953 mDirtyRegion.dump("mDirtyRegion");
954 mInvalidRegion.dump("mInvalidRegion");
955 }
956 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957
958 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700959 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960
961 glEnable(GL_SCISSOR_TEST);
962 //mDirtyRegion.dump("mDirtyRegion");
963}
964
965void SurfaceFlinger::drawWormhole() const
966{
967 const Region region(mWormholeRegion.intersect(mDirtyRegion));
968 if (region.isEmpty())
969 return;
970
971 const DisplayHardware& hw(graphicPlane(0).displayHardware());
972 const int32_t width = hw.getWidth();
973 const int32_t height = hw.getHeight();
974
975 glDisable(GL_BLEND);
976 glDisable(GL_DITHER);
977
978 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700979 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700980 Region::const_iterator it = region.begin();
981 Region::const_iterator const end = region.end();
982 while (it != end) {
983 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 const GLint sy = height - (r.top + r.height());
985 glScissor(r.left, sy, r.width(), r.height());
986 glClear(GL_COLOR_BUFFER_BIT);
987 }
988 } else {
989 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
990 { width, height }, { 0, height } };
991 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
992 glVertexPointer(2, GL_SHORT, 0, vertices);
993 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
994 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian0a917752010-06-14 21:20:00 -0700995#if defined(GL_OES_texture_external)
Andreas Hubere049a952010-06-25 09:25:19 -0700996 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopian0a917752010-06-14 21:20:00 -0700997#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 glEnable(GL_TEXTURE_2D);
999 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1000 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1001 glMatrixMode(GL_TEXTURE);
1002 glLoadIdentity();
1003 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001004 Region::const_iterator it = region.begin();
1005 Region::const_iterator const end = region.end();
1006 while (it != end) {
1007 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 const GLint sy = height - (r.top + r.height());
1009 glScissor(r.left, sy, r.width(), r.height());
1010 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1011 }
1012 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1013 }
1014}
1015
1016void SurfaceFlinger::debugShowFPS() const
1017{
1018 static int mFrameCount;
1019 static int mLastFrameCount = 0;
1020 static nsecs_t mLastFpsTime = 0;
1021 static float mFps = 0;
1022 mFrameCount++;
1023 nsecs_t now = systemTime();
1024 nsecs_t diff = now - mLastFpsTime;
1025 if (diff > ms2ns(250)) {
1026 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1027 mLastFpsTime = now;
1028 mLastFrameCount = mFrameCount;
1029 }
1030 // XXX: mFPS has the value we want
1031 }
1032
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001033status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034{
1035 Mutex::Autolock _l(mStateLock);
1036 addLayer_l(layer);
1037 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1038 return NO_ERROR;
1039}
1040
Mathias Agopian96f08192010-06-02 23:28:45 -07001041status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1042{
1043 ssize_t i = mCurrentState.layersSortedByZ.add(
1044 layer, &LayerBase::compareCurrentStateZ);
1045 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1046}
1047
1048ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1049 const sp<LayerBaseClient>& lbc)
1050{
1051 Mutex::Autolock _l(mStateLock);
1052
1053 // attach this layer to the client
1054 ssize_t name = client->attachLayer(lbc);
1055
1056 // add this layer to the current state list
1057 addLayer_l(lbc);
1058
1059 return name;
1060}
1061
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001062status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063{
1064 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001065 status_t err = purgatorizeLayer_l(layer);
1066 if (err == NO_ERROR)
1067 setTransactionFlags(eTransactionNeeded);
1068 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001073 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1074 if (lbc != 0) {
1075 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1076 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1078 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001079 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 return NO_ERROR;
1081 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001082 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083}
1084
Mathias Agopian9a112062009-04-17 19:36:26 -07001085status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1086{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001087 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001088 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001089
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001090 layerBase->onRemoved();
1091
Mathias Agopian3d579642009-06-04 18:46:21 -07001092 // it's possible that we don't find a layer, because it might
1093 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001094 // from the user because there is a race between Client::destroySurface(),
1095 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001096 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1097}
1098
Mathias Agopian96f08192010-06-02 23:28:45 -07001099status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100{
Mathias Agopian96f08192010-06-02 23:28:45 -07001101 layer->forceVisibilityTransaction();
1102 setTransactionFlags(eTraversalNeeded);
1103 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104}
1105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1107{
1108 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1109}
1110
Mathias Agopianbb641242010-05-18 17:06:55 -07001111uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112{
1113 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1114 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001115 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 }
1117 return old;
1118}
1119
1120void SurfaceFlinger::openGlobalTransaction()
1121{
1122 android_atomic_inc(&mTransactionCount);
1123}
1124
1125void SurfaceFlinger::closeGlobalTransaction()
1126{
1127 if (android_atomic_dec(&mTransactionCount) == 1) {
1128 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001129
1130 // if there is a transaction with a resize, wait for it to
1131 // take effect before returning.
1132 Mutex::Autolock _l(mStateLock);
1133 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001134 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1135 if (CC_UNLIKELY(err != NO_ERROR)) {
1136 // just in case something goes wrong in SF, return to the
1137 // called after a few seconds.
1138 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1139 mResizeTransationPending = false;
1140 break;
1141 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001142 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 }
1144}
1145
1146status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1147{
1148 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1149 return BAD_VALUE;
1150
1151 Mutex::Autolock _l(mStateLock);
1152 mCurrentState.freezeDisplay = 1;
1153 setTransactionFlags(eTransactionNeeded);
1154
1155 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001156 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 return NO_ERROR;
1158}
1159
1160status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1161{
1162 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1163 return BAD_VALUE;
1164
1165 Mutex::Autolock _l(mStateLock);
1166 mCurrentState.freezeDisplay = 0;
1167 setTransactionFlags(eTransactionNeeded);
1168
1169 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001170 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 return NO_ERROR;
1172}
1173
Mathias Agopianc08731e2009-03-27 18:11:38 -07001174int SurfaceFlinger::setOrientation(DisplayID dpy,
1175 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176{
1177 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1178 return BAD_VALUE;
1179
1180 Mutex::Autolock _l(mStateLock);
1181 if (mCurrentState.orientation != orientation) {
1182 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001183 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 mCurrentState.orientation = orientation;
1185 setTransactionFlags(eTransactionNeeded);
1186 mTransactionCV.wait(mStateLock);
1187 } else {
1188 orientation = BAD_VALUE;
1189 }
1190 }
1191 return orientation;
1192}
1193
Mathias Agopian96f08192010-06-02 23:28:45 -07001194sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001195 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1197 uint32_t flags)
1198{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001199 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001201
1202 if (int32_t(w|h) < 0) {
1203 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1204 int(w), int(h));
1205 return surfaceHandle;
1206 }
1207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001209 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 switch (flags & eFXSurfaceMask) {
1211 case eFXSurfaceNormal:
1212 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001213 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001215 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1216 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 }
1218 break;
1219 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001220 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 break;
1222 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 break;
1225 }
1226
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001227 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001228 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001229 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001230 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001233 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001234 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001235 params->identity = surfaceHandle->getIdentity();
1236 params->width = w;
1237 params->height = h;
1238 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001239 if (normalLayer != 0) {
1240 Mutex::Autolock _l(mStateLock);
1241 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1242 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001243 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001244
Mathias Agopian96f08192010-06-02 23:28:45 -07001245 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247
1248 return surfaceHandle;
1249}
1250
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001251sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001252 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001253 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001254 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255{
1256 // initialize the surfaces
1257 switch (format) { // TODO: take h/w into account
1258 case PIXEL_FORMAT_TRANSPARENT:
1259 case PIXEL_FORMAT_TRANSLUCENT:
1260 format = PIXEL_FORMAT_RGBA_8888;
1261 break;
1262 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001263 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 break;
1265 }
1266
Mathias Agopian96f08192010-06-02 23:28:45 -07001267 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001268 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001269 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001271 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 }
1273 return layer;
1274}
1275
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001276sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001277 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001278 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279{
Mathias Agopian96f08192010-06-02 23:28:45 -07001280 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 return layer;
1283}
1284
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001285sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001286 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001287 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288{
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 return layer;
1292}
1293
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001294sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001295 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001296 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297{
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 return layer;
1301}
1302
Mathias Agopian96f08192010-06-02 23:28:45 -07001303status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304{
Mathias Agopian9a112062009-04-17 19:36:26 -07001305 /*
1306 * called by the window manager, when a surface should be marked for
1307 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001308 *
1309 * The surface is removed from the current and drawing lists, but placed
1310 * in the purgatory queue, so it's not destroyed right-away (we need
1311 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001312 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001313
Mathias Agopian48d819a2009-09-10 19:41:18 -07001314 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001315 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001316 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001317 if (layer != 0) {
1318 err = purgatorizeLayer_l(layer);
1319 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001320 setTransactionFlags(eTransactionNeeded);
1321 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001322 }
1323 return err;
1324}
1325
1326status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1327{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001328 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001329
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001330 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001331 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001332 sp<LayerBaseClient> layer;
1333 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001334 MessageDestroySurface(
1335 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1336 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001337 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001338 sp<LayerBaseClient> l(layer);
1339 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001340 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001341 /*
1342 * remove the layer from the current list -- chances are that it's
1343 * not in the list anyway, because it should have been removed
1344 * already upon request of the client (eg: window manager).
1345 * However, a buggy client could have not done that.
1346 * Since we know we don't have any more clients, we don't need
1347 * to use the purgatory.
1348 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001349 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001350 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1351 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001352 return true;
1353 }
1354 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001355
Mathias Agopianbb641242010-05-18 17:06:55 -07001356 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 return NO_ERROR;
1358}
1359
1360status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001361 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 int32_t count,
1363 const layer_state_t* states)
1364{
1365 Mutex::Autolock _l(mStateLock);
1366 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001368 const layer_state_t& s(states[i]);
1369 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001370 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 if (what & ePositionChanged) {
1373 if (layer->setPosition(s.x, s.y))
1374 flags |= eTraversalNeeded;
1375 }
1376 if (what & eLayerChanged) {
1377 if (layer->setLayer(s.z)) {
1378 mCurrentState.layersSortedByZ.reorder(
1379 layer, &Layer::compareCurrentStateZ);
1380 // we need traversal (state changed)
1381 // AND transaction (list changed)
1382 flags |= eTransactionNeeded|eTraversalNeeded;
1383 }
1384 }
1385 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001386 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001388 mResizeTransationPending = true;
1389 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 }
1391 if (what & eAlphaChanged) {
1392 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1393 flags |= eTraversalNeeded;
1394 }
1395 if (what & eMatrixChanged) {
1396 if (layer->setMatrix(s.matrix))
1397 flags |= eTraversalNeeded;
1398 }
1399 if (what & eTransparentRegionChanged) {
1400 if (layer->setTransparentRegionHint(s.transparentRegion))
1401 flags |= eTraversalNeeded;
1402 }
1403 if (what & eVisibilityChanged) {
1404 if (layer->setFlags(s.flags, s.mask))
1405 flags |= eTraversalNeeded;
1406 }
1407 }
1408 }
1409 if (flags) {
1410 setTransactionFlags(flags);
1411 }
1412 return NO_ERROR;
1413}
1414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415void SurfaceFlinger::screenReleased(int dpy)
1416{
1417 // this may be called by a signal handler, we can't do too much in here
1418 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1419 signalEvent();
1420}
1421
1422void SurfaceFlinger::screenAcquired(int dpy)
1423{
1424 // this may be called by a signal handler, we can't do too much in here
1425 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1426 signalEvent();
1427}
1428
1429status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1430{
1431 const size_t SIZE = 1024;
1432 char buffer[SIZE];
1433 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001434 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 snprintf(buffer, SIZE, "Permission Denial: "
1436 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1437 IPCThreadState::self()->getCallingPid(),
1438 IPCThreadState::self()->getCallingUid());
1439 result.append(buffer);
1440 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001441
1442 // figure out if we're stuck somewhere
1443 const nsecs_t now = systemTime();
1444 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1445 const nsecs_t inTransaction(mDebugInTransaction);
1446 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1447 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1448
1449 // Try to get the main lock, but don't insist if we can't
1450 // (this would indicate SF is stuck, but we want to be able to
1451 // print something in dumpsys).
1452 int retry = 3;
1453 while (mStateLock.tryLock()<0 && --retry>=0) {
1454 usleep(1000000);
1455 }
1456 const bool locked(retry >= 0);
1457 if (!locked) {
1458 snprintf(buffer, SIZE,
1459 "SurfaceFlinger appears to be unresponsive, "
1460 "dumping anyways (no locks held)\n");
1461 result.append(buffer);
1462 }
1463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1465 const size_t count = currentLayers.size();
1466 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001467 const sp<LayerBase>& layer(currentLayers[i]);
1468 layer->dump(result, buffer, SIZE);
1469 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 s.transparentRegion.dump(result, "transparentRegion");
1471 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1472 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1473 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001474
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 mWormholeRegion.dump(result, "WormholeRegion");
1476 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1477 snprintf(buffer, SIZE,
1478 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1479 mFreezeDisplay?"yes":"no", mFreezeCount,
1480 mCurrentState.orientation, hw.canDraw());
1481 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001482 snprintf(buffer, SIZE,
1483 " last eglSwapBuffers() time: %f us\n"
1484 " last transaction time : %f us\n",
1485 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1486 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001487
Mathias Agopian9795c422009-08-26 16:36:26 -07001488 if (inSwapBuffersDuration || !locked) {
1489 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1490 inSwapBuffersDuration/1000.0);
1491 result.append(buffer);
1492 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001493
Mathias Agopian9795c422009-08-26 16:36:26 -07001494 if (inTransactionDuration || !locked) {
1495 snprintf(buffer, SIZE, " transaction time: %f us\n",
1496 inTransactionDuration/1000.0);
1497 result.append(buffer);
1498 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001499
Mathias Agopian3330b202009-10-05 17:07:12 -07001500 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001501 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001502
1503 if (locked) {
1504 mStateLock.unlock();
1505 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 }
1507 write(fd, result.string(), result.size());
1508 return NO_ERROR;
1509}
1510
1511status_t SurfaceFlinger::onTransact(
1512 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1513{
1514 switch (code) {
1515 case CREATE_CONNECTION:
1516 case OPEN_GLOBAL_TRANSACTION:
1517 case CLOSE_GLOBAL_TRANSACTION:
1518 case SET_ORIENTATION:
1519 case FREEZE_DISPLAY:
1520 case UNFREEZE_DISPLAY:
1521 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 {
1523 // codes that require permission check
1524 IPCThreadState* ipc = IPCThreadState::self();
1525 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001526 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001527 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1528 LOGE("Permission Denial: "
1529 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1530 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 }
1532 }
1533 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1535 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001536 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001537 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1538 IPCThreadState* ipc = IPCThreadState::self();
1539 const int pid = ipc->getCallingPid();
1540 const int uid = ipc->getCallingUid();
1541 LOGE("Permission Denial: "
1542 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 return PERMISSION_DENIED;
1544 }
1545 int n;
1546 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001547 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001549 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550 return NO_ERROR;
1551 case 1002: // SHOW_UPDATES
1552 n = data.readInt32();
1553 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1554 return NO_ERROR;
1555 case 1003: // SHOW_BACKGROUND
1556 n = data.readInt32();
1557 mDebugBackground = n ? 1 : 0;
1558 return NO_ERROR;
1559 case 1004:{ // repaint everything
1560 Mutex::Autolock _l(mStateLock);
1561 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1562 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1563 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001564 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001566 case 1005:{ // force transaction
1567 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1568 return NO_ERROR;
1569 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 case 1007: // set mFreezeCount
1571 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001572 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 return NO_ERROR;
1574 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001575 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 reply->writeInt32(0);
1577 reply->writeInt32(mDebugRegion);
1578 reply->writeInt32(mDebugBackground);
1579 return NO_ERROR;
1580 case 1013: {
1581 Mutex::Autolock _l(mStateLock);
1582 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1583 reply->writeInt32(hw.getPageFlipCount());
1584 }
1585 return NO_ERROR;
1586 }
1587 }
1588 return err;
1589}
1590
1591// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001592
1593sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1594{
1595 sp<Layer> result;
1596 Mutex::Autolock _l(mStateLock);
1597 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1598 return result;
1599}
1600
1601// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602
Mathias Agopian96f08192010-06-02 23:28:45 -07001603Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001604 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606}
1607
Mathias Agopian96f08192010-06-02 23:28:45 -07001608Client::~Client()
1609{
Mathias Agopian96f08192010-06-02 23:28:45 -07001610 const size_t count = mLayers.size();
1611 for (size_t i=0 ; i<count ; i++) {
1612 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1613 if (layer != 0) {
1614 mFlinger->removeLayer(layer);
1615 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 }
1617}
1618
Mathias Agopian96f08192010-06-02 23:28:45 -07001619status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001620 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001622
Mathias Agopian96f08192010-06-02 23:28:45 -07001623ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1624{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001625 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001626 mLayers.add(name, layer);
1627 return name;
1628}
1629
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001630void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001631{
Mathias Agopian96f08192010-06-02 23:28:45 -07001632 // we do a linear search here, because this doesn't happen often
1633 const size_t count = mLayers.size();
1634 for (size_t i=0 ; i<count ; i++) {
1635 if (mLayers.valueAt(i) == layer) {
1636 mLayers.removeItemsAt(i, 1);
1637 break;
1638 }
1639 }
1640}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001641sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1642 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001643 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1644 if (layer != 0) {
1645 lbc = layer.promote();
1646 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001647 }
1648 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649}
1650
Mathias Agopian96f08192010-06-02 23:28:45 -07001651sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001652 return 0;
1653}
1654ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1655 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656}
Mathias Agopian96f08192010-06-02 23:28:45 -07001657sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001658 ISurfaceComposerClient::surface_data_t* params, int pid,
1659 const String8& name,
1660 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 uint32_t flags)
1662{
Mathias Agopian96f08192010-06-02 23:28:45 -07001663 return mFlinger->createSurface(this, pid, name, params,
1664 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665}
Mathias Agopian96f08192010-06-02 23:28:45 -07001666status_t Client::destroySurface(SurfaceID sid) {
1667 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668}
Mathias Agopian96f08192010-06-02 23:28:45 -07001669status_t Client::setState(int32_t count, const layer_state_t* states) {
1670 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671}
1672
1673// ---------------------------------------------------------------------------
1674
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001675UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1676 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1677{
1678 const int pgsize = getpagesize();
1679 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1680
1681 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1682 "SurfaceFlinger Client control-block");
1683
1684 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1685 if (ctrlblk) { // construct the shared structure in-place.
1686 new(ctrlblk) SharedClient;
1687 }
1688}
1689
1690UserClient::~UserClient()
1691{
1692 if (ctrlblk) {
1693 ctrlblk->~SharedClient(); // destroy our shared-structure.
1694 }
1695
1696 /*
1697 * When a UserClient dies, it's unclear what to do exactly.
1698 * We could go ahead and destroy all surfaces linked to that client
1699 * however, it wouldn't be fair to the main Client
1700 * (usually the the window-manager), which might want to re-target
1701 * the layer to another UserClient.
1702 * I think the best is to do nothing, or not much; in most cases the
1703 * WM itself will go ahead and clean things up when it detects a client of
1704 * his has died.
1705 * The remaining question is what to display? currently we keep
1706 * just keep the current buffer.
1707 */
1708}
1709
1710status_t UserClient::initCheck() const {
1711 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1712}
1713
1714void UserClient::detachLayer(const Layer* layer)
1715{
1716 int32_t name = layer->getToken();
1717 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001718 int32_t mask = 1LU<<name;
1719 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1720 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1721 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001722 }
1723}
1724
1725sp<IMemoryHeap> UserClient::getControlBlock() const {
1726 return mCblkHeap;
1727}
1728
1729ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1730{
1731 int32_t name = NAME_NOT_FOUND;
1732 sp<Layer> layer(mFlinger->getLayer(sur));
1733 if (layer == 0) return name;
1734
Mathias Agopian579b3f82010-06-08 19:54:15 -07001735 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001736 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07001737 if ((name >= 0) && (layer->getClient() == this))
1738 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001739
1740 name = 0;
1741 do {
1742 int32_t mask = 1LU<<name;
1743 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1744 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001745 status_t err = layer->setToken(
1746 const_cast<UserClient*>(this), ctrlblk, name);
1747 if (err != NO_ERROR) {
1748 // free the name
1749 android_atomic_and(~mask, &mBitmap);
1750 name = err;
1751 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001752 break;
1753 }
1754 if (++name > 31)
1755 name = NO_MEMORY;
1756 } while(name >= 0);
1757
Mathias Agopian53503a92010-06-08 15:40:56 -07001758 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1759 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001760 return name;
1761}
1762
1763sp<ISurface> UserClient::createSurface(
1764 ISurfaceComposerClient::surface_data_t* params, int pid,
1765 const String8& name,
1766 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1767 uint32_t flags) {
1768 return 0;
1769}
1770status_t UserClient::destroySurface(SurfaceID sid) {
1771 return INVALID_OPERATION;
1772}
1773status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1774 return INVALID_OPERATION;
1775}
1776
1777// ---------------------------------------------------------------------------
1778
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779GraphicPlane::GraphicPlane()
1780 : mHw(0)
1781{
1782}
1783
1784GraphicPlane::~GraphicPlane() {
1785 delete mHw;
1786}
1787
1788bool GraphicPlane::initialized() const {
1789 return mHw ? true : false;
1790}
1791
Mathias Agopian2b92d892010-02-08 15:49:35 -08001792int GraphicPlane::getWidth() const {
1793 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794}
1795
Mathias Agopian2b92d892010-02-08 15:49:35 -08001796int GraphicPlane::getHeight() const {
1797 return mHeight;
1798}
1799
1800void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1801{
1802 mHw = hw;
1803
1804 // initialize the display orientation transform.
1805 // it's a constant that should come from the display driver.
1806 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1807 char property[PROPERTY_VALUE_MAX];
1808 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1809 //displayOrientation
1810 switch (atoi(property)) {
1811 case 90:
1812 displayOrientation = ISurfaceComposer::eOrientation90;
1813 break;
1814 case 270:
1815 displayOrientation = ISurfaceComposer::eOrientation270;
1816 break;
1817 }
1818 }
1819
1820 const float w = hw->getWidth();
1821 const float h = hw->getHeight();
1822 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1823 &mDisplayTransform);
1824 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1825 mDisplayWidth = h;
1826 mDisplayHeight = w;
1827 } else {
1828 mDisplayWidth = w;
1829 mDisplayHeight = h;
1830 }
1831
1832 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833}
1834
1835status_t GraphicPlane::orientationToTransfrom(
1836 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001837{
1838 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001839 switch (orientation) {
1840 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001841 flags = Transform::ROT_0;
1842 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001843 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001844 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001845 break;
1846 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001847 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 break;
1849 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001850 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 break;
1852 default:
1853 return BAD_VALUE;
1854 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001855 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001856 return NO_ERROR;
1857}
1858
1859status_t GraphicPlane::setOrientation(int orientation)
1860{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001861 // If the rotation can be handled in hardware, this is where
1862 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001863
1864 const DisplayHardware& hw(displayHardware());
1865 const float w = mDisplayWidth;
1866 const float h = mDisplayHeight;
1867 mWidth = int(w);
1868 mHeight = int(h);
1869
1870 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001871 GraphicPlane::orientationToTransfrom(orientation, w, h,
1872 &orientationTransform);
1873 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1874 mWidth = int(h);
1875 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001876 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001877
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001878 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001879 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 return NO_ERROR;
1881}
1882
1883const DisplayHardware& GraphicPlane::displayHardware() const {
1884 return *mHw;
1885}
1886
1887const Transform& GraphicPlane::transform() const {
1888 return mGlobalTransform;
1889}
1890
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001891EGLDisplay GraphicPlane::getEGLDisplay() const {
1892 return mHw->getEGLDisplay();
1893}
1894
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895// ---------------------------------------------------------------------------
1896
1897}; // namespace android