blob: 831c446fff9ca9c61578fbf74bc235704c5c332a [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());
494 if (LIKELY(hw.canDraw())) {
495 // 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{
Mathias Agopian0926f502009-05-04 14:17:04 -0700515 if (isFrozen()) {
516 // we are not allowed to draw, but pause a bit to make sure
517 // apps don't end up using the whole CPU, if they depend on
518 // surfaceflinger for synchronization.
519 usleep(8333); // 8.3ms ~ 120fps
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 return;
521 }
522
523 if (!mInvalidRegion.isEmpty()) {
524 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700525 const nsecs_t now = systemTime();
526 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700528 mLastSwapBufferTime = systemTime() - now;
529 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 }
532}
533
534void SurfaceFlinger::handleConsoleEvents()
535{
536 // something to do with the console
537 const DisplayHardware& hw = graphicPlane(0).displayHardware();
538
539 int what = android_atomic_and(0, &mConsoleSignals);
540 if (what & eConsoleAcquired) {
541 hw.acquireScreen();
542 }
543
544 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700545 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 hw.releaseScreen();
548 }
549
550 if (what & eConsoleReleased) {
551 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 hw.releaseScreen();
553 } else {
554 mDeferReleaseConsole = true;
555 }
556 }
557
558 mDirtyRegion.set(hw.bounds());
559}
560
561void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
562{
Mathias Agopian3d579642009-06-04 18:46:21 -0700563 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564
Mathias Agopian3d579642009-06-04 18:46:21 -0700565 { // scope for the lock
566 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700567 const nsecs_t now = systemTime();
568 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700569 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700570 mLastTransactionTime = systemTime() - now;
571 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700572 }
573
574 // do this without lock held
575 const size_t count = ditchedLayers.size();
576 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700577 if (ditchedLayers[i] != 0) {
578 //LOGD("ditching layer %p", ditchedLayers[i].get());
579 ditchedLayers[i]->ditch();
580 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700581 }
582}
583
584void SurfaceFlinger::handleTransactionLocked(
585 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
586{
587 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 const size_t count = currentLayers.size();
589
590 /*
591 * Traversal of the children
592 * (perform the transaction for each of them if needed)
593 */
594
595 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
596 if (layersNeedTransaction) {
597 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700598 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
600 if (!trFlags) continue;
601
602 const uint32_t flags = layer->doTransaction(0);
603 if (flags & Layer::eVisibleRegion)
604 mVisibleRegionsDirty = true;
605
606 if (flags & Layer::eRestartTransaction) {
607 // restart the transaction, but back-off a little
608 layer->setTransactionFlags(eTransactionNeeded);
609 setTransactionFlags(eTraversalNeeded, ms2ns(8));
610 }
611 }
612 }
613
614 /*
615 * Perform our own transaction if needed
616 */
617
618 if (transactionFlags & eTransactionNeeded) {
619 if (mCurrentState.orientation != mDrawingState.orientation) {
620 // the orientation has changed, recompute all visible regions
621 // and invalidate everything.
622
623 const int dpy = 0;
624 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700625 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 GraphicPlane& plane(graphicPlane(dpy));
627 plane.setOrientation(orientation);
628
629 // update the shared control block
630 const DisplayHardware& hw(plane.displayHardware());
631 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
632 dcblk->orientation = orientation;
633 if (orientation & eOrientationSwapMask) {
634 // 90 or 270 degrees orientation
635 dcblk->w = hw.getHeight();
636 dcblk->h = hw.getWidth();
637 } else {
638 dcblk->w = hw.getWidth();
639 dcblk->h = hw.getHeight();
640 }
641
642 mVisibleRegionsDirty = true;
643 mDirtyRegion.set(hw.bounds());
Mathias Agopianc08731e2009-03-27 18:11:38 -0700644 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 }
646
647 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
648 // freezing or unfreezing the display -> trigger animation if needed
649 mFreezeDisplay = mCurrentState.freezeDisplay;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 }
651
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700652 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
653 // layers have been added
654 mVisibleRegionsDirty = true;
655 }
656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 // some layers might have been removed, so
658 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700659 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700660 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700662 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700663 const size_t count = previousLayers.size();
664 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700665 const sp<LayerBase>& layer(previousLayers[i]);
666 if (currentLayers.indexOf( layer ) < 0) {
667 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700668 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700669 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700670 }
671 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 }
673
674 // get rid of all resources we don't need anymore
675 // (layers and clients)
676 free_resources_l();
677 }
678
679 commitTransaction();
680}
681
682sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
683{
684 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
685}
686
687void SurfaceFlinger::computeVisibleRegions(
688 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
689{
690 const GraphicPlane& plane(graphicPlane(0));
691 const Transform& planeTransform(plane.transform());
692
693 Region aboveOpaqueLayers;
694 Region aboveCoveredLayers;
695 Region dirty;
696
697 bool secureFrameBuffer = false;
698
699 size_t i = currentLayers.size();
700 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700701 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702 layer->validateVisibility(planeTransform);
703
704 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700705 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706
707 // handle hidden surfaces by setting the visible region to empty
708 Region opaqueRegion;
709 Region visibleRegion;
710 Region coveredRegion;
Mathias Agopian97011222009-07-28 10:57:27 -0700711 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700713 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 visibleRegion.set(bounds);
715 coveredRegion = visibleRegion;
716
717 // Remove the transparent area from the visible region
718 if (translucent) {
719 visibleRegion.subtractSelf(layer->transparentRegionScreen);
720 }
721
722 // compute the opaque region
723 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
724 // the opaque region is the visible region
725 opaqueRegion = visibleRegion;
726 }
727 }
728
729 // subtract the opaque region covered by the layers above us
730 visibleRegion.subtractSelf(aboveOpaqueLayers);
731 coveredRegion.andSelf(aboveCoveredLayers);
732
733 // compute this layer's dirty region
734 if (layer->contentDirty) {
735 // we need to invalidate the whole region
736 dirty = visibleRegion;
737 // as well, as the old visible region
738 dirty.orSelf(layer->visibleRegionScreen);
739 layer->contentDirty = false;
740 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700741 /* compute the exposed region:
742 * exposed = what's VISIBLE and NOT COVERED now
743 * but was COVERED before
744 */
745 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 }
747 dirty.subtractSelf(aboveOpaqueLayers);
748
749 // accumulate to the screen dirty region
750 dirtyRegion.orSelf(dirty);
751
Mathias Agopian62b74442009-04-14 23:02:51 -0700752 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700754 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755
756 // Store the visible region is screen space
757 layer->setVisibleRegion(visibleRegion);
758 layer->setCoveredRegion(coveredRegion);
759
Mathias Agopian97011222009-07-28 10:57:27 -0700760 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 if (layer->isSecure() && !visibleRegion.isEmpty()) {
762 secureFrameBuffer = true;
763 }
764 }
765
Mathias Agopian97011222009-07-28 10:57:27 -0700766 // invalidate the areas where a layer was removed
767 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
768 mDirtyRegionRemovedLayer.clear();
769
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 mSecureFrameBuffer = secureFrameBuffer;
771 opaqueRegion = aboveOpaqueLayers;
772}
773
774
775void SurfaceFlinger::commitTransaction()
776{
777 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700778 mResizeTransationPending = false;
779 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780}
781
782void SurfaceFlinger::handlePageFlip()
783{
784 bool visibleRegions = mVisibleRegionsDirty;
785 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
786 visibleRegions |= lockPageFlip(currentLayers);
787
788 const DisplayHardware& hw = graphicPlane(0).displayHardware();
789 const Region screenRegion(hw.bounds());
790 if (visibleRegions) {
791 Region opaqueRegion;
792 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
793 mWormholeRegion = screenRegion.subtract(opaqueRegion);
794 mVisibleRegionsDirty = false;
795 }
796
797 unlockPageFlip(currentLayers);
798 mDirtyRegion.andSelf(screenRegion);
799}
800
801bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
802{
803 bool recomputeVisibleRegions = false;
804 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700805 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700807 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 layer->lockPageFlip(recomputeVisibleRegions);
809 }
810 return recomputeVisibleRegions;
811}
812
813void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
814{
815 const GraphicPlane& plane(graphicPlane(0));
816 const Transform& planeTransform(plane.transform());
817 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700818 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700820 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 layer->unlockPageFlip(planeTransform, mDirtyRegion);
822 }
823}
824
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826void SurfaceFlinger::handleRepaint()
827{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700828 // compute the invalid region
829 mInvalidRegion.orSelf(mDirtyRegion);
830 if (mInvalidRegion.isEmpty()) {
831 // nothing to do
832 return;
833 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834
835 if (UNLIKELY(mDebugRegion)) {
836 debugFlashRegions();
837 }
838
Mathias Agopianb8a55602009-06-26 19:06:36 -0700839 // set the frame buffer
840 const DisplayHardware& hw(graphicPlane(0).displayHardware());
841 glMatrixMode(GL_MODELVIEW);
842 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843
844 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700845 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
846 (flags & DisplayHardware::BUFFER_PRESERVED))
847 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
849 // takes a rectangle, we must make sure to update that whole
850 // rectangle in that case
851 if (flags & DisplayHardware::SWAP_RECTANGLE) {
852 // FIXME: we really should be able to pass a region to
853 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 mDirtyRegion.set(mInvalidRegion.bounds());
855 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // in the BUFFER_PRESERVED case, obviously, we can update only
857 // what's needed and nothing more.
858 // NOTE: this is NOT a common case, as preserving the backbuffer
859 // is costly and usually involves copying the whole update back.
860 }
861 } else {
862 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
863 // We need to redraw the rectangle that will be updated
864 // (pushed to the framebuffer).
865 // This is needed because UPDATE_ON_DEMAND only takes one
866 // rectangle instead of a region (see DisplayHardware::flip())
867 mDirtyRegion.set(mInvalidRegion.bounds());
868 } else {
869 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870 mDirtyRegion.set(hw.bounds());
871 mInvalidRegion = mDirtyRegion;
872 }
873 }
874
875 // compose all surfaces
876 composeSurfaces(mDirtyRegion);
877
878 // clear the dirty regions
879 mDirtyRegion.clear();
880}
881
882void SurfaceFlinger::composeSurfaces(const Region& dirty)
883{
884 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
885 // should never happen unless the window manager has a bug
886 // draw something...
887 drawWormhole();
888 }
889 const SurfaceFlinger& flinger(*this);
890 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
891 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700892 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700894 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 const Region& visibleRegion(layer->visibleRegionScreen);
896 if (!visibleRegion.isEmpty()) {
897 const Region clip(dirty.intersect(visibleRegion));
898 if (!clip.isEmpty()) {
899 layer->draw(clip);
900 }
901 }
902 }
903}
904
905void SurfaceFlinger::unlockClients()
906{
907 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
908 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700909 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700911 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912 layer->finishPageFlip();
913 }
914}
915
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916void SurfaceFlinger::debugFlashRegions()
917{
Mathias Agopian0926f502009-05-04 14:17:04 -0700918 const DisplayHardware& hw(graphicPlane(0).displayHardware());
919 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700920
921 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
922 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700923 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
924 mDirtyRegion.bounds() : hw.bounds());
925 composeSurfaces(repaint);
926 }
927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 glDisable(GL_TEXTURE_2D);
929 glDisable(GL_BLEND);
930 glDisable(GL_DITHER);
931 glDisable(GL_SCISSOR_TEST);
932
Mathias Agopian0926f502009-05-04 14:17:04 -0700933 static int toggle = 0;
934 toggle = 1 - toggle;
935 if (toggle) {
936 glColor4x(0x10000, 0, 0x10000, 0x10000);
937 } else {
938 glColor4x(0x10000, 0x10000, 0, 0x10000);
939 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940
Mathias Agopian20f68782009-05-11 00:03:41 -0700941 Region::const_iterator it = mDirtyRegion.begin();
942 Region::const_iterator const end = mDirtyRegion.end();
943 while (it != end) {
944 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945 GLfloat vertices[][2] = {
946 { r.left, r.top },
947 { r.left, r.bottom },
948 { r.right, r.bottom },
949 { r.right, r.top }
950 };
951 glVertexPointer(2, GL_FLOAT, 0, vertices);
952 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
953 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700954
Mathias Agopianb8a55602009-06-26 19:06:36 -0700955 if (mInvalidRegion.isEmpty()) {
956 mDirtyRegion.dump("mDirtyRegion");
957 mInvalidRegion.dump("mInvalidRegion");
958 }
959 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960
961 if (mDebugRegion > 1)
962 usleep(mDebugRegion * 1000);
963
964 glEnable(GL_SCISSOR_TEST);
965 //mDirtyRegion.dump("mDirtyRegion");
966}
967
968void SurfaceFlinger::drawWormhole() const
969{
970 const Region region(mWormholeRegion.intersect(mDirtyRegion));
971 if (region.isEmpty())
972 return;
973
974 const DisplayHardware& hw(graphicPlane(0).displayHardware());
975 const int32_t width = hw.getWidth();
976 const int32_t height = hw.getHeight();
977
978 glDisable(GL_BLEND);
979 glDisable(GL_DITHER);
980
981 if (LIKELY(!mDebugBackground)) {
982 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700983 Region::const_iterator it = region.begin();
984 Region::const_iterator const end = region.end();
985 while (it != end) {
986 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 const GLint sy = height - (r.top + r.height());
988 glScissor(r.left, sy, r.width(), r.height());
989 glClear(GL_COLOR_BUFFER_BIT);
990 }
991 } else {
992 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
993 { width, height }, { 0, height } };
994 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
995 glVertexPointer(2, GL_SHORT, 0, vertices);
996 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
997 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
998 glEnable(GL_TEXTURE_2D);
999 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1000 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1001 glMatrixMode(GL_TEXTURE);
1002 glLoadIdentity();
1003 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001004 Region::const_iterator it = region.begin();
1005 Region::const_iterator const end = region.end();
1006 while (it != end) {
1007 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 const GLint sy = height - (r.top + r.height());
1009 glScissor(r.left, sy, r.width(), r.height());
1010 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1011 }
1012 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1013 }
1014}
1015
1016void SurfaceFlinger::debugShowFPS() const
1017{
1018 static int mFrameCount;
1019 static int mLastFrameCount = 0;
1020 static nsecs_t mLastFpsTime = 0;
1021 static float mFps = 0;
1022 mFrameCount++;
1023 nsecs_t now = systemTime();
1024 nsecs_t diff = now - mLastFpsTime;
1025 if (diff > ms2ns(250)) {
1026 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1027 mLastFpsTime = now;
1028 mLastFrameCount = mFrameCount;
1029 }
1030 // XXX: mFPS has the value we want
1031 }
1032
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001033status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034{
1035 Mutex::Autolock _l(mStateLock);
1036 addLayer_l(layer);
1037 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1038 return NO_ERROR;
1039}
1040
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001041status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042{
1043 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001044 status_t err = purgatorizeLayer_l(layer);
1045 if (err == NO_ERROR)
1046 setTransactionFlags(eTransactionNeeded);
1047 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048}
1049
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001050status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051{
1052 layer->forceVisibilityTransaction();
1053 setTransactionFlags(eTraversalNeeded);
1054 return NO_ERROR;
1055}
1056
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001057status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
Mathias Agopian0852e672009-09-04 19:50:23 -07001059 if (layer == 0)
1060 return BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061 ssize_t i = mCurrentState.layersSortedByZ.add(
1062 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001063 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1064 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 mLayerMap.add(lbc->serverIndex(), lbc);
1066 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 return NO_ERROR;
1068}
1069
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001070status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071{
1072 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1073 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001075 sp<LayerBaseClient> layer =
1076 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001077 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 mLayerMap.removeItem(layer->serverIndex());
1079 }
1080 return NO_ERROR;
1081 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001082 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083}
1084
Mathias Agopian9a112062009-04-17 19:36:26 -07001085status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1086{
Mathias Agopian1f0ffc42009-09-17 22:04:50 -07001087 // First add the layer to the purgatory list, which makes sure it won't
1088 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001089 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian1f0ffc42009-09-17 22:04:50 -07001090 if (err >= 0) {
1091 mLayerPurgatory.add(layerBase);
1092 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001093 // it's possible that we don't find a layer, because it might
1094 // have been destroyed already -- this is not technically an error
1095 // from the user because there is a race between BClient::destroySurface(),
1096 // ~BClient() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001097 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1098}
1099
1100
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101void SurfaceFlinger::free_resources_l()
1102{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 // free resources associated with disconnected clients
Mathias Agopianf9d93272009-06-19 17:00:27 -07001104 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 const size_t count = disconnectedClients.size();
1106 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001107 sp<Client> client = disconnectedClients[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 mTokens.release(client->cid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 }
1110 disconnectedClients.clear();
1111}
1112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1114{
1115 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1116}
1117
1118uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1119{
1120 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1121 if ((old & flags)==0) { // wake the server up
1122 if (delay > 0) {
1123 signalDelayedEvent(delay);
1124 } else {
1125 signalEvent();
1126 }
1127 }
1128 return old;
1129}
1130
1131void SurfaceFlinger::openGlobalTransaction()
1132{
1133 android_atomic_inc(&mTransactionCount);
1134}
1135
1136void SurfaceFlinger::closeGlobalTransaction()
1137{
1138 if (android_atomic_dec(&mTransactionCount) == 1) {
1139 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001140
1141 // if there is a transaction with a resize, wait for it to
1142 // take effect before returning.
1143 Mutex::Autolock _l(mStateLock);
1144 while (mResizeTransationPending) {
1145 mTransactionCV.wait(mStateLock);
1146 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 }
1148}
1149
1150status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1151{
1152 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1153 return BAD_VALUE;
1154
1155 Mutex::Autolock _l(mStateLock);
1156 mCurrentState.freezeDisplay = 1;
1157 setTransactionFlags(eTransactionNeeded);
1158
1159 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001160 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 return NO_ERROR;
1162}
1163
1164status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1165{
1166 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1167 return BAD_VALUE;
1168
1169 Mutex::Autolock _l(mStateLock);
1170 mCurrentState.freezeDisplay = 0;
1171 setTransactionFlags(eTransactionNeeded);
1172
1173 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001174 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 return NO_ERROR;
1176}
1177
Mathias Agopianc08731e2009-03-27 18:11:38 -07001178int SurfaceFlinger::setOrientation(DisplayID dpy,
1179 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180{
1181 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1182 return BAD_VALUE;
1183
1184 Mutex::Autolock _l(mStateLock);
1185 if (mCurrentState.orientation != orientation) {
1186 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001187 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 mCurrentState.orientation = orientation;
1189 setTransactionFlags(eTransactionNeeded);
1190 mTransactionCV.wait(mStateLock);
1191 } else {
1192 orientation = BAD_VALUE;
1193 }
1194 }
1195 return orientation;
1196}
1197
1198sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1199 ISurfaceFlingerClient::surface_data_t* params,
1200 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1201 uint32_t flags)
1202{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001203 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001205
1206 if (int32_t(w|h) < 0) {
1207 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1208 int(w), int(h));
1209 return surfaceHandle;
1210 }
1211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 Mutex::Autolock _l(mStateLock);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001213 sp<Client> client = mClientsMap.valueFor(clientId);
1214 if (UNLIKELY(client == 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1216 return surfaceHandle;
1217 }
1218
1219 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001220 int32_t id = client->generateId(pid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1222 LOGE("createSurface() failed, generateId = %d", id);
1223 return surfaceHandle;
1224 }
1225
1226 switch (flags & eFXSurfaceMask) {
1227 case eFXSurfaceNormal:
1228 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001229 layer = createPushBuffersSurfaceLocked(client, d, id,
1230 w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 } else {
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001232 layer = createNormalSurfaceLocked(client, d, id,
1233 w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 }
1235 break;
1236 case eFXSurfaceBlur:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001237 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 break;
1239 case eFXSurfaceDim:
Mathias Agopianf9d93272009-06-19 17:00:27 -07001240 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 break;
1242 }
1243
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001244 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 setTransactionFlags(eTransactionNeeded);
1246 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001247 if (surfaceHandle != 0) {
1248 params->token = surfaceHandle->getToken();
1249 params->identity = surfaceHandle->getIdentity();
1250 params->width = w;
1251 params->height = h;
1252 params->format = format;
1253 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 }
1255
1256 return surfaceHandle;
1257}
1258
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001259sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001260 const sp<Client>& client, DisplayID display,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001261 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1262 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263{
1264 // initialize the surfaces
1265 switch (format) { // TODO: take h/w into account
1266 case PIXEL_FORMAT_TRANSPARENT:
1267 case PIXEL_FORMAT_TRANSLUCENT:
1268 format = PIXEL_FORMAT_RGBA_8888;
1269 break;
1270 case PIXEL_FORMAT_OPAQUE:
1271 format = PIXEL_FORMAT_RGB_565;
1272 break;
1273 }
1274
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001275 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001276 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 if (LIKELY(err == NO_ERROR)) {
1278 layer->initStates(w, h, flags);
1279 addLayer_l(layer);
1280 } else {
1281 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001282 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 }
1284 return layer;
1285}
1286
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
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<LayerBlur> layer = new LayerBlur(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::createDimSurfaceLocked(
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<LayerDim> layer = new LayerDim(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 Agopian076b1cc2009-04-10 14:24:30 -07001307sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001308 const sp<Client>& client, DisplayID display,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1310{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001311 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 layer->initStates(w, h, flags);
1313 addLayer_l(layer);
1314 return layer;
1315}
1316
Mathias Agopian9a112062009-04-17 19:36:26 -07001317status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
Mathias Agopian9a112062009-04-17 19:36:26 -07001319 /*
1320 * called by the window manager, when a surface should be marked for
1321 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001322 *
1323 * The surface is removed from the current and drawing lists, but placed
1324 * in the purgatory queue, so it's not destroyed right-away (we need
1325 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001326 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001327
Mathias Agopian48d819a2009-09-10 19:41:18 -07001328 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001329 Mutex::Autolock _l(mStateLock);
1330 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001331 if (layer != 0) {
1332 err = purgatorizeLayer_l(layer);
1333 if (err == NO_ERROR) {
1334 layer->onRemoved();
1335 setTransactionFlags(eTransactionNeeded);
1336 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001337 }
1338 return err;
1339}
1340
1341status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1342{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001343 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001344
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001345 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001346 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001347 sp<LayerBaseClient> layer;
1348 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001349 MessageDestroySurface(
1350 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1351 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001352 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001353 sp<LayerBaseClient> l(layer);
1354 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001355 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001356 /*
1357 * remove the layer from the current list -- chances are that it's
1358 * not in the list anyway, because it should have been removed
1359 * already upon request of the client (eg: window manager).
1360 * However, a buggy client could have not done that.
1361 * Since we know we don't have any more clients, we don't need
1362 * to use the purgatory.
1363 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001364 status_t err = flinger->removeLayer_l(l);
Mathias Agopian1f0ffc42009-09-17 22:04:50 -07001365 if (err == NAME_NOT_FOUND) {
1366 // The surface wasn't in the current list, which means it was
1367 // removed already, which means it is in the purgatory,
1368 // and need to be removed from there.
1369 // This needs to happen from the main thread since its dtor
1370 // must run from there (b/c of OpenGL ES). Additionally, we
1371 // can't really acquire our internal lock from
1372 // destroySurface() -- see postMessage() below.
1373 ssize_t idx = flinger->mLayerPurgatory.remove(l);
1374 LOGE_IF(idx < 0,
1375 "layer=%p is not in the purgatory list", l.get());
1376 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001377 return true;
1378 }
1379 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001380
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001381 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 return NO_ERROR;
1383}
1384
1385status_t SurfaceFlinger::setClientState(
1386 ClientID cid,
1387 int32_t count,
1388 const layer_state_t* states)
1389{
1390 Mutex::Autolock _l(mStateLock);
1391 uint32_t flags = 0;
1392 cid <<= 16;
1393 for (int i=0 ; i<count ; i++) {
1394 const layer_state_t& s = states[i];
Mathias Agopian3d579642009-06-04 18:46:21 -07001395 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001396 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 if (what & ePositionChanged) {
1399 if (layer->setPosition(s.x, s.y))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eLayerChanged) {
1403 if (layer->setLayer(s.z)) {
1404 mCurrentState.layersSortedByZ.reorder(
1405 layer, &Layer::compareCurrentStateZ);
1406 // we need traversal (state changed)
1407 // AND transaction (list changed)
1408 flags |= eTransactionNeeded|eTraversalNeeded;
1409 }
1410 }
1411 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001412 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001414 mResizeTransationPending = true;
1415 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 }
1417 if (what & eAlphaChanged) {
1418 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1419 flags |= eTraversalNeeded;
1420 }
1421 if (what & eMatrixChanged) {
1422 if (layer->setMatrix(s.matrix))
1423 flags |= eTraversalNeeded;
1424 }
1425 if (what & eTransparentRegionChanged) {
1426 if (layer->setTransparentRegionHint(s.transparentRegion))
1427 flags |= eTraversalNeeded;
1428 }
1429 if (what & eVisibilityChanged) {
1430 if (layer->setFlags(s.flags, s.mask))
1431 flags |= eTraversalNeeded;
1432 }
1433 }
1434 }
1435 if (flags) {
1436 setTransactionFlags(flags);
1437 }
1438 return NO_ERROR;
1439}
1440
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001441sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001443 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1444 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445}
1446
1447void SurfaceFlinger::screenReleased(int dpy)
1448{
1449 // this may be called by a signal handler, we can't do too much in here
1450 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1451 signalEvent();
1452}
1453
1454void SurfaceFlinger::screenAcquired(int dpy)
1455{
1456 // this may be called by a signal handler, we can't do too much in here
1457 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1458 signalEvent();
1459}
1460
1461status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1462{
1463 const size_t SIZE = 1024;
1464 char buffer[SIZE];
1465 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001466 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 snprintf(buffer, SIZE, "Permission Denial: "
1468 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1469 IPCThreadState::self()->getCallingPid(),
1470 IPCThreadState::self()->getCallingUid());
1471 result.append(buffer);
1472 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001473
1474 // figure out if we're stuck somewhere
1475 const nsecs_t now = systemTime();
1476 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1477 const nsecs_t inTransaction(mDebugInTransaction);
1478 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1479 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1480
1481 // Try to get the main lock, but don't insist if we can't
1482 // (this would indicate SF is stuck, but we want to be able to
1483 // print something in dumpsys).
1484 int retry = 3;
1485 while (mStateLock.tryLock()<0 && --retry>=0) {
1486 usleep(1000000);
1487 }
1488 const bool locked(retry >= 0);
1489 if (!locked) {
1490 snprintf(buffer, SIZE,
1491 "SurfaceFlinger appears to be unresponsive, "
1492 "dumping anyways (no locks held)\n");
1493 result.append(buffer);
1494 }
1495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 size_t s = mClientsMap.size();
1497 char name[64];
1498 for (size_t i=0 ; i<s ; i++) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001499 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 sprintf(name, " Client (id=0x%08x)", client->cid);
1501 client->dump(name);
1502 }
1503 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1504 const size_t count = currentLayers.size();
1505 for (size_t i=0 ; i<count ; i++) {
1506 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001507 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 const Layer::State& s = layer->drawingState();
1509 snprintf(buffer, SIZE,
1510 "+ %s %p\n"
1511 " "
1512 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1513 "needsBlending=%1d, invalidate=%1d, "
1514 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001515 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 s.z, layer->tx(), layer->ty(), s.w, s.h,
1517 layer->needsBlending(), layer->contentDirty,
1518 s.alpha, s.flags,
1519 s.transform[0], s.transform[1],
1520 s.transform[2], s.transform[3]);
1521 result.append(buffer);
1522 buffer[0] = 0;
1523 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001524 sp<LayerBaseClient> lbc =
1525 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1526 if (lbc != 0) {
Mathias Agopianf9d93272009-06-19 17:00:27 -07001527 sp<Client> client(lbc->client.promote());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 snprintf(buffer, SIZE,
1529 " "
1530 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopianf9d93272009-06-19 17:00:27 -07001531 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 lbc->getIdentity());
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001533
1534 result.append(buffer);
1535 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001538 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1539 if (l != 0) {
Mathias Agopian86f73292009-09-17 01:35:28 -07001540 SharedBufferStack::Statistics stats = l->lcblk->getStats();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001541 result.append( l->lcblk->dump(" ") );
1542 sp<const Buffer> buf0(l->getBuffer(0));
1543 sp<const Buffer> buf1(l->getBuffer(1));
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001544 uint32_t w0=0, h0=0, s0=0;
1545 uint32_t w1=0, h1=0, s1=0;
1546 if (buf0 != 0) {
1547 w0 = buf0->getWidth();
1548 h0 = buf0->getHeight();
1549 s0 = buf0->getStride();
1550 }
1551 if (buf1 != 0) {
1552 w1 = buf1->getWidth();
1553 h1 = buf1->getHeight();
1554 s1 = buf1->getStride();
1555 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 snprintf(buffer, SIZE,
1557 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001558 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopian86f73292009-09-17 01:35:28 -07001559 " freezeLock=%p, dq-q-time=%u us\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560 l->pixelFormat(),
Mathias Agopian4790a3c2009-09-14 15:59:16 -07001561 w0, h0, s0, w1, h1, s1,
Mathias Agopian86f73292009-09-17 01:35:28 -07001562 l->getFreezeLock().get(), stats.totalTime);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001563 result.append(buffer);
1564 buffer[0] = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566 s.transparentRegion.dump(result, "transparentRegion");
1567 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1568 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1569 }
1570 mWormholeRegion.dump(result, "WormholeRegion");
1571 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1572 snprintf(buffer, SIZE,
1573 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1574 mFreezeDisplay?"yes":"no", mFreezeCount,
1575 mCurrentState.orientation, hw.canDraw());
1576 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001577 snprintf(buffer, SIZE,
1578 " last eglSwapBuffers() time: %f us\n"
1579 " last transaction time : %f us\n",
1580 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1581 result.append(buffer);
1582 if (inSwapBuffersDuration || !locked) {
1583 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1584 inSwapBuffersDuration/1000.0);
1585 result.append(buffer);
1586 }
1587 if (inTransactionDuration || !locked) {
1588 snprintf(buffer, SIZE, " transaction time: %f us\n",
1589 inTransactionDuration/1000.0);
1590 result.append(buffer);
1591 }
Mathias Agopian759fdb22009-07-02 17:33:40 -07001592 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian3d579642009-06-04 18:46:21 -07001593 result.append(buffer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001594 const BufferAllocator& alloc(BufferAllocator::get());
1595 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001596
1597 if (locked) {
1598 mStateLock.unlock();
1599 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 }
1601 write(fd, result.string(), result.size());
1602 return NO_ERROR;
1603}
1604
1605status_t SurfaceFlinger::onTransact(
1606 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1607{
1608 switch (code) {
1609 case CREATE_CONNECTION:
1610 case OPEN_GLOBAL_TRANSACTION:
1611 case CLOSE_GLOBAL_TRANSACTION:
1612 case SET_ORIENTATION:
1613 case FREEZE_DISPLAY:
1614 case UNFREEZE_DISPLAY:
1615 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 {
1617 // codes that require permission check
1618 IPCThreadState* ipc = IPCThreadState::self();
1619 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001620 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001621 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1622 LOGE("Permission Denial: "
1623 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1624 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 }
1626 }
1627 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1629 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001630 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001631 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1632 IPCThreadState* ipc = IPCThreadState::self();
1633 const int pid = ipc->getCallingPid();
1634 const int uid = ipc->getCallingUid();
1635 LOGE("Permission Denial: "
1636 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637 return PERMISSION_DENIED;
1638 }
1639 int n;
1640 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001641 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001643 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 return NO_ERROR;
1645 case 1002: // SHOW_UPDATES
1646 n = data.readInt32();
1647 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1648 return NO_ERROR;
1649 case 1003: // SHOW_BACKGROUND
1650 n = data.readInt32();
1651 mDebugBackground = n ? 1 : 0;
1652 return NO_ERROR;
1653 case 1004:{ // repaint everything
1654 Mutex::Autolock _l(mStateLock);
1655 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1656 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1657 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001658 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001660 case 1005:{ // force transaction
1661 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1662 return NO_ERROR;
1663 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 case 1007: // set mFreezeCount
1665 mFreezeCount = data.readInt32();
1666 return NO_ERROR;
1667 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001668 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 reply->writeInt32(0);
1670 reply->writeInt32(mDebugRegion);
1671 reply->writeInt32(mDebugBackground);
1672 return NO_ERROR;
1673 case 1013: {
1674 Mutex::Autolock _l(mStateLock);
1675 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1676 reply->writeInt32(hw.getPageFlipCount());
1677 }
1678 return NO_ERROR;
1679 }
1680 }
1681 return err;
1682}
1683
1684// ---------------------------------------------------------------------------
1685#if 0
1686#pragma mark -
1687#endif
1688
1689Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1690 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1691{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 const int pgsize = getpagesize();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001693 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001694
1695 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1696 "SurfaceFlinger Client control-block");
1697
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001698 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001699 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001700 new(ctrlblk) SharedClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701 }
1702}
1703
1704Client::~Client() {
1705 if (ctrlblk) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001706 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 }
1708}
1709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710int32_t Client::generateId(int pid)
1711{
1712 const uint32_t i = clz( ~mBitmap );
1713 if (i >= NUM_LAYERS_MAX) {
1714 return NO_MEMORY;
1715 }
1716 mPid = pid;
1717 mInUse.add(uint8_t(i));
1718 mBitmap |= 1<<(31-i);
1719 return i;
1720}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001721
1722status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723{
1724 ssize_t idx = mInUse.indexOf(id);
1725 if (idx < 0)
1726 return NAME_NOT_FOUND;
1727 return mLayers.insertAt(layer, idx);
1728}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001729
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730void Client::free(int32_t id)
1731{
1732 ssize_t idx = mInUse.remove(uint8_t(id));
1733 if (idx >= 0) {
1734 mBitmap &= ~(1<<(31-id));
1735 mLayers.removeItemsAt(idx);
1736 }
1737}
1738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739bool Client::isValid(int32_t i) const {
1740 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1741}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001742
1743sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1744 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001746 if (idx >= 0) {
1747 lbc = mLayers[idx].promote();
1748 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1749 }
1750 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751}
1752
1753void Client::dump(const char* what)
1754{
1755}
1756
1757// ---------------------------------------------------------------------------
1758#if 0
1759#pragma mark -
1760#endif
1761
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001762BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763 : mId(cid), mFlinger(flinger), mCblk(cblk)
1764{
1765}
1766
1767BClient::~BClient() {
1768 // destroy all resources attached to this client
1769 mFlinger->destroyConnection(mId);
1770}
1771
Mathias Agopian7303c6b2009-07-02 18:11:53 -07001772sp<IMemoryHeap> BClient::getControlBlock() const {
1773 return mCblk;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774}
1775
1776sp<ISurface> BClient::createSurface(
1777 ISurfaceFlingerClient::surface_data_t* params, int pid,
1778 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1779 uint32_t flags)
1780{
1781 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1782}
1783
1784status_t BClient::destroySurface(SurfaceID sid)
1785{
1786 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001787 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788}
1789
1790status_t BClient::setState(int32_t count, const layer_state_t* states)
1791{
1792 return mFlinger->setClientState(mId, count, states);
1793}
1794
1795// ---------------------------------------------------------------------------
1796
1797GraphicPlane::GraphicPlane()
1798 : mHw(0)
1799{
1800}
1801
1802GraphicPlane::~GraphicPlane() {
1803 delete mHw;
1804}
1805
1806bool GraphicPlane::initialized() const {
1807 return mHw ? true : false;
1808}
1809
1810void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1811 mHw = hw;
1812}
1813
1814void GraphicPlane::setTransform(const Transform& tr) {
1815 mTransform = tr;
1816 mGlobalTransform = mOrientationTransform * mTransform;
1817}
1818
1819status_t GraphicPlane::orientationToTransfrom(
1820 int orientation, int w, int h, Transform* tr)
1821{
1822 float a, b, c, d, x, y;
1823 switch (orientation) {
1824 case ISurfaceComposer::eOrientationDefault:
1825 a=1; b=0; c=0; d=1; x=0; y=0;
1826 break;
1827 case ISurfaceComposer::eOrientation90:
1828 a=0; b=-1; c=1; d=0; x=w; y=0;
1829 break;
1830 case ISurfaceComposer::eOrientation180:
1831 a=-1; b=0; c=0; d=-1; x=w; y=h;
1832 break;
1833 case ISurfaceComposer::eOrientation270:
1834 a=0; b=1; c=-1; d=0; x=0; y=h;
1835 break;
1836 default:
1837 return BAD_VALUE;
1838 }
1839 tr->set(a, b, c, d);
1840 tr->set(x, y);
1841 return NO_ERROR;
1842}
1843
1844status_t GraphicPlane::setOrientation(int orientation)
1845{
1846 const DisplayHardware& hw(displayHardware());
1847 const float w = hw.getWidth();
1848 const float h = hw.getHeight();
1849
1850 if (orientation == ISurfaceComposer::eOrientationDefault) {
1851 // make sure the default orientation is optimal
1852 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001853 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854 mGlobalTransform = mTransform;
1855 return NO_ERROR;
1856 }
1857
1858 // If the rotation can be handled in hardware, this is where
1859 // the magic should happen.
1860 if (UNLIKELY(orientation == 42)) {
1861 float a, b, c, d, x, y;
1862 const float r = (3.14159265f / 180.0f) * 42.0f;
1863 const float si = sinf(r);
1864 const float co = cosf(r);
1865 a=co; b=-si; c=si; d=co;
1866 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1867 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1868 mOrientationTransform.set(a, b, c, d);
1869 mOrientationTransform.set(x, y);
1870 } else {
1871 GraphicPlane::orientationToTransfrom(orientation, w, h,
1872 &mOrientationTransform);
1873 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001874 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 mGlobalTransform = mOrientationTransform * mTransform;
1876 return NO_ERROR;
1877}
1878
1879const DisplayHardware& GraphicPlane::displayHardware() const {
1880 return *mHw;
1881}
1882
1883const Transform& GraphicPlane::transform() const {
1884 return mGlobalTransform;
1885}
1886
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001887EGLDisplay GraphicPlane::getEGLDisplay() const {
1888 return mHw->getEGLDisplay();
1889}
1890
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891// ---------------------------------------------------------------------------
1892
1893}; // namespace android