blob: c789941dfe57b7e86b0b304ce7bb9c51a1a429a4 [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
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070040#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080043#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044
45#include <ui/GraphicBufferAllocator.h>
46#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070047#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Mathias Agopiancde87a32012-09-13 14:09:01 -070049#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include <utils/String8.h>
51#include <utils/String16.h>
52#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian921e6ac2012-07-23 23:11:29 -070055#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070056#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080061#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070062#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080063#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070064#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073#define DISPLAY_COUNT 1
74
Mathias Agopianfee2b462013-07-03 12:34:01 -070075/*
76 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
77 * black pixels.
78 */
79#define DEBUG_SCREENSHOTS false
80
Mathias Agopianca088332013-03-28 17:44:13 -070081EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084// ---------------------------------------------------------------------------
85
Mathias Agopian99b49842011-06-27 16:05:52 -070086const String16 sHardwareTest("android.permission.HARDWARE_TEST");
87const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
88const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
89const String16 sDump("android.permission.DUMP");
90
91// ---------------------------------------------------------------------------
92
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093SurfaceFlinger::SurfaceFlinger()
94 : BnSurfaceComposer(), Thread(false),
95 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070096 mTransactionPending(false),
97 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070099 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700102 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800103 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700105 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700106 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700107 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700108 mDebugInSwapBuffers(0),
109 mLastSwapBufferTime(0),
110 mDebugInTransaction(0),
111 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700112 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113{
Steve Blocka19954a2012-01-04 20:05:49 +0000114 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115
116 // debugging stuff...
117 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118
Mathias Agopianb4b17302013-03-20 18:36:41 -0700119 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700120 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 property_get("debug.sf.showupdates", value, "0");
123 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700124
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700125 property_get("debug.sf.ddms", value, "0");
126 mDebugDDMS = atoi(value);
127 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700128 if (!startDdmConnection()) {
129 // start failed, and DDMS debugging not enabled
130 mDebugDDMS = 0;
131 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700132 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700133 ALOGI_IF(mDebugRegion, "showupdates enabled");
134 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800137void SurfaceFlinger::onFirstRef()
138{
139 mEventQueue.init(this);
140
141 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
142
143 // Wait for the main thread to be done with its initialization
144 mReadyToRunBarrier.wait();
145}
146
147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148SurfaceFlinger::~SurfaceFlinger()
149{
Mathias Agopiana4912602012-07-12 14:25:33 -0700150 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
151 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
152 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153}
154
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800155void SurfaceFlinger::binderDied(const wp<IBinder>& who)
156{
157 // the window manager died on us. prepare its eulogy.
158
Andy McFadden13a082e2012-08-24 10:16:42 -0700159 // restore initial conditions (default device unblank, etc)
160 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800161
162 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700163 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800164}
165
Mathias Agopian7e27f052010-05-28 14:22:23 -0700166sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167{
Mathias Agopian96f08192010-06-02 23:28:45 -0700168 sp<ISurfaceComposerClient> bclient;
169 sp<Client> client(new Client(this));
170 status_t err = client->initCheck();
171 if (err == NO_ERROR) {
172 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 return bclient;
175}
176
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700177sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
178 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700179{
180 class DisplayToken : public BBinder {
181 sp<SurfaceFlinger> flinger;
182 virtual ~DisplayToken() {
183 // no more references, this display must be terminated
184 Mutex::Autolock _l(flinger->mStateLock);
185 flinger->mCurrentState.displays.removeItem(this);
186 flinger->setTransactionFlags(eDisplayTransactionNeeded);
187 }
188 public:
189 DisplayToken(const sp<SurfaceFlinger>& flinger)
190 : flinger(flinger) {
191 }
192 };
193
194 sp<BBinder> token = new DisplayToken(this);
195
196 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700197 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700198 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700199 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700200 mCurrentState.displays.add(token, info);
201
202 return token;
203}
204
Jesse Hall692c7232012-11-08 15:41:56 -0800205void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
206 ALOGW_IF(mBuiltinDisplays[type],
207 "Overwriting display token for display type %d", type);
208 mBuiltinDisplays[type] = new BBinder();
209 DisplayDeviceState info(type);
210 // All non-virtual displays are currently considered secure.
211 info.isSecure = true;
212 mCurrentState.displays.add(mBuiltinDisplays[type], info);
213}
214
Mathias Agopiane57f2922012-08-09 16:29:12 -0700215sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700216 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700217 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
218 return NULL;
219 }
Jesse Hall692c7232012-11-08 15:41:56 -0800220 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700221}
222
Jamie Gennis9a78c902011-01-12 18:30:40 -0800223sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
224{
225 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
226 return gba;
227}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229void SurfaceFlinger::bootFinished()
230{
231 const nsecs_t now = systemTime();
232 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000233 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700234 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700235
236 // wait patiently for the window manager death
237 const String16 name("window");
238 sp<IBinder> window(defaultServiceManager()->getService(name));
239 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700240 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700241 }
242
243 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700244 // formerly we would just kill the process, but we now ask it to exit so it
245 // can choose where to stop the animation.
246 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247}
248
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700249void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
250 class MessageDestroyGLTexture : public MessageBase {
251 GLuint texture;
252 public:
253 MessageDestroyGLTexture(GLuint texture)
254 : texture(texture) {
255 }
256 virtual bool handler() {
257 glDeleteTextures(1, &texture);
258 return true;
259 }
260 };
261 postMessageAsync(new MessageDestroyGLTexture(texture));
262}
263
Mathias Agopian722b98f2012-09-25 18:24:31 -0700264status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700265 EGLDisplay dpy,
266 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700267 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700268 EGLConfig* outConfig)
269{
270 EGLConfig config = NULL;
271 EGLint numConfigs = -1, n=0;
272 eglGetConfigs(dpy, NULL, 0, &numConfigs);
273 EGLConfig* const configs = new EGLConfig[numConfigs];
274 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700275
Mathias Agopian722b98f2012-09-25 18:24:31 -0700276 if (n) {
277 if (attribute != EGL_NONE) {
278 for (int i=0 ; i<n ; i++) {
279 EGLint value = 0;
280 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
281 if (wanted == value) {
282 *outConfig = configs[i];
283 delete [] configs;
284 return NO_ERROR;
285 }
286 }
287 } else {
288 // just pick the first one
289 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 delete [] configs;
291 return NO_ERROR;
292 }
293 }
294 delete [] configs;
295 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296}
297
Mathias Agopian722b98f2012-09-25 18:24:31 -0700298class EGLAttributeVector {
299 struct Attribute;
300 class Adder;
301 friend class Adder;
302 KeyedVector<Attribute, EGLint> mList;
303 struct Attribute {
304 Attribute() {};
305 Attribute(EGLint v) : v(v) { }
306 EGLint v;
307 bool operator < (const Attribute& other) const {
308 // this places EGL_NONE at the end
309 EGLint lhs(v);
310 EGLint rhs(other.v);
311 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
312 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
313 return lhs < rhs;
314 }
315 };
316 class Adder {
317 friend class EGLAttributeVector;
318 EGLAttributeVector& v;
319 EGLint attribute;
320 Adder(EGLAttributeVector& v, EGLint attribute)
321 : v(v), attribute(attribute) {
322 }
323 public:
324 void operator = (EGLint value) {
325 if (attribute != EGL_NONE) {
326 v.mList.add(attribute, value);
327 }
328 }
329 operator EGLint () const { return v.mList[attribute]; }
330 };
331public:
332 EGLAttributeVector() {
333 mList.add(EGL_NONE, EGL_NONE);
334 }
335 void remove(EGLint attribute) {
336 if (attribute != EGL_NONE) {
337 mList.removeItem(attribute);
338 }
339 }
340 Adder operator [] (EGLint attribute) {
341 return Adder(*this, attribute);
342 }
343 EGLint operator [] (EGLint attribute) const {
344 return mList[attribute];
345 }
346 // cast-operator to (EGLint const*)
347 operator EGLint const* () const { return &mList.keyAt(0).v; }
348};
349
Mathias Agopiana4912602012-07-12 14:25:33 -0700350EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
351 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
352 // it is to be used with WIFI displays
353 EGLConfig config;
354 EGLint dummy;
355 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700356
Mathias Agopian722b98f2012-09-25 18:24:31 -0700357 EGLAttributeVector attribs;
358 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
359 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
360 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
361 attribs[EGL_RED_SIZE] = 8;
362 attribs[EGL_GREEN_SIZE] = 8;
363 attribs[EGL_BLUE_SIZE] = 8;
364
365 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
366 if (!err)
367 goto success;
368
369 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
370 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
371 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
372 err = selectConfigForAttribute(display, attribs,
373 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700374 if (!err)
375 goto success;
376
377 // maybe we failed because of EGL_RECORDABLE_ANDROID
378 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700379 attribs.remove(EGL_RECORDABLE_ANDROID);
380 err = selectConfigForAttribute(display, attribs,
381 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700382 if (!err)
383 goto success;
384
385 // allow less than 24-bit color; the non-gpu-accelerated emulator only
386 // supports 16-bit color
387 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700388 attribs.remove(EGL_RED_SIZE);
389 attribs.remove(EGL_GREEN_SIZE);
390 attribs.remove(EGL_BLUE_SIZE);
391 err = selectConfigForAttribute(display, attribs,
392 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700393 if (!err)
394 goto success;
395
396 // this EGL is too lame for Android
397 ALOGE("no suitable EGLConfig found, giving up");
398
399 return 0;
400
401success:
402 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700403 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700404 return config;
405}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406
Mathias Agopiana4912602012-07-12 14:25:33 -0700407EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
408 // Also create our EGLContext
409 EGLint contextAttributes[] = {
410#ifdef EGL_IMG_context_priority
411#ifdef HAS_CONTEXT_PRIORITY
412#warning "using EGL_IMG_context_priority"
413 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
414#endif
415#endif
416 EGL_NONE, EGL_NONE
417 };
418 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
419 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
420 return ctxt;
421}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422
Jesse Hall29c3f352013-05-21 15:36:55 -0700423static GlesVersion parseGlesVersion(const char* str) {
424 int major, minor;
425 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
426 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
427 return GLES_VERSION_1_0;
428 }
429
430 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
431 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
432 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
433 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
434
435 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
436 return GLES_VERSION_1_0;
437}
438
Mathias Agopiancde87a32012-09-13 14:09:01 -0700439void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700440 GLExtensions& extensions(GLExtensions::getInstance());
441 extensions.initWithGLStrings(
442 glGetString(GL_VENDOR),
443 glGetString(GL_RENDERER),
444 glGetString(GL_VERSION),
445 glGetString(GL_EXTENSIONS),
446 eglQueryString(display, EGL_VENDOR),
447 eglQueryString(display, EGL_VERSION),
448 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700449
Jesse Hall29c3f352013-05-21 15:36:55 -0700450 mGlesVersion = parseGlesVersion(extensions.getVersion());
451
Mathias Agopiana4912602012-07-12 14:25:33 -0700452 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
453 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700456 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458 glShadeModel(GL_FLAT);
459 glDisable(GL_DITHER);
460 glDisable(GL_CULL_FACE);
461
Mathias Agopiana4912602012-07-12 14:25:33 -0700462 struct pack565 {
463 inline uint16_t operator() (int r, int g, int b) const {
464 return (r<<11)|(g<<5)|b;
465 }
466 } pack565;
467
Jamie Gennis9575f602011-10-07 14:51:16 -0700468 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
469 glGenTextures(1, &mProtectedTexName);
470 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
471 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
472 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
473 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
474 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
475 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
476 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477
Mathias Agopiana4912602012-07-12 14:25:33 -0700478 // print some debugging info
479 EGLint r,g,b,a;
480 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
481 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
482 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
483 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
484 ALOGI("EGL informations:");
485 ALOGI("vendor : %s", extensions.getEglVendor());
486 ALOGI("version : %s", extensions.getEglVersion());
487 ALOGI("extensions: %s", extensions.getEglExtension());
488 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
489 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
490 ALOGI("OpenGL ES informations:");
491 ALOGI("vendor : %s", extensions.getVendor());
492 ALOGI("renderer : %s", extensions.getRenderer());
493 ALOGI("version : %s", extensions.getVersion());
494 ALOGI("extensions: %s", extensions.getExtension());
495 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
496 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
497}
498
Mathias Agopiana4912602012-07-12 14:25:33 -0700499status_t SurfaceFlinger::readyToRun()
500{
501 ALOGI( "SurfaceFlinger's main thread ready to run. "
502 "Initializing graphics H/W...");
503
Jesse Hall692c7232012-11-08 15:41:56 -0800504 Mutex::Autolock _l(mStateLock);
505
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700506 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700507 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
508 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700509
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700510 // Initialize the H/W composer object. There may or may not be an
511 // actual hardware composer underneath.
512 mHwc = new HWComposer(this,
513 *static_cast<HWComposer::EventHandler *>(this));
514
Mathias Agopiana4912602012-07-12 14:25:33 -0700515 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700516 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700517 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
518 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700519
Mathias Agopianed985572013-03-22 00:24:39 -0700520 // figure out which format we got
521 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
522 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
523
Mathias Agopianda27af92012-09-13 18:17:13 -0700524 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
525 "couldn't create EGLContext");
526
Mathias Agopiancde87a32012-09-13 14:09:01 -0700527 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700528 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700529 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700530 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700531 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700532 // All non-virtual displays are currently considered secure.
533 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800534 createBuiltinDisplayLocked(type);
535 wp<IBinder> token = mBuiltinDisplays[i];
536
Mathias Agopianf5a33922012-09-19 18:16:22 -0700537 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700538 type, allocateHwcDisplayId(type), isSecure, token,
539 new FramebufferSurface(*mHwc, i),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700540 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700541 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700542 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700543 // for displays other than the main display, so we always
544 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700545 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700546 hw->acquireScreen();
547 }
548 mDisplays.add(token, hw);
549 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700550 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700551
Mathias Agopianf5a33922012-09-19 18:16:22 -0700552 // we need a GL context current in a few places, when initializing
553 // OpenGL ES (see below), or creating a layer,
554 // or when a texture is (asynchronously) destroyed, and for that
555 // we need a valid surface, so it's convenient to use the main display
556 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700557 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700558
Mathias Agopiana4912602012-07-12 14:25:33 -0700559 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700560 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
561 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800562
Mathias Agopian028508c2012-07-25 21:12:12 -0700563 // start the EventThread
564 mEventThread = new EventThread(this);
565 mEventQueue.setEventThread(mEventThread);
566
Mathias Agopian92a979a2012-08-02 18:32:23 -0700567 // initialize our drawing state
568 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700569
Mathias Agopiancde87a32012-09-13 14:09:01 -0700570
Mathias Agopiana4912602012-07-12 14:25:33 -0700571 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800572 mReadyToRunBarrier.open();
573
Andy McFadden13a082e2012-08-24 10:16:42 -0700574 // set initial conditions (e.g. unblank default device)
575 initializeDisplays();
576
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700577 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700578 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 return NO_ERROR;
581}
582
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700583int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
584 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
585 type : mHwc->allocateDisplayId();
586}
587
Mathias Agopiana67e4182012-06-19 17:26:12 -0700588void SurfaceFlinger::startBootAnim() {
589 // start boot animation
590 property_set("service.bootanim.exit", "0");
591 property_set("ctl.start", "bootanim");
592}
593
Mathias Agopiana4912602012-07-12 14:25:33 -0700594uint32_t SurfaceFlinger::getMaxTextureSize() const {
595 return mMaxTextureSize;
596}
597
598uint32_t SurfaceFlinger::getMaxViewportDims() const {
599 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
600 mMaxViewportDims[0] : mMaxViewportDims[1];
601}
602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800604
Jamie Gennis582270d2011-08-17 18:19:00 -0700605bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800606 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800607 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800608 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700609 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800610}
611
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700612status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800613 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700614 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800615 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700616 type = i;
617 break;
618 }
619 }
620
621 if (type < 0) {
622 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700623 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700624
625 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700626 float xdpi = hwc.getDpiX(type);
627 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700628
629 // TODO: Not sure if display density should handled by SF any longer
630 class Density {
631 static int getDensityFromProperty(char const* propName) {
632 char property[PROPERTY_VALUE_MAX];
633 int density = 0;
634 if (property_get(propName, property, NULL) > 0) {
635 density = atoi(property);
636 }
637 return density;
638 }
639 public:
640 static int getEmuDensity() {
641 return getDensityFromProperty("qemu.sf.lcd_density"); }
642 static int getBuildDensity() {
643 return getDensityFromProperty("ro.sf.lcd_density"); }
644 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700645
646 if (type == DisplayDevice::DISPLAY_PRIMARY) {
647 // The density of the device is provided by a build property
648 float density = Density::getBuildDensity() / 160.0f;
649 if (density == 0) {
650 // the build doesn't provide a density -- this is wrong!
651 // use xdpi instead
652 ALOGE("ro.sf.lcd_density must be defined as a build property");
653 density = xdpi / 160.0f;
654 }
655 if (Density::getEmuDensity()) {
656 // if "qemu.sf.lcd_density" is specified, it overrides everything
657 xdpi = ydpi = density = Density::getEmuDensity();
658 density /= 160.0f;
659 }
660 info->density = density;
661
662 // TODO: this needs to go away (currently needed only by webkit)
663 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
664 info->orientation = hw->getOrientation();
665 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
666 } else {
667 // TODO: where should this value come from?
668 static const int TV_DENSITY = 213;
669 info->density = TV_DENSITY / 160.0f;
670 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700671 }
672
Mathias Agopian1604f772012-09-18 21:54:42 -0700673 info->w = hwc.getWidth(type);
674 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700675 info->xdpi = xdpi;
676 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700677 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700678
679 // All non-virtual displays are currently considered secure.
680 info->secure = true;
681
Mathias Agopian888c8222012-08-04 21:10:38 -0700682 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700683}
684
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800685// ----------------------------------------------------------------------------
686
687sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800688 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700689}
690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800692
693void SurfaceFlinger::waitForEvent() {
694 mEventQueue.waitMessage();
695}
696
697void SurfaceFlinger::signalTransaction() {
698 mEventQueue.invalidate();
699}
700
701void SurfaceFlinger::signalLayerUpdate() {
702 mEventQueue.invalidate();
703}
704
705void SurfaceFlinger::signalRefresh() {
706 mEventQueue.refresh();
707}
708
709status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
710 nsecs_t reltime, uint32_t flags) {
711 return mEventQueue.postMessage(msg, reltime);
712}
713
714status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
715 nsecs_t reltime, uint32_t flags) {
716 status_t res = mEventQueue.postMessage(msg, reltime);
717 if (res == NO_ERROR) {
718 msg->wait();
719 }
720 return res;
721}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722
Mathias Agopian4fec8732012-06-29 14:12:52 -0700723bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 return true;
726}
727
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700728void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700729 if (mEventThread == NULL) {
730 // This is a temporary workaround for b/7145521. A non-null pointer
731 // does not mean EventThread has finished initializing, so this
732 // is not a correct fix.
733 ALOGW("WARNING: EventThread not started, ignoring vsync");
734 return;
735 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700736 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
737 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700738 mEventThread->onVSyncReceived(type, timestamp);
739 }
740}
741
742void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
743 if (mEventThread == NULL) {
744 // This is a temporary workaround for b/7145521. A non-null pointer
745 // does not mean EventThread has finished initializing, so this
746 // is not a correct fix.
747 ALOGW("WARNING: EventThread not started, ignoring hotplug");
748 return;
749 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700750
Mathias Agopian148994e2012-09-19 17:31:36 -0700751 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700752 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800753 if (connected) {
754 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700755 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800756 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
757 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700758 }
759 setTransactionFlags(eDisplayTransactionNeeded);
760
Andy McFadden9e9689c2012-10-10 18:17:51 -0700761 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700762 }
Mathias Agopian86303202012-07-24 22:46:10 -0700763}
764
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700765void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
766 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700767}
768
Mathias Agopian4fec8732012-06-29 14:12:52 -0700769void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800770 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800771 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700772 case MessageQueue::TRANSACTION:
773 handleMessageTransaction();
774 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700775 case MessageQueue::INVALIDATE:
776 handleMessageTransaction();
777 handleMessageInvalidate();
778 signalRefresh();
779 break;
780 case MessageQueue::REFRESH:
781 handleMessageRefresh();
782 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800783 }
784}
785
Mathias Agopian4fec8732012-06-29 14:12:52 -0700786void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700787 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700788 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700789 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700790 }
791}
792
793void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700794 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700795 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700796}
797
798void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700799 ATRACE_CALL();
800 preComposition();
801 rebuildLayerStacks();
802 setUpHWComposer();
803 doDebugFlashRegions();
804 doComposition();
805 postComposition();
806}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700807
Mathias Agopiancd60f992012-08-16 16:28:27 -0700808void SurfaceFlinger::doDebugFlashRegions()
809{
810 // is debugging enabled
811 if (CC_LIKELY(!mDebugRegion))
812 return;
813
814 const bool repaintEverything = mRepaintEverything;
815 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
816 const sp<DisplayDevice>& hw(mDisplays[dpy]);
817 if (hw->canDraw()) {
818 // transform the dirty region into this screen's coordinate space
819 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
820 if (!dirtyRegion.isEmpty()) {
821 // redraw the whole screen
822 doComposeSurfaces(hw, Region(hw->bounds()));
823
824 // and draw the dirty region
825 glDisable(GL_TEXTURE_EXTERNAL_OES);
826 glDisable(GL_TEXTURE_2D);
827 glDisable(GL_BLEND);
828 glColor4f(1, 0, 1, 1);
829 const int32_t height = hw->getHeight();
830 Region::const_iterator it = dirtyRegion.begin();
831 Region::const_iterator const end = dirtyRegion.end();
832 while (it != end) {
833 const Rect& r = *it++;
834 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800835 { (GLfloat) r.left, (GLfloat) (height - r.top) },
836 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
837 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
838 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700839 };
840 glVertexPointer(2, GL_FLOAT, 0, vertices);
841 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
842 }
843 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700844 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845 }
846 }
847 }
848
849 postFramebuffer();
850
851 if (mDebugRegion > 1) {
852 usleep(mDebugRegion * 1000);
853 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700854
855 HWComposer& hwc(getHwComposer());
856 if (hwc.initCheck() == NO_ERROR) {
857 status_t err = hwc.prepare();
858 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
859 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700860}
861
862void SurfaceFlinger::preComposition()
863{
864 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700865 const LayerVector& layers(mDrawingState.layersSortedByZ);
866 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700867 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700868 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700869 needExtraInvalidate = true;
870 }
871 }
872 if (needExtraInvalidate) {
873 signalLayerUpdate();
874 }
875}
876
877void SurfaceFlinger::postComposition()
878{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700879 const LayerVector& layers(mDrawingState.layersSortedByZ);
880 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700882 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800884
885 if (mAnimCompositionPending) {
886 mAnimCompositionPending = false;
887
888 const HWComposer& hwc = getHwComposer();
889 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800890 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800891 mAnimFrameTracker.setActualPresentFence(presentFence);
892 } else {
893 // The HWC doesn't support present fences, so use the refresh
894 // timestamp instead.
895 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
896 mAnimFrameTracker.setActualPresentTime(presentTime);
897 }
898 mAnimFrameTracker.advanceFrame();
899 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700900}
901
902void SurfaceFlinger::rebuildLayerStacks() {
903 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700904 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700906 mVisibleRegionsDirty = false;
907 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700908
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700909 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700910 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700911 Region opaqueRegion;
912 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800913 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700914 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700915 const Transform& tr(hw->getTransform());
916 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700917 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700918 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700919 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700920
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700921 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700922 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700923 const sp<Layer>& layer(layers[i]);
924 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700925 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700926 Region drawRegion(tr.transform(
927 layer->visibleNonTransparentRegion));
928 drawRegion.andSelf(bounds);
929 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700930 layersSortedByZ.add(layer);
931 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700932 }
933 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700934 }
Mathias Agopian42977342012-08-05 00:40:46 -0700935 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700936 hw->undefinedRegion.set(bounds);
937 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
938 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700939 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700940 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700941}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700942
Mathias Agopiancd60f992012-08-16 16:28:27 -0700943void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700944 HWComposer& hwc(getHwComposer());
945 if (hwc.initCheck() == NO_ERROR) {
946 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700947 if (CC_UNLIKELY(mHwWorkListDirty)) {
948 mHwWorkListDirty = false;
949 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
950 sp<const DisplayDevice> hw(mDisplays[dpy]);
951 const int32_t id = hw->getHwcDisplayId();
952 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800953 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700954 hw->getVisibleLayersSortedByZ());
955 const size_t count = currentLayers.size();
956 if (hwc.createWorkList(id, count) == NO_ERROR) {
957 HWComposer::LayerListIterator cur = hwc.begin(id);
958 const HWComposer::LayerListIterator end = hwc.end(id);
959 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800960 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700961 layer->setGeometry(hw, *cur);
962 if (mDebugDisableHWC || mDebugRegion) {
963 cur->setSkip(true);
964 }
965 }
966 }
967 }
968 }
969 }
970
971 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700972 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700973 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700974 const int32_t id = hw->getHwcDisplayId();
975 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800976 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700977 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700978 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700979 HWComposer::LayerListIterator cur = hwc.begin(id);
980 const HWComposer::LayerListIterator end = hwc.end(id);
981 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
982 /*
983 * update the per-frame h/w composer data for each layer
984 * and build the transparent region of the FB
985 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800986 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700987 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700988 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700989 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700990 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700991
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700992 status_t err = hwc.prepare();
993 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
994 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700995}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700996
Mathias Agopiancd60f992012-08-16 16:28:27 -0700997void SurfaceFlinger::doComposition() {
998 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700999 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001000 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001001 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -07001002 if (hw->canDraw()) {
1003 // transform the dirty region into this screen's coordinate space
1004 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001005
1006 // repaint the framebuffer (if needed)
1007 doDisplayComposition(hw, dirtyRegion);
1008
Mathias Agopiancd60f992012-08-16 16:28:27 -07001009 hw->dirtyRegion.clear();
1010 hw->flip(hw->swapRegion);
1011 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001012 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001013 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001014 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001015 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001016 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001017}
1018
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019void SurfaceFlinger::postFramebuffer()
1020{
Mathias Agopian841cde52012-03-01 15:44:37 -08001021 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001022
Mathias Agopiana44b0412011-10-16 18:46:35 -07001023 const nsecs_t now = systemTime();
1024 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001025
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001026 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001027 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001028 if (!hwc.supportsFramebufferTarget()) {
1029 // EGL spec says:
1030 // "surface must be bound to the calling thread's current context,
1031 // for the current rendering API."
1032 DisplayDevice::makeCurrent(mEGLDisplay,
1033 getDefaultDisplayDevice(), mEGLContext);
1034 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001035 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001036 }
1037
Mathias Agopian92a979a2012-08-02 18:32:23 -07001038 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001039 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001041 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001042 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001043 int32_t id = hw->getHwcDisplayId();
1044 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001045 HWComposer::LayerListIterator cur = hwc.begin(id);
1046 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001047 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001048 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001049 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001050 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001051 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001052 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001053 }
Jesse Hallef194142012-06-14 14:45:17 -07001054 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001055 }
1056
Mathias Agopiana44b0412011-10-16 18:46:35 -07001057 mLastSwapBufferTime = systemTime() - now;
1058 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059}
1060
Mathias Agopian87baae12012-07-31 12:38:26 -07001061void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062{
Mathias Agopian841cde52012-03-01 15:44:37 -08001063 ATRACE_CALL();
1064
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001065 // here we keep a copy of the drawing state (that is the state that's
1066 // going to be overwritten by handleTransactionLocked()) outside of
1067 // mStateLock so that the side-effects of the State assignment
1068 // don't happen with mStateLock held (which can cause deadlocks).
1069 State drawingState(mDrawingState);
1070
Mathias Agopianca4d3602011-05-19 15:38:14 -07001071 Mutex::Autolock _l(mStateLock);
1072 const nsecs_t now = systemTime();
1073 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074
Mathias Agopianca4d3602011-05-19 15:38:14 -07001075 // Here we're guaranteed that some transaction flags are set
1076 // so we can call handleTransactionLocked() unconditionally.
1077 // We call getTransactionFlags(), which will also clear the flags,
1078 // with mStateLock held to guarantee that mCurrentState won't change
1079 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001080
Mathias Agopiane57f2922012-08-09 16:29:12 -07001081 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001082 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001083
Mathias Agopianca4d3602011-05-19 15:38:14 -07001084 mLastTransactionTime = systemTime() - now;
1085 mDebugInTransaction = 0;
1086 invalidateHwcGeometry();
1087 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001088}
1089
Mathias Agopian87baae12012-07-31 12:38:26 -07001090void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001091{
1092 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 const size_t count = currentLayers.size();
1094
1095 /*
1096 * Traversal of the children
1097 * (perform the transaction for each of them if needed)
1098 */
1099
Mathias Agopian3559b072012-08-15 13:46:03 -07001100 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001102 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1104 if (!trFlags) continue;
1105
1106 const uint32_t flags = layer->doTransaction(0);
1107 if (flags & Layer::eVisibleRegion)
1108 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 }
1110 }
1111
1112 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001113 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 */
1115
Mathias Agopiane57f2922012-08-09 16:29:12 -07001116 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001117 // here we take advantage of Vector's copy-on-write semantics to
1118 // improve performance by skipping the transaction entirely when
1119 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001120 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1121 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001122 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001124 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001125 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001126
1127 // find the displays that were removed
1128 // (ie: in drawing state but not in current state)
1129 // also handle displays that changed
1130 // (ie: displays that are in both lists)
1131 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001132 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1133 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001134 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001135 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001136 // Call makeCurrent() on the primary display so we can
1137 // be sure that nothing associated with this display
1138 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001139 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
1140 DisplayDevice::makeCurrent(mEGLDisplay, defaultDisplay, mEGLContext);
1141 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1142 if (hw != NULL)
1143 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001144 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1145 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001146 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001147 } else {
1148 ALOGW("trying to remove the main display");
1149 }
1150 } else {
1151 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001152 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001153 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001154 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001155 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001156 // recreating the DisplayDevice, so we just remove it
1157 // from the drawing state, so that it get re-added
1158 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001159 sp<DisplayDevice> hw(getDisplayDevice(display));
1160 if (hw != NULL)
1161 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001162 mDisplays.removeItem(display);
1163 mDrawingState.displays.removeItemsAt(i);
1164 dc--; i--;
1165 // at this point we must loop to the next item
1166 continue;
1167 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001168
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001169 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001170 if (disp != NULL) {
1171 if (state.layerStack != draw[i].layerStack) {
1172 disp->setLayerStack(state.layerStack);
1173 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001174 if ((state.orientation != draw[i].orientation)
1175 || (state.viewport != draw[i].viewport)
1176 || (state.frame != draw[i].frame))
1177 {
1178 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001179 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001180 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001181 }
1182 }
1183 }
1184
1185 // find displays that were added
1186 // (ie: in current state but not in drawing state)
1187 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001188 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001189 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001190
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001191 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001192 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001193 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001194 // Virtual displays without a surface are dormant:
1195 // they have external state (layer stack, projection,
1196 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001197 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001198 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001199 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001200 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001201 state.displayName);
1202 }
1203 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001204 ALOGE_IF(state.surface!=NULL,
1205 "adding a supported display, but rendering "
1206 "surface is provided (%p), ignoring it",
1207 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001208 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001209 // for supported (by hwc) displays we provide our
1210 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001211 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001212 }
1213
1214 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001215 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001216 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001217 state.type, hwcDisplayId, state.isSecure,
1218 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001219 hw->setLayerStack(state.layerStack);
1220 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001221 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001222 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001223 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001224 if (state.isVirtualDisplay()) {
1225 if (hwcDisplayId >= 0) {
1226 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1227 hw->getWidth(), hw->getHeight(),
1228 hw->getFormat());
1229 }
1230 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001231 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001232 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001233 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001234 }
1235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238
Mathias Agopian84300952012-11-21 16:02:13 -08001239 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1240 // The transform hint might have changed for some layers
1241 // (either because a display has changed, or because a layer
1242 // as changed).
1243 //
1244 // Walk through all the layers in currentLayers,
1245 // and update their transform hint.
1246 //
1247 // If a layer is visible only on a single display, then that
1248 // display is used to calculate the hint, otherwise we use the
1249 // default display.
1250 //
1251 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1252 // the hint is set before we acquire a buffer from the surface texture.
1253 //
1254 // NOTE: layer transactions have taken place already, so we use their
1255 // drawing state. However, SurfaceFlinger's own transaction has not
1256 // happened yet, so we must use the current state layer list
1257 // (soon to become the drawing state list).
1258 //
1259 sp<const DisplayDevice> disp;
1260 uint32_t currentlayerStack = 0;
1261 for (size_t i=0; i<count; i++) {
1262 // NOTE: we rely on the fact that layers are sorted by
1263 // layerStack first (so we don't have to traverse the list
1264 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001266 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001267 if (i==0 || currentlayerStack != layerStack) {
1268 currentlayerStack = layerStack;
1269 // figure out if this layerstack is mirrored
1270 // (more than one display) if so, pick the default display,
1271 // if not, pick the only display it's on.
1272 disp.clear();
1273 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1274 sp<const DisplayDevice> hw(mDisplays[dpy]);
1275 if (hw->getLayerStack() == currentlayerStack) {
1276 if (disp == NULL) {
1277 disp = hw;
1278 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001279 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001280 break;
1281 }
1282 }
1283 }
1284 }
Chet Haase91d25932013-04-11 15:24:55 -07001285 if (disp == NULL) {
1286 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1287 // redraw after transform hint changes. See bug 8508397.
1288
1289 // could be null when this layer is using a layerStack
1290 // that is not visible on any display. Also can occur at
1291 // screen off/on times.
1292 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001293 }
Chet Haase91d25932013-04-11 15:24:55 -07001294 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001295 }
1296 }
1297
1298
Mathias Agopian3559b072012-08-15 13:46:03 -07001299 /*
1300 * Perform our own transaction if needed
1301 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001302
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001303 const LayerVector& layers(mDrawingState.layersSortedByZ);
1304 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001305 // layers have been added
1306 mVisibleRegionsDirty = true;
1307 }
1308
1309 // some layers might have been removed, so
1310 // we need to update the regions they're exposing.
1311 if (mLayersRemoved) {
1312 mLayersRemoved = false;
1313 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001314 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001315 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001316 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001317 if (currentLayers.indexOf(layer) < 0) {
1318 // this layer is not visible anymore
1319 // TODO: we could traverse the tree from front to back and
1320 // compute the actual visible region
1321 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001322 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001323 Region visibleReg = s.transform.transform(
1324 Region(Rect(s.active.w, s.active.h)));
1325 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001326 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 }
1329
1330 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001331}
1332
1333void SurfaceFlinger::commitTransaction()
1334{
1335 if (!mLayersPendingRemoval.isEmpty()) {
1336 // Notify removed layers now that they can't be drawn from
1337 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1338 mLayersPendingRemoval[i]->onRemoved();
1339 }
1340 mLayersPendingRemoval.clear();
1341 }
1342
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001343 // If this transaction is part of a window animation then the next frame
1344 // we composite should be considered an animation as well.
1345 mAnimCompositionPending = mAnimTransactionPending;
1346
Mathias Agopian4fec8732012-06-29 14:12:52 -07001347 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001348 mTransactionPending = false;
1349 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001350 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351}
1352
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001354 const LayerVector& currentLayers, uint32_t layerStack,
1355 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356{
Mathias Agopian841cde52012-03-01 15:44:37 -08001357 ATRACE_CALL();
1358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 Region aboveOpaqueLayers;
1360 Region aboveCoveredLayers;
1361 Region dirty;
1362
Mathias Agopian87baae12012-07-31 12:38:26 -07001363 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364
1365 size_t i = currentLayers.size();
1366 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001367 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368
1369 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001370 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371
Jesse Hall01e29052013-02-19 16:13:35 -08001372 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001373 if (s.layerStack != layerStack)
1374 continue;
1375
Mathias Agopianab028732010-03-16 16:41:46 -07001376 /*
1377 * opaqueRegion: area of a surface that is fully opaque.
1378 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001380
1381 /*
1382 * visibleRegion: area of a surface that is visible on screen
1383 * and not fully transparent. This is essentially the layer's
1384 * footprint minus the opaque regions above it.
1385 * Areas covered by a translucent surface are considered visible.
1386 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001388
1389 /*
1390 * coveredRegion: area of a surface that is covered by all
1391 * visible regions above it (which includes the translucent areas).
1392 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001394
Jesse Halla8026d22012-09-25 13:25:04 -07001395 /*
1396 * transparentRegion: area of a surface that is hinted to be completely
1397 * transparent. This is only used to tell when the layer has no visible
1398 * non-transparent regions and can be removed from the layer list. It
1399 * does not affect the visibleRegion of this layer or any layers
1400 * beneath it. The hint may not be correct if apps don't respect the
1401 * SurfaceView restrictions (which, sadly, some don't).
1402 */
1403 Region transparentRegion;
1404
Mathias Agopianab028732010-03-16 16:41:46 -07001405
1406 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001407 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001408 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001409 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001411 if (!visibleRegion.isEmpty()) {
1412 // Remove the transparent area from the visible region
1413 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001414 const Transform tr(s.transform);
1415 if (tr.transformed()) {
1416 if (tr.preserveRects()) {
1417 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001418 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001419 } else {
1420 // transformation too complex, can't do the
1421 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001422 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001423 }
1424 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001425 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001426 }
Mathias Agopianab028732010-03-16 16:41:46 -07001427 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428
Mathias Agopianab028732010-03-16 16:41:46 -07001429 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001430 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001431 if (s.alpha==255 && !translucent &&
1432 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1433 // the opaque region is the layer's footprint
1434 opaqueRegion = visibleRegion;
1435 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 }
1437 }
1438
Mathias Agopianab028732010-03-16 16:41:46 -07001439 // Clip the covered region to the visible region
1440 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1441
1442 // Update aboveCoveredLayers for next (lower) layer
1443 aboveCoveredLayers.orSelf(visibleRegion);
1444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 // subtract the opaque region covered by the layers above us
1446 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447
1448 // compute this layer's dirty region
1449 if (layer->contentDirty) {
1450 // we need to invalidate the whole region
1451 dirty = visibleRegion;
1452 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001453 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 layer->contentDirty = false;
1455 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001456 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001457 * the exposed region consists of two components:
1458 * 1) what's VISIBLE now and was COVERED before
1459 * 2) what's EXPOSED now less what was EXPOSED before
1460 *
1461 * note that (1) is conservative, we start with the whole
1462 * visible region but only keep what used to be covered by
1463 * something -- which mean it may have been exposed.
1464 *
1465 * (2) handles areas that were not covered by anything but got
1466 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001467 */
Mathias Agopianab028732010-03-16 16:41:46 -07001468 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001469 const Region oldVisibleRegion = layer->visibleRegion;
1470 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001471 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1472 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 }
1474 dirty.subtractSelf(aboveOpaqueLayers);
1475
1476 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001477 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478
Mathias Agopianab028732010-03-16 16:41:46 -07001479 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001481
Jesse Halla8026d22012-09-25 13:25:04 -07001482 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 layer->setVisibleRegion(visibleRegion);
1484 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001485 layer->setVisibleNonTransparentRegion(
1486 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 }
1488
Mathias Agopian87baae12012-07-31 12:38:26 -07001489 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490}
1491
Mathias Agopian87baae12012-07-31 12:38:26 -07001492void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1493 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001494 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001495 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1496 if (hw->getLayerStack() == layerStack) {
1497 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001498 }
1499 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001500}
1501
1502void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001504 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505
Mathias Agopian4fec8732012-06-29 14:12:52 -07001506 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001507 const LayerVector& layers(mDrawingState.layersSortedByZ);
1508 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001509 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001510 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001511 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001512 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001513 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001514 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001515
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001516 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517}
1518
Mathias Agopianad456f92011-01-13 17:53:01 -08001519void SurfaceFlinger::invalidateHwcGeometry()
1520{
1521 mHwWorkListDirty = true;
1522}
1523
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001524
Mathias Agopiancd60f992012-08-16 16:28:27 -07001525void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001526 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527{
Mathias Agopian87baae12012-07-31 12:38:26 -07001528 Region dirtyRegion(inDirtyRegion);
1529
Mathias Agopianb8a55602009-06-26 19:06:36 -07001530 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001531 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532
Mathias Agopian42977342012-08-05 00:40:46 -07001533 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001534 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001535 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1536 // takes a rectangle, we must make sure to update that whole
1537 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001538 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001539 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001540 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001541 // We need to redraw the rectangle that will be updated
1542 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001543 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001544 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001545 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001546 } else {
1547 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001548 dirtyRegion.set(hw->bounds());
1549 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550 }
1551 }
1552
Mathias Agopiancd60f992012-08-16 16:28:27 -07001553 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001555 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001556 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001557
1558 // swap buffers (presentation)
1559 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560}
1561
Mathias Agopiancd60f992012-08-16 16:28:27 -07001562void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001563{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001564 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001565 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001566 HWComposer::LayerListIterator cur = hwc.begin(id);
1567 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001568
Mathias Agopian85d751c2012-08-29 16:59:24 -07001569 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1570 if (hasGlesComposition) {
Michael Chockc8c71092013-03-04 15:15:46 -08001571 if (!DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext)) {
1572 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1573 hw->getDisplayName().string());
1574 return;
1575 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001576
1577 // set the frame buffer
1578 glMatrixMode(GL_MODELVIEW);
1579 glLoadIdentity();
1580
1581 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001582 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001583 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001584 // when using overlays, we assume a fully transparent framebuffer
1585 // NOTE: we could reduce how much we need to clear, for instance
1586 // remove where there are opaque FB layers. however, on some
1587 // GPUs doing a "clean slate" glClear might be more efficient.
1588 // We'll revisit later if needed.
1589 glClearColor(0, 0, 0, 0);
1590 glClear(GL_COLOR_BUFFER_BIT);
1591 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001592 // we start with the whole screen area
1593 const Region bounds(hw->getBounds());
1594
1595 // we remove the scissor part
1596 // we're left with the letterbox region
1597 // (common case is that letterbox ends-up being empty)
1598 const Region letterbox(bounds.subtract(hw->getScissor()));
1599
1600 // compute the area to clear
1601 Region region(hw->undefinedRegion.merge(letterbox));
1602
1603 // but limit it to the dirty region
1604 region.andSelf(dirty);
1605
Mathias Agopianb9494d52012-04-18 02:28:45 -07001606 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001607 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001608 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001609 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001610 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001611 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001612
Mathias Agopian766dc492012-10-30 18:08:06 -07001613 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1614 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001615 // scissor on the main display. It should never be needed
1616 // anyways (though in theory it could since the API allows it).
1617 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001618 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001619 if (scissor != bounds) {
1620 // scissor doesn't match the screen's dimensions, so we
1621 // need to clear everything outside of it and enable
1622 // the GL scissor so we don't draw anything where we shouldn't
1623 const GLint height = hw->getHeight();
1624 glScissor(scissor.left, height - scissor.bottom,
1625 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001626 // enable scissor for this frame
1627 glEnable(GL_SCISSOR_TEST);
1628 }
1629 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001630 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001631
Mathias Agopian85d751c2012-08-29 16:59:24 -07001632 /*
1633 * and then, render the layers targeted at the framebuffer
1634 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001635
Mathias Agopian13127d82013-03-05 17:47:11 -08001636 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001637 const size_t count = layers.size();
1638 const Transform& tr = hw->getTransform();
1639 if (cur != end) {
1640 // we're using h/w composer
1641 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001642 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001643 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001644 if (!clip.isEmpty()) {
1645 switch (cur->getCompositionType()) {
1646 case HWC_OVERLAY: {
1647 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1648 && i
1649 && layer->isOpaque()
1650 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001651 // never clear the very first layer since we're
1652 // guaranteed the FB is already cleared
1653 layer->clearWithOpenGL(hw, clip);
1654 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001655 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001656 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001657 case HWC_FRAMEBUFFER: {
1658 layer->draw(hw, clip);
1659 break;
1660 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001661 case HWC_FRAMEBUFFER_TARGET: {
1662 // this should not happen as the iterator shouldn't
1663 // let us get there.
1664 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1665 break;
1666 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001667 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001668 }
1669 layer->setAcquireFence(hw, *cur);
1670 }
1671 } else {
1672 // we're not using h/w composer
1673 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001674 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001675 const Region clip(dirty.intersect(
1676 tr.transform(layer->visibleRegion)));
1677 if (!clip.isEmpty()) {
1678 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001679 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001680 }
1681 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001682
1683 // disable scissor at the end of the frame
1684 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685}
1686
Mathias Agopian55801e42012-08-27 18:54:24 -07001687void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1688 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001690 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001691 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001692 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001693 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001694
Mathias Agopian55801e42012-08-27 18:54:24 -07001695 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001696 Region::const_iterator it = region.begin();
1697 Region::const_iterator const end = region.end();
1698 while (it != end) {
1699 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001700 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001701 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1702 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1703 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1704 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001705 };
1706 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001707 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1708 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709}
1710
Mathias Agopianac9fa422013-02-11 16:40:36 -08001711void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1712 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001713 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001714 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001715{
Mathias Agopian96f08192010-06-02 23:28:45 -07001716 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001717 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001718
Mathias Agopian96f08192010-06-02 23:28:45 -07001719 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001720 Mutex::Autolock _l(mStateLock);
1721 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001722 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001723}
1724
Mathias Agopian13127d82013-03-05 17:47:11 -08001725status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726{
1727 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001728 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001730 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001731 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001732 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 return NO_ERROR;
1734 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001735 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736}
1737
Mathias Agopiandea20b12011-05-03 17:04:02 -07001738uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1739{
1740 return android_atomic_release_load(&mTransactionFlags);
1741}
1742
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1744{
1745 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1746}
1747
Mathias Agopianbb641242010-05-18 17:06:55 -07001748uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749{
1750 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1751 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001752 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753 }
1754 return old;
1755}
1756
Mathias Agopian8b33f032012-07-24 20:43:54 -07001757void SurfaceFlinger::setTransactionState(
1758 const Vector<ComposerState>& state,
1759 const Vector<DisplayState>& displays,
1760 uint32_t flags)
1761{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001762 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001763 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001764 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001765
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001766 if (flags & eAnimation) {
1767 // For window updates that are part of an animation we must wait for
1768 // previous animation "frames" to be handled.
1769 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001770 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001771 if (CC_UNLIKELY(err != NO_ERROR)) {
1772 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001773 // caller after a few seconds.
1774 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1775 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001776 mAnimTransactionPending = false;
1777 break;
1778 }
1779 }
1780 }
1781
Mathias Agopiane57f2922012-08-09 16:29:12 -07001782 size_t count = displays.size();
1783 for (size_t i=0 ; i<count ; i++) {
1784 const DisplayState& s(displays[i]);
1785 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001786 }
1787
Mathias Agopiane57f2922012-08-09 16:29:12 -07001788 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001789 for (size_t i=0 ; i<count ; i++) {
1790 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001791 // Here we need to check that the interface we're given is indeed
1792 // one of our own. A malicious client could give us a NULL
1793 // IInterface, or one of its own or even one of our own but a
1794 // different type. All these situations would cause us to crash.
1795 //
1796 // NOTE: it would be better to use RTTI as we could directly check
1797 // that we have a Client*. however, RTTI is disabled in Android.
1798 if (s.client != NULL) {
1799 sp<IBinder> binder = s.client->asBinder();
1800 if (binder != NULL) {
1801 String16 desc(binder->getInterfaceDescriptor());
1802 if (desc == ISurfaceComposerClient::descriptor) {
1803 sp<Client> client( static_cast<Client *>(s.client.get()) );
1804 transactionFlags |= setClientStateLocked(client, s.state);
1805 }
1806 }
1807 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001808 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001809
Mathias Agopian386aa982011-11-07 21:58:03 -08001810 if (transactionFlags) {
1811 // this triggers the transaction
1812 setTransactionFlags(transactionFlags);
1813
1814 // if this is a synchronous transaction, wait for it to take effect
1815 // before returning.
1816 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001817 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001818 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001819 if (flags & eAnimation) {
1820 mAnimTransactionPending = true;
1821 }
1822 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001823 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1824 if (CC_UNLIKELY(err != NO_ERROR)) {
1825 // just in case something goes wrong in SF, return to the
1826 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001827 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1828 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001829 break;
1830 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001831 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832 }
1833}
1834
Mathias Agopiane57f2922012-08-09 16:29:12 -07001835uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1836{
Jesse Hall9a143922012-10-04 16:29:19 -07001837 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1838 if (dpyIdx < 0)
1839 return 0;
1840
Mathias Agopiane57f2922012-08-09 16:29:12 -07001841 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001842 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001843 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001844 const uint32_t what = s.what;
1845 if (what & DisplayState::eSurfaceChanged) {
1846 if (disp.surface->asBinder() != s.surface->asBinder()) {
1847 disp.surface = s.surface;
1848 flags |= eDisplayTransactionNeeded;
1849 }
1850 }
1851 if (what & DisplayState::eLayerStackChanged) {
1852 if (disp.layerStack != s.layerStack) {
1853 disp.layerStack = s.layerStack;
1854 flags |= eDisplayTransactionNeeded;
1855 }
1856 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001857 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001858 if (disp.orientation != s.orientation) {
1859 disp.orientation = s.orientation;
1860 flags |= eDisplayTransactionNeeded;
1861 }
1862 if (disp.frame != s.frame) {
1863 disp.frame = s.frame;
1864 flags |= eDisplayTransactionNeeded;
1865 }
1866 if (disp.viewport != s.viewport) {
1867 disp.viewport = s.viewport;
1868 flags |= eDisplayTransactionNeeded;
1869 }
1870 }
1871 }
1872 return flags;
1873}
1874
1875uint32_t SurfaceFlinger::setClientStateLocked(
1876 const sp<Client>& client,
1877 const layer_state_t& s)
1878{
1879 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001880 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001881 if (layer != 0) {
1882 const uint32_t what = s.what;
1883 if (what & layer_state_t::ePositionChanged) {
1884 if (layer->setPosition(s.x, s.y))
1885 flags |= eTraversalNeeded;
1886 }
1887 if (what & layer_state_t::eLayerChanged) {
1888 // NOTE: index needs to be calculated before we update the state
1889 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1890 if (layer->setLayer(s.z)) {
1891 mCurrentState.layersSortedByZ.removeAt(idx);
1892 mCurrentState.layersSortedByZ.add(layer);
1893 // we need traversal (state changed)
1894 // AND transaction (list changed)
1895 flags |= eTransactionNeeded|eTraversalNeeded;
1896 }
1897 }
1898 if (what & layer_state_t::eSizeChanged) {
1899 if (layer->setSize(s.w, s.h)) {
1900 flags |= eTraversalNeeded;
1901 }
1902 }
1903 if (what & layer_state_t::eAlphaChanged) {
1904 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1905 flags |= eTraversalNeeded;
1906 }
1907 if (what & layer_state_t::eMatrixChanged) {
1908 if (layer->setMatrix(s.matrix))
1909 flags |= eTraversalNeeded;
1910 }
1911 if (what & layer_state_t::eTransparentRegionChanged) {
1912 if (layer->setTransparentRegionHint(s.transparentRegion))
1913 flags |= eTraversalNeeded;
1914 }
1915 if (what & layer_state_t::eVisibilityChanged) {
1916 if (layer->setFlags(s.flags, s.mask))
1917 flags |= eTraversalNeeded;
1918 }
1919 if (what & layer_state_t::eCropChanged) {
1920 if (layer->setCrop(s.crop))
1921 flags |= eTraversalNeeded;
1922 }
1923 if (what & layer_state_t::eLayerStackChanged) {
1924 // NOTE: index needs to be calculated before we update the state
1925 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1926 if (layer->setLayerStack(s.layerStack)) {
1927 mCurrentState.layersSortedByZ.removeAt(idx);
1928 mCurrentState.layersSortedByZ.add(layer);
1929 // we need traversal (state changed)
1930 // AND transaction (list changed)
1931 flags |= eTransactionNeeded|eTraversalNeeded;
1932 }
1933 }
1934 }
1935 return flags;
1936}
1937
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001938status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001939 const String8& name,
1940 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001941 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1942 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001944 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001945 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001946 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001947 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001948 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001949 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001950
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001951 status_t result = NO_ERROR;
1952
1953 sp<Layer> layer;
1954
Mathias Agopian3165cc22012-08-08 19:42:09 -07001955 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1956 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001957 result = createNormalLayer(client,
1958 name, w, h, flags, format,
1959 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001961 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001962 result = createDimLayer(client,
1963 name, w, h, flags,
1964 handle, gbp, &layer);
1965 break;
1966 default:
1967 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001968 break;
1969 }
1970
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001971 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001972 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001973 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001974 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001975 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001976}
1977
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001978status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1979 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1980 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001981{
1982 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001983 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984 case PIXEL_FORMAT_TRANSPARENT:
1985 case PIXEL_FORMAT_TRANSLUCENT:
1986 format = PIXEL_FORMAT_RGBA_8888;
1987 break;
1988 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001989#ifdef NO_RGBX_8888
1990 format = PIXEL_FORMAT_RGB_565;
1991#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001992 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001993#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001994 break;
1995 }
1996
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001997#ifdef NO_RGBX_8888
1998 if (format == PIXEL_FORMAT_RGBX_8888)
1999 format = PIXEL_FORMAT_RGBA_8888;
2000#endif
2001
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002002 *outLayer = new Layer(this, client, name, w, h, flags);
2003 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2004 if (err == NO_ERROR) {
2005 *handle = (*outLayer)->getHandle();
2006 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002007 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002008
2009 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2010 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002011}
2012
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002013status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2014 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2015 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002016{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002017 *outLayer = new LayerDim(this, client, name, w, h, flags);
2018 *handle = (*outLayer)->getHandle();
2019 *gbp = (*outLayer)->getBufferQueue();
2020 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002021}
2022
Mathias Agopianac9fa422013-02-11 16:40:36 -08002023status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002024{
Mathias Agopian67106042013-03-14 19:18:13 -07002025 // called by the window manager when it wants to remove a Layer
2026 status_t err = NO_ERROR;
2027 sp<Layer> l(client->getLayerUser(handle));
2028 if (l != NULL) {
2029 err = removeLayer(l);
2030 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2031 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002032 }
2033 return err;
2034}
2035
Mathias Agopian13127d82013-03-05 17:47:11 -08002036status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002037{
Mathias Agopian67106042013-03-14 19:18:13 -07002038 // called by ~LayerCleaner() when all references to the IBinder (handle)
2039 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002040 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002041 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002042 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002043 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002044 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002045 "error removing layer=%p (%s)", l.get(), strerror(-err));
2046 }
2047 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048}
2049
Mathias Agopianb60314a2012-04-10 22:09:54 -07002050// ---------------------------------------------------------------------------
2051
Andy McFadden13a082e2012-08-24 10:16:42 -07002052void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002053 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002054 Vector<ComposerState> state;
2055 Vector<DisplayState> displays;
2056 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002057 d.what = DisplayState::eDisplayProjectionChanged |
2058 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002059 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002060 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002061 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002062 d.frame.makeInvalid();
2063 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002064 displays.add(d);
2065 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002066 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002067}
2068
2069void SurfaceFlinger::initializeDisplays() {
2070 class MessageScreenInitialized : public MessageBase {
2071 SurfaceFlinger* flinger;
2072 public:
2073 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2074 virtual bool handler() {
2075 flinger->onInitializeDisplays();
2076 return true;
2077 }
2078 };
2079 sp<MessageBase> msg = new MessageScreenInitialized(this);
2080 postMessageAsync(msg); // we may be called from main thread, use async message
2081}
2082
2083
Mathias Agopiancde87a32012-09-13 14:09:01 -07002084void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002085 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2086 if (hw->isScreenAcquired()) {
2087 // this is expected, e.g. when power manager wakes up during boot
2088 ALOGD(" screen was previously acquired");
2089 return;
2090 }
2091
Mathias Agopian42977342012-08-05 00:40:46 -07002092 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002093 int32_t type = hw->getDisplayType();
2094 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2095 // built-in display, tell the HWC
2096 getHwComposer().acquire(type);
2097
2098 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2099 // FIXME: eventthread only knows about the main display right now
2100 mEventThread->onScreenAcquired();
2101 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002102 }
Mathias Agopian20128302012-08-13 18:32:13 -07002103 mVisibleRegionsDirty = true;
2104 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002105}
2106
Mathias Agopiancde87a32012-09-13 14:09:01 -07002107void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002108 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2109 if (!hw->isScreenAcquired()) {
2110 ALOGD(" screen was previously released");
2111 return;
2112 }
2113
2114 hw->releaseScreen();
2115 int32_t type = hw->getDisplayType();
2116 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2117 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002118 // FIXME: eventthread only knows about the main display right now
2119 mEventThread->onScreenReleased();
2120 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002121
2122 // built-in display, tell the HWC
2123 getHwComposer().release(type);
2124 }
2125 mVisibleRegionsDirty = true;
2126 // from this point on, SF will stop drawing on this display
2127}
2128
2129void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2130 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002131 SurfaceFlinger& mFlinger;
2132 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002133 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002134 MessageScreenAcquired(SurfaceFlinger& flinger,
2135 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002136 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002137 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2138 if (hw == NULL) {
2139 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2140 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2141 ALOGW("Attempt to unblank virtual display");
2142 } else {
2143 mFlinger.onScreenAcquired(hw);
2144 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002145 return true;
2146 }
2147 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002148 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2149 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002150}
2151
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002152void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002153 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002154 SurfaceFlinger& mFlinger;
2155 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002156 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002157 MessageScreenReleased(SurfaceFlinger& flinger,
2158 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002159 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002160 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2161 if (hw == NULL) {
2162 ALOGE("Attempt to blank null display %p", mDisplay.get());
2163 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2164 ALOGW("Attempt to blank virtual display");
2165 } else {
2166 mFlinger.onScreenReleased(hw);
2167 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002168 return true;
2169 }
2170 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002171 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2172 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002173}
2174
2175// ---------------------------------------------------------------------------
2176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002177status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2178{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002179 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002180
Mathias Agopianbd115332013-04-18 16:41:04 -07002181 IPCThreadState* ipc = IPCThreadState::self();
2182 const int pid = ipc->getCallingPid();
2183 const int uid = ipc->getCallingUid();
2184 if ((uid != AID_SHELL) &&
2185 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002186 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002187 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002188 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002189 // Try to get the main lock, but don't insist if we can't
2190 // (this would indicate SF is stuck, but we want to be able to
2191 // print something in dumpsys).
2192 int retry = 3;
2193 while (mStateLock.tryLock()<0 && --retry>=0) {
2194 usleep(1000000);
2195 }
2196 const bool locked(retry >= 0);
2197 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002198 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002199 "SurfaceFlinger appears to be unresponsive, "
2200 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002201 }
2202
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002203 bool dumpAll = true;
2204 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002205 size_t numArgs = args.size();
2206 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002207 if ((index < numArgs) &&
2208 (args[index] == String16("--list"))) {
2209 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002210 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002211 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002212 }
2213
2214 if ((index < numArgs) &&
2215 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002216 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002217 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002218 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002219 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002220
2221 if ((index < numArgs) &&
2222 (args[index] == String16("--latency-clear"))) {
2223 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002224 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002225 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002226 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002227 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002228
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002229 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002230 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002231 }
2232
Mathias Agopian9795c422009-08-26 16:36:26 -07002233 if (locked) {
2234 mStateLock.unlock();
2235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002236 }
2237 write(fd, result.string(), result.size());
2238 return NO_ERROR;
2239}
2240
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002241void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002242 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002243{
2244 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2245 const size_t count = currentLayers.size();
2246 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002247 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002248 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002249 }
2250}
2251
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002252void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002253 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002254{
2255 String8 name;
2256 if (index < args.size()) {
2257 name = String8(args[index]);
2258 index++;
2259 }
2260
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002261 const nsecs_t period =
2262 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2263 result.appendFormat("%lld\n", period);
2264
2265 if (name.isEmpty()) {
2266 mAnimFrameTracker.dump(result);
2267 } else {
2268 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2269 const size_t count = currentLayers.size();
2270 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002271 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002272 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002273 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002274 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002275 }
2276 }
2277}
2278
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002279void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002280 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002281{
2282 String8 name;
2283 if (index < args.size()) {
2284 name = String8(args[index]);
2285 index++;
2286 }
2287
2288 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2289 const size_t count = currentLayers.size();
2290 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002291 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002292 if (name.isEmpty() || (name == layer->getName())) {
2293 layer->clearStats();
2294 }
2295 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002296
2297 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002298}
2299
Andy McFadden4803b742012-09-24 19:07:20 -07002300/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2301{
2302 static const char* config =
2303 " [sf"
2304#ifdef NO_RGBX_8888
2305 " NO_RGBX_8888"
2306#endif
2307#ifdef HAS_CONTEXT_PRIORITY
2308 " HAS_CONTEXT_PRIORITY"
2309#endif
2310#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2311 " NEVER_DEFAULT_TO_ASYNC_MODE"
2312#endif
2313#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2314 " TARGET_DISABLE_TRIPLE_BUFFERING"
2315#endif
2316 "]";
2317 result.append(config);
2318}
2319
Mathias Agopian74d211a2013-04-22 16:55:35 +02002320void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2321 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002322{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002323 bool colorize = false;
2324 if (index < args.size()
2325 && (args[index] == String16("--color"))) {
2326 colorize = true;
2327 index++;
2328 }
2329
2330 Colorizer colorizer(colorize);
2331
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002332 // figure out if we're stuck somewhere
2333 const nsecs_t now = systemTime();
2334 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2335 const nsecs_t inTransaction(mDebugInTransaction);
2336 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2337 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2338
2339 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002340 * Dump library configuration.
2341 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002342
2343 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002344 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002345 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002346 appendSfConfigString(result);
2347 appendUiConfigString(result);
2348 appendGuiConfigString(result);
2349 result.append("\n");
2350
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002351 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002352 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002353 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002354 result.append(SyncFeatures::getInstance().toString());
2355 result.append("\n");
2356
Andy McFadden4803b742012-09-24 19:07:20 -07002357 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358 * Dump the visible layer list
2359 */
2360 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2361 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002362 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002363 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002364 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002365 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002366 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002367 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002368 }
2369
2370 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002371 * Dump Display state
2372 */
2373
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002374 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002375 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002376 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002377 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2378 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002379 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002380 }
2381
2382 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002383 * Dump SurfaceFlinger global state
2384 */
2385
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002386 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002387 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002388 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002389
Mathias Agopian888c8222012-08-04 21:10:38 -07002390 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002391 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002392 const GLExtensions& extensions(GLExtensions::getInstance());
Mathias Agopianca088332013-03-28 17:44:13 -07002393
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002394 colorizer.bold(result);
2395 result.appendFormat("EGL implementation : %s\n",
2396 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2397 colorizer.reset(result);
2398 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002399 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002400
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002401 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002402 result.appendFormat("GLES: %s, %s, %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002403 extensions.getVendor(),
2404 extensions.getRenderer(),
2405 extensions.getVersion());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002406 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002407 result.appendFormat("%s\n", extensions.getExtension());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002408
Mathias Agopian42977342012-08-05 00:40:46 -07002409 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002410 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002411 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002412 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002413 " last eglSwapBuffers() time: %f us\n"
2414 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002415 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002416 " refresh-rate : %f fps\n"
2417 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002418 " y-dpi : %f\n"
2419 " EGL_NATIVE_VISUAL_ID : %d\n"
2420 " gpu_to_cpu_unsupported : %d\n"
2421 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002422 mLastSwapBufferTime/1000.0,
2423 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002424 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002425 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2426 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002427 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2428 mEGLNativeVisualId,
2429 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002430
Mathias Agopian74d211a2013-04-22 16:55:35 +02002431 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002432 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002433
Mathias Agopian74d211a2013-04-22 16:55:35 +02002434 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002435 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002436
2437 /*
2438 * VSYNC state
2439 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002440 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002441
2442 /*
2443 * Dump HWComposer state
2444 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002445 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002446 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002447 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002448 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002449 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2450 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002451 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002452
2453 /*
2454 * Dump gralloc state
2455 */
2456 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2457 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002458}
2459
Mathias Agopian13127d82013-03-05 17:47:11 -08002460const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002461SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002462 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002463 wp<IBinder> dpy;
2464 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2465 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2466 dpy = mDisplays.keyAt(i);
2467 break;
2468 }
2469 }
2470 if (dpy == NULL) {
2471 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2472 // Just use the primary display so we have something to return
2473 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2474 }
2475 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002476}
2477
Keun young Park63f165f2012-08-31 10:53:36 -07002478bool SurfaceFlinger::startDdmConnection()
2479{
2480 void* libddmconnection_dso =
2481 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2482 if (!libddmconnection_dso) {
2483 return false;
2484 }
2485 void (*DdmConnection_start)(const char* name);
2486 DdmConnection_start =
2487 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2488 if (!DdmConnection_start) {
2489 dlclose(libddmconnection_dso);
2490 return false;
2491 }
2492 (*DdmConnection_start)(getServiceName());
2493 return true;
2494}
2495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496status_t SurfaceFlinger::onTransact(
2497 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2498{
2499 switch (code) {
2500 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002501 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002502 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002503 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002504 case BLANK:
2505 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 {
2507 // codes that require permission check
2508 IPCThreadState* ipc = IPCThreadState::self();
2509 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002510 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002511 if ((uid != AID_GRAPHICS) &&
2512 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002513 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002514 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2515 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002517 break;
2518 }
2519 case CAPTURE_SCREEN:
2520 {
2521 // codes that require permission check
2522 IPCThreadState* ipc = IPCThreadState::self();
2523 const int pid = ipc->getCallingPid();
2524 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002525 if ((uid != AID_GRAPHICS) &&
2526 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002527 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002528 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2529 return PERMISSION_DENIED;
2530 }
2531 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532 }
2533 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002535 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2536 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002537 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002538 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002539 IPCThreadState* ipc = IPCThreadState::self();
2540 const int pid = ipc->getCallingPid();
2541 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002542 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002543 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002544 return PERMISSION_DENIED;
2545 }
2546 int n;
2547 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002548 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002549 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550 return NO_ERROR;
2551 case 1002: // SHOW_UPDATES
2552 n = data.readInt32();
2553 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002554 invalidateHwcGeometry();
2555 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002557 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002558 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002559 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002560 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002561 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002562 setTransactionFlags(
2563 eTransactionNeeded|
2564 eDisplayTransactionNeeded|
2565 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002566 return NO_ERROR;
2567 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002568 case 1006:{ // send empty update
2569 signalRefresh();
2570 return NO_ERROR;
2571 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002572 case 1008: // toggle use of hw composer
2573 n = data.readInt32();
2574 mDebugDisableHWC = n ? 1 : 0;
2575 invalidateHwcGeometry();
2576 repaintEverything();
2577 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002578 case 1009: // toggle use of transform hint
2579 n = data.readInt32();
2580 mDebugDisableTransformHint = n ? 1 : 0;
2581 invalidateHwcGeometry();
2582 repaintEverything();
2583 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002584 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002585 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002586 reply->writeInt32(0);
2587 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002588 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002589 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 return NO_ERROR;
2591 case 1013: {
2592 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002593 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2594 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002595 }
2596 return NO_ERROR;
2597 }
2598 }
2599 return err;
2600}
2601
Mathias Agopian53331da2011-08-22 21:44:41 -07002602void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002603 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002604 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002605}
2606
Mathias Agopian59119e62010-10-11 12:37:43 -07002607// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002608// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002609// ---------------------------------------------------------------------------
2610
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002611status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2612 const sp<IGraphicBufferProducer>& producer,
2613 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002614 uint32_t minLayerZ, uint32_t maxLayerZ,
2615 bool isCpuConsumer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002616
2617 if (CC_UNLIKELY(display == 0))
2618 return BAD_VALUE;
2619
2620 if (CC_UNLIKELY(producer == 0))
2621 return BAD_VALUE;
2622
2623 class MessageCaptureScreen : public MessageBase {
2624 SurfaceFlinger* flinger;
2625 sp<IBinder> display;
2626 sp<IGraphicBufferProducer> producer;
2627 uint32_t reqWidth, reqHeight;
2628 uint32_t minLayerZ,maxLayerZ;
Mathias Agopianabe815d2013-03-19 22:22:21 -07002629 bool isCpuConsumer;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002630 status_t result;
2631 public:
2632 MessageCaptureScreen(SurfaceFlinger* flinger,
2633 const sp<IBinder>& display,
2634 const sp<IGraphicBufferProducer>& producer,
2635 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002636 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002637 : flinger(flinger), display(display), producer(producer),
2638 reqWidth(reqWidth), reqHeight(reqHeight),
2639 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopianabe815d2013-03-19 22:22:21 -07002640 isCpuConsumer(isCpuConsumer),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002641 result(PERMISSION_DENIED)
2642 {
2643 }
2644 status_t getResult() const {
2645 return result;
2646 }
2647 virtual bool handler() {
2648 Mutex::Autolock _l(flinger->mStateLock);
2649 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002650 bool useReadPixels = isCpuConsumer && !flinger->mGpuToCpuSupported;
2651 result = flinger->captureScreenImplLocked(hw,
2652 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2653 useReadPixels);
Mathias Agopianb4b17302013-03-20 18:36:41 -07002654
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002655 return true;
2656 }
2657 };
2658
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002659 // make sure to process transactions before screenshots -- a transaction
2660 // might already be pending but scheduled for VSYNC; this guarantees we
2661 // will handle it before the screenshot. When VSYNC finally arrives
2662 // the scheduled transaction will be a no-op. If no transactions are
2663 // scheduled at this time, this will end-up being a no-op as well.
2664 mEventQueue.invalidateTransactionNow();
2665
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002666 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002667 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2668 isCpuConsumer);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002669 status_t res = postMessageSync(msg);
2670 if (res == NO_ERROR) {
2671 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2672 }
2673 return res;
2674}
2675
Mathias Agopian180f10d2013-04-10 22:55:41 -07002676
2677void SurfaceFlinger::renderScreenImplLocked(
2678 const sp<const DisplayDevice>& hw,
2679 uint32_t reqWidth, uint32_t reqHeight,
2680 uint32_t minLayerZ, uint32_t maxLayerZ,
2681 bool yswap)
2682{
2683 ATRACE_CALL();
2684
2685 // get screen geometry
2686 const uint32_t hw_w = hw->getWidth();
2687 const uint32_t hw_h = hw->getHeight();
2688
2689 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2690
2691 // make sure to clear all GL error flags
2692 while ( glGetError() != GL_NO_ERROR ) ;
2693
2694 // set-up our viewport
2695 glViewport(0, 0, reqWidth, reqHeight);
2696 glMatrixMode(GL_PROJECTION);
2697 glLoadIdentity();
2698 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2699 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2700 glMatrixMode(GL_MODELVIEW);
2701 glLoadIdentity();
2702
2703 // redraw the screen entirely...
2704 glDisable(GL_SCISSOR_TEST);
2705 glClearColor(0,0,0,1);
2706 glClear(GL_COLOR_BUFFER_BIT);
2707 glDisable(GL_TEXTURE_EXTERNAL_OES);
2708 glDisable(GL_TEXTURE_2D);
2709
2710 const LayerVector& layers( mDrawingState.layersSortedByZ );
2711 const size_t count = layers.size();
2712 for (size_t i=0 ; i<count ; ++i) {
2713 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002714 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002715 if (state.layerStack == hw->getLayerStack()) {
2716 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2717 if (layer->isVisible()) {
2718 if (filtering) layer->setFiltering(true);
2719 layer->draw(hw);
2720 if (filtering) layer->setFiltering(false);
2721 }
2722 }
2723 }
2724 }
2725
2726 // compositionComplete is needed for older driver
2727 hw->compositionComplete();
2728}
2729
2730
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002731status_t SurfaceFlinger::captureScreenImplLocked(
2732 const sp<const DisplayDevice>& hw,
2733 const sp<IGraphicBufferProducer>& producer,
2734 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002735 uint32_t minLayerZ, uint32_t maxLayerZ,
2736 bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002737{
2738 ATRACE_CALL();
2739
2740 if (!GLExtensions::getInstance().haveFramebufferObject()) {
2741 return INVALID_OPERATION;
2742 }
2743
Mathias Agopian180f10d2013-04-10 22:55:41 -07002744 // get screen geometry
2745 const uint32_t hw_w = hw->getWidth();
2746 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002747
Mathias Agopian180f10d2013-04-10 22:55:41 -07002748 // if we have secure windows on this display, never allow the screen capture
2749 if (hw->getSecureLayerVisible()) {
2750 ALOGW("FB is protected: PERMISSION_DENIED");
2751 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002752 }
2753
Mathias Agopian180f10d2013-04-10 22:55:41 -07002754 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2755 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2756 reqWidth, reqHeight, hw_w, hw_h);
2757 return BAD_VALUE;
2758 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002759
Mathias Agopian180f10d2013-04-10 22:55:41 -07002760 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2761 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2762
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002763 // create a surface (because we're a producer, and we need to
2764 // dequeue/queue a buffer)
2765 sp<Surface> sur = new Surface(producer);
2766 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002767
2768 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002769 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
2770 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
2771 if (!useReadPixels) {
2772 usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002773 }
2774
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002775 int err = 0;
2776 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2777 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2778 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002779
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002780 if (err == NO_ERROR) {
2781 ANativeWindowBuffer* buffer;
2782 /* TODO: Once we have the sync framework everywhere this can use
2783 * server-side waits on the fence that dequeueBuffer returns.
2784 */
2785 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2786 if (result == NO_ERROR) {
2787 // create an EGLImage from the buffer so we can later
2788 // turn it into a texture
2789 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2790 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2791 if (image != EGL_NO_IMAGE_KHR) {
2792 GLuint tname, name;
2793 if (!useReadPixels) {
2794 // turn our EGLImage into a texture
2795 glGenTextures(1, &tname);
2796 glBindTexture(GL_TEXTURE_2D, tname);
2797 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2798 // create a Framebuffer Object to render into
2799 glGenFramebuffersOES(1, &name);
2800 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2801 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2802 GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
2803 } else {
2804 // since we're going to use glReadPixels() anyways,
2805 // use an intermediate renderbuffer instead
2806 glGenRenderbuffersOES(1, &tname);
2807 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2808 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
2809 // create a FBO to render into
2810 glGenFramebuffersOES(1, &name);
2811 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2812 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2813 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2814 }
2815
2816 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2817 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2818 // this will in fact render into our dequeued buffer
2819 // via an FBO, which means we didn't have to create
2820 // an EGLSurface and therefore we're not
2821 // dependent on the context's EGLConfig.
2822 renderScreenImplLocked(hw, reqWidth, reqHeight,
2823 minLayerZ, maxLayerZ, true);
2824
2825 if (useReadPixels) {
2826 sp<GraphicBuffer> buf = static_cast<GraphicBuffer*>(buffer);
2827 void* vaddr;
2828 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
2829 glReadPixels(0, 0, buffer->stride, reqHeight,
2830 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
Mathias Agopianfee2b462013-07-03 12:34:01 -07002831 checkScreenshot(buf, vaddr, hw, minLayerZ, maxLayerZ);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002832 buf->unlock();
2833 }
2834 }
2835 } else {
2836 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2837 result = INVALID_OPERATION;
2838 }
2839
2840 // back to main framebuffer
2841 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2842 glDeleteFramebuffersOES(1, &name);
2843 if (!useReadPixels) {
2844 glDeleteTextures(1, &tname);
2845 } else {
2846 glDeleteRenderbuffersOES(1, &tname);
2847 }
2848 // destroy our image
2849 eglDestroyImageKHR(mEGLDisplay, image);
2850 } else {
2851 result = BAD_VALUE;
2852 }
2853 window->queueBuffer(window, buffer, -1);
2854 }
2855 } else {
2856 result = BAD_VALUE;
2857 }
2858 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2859 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002860
2861 DisplayDevice::setViewportAndProjection(hw);
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002862
Mathias Agopian74c40c02010-09-29 13:02:36 -07002863 return result;
2864}
2865
Mathias Agopianfee2b462013-07-03 12:34:01 -07002866void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2867 const sp<const DisplayDevice>& hw,
2868 uint32_t minLayerZ, uint32_t maxLayerZ) {
2869 if (DEBUG_SCREENSHOTS) {
2870 for (ssize_t y=0 ; y<buf->height ; y++) {
2871 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2872 for (ssize_t x=0 ; x<buf->width ; x++) {
2873 if (p[x] != 0xFF000000) return;
2874 }
2875 }
2876 ALOGE("*** we just took a black screenshot ***\n"
2877 "requested minz=%d, maxz=%d, layerStack=%d",
2878 minLayerZ, maxLayerZ, hw->getLayerStack());
2879 const LayerVector& layers( mDrawingState.layersSortedByZ );
2880 const size_t count = layers.size();
2881 for (size_t i=0 ; i<count ; ++i) {
2882 const sp<Layer>& layer(layers[i]);
2883 const Layer::State& state(layer->getDrawingState());
2884 const bool visible = (state.layerStack == hw->getLayerStack())
2885 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2886 && (layer->isVisible());
2887 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2888 visible ? '+' : '-',
2889 i, layer->getName().string(), state.layerStack, state.z,
2890 layer->isVisible(), state.flags, state.alpha);
2891 }
2892 }
2893}
2894
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002895// ---------------------------------------------------------------------------
2896
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002897SurfaceFlinger::LayerVector::LayerVector() {
2898}
2899
2900SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002901 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002902}
2903
2904int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2905 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002906{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002907 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002908 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2909 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002910
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002911 uint32_t ls = l->getCurrentState().layerStack;
2912 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002913 if (ls != rs)
2914 return ls - rs;
2915
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002916 uint32_t lz = l->getCurrentState().z;
2917 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002918 if (lz != rz)
2919 return lz - rz;
2920
2921 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002922}
2923
2924// ---------------------------------------------------------------------------
2925
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002926SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2927 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002928}
2929
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002930SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002931 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002932 viewport.makeInvalid();
2933 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002934}
2935
2936// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002938}; // namespace android