blob: f2b918fa2a1c7fe71087c49a94c830c3262b86d7 [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
40#include <ui/PixelFormat.h>
41#include <ui/DisplayInfo.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"
Mathias Agopiancbb288b2009-09-07 16:32:45 -070047#include "Buffer.h"
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
50#include "LayerBlur.h"
51#include "LayerBuffer.h"
52#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopiana1ecca92009-05-21 19:21:59 -070057/* ideally AID_GRAPHICS would be in a semi-public header
58 * or there would be a way to map a user/group name to its id
59 */
60#ifndef AID_GRAPHICS
61#define AID_GRAPHICS 1003
62#endif
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#define DISPLAY_COUNT 1
65
66namespace android {
67
68// ---------------------------------------------------------------------------
69
70void SurfaceFlinger::instantiate() {
71 defaultServiceManager()->addService(
72 String16("SurfaceFlinger"), new SurfaceFlinger());
73}
74
75void SurfaceFlinger::shutdown() {
76 // we should unregister here, but not really because
77 // when (if) the service manager goes away, all the services
78 // it has a reference to will leave too.
79}
80
81// ---------------------------------------------------------------------------
82
83SurfaceFlinger::LayerVector::LayerVector(const SurfaceFlinger::LayerVector& rhs)
84 : lookup(rhs.lookup), layers(rhs.layers)
85{
86}
87
88ssize_t SurfaceFlinger::LayerVector::indexOf(
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090{
91 if (guess<size() && lookup.keyAt(guess) == key)
92 return guess;
93 const ssize_t i = lookup.indexOfKey(key);
94 if (i>=0) {
95 const size_t idx = lookup.valueAt(i);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 LOGE_IF(layers[idx]!=key,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 return idx;
100 }
101 return i;
102}
103
104ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105 const sp<LayerBase>& layer,
106 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107{
108 size_t count = layers.size();
109 ssize_t l = 0;
110 ssize_t h = count-1;
111 ssize_t mid;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112 sp<LayerBase> const* a = layers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 while (l <= h) {
114 mid = l + (h - l)/2;
115 const int c = cmp(a+mid, &layer);
116 if (c == 0) { l = mid; break; }
117 else if (c<0) { l = mid+1; }
118 else { h = mid-1; }
119 }
120 size_t order = l;
121 while (order<count && !cmp(&layer, a+order)) {
122 order++;
123 }
124 count = lookup.size();
125 for (size_t i=0 ; i<count ; i++) {
126 if (lookup.valueAt(i) >= order) {
127 lookup.editValueAt(i)++;
128 }
129 }
130 layers.insertAt(layer, order);
131 lookup.add(layer, order);
132 return order;
133}
134
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136{
137 const ssize_t keyIndex = lookup.indexOfKey(layer);
138 if (keyIndex >= 0) {
139 const size_t index = lookup.valueAt(keyIndex);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 LOGE_IF(layers[index]!=layer,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142 this, int(index), layers[index].get(), layer.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 layers.removeItemsAt(index);
144 lookup.removeItemsAt(keyIndex);
145 const size_t count = lookup.size();
146 for (size_t i=0 ; i<count ; i++) {
147 if (lookup.valueAt(i) >= size_t(index)) {
148 lookup.editValueAt(i)--;
149 }
150 }
151 return index;
152 }
153 return NAME_NOT_FOUND;
154}
155
156ssize_t SurfaceFlinger::LayerVector::reorder(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157 const sp<LayerBase>& layer,
158 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
160 // XXX: it's a little lame. but oh well...
161 ssize_t err = remove(layer);
162 if (err >=0)
163 err = add(layer, cmp);
164 return err;
165}
166
167// ---------------------------------------------------------------------------
168#if 0
169#pragma mark -
170#endif
171
172SurfaceFlinger::SurfaceFlinger()
173 : BnSurfaceComposer(), Thread(false),
174 mTransactionFlags(0),
175 mTransactionCount(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700176 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 mBootTime(systemTime()),
Mathias Agopian375f5632009-06-15 18:24:59 -0700179 mHardwareTest("android.permission.HARDWARE_TEST"),
180 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
181 mDump("android.permission.DUMP"),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 mVisibleRegionsDirty(false),
183 mDeferReleaseConsole(false),
184 mFreezeDisplay(false),
185 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700186 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700189 mDebugInSwapBuffers(0),
190 mLastSwapBufferTime(0),
191 mDebugInTransaction(0),
192 mLastTransactionTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 mConsoleSignals(0),
194 mSecureFrameBuffer(0)
195{
196 init();
197}
198
199void SurfaceFlinger::init()
200{
201 LOGI("SurfaceFlinger is starting");
202
203 // debugging stuff...
204 char value[PROPERTY_VALUE_MAX];
205 property_get("debug.sf.showupdates", value, "0");
206 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 property_get("debug.sf.showbackground", value, "0");
208 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209
210 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
214SurfaceFlinger::~SurfaceFlinger()
215{
216 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217}
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
220{
221 return graphicPlane(0).displayHardware().getOverlayEngine();
222}
223
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700224sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700226 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227}
228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
230{
231 Mutex::Autolock _l(mStateLock);
232 uint32_t token = mTokens.acquire();
233
Mathias Agopianf9d93272009-06-19 17:00:27 -0700234 sp<Client> client = new Client(token, this);
235 if (client->ctrlblk == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 mTokens.release(token);
237 return 0;
238 }
239 status_t err = mClientsMap.add(token, client);
240 if (err < 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 mTokens.release(token);
242 return 0;
243 }
244 sp<BClient> bclient =
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700245 new BClient(this, token, client->getControlBlockMemory());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 return bclient;
247}
248
249void SurfaceFlinger::destroyConnection(ClientID cid)
250{
251 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -0700252 sp<Client> client = mClientsMap.valueFor(cid);
253 if (client != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 // free all the layers this client owns
Mathias Agopian3d579642009-06-04 18:46:21 -0700255 Vector< wp<LayerBaseClient> > layers(client->getLayers());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 const size_t count = layers.size();
257 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700258 sp<LayerBaseClient> layer(layers[i].promote());
259 if (layer != 0) {
Mathias Agopian3d579642009-06-04 18:46:21 -0700260 purgatorizeLayer_l(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700261 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 }
263
264 // the resources associated with this client will be freed
265 // during the next transaction, after these surfaces have been
266 // properly removed from the screen
267
268 // remove this client from our ClientID->Client mapping.
269 mClientsMap.removeItem(cid);
270
271 // and add it to the list of disconnected clients
272 mDisconnectedClients.add(client);
273
274 // request a transaction
275 setTransactionFlags(eTransactionNeeded);
276 }
277}
278
279const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
280{
281 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
282 const GraphicPlane& plane(mGraphicPlanes[dpy]);
283 return plane;
284}
285
286GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
287{
288 return const_cast<GraphicPlane&>(
289 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
290}
291
292void SurfaceFlinger::bootFinished()
293{
294 const nsecs_t now = systemTime();
295 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700296 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
297 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
300void SurfaceFlinger::onFirstRef()
301{
302 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
303
304 // Wait for the main thread to be done with its initialization
305 mReadyToRunBarrier.wait();
306}
307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308static inline uint16_t pack565(int r, int g, int b) {
309 return (r<<11)|(g<<5)|b;
310}
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312status_t SurfaceFlinger::readyToRun()
313{
314 LOGI( "SurfaceFlinger's main thread ready to run. "
315 "Initializing graphics H/W...");
316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 // we only support one display currently
318 int dpy = 0;
319
320 {
321 // initialize the main display
322 GraphicPlane& plane(graphicPlane(dpy));
323 DisplayHardware* const hw = new DisplayHardware(this, dpy);
324 plane.setDisplayHardware(hw);
325 }
326
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700327 // create the shared control-block
328 mServerHeap = new MemoryHeapBase(4096,
329 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
330 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
331
332 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
333 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
334
335 new(mServerCblk) surface_flinger_cblk_t;
336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 // initialize primary screen
338 // (other display should be initialized in the same manner, but
339 // asynchronously, as they could come and go. None of this is supported
340 // yet).
341 const GraphicPlane& plane(graphicPlane(dpy));
342 const DisplayHardware& hw = plane.displayHardware();
343 const uint32_t w = hw.getWidth();
344 const uint32_t h = hw.getHeight();
345 const uint32_t f = hw.getFormat();
346 hw.makeCurrent();
347
348 // initialize the shared control block
349 mServerCblk->connected |= 1<<dpy;
350 display_cblk_t* dcblk = mServerCblk->displays + dpy;
351 memset(dcblk, 0, sizeof(display_cblk_t));
352 dcblk->w = w;
353 dcblk->h = h;
354 dcblk->format = f;
355 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
356 dcblk->xdpi = hw.getDpiX();
357 dcblk->ydpi = hw.getDpiY();
358 dcblk->fps = hw.getRefreshRate();
359 dcblk->density = hw.getDensity();
360 asm volatile ("":::"memory");
361
362 // Initialize OpenGL|ES
363 glActiveTexture(GL_TEXTURE0);
364 glBindTexture(GL_TEXTURE_2D, 0);
365 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
366 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
367 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
368 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
369 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
370 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
371 glPixelStorei(GL_PACK_ALIGNMENT, 4);
372 glEnableClientState(GL_VERTEX_ARRAY);
373 glEnable(GL_SCISSOR_TEST);
374 glShadeModel(GL_FLAT);
375 glDisable(GL_DITHER);
376 glDisable(GL_CULL_FACE);
377
378 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
379 const uint16_t g1 = pack565(0x17,0x2f,0x17);
380 const uint16_t textureData[4] = { g0, g1, g1, g0 };
381 glGenTextures(1, &mWormholeTexName);
382 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
383 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
384 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
385 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
386 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
387 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
388 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
389
390 glViewport(0, 0, w, h);
391 glMatrixMode(GL_PROJECTION);
392 glLoadIdentity();
393 glOrthof(0, w, h, 0, 0, 1);
394
395 LayerDim::initDimmer(this, w, h);
396
397 mReadyToRunBarrier.open();
398
399 /*
400 * We're now ready to accept clients...
401 */
402
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700403 // start boot animation
404 property_set("ctl.start", "bootanim");
405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 return NO_ERROR;
407}
408
409// ----------------------------------------------------------------------------
410#if 0
411#pragma mark -
412#pragma mark Events Handler
413#endif
414
415void SurfaceFlinger::waitForEvent()
416{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700417 while (true) {
418 nsecs_t timeout = -1;
419 if (UNLIKELY(isFrozen())) {
420 // wait 5 seconds
421 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
422 const nsecs_t now = systemTime();
423 if (mFreezeDisplayTime == 0) {
424 mFreezeDisplayTime = now;
425 }
426 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
427 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700428 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700429
Mathias Agopianb6683b52009-04-28 03:17:50 -0700430 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700431 if (msg != 0) {
432 mFreezeDisplayTime = 0;
433 switch (msg->what) {
434 case MessageQueue::INVALIDATE:
435 // invalidate message, just return to the main loop
436 return;
437 }
438 } else {
439 // we timed out
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 if (isFrozen()) {
441 // we timed out and are still frozen
442 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
443 mFreezeDisplay, mFreezeCount);
444 mFreezeCount = 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700445 mFreezeDisplay = false;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700446 return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 }
448 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 }
450}
451
452void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700453 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454}
455
456void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700457 // this is the IPC call
458 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459}
460
461void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
462{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700463 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464}
465
466// ----------------------------------------------------------------------------
467#if 0
468#pragma mark -
469#pragma mark Main loop
470#endif
471
472bool SurfaceFlinger::threadLoop()
473{
474 waitForEvent();
475
476 // check for transactions
477 if (UNLIKELY(mConsoleSignals)) {
478 handleConsoleEvents();
479 }
480
481 if (LIKELY(mTransactionCount == 0)) {
482 // if we're in a global transaction, don't do anything.
483 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
484 uint32_t transactionFlags = getTransactionFlags(mask);
485 if (LIKELY(transactionFlags)) {
486 handleTransaction(transactionFlags);
487 }
488 }
489
490 // post surfaces (if needed)
491 handlePageFlip();
492
493 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700494 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 // repaint the framebuffer (if needed)
496 handleRepaint();
497
Mathias Agopian74faca22009-09-17 16:18:16 -0700498 // inform the h/w that we're done compositing
499 hw.compositionComplete();
500
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 // release the clients before we flip ('cause flip might block)
502 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504 postFramebuffer();
505 } else {
506 // pretend we did the post
507 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 usleep(16667); // 60 fps period
509 }
510 return true;
511}
512
513void SurfaceFlinger::postFramebuffer()
514{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 if (!mInvalidRegion.isEmpty()) {
516 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700517 const nsecs_t now = systemTime();
518 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700520 mLastSwapBufferTime = systemTime() - now;
521 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 }
524}
525
526void SurfaceFlinger::handleConsoleEvents()
527{
528 // something to do with the console
529 const DisplayHardware& hw = graphicPlane(0).displayHardware();
530
531 int what = android_atomic_and(0, &mConsoleSignals);
532 if (what & eConsoleAcquired) {
533 hw.acquireScreen();
534 }
535
536 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700537 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 hw.releaseScreen();
540 }
541
542 if (what & eConsoleReleased) {
543 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 hw.releaseScreen();
545 } else {
546 mDeferReleaseConsole = true;
547 }
548 }
549
550 mDirtyRegion.set(hw.bounds());
551}
552
553void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
554{
Mathias Agopian3d579642009-06-04 18:46:21 -0700555 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556
Mathias Agopian3d579642009-06-04 18:46:21 -0700557 { // scope for the lock
558 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700559 const nsecs_t now = systemTime();
560 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700561 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700562 mLastTransactionTime = systemTime() - now;
563 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700564 }
565
566 // do this without lock held
567 const size_t count = ditchedLayers.size();
568 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700569 if (ditchedLayers[i] != 0) {
570 //LOGD("ditching layer %p", ditchedLayers[i].get());
571 ditchedLayers[i]->ditch();
572 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700573 }
574}
575
576void SurfaceFlinger::handleTransactionLocked(
577 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
578{
579 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 const size_t count = currentLayers.size();
581
582 /*
583 * Traversal of the children
584 * (perform the transaction for each of them if needed)
585 */
586
587 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
588 if (layersNeedTransaction) {
589 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
592 if (!trFlags) continue;
593
594 const uint32_t flags = layer->doTransaction(0);
595 if (flags & Layer::eVisibleRegion)
596 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 }
598 }
599
600 /*
601 * Perform our own transaction if needed
602 */
603
604 if (transactionFlags & eTransactionNeeded) {
605 if (mCurrentState.orientation != mDrawingState.orientation) {
606 // the orientation has changed, recompute all visible regions
607 // and invalidate everything.
608
609 const int dpy = 0;
610 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700611 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 GraphicPlane& plane(graphicPlane(dpy));
613 plane.setOrientation(orientation);
614
615 // update the shared control block
616 const DisplayHardware& hw(plane.displayHardware());
617 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
618 dcblk->orientation = orientation;
619 if (orientation & eOrientationSwapMask) {
620 // 90 or 270 degrees orientation
621 dcblk->w = hw.getHeight();
622 dcblk->h = hw.getWidth();
623 } else {
624 dcblk->w = hw.getWidth();
625 dcblk->h = hw.getHeight();
626 }
627
628 mVisibleRegionsDirty = true;
629 mDirtyRegion.set(hw.bounds());
Mathias Agopianc08731e2009-03-27 18:11:38 -0700630 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 }
632
633 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
634 // freezing or unfreezing the display -> trigger animation if needed
635 mFreezeDisplay = mCurrentState.freezeDisplay;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 }
637
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700638 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
639 // layers have been added
640 mVisibleRegionsDirty = true;
641 }
642
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 // some layers might have been removed, so
644 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700646 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700648 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700649 const size_t count = previousLayers.size();
650 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700651 const sp<LayerBase>& layer(previousLayers[i]);
652 if (currentLayers.indexOf( layer ) < 0) {
653 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700654 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700655 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700656 }
657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 }
659
660 // get rid of all resources we don't need anymore
661 // (layers and clients)
662 free_resources_l();
663 }
664
665 commitTransaction();
666}
667
668sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
669{
670 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
671}
672
673void SurfaceFlinger::computeVisibleRegions(
674 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
675{
676 const GraphicPlane& plane(graphicPlane(0));
677 const Transform& planeTransform(plane.transform());
678
679 Region aboveOpaqueLayers;
680 Region aboveCoveredLayers;
681 Region dirty;
682
683 bool secureFrameBuffer = false;
684
685 size_t i = currentLayers.size();
686 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700687 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 layer->validateVisibility(planeTransform);
689
690 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700691 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
693 // handle hidden surfaces by setting the visible region to empty
694 Region opaqueRegion;
695 Region visibleRegion;
696 Region coveredRegion;
Mathias Agopian97011222009-07-28 10:57:27 -0700697 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700699 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 visibleRegion.set(bounds);
701 coveredRegion = visibleRegion;
702
703 // Remove the transparent area from the visible region
704 if (translucent) {
705 visibleRegion.subtractSelf(layer->transparentRegionScreen);
706 }
707
708 // compute the opaque region
709 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
710 // the opaque region is the visible region
711 opaqueRegion = visibleRegion;
712 }
713 }
714
715 // subtract the opaque region covered by the layers above us
716 visibleRegion.subtractSelf(aboveOpaqueLayers);
717 coveredRegion.andSelf(aboveCoveredLayers);
718
719 // compute this layer's dirty region
720 if (layer->contentDirty) {
721 // we need to invalidate the whole region
722 dirty = visibleRegion;
723 // as well, as the old visible region
724 dirty.orSelf(layer->visibleRegionScreen);
725 layer->contentDirty = false;
726 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700727 /* compute the exposed region:
728 * exposed = what's VISIBLE and NOT COVERED now
729 * but was COVERED before
730 */
731 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 }
733 dirty.subtractSelf(aboveOpaqueLayers);
734
735 // accumulate to the screen dirty region
736 dirtyRegion.orSelf(dirty);
737
Mathias Agopian62b74442009-04-14 23:02:51 -0700738 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700740 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741
742 // Store the visible region is screen space
743 layer->setVisibleRegion(visibleRegion);
744 layer->setCoveredRegion(coveredRegion);
745
Mathias Agopian97011222009-07-28 10:57:27 -0700746 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 if (layer->isSecure() && !visibleRegion.isEmpty()) {
748 secureFrameBuffer = true;
749 }
750 }
751
Mathias Agopian97011222009-07-28 10:57:27 -0700752 // invalidate the areas where a layer was removed
753 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
754 mDirtyRegionRemovedLayer.clear();
755
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 mSecureFrameBuffer = secureFrameBuffer;
757 opaqueRegion = aboveOpaqueLayers;
758}
759
760
761void SurfaceFlinger::commitTransaction()
762{
763 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700764 mResizeTransationPending = false;
765 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766}
767
768void SurfaceFlinger::handlePageFlip()
769{
770 bool visibleRegions = mVisibleRegionsDirty;
771 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
772 visibleRegions |= lockPageFlip(currentLayers);
773
774 const DisplayHardware& hw = graphicPlane(0).displayHardware();
775 const Region screenRegion(hw.bounds());
776 if (visibleRegions) {
777 Region opaqueRegion;
778 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
779 mWormholeRegion = screenRegion.subtract(opaqueRegion);
780 mVisibleRegionsDirty = false;
781 }
782
783 unlockPageFlip(currentLayers);
784 mDirtyRegion.andSelf(screenRegion);
785}
786
787bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
788{
789 bool recomputeVisibleRegions = false;
790 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700791 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700793 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 layer->lockPageFlip(recomputeVisibleRegions);
795 }
796 return recomputeVisibleRegions;
797}
798
799void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
800{
801 const GraphicPlane& plane(graphicPlane(0));
802 const Transform& planeTransform(plane.transform());
803 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700804 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700806 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807 layer->unlockPageFlip(planeTransform, mDirtyRegion);
808 }
809}
810
Mathias Agopianb8a55602009-06-26 19:06:36 -0700811
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812void SurfaceFlinger::handleRepaint()
813{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700814 // compute the invalid region
815 mInvalidRegion.orSelf(mDirtyRegion);
816 if (mInvalidRegion.isEmpty()) {
817 // nothing to do
818 return;
819 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820
821 if (UNLIKELY(mDebugRegion)) {
822 debugFlashRegions();
823 }
824
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825 // set the frame buffer
826 const DisplayHardware& hw(graphicPlane(0).displayHardware());
827 glMatrixMode(GL_MODELVIEW);
828 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829
830 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700831 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
832 (flags & DisplayHardware::BUFFER_PRESERVED))
833 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700834 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
835 // takes a rectangle, we must make sure to update that whole
836 // rectangle in that case
837 if (flags & DisplayHardware::SWAP_RECTANGLE) {
838 // FIXME: we really should be able to pass a region to
839 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 mDirtyRegion.set(mInvalidRegion.bounds());
841 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700842 // in the BUFFER_PRESERVED case, obviously, we can update only
843 // what's needed and nothing more.
844 // NOTE: this is NOT a common case, as preserving the backbuffer
845 // is costly and usually involves copying the whole update back.
846 }
847 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700848 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700849 // We need to redraw the rectangle that will be updated
850 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700851 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700852 // rectangle instead of a region (see DisplayHardware::flip())
853 mDirtyRegion.set(mInvalidRegion.bounds());
854 } else {
855 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 mDirtyRegion.set(hw.bounds());
857 mInvalidRegion = mDirtyRegion;
858 }
859 }
860
861 // compose all surfaces
862 composeSurfaces(mDirtyRegion);
863
864 // clear the dirty regions
865 mDirtyRegion.clear();
866}
867
868void SurfaceFlinger::composeSurfaces(const Region& dirty)
869{
870 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
871 // should never happen unless the window manager has a bug
872 // draw something...
873 drawWormhole();
874 }
875 const SurfaceFlinger& flinger(*this);
876 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
877 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700878 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700880 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881 const Region& visibleRegion(layer->visibleRegionScreen);
882 if (!visibleRegion.isEmpty()) {
883 const Region clip(dirty.intersect(visibleRegion));
884 if (!clip.isEmpty()) {
885 layer->draw(clip);
886 }
887 }
888 }
889}
890
891void SurfaceFlinger::unlockClients()
892{
893 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
894 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700895 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700897 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 layer->finishPageFlip();
899 }
900}
901
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902void SurfaceFlinger::debugFlashRegions()
903{
Mathias Agopian0926f502009-05-04 14:17:04 -0700904 const DisplayHardware& hw(graphicPlane(0).displayHardware());
905 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700906
907 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
908 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700909 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopian0926f502009-05-04 14:17:04 -0700910 mDirtyRegion.bounds() : hw.bounds());
911 composeSurfaces(repaint);
912 }
913
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914 glDisable(GL_TEXTURE_2D);
915 glDisable(GL_BLEND);
916 glDisable(GL_DITHER);
917 glDisable(GL_SCISSOR_TEST);
918
Mathias Agopian0926f502009-05-04 14:17:04 -0700919 static int toggle = 0;
920 toggle = 1 - toggle;
921 if (toggle) {
922 glColor4x(0x10000, 0, 0x10000, 0x10000);
923 } else {
924 glColor4x(0x10000, 0x10000, 0, 0x10000);
925 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926
Mathias Agopian20f68782009-05-11 00:03:41 -0700927 Region::const_iterator it = mDirtyRegion.begin();
928 Region::const_iterator const end = mDirtyRegion.end();
929 while (it != end) {
930 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 GLfloat vertices[][2] = {
932 { r.left, r.top },
933 { r.left, r.bottom },
934 { r.right, r.bottom },
935 { r.right, r.top }
936 };
937 glVertexPointer(2, GL_FLOAT, 0, vertices);
938 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
939 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700940
Mathias Agopianb8a55602009-06-26 19:06:36 -0700941 if (mInvalidRegion.isEmpty()) {
942 mDirtyRegion.dump("mDirtyRegion");
943 mInvalidRegion.dump("mInvalidRegion");
944 }
945 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946
947 if (mDebugRegion > 1)
948 usleep(mDebugRegion * 1000);
949
950 glEnable(GL_SCISSOR_TEST);
951 //mDirtyRegion.dump("mDirtyRegion");
952}
953
954void SurfaceFlinger::drawWormhole() const
955{
956 const Region region(mWormholeRegion.intersect(mDirtyRegion));
957 if (region.isEmpty())
958 return;
959
960 const DisplayHardware& hw(graphicPlane(0).displayHardware());
961 const int32_t width = hw.getWidth();
962 const int32_t height = hw.getHeight();
963
964 glDisable(GL_BLEND);
965 glDisable(GL_DITHER);
966
967 if (LIKELY(!mDebugBackground)) {
968 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700969 Region::const_iterator it = region.begin();
970 Region::const_iterator const end = region.end();
971 while (it != end) {
972 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 const GLint sy = height - (r.top + r.height());
974 glScissor(r.left, sy, r.width(), r.height());
975 glClear(GL_COLOR_BUFFER_BIT);
976 }
977 } else {
978 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
979 { width, height }, { 0, height } };
980 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
981 glVertexPointer(2, GL_SHORT, 0, vertices);
982 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
983 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
984 glEnable(GL_TEXTURE_2D);
985 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
986 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
987 glMatrixMode(GL_TEXTURE);
988 glLoadIdentity();
989 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700990 Region::const_iterator it = region.begin();
991 Region::const_iterator const end = region.end();
992 while (it != end) {
993 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 const GLint sy = height - (r.top + r.height());
995 glScissor(r.left, sy, r.width(), r.height());
996 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
997 }
998 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
999 }
1000}
1001
1002void SurfaceFlinger::debugShowFPS() const
1003{
1004 static int mFrameCount;
1005 static int mLastFrameCount = 0;
1006 static nsecs_t mLastFpsTime = 0;
1007 static float mFps = 0;
1008 mFrameCount++;
1009 nsecs_t now = systemTime();
1010 nsecs_t diff = now - mLastFpsTime;
1011 if (diff > ms2ns(250)) {
1012 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1013 mLastFpsTime = now;
1014 mLastFrameCount = mFrameCount;
1015 }
1016 // XXX: mFPS has the value we want
1017 }
1018
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001019status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020{
1021 Mutex::Autolock _l(mStateLock);
1022 addLayer_l(layer);
1023 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1024 return NO_ERROR;
1025}
1026
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001027status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028{
1029 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001030 status_t err = purgatorizeLayer_l(layer);
1031 if (err == NO_ERROR)
1032 setTransactionFlags(eTransactionNeeded);
1033 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034}
1035
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001036status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037{
1038 layer->forceVisibilityTransaction();
1039 setTransactionFlags(eTraversalNeeded);
1040 return NO_ERROR;
1041}
1042
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001043status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044{
Mathias Agopian0852e672009-09-04 19:50:23 -07001045 if (layer == 0)
1046 return BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 ssize_t i = mCurrentState.layersSortedByZ.add(
1048 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001049 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1050 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 mLayerMap.add(lbc->serverIndex(), lbc);
1052 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 return NO_ERROR;
1054}
1055
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001056status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057{
1058 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1059 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001060 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001061 sp<LayerBaseClient> layer =
1062 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001063 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 mLayerMap.removeItem(layer->serverIndex());
1065 }
1066 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
1080 // from the user because there is a race between BClient::destroySurface(),
1081 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001082 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1083}
1084
1085
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086void SurfaceFlinger::free_resources_l()
1087{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001089 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 const size_t count = disconnectedClients.size();
1091 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001092 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094 }
1095 disconnectedClients.clear();
1096}
1097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1099{
1100 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1101}
1102
1103uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1104{
1105 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1106 if ((old & flags)==0) { // wake the server up
1107 if (delay > 0) {
1108 signalDelayedEvent(delay);
1109 } else {
1110 signalEvent();
1111 }
1112 }
1113 return old;
1114}
1115
1116void SurfaceFlinger::openGlobalTransaction()
1117{
1118 android_atomic_inc(&mTransactionCount);
1119}
1120
1121void SurfaceFlinger::closeGlobalTransaction()
1122{
1123 if (android_atomic_dec(&mTransactionCount) == 1) {
1124 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001125
1126 // if there is a transaction with a resize, wait for it to
1127 // take effect before returning.
1128 Mutex::Autolock _l(mStateLock);
1129 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001130 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1131 if (CC_UNLIKELY(err != NO_ERROR)) {
1132 // just in case something goes wrong in SF, return to the
1133 // called after a few seconds.
1134 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1135 mResizeTransationPending = false;
1136 break;
1137 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 }
1140}
1141
1142status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1143{
1144 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1145 return BAD_VALUE;
1146
1147 Mutex::Autolock _l(mStateLock);
1148 mCurrentState.freezeDisplay = 1;
1149 setTransactionFlags(eTransactionNeeded);
1150
1151 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001152 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 return NO_ERROR;
1154}
1155
1156status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1157{
1158 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1159 return BAD_VALUE;
1160
1161 Mutex::Autolock _l(mStateLock);
1162 mCurrentState.freezeDisplay = 0;
1163 setTransactionFlags(eTransactionNeeded);
1164
1165 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001166 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 return NO_ERROR;
1168}
1169
Mathias Agopianc08731e2009-03-27 18:11:38 -07001170int SurfaceFlinger::setOrientation(DisplayID dpy,
1171 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172{
1173 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1174 return BAD_VALUE;
1175
1176 Mutex::Autolock _l(mStateLock);
1177 if (mCurrentState.orientation != orientation) {
1178 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001179 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 mCurrentState.orientation = orientation;
1181 setTransactionFlags(eTransactionNeeded);
1182 mTransactionCV.wait(mStateLock);
1183 } else {
1184 orientation = BAD_VALUE;
1185 }
1186 }
1187 return orientation;
1188}
1189
1190sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1191 ISurfaceFlingerClient::surface_data_t* params,
1192 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1193 uint32_t flags)
1194{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001195 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001197
1198 if (int32_t(w|h) < 0) {
1199 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1200 int(w), int(h));
1201 return surfaceHandle;
1202 }
1203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001205 sp<Client> client = mClientsMap.valueFor(clientId);
1206 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1208 return surfaceHandle;
1209 }
1210
1211 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001212 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1214 LOGE("createSurface() failed, generateId = %d", id);
1215 return surfaceHandle;
1216 }
1217
1218 switch (flags & eFXSurfaceMask) {
1219 case eFXSurfaceNormal:
1220 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001221 layer = createPushBuffersSurfaceLocked(client, d, id,
1222 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001224 layer = createNormalSurfaceLocked(client, d, id,
1225 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 }
1227 break;
1228 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001229 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230 break;
1231 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001232 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 break;
1234 }
1235
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001236 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 setTransactionFlags(eTransactionNeeded);
1238 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001239 if (surfaceHandle != 0) {
1240 params->token = surfaceHandle->getToken();
1241 params->identity = surfaceHandle->getIdentity();
1242 params->width = w;
1243 params->height = h;
1244 params->format = format;
1245 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247
1248 return surfaceHandle;
1249}
1250
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001251sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001252 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001253 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1254 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:
1263 format = PIXEL_FORMAT_RGB_565;
1264 break;
1265 }
1266
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001267 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001268 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 if (LIKELY(err == NO_ERROR)) {
1270 layer->initStates(w, h, flags);
1271 addLayer_l(layer);
1272 } else {
1273 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001274 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 }
1276 return layer;
1277}
1278
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001279sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001280 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1282{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001283 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 layer->initStates(w, h, flags);
1285 addLayer_l(layer);
1286 return layer;
1287}
1288
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001289sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001290 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1292{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001293 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 layer->initStates(w, h, flags);
1295 addLayer_l(layer);
1296 return layer;
1297}
1298
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001299sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001300 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1302{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001303 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 layer->initStates(w, h, flags);
1305 addLayer_l(layer);
1306 return layer;
1307}
1308
Mathias Agopian9a112062009-04-17 19:36:26 -07001309status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310{
Mathias Agopian9a112062009-04-17 19:36:26 -07001311 /*
1312 * called by the window manager, when a surface should be marked for
1313 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001314 *
1315 * The surface is removed from the current and drawing lists, but placed
1316 * in the purgatory queue, so it's not destroyed right-away (we need
1317 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001318 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001319
Mathias Agopian48d819a2009-09-10 19:41:18 -07001320 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001321 Mutex::Autolock _l(mStateLock);
1322 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001323 if (layer != 0) {
1324 err = purgatorizeLayer_l(layer);
1325 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001326 setTransactionFlags(eTransactionNeeded);
1327 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001328 }
1329 return err;
1330}
1331
1332status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1333{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001334 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001335
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001336 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001337 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001338 sp<LayerBaseClient> layer;
1339 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 MessageDestroySurface(
1341 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1342 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001344 sp<LayerBaseClient> l(layer);
1345 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001346 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001347 /*
1348 * remove the layer from the current list -- chances are that it's
1349 * not in the list anyway, because it should have been removed
1350 * already upon request of the client (eg: window manager).
1351 * However, a buggy client could have not done that.
1352 * Since we know we don't have any more clients, we don't need
1353 * to use the purgatory.
1354 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001355 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001356 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1357 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001358 return true;
1359 }
1360 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001361
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001362 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 return NO_ERROR;
1364}
1365
1366status_t SurfaceFlinger::setClientState(
1367 ClientID cid,
1368 int32_t count,
1369 const layer_state_t* states)
1370{
1371 Mutex::Autolock _l(mStateLock);
1372 uint32_t flags = 0;
1373 cid <<= 16;
1374 for (int i=0 ; i<count ; i++) {
1375 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001376 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 if (what & ePositionChanged) {
1380 if (layer->setPosition(s.x, s.y))
1381 flags |= eTraversalNeeded;
1382 }
1383 if (what & eLayerChanged) {
1384 if (layer->setLayer(s.z)) {
1385 mCurrentState.layersSortedByZ.reorder(
1386 layer, &Layer::compareCurrentStateZ);
1387 // we need traversal (state changed)
1388 // AND transaction (list changed)
1389 flags |= eTransactionNeeded|eTraversalNeeded;
1390 }
1391 }
1392 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001393 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001395 mResizeTransationPending = true;
1396 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 }
1398 if (what & eAlphaChanged) {
1399 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eMatrixChanged) {
1403 if (layer->setMatrix(s.matrix))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eTransparentRegionChanged) {
1407 if (layer->setTransparentRegionHint(s.transparentRegion))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eVisibilityChanged) {
1411 if (layer->setFlags(s.flags, s.mask))
1412 flags |= eTraversalNeeded;
1413 }
1414 }
1415 }
1416 if (flags) {
1417 setTransactionFlags(flags);
1418 }
1419 return NO_ERROR;
1420}
1421
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001422sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001424 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1425 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426}
1427
1428void SurfaceFlinger::screenReleased(int dpy)
1429{
1430 // this may be called by a signal handler, we can't do too much in here
1431 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1432 signalEvent();
1433}
1434
1435void SurfaceFlinger::screenAcquired(int dpy)
1436{
1437 // this may be called by a signal handler, we can't do too much in here
1438 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1439 signalEvent();
1440}
1441
1442status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1443{
1444 const size_t SIZE = 1024;
1445 char buffer[SIZE];
1446 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001447 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 snprintf(buffer, SIZE, "Permission Denial: "
1449 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1450 IPCThreadState::self()->getCallingPid(),
1451 IPCThreadState::self()->getCallingUid());
1452 result.append(buffer);
1453 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001454
1455 // figure out if we're stuck somewhere
1456 const nsecs_t now = systemTime();
1457 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1458 const nsecs_t inTransaction(mDebugInTransaction);
1459 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1460 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1461
1462 // Try to get the main lock, but don't insist if we can't
1463 // (this would indicate SF is stuck, but we want to be able to
1464 // print something in dumpsys).
1465 int retry = 3;
1466 while (mStateLock.tryLock()<0 && --retry>=0) {
1467 usleep(1000000);
1468 }
1469 const bool locked(retry >= 0);
1470 if (!locked) {
1471 snprintf(buffer, SIZE,
1472 "SurfaceFlinger appears to be unresponsive, "
1473 "dumping anyways (no locks held)\n");
1474 result.append(buffer);
1475 }
1476
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 size_t s = mClientsMap.size();
1478 char name[64];
1479 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001480 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 sprintf(name, " Client (id=0x%08x)", client->cid);
1482 client->dump(name);
1483 }
1484 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1485 const size_t count = currentLayers.size();
1486 for (size_t i=0 ; i<count ; i++) {
1487 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001488 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 const Layer::State& s = layer->drawingState();
1490 snprintf(buffer, SIZE,
1491 "+ %s %p\n"
1492 " "
1493 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
Mathias Agopian401c2572009-09-23 19:16:27 -07001494 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001496 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 s.z, layer->tx(), layer->ty(), s.w, s.h,
Mathias Agopian401c2572009-09-23 19:16:27 -07001498 layer->needsBlending(), layer->needsDithering(),
1499 layer->contentDirty,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 s.alpha, s.flags,
1501 s.transform[0], s.transform[1],
1502 s.transform[2], s.transform[3]);
1503 result.append(buffer);
1504 buffer[0] = 0;
1505 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001506 sp<LayerBaseClient> lbc =
1507 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1508 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001509 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 snprintf(buffer, SIZE,
1511 " "
1512 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001513 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 lbc->getIdentity());
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001515
1516 result.append(buffer);
1517 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001520 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1521 if (l != 0) {
Mathias Agopian86f73292009-09-17 01:35:28 -07001522 SharedBufferStack::Statistics stats = l->lcblk->getStats();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001523 result.append( l->lcblk->dump(" ") );
1524 sp<const Buffer> buf0(l->getBuffer(0));
1525 sp<const Buffer> buf1(l->getBuffer(1));
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001526 uint32_t w0=0, h0=0, s0=0;
1527 uint32_t w1=0, h1=0, s1=0;
1528 if (buf0 != 0) {
1529 w0 = buf0->getWidth();
1530 h0 = buf0->getHeight();
1531 s0 = buf0->getStride();
1532 }
1533 if (buf1 != 0) {
1534 w1 = buf1->getWidth();
1535 h1 = buf1->getHeight();
1536 s1 = buf1->getStride();
1537 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 snprintf(buffer, SIZE,
1539 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001540 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian86f73292009-09-17 01:35:28 -07001541 " freezeLock=%p, dq-q-time=%u us\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542 l->pixelFormat(),
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001543 w0, h0, s0, w1, h1, s1,
Mathias Agopian86f73292009-09-17 01:35:28 -07001544 l->getFreezeLock().get(), stats.totalTime);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001545 result.append(buffer);
1546 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 s.transparentRegion.dump(result, "transparentRegion");
1549 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1550 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1551 }
1552 mWormholeRegion.dump(result, "WormholeRegion");
1553 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1554 snprintf(buffer, SIZE,
1555 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1556 mFreezeDisplay?"yes":"no", mFreezeCount,
1557 mCurrentState.orientation, hw.canDraw());
1558 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001559 snprintf(buffer, SIZE,
1560 " last eglSwapBuffers() time: %f us\n"
1561 " last transaction time : %f us\n",
1562 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1563 result.append(buffer);
1564 if (inSwapBuffersDuration || !locked) {
1565 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1566 inSwapBuffersDuration/1000.0);
1567 result.append(buffer);
1568 }
1569 if (inTransactionDuration || !locked) {
1570 snprintf(buffer, SIZE, " transaction time: %f us\n",
1571 inTransactionDuration/1000.0);
1572 result.append(buffer);
1573 }
Mathias Agopian759fdb22009-07-02 17:33:40 -07001574 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001575 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001576 const BufferAllocator& alloc(BufferAllocator::get());
1577 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001578
1579 if (locked) {
1580 mStateLock.unlock();
1581 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 }
1583 write(fd, result.string(), result.size());
1584 return NO_ERROR;
1585}
1586
1587status_t SurfaceFlinger::onTransact(
1588 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1589{
1590 switch (code) {
1591 case CREATE_CONNECTION:
1592 case OPEN_GLOBAL_TRANSACTION:
1593 case CLOSE_GLOBAL_TRANSACTION:
1594 case SET_ORIENTATION:
1595 case FREEZE_DISPLAY:
1596 case UNFREEZE_DISPLAY:
1597 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 {
1599 // codes that require permission check
1600 IPCThreadState* ipc = IPCThreadState::self();
1601 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001602 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001603 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1604 LOGE("Permission Denial: "
1605 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1606 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 }
1608 }
1609 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1611 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001612 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001613 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1614 IPCThreadState* ipc = IPCThreadState::self();
1615 const int pid = ipc->getCallingPid();
1616 const int uid = ipc->getCallingUid();
1617 LOGE("Permission Denial: "
1618 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 return PERMISSION_DENIED;
1620 }
1621 int n;
1622 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001623 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001625 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626 return NO_ERROR;
1627 case 1002: // SHOW_UPDATES
1628 n = data.readInt32();
1629 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1630 return NO_ERROR;
1631 case 1003: // SHOW_BACKGROUND
1632 n = data.readInt32();
1633 mDebugBackground = n ? 1 : 0;
1634 return NO_ERROR;
1635 case 1004:{ // repaint everything
1636 Mutex::Autolock _l(mStateLock);
1637 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1638 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1639 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001640 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001642 case 1005:{ // force transaction
1643 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1644 return NO_ERROR;
1645 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 case 1007: // set mFreezeCount
1647 mFreezeCount = data.readInt32();
1648 return NO_ERROR;
1649 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001650 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651 reply->writeInt32(0);
1652 reply->writeInt32(mDebugRegion);
1653 reply->writeInt32(mDebugBackground);
1654 return NO_ERROR;
1655 case 1013: {
1656 Mutex::Autolock _l(mStateLock);
1657 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1658 reply->writeInt32(hw.getPageFlipCount());
1659 }
1660 return NO_ERROR;
1661 }
1662 }
1663 return err;
1664}
1665
1666// ---------------------------------------------------------------------------
1667#if 0
1668#pragma mark -
1669#endif
1670
1671Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1672 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1673{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001675 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001676
1677 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1678 "SurfaceFlinger Client control-block");
1679
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001680 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001681 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001682 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 }
1684}
1685
1686Client::~Client() {
1687 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001688 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 }
1690}
1691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692int32_t Client::generateId(int pid)
1693{
1694 const uint32_t i = clz( ~mBitmap );
1695 if (i >= NUM_LAYERS_MAX) {
1696 return NO_MEMORY;
1697 }
1698 mPid = pid;
1699 mInUse.add(uint8_t(i));
1700 mBitmap |= 1<<(31-i);
1701 return i;
1702}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001703
1704status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705{
1706 ssize_t idx = mInUse.indexOf(id);
1707 if (idx < 0)
1708 return NAME_NOT_FOUND;
1709 return mLayers.insertAt(layer, idx);
1710}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712void Client::free(int32_t id)
1713{
1714 ssize_t idx = mInUse.remove(uint8_t(id));
1715 if (idx >= 0) {
1716 mBitmap &= ~(1<<(31-id));
1717 mLayers.removeItemsAt(idx);
1718 }
1719}
1720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721bool Client::isValid(int32_t i) const {
1722 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1723}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001724
1725sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1726 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001728 if (idx >= 0) {
1729 lbc = mLayers[idx].promote();
1730 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1731 }
1732 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733}
1734
1735void Client::dump(const char* what)
1736{
1737}
1738
1739// ---------------------------------------------------------------------------
1740#if 0
1741#pragma mark -
1742#endif
1743
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001744BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 : mId(cid), mFlinger(flinger), mCblk(cblk)
1746{
1747}
1748
1749BClient::~BClient() {
1750 // destroy all resources attached to this client
1751 mFlinger->destroyConnection(mId);
1752}
1753
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001754sp<IMemoryHeap> BClient::getControlBlock() const {
1755 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756}
1757
1758sp<ISurface> BClient::createSurface(
1759 ISurfaceFlingerClient::surface_data_t* params, int pid,
1760 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1761 uint32_t flags)
1762{
1763 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1764}
1765
1766status_t BClient::destroySurface(SurfaceID sid)
1767{
1768 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001769 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001770}
1771
1772status_t BClient::setState(int32_t count, const layer_state_t* states)
1773{
1774 return mFlinger->setClientState(mId, count, states);
1775}
1776
1777// ---------------------------------------------------------------------------
1778
1779GraphicPlane::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
1792void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1793 mHw = hw;
1794}
1795
1796void GraphicPlane::setTransform(const Transform& tr) {
1797 mTransform = tr;
1798 mGlobalTransform = mOrientationTransform * mTransform;
1799}
1800
1801status_t GraphicPlane::orientationToTransfrom(
1802 int orientation, int w, int h, Transform* tr)
1803{
1804 float a, b, c, d, x, y;
1805 switch (orientation) {
1806 case ISurfaceComposer::eOrientationDefault:
1807 a=1; b=0; c=0; d=1; x=0; y=0;
1808 break;
1809 case ISurfaceComposer::eOrientation90:
1810 a=0; b=-1; c=1; d=0; x=w; y=0;
1811 break;
1812 case ISurfaceComposer::eOrientation180:
1813 a=-1; b=0; c=0; d=-1; x=w; y=h;
1814 break;
1815 case ISurfaceComposer::eOrientation270:
1816 a=0; b=1; c=-1; d=0; x=0; y=h;
1817 break;
1818 default:
1819 return BAD_VALUE;
1820 }
1821 tr->set(a, b, c, d);
1822 tr->set(x, y);
1823 return NO_ERROR;
1824}
1825
1826status_t GraphicPlane::setOrientation(int orientation)
1827{
1828 const DisplayHardware& hw(displayHardware());
1829 const float w = hw.getWidth();
1830 const float h = hw.getHeight();
1831
1832 if (orientation == ISurfaceComposer::eOrientationDefault) {
1833 // make sure the default orientation is optimal
1834 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001835 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836 mGlobalTransform = mTransform;
1837 return NO_ERROR;
1838 }
1839
1840 // If the rotation can be handled in hardware, this is where
1841 // the magic should happen.
1842 if (UNLIKELY(orientation == 42)) {
1843 float a, b, c, d, x, y;
1844 const float r = (3.14159265f / 180.0f) * 42.0f;
1845 const float si = sinf(r);
1846 const float co = cosf(r);
1847 a=co; b=-si; c=si; d=co;
1848 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1849 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1850 mOrientationTransform.set(a, b, c, d);
1851 mOrientationTransform.set(x, y);
1852 } else {
1853 GraphicPlane::orientationToTransfrom(orientation, w, h,
1854 &mOrientationTransform);
1855 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001856 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001857 mGlobalTransform = mOrientationTransform * mTransform;
1858 return NO_ERROR;
1859}
1860
1861const DisplayHardware& GraphicPlane::displayHardware() const {
1862 return *mHw;
1863}
1864
1865const Transform& GraphicPlane::transform() const {
1866 return mGlobalTransform;
1867}
1868
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001869EGLDisplay GraphicPlane::getEGLDisplay() const {
1870 return mHw->getEGLDisplay();
1871}
1872
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873// ---------------------------------------------------------------------------
1874
1875}; // namespace android