blob: e8d0d5b4ec8d9c82d558a89b6fa79afdc73dd8b6 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
240{
241 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
242 const GraphicPlane& plane(mGraphicPlanes[dpy]);
243 return plane;
244}
245
246GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
247{
248 return const_cast<GraphicPlane&>(
249 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
250}
251
252void SurfaceFlinger::bootFinished()
253{
254 const nsecs_t now = systemTime();
255 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700256 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700257 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700258 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259}
260
261void SurfaceFlinger::onFirstRef()
262{
263 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
264
265 // Wait for the main thread to be done with its initialization
266 mReadyToRunBarrier.wait();
267}
268
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269static inline uint16_t pack565(int r, int g, int b) {
270 return (r<<11)|(g<<5)|b;
271}
272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273status_t SurfaceFlinger::readyToRun()
274{
275 LOGI( "SurfaceFlinger's main thread ready to run. "
276 "Initializing graphics H/W...");
277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 // we only support one display currently
279 int dpy = 0;
280
281 {
282 // initialize the main display
283 GraphicPlane& plane(graphicPlane(dpy));
284 DisplayHardware* const hw = new DisplayHardware(this, dpy);
285 plane.setDisplayHardware(hw);
286 }
287
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700288 // create the shared control-block
289 mServerHeap = new MemoryHeapBase(4096,
290 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
291 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
292
293 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
294 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
295
296 new(mServerCblk) surface_flinger_cblk_t;
297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 // initialize primary screen
299 // (other display should be initialized in the same manner, but
300 // asynchronously, as they could come and go. None of this is supported
301 // yet).
302 const GraphicPlane& plane(graphicPlane(dpy));
303 const DisplayHardware& hw = plane.displayHardware();
304 const uint32_t w = hw.getWidth();
305 const uint32_t h = hw.getHeight();
306 const uint32_t f = hw.getFormat();
307 hw.makeCurrent();
308
309 // initialize the shared control block
310 mServerCblk->connected |= 1<<dpy;
311 display_cblk_t* dcblk = mServerCblk->displays + dpy;
312 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800313 dcblk->w = plane.getWidth();
314 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 dcblk->format = f;
316 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
317 dcblk->xdpi = hw.getDpiX();
318 dcblk->ydpi = hw.getDpiY();
319 dcblk->fps = hw.getRefreshRate();
320 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321
322 // Initialize OpenGL|ES
323 glActiveTexture(GL_TEXTURE0);
324 glBindTexture(GL_TEXTURE_2D, 0);
325 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
326 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
327 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
328 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
329 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
330 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
331 glPixelStorei(GL_PACK_ALIGNMENT, 4);
332 glEnableClientState(GL_VERTEX_ARRAY);
333 glEnable(GL_SCISSOR_TEST);
334 glShadeModel(GL_FLAT);
335 glDisable(GL_DITHER);
336 glDisable(GL_CULL_FACE);
337
338 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
339 const uint16_t g1 = pack565(0x17,0x2f,0x17);
340 const uint16_t textureData[4] = { g0, g1, g1, g0 };
341 glGenTextures(1, &mWormholeTexName);
342 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
343 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
344 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
347 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
348 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
349
350 glViewport(0, 0, w, h);
351 glMatrixMode(GL_PROJECTION);
352 glLoadIdentity();
353 glOrthof(0, w, h, 0, 0, 1);
354
355 LayerDim::initDimmer(this, w, h);
356
357 mReadyToRunBarrier.open();
358
359 /*
360 * We're now ready to accept clients...
361 */
362
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700363 // start boot animation
364 property_set("ctl.start", "bootanim");
365
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366 return NO_ERROR;
367}
368
369// ----------------------------------------------------------------------------
370#if 0
371#pragma mark -
372#pragma mark Events Handler
373#endif
374
375void SurfaceFlinger::waitForEvent()
376{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700377 while (true) {
378 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800379 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700380 if (UNLIKELY(isFrozen())) {
381 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700382 const nsecs_t now = systemTime();
383 if (mFreezeDisplayTime == 0) {
384 mFreezeDisplayTime = now;
385 }
386 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
387 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700388 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700389
Mathias Agopianbb641242010-05-18 17:06:55 -0700390 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800391
392 // see if we timed out
393 if (isFrozen()) {
394 const nsecs_t now = systemTime();
395 nsecs_t frozenTime = (now - mFreezeDisplayTime);
396 if (frozenTime >= freezeDisplayTimeout) {
397 // we timed out and are still frozen
398 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
399 mFreezeDisplay, mFreezeCount);
400 mFreezeDisplayTime = 0;
401 mFreezeCount = 0;
402 mFreezeDisplay = false;
403 }
404 }
405
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700406 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700407 switch (msg->what) {
408 case MessageQueue::INVALIDATE:
409 // invalidate message, just return to the main loop
410 return;
411 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 }
414}
415
416void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700417 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418}
419
420void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700421 // this is the IPC call
422 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423}
424
Mathias Agopianbb641242010-05-18 17:06:55 -0700425status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
426 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427{
Mathias Agopianbb641242010-05-18 17:06:55 -0700428 return mEventQueue.postMessage(msg, reltime, flags);
429}
430
431status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
432 nsecs_t reltime, uint32_t flags)
433{
434 status_t res = mEventQueue.postMessage(msg, reltime, flags);
435 if (res == NO_ERROR) {
436 msg->wait();
437 }
438 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439}
440
441// ----------------------------------------------------------------------------
442#if 0
443#pragma mark -
444#pragma mark Main loop
445#endif
446
447bool SurfaceFlinger::threadLoop()
448{
449 waitForEvent();
450
451 // check for transactions
452 if (UNLIKELY(mConsoleSignals)) {
453 handleConsoleEvents();
454 }
455
456 if (LIKELY(mTransactionCount == 0)) {
457 // if we're in a global transaction, don't do anything.
458 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
459 uint32_t transactionFlags = getTransactionFlags(mask);
460 if (LIKELY(transactionFlags)) {
461 handleTransaction(transactionFlags);
462 }
463 }
464
465 // post surfaces (if needed)
466 handlePageFlip();
467
468 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700469 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470 // repaint the framebuffer (if needed)
471 handleRepaint();
472
Mathias Agopian74faca22009-09-17 16:18:16 -0700473 // inform the h/w that we're done compositing
474 hw.compositionComplete();
475
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 // release the clients before we flip ('cause flip might block)
477 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 postFramebuffer();
480 } else {
481 // pretend we did the post
482 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 usleep(16667); // 60 fps period
484 }
485 return true;
486}
487
488void SurfaceFlinger::postFramebuffer()
489{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490 if (!mInvalidRegion.isEmpty()) {
491 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700492 const nsecs_t now = systemTime();
493 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700495 mLastSwapBufferTime = systemTime() - now;
496 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 }
499}
500
501void SurfaceFlinger::handleConsoleEvents()
502{
503 // something to do with the console
504 const DisplayHardware& hw = graphicPlane(0).displayHardware();
505
506 int what = android_atomic_and(0, &mConsoleSignals);
507 if (what & eConsoleAcquired) {
508 hw.acquireScreen();
509 }
510
511 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700512 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 hw.releaseScreen();
515 }
516
517 if (what & eConsoleReleased) {
518 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 hw.releaseScreen();
520 } else {
521 mDeferReleaseConsole = true;
522 }
523 }
524
525 mDirtyRegion.set(hw.bounds());
526}
527
528void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
529{
Mathias Agopian3d579642009-06-04 18:46:21 -0700530 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531
Mathias Agopian3d579642009-06-04 18:46:21 -0700532 { // scope for the lock
533 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700534 const nsecs_t now = systemTime();
535 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700536 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700537 mLastTransactionTime = systemTime() - now;
538 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700539 }
540
541 // do this without lock held
542 const size_t count = ditchedLayers.size();
543 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700544 if (ditchedLayers[i] != 0) {
545 //LOGD("ditching layer %p", ditchedLayers[i].get());
546 ditchedLayers[i]->ditch();
547 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700548 }
549}
550
551void SurfaceFlinger::handleTransactionLocked(
552 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
553{
554 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 const size_t count = currentLayers.size();
556
557 /*
558 * Traversal of the children
559 * (perform the transaction for each of them if needed)
560 */
561
562 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
563 if (layersNeedTransaction) {
564 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
567 if (!trFlags) continue;
568
569 const uint32_t flags = layer->doTransaction(0);
570 if (flags & Layer::eVisibleRegion)
571 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 }
573 }
574
575 /*
576 * Perform our own transaction if needed
577 */
578
579 if (transactionFlags & eTransactionNeeded) {
580 if (mCurrentState.orientation != mDrawingState.orientation) {
581 // the orientation has changed, recompute all visible regions
582 // and invalidate everything.
583
584 const int dpy = 0;
585 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700586 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 GraphicPlane& plane(graphicPlane(dpy));
588 plane.setOrientation(orientation);
589
590 // update the shared control block
591 const DisplayHardware& hw(plane.displayHardware());
592 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
593 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800594 dcblk->w = plane.getWidth();
595 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596
597 mVisibleRegionsDirty = true;
598 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 }
600
601 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
602 // freezing or unfreezing the display -> trigger animation if needed
603 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800604 if (mFreezeDisplay)
605 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 }
607
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700608 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
609 // layers have been added
610 mVisibleRegionsDirty = true;
611 }
612
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 // some layers might have been removed, so
614 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700615 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700616 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700618 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700619 const size_t count = previousLayers.size();
620 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700621 const sp<LayerBase>& layer(previousLayers[i]);
622 if (currentLayers.indexOf( layer ) < 0) {
623 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700624 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700625 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700626 }
627 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 }
630
631 commitTransaction();
632}
633
634sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
635{
636 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
637}
638
639void SurfaceFlinger::computeVisibleRegions(
640 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
641{
642 const GraphicPlane& plane(graphicPlane(0));
643 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700644 const DisplayHardware& hw(plane.displayHardware());
645 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
647 Region aboveOpaqueLayers;
648 Region aboveCoveredLayers;
649 Region dirty;
650
651 bool secureFrameBuffer = false;
652
653 size_t i = currentLayers.size();
654 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700655 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 layer->validateVisibility(planeTransform);
657
658 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700659 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660
Mathias Agopianab028732010-03-16 16:41:46 -0700661 /*
662 * opaqueRegion: area of a surface that is fully opaque.
663 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700665
666 /*
667 * visibleRegion: area of a surface that is visible on screen
668 * and not fully transparent. This is essentially the layer's
669 * footprint minus the opaque regions above it.
670 * Areas covered by a translucent surface are considered visible.
671 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700673
674 /*
675 * coveredRegion: area of a surface that is covered by all
676 * visible regions above it (which includes the translucent areas).
677 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700679
680
681 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700682 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700684 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700686 visibleRegion.andSelf(screenRegion);
687 if (!visibleRegion.isEmpty()) {
688 // Remove the transparent area from the visible region
689 if (translucent) {
690 visibleRegion.subtractSelf(layer->transparentRegionScreen);
691 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
Mathias Agopianab028732010-03-16 16:41:46 -0700693 // compute the opaque region
694 const int32_t layerOrientation = layer->getOrientation();
695 if (s.alpha==255 && !translucent &&
696 ((layerOrientation & Transform::ROT_INVALID) == false)) {
697 // the opaque region is the layer's footprint
698 opaqueRegion = visibleRegion;
699 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 }
701 }
702
Mathias Agopianab028732010-03-16 16:41:46 -0700703 // Clip the covered region to the visible region
704 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
705
706 // Update aboveCoveredLayers for next (lower) layer
707 aboveCoveredLayers.orSelf(visibleRegion);
708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 // subtract the opaque region covered by the layers above us
710 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711
712 // compute this layer's dirty region
713 if (layer->contentDirty) {
714 // we need to invalidate the whole region
715 dirty = visibleRegion;
716 // as well, as the old visible region
717 dirty.orSelf(layer->visibleRegionScreen);
718 layer->contentDirty = false;
719 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700720 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700721 * the exposed region consists of two components:
722 * 1) what's VISIBLE now and was COVERED before
723 * 2) what's EXPOSED now less what was EXPOSED before
724 *
725 * note that (1) is conservative, we start with the whole
726 * visible region but only keep what used to be covered by
727 * something -- which mean it may have been exposed.
728 *
729 * (2) handles areas that were not covered by anything but got
730 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700731 */
Mathias Agopianab028732010-03-16 16:41:46 -0700732 const Region newExposed = visibleRegion - coveredRegion;
733 const Region oldVisibleRegion = layer->visibleRegionScreen;
734 const Region oldCoveredRegion = layer->coveredRegionScreen;
735 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
736 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 }
738 dirty.subtractSelf(aboveOpaqueLayers);
739
740 // accumulate to the screen dirty region
741 dirtyRegion.orSelf(dirty);
742
Mathias Agopianab028732010-03-16 16:41:46 -0700743 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745
746 // Store the visible region is screen space
747 layer->setVisibleRegion(visibleRegion);
748 layer->setCoveredRegion(coveredRegion);
749
Mathias Agopian97011222009-07-28 10:57:27 -0700750 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 if (layer->isSecure() && !visibleRegion.isEmpty()) {
752 secureFrameBuffer = true;
753 }
754 }
755
Mathias Agopian97011222009-07-28 10:57:27 -0700756 // invalidate the areas where a layer was removed
757 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
758 mDirtyRegionRemovedLayer.clear();
759
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 mSecureFrameBuffer = secureFrameBuffer;
761 opaqueRegion = aboveOpaqueLayers;
762}
763
764
765void SurfaceFlinger::commitTransaction()
766{
767 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700768 mResizeTransationPending = false;
769 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770}
771
772void SurfaceFlinger::handlePageFlip()
773{
774 bool visibleRegions = mVisibleRegionsDirty;
775 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
776 visibleRegions |= lockPageFlip(currentLayers);
777
778 const DisplayHardware& hw = graphicPlane(0).displayHardware();
779 const Region screenRegion(hw.bounds());
780 if (visibleRegions) {
781 Region opaqueRegion;
782 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
783 mWormholeRegion = screenRegion.subtract(opaqueRegion);
784 mVisibleRegionsDirty = false;
785 }
786
787 unlockPageFlip(currentLayers);
788 mDirtyRegion.andSelf(screenRegion);
789}
790
791bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
792{
793 bool recomputeVisibleRegions = false;
794 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700795 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700797 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 layer->lockPageFlip(recomputeVisibleRegions);
799 }
800 return recomputeVisibleRegions;
801}
802
803void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
804{
805 const GraphicPlane& plane(graphicPlane(0));
806 const Transform& planeTransform(plane.transform());
807 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700808 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700810 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 layer->unlockPageFlip(planeTransform, mDirtyRegion);
812 }
813}
814
Mathias Agopianb8a55602009-06-26 19:06:36 -0700815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816void SurfaceFlinger::handleRepaint()
817{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700818 // compute the invalid region
819 mInvalidRegion.orSelf(mDirtyRegion);
820 if (mInvalidRegion.isEmpty()) {
821 // nothing to do
822 return;
823 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824
825 if (UNLIKELY(mDebugRegion)) {
826 debugFlashRegions();
827 }
828
Mathias Agopianb8a55602009-06-26 19:06:36 -0700829 // set the frame buffer
830 const DisplayHardware& hw(graphicPlane(0).displayHardware());
831 glMatrixMode(GL_MODELVIEW);
832 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833
834 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700835 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
836 (flags & DisplayHardware::BUFFER_PRESERVED))
837 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
839 // takes a rectangle, we must make sure to update that whole
840 // rectangle in that case
841 if (flags & DisplayHardware::SWAP_RECTANGLE) {
842 // FIXME: we really should be able to pass a region to
843 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844 mDirtyRegion.set(mInvalidRegion.bounds());
845 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700846 // in the BUFFER_PRESERVED case, obviously, we can update only
847 // what's needed and nothing more.
848 // NOTE: this is NOT a common case, as preserving the backbuffer
849 // is costly and usually involves copying the whole update back.
850 }
851 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700852 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // We need to redraw the rectangle that will be updated
854 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700855 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // rectangle instead of a region (see DisplayHardware::flip())
857 mDirtyRegion.set(mInvalidRegion.bounds());
858 } else {
859 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 mDirtyRegion.set(hw.bounds());
861 mInvalidRegion = mDirtyRegion;
862 }
863 }
864
865 // compose all surfaces
866 composeSurfaces(mDirtyRegion);
867
868 // clear the dirty regions
869 mDirtyRegion.clear();
870}
871
872void SurfaceFlinger::composeSurfaces(const Region& dirty)
873{
874 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
875 // should never happen unless the window manager has a bug
876 // draw something...
877 drawWormhole();
878 }
879 const SurfaceFlinger& flinger(*this);
880 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
881 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700882 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700884 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 const Region& visibleRegion(layer->visibleRegionScreen);
886 if (!visibleRegion.isEmpty()) {
887 const Region clip(dirty.intersect(visibleRegion));
888 if (!clip.isEmpty()) {
889 layer->draw(clip);
890 }
891 }
892 }
893}
894
895void SurfaceFlinger::unlockClients()
896{
897 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
898 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700899 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700901 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 layer->finishPageFlip();
903 }
904}
905
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906void SurfaceFlinger::debugFlashRegions()
907{
Mathias Agopian0926f502009-05-04 14:17:04 -0700908 const DisplayHardware& hw(graphicPlane(0).displayHardware());
909 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700910
911 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
912 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700913 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopian0926f502009-05-04 14:17:04 -0700914 mDirtyRegion.bounds() : hw.bounds());
915 composeSurfaces(repaint);
916 }
917
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 glDisable(GL_TEXTURE_2D);
919 glDisable(GL_BLEND);
920 glDisable(GL_DITHER);
921 glDisable(GL_SCISSOR_TEST);
922
Mathias Agopian0926f502009-05-04 14:17:04 -0700923 static int toggle = 0;
924 toggle = 1 - toggle;
925 if (toggle) {
926 glColor4x(0x10000, 0, 0x10000, 0x10000);
927 } else {
928 glColor4x(0x10000, 0x10000, 0, 0x10000);
929 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930
Mathias Agopian20f68782009-05-11 00:03:41 -0700931 Region::const_iterator it = mDirtyRegion.begin();
932 Region::const_iterator const end = mDirtyRegion.end();
933 while (it != end) {
934 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 GLfloat vertices[][2] = {
936 { r.left, r.top },
937 { r.left, r.bottom },
938 { r.right, r.bottom },
939 { r.right, r.top }
940 };
941 glVertexPointer(2, GL_FLOAT, 0, vertices);
942 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
943 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700944
Mathias Agopianb8a55602009-06-26 19:06:36 -0700945 if (mInvalidRegion.isEmpty()) {
946 mDirtyRegion.dump("mDirtyRegion");
947 mInvalidRegion.dump("mInvalidRegion");
948 }
949 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950
951 if (mDebugRegion > 1)
952 usleep(mDebugRegion * 1000);
953
954 glEnable(GL_SCISSOR_TEST);
955 //mDirtyRegion.dump("mDirtyRegion");
956}
957
958void SurfaceFlinger::drawWormhole() const
959{
960 const Region region(mWormholeRegion.intersect(mDirtyRegion));
961 if (region.isEmpty())
962 return;
963
964 const DisplayHardware& hw(graphicPlane(0).displayHardware());
965 const int32_t width = hw.getWidth();
966 const int32_t height = hw.getHeight();
967
968 glDisable(GL_BLEND);
969 glDisable(GL_DITHER);
970
971 if (LIKELY(!mDebugBackground)) {
972 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700973 Region::const_iterator it = region.begin();
974 Region::const_iterator const end = region.end();
975 while (it != end) {
976 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 const GLint sy = height - (r.top + r.height());
978 glScissor(r.left, sy, r.width(), r.height());
979 glClear(GL_COLOR_BUFFER_BIT);
980 }
981 } else {
982 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
983 { width, height }, { 0, height } };
984 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
985 glVertexPointer(2, GL_SHORT, 0, vertices);
986 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
987 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
988 glEnable(GL_TEXTURE_2D);
989 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
990 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
991 glMatrixMode(GL_TEXTURE);
992 glLoadIdentity();
993 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700994 Region::const_iterator it = region.begin();
995 Region::const_iterator const end = region.end();
996 while (it != end) {
997 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 const GLint sy = height - (r.top + r.height());
999 glScissor(r.left, sy, r.width(), r.height());
1000 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1001 }
1002 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1003 }
1004}
1005
1006void SurfaceFlinger::debugShowFPS() const
1007{
1008 static int mFrameCount;
1009 static int mLastFrameCount = 0;
1010 static nsecs_t mLastFpsTime = 0;
1011 static float mFps = 0;
1012 mFrameCount++;
1013 nsecs_t now = systemTime();
1014 nsecs_t diff = now - mLastFpsTime;
1015 if (diff > ms2ns(250)) {
1016 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1017 mLastFpsTime = now;
1018 mLastFrameCount = mFrameCount;
1019 }
1020 // XXX: mFPS has the value we want
1021 }
1022
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001023status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024{
1025 Mutex::Autolock _l(mStateLock);
1026 addLayer_l(layer);
1027 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1028 return NO_ERROR;
1029}
1030
Mathias Agopian96f08192010-06-02 23:28:45 -07001031status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1032{
1033 ssize_t i = mCurrentState.layersSortedByZ.add(
1034 layer, &LayerBase::compareCurrentStateZ);
1035 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1036}
1037
1038ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1039 const sp<LayerBaseClient>& lbc)
1040{
1041 Mutex::Autolock _l(mStateLock);
1042
1043 // attach this layer to the client
1044 ssize_t name = client->attachLayer(lbc);
1045
1046 // add this layer to the current state list
1047 addLayer_l(lbc);
1048
1049 return name;
1050}
1051
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001052status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053{
1054 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001055 status_t err = purgatorizeLayer_l(layer);
1056 if (err == NO_ERROR)
1057 setTransactionFlags(eTransactionNeeded);
1058 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059}
1060
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001061status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062{
1063 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1064 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001065 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 return NO_ERROR;
1067 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001068 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
Mathias Agopian9a112062009-04-17 19:36:26 -07001071status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1072{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001073 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001074 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001075
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001076 layerBase->onRemoved();
1077
Mathias Agopian3d579642009-06-04 18:46:21 -07001078 // it's possible that we don't find a layer, because it might
1079 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001080 // from the user because there is a race between Client::destroySurface(),
1081 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001082 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1083}
1084
Mathias Agopian96f08192010-06-02 23:28:45 -07001085status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086{
Mathias Agopian96f08192010-06-02 23:28:45 -07001087 layer->forceVisibilityTransaction();
1088 setTransactionFlags(eTraversalNeeded);
1089 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090}
1091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1093{
1094 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1095}
1096
Mathias Agopianbb641242010-05-18 17:06:55 -07001097uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098{
1099 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1100 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001101 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 }
1103 return old;
1104}
1105
1106void SurfaceFlinger::openGlobalTransaction()
1107{
1108 android_atomic_inc(&mTransactionCount);
1109}
1110
1111void SurfaceFlinger::closeGlobalTransaction()
1112{
1113 if (android_atomic_dec(&mTransactionCount) == 1) {
1114 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001115
1116 // if there is a transaction with a resize, wait for it to
1117 // take effect before returning.
1118 Mutex::Autolock _l(mStateLock);
1119 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001120 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1121 if (CC_UNLIKELY(err != NO_ERROR)) {
1122 // just in case something goes wrong in SF, return to the
1123 // called after a few seconds.
1124 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1125 mResizeTransationPending = false;
1126 break;
1127 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001128 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 }
1130}
1131
1132status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1133{
1134 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1135 return BAD_VALUE;
1136
1137 Mutex::Autolock _l(mStateLock);
1138 mCurrentState.freezeDisplay = 1;
1139 setTransactionFlags(eTransactionNeeded);
1140
1141 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001142 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 return NO_ERROR;
1144}
1145
1146status_t SurfaceFlinger::unfreezeDisplay(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 = 0;
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
Mathias Agopianc08731e2009-03-27 18:11:38 -07001160int SurfaceFlinger::setOrientation(DisplayID dpy,
1161 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162{
1163 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1164 return BAD_VALUE;
1165
1166 Mutex::Autolock _l(mStateLock);
1167 if (mCurrentState.orientation != orientation) {
1168 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001169 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 mCurrentState.orientation = orientation;
1171 setTransactionFlags(eTransactionNeeded);
1172 mTransactionCV.wait(mStateLock);
1173 } else {
1174 orientation = BAD_VALUE;
1175 }
1176 }
1177 return orientation;
1178}
1179
Mathias Agopian96f08192010-06-02 23:28:45 -07001180sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001181 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1183 uint32_t flags)
1184{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001185 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001187
1188 if (int32_t(w|h) < 0) {
1189 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1190 int(w), int(h));
1191 return surfaceHandle;
1192 }
1193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 switch (flags & eFXSurfaceMask) {
1196 case eFXSurfaceNormal:
1197 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001198 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 } else {
Mathias Agopian96f08192010-06-02 23:28:45 -07001200 layer = createNormalSurface(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 }
1202 break;
1203 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001204 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 break;
1206 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001207 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 break;
1209 }
1210
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001211 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001212 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001213 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001214 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001216 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001217 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001218 params->identity = surfaceHandle->getIdentity();
1219 params->width = w;
1220 params->height = h;
1221 params->format = format;
1222 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 }
1225
1226 return surfaceHandle;
1227}
1228
Mathias Agopian96f08192010-06-02 23:28:45 -07001229sp<LayerBaseClient> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001230 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001232 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233{
1234 // initialize the surfaces
1235 switch (format) { // TODO: take h/w into account
1236 case PIXEL_FORMAT_TRANSPARENT:
1237 case PIXEL_FORMAT_TRANSLUCENT:
1238 format = PIXEL_FORMAT_RGBA_8888;
1239 break;
1240 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001241 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 break;
1243 }
1244
Mathias Agopian96f08192010-06-02 23:28:45 -07001245 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001246 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001247 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001249 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 }
1251 return layer;
1252}
1253
Mathias Agopian96f08192010-06-02 23:28:45 -07001254sp<LayerBaseClient> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001255 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001256 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257{
Mathias Agopian96f08192010-06-02 23:28:45 -07001258 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 return layer;
1261}
1262
Mathias Agopian96f08192010-06-02 23:28:45 -07001263sp<LayerBaseClient> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001264 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001265 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266{
Mathias Agopian96f08192010-06-02 23:28:45 -07001267 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 return layer;
1270}
1271
Mathias Agopian96f08192010-06-02 23:28:45 -07001272sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001273 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001274 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275{
Mathias Agopian96f08192010-06-02 23:28:45 -07001276 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 return layer;
1279}
1280
Mathias Agopian96f08192010-06-02 23:28:45 -07001281status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282{
Mathias Agopian9a112062009-04-17 19:36:26 -07001283 /*
1284 * called by the window manager, when a surface should be marked for
1285 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001286 *
1287 * The surface is removed from the current and drawing lists, but placed
1288 * in the purgatory queue, so it's not destroyed right-away (we need
1289 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001290 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001291
Mathias Agopian48d819a2009-09-10 19:41:18 -07001292 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001293 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001294 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001295 if (layer != 0) {
1296 err = purgatorizeLayer_l(layer);
1297 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001298 setTransactionFlags(eTransactionNeeded);
1299 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001300 }
1301 return err;
1302}
1303
1304status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1305{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001306 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001307
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001308 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001309 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001310 sp<LayerBaseClient> layer;
1311 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001312 MessageDestroySurface(
1313 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1314 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001315 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001316 sp<LayerBaseClient> l(layer);
1317 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001318 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001319 /*
1320 * remove the layer from the current list -- chances are that it's
1321 * not in the list anyway, because it should have been removed
1322 * already upon request of the client (eg: window manager).
1323 * However, a buggy client could have not done that.
1324 * Since we know we don't have any more clients, we don't need
1325 * to use the purgatory.
1326 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001327 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001328 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1329 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001330 return true;
1331 }
1332 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001333
Mathias Agopianbb641242010-05-18 17:06:55 -07001334 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 return NO_ERROR;
1336}
1337
1338status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001339 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 int32_t count,
1341 const layer_state_t* states)
1342{
1343 Mutex::Autolock _l(mStateLock);
1344 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001346 const layer_state_t& s(states[i]);
1347 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001348 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 if (what & ePositionChanged) {
1351 if (layer->setPosition(s.x, s.y))
1352 flags |= eTraversalNeeded;
1353 }
1354 if (what & eLayerChanged) {
1355 if (layer->setLayer(s.z)) {
1356 mCurrentState.layersSortedByZ.reorder(
1357 layer, &Layer::compareCurrentStateZ);
1358 // we need traversal (state changed)
1359 // AND transaction (list changed)
1360 flags |= eTransactionNeeded|eTraversalNeeded;
1361 }
1362 }
1363 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001364 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001366 mResizeTransationPending = true;
1367 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 }
1369 if (what & eAlphaChanged) {
1370 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1371 flags |= eTraversalNeeded;
1372 }
1373 if (what & eMatrixChanged) {
1374 if (layer->setMatrix(s.matrix))
1375 flags |= eTraversalNeeded;
1376 }
1377 if (what & eTransparentRegionChanged) {
1378 if (layer->setTransparentRegionHint(s.transparentRegion))
1379 flags |= eTraversalNeeded;
1380 }
1381 if (what & eVisibilityChanged) {
1382 if (layer->setFlags(s.flags, s.mask))
1383 flags |= eTraversalNeeded;
1384 }
1385 }
1386 }
1387 if (flags) {
1388 setTransactionFlags(flags);
1389 }
1390 return NO_ERROR;
1391}
1392
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393void SurfaceFlinger::screenReleased(int dpy)
1394{
1395 // this may be called by a signal handler, we can't do too much in here
1396 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1397 signalEvent();
1398}
1399
1400void SurfaceFlinger::screenAcquired(int dpy)
1401{
1402 // this may be called by a signal handler, we can't do too much in here
1403 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1404 signalEvent();
1405}
1406
1407status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1408{
1409 const size_t SIZE = 1024;
1410 char buffer[SIZE];
1411 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001412 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 snprintf(buffer, SIZE, "Permission Denial: "
1414 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1415 IPCThreadState::self()->getCallingPid(),
1416 IPCThreadState::self()->getCallingUid());
1417 result.append(buffer);
1418 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001419
1420 // figure out if we're stuck somewhere
1421 const nsecs_t now = systemTime();
1422 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1423 const nsecs_t inTransaction(mDebugInTransaction);
1424 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1425 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1426
1427 // Try to get the main lock, but don't insist if we can't
1428 // (this would indicate SF is stuck, but we want to be able to
1429 // print something in dumpsys).
1430 int retry = 3;
1431 while (mStateLock.tryLock()<0 && --retry>=0) {
1432 usleep(1000000);
1433 }
1434 const bool locked(retry >= 0);
1435 if (!locked) {
1436 snprintf(buffer, SIZE,
1437 "SurfaceFlinger appears to be unresponsive, "
1438 "dumping anyways (no locks held)\n");
1439 result.append(buffer);
1440 }
1441
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1443 const size_t count = currentLayers.size();
1444 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001445 const sp<LayerBase>& layer(currentLayers[i]);
1446 layer->dump(result, buffer, SIZE);
1447 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 s.transparentRegion.dump(result, "transparentRegion");
1449 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1450 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1451 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001452
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453 mWormholeRegion.dump(result, "WormholeRegion");
1454 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1455 snprintf(buffer, SIZE,
1456 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1457 mFreezeDisplay?"yes":"no", mFreezeCount,
1458 mCurrentState.orientation, hw.canDraw());
1459 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001460 snprintf(buffer, SIZE,
1461 " last eglSwapBuffers() time: %f us\n"
1462 " last transaction time : %f us\n",
1463 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1464 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001465
Mathias Agopian9795c422009-08-26 16:36:26 -07001466 if (inSwapBuffersDuration || !locked) {
1467 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1468 inSwapBuffersDuration/1000.0);
1469 result.append(buffer);
1470 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001471
Mathias Agopian9795c422009-08-26 16:36:26 -07001472 if (inTransactionDuration || !locked) {
1473 snprintf(buffer, SIZE, " transaction time: %f us\n",
1474 inTransactionDuration/1000.0);
1475 result.append(buffer);
1476 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001477
Mathias Agopian3330b202009-10-05 17:07:12 -07001478 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001479 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001480
1481 if (locked) {
1482 mStateLock.unlock();
1483 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 }
1485 write(fd, result.string(), result.size());
1486 return NO_ERROR;
1487}
1488
1489status_t SurfaceFlinger::onTransact(
1490 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1491{
1492 switch (code) {
1493 case CREATE_CONNECTION:
1494 case OPEN_GLOBAL_TRANSACTION:
1495 case CLOSE_GLOBAL_TRANSACTION:
1496 case SET_ORIENTATION:
1497 case FREEZE_DISPLAY:
1498 case UNFREEZE_DISPLAY:
1499 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 {
1501 // codes that require permission check
1502 IPCThreadState* ipc = IPCThreadState::self();
1503 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001504 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001505 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1506 LOGE("Permission Denial: "
1507 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1508 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 }
1510 }
1511 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1513 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001514 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001515 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1516 IPCThreadState* ipc = IPCThreadState::self();
1517 const int pid = ipc->getCallingPid();
1518 const int uid = ipc->getCallingUid();
1519 LOGE("Permission Denial: "
1520 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 return PERMISSION_DENIED;
1522 }
1523 int n;
1524 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001525 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001527 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 return NO_ERROR;
1529 case 1002: // SHOW_UPDATES
1530 n = data.readInt32();
1531 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1532 return NO_ERROR;
1533 case 1003: // SHOW_BACKGROUND
1534 n = data.readInt32();
1535 mDebugBackground = n ? 1 : 0;
1536 return NO_ERROR;
1537 case 1004:{ // repaint everything
1538 Mutex::Autolock _l(mStateLock);
1539 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1540 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1541 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001542 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001544 case 1005:{ // force transaction
1545 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1546 return NO_ERROR;
1547 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 case 1007: // set mFreezeCount
1549 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001550 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551 return NO_ERROR;
1552 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001553 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554 reply->writeInt32(0);
1555 reply->writeInt32(mDebugRegion);
1556 reply->writeInt32(mDebugBackground);
1557 return NO_ERROR;
1558 case 1013: {
1559 Mutex::Autolock _l(mStateLock);
1560 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1561 reply->writeInt32(hw.getPageFlipCount());
1562 }
1563 return NO_ERROR;
1564 }
1565 }
1566 return err;
1567}
1568
1569// ---------------------------------------------------------------------------
1570#if 0
1571#pragma mark -
1572#endif
1573
Mathias Agopian96f08192010-06-02 23:28:45 -07001574Client::Client(const sp<SurfaceFlinger>& flinger)
1575 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001578 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001579
1580 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1581 "SurfaceFlinger Client control-block");
1582
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001583 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001584 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001585 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001587
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588}
1589
Mathias Agopian96f08192010-06-02 23:28:45 -07001590Client::~Client()
1591{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001593 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595
Mathias Agopian96f08192010-06-02 23:28:45 -07001596 const size_t count = mLayers.size();
1597 for (size_t i=0 ; i<count ; i++) {
1598 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1599 if (layer != 0) {
1600 mFlinger->removeLayer(layer);
1601 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602 }
1603}
1604
Mathias Agopian96f08192010-06-02 23:28:45 -07001605status_t Client::initCheck() const {
1606 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001608
Mathias Agopian96f08192010-06-02 23:28:45 -07001609ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1610{
1611 // TODO: get rid of this
1612 int32_t name = 0;
1613 while (mBitmap & (1LU<<name)) {
1614 name++;
1615 if (name >= 31)
1616 return NO_MEMORY;
1617 }
1618 mBitmap |= 1LU<<name;
1619 layer->setToken(name);
1620 mLayers.add(name, layer);
1621 return name;
1622}
1623
1624void Client::free(LayerBaseClient const* layer)
1625{
1626 // TODO: get rid of this
1627 int32_t name = layer->getToken();
1628 if (name >= 0) {
1629 mBitmap &= ~(1LU<<name);
1630 }
1631
1632 // 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 {
1652 return mCblkHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653}
Mathias Agopian96f08192010-06-02 23:28:45 -07001654sp<ISurface> Client::createSurface(
1655 ISurfaceComposerClient::surface_data_t* params,
1656 int pid, const String8& name,
1657 DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 uint32_t flags)
1659{
Mathias Agopian96f08192010-06-02 23:28:45 -07001660 return mFlinger->createSurface(this, pid, name, params,
1661 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662}
Mathias Agopian96f08192010-06-02 23:28:45 -07001663status_t Client::destroySurface(SurfaceID sid) {
1664 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665}
Mathias Agopian96f08192010-06-02 23:28:45 -07001666status_t Client::setState(int32_t count, const layer_state_t* states) {
1667 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668}
1669
1670// ---------------------------------------------------------------------------
1671
1672GraphicPlane::GraphicPlane()
1673 : mHw(0)
1674{
1675}
1676
1677GraphicPlane::~GraphicPlane() {
1678 delete mHw;
1679}
1680
1681bool GraphicPlane::initialized() const {
1682 return mHw ? true : false;
1683}
1684
Mathias Agopian2b92d892010-02-08 15:49:35 -08001685int GraphicPlane::getWidth() const {
1686 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687}
1688
Mathias Agopian2b92d892010-02-08 15:49:35 -08001689int GraphicPlane::getHeight() const {
1690 return mHeight;
1691}
1692
1693void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1694{
1695 mHw = hw;
1696
1697 // initialize the display orientation transform.
1698 // it's a constant that should come from the display driver.
1699 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1700 char property[PROPERTY_VALUE_MAX];
1701 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1702 //displayOrientation
1703 switch (atoi(property)) {
1704 case 90:
1705 displayOrientation = ISurfaceComposer::eOrientation90;
1706 break;
1707 case 270:
1708 displayOrientation = ISurfaceComposer::eOrientation270;
1709 break;
1710 }
1711 }
1712
1713 const float w = hw->getWidth();
1714 const float h = hw->getHeight();
1715 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1716 &mDisplayTransform);
1717 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1718 mDisplayWidth = h;
1719 mDisplayHeight = w;
1720 } else {
1721 mDisplayWidth = w;
1722 mDisplayHeight = h;
1723 }
1724
1725 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726}
1727
1728status_t GraphicPlane::orientationToTransfrom(
1729 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001730{
1731 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732 switch (orientation) {
1733 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001734 flags = Transform::ROT_0;
1735 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001737 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 break;
1739 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001740 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741 break;
1742 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001743 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 break;
1745 default:
1746 return BAD_VALUE;
1747 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001748 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749 return NO_ERROR;
1750}
1751
1752status_t GraphicPlane::setOrientation(int orientation)
1753{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 // If the rotation can be handled in hardware, this is where
1755 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001756
1757 const DisplayHardware& hw(displayHardware());
1758 const float w = mDisplayWidth;
1759 const float h = mDisplayHeight;
1760 mWidth = int(w);
1761 mHeight = int(h);
1762
1763 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001764 GraphicPlane::orientationToTransfrom(orientation, w, h,
1765 &orientationTransform);
1766 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1767 mWidth = int(h);
1768 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001770
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001771 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001772 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773 return NO_ERROR;
1774}
1775
1776const DisplayHardware& GraphicPlane::displayHardware() const {
1777 return *mHw;
1778}
1779
1780const Transform& GraphicPlane::transform() const {
1781 return mGlobalTransform;
1782}
1783
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001784EGLDisplay GraphicPlane::getEGLDisplay() const {
1785 return mHw->getEGLDisplay();
1786}
1787
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788// ---------------------------------------------------------------------------
1789
1790}; // namespace android