blob: 23efd161fad741594504d89ab5d9112e675954de [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/String8.h>
37#include <utils/String16.h>
38#include <utils/StopWatch.h>
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <ui/PixelFormat.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 Agopian1f7bec62010-06-25 18:02:21 -070047#include "GLExtensions.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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54#include "DisplayHardware/DisplayHardware.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopiana1ecca92009-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 Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -070088 const sp<LayerBase>& key, size_t guess) const
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -070095 LOGE_IF(layers[idx]!=key,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 "LayerVector[%p]: layers[%d]=%p, key=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097 this, int(idx), layers[idx].get(), key.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 return idx;
99 }
100 return i;
101}
102
103ssize_t SurfaceFlinger::LayerVector::add(
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104 const sp<LayerBase>& layer,
105 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700111 sp<LayerBase> const* a = layers.array();
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700134ssize_t SurfaceFlinger::LayerVector::remove(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700139 LOGE_IF(layers[index]!=layer,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 "LayerVector[%p]: layers[%u]=%p, layer=%p",
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 this, int(index), layers[index].get(), layer.get());
The Android Open Source Projectedbf3b62009-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 Agopian076b1cc2009-04-10 14:24:30 -0700156 const sp<LayerBase>& layer,
157 Vector< sp<LayerBase> >::compar_t cmp)
The Android Open Source Projectedbf3b62009-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 Agopiancbb288b2009-09-07 16:32:45 -0700175 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700176 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 mBootTime(systemTime()),
Mathias Agopian375f5632009-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 Projectedbf3b62009-03-03 19:31:44 -0800181 mVisibleRegionsDirty(false),
182 mDeferReleaseConsole(false),
183 mFreezeDisplay(false),
184 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700185 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 mDebugBackground(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700188 mDebugInSwapBuffers(0),
189 mLastSwapBufferTime(0),
190 mDebugInTransaction(0),
191 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700192 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 mConsoleSignals(0),
194 mSecureFrameBuffer(0)
195{
196 init();
197}
198
199void SurfaceFlinger::init()
200{
201 LOGI("SurfaceFlinger is starting");
202
203 // debugging stuff...
204 char value[PROPERTY_VALUE_MAX];
205 property_get("debug.sf.showupdates", value, "0");
206 mDebugRegion = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 property_get("debug.sf.showbackground", value, "0");
208 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209
Mathias Agopian78fd5012010-04-20 14:51:04 -0700210 LOGI_IF(mDebugRegion, "showupdates enabled");
211 LOGI_IF(mDebugBackground, "showbackground enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
214SurfaceFlinger::~SurfaceFlinger()
215{
216 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217}
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219overlay_control_device_t* SurfaceFlinger::getOverlayEngine() const
220{
221 return graphicPlane(0).displayHardware().getOverlayEngine();
222}
223
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700224sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700226 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227}
228
Mathias Agopian7e27f052010-05-28 14:22:23 -0700229sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230{
Mathias Agopian96f08192010-06-02 23:28:45 -0700231 sp<ISurfaceComposerClient> bclient;
232 sp<Client> client(new Client(this));
233 status_t err = client->initCheck();
234 if (err == NO_ERROR) {
235 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 return bclient;
238}
239
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700240sp<ISurfaceComposerClient> SurfaceFlinger::createClientConnection()
241{
242 sp<ISurfaceComposerClient> bclient;
243 sp<UserClient> client(new UserClient(this));
244 status_t err = client->initCheck();
245 if (err == NO_ERROR) {
246 bclient = client;
247 }
248 return bclient;
249}
250
251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
253{
254 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
255 const GraphicPlane& plane(mGraphicPlanes[dpy]);
256 return plane;
257}
258
259GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
260{
261 return const_cast<GraphicPlane&>(
262 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
263}
264
265void SurfaceFlinger::bootFinished()
266{
267 const nsecs_t now = systemTime();
268 const nsecs_t duration = now - mBootTime;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700269 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700270 mBootFinished = true;
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700271 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272}
273
274void SurfaceFlinger::onFirstRef()
275{
276 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
277
278 // Wait for the main thread to be done with its initialization
279 mReadyToRunBarrier.wait();
280}
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282static inline uint16_t pack565(int r, int g, int b) {
283 return (r<<11)|(g<<5)|b;
284}
285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286status_t SurfaceFlinger::readyToRun()
287{
288 LOGI( "SurfaceFlinger's main thread ready to run. "
289 "Initializing graphics H/W...");
290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 // we only support one display currently
292 int dpy = 0;
293
294 {
295 // initialize the main display
296 GraphicPlane& plane(graphicPlane(dpy));
297 DisplayHardware* const hw = new DisplayHardware(this, dpy);
298 plane.setDisplayHardware(hw);
299 }
300
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700301 // create the shared control-block
302 mServerHeap = new MemoryHeapBase(4096,
303 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
304 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
305
306 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
307 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
308
309 new(mServerCblk) surface_flinger_cblk_t;
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 // initialize primary screen
312 // (other display should be initialized in the same manner, but
313 // asynchronously, as they could come and go. None of this is supported
314 // yet).
315 const GraphicPlane& plane(graphicPlane(dpy));
316 const DisplayHardware& hw = plane.displayHardware();
317 const uint32_t w = hw.getWidth();
318 const uint32_t h = hw.getHeight();
319 const uint32_t f = hw.getFormat();
320 hw.makeCurrent();
321
322 // initialize the shared control block
323 mServerCblk->connected |= 1<<dpy;
324 display_cblk_t* dcblk = mServerCblk->displays + dpy;
325 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800326 dcblk->w = plane.getWidth();
327 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 dcblk->format = f;
329 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
330 dcblk->xdpi = hw.getDpiX();
331 dcblk->ydpi = hw.getDpiY();
332 dcblk->fps = hw.getRefreshRate();
333 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334
335 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
337 glPixelStorei(GL_PACK_ALIGNMENT, 4);
338 glEnableClientState(GL_VERTEX_ARRAY);
339 glEnable(GL_SCISSOR_TEST);
340 glShadeModel(GL_FLAT);
341 glDisable(GL_DITHER);
342 glDisable(GL_CULL_FACE);
343
344 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
345 const uint16_t g1 = pack565(0x17,0x2f,0x17);
346 const uint16_t textureData[4] = { g0, g1, g1, g0 };
347 glGenTextures(1, &mWormholeTexName);
348 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
350 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
351 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
352 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
353 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
354 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
355
356 glViewport(0, 0, w, h);
357 glMatrixMode(GL_PROJECTION);
358 glLoadIdentity();
359 glOrthof(0, w, h, 0, 0, 1);
360
361 LayerDim::initDimmer(this, w, h);
362
363 mReadyToRunBarrier.open();
364
365 /*
366 * We're now ready to accept clients...
367 */
368
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700369 // start boot animation
370 property_set("ctl.start", "bootanim");
371
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 return NO_ERROR;
373}
374
375// ----------------------------------------------------------------------------
376#if 0
377#pragma mark -
378#pragma mark Events Handler
379#endif
380
381void SurfaceFlinger::waitForEvent()
382{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700383 while (true) {
384 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800385 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700386 if (UNLIKELY(isFrozen())) {
387 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700388 const nsecs_t now = systemTime();
389 if (mFreezeDisplayTime == 0) {
390 mFreezeDisplayTime = now;
391 }
392 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
393 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700394 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700395
Mathias Agopianbb641242010-05-18 17:06:55 -0700396 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800397
398 // see if we timed out
399 if (isFrozen()) {
400 const nsecs_t now = systemTime();
401 nsecs_t frozenTime = (now - mFreezeDisplayTime);
402 if (frozenTime >= freezeDisplayTimeout) {
403 // we timed out and are still frozen
404 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
405 mFreezeDisplay, mFreezeCount);
406 mFreezeDisplayTime = 0;
407 mFreezeCount = 0;
408 mFreezeDisplay = false;
409 }
410 }
411
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700412 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700413 switch (msg->what) {
414 case MessageQueue::INVALIDATE:
415 // invalidate message, just return to the main loop
416 return;
417 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 }
420}
421
422void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700423 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424}
425
426void SurfaceFlinger::signal() const {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700427 // this is the IPC call
428 const_cast<SurfaceFlinger*>(this)->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429}
430
Mathias Agopianbb641242010-05-18 17:06:55 -0700431status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
432 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433{
Mathias Agopianbb641242010-05-18 17:06:55 -0700434 return mEventQueue.postMessage(msg, reltime, flags);
435}
436
437status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
438 nsecs_t reltime, uint32_t flags)
439{
440 status_t res = mEventQueue.postMessage(msg, reltime, flags);
441 if (res == NO_ERROR) {
442 msg->wait();
443 }
444 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445}
446
447// ----------------------------------------------------------------------------
448#if 0
449#pragma mark -
450#pragma mark Main loop
451#endif
452
453bool SurfaceFlinger::threadLoop()
454{
455 waitForEvent();
456
457 // check for transactions
458 if (UNLIKELY(mConsoleSignals)) {
459 handleConsoleEvents();
460 }
461
462 if (LIKELY(mTransactionCount == 0)) {
463 // if we're in a global transaction, don't do anything.
464 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
465 uint32_t transactionFlags = getTransactionFlags(mask);
466 if (LIKELY(transactionFlags)) {
467 handleTransaction(transactionFlags);
468 }
469 }
470
471 // post surfaces (if needed)
472 handlePageFlip();
473
474 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700475 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 // repaint the framebuffer (if needed)
477 handleRepaint();
478
Mathias Agopian74faca22009-09-17 16:18:16 -0700479 // inform the h/w that we're done compositing
480 hw.compositionComplete();
481
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 // release the clients before we flip ('cause flip might block)
483 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 postFramebuffer();
486 } else {
487 // pretend we did the post
488 unlockClients();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 usleep(16667); // 60 fps period
490 }
491 return true;
492}
493
494void SurfaceFlinger::postFramebuffer()
495{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 if (!mInvalidRegion.isEmpty()) {
497 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700498 const nsecs_t now = systemTime();
499 mDebugInSwapBuffers = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 hw.flip(mInvalidRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700501 mLastSwapBufferTime = systemTime() - now;
502 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 mInvalidRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504 }
505}
506
507void SurfaceFlinger::handleConsoleEvents()
508{
509 // something to do with the console
510 const DisplayHardware& hw = graphicPlane(0).displayHardware();
511
512 int what = android_atomic_and(0, &mConsoleSignals);
513 if (what & eConsoleAcquired) {
514 hw.acquireScreen();
515 }
516
517 if (mDeferReleaseConsole && hw.canDraw()) {
Mathias Agopian62b74442009-04-14 23:02:51 -0700518 // We got the release signal before the acquire signal
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 mDeferReleaseConsole = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 hw.releaseScreen();
521 }
522
523 if (what & eConsoleReleased) {
524 if (hw.canDraw()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525 hw.releaseScreen();
526 } else {
527 mDeferReleaseConsole = true;
528 }
529 }
530
531 mDirtyRegion.set(hw.bounds());
532}
533
534void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
535{
Mathias Agopian3d579642009-06-04 18:46:21 -0700536 Vector< sp<LayerBase> > ditchedLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537
Mathias Agopian3d579642009-06-04 18:46:21 -0700538 { // scope for the lock
539 Mutex::Autolock _l(mStateLock);
Mathias Agopian9795c422009-08-26 16:36:26 -0700540 const nsecs_t now = systemTime();
541 mDebugInTransaction = now;
Mathias Agopian3d579642009-06-04 18:46:21 -0700542 handleTransactionLocked(transactionFlags, ditchedLayers);
Mathias Agopian9795c422009-08-26 16:36:26 -0700543 mLastTransactionTime = systemTime() - now;
544 mDebugInTransaction = 0;
Mathias Agopian3d579642009-06-04 18:46:21 -0700545 }
546
547 // do this without lock held
548 const size_t count = ditchedLayers.size();
549 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0852e672009-09-04 19:50:23 -0700550 if (ditchedLayers[i] != 0) {
551 //LOGD("ditching layer %p", ditchedLayers[i].get());
552 ditchedLayers[i]->ditch();
553 }
Mathias Agopian3d579642009-06-04 18:46:21 -0700554 }
555}
556
557void SurfaceFlinger::handleTransactionLocked(
558 uint32_t transactionFlags, Vector< sp<LayerBase> >& ditchedLayers)
559{
560 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 const size_t count = currentLayers.size();
562
563 /*
564 * Traversal of the children
565 * (perform the transaction for each of them if needed)
566 */
567
568 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
569 if (layersNeedTransaction) {
570 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700571 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
573 if (!trFlags) continue;
574
575 const uint32_t flags = layer->doTransaction(0);
576 if (flags & Layer::eVisibleRegion)
577 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 }
579 }
580
581 /*
582 * Perform our own transaction if needed
583 */
584
585 if (transactionFlags & eTransactionNeeded) {
586 if (mCurrentState.orientation != mDrawingState.orientation) {
587 // the orientation has changed, recompute all visible regions
588 // and invalidate everything.
589
590 const int dpy = 0;
591 const int orientation = mCurrentState.orientation;
Mathias Agopianc08731e2009-03-27 18:11:38 -0700592 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 GraphicPlane& plane(graphicPlane(dpy));
594 plane.setOrientation(orientation);
595
596 // update the shared control block
597 const DisplayHardware& hw(plane.displayHardware());
598 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
599 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800600 dcblk->w = plane.getWidth();
601 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602
603 mVisibleRegionsDirty = true;
604 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 }
606
607 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
608 // freezing or unfreezing the display -> trigger animation if needed
609 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800610 if (mFreezeDisplay)
611 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 }
613
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700614 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
615 // layers have been added
616 mVisibleRegionsDirty = true;
617 }
618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 // some layers might have been removed, so
620 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700621 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700622 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700624 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700625 const size_t count = previousLayers.size();
626 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700627 const sp<LayerBase>& layer(previousLayers[i]);
628 if (currentLayers.indexOf( layer ) < 0) {
629 // this layer is not visible anymore
Mathias Agopian3d579642009-06-04 18:46:21 -0700630 ditchedLayers.add(layer);
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700631 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700632 }
633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 }
636
637 commitTransaction();
638}
639
640sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
641{
642 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
643}
644
645void SurfaceFlinger::computeVisibleRegions(
646 LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
647{
648 const GraphicPlane& plane(graphicPlane(0));
649 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700650 const DisplayHardware& hw(plane.displayHardware());
651 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652
653 Region aboveOpaqueLayers;
654 Region aboveCoveredLayers;
655 Region dirty;
656
657 bool secureFrameBuffer = false;
658
659 size_t i = currentLayers.size();
660 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 layer->validateVisibility(planeTransform);
663
664 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700665 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666
Mathias Agopianab028732010-03-16 16:41:46 -0700667 /*
668 * opaqueRegion: area of a surface that is fully opaque.
669 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700671
672 /*
673 * visibleRegion: area of a surface that is visible on screen
674 * and not fully transparent. This is essentially the layer's
675 * footprint minus the opaque regions above it.
676 * Areas covered by a translucent surface are considered visible.
677 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700679
680 /*
681 * coveredRegion: area of a surface that is covered by all
682 * visible regions above it (which includes the translucent areas).
683 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700685
686
687 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700688 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 const bool translucent = layer->needsBlending();
Mathias Agopian97011222009-07-28 10:57:27 -0700690 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700692 visibleRegion.andSelf(screenRegion);
693 if (!visibleRegion.isEmpty()) {
694 // Remove the transparent area from the visible region
695 if (translucent) {
696 visibleRegion.subtractSelf(layer->transparentRegionScreen);
697 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698
Mathias Agopianab028732010-03-16 16:41:46 -0700699 // compute the opaque region
700 const int32_t layerOrientation = layer->getOrientation();
701 if (s.alpha==255 && !translucent &&
702 ((layerOrientation & Transform::ROT_INVALID) == false)) {
703 // the opaque region is the layer's footprint
704 opaqueRegion = visibleRegion;
705 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 }
707 }
708
Mathias Agopianab028732010-03-16 16:41:46 -0700709 // Clip the covered region to the visible region
710 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
711
712 // Update aboveCoveredLayers for next (lower) layer
713 aboveCoveredLayers.orSelf(visibleRegion);
714
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 // subtract the opaque region covered by the layers above us
716 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717
718 // compute this layer's dirty region
719 if (layer->contentDirty) {
720 // we need to invalidate the whole region
721 dirty = visibleRegion;
722 // as well, as the old visible region
723 dirty.orSelf(layer->visibleRegionScreen);
724 layer->contentDirty = false;
725 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700726 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700727 * the exposed region consists of two components:
728 * 1) what's VISIBLE now and was COVERED before
729 * 2) what's EXPOSED now less what was EXPOSED before
730 *
731 * note that (1) is conservative, we start with the whole
732 * visible region but only keep what used to be covered by
733 * something -- which mean it may have been exposed.
734 *
735 * (2) handles areas that were not covered by anything but got
736 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700737 */
Mathias Agopianab028732010-03-16 16:41:46 -0700738 const Region newExposed = visibleRegion - coveredRegion;
739 const Region oldVisibleRegion = layer->visibleRegionScreen;
740 const Region oldCoveredRegion = layer->coveredRegionScreen;
741 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
742 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 }
744 dirty.subtractSelf(aboveOpaqueLayers);
745
746 // accumulate to the screen dirty region
747 dirtyRegion.orSelf(dirty);
748
Mathias Agopianab028732010-03-16 16:41:46 -0700749 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 aboveOpaqueLayers.orSelf(opaqueRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751
752 // Store the visible region is screen space
753 layer->setVisibleRegion(visibleRegion);
754 layer->setCoveredRegion(coveredRegion);
755
Mathias Agopian97011222009-07-28 10:57:27 -0700756 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 if (layer->isSecure() && !visibleRegion.isEmpty()) {
758 secureFrameBuffer = true;
759 }
760 }
761
Mathias Agopian97011222009-07-28 10:57:27 -0700762 // invalidate the areas where a layer was removed
763 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
764 mDirtyRegionRemovedLayer.clear();
765
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 mSecureFrameBuffer = secureFrameBuffer;
767 opaqueRegion = aboveOpaqueLayers;
768}
769
770
771void SurfaceFlinger::commitTransaction()
772{
773 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700774 mResizeTransationPending = false;
775 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776}
777
778void SurfaceFlinger::handlePageFlip()
779{
780 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700781 LayerVector& currentLayers = const_cast<LayerVector&>(
782 mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783 visibleRegions |= lockPageFlip(currentLayers);
784
785 const DisplayHardware& hw = graphicPlane(0).displayHardware();
786 const Region screenRegion(hw.bounds());
787 if (visibleRegions) {
788 Region opaqueRegion;
789 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
790 mWormholeRegion = screenRegion.subtract(opaqueRegion);
791 mVisibleRegionsDirty = false;
792 }
793
794 unlockPageFlip(currentLayers);
795 mDirtyRegion.andSelf(screenRegion);
796}
797
798bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
799{
800 bool recomputeVisibleRegions = false;
801 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700802 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700804 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 layer->lockPageFlip(recomputeVisibleRegions);
806 }
807 return recomputeVisibleRegions;
808}
809
810void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
811{
812 const GraphicPlane& plane(graphicPlane(0));
813 const Transform& planeTransform(plane.transform());
814 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700815 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700817 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 layer->unlockPageFlip(planeTransform, mDirtyRegion);
819 }
820}
821
Mathias Agopianb8a55602009-06-26 19:06:36 -0700822
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823void SurfaceFlinger::handleRepaint()
824{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825 // compute the invalid region
826 mInvalidRegion.orSelf(mDirtyRegion);
827 if (mInvalidRegion.isEmpty()) {
828 // nothing to do
829 return;
830 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831
832 if (UNLIKELY(mDebugRegion)) {
833 debugFlashRegions();
834 }
835
Mathias Agopianb8a55602009-06-26 19:06:36 -0700836 // set the frame buffer
837 const DisplayHardware& hw(graphicPlane(0).displayHardware());
838 glMatrixMode(GL_MODELVIEW);
839 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840
841 uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700842 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
843 (flags & DisplayHardware::BUFFER_PRESERVED))
844 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700845 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
846 // takes a rectangle, we must make sure to update that whole
847 // rectangle in that case
848 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700849 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700850 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 mDirtyRegion.set(mInvalidRegion.bounds());
852 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // in the BUFFER_PRESERVED case, obviously, we can update only
854 // what's needed and nothing more.
855 // NOTE: this is NOT a common case, as preserving the backbuffer
856 // is costly and usually involves copying the whole update back.
857 }
858 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700859 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700860 // We need to redraw the rectangle that will be updated
861 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700862 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700863 // rectangle instead of a region (see DisplayHardware::flip())
864 mDirtyRegion.set(mInvalidRegion.bounds());
865 } else {
866 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 mDirtyRegion.set(hw.bounds());
868 mInvalidRegion = mDirtyRegion;
869 }
870 }
871
872 // compose all surfaces
873 composeSurfaces(mDirtyRegion);
874
875 // clear the dirty regions
876 mDirtyRegion.clear();
877}
878
879void SurfaceFlinger::composeSurfaces(const Region& dirty)
880{
881 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
882 // should never happen unless the window manager has a bug
883 // draw something...
884 drawWormhole();
885 }
886 const SurfaceFlinger& flinger(*this);
887 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
888 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700889 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700891 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 const Region& visibleRegion(layer->visibleRegionScreen);
893 if (!visibleRegion.isEmpty()) {
894 const Region clip(dirty.intersect(visibleRegion));
895 if (!clip.isEmpty()) {
896 layer->draw(clip);
897 }
898 }
899 }
900}
901
902void SurfaceFlinger::unlockClients()
903{
904 const LayerVector& drawingLayers(mDrawingState.layersSortedByZ);
905 const size_t count = drawingLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700906 sp<LayerBase> const* const layers = drawingLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700908 const sp<LayerBase>& layer = layers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 layer->finishPageFlip();
910 }
911}
912
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913void SurfaceFlinger::debugFlashRegions()
914{
Mathias Agopian0a917752010-06-14 21:20:00 -0700915 const DisplayHardware& hw(graphicPlane(0).displayHardware());
916 const uint32_t flags = hw.getFlags();
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700917
Mathias Agopian0a917752010-06-14 21:20:00 -0700918 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
919 (flags & DisplayHardware::BUFFER_PRESERVED))) {
920 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
921 mDirtyRegion.bounds() : hw.bounds());
922 composeSurfaces(repaint);
923 }
924
925 TextureManager::deactivateTextures();
926
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 glDisable(GL_BLEND);
928 glDisable(GL_DITHER);
929 glDisable(GL_SCISSOR_TEST);
930
Mathias Agopian0926f502009-05-04 14:17:04 -0700931 static int toggle = 0;
932 toggle = 1 - toggle;
933 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700934 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700935 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700936 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700937 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938
Mathias Agopian20f68782009-05-11 00:03:41 -0700939 Region::const_iterator it = mDirtyRegion.begin();
940 Region::const_iterator const end = mDirtyRegion.end();
941 while (it != end) {
942 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 GLfloat vertices[][2] = {
944 { r.left, r.top },
945 { r.left, r.bottom },
946 { r.right, r.bottom },
947 { r.right, r.top }
948 };
949 glVertexPointer(2, GL_FLOAT, 0, vertices);
950 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
951 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700952
Mathias Agopianb8a55602009-06-26 19:06:36 -0700953 if (mInvalidRegion.isEmpty()) {
954 mDirtyRegion.dump("mDirtyRegion");
955 mInvalidRegion.dump("mInvalidRegion");
956 }
957 hw.flip(mInvalidRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958
959 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700960 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961
962 glEnable(GL_SCISSOR_TEST);
963 //mDirtyRegion.dump("mDirtyRegion");
964}
965
966void SurfaceFlinger::drawWormhole() const
967{
968 const Region region(mWormholeRegion.intersect(mDirtyRegion));
969 if (region.isEmpty())
970 return;
971
972 const DisplayHardware& hw(graphicPlane(0).displayHardware());
973 const int32_t width = hw.getWidth();
974 const int32_t height = hw.getHeight();
975
976 glDisable(GL_BLEND);
977 glDisable(GL_DITHER);
978
979 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700980 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -0700981 Region::const_iterator it = region.begin();
982 Region::const_iterator const end = region.end();
983 while (it != end) {
984 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 const GLint sy = height - (r.top + r.height());
986 glScissor(r.left, sy, r.width(), r.height());
987 glClear(GL_COLOR_BUFFER_BIT);
988 }
989 } else {
990 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
991 { width, height }, { 0, height } };
992 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
993 glVertexPointer(2, GL_SHORT, 0, vertices);
994 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
995 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian0a917752010-06-14 21:20:00 -0700996#if defined(GL_OES_texture_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700997 if (GLExtensions::getInstance().haveTextureExternal()) {
998 glDisable(GL_TEXTURE_EXTERNAL_OES);
999 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001000#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 glEnable(GL_TEXTURE_2D);
1002 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1003 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1004 glMatrixMode(GL_TEXTURE);
1005 glLoadIdentity();
1006 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001007 Region::const_iterator it = region.begin();
1008 Region::const_iterator const end = region.end();
1009 while (it != end) {
1010 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 const GLint sy = height - (r.top + r.height());
1012 glScissor(r.left, sy, r.width(), r.height());
1013 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1014 }
1015 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1016 }
1017}
1018
1019void SurfaceFlinger::debugShowFPS() const
1020{
1021 static int mFrameCount;
1022 static int mLastFrameCount = 0;
1023 static nsecs_t mLastFpsTime = 0;
1024 static float mFps = 0;
1025 mFrameCount++;
1026 nsecs_t now = systemTime();
1027 nsecs_t diff = now - mLastFpsTime;
1028 if (diff > ms2ns(250)) {
1029 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1030 mLastFpsTime = now;
1031 mLastFrameCount = mFrameCount;
1032 }
1033 // XXX: mFPS has the value we want
1034 }
1035
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001036status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037{
1038 Mutex::Autolock _l(mStateLock);
1039 addLayer_l(layer);
1040 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1041 return NO_ERROR;
1042}
1043
Mathias Agopian96f08192010-06-02 23:28:45 -07001044status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1045{
1046 ssize_t i = mCurrentState.layersSortedByZ.add(
1047 layer, &LayerBase::compareCurrentStateZ);
1048 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1049}
1050
1051ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1052 const sp<LayerBaseClient>& lbc)
1053{
1054 Mutex::Autolock _l(mStateLock);
1055
1056 // attach this layer to the client
1057 ssize_t name = client->attachLayer(lbc);
1058
1059 // add this layer to the current state list
1060 addLayer_l(lbc);
1061
1062 return name;
1063}
1064
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001065status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066{
1067 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001068 status_t err = purgatorizeLayer_l(layer);
1069 if (err == NO_ERROR)
1070 setTransactionFlags(eTransactionNeeded);
1071 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072}
1073
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001076 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1077 if (lbc != 0) {
1078 mLayerMap.removeItem( lbc->getSurface()->asBinder() );
1079 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1081 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001082 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 return NO_ERROR;
1084 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001085 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086}
1087
Mathias Agopian9a112062009-04-17 19:36:26 -07001088status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1089{
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001090 // remove the layer from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001091 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001092
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001093 layerBase->onRemoved();
1094
Mathias Agopian3d579642009-06-04 18:46:21 -07001095 // it's possible that we don't find a layer, because it might
1096 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001097 // from the user because there is a race between Client::destroySurface(),
1098 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001099 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1100}
1101
Mathias Agopian96f08192010-06-02 23:28:45 -07001102status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103{
Mathias Agopian96f08192010-06-02 23:28:45 -07001104 layer->forceVisibilityTransaction();
1105 setTransactionFlags(eTraversalNeeded);
1106 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107}
1108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1110{
1111 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1112}
1113
Mathias Agopianbb641242010-05-18 17:06:55 -07001114uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115{
1116 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1117 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001118 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 }
1120 return old;
1121}
1122
1123void SurfaceFlinger::openGlobalTransaction()
1124{
1125 android_atomic_inc(&mTransactionCount);
1126}
1127
1128void SurfaceFlinger::closeGlobalTransaction()
1129{
1130 if (android_atomic_dec(&mTransactionCount) == 1) {
1131 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001132
1133 // if there is a transaction with a resize, wait for it to
1134 // take effect before returning.
1135 Mutex::Autolock _l(mStateLock);
1136 while (mResizeTransationPending) {
Mathias Agopian448f0152009-09-30 14:42:13 -07001137 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1138 if (CC_UNLIKELY(err != NO_ERROR)) {
1139 // just in case something goes wrong in SF, return to the
1140 // called after a few seconds.
1141 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1142 mResizeTransationPending = false;
1143 break;
1144 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 }
1147}
1148
1149status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1150{
1151 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1152 return BAD_VALUE;
1153
1154 Mutex::Autolock _l(mStateLock);
1155 mCurrentState.freezeDisplay = 1;
1156 setTransactionFlags(eTransactionNeeded);
1157
1158 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001159 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 return NO_ERROR;
1161}
1162
1163status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1164{
1165 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1166 return BAD_VALUE;
1167
1168 Mutex::Autolock _l(mStateLock);
1169 mCurrentState.freezeDisplay = 0;
1170 setTransactionFlags(eTransactionNeeded);
1171
1172 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001173 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174 return NO_ERROR;
1175}
1176
Mathias Agopianc08731e2009-03-27 18:11:38 -07001177int SurfaceFlinger::setOrientation(DisplayID dpy,
1178 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179{
1180 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1181 return BAD_VALUE;
1182
1183 Mutex::Autolock _l(mStateLock);
1184 if (mCurrentState.orientation != orientation) {
1185 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianc08731e2009-03-27 18:11:38 -07001186 mCurrentState.orientationType = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 mCurrentState.orientation = orientation;
1188 setTransactionFlags(eTransactionNeeded);
1189 mTransactionCV.wait(mStateLock);
1190 } else {
1191 orientation = BAD_VALUE;
1192 }
1193 }
1194 return orientation;
1195}
1196
Mathias Agopian96f08192010-06-02 23:28:45 -07001197sp<ISurface> SurfaceFlinger::createSurface(const sp<Client>& client, int pid,
Mathias Agopian7e27f052010-05-28 14:22:23 -07001198 const String8& name, ISurfaceComposerClient::surface_data_t* params,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1200 uint32_t flags)
1201{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001202 sp<LayerBaseClient> layer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 sp<LayerBaseClient::Surface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001204
1205 if (int32_t(w|h) < 0) {
1206 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1207 int(w), int(h));
1208 return surfaceHandle;
1209 }
1210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 switch (flags & eFXSurfaceMask) {
1214 case eFXSurfaceNormal:
1215 if (UNLIKELY(flags & ePushBuffers)) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001216 layer = createPushBuffersSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 } else {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001218 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1219 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 }
1221 break;
1222 case eFXSurfaceBlur:
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 layer = createBlurSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 break;
1225 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001226 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 break;
1228 }
1229
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001230 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001232 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001233 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 surfaceHandle = layer->getSurface();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001236 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001237 params->token = token;
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001238 params->identity = surfaceHandle->getIdentity();
1239 params->width = w;
1240 params->height = h;
1241 params->format = format;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001242 if (normalLayer != 0) {
1243 Mutex::Autolock _l(mStateLock);
1244 mLayerMap.add(surfaceHandle->asBinder(), normalLayer);
1245 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001246 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001247
Mathias Agopian96f08192010-06-02 23:28:45 -07001248 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 }
1250
1251 return surfaceHandle;
1252}
1253
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001254sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001255 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001256 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001257 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258{
1259 // initialize the surfaces
1260 switch (format) { // TODO: take h/w into account
1261 case PIXEL_FORMAT_TRANSPARENT:
1262 case PIXEL_FORMAT_TRANSLUCENT:
1263 format = PIXEL_FORMAT_RGBA_8888;
1264 break;
1265 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001266 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 break;
1268 }
1269
Mathias Agopian96f08192010-06-02 23:28:45 -07001270 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001271 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001272 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001274 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 }
1276 return layer;
1277}
1278
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001279sp<LayerBlur> SurfaceFlinger::createBlurSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001280 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001281 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282{
Mathias Agopian96f08192010-06-02 23:28:45 -07001283 sp<LayerBlur> layer = new LayerBlur(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 return layer;
1286}
1287
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001288sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001289 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001290 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291{
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 return layer;
1295}
1296
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001297sp<LayerBuffer> SurfaceFlinger::createPushBuffersSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001298 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300{
Mathias Agopian96f08192010-06-02 23:28:45 -07001301 sp<LayerBuffer> layer = new LayerBuffer(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 return layer;
1304}
1305
Mathias Agopian96f08192010-06-02 23:28:45 -07001306status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307{
Mathias Agopian9a112062009-04-17 19:36:26 -07001308 /*
1309 * called by the window manager, when a surface should be marked for
1310 * destruction.
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001311 *
1312 * The surface is removed from the current and drawing lists, but placed
1313 * in the purgatory queue, so it's not destroyed right-away (we need
1314 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001315 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001316
Mathias Agopian48d819a2009-09-10 19:41:18 -07001317 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001318 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001319 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001320 if (layer != 0) {
1321 err = purgatorizeLayer_l(layer);
1322 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001323 setTransactionFlags(eTransactionNeeded);
1324 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001325 }
1326 return err;
1327}
1328
1329status_t SurfaceFlinger::destroySurface(const sp<LayerBaseClient>& layer)
1330{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001331 // called by ~ISurface() when all references are gone
Mathias Agopian9a112062009-04-17 19:36:26 -07001332
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001333 class MessageDestroySurface : public MessageBase {
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001334 SurfaceFlinger* flinger;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001335 sp<LayerBaseClient> layer;
1336 public:
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001337 MessageDestroySurface(
1338 SurfaceFlinger* flinger, const sp<LayerBaseClient>& layer)
1339 : flinger(flinger), layer(layer) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001340 virtual bool handler() {
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001341 sp<LayerBaseClient> l(layer);
1342 layer.clear(); // clear it outside of the lock;
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001343 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian759fdb22009-07-02 17:33:40 -07001344 /*
1345 * remove the layer from the current list -- chances are that it's
1346 * not in the list anyway, because it should have been removed
1347 * already upon request of the client (eg: window manager).
1348 * However, a buggy client could have not done that.
1349 * Since we know we don't have any more clients, we don't need
1350 * to use the purgatory.
1351 */
Mathias Agopiancd8c5e22009-06-19 16:24:02 -07001352 status_t err = flinger->removeLayer_l(l);
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001353 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1354 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001355 return true;
1356 }
1357 };
Mathias Agopian3d579642009-06-04 18:46:21 -07001358
Mathias Agopianbb641242010-05-18 17:06:55 -07001359 postMessageAsync( new MessageDestroySurface(this, layer) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 return NO_ERROR;
1361}
1362
1363status_t SurfaceFlinger::setClientState(
Mathias Agopian96f08192010-06-02 23:28:45 -07001364 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 int32_t count,
1366 const layer_state_t* states)
1367{
1368 Mutex::Autolock _l(mStateLock);
1369 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 for (int i=0 ; i<count ; i++) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001371 const layer_state_t& s(states[i]);
1372 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001373 if (layer != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 const uint32_t what = s.what;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375 if (what & ePositionChanged) {
1376 if (layer->setPosition(s.x, s.y))
1377 flags |= eTraversalNeeded;
1378 }
1379 if (what & eLayerChanged) {
1380 if (layer->setLayer(s.z)) {
1381 mCurrentState.layersSortedByZ.reorder(
1382 layer, &Layer::compareCurrentStateZ);
1383 // we need traversal (state changed)
1384 // AND transaction (list changed)
1385 flags |= eTransactionNeeded|eTraversalNeeded;
1386 }
1387 }
1388 if (what & eSizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001389 if (layer->setSize(s.w, s.h)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 flags |= eTraversalNeeded;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001391 mResizeTransationPending = true;
1392 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 }
1394 if (what & eAlphaChanged) {
1395 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1396 flags |= eTraversalNeeded;
1397 }
1398 if (what & eMatrixChanged) {
1399 if (layer->setMatrix(s.matrix))
1400 flags |= eTraversalNeeded;
1401 }
1402 if (what & eTransparentRegionChanged) {
1403 if (layer->setTransparentRegionHint(s.transparentRegion))
1404 flags |= eTraversalNeeded;
1405 }
1406 if (what & eVisibilityChanged) {
1407 if (layer->setFlags(s.flags, s.mask))
1408 flags |= eTraversalNeeded;
1409 }
1410 }
1411 }
1412 if (flags) {
1413 setTransactionFlags(flags);
1414 }
1415 return NO_ERROR;
1416}
1417
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418void SurfaceFlinger::screenReleased(int dpy)
1419{
1420 // this may be called by a signal handler, we can't do too much in here
1421 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1422 signalEvent();
1423}
1424
1425void SurfaceFlinger::screenAcquired(int dpy)
1426{
1427 // this may be called by a signal handler, we can't do too much in here
1428 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1429 signalEvent();
1430}
1431
1432status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1433{
1434 const size_t SIZE = 1024;
1435 char buffer[SIZE];
1436 String8 result;
Mathias Agopian375f5632009-06-15 18:24:59 -07001437 if (!mDump.checkCalling()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 snprintf(buffer, SIZE, "Permission Denial: "
1439 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1440 IPCThreadState::self()->getCallingPid(),
1441 IPCThreadState::self()->getCallingUid());
1442 result.append(buffer);
1443 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001444
1445 // figure out if we're stuck somewhere
1446 const nsecs_t now = systemTime();
1447 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1448 const nsecs_t inTransaction(mDebugInTransaction);
1449 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1450 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1451
1452 // Try to get the main lock, but don't insist if we can't
1453 // (this would indicate SF is stuck, but we want to be able to
1454 // print something in dumpsys).
1455 int retry = 3;
1456 while (mStateLock.tryLock()<0 && --retry>=0) {
1457 usleep(1000000);
1458 }
1459 const bool locked(retry >= 0);
1460 if (!locked) {
1461 snprintf(buffer, SIZE,
1462 "SurfaceFlinger appears to be unresponsive, "
1463 "dumping anyways (no locks held)\n");
1464 result.append(buffer);
1465 }
1466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1468 const size_t count = currentLayers.size();
1469 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001470 const sp<LayerBase>& layer(currentLayers[i]);
1471 layer->dump(result, buffer, SIZE);
1472 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 s.transparentRegion.dump(result, "transparentRegion");
1474 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1475 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1476 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 mWormholeRegion.dump(result, "WormholeRegion");
1479 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1480 snprintf(buffer, SIZE,
1481 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1482 mFreezeDisplay?"yes":"no", mFreezeCount,
1483 mCurrentState.orientation, hw.canDraw());
1484 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001485 snprintf(buffer, SIZE,
1486 " last eglSwapBuffers() time: %f us\n"
1487 " last transaction time : %f us\n",
1488 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1489 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001490
Mathias Agopian9795c422009-08-26 16:36:26 -07001491 if (inSwapBuffersDuration || !locked) {
1492 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1493 inSwapBuffersDuration/1000.0);
1494 result.append(buffer);
1495 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001496
Mathias Agopian9795c422009-08-26 16:36:26 -07001497 if (inTransactionDuration || !locked) {
1498 snprintf(buffer, SIZE, " transaction time: %f us\n",
1499 inTransactionDuration/1000.0);
1500 result.append(buffer);
1501 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001502
Mathias Agopian3330b202009-10-05 17:07:12 -07001503 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001504 alloc.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001505
1506 if (locked) {
1507 mStateLock.unlock();
1508 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 }
1510 write(fd, result.string(), result.size());
1511 return NO_ERROR;
1512}
1513
1514status_t SurfaceFlinger::onTransact(
1515 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1516{
1517 switch (code) {
1518 case CREATE_CONNECTION:
1519 case OPEN_GLOBAL_TRANSACTION:
1520 case CLOSE_GLOBAL_TRANSACTION:
1521 case SET_ORIENTATION:
1522 case FREEZE_DISPLAY:
1523 case UNFREEZE_DISPLAY:
1524 case BOOT_FINISHED:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 {
1526 // codes that require permission check
1527 IPCThreadState* ipc = IPCThreadState::self();
1528 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001529 const int uid = ipc->getCallingUid();
Mathias Agopian375f5632009-06-15 18:24:59 -07001530 if ((uid != AID_GRAPHICS) && !mAccessSurfaceFlinger.check(pid, uid)) {
1531 LOGE("Permission Denial: "
1532 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1533 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534 }
1535 }
1536 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1538 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001539 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian375f5632009-06-15 18:24:59 -07001540 if (UNLIKELY(!mHardwareTest.checkCalling())) {
1541 IPCThreadState* ipc = IPCThreadState::self();
1542 const int pid = ipc->getCallingPid();
1543 const int uid = ipc->getCallingUid();
1544 LOGE("Permission Denial: "
1545 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 return PERMISSION_DENIED;
1547 }
1548 int n;
1549 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001550 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551 return NO_ERROR;
Mathias Agopiancbc93ca2009-04-21 18:28:33 -07001552 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 return NO_ERROR;
1554 case 1002: // SHOW_UPDATES
1555 n = data.readInt32();
1556 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
1557 return NO_ERROR;
1558 case 1003: // SHOW_BACKGROUND
1559 n = data.readInt32();
1560 mDebugBackground = n ? 1 : 0;
1561 return NO_ERROR;
1562 case 1004:{ // repaint everything
1563 Mutex::Autolock _l(mStateLock);
1564 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1565 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1566 signalEvent();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001567 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001569 case 1005:{ // force transaction
1570 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1571 return NO_ERROR;
1572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 case 1007: // set mFreezeCount
1574 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001575 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 return NO_ERROR;
1577 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001578 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579 reply->writeInt32(0);
1580 reply->writeInt32(mDebugRegion);
1581 reply->writeInt32(mDebugBackground);
1582 return NO_ERROR;
1583 case 1013: {
1584 Mutex::Autolock _l(mStateLock);
1585 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1586 reply->writeInt32(hw.getPageFlipCount());
1587 }
1588 return NO_ERROR;
1589 }
1590 }
1591 return err;
1592}
1593
1594// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001595
1596sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
1597{
1598 sp<Layer> result;
1599 Mutex::Autolock _l(mStateLock);
1600 result = mLayerMap.valueFor( sur->asBinder() ).promote();
1601 return result;
1602}
1603
1604// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605
Mathias Agopian96f08192010-06-02 23:28:45 -07001606Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001607 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609}
1610
Mathias Agopian96f08192010-06-02 23:28:45 -07001611Client::~Client()
1612{
Mathias Agopian96f08192010-06-02 23:28:45 -07001613 const size_t count = mLayers.size();
1614 for (size_t i=0 ; i<count ; i++) {
1615 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
1616 if (layer != 0) {
1617 mFlinger->removeLayer(layer);
1618 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 }
1620}
1621
Mathias Agopian96f08192010-06-02 23:28:45 -07001622status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001623 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001625
Mathias Agopian96f08192010-06-02 23:28:45 -07001626ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
1627{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001628 int32_t name = android_atomic_inc(&mNameGenerator);
Mathias Agopian96f08192010-06-02 23:28:45 -07001629 mLayers.add(name, layer);
1630 return name;
1631}
1632
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001633void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07001634{
Mathias Agopian96f08192010-06-02 23:28:45 -07001635 // we do a linear search here, because this doesn't happen often
1636 const size_t count = mLayers.size();
1637 for (size_t i=0 ; i<count ; i++) {
1638 if (mLayers.valueAt(i) == layer) {
1639 mLayers.removeItemsAt(i, 1);
1640 break;
1641 }
1642 }
1643}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001644sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
1645 sp<LayerBaseClient> lbc;
Mathias Agopian96f08192010-06-02 23:28:45 -07001646 const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
1647 if (layer != 0) {
1648 lbc = layer.promote();
1649 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001650 }
1651 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652}
1653
Mathias Agopian96f08192010-06-02 23:28:45 -07001654sp<IMemoryHeap> Client::getControlBlock() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001655 return 0;
1656}
1657ssize_t Client::getTokenForSurface(const sp<ISurface>& sur) const {
1658 return -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659}
Mathias Agopian96f08192010-06-02 23:28:45 -07001660sp<ISurface> Client::createSurface(
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001661 ISurfaceComposerClient::surface_data_t* params, int pid,
1662 const String8& name,
1663 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 uint32_t flags)
1665{
Mathias Agopian96f08192010-06-02 23:28:45 -07001666 return mFlinger->createSurface(this, pid, name, params,
1667 display, w, h, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668}
Mathias Agopian96f08192010-06-02 23:28:45 -07001669status_t Client::destroySurface(SurfaceID sid) {
1670 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671}
Mathias Agopian96f08192010-06-02 23:28:45 -07001672status_t Client::setState(int32_t count, const layer_state_t* states) {
1673 return mFlinger->setClientState(this, count, states);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674}
1675
1676// ---------------------------------------------------------------------------
1677
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001678UserClient::UserClient(const sp<SurfaceFlinger>& flinger)
1679 : ctrlblk(0), mBitmap(0), mFlinger(flinger)
1680{
1681 const int pgsize = getpagesize();
1682 const int cblksize = ((sizeof(SharedClient)+(pgsize-1))&~(pgsize-1));
1683
1684 mCblkHeap = new MemoryHeapBase(cblksize, 0,
1685 "SurfaceFlinger Client control-block");
1686
1687 ctrlblk = static_cast<SharedClient *>(mCblkHeap->getBase());
1688 if (ctrlblk) { // construct the shared structure in-place.
1689 new(ctrlblk) SharedClient;
1690 }
1691}
1692
1693UserClient::~UserClient()
1694{
1695 if (ctrlblk) {
1696 ctrlblk->~SharedClient(); // destroy our shared-structure.
1697 }
1698
1699 /*
1700 * When a UserClient dies, it's unclear what to do exactly.
1701 * We could go ahead and destroy all surfaces linked to that client
1702 * however, it wouldn't be fair to the main Client
1703 * (usually the the window-manager), which might want to re-target
1704 * the layer to another UserClient.
1705 * I think the best is to do nothing, or not much; in most cases the
1706 * WM itself will go ahead and clean things up when it detects a client of
1707 * his has died.
1708 * The remaining question is what to display? currently we keep
1709 * just keep the current buffer.
1710 */
1711}
1712
1713status_t UserClient::initCheck() const {
1714 return ctrlblk == 0 ? NO_INIT : NO_ERROR;
1715}
1716
1717void UserClient::detachLayer(const Layer* layer)
1718{
1719 int32_t name = layer->getToken();
1720 if (name >= 0) {
Mathias Agopian579b3f82010-06-08 19:54:15 -07001721 int32_t mask = 1LU<<name;
1722 if ((android_atomic_and(~mask, &mBitmap) & mask) == 0) {
1723 LOGW("token %d wasn't marked as used %08x", name, int(mBitmap));
1724 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001725 }
1726}
1727
1728sp<IMemoryHeap> UserClient::getControlBlock() const {
1729 return mCblkHeap;
1730}
1731
1732ssize_t UserClient::getTokenForSurface(const sp<ISurface>& sur) const
1733{
1734 int32_t name = NAME_NOT_FOUND;
1735 sp<Layer> layer(mFlinger->getLayer(sur));
1736 if (layer == 0) return name;
1737
Mathias Agopian579b3f82010-06-08 19:54:15 -07001738 // if this layer already has a token, just return it
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001739 name = layer->getToken();
Mathias Agopian579b3f82010-06-08 19:54:15 -07001740 if ((name >= 0) && (layer->getClient() == this))
1741 return name;
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001742
1743 name = 0;
1744 do {
1745 int32_t mask = 1LU<<name;
1746 if ((android_atomic_or(mask, &mBitmap) & mask) == 0) {
1747 // we found and locked that name
Mathias Agopian579b3f82010-06-08 19:54:15 -07001748 status_t err = layer->setToken(
1749 const_cast<UserClient*>(this), ctrlblk, name);
1750 if (err != NO_ERROR) {
1751 // free the name
1752 android_atomic_and(~mask, &mBitmap);
1753 name = err;
1754 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001755 break;
1756 }
1757 if (++name > 31)
1758 name = NO_MEMORY;
1759 } while(name >= 0);
1760
Mathias Agopian53503a92010-06-08 15:40:56 -07001761 //LOGD("getTokenForSurface(%p) => %d (client=%p, bitmap=%08lx)",
1762 // sur->asBinder().get(), name, this, mBitmap);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001763 return name;
1764}
1765
1766sp<ISurface> UserClient::createSurface(
1767 ISurfaceComposerClient::surface_data_t* params, int pid,
1768 const String8& name,
1769 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
1770 uint32_t flags) {
1771 return 0;
1772}
1773status_t UserClient::destroySurface(SurfaceID sid) {
1774 return INVALID_OPERATION;
1775}
1776status_t UserClient::setState(int32_t count, const layer_state_t* states) {
1777 return INVALID_OPERATION;
1778}
1779
1780// ---------------------------------------------------------------------------
1781
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782GraphicPlane::GraphicPlane()
1783 : mHw(0)
1784{
1785}
1786
1787GraphicPlane::~GraphicPlane() {
1788 delete mHw;
1789}
1790
1791bool GraphicPlane::initialized() const {
1792 return mHw ? true : false;
1793}
1794
Mathias Agopian2b92d892010-02-08 15:49:35 -08001795int GraphicPlane::getWidth() const {
1796 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797}
1798
Mathias Agopian2b92d892010-02-08 15:49:35 -08001799int GraphicPlane::getHeight() const {
1800 return mHeight;
1801}
1802
1803void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
1804{
1805 mHw = hw;
1806
1807 // initialize the display orientation transform.
1808 // it's a constant that should come from the display driver.
1809 int displayOrientation = ISurfaceComposer::eOrientationDefault;
1810 char property[PROPERTY_VALUE_MAX];
1811 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
1812 //displayOrientation
1813 switch (atoi(property)) {
1814 case 90:
1815 displayOrientation = ISurfaceComposer::eOrientation90;
1816 break;
1817 case 270:
1818 displayOrientation = ISurfaceComposer::eOrientation270;
1819 break;
1820 }
1821 }
1822
1823 const float w = hw->getWidth();
1824 const float h = hw->getHeight();
1825 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
1826 &mDisplayTransform);
1827 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
1828 mDisplayWidth = h;
1829 mDisplayHeight = w;
1830 } else {
1831 mDisplayWidth = w;
1832 mDisplayHeight = h;
1833 }
1834
1835 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836}
1837
1838status_t GraphicPlane::orientationToTransfrom(
1839 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08001840{
1841 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842 switch (orientation) {
1843 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08001844 flags = Transform::ROT_0;
1845 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08001847 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 break;
1849 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08001850 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 break;
1852 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08001853 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854 break;
1855 default:
1856 return BAD_VALUE;
1857 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001858 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001859 return NO_ERROR;
1860}
1861
1862status_t GraphicPlane::setOrientation(int orientation)
1863{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001864 // If the rotation can be handled in hardware, this is where
1865 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08001866
1867 const DisplayHardware& hw(displayHardware());
1868 const float w = mDisplayWidth;
1869 const float h = mDisplayHeight;
1870 mWidth = int(w);
1871 mHeight = int(h);
1872
1873 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08001874 GraphicPlane::orientationToTransfrom(orientation, w, h,
1875 &orientationTransform);
1876 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
1877 mWidth = int(h);
1878 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879 }
Mathias Agopianeda65402010-02-22 03:15:57 -08001880
Mathias Agopian0d1318b2009-03-27 17:58:20 -07001881 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08001882 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 return NO_ERROR;
1884}
1885
1886const DisplayHardware& GraphicPlane::displayHardware() const {
1887 return *mHw;
1888}
1889
1890const Transform& GraphicPlane::transform() const {
1891 return mGlobalTransform;
1892}
1893
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001894EGLDisplay GraphicPlane::getEGLDisplay() const {
1895 return mHw->getEGLDisplay();
1896}
1897
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001898// ---------------------------------------------------------------------------
1899
1900}; // namespace android