blob: 5d138d140599dc9cfe0f099461b5068eecbb1183 [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>
Mathias Agopian875d8e12013-06-07 15:35:48 -070027#include <GLES/glext.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
38
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070041#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080044#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045
46#include <ui/GraphicBufferAllocator.h>
47#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070048#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Mathias Agopiancde87a32012-09-13 14:09:01 -070050#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/String8.h>
52#include <utils/String16.h>
53#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080054#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian921e6ac2012-07-23 23:11:29 -070056#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070057#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080062#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#include "RenderEngine/RenderEngine.h"
74
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#define DISPLAY_COUNT 1
76
Mathias Agopianca088332013-03-28 17:44:13 -070077EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080// ---------------------------------------------------------------------------
81
Mathias Agopian99b49842011-06-27 16:05:52 -070082const String16 sHardwareTest("android.permission.HARDWARE_TEST");
83const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
84const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
85const String16 sDump("android.permission.DUMP");
86
87// ---------------------------------------------------------------------------
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089SurfaceFlinger::SurfaceFlinger()
90 : BnSurfaceComposer(), Thread(false),
91 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070092 mTransactionPending(false),
93 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070095 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -070096 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070099 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800100 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700102 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700103 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700104 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700105 mDebugInSwapBuffers(0),
106 mLastSwapBufferTime(0),
107 mDebugInTransaction(0),
108 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700109 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110{
Steve Blocka19954a2012-01-04 20:05:49 +0000111 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112
113 // debugging stuff...
114 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115
Mathias Agopianb4b17302013-03-20 18:36:41 -0700116 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700117 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 property_get("debug.sf.showupdates", value, "0");
120 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700122 property_get("debug.sf.ddms", value, "0");
123 mDebugDDMS = atoi(value);
124 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700125 if (!startDdmConnection()) {
126 // start failed, and DDMS debugging not enabled
127 mDebugDDMS = 0;
128 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700129 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700130 ALOGI_IF(mDebugRegion, "showupdates enabled");
131 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132}
133
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800134void SurfaceFlinger::onFirstRef()
135{
136 mEventQueue.init(this);
137
138 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
139
140 // Wait for the main thread to be done with its initialization
141 mReadyToRunBarrier.wait();
142}
143
144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145SurfaceFlinger::~SurfaceFlinger()
146{
Mathias Agopiana4912602012-07-12 14:25:33 -0700147 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
148 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
149 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150}
151
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152void SurfaceFlinger::binderDied(const wp<IBinder>& who)
153{
154 // the window manager died on us. prepare its eulogy.
155
Andy McFadden13a082e2012-08-24 10:16:42 -0700156 // restore initial conditions (default device unblank, etc)
157 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800158
159 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700160 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800161}
162
Mathias Agopian7e27f052010-05-28 14:22:23 -0700163sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Mathias Agopian96f08192010-06-02 23:28:45 -0700165 sp<ISurfaceComposerClient> bclient;
166 sp<Client> client(new Client(this));
167 status_t err = client->initCheck();
168 if (err == NO_ERROR) {
169 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 return bclient;
172}
173
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700174sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
175 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700176{
177 class DisplayToken : public BBinder {
178 sp<SurfaceFlinger> flinger;
179 virtual ~DisplayToken() {
180 // no more references, this display must be terminated
181 Mutex::Autolock _l(flinger->mStateLock);
182 flinger->mCurrentState.displays.removeItem(this);
183 flinger->setTransactionFlags(eDisplayTransactionNeeded);
184 }
185 public:
186 DisplayToken(const sp<SurfaceFlinger>& flinger)
187 : flinger(flinger) {
188 }
189 };
190
191 sp<BBinder> token = new DisplayToken(this);
192
193 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700194 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700195 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700196 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700197 mCurrentState.displays.add(token, info);
198
199 return token;
200}
201
Jesse Hall692c7232012-11-08 15:41:56 -0800202void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
203 ALOGW_IF(mBuiltinDisplays[type],
204 "Overwriting display token for display type %d", type);
205 mBuiltinDisplays[type] = new BBinder();
206 DisplayDeviceState info(type);
207 // All non-virtual displays are currently considered secure.
208 info.isSecure = true;
209 mCurrentState.displays.add(mBuiltinDisplays[type], info);
210}
211
Mathias Agopiane57f2922012-08-09 16:29:12 -0700212sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700213 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700214 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
215 return NULL;
216 }
Jesse Hall692c7232012-11-08 15:41:56 -0800217 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218}
219
Jamie Gennis9a78c902011-01-12 18:30:40 -0800220sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
221{
222 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
223 return gba;
224}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226void SurfaceFlinger::bootFinished()
227{
228 const nsecs_t now = systemTime();
229 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000230 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700231 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700232
233 // wait patiently for the window manager death
234 const String16 name("window");
235 sp<IBinder> window(defaultServiceManager()->getService(name));
236 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700237 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700238 }
239
240 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700241 // formerly we would just kill the process, but we now ask it to exit so it
242 // can choose where to stop the animation.
243 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700246void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
247 class MessageDestroyGLTexture : public MessageBase {
248 GLuint texture;
249 public:
250 MessageDestroyGLTexture(GLuint texture)
251 : texture(texture) {
252 }
253 virtual bool handler() {
254 glDeleteTextures(1, &texture);
255 return true;
256 }
257 };
258 postMessageAsync(new MessageDestroyGLTexture(texture));
259}
260
Mathias Agopian722b98f2012-09-25 18:24:31 -0700261status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700262 EGLDisplay dpy,
263 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700264 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700265 EGLConfig* outConfig)
266{
267 EGLConfig config = NULL;
268 EGLint numConfigs = -1, n=0;
269 eglGetConfigs(dpy, NULL, 0, &numConfigs);
270 EGLConfig* const configs = new EGLConfig[numConfigs];
271 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700272
Mathias Agopian722b98f2012-09-25 18:24:31 -0700273 if (n) {
274 if (attribute != EGL_NONE) {
275 for (int i=0 ; i<n ; i++) {
276 EGLint value = 0;
277 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
278 if (wanted == value) {
279 *outConfig = configs[i];
280 delete [] configs;
281 return NO_ERROR;
282 }
283 }
284 } else {
285 // just pick the first one
286 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 delete [] configs;
288 return NO_ERROR;
289 }
290 }
291 delete [] configs;
292 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293}
294
Mathias Agopian722b98f2012-09-25 18:24:31 -0700295class EGLAttributeVector {
296 struct Attribute;
297 class Adder;
298 friend class Adder;
299 KeyedVector<Attribute, EGLint> mList;
300 struct Attribute {
301 Attribute() {};
302 Attribute(EGLint v) : v(v) { }
303 EGLint v;
304 bool operator < (const Attribute& other) const {
305 // this places EGL_NONE at the end
306 EGLint lhs(v);
307 EGLint rhs(other.v);
308 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
309 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
310 return lhs < rhs;
311 }
312 };
313 class Adder {
314 friend class EGLAttributeVector;
315 EGLAttributeVector& v;
316 EGLint attribute;
317 Adder(EGLAttributeVector& v, EGLint attribute)
318 : v(v), attribute(attribute) {
319 }
320 public:
321 void operator = (EGLint value) {
322 if (attribute != EGL_NONE) {
323 v.mList.add(attribute, value);
324 }
325 }
326 operator EGLint () const { return v.mList[attribute]; }
327 };
328public:
329 EGLAttributeVector() {
330 mList.add(EGL_NONE, EGL_NONE);
331 }
332 void remove(EGLint attribute) {
333 if (attribute != EGL_NONE) {
334 mList.removeItem(attribute);
335 }
336 }
337 Adder operator [] (EGLint attribute) {
338 return Adder(*this, attribute);
339 }
340 EGLint operator [] (EGLint attribute) const {
341 return mList[attribute];
342 }
343 // cast-operator to (EGLint const*)
344 operator EGLint const* () const { return &mList.keyAt(0).v; }
345};
346
Mathias Agopiana4912602012-07-12 14:25:33 -0700347EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
348 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
349 // it is to be used with WIFI displays
350 EGLConfig config;
351 EGLint dummy;
352 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700353
Mathias Agopian722b98f2012-09-25 18:24:31 -0700354 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700355 // TODO: enable ES2
356 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
357 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700358 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
359 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
360 attribs[EGL_RED_SIZE] = 8;
361 attribs[EGL_GREEN_SIZE] = 8;
362 attribs[EGL_BLUE_SIZE] = 8;
363
364 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
365 if (!err)
366 goto success;
367
Mathias Agopian875d8e12013-06-07 15:35:48 -0700368 // this didn't work, probably because we're on the emulator...
369 // try a simplified query
370 ALOGW("no suitable EGLConfig found, trying a simpler query");
371 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700372 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700373 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700374 attribs.remove(EGL_RED_SIZE);
375 attribs.remove(EGL_GREEN_SIZE);
376 attribs.remove(EGL_BLUE_SIZE);
377 err = selectConfigForAttribute(display, attribs,
378 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700379 if (!err)
380 goto success;
381
382 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700383 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700384 return 0;
385
386success:
387 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700388 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700389 return config;
390}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391
Mathias Agopiana4912602012-07-12 14:25:33 -0700392
Mathias Agopiana4912602012-07-12 14:25:33 -0700393status_t SurfaceFlinger::readyToRun()
394{
395 ALOGI( "SurfaceFlinger's main thread ready to run. "
396 "Initializing graphics H/W...");
397
Jesse Hall692c7232012-11-08 15:41:56 -0800398 Mutex::Autolock _l(mStateLock);
399
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700400 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700401 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
402 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700403
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700404 // Initialize the H/W composer object. There may or may not be an
405 // actual hardware composer underneath.
406 mHwc = new HWComposer(this,
407 *static_cast<HWComposer::EventHandler *>(this));
408
Mathias Agopian875d8e12013-06-07 15:35:48 -0700409 // initialize the config and context (can't fail)
410 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
411
412 // print some debugging info
413 EGLint r,g,b,a;
414 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
415 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
416 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
417 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
418 ALOGI("EGL informations:");
419 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
420 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
421 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
422 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
423 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
424
425 // get a RenderEngine for the given display / config (can't fail)
426 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
427
428 // retrieve the EGL context that was selected/created
429 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700430
Mathias Agopianed985572013-03-22 00:24:39 -0700431 // figure out which format we got
432 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
433 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
434
Mathias Agopianda27af92012-09-13 18:17:13 -0700435 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
436 "couldn't create EGLContext");
437
Mathias Agopiancde87a32012-09-13 14:09:01 -0700438 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700439 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700440 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700441 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700442 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700443 // All non-virtual displays are currently considered secure.
444 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800445 createBuiltinDisplayLocked(type);
446 wp<IBinder> token = mBuiltinDisplays[i];
447
Mathias Agopianf5a33922012-09-19 18:16:22 -0700448 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700449 type, allocateHwcDisplayId(type), isSecure, token,
450 new FramebufferSurface(*mHwc, i),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700451 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700452 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700453 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700454 // for displays other than the main display, so we always
455 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700456 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700457 hw->acquireScreen();
458 }
459 mDisplays.add(token, hw);
460 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700461 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700462
Mathias Agopian028508c2012-07-25 21:12:12 -0700463 // start the EventThread
464 mEventThread = new EventThread(this);
465 mEventQueue.setEventThread(mEventThread);
466
Mathias Agopian92a979a2012-08-02 18:32:23 -0700467 // initialize our drawing state
468 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700469
Mathias Agopiana4912602012-07-12 14:25:33 -0700470 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800471 mReadyToRunBarrier.open();
472
Andy McFadden13a082e2012-08-24 10:16:42 -0700473 // set initial conditions (e.g. unblank default device)
474 initializeDisplays();
475
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700476 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700477 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700478
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 return NO_ERROR;
480}
481
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700482int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
483 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
484 type : mHwc->allocateDisplayId();
485}
486
Mathias Agopiana67e4182012-06-19 17:26:12 -0700487void SurfaceFlinger::startBootAnim() {
488 // start boot animation
489 property_set("service.bootanim.exit", "0");
490 property_set("ctl.start", "bootanim");
491}
492
Mathias Agopian875d8e12013-06-07 15:35:48 -0700493size_t SurfaceFlinger::getMaxTextureSize() const {
494 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700495}
496
Mathias Agopian875d8e12013-06-07 15:35:48 -0700497size_t SurfaceFlinger::getMaxViewportDims() const {
498 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700499}
500
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800502
Jamie Gennis582270d2011-08-17 18:19:00 -0700503bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800504 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800505 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800506 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700507 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800508}
509
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700510status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800511 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700512 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800513 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700514 type = i;
515 break;
516 }
517 }
518
519 if (type < 0) {
520 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700521 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700522
523 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700524 float xdpi = hwc.getDpiX(type);
525 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700526
527 // TODO: Not sure if display density should handled by SF any longer
528 class Density {
529 static int getDensityFromProperty(char const* propName) {
530 char property[PROPERTY_VALUE_MAX];
531 int density = 0;
532 if (property_get(propName, property, NULL) > 0) {
533 density = atoi(property);
534 }
535 return density;
536 }
537 public:
538 static int getEmuDensity() {
539 return getDensityFromProperty("qemu.sf.lcd_density"); }
540 static int getBuildDensity() {
541 return getDensityFromProperty("ro.sf.lcd_density"); }
542 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700543
544 if (type == DisplayDevice::DISPLAY_PRIMARY) {
545 // The density of the device is provided by a build property
546 float density = Density::getBuildDensity() / 160.0f;
547 if (density == 0) {
548 // the build doesn't provide a density -- this is wrong!
549 // use xdpi instead
550 ALOGE("ro.sf.lcd_density must be defined as a build property");
551 density = xdpi / 160.0f;
552 }
553 if (Density::getEmuDensity()) {
554 // if "qemu.sf.lcd_density" is specified, it overrides everything
555 xdpi = ydpi = density = Density::getEmuDensity();
556 density /= 160.0f;
557 }
558 info->density = density;
559
560 // TODO: this needs to go away (currently needed only by webkit)
561 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
562 info->orientation = hw->getOrientation();
563 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
564 } else {
565 // TODO: where should this value come from?
566 static const int TV_DENSITY = 213;
567 info->density = TV_DENSITY / 160.0f;
568 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700569 }
570
Mathias Agopian1604f772012-09-18 21:54:42 -0700571 info->w = hwc.getWidth(type);
572 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700573 info->xdpi = xdpi;
574 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700575 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700576
577 // All non-virtual displays are currently considered secure.
578 info->secure = true;
579
Mathias Agopian888c8222012-08-04 21:10:38 -0700580 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700581}
582
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800583// ----------------------------------------------------------------------------
584
585sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800586 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700587}
588
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800590
591void SurfaceFlinger::waitForEvent() {
592 mEventQueue.waitMessage();
593}
594
595void SurfaceFlinger::signalTransaction() {
596 mEventQueue.invalidate();
597}
598
599void SurfaceFlinger::signalLayerUpdate() {
600 mEventQueue.invalidate();
601}
602
603void SurfaceFlinger::signalRefresh() {
604 mEventQueue.refresh();
605}
606
607status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
608 nsecs_t reltime, uint32_t flags) {
609 return mEventQueue.postMessage(msg, reltime);
610}
611
612status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
613 nsecs_t reltime, uint32_t flags) {
614 status_t res = mEventQueue.postMessage(msg, reltime);
615 if (res == NO_ERROR) {
616 msg->wait();
617 }
618 return res;
619}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620
Mathias Agopian4fec8732012-06-29 14:12:52 -0700621bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 return true;
624}
625
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700626void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700627 if (mEventThread == NULL) {
628 // This is a temporary workaround for b/7145521. A non-null pointer
629 // does not mean EventThread has finished initializing, so this
630 // is not a correct fix.
631 ALOGW("WARNING: EventThread not started, ignoring vsync");
632 return;
633 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700634 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
635 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700636 mEventThread->onVSyncReceived(type, timestamp);
637 }
638}
639
640void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
641 if (mEventThread == NULL) {
642 // This is a temporary workaround for b/7145521. A non-null pointer
643 // does not mean EventThread has finished initializing, so this
644 // is not a correct fix.
645 ALOGW("WARNING: EventThread not started, ignoring hotplug");
646 return;
647 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700648
Mathias Agopian148994e2012-09-19 17:31:36 -0700649 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700650 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800651 if (connected) {
652 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700653 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800654 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
655 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700656 }
657 setTransactionFlags(eDisplayTransactionNeeded);
658
Andy McFadden9e9689c2012-10-10 18:17:51 -0700659 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700660 }
Mathias Agopian86303202012-07-24 22:46:10 -0700661}
662
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700663void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
664 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700665}
666
Mathias Agopian4fec8732012-06-29 14:12:52 -0700667void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800668 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800669 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700670 case MessageQueue::TRANSACTION:
671 handleMessageTransaction();
672 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700673 case MessageQueue::INVALIDATE:
674 handleMessageTransaction();
675 handleMessageInvalidate();
676 signalRefresh();
677 break;
678 case MessageQueue::REFRESH:
679 handleMessageRefresh();
680 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800681 }
682}
683
Mathias Agopian4fec8732012-06-29 14:12:52 -0700684void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700685 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700686 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700687 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700688 }
689}
690
691void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700692 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700693 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700694}
695
696void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700697 ATRACE_CALL();
698 preComposition();
699 rebuildLayerStacks();
700 setUpHWComposer();
701 doDebugFlashRegions();
702 doComposition();
703 postComposition();
704}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700705
Mathias Agopiancd60f992012-08-16 16:28:27 -0700706void SurfaceFlinger::doDebugFlashRegions()
707{
708 // is debugging enabled
709 if (CC_LIKELY(!mDebugRegion))
710 return;
711
712 const bool repaintEverything = mRepaintEverything;
713 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
714 const sp<DisplayDevice>& hw(mDisplays[dpy]);
715 if (hw->canDraw()) {
716 // transform the dirty region into this screen's coordinate space
717 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
718 if (!dirtyRegion.isEmpty()) {
719 // redraw the whole screen
720 doComposeSurfaces(hw, Region(hw->bounds()));
721
722 // and draw the dirty region
723 glDisable(GL_TEXTURE_EXTERNAL_OES);
724 glDisable(GL_TEXTURE_2D);
725 glDisable(GL_BLEND);
726 glColor4f(1, 0, 1, 1);
727 const int32_t height = hw->getHeight();
728 Region::const_iterator it = dirtyRegion.begin();
729 Region::const_iterator const end = dirtyRegion.end();
730 while (it != end) {
731 const Rect& r = *it++;
732 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800733 { (GLfloat) r.left, (GLfloat) (height - r.top) },
734 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
735 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
736 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700737 };
738 glVertexPointer(2, GL_FLOAT, 0, vertices);
739 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
740 }
741 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700742 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700743 }
744 }
745 }
746
747 postFramebuffer();
748
749 if (mDebugRegion > 1) {
750 usleep(mDebugRegion * 1000);
751 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700752
753 HWComposer& hwc(getHwComposer());
754 if (hwc.initCheck() == NO_ERROR) {
755 status_t err = hwc.prepare();
756 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
757 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700758}
759
760void SurfaceFlinger::preComposition()
761{
762 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700763 const LayerVector& layers(mDrawingState.layersSortedByZ);
764 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700765 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700766 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700767 needExtraInvalidate = true;
768 }
769 }
770 if (needExtraInvalidate) {
771 signalLayerUpdate();
772 }
773}
774
775void SurfaceFlinger::postComposition()
776{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700777 const LayerVector& layers(mDrawingState.layersSortedByZ);
778 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700779 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700780 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700781 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800782
783 if (mAnimCompositionPending) {
784 mAnimCompositionPending = false;
785
786 const HWComposer& hwc = getHwComposer();
787 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800788 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800789 mAnimFrameTracker.setActualPresentFence(presentFence);
790 } else {
791 // The HWC doesn't support present fences, so use the refresh
792 // timestamp instead.
793 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
794 mAnimFrameTracker.setActualPresentTime(presentTime);
795 }
796 mAnimFrameTracker.advanceFrame();
797 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700798}
799
800void SurfaceFlinger::rebuildLayerStacks() {
801 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700802 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700803 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700804 mVisibleRegionsDirty = false;
805 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700806
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700807 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700808 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700809 Region opaqueRegion;
810 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800811 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700812 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700813 const Transform& tr(hw->getTransform());
814 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700815 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700816 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700817 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700818
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700819 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700820 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700821 const sp<Layer>& layer(layers[i]);
822 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700823 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700824 Region drawRegion(tr.transform(
825 layer->visibleNonTransparentRegion));
826 drawRegion.andSelf(bounds);
827 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700828 layersSortedByZ.add(layer);
829 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700830 }
831 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700832 }
Mathias Agopian42977342012-08-05 00:40:46 -0700833 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700834 hw->undefinedRegion.set(bounds);
835 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
836 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700837 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700838 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700839}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700840
Mathias Agopiancd60f992012-08-16 16:28:27 -0700841void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700842 HWComposer& hwc(getHwComposer());
843 if (hwc.initCheck() == NO_ERROR) {
844 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700845 if (CC_UNLIKELY(mHwWorkListDirty)) {
846 mHwWorkListDirty = false;
847 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
848 sp<const DisplayDevice> hw(mDisplays[dpy]);
849 const int32_t id = hw->getHwcDisplayId();
850 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800851 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700852 hw->getVisibleLayersSortedByZ());
853 const size_t count = currentLayers.size();
854 if (hwc.createWorkList(id, count) == NO_ERROR) {
855 HWComposer::LayerListIterator cur = hwc.begin(id);
856 const HWComposer::LayerListIterator end = hwc.end(id);
857 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700859 layer->setGeometry(hw, *cur);
860 if (mDebugDisableHWC || mDebugRegion) {
861 cur->setSkip(true);
862 }
863 }
864 }
865 }
866 }
867 }
868
869 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700870 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700871 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700872 const int32_t id = hw->getHwcDisplayId();
873 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800874 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700875 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700876 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700877 HWComposer::LayerListIterator cur = hwc.begin(id);
878 const HWComposer::LayerListIterator end = hwc.end(id);
879 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
880 /*
881 * update the per-frame h/w composer data for each layer
882 * and build the transparent region of the FB
883 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800884 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700885 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700886 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700887 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700888 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700889
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700890 status_t err = hwc.prepare();
891 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
892 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700893}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700894
Mathias Agopiancd60f992012-08-16 16:28:27 -0700895void SurfaceFlinger::doComposition() {
896 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700897 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700898 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700899 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700900 if (hw->canDraw()) {
901 // transform the dirty region into this screen's coordinate space
902 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800903
904 // repaint the framebuffer (if needed)
905 doDisplayComposition(hw, dirtyRegion);
906
Mathias Agopiancd60f992012-08-16 16:28:27 -0700907 hw->dirtyRegion.clear();
908 hw->flip(hw->swapRegion);
909 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700910 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700911 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700912 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700913 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700914 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700915}
916
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917void SurfaceFlinger::postFramebuffer()
918{
Mathias Agopian841cde52012-03-01 15:44:37 -0800919 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800920
Mathias Agopiana44b0412011-10-16 18:46:35 -0700921 const nsecs_t now = systemTime();
922 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700923
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700924 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700925 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700926 if (!hwc.supportsFramebufferTarget()) {
927 // EGL spec says:
928 // "surface must be bound to the calling thread's current context,
929 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700930 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700931 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700932 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700933 }
934
Mathias Agopian92a979a2012-08-02 18:32:23 -0700935 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700936 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800937 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700938 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700939 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700940 int32_t id = hw->getHwcDisplayId();
941 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700942 HWComposer::LayerListIterator cur = hwc.begin(id);
943 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700944 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700945 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700946 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700947 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700948 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700949 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700950 }
Jesse Hallef194142012-06-14 14:45:17 -0700951 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800952 }
953
Mathias Agopiana44b0412011-10-16 18:46:35 -0700954 mLastSwapBufferTime = systemTime() - now;
955 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956}
957
Mathias Agopian87baae12012-07-31 12:38:26 -0700958void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959{
Mathias Agopian841cde52012-03-01 15:44:37 -0800960 ATRACE_CALL();
961
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700962 // here we keep a copy of the drawing state (that is the state that's
963 // going to be overwritten by handleTransactionLocked()) outside of
964 // mStateLock so that the side-effects of the State assignment
965 // don't happen with mStateLock held (which can cause deadlocks).
966 State drawingState(mDrawingState);
967
Mathias Agopianca4d3602011-05-19 15:38:14 -0700968 Mutex::Autolock _l(mStateLock);
969 const nsecs_t now = systemTime();
970 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
Mathias Agopianca4d3602011-05-19 15:38:14 -0700972 // Here we're guaranteed that some transaction flags are set
973 // so we can call handleTransactionLocked() unconditionally.
974 // We call getTransactionFlags(), which will also clear the flags,
975 // with mStateLock held to guarantee that mCurrentState won't change
976 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700977
Mathias Agopiane57f2922012-08-09 16:29:12 -0700978 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700979 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700980
Mathias Agopianca4d3602011-05-19 15:38:14 -0700981 mLastTransactionTime = systemTime() - now;
982 mDebugInTransaction = 0;
983 invalidateHwcGeometry();
984 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700985}
986
Mathias Agopian87baae12012-07-31 12:38:26 -0700987void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700988{
989 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 const size_t count = currentLayers.size();
991
992 /*
993 * Traversal of the children
994 * (perform the transaction for each of them if needed)
995 */
996
Mathias Agopian3559b072012-08-15 13:46:03 -0700997 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800999 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1001 if (!trFlags) continue;
1002
1003 const uint32_t flags = layer->doTransaction(0);
1004 if (flags & Layer::eVisibleRegion)
1005 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 }
1007 }
1008
1009 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001010 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 */
1012
Mathias Agopiane57f2922012-08-09 16:29:12 -07001013 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001014 // here we take advantage of Vector's copy-on-write semantics to
1015 // improve performance by skipping the transaction entirely when
1016 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001017 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1018 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001019 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001021 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001022 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001023
1024 // find the displays that were removed
1025 // (ie: in drawing state but not in current state)
1026 // also handle displays that changed
1027 // (ie: displays that are in both lists)
1028 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001029 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1030 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001031 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001032 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001033 // Call makeCurrent() on the primary display so we can
1034 // be sure that nothing associated with this display
1035 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001036 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001037 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001038 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1039 if (hw != NULL)
1040 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001041 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1042 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001043 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001044 } else {
1045 ALOGW("trying to remove the main display");
1046 }
1047 } else {
1048 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001049 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001050 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001051 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001052 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001053 // recreating the DisplayDevice, so we just remove it
1054 // from the drawing state, so that it get re-added
1055 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001056 sp<DisplayDevice> hw(getDisplayDevice(display));
1057 if (hw != NULL)
1058 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001059 mDisplays.removeItem(display);
1060 mDrawingState.displays.removeItemsAt(i);
1061 dc--; i--;
1062 // at this point we must loop to the next item
1063 continue;
1064 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001065
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001066 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001067 if (disp != NULL) {
1068 if (state.layerStack != draw[i].layerStack) {
1069 disp->setLayerStack(state.layerStack);
1070 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001071 if ((state.orientation != draw[i].orientation)
1072 || (state.viewport != draw[i].viewport)
1073 || (state.frame != draw[i].frame))
1074 {
1075 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001076 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001077 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001078 }
1079 }
1080 }
1081
1082 // find displays that were added
1083 // (ie: in current state but not in drawing state)
1084 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001085 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001086 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001087
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001088 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001089 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001090 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001091 // Virtual displays without a surface are dormant:
1092 // they have external state (layer stack, projection,
1093 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001094 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001095 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001096 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001097 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001098 state.displayName);
1099 }
1100 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001101 ALOGE_IF(state.surface!=NULL,
1102 "adding a supported display, but rendering "
1103 "surface is provided (%p), ignoring it",
1104 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001105 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001106 // for supported (by hwc) displays we provide our
1107 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001108 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001109 }
1110
1111 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001112 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001113 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001114 state.type, hwcDisplayId, state.isSecure,
1115 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001116 hw->setLayerStack(state.layerStack);
1117 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001118 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001119 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001120 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001121 if (state.isVirtualDisplay()) {
1122 if (hwcDisplayId >= 0) {
1123 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1124 hw->getWidth(), hw->getHeight(),
1125 hw->getFormat());
1126 }
1127 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001128 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001129 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001130 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001131 }
1132 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001134 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135
Mathias Agopian84300952012-11-21 16:02:13 -08001136 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1137 // The transform hint might have changed for some layers
1138 // (either because a display has changed, or because a layer
1139 // as changed).
1140 //
1141 // Walk through all the layers in currentLayers,
1142 // and update their transform hint.
1143 //
1144 // If a layer is visible only on a single display, then that
1145 // display is used to calculate the hint, otherwise we use the
1146 // default display.
1147 //
1148 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1149 // the hint is set before we acquire a buffer from the surface texture.
1150 //
1151 // NOTE: layer transactions have taken place already, so we use their
1152 // drawing state. However, SurfaceFlinger's own transaction has not
1153 // happened yet, so we must use the current state layer list
1154 // (soon to become the drawing state list).
1155 //
1156 sp<const DisplayDevice> disp;
1157 uint32_t currentlayerStack = 0;
1158 for (size_t i=0; i<count; i++) {
1159 // NOTE: we rely on the fact that layers are sorted by
1160 // layerStack first (so we don't have to traverse the list
1161 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001162 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001163 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001164 if (i==0 || currentlayerStack != layerStack) {
1165 currentlayerStack = layerStack;
1166 // figure out if this layerstack is mirrored
1167 // (more than one display) if so, pick the default display,
1168 // if not, pick the only display it's on.
1169 disp.clear();
1170 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1171 sp<const DisplayDevice> hw(mDisplays[dpy]);
1172 if (hw->getLayerStack() == currentlayerStack) {
1173 if (disp == NULL) {
1174 disp = hw;
1175 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001176 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001177 break;
1178 }
1179 }
1180 }
1181 }
Chet Haase91d25932013-04-11 15:24:55 -07001182 if (disp == NULL) {
1183 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1184 // redraw after transform hint changes. See bug 8508397.
1185
1186 // could be null when this layer is using a layerStack
1187 // that is not visible on any display. Also can occur at
1188 // screen off/on times.
1189 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001190 }
Chet Haase91d25932013-04-11 15:24:55 -07001191 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001192 }
1193 }
1194
1195
Mathias Agopian3559b072012-08-15 13:46:03 -07001196 /*
1197 * Perform our own transaction if needed
1198 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001199
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001200 const LayerVector& layers(mDrawingState.layersSortedByZ);
1201 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001202 // layers have been added
1203 mVisibleRegionsDirty = true;
1204 }
1205
1206 // some layers might have been removed, so
1207 // we need to update the regions they're exposing.
1208 if (mLayersRemoved) {
1209 mLayersRemoved = false;
1210 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001211 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001212 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001213 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001214 if (currentLayers.indexOf(layer) < 0) {
1215 // this layer is not visible anymore
1216 // TODO: we could traverse the tree from front to back and
1217 // compute the actual visible region
1218 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001219 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001220 Region visibleReg = s.transform.transform(
1221 Region(Rect(s.active.w, s.active.h)));
1222 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001223 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 }
1226
1227 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001228}
1229
1230void SurfaceFlinger::commitTransaction()
1231{
1232 if (!mLayersPendingRemoval.isEmpty()) {
1233 // Notify removed layers now that they can't be drawn from
1234 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1235 mLayersPendingRemoval[i]->onRemoved();
1236 }
1237 mLayersPendingRemoval.clear();
1238 }
1239
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001240 // If this transaction is part of a window animation then the next frame
1241 // we composite should be considered an animation as well.
1242 mAnimCompositionPending = mAnimTransactionPending;
1243
Mathias Agopian4fec8732012-06-29 14:12:52 -07001244 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001245 mTransactionPending = false;
1246 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001247 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248}
1249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001251 const LayerVector& currentLayers, uint32_t layerStack,
1252 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253{
Mathias Agopian841cde52012-03-01 15:44:37 -08001254 ATRACE_CALL();
1255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 Region aboveOpaqueLayers;
1257 Region aboveCoveredLayers;
1258 Region dirty;
1259
Mathias Agopian87baae12012-07-31 12:38:26 -07001260 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261
1262 size_t i = currentLayers.size();
1263 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001264 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265
1266 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001267 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268
Jesse Hall01e29052013-02-19 16:13:35 -08001269 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001270 if (s.layerStack != layerStack)
1271 continue;
1272
Mathias Agopianab028732010-03-16 16:41:46 -07001273 /*
1274 * opaqueRegion: area of a surface that is fully opaque.
1275 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001277
1278 /*
1279 * visibleRegion: area of a surface that is visible on screen
1280 * and not fully transparent. This is essentially the layer's
1281 * footprint minus the opaque regions above it.
1282 * Areas covered by a translucent surface are considered visible.
1283 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001285
1286 /*
1287 * coveredRegion: area of a surface that is covered by all
1288 * visible regions above it (which includes the translucent areas).
1289 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001291
Jesse Halla8026d22012-09-25 13:25:04 -07001292 /*
1293 * transparentRegion: area of a surface that is hinted to be completely
1294 * transparent. This is only used to tell when the layer has no visible
1295 * non-transparent regions and can be removed from the layer list. It
1296 * does not affect the visibleRegion of this layer or any layers
1297 * beneath it. The hint may not be correct if apps don't respect the
1298 * SurfaceView restrictions (which, sadly, some don't).
1299 */
1300 Region transparentRegion;
1301
Mathias Agopianab028732010-03-16 16:41:46 -07001302
1303 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001304 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001305 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001306 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001308 if (!visibleRegion.isEmpty()) {
1309 // Remove the transparent area from the visible region
1310 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001311 const Transform tr(s.transform);
1312 if (tr.transformed()) {
1313 if (tr.preserveRects()) {
1314 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001315 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001316 } else {
1317 // transformation too complex, can't do the
1318 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001319 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001320 }
1321 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001322 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001323 }
Mathias Agopianab028732010-03-16 16:41:46 -07001324 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325
Mathias Agopianab028732010-03-16 16:41:46 -07001326 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001327 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001328 if (s.alpha==255 && !translucent &&
1329 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1330 // the opaque region is the layer's footprint
1331 opaqueRegion = visibleRegion;
1332 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 }
1334 }
1335
Mathias Agopianab028732010-03-16 16:41:46 -07001336 // Clip the covered region to the visible region
1337 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1338
1339 // Update aboveCoveredLayers for next (lower) layer
1340 aboveCoveredLayers.orSelf(visibleRegion);
1341
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 // subtract the opaque region covered by the layers above us
1343 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344
1345 // compute this layer's dirty region
1346 if (layer->contentDirty) {
1347 // we need to invalidate the whole region
1348 dirty = visibleRegion;
1349 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001350 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 layer->contentDirty = false;
1352 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001353 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001354 * the exposed region consists of two components:
1355 * 1) what's VISIBLE now and was COVERED before
1356 * 2) what's EXPOSED now less what was EXPOSED before
1357 *
1358 * note that (1) is conservative, we start with the whole
1359 * visible region but only keep what used to be covered by
1360 * something -- which mean it may have been exposed.
1361 *
1362 * (2) handles areas that were not covered by anything but got
1363 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001364 */
Mathias Agopianab028732010-03-16 16:41:46 -07001365 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001366 const Region oldVisibleRegion = layer->visibleRegion;
1367 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001368 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1369 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 }
1371 dirty.subtractSelf(aboveOpaqueLayers);
1372
1373 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001374 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375
Mathias Agopianab028732010-03-16 16:41:46 -07001376 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001378
Jesse Halla8026d22012-09-25 13:25:04 -07001379 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 layer->setVisibleRegion(visibleRegion);
1381 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001382 layer->setVisibleNonTransparentRegion(
1383 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384 }
1385
Mathias Agopian87baae12012-07-31 12:38:26 -07001386 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387}
1388
Mathias Agopian87baae12012-07-31 12:38:26 -07001389void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1390 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001391 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001392 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1393 if (hw->getLayerStack() == layerStack) {
1394 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001395 }
1396 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001397}
1398
1399void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001401 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402
Mathias Agopian4fec8732012-06-29 14:12:52 -07001403 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001404 const LayerVector& layers(mDrawingState.layersSortedByZ);
1405 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001406 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001407 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001408 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001409 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001410 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001411 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001412
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001413 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414}
1415
Mathias Agopianad456f92011-01-13 17:53:01 -08001416void SurfaceFlinger::invalidateHwcGeometry()
1417{
1418 mHwWorkListDirty = true;
1419}
1420
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001421
Mathias Agopiancd60f992012-08-16 16:28:27 -07001422void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001423 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424{
Mathias Agopian87baae12012-07-31 12:38:26 -07001425 Region dirtyRegion(inDirtyRegion);
1426
Mathias Agopianb8a55602009-06-26 19:06:36 -07001427 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001428 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429
Mathias Agopian42977342012-08-05 00:40:46 -07001430 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001431 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001432 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1433 // takes a rectangle, we must make sure to update that whole
1434 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001435 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001436 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001437 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001438 // We need to redraw the rectangle that will be updated
1439 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001440 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001441 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001442 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001443 } else {
1444 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001445 dirtyRegion.set(hw->bounds());
1446 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447 }
1448 }
1449
Mathias Agopiancd60f992012-08-16 16:28:27 -07001450 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001452 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001453 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001454
1455 // swap buffers (presentation)
1456 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457}
1458
Mathias Agopiancd60f992012-08-16 16:28:27 -07001459void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001460{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001461 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001462 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001463 HWComposer::LayerListIterator cur = hwc.begin(id);
1464 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001465
Mathias Agopian85d751c2012-08-29 16:59:24 -07001466 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1467 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001468 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001469 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1470 hw->getDisplayName().string());
1471 return;
1472 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001473
1474 // set the frame buffer
1475 glMatrixMode(GL_MODELVIEW);
1476 glLoadIdentity();
1477
1478 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001479 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001480 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001481 // when using overlays, we assume a fully transparent framebuffer
1482 // NOTE: we could reduce how much we need to clear, for instance
1483 // remove where there are opaque FB layers. however, on some
1484 // GPUs doing a "clean slate" glClear might be more efficient.
1485 // We'll revisit later if needed.
1486 glClearColor(0, 0, 0, 0);
1487 glClear(GL_COLOR_BUFFER_BIT);
1488 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001489 // we start with the whole screen area
1490 const Region bounds(hw->getBounds());
1491
1492 // we remove the scissor part
1493 // we're left with the letterbox region
1494 // (common case is that letterbox ends-up being empty)
1495 const Region letterbox(bounds.subtract(hw->getScissor()));
1496
1497 // compute the area to clear
1498 Region region(hw->undefinedRegion.merge(letterbox));
1499
1500 // but limit it to the dirty region
1501 region.andSelf(dirty);
1502
Mathias Agopianb9494d52012-04-18 02:28:45 -07001503 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001504 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001505 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001506 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001507 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001508 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001509
Mathias Agopian766dc492012-10-30 18:08:06 -07001510 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1511 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001512 // scissor on the main display. It should never be needed
1513 // anyways (though in theory it could since the API allows it).
1514 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001515 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001516 if (scissor != bounds) {
1517 // scissor doesn't match the screen's dimensions, so we
1518 // need to clear everything outside of it and enable
1519 // the GL scissor so we don't draw anything where we shouldn't
1520 const GLint height = hw->getHeight();
1521 glScissor(scissor.left, height - scissor.bottom,
1522 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001523 // enable scissor for this frame
1524 glEnable(GL_SCISSOR_TEST);
1525 }
1526 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001527 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001528
Mathias Agopian85d751c2012-08-29 16:59:24 -07001529 /*
1530 * and then, render the layers targeted at the framebuffer
1531 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001532
Mathias Agopian13127d82013-03-05 17:47:11 -08001533 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001534 const size_t count = layers.size();
1535 const Transform& tr = hw->getTransform();
1536 if (cur != end) {
1537 // we're using h/w composer
1538 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001539 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001540 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001541 if (!clip.isEmpty()) {
1542 switch (cur->getCompositionType()) {
1543 case HWC_OVERLAY: {
1544 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1545 && i
1546 && layer->isOpaque()
1547 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001548 // never clear the very first layer since we're
1549 // guaranteed the FB is already cleared
1550 layer->clearWithOpenGL(hw, clip);
1551 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001552 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001553 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001554 case HWC_FRAMEBUFFER: {
1555 layer->draw(hw, clip);
1556 break;
1557 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001558 case HWC_FRAMEBUFFER_TARGET: {
1559 // this should not happen as the iterator shouldn't
1560 // let us get there.
1561 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1562 break;
1563 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001564 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001565 }
1566 layer->setAcquireFence(hw, *cur);
1567 }
1568 } else {
1569 // we're not using h/w composer
1570 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001571 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001572 const Region clip(dirty.intersect(
1573 tr.transform(layer->visibleRegion)));
1574 if (!clip.isEmpty()) {
1575 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001576 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001577 }
1578 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001579
1580 // disable scissor at the end of the frame
1581 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582}
1583
Mathias Agopian55801e42012-08-27 18:54:24 -07001584void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1585 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001587 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001588 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001589 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001590 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001591
Mathias Agopian55801e42012-08-27 18:54:24 -07001592 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001593 Region::const_iterator it = region.begin();
1594 Region::const_iterator const end = region.end();
1595 while (it != end) {
1596 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001597 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001598 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1599 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1600 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1601 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001602 };
1603 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001604 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1605 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606}
1607
Mathias Agopianac9fa422013-02-11 16:40:36 -08001608void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1609 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001610 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001611 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001612{
Mathias Agopian96f08192010-06-02 23:28:45 -07001613 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001614 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001615
Mathias Agopian96f08192010-06-02 23:28:45 -07001616 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001617 Mutex::Autolock _l(mStateLock);
1618 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001619 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001620}
1621
Mathias Agopian13127d82013-03-05 17:47:11 -08001622status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623{
1624 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001625 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001627 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001628 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001629 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 return NO_ERROR;
1631 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001632 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633}
1634
Mathias Agopiandea20b12011-05-03 17:04:02 -07001635uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1636{
1637 return android_atomic_release_load(&mTransactionFlags);
1638}
1639
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1641{
1642 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1643}
1644
Mathias Agopianbb641242010-05-18 17:06:55 -07001645uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646{
1647 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1648 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001649 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 }
1651 return old;
1652}
1653
Mathias Agopian8b33f032012-07-24 20:43:54 -07001654void SurfaceFlinger::setTransactionState(
1655 const Vector<ComposerState>& state,
1656 const Vector<DisplayState>& displays,
1657 uint32_t flags)
1658{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001659 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001660 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001661 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001662
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001663 if (flags & eAnimation) {
1664 // For window updates that are part of an animation we must wait for
1665 // previous animation "frames" to be handled.
1666 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001667 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001668 if (CC_UNLIKELY(err != NO_ERROR)) {
1669 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001670 // caller after a few seconds.
1671 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1672 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001673 mAnimTransactionPending = false;
1674 break;
1675 }
1676 }
1677 }
1678
Mathias Agopiane57f2922012-08-09 16:29:12 -07001679 size_t count = displays.size();
1680 for (size_t i=0 ; i<count ; i++) {
1681 const DisplayState& s(displays[i]);
1682 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001683 }
1684
Mathias Agopiane57f2922012-08-09 16:29:12 -07001685 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001686 for (size_t i=0 ; i<count ; i++) {
1687 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001688 // Here we need to check that the interface we're given is indeed
1689 // one of our own. A malicious client could give us a NULL
1690 // IInterface, or one of its own or even one of our own but a
1691 // different type. All these situations would cause us to crash.
1692 //
1693 // NOTE: it would be better to use RTTI as we could directly check
1694 // that we have a Client*. however, RTTI is disabled in Android.
1695 if (s.client != NULL) {
1696 sp<IBinder> binder = s.client->asBinder();
1697 if (binder != NULL) {
1698 String16 desc(binder->getInterfaceDescriptor());
1699 if (desc == ISurfaceComposerClient::descriptor) {
1700 sp<Client> client( static_cast<Client *>(s.client.get()) );
1701 transactionFlags |= setClientStateLocked(client, s.state);
1702 }
1703 }
1704 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001705 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001706
Mathias Agopian386aa982011-11-07 21:58:03 -08001707 if (transactionFlags) {
1708 // this triggers the transaction
1709 setTransactionFlags(transactionFlags);
1710
1711 // if this is a synchronous transaction, wait for it to take effect
1712 // before returning.
1713 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001714 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001715 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001716 if (flags & eAnimation) {
1717 mAnimTransactionPending = true;
1718 }
1719 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001720 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1721 if (CC_UNLIKELY(err != NO_ERROR)) {
1722 // just in case something goes wrong in SF, return to the
1723 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001724 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1725 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001726 break;
1727 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001728 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 }
1730}
1731
Mathias Agopiane57f2922012-08-09 16:29:12 -07001732uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1733{
Jesse Hall9a143922012-10-04 16:29:19 -07001734 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1735 if (dpyIdx < 0)
1736 return 0;
1737
Mathias Agopiane57f2922012-08-09 16:29:12 -07001738 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001739 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001740 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001741 const uint32_t what = s.what;
1742 if (what & DisplayState::eSurfaceChanged) {
1743 if (disp.surface->asBinder() != s.surface->asBinder()) {
1744 disp.surface = s.surface;
1745 flags |= eDisplayTransactionNeeded;
1746 }
1747 }
1748 if (what & DisplayState::eLayerStackChanged) {
1749 if (disp.layerStack != s.layerStack) {
1750 disp.layerStack = s.layerStack;
1751 flags |= eDisplayTransactionNeeded;
1752 }
1753 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001754 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001755 if (disp.orientation != s.orientation) {
1756 disp.orientation = s.orientation;
1757 flags |= eDisplayTransactionNeeded;
1758 }
1759 if (disp.frame != s.frame) {
1760 disp.frame = s.frame;
1761 flags |= eDisplayTransactionNeeded;
1762 }
1763 if (disp.viewport != s.viewport) {
1764 disp.viewport = s.viewport;
1765 flags |= eDisplayTransactionNeeded;
1766 }
1767 }
1768 }
1769 return flags;
1770}
1771
1772uint32_t SurfaceFlinger::setClientStateLocked(
1773 const sp<Client>& client,
1774 const layer_state_t& s)
1775{
1776 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001777 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001778 if (layer != 0) {
1779 const uint32_t what = s.what;
1780 if (what & layer_state_t::ePositionChanged) {
1781 if (layer->setPosition(s.x, s.y))
1782 flags |= eTraversalNeeded;
1783 }
1784 if (what & layer_state_t::eLayerChanged) {
1785 // NOTE: index needs to be calculated before we update the state
1786 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1787 if (layer->setLayer(s.z)) {
1788 mCurrentState.layersSortedByZ.removeAt(idx);
1789 mCurrentState.layersSortedByZ.add(layer);
1790 // we need traversal (state changed)
1791 // AND transaction (list changed)
1792 flags |= eTransactionNeeded|eTraversalNeeded;
1793 }
1794 }
1795 if (what & layer_state_t::eSizeChanged) {
1796 if (layer->setSize(s.w, s.h)) {
1797 flags |= eTraversalNeeded;
1798 }
1799 }
1800 if (what & layer_state_t::eAlphaChanged) {
1801 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1802 flags |= eTraversalNeeded;
1803 }
1804 if (what & layer_state_t::eMatrixChanged) {
1805 if (layer->setMatrix(s.matrix))
1806 flags |= eTraversalNeeded;
1807 }
1808 if (what & layer_state_t::eTransparentRegionChanged) {
1809 if (layer->setTransparentRegionHint(s.transparentRegion))
1810 flags |= eTraversalNeeded;
1811 }
1812 if (what & layer_state_t::eVisibilityChanged) {
1813 if (layer->setFlags(s.flags, s.mask))
1814 flags |= eTraversalNeeded;
1815 }
1816 if (what & layer_state_t::eCropChanged) {
1817 if (layer->setCrop(s.crop))
1818 flags |= eTraversalNeeded;
1819 }
1820 if (what & layer_state_t::eLayerStackChanged) {
1821 // NOTE: index needs to be calculated before we update the state
1822 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1823 if (layer->setLayerStack(s.layerStack)) {
1824 mCurrentState.layersSortedByZ.removeAt(idx);
1825 mCurrentState.layersSortedByZ.add(layer);
1826 // we need traversal (state changed)
1827 // AND transaction (list changed)
1828 flags |= eTransactionNeeded|eTraversalNeeded;
1829 }
1830 }
1831 }
1832 return flags;
1833}
1834
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001835status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001836 const String8& name,
1837 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001838 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1839 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001840{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001841 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001842 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001843 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001844 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001845 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001846 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001847
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001848 status_t result = NO_ERROR;
1849
1850 sp<Layer> layer;
1851
Mathias Agopian3165cc22012-08-08 19:42:09 -07001852 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1853 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001854 result = createNormalLayer(client,
1855 name, w, h, flags, format,
1856 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001857 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001858 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001859 result = createDimLayer(client,
1860 name, w, h, flags,
1861 handle, gbp, &layer);
1862 break;
1863 default:
1864 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865 break;
1866 }
1867
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001868 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001869 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001870 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001872 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873}
1874
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001875status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1876 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1877 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878{
1879 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001880 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 case PIXEL_FORMAT_TRANSPARENT:
1882 case PIXEL_FORMAT_TRANSLUCENT:
1883 format = PIXEL_FORMAT_RGBA_8888;
1884 break;
1885 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001886#ifdef NO_RGBX_8888
1887 format = PIXEL_FORMAT_RGB_565;
1888#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001889 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001890#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891 break;
1892 }
1893
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001894#ifdef NO_RGBX_8888
1895 if (format == PIXEL_FORMAT_RGBX_8888)
1896 format = PIXEL_FORMAT_RGBA_8888;
1897#endif
1898
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001899 *outLayer = new Layer(this, client, name, w, h, flags);
1900 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1901 if (err == NO_ERROR) {
1902 *handle = (*outLayer)->getHandle();
1903 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001905
1906 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1907 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908}
1909
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001910status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1911 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1912 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914 *outLayer = new LayerDim(this, client, name, w, h, flags);
1915 *handle = (*outLayer)->getHandle();
1916 *gbp = (*outLayer)->getBufferQueue();
1917 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001918}
1919
Mathias Agopianac9fa422013-02-11 16:40:36 -08001920status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001921{
Mathias Agopian67106042013-03-14 19:18:13 -07001922 // called by the window manager when it wants to remove a Layer
1923 status_t err = NO_ERROR;
1924 sp<Layer> l(client->getLayerUser(handle));
1925 if (l != NULL) {
1926 err = removeLayer(l);
1927 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1928 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001929 }
1930 return err;
1931}
1932
Mathias Agopian13127d82013-03-05 17:47:11 -08001933status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001934{
Mathias Agopian67106042013-03-14 19:18:13 -07001935 // called by ~LayerCleaner() when all references to the IBinder (handle)
1936 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001937 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001938 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001939 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001940 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001941 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001942 "error removing layer=%p (%s)", l.get(), strerror(-err));
1943 }
1944 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945}
1946
Mathias Agopianb60314a2012-04-10 22:09:54 -07001947// ---------------------------------------------------------------------------
1948
Andy McFadden13a082e2012-08-24 10:16:42 -07001949void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001950 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001951 Vector<ComposerState> state;
1952 Vector<DisplayState> displays;
1953 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001954 d.what = DisplayState::eDisplayProjectionChanged |
1955 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001956 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001957 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001958 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001959 d.frame.makeInvalid();
1960 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001961 displays.add(d);
1962 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001963 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001964}
1965
1966void SurfaceFlinger::initializeDisplays() {
1967 class MessageScreenInitialized : public MessageBase {
1968 SurfaceFlinger* flinger;
1969 public:
1970 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1971 virtual bool handler() {
1972 flinger->onInitializeDisplays();
1973 return true;
1974 }
1975 };
1976 sp<MessageBase> msg = new MessageScreenInitialized(this);
1977 postMessageAsync(msg); // we may be called from main thread, use async message
1978}
1979
1980
Mathias Agopiancde87a32012-09-13 14:09:01 -07001981void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001982 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
1983 if (hw->isScreenAcquired()) {
1984 // this is expected, e.g. when power manager wakes up during boot
1985 ALOGD(" screen was previously acquired");
1986 return;
1987 }
1988
Mathias Agopian42977342012-08-05 00:40:46 -07001989 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001990 int32_t type = hw->getDisplayType();
1991 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
1992 // built-in display, tell the HWC
1993 getHwComposer().acquire(type);
1994
1995 if (type == DisplayDevice::DISPLAY_PRIMARY) {
1996 // FIXME: eventthread only knows about the main display right now
1997 mEventThread->onScreenAcquired();
1998 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07001999 }
Mathias Agopian20128302012-08-13 18:32:13 -07002000 mVisibleRegionsDirty = true;
2001 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002002}
2003
Mathias Agopiancde87a32012-09-13 14:09:01 -07002004void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002005 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2006 if (!hw->isScreenAcquired()) {
2007 ALOGD(" screen was previously released");
2008 return;
2009 }
2010
2011 hw->releaseScreen();
2012 int32_t type = hw->getDisplayType();
2013 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2014 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002015 // FIXME: eventthread only knows about the main display right now
2016 mEventThread->onScreenReleased();
2017 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002018
2019 // built-in display, tell the HWC
2020 getHwComposer().release(type);
2021 }
2022 mVisibleRegionsDirty = true;
2023 // from this point on, SF will stop drawing on this display
2024}
2025
2026void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2027 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002028 SurfaceFlinger& mFlinger;
2029 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002030 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002031 MessageScreenAcquired(SurfaceFlinger& flinger,
2032 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002033 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002034 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2035 if (hw == NULL) {
2036 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2037 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2038 ALOGW("Attempt to unblank virtual display");
2039 } else {
2040 mFlinger.onScreenAcquired(hw);
2041 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002042 return true;
2043 }
2044 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002045 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2046 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002047}
2048
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002049void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002050 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002051 SurfaceFlinger& mFlinger;
2052 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002053 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002054 MessageScreenReleased(SurfaceFlinger& flinger,
2055 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002056 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002057 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2058 if (hw == NULL) {
2059 ALOGE("Attempt to blank null display %p", mDisplay.get());
2060 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2061 ALOGW("Attempt to blank virtual display");
2062 } else {
2063 mFlinger.onScreenReleased(hw);
2064 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002065 return true;
2066 }
2067 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002068 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2069 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002070}
2071
2072// ---------------------------------------------------------------------------
2073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2075{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002076 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002077
Mathias Agopianbd115332013-04-18 16:41:04 -07002078 IPCThreadState* ipc = IPCThreadState::self();
2079 const int pid = ipc->getCallingPid();
2080 const int uid = ipc->getCallingUid();
2081 if ((uid != AID_SHELL) &&
2082 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002083 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002084 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002086 // Try to get the main lock, but don't insist if we can't
2087 // (this would indicate SF is stuck, but we want to be able to
2088 // print something in dumpsys).
2089 int retry = 3;
2090 while (mStateLock.tryLock()<0 && --retry>=0) {
2091 usleep(1000000);
2092 }
2093 const bool locked(retry >= 0);
2094 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002095 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002096 "SurfaceFlinger appears to be unresponsive, "
2097 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002098 }
2099
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002100 bool dumpAll = true;
2101 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002102 size_t numArgs = args.size();
2103 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002104 if ((index < numArgs) &&
2105 (args[index] == String16("--list"))) {
2106 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002107 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002108 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002109 }
2110
2111 if ((index < numArgs) &&
2112 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002113 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002114 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002115 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002116 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002117
2118 if ((index < numArgs) &&
2119 (args[index] == String16("--latency-clear"))) {
2120 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002121 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002122 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002123 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002124 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002125
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002126 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002127 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002128 }
2129
Mathias Agopian9795c422009-08-26 16:36:26 -07002130 if (locked) {
2131 mStateLock.unlock();
2132 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002133 }
2134 write(fd, result.string(), result.size());
2135 return NO_ERROR;
2136}
2137
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002138void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002139 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002140{
2141 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2142 const size_t count = currentLayers.size();
2143 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002144 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002145 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002146 }
2147}
2148
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002149void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002150 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002151{
2152 String8 name;
2153 if (index < args.size()) {
2154 name = String8(args[index]);
2155 index++;
2156 }
2157
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002158 const nsecs_t period =
2159 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2160 result.appendFormat("%lld\n", period);
2161
2162 if (name.isEmpty()) {
2163 mAnimFrameTracker.dump(result);
2164 } else {
2165 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2166 const size_t count = currentLayers.size();
2167 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002168 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002169 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002170 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002171 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002172 }
2173 }
2174}
2175
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002176void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002177 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002178{
2179 String8 name;
2180 if (index < args.size()) {
2181 name = String8(args[index]);
2182 index++;
2183 }
2184
2185 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2186 const size_t count = currentLayers.size();
2187 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002188 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002189 if (name.isEmpty() || (name == layer->getName())) {
2190 layer->clearStats();
2191 }
2192 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002193
2194 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002195}
2196
Andy McFadden4803b742012-09-24 19:07:20 -07002197/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2198{
2199 static const char* config =
2200 " [sf"
2201#ifdef NO_RGBX_8888
2202 " NO_RGBX_8888"
2203#endif
2204#ifdef HAS_CONTEXT_PRIORITY
2205 " HAS_CONTEXT_PRIORITY"
2206#endif
2207#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2208 " NEVER_DEFAULT_TO_ASYNC_MODE"
2209#endif
2210#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2211 " TARGET_DISABLE_TRIPLE_BUFFERING"
2212#endif
2213 "]";
2214 result.append(config);
2215}
2216
Mathias Agopian74d211a2013-04-22 16:55:35 +02002217void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2218 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002219{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002220 bool colorize = false;
2221 if (index < args.size()
2222 && (args[index] == String16("--color"))) {
2223 colorize = true;
2224 index++;
2225 }
2226
2227 Colorizer colorizer(colorize);
2228
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002229 // figure out if we're stuck somewhere
2230 const nsecs_t now = systemTime();
2231 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2232 const nsecs_t inTransaction(mDebugInTransaction);
2233 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2234 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2235
2236 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002237 * Dump library configuration.
2238 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002239
2240 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002241 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002242 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002243 appendSfConfigString(result);
2244 appendUiConfigString(result);
2245 appendGuiConfigString(result);
2246 result.append("\n");
2247
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002248 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002249 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002250 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002251 result.append(SyncFeatures::getInstance().toString());
2252 result.append("\n");
2253
Andy McFadden4803b742012-09-24 19:07:20 -07002254 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002255 * Dump the visible layer list
2256 */
2257 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2258 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002259 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002260 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002261 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002262 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002263 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002264 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002265 }
2266
2267 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002268 * Dump Display state
2269 */
2270
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002271 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002272 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002273 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002274 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2275 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002276 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002277 }
2278
2279 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002280 * Dump SurfaceFlinger global state
2281 */
2282
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002283 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002284 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002285 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002286
Mathias Agopian888c8222012-08-04 21:10:38 -07002287 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002288 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002289
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002290 colorizer.bold(result);
2291 result.appendFormat("EGL implementation : %s\n",
2292 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2293 colorizer.reset(result);
2294 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002295 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002296
Mathias Agopian875d8e12013-06-07 15:35:48 -07002297 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002298
Mathias Agopian42977342012-08-05 00:40:46 -07002299 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002300 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002301 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002302 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002303 " last eglSwapBuffers() time: %f us\n"
2304 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002305 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002306 " refresh-rate : %f fps\n"
2307 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002308 " y-dpi : %f\n"
2309 " EGL_NATIVE_VISUAL_ID : %d\n"
2310 " gpu_to_cpu_unsupported : %d\n"
2311 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002312 mLastSwapBufferTime/1000.0,
2313 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002314 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002315 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2316 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002317 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2318 mEGLNativeVisualId,
2319 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002320
Mathias Agopian74d211a2013-04-22 16:55:35 +02002321 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002322 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002323
Mathias Agopian74d211a2013-04-22 16:55:35 +02002324 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002325 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002326
2327 /*
2328 * VSYNC state
2329 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002330 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002331
2332 /*
2333 * Dump HWComposer state
2334 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002335 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002336 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002337 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002338 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002339 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2340 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002341 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002342
2343 /*
2344 * Dump gralloc state
2345 */
2346 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2347 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002348}
2349
Mathias Agopian13127d82013-03-05 17:47:11 -08002350const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002351SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002352 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002353 wp<IBinder> dpy;
2354 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2355 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2356 dpy = mDisplays.keyAt(i);
2357 break;
2358 }
2359 }
2360 if (dpy == NULL) {
2361 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2362 // Just use the primary display so we have something to return
2363 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2364 }
2365 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002366}
2367
Keun young Park63f165f2012-08-31 10:53:36 -07002368bool SurfaceFlinger::startDdmConnection()
2369{
2370 void* libddmconnection_dso =
2371 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2372 if (!libddmconnection_dso) {
2373 return false;
2374 }
2375 void (*DdmConnection_start)(const char* name);
2376 DdmConnection_start =
2377 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2378 if (!DdmConnection_start) {
2379 dlclose(libddmconnection_dso);
2380 return false;
2381 }
2382 (*DdmConnection_start)(getServiceName());
2383 return true;
2384}
2385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002386status_t SurfaceFlinger::onTransact(
2387 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2388{
2389 switch (code) {
2390 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002391 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002392 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002393 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002394 case BLANK:
2395 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002396 {
2397 // codes that require permission check
2398 IPCThreadState* ipc = IPCThreadState::self();
2399 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002400 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002401 if ((uid != AID_GRAPHICS) &&
2402 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002403 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002404 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2405 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002406 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002407 break;
2408 }
2409 case CAPTURE_SCREEN:
2410 {
2411 // codes that require permission check
2412 IPCThreadState* ipc = IPCThreadState::self();
2413 const int pid = ipc->getCallingPid();
2414 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002415 if ((uid != AID_GRAPHICS) &&
2416 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002417 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002418 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2419 return PERMISSION_DENIED;
2420 }
2421 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002422 }
2423 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002425 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2426 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002427 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002428 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002429 IPCThreadState* ipc = IPCThreadState::self();
2430 const int pid = ipc->getCallingPid();
2431 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002432 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002433 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002434 return PERMISSION_DENIED;
2435 }
2436 int n;
2437 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002438 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002439 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002440 return NO_ERROR;
2441 case 1002: // SHOW_UPDATES
2442 n = data.readInt32();
2443 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002444 invalidateHwcGeometry();
2445 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002446 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002448 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002449 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002450 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002451 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002452 setTransactionFlags(
2453 eTransactionNeeded|
2454 eDisplayTransactionNeeded|
2455 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002456 return NO_ERROR;
2457 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002458 case 1006:{ // send empty update
2459 signalRefresh();
2460 return NO_ERROR;
2461 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002462 case 1008: // toggle use of hw composer
2463 n = data.readInt32();
2464 mDebugDisableHWC = n ? 1 : 0;
2465 invalidateHwcGeometry();
2466 repaintEverything();
2467 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002468 case 1009: // toggle use of transform hint
2469 n = data.readInt32();
2470 mDebugDisableTransformHint = n ? 1 : 0;
2471 invalidateHwcGeometry();
2472 repaintEverything();
2473 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002474 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002475 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 reply->writeInt32(0);
2477 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002478 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002479 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 return NO_ERROR;
2481 case 1013: {
2482 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002483 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2484 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 }
2486 return NO_ERROR;
2487 }
2488 }
2489 return err;
2490}
2491
Mathias Agopian53331da2011-08-22 21:44:41 -07002492void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002493 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002494 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002495}
2496
Mathias Agopian59119e62010-10-11 12:37:43 -07002497// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002498// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002499// ---------------------------------------------------------------------------
2500
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002501status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2502 const sp<IGraphicBufferProducer>& producer,
2503 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002504 uint32_t minLayerZ, uint32_t maxLayerZ,
2505 bool isCpuConsumer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002506
2507 if (CC_UNLIKELY(display == 0))
2508 return BAD_VALUE;
2509
2510 if (CC_UNLIKELY(producer == 0))
2511 return BAD_VALUE;
2512
2513 class MessageCaptureScreen : public MessageBase {
2514 SurfaceFlinger* flinger;
2515 sp<IBinder> display;
2516 sp<IGraphicBufferProducer> producer;
2517 uint32_t reqWidth, reqHeight;
2518 uint32_t minLayerZ,maxLayerZ;
Mathias Agopianabe815d2013-03-19 22:22:21 -07002519 bool isCpuConsumer;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002520 status_t result;
2521 public:
2522 MessageCaptureScreen(SurfaceFlinger* flinger,
2523 const sp<IBinder>& display,
2524 const sp<IGraphicBufferProducer>& producer,
2525 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002526 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002527 : flinger(flinger), display(display), producer(producer),
2528 reqWidth(reqWidth), reqHeight(reqHeight),
2529 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopianabe815d2013-03-19 22:22:21 -07002530 isCpuConsumer(isCpuConsumer),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002531 result(PERMISSION_DENIED)
2532 {
2533 }
2534 status_t getResult() const {
2535 return result;
2536 }
2537 virtual bool handler() {
2538 Mutex::Autolock _l(flinger->mStateLock);
2539 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002540 bool useReadPixels = isCpuConsumer && !flinger->mGpuToCpuSupported;
2541 result = flinger->captureScreenImplLocked(hw,
2542 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2543 useReadPixels);
Mathias Agopianb4b17302013-03-20 18:36:41 -07002544
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002545 return true;
2546 }
2547 };
2548
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002549 // make sure to process transactions before screenshots -- a transaction
2550 // might already be pending but scheduled for VSYNC; this guarantees we
2551 // will handle it before the screenshot. When VSYNC finally arrives
2552 // the scheduled transaction will be a no-op. If no transactions are
2553 // scheduled at this time, this will end-up being a no-op as well.
2554 mEventQueue.invalidateTransactionNow();
2555
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002556 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002557 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2558 isCpuConsumer);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002559 status_t res = postMessageSync(msg);
2560 if (res == NO_ERROR) {
2561 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2562 }
2563 return res;
2564}
2565
Mathias Agopian180f10d2013-04-10 22:55:41 -07002566
2567void SurfaceFlinger::renderScreenImplLocked(
2568 const sp<const DisplayDevice>& hw,
2569 uint32_t reqWidth, uint32_t reqHeight,
2570 uint32_t minLayerZ, uint32_t maxLayerZ,
2571 bool yswap)
2572{
2573 ATRACE_CALL();
2574
2575 // get screen geometry
2576 const uint32_t hw_w = hw->getWidth();
2577 const uint32_t hw_h = hw->getHeight();
2578
2579 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2580
2581 // make sure to clear all GL error flags
2582 while ( glGetError() != GL_NO_ERROR ) ;
2583
2584 // set-up our viewport
2585 glViewport(0, 0, reqWidth, reqHeight);
2586 glMatrixMode(GL_PROJECTION);
2587 glLoadIdentity();
2588 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2589 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2590 glMatrixMode(GL_MODELVIEW);
2591 glLoadIdentity();
2592
2593 // redraw the screen entirely...
2594 glDisable(GL_SCISSOR_TEST);
2595 glClearColor(0,0,0,1);
2596 glClear(GL_COLOR_BUFFER_BIT);
2597 glDisable(GL_TEXTURE_EXTERNAL_OES);
2598 glDisable(GL_TEXTURE_2D);
2599
2600 const LayerVector& layers( mDrawingState.layersSortedByZ );
2601 const size_t count = layers.size();
2602 for (size_t i=0 ; i<count ; ++i) {
2603 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002604 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002605 if (state.layerStack == hw->getLayerStack()) {
2606 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2607 if (layer->isVisible()) {
2608 if (filtering) layer->setFiltering(true);
2609 layer->draw(hw);
2610 if (filtering) layer->setFiltering(false);
2611 }
2612 }
2613 }
2614 }
2615
2616 // compositionComplete is needed for older driver
2617 hw->compositionComplete();
2618}
2619
2620
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002621status_t SurfaceFlinger::captureScreenImplLocked(
2622 const sp<const DisplayDevice>& hw,
2623 const sp<IGraphicBufferProducer>& producer,
2624 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002625 uint32_t minLayerZ, uint32_t maxLayerZ,
2626 bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002627{
2628 ATRACE_CALL();
2629
Mathias Agopian180f10d2013-04-10 22:55:41 -07002630 // get screen geometry
2631 const uint32_t hw_w = hw->getWidth();
2632 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002633
Mathias Agopian180f10d2013-04-10 22:55:41 -07002634 // if we have secure windows on this display, never allow the screen capture
2635 if (hw->getSecureLayerVisible()) {
2636 ALOGW("FB is protected: PERMISSION_DENIED");
2637 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002638 }
2639
Mathias Agopian180f10d2013-04-10 22:55:41 -07002640 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2641 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2642 reqWidth, reqHeight, hw_w, hw_h);
2643 return BAD_VALUE;
2644 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002645
Mathias Agopian180f10d2013-04-10 22:55:41 -07002646 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2647 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2648
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002649 // create a surface (because we're a producer, and we need to
2650 // dequeue/queue a buffer)
2651 sp<Surface> sur = new Surface(producer);
2652 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002653
2654 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002655 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
2656 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
2657 if (!useReadPixels) {
2658 usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002659 }
2660
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002661 int err = 0;
2662 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2663 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2664 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002665
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002666 if (err == NO_ERROR) {
2667 ANativeWindowBuffer* buffer;
2668 /* TODO: Once we have the sync framework everywhere this can use
2669 * server-side waits on the fence that dequeueBuffer returns.
2670 */
2671 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2672 if (result == NO_ERROR) {
2673 // create an EGLImage from the buffer so we can later
2674 // turn it into a texture
2675 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2676 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2677 if (image != EGL_NO_IMAGE_KHR) {
2678 GLuint tname, name;
2679 if (!useReadPixels) {
2680 // turn our EGLImage into a texture
2681 glGenTextures(1, &tname);
2682 glBindTexture(GL_TEXTURE_2D, tname);
2683 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2684 // create a Framebuffer Object to render into
2685 glGenFramebuffersOES(1, &name);
2686 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2687 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
Mathias Agopian875d8e12013-06-07 15:35:48 -07002688 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002689 } else {
2690 // since we're going to use glReadPixels() anyways,
2691 // use an intermediate renderbuffer instead
2692 glGenRenderbuffersOES(1, &tname);
2693 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2694 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
2695 // create a FBO to render into
2696 glGenFramebuffersOES(1, &name);
2697 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2698 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2699 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2700 }
2701
2702 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2703 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2704 // this will in fact render into our dequeued buffer
2705 // via an FBO, which means we didn't have to create
2706 // an EGLSurface and therefore we're not
2707 // dependent on the context's EGLConfig.
2708 renderScreenImplLocked(hw, reqWidth, reqHeight,
2709 minLayerZ, maxLayerZ, true);
2710
2711 if (useReadPixels) {
2712 sp<GraphicBuffer> buf = static_cast<GraphicBuffer*>(buffer);
2713 void* vaddr;
2714 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
2715 glReadPixels(0, 0, buffer->stride, reqHeight,
2716 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
2717 buf->unlock();
2718 }
2719 }
2720 } else {
2721 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2722 result = INVALID_OPERATION;
2723 }
2724
2725 // back to main framebuffer
2726 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2727 glDeleteFramebuffersOES(1, &name);
2728 if (!useReadPixels) {
2729 glDeleteTextures(1, &tname);
2730 } else {
2731 glDeleteRenderbuffersOES(1, &tname);
2732 }
2733 // destroy our image
2734 eglDestroyImageKHR(mEGLDisplay, image);
2735 } else {
2736 result = BAD_VALUE;
2737 }
2738 window->queueBuffer(window, buffer, -1);
2739 }
2740 } else {
2741 result = BAD_VALUE;
2742 }
2743 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2744 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002745
Mathias Agopian875d8e12013-06-07 15:35:48 -07002746 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002747
Mathias Agopian74c40c02010-09-29 13:02:36 -07002748 return result;
2749}
2750
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002751// ---------------------------------------------------------------------------
2752
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002753SurfaceFlinger::LayerVector::LayerVector() {
2754}
2755
2756SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002757 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002758}
2759
2760int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2761 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002762{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002763 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002764 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2765 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002766
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002767 uint32_t ls = l->getCurrentState().layerStack;
2768 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002769 if (ls != rs)
2770 return ls - rs;
2771
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002772 uint32_t lz = l->getCurrentState().z;
2773 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002774 if (lz != rz)
2775 return lz - rz;
2776
2777 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002778}
2779
2780// ---------------------------------------------------------------------------
2781
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002782SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2783 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002784}
2785
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002786SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002787 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002788 viewport.makeInvalid();
2789 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002790}
2791
2792// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002793
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002794}; // namespace android