blob: 965b7dd28f320a252ce5f9f1a0ba54c215e7e8e2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-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 Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian6950e422009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041#include <ui/PixelFormat.h>
42#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44#include <pixelflinger/pixelflinger.h>
45#include <GLES/gl.h>
46
47#include "clz.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048#include "Layer.h"
49#include "LayerBlur.h"
50#include "LayerBuffer.h"
51#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Mathias Agopian627e7b52009-05-21 19:21:59 -070056/* ideally AID_GRAPHICS would be in a semi-public header
57 * or there would be a way to map a user/group name to its id
58 */
59#ifndef AID_GRAPHICS
60#define AID_GRAPHICS 1003
61#endif
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063#define DISPLAY_COUNT 1
64
65namespace android {
66
67// ---------------------------------------------------------------------------
68
69void SurfaceFlinger::instantiate() {
70 defaultServiceManager()->addService(
71 String16("SurfaceFlinger"), new SurfaceFlinger());
72}
73
74void SurfaceFlinger::shutdown() {
75 // we should unregister here, but not really because
76 // when (if) the service manager goes away, all the services
77 // it has a reference to will leave too.
78}
79
80// ---------------------------------------------------------------------------
81
82SurfaceFlinger::LayerVector::LayerVector(const SurfaceFlinger::LayerVector& rhs)
83 : lookup(rhs.lookup), layers(rhs.layers)
84{
85}
86
87ssize_t SurfaceFlinger::LayerVector::indexOf(
Mathias Agopian1473f462009-04-10 14:24:30 -070088 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089{
90 if (guess<size() && lookup.keyAt(guess) == key)
91 return guess;
92 const ssize_t i = lookup.indexOfKey(key);
93 if (i>=0) {
94 const size_t idx = lookup.valueAt(i);
Mathias Agopian1473f462009-04-10 14:24:30 -070095 LOGE_IF(layers[idx]!=key,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian1473f462009-04-10 14:24:30 -070097 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 return idx;
99 }
100 return i;
101}
102
103ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian1473f462009-04-10 14:24:30 -0700104 const sp<LayerBase>& layer,
105 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106{
107 size_t count = layers.size();
108 ssize_t l = 0;
109 ssize_t h = count-1;
110 ssize_t mid;
Mathias Agopian1473f462009-04-10 14:24:30 -0700111 sp<LayerBase> const* a = layers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 while (l <= h) {
113 mid = l + (h - l)/2;
114 const int c = cmp(a+mid, &layer);
115 if (c == 0) { l = mid; break; }
116 else if (c<0) { l = mid+1; }
117 else { h = mid-1; }
118 }
119 size_t order = l;
120 while (order<count && !cmp(&layer, a+order)) {
121 order++;
122 }
123 count = lookup.size();
124 for (size_t i=0 ; i<count ; i++) {
125 if (lookup.valueAt(i) >= order) {
126 lookup.editValueAt(i)++;
127 }
128 }
129 layers.insertAt(layer, order);
130 lookup.add(layer, order);
131 return order;
132}
133
Mathias Agopian1473f462009-04-10 14:24:30 -0700134ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135{
136 const ssize_t keyIndex = lookup.indexOfKey(layer);
137 if (keyIndex >= 0) {
138 const size_t index = lookup.valueAt(keyIndex);
Mathias Agopian1473f462009-04-10 14:24:30 -0700139 LOGE_IF(layers[index]!=layer,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian1473f462009-04-10 14:24:30 -0700141 this, int(index), layers[index].get(), layer.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 layers.removeItemsAt(index);
143 lookup.removeItemsAt(keyIndex);
144 const size_t count = lookup.size();
145 for (size_t i=0 ; i<count ; i++) {
146 if (lookup.valueAt(i) >= size_t(index)) {
147 lookup.editValueAt(i)--;
148 }
149 }
150 return index;
151 }
152 return NAME_NOT_FOUND;
153}
154
155ssize_t SurfaceFlinger::LayerVector::reorder(
Mathias Agopian1473f462009-04-10 14:24:30 -0700156 const sp<LayerBase>& layer,
157 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158{
159 // XXX: it's a little lame. but oh well...
160 ssize_t err = remove(layer);
161 if (err >=0)
162 err = add(layer, cmp);
163 return err;
164}
165
166// ---------------------------------------------------------------------------
167#if 0
168#pragma mark -
169#endif
170
171SurfaceFlinger::SurfaceFlinger()
172 : BnSurfaceComposer(), Thread(false),
173 mTransactionFlags(0),
174 mTransactionCount(0),
Mathias Agopian9779b2212009-09-07 16:32:45 -0700175 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -0700176 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 mBootTime(systemTime()),
Mathias Agopian151e8592009-06-15 18:24:59 -0700178 mHardwareTest("android.permission.HARDWARE_TEST"),
179 mAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"),
180 mDump("android.permission.DUMP"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 mVisibleRegionsDirty(false),
182 mDeferReleaseConsole(false),
183 mFreezeDisplay(false),
184 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700185 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 mDebugBackground(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -0700188 mDebugInSwapBuffers(0),
189 mLastSwapBufferTime(0),
190 mDebugInTransaction(0),
191 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -0700192 mBootFinished(false),
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800207 property_get("debug.sf.showbackground", value, "0");
208 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
210 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212}
213
214SurfaceFlinger::~SurfaceFlinger()
215{
216 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217}
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
220{
221 return graphicPlane(0).displayHardware().getOverlayEngine();
222}
223
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700224sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700226 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227}
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
230{
231 Mutex::Autolock _l(mStateLock);
232 uint32_t token = mTokens.acquire();
233
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700234 sp<Client> client = new Client(token, this);
235 if (client->ctrlblk == 0) {
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800241 mTokens.release(token);
242 return 0;
243 }
244 sp<BClient> bclient =
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700245 new BClient(this, token, client->getControlBlockMemory());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 return bclient;
247}
248
249void SurfaceFlinger::destroyConnection(ClientID cid)
250{
251 Mutex::Autolock _l(mStateLock);
Mathias Agopian6edf5af2009-06-19 17:00:27 -0700252 sp<Client> client = mClientsMap.valueFor(cid);
253 if (client != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 // free all the layers this client owns
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700255 Vector< wp<LayerBaseClient> > layers(client->getLayers());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 const size_t count = layers.size();
257 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700258 sp<LayerBaseClient> layer(layers[i].promote());
259 if (layer != 0) {
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700260 purgatorizeLayer_l(layer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700261 }
The Android Open Source Project9066cfe2009-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 Agopian627e7b52009-05-21 19:21:59 -0700296 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700297 mBootFinished = true;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700298 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299}
300
301void SurfaceFlinger::onFirstRef()
302{
303 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
304
305 // Wait for the main thread to be done with its initialization
306 mReadyToRunBarrier.wait();
307}
308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309static inline uint16_t pack565(int r, int g, int b) {
310 return (r<<11)|(g<<5)|b;
311}
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313status_t SurfaceFlinger::readyToRun()
314{
315 LOGI( "SurfaceFlinger's main thread ready to run. "
316 "Initializing graphics H/W...");
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 // we only support one display currently
319 int dpy = 0;
320
321 {
322 // initialize the main display
323 GraphicPlane& plane(graphicPlane(dpy));
324 DisplayHardware* const hw = new DisplayHardware(this, dpy);
325 plane.setDisplayHardware(hw);
326 }
327
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700328 // create the shared control-block
329 mServerHeap = new MemoryHeapBase(4096,
330 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
331 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
332
333 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
334 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
335
336 new(mServerCblk) surface_flinger_cblk_t;
337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 // initialize primary screen
339 // (other display should be initialized in the same manner, but
340 // asynchronously, as they could come and go. None of this is supported
341 // yet).
342 const GraphicPlane& plane(graphicPlane(dpy));
343 const DisplayHardware& hw = plane.displayHardware();
344 const uint32_t w = hw.getWidth();
345 const uint32_t h = hw.getHeight();
346 const uint32_t f = hw.getFormat();
347 hw.makeCurrent();
348
349 // initialize the shared control block
350 mServerCblk->connected |= 1<<dpy;
351 display_cblk_t* dcblk = mServerCblk->displays + dpy;
352 memset(dcblk, 0, sizeof(display_cblk_t));
353 dcblk->w = w;
354 dcblk->h = h;
355 dcblk->format = f;
356 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
357 dcblk->xdpi = hw.getDpiX();
358 dcblk->ydpi = hw.getDpiY();
359 dcblk->fps = hw.getRefreshRate();
360 dcblk->density = hw.getDensity();
361 asm volatile ("":::"memory");
362
363 // Initialize OpenGL|ES
364 glActiveTexture(GL_TEXTURE0);
365 glBindTexture(GL_TEXTURE_2D, 0);
366 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
367 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
368 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
369 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
370 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
371 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
372 glPixelStorei(GL_PACK_ALIGNMENT, 4);
373 glEnableClientState(GL_VERTEX_ARRAY);
374 glEnable(GL_SCISSOR_TEST);
375 glShadeModel(GL_FLAT);
376 glDisable(GL_DITHER);
377 glDisable(GL_CULL_FACE);
378
379 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
380 const uint16_t g1 = pack565(0x17,0x2f,0x17);
381 const uint16_t textureData[4] = { g0, g1, g1, g0 };
382 glGenTextures(1, &mWormholeTexName);
383 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
384 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
385 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
386 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
387 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
388 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
389 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
390
391 glViewport(0, 0, w, h);
392 glMatrixMode(GL_PROJECTION);
393 glLoadIdentity();
394 glOrthof(0, w, h, 0, 0, 1);
395
396 LayerDim::initDimmer(this, w, h);
397
398 mReadyToRunBarrier.open();
399
400 /*
401 * We're now ready to accept clients...
402 */
403
Mathias Agopian627e7b52009-05-21 19:21:59 -0700404 // start boot animation
405 property_set("ctl.start", "bootanim");
406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 return NO_ERROR;
408}
409
410// ----------------------------------------------------------------------------
411#if 0
412#pragma mark -
413#pragma mark Events Handler
414#endif
415
416void SurfaceFlinger::waitForEvent()
417{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700418 while (true) {
419 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800420 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700421 if (UNLIKELY(isFrozen())) {
422 // wait 5 seconds
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700423 const nsecs_t now = systemTime();
424 if (mFreezeDisplayTime == 0) {
425 mFreezeDisplayTime = now;
426 }
427 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
428 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700429 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700430
Mathias Agopianbdbe6022009-04-28 03:17:50 -0700431 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-12-01 17:23:28 -0800432
433 // see if we timed out
434 if (isFrozen()) {
435 const nsecs_t now = systemTime();
436 nsecs_t frozenTime = (now - mFreezeDisplayTime);
437 if (frozenTime >= freezeDisplayTimeout) {
438 // we timed out and are still frozen
439 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
440 mFreezeDisplay, mFreezeCount);
441 mFreezeDisplayTime = 0;
442 mFreezeCount = 0;
443 mFreezeDisplay = false;
444 }
445 }
446
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700447 if (msg != 0) {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700448 switch (msg->what) {
449 case MessageQueue::INVALIDATE:
450 // invalidate message, just return to the main loop
451 return;
452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455}
456
457void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700458 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459}
460
461void SurfaceFlinger::signal() const {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700462 // this is the IPC call
463 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464}
465
466void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
467{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700468 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469}
470
471// ----------------------------------------------------------------------------
472#if 0
473#pragma mark -
474#pragma mark Main loop
475#endif
476
477bool SurfaceFlinger::threadLoop()
478{
479 waitForEvent();
480
481 // check for transactions
482 if (UNLIKELY(mConsoleSignals)) {
483 handleConsoleEvents();
484 }
485
486 if (LIKELY(mTransactionCount == 0)) {
487 // if we're in a global transaction, don't do anything.
488 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
489 uint32_t transactionFlags = getTransactionFlags(mask);
490 if (LIKELY(transactionFlags)) {
491 handleTransaction(transactionFlags);
492 }
493 }
494
495 // post surfaces (if needed)
496 handlePageFlip();
497
498 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700499 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 // repaint the framebuffer (if needed)
501 handleRepaint();
502
Mathias Agopianb1a18742009-09-17 16:18:16 -0700503 // inform the h/w that we're done compositing
504 hw.compositionComplete();
505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 // release the clients before we flip ('cause flip might block)
507 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 postFramebuffer();
510 } else {
511 // pretend we did the post
512 unlockClients();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 usleep(16667); // 60 fps period
514 }
515 return true;
516}
517
518void SurfaceFlinger::postFramebuffer()
519{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 if (!mInvalidRegion.isEmpty()) {
521 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700522 const nsecs_t now = systemTime();
523 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700525 mLastSwapBufferTime = systemTime() - now;
526 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529}
530
531void SurfaceFlinger::handleConsoleEvents()
532{
533 // something to do with the console
534 const DisplayHardware& hw = graphicPlane(0).displayHardware();
535
536 int what = android_atomic_and(0, &mConsoleSignals);
537 if (what & eConsoleAcquired) {
538 hw.acquireScreen();
539 }
540
541 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700542 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 hw.releaseScreen();
545 }
546
547 if (what & eConsoleReleased) {
548 if (hw.canDraw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 hw.releaseScreen();
550 } else {
551 mDeferReleaseConsole = true;
552 }
553 }
554
555 mDirtyRegion.set(hw.bounds());
556}
557
558void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
559{
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700560 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700562 { // scope for the lock
563 Mutex::Autolock _l(mStateLock);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700564 const nsecs_t now = systemTime();
565 mDebugInTransaction = now;
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700566 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700567 mLastTransactionTime = systemTime() - now;
568 mDebugInTransaction = 0;
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700569 }
570
571 // do this without lock held
572 const size_t count = ditchedLayers.size();
573 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianffae4fc2009-09-04 19:50:23 -0700574 if (ditchedLayers[i] != 0) {
575 //LOGD("ditching layer %p", ditchedLayers[i].get());
576 ditchedLayers[i]->ditch();
577 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700578 }
579}
580
581void SurfaceFlinger::handleTransactionLocked(
582 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
583{
584 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 const size_t count = currentLayers.size();
586
587 /*
588 * Traversal of the children
589 * (perform the transaction for each of them if needed)
590 */
591
592 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
593 if (layersNeedTransaction) {
594 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700595 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
597 if (!trFlags) continue;
598
599 const uint32_t flags = layer->doTransaction(0);
600 if (flags & Layer::eVisibleRegion)
601 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603 }
604
605 /*
606 * Perform our own transaction if needed
607 */
608
609 if (transactionFlags & eTransactionNeeded) {
610 if (mCurrentState.orientation != mDrawingState.orientation) {
611 // the orientation has changed, recompute all visible regions
612 // and invalidate everything.
613
614 const int dpy = 0;
615 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700616 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 GraphicPlane& plane(graphicPlane(dpy));
618 plane.setOrientation(orientation);
619
620 // update the shared control block
621 const DisplayHardware& hw(plane.displayHardware());
622 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
623 dcblk->orientation = orientation;
624 if (orientation & eOrientationSwapMask) {
625 // 90 or 270 degrees orientation
626 dcblk->w = hw.getHeight();
627 dcblk->h = hw.getWidth();
628 } else {
629 dcblk->w = hw.getWidth();
630 dcblk->h = hw.getHeight();
631 }
632
633 mVisibleRegionsDirty = true;
634 mDirtyRegion.set(hw.bounds());
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700635 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637
638 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
639 // freezing or unfreezing the display -> trigger animation if needed
640 mFreezeDisplay = mCurrentState.freezeDisplay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700643 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
644 // layers have been added
645 mVisibleRegionsDirty = true;
646 }
647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 // some layers might have been removed, so
649 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700650 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700651 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700653 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700654 const size_t count = previousLayers.size();
655 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700656 const sp<LayerBase>& layer(previousLayers[i]);
657 if (currentLayers.indexOf( layer ) < 0) {
658 // this layer is not visible anymore
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700659 ditchedLayers.add(layer);
Mathias Agopian33863dd2009-07-28 14:20:21 -0700660 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700661 }
662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664
665 // get rid of all resources we don't need anymore
666 // (layers and clients)
667 free_resources_l();
668 }
669
670 commitTransaction();
671}
672
673sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
674{
675 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
676}
677
678void SurfaceFlinger::computeVisibleRegions(
679 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
680{
681 const GraphicPlane& plane(graphicPlane(0));
682 const Transform& planeTransform(plane.transform());
683
684 Region aboveOpaqueLayers;
685 Region aboveCoveredLayers;
686 Region dirty;
687
688 bool secureFrameBuffer = false;
689
690 size_t i = currentLayers.size();
691 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700692 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 layer->validateVisibility(planeTransform);
694
695 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700696 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697
698 // handle hidden surfaces by setting the visible region to empty
699 Region opaqueRegion;
700 Region visibleRegion;
701 Region coveredRegion;
Mathias Agopian12cedff2009-07-28 10:57:27 -0700702 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 const bool translucent = layer->needsBlending();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700704 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 visibleRegion.set(bounds);
706 coveredRegion = visibleRegion;
707
708 // Remove the transparent area from the visible region
709 if (translucent) {
710 visibleRegion.subtractSelf(layer->transparentRegionScreen);
711 }
712
713 // compute the opaque region
714 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
715 // the opaque region is the visible region
716 opaqueRegion = visibleRegion;
717 }
718 }
719
720 // subtract the opaque region covered by the layers above us
721 visibleRegion.subtractSelf(aboveOpaqueLayers);
722 coveredRegion.andSelf(aboveCoveredLayers);
723
724 // compute this layer's dirty region
725 if (layer->contentDirty) {
726 // we need to invalidate the whole region
727 dirty = visibleRegion;
728 // as well, as the old visible region
729 dirty.orSelf(layer->visibleRegionScreen);
730 layer->contentDirty = false;
731 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700732 /* compute the exposed region:
733 * exposed = what's VISIBLE and NOT COVERED now
734 * but was COVERED before
735 */
736 dirty = (visibleRegion - coveredRegion) & layer->coveredRegionScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738 dirty.subtractSelf(aboveOpaqueLayers);
739
740 // accumulate to the screen dirty region
741 dirtyRegion.orSelf(dirty);
742
Mathias Agopianed81f222009-04-14 23:02:51 -0700743 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 aboveOpaqueLayers.orSelf(opaqueRegion);
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700745 aboveCoveredLayers.orSelf(visibleRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746
747 // Store the visible region is screen space
748 layer->setVisibleRegion(visibleRegion);
749 layer->setCoveredRegion(coveredRegion);
750
Mathias Agopian12cedff2009-07-28 10:57:27 -0700751 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 if (layer->isSecure() && !visibleRegion.isEmpty()) {
753 secureFrameBuffer = true;
754 }
755 }
756
Mathias Agopian12cedff2009-07-28 10:57:27 -0700757 // invalidate the areas where a layer was removed
758 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
759 mDirtyRegionRemovedLayer.clear();
760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 mSecureFrameBuffer = secureFrameBuffer;
762 opaqueRegion = aboveOpaqueLayers;
763}
764
765
766void SurfaceFlinger::commitTransaction()
767{
768 mDrawingState = mCurrentState;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700769 mResizeTransationPending = false;
770 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771}
772
773void SurfaceFlinger::handlePageFlip()
774{
775 bool visibleRegions = mVisibleRegionsDirty;
776 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
777 visibleRegions |= lockPageFlip(currentLayers);
778
779 const DisplayHardware& hw = graphicPlane(0).displayHardware();
780 const Region screenRegion(hw.bounds());
781 if (visibleRegions) {
782 Region opaqueRegion;
783 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
784 mWormholeRegion = screenRegion.subtract(opaqueRegion);
785 mVisibleRegionsDirty = false;
786 }
787
788 unlockPageFlip(currentLayers);
789 mDirtyRegion.andSelf(screenRegion);
790}
791
792bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
793{
794 bool recomputeVisibleRegions = false;
795 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700796 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700798 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 layer->lockPageFlip(recomputeVisibleRegions);
800 }
801 return recomputeVisibleRegions;
802}
803
804void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
805{
806 const GraphicPlane& plane(graphicPlane(0));
807 const Transform& planeTransform(plane.transform());
808 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700809 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700811 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 layer->unlockPageFlip(planeTransform, mDirtyRegion);
813 }
814}
815
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817void SurfaceFlinger::handleRepaint()
818{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700819 // compute the invalid region
820 mInvalidRegion.orSelf(mDirtyRegion);
821 if (mInvalidRegion.isEmpty()) {
822 // nothing to do
823 return;
824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825
826 if (UNLIKELY(mDebugRegion)) {
827 debugFlashRegions();
828 }
829
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700830 // set the frame buffer
831 const DisplayHardware& hw(graphicPlane(0).displayHardware());
832 glMatrixMode(GL_MODELVIEW);
833 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834
835 uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700836 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
837 (flags & DisplayHardware::BUFFER_PRESERVED))
838 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700839 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
840 // takes a rectangle, we must make sure to update that whole
841 // rectangle in that case
842 if (flags & DisplayHardware::SWAP_RECTANGLE) {
843 // FIXME: we really should be able to pass a region to
844 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 mDirtyRegion.set(mInvalidRegion.bounds());
846 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700847 // in the BUFFER_PRESERVED case, obviously, we can update only
848 // what's needed and nothing more.
849 // NOTE: this is NOT a common case, as preserving the backbuffer
850 // is costly and usually involves copying the whole update back.
851 }
852 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700853 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700854 // We need to redraw the rectangle that will be updated
855 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700856 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700857 // rectangle instead of a region (see DisplayHardware::flip())
858 mDirtyRegion.set(mInvalidRegion.bounds());
859 } else {
860 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 mDirtyRegion.set(hw.bounds());
862 mInvalidRegion = mDirtyRegion;
863 }
864 }
865
866 // compose all surfaces
867 composeSurfaces(mDirtyRegion);
868
869 // clear the dirty regions
870 mDirtyRegion.clear();
871}
872
873void SurfaceFlinger::composeSurfaces(const Region& dirty)
874{
875 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
876 // should never happen unless the window manager has a bug
877 // draw something...
878 drawWormhole();
879 }
880 const SurfaceFlinger& flinger(*this);
881 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
882 const size_t count = drawingLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700883 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700885 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 const Region& visibleRegion(layer->visibleRegionScreen);
887 if (!visibleRegion.isEmpty()) {
888 const Region clip(dirty.intersect(visibleRegion));
889 if (!clip.isEmpty()) {
890 layer->draw(clip);
891 }
892 }
893 }
894}
895
896void SurfaceFlinger::unlockClients()
897{
898 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
899 const size_t count = drawingLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700900 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700902 const sp<LayerBase>& layer = layers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 layer->finishPageFlip();
904 }
905}
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907void SurfaceFlinger::debugFlashRegions()
908{
Mathias Agopiandff8e582009-05-04 14:17:04 -0700909 const DisplayHardware& hw(graphicPlane(0).displayHardware());
910 const uint32_t flags = hw.getFlags();
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700911
912 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
913 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700914 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
Mathias Agopiandff8e582009-05-04 14:17:04 -0700915 mDirtyRegion.bounds() : hw.bounds());
916 composeSurfaces(repaint);
917 }
918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 glDisable(GL_TEXTURE_2D);
920 glDisable(GL_BLEND);
921 glDisable(GL_DITHER);
922 glDisable(GL_SCISSOR_TEST);
923
Mathias Agopiandff8e582009-05-04 14:17:04 -0700924 static int toggle = 0;
925 toggle = 1 - toggle;
926 if (toggle) {
927 glColor4x(0x10000, 0, 0x10000, 0x10000);
928 } else {
929 glColor4x(0x10000, 0x10000, 0, 0x10000);
930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700932 Region::const_iterator it = mDirtyRegion.begin();
933 Region::const_iterator const end = mDirtyRegion.end();
934 while (it != end) {
935 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 GLfloat vertices[][2] = {
937 { r.left, r.top },
938 { r.left, r.bottom },
939 { r.right, r.bottom },
940 { r.right, r.top }
941 };
942 glVertexPointer(2, GL_FLOAT, 0, vertices);
943 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
944 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700945
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700946 if (mInvalidRegion.isEmpty()) {
947 mDirtyRegion.dump("mDirtyRegion");
948 mInvalidRegion.dump("mInvalidRegion");
949 }
950 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951
952 if (mDebugRegion > 1)
953 usleep(mDebugRegion * 1000);
954
955 glEnable(GL_SCISSOR_TEST);
956 //mDirtyRegion.dump("mDirtyRegion");
957}
958
959void SurfaceFlinger::drawWormhole() const
960{
961 const Region region(mWormholeRegion.intersect(mDirtyRegion));
962 if (region.isEmpty())
963 return;
964
965 const DisplayHardware& hw(graphicPlane(0).displayHardware());
966 const int32_t width = hw.getWidth();
967 const int32_t height = hw.getHeight();
968
969 glDisable(GL_BLEND);
970 glDisable(GL_DITHER);
971
972 if (LIKELY(!mDebugBackground)) {
973 glClearColorx(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700974 Region::const_iterator it = region.begin();
975 Region::const_iterator const end = region.end();
976 while (it != end) {
977 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 const GLint sy = height - (r.top + r.height());
979 glScissor(r.left, sy, r.width(), r.height());
980 glClear(GL_COLOR_BUFFER_BIT);
981 }
982 } else {
983 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
984 { width, height }, { 0, height } };
985 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
986 glVertexPointer(2, GL_SHORT, 0, vertices);
987 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
988 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
989 glEnable(GL_TEXTURE_2D);
990 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
991 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
992 glMatrixMode(GL_TEXTURE);
993 glLoadIdentity();
994 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700995 Region::const_iterator it = region.begin();
996 Region::const_iterator const end = region.end();
997 while (it != end) {
998 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 const GLint sy = height - (r.top + r.height());
1000 glScissor(r.left, sy, r.width(), r.height());
1001 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1002 }
1003 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1004 }
1005}
1006
1007void SurfaceFlinger::debugShowFPS() const
1008{
1009 static int mFrameCount;
1010 static int mLastFrameCount = 0;
1011 static nsecs_t mLastFpsTime = 0;
1012 static float mFps = 0;
1013 mFrameCount++;
1014 nsecs_t now = systemTime();
1015 nsecs_t diff = now - mLastFpsTime;
1016 if (diff > ms2ns(250)) {
1017 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1018 mLastFpsTime = now;
1019 mLastFrameCount = mFrameCount;
1020 }
1021 // XXX: mFPS has the value we want
1022 }
1023
Mathias Agopian1473f462009-04-10 14:24:30 -07001024status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025{
1026 Mutex::Autolock _l(mStateLock);
1027 addLayer_l(layer);
1028 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1029 return NO_ERROR;
1030}
1031
Mathias Agopian1473f462009-04-10 14:24:30 -07001032status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033{
1034 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001035 status_t err = purgatorizeLayer_l(layer);
1036 if (err == NO_ERROR)
1037 setTransactionFlags(eTransactionNeeded);
1038 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039}
1040
Mathias Agopian1473f462009-04-10 14:24:30 -07001041status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042{
1043 layer->forceVisibilityTransaction();
1044 setTransactionFlags(eTraversalNeeded);
1045 return NO_ERROR;
1046}
1047
Mathias Agopian1473f462009-04-10 14:24:30 -07001048status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049{
Mathias Agopianffae4fc2009-09-04 19:50:23 -07001050 if (layer == 0)
1051 return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 ssize_t i = mCurrentState.layersSortedByZ.add(
1053 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian1473f462009-04-10 14:24:30 -07001054 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1055 if (lbc != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 mLayerMap.add(lbc->serverIndex(), lbc);
1057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 return NO_ERROR;
1059}
1060
Mathias Agopian1473f462009-04-10 14:24:30 -07001061status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062{
1063 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1064 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001065 mLayersRemoved = true;
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001066 sp<LayerBaseClient> layer =
1067 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001068 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 mLayerMap.removeItem(layer->serverIndex());
1070 }
1071 return NO_ERROR;
1072 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001073 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074}
1075
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001076status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1077{
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001078 // remove the layer from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001079 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001080
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001081 layerBase->onRemoved();
1082
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001083 // it's possible that we don't find a layer, because it might
1084 // have been destroyed already -- this is not technically an error
1085 // from the user because there is a race between BClient::destroySurface(),
1086 // ~BClient() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001087 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1088}
1089
1090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091void SurfaceFlinger::free_resources_l()
1092{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 // free resources associated with disconnected clients
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001094 Vector< sp<Client> >& disconnectedClients(mDisconnectedClients);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 const size_t count = disconnectedClients.size();
1096 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001097 sp<Client> client = disconnectedClients[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 mTokens.release(client->cid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 disconnectedClients.clear();
1101}
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1104{
1105 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1106}
1107
1108uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1109{
1110 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1111 if ((old & flags)==0) { // wake the server up
1112 if (delay > 0) {
1113 signalDelayedEvent(delay);
1114 } else {
1115 signalEvent();
1116 }
1117 }
1118 return old;
1119}
1120
1121void SurfaceFlinger::openGlobalTransaction()
1122{
1123 android_atomic_inc(&mTransactionCount);
1124}
1125
1126void SurfaceFlinger::closeGlobalTransaction()
1127{
1128 if (android_atomic_dec(&mTransactionCount) == 1) {
1129 signalEvent();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001130
1131 // if there is a transaction with a resize, wait for it to
1132 // take effect before returning.
1133 Mutex::Autolock _l(mStateLock);
1134 while (mResizeTransationPending) {
Mathias Agopian98a9c562009-09-30 14:42:13 -07001135 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1136 if (CC_UNLIKELY(err != NO_ERROR)) {
1137 // just in case something goes wrong in SF, return to the
1138 // called after a few seconds.
1139 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1140 mResizeTransationPending = false;
1141 break;
1142 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145}
1146
1147status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1148{
1149 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1150 return BAD_VALUE;
1151
1152 Mutex::Autolock _l(mStateLock);
1153 mCurrentState.freezeDisplay = 1;
1154 setTransactionFlags(eTransactionNeeded);
1155
1156 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001157 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 return NO_ERROR;
1159}
1160
1161status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1162{
1163 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1164 return BAD_VALUE;
1165
1166 Mutex::Autolock _l(mStateLock);
1167 mCurrentState.freezeDisplay = 0;
1168 setTransactionFlags(eTransactionNeeded);
1169
1170 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001171 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 return NO_ERROR;
1173}
1174
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001175int SurfaceFlinger::setOrientation(DisplayID dpy,
1176 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177{
1178 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1179 return BAD_VALUE;
1180
1181 Mutex::Autolock _l(mStateLock);
1182 if (mCurrentState.orientation != orientation) {
1183 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001184 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 mCurrentState.orientation = orientation;
1186 setTransactionFlags(eTransactionNeeded);
1187 mTransactionCV.wait(mStateLock);
1188 } else {
1189 orientation = BAD_VALUE;
1190 }
1191 }
1192 return orientation;
1193}
1194
1195sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1196 ISurfaceFlingerClient::surface_data_t* params,
1197 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1198 uint32_t flags)
1199{
Mathias Agopian1473f462009-04-10 14:24:30 -07001200 sp<LayerBaseClient> layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001202
1203 if (int32_t(w|h) < 0) {
1204 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1205 int(w), int(h));
1206 return surfaceHandle;
1207 }
1208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 Mutex::Autolock _l(mStateLock);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001210 sp<Client> client = mClientsMap.valueFor(clientId);
1211 if (UNLIKELY(client == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1213 return surfaceHandle;
1214 }
1215
1216 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001217 int32_t id = client->generateId(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1219 LOGE("createSurface() failed, generateId = %d", id);
1220 return surfaceHandle;
1221 }
1222
1223 switch (flags & eFXSurfaceMask) {
1224 case eFXSurfaceNormal:
1225 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian18b6b492009-08-19 17:46:26 -07001226 layer = createPushBuffersSurfaceLocked(client, d, id,
1227 w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 } else {
Mathias Agopian18b6b492009-08-19 17:46:26 -07001229 layer = createNormalSurfaceLocked(client, d, id,
1230 w, h, flags, format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 break;
1233 case eFXSurfaceBlur:
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001234 layer = createBlurSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 break;
1236 case eFXSurfaceDim:
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001237 layer = createDimSurfaceLocked(client, d, id, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 break;
1239 }
1240
Mathias Agopian1473f462009-04-10 14:24:30 -07001241 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 setTransactionFlags(eTransactionNeeded);
1243 surfaceHandle = layer->getSurface();
Mathias Agopian18b6b492009-08-19 17:46:26 -07001244 if (surfaceHandle != 0) {
1245 params->token = surfaceHandle->getToken();
1246 params->identity = surfaceHandle->getIdentity();
1247 params->width = w;
1248 params->height = h;
1249 params->format = format;
1250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252
1253 return surfaceHandle;
1254}
1255
Mathias Agopian1473f462009-04-10 14:24:30 -07001256sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001257 const sp<Client>& client, DisplayID display,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001258 int32_t id, uint32_t w, uint32_t h, uint32_t flags,
1259 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260{
1261 // initialize the surfaces
1262 switch (format) { // TODO: take h/w into account
1263 case PIXEL_FORMAT_TRANSPARENT:
1264 case PIXEL_FORMAT_TRANSLUCENT:
1265 format = PIXEL_FORMAT_RGBA_8888;
1266 break;
1267 case PIXEL_FORMAT_OPAQUE:
1268 format = PIXEL_FORMAT_RGB_565;
1269 break;
1270 }
1271
Mathias Agopian1473f462009-04-10 14:24:30 -07001272 sp<Layer> layer = new Layer(this, display, client, id);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001273 status_t err = layer->setBuffers(w, h, format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 if (LIKELY(err == NO_ERROR)) {
1275 layer->initStates(w, h, flags);
1276 addLayer_l(layer);
1277 } else {
1278 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001279 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281 return layer;
1282}
1283
Mathias Agopian1473f462009-04-10 14:24:30 -07001284sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001285 const sp<Client>& client, DisplayID display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1287{
Mathias Agopian1473f462009-04-10 14:24:30 -07001288 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 layer->initStates(w, h, flags);
1290 addLayer_l(layer);
1291 return layer;
1292}
1293
Mathias Agopian1473f462009-04-10 14:24:30 -07001294sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001295 const sp<Client>& client, DisplayID display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1297{
Mathias Agopian1473f462009-04-10 14:24:30 -07001298 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 layer->initStates(w, h, flags);
1300 addLayer_l(layer);
1301 return layer;
1302}
1303
Mathias Agopian1473f462009-04-10 14:24:30 -07001304sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001305 const sp<Client>& client, DisplayID display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1307{
Mathias Agopian1473f462009-04-10 14:24:30 -07001308 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 layer->initStates(w, h, flags);
1310 addLayer_l(layer);
1311 return layer;
1312}
1313
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001314status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001316 /*
1317 * called by the window manager, when a surface should be marked for
1318 * destruction.
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001319 *
1320 * The surface is removed from the current and drawing lists, but placed
1321 * in the purgatory queue, so it's not destroyed right-away (we need
1322 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001323 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001324
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001325 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001326 Mutex::Autolock _l(mStateLock);
1327 sp<LayerBaseClient> layer = getLayerUser_l(index);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001328 if (layer != 0) {
1329 err = purgatorizeLayer_l(layer);
1330 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001331 setTransactionFlags(eTransactionNeeded);
1332 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001333 }
1334 return err;
1335}
1336
1337status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1338{
Mathias Agopian359140c2009-07-02 17:33:40 -07001339 // called by ~ISurface() when all references are gone
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001340
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001341 class MessageDestroySurface : public MessageBase {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001342 SurfaceFlinger* flinger;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001343 sp<LayerBaseClient> layer;
1344 public:
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001345 MessageDestroySurface(
1346 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1347 : flinger(flinger), layer(layer) { }
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001348 virtual bool handler() {
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001349 sp<LayerBaseClient> l(layer);
1350 layer.clear(); // clear it outside of the lock;
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001351 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian359140c2009-07-02 17:33:40 -07001352 /*
1353 * remove the layer from the current list -- chances are that it's
1354 * not in the list anyway, because it should have been removed
1355 * already upon request of the client (eg: window manager).
1356 * However, a buggy client could have not done that.
1357 * Since we know we don't have any more clients, we don't need
1358 * to use the purgatory.
1359 */
Mathias Agopianc8fb5b12009-06-19 16:24:02 -07001360 status_t err = flinger->removeLayer_l(l);
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001361 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1362 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001363 return true;
1364 }
1365 };
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001366
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001367 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 return NO_ERROR;
1369}
1370
1371status_t SurfaceFlinger::setClientState(
1372 ClientID cid,
1373 int32_t count,
1374 const layer_state_t* states)
1375{
1376 Mutex::Autolock _l(mStateLock);
1377 uint32_t flags = 0;
1378 cid <<= 16;
1379 for (int i=0 ; i<count ; i++) {
1380 const layer_state_t& s = states[i];
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001381 sp<LayerBaseClient> layer(getLayerUser_l(s.surface | cid));
Mathias Agopian1473f462009-04-10 14:24:30 -07001382 if (layer != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 const uint32_t what = s.what;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 if (what & ePositionChanged) {
1385 if (layer->setPosition(s.x, s.y))
1386 flags |= eTraversalNeeded;
1387 }
1388 if (what & eLayerChanged) {
1389 if (layer->setLayer(s.z)) {
1390 mCurrentState.layersSortedByZ.reorder(
1391 layer, &Layer::compareCurrentStateZ);
1392 // we need traversal (state changed)
1393 // AND transaction (list changed)
1394 flags |= eTransactionNeeded|eTraversalNeeded;
1395 }
1396 }
1397 if (what & eSizeChanged) {
Mathias Agopian9779b2212009-09-07 16:32:45 -07001398 if (layer->setSize(s.w, s.h)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 flags |= eTraversalNeeded;
Mathias Agopian9779b2212009-09-07 16:32:45 -07001400 mResizeTransationPending = true;
1401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 if (what & eAlphaChanged) {
1404 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1405 flags |= eTraversalNeeded;
1406 }
1407 if (what & eMatrixChanged) {
1408 if (layer->setMatrix(s.matrix))
1409 flags |= eTraversalNeeded;
1410 }
1411 if (what & eTransparentRegionChanged) {
1412 if (layer->setTransparentRegionHint(s.transparentRegion))
1413 flags |= eTraversalNeeded;
1414 }
1415 if (what & eVisibilityChanged) {
1416 if (layer->setFlags(s.flags, s.mask))
1417 flags |= eTraversalNeeded;
1418 }
1419 }
1420 }
1421 if (flags) {
1422 setTransactionFlags(flags);
1423 }
1424 return NO_ERROR;
1425}
1426
Mathias Agopian1473f462009-04-10 14:24:30 -07001427sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428{
Mathias Agopian1473f462009-04-10 14:24:30 -07001429 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1430 return layer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431}
1432
1433void SurfaceFlinger::screenReleased(int dpy)
1434{
1435 // this may be called by a signal handler, we can't do too much in here
1436 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1437 signalEvent();
1438}
1439
1440void SurfaceFlinger::screenAcquired(int dpy)
1441{
1442 // this may be called by a signal handler, we can't do too much in here
1443 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1444 signalEvent();
1445}
1446
1447status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1448{
1449 const size_t SIZE = 1024;
1450 char buffer[SIZE];
1451 String8 result;
Mathias Agopian151e8592009-06-15 18:24:59 -07001452 if (!mDump.checkCalling()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 snprintf(buffer, SIZE, "Permission Denial: "
1454 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1455 IPCThreadState::self()->getCallingPid(),
1456 IPCThreadState::self()->getCallingUid());
1457 result.append(buffer);
1458 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001459
1460 // figure out if we're stuck somewhere
1461 const nsecs_t now = systemTime();
1462 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1463 const nsecs_t inTransaction(mDebugInTransaction);
1464 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1465 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1466
1467 // Try to get the main lock, but don't insist if we can't
1468 // (this would indicate SF is stuck, but we want to be able to
1469 // print something in dumpsys).
1470 int retry = 3;
1471 while (mStateLock.tryLock()<0 && --retry>=0) {
1472 usleep(1000000);
1473 }
1474 const bool locked(retry >= 0);
1475 if (!locked) {
1476 snprintf(buffer, SIZE,
1477 "SurfaceFlinger appears to be unresponsive, "
1478 "dumping anyways (no locks held)\n");
1479 result.append(buffer);
1480 }
1481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 size_t s = mClientsMap.size();
1483 char name[64];
1484 for (size_t i=0 ; i<s ; i++) {
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001485 sp<Client> client = mClientsMap.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 sprintf(name, " Client (id=0x%08x)", client->cid);
1487 client->dump(name);
1488 }
1489 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1490 const size_t count = currentLayers.size();
1491 for (size_t i=0 ; i<count ; i++) {
1492 /*** LayerBase ***/
Mathias Agopian1473f462009-04-10 14:24:30 -07001493 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 const Layer::State& s = layer->drawingState();
1495 snprintf(buffer, SIZE,
1496 "+ %s %p\n"
1497 " "
1498 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
Mathias Agopiancc934762009-09-23 19:16:27 -07001499 "needsBlending=%1d, needsDithering=%1d, invalidate=%1d, "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian1473f462009-04-10 14:24:30 -07001501 layer->getTypeID(), layer.get(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 s.z, layer->tx(), layer->ty(), s.w, s.h,
Mathias Agopiancc934762009-09-23 19:16:27 -07001503 layer->needsBlending(), layer->needsDithering(),
1504 layer->contentDirty,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 s.alpha, s.flags,
1506 s.transform[0], s.transform[1],
1507 s.transform[2], s.transform[3]);
1508 result.append(buffer);
1509 buffer[0] = 0;
1510 /*** LayerBaseClient ***/
Mathias Agopian1473f462009-04-10 14:24:30 -07001511 sp<LayerBaseClient> lbc =
1512 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1513 if (lbc != 0) {
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001514 sp<Client> client(lbc->client.promote());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 snprintf(buffer, SIZE,
1516 " "
1517 "id=0x%08x, client=0x%08x, identity=%u\n",
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001518 lbc->clientIndex(), client.get() ? client->cid : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 lbc->getIdentity());
Mathias Agopian9779b2212009-09-07 16:32:45 -07001520
1521 result.append(buffer);
1522 buffer[0] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 /*** Layer ***/
Mathias Agopian1473f462009-04-10 14:24:30 -07001525 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1526 if (l != 0) {
Mathias Agopianbcef9ac2009-09-17 01:35:28 -07001527 SharedBufferStack::Statistics stats = l->lcblk->getStats();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001528 result.append( l->lcblk->dump(" ") );
Mathias Agopian6950e422009-10-05 17:07:12 -07001529 sp<const GraphicBuffer> buf0(l->getBuffer(0));
1530 sp<const GraphicBuffer> buf1(l->getBuffer(1));
Mathias Agopian53973fd2009-09-14 15:59:16 -07001531 uint32_t w0=0, h0=0, s0=0;
1532 uint32_t w1=0, h1=0, s1=0;
1533 if (buf0 != 0) {
1534 w0 = buf0->getWidth();
1535 h0 = buf0->getHeight();
1536 s0 = buf0->getStride();
1537 }
1538 if (buf1 != 0) {
1539 w1 = buf1->getWidth();
1540 h1 = buf1->getHeight();
1541 s1 = buf1->getStride();
1542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 snprintf(buffer, SIZE,
1544 " "
Mathias Agopian1473f462009-04-10 14:24:30 -07001545 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
Mathias Agopianbcef9ac2009-09-17 01:35:28 -07001546 " freezeLock=%p, dq-q-time=%u us\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 l->pixelFormat(),
Mathias Agopian53973fd2009-09-14 15:59:16 -07001548 w0, h0, s0, w1, h1, s1,
Mathias Agopianbcef9ac2009-09-17 01:35:28 -07001549 l->getFreezeLock().get(), stats.totalTime);
Mathias Agopian9779b2212009-09-07 16:32:45 -07001550 result.append(buffer);
1551 buffer[0] = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 s.transparentRegion.dump(result, "transparentRegion");
1554 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1555 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1556 }
1557 mWormholeRegion.dump(result, "WormholeRegion");
1558 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1559 snprintf(buffer, SIZE,
1560 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1561 mFreezeDisplay?"yes":"no", mFreezeCount,
1562 mCurrentState.orientation, hw.canDraw());
1563 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001564 snprintf(buffer, SIZE,
1565 " last eglSwapBuffers() time: %f us\n"
1566 " last transaction time : %f us\n",
1567 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1568 result.append(buffer);
1569 if (inSwapBuffersDuration || !locked) {
1570 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1571 inSwapBuffersDuration/1000.0);
1572 result.append(buffer);
1573 }
1574 if (inTransactionDuration || !locked) {
1575 snprintf(buffer, SIZE, " transaction time: %f us\n",
1576 inTransactionDuration/1000.0);
1577 result.append(buffer);
1578 }
Mathias Agopian359140c2009-07-02 17:33:40 -07001579 snprintf(buffer, SIZE, " client count: %d\n", mClientsMap.size());
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001580 result.append(buffer);
Mathias Agopian6950e422009-10-05 17:07:12 -07001581 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001582 alloc.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001583
1584 if (locked) {
1585 mStateLock.unlock();
1586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
1588 write(fd, result.string(), result.size());
1589 return NO_ERROR;
1590}
1591
1592status_t SurfaceFlinger::onTransact(
1593 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1594{
1595 switch (code) {
1596 case CREATE_CONNECTION:
1597 case OPEN_GLOBAL_TRANSACTION:
1598 case CLOSE_GLOBAL_TRANSACTION:
1599 case SET_ORIENTATION:
1600 case FREEZE_DISPLAY:
1601 case UNFREEZE_DISPLAY:
1602 case BOOT_FINISHED:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 {
1604 // codes that require permission check
1605 IPCThreadState* ipc = IPCThreadState::self();
1606 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001607 const int uid = ipc->getCallingUid();
Mathias Agopian151e8592009-06-15 18:24:59 -07001608 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1609 LOGE("Permission Denial: "
1610 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1611 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 }
1613 }
1614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1616 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001617 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian151e8592009-06-15 18:24:59 -07001618 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1619 IPCThreadState* ipc = IPCThreadState::self();
1620 const int pid = ipc->getCallingPid();
1621 const int uid = ipc->getCallingUid();
1622 LOGE("Permission Denial: "
1623 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 return PERMISSION_DENIED;
1625 }
1626 int n;
1627 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001628 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 return NO_ERROR;
Mathias Agopianef07dda2009-04-21 18:28:33 -07001630 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 return NO_ERROR;
1632 case 1002: // SHOW_UPDATES
1633 n = data.readInt32();
1634 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1635 return NO_ERROR;
1636 case 1003: // SHOW_BACKGROUND
1637 n = data.readInt32();
1638 mDebugBackground = n ? 1 : 0;
1639 return NO_ERROR;
1640 case 1004:{ // repaint everything
1641 Mutex::Autolock _l(mStateLock);
1642 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1643 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1644 signalEvent();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001645 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001647 case 1005:{ // force transaction
1648 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1649 return NO_ERROR;
1650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 case 1007: // set mFreezeCount
1652 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001653 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 return NO_ERROR;
1655 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001656 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 reply->writeInt32(0);
1658 reply->writeInt32(mDebugRegion);
1659 reply->writeInt32(mDebugBackground);
1660 return NO_ERROR;
1661 case 1013: {
1662 Mutex::Autolock _l(mStateLock);
1663 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1664 reply->writeInt32(hw.getPageFlipCount());
1665 }
1666 return NO_ERROR;
1667 }
1668 }
1669 return err;
1670}
1671
1672// ---------------------------------------------------------------------------
1673#if 0
1674#pragma mark -
1675#endif
1676
1677Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1678 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1679{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 const int pgsize = getpagesize();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001681 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001682
1683 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1684 "SurfaceFlinger Client control-block");
1685
Mathias Agopian9779b2212009-09-07 16:32:45 -07001686 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001687 if (ctrlblk) { // construct the shared structure in-place.
Mathias Agopian9779b2212009-09-07 16:32:45 -07001688 new(ctrlblk) SharedClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
1690}
1691
1692Client::~Client() {
1693 if (ctrlblk) {
Mathias Agopian9779b2212009-09-07 16:32:45 -07001694 ctrlblk->~SharedClient(); // destroy our shared-structure.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
1696}
1697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698int32_t Client::generateId(int pid)
1699{
1700 const uint32_t i = clz( ~mBitmap );
1701 if (i >= NUM_LAYERS_MAX) {
1702 return NO_MEMORY;
1703 }
1704 mPid = pid;
1705 mInUse.add(uint8_t(i));
1706 mBitmap |= 1<<(31-i);
1707 return i;
1708}
Mathias Agopian1473f462009-04-10 14:24:30 -07001709
1710status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711{
1712 ssize_t idx = mInUse.indexOf(id);
1713 if (idx < 0)
1714 return NAME_NOT_FOUND;
1715 return mLayers.insertAt(layer, idx);
1716}
Mathias Agopian1473f462009-04-10 14:24:30 -07001717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718void Client::free(int32_t id)
1719{
1720 ssize_t idx = mInUse.remove(uint8_t(id));
1721 if (idx >= 0) {
1722 mBitmap &= ~(1<<(31-id));
1723 mLayers.removeItemsAt(idx);
1724 }
1725}
1726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727bool Client::isValid(int32_t i) const {
1728 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1729}
Mathias Agopian1473f462009-04-10 14:24:30 -07001730
1731sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1732 sp<LayerBaseClient> lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07001734 if (idx >= 0) {
1735 lbc = mLayers[idx].promote();
1736 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1737 }
1738 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739}
1740
1741void Client::dump(const char* what)
1742{
1743}
1744
1745// ---------------------------------------------------------------------------
1746#if 0
1747#pragma mark -
1748#endif
1749
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001750BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemoryHeap>& cblk)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 : mId(cid), mFlinger(flinger), mCblk(cblk)
1752{
1753}
1754
1755BClient::~BClient() {
1756 // destroy all resources attached to this client
1757 mFlinger->destroyConnection(mId);
1758}
1759
Mathias Agopiand763b5d2009-07-02 18:11:53 -07001760sp<IMemoryHeap> BClient::getControlBlock() const {
1761 return mCblk;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762}
1763
1764sp<ISurface> BClient::createSurface(
1765 ISurfaceFlingerClient::surface_data_t* params, int pid,
1766 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1767 uint32_t flags)
1768{
1769 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1770}
1771
1772status_t BClient::destroySurface(SurfaceID sid)
1773{
1774 sid |= (mId << 16); // add the client-part to id
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001775 return mFlinger->removeSurface(sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776}
1777
1778status_t BClient::setState(int32_t count, const layer_state_t* states)
1779{
1780 return mFlinger->setClientState(mId, count, states);
1781}
1782
1783// ---------------------------------------------------------------------------
1784
1785GraphicPlane::GraphicPlane()
1786 : mHw(0)
1787{
1788}
1789
1790GraphicPlane::~GraphicPlane() {
1791 delete mHw;
1792}
1793
1794bool GraphicPlane::initialized() const {
1795 return mHw ? true : false;
1796}
1797
1798void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1799 mHw = hw;
1800}
1801
1802void GraphicPlane::setTransform(const Transform& tr) {
1803 mTransform = tr;
1804 mGlobalTransform = mOrientationTransform * mTransform;
1805}
1806
1807status_t GraphicPlane::orientationToTransfrom(
1808 int orientation, int w, int h, Transform* tr)
1809{
1810 float a, b, c, d, x, y;
1811 switch (orientation) {
1812 case ISurfaceComposer::eOrientationDefault:
1813 a=1; b=0; c=0; d=1; x=0; y=0;
1814 break;
1815 case ISurfaceComposer::eOrientation90:
1816 a=0; b=-1; c=1; d=0; x=w; y=0;
1817 break;
1818 case ISurfaceComposer::eOrientation180:
1819 a=-1; b=0; c=0; d=-1; x=w; y=h;
1820 break;
1821 case ISurfaceComposer::eOrientation270:
1822 a=0; b=1; c=-1; d=0; x=0; y=h;
1823 break;
1824 default:
1825 return BAD_VALUE;
1826 }
1827 tr->set(a, b, c, d);
1828 tr->set(x, y);
1829 return NO_ERROR;
1830}
1831
1832status_t GraphicPlane::setOrientation(int orientation)
1833{
1834 const DisplayHardware& hw(displayHardware());
1835 const float w = hw.getWidth();
1836 const float h = hw.getHeight();
1837
1838 if (orientation == ISurfaceComposer::eOrientationDefault) {
1839 // make sure the default orientation is optimal
1840 mOrientationTransform.reset();
Mathias Agopian3552f532009-03-27 17:58:20 -07001841 mOrientation = orientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 mGlobalTransform = mTransform;
1843 return NO_ERROR;
1844 }
1845
1846 // If the rotation can be handled in hardware, this is where
1847 // the magic should happen.
1848 if (UNLIKELY(orientation == 42)) {
1849 float a, b, c, d, x, y;
1850 const float r = (3.14159265f / 180.0f) * 42.0f;
1851 const float si = sinf(r);
1852 const float co = cosf(r);
1853 a=co; b=-si; c=si; d=co;
1854 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1855 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1856 mOrientationTransform.set(a, b, c, d);
1857 mOrientationTransform.set(x, y);
1858 } else {
1859 GraphicPlane::orientationToTransfrom(orientation, w, h,
1860 &mOrientationTransform);
1861 }
Mathias Agopian3552f532009-03-27 17:58:20 -07001862 mOrientation = orientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 mGlobalTransform = mOrientationTransform * mTransform;
1864 return NO_ERROR;
1865}
1866
1867const DisplayHardware& GraphicPlane::displayHardware() const {
1868 return *mHw;
1869}
1870
1871const Transform& GraphicPlane::transform() const {
1872 return mGlobalTransform;
1873}
1874
Mathias Agopian1473f462009-04-10 14:24:30 -07001875EGLDisplay GraphicPlane::getEGLDisplay() const {
1876 return mHw->getEGLDisplay();
1877}
1878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879// ---------------------------------------------------------------------------
1880
1881}; // namespace android