blob: a4426cde15463871f74a0fe89ecd2a8352775367 [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
58#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080059#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070060#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070061#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070063#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopiana4912602012-07-12 14:25:33 -070068#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070069#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070070#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072#define DISPLAY_COUNT 1
73
Mathias Agopianca088332013-03-28 17:44:13 -070074EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
75
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077// ---------------------------------------------------------------------------
78
Mathias Agopian99b49842011-06-27 16:05:52 -070079const String16 sHardwareTest("android.permission.HARDWARE_TEST");
80const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
81const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
82const String16 sDump("android.permission.DUMP");
83
84// ---------------------------------------------------------------------------
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086SurfaceFlinger::SurfaceFlinger()
87 : BnSurfaceComposer(), Thread(false),
88 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070089 mTransactionPending(false),
90 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070092 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070095 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -080096 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070098 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070099 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700100 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700101 mDebugInSwapBuffers(0),
102 mLastSwapBufferTime(0),
103 mDebugInTransaction(0),
104 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700105 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106{
Steve Blocka19954a2012-01-04 20:05:49 +0000107 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111
Mathias Agopianb4b17302013-03-20 18:36:41 -0700112 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700113 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 property_get("debug.sf.showupdates", value, "0");
116 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700121 if (!startDdmConnection()) {
122 // start failed, and DDMS debugging not enabled
123 mDebugDDMS = 0;
124 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700125 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700126 ALOGI_IF(mDebugRegion, "showupdates enabled");
127 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128}
129
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800130void SurfaceFlinger::onFirstRef()
131{
132 mEventQueue.init(this);
133
134 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
135
136 // Wait for the main thread to be done with its initialization
137 mReadyToRunBarrier.wait();
138}
139
140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141SurfaceFlinger::~SurfaceFlinger()
142{
Mathias Agopiana4912602012-07-12 14:25:33 -0700143 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
144 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
145 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146}
147
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148void SurfaceFlinger::binderDied(const wp<IBinder>& who)
149{
150 // the window manager died on us. prepare its eulogy.
151
Andy McFadden13a082e2012-08-24 10:16:42 -0700152 // restore initial conditions (default device unblank, etc)
153 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800154
155 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700156 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800157}
158
Mathias Agopian7e27f052010-05-28 14:22:23 -0700159sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Mathias Agopian96f08192010-06-02 23:28:45 -0700161 sp<ISurfaceComposerClient> bclient;
162 sp<Client> client(new Client(this));
163 status_t err = client->initCheck();
164 if (err == NO_ERROR) {
165 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 return bclient;
168}
169
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700170sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
171 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700172{
173 class DisplayToken : public BBinder {
174 sp<SurfaceFlinger> flinger;
175 virtual ~DisplayToken() {
176 // no more references, this display must be terminated
177 Mutex::Autolock _l(flinger->mStateLock);
178 flinger->mCurrentState.displays.removeItem(this);
179 flinger->setTransactionFlags(eDisplayTransactionNeeded);
180 }
181 public:
182 DisplayToken(const sp<SurfaceFlinger>& flinger)
183 : flinger(flinger) {
184 }
185 };
186
187 sp<BBinder> token = new DisplayToken(this);
188
189 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700190 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700191 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700192 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700193 mCurrentState.displays.add(token, info);
194
195 return token;
196}
197
Jesse Hall692c7232012-11-08 15:41:56 -0800198void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
199 ALOGW_IF(mBuiltinDisplays[type],
200 "Overwriting display token for display type %d", type);
201 mBuiltinDisplays[type] = new BBinder();
202 DisplayDeviceState info(type);
203 // All non-virtual displays are currently considered secure.
204 info.isSecure = true;
205 mCurrentState.displays.add(mBuiltinDisplays[type], info);
206}
207
Mathias Agopiane57f2922012-08-09 16:29:12 -0700208sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700209 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700210 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
211 return NULL;
212 }
Jesse Hall692c7232012-11-08 15:41:56 -0800213 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700214}
215
Jamie Gennis9a78c902011-01-12 18:30:40 -0800216sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
217{
218 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
219 return gba;
220}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222void SurfaceFlinger::bootFinished()
223{
224 const nsecs_t now = systemTime();
225 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000226 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700227 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700228
229 // wait patiently for the window manager death
230 const String16 name("window");
231 sp<IBinder> window(defaultServiceManager()->getService(name));
232 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700233 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700234 }
235
236 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700237 // formerly we would just kill the process, but we now ask it to exit so it
238 // can choose where to stop the animation.
239 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240}
241
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700242void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
243 class MessageDestroyGLTexture : public MessageBase {
244 GLuint texture;
245 public:
246 MessageDestroyGLTexture(GLuint texture)
247 : texture(texture) {
248 }
249 virtual bool handler() {
250 glDeleteTextures(1, &texture);
251 return true;
252 }
253 };
254 postMessageAsync(new MessageDestroyGLTexture(texture));
255}
256
Mathias Agopian722b98f2012-09-25 18:24:31 -0700257status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700258 EGLDisplay dpy,
259 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700260 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700261 EGLConfig* outConfig)
262{
263 EGLConfig config = NULL;
264 EGLint numConfigs = -1, n=0;
265 eglGetConfigs(dpy, NULL, 0, &numConfigs);
266 EGLConfig* const configs = new EGLConfig[numConfigs];
267 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700268
Mathias Agopian722b98f2012-09-25 18:24:31 -0700269 if (n) {
270 if (attribute != EGL_NONE) {
271 for (int i=0 ; i<n ; i++) {
272 EGLint value = 0;
273 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
274 if (wanted == value) {
275 *outConfig = configs[i];
276 delete [] configs;
277 return NO_ERROR;
278 }
279 }
280 } else {
281 // just pick the first one
282 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700283 delete [] configs;
284 return NO_ERROR;
285 }
286 }
287 delete [] configs;
288 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289}
290
Mathias Agopian722b98f2012-09-25 18:24:31 -0700291class EGLAttributeVector {
292 struct Attribute;
293 class Adder;
294 friend class Adder;
295 KeyedVector<Attribute, EGLint> mList;
296 struct Attribute {
297 Attribute() {};
298 Attribute(EGLint v) : v(v) { }
299 EGLint v;
300 bool operator < (const Attribute& other) const {
301 // this places EGL_NONE at the end
302 EGLint lhs(v);
303 EGLint rhs(other.v);
304 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
305 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
306 return lhs < rhs;
307 }
308 };
309 class Adder {
310 friend class EGLAttributeVector;
311 EGLAttributeVector& v;
312 EGLint attribute;
313 Adder(EGLAttributeVector& v, EGLint attribute)
314 : v(v), attribute(attribute) {
315 }
316 public:
317 void operator = (EGLint value) {
318 if (attribute != EGL_NONE) {
319 v.mList.add(attribute, value);
320 }
321 }
322 operator EGLint () const { return v.mList[attribute]; }
323 };
324public:
325 EGLAttributeVector() {
326 mList.add(EGL_NONE, EGL_NONE);
327 }
328 void remove(EGLint attribute) {
329 if (attribute != EGL_NONE) {
330 mList.removeItem(attribute);
331 }
332 }
333 Adder operator [] (EGLint attribute) {
334 return Adder(*this, attribute);
335 }
336 EGLint operator [] (EGLint attribute) const {
337 return mList[attribute];
338 }
339 // cast-operator to (EGLint const*)
340 operator EGLint const* () const { return &mList.keyAt(0).v; }
341};
342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
344 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
345 // it is to be used with WIFI displays
346 EGLConfig config;
347 EGLint dummy;
348 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700349
Mathias Agopian722b98f2012-09-25 18:24:31 -0700350 EGLAttributeVector attribs;
351 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
352 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
353 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
354 attribs[EGL_RED_SIZE] = 8;
355 attribs[EGL_GREEN_SIZE] = 8;
356 attribs[EGL_BLUE_SIZE] = 8;
357
358 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
359 if (!err)
360 goto success;
361
362 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
363 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
364 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
365 err = selectConfigForAttribute(display, attribs,
366 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700367 if (!err)
368 goto success;
369
370 // maybe we failed because of EGL_RECORDABLE_ANDROID
371 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700372 attribs.remove(EGL_RECORDABLE_ANDROID);
373 err = selectConfigForAttribute(display, attribs,
374 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700375 if (!err)
376 goto success;
377
378 // allow less than 24-bit color; the non-gpu-accelerated emulator only
379 // supports 16-bit color
380 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700381 attribs.remove(EGL_RED_SIZE);
382 attribs.remove(EGL_GREEN_SIZE);
383 attribs.remove(EGL_BLUE_SIZE);
384 err = selectConfigForAttribute(display, attribs,
385 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700386 if (!err)
387 goto success;
388
389 // this EGL is too lame for Android
390 ALOGE("no suitable EGLConfig found, giving up");
391
392 return 0;
393
394success:
395 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700396 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700397 return config;
398}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399
Mathias Agopiana4912602012-07-12 14:25:33 -0700400EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
401 // Also create our EGLContext
402 EGLint contextAttributes[] = {
403#ifdef EGL_IMG_context_priority
404#ifdef HAS_CONTEXT_PRIORITY
405#warning "using EGL_IMG_context_priority"
406 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
407#endif
408#endif
409 EGL_NONE, EGL_NONE
410 };
411 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
412 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
413 return ctxt;
414}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415
Mathias Agopiancde87a32012-09-13 14:09:01 -0700416void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700417 GLExtensions& extensions(GLExtensions::getInstance());
418 extensions.initWithGLStrings(
419 glGetString(GL_VENDOR),
420 glGetString(GL_RENDERER),
421 glGetString(GL_VERSION),
422 glGetString(GL_EXTENSIONS),
423 eglQueryString(display, EGL_VENDOR),
424 eglQueryString(display, EGL_VERSION),
425 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700426
Mathias Agopiana4912602012-07-12 14:25:33 -0700427 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
428 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700429
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700431 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 glShadeModel(GL_FLAT);
434 glDisable(GL_DITHER);
435 glDisable(GL_CULL_FACE);
436
Mathias Agopiana4912602012-07-12 14:25:33 -0700437 struct pack565 {
438 inline uint16_t operator() (int r, int g, int b) const {
439 return (r<<11)|(g<<5)|b;
440 }
441 } pack565;
442
Jamie Gennis9575f602011-10-07 14:51:16 -0700443 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
444 glGenTextures(1, &mProtectedTexName);
445 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
446 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
447 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
448 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
449 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
450 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
451 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452
Mathias Agopiana4912602012-07-12 14:25:33 -0700453 // print some debugging info
454 EGLint r,g,b,a;
455 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
456 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
457 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
458 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
459 ALOGI("EGL informations:");
460 ALOGI("vendor : %s", extensions.getEglVendor());
461 ALOGI("version : %s", extensions.getEglVersion());
462 ALOGI("extensions: %s", extensions.getEglExtension());
463 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
464 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
465 ALOGI("OpenGL ES informations:");
466 ALOGI("vendor : %s", extensions.getVendor());
467 ALOGI("renderer : %s", extensions.getRenderer());
468 ALOGI("version : %s", extensions.getVersion());
469 ALOGI("extensions: %s", extensions.getExtension());
470 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
471 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
472}
473
Mathias Agopiana4912602012-07-12 14:25:33 -0700474status_t SurfaceFlinger::readyToRun()
475{
476 ALOGI( "SurfaceFlinger's main thread ready to run. "
477 "Initializing graphics H/W...");
478
Jesse Hall692c7232012-11-08 15:41:56 -0800479 Mutex::Autolock _l(mStateLock);
480
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700481 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700482 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
483 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700484
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700485 // Initialize the H/W composer object. There may or may not be an
486 // actual hardware composer underneath.
487 mHwc = new HWComposer(this,
488 *static_cast<HWComposer::EventHandler *>(this));
489
Mathias Agopiana4912602012-07-12 14:25:33 -0700490 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700491 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700492 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
493 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700494
Mathias Agopianed985572013-03-22 00:24:39 -0700495 // figure out which format we got
496 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
497 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
498
Mathias Agopianda27af92012-09-13 18:17:13 -0700499 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
500 "couldn't create EGLContext");
501
Mathias Agopiancde87a32012-09-13 14:09:01 -0700502 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700503 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700504 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700505 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700506 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700507 // All non-virtual displays are currently considered secure.
508 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800509 createBuiltinDisplayLocked(type);
510 wp<IBinder> token = mBuiltinDisplays[i];
511
Mathias Agopianf5a33922012-09-19 18:16:22 -0700512 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700513 type, allocateHwcDisplayId(type), isSecure, token,
514 new FramebufferSurface(*mHwc, i),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700515 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700516 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700517 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700518 // for displays other than the main display, so we always
519 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700520 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700521 hw->acquireScreen();
522 }
523 mDisplays.add(token, hw);
524 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700525 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700526
Mathias Agopianf5a33922012-09-19 18:16:22 -0700527 // we need a GL context current in a few places, when initializing
528 // OpenGL ES (see below), or creating a layer,
529 // or when a texture is (asynchronously) destroyed, and for that
530 // we need a valid surface, so it's convenient to use the main display
531 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700532 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700533
Mathias Agopiana4912602012-07-12 14:25:33 -0700534 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700535 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
536 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800537
Mathias Agopian028508c2012-07-25 21:12:12 -0700538 // start the EventThread
539 mEventThread = new EventThread(this);
540 mEventQueue.setEventThread(mEventThread);
541
Mathias Agopian92a979a2012-08-02 18:32:23 -0700542 // initialize our drawing state
543 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700544
Mathias Agopiancde87a32012-09-13 14:09:01 -0700545
Mathias Agopiana4912602012-07-12 14:25:33 -0700546 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800547 mReadyToRunBarrier.open();
548
Andy McFadden13a082e2012-08-24 10:16:42 -0700549 // set initial conditions (e.g. unblank default device)
550 initializeDisplays();
551
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700552 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700553 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700554
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 return NO_ERROR;
556}
557
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700558int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
559 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
560 type : mHwc->allocateDisplayId();
561}
562
Mathias Agopiana67e4182012-06-19 17:26:12 -0700563void SurfaceFlinger::startBootAnim() {
564 // start boot animation
565 property_set("service.bootanim.exit", "0");
566 property_set("ctl.start", "bootanim");
567}
568
Mathias Agopiana4912602012-07-12 14:25:33 -0700569uint32_t SurfaceFlinger::getMaxTextureSize() const {
570 return mMaxTextureSize;
571}
572
573uint32_t SurfaceFlinger::getMaxViewportDims() const {
574 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
575 mMaxViewportDims[0] : mMaxViewportDims[1];
576}
577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800579
Jamie Gennis582270d2011-08-17 18:19:00 -0700580bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800581 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800582 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800583 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700584 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800585}
586
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700587status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800588 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700589 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800590 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700591 type = i;
592 break;
593 }
594 }
595
596 if (type < 0) {
597 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700598 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700599
600 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700601 float xdpi = hwc.getDpiX(type);
602 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700603
604 // TODO: Not sure if display density should handled by SF any longer
605 class Density {
606 static int getDensityFromProperty(char const* propName) {
607 char property[PROPERTY_VALUE_MAX];
608 int density = 0;
609 if (property_get(propName, property, NULL) > 0) {
610 density = atoi(property);
611 }
612 return density;
613 }
614 public:
615 static int getEmuDensity() {
616 return getDensityFromProperty("qemu.sf.lcd_density"); }
617 static int getBuildDensity() {
618 return getDensityFromProperty("ro.sf.lcd_density"); }
619 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700620
621 if (type == DisplayDevice::DISPLAY_PRIMARY) {
622 // The density of the device is provided by a build property
623 float density = Density::getBuildDensity() / 160.0f;
624 if (density == 0) {
625 // the build doesn't provide a density -- this is wrong!
626 // use xdpi instead
627 ALOGE("ro.sf.lcd_density must be defined as a build property");
628 density = xdpi / 160.0f;
629 }
630 if (Density::getEmuDensity()) {
631 // if "qemu.sf.lcd_density" is specified, it overrides everything
632 xdpi = ydpi = density = Density::getEmuDensity();
633 density /= 160.0f;
634 }
635 info->density = density;
636
637 // TODO: this needs to go away (currently needed only by webkit)
638 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
639 info->orientation = hw->getOrientation();
640 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
641 } else {
642 // TODO: where should this value come from?
643 static const int TV_DENSITY = 213;
644 info->density = TV_DENSITY / 160.0f;
645 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700646 }
647
Mathias Agopian1604f772012-09-18 21:54:42 -0700648 info->w = hwc.getWidth(type);
649 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700650 info->xdpi = xdpi;
651 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700652 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700653
654 // All non-virtual displays are currently considered secure.
655 info->secure = true;
656
Mathias Agopian888c8222012-08-04 21:10:38 -0700657 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700658}
659
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800660// ----------------------------------------------------------------------------
661
662sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800663 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700664}
665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800667
668void SurfaceFlinger::waitForEvent() {
669 mEventQueue.waitMessage();
670}
671
672void SurfaceFlinger::signalTransaction() {
673 mEventQueue.invalidate();
674}
675
676void SurfaceFlinger::signalLayerUpdate() {
677 mEventQueue.invalidate();
678}
679
680void SurfaceFlinger::signalRefresh() {
681 mEventQueue.refresh();
682}
683
684status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
685 nsecs_t reltime, uint32_t flags) {
686 return mEventQueue.postMessage(msg, reltime);
687}
688
689status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
690 nsecs_t reltime, uint32_t flags) {
691 status_t res = mEventQueue.postMessage(msg, reltime);
692 if (res == NO_ERROR) {
693 msg->wait();
694 }
695 return res;
696}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697
Mathias Agopian4fec8732012-06-29 14:12:52 -0700698bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 return true;
701}
702
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700703void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700704 if (mEventThread == NULL) {
705 // This is a temporary workaround for b/7145521. A non-null pointer
706 // does not mean EventThread has finished initializing, so this
707 // is not a correct fix.
708 ALOGW("WARNING: EventThread not started, ignoring vsync");
709 return;
710 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700711 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
712 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700713 mEventThread->onVSyncReceived(type, timestamp);
714 }
715}
716
717void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
718 if (mEventThread == NULL) {
719 // This is a temporary workaround for b/7145521. A non-null pointer
720 // does not mean EventThread has finished initializing, so this
721 // is not a correct fix.
722 ALOGW("WARNING: EventThread not started, ignoring hotplug");
723 return;
724 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700725
Mathias Agopian148994e2012-09-19 17:31:36 -0700726 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700727 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800728 if (connected) {
729 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700730 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800731 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
732 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700733 }
734 setTransactionFlags(eDisplayTransactionNeeded);
735
Andy McFadden9e9689c2012-10-10 18:17:51 -0700736 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700737 }
Mathias Agopian86303202012-07-24 22:46:10 -0700738}
739
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700740void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
741 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700742}
743
Mathias Agopian4fec8732012-06-29 14:12:52 -0700744void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800745 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800746 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700747 case MessageQueue::INVALIDATE:
748 handleMessageTransaction();
749 handleMessageInvalidate();
750 signalRefresh();
751 break;
752 case MessageQueue::REFRESH:
753 handleMessageRefresh();
754 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800755 }
756}
757
Mathias Agopian4fec8732012-06-29 14:12:52 -0700758void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700759 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700760 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700761 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700762 }
763}
764
765void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700766 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700767 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700768}
769
770void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700771 ATRACE_CALL();
772 preComposition();
773 rebuildLayerStacks();
774 setUpHWComposer();
775 doDebugFlashRegions();
776 doComposition();
777 postComposition();
778}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700779
Mathias Agopiancd60f992012-08-16 16:28:27 -0700780void SurfaceFlinger::doDebugFlashRegions()
781{
782 // is debugging enabled
783 if (CC_LIKELY(!mDebugRegion))
784 return;
785
786 const bool repaintEverything = mRepaintEverything;
787 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
788 const sp<DisplayDevice>& hw(mDisplays[dpy]);
789 if (hw->canDraw()) {
790 // transform the dirty region into this screen's coordinate space
791 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
792 if (!dirtyRegion.isEmpty()) {
793 // redraw the whole screen
794 doComposeSurfaces(hw, Region(hw->bounds()));
795
796 // and draw the dirty region
797 glDisable(GL_TEXTURE_EXTERNAL_OES);
798 glDisable(GL_TEXTURE_2D);
799 glDisable(GL_BLEND);
800 glColor4f(1, 0, 1, 1);
801 const int32_t height = hw->getHeight();
802 Region::const_iterator it = dirtyRegion.begin();
803 Region::const_iterator const end = dirtyRegion.end();
804 while (it != end) {
805 const Rect& r = *it++;
806 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800807 { (GLfloat) r.left, (GLfloat) (height - r.top) },
808 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
809 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
810 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811 };
812 glVertexPointer(2, GL_FLOAT, 0, vertices);
813 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
814 }
815 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700816 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700817 }
818 }
819 }
820
821 postFramebuffer();
822
823 if (mDebugRegion > 1) {
824 usleep(mDebugRegion * 1000);
825 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700826
827 HWComposer& hwc(getHwComposer());
828 if (hwc.initCheck() == NO_ERROR) {
829 status_t err = hwc.prepare();
830 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
831 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700832}
833
834void SurfaceFlinger::preComposition()
835{
836 bool needExtraInvalidate = false;
837 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
838 const size_t count = currentLayers.size();
839 for (size_t i=0 ; i<count ; i++) {
840 if (currentLayers[i]->onPreComposition()) {
841 needExtraInvalidate = true;
842 }
843 }
844 if (needExtraInvalidate) {
845 signalLayerUpdate();
846 }
847}
848
849void SurfaceFlinger::postComposition()
850{
851 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
852 const size_t count = currentLayers.size();
853 for (size_t i=0 ; i<count ; i++) {
854 currentLayers[i]->onPostComposition();
855 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800856
857 if (mAnimCompositionPending) {
858 mAnimCompositionPending = false;
859
860 const HWComposer& hwc = getHwComposer();
861 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800862 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800863 mAnimFrameTracker.setActualPresentFence(presentFence);
864 } else {
865 // The HWC doesn't support present fences, so use the refresh
866 // timestamp instead.
867 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
868 mAnimFrameTracker.setActualPresentTime(presentTime);
869 }
870 mAnimFrameTracker.advanceFrame();
871 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700872}
873
874void SurfaceFlinger::rebuildLayerStacks() {
875 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700876 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700877 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700878 mVisibleRegionsDirty = false;
879 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700880
Mathias Agopian87baae12012-07-31 12:38:26 -0700881 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700882 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700883 Region opaqueRegion;
884 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700886 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700887 const Transform& tr(hw->getTransform());
888 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700889 if (hw->canDraw()) {
890 SurfaceFlinger::computeVisibleRegions(currentLayers,
891 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700892
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700893 const size_t count = currentLayers.size();
894 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800895 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700896 const Layer::State& s(layer->drawingState());
897 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700898 Region drawRegion(tr.transform(
899 layer->visibleNonTransparentRegion));
900 drawRegion.andSelf(bounds);
901 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700902 layersSortedByZ.add(layer);
903 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700904 }
905 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700906 }
Mathias Agopian42977342012-08-05 00:40:46 -0700907 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700908 hw->undefinedRegion.set(bounds);
909 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
910 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700911 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700912 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700914
Mathias Agopiancd60f992012-08-16 16:28:27 -0700915void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700916 HWComposer& hwc(getHwComposer());
917 if (hwc.initCheck() == NO_ERROR) {
918 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700919 if (CC_UNLIKELY(mHwWorkListDirty)) {
920 mHwWorkListDirty = false;
921 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
922 sp<const DisplayDevice> hw(mDisplays[dpy]);
923 const int32_t id = hw->getHwcDisplayId();
924 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800925 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700926 hw->getVisibleLayersSortedByZ());
927 const size_t count = currentLayers.size();
928 if (hwc.createWorkList(id, count) == NO_ERROR) {
929 HWComposer::LayerListIterator cur = hwc.begin(id);
930 const HWComposer::LayerListIterator end = hwc.end(id);
931 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800932 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700933 layer->setGeometry(hw, *cur);
934 if (mDebugDisableHWC || mDebugRegion) {
935 cur->setSkip(true);
936 }
937 }
938 }
939 }
940 }
941 }
942
943 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700944 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700945 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700946 const int32_t id = hw->getHwcDisplayId();
947 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800948 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700949 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700950 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700951 HWComposer::LayerListIterator cur = hwc.begin(id);
952 const HWComposer::LayerListIterator end = hwc.end(id);
953 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
954 /*
955 * update the per-frame h/w composer data for each layer
956 * and build the transparent region of the FB
957 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800958 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700959 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700960 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700962 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700963
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 status_t err = hwc.prepare();
965 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
966 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700967}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968
Mathias Agopiancd60f992012-08-16 16:28:27 -0700969void SurfaceFlinger::doComposition() {
970 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700971 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700972 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700973 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700974 if (hw->canDraw()) {
975 // transform the dirty region into this screen's coordinate space
976 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800977
978 // repaint the framebuffer (if needed)
979 doDisplayComposition(hw, dirtyRegion);
980
Mathias Agopiancd60f992012-08-16 16:28:27 -0700981 hw->dirtyRegion.clear();
982 hw->flip(hw->swapRegion);
983 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700984 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700986 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700987 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700988 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700989}
990
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991void SurfaceFlinger::postFramebuffer()
992{
Mathias Agopian841cde52012-03-01 15:44:37 -0800993 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800994
Mathias Agopiana44b0412011-10-16 18:46:35 -0700995 const nsecs_t now = systemTime();
996 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700997
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700998 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700999 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001000 if (!hwc.supportsFramebufferTarget()) {
1001 // EGL spec says:
1002 // "surface must be bound to the calling thread's current context,
1003 // for the current rendering API."
1004 DisplayDevice::makeCurrent(mEGLDisplay,
1005 getDefaultDisplayDevice(), mEGLContext);
1006 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001007 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001008 }
1009
Mathias Agopian92a979a2012-08-02 18:32:23 -07001010 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001011 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001012 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001013 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001014 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001015 int32_t id = hw->getHwcDisplayId();
1016 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001017 HWComposer::LayerListIterator cur = hwc.begin(id);
1018 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001019 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001020 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001021 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001022 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001023 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001024 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001025 }
Jesse Hallef194142012-06-14 14:45:17 -07001026 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001027 }
1028
Mathias Agopiana44b0412011-10-16 18:46:35 -07001029 mLastSwapBufferTime = systemTime() - now;
1030 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031}
1032
Mathias Agopian87baae12012-07-31 12:38:26 -07001033void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034{
Mathias Agopian841cde52012-03-01 15:44:37 -08001035 ATRACE_CALL();
1036
Mathias Agopianca4d3602011-05-19 15:38:14 -07001037 Mutex::Autolock _l(mStateLock);
1038 const nsecs_t now = systemTime();
1039 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040
Mathias Agopianca4d3602011-05-19 15:38:14 -07001041 // Here we're guaranteed that some transaction flags are set
1042 // so we can call handleTransactionLocked() unconditionally.
1043 // We call getTransactionFlags(), which will also clear the flags,
1044 // with mStateLock held to guarantee that mCurrentState won't change
1045 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001046
Mathias Agopiane57f2922012-08-09 16:29:12 -07001047 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001048 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001049
Mathias Agopianca4d3602011-05-19 15:38:14 -07001050 mLastTransactionTime = systemTime() - now;
1051 mDebugInTransaction = 0;
1052 invalidateHwcGeometry();
1053 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001054}
1055
Mathias Agopian87baae12012-07-31 12:38:26 -07001056void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001057{
1058 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 const size_t count = currentLayers.size();
1060
1061 /*
1062 * Traversal of the children
1063 * (perform the transaction for each of them if needed)
1064 */
1065
Mathias Agopian3559b072012-08-15 13:46:03 -07001066 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1070 if (!trFlags) continue;
1071
1072 const uint32_t flags = layer->doTransaction(0);
1073 if (flags & Layer::eVisibleRegion)
1074 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 }
1076 }
1077
1078 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001079 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 */
1081
Mathias Agopiane57f2922012-08-09 16:29:12 -07001082 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001083 // here we take advantage of Vector's copy-on-write semantics to
1084 // improve performance by skipping the transaction entirely when
1085 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001086 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1087 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001088 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001090 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001091 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001092
1093 // find the displays that were removed
1094 // (ie: in drawing state but not in current state)
1095 // also handle displays that changed
1096 // (ie: displays that are in both lists)
1097 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001098 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1099 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001100 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001101 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001102 // Call makeCurrent() on the primary display so we can
1103 // be sure that nothing associated with this display
1104 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001105 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
1106 DisplayDevice::makeCurrent(mEGLDisplay, defaultDisplay, mEGLContext);
1107 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1108 if (hw != NULL)
1109 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001110 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1111 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001112 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001113 } else {
1114 ALOGW("trying to remove the main display");
1115 }
1116 } else {
1117 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001118 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001119 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001120 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001121 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001122 // recreating the DisplayDevice, so we just remove it
1123 // from the drawing state, so that it get re-added
1124 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001125 sp<DisplayDevice> hw(getDisplayDevice(display));
1126 if (hw != NULL)
1127 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001128 mDisplays.removeItem(display);
1129 mDrawingState.displays.removeItemsAt(i);
1130 dc--; i--;
1131 // at this point we must loop to the next item
1132 continue;
1133 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001134
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001135 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001136 if (disp != NULL) {
1137 if (state.layerStack != draw[i].layerStack) {
1138 disp->setLayerStack(state.layerStack);
1139 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001140 if ((state.orientation != draw[i].orientation)
1141 || (state.viewport != draw[i].viewport)
1142 || (state.frame != draw[i].frame))
1143 {
1144 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001145 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001146 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001147 }
1148 }
1149 }
1150
1151 // find displays that were added
1152 // (ie: in current state but not in drawing state)
1153 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001154 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001155 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001156
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001157 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001158 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001159 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001160 // Virtual displays without a surface are dormant:
1161 // they have external state (layer stack, projection,
1162 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001163 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001164 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001165 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001166 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001167 state.displayName);
1168 }
1169 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001170 ALOGE_IF(state.surface!=NULL,
1171 "adding a supported display, but rendering "
1172 "surface is provided (%p), ignoring it",
1173 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001174 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001175 // for supported (by hwc) displays we provide our
1176 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001177 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001178 }
1179
1180 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001181 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001182 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001183 state.type, hwcDisplayId, state.isSecure,
1184 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001185 hw->setLayerStack(state.layerStack);
1186 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001187 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001188 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001189 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001190 if (state.isVirtualDisplay()) {
1191 if (hwcDisplayId >= 0) {
1192 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1193 hw->getWidth(), hw->getHeight(),
1194 hw->getFormat());
1195 }
1196 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001197 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001198 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001199 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001200 }
1201 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001203 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204
Mathias Agopian84300952012-11-21 16:02:13 -08001205 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1206 // The transform hint might have changed for some layers
1207 // (either because a display has changed, or because a layer
1208 // as changed).
1209 //
1210 // Walk through all the layers in currentLayers,
1211 // and update their transform hint.
1212 //
1213 // If a layer is visible only on a single display, then that
1214 // display is used to calculate the hint, otherwise we use the
1215 // default display.
1216 //
1217 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1218 // the hint is set before we acquire a buffer from the surface texture.
1219 //
1220 // NOTE: layer transactions have taken place already, so we use their
1221 // drawing state. However, SurfaceFlinger's own transaction has not
1222 // happened yet, so we must use the current state layer list
1223 // (soon to become the drawing state list).
1224 //
1225 sp<const DisplayDevice> disp;
1226 uint32_t currentlayerStack = 0;
1227 for (size_t i=0; i<count; i++) {
1228 // NOTE: we rely on the fact that layers are sorted by
1229 // layerStack first (so we don't have to traverse the list
1230 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 const sp<Layer>& layer(currentLayers[i]);
1232 uint32_t layerStack = layer->drawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001233 if (i==0 || currentlayerStack != layerStack) {
1234 currentlayerStack = layerStack;
1235 // figure out if this layerstack is mirrored
1236 // (more than one display) if so, pick the default display,
1237 // if not, pick the only display it's on.
1238 disp.clear();
1239 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1240 sp<const DisplayDevice> hw(mDisplays[dpy]);
1241 if (hw->getLayerStack() == currentlayerStack) {
1242 if (disp == NULL) {
1243 disp = hw;
1244 } else {
1245 disp = getDefaultDisplayDevice();
1246 break;
1247 }
1248 }
1249 }
1250 }
1251 if (disp != NULL) {
1252 // presumably this means this layer is using a layerStack
1253 // that is not visible on any display
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001255 }
1256 }
1257 }
1258
1259
Mathias Agopian3559b072012-08-15 13:46:03 -07001260 /*
1261 * Perform our own transaction if needed
1262 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001263
Mathias Agopiancd60f992012-08-16 16:28:27 -07001264 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1265 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001266 // layers have been added
1267 mVisibleRegionsDirty = true;
1268 }
1269
1270 // some layers might have been removed, so
1271 // we need to update the regions they're exposing.
1272 if (mLayersRemoved) {
1273 mLayersRemoved = false;
1274 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001275 const size_t count = previousLayers.size();
1276 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 const sp<Layer>& layer(previousLayers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001278 if (currentLayers.indexOf(layer) < 0) {
1279 // this layer is not visible anymore
1280 // TODO: we could traverse the tree from front to back and
1281 // compute the actual visible region
1282 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001283 const Layer::State& s(layer->drawingState());
1284 Region visibleReg = s.transform.transform(
1285 Region(Rect(s.active.w, s.active.h)));
1286 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001287 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 }
1290
1291 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001292}
1293
1294void SurfaceFlinger::commitTransaction()
1295{
1296 if (!mLayersPendingRemoval.isEmpty()) {
1297 // Notify removed layers now that they can't be drawn from
1298 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1299 mLayersPendingRemoval[i]->onRemoved();
1300 }
1301 mLayersPendingRemoval.clear();
1302 }
1303
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001304 // If this transaction is part of a window animation then the next frame
1305 // we composite should be considered an animation as well.
1306 mAnimCompositionPending = mAnimTransactionPending;
1307
Mathias Agopian4fec8732012-06-29 14:12:52 -07001308 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001309 mTransactionPending = false;
1310 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001311 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312}
1313
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001315 const LayerVector& currentLayers, uint32_t layerStack,
1316 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317{
Mathias Agopian841cde52012-03-01 15:44:37 -08001318 ATRACE_CALL();
1319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 Region aboveOpaqueLayers;
1321 Region aboveCoveredLayers;
1322 Region dirty;
1323
Mathias Agopian87baae12012-07-31 12:38:26 -07001324 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325
1326 size_t i = currentLayers.size();
1327 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001328 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329
1330 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001331 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332
Jesse Hall01e29052013-02-19 16:13:35 -08001333 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001334 if (s.layerStack != layerStack)
1335 continue;
1336
Mathias Agopianab028732010-03-16 16:41:46 -07001337 /*
1338 * opaqueRegion: area of a surface that is fully opaque.
1339 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001341
1342 /*
1343 * visibleRegion: area of a surface that is visible on screen
1344 * and not fully transparent. This is essentially the layer's
1345 * footprint minus the opaque regions above it.
1346 * Areas covered by a translucent surface are considered visible.
1347 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001349
1350 /*
1351 * coveredRegion: area of a surface that is covered by all
1352 * visible regions above it (which includes the translucent areas).
1353 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001355
Jesse Halla8026d22012-09-25 13:25:04 -07001356 /*
1357 * transparentRegion: area of a surface that is hinted to be completely
1358 * transparent. This is only used to tell when the layer has no visible
1359 * non-transparent regions and can be removed from the layer list. It
1360 * does not affect the visibleRegion of this layer or any layers
1361 * beneath it. The hint may not be correct if apps don't respect the
1362 * SurfaceView restrictions (which, sadly, some don't).
1363 */
1364 Region transparentRegion;
1365
Mathias Agopianab028732010-03-16 16:41:46 -07001366
1367 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001368 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001369 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001370 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001372 if (!visibleRegion.isEmpty()) {
1373 // Remove the transparent area from the visible region
1374 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001375 const Transform tr(s.transform);
1376 if (tr.transformed()) {
1377 if (tr.preserveRects()) {
1378 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001379 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001380 } else {
1381 // transformation too complex, can't do the
1382 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001383 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001384 }
1385 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001386 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001387 }
Mathias Agopianab028732010-03-16 16:41:46 -07001388 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389
Mathias Agopianab028732010-03-16 16:41:46 -07001390 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001391 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001392 if (s.alpha==255 && !translucent &&
1393 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1394 // the opaque region is the layer's footprint
1395 opaqueRegion = visibleRegion;
1396 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 }
1398 }
1399
Mathias Agopianab028732010-03-16 16:41:46 -07001400 // Clip the covered region to the visible region
1401 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1402
1403 // Update aboveCoveredLayers for next (lower) layer
1404 aboveCoveredLayers.orSelf(visibleRegion);
1405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 // subtract the opaque region covered by the layers above us
1407 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408
1409 // compute this layer's dirty region
1410 if (layer->contentDirty) {
1411 // we need to invalidate the whole region
1412 dirty = visibleRegion;
1413 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001414 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 layer->contentDirty = false;
1416 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001417 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001418 * the exposed region consists of two components:
1419 * 1) what's VISIBLE now and was COVERED before
1420 * 2) what's EXPOSED now less what was EXPOSED before
1421 *
1422 * note that (1) is conservative, we start with the whole
1423 * visible region but only keep what used to be covered by
1424 * something -- which mean it may have been exposed.
1425 *
1426 * (2) handles areas that were not covered by anything but got
1427 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001428 */
Mathias Agopianab028732010-03-16 16:41:46 -07001429 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001430 const Region oldVisibleRegion = layer->visibleRegion;
1431 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001432 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1433 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 }
1435 dirty.subtractSelf(aboveOpaqueLayers);
1436
1437 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001438 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439
Mathias Agopianab028732010-03-16 16:41:46 -07001440 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001442
Jesse Halla8026d22012-09-25 13:25:04 -07001443 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 layer->setVisibleRegion(visibleRegion);
1445 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001446 layer->setVisibleNonTransparentRegion(
1447 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 }
1449
Mathias Agopian87baae12012-07-31 12:38:26 -07001450 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451}
1452
Mathias Agopian87baae12012-07-31 12:38:26 -07001453void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1454 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001455 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001456 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1457 if (hw->getLayerStack() == layerStack) {
1458 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001459 }
1460 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001461}
1462
1463void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001465 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466
Mathias Agopian4fec8732012-06-29 14:12:52 -07001467 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001468 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001469 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001470 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001471 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001472 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001473 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001474 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001475 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001476
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001477 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478}
1479
Mathias Agopianad456f92011-01-13 17:53:01 -08001480void SurfaceFlinger::invalidateHwcGeometry()
1481{
1482 mHwWorkListDirty = true;
1483}
1484
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001485
Mathias Agopiancd60f992012-08-16 16:28:27 -07001486void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001487 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488{
Mathias Agopian87baae12012-07-31 12:38:26 -07001489 Region dirtyRegion(inDirtyRegion);
1490
Mathias Agopianb8a55602009-06-26 19:06:36 -07001491 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001492 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493
Mathias Agopian42977342012-08-05 00:40:46 -07001494 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001495 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001496 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1497 // takes a rectangle, we must make sure to update that whole
1498 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001499 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001500 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001501 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001502 // We need to redraw the rectangle that will be updated
1503 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001504 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001505 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001506 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001507 } else {
1508 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001509 dirtyRegion.set(hw->bounds());
1510 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511 }
1512 }
1513
Mathias Agopiancd60f992012-08-16 16:28:27 -07001514 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001516 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001517 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001518
1519 // swap buffers (presentation)
1520 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521}
1522
Mathias Agopiancd60f992012-08-16 16:28:27 -07001523void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001524{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001525 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001526 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001527 HWComposer::LayerListIterator cur = hwc.begin(id);
1528 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001529
Mathias Agopian85d751c2012-08-29 16:59:24 -07001530 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1531 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001532 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001533
1534 // set the frame buffer
1535 glMatrixMode(GL_MODELVIEW);
1536 glLoadIdentity();
1537
1538 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001539 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001540 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001541 // when using overlays, we assume a fully transparent framebuffer
1542 // NOTE: we could reduce how much we need to clear, for instance
1543 // remove where there are opaque FB layers. however, on some
1544 // GPUs doing a "clean slate" glClear might be more efficient.
1545 // We'll revisit later if needed.
1546 glClearColor(0, 0, 0, 0);
1547 glClear(GL_COLOR_BUFFER_BIT);
1548 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001549 // we start with the whole screen area
1550 const Region bounds(hw->getBounds());
1551
1552 // we remove the scissor part
1553 // we're left with the letterbox region
1554 // (common case is that letterbox ends-up being empty)
1555 const Region letterbox(bounds.subtract(hw->getScissor()));
1556
1557 // compute the area to clear
1558 Region region(hw->undefinedRegion.merge(letterbox));
1559
1560 // but limit it to the dirty region
1561 region.andSelf(dirty);
1562
Mathias Agopianb9494d52012-04-18 02:28:45 -07001563 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001564 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001565 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001566 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001567 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001568 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001569
Mathias Agopian766dc492012-10-30 18:08:06 -07001570 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1571 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001572 // scissor on the main display. It should never be needed
1573 // anyways (though in theory it could since the API allows it).
1574 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001575 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001576 if (scissor != bounds) {
1577 // scissor doesn't match the screen's dimensions, so we
1578 // need to clear everything outside of it and enable
1579 // the GL scissor so we don't draw anything where we shouldn't
1580 const GLint height = hw->getHeight();
1581 glScissor(scissor.left, height - scissor.bottom,
1582 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001583 // enable scissor for this frame
1584 glEnable(GL_SCISSOR_TEST);
1585 }
1586 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001587 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001588
Mathias Agopian85d751c2012-08-29 16:59:24 -07001589 /*
1590 * and then, render the layers targeted at the framebuffer
1591 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001592
Mathias Agopian13127d82013-03-05 17:47:11 -08001593 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001594 const size_t count = layers.size();
1595 const Transform& tr = hw->getTransform();
1596 if (cur != end) {
1597 // we're using h/w composer
1598 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001599 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001600 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001601 if (!clip.isEmpty()) {
1602 switch (cur->getCompositionType()) {
1603 case HWC_OVERLAY: {
1604 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1605 && i
1606 && layer->isOpaque()
1607 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001608 // never clear the very first layer since we're
1609 // guaranteed the FB is already cleared
1610 layer->clearWithOpenGL(hw, clip);
1611 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001612 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001613 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001614 case HWC_FRAMEBUFFER: {
1615 layer->draw(hw, clip);
1616 break;
1617 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001618 case HWC_FRAMEBUFFER_TARGET: {
1619 // this should not happen as the iterator shouldn't
1620 // let us get there.
1621 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1622 break;
1623 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001624 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001625 }
1626 layer->setAcquireFence(hw, *cur);
1627 }
1628 } else {
1629 // we're not using h/w composer
1630 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001631 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001632 const Region clip(dirty.intersect(
1633 tr.transform(layer->visibleRegion)));
1634 if (!clip.isEmpty()) {
1635 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001636 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001637 }
1638 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001639
1640 // disable scissor at the end of the frame
1641 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642}
1643
Mathias Agopian55801e42012-08-27 18:54:24 -07001644void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1645 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001647 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001648 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001649 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001650 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001651
Mathias Agopian55801e42012-08-27 18:54:24 -07001652 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001653 Region::const_iterator it = region.begin();
1654 Region::const_iterator const end = region.end();
1655 while (it != end) {
1656 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001657 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001658 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1659 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1660 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1661 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001662 };
1663 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001664 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1665 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666}
1667
Mathias Agopianac9fa422013-02-11 16:40:36 -08001668void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1669 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001670 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001671 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001672{
Mathias Agopian96f08192010-06-02 23:28:45 -07001673 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001674 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001675
Mathias Agopian96f08192010-06-02 23:28:45 -07001676 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001677 Mutex::Autolock _l(mStateLock);
1678 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001679 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001680}
1681
Mathias Agopian13127d82013-03-05 17:47:11 -08001682status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683{
1684 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001685 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001687 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001688 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001689 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 return NO_ERROR;
1691 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001692 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693}
1694
Mathias Agopiandea20b12011-05-03 17:04:02 -07001695uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1696{
1697 return android_atomic_release_load(&mTransactionFlags);
1698}
1699
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1701{
1702 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1703}
1704
Mathias Agopianbb641242010-05-18 17:06:55 -07001705uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706{
1707 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1708 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001709 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 }
1711 return old;
1712}
1713
Mathias Agopian8b33f032012-07-24 20:43:54 -07001714void SurfaceFlinger::setTransactionState(
1715 const Vector<ComposerState>& state,
1716 const Vector<DisplayState>& displays,
1717 uint32_t flags)
1718{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001719 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001720 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001721 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001722
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001723 if (flags & eAnimation) {
1724 // For window updates that are part of an animation we must wait for
1725 // previous animation "frames" to be handled.
1726 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001727 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001728 if (CC_UNLIKELY(err != NO_ERROR)) {
1729 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001730 // caller after a few seconds.
1731 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1732 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001733 mAnimTransactionPending = false;
1734 break;
1735 }
1736 }
1737 }
1738
Mathias Agopiane57f2922012-08-09 16:29:12 -07001739 size_t count = displays.size();
1740 for (size_t i=0 ; i<count ; i++) {
1741 const DisplayState& s(displays[i]);
1742 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001743 }
1744
Mathias Agopiane57f2922012-08-09 16:29:12 -07001745 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001746 for (size_t i=0 ; i<count ; i++) {
1747 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001748 // Here we need to check that the interface we're given is indeed
1749 // one of our own. A malicious client could give us a NULL
1750 // IInterface, or one of its own or even one of our own but a
1751 // different type. All these situations would cause us to crash.
1752 //
1753 // NOTE: it would be better to use RTTI as we could directly check
1754 // that we have a Client*. however, RTTI is disabled in Android.
1755 if (s.client != NULL) {
1756 sp<IBinder> binder = s.client->asBinder();
1757 if (binder != NULL) {
1758 String16 desc(binder->getInterfaceDescriptor());
1759 if (desc == ISurfaceComposerClient::descriptor) {
1760 sp<Client> client( static_cast<Client *>(s.client.get()) );
1761 transactionFlags |= setClientStateLocked(client, s.state);
1762 }
1763 }
1764 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001765 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001766
Mathias Agopian386aa982011-11-07 21:58:03 -08001767 if (transactionFlags) {
1768 // this triggers the transaction
1769 setTransactionFlags(transactionFlags);
1770
1771 // if this is a synchronous transaction, wait for it to take effect
1772 // before returning.
1773 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001774 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001775 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001776 if (flags & eAnimation) {
1777 mAnimTransactionPending = true;
1778 }
1779 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001780 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1781 if (CC_UNLIKELY(err != NO_ERROR)) {
1782 // just in case something goes wrong in SF, return to the
1783 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001784 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1785 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001786 break;
1787 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001788 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001789 }
1790}
1791
Mathias Agopiane57f2922012-08-09 16:29:12 -07001792uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1793{
Jesse Hall9a143922012-10-04 16:29:19 -07001794 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1795 if (dpyIdx < 0)
1796 return 0;
1797
Mathias Agopiane57f2922012-08-09 16:29:12 -07001798 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001799 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001800 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001801 const uint32_t what = s.what;
1802 if (what & DisplayState::eSurfaceChanged) {
1803 if (disp.surface->asBinder() != s.surface->asBinder()) {
1804 disp.surface = s.surface;
1805 flags |= eDisplayTransactionNeeded;
1806 }
1807 }
1808 if (what & DisplayState::eLayerStackChanged) {
1809 if (disp.layerStack != s.layerStack) {
1810 disp.layerStack = s.layerStack;
1811 flags |= eDisplayTransactionNeeded;
1812 }
1813 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001814 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001815 if (disp.orientation != s.orientation) {
1816 disp.orientation = s.orientation;
1817 flags |= eDisplayTransactionNeeded;
1818 }
1819 if (disp.frame != s.frame) {
1820 disp.frame = s.frame;
1821 flags |= eDisplayTransactionNeeded;
1822 }
1823 if (disp.viewport != s.viewport) {
1824 disp.viewport = s.viewport;
1825 flags |= eDisplayTransactionNeeded;
1826 }
1827 }
1828 }
1829 return flags;
1830}
1831
1832uint32_t SurfaceFlinger::setClientStateLocked(
1833 const sp<Client>& client,
1834 const layer_state_t& s)
1835{
1836 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001837 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001838 if (layer != 0) {
1839 const uint32_t what = s.what;
1840 if (what & layer_state_t::ePositionChanged) {
1841 if (layer->setPosition(s.x, s.y))
1842 flags |= eTraversalNeeded;
1843 }
1844 if (what & layer_state_t::eLayerChanged) {
1845 // NOTE: index needs to be calculated before we update the state
1846 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1847 if (layer->setLayer(s.z)) {
1848 mCurrentState.layersSortedByZ.removeAt(idx);
1849 mCurrentState.layersSortedByZ.add(layer);
1850 // we need traversal (state changed)
1851 // AND transaction (list changed)
1852 flags |= eTransactionNeeded|eTraversalNeeded;
1853 }
1854 }
1855 if (what & layer_state_t::eSizeChanged) {
1856 if (layer->setSize(s.w, s.h)) {
1857 flags |= eTraversalNeeded;
1858 }
1859 }
1860 if (what & layer_state_t::eAlphaChanged) {
1861 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1862 flags |= eTraversalNeeded;
1863 }
1864 if (what & layer_state_t::eMatrixChanged) {
1865 if (layer->setMatrix(s.matrix))
1866 flags |= eTraversalNeeded;
1867 }
1868 if (what & layer_state_t::eTransparentRegionChanged) {
1869 if (layer->setTransparentRegionHint(s.transparentRegion))
1870 flags |= eTraversalNeeded;
1871 }
1872 if (what & layer_state_t::eVisibilityChanged) {
1873 if (layer->setFlags(s.flags, s.mask))
1874 flags |= eTraversalNeeded;
1875 }
1876 if (what & layer_state_t::eCropChanged) {
1877 if (layer->setCrop(s.crop))
1878 flags |= eTraversalNeeded;
1879 }
1880 if (what & layer_state_t::eLayerStackChanged) {
1881 // NOTE: index needs to be calculated before we update the state
1882 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1883 if (layer->setLayerStack(s.layerStack)) {
1884 mCurrentState.layersSortedByZ.removeAt(idx);
1885 mCurrentState.layersSortedByZ.add(layer);
1886 // we need traversal (state changed)
1887 // AND transaction (list changed)
1888 flags |= eTransactionNeeded|eTraversalNeeded;
1889 }
1890 }
1891 }
1892 return flags;
1893}
1894
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001895status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001896 const String8& name,
1897 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001898 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1899 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001901 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001902 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001903 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001904 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001905 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001906 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001907
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001908 status_t result = NO_ERROR;
1909
1910 sp<Layer> layer;
1911
Mathias Agopian3165cc22012-08-08 19:42:09 -07001912 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1913 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914 result = createNormalLayer(client,
1915 name, w, h, flags, format,
1916 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001918 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001919 result = createDimLayer(client,
1920 name, w, h, flags,
1921 handle, gbp, &layer);
1922 break;
1923 default:
1924 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 break;
1926 }
1927
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001928 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001929 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001930 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001931 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001932 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933}
1934
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001935status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1936 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1937 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938{
1939 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001940 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001941 case PIXEL_FORMAT_TRANSPARENT:
1942 case PIXEL_FORMAT_TRANSLUCENT:
1943 format = PIXEL_FORMAT_RGBA_8888;
1944 break;
1945 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001946#ifdef NO_RGBX_8888
1947 format = PIXEL_FORMAT_RGB_565;
1948#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001949 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001950#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951 break;
1952 }
1953
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001954#ifdef NO_RGBX_8888
1955 if (format == PIXEL_FORMAT_RGBX_8888)
1956 format = PIXEL_FORMAT_RGBA_8888;
1957#endif
1958
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001959 *outLayer = new Layer(this, client, name, w, h, flags);
1960 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1961 if (err == NO_ERROR) {
1962 *handle = (*outLayer)->getHandle();
1963 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001964 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001965
1966 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1967 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001968}
1969
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001970status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1971 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1972 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001973{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001974 *outLayer = new LayerDim(this, client, name, w, h, flags);
1975 *handle = (*outLayer)->getHandle();
1976 *gbp = (*outLayer)->getBufferQueue();
1977 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001978}
1979
Mathias Agopianac9fa422013-02-11 16:40:36 -08001980status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001981{
Mathias Agopian67106042013-03-14 19:18:13 -07001982 // called by the window manager when it wants to remove a Layer
1983 status_t err = NO_ERROR;
1984 sp<Layer> l(client->getLayerUser(handle));
1985 if (l != NULL) {
1986 err = removeLayer(l);
1987 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1988 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001989 }
1990 return err;
1991}
1992
Mathias Agopian13127d82013-03-05 17:47:11 -08001993status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001994{
Mathias Agopian67106042013-03-14 19:18:13 -07001995 // called by ~LayerCleaner() when all references to the IBinder (handle)
1996 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001997 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001998 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001999 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002000 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002001 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002002 "error removing layer=%p (%s)", l.get(), strerror(-err));
2003 }
2004 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002005}
2006
Mathias Agopianb60314a2012-04-10 22:09:54 -07002007// ---------------------------------------------------------------------------
2008
Andy McFadden13a082e2012-08-24 10:16:42 -07002009void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002010 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002011 Vector<ComposerState> state;
2012 Vector<DisplayState> displays;
2013 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002014 d.what = DisplayState::eDisplayProjectionChanged |
2015 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002016 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002017 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002018 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002019 d.frame.makeInvalid();
2020 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002021 displays.add(d);
2022 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002023 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002024}
2025
2026void SurfaceFlinger::initializeDisplays() {
2027 class MessageScreenInitialized : public MessageBase {
2028 SurfaceFlinger* flinger;
2029 public:
2030 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2031 virtual bool handler() {
2032 flinger->onInitializeDisplays();
2033 return true;
2034 }
2035 };
2036 sp<MessageBase> msg = new MessageScreenInitialized(this);
2037 postMessageAsync(msg); // we may be called from main thread, use async message
2038}
2039
2040
Mathias Agopiancde87a32012-09-13 14:09:01 -07002041void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002042 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2043 if (hw->isScreenAcquired()) {
2044 // this is expected, e.g. when power manager wakes up during boot
2045 ALOGD(" screen was previously acquired");
2046 return;
2047 }
2048
Mathias Agopian42977342012-08-05 00:40:46 -07002049 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002050 int32_t type = hw->getDisplayType();
2051 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2052 // built-in display, tell the HWC
2053 getHwComposer().acquire(type);
2054
2055 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2056 // FIXME: eventthread only knows about the main display right now
2057 mEventThread->onScreenAcquired();
2058 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002059 }
Mathias Agopian20128302012-08-13 18:32:13 -07002060 mVisibleRegionsDirty = true;
2061 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002062}
2063
Mathias Agopiancde87a32012-09-13 14:09:01 -07002064void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002065 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2066 if (!hw->isScreenAcquired()) {
2067 ALOGD(" screen was previously released");
2068 return;
2069 }
2070
2071 hw->releaseScreen();
2072 int32_t type = hw->getDisplayType();
2073 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2074 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002075 // FIXME: eventthread only knows about the main display right now
2076 mEventThread->onScreenReleased();
2077 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002078
2079 // built-in display, tell the HWC
2080 getHwComposer().release(type);
2081 }
2082 mVisibleRegionsDirty = true;
2083 // from this point on, SF will stop drawing on this display
2084}
2085
2086void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2087 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002088 SurfaceFlinger& mFlinger;
2089 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002090 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002091 MessageScreenAcquired(SurfaceFlinger& flinger,
2092 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002093 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002094 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2095 if (hw == NULL) {
2096 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2097 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2098 ALOGW("Attempt to unblank virtual display");
2099 } else {
2100 mFlinger.onScreenAcquired(hw);
2101 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002102 return true;
2103 }
2104 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002105 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2106 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107}
2108
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002109void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002110 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002111 SurfaceFlinger& mFlinger;
2112 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002113 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002114 MessageScreenReleased(SurfaceFlinger& flinger,
2115 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002116 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002117 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2118 if (hw == NULL) {
2119 ALOGE("Attempt to blank null display %p", mDisplay.get());
2120 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2121 ALOGW("Attempt to blank virtual display");
2122 } else {
2123 mFlinger.onScreenReleased(hw);
2124 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002125 return true;
2126 }
2127 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002128 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2129 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002130}
2131
2132// ---------------------------------------------------------------------------
2133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002134status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2135{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002136 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002137 char buffer[SIZE];
2138 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002139
2140 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002141 snprintf(buffer, SIZE, "Permission Denial: "
2142 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2143 IPCThreadState::self()->getCallingPid(),
2144 IPCThreadState::self()->getCallingUid());
2145 result.append(buffer);
2146 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002147 // Try to get the main lock, but don't insist if we can't
2148 // (this would indicate SF is stuck, but we want to be able to
2149 // print something in dumpsys).
2150 int retry = 3;
2151 while (mStateLock.tryLock()<0 && --retry>=0) {
2152 usleep(1000000);
2153 }
2154 const bool locked(retry >= 0);
2155 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002156 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002157 "SurfaceFlinger appears to be unresponsive, "
2158 "dumping anyways (no locks held)\n");
2159 result.append(buffer);
2160 }
2161
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002162 bool dumpAll = true;
2163 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002164 size_t numArgs = args.size();
2165 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002166 if ((index < numArgs) &&
2167 (args[index] == String16("--list"))) {
2168 index++;
2169 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002170 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002171 }
2172
2173 if ((index < numArgs) &&
2174 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002175 index++;
2176 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002177 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002178 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002179
2180 if ((index < numArgs) &&
2181 (args[index] == String16("--latency-clear"))) {
2182 index++;
2183 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002184 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002185 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002186 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002187
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002188 if (dumpAll) {
2189 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002190 }
2191
Mathias Agopian9795c422009-08-26 16:36:26 -07002192 if (locked) {
2193 mStateLock.unlock();
2194 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195 }
2196 write(fd, result.string(), result.size());
2197 return NO_ERROR;
2198}
2199
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002200void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2201 String8& result, char* buffer, size_t SIZE) const
2202{
2203 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2204 const size_t count = currentLayers.size();
2205 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002206 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002207 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2208 result.append(buffer);
2209 }
2210}
2211
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002212void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2213 String8& result, char* buffer, size_t SIZE) const
2214{
2215 String8 name;
2216 if (index < args.size()) {
2217 name = String8(args[index]);
2218 index++;
2219 }
2220
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002221 const nsecs_t period =
2222 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2223 result.appendFormat("%lld\n", period);
2224
2225 if (name.isEmpty()) {
2226 mAnimFrameTracker.dump(result);
2227 } else {
2228 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2229 const size_t count = currentLayers.size();
2230 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002231 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002232 if (name == layer->getName()) {
2233 layer->dumpStats(result, buffer, SIZE);
2234 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002235 }
2236 }
2237}
2238
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002239void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002240 String8& result, char* buffer, size_t SIZE)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002241{
2242 String8 name;
2243 if (index < args.size()) {
2244 name = String8(args[index]);
2245 index++;
2246 }
2247
2248 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2249 const size_t count = currentLayers.size();
2250 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002251 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002252 if (name.isEmpty() || (name == layer->getName())) {
2253 layer->clearStats();
2254 }
2255 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002256
2257 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002258}
2259
Andy McFadden4803b742012-09-24 19:07:20 -07002260/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2261{
2262 static const char* config =
2263 " [sf"
2264#ifdef NO_RGBX_8888
2265 " NO_RGBX_8888"
2266#endif
2267#ifdef HAS_CONTEXT_PRIORITY
2268 " HAS_CONTEXT_PRIORITY"
2269#endif
2270#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2271 " NEVER_DEFAULT_TO_ASYNC_MODE"
2272#endif
2273#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2274 " TARGET_DISABLE_TRIPLE_BUFFERING"
2275#endif
2276 "]";
2277 result.append(config);
2278}
2279
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002280void SurfaceFlinger::dumpAllLocked(
2281 String8& result, char* buffer, size_t SIZE) const
2282{
2283 // figure out if we're stuck somewhere
2284 const nsecs_t now = systemTime();
2285 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2286 const nsecs_t inTransaction(mDebugInTransaction);
2287 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2288 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2289
2290 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002291 * Dump library configuration.
2292 */
2293 result.append("Build configuration:");
2294 appendSfConfigString(result);
2295 appendUiConfigString(result);
2296 appendGuiConfigString(result);
2297 result.append("\n");
2298
Mathias Agopianca088332013-03-28 17:44:13 -07002299 result.append("Sync configuration: ");
2300 result.append(SyncFeatures::getInstance().toString());
2301 result.append("\n");
2302
Andy McFadden4803b742012-09-24 19:07:20 -07002303 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002304 * Dump the visible layer list
2305 */
2306 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2307 const size_t count = currentLayers.size();
2308 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2309 result.append(buffer);
2310 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002311 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002312 layer->dump(result, buffer, SIZE);
2313 }
2314
2315 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002316 * Dump Display state
2317 */
2318
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002319 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2320 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002321 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2322 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002323 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002324 }
2325
2326 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002327 * Dump SurfaceFlinger global state
2328 */
2329
2330 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2331 result.append(buffer);
2332
Mathias Agopian888c8222012-08-04 21:10:38 -07002333 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002334 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002335 const GLExtensions& extensions(GLExtensions::getInstance());
Mathias Agopianca088332013-03-28 17:44:13 -07002336
2337 snprintf(buffer, SIZE, "EGL implementation : %s\n",
2338 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2339 result.append(buffer);
2340 snprintf(buffer, SIZE, "%s\n",
2341 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
2342 result.append(buffer);
2343
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002344 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2345 extensions.getVendor(),
2346 extensions.getRenderer(),
2347 extensions.getVersion());
2348 result.append(buffer);
Mathias Agopianca088332013-03-28 17:44:13 -07002349 snprintf(buffer, SIZE, "%s\n", extensions.getExtension());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002350 result.append(buffer);
2351
Mathias Agopian42977342012-08-05 00:40:46 -07002352 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002353 snprintf(buffer, SIZE,
2354 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002355 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356 result.append(buffer);
2357 snprintf(buffer, SIZE,
2358 " last eglSwapBuffers() time: %f us\n"
2359 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002360 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002361 " refresh-rate : %f fps\n"
2362 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002363 " y-dpi : %f\n"
2364 " EGL_NATIVE_VISUAL_ID : %d\n"
2365 " gpu_to_cpu_unsupported : %d\n"
2366 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002367 mLastSwapBufferTime/1000.0,
2368 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002369 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002370 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2371 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002372 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2373 mEGLNativeVisualId,
2374 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002375 result.append(buffer);
2376
2377 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2378 inSwapBuffersDuration/1000.0);
2379 result.append(buffer);
2380
2381 snprintf(buffer, SIZE, " transaction time: %f us\n",
2382 inTransactionDuration/1000.0);
2383 result.append(buffer);
2384
2385 /*
2386 * VSYNC state
2387 */
2388 mEventThread->dump(result, buffer, SIZE);
2389
2390 /*
2391 * Dump HWComposer state
2392 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002393 snprintf(buffer, SIZE, "h/w composer state:\n");
2394 result.append(buffer);
2395 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2396 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2397 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2398 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002399 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002400
2401 /*
2402 * Dump gralloc state
2403 */
2404 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2405 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002406}
2407
Mathias Agopian13127d82013-03-05 17:47:11 -08002408const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002409SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002410 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002411 wp<IBinder> dpy;
2412 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2413 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2414 dpy = mDisplays.keyAt(i);
2415 break;
2416 }
2417 }
2418 if (dpy == NULL) {
2419 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2420 // Just use the primary display so we have something to return
2421 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2422 }
2423 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002424}
2425
Keun young Park63f165f2012-08-31 10:53:36 -07002426bool SurfaceFlinger::startDdmConnection()
2427{
2428 void* libddmconnection_dso =
2429 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2430 if (!libddmconnection_dso) {
2431 return false;
2432 }
2433 void (*DdmConnection_start)(const char* name);
2434 DdmConnection_start =
2435 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2436 if (!DdmConnection_start) {
2437 dlclose(libddmconnection_dso);
2438 return false;
2439 }
2440 (*DdmConnection_start)(getServiceName());
2441 return true;
2442}
2443
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444status_t SurfaceFlinger::onTransact(
2445 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2446{
2447 switch (code) {
2448 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002449 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002450 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002451 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002452 case BLANK:
2453 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002454 {
2455 // codes that require permission check
2456 IPCThreadState* ipc = IPCThreadState::self();
2457 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002458 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002459 if ((uid != AID_GRAPHICS) &&
2460 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002461 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002462 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2463 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002465 break;
2466 }
2467 case CAPTURE_SCREEN:
2468 {
2469 // codes that require permission check
2470 IPCThreadState* ipc = IPCThreadState::self();
2471 const int pid = ipc->getCallingPid();
2472 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002473 if ((uid != AID_GRAPHICS) &&
2474 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002475 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002476 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2477 return PERMISSION_DENIED;
2478 }
2479 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 }
2481 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2484 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002485 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002486 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002487 IPCThreadState* ipc = IPCThreadState::self();
2488 const int pid = ipc->getCallingPid();
2489 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002490 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002491 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002492 return PERMISSION_DENIED;
2493 }
2494 int n;
2495 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002496 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002497 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498 return NO_ERROR;
2499 case 1002: // SHOW_UPDATES
2500 n = data.readInt32();
2501 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002502 invalidateHwcGeometry();
2503 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002506 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002507 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002509 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002510 setTransactionFlags(
2511 eTransactionNeeded|
2512 eDisplayTransactionNeeded|
2513 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002514 return NO_ERROR;
2515 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002516 case 1006:{ // send empty update
2517 signalRefresh();
2518 return NO_ERROR;
2519 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002520 case 1008: // toggle use of hw composer
2521 n = data.readInt32();
2522 mDebugDisableHWC = n ? 1 : 0;
2523 invalidateHwcGeometry();
2524 repaintEverything();
2525 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002526 case 1009: // toggle use of transform hint
2527 n = data.readInt32();
2528 mDebugDisableTransformHint = n ? 1 : 0;
2529 invalidateHwcGeometry();
2530 repaintEverything();
2531 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002533 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534 reply->writeInt32(0);
2535 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002536 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002537 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002538 return NO_ERROR;
2539 case 1013: {
2540 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002541 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2542 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543 }
2544 return NO_ERROR;
2545 }
2546 }
2547 return err;
2548}
2549
Mathias Agopian53331da2011-08-22 21:44:41 -07002550void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002551 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002552 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002553}
2554
Mathias Agopian59119e62010-10-11 12:37:43 -07002555// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002556// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002557// ---------------------------------------------------------------------------
2558
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002559status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2560 const sp<IGraphicBufferProducer>& producer,
2561 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002562 uint32_t minLayerZ, uint32_t maxLayerZ,
2563 bool isCpuConsumer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002564
2565 if (CC_UNLIKELY(display == 0))
2566 return BAD_VALUE;
2567
2568 if (CC_UNLIKELY(producer == 0))
2569 return BAD_VALUE;
2570
2571 class MessageCaptureScreen : public MessageBase {
2572 SurfaceFlinger* flinger;
2573 sp<IBinder> display;
2574 sp<IGraphicBufferProducer> producer;
2575 uint32_t reqWidth, reqHeight;
2576 uint32_t minLayerZ,maxLayerZ;
Mathias Agopianabe815d2013-03-19 22:22:21 -07002577 bool isCpuConsumer;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002578 status_t result;
2579 public:
2580 MessageCaptureScreen(SurfaceFlinger* flinger,
2581 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, bool isCpuConsumer)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002585 : flinger(flinger), display(display), producer(producer),
2586 reqWidth(reqWidth), reqHeight(reqHeight),
2587 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopianabe815d2013-03-19 22:22:21 -07002588 isCpuConsumer(isCpuConsumer),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002589 result(PERMISSION_DENIED)
2590 {
2591 }
2592 status_t getResult() const {
2593 return result;
2594 }
2595 virtual bool handler() {
2596 Mutex::Autolock _l(flinger->mStateLock);
2597 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopianb4b17302013-03-20 18:36:41 -07002598
Mathias Agopianed985572013-03-22 00:24:39 -07002599 bool useReadPixels = false;
2600 if (isCpuConsumer) {
2601 bool formatSupportedBytBitmap =
2602 (flinger->mEGLNativeVisualId == HAL_PIXEL_FORMAT_RGBA_8888) ||
2603 (flinger->mEGLNativeVisualId == HAL_PIXEL_FORMAT_RGBX_8888);
2604 if (formatSupportedBytBitmap == false) {
2605 // the pixel format we have is not compatible with
2606 // Bitmap.java, which is the likely client of this API,
2607 // so we just revert to glReadPixels() in that case.
2608 useReadPixels = true;
2609 }
2610 if (flinger->mGpuToCpuSupported == false) {
2611 // When we know the GL->CPU path works, we can call
2612 // captureScreenImplLocked() directly, instead of using the
2613 // glReadPixels() workaround.
2614 useReadPixels = true;
2615 }
2616 }
2617
2618 if (!useReadPixels) {
Mathias Agopianb4b17302013-03-20 18:36:41 -07002619 result = flinger->captureScreenImplLocked(hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002620 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
2621 } else {
Mathias Agopianb4b17302013-03-20 18:36:41 -07002622 result = flinger->captureScreenImplCpuConsumerLocked(hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002623 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
2624 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002625 return true;
2626 }
2627 };
2628
2629 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002630 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2631 isCpuConsumer);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002632 status_t res = postMessageSync(msg);
2633 if (res == NO_ERROR) {
2634 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2635 }
2636 return res;
2637}
2638
2639status_t SurfaceFlinger::captureScreenImplLocked(
2640 const sp<const DisplayDevice>& hw,
2641 const sp<IGraphicBufferProducer>& producer,
2642 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002643 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002644{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002645 ATRACE_CALL();
2646
Mathias Agopian74c40c02010-09-29 13:02:36 -07002647 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002648 const uint32_t hw_w = hw->getWidth();
2649 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002650
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002651 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002652 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002653 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002654 return PERMISSION_DENIED;
2655 }
2656
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002657 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2658 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2659 reqWidth, reqHeight, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002660 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002661 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002662
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002663 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2664 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2665 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002666
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002667 // Create a surface to render into
2668 sp<Surface> surface = new Surface(producer);
2669 ANativeWindow* const window = surface.get();
2670
2671 // set the buffer size to what the user requested
2672 native_window_set_buffers_user_dimensions(window, reqWidth, reqHeight);
2673
2674 // and create the corresponding EGLSurface
2675 EGLSurface eglSurface = eglCreateWindowSurface(
2676 mEGLDisplay, mEGLConfig, window, NULL);
2677 if (eglSurface == EGL_NO_SURFACE) {
2678 ALOGE("captureScreenImplLocked: eglCreateWindowSurface() failed 0x%4x",
2679 eglGetError());
2680 return BAD_VALUE;
2681 }
2682
2683 if (!eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) {
2684 ALOGE("captureScreenImplLocked: eglMakeCurrent() failed 0x%4x",
2685 eglGetError());
2686 eglDestroySurface(mEGLDisplay, eglSurface);
2687 return BAD_VALUE;
2688 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002689
Mathias Agopian74c40c02010-09-29 13:02:36 -07002690 // make sure to clear all GL error flags
2691 while ( glGetError() != GL_NO_ERROR ) ;
2692
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002693 // set-up our viewport
2694 glViewport(0, 0, reqWidth, reqHeight);
2695 glMatrixMode(GL_PROJECTION);
2696 glLoadIdentity();
2697 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2698 glMatrixMode(GL_MODELVIEW);
2699 glLoadIdentity();
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002700
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002701 // redraw the screen entirely...
2702 glDisable(GL_TEXTURE_EXTERNAL_OES);
2703 glDisable(GL_TEXTURE_2D);
2704 glClearColor(0,0,0,1);
2705 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002706
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002707 const LayerVector& layers( mDrawingState.layersSortedByZ );
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002708 const size_t count = layers.size();
2709 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002710 const sp<Layer>& layer(layers[i]);
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002711 const Layer::State& state(layer->drawingState());
2712 if (state.layerStack == hw->getLayerStack()) {
2713 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
Mathias Agopiana493be52013-03-15 16:08:10 -07002714 if (layer->isVisible()) {
2715 if (filtering) layer->setFiltering(true);
2716 layer->draw(hw);
2717 if (filtering) layer->setFiltering(false);
2718 }
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002719 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002720 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002721 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002722
Mathias Agopiand5776412013-03-08 13:54:46 -08002723 // compositionComplete is needed for older driver
2724 hw->compositionComplete();
2725
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002726 // and finishing things up...
2727 if (eglSwapBuffers(mEGLDisplay, eglSurface) != EGL_TRUE) {
2728 ALOGE("captureScreenImplLocked: eglSwapBuffers() failed 0x%4x",
2729 eglGetError());
2730 eglDestroySurface(mEGLDisplay, eglSurface);
2731 return BAD_VALUE;
2732 }
2733
2734 eglDestroySurface(mEGLDisplay, eglSurface);
Mathias Agopianabe815d2013-03-19 22:22:21 -07002735
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002736 return NO_ERROR;
2737}
2738
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002739
Mathias Agopianabe815d2013-03-19 22:22:21 -07002740status_t SurfaceFlinger::captureScreenImplCpuConsumerLocked(
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002741 const sp<const DisplayDevice>& hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002742 const sp<IGraphicBufferProducer>& producer,
2743 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002744 uint32_t minLayerZ, uint32_t maxLayerZ)
2745{
2746 ATRACE_CALL();
2747
2748 if (!GLExtensions::getInstance().haveFramebufferObject()) {
2749 return INVALID_OPERATION;
2750 }
2751
2752 // create the texture that will receive the screenshot, later we'll
2753 // attach a FBO to it so we can call glReadPixels().
2754 GLuint tname;
2755 glGenTextures(1, &tname);
2756 glBindTexture(GL_TEXTURE_2D, tname);
2757 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2758 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2759
2760 // the GLConsumer will provide the BufferQueue
2761 sp<GLConsumer> consumer = new GLConsumer(tname, true, GL_TEXTURE_2D);
2762 consumer->getBufferQueue()->setDefaultBufferFormat(HAL_PIXEL_FORMAT_RGBA_8888);
2763
2764 // call the new screenshot taking code, passing a BufferQueue to it
2765 status_t result = captureScreenImplLocked(hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002766 consumer->getBufferQueue(), reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002767
2768 if (result == NO_ERROR) {
2769 result = consumer->updateTexImage();
2770 if (result == NO_ERROR) {
2771 // create a FBO
2772 GLuint name;
2773 glGenFramebuffersOES(1, &name);
2774 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2775 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2776 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
2777
Mathias Agopianabe815d2013-03-19 22:22:21 -07002778 reqWidth = consumer->getCurrentBuffer()->getWidth();
2779 reqHeight = consumer->getCurrentBuffer()->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002780
Mathias Agopianabe815d2013-03-19 22:22:21 -07002781 {
2782 // in this block we render the screenshot into the
2783 // CpuConsumer using glReadPixels from our GLConsumer,
2784 // Some older drivers don't support the GL->CPU path so
2785 // have to wrap it with a CPU->CPU path, which is what
2786 // glReadPixels essentially is
2787
2788 sp<Surface> sur = new Surface(producer);
2789 ANativeWindow* window = sur.get();
2790 ANativeWindowBuffer* buffer;
2791 void* vaddr;
2792
2793 if (native_window_api_connect(window,
2794 NATIVE_WINDOW_API_CPU) == NO_ERROR) {
2795 int err = 0;
2796 err = native_window_set_buffers_dimensions(window,
2797 reqWidth, reqHeight);
2798 err |= native_window_set_buffers_format(window,
2799 HAL_PIXEL_FORMAT_RGBA_8888);
2800 err |= native_window_set_usage(window,
2801 GRALLOC_USAGE_SW_READ_OFTEN |
2802 GRALLOC_USAGE_SW_WRITE_OFTEN);
2803
2804 if (err == NO_ERROR) {
2805 if (native_window_dequeue_buffer_and_wait(window,
2806 &buffer) == NO_ERROR) {
2807 sp<GraphicBuffer> buf =
2808 static_cast<GraphicBuffer*>(buffer);
2809 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2810 &vaddr) == NO_ERROR) {
2811 if (buffer->stride != int(reqWidth)) {
2812 // we're unlucky here, glReadPixels is
2813 // not able to deal with a stride not
2814 // equal to the width.
2815 uint32_t* tmp = new uint32_t[reqWidth*reqHeight];
2816 if (tmp != NULL) {
2817 glReadPixels(0, 0, reqWidth, reqHeight,
2818 GL_RGBA, GL_UNSIGNED_BYTE, tmp);
2819 for (size_t y=0 ; y<reqHeight ; y++) {
2820 memcpy((uint32_t*)vaddr + y*buffer->stride,
2821 tmp + y*reqWidth, reqWidth*4);
2822 }
2823 delete [] tmp;
2824 }
2825 } else {
2826 glReadPixels(0, 0, reqWidth, reqHeight,
2827 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
2828 }
2829 buf->unlock();
2830 }
2831 window->queueBuffer(window, buffer, -1);
2832 }
2833 }
2834 native_window_api_disconnect(window, NATIVE_WINDOW_API_CPU);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002835 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002836 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002837
2838 // back to main framebuffer
2839 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2840 glDeleteFramebuffersOES(1, &name);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002841 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002842 }
2843
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002844 glDeleteTextures(1, &tname);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002845
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002846 DisplayDevice::makeCurrent(mEGLDisplay,
2847 getDefaultDisplayDevice(), mEGLContext);
2848
Mathias Agopian74c40c02010-09-29 13:02:36 -07002849 return result;
2850}
2851
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002852// ---------------------------------------------------------------------------
2853
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002854SurfaceFlinger::LayerVector::LayerVector() {
2855}
2856
2857SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002858 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002859}
2860
2861int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2862 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002863{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002864 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002865 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2866 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002867
2868 uint32_t ls = l->currentState().layerStack;
2869 uint32_t rs = r->currentState().layerStack;
2870 if (ls != rs)
2871 return ls - rs;
2872
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002873 uint32_t lz = l->currentState().z;
2874 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002875 if (lz != rz)
2876 return lz - rz;
2877
2878 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002879}
2880
2881// ---------------------------------------------------------------------------
2882
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002883SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2884 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002885}
2886
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002887SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002888 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002889 viewport.makeInvalid();
2890 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002891}
2892
2893// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002894
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002895}; // namespace android