blob: 6ab33ef31be86850780a3497c426b67297d61f42 [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 Agopianca088332013-03-28 17:44:13 -070075EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
76
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078// ---------------------------------------------------------------------------
79
Mathias Agopian99b49842011-06-27 16:05:52 -070080const String16 sHardwareTest("android.permission.HARDWARE_TEST");
81const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
82const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
83const String16 sDump("android.permission.DUMP");
84
85// ---------------------------------------------------------------------------
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087SurfaceFlinger::SurfaceFlinger()
88 : BnSurfaceComposer(), Thread(false),
89 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070090 mTransactionPending(false),
91 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070093 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070096 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -080097 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070099 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700100 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700101 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700102 mDebugInSwapBuffers(0),
103 mLastSwapBufferTime(0),
104 mDebugInTransaction(0),
105 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700106 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107{
Steve Blocka19954a2012-01-04 20:05:49 +0000108 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
110 // debugging stuff...
111 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700112
Mathias Agopianb4b17302013-03-20 18:36:41 -0700113 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700114 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 property_get("debug.sf.showupdates", value, "0");
117 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700119 property_get("debug.sf.ddms", value, "0");
120 mDebugDDMS = atoi(value);
121 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700122 if (!startDdmConnection()) {
123 // start failed, and DDMS debugging not enabled
124 mDebugDDMS = 0;
125 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700127 ALOGI_IF(mDebugRegion, "showupdates enabled");
128 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800131void SurfaceFlinger::onFirstRef()
132{
133 mEventQueue.init(this);
134
135 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
136
137 // Wait for the main thread to be done with its initialization
138 mReadyToRunBarrier.wait();
139}
140
141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142SurfaceFlinger::~SurfaceFlinger()
143{
Mathias Agopiana4912602012-07-12 14:25:33 -0700144 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
145 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
146 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}
148
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800149void SurfaceFlinger::binderDied(const wp<IBinder>& who)
150{
151 // the window manager died on us. prepare its eulogy.
152
Andy McFadden13a082e2012-08-24 10:16:42 -0700153 // restore initial conditions (default device unblank, etc)
154 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800155
156 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700157 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800158}
159
Mathias Agopian7e27f052010-05-28 14:22:23 -0700160sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161{
Mathias Agopian96f08192010-06-02 23:28:45 -0700162 sp<ISurfaceComposerClient> bclient;
163 sp<Client> client(new Client(this));
164 status_t err = client->initCheck();
165 if (err == NO_ERROR) {
166 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 return bclient;
169}
170
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700171sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
172 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700173{
174 class DisplayToken : public BBinder {
175 sp<SurfaceFlinger> flinger;
176 virtual ~DisplayToken() {
177 // no more references, this display must be terminated
178 Mutex::Autolock _l(flinger->mStateLock);
179 flinger->mCurrentState.displays.removeItem(this);
180 flinger->setTransactionFlags(eDisplayTransactionNeeded);
181 }
182 public:
183 DisplayToken(const sp<SurfaceFlinger>& flinger)
184 : flinger(flinger) {
185 }
186 };
187
188 sp<BBinder> token = new DisplayToken(this);
189
190 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700191 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700192 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700193 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700194 mCurrentState.displays.add(token, info);
195
196 return token;
197}
198
Jesse Hall692c7232012-11-08 15:41:56 -0800199void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
200 ALOGW_IF(mBuiltinDisplays[type],
201 "Overwriting display token for display type %d", type);
202 mBuiltinDisplays[type] = new BBinder();
203 DisplayDeviceState info(type);
204 // All non-virtual displays are currently considered secure.
205 info.isSecure = true;
206 mCurrentState.displays.add(mBuiltinDisplays[type], info);
207}
208
Mathias Agopiane57f2922012-08-09 16:29:12 -0700209sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700210 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700211 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
212 return NULL;
213 }
Jesse Hall692c7232012-11-08 15:41:56 -0800214 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700215}
216
Jamie Gennis9a78c902011-01-12 18:30:40 -0800217sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
218{
219 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
220 return gba;
221}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223void SurfaceFlinger::bootFinished()
224{
225 const nsecs_t now = systemTime();
226 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000227 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700228 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700229
230 // wait patiently for the window manager death
231 const String16 name("window");
232 sp<IBinder> window(defaultServiceManager()->getService(name));
233 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700234 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700235 }
236
237 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700238 // formerly we would just kill the process, but we now ask it to exit so it
239 // can choose where to stop the animation.
240 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700243void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
244 class MessageDestroyGLTexture : public MessageBase {
245 GLuint texture;
246 public:
247 MessageDestroyGLTexture(GLuint texture)
248 : texture(texture) {
249 }
250 virtual bool handler() {
251 glDeleteTextures(1, &texture);
252 return true;
253 }
254 };
255 postMessageAsync(new MessageDestroyGLTexture(texture));
256}
257
Mathias Agopian722b98f2012-09-25 18:24:31 -0700258status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700259 EGLDisplay dpy,
260 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700261 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700262 EGLConfig* outConfig)
263{
264 EGLConfig config = NULL;
265 EGLint numConfigs = -1, n=0;
266 eglGetConfigs(dpy, NULL, 0, &numConfigs);
267 EGLConfig* const configs = new EGLConfig[numConfigs];
268 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700269
Mathias Agopian722b98f2012-09-25 18:24:31 -0700270 if (n) {
271 if (attribute != EGL_NONE) {
272 for (int i=0 ; i<n ; i++) {
273 EGLint value = 0;
274 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
275 if (wanted == value) {
276 *outConfig = configs[i];
277 delete [] configs;
278 return NO_ERROR;
279 }
280 }
281 } else {
282 // just pick the first one
283 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700284 delete [] configs;
285 return NO_ERROR;
286 }
287 }
288 delete [] configs;
289 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290}
291
Mathias Agopian722b98f2012-09-25 18:24:31 -0700292class EGLAttributeVector {
293 struct Attribute;
294 class Adder;
295 friend class Adder;
296 KeyedVector<Attribute, EGLint> mList;
297 struct Attribute {
298 Attribute() {};
299 Attribute(EGLint v) : v(v) { }
300 EGLint v;
301 bool operator < (const Attribute& other) const {
302 // this places EGL_NONE at the end
303 EGLint lhs(v);
304 EGLint rhs(other.v);
305 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
306 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
307 return lhs < rhs;
308 }
309 };
310 class Adder {
311 friend class EGLAttributeVector;
312 EGLAttributeVector& v;
313 EGLint attribute;
314 Adder(EGLAttributeVector& v, EGLint attribute)
315 : v(v), attribute(attribute) {
316 }
317 public:
318 void operator = (EGLint value) {
319 if (attribute != EGL_NONE) {
320 v.mList.add(attribute, value);
321 }
322 }
323 operator EGLint () const { return v.mList[attribute]; }
324 };
325public:
326 EGLAttributeVector() {
327 mList.add(EGL_NONE, EGL_NONE);
328 }
329 void remove(EGLint attribute) {
330 if (attribute != EGL_NONE) {
331 mList.removeItem(attribute);
332 }
333 }
334 Adder operator [] (EGLint attribute) {
335 return Adder(*this, attribute);
336 }
337 EGLint operator [] (EGLint attribute) const {
338 return mList[attribute];
339 }
340 // cast-operator to (EGLint const*)
341 operator EGLint const* () const { return &mList.keyAt(0).v; }
342};
343
Mathias Agopiana4912602012-07-12 14:25:33 -0700344EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
345 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
346 // it is to be used with WIFI displays
347 EGLConfig config;
348 EGLint dummy;
349 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700350
Mathias Agopian722b98f2012-09-25 18:24:31 -0700351 EGLAttributeVector attribs;
352 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
353 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
354 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
355 attribs[EGL_RED_SIZE] = 8;
356 attribs[EGL_GREEN_SIZE] = 8;
357 attribs[EGL_BLUE_SIZE] = 8;
358
359 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
360 if (!err)
361 goto success;
362
363 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
364 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
365 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
366 err = selectConfigForAttribute(display, attribs,
367 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700368 if (!err)
369 goto success;
370
371 // maybe we failed because of EGL_RECORDABLE_ANDROID
372 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700373 attribs.remove(EGL_RECORDABLE_ANDROID);
374 err = selectConfigForAttribute(display, attribs,
375 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700376 if (!err)
377 goto success;
378
379 // allow less than 24-bit color; the non-gpu-accelerated emulator only
380 // supports 16-bit color
381 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700382 attribs.remove(EGL_RED_SIZE);
383 attribs.remove(EGL_GREEN_SIZE);
384 attribs.remove(EGL_BLUE_SIZE);
385 err = selectConfigForAttribute(display, attribs,
386 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700387 if (!err)
388 goto success;
389
390 // this EGL is too lame for Android
391 ALOGE("no suitable EGLConfig found, giving up");
392
393 return 0;
394
395success:
396 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700397 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700398 return config;
399}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400
Mathias Agopiana4912602012-07-12 14:25:33 -0700401EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
402 // Also create our EGLContext
403 EGLint contextAttributes[] = {
404#ifdef EGL_IMG_context_priority
405#ifdef HAS_CONTEXT_PRIORITY
406#warning "using EGL_IMG_context_priority"
407 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
408#endif
409#endif
410 EGL_NONE, EGL_NONE
411 };
412 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
413 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
414 return ctxt;
415}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416
Mathias Agopiancde87a32012-09-13 14:09:01 -0700417void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700418 GLExtensions& extensions(GLExtensions::getInstance());
419 extensions.initWithGLStrings(
420 glGetString(GL_VENDOR),
421 glGetString(GL_RENDERER),
422 glGetString(GL_VERSION),
423 glGetString(GL_EXTENSIONS),
424 eglQueryString(display, EGL_VENDOR),
425 eglQueryString(display, EGL_VERSION),
426 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700427
Mathias Agopiana4912602012-07-12 14:25:33 -0700428 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
429 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700430
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700432 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 glShadeModel(GL_FLAT);
435 glDisable(GL_DITHER);
436 glDisable(GL_CULL_FACE);
437
Mathias Agopiana4912602012-07-12 14:25:33 -0700438 struct pack565 {
439 inline uint16_t operator() (int r, int g, int b) const {
440 return (r<<11)|(g<<5)|b;
441 }
442 } pack565;
443
Jamie Gennis9575f602011-10-07 14:51:16 -0700444 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
445 glGenTextures(1, &mProtectedTexName);
446 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
447 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
448 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
449 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
450 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
451 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
452 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453
Mathias Agopiana4912602012-07-12 14:25:33 -0700454 // print some debugging info
455 EGLint r,g,b,a;
456 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
457 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
458 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
459 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
460 ALOGI("EGL informations:");
461 ALOGI("vendor : %s", extensions.getEglVendor());
462 ALOGI("version : %s", extensions.getEglVersion());
463 ALOGI("extensions: %s", extensions.getEglExtension());
464 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
465 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
466 ALOGI("OpenGL ES informations:");
467 ALOGI("vendor : %s", extensions.getVendor());
468 ALOGI("renderer : %s", extensions.getRenderer());
469 ALOGI("version : %s", extensions.getVersion());
470 ALOGI("extensions: %s", extensions.getExtension());
471 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
472 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
473}
474
Mathias Agopiana4912602012-07-12 14:25:33 -0700475status_t SurfaceFlinger::readyToRun()
476{
477 ALOGI( "SurfaceFlinger's main thread ready to run. "
478 "Initializing graphics H/W...");
479
Jesse Hall692c7232012-11-08 15:41:56 -0800480 Mutex::Autolock _l(mStateLock);
481
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700482 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700483 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
484 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700485
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700486 // Initialize the H/W composer object. There may or may not be an
487 // actual hardware composer underneath.
488 mHwc = new HWComposer(this,
489 *static_cast<HWComposer::EventHandler *>(this));
490
Mathias Agopiana4912602012-07-12 14:25:33 -0700491 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700492 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700493 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
494 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700495
Mathias Agopianed985572013-03-22 00:24:39 -0700496 // figure out which format we got
497 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
498 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
499
Mathias Agopianda27af92012-09-13 18:17:13 -0700500 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
501 "couldn't create EGLContext");
502
Mathias Agopiancde87a32012-09-13 14:09:01 -0700503 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700504 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700505 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700506 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700507 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700508 // All non-virtual displays are currently considered secure.
509 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800510 createBuiltinDisplayLocked(type);
511 wp<IBinder> token = mBuiltinDisplays[i];
512
Mathias Agopianf5a33922012-09-19 18:16:22 -0700513 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700514 type, allocateHwcDisplayId(type), isSecure, token,
515 new FramebufferSurface(*mHwc, i),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700516 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700517 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700518 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700519 // for displays other than the main display, so we always
520 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700521 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700522 hw->acquireScreen();
523 }
524 mDisplays.add(token, hw);
525 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700526 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700527
Mathias Agopianf5a33922012-09-19 18:16:22 -0700528 // we need a GL context current in a few places, when initializing
529 // OpenGL ES (see below), or creating a layer,
530 // or when a texture is (asynchronously) destroyed, and for that
531 // we need a valid surface, so it's convenient to use the main display
532 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700533 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700534
Mathias Agopiana4912602012-07-12 14:25:33 -0700535 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700536 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
537 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800538
Mathias Agopian028508c2012-07-25 21:12:12 -0700539 // start the EventThread
540 mEventThread = new EventThread(this);
541 mEventQueue.setEventThread(mEventThread);
542
Mathias Agopian92a979a2012-08-02 18:32:23 -0700543 // initialize our drawing state
544 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700545
Mathias Agopiancde87a32012-09-13 14:09:01 -0700546
Mathias Agopiana4912602012-07-12 14:25:33 -0700547 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800548 mReadyToRunBarrier.open();
549
Andy McFadden13a082e2012-08-24 10:16:42 -0700550 // set initial conditions (e.g. unblank default device)
551 initializeDisplays();
552
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700553 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700554 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700555
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 return NO_ERROR;
557}
558
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700559int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
560 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
561 type : mHwc->allocateDisplayId();
562}
563
Mathias Agopiana67e4182012-06-19 17:26:12 -0700564void SurfaceFlinger::startBootAnim() {
565 // start boot animation
566 property_set("service.bootanim.exit", "0");
567 property_set("ctl.start", "bootanim");
568}
569
Mathias Agopiana4912602012-07-12 14:25:33 -0700570uint32_t SurfaceFlinger::getMaxTextureSize() const {
571 return mMaxTextureSize;
572}
573
574uint32_t SurfaceFlinger::getMaxViewportDims() const {
575 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
576 mMaxViewportDims[0] : mMaxViewportDims[1];
577}
578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800580
Jamie Gennis582270d2011-08-17 18:19:00 -0700581bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800582 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800583 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800584 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700585 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800586}
587
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700588status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800589 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700590 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800591 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700592 type = i;
593 break;
594 }
595 }
596
597 if (type < 0) {
598 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700599 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700600
601 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700602 float xdpi = hwc.getDpiX(type);
603 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700604
605 // TODO: Not sure if display density should handled by SF any longer
606 class Density {
607 static int getDensityFromProperty(char const* propName) {
608 char property[PROPERTY_VALUE_MAX];
609 int density = 0;
610 if (property_get(propName, property, NULL) > 0) {
611 density = atoi(property);
612 }
613 return density;
614 }
615 public:
616 static int getEmuDensity() {
617 return getDensityFromProperty("qemu.sf.lcd_density"); }
618 static int getBuildDensity() {
619 return getDensityFromProperty("ro.sf.lcd_density"); }
620 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700621
622 if (type == DisplayDevice::DISPLAY_PRIMARY) {
623 // The density of the device is provided by a build property
624 float density = Density::getBuildDensity() / 160.0f;
625 if (density == 0) {
626 // the build doesn't provide a density -- this is wrong!
627 // use xdpi instead
628 ALOGE("ro.sf.lcd_density must be defined as a build property");
629 density = xdpi / 160.0f;
630 }
631 if (Density::getEmuDensity()) {
632 // if "qemu.sf.lcd_density" is specified, it overrides everything
633 xdpi = ydpi = density = Density::getEmuDensity();
634 density /= 160.0f;
635 }
636 info->density = density;
637
638 // TODO: this needs to go away (currently needed only by webkit)
639 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
640 info->orientation = hw->getOrientation();
641 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
642 } else {
643 // TODO: where should this value come from?
644 static const int TV_DENSITY = 213;
645 info->density = TV_DENSITY / 160.0f;
646 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700647 }
648
Mathias Agopian1604f772012-09-18 21:54:42 -0700649 info->w = hwc.getWidth(type);
650 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700651 info->xdpi = xdpi;
652 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700653 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700654
655 // All non-virtual displays are currently considered secure.
656 info->secure = true;
657
Mathias Agopian888c8222012-08-04 21:10:38 -0700658 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700659}
660
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800661// ----------------------------------------------------------------------------
662
663sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800664 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700665}
666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800668
669void SurfaceFlinger::waitForEvent() {
670 mEventQueue.waitMessage();
671}
672
673void SurfaceFlinger::signalTransaction() {
674 mEventQueue.invalidate();
675}
676
677void SurfaceFlinger::signalLayerUpdate() {
678 mEventQueue.invalidate();
679}
680
681void SurfaceFlinger::signalRefresh() {
682 mEventQueue.refresh();
683}
684
685status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
686 nsecs_t reltime, uint32_t flags) {
687 return mEventQueue.postMessage(msg, reltime);
688}
689
690status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
691 nsecs_t reltime, uint32_t flags) {
692 status_t res = mEventQueue.postMessage(msg, reltime);
693 if (res == NO_ERROR) {
694 msg->wait();
695 }
696 return res;
697}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698
Mathias Agopian4fec8732012-06-29 14:12:52 -0700699bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 return true;
702}
703
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700704void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700705 if (mEventThread == NULL) {
706 // This is a temporary workaround for b/7145521. A non-null pointer
707 // does not mean EventThread has finished initializing, so this
708 // is not a correct fix.
709 ALOGW("WARNING: EventThread not started, ignoring vsync");
710 return;
711 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700712 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
713 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700714 mEventThread->onVSyncReceived(type, timestamp);
715 }
716}
717
718void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
719 if (mEventThread == NULL) {
720 // This is a temporary workaround for b/7145521. A non-null pointer
721 // does not mean EventThread has finished initializing, so this
722 // is not a correct fix.
723 ALOGW("WARNING: EventThread not started, ignoring hotplug");
724 return;
725 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700726
Mathias Agopian148994e2012-09-19 17:31:36 -0700727 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700728 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800729 if (connected) {
730 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700731 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800732 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
733 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700734 }
735 setTransactionFlags(eDisplayTransactionNeeded);
736
Andy McFadden9e9689c2012-10-10 18:17:51 -0700737 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700738 }
Mathias Agopian86303202012-07-24 22:46:10 -0700739}
740
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700741void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
742 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700743}
744
Mathias Agopian4fec8732012-06-29 14:12:52 -0700745void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800746 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800747 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700748 case MessageQueue::TRANSACTION:
749 handleMessageTransaction();
750 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700751 case MessageQueue::INVALIDATE:
752 handleMessageTransaction();
753 handleMessageInvalidate();
754 signalRefresh();
755 break;
756 case MessageQueue::REFRESH:
757 handleMessageRefresh();
758 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800759 }
760}
761
Mathias Agopian4fec8732012-06-29 14:12:52 -0700762void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700763 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700764 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700765 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700766 }
767}
768
769void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700771 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700772}
773
774void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700775 ATRACE_CALL();
776 preComposition();
777 rebuildLayerStacks();
778 setUpHWComposer();
779 doDebugFlashRegions();
780 doComposition();
781 postComposition();
782}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783
Mathias Agopiancd60f992012-08-16 16:28:27 -0700784void SurfaceFlinger::doDebugFlashRegions()
785{
786 // is debugging enabled
787 if (CC_LIKELY(!mDebugRegion))
788 return;
789
790 const bool repaintEverything = mRepaintEverything;
791 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
792 const sp<DisplayDevice>& hw(mDisplays[dpy]);
793 if (hw->canDraw()) {
794 // transform the dirty region into this screen's coordinate space
795 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
796 if (!dirtyRegion.isEmpty()) {
797 // redraw the whole screen
798 doComposeSurfaces(hw, Region(hw->bounds()));
799
800 // and draw the dirty region
801 glDisable(GL_TEXTURE_EXTERNAL_OES);
802 glDisable(GL_TEXTURE_2D);
803 glDisable(GL_BLEND);
804 glColor4f(1, 0, 1, 1);
805 const int32_t height = hw->getHeight();
806 Region::const_iterator it = dirtyRegion.begin();
807 Region::const_iterator const end = dirtyRegion.end();
808 while (it != end) {
809 const Rect& r = *it++;
810 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800811 { (GLfloat) r.left, (GLfloat) (height - r.top) },
812 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
813 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
814 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700815 };
816 glVertexPointer(2, GL_FLOAT, 0, vertices);
817 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
818 }
819 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700820 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700821 }
822 }
823 }
824
825 postFramebuffer();
826
827 if (mDebugRegion > 1) {
828 usleep(mDebugRegion * 1000);
829 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700830
831 HWComposer& hwc(getHwComposer());
832 if (hwc.initCheck() == NO_ERROR) {
833 status_t err = hwc.prepare();
834 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
835 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700836}
837
838void SurfaceFlinger::preComposition()
839{
840 bool needExtraInvalidate = false;
841 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
842 const size_t count = currentLayers.size();
843 for (size_t i=0 ; i<count ; i++) {
844 if (currentLayers[i]->onPreComposition()) {
845 needExtraInvalidate = true;
846 }
847 }
848 if (needExtraInvalidate) {
849 signalLayerUpdate();
850 }
851}
852
853void SurfaceFlinger::postComposition()
854{
855 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
856 const size_t count = currentLayers.size();
857 for (size_t i=0 ; i<count ; i++) {
858 currentLayers[i]->onPostComposition();
859 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800860
861 if (mAnimCompositionPending) {
862 mAnimCompositionPending = false;
863
864 const HWComposer& hwc = getHwComposer();
865 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800866 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800867 mAnimFrameTracker.setActualPresentFence(presentFence);
868 } else {
869 // The HWC doesn't support present fences, so use the refresh
870 // timestamp instead.
871 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
872 mAnimFrameTracker.setActualPresentTime(presentTime);
873 }
874 mAnimFrameTracker.advanceFrame();
875 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700876}
877
878void SurfaceFlinger::rebuildLayerStacks() {
879 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700880 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700882 mVisibleRegionsDirty = false;
883 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700884
Mathias Agopian87baae12012-07-31 12:38:26 -0700885 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700886 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700887 Region opaqueRegion;
888 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700890 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700891 const Transform& tr(hw->getTransform());
892 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700893 if (hw->canDraw()) {
894 SurfaceFlinger::computeVisibleRegions(currentLayers,
895 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700896
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700897 const size_t count = currentLayers.size();
898 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700900 const Layer::State& s(layer->drawingState());
901 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700902 Region drawRegion(tr.transform(
903 layer->visibleNonTransparentRegion));
904 drawRegion.andSelf(bounds);
905 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700906 layersSortedByZ.add(layer);
907 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700908 }
909 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700910 }
Mathias Agopian42977342012-08-05 00:40:46 -0700911 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700912 hw->undefinedRegion.set(bounds);
913 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
914 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700915 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700916 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700917}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700918
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700920 HWComposer& hwc(getHwComposer());
921 if (hwc.initCheck() == NO_ERROR) {
922 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700923 if (CC_UNLIKELY(mHwWorkListDirty)) {
924 mHwWorkListDirty = false;
925 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
926 sp<const DisplayDevice> hw(mDisplays[dpy]);
927 const int32_t id = hw->getHwcDisplayId();
928 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800929 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700930 hw->getVisibleLayersSortedByZ());
931 const size_t count = currentLayers.size();
932 if (hwc.createWorkList(id, count) == NO_ERROR) {
933 HWComposer::LayerListIterator cur = hwc.begin(id);
934 const HWComposer::LayerListIterator end = hwc.end(id);
935 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800936 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700937 layer->setGeometry(hw, *cur);
938 if (mDebugDisableHWC || mDebugRegion) {
939 cur->setSkip(true);
940 }
941 }
942 }
943 }
944 }
945 }
946
947 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700948 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700949 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700950 const int32_t id = hw->getHwcDisplayId();
951 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800952 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700953 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700954 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700955 HWComposer::LayerListIterator cur = hwc.begin(id);
956 const HWComposer::LayerListIterator end = hwc.end(id);
957 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
958 /*
959 * update the per-frame h/w composer data for each layer
960 * and build the transparent region of the FB
961 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800962 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700963 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700964 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700966 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700967
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968 status_t err = hwc.prepare();
969 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
970 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700971}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700972
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973void SurfaceFlinger::doComposition() {
974 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700975 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700976 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700977 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700978 if (hw->canDraw()) {
979 // transform the dirty region into this screen's coordinate space
980 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800981
982 // repaint the framebuffer (if needed)
983 doDisplayComposition(hw, dirtyRegion);
984
Mathias Agopiancd60f992012-08-16 16:28:27 -0700985 hw->dirtyRegion.clear();
986 hw->flip(hw->swapRegion);
987 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700988 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700989 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700990 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700991 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700992 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700993}
994
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995void SurfaceFlinger::postFramebuffer()
996{
Mathias Agopian841cde52012-03-01 15:44:37 -0800997 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800998
Mathias Agopiana44b0412011-10-16 18:46:35 -0700999 const nsecs_t now = systemTime();
1000 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001001
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001002 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001003 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001004 if (!hwc.supportsFramebufferTarget()) {
1005 // EGL spec says:
1006 // "surface must be bound to the calling thread's current context,
1007 // for the current rendering API."
1008 DisplayDevice::makeCurrent(mEGLDisplay,
1009 getDefaultDisplayDevice(), mEGLContext);
1010 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001011 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001012 }
1013
Mathias Agopian92a979a2012-08-02 18:32:23 -07001014 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001015 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001016 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001017 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001018 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001019 int32_t id = hw->getHwcDisplayId();
1020 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001021 HWComposer::LayerListIterator cur = hwc.begin(id);
1022 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001023 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001024 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001025 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001026 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001027 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001028 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001029 }
Jesse Hallef194142012-06-14 14:45:17 -07001030 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001031 }
1032
Mathias Agopiana44b0412011-10-16 18:46:35 -07001033 mLastSwapBufferTime = systemTime() - now;
1034 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035}
1036
Mathias Agopian87baae12012-07-31 12:38:26 -07001037void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038{
Mathias Agopian841cde52012-03-01 15:44:37 -08001039 ATRACE_CALL();
1040
Mathias Agopianca4d3602011-05-19 15:38:14 -07001041 Mutex::Autolock _l(mStateLock);
1042 const nsecs_t now = systemTime();
1043 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044
Mathias Agopianca4d3602011-05-19 15:38:14 -07001045 // Here we're guaranteed that some transaction flags are set
1046 // so we can call handleTransactionLocked() unconditionally.
1047 // We call getTransactionFlags(), which will also clear the flags,
1048 // with mStateLock held to guarantee that mCurrentState won't change
1049 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001050
Mathias Agopiane57f2922012-08-09 16:29:12 -07001051 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001052 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001053
Mathias Agopianca4d3602011-05-19 15:38:14 -07001054 mLastTransactionTime = systemTime() - now;
1055 mDebugInTransaction = 0;
1056 invalidateHwcGeometry();
1057 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001058}
1059
Mathias Agopian87baae12012-07-31 12:38:26 -07001060void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001061{
1062 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 const size_t count = currentLayers.size();
1064
1065 /*
1066 * Traversal of the children
1067 * (perform the transaction for each of them if needed)
1068 */
1069
Mathias Agopian3559b072012-08-15 13:46:03 -07001070 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1074 if (!trFlags) continue;
1075
1076 const uint32_t flags = layer->doTransaction(0);
1077 if (flags & Layer::eVisibleRegion)
1078 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 }
1080 }
1081
1082 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001083 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 */
1085
Mathias Agopiane57f2922012-08-09 16:29:12 -07001086 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001087 // here we take advantage of Vector's copy-on-write semantics to
1088 // improve performance by skipping the transaction entirely when
1089 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001090 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1091 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001092 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001094 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001095 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001096
1097 // find the displays that were removed
1098 // (ie: in drawing state but not in current state)
1099 // also handle displays that changed
1100 // (ie: displays that are in both lists)
1101 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001102 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1103 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001104 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001105 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001106 // Call makeCurrent() on the primary display so we can
1107 // be sure that nothing associated with this display
1108 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001109 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
1110 DisplayDevice::makeCurrent(mEGLDisplay, defaultDisplay, mEGLContext);
1111 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1112 if (hw != NULL)
1113 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001114 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1115 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001116 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001117 } else {
1118 ALOGW("trying to remove the main display");
1119 }
1120 } else {
1121 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001122 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001123 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001124 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001125 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001126 // recreating the DisplayDevice, so we just remove it
1127 // from the drawing state, so that it get re-added
1128 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001129 sp<DisplayDevice> hw(getDisplayDevice(display));
1130 if (hw != NULL)
1131 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001132 mDisplays.removeItem(display);
1133 mDrawingState.displays.removeItemsAt(i);
1134 dc--; i--;
1135 // at this point we must loop to the next item
1136 continue;
1137 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001138
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001139 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001140 if (disp != NULL) {
1141 if (state.layerStack != draw[i].layerStack) {
1142 disp->setLayerStack(state.layerStack);
1143 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001144 if ((state.orientation != draw[i].orientation)
1145 || (state.viewport != draw[i].viewport)
1146 || (state.frame != draw[i].frame))
1147 {
1148 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001149 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001150 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001151 }
1152 }
1153 }
1154
1155 // find displays that were added
1156 // (ie: in current state but not in drawing state)
1157 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001158 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001159 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001160
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001161 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001162 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001163 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001164 // Virtual displays without a surface are dormant:
1165 // they have external state (layer stack, projection,
1166 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001167 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001168 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001169 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001170 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001171 state.displayName);
1172 }
1173 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001174 ALOGE_IF(state.surface!=NULL,
1175 "adding a supported display, but rendering "
1176 "surface is provided (%p), ignoring it",
1177 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001178 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001179 // for supported (by hwc) displays we provide our
1180 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001181 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001182 }
1183
1184 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001185 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001186 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001187 state.type, hwcDisplayId, state.isSecure,
1188 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001189 hw->setLayerStack(state.layerStack);
1190 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001191 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001192 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001193 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001194 if (state.isVirtualDisplay()) {
1195 if (hwcDisplayId >= 0) {
1196 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1197 hw->getWidth(), hw->getHeight(),
1198 hw->getFormat());
1199 }
1200 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001201 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001202 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001203 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001204 }
1205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208
Mathias Agopian84300952012-11-21 16:02:13 -08001209 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1210 // The transform hint might have changed for some layers
1211 // (either because a display has changed, or because a layer
1212 // as changed).
1213 //
1214 // Walk through all the layers in currentLayers,
1215 // and update their transform hint.
1216 //
1217 // If a layer is visible only on a single display, then that
1218 // display is used to calculate the hint, otherwise we use the
1219 // default display.
1220 //
1221 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1222 // the hint is set before we acquire a buffer from the surface texture.
1223 //
1224 // NOTE: layer transactions have taken place already, so we use their
1225 // drawing state. However, SurfaceFlinger's own transaction has not
1226 // happened yet, so we must use the current state layer list
1227 // (soon to become the drawing state list).
1228 //
1229 sp<const DisplayDevice> disp;
1230 uint32_t currentlayerStack = 0;
1231 for (size_t i=0; i<count; i++) {
1232 // NOTE: we rely on the fact that layers are sorted by
1233 // layerStack first (so we don't have to traverse the list
1234 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001235 const sp<Layer>& layer(currentLayers[i]);
1236 uint32_t layerStack = layer->drawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001237 if (i==0 || currentlayerStack != layerStack) {
1238 currentlayerStack = layerStack;
1239 // figure out if this layerstack is mirrored
1240 // (more than one display) if so, pick the default display,
1241 // if not, pick the only display it's on.
1242 disp.clear();
1243 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1244 sp<const DisplayDevice> hw(mDisplays[dpy]);
1245 if (hw->getLayerStack() == currentlayerStack) {
1246 if (disp == NULL) {
1247 disp = hw;
1248 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001249 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001250 break;
1251 }
1252 }
1253 }
1254 }
Chet Haase91d25932013-04-11 15:24:55 -07001255 if (disp == NULL) {
1256 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1257 // redraw after transform hint changes. See bug 8508397.
1258
1259 // could be null when this layer is using a layerStack
1260 // that is not visible on any display. Also can occur at
1261 // screen off/on times.
1262 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001263 }
Chet Haase91d25932013-04-11 15:24:55 -07001264 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001265 }
1266 }
1267
1268
Mathias Agopian3559b072012-08-15 13:46:03 -07001269 /*
1270 * Perform our own transaction if needed
1271 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001272
Mathias Agopiancd60f992012-08-16 16:28:27 -07001273 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1274 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001275 // layers have been added
1276 mVisibleRegionsDirty = true;
1277 }
1278
1279 // some layers might have been removed, so
1280 // we need to update the regions they're exposing.
1281 if (mLayersRemoved) {
1282 mLayersRemoved = false;
1283 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001284 const size_t count = previousLayers.size();
1285 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 const sp<Layer>& layer(previousLayers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001287 if (currentLayers.indexOf(layer) < 0) {
1288 // this layer is not visible anymore
1289 // TODO: we could traverse the tree from front to back and
1290 // compute the actual visible region
1291 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001292 const Layer::State& s(layer->drawingState());
1293 Region visibleReg = s.transform.transform(
1294 Region(Rect(s.active.w, s.active.h)));
1295 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001296 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 }
1299
1300 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001301}
1302
1303void SurfaceFlinger::commitTransaction()
1304{
1305 if (!mLayersPendingRemoval.isEmpty()) {
1306 // Notify removed layers now that they can't be drawn from
1307 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1308 mLayersPendingRemoval[i]->onRemoved();
1309 }
1310 mLayersPendingRemoval.clear();
1311 }
1312
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001313 // If this transaction is part of a window animation then the next frame
1314 // we composite should be considered an animation as well.
1315 mAnimCompositionPending = mAnimTransactionPending;
1316
Mathias Agopian4fec8732012-06-29 14:12:52 -07001317 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001318 mTransactionPending = false;
1319 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001320 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321}
1322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001324 const LayerVector& currentLayers, uint32_t layerStack,
1325 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326{
Mathias Agopian841cde52012-03-01 15:44:37 -08001327 ATRACE_CALL();
1328
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 Region aboveOpaqueLayers;
1330 Region aboveCoveredLayers;
1331 Region dirty;
1332
Mathias Agopian87baae12012-07-31 12:38:26 -07001333 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334
1335 size_t i = currentLayers.size();
1336 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001337 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338
1339 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001340 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001341
Jesse Hall01e29052013-02-19 16:13:35 -08001342 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001343 if (s.layerStack != layerStack)
1344 continue;
1345
Mathias Agopianab028732010-03-16 16:41:46 -07001346 /*
1347 * opaqueRegion: area of a surface that is fully opaque.
1348 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001350
1351 /*
1352 * visibleRegion: area of a surface that is visible on screen
1353 * and not fully transparent. This is essentially the layer's
1354 * footprint minus the opaque regions above it.
1355 * Areas covered by a translucent surface are considered visible.
1356 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001358
1359 /*
1360 * coveredRegion: area of a surface that is covered by all
1361 * visible regions above it (which includes the translucent areas).
1362 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001364
Jesse Halla8026d22012-09-25 13:25:04 -07001365 /*
1366 * transparentRegion: area of a surface that is hinted to be completely
1367 * transparent. This is only used to tell when the layer has no visible
1368 * non-transparent regions and can be removed from the layer list. It
1369 * does not affect the visibleRegion of this layer or any layers
1370 * beneath it. The hint may not be correct if apps don't respect the
1371 * SurfaceView restrictions (which, sadly, some don't).
1372 */
1373 Region transparentRegion;
1374
Mathias Agopianab028732010-03-16 16:41:46 -07001375
1376 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001377 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001378 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001379 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001381 if (!visibleRegion.isEmpty()) {
1382 // Remove the transparent area from the visible region
1383 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001384 const Transform tr(s.transform);
1385 if (tr.transformed()) {
1386 if (tr.preserveRects()) {
1387 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001388 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001389 } else {
1390 // transformation too complex, can't do the
1391 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001392 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001393 }
1394 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001395 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001396 }
Mathias Agopianab028732010-03-16 16:41:46 -07001397 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398
Mathias Agopianab028732010-03-16 16:41:46 -07001399 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001400 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001401 if (s.alpha==255 && !translucent &&
1402 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1403 // the opaque region is the layer's footprint
1404 opaqueRegion = visibleRegion;
1405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 }
1407 }
1408
Mathias Agopianab028732010-03-16 16:41:46 -07001409 // Clip the covered region to the visible region
1410 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1411
1412 // Update aboveCoveredLayers for next (lower) layer
1413 aboveCoveredLayers.orSelf(visibleRegion);
1414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 // subtract the opaque region covered by the layers above us
1416 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417
1418 // compute this layer's dirty region
1419 if (layer->contentDirty) {
1420 // we need to invalidate the whole region
1421 dirty = visibleRegion;
1422 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001423 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 layer->contentDirty = false;
1425 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001426 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001427 * the exposed region consists of two components:
1428 * 1) what's VISIBLE now and was COVERED before
1429 * 2) what's EXPOSED now less what was EXPOSED before
1430 *
1431 * note that (1) is conservative, we start with the whole
1432 * visible region but only keep what used to be covered by
1433 * something -- which mean it may have been exposed.
1434 *
1435 * (2) handles areas that were not covered by anything but got
1436 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001437 */
Mathias Agopianab028732010-03-16 16:41:46 -07001438 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001439 const Region oldVisibleRegion = layer->visibleRegion;
1440 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001441 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1442 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444 dirty.subtractSelf(aboveOpaqueLayers);
1445
1446 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001447 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448
Mathias Agopianab028732010-03-16 16:41:46 -07001449 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001451
Jesse Halla8026d22012-09-25 13:25:04 -07001452 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453 layer->setVisibleRegion(visibleRegion);
1454 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001455 layer->setVisibleNonTransparentRegion(
1456 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457 }
1458
Mathias Agopian87baae12012-07-31 12:38:26 -07001459 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460}
1461
Mathias Agopian87baae12012-07-31 12:38:26 -07001462void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1463 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001464 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001465 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1466 if (hw->getLayerStack() == layerStack) {
1467 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001468 }
1469 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001470}
1471
1472void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001474 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475
Mathias Agopian4fec8732012-06-29 14:12:52 -07001476 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001477 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001478 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001479 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001480 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001481 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001482 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001483 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001484 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001485
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001486 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487}
1488
Mathias Agopianad456f92011-01-13 17:53:01 -08001489void SurfaceFlinger::invalidateHwcGeometry()
1490{
1491 mHwWorkListDirty = true;
1492}
1493
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001494
Mathias Agopiancd60f992012-08-16 16:28:27 -07001495void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001496 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497{
Mathias Agopian87baae12012-07-31 12:38:26 -07001498 Region dirtyRegion(inDirtyRegion);
1499
Mathias Agopianb8a55602009-06-26 19:06:36 -07001500 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001501 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502
Mathias Agopian42977342012-08-05 00:40:46 -07001503 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001504 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001505 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1506 // takes a rectangle, we must make sure to update that whole
1507 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001508 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001509 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001510 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001511 // We need to redraw the rectangle that will be updated
1512 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001513 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001514 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001515 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001516 } else {
1517 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001518 dirtyRegion.set(hw->bounds());
1519 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 }
1521 }
1522
Mathias Agopiancd60f992012-08-16 16:28:27 -07001523 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001525 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001526 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001527
1528 // swap buffers (presentation)
1529 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530}
1531
Mathias Agopiancd60f992012-08-16 16:28:27 -07001532void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001533{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001534 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001535 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001536 HWComposer::LayerListIterator cur = hwc.begin(id);
1537 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001538
Mathias Agopian85d751c2012-08-29 16:59:24 -07001539 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1540 if (hasGlesComposition) {
Michael Chockc8c71092013-03-04 15:15:46 -08001541 if (!DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext)) {
1542 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1543 hw->getDisplayName().string());
1544 return;
1545 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001546
1547 // set the frame buffer
1548 glMatrixMode(GL_MODELVIEW);
1549 glLoadIdentity();
1550
1551 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001552 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001553 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001554 // when using overlays, we assume a fully transparent framebuffer
1555 // NOTE: we could reduce how much we need to clear, for instance
1556 // remove where there are opaque FB layers. however, on some
1557 // GPUs doing a "clean slate" glClear might be more efficient.
1558 // We'll revisit later if needed.
1559 glClearColor(0, 0, 0, 0);
1560 glClear(GL_COLOR_BUFFER_BIT);
1561 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001562 // we start with the whole screen area
1563 const Region bounds(hw->getBounds());
1564
1565 // we remove the scissor part
1566 // we're left with the letterbox region
1567 // (common case is that letterbox ends-up being empty)
1568 const Region letterbox(bounds.subtract(hw->getScissor()));
1569
1570 // compute the area to clear
1571 Region region(hw->undefinedRegion.merge(letterbox));
1572
1573 // but limit it to the dirty region
1574 region.andSelf(dirty);
1575
Mathias Agopianb9494d52012-04-18 02:28:45 -07001576 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001577 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001578 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001579 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001580 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001581 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001582
Mathias Agopian766dc492012-10-30 18:08:06 -07001583 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1584 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001585 // scissor on the main display. It should never be needed
1586 // anyways (though in theory it could since the API allows it).
1587 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001588 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001589 if (scissor != bounds) {
1590 // scissor doesn't match the screen's dimensions, so we
1591 // need to clear everything outside of it and enable
1592 // the GL scissor so we don't draw anything where we shouldn't
1593 const GLint height = hw->getHeight();
1594 glScissor(scissor.left, height - scissor.bottom,
1595 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001596 // enable scissor for this frame
1597 glEnable(GL_SCISSOR_TEST);
1598 }
1599 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001600 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001601
Mathias Agopian85d751c2012-08-29 16:59:24 -07001602 /*
1603 * and then, render the layers targeted at the framebuffer
1604 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001605
Mathias Agopian13127d82013-03-05 17:47:11 -08001606 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001607 const size_t count = layers.size();
1608 const Transform& tr = hw->getTransform();
1609 if (cur != end) {
1610 // we're using h/w composer
1611 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001612 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001613 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001614 if (!clip.isEmpty()) {
1615 switch (cur->getCompositionType()) {
1616 case HWC_OVERLAY: {
1617 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1618 && i
1619 && layer->isOpaque()
1620 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001621 // never clear the very first layer since we're
1622 // guaranteed the FB is already cleared
1623 layer->clearWithOpenGL(hw, clip);
1624 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001625 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001626 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001627 case HWC_FRAMEBUFFER: {
1628 layer->draw(hw, clip);
1629 break;
1630 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001631 case HWC_FRAMEBUFFER_TARGET: {
1632 // this should not happen as the iterator shouldn't
1633 // let us get there.
1634 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1635 break;
1636 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001637 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001638 }
1639 layer->setAcquireFence(hw, *cur);
1640 }
1641 } else {
1642 // we're not using h/w composer
1643 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001644 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001645 const Region clip(dirty.intersect(
1646 tr.transform(layer->visibleRegion)));
1647 if (!clip.isEmpty()) {
1648 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001649 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001650 }
1651 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001652
1653 // disable scissor at the end of the frame
1654 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655}
1656
Mathias Agopian55801e42012-08-27 18:54:24 -07001657void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1658 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001660 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001661 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001662 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001663 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001664
Mathias Agopian55801e42012-08-27 18:54:24 -07001665 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001666 Region::const_iterator it = region.begin();
1667 Region::const_iterator const end = region.end();
1668 while (it != end) {
1669 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001670 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001671 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1672 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1673 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1674 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001675 };
1676 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001677 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1678 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679}
1680
Mathias Agopianac9fa422013-02-11 16:40:36 -08001681void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1682 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001683 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001684 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001685{
Mathias Agopian96f08192010-06-02 23:28:45 -07001686 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001687 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001688
Mathias Agopian96f08192010-06-02 23:28:45 -07001689 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001690 Mutex::Autolock _l(mStateLock);
1691 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001692 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001693}
1694
Mathias Agopian13127d82013-03-05 17:47:11 -08001695status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001696{
1697 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001698 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001700 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001701 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001702 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 return NO_ERROR;
1704 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001705 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706}
1707
Mathias Agopiandea20b12011-05-03 17:04:02 -07001708uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1709{
1710 return android_atomic_release_load(&mTransactionFlags);
1711}
1712
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1714{
1715 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1716}
1717
Mathias Agopianbb641242010-05-18 17:06:55 -07001718uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719{
1720 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1721 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001722 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723 }
1724 return old;
1725}
1726
Mathias Agopian8b33f032012-07-24 20:43:54 -07001727void SurfaceFlinger::setTransactionState(
1728 const Vector<ComposerState>& state,
1729 const Vector<DisplayState>& displays,
1730 uint32_t flags)
1731{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001732 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001733 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001734 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001735
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001736 if (flags & eAnimation) {
1737 // For window updates that are part of an animation we must wait for
1738 // previous animation "frames" to be handled.
1739 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001740 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001741 if (CC_UNLIKELY(err != NO_ERROR)) {
1742 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001743 // caller after a few seconds.
1744 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1745 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001746 mAnimTransactionPending = false;
1747 break;
1748 }
1749 }
1750 }
1751
Mathias Agopiane57f2922012-08-09 16:29:12 -07001752 size_t count = displays.size();
1753 for (size_t i=0 ; i<count ; i++) {
1754 const DisplayState& s(displays[i]);
1755 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001756 }
1757
Mathias Agopiane57f2922012-08-09 16:29:12 -07001758 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001759 for (size_t i=0 ; i<count ; i++) {
1760 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001761 // Here we need to check that the interface we're given is indeed
1762 // one of our own. A malicious client could give us a NULL
1763 // IInterface, or one of its own or even one of our own but a
1764 // different type. All these situations would cause us to crash.
1765 //
1766 // NOTE: it would be better to use RTTI as we could directly check
1767 // that we have a Client*. however, RTTI is disabled in Android.
1768 if (s.client != NULL) {
1769 sp<IBinder> binder = s.client->asBinder();
1770 if (binder != NULL) {
1771 String16 desc(binder->getInterfaceDescriptor());
1772 if (desc == ISurfaceComposerClient::descriptor) {
1773 sp<Client> client( static_cast<Client *>(s.client.get()) );
1774 transactionFlags |= setClientStateLocked(client, s.state);
1775 }
1776 }
1777 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001778 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001779
Mathias Agopian386aa982011-11-07 21:58:03 -08001780 if (transactionFlags) {
1781 // this triggers the transaction
1782 setTransactionFlags(transactionFlags);
1783
1784 // if this is a synchronous transaction, wait for it to take effect
1785 // before returning.
1786 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001787 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001788 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001789 if (flags & eAnimation) {
1790 mAnimTransactionPending = true;
1791 }
1792 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001793 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1794 if (CC_UNLIKELY(err != NO_ERROR)) {
1795 // just in case something goes wrong in SF, return to the
1796 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001797 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1798 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001799 break;
1800 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001801 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802 }
1803}
1804
Mathias Agopiane57f2922012-08-09 16:29:12 -07001805uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1806{
Jesse Hall9a143922012-10-04 16:29:19 -07001807 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1808 if (dpyIdx < 0)
1809 return 0;
1810
Mathias Agopiane57f2922012-08-09 16:29:12 -07001811 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001812 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001813 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001814 const uint32_t what = s.what;
1815 if (what & DisplayState::eSurfaceChanged) {
1816 if (disp.surface->asBinder() != s.surface->asBinder()) {
1817 disp.surface = s.surface;
1818 flags |= eDisplayTransactionNeeded;
1819 }
1820 }
1821 if (what & DisplayState::eLayerStackChanged) {
1822 if (disp.layerStack != s.layerStack) {
1823 disp.layerStack = s.layerStack;
1824 flags |= eDisplayTransactionNeeded;
1825 }
1826 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001827 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001828 if (disp.orientation != s.orientation) {
1829 disp.orientation = s.orientation;
1830 flags |= eDisplayTransactionNeeded;
1831 }
1832 if (disp.frame != s.frame) {
1833 disp.frame = s.frame;
1834 flags |= eDisplayTransactionNeeded;
1835 }
1836 if (disp.viewport != s.viewport) {
1837 disp.viewport = s.viewport;
1838 flags |= eDisplayTransactionNeeded;
1839 }
1840 }
1841 }
1842 return flags;
1843}
1844
1845uint32_t SurfaceFlinger::setClientStateLocked(
1846 const sp<Client>& client,
1847 const layer_state_t& s)
1848{
1849 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001850 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001851 if (layer != 0) {
1852 const uint32_t what = s.what;
1853 if (what & layer_state_t::ePositionChanged) {
1854 if (layer->setPosition(s.x, s.y))
1855 flags |= eTraversalNeeded;
1856 }
1857 if (what & layer_state_t::eLayerChanged) {
1858 // NOTE: index needs to be calculated before we update the state
1859 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1860 if (layer->setLayer(s.z)) {
1861 mCurrentState.layersSortedByZ.removeAt(idx);
1862 mCurrentState.layersSortedByZ.add(layer);
1863 // we need traversal (state changed)
1864 // AND transaction (list changed)
1865 flags |= eTransactionNeeded|eTraversalNeeded;
1866 }
1867 }
1868 if (what & layer_state_t::eSizeChanged) {
1869 if (layer->setSize(s.w, s.h)) {
1870 flags |= eTraversalNeeded;
1871 }
1872 }
1873 if (what & layer_state_t::eAlphaChanged) {
1874 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1875 flags |= eTraversalNeeded;
1876 }
1877 if (what & layer_state_t::eMatrixChanged) {
1878 if (layer->setMatrix(s.matrix))
1879 flags |= eTraversalNeeded;
1880 }
1881 if (what & layer_state_t::eTransparentRegionChanged) {
1882 if (layer->setTransparentRegionHint(s.transparentRegion))
1883 flags |= eTraversalNeeded;
1884 }
1885 if (what & layer_state_t::eVisibilityChanged) {
1886 if (layer->setFlags(s.flags, s.mask))
1887 flags |= eTraversalNeeded;
1888 }
1889 if (what & layer_state_t::eCropChanged) {
1890 if (layer->setCrop(s.crop))
1891 flags |= eTraversalNeeded;
1892 }
1893 if (what & layer_state_t::eLayerStackChanged) {
1894 // NOTE: index needs to be calculated before we update the state
1895 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1896 if (layer->setLayerStack(s.layerStack)) {
1897 mCurrentState.layersSortedByZ.removeAt(idx);
1898 mCurrentState.layersSortedByZ.add(layer);
1899 // we need traversal (state changed)
1900 // AND transaction (list changed)
1901 flags |= eTransactionNeeded|eTraversalNeeded;
1902 }
1903 }
1904 }
1905 return flags;
1906}
1907
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001908status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001909 const String8& name,
1910 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001911 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1912 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001915 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001916 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001917 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001918 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001919 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001920
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001921 status_t result = NO_ERROR;
1922
1923 sp<Layer> layer;
1924
Mathias Agopian3165cc22012-08-08 19:42:09 -07001925 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1926 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001927 result = createNormalLayer(client,
1928 name, w, h, flags, format,
1929 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001931 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001932 result = createDimLayer(client,
1933 name, w, h, flags,
1934 handle, gbp, &layer);
1935 break;
1936 default:
1937 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938 break;
1939 }
1940
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001941 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001942 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001943 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001945 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001946}
1947
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001948status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1949 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1950 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951{
1952 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001953 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954 case PIXEL_FORMAT_TRANSPARENT:
1955 case PIXEL_FORMAT_TRANSLUCENT:
1956 format = PIXEL_FORMAT_RGBA_8888;
1957 break;
1958 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001959#ifdef NO_RGBX_8888
1960 format = PIXEL_FORMAT_RGB_565;
1961#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001962 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001963#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001964 break;
1965 }
1966
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001967#ifdef NO_RGBX_8888
1968 if (format == PIXEL_FORMAT_RGBX_8888)
1969 format = PIXEL_FORMAT_RGBA_8888;
1970#endif
1971
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001972 *outLayer = new Layer(this, client, name, w, h, flags);
1973 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1974 if (err == NO_ERROR) {
1975 *handle = (*outLayer)->getHandle();
1976 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001977 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001978
1979 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1980 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001981}
1982
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001983status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1984 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1985 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001987 *outLayer = new LayerDim(this, client, name, w, h, flags);
1988 *handle = (*outLayer)->getHandle();
1989 *gbp = (*outLayer)->getBufferQueue();
1990 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001991}
1992
Mathias Agopianac9fa422013-02-11 16:40:36 -08001993status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001994{
Mathias Agopian67106042013-03-14 19:18:13 -07001995 // called by the window manager when it wants to remove a Layer
1996 status_t err = NO_ERROR;
1997 sp<Layer> l(client->getLayerUser(handle));
1998 if (l != NULL) {
1999 err = removeLayer(l);
2000 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2001 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002002 }
2003 return err;
2004}
2005
Mathias Agopian13127d82013-03-05 17:47:11 -08002006status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002007{
Mathias Agopian67106042013-03-14 19:18:13 -07002008 // called by ~LayerCleaner() when all references to the IBinder (handle)
2009 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002010 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002011 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002012 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002013 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002014 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002015 "error removing layer=%p (%s)", l.get(), strerror(-err));
2016 }
2017 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002018}
2019
Mathias Agopianb60314a2012-04-10 22:09:54 -07002020// ---------------------------------------------------------------------------
2021
Andy McFadden13a082e2012-08-24 10:16:42 -07002022void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002023 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002024 Vector<ComposerState> state;
2025 Vector<DisplayState> displays;
2026 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002027 d.what = DisplayState::eDisplayProjectionChanged |
2028 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002029 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002030 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002031 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002032 d.frame.makeInvalid();
2033 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002034 displays.add(d);
2035 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002036 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002037}
2038
2039void SurfaceFlinger::initializeDisplays() {
2040 class MessageScreenInitialized : public MessageBase {
2041 SurfaceFlinger* flinger;
2042 public:
2043 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2044 virtual bool handler() {
2045 flinger->onInitializeDisplays();
2046 return true;
2047 }
2048 };
2049 sp<MessageBase> msg = new MessageScreenInitialized(this);
2050 postMessageAsync(msg); // we may be called from main thread, use async message
2051}
2052
2053
Mathias Agopiancde87a32012-09-13 14:09:01 -07002054void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002055 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2056 if (hw->isScreenAcquired()) {
2057 // this is expected, e.g. when power manager wakes up during boot
2058 ALOGD(" screen was previously acquired");
2059 return;
2060 }
2061
Mathias Agopian42977342012-08-05 00:40:46 -07002062 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002063 int32_t type = hw->getDisplayType();
2064 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2065 // built-in display, tell the HWC
2066 getHwComposer().acquire(type);
2067
2068 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2069 // FIXME: eventthread only knows about the main display right now
2070 mEventThread->onScreenAcquired();
2071 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002072 }
Mathias Agopian20128302012-08-13 18:32:13 -07002073 mVisibleRegionsDirty = true;
2074 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002075}
2076
Mathias Agopiancde87a32012-09-13 14:09:01 -07002077void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002078 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2079 if (!hw->isScreenAcquired()) {
2080 ALOGD(" screen was previously released");
2081 return;
2082 }
2083
2084 hw->releaseScreen();
2085 int32_t type = hw->getDisplayType();
2086 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2087 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002088 // FIXME: eventthread only knows about the main display right now
2089 mEventThread->onScreenReleased();
2090 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002091
2092 // built-in display, tell the HWC
2093 getHwComposer().release(type);
2094 }
2095 mVisibleRegionsDirty = true;
2096 // from this point on, SF will stop drawing on this display
2097}
2098
2099void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2100 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002101 SurfaceFlinger& mFlinger;
2102 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002103 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002104 MessageScreenAcquired(SurfaceFlinger& flinger,
2105 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002106 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002107 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2108 if (hw == NULL) {
2109 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2110 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2111 ALOGW("Attempt to unblank virtual display");
2112 } else {
2113 mFlinger.onScreenAcquired(hw);
2114 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002115 return true;
2116 }
2117 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002118 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2119 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002120}
2121
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002122void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002123 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002124 SurfaceFlinger& mFlinger;
2125 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002126 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002127 MessageScreenReleased(SurfaceFlinger& flinger,
2128 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002129 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002130 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2131 if (hw == NULL) {
2132 ALOGE("Attempt to blank null display %p", mDisplay.get());
2133 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2134 ALOGW("Attempt to blank virtual display");
2135 } else {
2136 mFlinger.onScreenReleased(hw);
2137 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002138 return true;
2139 }
2140 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002141 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2142 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002143}
2144
2145// ---------------------------------------------------------------------------
2146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002147status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2148{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002149 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002150
Mathias Agopianbd115332013-04-18 16:41:04 -07002151 IPCThreadState* ipc = IPCThreadState::self();
2152 const int pid = ipc->getCallingPid();
2153 const int uid = ipc->getCallingUid();
2154 if ((uid != AID_SHELL) &&
2155 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002156 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002157 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002158 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002159 // Try to get the main lock, but don't insist if we can't
2160 // (this would indicate SF is stuck, but we want to be able to
2161 // print something in dumpsys).
2162 int retry = 3;
2163 while (mStateLock.tryLock()<0 && --retry>=0) {
2164 usleep(1000000);
2165 }
2166 const bool locked(retry >= 0);
2167 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002168 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002169 "SurfaceFlinger appears to be unresponsive, "
2170 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002171 }
2172
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002173 bool dumpAll = true;
2174 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002175 size_t numArgs = args.size();
2176 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002177 if ((index < numArgs) &&
2178 (args[index] == String16("--list"))) {
2179 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002180 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002181 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002182 }
2183
2184 if ((index < numArgs) &&
2185 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002186 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002187 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002188 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002189 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002190
2191 if ((index < numArgs) &&
2192 (args[index] == String16("--latency-clear"))) {
2193 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002194 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002195 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002196 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002197 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002198
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002199 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002200 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002201 }
2202
Mathias Agopian9795c422009-08-26 16:36:26 -07002203 if (locked) {
2204 mStateLock.unlock();
2205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002206 }
2207 write(fd, result.string(), result.size());
2208 return NO_ERROR;
2209}
2210
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002212 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002213{
2214 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2215 const size_t count = currentLayers.size();
2216 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002217 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002218 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002219 }
2220}
2221
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002222void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002223 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002224{
2225 String8 name;
2226 if (index < args.size()) {
2227 name = String8(args[index]);
2228 index++;
2229 }
2230
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002231 const nsecs_t period =
2232 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2233 result.appendFormat("%lld\n", period);
2234
2235 if (name.isEmpty()) {
2236 mAnimFrameTracker.dump(result);
2237 } else {
2238 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2239 const size_t count = currentLayers.size();
2240 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002241 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002242 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002243 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002244 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002245 }
2246 }
2247}
2248
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002249void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002250 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002251{
2252 String8 name;
2253 if (index < args.size()) {
2254 name = String8(args[index]);
2255 index++;
2256 }
2257
2258 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2259 const size_t count = currentLayers.size();
2260 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002261 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002262 if (name.isEmpty() || (name == layer->getName())) {
2263 layer->clearStats();
2264 }
2265 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002266
2267 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002268}
2269
Andy McFadden4803b742012-09-24 19:07:20 -07002270/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2271{
2272 static const char* config =
2273 " [sf"
2274#ifdef NO_RGBX_8888
2275 " NO_RGBX_8888"
2276#endif
2277#ifdef HAS_CONTEXT_PRIORITY
2278 " HAS_CONTEXT_PRIORITY"
2279#endif
2280#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2281 " NEVER_DEFAULT_TO_ASYNC_MODE"
2282#endif
2283#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2284 " TARGET_DISABLE_TRIPLE_BUFFERING"
2285#endif
2286 "]";
2287 result.append(config);
2288}
2289
Mathias Agopian74d211a2013-04-22 16:55:35 +02002290void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2291 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002292{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002293 bool colorize = false;
2294 if (index < args.size()
2295 && (args[index] == String16("--color"))) {
2296 colorize = true;
2297 index++;
2298 }
2299
2300 Colorizer colorizer(colorize);
2301
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002302 // figure out if we're stuck somewhere
2303 const nsecs_t now = systemTime();
2304 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2305 const nsecs_t inTransaction(mDebugInTransaction);
2306 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2307 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2308
2309 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002310 * Dump library configuration.
2311 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002312
2313 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002314 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002315 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002316 appendSfConfigString(result);
2317 appendUiConfigString(result);
2318 appendGuiConfigString(result);
2319 result.append("\n");
2320
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002321 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002322 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002323 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002324 result.append(SyncFeatures::getInstance().toString());
2325 result.append("\n");
2326
Andy McFadden4803b742012-09-24 19:07:20 -07002327 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002328 * Dump the visible layer list
2329 */
2330 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2331 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002332 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002333 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002334 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002335 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002336 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002337 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002338 }
2339
2340 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002341 * Dump Display state
2342 */
2343
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002344 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002345 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002346 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002347 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2348 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002349 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002350 }
2351
2352 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002353 * Dump SurfaceFlinger global state
2354 */
2355
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002356 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002357 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002358 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002359
Mathias Agopian888c8222012-08-04 21:10:38 -07002360 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002361 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002362 const GLExtensions& extensions(GLExtensions::getInstance());
Mathias Agopianca088332013-03-28 17:44:13 -07002363
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002364 colorizer.bold(result);
2365 result.appendFormat("EGL implementation : %s\n",
2366 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2367 colorizer.reset(result);
2368 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002369 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002370
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002371 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002372 result.appendFormat("GLES: %s, %s, %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002373 extensions.getVendor(),
2374 extensions.getRenderer(),
2375 extensions.getVersion());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002376 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002377 result.appendFormat("%s\n", extensions.getExtension());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002378
Mathias Agopian42977342012-08-05 00:40:46 -07002379 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002380 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002381 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002382 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002383 " last eglSwapBuffers() time: %f us\n"
2384 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002385 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002386 " refresh-rate : %f fps\n"
2387 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002388 " y-dpi : %f\n"
2389 " EGL_NATIVE_VISUAL_ID : %d\n"
2390 " gpu_to_cpu_unsupported : %d\n"
2391 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002392 mLastSwapBufferTime/1000.0,
2393 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002394 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002395 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2396 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002397 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2398 mEGLNativeVisualId,
2399 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002400
Mathias Agopian74d211a2013-04-22 16:55:35 +02002401 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002403
Mathias Agopian74d211a2013-04-22 16:55:35 +02002404 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002405 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002406
2407 /*
2408 * VSYNC state
2409 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002410 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002411
2412 /*
2413 * Dump HWComposer state
2414 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002415 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002416 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002417 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002418 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002419 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2420 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002421 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002422
2423 /*
2424 * Dump gralloc state
2425 */
2426 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2427 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002428}
2429
Mathias Agopian13127d82013-03-05 17:47:11 -08002430const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002431SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002432 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002433 wp<IBinder> dpy;
2434 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2435 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2436 dpy = mDisplays.keyAt(i);
2437 break;
2438 }
2439 }
2440 if (dpy == NULL) {
2441 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2442 // Just use the primary display so we have something to return
2443 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2444 }
2445 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002446}
2447
Keun young Park63f165f2012-08-31 10:53:36 -07002448bool SurfaceFlinger::startDdmConnection()
2449{
2450 void* libddmconnection_dso =
2451 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2452 if (!libddmconnection_dso) {
2453 return false;
2454 }
2455 void (*DdmConnection_start)(const char* name);
2456 DdmConnection_start =
2457 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2458 if (!DdmConnection_start) {
2459 dlclose(libddmconnection_dso);
2460 return false;
2461 }
2462 (*DdmConnection_start)(getServiceName());
2463 return true;
2464}
2465
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002466status_t SurfaceFlinger::onTransact(
2467 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2468{
2469 switch (code) {
2470 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002471 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002472 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002474 case BLANK:
2475 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 {
2477 // codes that require permission check
2478 IPCThreadState* ipc = IPCThreadState::self();
2479 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002480 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002481 if ((uid != AID_GRAPHICS) &&
2482 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002483 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002484 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2485 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002487 break;
2488 }
2489 case CAPTURE_SCREEN:
2490 {
2491 // codes that require permission check
2492 IPCThreadState* ipc = IPCThreadState::self();
2493 const int pid = ipc->getCallingPid();
2494 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002495 if ((uid != AID_GRAPHICS) &&
2496 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002497 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002498 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2499 return PERMISSION_DENIED;
2500 }
2501 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002502 }
2503 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2506 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002507 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002508 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002509 IPCThreadState* ipc = IPCThreadState::self();
2510 const int pid = ipc->getCallingPid();
2511 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002512 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002513 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514 return PERMISSION_DENIED;
2515 }
2516 int n;
2517 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002518 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002519 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 return NO_ERROR;
2521 case 1002: // SHOW_UPDATES
2522 n = data.readInt32();
2523 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002524 invalidateHwcGeometry();
2525 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002526 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002528 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002529 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002531 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002532 setTransactionFlags(
2533 eTransactionNeeded|
2534 eDisplayTransactionNeeded|
2535 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002536 return NO_ERROR;
2537 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002538 case 1006:{ // send empty update
2539 signalRefresh();
2540 return NO_ERROR;
2541 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002542 case 1008: // toggle use of hw composer
2543 n = data.readInt32();
2544 mDebugDisableHWC = n ? 1 : 0;
2545 invalidateHwcGeometry();
2546 repaintEverything();
2547 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002548 case 1009: // toggle use of transform hint
2549 n = data.readInt32();
2550 mDebugDisableTransformHint = n ? 1 : 0;
2551 invalidateHwcGeometry();
2552 repaintEverything();
2553 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002555 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 reply->writeInt32(0);
2557 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002558 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002559 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002560 return NO_ERROR;
2561 case 1013: {
2562 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002563 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2564 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002565 }
2566 return NO_ERROR;
2567 }
2568 }
2569 return err;
2570}
2571
Mathias Agopian53331da2011-08-22 21:44:41 -07002572void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002573 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002574 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002575}
2576
Mathias Agopian59119e62010-10-11 12:37:43 -07002577// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002578// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002579// ---------------------------------------------------------------------------
2580
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002581status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2582 const sp<IGraphicBufferProducer>& producer,
2583 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002584 uint32_t minLayerZ, uint32_t maxLayerZ,
2585 bool isCpuConsumer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002586
2587 if (CC_UNLIKELY(display == 0))
2588 return BAD_VALUE;
2589
2590 if (CC_UNLIKELY(producer == 0))
2591 return BAD_VALUE;
2592
2593 class MessageCaptureScreen : public MessageBase {
2594 SurfaceFlinger* flinger;
2595 sp<IBinder> display;
2596 sp<IGraphicBufferProducer> producer;
2597 uint32_t reqWidth, reqHeight;
2598 uint32_t minLayerZ,maxLayerZ;
Mathias Agopianabe815d2013-03-19 22:22:21 -07002599 bool isCpuConsumer;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002600 status_t result;
2601 public:
2602 MessageCaptureScreen(SurfaceFlinger* flinger,
2603 const sp<IBinder>& display,
2604 const sp<IGraphicBufferProducer>& producer,
2605 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002606 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002607 : flinger(flinger), display(display), producer(producer),
2608 reqWidth(reqWidth), reqHeight(reqHeight),
2609 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopianabe815d2013-03-19 22:22:21 -07002610 isCpuConsumer(isCpuConsumer),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002611 result(PERMISSION_DENIED)
2612 {
2613 }
2614 status_t getResult() const {
2615 return result;
2616 }
2617 virtual bool handler() {
2618 Mutex::Autolock _l(flinger->mStateLock);
2619 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002620 bool useReadPixels = isCpuConsumer && !flinger->mGpuToCpuSupported;
2621 result = flinger->captureScreenImplLocked(hw,
2622 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2623 useReadPixels);
Mathias Agopianb4b17302013-03-20 18:36:41 -07002624
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002625 return true;
2626 }
2627 };
2628
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002629 // make sure to process transactions before screenshots -- a transaction
2630 // might already be pending but scheduled for VSYNC; this guarantees we
2631 // will handle it before the screenshot. When VSYNC finally arrives
2632 // the scheduled transaction will be a no-op. If no transactions are
2633 // scheduled at this time, this will end-up being a no-op as well.
2634 mEventQueue.invalidateTransactionNow();
2635
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002636 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002637 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2638 isCpuConsumer);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002639 status_t res = postMessageSync(msg);
2640 if (res == NO_ERROR) {
2641 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2642 }
2643 return res;
2644}
2645
Mathias Agopian180f10d2013-04-10 22:55:41 -07002646
2647void SurfaceFlinger::renderScreenImplLocked(
2648 const sp<const DisplayDevice>& hw,
2649 uint32_t reqWidth, uint32_t reqHeight,
2650 uint32_t minLayerZ, uint32_t maxLayerZ,
2651 bool yswap)
2652{
2653 ATRACE_CALL();
2654
2655 // get screen geometry
2656 const uint32_t hw_w = hw->getWidth();
2657 const uint32_t hw_h = hw->getHeight();
2658
2659 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2660
2661 // make sure to clear all GL error flags
2662 while ( glGetError() != GL_NO_ERROR ) ;
2663
2664 // set-up our viewport
2665 glViewport(0, 0, reqWidth, reqHeight);
2666 glMatrixMode(GL_PROJECTION);
2667 glLoadIdentity();
2668 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2669 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2670 glMatrixMode(GL_MODELVIEW);
2671 glLoadIdentity();
2672
2673 // redraw the screen entirely...
2674 glDisable(GL_SCISSOR_TEST);
2675 glClearColor(0,0,0,1);
2676 glClear(GL_COLOR_BUFFER_BIT);
2677 glDisable(GL_TEXTURE_EXTERNAL_OES);
2678 glDisable(GL_TEXTURE_2D);
2679
2680 const LayerVector& layers( mDrawingState.layersSortedByZ );
2681 const size_t count = layers.size();
2682 for (size_t i=0 ; i<count ; ++i) {
2683 const sp<Layer>& layer(layers[i]);
2684 const Layer::State& state(layer->drawingState());
2685 if (state.layerStack == hw->getLayerStack()) {
2686 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2687 if (layer->isVisible()) {
2688 if (filtering) layer->setFiltering(true);
2689 layer->draw(hw);
2690 if (filtering) layer->setFiltering(false);
2691 }
2692 }
2693 }
2694 }
2695
2696 // compositionComplete is needed for older driver
2697 hw->compositionComplete();
2698}
2699
2700
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002701status_t SurfaceFlinger::captureScreenImplLocked(
2702 const sp<const DisplayDevice>& hw,
2703 const sp<IGraphicBufferProducer>& producer,
2704 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002705 uint32_t minLayerZ, uint32_t maxLayerZ,
2706 bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002707{
2708 ATRACE_CALL();
2709
2710 if (!GLExtensions::getInstance().haveFramebufferObject()) {
2711 return INVALID_OPERATION;
2712 }
2713
Mathias Agopian180f10d2013-04-10 22:55:41 -07002714 // get screen geometry
2715 const uint32_t hw_w = hw->getWidth();
2716 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002717
Mathias Agopian180f10d2013-04-10 22:55:41 -07002718 // if we have secure windows on this display, never allow the screen capture
2719 if (hw->getSecureLayerVisible()) {
2720 ALOGW("FB is protected: PERMISSION_DENIED");
2721 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002722 }
2723
Mathias Agopian180f10d2013-04-10 22:55:41 -07002724 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2725 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2726 reqWidth, reqHeight, hw_w, hw_h);
2727 return BAD_VALUE;
2728 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002729
Mathias Agopian180f10d2013-04-10 22:55:41 -07002730 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2731 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2732
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002733 // create a surface (because we're a producer, and we need to
2734 // dequeue/queue a buffer)
2735 sp<Surface> sur = new Surface(producer);
2736 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002737
2738 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002739 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
2740 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
2741 if (!useReadPixels) {
2742 usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002743 }
2744
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002745 int err = 0;
2746 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2747 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2748 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002749
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002750 if (err == NO_ERROR) {
2751 ANativeWindowBuffer* buffer;
2752 /* TODO: Once we have the sync framework everywhere this can use
2753 * server-side waits on the fence that dequeueBuffer returns.
2754 */
2755 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2756 if (result == NO_ERROR) {
2757 // create an EGLImage from the buffer so we can later
2758 // turn it into a texture
2759 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2760 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2761 if (image != EGL_NO_IMAGE_KHR) {
2762 GLuint tname, name;
2763 if (!useReadPixels) {
2764 // turn our EGLImage into a texture
2765 glGenTextures(1, &tname);
2766 glBindTexture(GL_TEXTURE_2D, tname);
2767 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2768 // create a Framebuffer Object to render into
2769 glGenFramebuffersOES(1, &name);
2770 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2771 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2772 GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
2773 } else {
2774 // since we're going to use glReadPixels() anyways,
2775 // use an intermediate renderbuffer instead
2776 glGenRenderbuffersOES(1, &tname);
2777 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2778 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
2779 // create a FBO to render into
2780 glGenFramebuffersOES(1, &name);
2781 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2782 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2783 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2784 }
2785
2786 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2787 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2788 // this will in fact render into our dequeued buffer
2789 // via an FBO, which means we didn't have to create
2790 // an EGLSurface and therefore we're not
2791 // dependent on the context's EGLConfig.
2792 renderScreenImplLocked(hw, reqWidth, reqHeight,
2793 minLayerZ, maxLayerZ, true);
2794
2795 if (useReadPixels) {
2796 sp<GraphicBuffer> buf = static_cast<GraphicBuffer*>(buffer);
2797 void* vaddr;
2798 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
2799 glReadPixels(0, 0, buffer->stride, reqHeight,
2800 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
2801 buf->unlock();
2802 }
2803 }
2804 } else {
2805 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2806 result = INVALID_OPERATION;
2807 }
2808
2809 // back to main framebuffer
2810 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2811 glDeleteFramebuffersOES(1, &name);
2812 if (!useReadPixels) {
2813 glDeleteTextures(1, &tname);
2814 } else {
2815 glDeleteRenderbuffersOES(1, &tname);
2816 }
2817 // destroy our image
2818 eglDestroyImageKHR(mEGLDisplay, image);
2819 } else {
2820 result = BAD_VALUE;
2821 }
2822 window->queueBuffer(window, buffer, -1);
2823 }
2824 } else {
2825 result = BAD_VALUE;
2826 }
2827 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2828 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002829
2830 DisplayDevice::setViewportAndProjection(hw);
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002831
Mathias Agopian74c40c02010-09-29 13:02:36 -07002832 return result;
2833}
2834
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002835// ---------------------------------------------------------------------------
2836
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002837SurfaceFlinger::LayerVector::LayerVector() {
2838}
2839
2840SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002841 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002842}
2843
2844int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2845 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002846{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002847 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002848 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2849 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002850
2851 uint32_t ls = l->currentState().layerStack;
2852 uint32_t rs = r->currentState().layerStack;
2853 if (ls != rs)
2854 return ls - rs;
2855
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002856 uint32_t lz = l->currentState().z;
2857 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002858 if (lz != rz)
2859 return lz - rz;
2860
2861 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002862}
2863
2864// ---------------------------------------------------------------------------
2865
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002866SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2867 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002868}
2869
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002870SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002871 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002872 viewport.makeInvalid();
2873 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002874}
2875
2876// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002877
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002878}; // namespace android