blob: 3733ede88f5e11d83b463de77dcb8420af5e0edf [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062#include "DispSync.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080063#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopiana4912602012-07-12 14:25:33 -070068#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070069#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070070#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopianff2ed702013-09-01 21:36:12 -070072#include "Effects/Daltonizer.h"
73
Mathias Agopian875d8e12013-06-07 15:35:48 -070074#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070075#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077#define DISPLAY_COUNT 1
78
Mathias Agopianfee2b462013-07-03 12:34:01 -070079/*
80 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
81 * black pixels.
82 */
83#define DEBUG_SCREENSHOTS false
84
Mathias Agopianca088332013-03-28 17:44:13 -070085EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070088
89// This works around the lack of support for the sync framework on some
90// devices.
91#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
92static const bool runningWithoutSyncFramework = true;
93#else
94static const bool runningWithoutSyncFramework = false;
95#endif
96
97// This is the phase offset in nanoseconds of the software vsync event
98// relative to the vsync event reported by HWComposer. The software vsync
99// event is when SurfaceFlinger and Choreographer-based applications run each
100// frame.
101//
102// This phase offset allows adjustment of the minimum latency from application
103// wake-up (by Choregographer) time to the time at which the resulting window
104// image is displayed. This value may be either positive (after the HW vsync)
105// or negative (before the HW vsync). Setting it to 0 will result in a
106// minimum latency of two vsync periods because the app and SurfaceFlinger
107// will run just after the HW vsync. Setting it to a positive number will
108// result in the minimum latency being:
109//
110// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
111//
112// Note that reducing this latency makes it more likely for the applications
113// to not have their window content image ready in time. When this happens
114// the latency will end up being an additional vsync period, and animations
115// will hiccup. Therefore, this latency should be tuned somewhat
116// conservatively (or at least with awareness of the trade-off being made).
117static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119// ---------------------------------------------------------------------------
120
Mathias Agopian99b49842011-06-27 16:05:52 -0700121const String16 sHardwareTest("android.permission.HARDWARE_TEST");
122const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
123const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
124const String16 sDump("android.permission.DUMP");
125
126// ---------------------------------------------------------------------------
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700129 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700131 mTransactionPending(false),
132 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700134 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700135 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700138 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800139 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700141 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700142 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700143 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700144 mDebugInSwapBuffers(0),
145 mLastSwapBufferTime(0),
146 mDebugInTransaction(0),
147 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700148 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700149 mPrimaryHWVsyncEnabled(false),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700150 mDaltonize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151{
Steve Blocka19954a2012-01-04 20:05:49 +0000152 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153
154 // debugging stuff...
155 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700156
Mathias Agopianb4b17302013-03-20 18:36:41 -0700157 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700158 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 property_get("debug.sf.showupdates", value, "0");
161 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700162
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163 property_get("debug.sf.ddms", value, "0");
164 mDebugDDMS = atoi(value);
165 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700166 if (!startDdmConnection()) {
167 // start failed, and DDMS debugging not enabled
168 mDebugDDMS = 0;
169 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700171 ALOGI_IF(mDebugRegion, "showupdates enabled");
172 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173}
174
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800175void SurfaceFlinger::onFirstRef()
176{
177 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800178}
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180SurfaceFlinger::~SurfaceFlinger()
181{
Mathias Agopiana4912602012-07-12 14:25:33 -0700182 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
183 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
184 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185}
186
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800187void SurfaceFlinger::binderDied(const wp<IBinder>& who)
188{
189 // the window manager died on us. prepare its eulogy.
190
Andy McFadden13a082e2012-08-24 10:16:42 -0700191 // restore initial conditions (default device unblank, etc)
192 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800193
194 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700195 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800196}
197
Mathias Agopian7e27f052010-05-28 14:22:23 -0700198sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199{
Mathias Agopian96f08192010-06-02 23:28:45 -0700200 sp<ISurfaceComposerClient> bclient;
201 sp<Client> client(new Client(this));
202 status_t err = client->initCheck();
203 if (err == NO_ERROR) {
204 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 return bclient;
207}
208
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700209sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
210 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700211{
212 class DisplayToken : public BBinder {
213 sp<SurfaceFlinger> flinger;
214 virtual ~DisplayToken() {
215 // no more references, this display must be terminated
216 Mutex::Autolock _l(flinger->mStateLock);
217 flinger->mCurrentState.displays.removeItem(this);
218 flinger->setTransactionFlags(eDisplayTransactionNeeded);
219 }
220 public:
221 DisplayToken(const sp<SurfaceFlinger>& flinger)
222 : flinger(flinger) {
223 }
224 };
225
226 sp<BBinder> token = new DisplayToken(this);
227
228 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700229 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700230 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700231 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700232 mCurrentState.displays.add(token, info);
233
234 return token;
235}
236
Jesse Hall6c913be2013-08-08 12:15:49 -0700237void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
238 Mutex::Autolock _l(mStateLock);
239
240 ssize_t idx = mCurrentState.displays.indexOfKey(display);
241 if (idx < 0) {
242 ALOGW("destroyDisplay: invalid display token");
243 return;
244 }
245
246 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
247 if (!info.isVirtualDisplay()) {
248 ALOGE("destroyDisplay called for non-virtual display");
249 return;
250 }
251
252 mCurrentState.displays.removeItemsAt(idx);
253 setTransactionFlags(eDisplayTransactionNeeded);
254}
255
Jesse Hall692c7232012-11-08 15:41:56 -0800256void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
257 ALOGW_IF(mBuiltinDisplays[type],
258 "Overwriting display token for display type %d", type);
259 mBuiltinDisplays[type] = new BBinder();
260 DisplayDeviceState info(type);
261 // All non-virtual displays are currently considered secure.
262 info.isSecure = true;
263 mCurrentState.displays.add(mBuiltinDisplays[type], info);
264}
265
Mathias Agopiane57f2922012-08-09 16:29:12 -0700266sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700267 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700268 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
269 return NULL;
270 }
Jesse Hall692c7232012-11-08 15:41:56 -0800271 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700272}
273
Jamie Gennis9a78c902011-01-12 18:30:40 -0800274sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
275{
276 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
277 return gba;
278}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280void SurfaceFlinger::bootFinished()
281{
282 const nsecs_t now = systemTime();
283 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000284 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700285 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700286
287 // wait patiently for the window manager death
288 const String16 name("window");
289 sp<IBinder> window(defaultServiceManager()->getService(name));
290 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700291 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700292 }
293
294 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700295 // formerly we would just kill the process, but we now ask it to exit so it
296 // can choose where to stop the animation.
297 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
Mathias Agopian3f844832013-08-07 21:24:32 -0700300void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700301 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700302 RenderEngine& engine;
303 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700304 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700305 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
306 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700307 }
308 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700309 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700310 return true;
311 }
312 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700314}
315
Mathias Agopian722b98f2012-09-25 18:24:31 -0700316status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700317 EGLDisplay dpy,
318 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700319 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700320 EGLConfig* outConfig)
321{
322 EGLConfig config = NULL;
323 EGLint numConfigs = -1, n=0;
324 eglGetConfigs(dpy, NULL, 0, &numConfigs);
325 EGLConfig* const configs = new EGLConfig[numConfigs];
326 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700327
Mathias Agopian722b98f2012-09-25 18:24:31 -0700328 if (n) {
329 if (attribute != EGL_NONE) {
330 for (int i=0 ; i<n ; i++) {
331 EGLint value = 0;
332 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
333 if (wanted == value) {
334 *outConfig = configs[i];
335 delete [] configs;
336 return NO_ERROR;
337 }
338 }
339 } else {
340 // just pick the first one
341 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700342 delete [] configs;
343 return NO_ERROR;
344 }
345 }
346 delete [] configs;
347 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348}
349
Mathias Agopian722b98f2012-09-25 18:24:31 -0700350class EGLAttributeVector {
351 struct Attribute;
352 class Adder;
353 friend class Adder;
354 KeyedVector<Attribute, EGLint> mList;
355 struct Attribute {
356 Attribute() {};
357 Attribute(EGLint v) : v(v) { }
358 EGLint v;
359 bool operator < (const Attribute& other) const {
360 // this places EGL_NONE at the end
361 EGLint lhs(v);
362 EGLint rhs(other.v);
363 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
364 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
365 return lhs < rhs;
366 }
367 };
368 class Adder {
369 friend class EGLAttributeVector;
370 EGLAttributeVector& v;
371 EGLint attribute;
372 Adder(EGLAttributeVector& v, EGLint attribute)
373 : v(v), attribute(attribute) {
374 }
375 public:
376 void operator = (EGLint value) {
377 if (attribute != EGL_NONE) {
378 v.mList.add(attribute, value);
379 }
380 }
381 operator EGLint () const { return v.mList[attribute]; }
382 };
383public:
384 EGLAttributeVector() {
385 mList.add(EGL_NONE, EGL_NONE);
386 }
387 void remove(EGLint attribute) {
388 if (attribute != EGL_NONE) {
389 mList.removeItem(attribute);
390 }
391 }
392 Adder operator [] (EGLint attribute) {
393 return Adder(*this, attribute);
394 }
395 EGLint operator [] (EGLint attribute) const {
396 return mList[attribute];
397 }
398 // cast-operator to (EGLint const*)
399 operator EGLint const* () const { return &mList.keyAt(0).v; }
400};
401
Jesse Hallb65f32e2013-09-27 22:10:47 -0700402status_t SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
403 EGLint renderableType, EGLConfig* config) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700404 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
405 // it is to be used with WIFI displays
Mathias Agopiana4912602012-07-12 14:25:33 -0700406 status_t err;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700407 EGLint wantedAttribute;
408 EGLint wantedAttributeValue;
Mathias Agopianda27af92012-09-13 18:17:13 -0700409
Mathias Agopian722b98f2012-09-25 18:24:31 -0700410 EGLAttributeVector attribs;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700411 if (renderableType) {
412 attribs[EGL_RENDERABLE_TYPE] = renderableType;
413 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
414 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
415 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
416 attribs[EGL_RED_SIZE] = 8;
417 attribs[EGL_GREEN_SIZE] = 8;
418 attribs[EGL_BLUE_SIZE] = 8;
419 wantedAttribute = EGL_NONE;
420 wantedAttributeValue = EGL_NONE;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700421
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700422 } else {
423 // if no renderable type specified, fallback to a simplified query
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700424 wantedAttribute = EGL_NATIVE_VISUAL_ID;
425 wantedAttributeValue = nativeVisualId;
426 }
427
428 err = selectConfigForAttribute(display, attribs, wantedAttribute,
Jesse Hallb65f32e2013-09-27 22:10:47 -0700429 wantedAttributeValue, config);
430 if (err == NO_ERROR) {
431 EGLint caveat;
432 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
433 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
434 }
435 return err;
Mathias Agopiana4912602012-07-12 14:25:33 -0700436}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700438class DispSyncSource : public VSyncSource, private DispSync::Callback {
439public:
440 DispSyncSource(DispSync* dispSync) : mValue(0), mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700441
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700442 virtual ~DispSyncSource() {}
443
444 virtual void setVSyncEnabled(bool enable) {
445 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
446 // with locking it in the onDispSyncEvent callback.
447 if (enable) {
448 status_t err = mDispSync->addEventListener(vsyncPhaseOffsetNs,
449 static_cast<DispSync::Callback*>(this));
450 if (err != NO_ERROR) {
451 ALOGE("error registering vsync callback: %s (%d)",
452 strerror(-err), err);
453 }
454 ATRACE_INT("VsyncOn", 1);
455 } else {
456 status_t err = mDispSync->removeEventListener(
457 static_cast<DispSync::Callback*>(this));
458 if (err != NO_ERROR) {
459 ALOGE("error unregistering vsync callback: %s (%d)",
460 strerror(-err), err);
461 }
462 ATRACE_INT("VsyncOn", 0);
463 }
464 }
465
466 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
467 Mutex::Autolock lock(mMutex);
468 mCallback = callback;
469 }
470
471private:
472 virtual void onDispSyncEvent(nsecs_t when) {
473 sp<VSyncSource::Callback> callback;
474 {
475 Mutex::Autolock lock(mMutex);
476 callback = mCallback;
477
478 mValue = (mValue + 1) % 2;
479 ATRACE_INT("VSYNC", mValue);
480 }
481
482 if (callback != NULL) {
483 callback->onVSyncEvent(when);
484 }
485 }
486
487 int mValue;
488
489 DispSync* mDispSync;
490 sp<VSyncSource::Callback> mCallback;
491 Mutex mMutex;
492};
493
494void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700495 ALOGI( "SurfaceFlinger's main thread ready to run. "
496 "Initializing graphics H/W...");
497
Jesse Hallb65f32e2013-09-27 22:10:47 -0700498 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800499 Mutex::Autolock _l(mStateLock);
500
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700501 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700502 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
503 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700504
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700505 // Initialize the H/W composer object. There may or may not be an
506 // actual hardware composer underneath.
507 mHwc = new HWComposer(this,
508 *static_cast<HWComposer::EventHandler *>(this));
509
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700510 // First try to get an ES2 config
Jesse Hallb65f32e2013-09-27 22:10:47 -0700511 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT,
512 &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700513
Jesse Hallb65f32e2013-09-27 22:10:47 -0700514 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700515 // If ES2 fails, try ES1
Jesse Hallb65f32e2013-09-27 22:10:47 -0700516 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(),
517 EGL_OPENGL_ES_BIT, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700518 }
519
Jesse Hallb65f32e2013-09-27 22:10:47 -0700520 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700521 // still didn't work, probably because we're on the emulator...
522 // try a simplified query
523 ALOGW("no suitable EGLConfig found, trying a simpler query");
Jesse Hallb65f32e2013-09-27 22:10:47 -0700524 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700525 }
526
Jesse Hallb65f32e2013-09-27 22:10:47 -0700527 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700528 // this EGL is too lame for android
529 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
530 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700531
532 // print some debugging info
533 EGLint r,g,b,a;
534 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
535 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
536 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
537 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
538 ALOGI("EGL informations:");
539 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
540 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
541 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
542 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
543 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
544
545 // get a RenderEngine for the given display / config (can't fail)
546 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
547
548 // retrieve the EGL context that was selected/created
549 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700550
Mathias Agopianed985572013-03-22 00:24:39 -0700551 // figure out which format we got
552 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
553 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
554
Mathias Agopianda27af92012-09-13 18:17:13 -0700555 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
556 "couldn't create EGLContext");
557
Mathias Agopiancde87a32012-09-13 14:09:01 -0700558 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700559 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700560 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700561 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700562 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700563 // All non-virtual displays are currently considered secure.
564 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800565 createBuiltinDisplayLocked(type);
566 wp<IBinder> token = mBuiltinDisplays[i];
567
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700568 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
569 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700570 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700571 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700572 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700573 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700574 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700575 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700576 // for displays other than the main display, so we always
577 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700578 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700579 hw->acquireScreen();
580 }
581 mDisplays.add(token, hw);
582 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700583 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700584
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700585 // make the GLContext current so that we can create textures when creating Layers
586 // (which may happens before we render something)
587 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
588
Mathias Agopian028508c2012-07-25 21:12:12 -0700589 // start the EventThread
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700590 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync);
591 mEventThread = new EventThread(vsyncSrc);
Mathias Agopian028508c2012-07-25 21:12:12 -0700592 mEventQueue.setEventThread(mEventThread);
593
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700594 // set a fake vsync period if there is no HWComposer
595 if (mHwc->initCheck() != NO_ERROR) {
596 mPrimaryDispSync.setPeriod(16666667);
597 }
598
Mathias Agopian92a979a2012-08-02 18:32:23 -0700599 // initialize our drawing state
600 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700601
Andy McFadden13a082e2012-08-24 10:16:42 -0700602 // set initial conditions (e.g. unblank default device)
603 initializeDisplays();
604
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700605 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700606 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607}
608
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700609int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700610 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700611 type : mHwc->allocateDisplayId();
612}
613
Mathias Agopiana67e4182012-06-19 17:26:12 -0700614void SurfaceFlinger::startBootAnim() {
615 // start boot animation
616 property_set("service.bootanim.exit", "0");
617 property_set("ctl.start", "bootanim");
618}
619
Mathias Agopian875d8e12013-06-07 15:35:48 -0700620size_t SurfaceFlinger::getMaxTextureSize() const {
621 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700622}
623
Mathias Agopian875d8e12013-06-07 15:35:48 -0700624size_t SurfaceFlinger::getMaxViewportDims() const {
625 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700626}
627
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800629
Jamie Gennis582270d2011-08-17 18:19:00 -0700630bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800631 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800632 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800633 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700634 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800635}
636
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700637status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800638 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700639 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800640 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700641 type = i;
642 break;
643 }
644 }
645
646 if (type < 0) {
647 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700648 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700649
650 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700651 float xdpi = hwc.getDpiX(type);
652 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700653
654 // TODO: Not sure if display density should handled by SF any longer
655 class Density {
656 static int getDensityFromProperty(char const* propName) {
657 char property[PROPERTY_VALUE_MAX];
658 int density = 0;
659 if (property_get(propName, property, NULL) > 0) {
660 density = atoi(property);
661 }
662 return density;
663 }
664 public:
665 static int getEmuDensity() {
666 return getDensityFromProperty("qemu.sf.lcd_density"); }
667 static int getBuildDensity() {
668 return getDensityFromProperty("ro.sf.lcd_density"); }
669 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700670
671 if (type == DisplayDevice::DISPLAY_PRIMARY) {
672 // The density of the device is provided by a build property
673 float density = Density::getBuildDensity() / 160.0f;
674 if (density == 0) {
675 // the build doesn't provide a density -- this is wrong!
676 // use xdpi instead
677 ALOGE("ro.sf.lcd_density must be defined as a build property");
678 density = xdpi / 160.0f;
679 }
680 if (Density::getEmuDensity()) {
681 // if "qemu.sf.lcd_density" is specified, it overrides everything
682 xdpi = ydpi = density = Density::getEmuDensity();
683 density /= 160.0f;
684 }
685 info->density = density;
686
687 // TODO: this needs to go away (currently needed only by webkit)
688 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
689 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700690 } else {
691 // TODO: where should this value come from?
692 static const int TV_DENSITY = 213;
693 info->density = TV_DENSITY / 160.0f;
694 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700695 }
696
Mathias Agopian1604f772012-09-18 21:54:42 -0700697 info->w = hwc.getWidth(type);
698 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700699 info->xdpi = xdpi;
700 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700701 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700702
703 // All non-virtual displays are currently considered secure.
704 info->secure = true;
705
Mathias Agopian888c8222012-08-04 21:10:38 -0700706 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700707}
708
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800709// ----------------------------------------------------------------------------
710
711sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800712 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700713}
714
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800716
717void SurfaceFlinger::waitForEvent() {
718 mEventQueue.waitMessage();
719}
720
721void SurfaceFlinger::signalTransaction() {
722 mEventQueue.invalidate();
723}
724
725void SurfaceFlinger::signalLayerUpdate() {
726 mEventQueue.invalidate();
727}
728
729void SurfaceFlinger::signalRefresh() {
730 mEventQueue.refresh();
731}
732
733status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
734 nsecs_t reltime, uint32_t flags) {
735 return mEventQueue.postMessage(msg, reltime);
736}
737
738status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
739 nsecs_t reltime, uint32_t flags) {
740 status_t res = mEventQueue.postMessage(msg, reltime);
741 if (res == NO_ERROR) {
742 msg->wait();
743 }
744 return res;
745}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700747void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700748 do {
749 waitForEvent();
750 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751}
752
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700753void SurfaceFlinger::enableHardwareVsync() {
754 Mutex::Autolock _l(mHWVsyncLock);
755 if (!mPrimaryHWVsyncEnabled) {
756 mPrimaryDispSync.beginResync();
757 eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
758 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700759 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700760}
761
762void SurfaceFlinger::resyncToHardwareVsync() {
763 Mutex::Autolock _l(mHWVsyncLock);
764
765 const nsecs_t period =
766 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
767
768 mPrimaryDispSync.reset();
769 mPrimaryDispSync.setPeriod(period);
770
771 if (!mPrimaryHWVsyncEnabled) {
772 mPrimaryDispSync.beginResync();
773 eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
774 mPrimaryHWVsyncEnabled = true;
775 }
776}
777
778void SurfaceFlinger::disableHardwareVsync() {
779 Mutex::Autolock _l(mHWVsyncLock);
780 if (mPrimaryHWVsyncEnabled) {
781 eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
782 mPrimaryDispSync.endResync();
783 mPrimaryHWVsyncEnabled = false;
784 }
785}
786
787void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
788 if (type == 0) {
789 bool needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
790
791 if (needsHwVsync) {
792 enableHardwareVsync();
793 } else {
794 disableHardwareVsync();
795 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700796 }
797}
798
799void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
800 if (mEventThread == NULL) {
801 // This is a temporary workaround for b/7145521. A non-null pointer
802 // does not mean EventThread has finished initializing, so this
803 // is not a correct fix.
804 ALOGW("WARNING: EventThread not started, ignoring hotplug");
805 return;
806 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700807
Jesse Hall9e663de2013-08-16 14:28:37 -0700808 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700809 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800810 if (connected) {
811 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700812 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800813 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
814 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700815 }
816 setTransactionFlags(eDisplayTransactionNeeded);
817
Andy McFadden9e9689c2012-10-10 18:17:51 -0700818 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700819 }
Mathias Agopian86303202012-07-24 22:46:10 -0700820}
821
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700822void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700823 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700824 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700825}
826
Mathias Agopian4fec8732012-06-29 14:12:52 -0700827void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800828 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800829 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700830 case MessageQueue::TRANSACTION:
831 handleMessageTransaction();
832 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700833 case MessageQueue::INVALIDATE:
834 handleMessageTransaction();
835 handleMessageInvalidate();
836 signalRefresh();
837 break;
838 case MessageQueue::REFRESH:
839 handleMessageRefresh();
840 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800841 }
842}
843
Mathias Agopian4fec8732012-06-29 14:12:52 -0700844void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700845 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700846 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700847 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700848 }
849}
850
851void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700852 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700853 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700854}
855
856void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700857 ATRACE_CALL();
858 preComposition();
859 rebuildLayerStacks();
860 setUpHWComposer();
861 doDebugFlashRegions();
862 doComposition();
863 postComposition();
864}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700865
Mathias Agopiancd60f992012-08-16 16:28:27 -0700866void SurfaceFlinger::doDebugFlashRegions()
867{
868 // is debugging enabled
869 if (CC_LIKELY(!mDebugRegion))
870 return;
871
872 const bool repaintEverything = mRepaintEverything;
873 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
874 const sp<DisplayDevice>& hw(mDisplays[dpy]);
875 if (hw->canDraw()) {
876 // transform the dirty region into this screen's coordinate space
877 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
878 if (!dirtyRegion.isEmpty()) {
879 // redraw the whole screen
880 doComposeSurfaces(hw, Region(hw->bounds()));
881
882 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700884 RenderEngine& engine(getRenderEngine());
885 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
886
Mathias Agopiancd60f992012-08-16 16:28:27 -0700887 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700888 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700889 }
890 }
891 }
892
893 postFramebuffer();
894
895 if (mDebugRegion > 1) {
896 usleep(mDebugRegion * 1000);
897 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700898
899 HWComposer& hwc(getHwComposer());
900 if (hwc.initCheck() == NO_ERROR) {
901 status_t err = hwc.prepare();
902 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
903 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700904}
905
906void SurfaceFlinger::preComposition()
907{
908 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700909 const LayerVector& layers(mDrawingState.layersSortedByZ);
910 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700912 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913 needExtraInvalidate = true;
914 }
915 }
916 if (needExtraInvalidate) {
917 signalLayerUpdate();
918 }
919}
920
921void SurfaceFlinger::postComposition()
922{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700923 const LayerVector& layers(mDrawingState.layersSortedByZ);
924 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700925 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700926 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700927 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800928
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700929 const HWComposer& hwc = getHwComposer();
930 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
931
932 if (presentFence->isValid()) {
933 if (mPrimaryDispSync.addPresentFence(presentFence)) {
934 enableHardwareVsync();
935 } else {
936 disableHardwareVsync();
937 }
938 }
939
940 if (runningWithoutSyncFramework) {
941 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
942 if (hw->isScreenAcquired()) {
943 enableHardwareVsync();
944 }
945 }
946
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800947 if (mAnimCompositionPending) {
948 mAnimCompositionPending = false;
949
Jesse Halla9a1b002013-02-27 16:39:25 -0800950 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800951 mAnimFrameTracker.setActualPresentFence(presentFence);
952 } else {
953 // The HWC doesn't support present fences, so use the refresh
954 // timestamp instead.
955 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
956 mAnimFrameTracker.setActualPresentTime(presentTime);
957 }
958 mAnimFrameTracker.advanceFrame();
959 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960}
961
962void SurfaceFlinger::rebuildLayerStacks() {
963 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700965 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700966 mVisibleRegionsDirty = false;
967 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700968
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700969 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700970 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700971 Region opaqueRegion;
972 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800973 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700974 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700975 const Transform& tr(hw->getTransform());
976 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700977 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700978 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700979 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700980
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700981 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700982 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700983 const sp<Layer>& layer(layers[i]);
984 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700985 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700986 Region drawRegion(tr.transform(
987 layer->visibleNonTransparentRegion));
988 drawRegion.andSelf(bounds);
989 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700990 layersSortedByZ.add(layer);
991 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700992 }
993 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700994 }
Mathias Agopian42977342012-08-05 00:40:46 -0700995 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700996 hw->undefinedRegion.set(bounds);
997 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
998 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700999 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001000 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001001}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001002
Mathias Agopiancd60f992012-08-16 16:28:27 -07001003void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001004 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1005 mDisplays[dpy]->beginFrame();
1006 }
1007
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001008 HWComposer& hwc(getHwComposer());
1009 if (hwc.initCheck() == NO_ERROR) {
1010 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001011 if (CC_UNLIKELY(mHwWorkListDirty)) {
1012 mHwWorkListDirty = false;
1013 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1014 sp<const DisplayDevice> hw(mDisplays[dpy]);
1015 const int32_t id = hw->getHwcDisplayId();
1016 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001017 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001018 hw->getVisibleLayersSortedByZ());
1019 const size_t count = currentLayers.size();
1020 if (hwc.createWorkList(id, count) == NO_ERROR) {
1021 HWComposer::LayerListIterator cur = hwc.begin(id);
1022 const HWComposer::LayerListIterator end = hwc.end(id);
1023 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001024 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001025 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001026 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001027 cur->setSkip(true);
1028 }
1029 }
1030 }
1031 }
1032 }
1033 }
1034
1035 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001036 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001037 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001038 const int32_t id = hw->getHwcDisplayId();
1039 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001041 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001042 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001043 HWComposer::LayerListIterator cur = hwc.begin(id);
1044 const HWComposer::LayerListIterator end = hwc.end(id);
1045 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1046 /*
1047 * update the per-frame h/w composer data for each layer
1048 * and build the transparent region of the FB
1049 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001051 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001052 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001053 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001054 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001055
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001056 status_t err = hwc.prepare();
1057 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001058
1059 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1060 sp<const DisplayDevice> hw(mDisplays[dpy]);
1061 hw->prepareFrame(hwc);
1062 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001063 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001064}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001065
Mathias Agopiancd60f992012-08-16 16:28:27 -07001066void SurfaceFlinger::doComposition() {
1067 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001068 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001069 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001070 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -07001071 if (hw->canDraw()) {
1072 // transform the dirty region into this screen's coordinate space
1073 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001074
1075 // repaint the framebuffer (if needed)
1076 doDisplayComposition(hw, dirtyRegion);
1077
Mathias Agopiancd60f992012-08-16 16:28:27 -07001078 hw->dirtyRegion.clear();
1079 hw->flip(hw->swapRegion);
1080 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001081 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001082 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001083 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001084 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001085 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001086}
1087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088void SurfaceFlinger::postFramebuffer()
1089{
Mathias Agopian841cde52012-03-01 15:44:37 -08001090 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001091
Mathias Agopiana44b0412011-10-16 18:46:35 -07001092 const nsecs_t now = systemTime();
1093 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001094
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001095 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001096 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001097 if (!hwc.supportsFramebufferTarget()) {
1098 // EGL spec says:
1099 // "surface must be bound to the calling thread's current context,
1100 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001101 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001102 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001103 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001104 }
1105
Mathias Agopian6da15f42013-09-25 20:40:07 -07001106 // make the default display current because the VirtualDisplayDevice code cannot
1107 // deal with dequeueBuffer() being called outside of the composition loop; however
1108 // the code below can call glFlush() which is allowed (and does in some case) call
1109 // dequeueBuffer().
1110 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1111
Mathias Agopian92a979a2012-08-02 18:32:23 -07001112 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001113 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001114 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001115 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001116 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001117 int32_t id = hw->getHwcDisplayId();
1118 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001119 HWComposer::LayerListIterator cur = hwc.begin(id);
1120 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001121 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001122 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001123 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001124 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001125 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001126 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001127 }
Jesse Hallef194142012-06-14 14:45:17 -07001128 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001129 }
1130
Mathias Agopiana44b0412011-10-16 18:46:35 -07001131 mLastSwapBufferTime = systemTime() - now;
1132 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001133
1134 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1135 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1136 logFrameStats();
1137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138}
1139
Mathias Agopian87baae12012-07-31 12:38:26 -07001140void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141{
Mathias Agopian841cde52012-03-01 15:44:37 -08001142 ATRACE_CALL();
1143
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001144 // here we keep a copy of the drawing state (that is the state that's
1145 // going to be overwritten by handleTransactionLocked()) outside of
1146 // mStateLock so that the side-effects of the State assignment
1147 // don't happen with mStateLock held (which can cause deadlocks).
1148 State drawingState(mDrawingState);
1149
Mathias Agopianca4d3602011-05-19 15:38:14 -07001150 Mutex::Autolock _l(mStateLock);
1151 const nsecs_t now = systemTime();
1152 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153
Mathias Agopianca4d3602011-05-19 15:38:14 -07001154 // Here we're guaranteed that some transaction flags are set
1155 // so we can call handleTransactionLocked() unconditionally.
1156 // We call getTransactionFlags(), which will also clear the flags,
1157 // with mStateLock held to guarantee that mCurrentState won't change
1158 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001159
Mathias Agopiane57f2922012-08-09 16:29:12 -07001160 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001161 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001162
Mathias Agopianca4d3602011-05-19 15:38:14 -07001163 mLastTransactionTime = systemTime() - now;
1164 mDebugInTransaction = 0;
1165 invalidateHwcGeometry();
1166 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001167}
1168
Mathias Agopian87baae12012-07-31 12:38:26 -07001169void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001170{
1171 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172 const size_t count = currentLayers.size();
1173
1174 /*
1175 * Traversal of the children
1176 * (perform the transaction for each of them if needed)
1177 */
1178
Mathias Agopian3559b072012-08-15 13:46:03 -07001179 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001181 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1183 if (!trFlags) continue;
1184
1185 const uint32_t flags = layer->doTransaction(0);
1186 if (flags & Layer::eVisibleRegion)
1187 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 }
1189 }
1190
1191 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001192 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 */
1194
Mathias Agopiane57f2922012-08-09 16:29:12 -07001195 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001196 // here we take advantage of Vector's copy-on-write semantics to
1197 // improve performance by skipping the transaction entirely when
1198 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001199 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1200 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001201 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001203 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001204 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001205
1206 // find the displays that were removed
1207 // (ie: in drawing state but not in current state)
1208 // also handle displays that changed
1209 // (ie: displays that are in both lists)
1210 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001211 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1212 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001213 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001214 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001215 // Call makeCurrent() on the primary display so we can
1216 // be sure that nothing associated with this display
1217 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001218 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001219 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001220 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1221 if (hw != NULL)
1222 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001223 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001224 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001225 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001226 } else {
1227 ALOGW("trying to remove the main display");
1228 }
1229 } else {
1230 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001231 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001232 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001233 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001234 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001235 // recreating the DisplayDevice, so we just remove it
1236 // from the drawing state, so that it get re-added
1237 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001238 sp<DisplayDevice> hw(getDisplayDevice(display));
1239 if (hw != NULL)
1240 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001241 mDisplays.removeItem(display);
1242 mDrawingState.displays.removeItemsAt(i);
1243 dc--; i--;
1244 // at this point we must loop to the next item
1245 continue;
1246 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001247
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001248 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001249 if (disp != NULL) {
1250 if (state.layerStack != draw[i].layerStack) {
1251 disp->setLayerStack(state.layerStack);
1252 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001253 if ((state.orientation != draw[i].orientation)
1254 || (state.viewport != draw[i].viewport)
1255 || (state.frame != draw[i].frame))
1256 {
1257 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001258 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001259 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001260 }
1261 }
1262 }
1263
1264 // find displays that were added
1265 // (ie: in current state but not in drawing state)
1266 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001267 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001268 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001269
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001270 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001271 sp<IGraphicBufferProducer> producer;
1272 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1273
Jesse Hall02d86562013-03-25 14:43:23 -07001274 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001275 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001276 // Virtual displays without a surface are dormant:
1277 // they have external state (layer stack, projection,
1278 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001279 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001280
Jesse Hall02d86562013-03-25 14:43:23 -07001281 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001282 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1283 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001284 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001285
1286 dispSurface = vds;
1287 if (hwcDisplayId >= 0) {
1288 producer = vds;
1289 } else {
1290 // There won't be any interaction with HWC for this virtual display,
1291 // so the GLES driver can pass buffers directly to the sink.
1292 producer = state.surface;
1293 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001294 }
1295 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001296 ALOGE_IF(state.surface!=NULL,
1297 "adding a supported display, but rendering "
1298 "surface is provided (%p), ignoring it",
1299 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001300 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001301 // for supported (by hwc) displays we provide our
1302 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001303 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1304 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001305 }
1306
1307 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001308 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001309 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001310 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001311 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001312 hw->setLayerStack(state.layerStack);
1313 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001314 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001315 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001316 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001317 if (state.isVirtualDisplay()) {
1318 if (hwcDisplayId >= 0) {
1319 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1320 hw->getWidth(), hw->getHeight(),
1321 hw->getFormat());
1322 }
1323 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001324 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001325 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001326 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001327 }
1328 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001330 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331
Mathias Agopian84300952012-11-21 16:02:13 -08001332 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1333 // The transform hint might have changed for some layers
1334 // (either because a display has changed, or because a layer
1335 // as changed).
1336 //
1337 // Walk through all the layers in currentLayers,
1338 // and update their transform hint.
1339 //
1340 // If a layer is visible only on a single display, then that
1341 // display is used to calculate the hint, otherwise we use the
1342 // default display.
1343 //
1344 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1345 // the hint is set before we acquire a buffer from the surface texture.
1346 //
1347 // NOTE: layer transactions have taken place already, so we use their
1348 // drawing state. However, SurfaceFlinger's own transaction has not
1349 // happened yet, so we must use the current state layer list
1350 // (soon to become the drawing state list).
1351 //
1352 sp<const DisplayDevice> disp;
1353 uint32_t currentlayerStack = 0;
1354 for (size_t i=0; i<count; i++) {
1355 // NOTE: we rely on the fact that layers are sorted by
1356 // layerStack first (so we don't have to traverse the list
1357 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001358 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001359 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001360 if (i==0 || currentlayerStack != layerStack) {
1361 currentlayerStack = layerStack;
1362 // figure out if this layerstack is mirrored
1363 // (more than one display) if so, pick the default display,
1364 // if not, pick the only display it's on.
1365 disp.clear();
1366 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1367 sp<const DisplayDevice> hw(mDisplays[dpy]);
1368 if (hw->getLayerStack() == currentlayerStack) {
1369 if (disp == NULL) {
1370 disp = hw;
1371 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001372 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001373 break;
1374 }
1375 }
1376 }
1377 }
Chet Haase91d25932013-04-11 15:24:55 -07001378 if (disp == NULL) {
1379 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1380 // redraw after transform hint changes. See bug 8508397.
1381
1382 // could be null when this layer is using a layerStack
1383 // that is not visible on any display. Also can occur at
1384 // screen off/on times.
1385 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001386 }
Chet Haase91d25932013-04-11 15:24:55 -07001387 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001388 }
1389 }
1390
1391
Mathias Agopian3559b072012-08-15 13:46:03 -07001392 /*
1393 * Perform our own transaction if needed
1394 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001395
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001396 const LayerVector& layers(mDrawingState.layersSortedByZ);
1397 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001398 // layers have been added
1399 mVisibleRegionsDirty = true;
1400 }
1401
1402 // some layers might have been removed, so
1403 // we need to update the regions they're exposing.
1404 if (mLayersRemoved) {
1405 mLayersRemoved = false;
1406 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001407 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001408 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001409 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001410 if (currentLayers.indexOf(layer) < 0) {
1411 // this layer is not visible anymore
1412 // TODO: we could traverse the tree from front to back and
1413 // compute the actual visible region
1414 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001415 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001416 Region visibleReg = s.transform.transform(
1417 Region(Rect(s.active.w, s.active.h)));
1418 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001419 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 }
1422
1423 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001424}
1425
1426void SurfaceFlinger::commitTransaction()
1427{
1428 if (!mLayersPendingRemoval.isEmpty()) {
1429 // Notify removed layers now that they can't be drawn from
1430 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1431 mLayersPendingRemoval[i]->onRemoved();
1432 }
1433 mLayersPendingRemoval.clear();
1434 }
1435
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001436 // If this transaction is part of a window animation then the next frame
1437 // we composite should be considered an animation as well.
1438 mAnimCompositionPending = mAnimTransactionPending;
1439
Mathias Agopian4fec8732012-06-29 14:12:52 -07001440 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001441 mTransactionPending = false;
1442 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001443 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444}
1445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001447 const LayerVector& currentLayers, uint32_t layerStack,
1448 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449{
Mathias Agopian841cde52012-03-01 15:44:37 -08001450 ATRACE_CALL();
1451
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 Region aboveOpaqueLayers;
1453 Region aboveCoveredLayers;
1454 Region dirty;
1455
Mathias Agopian87baae12012-07-31 12:38:26 -07001456 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457
1458 size_t i = currentLayers.size();
1459 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001460 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461
1462 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001463 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464
Jesse Hall01e29052013-02-19 16:13:35 -08001465 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001466 if (s.layerStack != layerStack)
1467 continue;
1468
Mathias Agopianab028732010-03-16 16:41:46 -07001469 /*
1470 * opaqueRegion: area of a surface that is fully opaque.
1471 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001473
1474 /*
1475 * visibleRegion: area of a surface that is visible on screen
1476 * and not fully transparent. This is essentially the layer's
1477 * footprint minus the opaque regions above it.
1478 * Areas covered by a translucent surface are considered visible.
1479 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001481
1482 /*
1483 * coveredRegion: area of a surface that is covered by all
1484 * visible regions above it (which includes the translucent areas).
1485 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001487
Jesse Halla8026d22012-09-25 13:25:04 -07001488 /*
1489 * transparentRegion: area of a surface that is hinted to be completely
1490 * transparent. This is only used to tell when the layer has no visible
1491 * non-transparent regions and can be removed from the layer list. It
1492 * does not affect the visibleRegion of this layer or any layers
1493 * beneath it. The hint may not be correct if apps don't respect the
1494 * SurfaceView restrictions (which, sadly, some don't).
1495 */
1496 Region transparentRegion;
1497
Mathias Agopianab028732010-03-16 16:41:46 -07001498
1499 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001500 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001501 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001502 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001504 if (!visibleRegion.isEmpty()) {
1505 // Remove the transparent area from the visible region
1506 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001507 const Transform tr(s.transform);
1508 if (tr.transformed()) {
1509 if (tr.preserveRects()) {
1510 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001511 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001512 } else {
1513 // transformation too complex, can't do the
1514 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001515 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001516 }
1517 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001518 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001519 }
Mathias Agopianab028732010-03-16 16:41:46 -07001520 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521
Mathias Agopianab028732010-03-16 16:41:46 -07001522 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001523 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001524 if (s.alpha==255 && !translucent &&
1525 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1526 // the opaque region is the layer's footprint
1527 opaqueRegion = visibleRegion;
1528 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529 }
1530 }
1531
Mathias Agopianab028732010-03-16 16:41:46 -07001532 // Clip the covered region to the visible region
1533 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1534
1535 // Update aboveCoveredLayers for next (lower) layer
1536 aboveCoveredLayers.orSelf(visibleRegion);
1537
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 // subtract the opaque region covered by the layers above us
1539 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540
1541 // compute this layer's dirty region
1542 if (layer->contentDirty) {
1543 // we need to invalidate the whole region
1544 dirty = visibleRegion;
1545 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001546 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 layer->contentDirty = false;
1548 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001549 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001550 * the exposed region consists of two components:
1551 * 1) what's VISIBLE now and was COVERED before
1552 * 2) what's EXPOSED now less what was EXPOSED before
1553 *
1554 * note that (1) is conservative, we start with the whole
1555 * visible region but only keep what used to be covered by
1556 * something -- which mean it may have been exposed.
1557 *
1558 * (2) handles areas that were not covered by anything but got
1559 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001560 */
Mathias Agopianab028732010-03-16 16:41:46 -07001561 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001562 const Region oldVisibleRegion = layer->visibleRegion;
1563 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001564 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1565 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566 }
1567 dirty.subtractSelf(aboveOpaqueLayers);
1568
1569 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001570 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571
Mathias Agopianab028732010-03-16 16:41:46 -07001572 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001574
Jesse Halla8026d22012-09-25 13:25:04 -07001575 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 layer->setVisibleRegion(visibleRegion);
1577 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001578 layer->setVisibleNonTransparentRegion(
1579 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 }
1581
Mathias Agopian87baae12012-07-31 12:38:26 -07001582 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583}
1584
Mathias Agopian87baae12012-07-31 12:38:26 -07001585void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1586 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001587 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001588 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1589 if (hw->getLayerStack() == layerStack) {
1590 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001591 }
1592 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001593}
1594
1595void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001597 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598
Mathias Agopian4fec8732012-06-29 14:12:52 -07001599 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001600 const LayerVector& layers(mDrawingState.layersSortedByZ);
1601 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001602 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001603 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001604 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001605 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001606 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001607 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001608
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001609 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610}
1611
Mathias Agopianad456f92011-01-13 17:53:01 -08001612void SurfaceFlinger::invalidateHwcGeometry()
1613{
1614 mHwWorkListDirty = true;
1615}
1616
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001617
Mathias Agopiancd60f992012-08-16 16:28:27 -07001618void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001619 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620{
Mathias Agopian87baae12012-07-31 12:38:26 -07001621 Region dirtyRegion(inDirtyRegion);
1622
Mathias Agopianb8a55602009-06-26 19:06:36 -07001623 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001624 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625
Mathias Agopian42977342012-08-05 00:40:46 -07001626 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001627 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001628 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1629 // takes a rectangle, we must make sure to update that whole
1630 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001631 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001632 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001633 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001634 // We need to redraw the rectangle that will be updated
1635 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001636 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001637 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001638 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001639 } else {
1640 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001641 dirtyRegion.set(hw->bounds());
1642 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 }
1644 }
1645
Mathias Agopianff2ed702013-09-01 21:36:12 -07001646 if (CC_LIKELY(!mDaltonize)) {
1647 doComposeSurfaces(hw, dirtyRegion);
1648 } else {
1649 RenderEngine& engine(getRenderEngine());
1650 engine.beginGroup(mDaltonizer());
1651 doComposeSurfaces(hw, dirtyRegion);
1652 engine.endGroup();
1653 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001655 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001656 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001657
1658 // swap buffers (presentation)
1659 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660}
1661
Mathias Agopiancd60f992012-08-16 16:28:27 -07001662void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001663{
Mathias Agopian3f844832013-08-07 21:24:32 -07001664 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001665 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001666 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001667 HWComposer::LayerListIterator cur = hwc.begin(id);
1668 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001669
Jesse Halld05a17f2013-09-30 16:49:30 -07001670 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001671 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001672 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001673 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1674 hw->getDisplayName().string());
1675 return;
1676 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001677
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001678 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001679 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001680 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001681 // when using overlays, we assume a fully transparent framebuffer
1682 // NOTE: we could reduce how much we need to clear, for instance
1683 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001684 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001685 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001686 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001687 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001688 // we start with the whole screen area
1689 const Region bounds(hw->getBounds());
1690
1691 // we remove the scissor part
1692 // we're left with the letterbox region
1693 // (common case is that letterbox ends-up being empty)
1694 const Region letterbox(bounds.subtract(hw->getScissor()));
1695
1696 // compute the area to clear
1697 Region region(hw->undefinedRegion.merge(letterbox));
1698
1699 // but limit it to the dirty region
1700 region.andSelf(dirty);
1701
Mathias Agopianb9494d52012-04-18 02:28:45 -07001702 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001703 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001704 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001705 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001706 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001707 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001708
Mathias Agopian766dc492012-10-30 18:08:06 -07001709 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1710 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001711 // scissor on the main display. It should never be needed
1712 // anyways (though in theory it could since the API allows it).
1713 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001714 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001715 if (scissor != bounds) {
1716 // scissor doesn't match the screen's dimensions, so we
1717 // need to clear everything outside of it and enable
1718 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001719
Mathias Agopianf45c5102012-10-24 16:29:17 -07001720 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001721 const uint32_t height = hw->getHeight();
1722 engine.setScissor(scissor.left, height - scissor.bottom,
1723 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001724 }
1725 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001726 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001727
Mathias Agopian85d751c2012-08-29 16:59:24 -07001728 /*
1729 * and then, render the layers targeted at the framebuffer
1730 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001731
Mathias Agopian13127d82013-03-05 17:47:11 -08001732 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001733 const size_t count = layers.size();
1734 const Transform& tr = hw->getTransform();
1735 if (cur != end) {
1736 // we're using h/w composer
1737 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001738 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001739 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001740 if (!clip.isEmpty()) {
1741 switch (cur->getCompositionType()) {
1742 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001743 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001744 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1745 && i
Mathias Agopianac683022013-10-01 15:36:52 -07001746 && layer->isOpaque() && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001747 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001748 // never clear the very first layer since we're
1749 // guaranteed the FB is already cleared
1750 layer->clearWithOpenGL(hw, clip);
1751 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001752 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001753 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001754 case HWC_FRAMEBUFFER: {
1755 layer->draw(hw, clip);
1756 break;
1757 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001758 case HWC_FRAMEBUFFER_TARGET: {
1759 // this should not happen as the iterator shouldn't
1760 // let us get there.
1761 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1762 break;
1763 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001764 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001765 }
1766 layer->setAcquireFence(hw, *cur);
1767 }
1768 } else {
1769 // we're not using h/w composer
1770 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001771 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001772 const Region clip(dirty.intersect(
1773 tr.transform(layer->visibleRegion)));
1774 if (!clip.isEmpty()) {
1775 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001776 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001777 }
1778 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001779
1780 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001781 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782}
1783
Mathias Agopian3f844832013-08-07 21:24:32 -07001784void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001785 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001786 RenderEngine& engine(getRenderEngine());
1787 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788}
1789
Mathias Agopianac9fa422013-02-11 16:40:36 -08001790void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1791 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001792 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001793 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001794{
Mathias Agopian96f08192010-06-02 23:28:45 -07001795 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001796 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001797
Mathias Agopian96f08192010-06-02 23:28:45 -07001798 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001799 Mutex::Autolock _l(mStateLock);
1800 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001801 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001802}
1803
Mathias Agopian3f844832013-08-07 21:24:32 -07001804status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001805 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001806 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001808 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001809 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001810 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 return NO_ERROR;
1812 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001813 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001814}
1815
Mathias Agopian3f844832013-08-07 21:24:32 -07001816uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001817 return android_atomic_release_load(&mTransactionFlags);
1818}
1819
Mathias Agopian3f844832013-08-07 21:24:32 -07001820uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1822}
1823
Mathias Agopian3f844832013-08-07 21:24:32 -07001824uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1826 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001827 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828 }
1829 return old;
1830}
1831
Mathias Agopian8b33f032012-07-24 20:43:54 -07001832void SurfaceFlinger::setTransactionState(
1833 const Vector<ComposerState>& state,
1834 const Vector<DisplayState>& displays,
1835 uint32_t flags)
1836{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001837 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001838 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001839 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001840
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001841 if (flags & eAnimation) {
1842 // For window updates that are part of an animation we must wait for
1843 // previous animation "frames" to be handled.
1844 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001845 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001846 if (CC_UNLIKELY(err != NO_ERROR)) {
1847 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001848 // caller after a few seconds.
1849 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1850 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001851 mAnimTransactionPending = false;
1852 break;
1853 }
1854 }
1855 }
1856
Mathias Agopiane57f2922012-08-09 16:29:12 -07001857 size_t count = displays.size();
1858 for (size_t i=0 ; i<count ; i++) {
1859 const DisplayState& s(displays[i]);
1860 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001861 }
1862
Mathias Agopiane57f2922012-08-09 16:29:12 -07001863 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001864 for (size_t i=0 ; i<count ; i++) {
1865 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001866 // Here we need to check that the interface we're given is indeed
1867 // one of our own. A malicious client could give us a NULL
1868 // IInterface, or one of its own or even one of our own but a
1869 // different type. All these situations would cause us to crash.
1870 //
1871 // NOTE: it would be better to use RTTI as we could directly check
1872 // that we have a Client*. however, RTTI is disabled in Android.
1873 if (s.client != NULL) {
1874 sp<IBinder> binder = s.client->asBinder();
1875 if (binder != NULL) {
1876 String16 desc(binder->getInterfaceDescriptor());
1877 if (desc == ISurfaceComposerClient::descriptor) {
1878 sp<Client> client( static_cast<Client *>(s.client.get()) );
1879 transactionFlags |= setClientStateLocked(client, s.state);
1880 }
1881 }
1882 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001883 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001884
Mathias Agopian386aa982011-11-07 21:58:03 -08001885 if (transactionFlags) {
1886 // this triggers the transaction
1887 setTransactionFlags(transactionFlags);
1888
1889 // if this is a synchronous transaction, wait for it to take effect
1890 // before returning.
1891 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001892 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001893 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001894 if (flags & eAnimation) {
1895 mAnimTransactionPending = true;
1896 }
1897 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001898 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1899 if (CC_UNLIKELY(err != NO_ERROR)) {
1900 // just in case something goes wrong in SF, return to the
1901 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001902 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1903 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001904 break;
1905 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001906 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907 }
1908}
1909
Mathias Agopiane57f2922012-08-09 16:29:12 -07001910uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1911{
Jesse Hall9a143922012-10-04 16:29:19 -07001912 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1913 if (dpyIdx < 0)
1914 return 0;
1915
Mathias Agopiane57f2922012-08-09 16:29:12 -07001916 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001917 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001918 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001919 const uint32_t what = s.what;
1920 if (what & DisplayState::eSurfaceChanged) {
1921 if (disp.surface->asBinder() != s.surface->asBinder()) {
1922 disp.surface = s.surface;
1923 flags |= eDisplayTransactionNeeded;
1924 }
1925 }
1926 if (what & DisplayState::eLayerStackChanged) {
1927 if (disp.layerStack != s.layerStack) {
1928 disp.layerStack = s.layerStack;
1929 flags |= eDisplayTransactionNeeded;
1930 }
1931 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001932 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001933 if (disp.orientation != s.orientation) {
1934 disp.orientation = s.orientation;
1935 flags |= eDisplayTransactionNeeded;
1936 }
1937 if (disp.frame != s.frame) {
1938 disp.frame = s.frame;
1939 flags |= eDisplayTransactionNeeded;
1940 }
1941 if (disp.viewport != s.viewport) {
1942 disp.viewport = s.viewport;
1943 flags |= eDisplayTransactionNeeded;
1944 }
1945 }
1946 }
1947 return flags;
1948}
1949
1950uint32_t SurfaceFlinger::setClientStateLocked(
1951 const sp<Client>& client,
1952 const layer_state_t& s)
1953{
1954 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001955 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001956 if (layer != 0) {
1957 const uint32_t what = s.what;
1958 if (what & layer_state_t::ePositionChanged) {
1959 if (layer->setPosition(s.x, s.y))
1960 flags |= eTraversalNeeded;
1961 }
1962 if (what & layer_state_t::eLayerChanged) {
1963 // NOTE: index needs to be calculated before we update the state
1964 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1965 if (layer->setLayer(s.z)) {
1966 mCurrentState.layersSortedByZ.removeAt(idx);
1967 mCurrentState.layersSortedByZ.add(layer);
1968 // we need traversal (state changed)
1969 // AND transaction (list changed)
1970 flags |= eTransactionNeeded|eTraversalNeeded;
1971 }
1972 }
1973 if (what & layer_state_t::eSizeChanged) {
1974 if (layer->setSize(s.w, s.h)) {
1975 flags |= eTraversalNeeded;
1976 }
1977 }
1978 if (what & layer_state_t::eAlphaChanged) {
1979 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1980 flags |= eTraversalNeeded;
1981 }
1982 if (what & layer_state_t::eMatrixChanged) {
1983 if (layer->setMatrix(s.matrix))
1984 flags |= eTraversalNeeded;
1985 }
1986 if (what & layer_state_t::eTransparentRegionChanged) {
1987 if (layer->setTransparentRegionHint(s.transparentRegion))
1988 flags |= eTraversalNeeded;
1989 }
1990 if (what & layer_state_t::eVisibilityChanged) {
1991 if (layer->setFlags(s.flags, s.mask))
1992 flags |= eTraversalNeeded;
1993 }
1994 if (what & layer_state_t::eCropChanged) {
1995 if (layer->setCrop(s.crop))
1996 flags |= eTraversalNeeded;
1997 }
1998 if (what & layer_state_t::eLayerStackChanged) {
1999 // NOTE: index needs to be calculated before we update the state
2000 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2001 if (layer->setLayerStack(s.layerStack)) {
2002 mCurrentState.layersSortedByZ.removeAt(idx);
2003 mCurrentState.layersSortedByZ.add(layer);
2004 // we need traversal (state changed)
2005 // AND transaction (list changed)
2006 flags |= eTransactionNeeded|eTraversalNeeded;
2007 }
2008 }
2009 }
2010 return flags;
2011}
2012
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002013status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002014 const String8& name,
2015 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002016 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2017 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002018{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002019 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002020 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002021 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002022 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002023 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002024 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002025
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002026 status_t result = NO_ERROR;
2027
2028 sp<Layer> layer;
2029
Mathias Agopian3165cc22012-08-08 19:42:09 -07002030 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2031 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002032 result = createNormalLayer(client,
2033 name, w, h, flags, format,
2034 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002035 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002036 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002037 result = createDimLayer(client,
2038 name, w, h, flags,
2039 handle, gbp, &layer);
2040 break;
2041 default:
2042 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002043 break;
2044 }
2045
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002046 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002047 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002048 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002049 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002050 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002051}
2052
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002053status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2054 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2055 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002056{
2057 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002058 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002059 case PIXEL_FORMAT_TRANSPARENT:
2060 case PIXEL_FORMAT_TRANSLUCENT:
2061 format = PIXEL_FORMAT_RGBA_8888;
2062 break;
2063 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002064#ifdef NO_RGBX_8888
2065 format = PIXEL_FORMAT_RGB_565;
2066#else
Mathias Agopian8f105402010-04-05 18:01:24 -07002067 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002068#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002069 break;
2070 }
2071
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002072#ifdef NO_RGBX_8888
2073 if (format == PIXEL_FORMAT_RGBX_8888)
2074 format = PIXEL_FORMAT_RGBA_8888;
2075#endif
2076
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002077 *outLayer = new Layer(this, client, name, w, h, flags);
2078 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2079 if (err == NO_ERROR) {
2080 *handle = (*outLayer)->getHandle();
2081 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002083
2084 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2085 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086}
2087
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002088status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2089 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2090 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002092 *outLayer = new LayerDim(this, client, name, w, h, flags);
2093 *handle = (*outLayer)->getHandle();
2094 *gbp = (*outLayer)->getBufferQueue();
2095 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002096}
2097
Mathias Agopianac9fa422013-02-11 16:40:36 -08002098status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002099{
Mathias Agopian67106042013-03-14 19:18:13 -07002100 // called by the window manager when it wants to remove a Layer
2101 status_t err = NO_ERROR;
2102 sp<Layer> l(client->getLayerUser(handle));
2103 if (l != NULL) {
2104 err = removeLayer(l);
2105 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2106 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002107 }
2108 return err;
2109}
2110
Mathias Agopian13127d82013-03-05 17:47:11 -08002111status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002112{
Mathias Agopian67106042013-03-14 19:18:13 -07002113 // called by ~LayerCleaner() when all references to the IBinder (handle)
2114 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002115 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002116 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002117 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002118 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002119 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002120 "error removing layer=%p (%s)", l.get(), strerror(-err));
2121 }
2122 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123}
2124
Mathias Agopianb60314a2012-04-10 22:09:54 -07002125// ---------------------------------------------------------------------------
2126
Andy McFadden13a082e2012-08-24 10:16:42 -07002127void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002128 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002129 Vector<ComposerState> state;
2130 Vector<DisplayState> displays;
2131 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002132 d.what = DisplayState::eDisplayProjectionChanged |
2133 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002134 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002135 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002136 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002137 d.frame.makeInvalid();
2138 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002139 displays.add(d);
2140 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002141 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002142
2143 const nsecs_t period =
2144 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2145 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002146}
2147
2148void SurfaceFlinger::initializeDisplays() {
2149 class MessageScreenInitialized : public MessageBase {
2150 SurfaceFlinger* flinger;
2151 public:
2152 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2153 virtual bool handler() {
2154 flinger->onInitializeDisplays();
2155 return true;
2156 }
2157 };
2158 sp<MessageBase> msg = new MessageScreenInitialized(this);
2159 postMessageAsync(msg); // we may be called from main thread, use async message
2160}
2161
2162
Mathias Agopiancde87a32012-09-13 14:09:01 -07002163void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002164 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2165 if (hw->isScreenAcquired()) {
2166 // this is expected, e.g. when power manager wakes up during boot
2167 ALOGD(" screen was previously acquired");
2168 return;
2169 }
2170
Mathias Agopian42977342012-08-05 00:40:46 -07002171 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002172 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002173 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002174 // built-in display, tell the HWC
2175 getHwComposer().acquire(type);
2176
2177 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2178 // FIXME: eventthread only knows about the main display right now
2179 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002180
2181 resyncToHardwareVsync();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002182 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002183 }
Mathias Agopian20128302012-08-13 18:32:13 -07002184 mVisibleRegionsDirty = true;
2185 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002186}
2187
Mathias Agopiancde87a32012-09-13 14:09:01 -07002188void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002189 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2190 if (!hw->isScreenAcquired()) {
2191 ALOGD(" screen was previously released");
2192 return;
2193 }
2194
2195 hw->releaseScreen();
2196 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002197 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002198 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002199 // FIXME: eventthread only knows about the main display right now
2200 mEventThread->onScreenReleased();
2201 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002202
2203 // built-in display, tell the HWC
2204 getHwComposer().release(type);
2205 }
2206 mVisibleRegionsDirty = true;
2207 // from this point on, SF will stop drawing on this display
2208}
2209
2210void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2211 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002212 SurfaceFlinger& mFlinger;
2213 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002214 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002215 MessageScreenAcquired(SurfaceFlinger& flinger,
2216 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002217 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002218 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2219 if (hw == NULL) {
2220 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002221 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002222 ALOGW("Attempt to unblank virtual display");
2223 } else {
2224 mFlinger.onScreenAcquired(hw);
2225 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002226 return true;
2227 }
2228 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002229 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2230 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231}
2232
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002233void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002234 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002235 SurfaceFlinger& mFlinger;
2236 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002237 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002238 MessageScreenReleased(SurfaceFlinger& flinger,
2239 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002240 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002241 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2242 if (hw == NULL) {
2243 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002244 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002245 ALOGW("Attempt to blank virtual display");
2246 } else {
2247 mFlinger.onScreenReleased(hw);
2248 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002249 return true;
2250 }
2251 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002252 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2253 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002254}
2255
2256// ---------------------------------------------------------------------------
2257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002258status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2259{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002260 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002261
Mathias Agopianbd115332013-04-18 16:41:04 -07002262 IPCThreadState* ipc = IPCThreadState::self();
2263 const int pid = ipc->getCallingPid();
2264 const int uid = ipc->getCallingUid();
2265 if ((uid != AID_SHELL) &&
2266 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002267 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002268 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002269 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002270 // Try to get the main lock, but don't insist if we can't
2271 // (this would indicate SF is stuck, but we want to be able to
2272 // print something in dumpsys).
2273 int retry = 3;
2274 while (mStateLock.tryLock()<0 && --retry>=0) {
2275 usleep(1000000);
2276 }
2277 const bool locked(retry >= 0);
2278 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002279 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002280 "SurfaceFlinger appears to be unresponsive, "
2281 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002282 }
2283
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002284 bool dumpAll = true;
2285 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002286 size_t numArgs = args.size();
2287 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002288 if ((index < numArgs) &&
2289 (args[index] == String16("--list"))) {
2290 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002291 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002292 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002293 }
2294
2295 if ((index < numArgs) &&
2296 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002297 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002298 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002299 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002300 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002301
2302 if ((index < numArgs) &&
2303 (args[index] == String16("--latency-clear"))) {
2304 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002305 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002306 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002307 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002309
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002310 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002311 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002312 }
2313
Mathias Agopian9795c422009-08-26 16:36:26 -07002314 if (locked) {
2315 mStateLock.unlock();
2316 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002317 }
2318 write(fd, result.string(), result.size());
2319 return NO_ERROR;
2320}
2321
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002322void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002323 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002324{
2325 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2326 const size_t count = currentLayers.size();
2327 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002328 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002329 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002330 }
2331}
2332
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002333void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002334 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002335{
2336 String8 name;
2337 if (index < args.size()) {
2338 name = String8(args[index]);
2339 index++;
2340 }
2341
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002342 const nsecs_t period =
2343 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2344 result.appendFormat("%lld\n", period);
2345
2346 if (name.isEmpty()) {
2347 mAnimFrameTracker.dump(result);
2348 } else {
2349 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2350 const size_t count = currentLayers.size();
2351 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002352 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002353 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002354 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002355 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356 }
2357 }
2358}
2359
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002360void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002361 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002362{
2363 String8 name;
2364 if (index < args.size()) {
2365 name = String8(args[index]);
2366 index++;
2367 }
2368
2369 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2370 const size_t count = currentLayers.size();
2371 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002372 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002373 if (name.isEmpty() || (name == layer->getName())) {
2374 layer->clearStats();
2375 }
2376 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002377
2378 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002379}
2380
Jamie Gennis6547ff42013-07-16 20:12:42 -07002381// This should only be called from the main thread. Otherwise it would need
2382// the lock and should use mCurrentState rather than mDrawingState.
2383void SurfaceFlinger::logFrameStats() {
2384 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2385 const size_t count = drawingLayers.size();
2386 for (size_t i=0 ; i<count ; i++) {
2387 const sp<Layer>& layer(drawingLayers[i]);
2388 layer->logFrameStats();
2389 }
2390
2391 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2392}
2393
Andy McFadden4803b742012-09-24 19:07:20 -07002394/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2395{
2396 static const char* config =
2397 " [sf"
2398#ifdef NO_RGBX_8888
2399 " NO_RGBX_8888"
2400#endif
2401#ifdef HAS_CONTEXT_PRIORITY
2402 " HAS_CONTEXT_PRIORITY"
2403#endif
2404#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2405 " NEVER_DEFAULT_TO_ASYNC_MODE"
2406#endif
2407#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2408 " TARGET_DISABLE_TRIPLE_BUFFERING"
2409#endif
2410 "]";
2411 result.append(config);
2412}
2413
Mathias Agopian74d211a2013-04-22 16:55:35 +02002414void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2415 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002416{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002417 bool colorize = false;
2418 if (index < args.size()
2419 && (args[index] == String16("--color"))) {
2420 colorize = true;
2421 index++;
2422 }
2423
2424 Colorizer colorizer(colorize);
2425
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002426 // figure out if we're stuck somewhere
2427 const nsecs_t now = systemTime();
2428 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2429 const nsecs_t inTransaction(mDebugInTransaction);
2430 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2431 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2432
2433 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002434 * Dump library configuration.
2435 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002436
2437 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002438 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002439 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002440 appendSfConfigString(result);
2441 appendUiConfigString(result);
2442 appendGuiConfigString(result);
2443 result.append("\n");
2444
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002445 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002446 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002447 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002448 result.append(SyncFeatures::getInstance().toString());
2449 result.append("\n");
2450
Andy McFadden4803b742012-09-24 19:07:20 -07002451 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002452 * Dump the visible layer list
2453 */
2454 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2455 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002456 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002457 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002458 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002459 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002460 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002461 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002462 }
2463
2464 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002465 * Dump Display state
2466 */
2467
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002468 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002469 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002470 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002471 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2472 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002473 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002474 }
2475
2476 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002477 * Dump SurfaceFlinger global state
2478 */
2479
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002480 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002481 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002482 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002483
Mathias Agopian888c8222012-08-04 21:10:38 -07002484 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002485 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002486
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002487 colorizer.bold(result);
2488 result.appendFormat("EGL implementation : %s\n",
2489 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2490 colorizer.reset(result);
2491 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002492 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002493
Mathias Agopian875d8e12013-06-07 15:35:48 -07002494 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002495
Mathias Agopian42977342012-08-05 00:40:46 -07002496 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002497 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002498 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002499 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002500 " last eglSwapBuffers() time: %f us\n"
2501 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002502 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002503 " refresh-rate : %f fps\n"
2504 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002505 " y-dpi : %f\n"
2506 " EGL_NATIVE_VISUAL_ID : %d\n"
2507 " gpu_to_cpu_unsupported : %d\n"
2508 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002509 mLastSwapBufferTime/1000.0,
2510 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002511 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002512 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2513 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002514 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2515 mEGLNativeVisualId,
2516 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002517
Mathias Agopian74d211a2013-04-22 16:55:35 +02002518 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002519 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002520
Mathias Agopian74d211a2013-04-22 16:55:35 +02002521 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002522 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002523
2524 /*
2525 * VSYNC state
2526 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002527 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002528
2529 /*
2530 * Dump HWComposer state
2531 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002532 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002533 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002534 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002535 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002536 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002537 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002538 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002539
2540 /*
2541 * Dump gralloc state
2542 */
2543 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2544 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002545}
2546
Mathias Agopian13127d82013-03-05 17:47:11 -08002547const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002548SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002549 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002550 wp<IBinder> dpy;
2551 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2552 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2553 dpy = mDisplays.keyAt(i);
2554 break;
2555 }
2556 }
2557 if (dpy == NULL) {
2558 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2559 // Just use the primary display so we have something to return
2560 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2561 }
2562 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002563}
2564
Keun young Park63f165f2012-08-31 10:53:36 -07002565bool SurfaceFlinger::startDdmConnection()
2566{
2567 void* libddmconnection_dso =
2568 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2569 if (!libddmconnection_dso) {
2570 return false;
2571 }
2572 void (*DdmConnection_start)(const char* name);
2573 DdmConnection_start =
2574 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2575 if (!DdmConnection_start) {
2576 dlclose(libddmconnection_dso);
2577 return false;
2578 }
2579 (*DdmConnection_start)(getServiceName());
2580 return true;
2581}
2582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002583status_t SurfaceFlinger::onTransact(
2584 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2585{
2586 switch (code) {
2587 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002588 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002589 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002591 case BLANK:
2592 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002593 {
2594 // codes that require permission check
2595 IPCThreadState* ipc = IPCThreadState::self();
2596 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002597 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002598 if ((uid != AID_GRAPHICS) &&
2599 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002600 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002601 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2602 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002603 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002604 break;
2605 }
2606 case CAPTURE_SCREEN:
2607 {
2608 // codes that require permission check
2609 IPCThreadState* ipc = IPCThreadState::self();
2610 const int pid = ipc->getCallingPid();
2611 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002612 if ((uid != AID_GRAPHICS) &&
2613 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002614 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002615 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2616 return PERMISSION_DENIED;
2617 }
2618 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002619 }
2620 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002621
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002622 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2623 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002624 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002625 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002626 IPCThreadState* ipc = IPCThreadState::self();
2627 const int pid = ipc->getCallingPid();
2628 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002629 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002630 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002631 return PERMISSION_DENIED;
2632 }
2633 int n;
2634 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002635 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002636 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637 return NO_ERROR;
2638 case 1002: // SHOW_UPDATES
2639 n = data.readInt32();
2640 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002641 invalidateHwcGeometry();
2642 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002643 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002645 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002646 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002647 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002648 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002649 setTransactionFlags(
2650 eTransactionNeeded|
2651 eDisplayTransactionNeeded|
2652 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002653 return NO_ERROR;
2654 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002655 case 1006:{ // send empty update
2656 signalRefresh();
2657 return NO_ERROR;
2658 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002659 case 1008: // toggle use of hw composer
2660 n = data.readInt32();
2661 mDebugDisableHWC = n ? 1 : 0;
2662 invalidateHwcGeometry();
2663 repaintEverything();
2664 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002665 case 1009: // toggle use of transform hint
2666 n = data.readInt32();
2667 mDebugDisableTransformHint = n ? 1 : 0;
2668 invalidateHwcGeometry();
2669 repaintEverything();
2670 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002671 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002672 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673 reply->writeInt32(0);
2674 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002675 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002676 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002677 return NO_ERROR;
2678 case 1013: {
2679 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002680 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2681 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002682 return NO_ERROR;
2683 }
2684 case 1014: {
2685 // daltonize
2686 n = data.readInt32();
2687 switch (n % 10) {
2688 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2689 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2690 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2691 }
2692 if (n >= 10) {
2693 mDaltonizer.setMode(Daltonizer::correction);
2694 } else {
2695 mDaltonizer.setMode(Daltonizer::simulation);
2696 }
2697 mDaltonize = n > 0;
2698 invalidateHwcGeometry();
2699 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002700 }
2701 return NO_ERROR;
2702 }
2703 }
2704 return err;
2705}
2706
Mathias Agopian53331da2011-08-22 21:44:41 -07002707void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002708 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002709 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002710}
2711
Mathias Agopian59119e62010-10-11 12:37:43 -07002712// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002713// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002714// ---------------------------------------------------------------------------
2715
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002716/* The code below is here to handle b/8734824
2717 *
2718 * We create a IGraphicBufferProducer wrapper that forwards all calls
2719 * to the calling binder thread, where they are executed. This allows
2720 * the calling thread to be reused (on the other side) and not
2721 * depend on having "enough" binder threads to handle the requests.
2722 *
2723 */
2724
2725class GraphicProducerWrapper : public BBinder, public MessageHandler {
2726 sp<IGraphicBufferProducer> impl;
2727 sp<Looper> looper;
2728 status_t result;
2729 bool exitPending;
2730 bool exitRequested;
2731 mutable Barrier barrier;
2732 volatile int32_t memoryBarrier;
2733 uint32_t code;
2734 Parcel const* data;
2735 Parcel* reply;
2736
2737 enum {
2738 MSG_API_CALL,
2739 MSG_EXIT
2740 };
2741
2742 /*
2743 * this is called by our "fake" BpGraphicBufferProducer. We package the
2744 * data and reply Parcel and forward them to the calling thread.
2745 */
2746 virtual status_t transact(uint32_t code,
2747 const Parcel& data, Parcel* reply, uint32_t flags) {
2748 this->code = code;
2749 this->data = &data;
2750 this->reply = reply;
2751 android_atomic_acquire_store(0, &memoryBarrier);
2752 if (exitPending) {
2753 // if we've exited, we run the message synchronously right here
2754 handleMessage(Message(MSG_API_CALL));
2755 } else {
2756 barrier.close();
2757 looper->sendMessage(this, Message(MSG_API_CALL));
2758 barrier.wait();
2759 }
2760 return NO_ERROR;
2761 }
2762
2763 /*
2764 * here we run on the binder calling thread. All we've got to do is
2765 * call the real BpGraphicBufferProducer.
2766 */
2767 virtual void handleMessage(const Message& message) {
2768 android_atomic_release_load(&memoryBarrier);
2769 if (message.what == MSG_API_CALL) {
2770 impl->asBinder()->transact(code, data[0], reply);
2771 barrier.open();
2772 } else if (message.what == MSG_EXIT) {
2773 exitRequested = true;
2774 }
2775 }
2776
2777public:
2778 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2779 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2780 exitPending(false), exitRequested(false) {
2781 }
2782
2783 status_t waitForResponse() {
2784 do {
2785 looper->pollOnce(-1);
2786 } while (!exitRequested);
2787 return result;
2788 }
2789
2790 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002791 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002792 exitPending = true;
2793 looper->sendMessage(this, Message(MSG_EXIT));
2794 }
2795};
2796
2797
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002798status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2799 const sp<IGraphicBufferProducer>& producer,
2800 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002801 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002802
2803 if (CC_UNLIKELY(display == 0))
2804 return BAD_VALUE;
2805
2806 if (CC_UNLIKELY(producer == 0))
2807 return BAD_VALUE;
2808
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002809 // if we have secure windows on this display, never allow the screen capture
2810 // unless the producer interface is local (i.e.: we can take a screenshot for
2811 // ourselves).
2812 if (!producer->asBinder()->localBinder()) {
2813 Mutex::Autolock _l(mStateLock);
2814 sp<const DisplayDevice> hw(getDisplayDevice(display));
2815 if (hw->getSecureLayerVisible()) {
2816 ALOGW("FB is protected: PERMISSION_DENIED");
2817 return PERMISSION_DENIED;
2818 }
2819 }
2820
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002821 class MessageCaptureScreen : public MessageBase {
2822 SurfaceFlinger* flinger;
2823 sp<IBinder> display;
2824 sp<IGraphicBufferProducer> producer;
2825 uint32_t reqWidth, reqHeight;
2826 uint32_t minLayerZ,maxLayerZ;
2827 status_t result;
2828 public:
2829 MessageCaptureScreen(SurfaceFlinger* flinger,
2830 const sp<IBinder>& display,
2831 const sp<IGraphicBufferProducer>& producer,
2832 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002833 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002834 : flinger(flinger), display(display), producer(producer),
2835 reqWidth(reqWidth), reqHeight(reqHeight),
2836 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2837 result(PERMISSION_DENIED)
2838 {
2839 }
2840 status_t getResult() const {
2841 return result;
2842 }
2843 virtual bool handler() {
2844 Mutex::Autolock _l(flinger->mStateLock);
2845 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002846 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002847 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002848 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002849 return true;
2850 }
2851 };
2852
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002853 // make sure to process transactions before screenshots -- a transaction
2854 // might already be pending but scheduled for VSYNC; this guarantees we
2855 // will handle it before the screenshot. When VSYNC finally arrives
2856 // the scheduled transaction will be a no-op. If no transactions are
2857 // scheduled at this time, this will end-up being a no-op as well.
2858 mEventQueue.invalidateTransactionNow();
2859
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002860 // this creates a "fake" BBinder which will serve as a "fake" remote
2861 // binder to receive the marshaled calls and forward them to the
2862 // real remote (a BpGraphicBufferProducer)
2863 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2864
2865 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2866 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002867 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002868 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002869 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002870
2871 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002872 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002873 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002874 }
2875 return res;
2876}
2877
Mathias Agopian180f10d2013-04-10 22:55:41 -07002878
2879void SurfaceFlinger::renderScreenImplLocked(
2880 const sp<const DisplayDevice>& hw,
2881 uint32_t reqWidth, uint32_t reqHeight,
2882 uint32_t minLayerZ, uint32_t maxLayerZ,
2883 bool yswap)
2884{
2885 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002886 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002887
2888 // get screen geometry
2889 const uint32_t hw_w = hw->getWidth();
2890 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002891 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2892
2893 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002894 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002895
2896 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002897 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2898 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002899
2900 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002901 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002902
2903 const LayerVector& layers( mDrawingState.layersSortedByZ );
2904 const size_t count = layers.size();
2905 for (size_t i=0 ; i<count ; ++i) {
2906 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002907 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002908 if (state.layerStack == hw->getLayerStack()) {
2909 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2910 if (layer->isVisible()) {
2911 if (filtering) layer->setFiltering(true);
2912 layer->draw(hw);
2913 if (filtering) layer->setFiltering(false);
2914 }
2915 }
2916 }
2917 }
2918
2919 // compositionComplete is needed for older driver
2920 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002921 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002922}
2923
2924
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002925status_t SurfaceFlinger::captureScreenImplLocked(
2926 const sp<const DisplayDevice>& hw,
2927 const sp<IGraphicBufferProducer>& producer,
2928 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002929 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002930{
2931 ATRACE_CALL();
2932
Mathias Agopian180f10d2013-04-10 22:55:41 -07002933 // get screen geometry
2934 const uint32_t hw_w = hw->getWidth();
2935 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002936
Mathias Agopian180f10d2013-04-10 22:55:41 -07002937 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2938 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2939 reqWidth, reqHeight, hw_w, hw_h);
2940 return BAD_VALUE;
2941 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002942
Mathias Agopian180f10d2013-04-10 22:55:41 -07002943 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2944 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2945
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002946 // create a surface (because we're a producer, and we need to
2947 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002948 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002949 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002950
2951 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002952 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002953 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2954 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002955
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002956 int err = 0;
2957 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002958 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002959 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2960 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002961
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002962 if (err == NO_ERROR) {
2963 ANativeWindowBuffer* buffer;
2964 /* TODO: Once we have the sync framework everywhere this can use
2965 * server-side waits on the fence that dequeueBuffer returns.
2966 */
2967 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2968 if (result == NO_ERROR) {
2969 // create an EGLImage from the buffer so we can later
2970 // turn it into a texture
2971 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2972 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2973 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002974 // this binds the given EGLImage as a framebuffer for the
2975 // duration of this scope.
2976 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2977 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002978 // this will in fact render into our dequeued buffer
2979 // via an FBO, which means we didn't have to create
2980 // an EGLSurface and therefore we're not
2981 // dependent on the context's EGLConfig.
2982 renderScreenImplLocked(hw, reqWidth, reqHeight,
2983 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002984
2985 if (DEBUG_SCREENSHOTS) {
2986 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2987 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2988 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2989 hw, minLayerZ, maxLayerZ);
2990 delete [] pixels;
2991 }
2992
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002993 } else {
2994 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2995 result = INVALID_OPERATION;
2996 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002997 // destroy our image
2998 eglDestroyImageKHR(mEGLDisplay, image);
2999 } else {
3000 result = BAD_VALUE;
3001 }
3002 window->queueBuffer(window, buffer, -1);
3003 }
3004 } else {
3005 result = BAD_VALUE;
3006 }
3007 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3008 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003009
Mathias Agopian74c40c02010-09-29 13:02:36 -07003010 return result;
3011}
3012
Mathias Agopiand5556842013-09-19 17:08:37 -07003013void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3014 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003015 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003016 for (size_t y=0 ; y<h ; y++) {
3017 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3018 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003019 if (p[x] != 0xFF000000) return;
3020 }
3021 }
3022 ALOGE("*** we just took a black screenshot ***\n"
3023 "requested minz=%d, maxz=%d, layerStack=%d",
3024 minLayerZ, maxLayerZ, hw->getLayerStack());
3025 const LayerVector& layers( mDrawingState.layersSortedByZ );
3026 const size_t count = layers.size();
3027 for (size_t i=0 ; i<count ; ++i) {
3028 const sp<Layer>& layer(layers[i]);
3029 const Layer::State& state(layer->getDrawingState());
3030 const bool visible = (state.layerStack == hw->getLayerStack())
3031 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3032 && (layer->isVisible());
3033 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
3034 visible ? '+' : '-',
3035 i, layer->getName().string(), state.layerStack, state.z,
3036 layer->isVisible(), state.flags, state.alpha);
3037 }
3038 }
3039}
3040
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003041// ---------------------------------------------------------------------------
3042
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003043SurfaceFlinger::LayerVector::LayerVector() {
3044}
3045
3046SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003047 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003048}
3049
3050int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3051 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003052{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003053 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003054 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3055 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003056
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003057 uint32_t ls = l->getCurrentState().layerStack;
3058 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003059 if (ls != rs)
3060 return ls - rs;
3061
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003062 uint32_t lz = l->getCurrentState().z;
3063 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003064 if (lz != rz)
3065 return lz - rz;
3066
3067 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003068}
3069
3070// ---------------------------------------------------------------------------
3071
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003072SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3073 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003074}
3075
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003076SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003077 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003078 viewport.makeInvalid();
3079 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003080}
3081
3082// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003083
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003084}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003085
3086
3087#if defined(__gl_h_)
3088#error "don't include gl/gl.h in this file"
3089#endif
3090
3091#if defined(__gl2_h_)
3092#error "don't include gl2/gl2.h in this file"
3093#endif