blob: eb0983ab58f48a5037343c31c03d8749195e46f6 [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 Agopian3d579642009-06-04 18:46:21 -07001076 // it's possible that we don't find a layer, because it might
1077 // have been destroyed already -- this is not technically an error
1078 // from the user because there is a race between BClient::destroySurface(),
1079 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001080 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1081}
1082
1083
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084void SurfaceFlinger::free_resources_l()
1085{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001087 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 const size_t count = disconnectedClients.size();
1089 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001090 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 }
1093 disconnectedClients.clear();
1094}
1095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1097{
1098 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1099}
1100
1101uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1102{
1103 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1104 if ((old & flags)==0) { // wake the server up
1105 if (delay > 0) {
1106 signalDelayedEvent(delay);
1107 } else {
1108 signalEvent();
1109 }
1110 }
1111 return old;
1112}
1113
1114void SurfaceFlinger::openGlobalTransaction()
1115{
1116 android_atomic_inc(&mTransactionCount);
1117}
1118
1119void SurfaceFlinger::closeGlobalTransaction()
1120{
1121 if (android_atomic_dec(&mTransactionCount) == 1) {
1122 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001123
1124 // if there is a transaction with a resize, wait for it to
1125 // take effect before returning.
1126 Mutex::Autolock _l(mStateLock);
1127 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001128 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1129 if (CC_UNLIKELY(err != NO_ERROR)) {
1130 // just in case something goes wrong in SF, return to the
1131 // called after a few seconds.
1132 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1133 mResizeTransationPending = false;
1134 break;
1135 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001136 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 }
1138}
1139
1140status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1141{
1142 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1143 return BAD_VALUE;
1144
1145 Mutex::Autolock _l(mStateLock);
1146 mCurrentState.freezeDisplay = 1;
1147 setTransactionFlags(eTransactionNeeded);
1148
1149 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001150 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151 return NO_ERROR;
1152}
1153
1154status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1155{
1156 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1157 return BAD_VALUE;
1158
1159 Mutex::Autolock _l(mStateLock);
1160 mCurrentState.freezeDisplay = 0;
1161 setTransactionFlags(eTransactionNeeded);
1162
1163 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001164 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 return NO_ERROR;
1166}
1167
Mathias Agopianc08731e2009-03-27 18:11:38 -07001168int SurfaceFlinger::setOrientation(DisplayID dpy,
1169 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170{
1171 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1172 return BAD_VALUE;
1173
1174 Mutex::Autolock _l(mStateLock);
1175 if (mCurrentState.orientation != orientation) {
1176 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001177 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 mCurrentState.orientation = orientation;
1179 setTransactionFlags(eTransactionNeeded);
1180 mTransactionCV.wait(mStateLock);
1181 } else {
1182 orientation = BAD_VALUE;
1183 }
1184 }
1185 return orientation;
1186}
1187
1188sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1189 ISurfaceFlingerClient::surface_data_t* params,
1190 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1191 uint32_t flags)
1192{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001193 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001195
1196 if (int32_t(w|h) < 0) {
1197 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1198 int(w), int(h));
1199 return surfaceHandle;
1200 }
1201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001203 sp<Client> client = mClientsMap.valueFor(clientId);
1204 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1206 return surfaceHandle;
1207 }
1208
1209 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001210 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1212 LOGE("createSurface() failed, generateId = %d", id);
1213 return surfaceHandle;
1214 }
1215
1216 switch (flags & eFXSurfaceMask) {
1217 case eFXSurfaceNormal:
1218 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001219 layer = createPushBuffersSurfaceLocked(client, d, id,
1220 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001222 layer = createNormalSurfaceLocked(client, d, id,
1223 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 }
1225 break;
1226 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001227 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 break;
1229 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001230 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 break;
1232 }
1233
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001234 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 setTransactionFlags(eTransactionNeeded);
1236 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001237 if (surfaceHandle != 0) {
1238 params->token = surfaceHandle->getToken();
1239 params->identity = surfaceHandle->getIdentity();
1240 params->width = w;
1241 params->height = h;
1242 params->format = format;
1243 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 }
1245
1246 return surfaceHandle;
1247}
1248
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001249sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001250 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001251 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1252 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253{
1254 // initialize the surfaces
1255 switch (format) { // TODO: take h/w into account
1256 case PIXEL_FORMAT_TRANSPARENT:
1257 case PIXEL_FORMAT_TRANSLUCENT:
1258 format = PIXEL_FORMAT_RGBA_8888;
1259 break;
1260 case PIXEL_FORMAT_OPAQUE:
1261 format = PIXEL_FORMAT_RGB_565;
1262 break;
1263 }
1264
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001265 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001266 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 if (LIKELY(err == NO_ERROR)) {
1268 layer->initStates(w, h, flags);
1269 addLayer_l(layer);
1270 } else {
1271 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001272 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 }
1274 return layer;
1275}
1276
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001277sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001278 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1280{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001281 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 layer->initStates(w, h, flags);
1283 addLayer_l(layer);
1284 return layer;
1285}
1286
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001288 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1290{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001291 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 layer->initStates(w, h, flags);
1293 addLayer_l(layer);
1294 return layer;
1295}
1296
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001297sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001298 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1300{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001301 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 layer->initStates(w, h, flags);
1303 addLayer_l(layer);
1304 return layer;
1305}
1306
Mathias Agopian9a112062009-04-17 19:36:26 -07001307status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308{
Mathias Agopian9a112062009-04-17 19:36:26 -07001309 /*
1310 * called by the window manager, when a surface should be marked for
1311 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001312 *
1313 * The surface is removed from the current and drawing lists, but placed
1314 * in the purgatory queue, so it's not destroyed right-away (we need
1315 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001316 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001317
Mathias Agopian48d819a2009-09-10 19:41:18 -07001318 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001319 Mutex::Autolock _l(mStateLock);
1320 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001321 if (layer != 0) {
1322 err = purgatorizeLayer_l(layer);
1323 if (err == NO_ERROR) {
1324 layer->onRemoved();
1325 setTransactionFlags(eTransactionNeeded);
1326 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001327 }
1328 return err;
1329}
1330
1331status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1332{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001333 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001334
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001335 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001336 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001337 sp<LayerBaseClient> layer;
1338 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001339 MessageDestroySurface(
1340 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1341 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001342 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001343 sp<LayerBaseClient> l(layer);
1344 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001345 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001346 /*
1347 * remove the layer from the current list -- chances are that it's
1348 * not in the list anyway, because it should have been removed
1349 * already upon request of the client (eg: window manager).
1350 * However, a buggy client could have not done that.
1351 * Since we know we don't have any more clients, we don't need
1352 * to use the purgatory.
1353 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001354 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001355 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1356 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001357 return true;
1358 }
1359 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001360
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001361 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 return NO_ERROR;
1363}
1364
1365status_t SurfaceFlinger::setClientState(
1366 ClientID cid,
1367 int32_t count,
1368 const layer_state_t* states)
1369{
1370 Mutex::Autolock _l(mStateLock);
1371 uint32_t flags = 0;
1372 cid <<= 16;
1373 for (int i=0 ; i<count ; i++) {
1374 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001375 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001376 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 if (what & ePositionChanged) {
1379 if (layer->setPosition(s.x, s.y))
1380 flags |= eTraversalNeeded;
1381 }
1382 if (what & eLayerChanged) {
1383 if (layer->setLayer(s.z)) {
1384 mCurrentState.layersSortedByZ.reorder(
1385 layer, &Layer::compareCurrentStateZ);
1386 // we need traversal (state changed)
1387 // AND transaction (list changed)
1388 flags |= eTransactionNeeded|eTraversalNeeded;
1389 }
1390 }
1391 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001392 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001394 mResizeTransationPending = true;
1395 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 }
1397 if (what & eAlphaChanged) {
1398 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1399 flags |= eTraversalNeeded;
1400 }
1401 if (what & eMatrixChanged) {
1402 if (layer->setMatrix(s.matrix))
1403 flags |= eTraversalNeeded;
1404 }
1405 if (what & eTransparentRegionChanged) {
1406 if (layer->setTransparentRegionHint(s.transparentRegion))
1407 flags |= eTraversalNeeded;
1408 }
1409 if (what & eVisibilityChanged) {
1410 if (layer->setFlags(s.flags, s.mask))
1411 flags |= eTraversalNeeded;
1412 }
1413 }
1414 }
1415 if (flags) {
1416 setTransactionFlags(flags);
1417 }
1418 return NO_ERROR;
1419}
1420
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001421sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001423 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1424 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425}
1426
1427void SurfaceFlinger::screenReleased(int dpy)
1428{
1429 // this may be called by a signal handler, we can't do too much in here
1430 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1431 signalEvent();
1432}
1433
1434void SurfaceFlinger::screenAcquired(int dpy)
1435{
1436 // this may be called by a signal handler, we can't do too much in here
1437 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1438 signalEvent();
1439}
1440
1441status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1442{
1443 const size_t SIZE = 1024;
1444 char buffer[SIZE];
1445 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001446 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447 snprintf(buffer, SIZE, "Permission Denial: "
1448 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1449 IPCThreadState::self()->getCallingPid(),
1450 IPCThreadState::self()->getCallingUid());
1451 result.append(buffer);
1452 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001453
1454 // figure out if we're stuck somewhere
1455 const nsecs_t now = systemTime();
1456 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1457 const nsecs_t inTransaction(mDebugInTransaction);
1458 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1459 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1460
1461 // Try to get the main lock, but don't insist if we can't
1462 // (this would indicate SF is stuck, but we want to be able to
1463 // print something in dumpsys).
1464 int retry = 3;
1465 while (mStateLock.tryLock()<0 && --retry>=0) {
1466 usleep(1000000);
1467 }
1468 const bool locked(retry >= 0);
1469 if (!locked) {
1470 snprintf(buffer, SIZE,
1471 "SurfaceFlinger appears to be unresponsive, "
1472 "dumping anyways (no locks held)\n");
1473 result.append(buffer);
1474 }
1475
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 size_t s = mClientsMap.size();
1477 char name[64];
1478 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001479 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 sprintf(name, " Client (id=0x%08x)", client->cid);
1481 client->dump(name);
1482 }
1483 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1484 const size_t count = currentLayers.size();
1485 for (size_t i=0 ; i<count ; i++) {
1486 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001487 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 const Layer::State& s = layer->drawingState();
1489 snprintf(buffer, SIZE,
1490 "+ %s %p\n"
1491 " "
1492 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
Mathias Agopian401c2572009-09-23 19:16:27 -07001493 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001495 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 s.z, layer->tx(), layer->ty(), s.w, s.h,
Mathias Agopian401c2572009-09-23 19:16:27 -07001497 layer->needsBlending(), layer->needsDithering(),
1498 layer->contentDirty,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499 s.alpha, s.flags,
1500 s.transform[0], s.transform[1],
1501 s.transform[2], s.transform[3]);
1502 result.append(buffer);
1503 buffer[0] = 0;
1504 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001505 sp<LayerBaseClient> lbc =
1506 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1507 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001508 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 snprintf(buffer, SIZE,
1510 " "
1511 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001512 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 lbc->getIdentity());
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001514
1515 result.append(buffer);
1516 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001519 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1520 if (l != 0) {
Mathias Agopian86f73292009-09-17 01:35:28 -07001521 SharedBufferStack::Statistics stats = l->lcblk->getStats();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001522 result.append( l->lcblk->dump(" ") );
1523 sp<const Buffer> buf0(l->getBuffer(0));
1524 sp<const Buffer> buf1(l->getBuffer(1));
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001525 uint32_t w0=0, h0=0, s0=0;
1526 uint32_t w1=0, h1=0, s1=0;
1527 if (buf0 != 0) {
1528 w0 = buf0->getWidth();
1529 h0 = buf0->getHeight();
1530 s0 = buf0->getStride();
1531 }
1532 if (buf1 != 0) {
1533 w1 = buf1->getWidth();
1534 h1 = buf1->getHeight();
1535 s1 = buf1->getStride();
1536 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 snprintf(buffer, SIZE,
1538 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001539 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian86f73292009-09-17 01:35:28 -07001540 " freezeLock=%p, dq-q-time=%u us\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 l->pixelFormat(),
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001542 w0, h0, s0, w1, h1, s1,
Mathias Agopian86f73292009-09-17 01:35:28 -07001543 l->getFreezeLock().get(), stats.totalTime);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001544 result.append(buffer);
1545 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 s.transparentRegion.dump(result, "transparentRegion");
1548 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1549 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1550 }
1551 mWormholeRegion.dump(result, "WormholeRegion");
1552 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1553 snprintf(buffer, SIZE,
1554 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1555 mFreezeDisplay?"yes":"no", mFreezeCount,
1556 mCurrentState.orientation, hw.canDraw());
1557 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001558 snprintf(buffer, SIZE,
1559 " last eglSwapBuffers() time: %f us\n"
1560 " last transaction time : %f us\n",
1561 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1562 result.append(buffer);
1563 if (inSwapBuffersDuration || !locked) {
1564 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1565 inSwapBuffersDuration/1000.0);
1566 result.append(buffer);
1567 }
1568 if (inTransactionDuration || !locked) {
1569 snprintf(buffer, SIZE, " transaction time: %f us\n",
1570 inTransactionDuration/1000.0);
1571 result.append(buffer);
1572 }
Mathias Agopian759fdb22009-07-02 17:33:40 -07001573 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001574 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001575 const BufferAllocator& alloc(BufferAllocator::get());
1576 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001577
1578 if (locked) {
1579 mStateLock.unlock();
1580 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 }
1582 write(fd, result.string(), result.size());
1583 return NO_ERROR;
1584}
1585
1586status_t SurfaceFlinger::onTransact(
1587 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1588{
1589 switch (code) {
1590 case CREATE_CONNECTION:
1591 case OPEN_GLOBAL_TRANSACTION:
1592 case CLOSE_GLOBAL_TRANSACTION:
1593 case SET_ORIENTATION:
1594 case FREEZE_DISPLAY:
1595 case UNFREEZE_DISPLAY:
1596 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 {
1598 // codes that require permission check
1599 IPCThreadState* ipc = IPCThreadState::self();
1600 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001601 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001602 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1603 LOGE("Permission Denial: "
1604 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1605 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 }
1607 }
1608 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1610 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001611 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001612 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1613 IPCThreadState* ipc = IPCThreadState::self();
1614 const int pid = ipc->getCallingPid();
1615 const int uid = ipc->getCallingUid();
1616 LOGE("Permission Denial: "
1617 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 return PERMISSION_DENIED;
1619 }
1620 int n;
1621 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001622 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001624 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 return NO_ERROR;
1626 case 1002: // SHOW_UPDATES
1627 n = data.readInt32();
1628 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1629 return NO_ERROR;
1630 case 1003: // SHOW_BACKGROUND
1631 n = data.readInt32();
1632 mDebugBackground = n ? 1 : 0;
1633 return NO_ERROR;
1634 case 1004:{ // repaint everything
1635 Mutex::Autolock _l(mStateLock);
1636 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1637 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1638 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001639 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001641 case 1005:{ // force transaction
1642 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1643 return NO_ERROR;
1644 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 case 1007: // set mFreezeCount
1646 mFreezeCount = data.readInt32();
1647 return NO_ERROR;
1648 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001649 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 reply->writeInt32(0);
1651 reply->writeInt32(mDebugRegion);
1652 reply->writeInt32(mDebugBackground);
1653 return NO_ERROR;
1654 case 1013: {
1655 Mutex::Autolock _l(mStateLock);
1656 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1657 reply->writeInt32(hw.getPageFlipCount());
1658 }
1659 return NO_ERROR;
1660 }
1661 }
1662 return err;
1663}
1664
1665// ---------------------------------------------------------------------------
1666#if 0
1667#pragma mark -
1668#endif
1669
1670Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1671 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1672{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001674 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001675
1676 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1677 "SurfaceFlinger Client control-block");
1678
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001679 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001680 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001681 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 }
1683}
1684
1685Client::~Client() {
1686 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001687 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688 }
1689}
1690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691int32_t Client::generateId(int pid)
1692{
1693 const uint32_t i = clz( ~mBitmap );
1694 if (i >= NUM_LAYERS_MAX) {
1695 return NO_MEMORY;
1696 }
1697 mPid = pid;
1698 mInUse.add(uint8_t(i));
1699 mBitmap |= 1<<(31-i);
1700 return i;
1701}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001702
1703status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704{
1705 ssize_t idx = mInUse.indexOf(id);
1706 if (idx < 0)
1707 return NAME_NOT_FOUND;
1708 return mLayers.insertAt(layer, idx);
1709}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001710
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711void Client::free(int32_t id)
1712{
1713 ssize_t idx = mInUse.remove(uint8_t(id));
1714 if (idx >= 0) {
1715 mBitmap &= ~(1<<(31-id));
1716 mLayers.removeItemsAt(idx);
1717 }
1718}
1719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720bool Client::isValid(int32_t i) const {
1721 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1722}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001723
1724sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1725 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001727 if (idx >= 0) {
1728 lbc = mLayers[idx].promote();
1729 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1730 }
1731 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732}
1733
1734void Client::dump(const char* what)
1735{
1736}
1737
1738// ---------------------------------------------------------------------------
1739#if 0
1740#pragma mark -
1741#endif
1742
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001743BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 : mId(cid), mFlinger(flinger), mCblk(cblk)
1745{
1746}
1747
1748BClient::~BClient() {
1749 // destroy all resources attached to this client
1750 mFlinger->destroyConnection(mId);
1751}
1752
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001753sp<IMemoryHeap> BClient::getControlBlock() const {
1754 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755}
1756
1757sp<ISurface> BClient::createSurface(
1758 ISurfaceFlingerClient::surface_data_t* params, int pid,
1759 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1760 uint32_t flags)
1761{
1762 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1763}
1764
1765status_t BClient::destroySurface(SurfaceID sid)
1766{
1767 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001768 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769}
1770
1771status_t BClient::setState(int32_t count, const layer_state_t* states)
1772{
1773 return mFlinger->setClientState(mId, count, states);
1774}
1775
1776// ---------------------------------------------------------------------------
1777
1778GraphicPlane::GraphicPlane()
1779 : mHw(0)
1780{
1781}
1782
1783GraphicPlane::~GraphicPlane() {
1784 delete mHw;
1785}
1786
1787bool GraphicPlane::initialized() const {
1788 return mHw ? true : false;
1789}
1790
1791void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1792 mHw = hw;
1793}
1794
1795void GraphicPlane::setTransform(const Transform& tr) {
1796 mTransform = tr;
1797 mGlobalTransform = mOrientationTransform * mTransform;
1798}
1799
1800status_t GraphicPlane::orientationToTransfrom(
1801 int orientation, int w, int h, Transform* tr)
1802{
1803 float a, b, c, d, x, y;
1804 switch (orientation) {
1805 case ISurfaceComposer::eOrientationDefault:
1806 a=1; b=0; c=0; d=1; x=0; y=0;
1807 break;
1808 case ISurfaceComposer::eOrientation90:
1809 a=0; b=-1; c=1; d=0; x=w; y=0;
1810 break;
1811 case ISurfaceComposer::eOrientation180:
1812 a=-1; b=0; c=0; d=-1; x=w; y=h;
1813 break;
1814 case ISurfaceComposer::eOrientation270:
1815 a=0; b=1; c=-1; d=0; x=0; y=h;
1816 break;
1817 default:
1818 return BAD_VALUE;
1819 }
1820 tr->set(a, b, c, d);
1821 tr->set(x, y);
1822 return NO_ERROR;
1823}
1824
1825status_t GraphicPlane::setOrientation(int orientation)
1826{
1827 const DisplayHardware& hw(displayHardware());
1828 const float w = hw.getWidth();
1829 const float h = hw.getHeight();
1830
1831 if (orientation == ISurfaceComposer::eOrientationDefault) {
1832 // make sure the default orientation is optimal
1833 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001834 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835 mGlobalTransform = mTransform;
1836 return NO_ERROR;
1837 }
1838
1839 // If the rotation can be handled in hardware, this is where
1840 // the magic should happen.
1841 if (UNLIKELY(orientation == 42)) {
1842 float a, b, c, d, x, y;
1843 const float r = (3.14159265f / 180.0f) * 42.0f;
1844 const float si = sinf(r);
1845 const float co = cosf(r);
1846 a=co; b=-si; c=si; d=co;
1847 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1848 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1849 mOrientationTransform.set(a, b, c, d);
1850 mOrientationTransform.set(x, y);
1851 } else {
1852 GraphicPlane::orientationToTransfrom(orientation, w, h,
1853 &mOrientationTransform);
1854 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001855 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001856 mGlobalTransform = mOrientationTransform * mTransform;
1857 return NO_ERROR;
1858}
1859
1860const DisplayHardware& GraphicPlane::displayHardware() const {
1861 return *mHw;
1862}
1863
1864const Transform& GraphicPlane::transform() const {
1865 return mGlobalTransform;
1866}
1867
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001868EGLDisplay GraphicPlane::getEGLDisplay() const {
1869 return mHw->getEGLDisplay();
1870}
1871
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001872// ---------------------------------------------------------------------------
1873
1874}; // namespace android