blob: 8149a6e76796423cec2ff4f0ec6131d9796e35e0 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
57#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080058#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070059#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070060#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080061#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070062#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070068#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana67932f2011-04-20 14:20:59 -070071
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080072#define EGL_VERSION_HW_ANDROID 0x3143
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
76namespace 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 Agopianda27af92012-09-13 18:17:13 -0700495 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
496 "couldn't create EGLContext");
497
Mathias Agopiancde87a32012-09-13 14:09:01 -0700498 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700499 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700500 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700501 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700502 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700503 // All non-virtual displays are currently considered secure.
504 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800505 createBuiltinDisplayLocked(type);
506 wp<IBinder> token = mBuiltinDisplays[i];
507
Mathias Agopianf5a33922012-09-19 18:16:22 -0700508 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700509 type, isSecure, token, new FramebufferSurface(*mHwc, i),
510 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700511 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700512 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700513 // for displays other than the main display, so we always
514 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700515 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700516 hw->acquireScreen();
517 }
518 mDisplays.add(token, hw);
519 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700520 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700521
Mathias Agopianf5a33922012-09-19 18:16:22 -0700522 // we need a GL context current in a few places, when initializing
523 // OpenGL ES (see below), or creating a layer,
524 // or when a texture is (asynchronously) destroyed, and for that
525 // we need a valid surface, so it's convenient to use the main display
526 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700527 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700528
Mathias Agopiana4912602012-07-12 14:25:33 -0700529 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700530 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
531 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800532
Mathias Agopian028508c2012-07-25 21:12:12 -0700533 // start the EventThread
534 mEventThread = new EventThread(this);
535 mEventQueue.setEventThread(mEventThread);
536
Mathias Agopian92a979a2012-08-02 18:32:23 -0700537 // initialize our drawing state
538 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700539
Mathias Agopiancde87a32012-09-13 14:09:01 -0700540
Mathias Agopiana4912602012-07-12 14:25:33 -0700541 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800542 mReadyToRunBarrier.open();
543
Andy McFadden13a082e2012-08-24 10:16:42 -0700544 // set initial conditions (e.g. unblank default device)
545 initializeDisplays();
546
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700547 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700548 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700549
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 return NO_ERROR;
551}
552
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700553int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
554 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
555 type : mHwc->allocateDisplayId();
556}
557
Mathias Agopiana67e4182012-06-19 17:26:12 -0700558void SurfaceFlinger::startBootAnim() {
559 // start boot animation
560 property_set("service.bootanim.exit", "0");
561 property_set("ctl.start", "bootanim");
562}
563
Mathias Agopiana4912602012-07-12 14:25:33 -0700564uint32_t SurfaceFlinger::getMaxTextureSize() const {
565 return mMaxTextureSize;
566}
567
568uint32_t SurfaceFlinger::getMaxViewportDims() const {
569 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
570 mMaxViewportDims[0] : mMaxViewportDims[1];
571}
572
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800574
Jamie Gennis582270d2011-08-17 18:19:00 -0700575bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800576 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800577 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800578 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700579 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800580}
581
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700582status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800583 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700584 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800585 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700586 type = i;
587 break;
588 }
589 }
590
591 if (type < 0) {
592 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700593 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700594
595 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700596 float xdpi = hwc.getDpiX(type);
597 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700598
599 // TODO: Not sure if display density should handled by SF any longer
600 class Density {
601 static int getDensityFromProperty(char const* propName) {
602 char property[PROPERTY_VALUE_MAX];
603 int density = 0;
604 if (property_get(propName, property, NULL) > 0) {
605 density = atoi(property);
606 }
607 return density;
608 }
609 public:
610 static int getEmuDensity() {
611 return getDensityFromProperty("qemu.sf.lcd_density"); }
612 static int getBuildDensity() {
613 return getDensityFromProperty("ro.sf.lcd_density"); }
614 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700615
616 if (type == DisplayDevice::DISPLAY_PRIMARY) {
617 // The density of the device is provided by a build property
618 float density = Density::getBuildDensity() / 160.0f;
619 if (density == 0) {
620 // the build doesn't provide a density -- this is wrong!
621 // use xdpi instead
622 ALOGE("ro.sf.lcd_density must be defined as a build property");
623 density = xdpi / 160.0f;
624 }
625 if (Density::getEmuDensity()) {
626 // if "qemu.sf.lcd_density" is specified, it overrides everything
627 xdpi = ydpi = density = Density::getEmuDensity();
628 density /= 160.0f;
629 }
630 info->density = density;
631
632 // TODO: this needs to go away (currently needed only by webkit)
633 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
634 info->orientation = hw->getOrientation();
635 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
636 } else {
637 // TODO: where should this value come from?
638 static const int TV_DENSITY = 213;
639 info->density = TV_DENSITY / 160.0f;
640 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700641 }
642
Mathias Agopian1604f772012-09-18 21:54:42 -0700643 info->w = hwc.getWidth(type);
644 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700645 info->xdpi = xdpi;
646 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700647 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700648
649 // All non-virtual displays are currently considered secure.
650 info->secure = true;
651
Mathias Agopian888c8222012-08-04 21:10:38 -0700652 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700653}
654
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800655// ----------------------------------------------------------------------------
656
657sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800658 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700659}
660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800662
663void SurfaceFlinger::waitForEvent() {
664 mEventQueue.waitMessage();
665}
666
667void SurfaceFlinger::signalTransaction() {
668 mEventQueue.invalidate();
669}
670
671void SurfaceFlinger::signalLayerUpdate() {
672 mEventQueue.invalidate();
673}
674
675void SurfaceFlinger::signalRefresh() {
676 mEventQueue.refresh();
677}
678
679status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
680 nsecs_t reltime, uint32_t flags) {
681 return mEventQueue.postMessage(msg, reltime);
682}
683
684status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
685 nsecs_t reltime, uint32_t flags) {
686 status_t res = mEventQueue.postMessage(msg, reltime);
687 if (res == NO_ERROR) {
688 msg->wait();
689 }
690 return res;
691}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
Mathias Agopian4fec8732012-06-29 14:12:52 -0700693bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 return true;
696}
697
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700698void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700699 if (mEventThread == NULL) {
700 // This is a temporary workaround for b/7145521. A non-null pointer
701 // does not mean EventThread has finished initializing, so this
702 // is not a correct fix.
703 ALOGW("WARNING: EventThread not started, ignoring vsync");
704 return;
705 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700706 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
707 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700708 mEventThread->onVSyncReceived(type, timestamp);
709 }
710}
711
712void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
713 if (mEventThread == NULL) {
714 // This is a temporary workaround for b/7145521. A non-null pointer
715 // does not mean EventThread has finished initializing, so this
716 // is not a correct fix.
717 ALOGW("WARNING: EventThread not started, ignoring hotplug");
718 return;
719 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700720
Mathias Agopian148994e2012-09-19 17:31:36 -0700721 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700722 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800723 if (connected) {
724 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700725 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800726 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
727 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700728 }
729 setTransactionFlags(eDisplayTransactionNeeded);
730
Andy McFadden9e9689c2012-10-10 18:17:51 -0700731 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700732 }
Mathias Agopian86303202012-07-24 22:46:10 -0700733}
734
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700735void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
736 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700737}
738
Mathias Agopian4fec8732012-06-29 14:12:52 -0700739void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800740 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800741 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700742 case MessageQueue::INVALIDATE:
743 handleMessageTransaction();
744 handleMessageInvalidate();
745 signalRefresh();
746 break;
747 case MessageQueue::REFRESH:
748 handleMessageRefresh();
749 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800750 }
751}
752
Mathias Agopian4fec8732012-06-29 14:12:52 -0700753void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700754 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700755 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700756 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700757 }
758}
759
760void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700761 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700762 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700763}
764
765void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700766 ATRACE_CALL();
767 preComposition();
768 rebuildLayerStacks();
769 setUpHWComposer();
770 doDebugFlashRegions();
771 doComposition();
772 postComposition();
773}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700774
Mathias Agopiancd60f992012-08-16 16:28:27 -0700775void SurfaceFlinger::doDebugFlashRegions()
776{
777 // is debugging enabled
778 if (CC_LIKELY(!mDebugRegion))
779 return;
780
781 const bool repaintEverything = mRepaintEverything;
782 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
783 const sp<DisplayDevice>& hw(mDisplays[dpy]);
784 if (hw->canDraw()) {
785 // transform the dirty region into this screen's coordinate space
786 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
787 if (!dirtyRegion.isEmpty()) {
788 // redraw the whole screen
789 doComposeSurfaces(hw, Region(hw->bounds()));
790
791 // and draw the dirty region
792 glDisable(GL_TEXTURE_EXTERNAL_OES);
793 glDisable(GL_TEXTURE_2D);
794 glDisable(GL_BLEND);
795 glColor4f(1, 0, 1, 1);
796 const int32_t height = hw->getHeight();
797 Region::const_iterator it = dirtyRegion.begin();
798 Region::const_iterator const end = dirtyRegion.end();
799 while (it != end) {
800 const Rect& r = *it++;
801 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800802 { (GLfloat) r.left, (GLfloat) (height - r.top) },
803 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
804 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
805 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700806 };
807 glVertexPointer(2, GL_FLOAT, 0, vertices);
808 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
809 }
810 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700811 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700812 }
813 }
814 }
815
816 postFramebuffer();
817
818 if (mDebugRegion > 1) {
819 usleep(mDebugRegion * 1000);
820 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700821
822 HWComposer& hwc(getHwComposer());
823 if (hwc.initCheck() == NO_ERROR) {
824 status_t err = hwc.prepare();
825 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
826 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700827}
828
829void SurfaceFlinger::preComposition()
830{
831 bool needExtraInvalidate = false;
832 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
833 const size_t count = currentLayers.size();
834 for (size_t i=0 ; i<count ; i++) {
835 if (currentLayers[i]->onPreComposition()) {
836 needExtraInvalidate = true;
837 }
838 }
839 if (needExtraInvalidate) {
840 signalLayerUpdate();
841 }
842}
843
844void SurfaceFlinger::postComposition()
845{
846 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
847 const size_t count = currentLayers.size();
848 for (size_t i=0 ; i<count ; i++) {
849 currentLayers[i]->onPostComposition();
850 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800851
852 if (mAnimCompositionPending) {
853 mAnimCompositionPending = false;
854
855 const HWComposer& hwc = getHwComposer();
856 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800857 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800858 mAnimFrameTracker.setActualPresentFence(presentFence);
859 } else {
860 // The HWC doesn't support present fences, so use the refresh
861 // timestamp instead.
862 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
863 mAnimFrameTracker.setActualPresentTime(presentTime);
864 }
865 mAnimFrameTracker.advanceFrame();
866 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700867}
868
869void SurfaceFlinger::rebuildLayerStacks() {
870 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700871 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700872 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700873 mVisibleRegionsDirty = false;
874 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700875
Mathias Agopian87baae12012-07-31 12:38:26 -0700876 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700877 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700878 Region opaqueRegion;
879 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700881 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700882 const Transform& tr(hw->getTransform());
883 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700884 if (hw->canDraw()) {
885 SurfaceFlinger::computeVisibleRegions(currentLayers,
886 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700887
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700888 const size_t count = currentLayers.size();
889 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800890 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700891 const Layer::State& s(layer->drawingState());
892 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700893 Region drawRegion(tr.transform(
894 layer->visibleNonTransparentRegion));
895 drawRegion.andSelf(bounds);
896 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700897 layersSortedByZ.add(layer);
898 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700899 }
900 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700901 }
Mathias Agopian42977342012-08-05 00:40:46 -0700902 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700903 hw->undefinedRegion.set(bounds);
904 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
905 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700906 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700907 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700908}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700909
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700911 HWComposer& hwc(getHwComposer());
912 if (hwc.initCheck() == NO_ERROR) {
913 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700914 if (CC_UNLIKELY(mHwWorkListDirty)) {
915 mHwWorkListDirty = false;
916 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
917 sp<const DisplayDevice> hw(mDisplays[dpy]);
918 const int32_t id = hw->getHwcDisplayId();
919 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800920 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700921 hw->getVisibleLayersSortedByZ());
922 const size_t count = currentLayers.size();
923 if (hwc.createWorkList(id, count) == NO_ERROR) {
924 HWComposer::LayerListIterator cur = hwc.begin(id);
925 const HWComposer::LayerListIterator end = hwc.end(id);
926 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800927 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700928 layer->setGeometry(hw, *cur);
929 if (mDebugDisableHWC || mDebugRegion) {
930 cur->setSkip(true);
931 }
932 }
933 }
934 }
935 }
936 }
937
938 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700939 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700940 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700941 const int32_t id = hw->getHwcDisplayId();
942 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800943 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700944 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700945 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700946 HWComposer::LayerListIterator cur = hwc.begin(id);
947 const HWComposer::LayerListIterator end = hwc.end(id);
948 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
949 /*
950 * update the per-frame h/w composer data for each layer
951 * and build the transparent region of the FB
952 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800953 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700954 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700955 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700956 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700957 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700958
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700959 status_t err = hwc.prepare();
960 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
961 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700962}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700963
Mathias Agopiancd60f992012-08-16 16:28:27 -0700964void SurfaceFlinger::doComposition() {
965 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700966 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700967 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700968 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700969 if (hw->canDraw()) {
970 // transform the dirty region into this screen's coordinate space
971 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800972
973 // repaint the framebuffer (if needed)
974 doDisplayComposition(hw, dirtyRegion);
975
Mathias Agopiancd60f992012-08-16 16:28:27 -0700976 hw->dirtyRegion.clear();
977 hw->flip(hw->swapRegion);
978 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700979 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700980 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700981 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700982 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700983 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700984}
985
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986void SurfaceFlinger::postFramebuffer()
987{
Mathias Agopian841cde52012-03-01 15:44:37 -0800988 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800989
Mathias Agopiana44b0412011-10-16 18:46:35 -0700990 const nsecs_t now = systemTime();
991 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700992
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700993 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700994 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700995 if (!hwc.supportsFramebufferTarget()) {
996 // EGL spec says:
997 // "surface must be bound to the calling thread's current context,
998 // for the current rendering API."
999 DisplayDevice::makeCurrent(mEGLDisplay,
1000 getDefaultDisplayDevice(), mEGLContext);
1001 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001002 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001003 }
1004
Mathias Agopian92a979a2012-08-02 18:32:23 -07001005 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001006 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001007 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001008 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001009 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001010 int32_t id = hw->getHwcDisplayId();
1011 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001012 HWComposer::LayerListIterator cur = hwc.begin(id);
1013 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001014 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001015 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001016 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001017 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001018 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001019 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001020 }
Jesse Hallef194142012-06-14 14:45:17 -07001021 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001022 }
1023
Mathias Agopiana44b0412011-10-16 18:46:35 -07001024 mLastSwapBufferTime = systemTime() - now;
1025 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026}
1027
Mathias Agopian87baae12012-07-31 12:38:26 -07001028void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029{
Mathias Agopian841cde52012-03-01 15:44:37 -08001030 ATRACE_CALL();
1031
Mathias Agopianca4d3602011-05-19 15:38:14 -07001032 Mutex::Autolock _l(mStateLock);
1033 const nsecs_t now = systemTime();
1034 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035
Mathias Agopianca4d3602011-05-19 15:38:14 -07001036 // Here we're guaranteed that some transaction flags are set
1037 // so we can call handleTransactionLocked() unconditionally.
1038 // We call getTransactionFlags(), which will also clear the flags,
1039 // with mStateLock held to guarantee that mCurrentState won't change
1040 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001041
Mathias Agopiane57f2922012-08-09 16:29:12 -07001042 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001043 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001044
Mathias Agopianca4d3602011-05-19 15:38:14 -07001045 mLastTransactionTime = systemTime() - now;
1046 mDebugInTransaction = 0;
1047 invalidateHwcGeometry();
1048 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001049}
1050
Mathias Agopian87baae12012-07-31 12:38:26 -07001051void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001052{
1053 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 const size_t count = currentLayers.size();
1055
1056 /*
1057 * Traversal of the children
1058 * (perform the transaction for each of them if needed)
1059 */
1060
Mathias Agopian3559b072012-08-15 13:46:03 -07001061 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1065 if (!trFlags) continue;
1066
1067 const uint32_t flags = layer->doTransaction(0);
1068 if (flags & Layer::eVisibleRegion)
1069 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 }
1071 }
1072
1073 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001074 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 */
1076
Mathias Agopiane57f2922012-08-09 16:29:12 -07001077 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001078 // here we take advantage of Vector's copy-on-write semantics to
1079 // improve performance by skipping the transaction entirely when
1080 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001081 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1082 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001083 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001085 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001086 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001087
1088 // find the displays that were removed
1089 // (ie: in drawing state but not in current state)
1090 // also handle displays that changed
1091 // (ie: displays that are in both lists)
1092 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001093 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1094 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001095 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001096 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001097 // Call makeCurrent() on the primary display so we can
1098 // be sure that nothing associated with this display
1099 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001100 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001101 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001102 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001103 getHwComposer().disconnectDisplay(draw[i].type);
Jesse Hall7adb0f82013-03-06 16:13:49 -08001104 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1105 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001106 } else {
1107 ALOGW("trying to remove the main display");
1108 }
1109 } else {
1110 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001111 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001112 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001113 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001114 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001115 // recreating the DisplayDevice, so we just remove it
1116 // from the drawing state, so that it get re-added
1117 // below.
1118 mDisplays.removeItem(display);
1119 mDrawingState.displays.removeItemsAt(i);
1120 dc--; i--;
1121 // at this point we must loop to the next item
1122 continue;
1123 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001124
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001125 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001126 if (disp != NULL) {
1127 if (state.layerStack != draw[i].layerStack) {
1128 disp->setLayerStack(state.layerStack);
1129 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001130 if ((state.orientation != draw[i].orientation)
1131 || (state.viewport != draw[i].viewport)
1132 || (state.frame != draw[i].frame))
1133 {
1134 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001135 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001136 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001137 }
1138 }
1139 }
1140
1141 // find displays that were added
1142 // (ie: in current state but not in drawing state)
1143 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001144 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001145 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001146
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001147 sp<DisplaySurface> dispSurface;
1148 if (state.isVirtualDisplay()) {
1149 if (state.surface != NULL) {
1150 dispSurface = new VirtualDisplaySurface(
1151 *mHwc, state.type, state.surface,
1152 state.displayName);
1153 }
1154 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001155 ALOGE_IF(state.surface!=NULL,
1156 "adding a supported display, but rendering "
1157 "surface is provided (%p), ignoring it",
1158 state.surface.get());
1159
1160 // for supported (by hwc) displays we provide our
1161 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001162 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001163 }
1164
1165 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001166 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001167 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001168 state.type, state.isSecure, display,
1169 dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001170 hw->setLayerStack(state.layerStack);
1171 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001172 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001173 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001174 mDisplays.add(display, hw);
Jesse Hall7adb0f82013-03-06 16:13:49 -08001175 if (state.type < DisplayDevice::NUM_DISPLAY_TYPES)
1176 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001177 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001178 }
1179 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001181 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182
Mathias Agopian84300952012-11-21 16:02:13 -08001183 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1184 // The transform hint might have changed for some layers
1185 // (either because a display has changed, or because a layer
1186 // as changed).
1187 //
1188 // Walk through all the layers in currentLayers,
1189 // and update their transform hint.
1190 //
1191 // If a layer is visible only on a single display, then that
1192 // display is used to calculate the hint, otherwise we use the
1193 // default display.
1194 //
1195 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1196 // the hint is set before we acquire a buffer from the surface texture.
1197 //
1198 // NOTE: layer transactions have taken place already, so we use their
1199 // drawing state. However, SurfaceFlinger's own transaction has not
1200 // happened yet, so we must use the current state layer list
1201 // (soon to become the drawing state list).
1202 //
1203 sp<const DisplayDevice> disp;
1204 uint32_t currentlayerStack = 0;
1205 for (size_t i=0; i<count; i++) {
1206 // NOTE: we rely on the fact that layers are sorted by
1207 // layerStack first (so we don't have to traverse the list
1208 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001209 const sp<Layer>& layer(currentLayers[i]);
1210 uint32_t layerStack = layer->drawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001211 if (i==0 || currentlayerStack != layerStack) {
1212 currentlayerStack = layerStack;
1213 // figure out if this layerstack is mirrored
1214 // (more than one display) if so, pick the default display,
1215 // if not, pick the only display it's on.
1216 disp.clear();
1217 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1218 sp<const DisplayDevice> hw(mDisplays[dpy]);
1219 if (hw->getLayerStack() == currentlayerStack) {
1220 if (disp == NULL) {
1221 disp = hw;
1222 } else {
1223 disp = getDefaultDisplayDevice();
1224 break;
1225 }
1226 }
1227 }
1228 }
1229 if (disp != NULL) {
1230 // presumably this means this layer is using a layerStack
1231 // that is not visible on any display
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001233 }
1234 }
1235 }
1236
1237
Mathias Agopian3559b072012-08-15 13:46:03 -07001238 /*
1239 * Perform our own transaction if needed
1240 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001241
Mathias Agopiancd60f992012-08-16 16:28:27 -07001242 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1243 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001244 // layers have been added
1245 mVisibleRegionsDirty = true;
1246 }
1247
1248 // some layers might have been removed, so
1249 // we need to update the regions they're exposing.
1250 if (mLayersRemoved) {
1251 mLayersRemoved = false;
1252 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001253 const size_t count = previousLayers.size();
1254 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001255 const sp<Layer>& layer(previousLayers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001256 if (currentLayers.indexOf(layer) < 0) {
1257 // this layer is not visible anymore
1258 // TODO: we could traverse the tree from front to back and
1259 // compute the actual visible region
1260 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001261 const Layer::State& s(layer->drawingState());
1262 Region visibleReg = s.transform.transform(
1263 Region(Rect(s.active.w, s.active.h)));
1264 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001265 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 }
1268
1269 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001270}
1271
1272void SurfaceFlinger::commitTransaction()
1273{
1274 if (!mLayersPendingRemoval.isEmpty()) {
1275 // Notify removed layers now that they can't be drawn from
1276 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1277 mLayersPendingRemoval[i]->onRemoved();
1278 }
1279 mLayersPendingRemoval.clear();
1280 }
1281
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001282 // If this transaction is part of a window animation then the next frame
1283 // we composite should be considered an animation as well.
1284 mAnimCompositionPending = mAnimTransactionPending;
1285
Mathias Agopian4fec8732012-06-29 14:12:52 -07001286 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001287 mTransactionPending = false;
1288 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001289 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290}
1291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001293 const LayerVector& currentLayers, uint32_t layerStack,
1294 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295{
Mathias Agopian841cde52012-03-01 15:44:37 -08001296 ATRACE_CALL();
1297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 Region aboveOpaqueLayers;
1299 Region aboveCoveredLayers;
1300 Region dirty;
1301
Mathias Agopian87baae12012-07-31 12:38:26 -07001302 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303
1304 size_t i = currentLayers.size();
1305 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001306 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307
1308 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001309 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310
Jesse Hall01e29052013-02-19 16:13:35 -08001311 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001312 if (s.layerStack != layerStack)
1313 continue;
1314
Mathias Agopianab028732010-03-16 16:41:46 -07001315 /*
1316 * opaqueRegion: area of a surface that is fully opaque.
1317 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001319
1320 /*
1321 * visibleRegion: area of a surface that is visible on screen
1322 * and not fully transparent. This is essentially the layer's
1323 * footprint minus the opaque regions above it.
1324 * Areas covered by a translucent surface are considered visible.
1325 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001327
1328 /*
1329 * coveredRegion: area of a surface that is covered by all
1330 * visible regions above it (which includes the translucent areas).
1331 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001333
Jesse Halla8026d22012-09-25 13:25:04 -07001334 /*
1335 * transparentRegion: area of a surface that is hinted to be completely
1336 * transparent. This is only used to tell when the layer has no visible
1337 * non-transparent regions and can be removed from the layer list. It
1338 * does not affect the visibleRegion of this layer or any layers
1339 * beneath it. The hint may not be correct if apps don't respect the
1340 * SurfaceView restrictions (which, sadly, some don't).
1341 */
1342 Region transparentRegion;
1343
Mathias Agopianab028732010-03-16 16:41:46 -07001344
1345 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001346 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001347 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001348 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001350 if (!visibleRegion.isEmpty()) {
1351 // Remove the transparent area from the visible region
1352 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001353 const Transform tr(s.transform);
1354 if (tr.transformed()) {
1355 if (tr.preserveRects()) {
1356 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001357 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001358 } else {
1359 // transformation too complex, can't do the
1360 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001361 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001362 }
1363 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001364 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001365 }
Mathias Agopianab028732010-03-16 16:41:46 -07001366 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367
Mathias Agopianab028732010-03-16 16:41:46 -07001368 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001369 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001370 if (s.alpha==255 && !translucent &&
1371 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1372 // the opaque region is the layer's footprint
1373 opaqueRegion = visibleRegion;
1374 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375 }
1376 }
1377
Mathias Agopianab028732010-03-16 16:41:46 -07001378 // Clip the covered region to the visible region
1379 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1380
1381 // Update aboveCoveredLayers for next (lower) layer
1382 aboveCoveredLayers.orSelf(visibleRegion);
1383
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384 // subtract the opaque region covered by the layers above us
1385 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386
1387 // compute this layer's dirty region
1388 if (layer->contentDirty) {
1389 // we need to invalidate the whole region
1390 dirty = visibleRegion;
1391 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001392 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 layer->contentDirty = false;
1394 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001395 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001396 * the exposed region consists of two components:
1397 * 1) what's VISIBLE now and was COVERED before
1398 * 2) what's EXPOSED now less what was EXPOSED before
1399 *
1400 * note that (1) is conservative, we start with the whole
1401 * visible region but only keep what used to be covered by
1402 * something -- which mean it may have been exposed.
1403 *
1404 * (2) handles areas that were not covered by anything but got
1405 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001406 */
Mathias Agopianab028732010-03-16 16:41:46 -07001407 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001408 const Region oldVisibleRegion = layer->visibleRegion;
1409 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001410 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1411 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 }
1413 dirty.subtractSelf(aboveOpaqueLayers);
1414
1415 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001416 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417
Mathias Agopianab028732010-03-16 16:41:46 -07001418 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001420
Jesse Halla8026d22012-09-25 13:25:04 -07001421 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 layer->setVisibleRegion(visibleRegion);
1423 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001424 layer->setVisibleNonTransparentRegion(
1425 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 }
1427
Mathias Agopian87baae12012-07-31 12:38:26 -07001428 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429}
1430
Mathias Agopian87baae12012-07-31 12:38:26 -07001431void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1432 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001433 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001434 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1435 if (hw->getLayerStack() == layerStack) {
1436 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001437 }
1438 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001439}
1440
1441void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001443 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444
Mathias Agopian4fec8732012-06-29 14:12:52 -07001445 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001446 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001448 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001449 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001450 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001451 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001452 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001453 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001454
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001455 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456}
1457
Mathias Agopianad456f92011-01-13 17:53:01 -08001458void SurfaceFlinger::invalidateHwcGeometry()
1459{
1460 mHwWorkListDirty = true;
1461}
1462
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001463
Mathias Agopiancd60f992012-08-16 16:28:27 -07001464void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001465 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466{
Mathias Agopian87baae12012-07-31 12:38:26 -07001467 Region dirtyRegion(inDirtyRegion);
1468
Mathias Agopianb8a55602009-06-26 19:06:36 -07001469 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001470 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471
Mathias Agopian42977342012-08-05 00:40:46 -07001472 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001473 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001474 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1475 // takes a rectangle, we must make sure to update that whole
1476 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001477 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001478 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001479 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001480 // We need to redraw the rectangle that will be updated
1481 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001482 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001483 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001484 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001485 } else {
1486 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001487 dirtyRegion.set(hw->bounds());
1488 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 }
1490 }
1491
Mathias Agopiancd60f992012-08-16 16:28:27 -07001492 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001494 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001495 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001496
1497 // swap buffers (presentation)
1498 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499}
1500
Mathias Agopiancd60f992012-08-16 16:28:27 -07001501void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001502{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001503 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001504 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001505 HWComposer::LayerListIterator cur = hwc.begin(id);
1506 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001507
Mathias Agopian85d751c2012-08-29 16:59:24 -07001508 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1509 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001510 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001511
1512 // set the frame buffer
1513 glMatrixMode(GL_MODELVIEW);
1514 glLoadIdentity();
1515
1516 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001517 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001518 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001519 // when using overlays, we assume a fully transparent framebuffer
1520 // NOTE: we could reduce how much we need to clear, for instance
1521 // remove where there are opaque FB layers. however, on some
1522 // GPUs doing a "clean slate" glClear might be more efficient.
1523 // We'll revisit later if needed.
1524 glClearColor(0, 0, 0, 0);
1525 glClear(GL_COLOR_BUFFER_BIT);
1526 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001527 // we start with the whole screen area
1528 const Region bounds(hw->getBounds());
1529
1530 // we remove the scissor part
1531 // we're left with the letterbox region
1532 // (common case is that letterbox ends-up being empty)
1533 const Region letterbox(bounds.subtract(hw->getScissor()));
1534
1535 // compute the area to clear
1536 Region region(hw->undefinedRegion.merge(letterbox));
1537
1538 // but limit it to the dirty region
1539 region.andSelf(dirty);
1540
Mathias Agopianb9494d52012-04-18 02:28:45 -07001541 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001542 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001543 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001544 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001545 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001546 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001547
Mathias Agopian766dc492012-10-30 18:08:06 -07001548 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1549 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001550 // scissor on the main display. It should never be needed
1551 // anyways (though in theory it could since the API allows it).
1552 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001553 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001554 if (scissor != bounds) {
1555 // scissor doesn't match the screen's dimensions, so we
1556 // need to clear everything outside of it and enable
1557 // the GL scissor so we don't draw anything where we shouldn't
1558 const GLint height = hw->getHeight();
1559 glScissor(scissor.left, height - scissor.bottom,
1560 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001561 // enable scissor for this frame
1562 glEnable(GL_SCISSOR_TEST);
1563 }
1564 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001565 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001566
Mathias Agopian85d751c2012-08-29 16:59:24 -07001567 /*
1568 * and then, render the layers targeted at the framebuffer
1569 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001570
Mathias Agopian13127d82013-03-05 17:47:11 -08001571 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001572 const size_t count = layers.size();
1573 const Transform& tr = hw->getTransform();
1574 if (cur != end) {
1575 // we're using h/w composer
1576 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001577 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001578 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001579 if (!clip.isEmpty()) {
1580 switch (cur->getCompositionType()) {
1581 case HWC_OVERLAY: {
1582 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1583 && i
1584 && layer->isOpaque()
1585 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001586 // never clear the very first layer since we're
1587 // guaranteed the FB is already cleared
1588 layer->clearWithOpenGL(hw, clip);
1589 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001590 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001591 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001592 case HWC_FRAMEBUFFER: {
1593 layer->draw(hw, clip);
1594 break;
1595 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001596 case HWC_FRAMEBUFFER_TARGET: {
1597 // this should not happen as the iterator shouldn't
1598 // let us get there.
1599 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1600 break;
1601 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001602 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001603 }
1604 layer->setAcquireFence(hw, *cur);
1605 }
1606 } else {
1607 // we're not using h/w composer
1608 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001609 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001610 const Region clip(dirty.intersect(
1611 tr.transform(layer->visibleRegion)));
1612 if (!clip.isEmpty()) {
1613 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001614 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001615 }
1616 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001617
1618 // disable scissor at the end of the frame
1619 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620}
1621
Mathias Agopian55801e42012-08-27 18:54:24 -07001622void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1623 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001625 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001626 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001627 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001628 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001629
Mathias Agopian55801e42012-08-27 18:54:24 -07001630 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001631 Region::const_iterator it = region.begin();
1632 Region::const_iterator const end = region.end();
1633 while (it != end) {
1634 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001635 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001636 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1637 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1638 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1639 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001640 };
1641 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001642 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1643 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644}
1645
Mathias Agopianac9fa422013-02-11 16:40:36 -08001646void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1647 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001648 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001649 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001650{
Mathias Agopian96f08192010-06-02 23:28:45 -07001651 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001652 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001653
Mathias Agopian96f08192010-06-02 23:28:45 -07001654 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001655 Mutex::Autolock _l(mStateLock);
1656 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001657 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001658}
1659
Mathias Agopian13127d82013-03-05 17:47:11 -08001660status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661{
1662 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001663 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001665 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001666 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001667 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668 return NO_ERROR;
1669 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001670 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671}
1672
Mathias Agopiandea20b12011-05-03 17:04:02 -07001673uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1674{
1675 return android_atomic_release_load(&mTransactionFlags);
1676}
1677
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1679{
1680 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1681}
1682
Mathias Agopianbb641242010-05-18 17:06:55 -07001683uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684{
1685 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1686 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001687 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688 }
1689 return old;
1690}
1691
Mathias Agopian8b33f032012-07-24 20:43:54 -07001692void SurfaceFlinger::setTransactionState(
1693 const Vector<ComposerState>& state,
1694 const Vector<DisplayState>& displays,
1695 uint32_t flags)
1696{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001697 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001698 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001699 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001700
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001701 if (flags & eAnimation) {
1702 // For window updates that are part of an animation we must wait for
1703 // previous animation "frames" to be handled.
1704 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001705 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001706 if (CC_UNLIKELY(err != NO_ERROR)) {
1707 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001708 // caller after a few seconds.
1709 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1710 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001711 mAnimTransactionPending = false;
1712 break;
1713 }
1714 }
1715 }
1716
Mathias Agopiane57f2922012-08-09 16:29:12 -07001717 size_t count = displays.size();
1718 for (size_t i=0 ; i<count ; i++) {
1719 const DisplayState& s(displays[i]);
1720 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001721 }
1722
Mathias Agopiane57f2922012-08-09 16:29:12 -07001723 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001724 for (size_t i=0 ; i<count ; i++) {
1725 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001726 // Here we need to check that the interface we're given is indeed
1727 // one of our own. A malicious client could give us a NULL
1728 // IInterface, or one of its own or even one of our own but a
1729 // different type. All these situations would cause us to crash.
1730 //
1731 // NOTE: it would be better to use RTTI as we could directly check
1732 // that we have a Client*. however, RTTI is disabled in Android.
1733 if (s.client != NULL) {
1734 sp<IBinder> binder = s.client->asBinder();
1735 if (binder != NULL) {
1736 String16 desc(binder->getInterfaceDescriptor());
1737 if (desc == ISurfaceComposerClient::descriptor) {
1738 sp<Client> client( static_cast<Client *>(s.client.get()) );
1739 transactionFlags |= setClientStateLocked(client, s.state);
1740 }
1741 }
1742 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001743 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001744
Mathias Agopian386aa982011-11-07 21:58:03 -08001745 if (transactionFlags) {
1746 // this triggers the transaction
1747 setTransactionFlags(transactionFlags);
1748
1749 // if this is a synchronous transaction, wait for it to take effect
1750 // before returning.
1751 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001752 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001753 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001754 if (flags & eAnimation) {
1755 mAnimTransactionPending = true;
1756 }
1757 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001758 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1759 if (CC_UNLIKELY(err != NO_ERROR)) {
1760 // just in case something goes wrong in SF, return to the
1761 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001762 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1763 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001764 break;
1765 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001766 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 }
1768}
1769
Mathias Agopiane57f2922012-08-09 16:29:12 -07001770uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1771{
Jesse Hall9a143922012-10-04 16:29:19 -07001772 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1773 if (dpyIdx < 0)
1774 return 0;
1775
Mathias Agopiane57f2922012-08-09 16:29:12 -07001776 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001777 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001778 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001779 const uint32_t what = s.what;
1780 if (what & DisplayState::eSurfaceChanged) {
1781 if (disp.surface->asBinder() != s.surface->asBinder()) {
1782 disp.surface = s.surface;
1783 flags |= eDisplayTransactionNeeded;
1784 }
1785 }
1786 if (what & DisplayState::eLayerStackChanged) {
1787 if (disp.layerStack != s.layerStack) {
1788 disp.layerStack = s.layerStack;
1789 flags |= eDisplayTransactionNeeded;
1790 }
1791 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001792 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001793 if (disp.orientation != s.orientation) {
1794 disp.orientation = s.orientation;
1795 flags |= eDisplayTransactionNeeded;
1796 }
1797 if (disp.frame != s.frame) {
1798 disp.frame = s.frame;
1799 flags |= eDisplayTransactionNeeded;
1800 }
1801 if (disp.viewport != s.viewport) {
1802 disp.viewport = s.viewport;
1803 flags |= eDisplayTransactionNeeded;
1804 }
1805 }
1806 }
1807 return flags;
1808}
1809
1810uint32_t SurfaceFlinger::setClientStateLocked(
1811 const sp<Client>& client,
1812 const layer_state_t& s)
1813{
1814 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001815 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001816 if (layer != 0) {
1817 const uint32_t what = s.what;
1818 if (what & layer_state_t::ePositionChanged) {
1819 if (layer->setPosition(s.x, s.y))
1820 flags |= eTraversalNeeded;
1821 }
1822 if (what & layer_state_t::eLayerChanged) {
1823 // NOTE: index needs to be calculated before we update the state
1824 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1825 if (layer->setLayer(s.z)) {
1826 mCurrentState.layersSortedByZ.removeAt(idx);
1827 mCurrentState.layersSortedByZ.add(layer);
1828 // we need traversal (state changed)
1829 // AND transaction (list changed)
1830 flags |= eTransactionNeeded|eTraversalNeeded;
1831 }
1832 }
1833 if (what & layer_state_t::eSizeChanged) {
1834 if (layer->setSize(s.w, s.h)) {
1835 flags |= eTraversalNeeded;
1836 }
1837 }
1838 if (what & layer_state_t::eAlphaChanged) {
1839 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1840 flags |= eTraversalNeeded;
1841 }
1842 if (what & layer_state_t::eMatrixChanged) {
1843 if (layer->setMatrix(s.matrix))
1844 flags |= eTraversalNeeded;
1845 }
1846 if (what & layer_state_t::eTransparentRegionChanged) {
1847 if (layer->setTransparentRegionHint(s.transparentRegion))
1848 flags |= eTraversalNeeded;
1849 }
1850 if (what & layer_state_t::eVisibilityChanged) {
1851 if (layer->setFlags(s.flags, s.mask))
1852 flags |= eTraversalNeeded;
1853 }
1854 if (what & layer_state_t::eCropChanged) {
1855 if (layer->setCrop(s.crop))
1856 flags |= eTraversalNeeded;
1857 }
1858 if (what & layer_state_t::eLayerStackChanged) {
1859 // NOTE: index needs to be calculated before we update the state
1860 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1861 if (layer->setLayerStack(s.layerStack)) {
1862 mCurrentState.layersSortedByZ.removeAt(idx);
1863 mCurrentState.layersSortedByZ.add(layer);
1864 // we need traversal (state changed)
1865 // AND transaction (list changed)
1866 flags |= eTransactionNeeded|eTraversalNeeded;
1867 }
1868 }
1869 }
1870 return flags;
1871}
1872
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001873status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001874 const String8& name,
1875 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001876 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1877 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001879 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001880 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001881 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001882 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001883 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001884 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001885
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001886 status_t result = NO_ERROR;
1887
1888 sp<Layer> layer;
1889
Mathias Agopian3165cc22012-08-08 19:42:09 -07001890 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1891 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001892 result = createNormalLayer(client,
1893 name, w, h, flags, format,
1894 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001896 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001897 result = createDimLayer(client,
1898 name, w, h, flags,
1899 handle, gbp, &layer);
1900 break;
1901 default:
1902 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001903 break;
1904 }
1905
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001906 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001907 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001908 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001909 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001910 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001911}
1912
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001913status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1914 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1915 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001916{
1917 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001918 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 case PIXEL_FORMAT_TRANSPARENT:
1920 case PIXEL_FORMAT_TRANSLUCENT:
1921 format = PIXEL_FORMAT_RGBA_8888;
1922 break;
1923 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001924#ifdef NO_RGBX_8888
1925 format = PIXEL_FORMAT_RGB_565;
1926#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001927 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001928#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929 break;
1930 }
1931
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001932#ifdef NO_RGBX_8888
1933 if (format == PIXEL_FORMAT_RGBX_8888)
1934 format = PIXEL_FORMAT_RGBA_8888;
1935#endif
1936
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001937 *outLayer = new Layer(this, client, name, w, h, flags);
1938 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1939 if (err == NO_ERROR) {
1940 *handle = (*outLayer)->getHandle();
1941 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001942 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001943
1944 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1945 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001946}
1947
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001948status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1949 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1950 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001952 *outLayer = new LayerDim(this, client, name, w, h, flags);
1953 *handle = (*outLayer)->getHandle();
1954 *gbp = (*outLayer)->getBufferQueue();
1955 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001956}
1957
Mathias Agopianac9fa422013-02-11 16:40:36 -08001958status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959{
Mathias Agopian67106042013-03-14 19:18:13 -07001960 // called by the window manager when it wants to remove a Layer
1961 status_t err = NO_ERROR;
1962 sp<Layer> l(client->getLayerUser(handle));
1963 if (l != NULL) {
1964 err = removeLayer(l);
1965 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1966 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001967 }
1968 return err;
1969}
1970
Mathias Agopian13127d82013-03-05 17:47:11 -08001971status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001972{
Mathias Agopian67106042013-03-14 19:18:13 -07001973 // called by ~LayerCleaner() when all references to the IBinder (handle)
1974 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001975 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001976 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001977 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001978 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001979 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001980 "error removing layer=%p (%s)", l.get(), strerror(-err));
1981 }
1982 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001983}
1984
Mathias Agopianb60314a2012-04-10 22:09:54 -07001985// ---------------------------------------------------------------------------
1986
Andy McFadden13a082e2012-08-24 10:16:42 -07001987void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001988 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001989 Vector<ComposerState> state;
1990 Vector<DisplayState> displays;
1991 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001992 d.what = DisplayState::eDisplayProjectionChanged |
1993 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001994 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001995 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001996 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001997 d.frame.makeInvalid();
1998 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001999 displays.add(d);
2000 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002001 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002002}
2003
2004void SurfaceFlinger::initializeDisplays() {
2005 class MessageScreenInitialized : public MessageBase {
2006 SurfaceFlinger* flinger;
2007 public:
2008 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2009 virtual bool handler() {
2010 flinger->onInitializeDisplays();
2011 return true;
2012 }
2013 };
2014 sp<MessageBase> msg = new MessageScreenInitialized(this);
2015 postMessageAsync(msg); // we may be called from main thread, use async message
2016}
2017
2018
Mathias Agopiancde87a32012-09-13 14:09:01 -07002019void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002020 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2021 if (hw->isScreenAcquired()) {
2022 // this is expected, e.g. when power manager wakes up during boot
2023 ALOGD(" screen was previously acquired");
2024 return;
2025 }
2026
Mathias Agopian42977342012-08-05 00:40:46 -07002027 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002028 int32_t type = hw->getDisplayType();
2029 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2030 // built-in display, tell the HWC
2031 getHwComposer().acquire(type);
2032
2033 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2034 // FIXME: eventthread only knows about the main display right now
2035 mEventThread->onScreenAcquired();
2036 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002037 }
Mathias Agopian20128302012-08-13 18:32:13 -07002038 mVisibleRegionsDirty = true;
2039 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002040}
2041
Mathias Agopiancde87a32012-09-13 14:09:01 -07002042void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002043 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2044 if (!hw->isScreenAcquired()) {
2045 ALOGD(" screen was previously released");
2046 return;
2047 }
2048
2049 hw->releaseScreen();
2050 int32_t type = hw->getDisplayType();
2051 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2052 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002053 // FIXME: eventthread only knows about the main display right now
2054 mEventThread->onScreenReleased();
2055 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002056
2057 // built-in display, tell the HWC
2058 getHwComposer().release(type);
2059 }
2060 mVisibleRegionsDirty = true;
2061 // from this point on, SF will stop drawing on this display
2062}
2063
2064void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2065 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002066 SurfaceFlinger& mFlinger;
2067 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002068 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002069 MessageScreenAcquired(SurfaceFlinger& flinger,
2070 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002071 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002072 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2073 if (hw == NULL) {
2074 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2075 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2076 ALOGW("Attempt to unblank virtual display");
2077 } else {
2078 mFlinger.onScreenAcquired(hw);
2079 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002080 return true;
2081 }
2082 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002083 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2084 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085}
2086
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002087void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002088 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002089 SurfaceFlinger& mFlinger;
2090 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002091 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002092 MessageScreenReleased(SurfaceFlinger& flinger,
2093 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002094 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002095 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2096 if (hw == NULL) {
2097 ALOGE("Attempt to blank null display %p", mDisplay.get());
2098 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2099 ALOGW("Attempt to blank virtual display");
2100 } else {
2101 mFlinger.onScreenReleased(hw);
2102 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002103 return true;
2104 }
2105 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002106 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2107 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002108}
2109
2110// ---------------------------------------------------------------------------
2111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002112status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2113{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002114 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002115 char buffer[SIZE];
2116 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002117
2118 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119 snprintf(buffer, SIZE, "Permission Denial: "
2120 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2121 IPCThreadState::self()->getCallingPid(),
2122 IPCThreadState::self()->getCallingUid());
2123 result.append(buffer);
2124 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002125 // Try to get the main lock, but don't insist if we can't
2126 // (this would indicate SF is stuck, but we want to be able to
2127 // print something in dumpsys).
2128 int retry = 3;
2129 while (mStateLock.tryLock()<0 && --retry>=0) {
2130 usleep(1000000);
2131 }
2132 const bool locked(retry >= 0);
2133 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002134 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002135 "SurfaceFlinger appears to be unresponsive, "
2136 "dumping anyways (no locks held)\n");
2137 result.append(buffer);
2138 }
2139
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002140 bool dumpAll = true;
2141 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002142 size_t numArgs = args.size();
2143 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002144 if ((index < numArgs) &&
2145 (args[index] == String16("--list"))) {
2146 index++;
2147 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002148 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002149 }
2150
2151 if ((index < numArgs) &&
2152 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002153 index++;
2154 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002155 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002156 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002157
2158 if ((index < numArgs) &&
2159 (args[index] == String16("--latency-clear"))) {
2160 index++;
2161 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002162 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002163 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002164 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002165
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002166 if (dumpAll) {
2167 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002168 }
2169
Mathias Agopian9795c422009-08-26 16:36:26 -07002170 if (locked) {
2171 mStateLock.unlock();
2172 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002173 }
2174 write(fd, result.string(), result.size());
2175 return NO_ERROR;
2176}
2177
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002178void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2179 String8& result, char* buffer, size_t SIZE) const
2180{
2181 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2182 const size_t count = currentLayers.size();
2183 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002184 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002185 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2186 result.append(buffer);
2187 }
2188}
2189
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002190void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2191 String8& result, char* buffer, size_t SIZE) const
2192{
2193 String8 name;
2194 if (index < args.size()) {
2195 name = String8(args[index]);
2196 index++;
2197 }
2198
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002199 const nsecs_t period =
2200 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2201 result.appendFormat("%lld\n", period);
2202
2203 if (name.isEmpty()) {
2204 mAnimFrameTracker.dump(result);
2205 } else {
2206 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2207 const size_t count = currentLayers.size();
2208 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002209 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002210 if (name == layer->getName()) {
2211 layer->dumpStats(result, buffer, SIZE);
2212 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002213 }
2214 }
2215}
2216
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002217void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002218 String8& result, char* buffer, size_t SIZE)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002219{
2220 String8 name;
2221 if (index < args.size()) {
2222 name = String8(args[index]);
2223 index++;
2224 }
2225
2226 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2227 const size_t count = currentLayers.size();
2228 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002229 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002230 if (name.isEmpty() || (name == layer->getName())) {
2231 layer->clearStats();
2232 }
2233 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002234
2235 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002236}
2237
Andy McFadden4803b742012-09-24 19:07:20 -07002238/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2239{
2240 static const char* config =
2241 " [sf"
2242#ifdef NO_RGBX_8888
2243 " NO_RGBX_8888"
2244#endif
2245#ifdef HAS_CONTEXT_PRIORITY
2246 " HAS_CONTEXT_PRIORITY"
2247#endif
2248#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2249 " NEVER_DEFAULT_TO_ASYNC_MODE"
2250#endif
2251#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2252 " TARGET_DISABLE_TRIPLE_BUFFERING"
2253#endif
2254 "]";
2255 result.append(config);
2256}
2257
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002258void SurfaceFlinger::dumpAllLocked(
2259 String8& result, char* buffer, size_t SIZE) const
2260{
2261 // figure out if we're stuck somewhere
2262 const nsecs_t now = systemTime();
2263 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2264 const nsecs_t inTransaction(mDebugInTransaction);
2265 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2266 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2267
2268 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002269 * Dump library configuration.
2270 */
2271 result.append("Build configuration:");
2272 appendSfConfigString(result);
2273 appendUiConfigString(result);
2274 appendGuiConfigString(result);
2275 result.append("\n");
2276
2277 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002278 * Dump the visible layer list
2279 */
2280 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2281 const size_t count = currentLayers.size();
2282 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2283 result.append(buffer);
2284 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002285 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002286 layer->dump(result, buffer, SIZE);
2287 }
2288
2289 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002290 * Dump Display state
2291 */
2292
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002293 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2294 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002295 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2296 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002297 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002298 }
2299
2300 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002301 * Dump SurfaceFlinger global state
2302 */
2303
2304 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2305 result.append(buffer);
2306
Mathias Agopian888c8222012-08-04 21:10:38 -07002307 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002308 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002309 const GLExtensions& extensions(GLExtensions::getInstance());
2310 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2311 extensions.getVendor(),
2312 extensions.getRenderer(),
2313 extensions.getVersion());
2314 result.append(buffer);
2315
2316 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002317 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002318 result.append(buffer);
2319
2320 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2321 result.append(buffer);
2322
Mathias Agopian42977342012-08-05 00:40:46 -07002323 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002324 snprintf(buffer, SIZE,
2325 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002326 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002327 result.append(buffer);
2328 snprintf(buffer, SIZE,
2329 " last eglSwapBuffers() time: %f us\n"
2330 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002331 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002332 " refresh-rate : %f fps\n"
2333 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002334 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002335 mLastSwapBufferTime/1000.0,
2336 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002337 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002338 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2339 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2340 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002341 result.append(buffer);
2342
2343 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2344 inSwapBuffersDuration/1000.0);
2345 result.append(buffer);
2346
2347 snprintf(buffer, SIZE, " transaction time: %f us\n",
2348 inTransactionDuration/1000.0);
2349 result.append(buffer);
2350
2351 /*
2352 * VSYNC state
2353 */
2354 mEventThread->dump(result, buffer, SIZE);
2355
2356 /*
2357 * Dump HWComposer state
2358 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002359 snprintf(buffer, SIZE, "h/w composer state:\n");
2360 result.append(buffer);
2361 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2362 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2363 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2364 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002365 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002366
2367 /*
2368 * Dump gralloc state
2369 */
2370 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2371 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002372}
2373
Mathias Agopian13127d82013-03-05 17:47:11 -08002374const Vector< sp<Layer> >&
Mathias Agopiancb558572012-10-04 15:58:54 -07002375SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002376 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002377 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2378}
2379
Keun young Park63f165f2012-08-31 10:53:36 -07002380bool SurfaceFlinger::startDdmConnection()
2381{
2382 void* libddmconnection_dso =
2383 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2384 if (!libddmconnection_dso) {
2385 return false;
2386 }
2387 void (*DdmConnection_start)(const char* name);
2388 DdmConnection_start =
2389 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2390 if (!DdmConnection_start) {
2391 dlclose(libddmconnection_dso);
2392 return false;
2393 }
2394 (*DdmConnection_start)(getServiceName());
2395 return true;
2396}
2397
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002398status_t SurfaceFlinger::onTransact(
2399 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2400{
2401 switch (code) {
2402 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002403 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002404 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002405 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002406 case BLANK:
2407 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002408 {
2409 // codes that require permission check
2410 IPCThreadState* ipc = IPCThreadState::self();
2411 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002412 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002413 if ((uid != AID_GRAPHICS) &&
2414 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002415 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002416 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2417 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002418 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002419 break;
2420 }
2421 case CAPTURE_SCREEN:
2422 {
2423 // codes that require permission check
2424 IPCThreadState* ipc = IPCThreadState::self();
2425 const int pid = ipc->getCallingPid();
2426 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002427 if ((uid != AID_GRAPHICS) &&
2428 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002429 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002430 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2431 return PERMISSION_DENIED;
2432 }
2433 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002434 }
2435 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002437 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2438 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002439 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002440 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002441 IPCThreadState* ipc = IPCThreadState::self();
2442 const int pid = ipc->getCallingPid();
2443 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002444 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002445 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002446 return PERMISSION_DENIED;
2447 }
2448 int n;
2449 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002450 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002451 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002452 return NO_ERROR;
2453 case 1002: // SHOW_UPDATES
2454 n = data.readInt32();
2455 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002456 invalidateHwcGeometry();
2457 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002458 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002459 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002460 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002461 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002462 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002463 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002464 setTransactionFlags(
2465 eTransactionNeeded|
2466 eDisplayTransactionNeeded|
2467 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002468 return NO_ERROR;
2469 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002470 case 1006:{ // send empty update
2471 signalRefresh();
2472 return NO_ERROR;
2473 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002474 case 1008: // toggle use of hw composer
2475 n = data.readInt32();
2476 mDebugDisableHWC = n ? 1 : 0;
2477 invalidateHwcGeometry();
2478 repaintEverything();
2479 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002480 case 1009: // toggle use of transform hint
2481 n = data.readInt32();
2482 mDebugDisableTransformHint = n ? 1 : 0;
2483 invalidateHwcGeometry();
2484 repaintEverything();
2485 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002487 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488 reply->writeInt32(0);
2489 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002490 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002491 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002492 return NO_ERROR;
2493 case 1013: {
2494 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002495 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2496 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497 }
2498 return NO_ERROR;
2499 }
2500 }
2501 return err;
2502}
2503
Mathias Agopian53331da2011-08-22 21:44:41 -07002504void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002505 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002506 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002507}
2508
Mathias Agopian59119e62010-10-11 12:37:43 -07002509// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002510// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002511// ---------------------------------------------------------------------------
2512
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002513status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2514 const sp<IGraphicBufferProducer>& producer,
2515 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002516 uint32_t minLayerZ, uint32_t maxLayerZ,
2517 bool isCpuConsumer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002518
2519 if (CC_UNLIKELY(display == 0))
2520 return BAD_VALUE;
2521
2522 if (CC_UNLIKELY(producer == 0))
2523 return BAD_VALUE;
2524
2525 class MessageCaptureScreen : public MessageBase {
2526 SurfaceFlinger* flinger;
2527 sp<IBinder> display;
2528 sp<IGraphicBufferProducer> producer;
2529 uint32_t reqWidth, reqHeight;
2530 uint32_t minLayerZ,maxLayerZ;
Mathias Agopianabe815d2013-03-19 22:22:21 -07002531 bool isCpuConsumer;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002532 status_t result;
2533 public:
2534 MessageCaptureScreen(SurfaceFlinger* flinger,
2535 const sp<IBinder>& display,
2536 const sp<IGraphicBufferProducer>& producer,
2537 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002538 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002539 : flinger(flinger), display(display), producer(producer),
2540 reqWidth(reqWidth), reqHeight(reqHeight),
2541 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopianabe815d2013-03-19 22:22:21 -07002542 isCpuConsumer(isCpuConsumer),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002543 result(PERMISSION_DENIED)
2544 {
2545 }
2546 status_t getResult() const {
2547 return result;
2548 }
2549 virtual bool handler() {
2550 Mutex::Autolock _l(flinger->mStateLock);
2551 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopianb4b17302013-03-20 18:36:41 -07002552 // When we know the GL->CPU path works, we can call
Mathias Agopianabe815d2013-03-19 22:22:21 -07002553 // captureScreenImplLocked() directly, instead of using the
2554 // "CpuConsumer" version, which is much less efficient -- it is
2555 // however needed by some older drivers.
Mathias Agopianb4b17302013-03-20 18:36:41 -07002556
2557 if (flinger->mGpuToCpuSupported || !isCpuConsumer) {
2558 result = flinger->captureScreenImplLocked(hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002559 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
2560 } else {
Mathias Agopianb4b17302013-03-20 18:36:41 -07002561 result = flinger->captureScreenImplCpuConsumerLocked(hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002562 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
2563 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002564 return true;
2565 }
2566 };
2567
2568 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002569 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2570 isCpuConsumer);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002571 status_t res = postMessageSync(msg);
2572 if (res == NO_ERROR) {
2573 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2574 }
2575 return res;
2576}
2577
2578status_t SurfaceFlinger::captureScreenImplLocked(
2579 const sp<const DisplayDevice>& hw,
2580 const sp<IGraphicBufferProducer>& producer,
2581 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002582 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002583{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002584 ATRACE_CALL();
2585
Mathias Agopian74c40c02010-09-29 13:02:36 -07002586 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002587 const uint32_t hw_w = hw->getWidth();
2588 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002589
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002590 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002591 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002592 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002593 return PERMISSION_DENIED;
2594 }
2595
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002596 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2597 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2598 reqWidth, reqHeight, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002599 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002600 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002601
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002602 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2603 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2604 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002605
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002606 // Create a surface to render into
2607 sp<Surface> surface = new Surface(producer);
2608 ANativeWindow* const window = surface.get();
2609
2610 // set the buffer size to what the user requested
2611 native_window_set_buffers_user_dimensions(window, reqWidth, reqHeight);
2612
2613 // and create the corresponding EGLSurface
2614 EGLSurface eglSurface = eglCreateWindowSurface(
2615 mEGLDisplay, mEGLConfig, window, NULL);
2616 if (eglSurface == EGL_NO_SURFACE) {
2617 ALOGE("captureScreenImplLocked: eglCreateWindowSurface() failed 0x%4x",
2618 eglGetError());
2619 return BAD_VALUE;
2620 }
2621
2622 if (!eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) {
2623 ALOGE("captureScreenImplLocked: eglMakeCurrent() failed 0x%4x",
2624 eglGetError());
2625 eglDestroySurface(mEGLDisplay, eglSurface);
2626 return BAD_VALUE;
2627 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002628
Mathias Agopian74c40c02010-09-29 13:02:36 -07002629 // make sure to clear all GL error flags
2630 while ( glGetError() != GL_NO_ERROR ) ;
2631
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002632 // set-up our viewport
2633 glViewport(0, 0, reqWidth, reqHeight);
2634 glMatrixMode(GL_PROJECTION);
2635 glLoadIdentity();
2636 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2637 glMatrixMode(GL_MODELVIEW);
2638 glLoadIdentity();
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002639
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002640 // redraw the screen entirely...
2641 glDisable(GL_TEXTURE_EXTERNAL_OES);
2642 glDisable(GL_TEXTURE_2D);
2643 glClearColor(0,0,0,1);
2644 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002645
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002646 const LayerVector& layers( mDrawingState.layersSortedByZ );
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002647 const size_t count = layers.size();
2648 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002649 const sp<Layer>& layer(layers[i]);
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002650 const Layer::State& state(layer->drawingState());
2651 if (state.layerStack == hw->getLayerStack()) {
2652 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
Mathias Agopiana493be52013-03-15 16:08:10 -07002653 if (layer->isVisible()) {
2654 if (filtering) layer->setFiltering(true);
2655 layer->draw(hw);
2656 if (filtering) layer->setFiltering(false);
2657 }
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002658 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002659 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002660 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002661
Mathias Agopiand5776412013-03-08 13:54:46 -08002662 // compositionComplete is needed for older driver
2663 hw->compositionComplete();
2664
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002665 // and finishing things up...
2666 if (eglSwapBuffers(mEGLDisplay, eglSurface) != EGL_TRUE) {
2667 ALOGE("captureScreenImplLocked: eglSwapBuffers() failed 0x%4x",
2668 eglGetError());
2669 eglDestroySurface(mEGLDisplay, eglSurface);
2670 return BAD_VALUE;
2671 }
2672
2673 eglDestroySurface(mEGLDisplay, eglSurface);
Mathias Agopianabe815d2013-03-19 22:22:21 -07002674
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002675 return NO_ERROR;
2676}
2677
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002678
Mathias Agopianabe815d2013-03-19 22:22:21 -07002679status_t SurfaceFlinger::captureScreenImplCpuConsumerLocked(
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002680 const sp<const DisplayDevice>& hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002681 const sp<IGraphicBufferProducer>& producer,
2682 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002683 uint32_t minLayerZ, uint32_t maxLayerZ)
2684{
2685 ATRACE_CALL();
2686
2687 if (!GLExtensions::getInstance().haveFramebufferObject()) {
2688 return INVALID_OPERATION;
2689 }
2690
2691 // create the texture that will receive the screenshot, later we'll
2692 // attach a FBO to it so we can call glReadPixels().
2693 GLuint tname;
2694 glGenTextures(1, &tname);
2695 glBindTexture(GL_TEXTURE_2D, tname);
2696 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2697 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2698
2699 // the GLConsumer will provide the BufferQueue
2700 sp<GLConsumer> consumer = new GLConsumer(tname, true, GL_TEXTURE_2D);
2701 consumer->getBufferQueue()->setDefaultBufferFormat(HAL_PIXEL_FORMAT_RGBA_8888);
2702
2703 // call the new screenshot taking code, passing a BufferQueue to it
2704 status_t result = captureScreenImplLocked(hw,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002705 consumer->getBufferQueue(), reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002706
2707 if (result == NO_ERROR) {
2708 result = consumer->updateTexImage();
2709 if (result == NO_ERROR) {
2710 // create a FBO
2711 GLuint name;
2712 glGenFramebuffersOES(1, &name);
2713 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2714 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2715 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
2716
Mathias Agopianabe815d2013-03-19 22:22:21 -07002717 reqWidth = consumer->getCurrentBuffer()->getWidth();
2718 reqHeight = consumer->getCurrentBuffer()->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002719
Mathias Agopianabe815d2013-03-19 22:22:21 -07002720 {
2721 // in this block we render the screenshot into the
2722 // CpuConsumer using glReadPixels from our GLConsumer,
2723 // Some older drivers don't support the GL->CPU path so
2724 // have to wrap it with a CPU->CPU path, which is what
2725 // glReadPixels essentially is
2726
2727 sp<Surface> sur = new Surface(producer);
2728 ANativeWindow* window = sur.get();
2729 ANativeWindowBuffer* buffer;
2730 void* vaddr;
2731
2732 if (native_window_api_connect(window,
2733 NATIVE_WINDOW_API_CPU) == NO_ERROR) {
2734 int err = 0;
2735 err = native_window_set_buffers_dimensions(window,
2736 reqWidth, reqHeight);
2737 err |= native_window_set_buffers_format(window,
2738 HAL_PIXEL_FORMAT_RGBA_8888);
2739 err |= native_window_set_usage(window,
2740 GRALLOC_USAGE_SW_READ_OFTEN |
2741 GRALLOC_USAGE_SW_WRITE_OFTEN);
2742
2743 if (err == NO_ERROR) {
2744 if (native_window_dequeue_buffer_and_wait(window,
2745 &buffer) == NO_ERROR) {
2746 sp<GraphicBuffer> buf =
2747 static_cast<GraphicBuffer*>(buffer);
2748 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2749 &vaddr) == NO_ERROR) {
2750 if (buffer->stride != int(reqWidth)) {
2751 // we're unlucky here, glReadPixels is
2752 // not able to deal with a stride not
2753 // equal to the width.
2754 uint32_t* tmp = new uint32_t[reqWidth*reqHeight];
2755 if (tmp != NULL) {
2756 glReadPixels(0, 0, reqWidth, reqHeight,
2757 GL_RGBA, GL_UNSIGNED_BYTE, tmp);
2758 for (size_t y=0 ; y<reqHeight ; y++) {
2759 memcpy((uint32_t*)vaddr + y*buffer->stride,
2760 tmp + y*reqWidth, reqWidth*4);
2761 }
2762 delete [] tmp;
2763 }
2764 } else {
2765 glReadPixels(0, 0, reqWidth, reqHeight,
2766 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
2767 }
2768 buf->unlock();
2769 }
2770 window->queueBuffer(window, buffer, -1);
2771 }
2772 }
2773 native_window_api_disconnect(window, NATIVE_WINDOW_API_CPU);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002774 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002775 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002776
2777 // back to main framebuffer
2778 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2779 glDeleteFramebuffersOES(1, &name);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002780 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002781 }
2782
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002783 glDeleteTextures(1, &tname);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002784
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002785 DisplayDevice::makeCurrent(mEGLDisplay,
2786 getDefaultDisplayDevice(), mEGLContext);
2787
Mathias Agopian74c40c02010-09-29 13:02:36 -07002788 return result;
2789}
2790
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002791// ---------------------------------------------------------------------------
2792
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002793SurfaceFlinger::LayerVector::LayerVector() {
2794}
2795
2796SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002797 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002798}
2799
2800int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2801 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002802{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002803 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002804 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2805 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002806
2807 uint32_t ls = l->currentState().layerStack;
2808 uint32_t rs = r->currentState().layerStack;
2809 if (ls != rs)
2810 return ls - rs;
2811
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002812 uint32_t lz = l->currentState().z;
2813 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002814 if (lz != rz)
2815 return lz - rz;
2816
2817 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002818}
2819
2820// ---------------------------------------------------------------------------
2821
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002822SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2823 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002824}
2825
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002826SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002827 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002828 viewport.makeInvalid();
2829 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002830}
2831
2832// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002833
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002834}; // namespace android