blob: 70f34a1a4418e882a6fd72cc3fefd84c915c2be1 [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>
34#include <binder/MemoryDealer.h>
35#include <binder/MemoryBase.h>
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 Agopian076b1cc2009-04-10 14:24:30 -070047#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
49#include "LayerBlur.h"
50#include "LayerBuffer.h"
51#include "LayerDim.h"
52#include "LayerBitmap.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 Agopian076b1cc2009-04-10 14:24:30 -0700176 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 mBootTime(systemTime()),
178 mLastScheduledBroadcast(NULL),
179 mVisibleRegionsDirty(false),
180 mDeferReleaseConsole(false),
181 mFreezeDisplay(false),
182 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700183 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 mDebugBackground(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 mConsoleSignals(0),
187 mSecureFrameBuffer(0)
188{
189 init();
190}
191
192void SurfaceFlinger::init()
193{
194 LOGI("SurfaceFlinger is starting");
195
196 // debugging stuff...
197 char value[PROPERTY_VALUE_MAX];
198 property_get("debug.sf.showupdates", value, "0");
199 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 property_get("debug.sf.showbackground", value, "0");
201 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202
203 LOGI_IF(mDebugRegion, "showupdates enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205}
206
207SurfaceFlinger::~SurfaceFlinger()
208{
209 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210}
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
213{
214 return graphicPlane(0).displayHardware().getOverlayEngine();
215}
216
217sp<IMemory> SurfaceFlinger::getCblk() const
218{
219 return mServerCblkMemory;
220}
221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()
223{
224 Mutex::Autolock _l(mStateLock);
225 uint32_t token = mTokens.acquire();
226
227 Client* client = new Client(token, this);
228 if ((client == 0) || (client->ctrlblk == 0)) {
229 mTokens.release(token);
230 return 0;
231 }
232 status_t err = mClientsMap.add(token, client);
233 if (err < 0) {
234 delete client;
235 mTokens.release(token);
236 return 0;
237 }
238 sp<BClient> bclient =
239 new BClient(this, token, client->controlBlockMemory());
240 return bclient;
241}
242
243void SurfaceFlinger::destroyConnection(ClientID cid)
244{
245 Mutex::Autolock _l(mStateLock);
246 Client* const client = mClientsMap.valueFor(cid);
247 if (client) {
248 // free all the layers this client owns
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 const Vector< wp<LayerBaseClient> >& layers = client->getLayers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 const size_t count = layers.size();
251 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 sp<LayerBaseClient> layer(layers[i].promote());
253 if (layer != 0) {
254 removeLayer_l(layer);
255 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 }
257
258 // the resources associated with this client will be freed
259 // during the next transaction, after these surfaces have been
260 // properly removed from the screen
261
262 // remove this client from our ClientID->Client mapping.
263 mClientsMap.removeItem(cid);
264
265 // and add it to the list of disconnected clients
266 mDisconnectedClients.add(client);
267
268 // request a transaction
269 setTransactionFlags(eTransactionNeeded);
270 }
271}
272
273const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
274{
275 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
276 const GraphicPlane& plane(mGraphicPlanes[dpy]);
277 return plane;
278}
279
280GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
281{
282 return const_cast<GraphicPlane&>(
283 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
284}
285
286void SurfaceFlinger::bootFinished()
287{
288 const nsecs_t now = systemTime();
289 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700290 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
291 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292}
293
294void SurfaceFlinger::onFirstRef()
295{
296 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
297
298 // Wait for the main thread to be done with its initialization
299 mReadyToRunBarrier.wait();
300}
301
302
303static inline uint16_t pack565(int r, int g, int b) {
304 return (r<<11)|(g<<5)|b;
305}
306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307status_t SurfaceFlinger::readyToRun()
308{
309 LOGI( "SurfaceFlinger's main thread ready to run. "
310 "Initializing graphics H/W...");
311
312 // create the shared control-block
313 mServerHeap = new MemoryDealer(4096, MemoryDealer::READ_ONLY);
314 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
315
316 mServerCblkMemory = mServerHeap->allocate(4096);
317 LOGE_IF(mServerCblkMemory==0, "can't create shared control block");
318
319 mServerCblk = static_cast<surface_flinger_cblk_t *>(mServerCblkMemory->pointer());
320 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
321 new(mServerCblk) surface_flinger_cblk_t;
322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 // we only support one display currently
324 int dpy = 0;
325
326 {
327 // initialize the main display
328 GraphicPlane& plane(graphicPlane(dpy));
329 DisplayHardware* const hw = new DisplayHardware(this, dpy);
330 plane.setDisplayHardware(hw);
331 }
332
333 // initialize primary screen
334 // (other display should be initialized in the same manner, but
335 // asynchronously, as they could come and go. None of this is supported
336 // yet).
337 const GraphicPlane& plane(graphicPlane(dpy));
338 const DisplayHardware& hw = plane.displayHardware();
339 const uint32_t w = hw.getWidth();
340 const uint32_t h = hw.getHeight();
341 const uint32_t f = hw.getFormat();
342 hw.makeCurrent();
343
344 // initialize the shared control block
345 mServerCblk->connected |= 1<<dpy;
346 display_cblk_t* dcblk = mServerCblk->displays + dpy;
347 memset(dcblk, 0, sizeof(display_cblk_t));
348 dcblk->w = w;
349 dcblk->h = h;
350 dcblk->format = f;
351 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
352 dcblk->xdpi = hw.getDpiX();
353 dcblk->ydpi = hw.getDpiY();
354 dcblk->fps = hw.getRefreshRate();
355 dcblk->density = hw.getDensity();
356 asm volatile ("":::"memory");
357
358 // Initialize OpenGL|ES
359 glActiveTexture(GL_TEXTURE0);
360 glBindTexture(GL_TEXTURE_2D, 0);
361 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
362 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
363 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
364 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
365 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
366 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
367 glPixelStorei(GL_PACK_ALIGNMENT, 4);
368 glEnableClientState(GL_VERTEX_ARRAY);
369 glEnable(GL_SCISSOR_TEST);
370 glShadeModel(GL_FLAT);
371 glDisable(GL_DITHER);
372 glDisable(GL_CULL_FACE);
373
374 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
375 const uint16_t g1 = pack565(0x17,0x2f,0x17);
376 const uint16_t textureData[4] = { g0, g1, g1, g0 };
377 glGenTextures(1, &mWormholeTexName);
378 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
379 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
380 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
381 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
382 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
383 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
384 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
385
386 glViewport(0, 0, w, h);
387 glMatrixMode(GL_PROJECTION);
388 glLoadIdentity();
389 glOrthof(0, w, h, 0, 0, 1);
390
391 LayerDim::initDimmer(this, w, h);
392
393 mReadyToRunBarrier.open();
394
395 /*
396 * We're now ready to accept clients...
397 */
398
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700399 // start boot animation
400 property_set("ctl.start", "bootanim");
401
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 return NO_ERROR;
403}
404
405// ----------------------------------------------------------------------------
406#if 0
407#pragma mark -
408#pragma mark Events Handler
409#endif
410
411void SurfaceFlinger::waitForEvent()
412{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700413 while (true) {
414 nsecs_t timeout = -1;
415 if (UNLIKELY(isFrozen())) {
416 // wait 5 seconds
417 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
418 const nsecs_t now = systemTime();
419 if (mFreezeDisplayTime == 0) {
420 mFreezeDisplayTime = now;
421 }
422 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
423 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700424 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700425
Mathias Agopianb6683b52009-04-28 03:17:50 -0700426 MessageList::value_type msg = mEventQueue.waitMessage(timeout);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700427 if (msg != 0) {
428 mFreezeDisplayTime = 0;
429 switch (msg->what) {
430 case MessageQueue::INVALIDATE:
431 // invalidate message, just return to the main loop
432 return;
433 }
434 } else {
435 // we timed out
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 if (isFrozen()) {
437 // we timed out and are still frozen
438 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
439 mFreezeDisplay, mFreezeCount);
440 mFreezeCount = 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700441 mFreezeDisplay = false;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700442 return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 }
444 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 }
446}
447
448void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700449 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450}
451
452void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700453 // this is the IPC call
454 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455}
456
457void SurfaceFlinger::signalDelayedEvent(nsecs_t delay)
458{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700459 mEventQueue.postMessage( new MessageBase(MessageQueue::INVALIDATE), delay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460}
461
462// ----------------------------------------------------------------------------
463#if 0
464#pragma mark -
465#pragma mark Main loop
466#endif
467
468bool SurfaceFlinger::threadLoop()
469{
470 waitForEvent();
471
472 // check for transactions
473 if (UNLIKELY(mConsoleSignals)) {
474 handleConsoleEvents();
475 }
476
477 if (LIKELY(mTransactionCount == 0)) {
478 // if we're in a global transaction, don't do anything.
479 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
480 uint32_t transactionFlags = getTransactionFlags(mask);
481 if (LIKELY(transactionFlags)) {
482 handleTransaction(transactionFlags);
483 }
484 }
485
486 // post surfaces (if needed)
487 handlePageFlip();
488
489 const DisplayHardware& hw(graphicPlane(0).displayHardware());
490 if (LIKELY(hw.canDraw())) {
491 // repaint the framebuffer (if needed)
492 handleRepaint();
493
494 // release the clients before we flip ('cause flip might block)
495 unlockClients();
496 executeScheduledBroadcasts();
497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 postFramebuffer();
499 } else {
500 // pretend we did the post
501 unlockClients();
502 executeScheduledBroadcasts();
503 usleep(16667); // 60 fps period
504 }
505 return true;
506}
507
508void SurfaceFlinger::postFramebuffer()
509{
Mathias Agopian0926f502009-05-04 14:17:04 -0700510 if (isFrozen()) {
511 // we are not allowed to draw, but pause a bit to make sure
512 // apps don't end up using the whole CPU, if they depend on
513 // surfaceflinger for synchronization.
514 usleep(8333); // 8.3ms ~ 120fps
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 return;
516 }
517
518 if (!mInvalidRegion.isEmpty()) {
519 const DisplayHardware& hw(graphicPlane(0).displayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 }
523}
524
525void SurfaceFlinger::handleConsoleEvents()
526{
527 // something to do with the console
528 const DisplayHardware& hw = graphicPlane(0).displayHardware();
529
530 int what = android_atomic_and(0, &mConsoleSignals);
531 if (what & eConsoleAcquired) {
532 hw.acquireScreen();
533 }
534
535 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700536 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 hw.releaseScreen();
539 }
540
541 if (what & eConsoleReleased) {
542 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 hw.releaseScreen();
544 } else {
545 mDeferReleaseConsole = true;
546 }
547 }
548
549 mDirtyRegion.set(hw.bounds());
550}
551
552void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
553{
554 Mutex::Autolock _l(mStateLock);
555
556 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
557 const size_t count = currentLayers.size();
558
559 /*
560 * Traversal of the children
561 * (perform the transaction for each of them if needed)
562 */
563
564 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
565 if (layersNeedTransaction) {
566 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
569 if (!trFlags) continue;
570
571 const uint32_t flags = layer->doTransaction(0);
572 if (flags & Layer::eVisibleRegion)
573 mVisibleRegionsDirty = true;
574
575 if (flags & Layer::eRestartTransaction) {
576 // restart the transaction, but back-off a little
577 layer->setTransactionFlags(eTransactionNeeded);
578 setTransactionFlags(eTraversalNeeded, ms2ns(8));
579 }
580 }
581 }
582
583 /*
584 * Perform our own transaction if needed
585 */
586
587 if (transactionFlags & eTransactionNeeded) {
588 if (mCurrentState.orientation != mDrawingState.orientation) {
589 // the orientation has changed, recompute all visible regions
590 // and invalidate everything.
591
592 const int dpy = 0;
593 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700594 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 GraphicPlane& plane(graphicPlane(dpy));
596 plane.setOrientation(orientation);
597
598 // update the shared control block
599 const DisplayHardware& hw(plane.displayHardware());
600 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
601 dcblk->orientation = orientation;
602 if (orientation & eOrientationSwapMask) {
603 // 90 or 270 degrees orientation
604 dcblk->w = hw.getHeight();
605 dcblk->h = hw.getWidth();
606 } else {
607 dcblk->w = hw.getWidth();
608 dcblk->h = hw.getHeight();
609 }
610
611 mVisibleRegionsDirty = true;
612 mDirtyRegion.set(hw.bounds());
Mathias Agopianc08731e2009-03-27 18:11:38 -0700613 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 }
615
616 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
617 // freezing or unfreezing the display -> trigger animation if needed
618 mFreezeDisplay = mCurrentState.freezeDisplay;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 }
620
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700621 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
622 // layers have been added
623 mVisibleRegionsDirty = true;
624 }
625
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 // some layers might have been removed, so
627 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700628 if (mLayersRemoved) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700630 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
631 const ssize_t count = previousLayers.size();
632 for (ssize_t i=0 ; i<count ; i++) {
633 const sp<LayerBase>& layer(previousLayers[i]);
634 if (currentLayers.indexOf( layer ) < 0) {
635 // this layer is not visible anymore
636 // FIXME: would be better to call without the lock held
637 //LOGD("ditching layer %p", layer.get());
638 layer->ditch();
639 }
640 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 }
642
643 // get rid of all resources we don't need anymore
644 // (layers and clients)
645 free_resources_l();
646 }
647
648 commitTransaction();
649}
650
651sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
652{
653 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
654}
655
656void SurfaceFlinger::computeVisibleRegions(
657 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
658{
659 const GraphicPlane& plane(graphicPlane(0));
660 const Transform& planeTransform(plane.transform());
661
662 Region aboveOpaqueLayers;
663 Region aboveCoveredLayers;
664 Region dirty;
665
666 bool secureFrameBuffer = false;
667
668 size_t i = currentLayers.size();
669 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700670 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 layer->validateVisibility(planeTransform);
672
673 // start with the whole surface at its current location
674 const Layer::State& s = layer->drawingState();
675 const Rect bounds(layer->visibleBounds());
676
677 // handle hidden surfaces by setting the visible region to empty
678 Region opaqueRegion;
679 Region visibleRegion;
680 Region coveredRegion;
681 if (UNLIKELY((s.flags & ISurfaceComposer::eLayerHidden) || !s.alpha)) {
682 visibleRegion.clear();
683 } else {
684 const bool translucent = layer->needsBlending();
685 visibleRegion.set(bounds);
686 coveredRegion = visibleRegion;
687
688 // Remove the transparent area from the visible region
689 if (translucent) {
690 visibleRegion.subtractSelf(layer->transparentRegionScreen);
691 }
692
693 // compute the opaque region
694 if (s.alpha==255 && !translucent && layer->getOrientation()>=0) {
695 // the opaque region is the visible region
696 opaqueRegion = visibleRegion;
697 }
698 }
699
700 // subtract the opaque region covered by the layers above us
701 visibleRegion.subtractSelf(aboveOpaqueLayers);
702 coveredRegion.andSelf(aboveCoveredLayers);
703
704 // compute this layer's dirty region
705 if (layer->contentDirty) {
706 // we need to invalidate the whole region
707 dirty = visibleRegion;
708 // as well, as the old visible region
709 dirty.orSelf(layer->visibleRegionScreen);
710 layer->contentDirty = false;
711 } else {
712 // compute the exposed region
713 // dirty = what's visible now - what's wasn't covered before
714 // = what's visible now & what's was covered before
715 dirty = visibleRegion.intersect(layer->coveredRegionScreen);
716 }
717 dirty.subtractSelf(aboveOpaqueLayers);
718
719 // accumulate to the screen dirty region
720 dirtyRegion.orSelf(dirty);
721
Mathias Agopian62b74442009-04-14 23:02:51 -0700722 // Update aboveOpaqueLayers/aboveCoveredLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 aboveOpaqueLayers.orSelf(opaqueRegion);
724 aboveCoveredLayers.orSelf(bounds);
725
726 // Store the visible region is screen space
727 layer->setVisibleRegion(visibleRegion);
728 layer->setCoveredRegion(coveredRegion);
729
Mathias Agopian62b74442009-04-14 23:02:51 -0700730 // If a secure layer is partially visible, lock down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 if (layer->isSecure() && !visibleRegion.isEmpty()) {
732 secureFrameBuffer = true;
733 }
734 }
735
736 mSecureFrameBuffer = secureFrameBuffer;
737 opaqueRegion = aboveOpaqueLayers;
738}
739
740
741void SurfaceFlinger::commitTransaction()
742{
743 mDrawingState = mCurrentState;
744 mTransactionCV.signal();
745}
746
747void SurfaceFlinger::handlePageFlip()
748{
749 bool visibleRegions = mVisibleRegionsDirty;
750 LayerVector& currentLayers = const_cast<LayerVector&>(mDrawingState.layersSortedByZ);
751 visibleRegions |= lockPageFlip(currentLayers);
752
753 const DisplayHardware& hw = graphicPlane(0).displayHardware();
754 const Region screenRegion(hw.bounds());
755 if (visibleRegions) {
756 Region opaqueRegion;
757 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
758 mWormholeRegion = screenRegion.subtract(opaqueRegion);
759 mVisibleRegionsDirty = false;
760 }
761
762 unlockPageFlip(currentLayers);
763 mDirtyRegion.andSelf(screenRegion);
764}
765
766bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
767{
768 bool recomputeVisibleRegions = false;
769 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700770 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700772 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 layer->lockPageFlip(recomputeVisibleRegions);
774 }
775 return recomputeVisibleRegions;
776}
777
778void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
779{
780 const GraphicPlane& plane(graphicPlane(0));
781 const Transform& planeTransform(plane.transform());
782 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700783 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700785 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 layer->unlockPageFlip(planeTransform, mDirtyRegion);
787 }
788}
789
790void SurfaceFlinger::handleRepaint()
791{
792 // set the frame buffer
793 const DisplayHardware& hw(graphicPlane(0).displayHardware());
794 glMatrixMode(GL_MODELVIEW);
795 glLoadIdentity();
796
797 if (UNLIKELY(mDebugRegion)) {
798 debugFlashRegions();
799 }
800
801 // compute the invalid region
802 mInvalidRegion.orSelf(mDirtyRegion);
803
804 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700805 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
806 (flags & DisplayHardware::BUFFER_PRESERVED))
807 {
808 // we can redraw only what's dirty
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 } else {
810 if (flags & DisplayHardware::UPDATE_ON_DEMAND) {
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700811 // we need to redraw the rectangle that will be updated
812 // (pushed to the framebuffer).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813 mDirtyRegion.set(mInvalidRegion.bounds());
814 } else {
815 // we need to redraw everything
816 mDirtyRegion.set(hw.bounds());
817 mInvalidRegion = mDirtyRegion;
818 }
819 }
820
821 // compose all surfaces
822 composeSurfaces(mDirtyRegion);
823
824 // clear the dirty regions
825 mDirtyRegion.clear();
826}
827
828void SurfaceFlinger::composeSurfaces(const Region& dirty)
829{
830 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
831 // should never happen unless the window manager has a bug
832 // draw something...
833 drawWormhole();
834 }
835 const SurfaceFlinger& flinger(*this);
836 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
837 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700838 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700840 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 const Region& visibleRegion(layer->visibleRegionScreen);
842 if (!visibleRegion.isEmpty()) {
843 const Region clip(dirty.intersect(visibleRegion));
844 if (!clip.isEmpty()) {
845 layer->draw(clip);
846 }
847 }
848 }
849}
850
851void SurfaceFlinger::unlockClients()
852{
853 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
854 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700855 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700857 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 layer->finishPageFlip();
859 }
860}
861
862void SurfaceFlinger::scheduleBroadcast(Client* client)
863{
864 if (mLastScheduledBroadcast != client) {
865 mLastScheduledBroadcast = client;
866 mScheduledBroadcasts.add(client);
867 }
868}
869
870void SurfaceFlinger::executeScheduledBroadcasts()
871{
872 SortedVector<Client*>& list = mScheduledBroadcasts;
873 size_t count = list.size();
874 while (count--) {
875 per_client_cblk_t* const cblk = list[count]->ctrlblk;
876 if (cblk->lock.tryLock() == NO_ERROR) {
877 cblk->cv.broadcast();
878 list.removeAt(count);
879 cblk->lock.unlock();
880 } else {
881 // schedule another round
882 LOGW("executeScheduledBroadcasts() skipped, "
883 "contention on the client. We'll try again later...");
884 signalDelayedEvent(ms2ns(4));
885 }
886 }
887 mLastScheduledBroadcast = 0;
888}
889
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890void SurfaceFlinger::debugFlashRegions()
891{
Mathias Agopian0926f502009-05-04 14:17:04 -0700892 const DisplayHardware& hw(graphicPlane(0).displayHardware());
893 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700894
895 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
896 (flags & DisplayHardware::BUFFER_PRESERVED))) {
Mathias Agopian0926f502009-05-04 14:17:04 -0700897 const Region repaint((flags & DisplayHardware::UPDATE_ON_DEMAND) ?
898 mDirtyRegion.bounds() : hw.bounds());
899 composeSurfaces(repaint);
900 }
901
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 glDisable(GL_TEXTURE_2D);
903 glDisable(GL_BLEND);
904 glDisable(GL_DITHER);
905 glDisable(GL_SCISSOR_TEST);
906
Mathias Agopian0926f502009-05-04 14:17:04 -0700907 static int toggle = 0;
908 toggle = 1 - toggle;
909 if (toggle) {
910 glColor4x(0x10000, 0, 0x10000, 0x10000);
911 } else {
912 glColor4x(0x10000, 0x10000, 0, 0x10000);
913 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914
Mathias Agopian20f68782009-05-11 00:03:41 -0700915 Region::const_iterator it = mDirtyRegion.begin();
916 Region::const_iterator const end = mDirtyRegion.end();
917 while (it != end) {
918 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 GLfloat vertices[][2] = {
920 { r.left, r.top },
921 { r.left, r.bottom },
922 { r.right, r.bottom },
923 { r.right, r.top }
924 };
925 glVertexPointer(2, GL_FLOAT, 0, vertices);
926 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
927 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 hw.flip(mDirtyRegion.merge(mInvalidRegion));
930 mInvalidRegion.clear();
931
932 if (mDebugRegion > 1)
933 usleep(mDebugRegion * 1000);
934
935 glEnable(GL_SCISSOR_TEST);
936 //mDirtyRegion.dump("mDirtyRegion");
937}
938
939void SurfaceFlinger::drawWormhole() const
940{
941 const Region region(mWormholeRegion.intersect(mDirtyRegion));
942 if (region.isEmpty())
943 return;
944
945 const DisplayHardware& hw(graphicPlane(0).displayHardware());
946 const int32_t width = hw.getWidth();
947 const int32_t height = hw.getHeight();
948
949 glDisable(GL_BLEND);
950 glDisable(GL_DITHER);
951
952 if (LIKELY(!mDebugBackground)) {
953 glClearColorx(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700954 Region::const_iterator it = region.begin();
955 Region::const_iterator const end = region.end();
956 while (it != end) {
957 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 const GLint sy = height - (r.top + r.height());
959 glScissor(r.left, sy, r.width(), r.height());
960 glClear(GL_COLOR_BUFFER_BIT);
961 }
962 } else {
963 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
964 { width, height }, { 0, height } };
965 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
966 glVertexPointer(2, GL_SHORT, 0, vertices);
967 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
968 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
969 glEnable(GL_TEXTURE_2D);
970 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
971 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
972 glMatrixMode(GL_TEXTURE);
973 glLoadIdentity();
974 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -0700975 Region::const_iterator it = region.begin();
976 Region::const_iterator const end = region.end();
977 while (it != end) {
978 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979 const GLint sy = height - (r.top + r.height());
980 glScissor(r.left, sy, r.width(), r.height());
981 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
982 }
983 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
984 }
985}
986
987void SurfaceFlinger::debugShowFPS() const
988{
989 static int mFrameCount;
990 static int mLastFrameCount = 0;
991 static nsecs_t mLastFpsTime = 0;
992 static float mFps = 0;
993 mFrameCount++;
994 nsecs_t now = systemTime();
995 nsecs_t diff = now - mLastFpsTime;
996 if (diff > ms2ns(250)) {
997 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
998 mLastFpsTime = now;
999 mLastFrameCount = mFrameCount;
1000 }
1001 // XXX: mFPS has the value we want
1002 }
1003
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001004status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005{
1006 Mutex::Autolock _l(mStateLock);
1007 addLayer_l(layer);
1008 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1009 return NO_ERROR;
1010}
1011
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001012status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013{
1014 Mutex::Autolock _l(mStateLock);
1015 removeLayer_l(layer);
1016 setTransactionFlags(eTransactionNeeded);
1017 return NO_ERROR;
1018}
1019
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001020status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021{
1022 layer->forceVisibilityTransaction();
1023 setTransactionFlags(eTraversalNeeded);
1024 return NO_ERROR;
1025}
1026
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001027status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028{
1029 ssize_t i = mCurrentState.layersSortedByZ.add(
1030 layer, &LayerBase::compareCurrentStateZ);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001031 sp<LayerBaseClient> lbc = LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1032 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 mLayerMap.add(lbc->serverIndex(), lbc);
1034 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 return NO_ERROR;
1036}
1037
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001038status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039{
1040 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1041 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001042 mLayersRemoved = true;
Mathias Agopian9a112062009-04-17 19:36:26 -07001043 sp<LayerBaseClient> layer =
1044 LayerBase::dynamicCast< LayerBaseClient* >(layerBase.get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001045 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 mLayerMap.removeItem(layer->serverIndex());
1047 }
1048 return NO_ERROR;
1049 }
1050 // it's possible that we don't find a layer, because it might
1051 // have been destroyed already -- this is not technically an error
Mathias Agopian9a112062009-04-17 19:36:26 -07001052 // from the user because there is a race between BClient::destroySurface(),
1053 // ~BClient() and destroySurface-from-a-transaction.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 return (index == NAME_NOT_FOUND) ? status_t(NO_ERROR) : index;
1055}
1056
Mathias Agopian9a112062009-04-17 19:36:26 -07001057status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1058{
1059 // First add the layer to the purgatory list, which makes sure it won't
1060 // go away, then remove it from the main list (through a transaction).
1061 ssize_t err = removeLayer_l(layerBase);
1062 if (err >= 0) {
1063 mLayerPurgatory.add(layerBase);
1064 }
1065 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1066}
1067
1068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069void SurfaceFlinger::free_resources_l()
1070{
1071 // Destroy layers that were removed
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001072 mLayersRemoved = false;
1073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 // free resources associated with disconnected clients
1075 SortedVector<Client*>& scheduledBroadcasts(mScheduledBroadcasts);
1076 Vector<Client*>& disconnectedClients(mDisconnectedClients);
1077 const size_t count = disconnectedClients.size();
1078 for (size_t i=0 ; i<count ; i++) {
1079 Client* client = disconnectedClients[i];
1080 // if this client is the scheduled broadcast list,
1081 // remove it from there (and we don't need to signal it
1082 // since it is dead).
1083 int32_t index = scheduledBroadcasts.indexOf(client);
1084 if (index >= 0) {
1085 scheduledBroadcasts.removeItemsAt(index);
1086 }
1087 mTokens.release(client->cid);
1088 delete client;
1089 }
1090 disconnectedClients.clear();
1091}
1092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1094{
1095 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1096}
1097
1098uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, nsecs_t delay)
1099{
1100 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1101 if ((old & flags)==0) { // wake the server up
1102 if (delay > 0) {
1103 signalDelayedEvent(delay);
1104 } else {
1105 signalEvent();
1106 }
1107 }
1108 return old;
1109}
1110
1111void SurfaceFlinger::openGlobalTransaction()
1112{
1113 android_atomic_inc(&mTransactionCount);
1114}
1115
1116void SurfaceFlinger::closeGlobalTransaction()
1117{
1118 if (android_atomic_dec(&mTransactionCount) == 1) {
1119 signalEvent();
1120 }
1121}
1122
1123status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1124{
1125 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1126 return BAD_VALUE;
1127
1128 Mutex::Autolock _l(mStateLock);
1129 mCurrentState.freezeDisplay = 1;
1130 setTransactionFlags(eTransactionNeeded);
1131
1132 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001133 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 return NO_ERROR;
1135}
1136
1137status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1138{
1139 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1140 return BAD_VALUE;
1141
1142 Mutex::Autolock _l(mStateLock);
1143 mCurrentState.freezeDisplay = 0;
1144 setTransactionFlags(eTransactionNeeded);
1145
1146 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001147 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 return NO_ERROR;
1149}
1150
Mathias Agopianc08731e2009-03-27 18:11:38 -07001151int SurfaceFlinger::setOrientation(DisplayID dpy,
1152 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153{
1154 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1155 return BAD_VALUE;
1156
1157 Mutex::Autolock _l(mStateLock);
1158 if (mCurrentState.orientation != orientation) {
1159 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001160 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 mCurrentState.orientation = orientation;
1162 setTransactionFlags(eTransactionNeeded);
1163 mTransactionCV.wait(mStateLock);
1164 } else {
1165 orientation = BAD_VALUE;
1166 }
1167 }
1168 return orientation;
1169}
1170
1171sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
1172 ISurfaceFlingerClient::surface_data_t* params,
1173 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1174 uint32_t flags)
1175{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001176 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 sp<LayerBaseClient::Surface> surfaceHandle;
1178 Mutex::Autolock _l(mStateLock);
1179 Client* const c = mClientsMap.valueFor(clientId);
1180 if (UNLIKELY(!c)) {
1181 LOGE("createSurface() failed, client not found (id=%d)", clientId);
1182 return surfaceHandle;
1183 }
1184
1185 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
1186 int32_t id = c->generateId(pid);
1187 if (uint32_t(id) >= NUM_LAYERS_MAX) {
1188 LOGE("createSurface() failed, generateId = %d", id);
1189 return surfaceHandle;
1190 }
1191
1192 switch (flags & eFXSurfaceMask) {
1193 case eFXSurfaceNormal:
1194 if (UNLIKELY(flags & ePushBuffers)) {
1195 layer = createPushBuffersSurfaceLocked(c, d, id, w, h, flags);
1196 } else {
1197 layer = createNormalSurfaceLocked(c, d, id, w, h, format, flags);
1198 }
1199 break;
1200 case eFXSurfaceBlur:
1201 layer = createBlurSurfaceLocked(c, d, id, w, h, flags);
1202 break;
1203 case eFXSurfaceDim:
1204 layer = createDimSurfaceLocked(c, d, id, w, h, flags);
1205 break;
1206 }
1207
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001208 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 setTransactionFlags(eTransactionNeeded);
1210 surfaceHandle = layer->getSurface();
1211 if (surfaceHandle != 0)
1212 surfaceHandle->getSurfaceData(params);
1213 }
1214
1215 return surfaceHandle;
1216}
1217
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001218sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 Client* client, DisplayID display,
1220 int32_t id, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
1221{
1222 // initialize the surfaces
1223 switch (format) { // TODO: take h/w into account
1224 case PIXEL_FORMAT_TRANSPARENT:
1225 case PIXEL_FORMAT_TRANSLUCENT:
1226 format = PIXEL_FORMAT_RGBA_8888;
1227 break;
1228 case PIXEL_FORMAT_OPAQUE:
1229 format = PIXEL_FORMAT_RGB_565;
1230 break;
1231 }
1232
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001233 sp<Layer> layer = new Layer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 status_t err = layer->setBuffers(client, w, h, format, flags);
1235 if (LIKELY(err == NO_ERROR)) {
1236 layer->initStates(w, h, flags);
1237 addLayer_l(layer);
1238 } else {
1239 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001240 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 }
1242 return layer;
1243}
1244
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001245sp<LayerBaseClient> SurfaceFlinger::createBlurSurfaceLocked(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 Client* client, DisplayID display,
1247 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1248{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001249 sp<LayerBlur> layer = new LayerBlur(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 layer->initStates(w, h, flags);
1251 addLayer_l(layer);
1252 return layer;
1253}
1254
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001255sp<LayerBaseClient> SurfaceFlinger::createDimSurfaceLocked(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 Client* client, DisplayID display,
1257 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1258{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001259 sp<LayerDim> layer = new LayerDim(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 layer->initStates(w, h, flags);
1261 addLayer_l(layer);
1262 return layer;
1263}
1264
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001265sp<LayerBaseClient> SurfaceFlinger::createPushBuffersSurfaceLocked(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 Client* client, DisplayID display,
1267 int32_t id, uint32_t w, uint32_t h, uint32_t flags)
1268{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001269 sp<LayerBuffer> layer = new LayerBuffer(this, display, client, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 layer->initStates(w, h, flags);
1271 addLayer_l(layer);
1272 return layer;
1273}
1274
Mathias Agopian9a112062009-04-17 19:36:26 -07001275status_t SurfaceFlinger::removeSurface(SurfaceID index)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276{
Mathias Agopian9a112062009-04-17 19:36:26 -07001277 /*
1278 * called by the window manager, when a surface should be marked for
1279 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001280 *
1281 * The surface is removed from the current and drawing lists, but placed
1282 * in the purgatory queue, so it's not destroyed right-away (we need
1283 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001284 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001285
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001286 Mutex::Autolock _l(mStateLock);
1287 sp<LayerBaseClient> layer = getLayerUser_l(index);
1288 status_t err = purgatorizeLayer_l(layer);
1289 if (err == NO_ERROR) {
1290 setTransactionFlags(eTransactionNeeded);
Mathias Agopian9a112062009-04-17 19:36:26 -07001291 }
1292 return err;
1293}
1294
1295status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1296{
1297 /*
1298 * called by ~ISurface() when all references are gone
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001299 *
1300 * the surface must be removed from purgatory from the main thread
1301 * since its dtor must run from there (b/c of OpenGL ES).
Mathias Agopian9a112062009-04-17 19:36:26 -07001302 */
1303
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001304 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001305 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001306 sp<LayerBaseClient> layer;
1307 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001308 MessageDestroySurface(
1309 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1310 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001311 virtual bool handler() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001312 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001313 ssize_t idx = flinger->mLayerPurgatory.remove(layer);
1314 LOGE_IF(idx<0, "layer=%p is not in the purgatory list", layer.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001315 return true;
1316 }
1317 };
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001318 mEventQueue.postMessage( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 return NO_ERROR;
1320}
1321
1322status_t SurfaceFlinger::setClientState(
1323 ClientID cid,
1324 int32_t count,
1325 const layer_state_t* states)
1326{
1327 Mutex::Autolock _l(mStateLock);
1328 uint32_t flags = 0;
1329 cid <<= 16;
1330 for (int i=0 ; i<count ; i++) {
1331 const layer_state_t& s = states[i];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001332 const sp<LayerBaseClient>& layer = getLayerUser_l(s.surface | cid);
1333 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 const uint32_t what = s.what;
1335 // check if it has been destroyed first
1336 if (what & eDestroyed) {
1337 if (removeLayer_l(layer) == NO_ERROR) {
1338 flags |= eTransactionNeeded;
1339 // we skip everything else... well, no, not really
1340 // we skip ONLY that transaction.
1341 continue;
1342 }
1343 }
1344 if (what & ePositionChanged) {
1345 if (layer->setPosition(s.x, s.y))
1346 flags |= eTraversalNeeded;
1347 }
1348 if (what & eLayerChanged) {
1349 if (layer->setLayer(s.z)) {
1350 mCurrentState.layersSortedByZ.reorder(
1351 layer, &Layer::compareCurrentStateZ);
1352 // we need traversal (state changed)
1353 // AND transaction (list changed)
1354 flags |= eTransactionNeeded|eTraversalNeeded;
1355 }
1356 }
1357 if (what & eSizeChanged) {
1358 if (layer->setSize(s.w, s.h))
1359 flags |= eTraversalNeeded;
1360 }
1361 if (what & eAlphaChanged) {
1362 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1363 flags |= eTraversalNeeded;
1364 }
1365 if (what & eMatrixChanged) {
1366 if (layer->setMatrix(s.matrix))
1367 flags |= eTraversalNeeded;
1368 }
1369 if (what & eTransparentRegionChanged) {
1370 if (layer->setTransparentRegionHint(s.transparentRegion))
1371 flags |= eTraversalNeeded;
1372 }
1373 if (what & eVisibilityChanged) {
1374 if (layer->setFlags(s.flags, s.mask))
1375 flags |= eTraversalNeeded;
1376 }
1377 }
1378 }
1379 if (flags) {
1380 setTransactionFlags(flags);
1381 }
1382 return NO_ERROR;
1383}
1384
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001385sp<LayerBaseClient> SurfaceFlinger::getLayerUser_l(SurfaceID s) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001387 sp<LayerBaseClient> layer = mLayerMap.valueFor(s);
1388 return layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389}
1390
1391void SurfaceFlinger::screenReleased(int dpy)
1392{
1393 // this may be called by a signal handler, we can't do too much in here
1394 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1395 signalEvent();
1396}
1397
1398void SurfaceFlinger::screenAcquired(int dpy)
1399{
1400 // this may be called by a signal handler, we can't do too much in here
1401 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1402 signalEvent();
1403}
1404
1405status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1406{
1407 const size_t SIZE = 1024;
1408 char buffer[SIZE];
1409 String8 result;
1410 if (checkCallingPermission(
1411 String16("android.permission.DUMP")) == false)
1412 { // not allowed
1413 snprintf(buffer, SIZE, "Permission Denial: "
1414 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1415 IPCThreadState::self()->getCallingPid(),
1416 IPCThreadState::self()->getCallingUid());
1417 result.append(buffer);
1418 } else {
1419 Mutex::Autolock _l(mStateLock);
1420 size_t s = mClientsMap.size();
1421 char name[64];
1422 for (size_t i=0 ; i<s ; i++) {
1423 Client* client = mClientsMap.valueAt(i);
1424 sprintf(name, " Client (id=0x%08x)", client->cid);
1425 client->dump(name);
1426 }
1427 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1428 const size_t count = currentLayers.size();
1429 for (size_t i=0 ; i<count ; i++) {
1430 /*** LayerBase ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001431 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 const Layer::State& s = layer->drawingState();
1433 snprintf(buffer, SIZE,
1434 "+ %s %p\n"
1435 " "
1436 "z=%9d, pos=(%4d,%4d), size=(%4d,%4d), "
1437 "needsBlending=%1d, invalidate=%1d, "
1438 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001439 layer->getTypeID(), layer.get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 s.z, layer->tx(), layer->ty(), s.w, s.h,
1441 layer->needsBlending(), layer->contentDirty,
1442 s.alpha, s.flags,
1443 s.transform[0], s.transform[1],
1444 s.transform[2], s.transform[3]);
1445 result.append(buffer);
1446 buffer[0] = 0;
1447 /*** LayerBaseClient ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001448 sp<LayerBaseClient> lbc =
1449 LayerBase::dynamicCast< LayerBaseClient* >(layer.get());
1450 if (lbc != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 snprintf(buffer, SIZE,
1452 " "
1453 "id=0x%08x, client=0x%08x, identity=%u\n",
1454 lbc->clientIndex(), lbc->client ? lbc->client->cid : 0,
1455 lbc->getIdentity());
1456 }
1457 result.append(buffer);
1458 buffer[0] = 0;
1459 /*** Layer ***/
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001460 sp<Layer> l = LayerBase::dynamicCast< Layer* >(layer.get());
1461 if (l != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 const LayerBitmap& buf0(l->getBuffer(0));
1463 const LayerBitmap& buf1(l->getBuffer(1));
1464 snprintf(buffer, SIZE,
1465 " "
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001466 "format=%2d, [%3ux%3u:%3u] [%3ux%3u:%3u],"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 " freezeLock=%p, swapState=0x%08x\n",
1468 l->pixelFormat(),
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001469 buf0.getWidth(), buf0.getHeight(),
1470 buf0.getBuffer()->getStride(),
1471 buf1.getWidth(), buf1.getHeight(),
1472 buf1.getBuffer()->getStride(),
1473 l->getFreezeLock().get(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 l->lcblk->swapState);
1475 }
1476 result.append(buffer);
1477 buffer[0] = 0;
1478 s.transparentRegion.dump(result, "transparentRegion");
1479 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1480 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1481 }
1482 mWormholeRegion.dump(result, "WormholeRegion");
1483 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1484 snprintf(buffer, SIZE,
1485 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1486 mFreezeDisplay?"yes":"no", mFreezeCount,
1487 mCurrentState.orientation, hw.canDraw());
1488 result.append(buffer);
1489
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001490 const BufferAllocator& alloc(BufferAllocator::get());
1491 alloc.dump(result);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 }
1493 write(fd, result.string(), result.size());
1494 return NO_ERROR;
1495}
1496
1497status_t SurfaceFlinger::onTransact(
1498 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1499{
1500 switch (code) {
1501 case CREATE_CONNECTION:
1502 case OPEN_GLOBAL_TRANSACTION:
1503 case CLOSE_GLOBAL_TRANSACTION:
1504 case SET_ORIENTATION:
1505 case FREEZE_DISPLAY:
1506 case UNFREEZE_DISPLAY:
1507 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 {
1509 // codes that require permission check
1510 IPCThreadState* ipc = IPCThreadState::self();
1511 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001512 const int uid = ipc->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 const int self_pid = getpid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001514 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515 // we're called from a different process, do the real check
1516 if (!checkCallingPermission(
1517 String16("android.permission.ACCESS_SURFACE_FLINGER")))
1518 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 LOGE("Permission Denial: "
1520 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1521 return PERMISSION_DENIED;
1522 }
1523 }
1524 }
1525 }
1526
1527 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1528 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1529 // HARDWARE_TEST stuff...
1530 if (UNLIKELY(checkCallingPermission(
1531 String16("android.permission.HARDWARE_TEST")) == false))
1532 { // not allowed
1533 LOGE("Permission Denial: pid=%d, uid=%d\n",
1534 IPCThreadState::self()->getCallingPid(),
1535 IPCThreadState::self()->getCallingUid());
1536 return PERMISSION_DENIED;
1537 }
1538 int n;
1539 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001540 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001542 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 return NO_ERROR;
1544 case 1002: // SHOW_UPDATES
1545 n = data.readInt32();
1546 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1547 return NO_ERROR;
1548 case 1003: // SHOW_BACKGROUND
1549 n = data.readInt32();
1550 mDebugBackground = n ? 1 : 0;
1551 return NO_ERROR;
1552 case 1004:{ // repaint everything
1553 Mutex::Autolock _l(mStateLock);
1554 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1555 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1556 signalEvent();
1557 }
1558 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 case 1007: // set mFreezeCount
1560 mFreezeCount = data.readInt32();
1561 return NO_ERROR;
1562 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001563 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 reply->writeInt32(0);
1565 reply->writeInt32(mDebugRegion);
1566 reply->writeInt32(mDebugBackground);
1567 return NO_ERROR;
1568 case 1013: {
1569 Mutex::Autolock _l(mStateLock);
1570 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1571 reply->writeInt32(hw.getPageFlipCount());
1572 }
1573 return NO_ERROR;
1574 }
1575 }
1576 return err;
1577}
1578
1579// ---------------------------------------------------------------------------
1580#if 0
1581#pragma mark -
1582#endif
1583
1584Client::Client(ClientID clientID, const sp<SurfaceFlinger>& flinger)
1585 : ctrlblk(0), cid(clientID), mPid(0), mBitmap(0), mFlinger(flinger)
1586{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 const int pgsize = getpagesize();
1588 const int cblksize=((sizeof(per_client_cblk_t)+(pgsize-1))&~(pgsize-1));
1589 mCblkHeap = new MemoryDealer(cblksize);
1590 mCblkMemory = mCblkHeap->allocate(cblksize);
1591 if (mCblkMemory != 0) {
1592 ctrlblk = static_cast<per_client_cblk_t *>(mCblkMemory->pointer());
1593 if (ctrlblk) { // construct the shared structure in-place.
1594 new(ctrlblk) per_client_cblk_t;
1595 }
1596 }
1597}
1598
1599Client::~Client() {
1600 if (ctrlblk) {
1601 const int pgsize = getpagesize();
1602 ctrlblk->~per_client_cblk_t(); // destroy our shared-structure.
1603 }
1604}
1605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606int32_t Client::generateId(int pid)
1607{
1608 const uint32_t i = clz( ~mBitmap );
1609 if (i >= NUM_LAYERS_MAX) {
1610 return NO_MEMORY;
1611 }
1612 mPid = pid;
1613 mInUse.add(uint8_t(i));
1614 mBitmap |= 1<<(31-i);
1615 return i;
1616}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001617
1618status_t Client::bindLayer(const sp<LayerBaseClient>& layer, int32_t id)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619{
1620 ssize_t idx = mInUse.indexOf(id);
1621 if (idx < 0)
1622 return NAME_NOT_FOUND;
1623 return mLayers.insertAt(layer, idx);
1624}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001625
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626void Client::free(int32_t id)
1627{
1628 ssize_t idx = mInUse.remove(uint8_t(id));
1629 if (idx >= 0) {
1630 mBitmap &= ~(1<<(31-id));
1631 mLayers.removeItemsAt(idx);
1632 }
1633}
1634
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635bool Client::isValid(int32_t i) const {
1636 return (uint32_t(i)<NUM_LAYERS_MAX) && (mBitmap & (1<<(31-i)));
1637}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001638
1639sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1640 sp<LayerBaseClient> lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 ssize_t idx = mInUse.indexOf(uint8_t(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001642 if (idx >= 0) {
1643 lbc = mLayers[idx].promote();
1644 LOGE_IF(lbc==0, "getLayerUser(i=%d), idx=%d is dead", int(i), int(idx));
1645 }
1646 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647}
1648
1649void Client::dump(const char* what)
1650{
1651}
1652
1653// ---------------------------------------------------------------------------
1654#if 0
1655#pragma mark -
1656#endif
1657
1658BClient::BClient(SurfaceFlinger *flinger, ClientID cid, const sp<IMemory>& cblk)
1659 : mId(cid), mFlinger(flinger), mCblk(cblk)
1660{
1661}
1662
1663BClient::~BClient() {
1664 // destroy all resources attached to this client
1665 mFlinger->destroyConnection(mId);
1666}
1667
1668void BClient::getControlBlocks(sp<IMemory>* ctrl) const {
1669 *ctrl = mCblk;
1670}
1671
1672sp<ISurface> BClient::createSurface(
1673 ISurfaceFlingerClient::surface_data_t* params, int pid,
1674 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1675 uint32_t flags)
1676{
1677 return mFlinger->createSurface(mId, pid, params, display, w, h, format, flags);
1678}
1679
1680status_t BClient::destroySurface(SurfaceID sid)
1681{
1682 sid |= (mId << 16); // add the client-part to id
Mathias Agopian9a112062009-04-17 19:36:26 -07001683 return mFlinger->removeSurface(sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684}
1685
1686status_t BClient::setState(int32_t count, const layer_state_t* states)
1687{
1688 return mFlinger->setClientState(mId, count, states);
1689}
1690
1691// ---------------------------------------------------------------------------
1692
1693GraphicPlane::GraphicPlane()
1694 : mHw(0)
1695{
1696}
1697
1698GraphicPlane::~GraphicPlane() {
1699 delete mHw;
1700}
1701
1702bool GraphicPlane::initialized() const {
1703 return mHw ? true : false;
1704}
1705
1706void GraphicPlane::setDisplayHardware(DisplayHardware *hw) {
1707 mHw = hw;
1708}
1709
1710void GraphicPlane::setTransform(const Transform& tr) {
1711 mTransform = tr;
1712 mGlobalTransform = mOrientationTransform * mTransform;
1713}
1714
1715status_t GraphicPlane::orientationToTransfrom(
1716 int orientation, int w, int h, Transform* tr)
1717{
1718 float a, b, c, d, x, y;
1719 switch (orientation) {
1720 case ISurfaceComposer::eOrientationDefault:
1721 a=1; b=0; c=0; d=1; x=0; y=0;
1722 break;
1723 case ISurfaceComposer::eOrientation90:
1724 a=0; b=-1; c=1; d=0; x=w; y=0;
1725 break;
1726 case ISurfaceComposer::eOrientation180:
1727 a=-1; b=0; c=0; d=-1; x=w; y=h;
1728 break;
1729 case ISurfaceComposer::eOrientation270:
1730 a=0; b=1; c=-1; d=0; x=0; y=h;
1731 break;
1732 default:
1733 return BAD_VALUE;
1734 }
1735 tr->set(a, b, c, d);
1736 tr->set(x, y);
1737 return NO_ERROR;
1738}
1739
1740status_t GraphicPlane::setOrientation(int orientation)
1741{
1742 const DisplayHardware& hw(displayHardware());
1743 const float w = hw.getWidth();
1744 const float h = hw.getHeight();
1745
1746 if (orientation == ISurfaceComposer::eOrientationDefault) {
1747 // make sure the default orientation is optimal
1748 mOrientationTransform.reset();
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001749 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 mGlobalTransform = mTransform;
1751 return NO_ERROR;
1752 }
1753
1754 // If the rotation can be handled in hardware, this is where
1755 // the magic should happen.
1756 if (UNLIKELY(orientation == 42)) {
1757 float a, b, c, d, x, y;
1758 const float r = (3.14159265f / 180.0f) * 42.0f;
1759 const float si = sinf(r);
1760 const float co = cosf(r);
1761 a=co; b=-si; c=si; d=co;
1762 x = si*(h*0.5f) + (1-co)*(w*0.5f);
1763 y =-si*(w*0.5f) + (1-co)*(h*0.5f);
1764 mOrientationTransform.set(a, b, c, d);
1765 mOrientationTransform.set(x, y);
1766 } else {
1767 GraphicPlane::orientationToTransfrom(orientation, w, h,
1768 &mOrientationTransform);
1769 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001770 mOrientation = orientation;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 mGlobalTransform = mOrientationTransform * mTransform;
1772 return NO_ERROR;
1773}
1774
1775const DisplayHardware& GraphicPlane::displayHardware() const {
1776 return *mHw;
1777}
1778
1779const Transform& GraphicPlane::transform() const {
1780 return mGlobalTransform;
1781}
1782
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001783EGLDisplay GraphicPlane::getEGLDisplay() const {
1784 return mHw->getEGLDisplay();
1785}
1786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001787// ---------------------------------------------------------------------------
1788
1789}; // namespace android