1 /*
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
17 //#define LOG_NDEBUG 0
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20 #include <sys/types.h>
21 #include <errno.h>
22 #include <dlfcn.h>
23
24 #include <algorithm>
25 #include <cinttypes>
26 #include <cmath>
27 #include <cstdint>
28 #include <functional>
29 #include <mutex>
30 #include <optional>
31 #include <unordered_map>
32
33 #include <cutils/properties.h>
34 #include <log/log.h>
35
36 #include <binder/IPCThreadState.h>
37 #include <binder/IServiceManager.h>
38 #include <binder/PermissionCache.h>
39
40 #include <compositionengine/CompositionEngine.h>
41 #include <compositionengine/Display.h>
42 #include <compositionengine/DisplayColorProfile.h>
43 #include <compositionengine/Layer.h>
44 #include <compositionengine/OutputLayer.h>
45 #include <compositionengine/RenderSurface.h>
46 #include <compositionengine/impl/LayerCompositionState.h>
47 #include <compositionengine/impl/OutputCompositionState.h>
48 #include <compositionengine/impl/OutputLayerCompositionState.h>
49 #include <dvr/vr_flinger.h>
50 #include <gui/BufferQueue.h>
51 #include <gui/DebugEGLImageTracker.h>
52
53 #include <gui/GuiConfig.h>
54 #include <gui/IDisplayEventConnection.h>
55 #include <gui/IProducerListener.h>
56 #include <gui/LayerDebugInfo.h>
57 #include <gui/Surface.h>
58 #include <hidl/ServiceManagement.h>
59 #include <input/IInputFlinger.h>
60 #include <renderengine/RenderEngine.h>
61 #include <ui/ColorSpace.h>
62 #include <ui/DebugUtils.h>
63 #include <ui/DisplayInfo.h>
64 #include <ui/DisplayStatInfo.h>
65 #include <ui/GraphicBufferAllocator.h>
66 #include <ui/PixelFormat.h>
67 #include <ui/UiConfig.h>
68 #include <utils/StopWatch.h>
69 #include <utils/String16.h>
70 #include <utils/String8.h>
71 #include <utils/Timers.h>
72 #include <utils/Trace.h>
73 #include <utils/misc.h>
74
75 #include <private/android_filesystem_config.h>
76 #include <private/gui/SyncFeatures.h>
77
78 #include "BufferLayer.h"
79 #include "BufferQueueLayer.h"
80 #include "BufferStateLayer.h"
81 #include "Client.h"
82 #include "ColorLayer.h"
83 #include "Colorizer.h"
84 #include "ContainerLayer.h"
85 #include "DisplayDevice.h"
86 #include "Layer.h"
87 #include "LayerVector.h"
88 #include "MonitoredProducer.h"
89 #include "NativeWindowSurface.h"
90 #include "RefreshRateOverlay.h"
91 #include "StartPropertySetThread.h"
92 #include "SurfaceFlinger.h"
93 #include "SurfaceInterceptor.h"
94
95 #include "DisplayHardware/ComposerHal.h"
96 #include "DisplayHardware/DisplayIdentification.h"
97 #include "DisplayHardware/FramebufferSurface.h"
98 #include "DisplayHardware/HWComposer.h"
99 #include "DisplayHardware/VirtualDisplaySurface.h"
100 #include "Effects/Daltonizer.h"
101 #include "RegionSamplingThread.h"
102 #include "Scheduler/DispSync.h"
103 #include "Scheduler/DispSyncSource.h"
104 #include "Scheduler/EventControlThread.h"
105 #include "Scheduler/EventThread.h"
106 #include "Scheduler/InjectVSyncSource.h"
107 #include "Scheduler/MessageQueue.h"
108 #include "Scheduler/PhaseOffsets.h"
109 #include "Scheduler/Scheduler.h"
110 #include "TimeStats/TimeStats.h"
111
112 #include <cutils/compiler.h>
113
114 #include "android-base/stringprintf.h"
115
116 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
117 #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
118 #include <android/hardware/configstore/1.1/types.h>
119 #include <android/hardware/power/1.0/IPower.h>
120 #include <configstore/Utils.h>
121
122 #include <layerproto/LayerProtoParser.h>
123 #include "SurfaceFlingerProperties.h"
124
125 namespace android {
126
127 using namespace android::hardware::configstore;
128 using namespace android::hardware::configstore::V1_0;
129 using namespace android::sysprop;
130
131 using android::hardware::power::V1_0::PowerHint;
132 using base::StringAppendF;
133 using ui::ColorMode;
134 using ui::Dataspace;
135 using ui::DisplayPrimaries;
136 using ui::Hdr;
137 using ui::RenderIntent;
138
139 namespace {
140
141 #pragma clang diagnostic push
142 #pragma clang diagnostic error "-Wswitch-enum"
143
isWideColorMode(const ColorMode colorMode)144 bool isWideColorMode(const ColorMode colorMode) {
145 switch (colorMode) {
146 case ColorMode::DISPLAY_P3:
147 case ColorMode::ADOBE_RGB:
148 case ColorMode::DCI_P3:
149 case ColorMode::BT2020:
150 case ColorMode::DISPLAY_BT2020:
151 case ColorMode::BT2100_PQ:
152 case ColorMode::BT2100_HLG:
153 return true;
154 case ColorMode::NATIVE:
155 case ColorMode::STANDARD_BT601_625:
156 case ColorMode::STANDARD_BT601_625_UNADJUSTED:
157 case ColorMode::STANDARD_BT601_525:
158 case ColorMode::STANDARD_BT601_525_UNADJUSTED:
159 case ColorMode::STANDARD_BT709:
160 case ColorMode::SRGB:
161 return false;
162 }
163 return false;
164 }
165
isHdrColorMode(const ColorMode colorMode)166 bool isHdrColorMode(const ColorMode colorMode) {
167 switch (colorMode) {
168 case ColorMode::BT2100_PQ:
169 case ColorMode::BT2100_HLG:
170 return true;
171 case ColorMode::DISPLAY_P3:
172 case ColorMode::ADOBE_RGB:
173 case ColorMode::DCI_P3:
174 case ColorMode::BT2020:
175 case ColorMode::DISPLAY_BT2020:
176 case ColorMode::NATIVE:
177 case ColorMode::STANDARD_BT601_625:
178 case ColorMode::STANDARD_BT601_625_UNADJUSTED:
179 case ColorMode::STANDARD_BT601_525:
180 case ColorMode::STANDARD_BT601_525_UNADJUSTED:
181 case ColorMode::STANDARD_BT709:
182 case ColorMode::SRGB:
183 return false;
184 }
185 return false;
186 }
187
fromSurfaceComposerRotation(ISurfaceComposer::Rotation rotation)188 ui::Transform::orientation_flags fromSurfaceComposerRotation(ISurfaceComposer::Rotation rotation) {
189 switch (rotation) {
190 case ISurfaceComposer::eRotateNone:
191 return ui::Transform::ROT_0;
192 case ISurfaceComposer::eRotate90:
193 return ui::Transform::ROT_90;
194 case ISurfaceComposer::eRotate180:
195 return ui::Transform::ROT_180;
196 case ISurfaceComposer::eRotate270:
197 return ui::Transform::ROT_270;
198 }
199 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
200 return ui::Transform::ROT_0;
201 }
202
203 #pragma clang diagnostic pop
204
205 class ConditionalLock {
206 public:
ConditionalLock(Mutex & mutex,bool lock)207 ConditionalLock(Mutex& mutex, bool lock) : mMutex(mutex), mLocked(lock) {
208 if (lock) {
209 mMutex.lock();
210 }
211 }
~ConditionalLock()212 ~ConditionalLock() { if (mLocked) mMutex.unlock(); }
213 private:
214 Mutex& mMutex;
215 bool mLocked;
216 };
217
218 // Currently we only support V0_SRGB and DISPLAY_P3 as composition preference.
validateCompositionDataspace(Dataspace dataspace)219 bool validateCompositionDataspace(Dataspace dataspace) {
220 return dataspace == Dataspace::V0_SRGB || dataspace == Dataspace::DISPLAY_P3;
221 }
222
223 } // namespace anonymous
224
225 // ---------------------------------------------------------------------------
226
227 const String16 sHardwareTest("android.permission.HARDWARE_TEST");
228 const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
229 const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
230 const String16 sDump("android.permission.DUMP");
231
232 // ---------------------------------------------------------------------------
233 int64_t SurfaceFlinger::dispSyncPresentTimeOffset;
234 bool SurfaceFlinger::useHwcForRgbToYuv;
235 uint64_t SurfaceFlinger::maxVirtualDisplaySize;
236 bool SurfaceFlinger::hasSyncFramework;
237 bool SurfaceFlinger::useVrFlinger;
238 int64_t SurfaceFlinger::maxFrameBufferAcquiredBuffers;
239 bool SurfaceFlinger::hasWideColorDisplay;
240 int SurfaceFlinger::primaryDisplayOrientation = DisplayState::eOrientationDefault;
241 bool SurfaceFlinger::useColorManagement;
242 bool SurfaceFlinger::useContextPriority;
243 Dataspace SurfaceFlinger::defaultCompositionDataspace = Dataspace::V0_SRGB;
244 ui::PixelFormat SurfaceFlinger::defaultCompositionPixelFormat = ui::PixelFormat::RGBA_8888;
245 Dataspace SurfaceFlinger::wideColorGamutCompositionDataspace = Dataspace::V0_SRGB;
246 ui::PixelFormat SurfaceFlinger::wideColorGamutCompositionPixelFormat = ui::PixelFormat::RGBA_8888;
247
getHwcServiceName()248 std::string getHwcServiceName() {
249 char value[PROPERTY_VALUE_MAX] = {};
250 property_get("debug.sf.hwc_service_name", value, "default");
251 ALOGI("Using HWComposer service: '%s'", value);
252 return std::string(value);
253 }
254
useTrebleTestingOverride()255 bool useTrebleTestingOverride() {
256 char value[PROPERTY_VALUE_MAX] = {};
257 property_get("debug.sf.treble_testing_override", value, "false");
258 ALOGI("Treble testing override: '%s'", value);
259 return std::string(value) == "true";
260 }
261
decodeDisplayColorSetting(DisplayColorSetting displayColorSetting)262 std::string decodeDisplayColorSetting(DisplayColorSetting displayColorSetting) {
263 switch(displayColorSetting) {
264 case DisplayColorSetting::MANAGED:
265 return std::string("Managed");
266 case DisplayColorSetting::UNMANAGED:
267 return std::string("Unmanaged");
268 case DisplayColorSetting::ENHANCED:
269 return std::string("Enhanced");
270 default:
271 return std::string("Unknown ") +
272 std::to_string(static_cast<int>(displayColorSetting));
273 }
274 }
275
SurfaceFlingerBE()276 SurfaceFlingerBE::SurfaceFlingerBE() : mHwcServiceName(getHwcServiceName()) {}
277
SurfaceFlinger(Factory & factory,SkipInitializationTag)278 SurfaceFlinger::SurfaceFlinger(Factory& factory, SkipInitializationTag)
279 : mFactory(factory),
280 mPhaseOffsets(mFactory.createPhaseOffsets()),
281 mInterceptor(mFactory.createSurfaceInterceptor(this)),
282 mTimeStats(mFactory.createTimeStats()),
283 mEventQueue(mFactory.createMessageQueue()),
284 mCompositionEngine(mFactory.createCompositionEngine()) {}
285
SurfaceFlinger(Factory & factory)286 SurfaceFlinger::SurfaceFlinger(Factory& factory) : SurfaceFlinger(factory, SkipInitialization) {
287 ALOGI("SurfaceFlinger is starting");
288
289 hasSyncFramework = running_without_sync_framework(true);
290
291 dispSyncPresentTimeOffset = present_time_offset_from_vsync_ns(0);
292
293 useHwcForRgbToYuv = force_hwc_copy_for_virtual_displays(false);
294
295 maxVirtualDisplaySize = max_virtual_display_dimension(0);
296
297 // Vr flinger is only enabled on Daydream ready devices.
298 useVrFlinger = use_vr_flinger(false);
299
300 maxFrameBufferAcquiredBuffers = max_frame_buffer_acquired_buffers(2);
301
302 hasWideColorDisplay = has_wide_color_display(false);
303
304 useColorManagement = use_color_management(false);
305
306 mDefaultCompositionDataspace =
307 static_cast<ui::Dataspace>(default_composition_dataspace(Dataspace::V0_SRGB));
308 mWideColorGamutCompositionDataspace = static_cast<ui::Dataspace>(wcg_composition_dataspace(
309 hasWideColorDisplay ? Dataspace::DISPLAY_P3 : Dataspace::V0_SRGB));
310 defaultCompositionDataspace = mDefaultCompositionDataspace;
311 wideColorGamutCompositionDataspace = mWideColorGamutCompositionDataspace;
312 defaultCompositionPixelFormat = static_cast<ui::PixelFormat>(
313 default_composition_pixel_format(ui::PixelFormat::RGBA_8888));
314 wideColorGamutCompositionPixelFormat =
315 static_cast<ui::PixelFormat>(wcg_composition_pixel_format(ui::PixelFormat::RGBA_8888));
316
317 mColorSpaceAgnosticDataspace =
318 static_cast<ui::Dataspace>(color_space_agnostic_dataspace(Dataspace::UNKNOWN));
319
320 useContextPriority = use_context_priority(true);
321
322 auto tmpPrimaryDisplayOrientation = primary_display_orientation(
323 SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_0);
324 switch (tmpPrimaryDisplayOrientation) {
325 case SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_90:
326 SurfaceFlinger::primaryDisplayOrientation = DisplayState::eOrientation90;
327 break;
328 case SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_180:
329 SurfaceFlinger::primaryDisplayOrientation = DisplayState::eOrientation180;
330 break;
331 case SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_270:
332 SurfaceFlinger::primaryDisplayOrientation = DisplayState::eOrientation270;
333 break;
334 default:
335 SurfaceFlinger::primaryDisplayOrientation = DisplayState::eOrientationDefault;
336 break;
337 }
338 ALOGV("Primary Display Orientation is set to %2d.", SurfaceFlinger::primaryDisplayOrientation);
339
340 mInternalDisplayPrimaries = sysprop::getDisplayNativePrimaries();
341
342 // debugging stuff...
343 char value[PROPERTY_VALUE_MAX];
344
345 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
346 mGpuToCpuSupported = !atoi(value);
347
348 property_get("debug.sf.showupdates", value, "0");
349 mDebugRegion = atoi(value);
350
351 ALOGI_IF(mDebugRegion, "showupdates enabled");
352
353 // DDMS debugging deprecated (b/120782499)
354 property_get("debug.sf.ddms", value, "0");
355 int debugDdms = atoi(value);
356 ALOGI_IF(debugDdms, "DDMS debugging not supported");
357
358 property_get("debug.sf.disable_backpressure", value, "0");
359 mPropagateBackpressure = !atoi(value);
360 ALOGI_IF(!mPropagateBackpressure, "Disabling backpressure propagation");
361
362 property_get("debug.sf.enable_gl_backpressure", value, "0");
363 mPropagateBackpressureClientComposition = atoi(value);
364 ALOGI_IF(mPropagateBackpressureClientComposition,
365 "Enabling backpressure propagation for Client Composition");
366
367 property_get("debug.sf.enable_hwc_vds", value, "0");
368 mUseHwcVirtualDisplays = atoi(value);
369 ALOGI_IF(mUseHwcVirtualDisplays, "Enabling HWC virtual displays");
370
371 property_get("ro.sf.disable_triple_buffer", value, "0");
372 mLayerTripleBufferingDisabled = atoi(value);
373 ALOGI_IF(mLayerTripleBufferingDisabled, "Disabling Triple Buffering");
374
375 const size_t defaultListSize = MAX_LAYERS;
376 auto listSize = property_get_int32("debug.sf.max_igbp_list_size", int32_t(defaultListSize));
377 mMaxGraphicBufferProducerListSize = (listSize > 0) ? size_t(listSize) : defaultListSize;
378
379 mUseSmart90ForVideo = use_smart_90_for_video(false);
380 property_get("debug.sf.use_smart_90_for_video", value, "0");
381
382 int int_value = atoi(value);
383 if (int_value) {
384 mUseSmart90ForVideo = true;
385 }
386
387 property_get("debug.sf.luma_sampling", value, "1");
388 mLumaSampling = atoi(value);
389
390 const auto [early, gl, late] = mPhaseOffsets->getCurrentOffsets();
391 mVsyncModulator.setPhaseOffsets(early, gl, late,
392 mPhaseOffsets->getOffsetThresholdForNextVsync());
393
394 // We should be reading 'persist.sys.sf.color_saturation' here
395 // but since /data may be encrypted, we need to wait until after vold
396 // comes online to attempt to read the property. The property is
397 // instead read after the boot animation
398
399 if (useTrebleTestingOverride()) {
400 // Without the override SurfaceFlinger cannot connect to HIDL
401 // services that are not listed in the manifests. Considered
402 // deriving the setting from the set service name, but it
403 // would be brittle if the name that's not 'default' is used
404 // for production purposes later on.
405 android::hardware::details::setTrebleTestingOverride(true);
406 }
407 }
408
onFirstRef()409 void SurfaceFlinger::onFirstRef()
410 {
411 mEventQueue->init(this);
412 }
413
414 SurfaceFlinger::~SurfaceFlinger() = default;
415
binderDied(const wp<IBinder> &)416 void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
417 {
418 // the window manager died on us. prepare its eulogy.
419
420 // restore initial conditions (default device unblank, etc)
421 initializeDisplays();
422
423 // restart the boot-animation
424 startBootAnim();
425 }
426
initClient(const sp<Client> & client)427 static sp<ISurfaceComposerClient> initClient(const sp<Client>& client) {
428 status_t err = client->initCheck();
429 if (err == NO_ERROR) {
430 return client;
431 }
432 return nullptr;
433 }
434
createConnection()435 sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() {
436 return initClient(new Client(this));
437 }
438
createDisplay(const String8 & displayName,bool secure)439 sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
440 bool secure)
441 {
442 class DisplayToken : public BBinder {
443 sp<SurfaceFlinger> flinger;
444 virtual ~DisplayToken() {
445 // no more references, this display must be terminated
446 Mutex::Autolock _l(flinger->mStateLock);
447 flinger->mCurrentState.displays.removeItem(this);
448 flinger->setTransactionFlags(eDisplayTransactionNeeded);
449 }
450 public:
451 explicit DisplayToken(const sp<SurfaceFlinger>& flinger)
452 : flinger(flinger) {
453 }
454 };
455
456 sp<BBinder> token = new DisplayToken(this);
457
458 Mutex::Autolock _l(mStateLock);
459 // Display ID is assigned when virtual display is allocated by HWC.
460 DisplayDeviceState state;
461 state.isSecure = secure;
462 state.displayName = displayName;
463 mCurrentState.displays.add(token, state);
464 mInterceptor->saveDisplayCreation(state);
465 return token;
466 }
467
destroyDisplay(const sp<IBinder> & displayToken)468 void SurfaceFlinger::destroyDisplay(const sp<IBinder>& displayToken) {
469 Mutex::Autolock _l(mStateLock);
470
471 ssize_t index = mCurrentState.displays.indexOfKey(displayToken);
472 if (index < 0) {
473 ALOGE("destroyDisplay: Invalid display token %p", displayToken.get());
474 return;
475 }
476
477 const DisplayDeviceState& state = mCurrentState.displays.valueAt(index);
478 if (!state.isVirtual()) {
479 ALOGE("destroyDisplay called for non-virtual display");
480 return;
481 }
482 mInterceptor->saveDisplayDeletion(state.sequenceId);
483 mCurrentState.displays.removeItemsAt(index);
484 setTransactionFlags(eDisplayTransactionNeeded);
485 }
486
getPhysicalDisplayIds() const487 std::vector<PhysicalDisplayId> SurfaceFlinger::getPhysicalDisplayIds() const {
488 Mutex::Autolock lock(mStateLock);
489
490 const auto internalDisplayId = getInternalDisplayIdLocked();
491 if (!internalDisplayId) {
492 return {};
493 }
494
495 std::vector<PhysicalDisplayId> displayIds;
496 displayIds.reserve(mPhysicalDisplayTokens.size());
497 displayIds.push_back(internalDisplayId->value);
498
499 for (const auto& [id, token] : mPhysicalDisplayTokens) {
500 if (id != *internalDisplayId) {
501 displayIds.push_back(id.value);
502 }
503 }
504
505 return displayIds;
506 }
507
getPhysicalDisplayToken(PhysicalDisplayId displayId) const508 sp<IBinder> SurfaceFlinger::getPhysicalDisplayToken(PhysicalDisplayId displayId) const {
509 Mutex::Autolock lock(mStateLock);
510 return getPhysicalDisplayTokenLocked(DisplayId{displayId});
511 }
512
getColorManagement(bool * outGetColorManagement) const513 status_t SurfaceFlinger::getColorManagement(bool* outGetColorManagement) const {
514 if (!outGetColorManagement) {
515 return BAD_VALUE;
516 }
517 *outGetColorManagement = useColorManagement;
518 return NO_ERROR;
519 }
520
getHwComposer() const521 HWComposer& SurfaceFlinger::getHwComposer() const {
522 return mCompositionEngine->getHwComposer();
523 }
524
getRenderEngine() const525 renderengine::RenderEngine& SurfaceFlinger::getRenderEngine() const {
526 return mCompositionEngine->getRenderEngine();
527 }
528
getCompositionEngine() const529 compositionengine::CompositionEngine& SurfaceFlinger::getCompositionEngine() const {
530 return *mCompositionEngine.get();
531 }
532
bootFinished()533 void SurfaceFlinger::bootFinished()
534 {
535 if (mStartPropertySetThread->join() != NO_ERROR) {
536 ALOGE("Join StartPropertySetThread failed!");
537 }
538 const nsecs_t now = systemTime();
539 const nsecs_t duration = now - mBootTime;
540 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
541
542 // wait patiently for the window manager death
543 const String16 name("window");
544 mWindowManager = defaultServiceManager()->getService(name);
545 if (mWindowManager != 0) {
546 mWindowManager->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
547 }
548 sp<IBinder> input(defaultServiceManager()->getService(
549 String16("inputflinger")));
550 if (input == nullptr) {
551 ALOGE("Failed to link to input service");
552 } else {
553 mInputFlinger = interface_cast<IInputFlinger>(input);
554 }
555
556 if (mVrFlinger) {
557 mVrFlinger->OnBootFinished();
558 }
559
560 // stop boot animation
561 // formerly we would just kill the process, but we now ask it to exit so it
562 // can choose where to stop the animation.
563 property_set("service.bootanim.exit", "1");
564
565 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
566 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
567 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
568
569 postMessageAsync(new LambdaMessage([this]() NO_THREAD_SAFETY_ANALYSIS {
570 readPersistentProperties();
571 mBootStage = BootStage::FINISHED;
572
573 if (mRefreshRateConfigs->refreshRateSwitchingSupported()) {
574 // set the refresh rate according to the policy
575 const auto& performanceRefreshRate =
576 mRefreshRateConfigs->getRefreshRateFromType(RefreshRateType::PERFORMANCE);
577
578 if (isDisplayConfigAllowed(performanceRefreshRate.configId)) {
579 setRefreshRateTo(RefreshRateType::PERFORMANCE, Scheduler::ConfigEvent::Changed);
580 } else {
581 setRefreshRateTo(RefreshRateType::DEFAULT, Scheduler::ConfigEvent::Changed);
582 }
583 }
584 }));
585 }
586
getNewTexture()587 uint32_t SurfaceFlinger::getNewTexture() {
588 {
589 std::lock_guard lock(mTexturePoolMutex);
590 if (!mTexturePool.empty()) {
591 uint32_t name = mTexturePool.back();
592 mTexturePool.pop_back();
593 ATRACE_INT("TexturePoolSize", mTexturePool.size());
594 return name;
595 }
596
597 // The pool was too small, so increase it for the future
598 ++mTexturePoolSize;
599 }
600
601 // The pool was empty, so we need to get a new texture name directly using a
602 // blocking call to the main thread
603 uint32_t name = 0;
604 postMessageSync(new LambdaMessage([&]() { getRenderEngine().genTextures(1, &name); }));
605 return name;
606 }
607
deleteTextureAsync(uint32_t texture)608 void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
609 std::lock_guard lock(mTexturePoolMutex);
610 // We don't change the pool size, so the fix-up logic in postComposition will decide whether
611 // to actually delete this or not based on mTexturePoolSize
612 mTexturePool.push_back(texture);
613 ATRACE_INT("TexturePoolSize", mTexturePool.size());
614 }
615
616 // Do not call property_set on main thread which will be blocked by init
617 // Use StartPropertySetThread instead.
init()618 void SurfaceFlinger::init() {
619 ALOGI( "SurfaceFlinger's main thread ready to run. "
620 "Initializing graphics H/W...");
621
622 ALOGI("Phase offset NS: %" PRId64 "", mPhaseOffsets->getCurrentAppOffset());
623
624 Mutex::Autolock _l(mStateLock);
625
626 // Get a RenderEngine for the given display / config (can't fail)
627 int32_t renderEngineFeature = 0;
628 renderEngineFeature |= (useColorManagement ?
629 renderengine::RenderEngine::USE_COLOR_MANAGEMENT : 0);
630 renderEngineFeature |= (useContextPriority ?
631 renderengine::RenderEngine::USE_HIGH_PRIORITY_CONTEXT : 0);
632 renderEngineFeature |=
633 (enable_protected_contents(false) ? renderengine::RenderEngine::ENABLE_PROTECTED_CONTEXT
634 : 0);
635
636 // TODO(b/77156734): We need to stop casting and use HAL types when possible.
637 // Sending maxFrameBufferAcquiredBuffers as the cache size is tightly tuned to single-display.
638 mCompositionEngine->setRenderEngine(
639 renderengine::RenderEngine::create(static_cast<int32_t>(defaultCompositionPixelFormat),
640 renderEngineFeature, maxFrameBufferAcquiredBuffers));
641
642 LOG_ALWAYS_FATAL_IF(mVrFlingerRequestsDisplay,
643 "Starting with vr flinger active is not currently supported.");
644 mCompositionEngine->setHwComposer(getFactory().createHWComposer(getBE().mHwcServiceName));
645 mCompositionEngine->getHwComposer().registerCallback(this, getBE().mComposerSequenceId);
646 // Process any initial hotplug and resulting display changes.
647 processDisplayHotplugEventsLocked();
648 const auto display = getDefaultDisplayDeviceLocked();
649 LOG_ALWAYS_FATAL_IF(!display, "Missing internal display after registering composer callback.");
650 LOG_ALWAYS_FATAL_IF(!getHwComposer().isConnected(*display->getId()),
651 "Internal display is disconnected.");
652
653 if (useVrFlinger) {
654 auto vrFlingerRequestDisplayCallback = [this](bool requestDisplay) {
655 // This callback is called from the vr flinger dispatch thread. We
656 // need to call signalTransaction(), which requires holding
657 // mStateLock when we're not on the main thread. Acquiring
658 // mStateLock from the vr flinger dispatch thread might trigger a
659 // deadlock in surface flinger (see b/66916578), so post a message
660 // to be handled on the main thread instead.
661 postMessageAsync(new LambdaMessage([=] {
662 ALOGI("VR request display mode: requestDisplay=%d", requestDisplay);
663 mVrFlingerRequestsDisplay = requestDisplay;
664 signalTransaction();
665 }));
666 };
667 mVrFlinger = dvr::VrFlinger::Create(getHwComposer().getComposer(),
668 getHwComposer()
669 .fromPhysicalDisplayId(*display->getId())
670 .value_or(0),
671 vrFlingerRequestDisplayCallback);
672 if (!mVrFlinger) {
673 ALOGE("Failed to start vrflinger");
674 }
675 }
676
677 // initialize our drawing state
678 mDrawingState = mCurrentState;
679
680 // set initial conditions (e.g. unblank default device)
681 initializeDisplays();
682
683 getRenderEngine().primeCache();
684
685 // Inform native graphics APIs whether the present timestamp is supported:
686
687 const bool presentFenceReliable =
688 !getHwComposer().hasCapability(HWC2::Capability::PresentFenceIsNotReliable);
689 mStartPropertySetThread = getFactory().createStartPropertySetThread(presentFenceReliable);
690
691 if (mStartPropertySetThread->Start() != NO_ERROR) {
692 ALOGE("Run StartPropertySetThread failed!");
693 }
694
695 ALOGV("Done initializing");
696 }
697
readPersistentProperties()698 void SurfaceFlinger::readPersistentProperties() {
699 Mutex::Autolock _l(mStateLock);
700
701 char value[PROPERTY_VALUE_MAX];
702
703 property_get("persist.sys.sf.color_saturation", value, "1.0");
704 mGlobalSaturationFactor = atof(value);
705 updateColorMatrixLocked();
706 ALOGV("Saturation is set to %.2f", mGlobalSaturationFactor);
707
708 property_get("persist.sys.sf.native_mode", value, "0");
709 mDisplayColorSetting = static_cast<DisplayColorSetting>(atoi(value));
710
711 property_get("persist.sys.sf.color_mode", value, "0");
712 mForceColorMode = static_cast<ColorMode>(atoi(value));
713 }
714
startBootAnim()715 void SurfaceFlinger::startBootAnim() {
716 // Start boot animation service by setting a property mailbox
717 // if property setting thread is already running, Start() will be just a NOP
718 mStartPropertySetThread->Start();
719 // Wait until property was set
720 if (mStartPropertySetThread->join() != NO_ERROR) {
721 ALOGE("Join StartPropertySetThread failed!");
722 }
723 }
724
getMaxTextureSize() const725 size_t SurfaceFlinger::getMaxTextureSize() const {
726 return getRenderEngine().getMaxTextureSize();
727 }
728
getMaxViewportDims() const729 size_t SurfaceFlinger::getMaxViewportDims() const {
730 return getRenderEngine().getMaxViewportDims();
731 }
732
733 // ----------------------------------------------------------------------------
734
authenticateSurfaceTexture(const sp<IGraphicBufferProducer> & bufferProducer) const735 bool SurfaceFlinger::authenticateSurfaceTexture(
736 const sp<IGraphicBufferProducer>& bufferProducer) const {
737 Mutex::Autolock _l(mStateLock);
738 return authenticateSurfaceTextureLocked(bufferProducer);
739 }
740
authenticateSurfaceTextureLocked(const sp<IGraphicBufferProducer> & bufferProducer) const741 bool SurfaceFlinger::authenticateSurfaceTextureLocked(
742 const sp<IGraphicBufferProducer>& bufferProducer) const {
743 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
744 return mGraphicBufferProducerList.count(surfaceTextureBinder.get()) > 0;
745 }
746
getSupportedFrameTimestamps(std::vector<FrameEvent> * outSupported) const747 status_t SurfaceFlinger::getSupportedFrameTimestamps(
748 std::vector<FrameEvent>* outSupported) const {
749 *outSupported = {
750 FrameEvent::REQUESTED_PRESENT,
751 FrameEvent::ACQUIRE,
752 FrameEvent::LATCH,
753 FrameEvent::FIRST_REFRESH_START,
754 FrameEvent::LAST_REFRESH_START,
755 FrameEvent::GPU_COMPOSITION_DONE,
756 FrameEvent::DEQUEUE_READY,
757 FrameEvent::RELEASE,
758 };
759 ConditionalLock _l(mStateLock,
760 std::this_thread::get_id() != mMainThreadId);
761 if (!getHwComposer().hasCapability(
762 HWC2::Capability::PresentFenceIsNotReliable)) {
763 outSupported->push_back(FrameEvent::DISPLAY_PRESENT);
764 }
765 return NO_ERROR;
766 }
767
getDisplayConfigs(const sp<IBinder> & displayToken,Vector<DisplayInfo> * configs)768 status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& displayToken,
769 Vector<DisplayInfo>* configs) {
770 if (!displayToken || !configs) {
771 return BAD_VALUE;
772 }
773
774 Mutex::Autolock lock(mStateLock);
775
776 const auto displayId = getPhysicalDisplayIdLocked(displayToken);
777 if (!displayId) {
778 return NAME_NOT_FOUND;
779 }
780
781 // TODO: Not sure if display density should handled by SF any longer
782 class Density {
783 static float getDensityFromProperty(char const* propName) {
784 char property[PROPERTY_VALUE_MAX];
785 float density = 0.0f;
786 if (property_get(propName, property, nullptr) > 0) {
787 density = strtof(property, nullptr);
788 }
789 return density;
790 }
791 public:
792 static float getEmuDensity() {
793 return getDensityFromProperty("qemu.sf.lcd_density"); }
794 static float getBuildDensity() {
795 return getDensityFromProperty("ro.sf.lcd_density"); }
796 };
797
798 configs->clear();
799
800 for (const auto& hwConfig : getHwComposer().getConfigs(*displayId)) {
801 DisplayInfo info = DisplayInfo();
802
803 float xdpi = hwConfig->getDpiX();
804 float ydpi = hwConfig->getDpiY();
805
806 info.w = hwConfig->getWidth();
807 info.h = hwConfig->getHeight();
808 // Default display viewport to display width and height
809 info.viewportW = info.w;
810 info.viewportH = info.h;
811
812 if (displayId == getInternalDisplayIdLocked()) {
813 // The density of the device is provided by a build property
814 float density = Density::getBuildDensity() / 160.0f;
815 if (density == 0) {
816 // the build doesn't provide a density -- this is wrong!
817 // use xdpi instead
818 ALOGE("ro.sf.lcd_density must be defined as a build property");
819 density = xdpi / 160.0f;
820 }
821 if (Density::getEmuDensity()) {
822 // if "qemu.sf.lcd_density" is specified, it overrides everything
823 xdpi = ydpi = density = Density::getEmuDensity();
824 density /= 160.0f;
825 }
826 info.density = density;
827
828 // TODO: this needs to go away (currently needed only by webkit)
829 const auto display = getDefaultDisplayDeviceLocked();
830 info.orientation = display ? display->getOrientation() : 0;
831
832 // This is for screenrecord
833 const Rect viewport = display->getViewport();
834 if (viewport.isValid()) {
835 info.viewportW = uint32_t(viewport.getWidth());
836 info.viewportH = uint32_t(viewport.getHeight());
837 }
838 } else {
839 // TODO: where should this value come from?
840 static const int TV_DENSITY = 213;
841 info.density = TV_DENSITY / 160.0f;
842 info.orientation = 0;
843 }
844
845 info.xdpi = xdpi;
846 info.ydpi = ydpi;
847 info.fps = 1e9 / hwConfig->getVsyncPeriod();
848 const auto refreshRateType =
849 mRefreshRateConfigs->getRefreshRateTypeFromHwcConfigId(hwConfig->getId());
850 const auto offset = mPhaseOffsets->getOffsetsForRefreshRate(refreshRateType);
851 info.appVsyncOffset = offset.late.app;
852
853 // This is how far in advance a buffer must be queued for
854 // presentation at a given time. If you want a buffer to appear
855 // on the screen at time N, you must submit the buffer before
856 // (N - presentationDeadline).
857 //
858 // Normally it's one full refresh period (to give SF a chance to
859 // latch the buffer), but this can be reduced by configuring a
860 // DispSync offset. Any additional delays introduced by the hardware
861 // composer or panel must be accounted for here.
862 //
863 // We add an additional 1ms to allow for processing time and
864 // differences between the ideal and actual refresh rate.
865 info.presentationDeadline = hwConfig->getVsyncPeriod() - offset.late.sf + 1000000;
866
867 // All non-virtual displays are currently considered secure.
868 info.secure = true;
869
870 if (displayId == getInternalDisplayIdLocked() &&
871 primaryDisplayOrientation & DisplayState::eOrientationSwapMask) {
872 std::swap(info.w, info.h);
873 }
874
875 configs->push_back(info);
876 }
877
878 return NO_ERROR;
879 }
880
getDisplayStats(const sp<IBinder> &,DisplayStatInfo * stats)881 status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>&, DisplayStatInfo* stats) {
882 if (!stats) {
883 return BAD_VALUE;
884 }
885
886 mScheduler->getDisplayStatInfo(stats);
887 return NO_ERROR;
888 }
889
getActiveConfig(const sp<IBinder> & displayToken)890 int SurfaceFlinger::getActiveConfig(const sp<IBinder>& displayToken) {
891 const auto display = getDisplayDevice(displayToken);
892 if (!display) {
893 ALOGE("getActiveConfig: Invalid display token %p", displayToken.get());
894 return BAD_VALUE;
895 }
896
897 return display->getActiveConfig();
898 }
899
setDesiredActiveConfig(const ActiveConfigInfo & info)900 void SurfaceFlinger::setDesiredActiveConfig(const ActiveConfigInfo& info) {
901 ATRACE_CALL();
902
903 // Don't check against the current mode yet. Worst case we set the desired
904 // config twice. However event generation config might have changed so we need to update it
905 // accordingly
906 std::lock_guard<std::mutex> lock(mActiveConfigLock);
907 const Scheduler::ConfigEvent prevConfig = mDesiredActiveConfig.event;
908 mDesiredActiveConfig = info;
909 mDesiredActiveConfig.event = mDesiredActiveConfig.event | prevConfig;
910
911 if (!mDesiredActiveConfigChanged) {
912 // This will trigger HWC refresh without resetting the idle timer.
913 repaintEverythingForHWC();
914 // Start receiving vsync samples now, so that we can detect a period
915 // switch.
916 mScheduler->resyncToHardwareVsync(true, getVsyncPeriod());
917 // As we called to set period, we will call to onRefreshRateChangeCompleted once
918 // DispSync model is locked.
919 mVsyncModulator.onRefreshRateChangeInitiated();
920 mPhaseOffsets->setRefreshRateType(info.type);
921 const auto [early, gl, late] = mPhaseOffsets->getCurrentOffsets();
922 mVsyncModulator.setPhaseOffsets(early, gl, late,
923 mPhaseOffsets->getOffsetThresholdForNextVsync());
924 }
925 mDesiredActiveConfigChanged = true;
926 ATRACE_INT("DesiredActiveConfigChanged", mDesiredActiveConfigChanged);
927
928 if (mRefreshRateOverlay) {
929 mRefreshRateOverlay->changeRefreshRate(mDesiredActiveConfig.type);
930 }
931 }
932
setActiveConfig(const sp<IBinder> & displayToken,int mode)933 status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& displayToken, int mode) {
934 ATRACE_CALL();
935
936 std::vector<int32_t> allowedConfig;
937 allowedConfig.push_back(mode);
938
939 return setAllowedDisplayConfigs(displayToken, allowedConfig);
940 }
941
setActiveConfigInternal()942 void SurfaceFlinger::setActiveConfigInternal() {
943 ATRACE_CALL();
944
945 const auto display = getDefaultDisplayDeviceLocked();
946 if (!display) {
947 return;
948 }
949
950 std::lock_guard<std::mutex> lock(mActiveConfigLock);
951 mRefreshRateConfigs->setCurrentConfig(mUpcomingActiveConfig.configId);
952 mRefreshRateStats->setConfigMode(mUpcomingActiveConfig.configId);
953
954 display->setActiveConfig(mUpcomingActiveConfig.configId);
955
956 mPhaseOffsets->setRefreshRateType(mUpcomingActiveConfig.type);
957 const auto [early, gl, late] = mPhaseOffsets->getCurrentOffsets();
958 mVsyncModulator.setPhaseOffsets(early, gl, late,
959 mPhaseOffsets->getOffsetThresholdForNextVsync());
960 ATRACE_INT("ActiveConfigMode", mUpcomingActiveConfig.configId);
961
962 if (mUpcomingActiveConfig.event != Scheduler::ConfigEvent::None) {
963 mScheduler->onConfigChanged(mAppConnectionHandle, display->getId()->value,
964 mUpcomingActiveConfig.configId);
965 }
966 }
967
desiredActiveConfigChangeDone()968 void SurfaceFlinger::desiredActiveConfigChangeDone() {
969 std::lock_guard<std::mutex> lock(mActiveConfigLock);
970 mDesiredActiveConfig.event = Scheduler::ConfigEvent::None;
971 mDesiredActiveConfigChanged = false;
972 ATRACE_INT("DesiredActiveConfigChanged", mDesiredActiveConfigChanged);
973
974 mScheduler->resyncToHardwareVsync(true, getVsyncPeriod());
975 mPhaseOffsets->setRefreshRateType(mUpcomingActiveConfig.type);
976 const auto [early, gl, late] = mPhaseOffsets->getCurrentOffsets();
977 mVsyncModulator.setPhaseOffsets(early, gl, late,
978 mPhaseOffsets->getOffsetThresholdForNextVsync());
979 }
980
performSetActiveConfig()981 bool SurfaceFlinger::performSetActiveConfig() {
982 ATRACE_CALL();
983 if (mCheckPendingFence) {
984 if (previousFrameMissed()) {
985 // fence has not signaled yet. wait for the next invalidate
986 mEventQueue->invalidate();
987 return true;
988 }
989
990 // We received the present fence from the HWC, so we assume it successfully updated
991 // the config, hence we update SF.
992 mCheckPendingFence = false;
993 setActiveConfigInternal();
994 }
995
996 // Store the local variable to release the lock.
997 ActiveConfigInfo desiredActiveConfig;
998 {
999 std::lock_guard<std::mutex> lock(mActiveConfigLock);
1000 if (!mDesiredActiveConfigChanged) {
1001 return false;
1002 }
1003 desiredActiveConfig = mDesiredActiveConfig;
1004 }
1005
1006 const auto display = getDefaultDisplayDeviceLocked();
1007 if (!display || display->getActiveConfig() == desiredActiveConfig.configId) {
1008 // display is not valid or we are already in the requested mode
1009 // on both cases there is nothing left to do
1010 desiredActiveConfigChangeDone();
1011 return false;
1012 }
1013
1014 // Desired active config was set, it is different than the config currently in use, however
1015 // allowed configs might have change by the time we process the refresh.
1016 // Make sure the desired config is still allowed
1017 if (!isDisplayConfigAllowed(desiredActiveConfig.configId)) {
1018 desiredActiveConfigChangeDone();
1019 return false;
1020 }
1021
1022 mUpcomingActiveConfig = desiredActiveConfig;
1023 const auto displayId = display->getId();
1024 LOG_ALWAYS_FATAL_IF(!displayId);
1025
1026 ATRACE_INT("ActiveConfigModeHWC", mUpcomingActiveConfig.configId);
1027 getHwComposer().setActiveConfig(*displayId, mUpcomingActiveConfig.configId);
1028
1029 // we need to submit an empty frame to HWC to start the process
1030 mCheckPendingFence = true;
1031 mEventQueue->invalidate();
1032 return false;
1033 }
1034
getDisplayColorModes(const sp<IBinder> & displayToken,Vector<ColorMode> * outColorModes)1035 status_t SurfaceFlinger::getDisplayColorModes(const sp<IBinder>& displayToken,
1036 Vector<ColorMode>* outColorModes) {
1037 if (!displayToken || !outColorModes) {
1038 return BAD_VALUE;
1039 }
1040
1041 std::vector<ColorMode> modes;
1042 bool isInternalDisplay = false;
1043 {
1044 ConditionalLock lock(mStateLock, std::this_thread::get_id() != mMainThreadId);
1045
1046 const auto displayId = getPhysicalDisplayIdLocked(displayToken);
1047 if (!displayId) {
1048 return NAME_NOT_FOUND;
1049 }
1050
1051 modes = getHwComposer().getColorModes(*displayId);
1052 isInternalDisplay = displayId == getInternalDisplayIdLocked();
1053 }
1054 outColorModes->clear();
1055
1056 // If it's built-in display and the configuration claims it's not wide color capable,
1057 // filter out all wide color modes. The typical reason why this happens is that the
1058 // hardware is not good enough to support GPU composition of wide color, and thus the
1059 // OEMs choose to disable this capability.
1060 if (isInternalDisplay && !hasWideColorDisplay) {
1061 std::remove_copy_if(modes.cbegin(), modes.cend(), std::back_inserter(*outColorModes),
1062 isWideColorMode);
1063 } else {
1064 std::copy(modes.cbegin(), modes.cend(), std::back_inserter(*outColorModes));
1065 }
1066
1067 return NO_ERROR;
1068 }
1069
getDisplayNativePrimaries(const sp<IBinder> & displayToken,ui::DisplayPrimaries & primaries)1070 status_t SurfaceFlinger::getDisplayNativePrimaries(const sp<IBinder>& displayToken,
1071 ui::DisplayPrimaries &primaries) {
1072 if (!displayToken) {
1073 return BAD_VALUE;
1074 }
1075
1076 // Currently we only support this API for a single internal display.
1077 if (getInternalDisplayToken() != displayToken) {
1078 return BAD_VALUE;
1079 }
1080
1081 memcpy(&primaries, &mInternalDisplayPrimaries, sizeof(ui::DisplayPrimaries));
1082 return NO_ERROR;
1083 }
1084
getActiveColorMode(const sp<IBinder> & displayToken)1085 ColorMode SurfaceFlinger::getActiveColorMode(const sp<IBinder>& displayToken) {
1086 if (const auto display = getDisplayDevice(displayToken)) {
1087 return display->getCompositionDisplay()->getState().colorMode;
1088 }
1089 return static_cast<ColorMode>(BAD_VALUE);
1090 }
1091
setActiveColorMode(const sp<IBinder> & displayToken,ColorMode mode)1092 status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& displayToken, ColorMode mode) {
1093 postMessageSync(new LambdaMessage([&] {
1094 Vector<ColorMode> modes;
1095 getDisplayColorModes(displayToken, &modes);
1096 bool exists = std::find(std::begin(modes), std::end(modes), mode) != std::end(modes);
1097 if (mode < ColorMode::NATIVE || !exists) {
1098 ALOGE("Attempt to set invalid active color mode %s (%d) for display token %p",
1099 decodeColorMode(mode).c_str(), mode, displayToken.get());
1100 return;
1101 }
1102 const auto display = getDisplayDevice(displayToken);
1103 if (!display) {
1104 ALOGE("Attempt to set active color mode %s (%d) for invalid display token %p",
1105 decodeColorMode(mode).c_str(), mode, displayToken.get());
1106 } else if (display->isVirtual()) {
1107 ALOGW("Attempt to set active color mode %s (%d) for virtual display",
1108 decodeColorMode(mode).c_str(), mode);
1109 } else {
1110 display->getCompositionDisplay()->setColorMode(mode, Dataspace::UNKNOWN,
1111 RenderIntent::COLORIMETRIC);
1112 }
1113 }));
1114
1115 return NO_ERROR;
1116 }
1117
clearAnimationFrameStats()1118 status_t SurfaceFlinger::clearAnimationFrameStats() {
1119 Mutex::Autolock _l(mStateLock);
1120 mAnimFrameTracker.clearStats();
1121 return NO_ERROR;
1122 }
1123
getAnimationFrameStats(FrameStats * outStats) const1124 status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
1125 Mutex::Autolock _l(mStateLock);
1126 mAnimFrameTracker.getStats(outStats);
1127 return NO_ERROR;
1128 }
1129
getHdrCapabilities(const sp<IBinder> & displayToken,HdrCapabilities * outCapabilities) const1130 status_t SurfaceFlinger::getHdrCapabilities(const sp<IBinder>& displayToken,
1131 HdrCapabilities* outCapabilities) const {
1132 Mutex::Autolock _l(mStateLock);
1133
1134 const auto display = getDisplayDeviceLocked(displayToken);
1135 if (!display) {
1136 ALOGE("getHdrCapabilities: Invalid display token %p", displayToken.get());
1137 return BAD_VALUE;
1138 }
1139
1140 // At this point the DisplayDeivce should already be set up,
1141 // meaning the luminance information is already queried from
1142 // hardware composer and stored properly.
1143 const HdrCapabilities& capabilities = display->getHdrCapabilities();
1144 *outCapabilities = HdrCapabilities(capabilities.getSupportedHdrTypes(),
1145 capabilities.getDesiredMaxLuminance(),
1146 capabilities.getDesiredMaxAverageLuminance(),
1147 capabilities.getDesiredMinLuminance());
1148
1149 return NO_ERROR;
1150 }
1151
getDisplayedContentSamplingAttributes(const sp<IBinder> & displayToken,ui::PixelFormat * outFormat,ui::Dataspace * outDataspace,uint8_t * outComponentMask) const1152 status_t SurfaceFlinger::getDisplayedContentSamplingAttributes(const sp<IBinder>& displayToken,
1153 ui::PixelFormat* outFormat,
1154 ui::Dataspace* outDataspace,
1155 uint8_t* outComponentMask) const {
1156 if (!outFormat || !outDataspace || !outComponentMask) {
1157 return BAD_VALUE;
1158 }
1159 const auto display = getDisplayDevice(displayToken);
1160 if (!display || !display->getId()) {
1161 ALOGE("getDisplayedContentSamplingAttributes: Bad display token: %p", display.get());
1162 return BAD_VALUE;
1163 }
1164 return getHwComposer().getDisplayedContentSamplingAttributes(*display->getId(), outFormat,
1165 outDataspace, outComponentMask);
1166 }
1167
setDisplayContentSamplingEnabled(const sp<IBinder> & displayToken,bool enable,uint8_t componentMask,uint64_t maxFrames) const1168 status_t SurfaceFlinger::setDisplayContentSamplingEnabled(const sp<IBinder>& displayToken,
1169 bool enable, uint8_t componentMask,
1170 uint64_t maxFrames) const {
1171 const auto display = getDisplayDevice(displayToken);
1172 if (!display || !display->getId()) {
1173 ALOGE("setDisplayContentSamplingEnabled: Bad display token: %p", display.get());
1174 return BAD_VALUE;
1175 }
1176
1177 return getHwComposer().setDisplayContentSamplingEnabled(*display->getId(), enable,
1178 componentMask, maxFrames);
1179 }
1180
getDisplayedContentSample(const sp<IBinder> & displayToken,uint64_t maxFrames,uint64_t timestamp,DisplayedFrameStats * outStats) const1181 status_t SurfaceFlinger::getDisplayedContentSample(const sp<IBinder>& displayToken,
1182 uint64_t maxFrames, uint64_t timestamp,
1183 DisplayedFrameStats* outStats) const {
1184 const auto display = getDisplayDevice(displayToken);
1185 if (!display || !display->getId()) {
1186 ALOGE("getDisplayContentSample: Bad display token: %p", displayToken.get());
1187 return BAD_VALUE;
1188 }
1189
1190 return getHwComposer().getDisplayedContentSample(*display->getId(), maxFrames, timestamp,
1191 outStats);
1192 }
1193
getProtectedContentSupport(bool * outSupported) const1194 status_t SurfaceFlinger::getProtectedContentSupport(bool* outSupported) const {
1195 if (!outSupported) {
1196 return BAD_VALUE;
1197 }
1198 *outSupported = getRenderEngine().supportsProtectedContent();
1199 return NO_ERROR;
1200 }
1201
isWideColorDisplay(const sp<IBinder> & displayToken,bool * outIsWideColorDisplay) const1202 status_t SurfaceFlinger::isWideColorDisplay(const sp<IBinder>& displayToken,
1203 bool* outIsWideColorDisplay) const {
1204 if (!displayToken || !outIsWideColorDisplay) {
1205 return BAD_VALUE;
1206 }
1207 Mutex::Autolock _l(mStateLock);
1208 const auto display = getDisplayDeviceLocked(displayToken);
1209 if (!display) {
1210 return BAD_VALUE;
1211 }
1212
1213 // Use hasWideColorDisplay to override built-in display.
1214 const auto displayId = display->getId();
1215 if (displayId && displayId == getInternalDisplayIdLocked()) {
1216 *outIsWideColorDisplay = hasWideColorDisplay;
1217 return NO_ERROR;
1218 }
1219 *outIsWideColorDisplay = display->hasWideColorGamut();
1220 return NO_ERROR;
1221 }
1222
enableVSyncInjections(bool enable)1223 status_t SurfaceFlinger::enableVSyncInjections(bool enable) {
1224 postMessageSync(new LambdaMessage([&] {
1225 Mutex::Autolock _l(mStateLock);
1226
1227 if (mInjectVSyncs == enable) {
1228 return;
1229 }
1230
1231 // TODO(b/128863962): Part of the Injector should be refactored, so that it
1232 // can be passed to Scheduler.
1233 if (enable) {
1234 ALOGV("VSync Injections enabled");
1235 if (mVSyncInjector.get() == nullptr) {
1236 mVSyncInjector = std::make_unique<InjectVSyncSource>();
1237 mInjectorEventThread = std::make_unique<
1238 impl::EventThread>(mVSyncInjector.get(),
1239 impl::EventThread::InterceptVSyncsCallback(),
1240 "injEventThread");
1241 }
1242 mEventQueue->setEventThread(mInjectorEventThread.get(), [&] { mScheduler->resync(); });
1243 } else {
1244 ALOGV("VSync Injections disabled");
1245 mEventQueue->setEventThread(mScheduler->getEventThread(mSfConnectionHandle),
1246 [&] { mScheduler->resync(); });
1247 }
1248
1249 mInjectVSyncs = enable;
1250 }));
1251
1252 return NO_ERROR;
1253 }
1254
injectVSync(nsecs_t when)1255 status_t SurfaceFlinger::injectVSync(nsecs_t when) {
1256 Mutex::Autolock _l(mStateLock);
1257
1258 if (!mInjectVSyncs) {
1259 ALOGE("VSync Injections not enabled");
1260 return BAD_VALUE;
1261 }
1262 if (mInjectVSyncs && mInjectorEventThread.get() != nullptr) {
1263 ALOGV("Injecting VSync inside SurfaceFlinger");
1264 mVSyncInjector->onInjectSyncEvent(when);
1265 }
1266 return NO_ERROR;
1267 }
1268
getLayerDebugInfo(std::vector<LayerDebugInfo> * outLayers) const1269 status_t SurfaceFlinger::getLayerDebugInfo(std::vector<LayerDebugInfo>* outLayers) const
1270 NO_THREAD_SAFETY_ANALYSIS {
1271 // Try to acquire a lock for 1s, fail gracefully
1272 const status_t err = mStateLock.timedLock(s2ns(1));
1273 const bool locked = (err == NO_ERROR);
1274 if (!locked) {
1275 ALOGE("LayerDebugInfo: SurfaceFlinger unresponsive (%s [%d]) - exit", strerror(-err), err);
1276 return TIMED_OUT;
1277 }
1278
1279 outLayers->clear();
1280 mCurrentState.traverseInZOrder([&](Layer* layer) {
1281 outLayers->push_back(layer->getLayerDebugInfo());
1282 });
1283
1284 mStateLock.unlock();
1285 return NO_ERROR;
1286 }
1287
getCompositionPreference(Dataspace * outDataspace,ui::PixelFormat * outPixelFormat,Dataspace * outWideColorGamutDataspace,ui::PixelFormat * outWideColorGamutPixelFormat) const1288 status_t SurfaceFlinger::getCompositionPreference(
1289 Dataspace* outDataspace, ui::PixelFormat* outPixelFormat,
1290 Dataspace* outWideColorGamutDataspace,
1291 ui::PixelFormat* outWideColorGamutPixelFormat) const {
1292 *outDataspace = mDefaultCompositionDataspace;
1293 *outPixelFormat = defaultCompositionPixelFormat;
1294 *outWideColorGamutDataspace = mWideColorGamutCompositionDataspace;
1295 *outWideColorGamutPixelFormat = wideColorGamutCompositionPixelFormat;
1296 return NO_ERROR;
1297 }
1298
addRegionSamplingListener(const Rect & samplingArea,const sp<IBinder> & stopLayerHandle,const sp<IRegionSamplingListener> & listener)1299 status_t SurfaceFlinger::addRegionSamplingListener(const Rect& samplingArea,
1300 const sp<IBinder>& stopLayerHandle,
1301 const sp<IRegionSamplingListener>& listener) {
1302 if (!listener || samplingArea == Rect::INVALID_RECT) {
1303 return BAD_VALUE;
1304 }
1305 mRegionSamplingThread->addListener(samplingArea, stopLayerHandle, listener);
1306 return NO_ERROR;
1307 }
1308
removeRegionSamplingListener(const sp<IRegionSamplingListener> & listener)1309 status_t SurfaceFlinger::removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) {
1310 if (!listener) {
1311 return BAD_VALUE;
1312 }
1313 mRegionSamplingThread->removeListener(listener);
1314 return NO_ERROR;
1315 }
1316
getDisplayBrightnessSupport(const sp<IBinder> & displayToken,bool * outSupport) const1317 status_t SurfaceFlinger::getDisplayBrightnessSupport(const sp<IBinder>& displayToken,
1318 bool* outSupport) const {
1319 if (!displayToken || !outSupport) {
1320 return BAD_VALUE;
1321 }
1322 const auto displayId = getPhysicalDisplayIdLocked(displayToken);
1323 if (!displayId) {
1324 return NAME_NOT_FOUND;
1325 }
1326 *outSupport =
1327 getHwComposer().hasDisplayCapability(displayId, HWC2::DisplayCapability::Brightness);
1328 return NO_ERROR;
1329 }
1330
setDisplayBrightness(const sp<IBinder> & displayToken,float brightness) const1331 status_t SurfaceFlinger::setDisplayBrightness(const sp<IBinder>& displayToken,
1332 float brightness) const {
1333 if (!displayToken) {
1334 return BAD_VALUE;
1335 }
1336 const auto displayId = getPhysicalDisplayIdLocked(displayToken);
1337 if (!displayId) {
1338 return NAME_NOT_FOUND;
1339 }
1340 return getHwComposer().setDisplayBrightness(*displayId, brightness);
1341 }
1342
notifyPowerHint(int32_t hintId)1343 status_t SurfaceFlinger::notifyPowerHint(int32_t hintId) {
1344 PowerHint powerHint = static_cast<PowerHint>(hintId);
1345
1346 if (powerHint == PowerHint::INTERACTION) {
1347 mScheduler->notifyTouchEvent();
1348 }
1349
1350 return NO_ERROR;
1351 }
1352
1353 // ----------------------------------------------------------------------------
1354
createDisplayEventConnection(ISurfaceComposer::VsyncSource vsyncSource,ISurfaceComposer::ConfigChanged configChanged)1355 sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection(
1356 ISurfaceComposer::VsyncSource vsyncSource, ISurfaceComposer::ConfigChanged configChanged) {
1357 const auto& handle =
1358 vsyncSource == eVsyncSourceSurfaceFlinger ? mSfConnectionHandle : mAppConnectionHandle;
1359
1360 return mScheduler->createDisplayEventConnection(handle, configChanged);
1361 }
1362
1363 // ----------------------------------------------------------------------------
1364
waitForEvent()1365 void SurfaceFlinger::waitForEvent() {
1366 mEventQueue->waitMessage();
1367 }
1368
signalTransaction()1369 void SurfaceFlinger::signalTransaction() {
1370 mScheduler->resetIdleTimer();
1371 mEventQueue->invalidate();
1372 }
1373
signalLayerUpdate()1374 void SurfaceFlinger::signalLayerUpdate() {
1375 mScheduler->resetIdleTimer();
1376 mEventQueue->invalidate();
1377 }
1378
signalRefresh()1379 void SurfaceFlinger::signalRefresh() {
1380 mRefreshPending = true;
1381 mEventQueue->refresh();
1382 }
1383
postMessageAsync(const sp<MessageBase> & msg,nsecs_t reltime,uint32_t)1384 status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
1385 nsecs_t reltime, uint32_t /* flags */) {
1386 return mEventQueue->postMessage(msg, reltime);
1387 }
1388
postMessageSync(const sp<MessageBase> & msg,nsecs_t reltime,uint32_t)1389 status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
1390 nsecs_t reltime, uint32_t /* flags */) {
1391 status_t res = mEventQueue->postMessage(msg, reltime);
1392 if (res == NO_ERROR) {
1393 msg->wait();
1394 }
1395 return res;
1396 }
1397
run()1398 void SurfaceFlinger::run() {
1399 do {
1400 waitForEvent();
1401 } while (true);
1402 }
1403
getVsyncPeriod() const1404 nsecs_t SurfaceFlinger::getVsyncPeriod() const {
1405 const auto displayId = getInternalDisplayIdLocked();
1406 if (!displayId || !getHwComposer().isConnected(*displayId)) {
1407 return 0;
1408 }
1409
1410 const auto config = getHwComposer().getActiveConfig(*displayId);
1411 return config ? config->getVsyncPeriod() : 0;
1412 }
1413
onVsyncReceived(int32_t sequenceId,hwc2_display_t hwcDisplayId,int64_t timestamp)1414 void SurfaceFlinger::onVsyncReceived(int32_t sequenceId, hwc2_display_t hwcDisplayId,
1415 int64_t timestamp) {
1416 ATRACE_NAME("SF onVsync");
1417
1418 Mutex::Autolock lock(mStateLock);
1419 // Ignore any vsyncs from a previous hardware composer.
1420 if (sequenceId != getBE().mComposerSequenceId) {
1421 return;
1422 }
1423
1424 if (!getHwComposer().onVsync(hwcDisplayId, timestamp)) {
1425 return;
1426 }
1427
1428 if (hwcDisplayId != getHwComposer().getInternalHwcDisplayId()) {
1429 // For now, we don't do anything with external display vsyncs.
1430 return;
1431 }
1432
1433 bool periodFlushed = false;
1434 mScheduler->addResyncSample(timestamp, &periodFlushed);
1435 if (periodFlushed) {
1436 mVsyncModulator.onRefreshRateChangeCompleted();
1437 }
1438 }
1439
getCompositorTiming(CompositorTiming * compositorTiming)1440 void SurfaceFlinger::getCompositorTiming(CompositorTiming* compositorTiming) {
1441 std::lock_guard<std::mutex> lock(getBE().mCompositorTimingLock);
1442 *compositorTiming = getBE().mCompositorTiming;
1443 }
1444
isDisplayConfigAllowed(int32_t configId)1445 bool SurfaceFlinger::isDisplayConfigAllowed(int32_t configId) {
1446 return mAllowedDisplayConfigs.empty() || mAllowedDisplayConfigs.count(configId);
1447 }
1448
setRefreshRateTo(RefreshRateType refreshRate,Scheduler::ConfigEvent event)1449 void SurfaceFlinger::setRefreshRateTo(RefreshRateType refreshRate, Scheduler::ConfigEvent event) {
1450 const auto display = getDefaultDisplayDeviceLocked();
1451 if (!display || mBootStage != BootStage::FINISHED) {
1452 return;
1453 }
1454 ATRACE_CALL();
1455
1456 // Don't do any updating if the current fps is the same as the new one.
1457 const auto& refreshRateConfig = mRefreshRateConfigs->getRefreshRateFromType(refreshRate);
1458 const int desiredConfigId = refreshRateConfig.configId;
1459
1460 if (!isDisplayConfigAllowed(desiredConfigId)) {
1461 ALOGV("Skipping config %d as it is not part of allowed configs", desiredConfigId);
1462 return;
1463 }
1464
1465 setDesiredActiveConfig({refreshRate, desiredConfigId, event});
1466 }
1467
onHotplugReceived(int32_t sequenceId,hwc2_display_t hwcDisplayId,HWC2::Connection connection)1468 void SurfaceFlinger::onHotplugReceived(int32_t sequenceId, hwc2_display_t hwcDisplayId,
1469 HWC2::Connection connection) {
1470 ALOGV("%s(%d, %" PRIu64 ", %s)", __FUNCTION__, sequenceId, hwcDisplayId,
1471 connection == HWC2::Connection::Connected ? "connected" : "disconnected");
1472
1473 // Ignore events that do not have the right sequenceId.
1474 if (sequenceId != getBE().mComposerSequenceId) {
1475 return;
1476 }
1477
1478 // Only lock if we're not on the main thread. This function is normally
1479 // called on a hwbinder thread, but for the primary display it's called on
1480 // the main thread with the state lock already held, so don't attempt to
1481 // acquire it here.
1482 ConditionalLock lock(mStateLock, std::this_thread::get_id() != mMainThreadId);
1483
1484 mPendingHotplugEvents.emplace_back(HotplugEvent{hwcDisplayId, connection});
1485
1486 if (std::this_thread::get_id() == mMainThreadId) {
1487 // Process all pending hot plug events immediately if we are on the main thread.
1488 processDisplayHotplugEventsLocked();
1489 }
1490
1491 setTransactionFlags(eDisplayTransactionNeeded);
1492 }
1493
onRefreshReceived(int sequenceId,hwc2_display_t)1494 void SurfaceFlinger::onRefreshReceived(int sequenceId, hwc2_display_t /*hwcDisplayId*/) {
1495 Mutex::Autolock lock(mStateLock);
1496 if (sequenceId != getBE().mComposerSequenceId) {
1497 return;
1498 }
1499 repaintEverythingForHWC();
1500 }
1501
setPrimaryVsyncEnabled(bool enabled)1502 void SurfaceFlinger::setPrimaryVsyncEnabled(bool enabled) {
1503 ATRACE_CALL();
1504
1505 // Enable / Disable HWVsync from the main thread to avoid race conditions with
1506 // display power state.
1507 postMessageAsync(new LambdaMessage(
1508 [=]() NO_THREAD_SAFETY_ANALYSIS { setPrimaryVsyncEnabledInternal(enabled); }));
1509 }
1510
setPrimaryVsyncEnabledInternal(bool enabled)1511 void SurfaceFlinger::setPrimaryVsyncEnabledInternal(bool enabled) {
1512 ATRACE_CALL();
1513
1514 mHWCVsyncPendingState = enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable;
1515
1516 if (const auto displayId = getInternalDisplayIdLocked()) {
1517 sp<DisplayDevice> display = getDefaultDisplayDeviceLocked();
1518 if (display && display->isPoweredOn()) {
1519 setVsyncEnabledInHWC(*displayId, mHWCVsyncPendingState);
1520 }
1521 }
1522 }
1523
1524 // Note: it is assumed the caller holds |mStateLock| when this is called
resetDisplayState()1525 void SurfaceFlinger::resetDisplayState() {
1526 mScheduler->disableHardwareVsync(true);
1527 // Clear the drawing state so that the logic inside of
1528 // handleTransactionLocked will fire. It will determine the delta between
1529 // mCurrentState and mDrawingState and re-apply all changes when we make the
1530 // transition.
1531 mDrawingState.displays.clear();
1532 mDisplays.clear();
1533 }
1534
updateVrFlinger()1535 void SurfaceFlinger::updateVrFlinger() {
1536 ATRACE_CALL();
1537 if (!mVrFlinger)
1538 return;
1539 bool vrFlingerRequestsDisplay = mVrFlingerRequestsDisplay;
1540 if (vrFlingerRequestsDisplay == getHwComposer().isUsingVrComposer()) {
1541 return;
1542 }
1543
1544 if (vrFlingerRequestsDisplay && !getHwComposer().getComposer()->isRemote()) {
1545 ALOGE("Vr flinger is only supported for remote hardware composer"
1546 " service connections. Ignoring request to transition to vr"
1547 " flinger.");
1548 mVrFlingerRequestsDisplay = false;
1549 return;
1550 }
1551
1552 Mutex::Autolock _l(mStateLock);
1553
1554 sp<DisplayDevice> display = getDefaultDisplayDeviceLocked();
1555 LOG_ALWAYS_FATAL_IF(!display);
1556
1557 const int currentDisplayPowerMode = display->getPowerMode();
1558
1559 // Clear out all the output layers from the composition engine for all
1560 // displays before destroying the hardware composer interface. This ensures
1561 // any HWC layers are destroyed through that interface before it becomes
1562 // invalid.
1563 for (const auto& [token, displayDevice] : mDisplays) {
1564 displayDevice->getCompositionDisplay()->setOutputLayersOrderedByZ(
1565 compositionengine::Output::OutputLayers());
1566 }
1567
1568 // This DisplayDevice will no longer be relevant once resetDisplayState() is
1569 // called below. Clear the reference now so we don't accidentally use it
1570 // later.
1571 display.clear();
1572
1573 if (!vrFlingerRequestsDisplay) {
1574 mVrFlinger->SeizeDisplayOwnership();
1575 }
1576
1577 resetDisplayState();
1578 // Delete the current instance before creating the new one
1579 mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>());
1580 mCompositionEngine->setHwComposer(getFactory().createHWComposer(
1581 vrFlingerRequestsDisplay ? "vr" : getBE().mHwcServiceName));
1582 getHwComposer().registerCallback(this, ++getBE().mComposerSequenceId);
1583
1584 LOG_ALWAYS_FATAL_IF(!getHwComposer().getComposer()->isRemote(),
1585 "Switched to non-remote hardware composer");
1586
1587 if (vrFlingerRequestsDisplay) {
1588 mVrFlinger->GrantDisplayOwnership();
1589 }
1590
1591 mVisibleRegionsDirty = true;
1592 invalidateHwcGeometry();
1593
1594 // Re-enable default display.
1595 display = getDefaultDisplayDeviceLocked();
1596 LOG_ALWAYS_FATAL_IF(!display);
1597 setPowerModeInternal(display, currentDisplayPowerMode);
1598
1599 // Reset the timing values to account for the period of the swapped in HWC
1600 const nsecs_t vsyncPeriod = getVsyncPeriod();
1601 mAnimFrameTracker.setDisplayRefreshPeriod(vsyncPeriod);
1602
1603 // The present fences returned from vr_hwc are not an accurate
1604 // representation of vsync times.
1605 mScheduler->setIgnorePresentFences(getHwComposer().isUsingVrComposer() || !hasSyncFramework);
1606
1607 // Use phase of 0 since phase is not known.
1608 // Use latency of 0, which will snap to the ideal latency.
1609 DisplayStatInfo stats{0 /* vsyncTime */, vsyncPeriod};
1610 setCompositorTimingSnapped(stats, 0);
1611
1612 mScheduler->resyncToHardwareVsync(false, vsyncPeriod);
1613
1614 mRepaintEverything = true;
1615 setTransactionFlags(eDisplayTransactionNeeded);
1616 }
1617
previousFrameMissed(int graceTimeMs)1618 bool SurfaceFlinger::previousFrameMissed(int graceTimeMs) NO_THREAD_SAFETY_ANALYSIS {
1619 ATRACE_CALL();
1620 // We are storing the last 2 present fences. If sf's phase offset is to be
1621 // woken up before the actual vsync but targeting the next vsync, we need to check
1622 // fence N-2
1623 const sp<Fence>& fence =
1624 mVsyncModulator.getOffsets().sf < mPhaseOffsets->getOffsetThresholdForNextVsync()
1625 ? mPreviousPresentFences[0]
1626 : mPreviousPresentFences[1];
1627
1628 if (fence == Fence::NO_FENCE) {
1629 return false;
1630 }
1631
1632 if (graceTimeMs > 0 && fence->getStatus() == Fence::Status::Unsignaled) {
1633 fence->wait(graceTimeMs);
1634 }
1635
1636 return (fence->getStatus() == Fence::Status::Unsignaled);
1637 }
1638
populateExpectedPresentTime()1639 void SurfaceFlinger::populateExpectedPresentTime() NO_THREAD_SAFETY_ANALYSIS {
1640 DisplayStatInfo stats;
1641 mScheduler->getDisplayStatInfo(&stats);
1642 const nsecs_t presentTime = mScheduler->getDispSyncExpectedPresentTime();
1643 // Inflate the expected present time if we're targetting the next vsync.
1644 mExpectedPresentTime =
1645 mVsyncModulator.getOffsets().sf < mPhaseOffsets->getOffsetThresholdForNextVsync()
1646 ? presentTime
1647 : presentTime + stats.vsyncPeriod;
1648 }
1649
onMessageReceived(int32_t what)1650 void SurfaceFlinger::onMessageReceived(int32_t what) NO_THREAD_SAFETY_ANALYSIS {
1651 ATRACE_CALL();
1652 switch (what) {
1653 case MessageQueue::INVALIDATE: {
1654 // calculate the expected present time once and use the cached
1655 // value throughout this frame to make sure all layers are
1656 // seeing this same value.
1657 populateExpectedPresentTime();
1658
1659 // When Backpressure propagation is enabled we want to give a small grace period
1660 // for the present fence to fire instead of just giving up on this frame to handle cases
1661 // where present fence is just about to get signaled.
1662 const int graceTimeForPresentFenceMs =
1663 (mPropagateBackpressure &&
1664 (mPropagateBackpressureClientComposition || !mHadClientComposition))
1665 ? 1
1666 : 0;
1667 bool frameMissed = previousFrameMissed(graceTimeForPresentFenceMs);
1668 bool hwcFrameMissed = mHadDeviceComposition && frameMissed;
1669 bool gpuFrameMissed = mHadClientComposition && frameMissed;
1670 ATRACE_INT("FrameMissed", static_cast<int>(frameMissed));
1671 ATRACE_INT("HwcFrameMissed", static_cast<int>(hwcFrameMissed));
1672 ATRACE_INT("GpuFrameMissed", static_cast<int>(gpuFrameMissed));
1673 if (frameMissed) {
1674 mFrameMissedCount++;
1675 mTimeStats->incrementMissedFrames();
1676 }
1677
1678 if (hwcFrameMissed) {
1679 mHwcFrameMissedCount++;
1680 }
1681
1682 if (gpuFrameMissed) {
1683 mGpuFrameMissedCount++;
1684 }
1685
1686 if (mUseSmart90ForVideo) {
1687 // This call is made each time SF wakes up and creates a new frame. It is part
1688 // of video detection feature.
1689 mScheduler->updateFpsBasedOnContent();
1690 }
1691
1692 if (performSetActiveConfig()) {
1693 break;
1694 }
1695
1696 if (frameMissed && mPropagateBackpressure) {
1697 if ((hwcFrameMissed && !gpuFrameMissed) ||
1698 mPropagateBackpressureClientComposition) {
1699 signalLayerUpdate();
1700 break;
1701 }
1702 }
1703
1704 // Now that we're going to make it to the handleMessageTransaction()
1705 // call below it's safe to call updateVrFlinger(), which will
1706 // potentially trigger a display handoff.
1707 updateVrFlinger();
1708
1709 bool refreshNeeded = handleMessageTransaction();
1710 refreshNeeded |= handleMessageInvalidate();
1711
1712 updateCursorAsync();
1713 updateInputFlinger();
1714
1715 refreshNeeded |= mRepaintEverything;
1716 if (refreshNeeded && CC_LIKELY(mBootStage != BootStage::BOOTLOADER)) {
1717 // Signal a refresh if a transaction modified the window state,
1718 // a new buffer was latched, or if HWC has requested a full
1719 // repaint
1720 signalRefresh();
1721 }
1722 break;
1723 }
1724 case MessageQueue::REFRESH: {
1725 handleMessageRefresh();
1726 break;
1727 }
1728 }
1729 }
1730
handleMessageTransaction()1731 bool SurfaceFlinger::handleMessageTransaction() {
1732 ATRACE_CALL();
1733 uint32_t transactionFlags = peekTransactionFlags();
1734
1735 bool flushedATransaction = flushTransactionQueues();
1736
1737 bool runHandleTransaction = transactionFlags &&
1738 ((transactionFlags != eTransactionFlushNeeded) || flushedATransaction);
1739
1740 if (runHandleTransaction) {
1741 handleTransaction(eTransactionMask);
1742 } else {
1743 getTransactionFlags(eTransactionFlushNeeded);
1744 }
1745
1746 if (transactionFlushNeeded()) {
1747 setTransactionFlags(eTransactionFlushNeeded);
1748 }
1749
1750 return runHandleTransaction;
1751 }
1752
handleMessageRefresh()1753 void SurfaceFlinger::handleMessageRefresh() {
1754 ATRACE_CALL();
1755
1756 mRefreshPending = false;
1757
1758 const bool repaintEverything = mRepaintEverything.exchange(false);
1759 preComposition();
1760 rebuildLayerStacks();
1761 calculateWorkingSet();
1762 long compositionTime = elapsedRealtimeNano();
1763 for (const auto& [token, display] : mDisplays) {
1764 beginFrame(display);
1765 prepareFrame(display);
1766 doDebugFlashRegions(display, repaintEverything);
1767 doComposition(display, repaintEverything);
1768 }
1769
1770 logLayerStats();
1771
1772 postFrame();
1773 postComposition();
1774
1775 mHadClientComposition = false;
1776 mHadDeviceComposition = false;
1777 for (const auto& [token, displayDevice] : mDisplays) {
1778 auto display = displayDevice->getCompositionDisplay();
1779 const auto displayId = display->getId();
1780 mHadClientComposition =
1781 mHadClientComposition || getHwComposer().hasClientComposition(displayId);
1782 mHadDeviceComposition =
1783 mHadDeviceComposition || getHwComposer().hasDeviceComposition(displayId);
1784 }
1785
1786 mVsyncModulator.onRefreshed(mHadClientComposition);
1787
1788 mLayersWithQueuedFrames.clear();
1789 if (mVisibleRegionsDirty) {
1790 mVisibleRegionsDirty = false;
1791 if (mTracingEnabled) {
1792 mTracing.notify(compositionTime, "visibleRegionsDirty");
1793 }
1794 }
1795 }
1796
1797
handleMessageInvalidate()1798 bool SurfaceFlinger::handleMessageInvalidate() {
1799 ATRACE_CALL();
1800 bool refreshNeeded = handlePageFlip();
1801
1802 if (mVisibleRegionsDirty) {
1803 computeLayerBounds();
1804 }
1805
1806 for (auto& layer : mLayersPendingRefresh) {
1807 Region visibleReg;
1808 visibleReg.set(layer->getScreenBounds());
1809 invalidateLayerStack(layer, visibleReg);
1810 }
1811 mLayersPendingRefresh.clear();
1812 return refreshNeeded;
1813 }
1814
calculateWorkingSet()1815 void SurfaceFlinger::calculateWorkingSet() {
1816 ATRACE_CALL();
1817 ALOGV(__FUNCTION__);
1818
1819 // build the h/w work list
1820 if (CC_UNLIKELY(mGeometryInvalid)) {
1821 mGeometryInvalid = false;
1822 for (const auto& [token, displayDevice] : mDisplays) {
1823 auto display = displayDevice->getCompositionDisplay();
1824
1825 uint32_t zOrder = 0;
1826
1827 for (auto& layer : display->getOutputLayersOrderedByZ()) {
1828 auto& compositionState = layer->editState();
1829 compositionState.forceClientComposition = false;
1830 if (!compositionState.hwc || mDebugDisableHWC || mDebugRegion) {
1831 compositionState.forceClientComposition = true;
1832 }
1833
1834 // The output Z order is set here based on a simple counter.
1835 compositionState.z = zOrder++;
1836
1837 // Update the display independent composition state. This goes
1838 // to the general composition layer state structure.
1839 // TODO: Do this once per compositionengine::CompositionLayer.
1840 layer->getLayerFE().latchCompositionState(layer->getLayer().editState().frontEnd,
1841 true);
1842
1843 // Recalculate the geometry state of the output layer.
1844 layer->updateCompositionState(true);
1845
1846 // Write the updated geometry state to the HWC
1847 layer->writeStateToHWC(true);
1848 }
1849 }
1850 }
1851
1852 // Set the per-frame data
1853 for (const auto& [token, displayDevice] : mDisplays) {
1854 auto display = displayDevice->getCompositionDisplay();
1855 const auto displayId = display->getId();
1856 if (!displayId) {
1857 continue;
1858 }
1859 auto* profile = display->getDisplayColorProfile();
1860
1861 if (mDrawingState.colorMatrixChanged) {
1862 display->setColorTransform(mDrawingState.colorMatrix);
1863 }
1864 Dataspace targetDataspace = Dataspace::UNKNOWN;
1865 if (useColorManagement) {
1866 ColorMode colorMode;
1867 RenderIntent renderIntent;
1868 pickColorMode(displayDevice, &colorMode, &targetDataspace, &renderIntent);
1869 display->setColorMode(colorMode, targetDataspace, renderIntent);
1870
1871 if (isHdrColorMode(colorMode)) {
1872 targetDataspace = Dataspace::UNKNOWN;
1873 } else if (mColorSpaceAgnosticDataspace != Dataspace::UNKNOWN) {
1874 targetDataspace = mColorSpaceAgnosticDataspace;
1875 }
1876 }
1877
1878 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1879 if (layer->isHdrY410()) {
1880 layer->forceClientComposition(displayDevice);
1881 } else if ((layer->getDataSpace() == Dataspace::BT2020_PQ ||
1882 layer->getDataSpace() == Dataspace::BT2020_ITU_PQ) &&
1883 !profile->hasHDR10Support()) {
1884 layer->forceClientComposition(displayDevice);
1885 } else if ((layer->getDataSpace() == Dataspace::BT2020_HLG ||
1886 layer->getDataSpace() == Dataspace::BT2020_ITU_HLG) &&
1887 !profile->hasHLGSupport()) {
1888 layer->forceClientComposition(displayDevice);
1889 }
1890
1891 if (layer->getRoundedCornerState().radius > 0.0f) {
1892 layer->forceClientComposition(displayDevice);
1893 }
1894
1895 if (layer->getForceClientComposition(displayDevice)) {
1896 ALOGV("[%s] Requesting Client composition", layer->getName().string());
1897 layer->setCompositionType(displayDevice,
1898 Hwc2::IComposerClient::Composition::CLIENT);
1899 continue;
1900 }
1901
1902 const auto& displayState = display->getState();
1903 layer->setPerFrameData(displayDevice, displayState.transform, displayState.viewport,
1904 displayDevice->getSupportedPerFrameMetadata(), targetDataspace);
1905 }
1906 }
1907
1908 mDrawingState.colorMatrixChanged = false;
1909
1910 for (const auto& [token, displayDevice] : mDisplays) {
1911 auto display = displayDevice->getCompositionDisplay();
1912 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1913 auto& layerState = layer->getCompositionLayer()->editState().frontEnd;
1914 layerState.compositionType = static_cast<Hwc2::IComposerClient::Composition>(
1915 layer->getCompositionType(displayDevice));
1916 }
1917 }
1918 }
1919
doDebugFlashRegions(const sp<DisplayDevice> & displayDevice,bool repaintEverything)1920 void SurfaceFlinger::doDebugFlashRegions(const sp<DisplayDevice>& displayDevice,
1921 bool repaintEverything) {
1922 auto display = displayDevice->getCompositionDisplay();
1923 const auto& displayState = display->getState();
1924
1925 // is debugging enabled
1926 if (CC_LIKELY(!mDebugRegion))
1927 return;
1928
1929 if (displayState.isEnabled) {
1930 // transform the dirty region into this screen's coordinate space
1931 const Region dirtyRegion = display->getDirtyRegion(repaintEverything);
1932 if (!dirtyRegion.isEmpty()) {
1933 base::unique_fd readyFence;
1934 // redraw the whole screen
1935 doComposeSurfaces(displayDevice, dirtyRegion, &readyFence);
1936
1937 display->getRenderSurface()->queueBuffer(std::move(readyFence));
1938 }
1939 }
1940
1941 postFramebuffer(displayDevice);
1942
1943 if (mDebugRegion > 1) {
1944 usleep(mDebugRegion * 1000);
1945 }
1946
1947 prepareFrame(displayDevice);
1948 }
1949
logLayerStats()1950 void SurfaceFlinger::logLayerStats() {
1951 ATRACE_CALL();
1952 if (CC_UNLIKELY(mLayerStats.isEnabled())) {
1953 for (const auto& [token, display] : mDisplays) {
1954 if (display->isPrimary()) {
1955 mLayerStats.logLayerStats(dumpVisibleLayersProtoInfo(display));
1956 return;
1957 }
1958 }
1959
1960 ALOGE("logLayerStats: no primary display");
1961 }
1962 }
1963
preComposition()1964 void SurfaceFlinger::preComposition()
1965 {
1966 ATRACE_CALL();
1967 ALOGV("preComposition");
1968
1969 mRefreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
1970
1971 bool needExtraInvalidate = false;
1972 mDrawingState.traverseInZOrder([&](Layer* layer) {
1973 if (layer->onPreComposition(mRefreshStartTime)) {
1974 needExtraInvalidate = true;
1975 }
1976 });
1977
1978 if (needExtraInvalidate) {
1979 signalLayerUpdate();
1980 }
1981 }
1982
updateCompositorTiming(const DisplayStatInfo & stats,nsecs_t compositeTime,std::shared_ptr<FenceTime> & presentFenceTime)1983 void SurfaceFlinger::updateCompositorTiming(const DisplayStatInfo& stats, nsecs_t compositeTime,
1984 std::shared_ptr<FenceTime>& presentFenceTime) {
1985 // Update queue of past composite+present times and determine the
1986 // most recently known composite to present latency.
1987 getBE().mCompositePresentTimes.push({compositeTime, presentFenceTime});
1988 nsecs_t compositeToPresentLatency = -1;
1989 while (!getBE().mCompositePresentTimes.empty()) {
1990 SurfaceFlingerBE::CompositePresentTime& cpt = getBE().mCompositePresentTimes.front();
1991 // Cached values should have been updated before calling this method,
1992 // which helps avoid duplicate syscalls.
1993 nsecs_t displayTime = cpt.display->getCachedSignalTime();
1994 if (displayTime == Fence::SIGNAL_TIME_PENDING) {
1995 break;
1996 }
1997 compositeToPresentLatency = displayTime - cpt.composite;
1998 getBE().mCompositePresentTimes.pop();
1999 }
2000
2001 // Don't let mCompositePresentTimes grow unbounded, just in case.
2002 while (getBE().mCompositePresentTimes.size() > 16) {
2003 getBE().mCompositePresentTimes.pop();
2004 }
2005
2006 setCompositorTimingSnapped(stats, compositeToPresentLatency);
2007 }
2008
setCompositorTimingSnapped(const DisplayStatInfo & stats,nsecs_t compositeToPresentLatency)2009 void SurfaceFlinger::setCompositorTimingSnapped(const DisplayStatInfo& stats,
2010 nsecs_t compositeToPresentLatency) {
2011 // Integer division and modulo round toward 0 not -inf, so we need to
2012 // treat negative and positive offsets differently.
2013 nsecs_t idealLatency = (mPhaseOffsets->getCurrentSfOffset() > 0)
2014 ? (stats.vsyncPeriod - (mPhaseOffsets->getCurrentSfOffset() % stats.vsyncPeriod))
2015 : ((-mPhaseOffsets->getCurrentSfOffset()) % stats.vsyncPeriod);
2016
2017 // Just in case mPhaseOffsets->getCurrentSfOffset() == -vsyncInterval.
2018 if (idealLatency <= 0) {
2019 idealLatency = stats.vsyncPeriod;
2020 }
2021
2022 // Snap the latency to a value that removes scheduling jitter from the
2023 // composition and present times, which often have >1ms of jitter.
2024 // Reducing jitter is important if an app attempts to extrapolate
2025 // something (such as user input) to an accurate diasplay time.
2026 // Snapping also allows an app to precisely calculate mPhaseOffsets->getCurrentSfOffset()
2027 // with (presentLatency % interval).
2028 nsecs_t bias = stats.vsyncPeriod / 2;
2029 int64_t extraVsyncs = (compositeToPresentLatency - idealLatency + bias) / stats.vsyncPeriod;
2030 nsecs_t snappedCompositeToPresentLatency =
2031 (extraVsyncs > 0) ? idealLatency + (extraVsyncs * stats.vsyncPeriod) : idealLatency;
2032
2033 std::lock_guard<std::mutex> lock(getBE().mCompositorTimingLock);
2034 getBE().mCompositorTiming.deadline = stats.vsyncTime - idealLatency;
2035 getBE().mCompositorTiming.interval = stats.vsyncPeriod;
2036 getBE().mCompositorTiming.presentLatency = snappedCompositeToPresentLatency;
2037 }
2038
postComposition()2039 void SurfaceFlinger::postComposition()
2040 {
2041 ATRACE_CALL();
2042 ALOGV("postComposition");
2043
2044 // Release any buffers which were replaced this frame
2045 nsecs_t dequeueReadyTime = systemTime();
2046 for (auto& layer : mLayersWithQueuedFrames) {
2047 layer->releasePendingBuffer(dequeueReadyTime);
2048 }
2049
2050 // |mStateLock| not needed as we are on the main thread
2051 const auto displayDevice = getDefaultDisplayDeviceLocked();
2052
2053 getBE().mGlCompositionDoneTimeline.updateSignalTimes();
2054 std::shared_ptr<FenceTime> glCompositionDoneFenceTime;
2055 if (displayDevice && getHwComposer().hasClientComposition(displayDevice->getId())) {
2056 glCompositionDoneFenceTime =
2057 std::make_shared<FenceTime>(displayDevice->getCompositionDisplay()
2058 ->getRenderSurface()
2059 ->getClientTargetAcquireFence());
2060 getBE().mGlCompositionDoneTimeline.push(glCompositionDoneFenceTime);
2061 } else {
2062 glCompositionDoneFenceTime = FenceTime::NO_FENCE;
2063 }
2064
2065 getBE().mDisplayTimeline.updateSignalTimes();
2066 mPreviousPresentFences[1] = mPreviousPresentFences[0];
2067 mPreviousPresentFences[0] = displayDevice
2068 ? getHwComposer().getPresentFence(*displayDevice->getId())
2069 : Fence::NO_FENCE;
2070 auto presentFenceTime = std::make_shared<FenceTime>(mPreviousPresentFences[0]);
2071 getBE().mDisplayTimeline.push(presentFenceTime);
2072
2073 DisplayStatInfo stats;
2074 mScheduler->getDisplayStatInfo(&stats);
2075
2076 // We use the mRefreshStartTime which might be sampled a little later than
2077 // when we started doing work for this frame, but that should be okay
2078 // since updateCompositorTiming has snapping logic.
2079 updateCompositorTiming(stats, mRefreshStartTime, presentFenceTime);
2080 CompositorTiming compositorTiming;
2081 {
2082 std::lock_guard<std::mutex> lock(getBE().mCompositorTimingLock);
2083 compositorTiming = getBE().mCompositorTiming;
2084 }
2085
2086 mDrawingState.traverseInZOrder([&](Layer* layer) {
2087 bool frameLatched =
2088 layer->onPostComposition(displayDevice->getId(), glCompositionDoneFenceTime,
2089 presentFenceTime, compositorTiming);
2090 if (frameLatched) {
2091 recordBufferingStats(layer->getName().string(),
2092 layer->getOccupancyHistory(false));
2093 }
2094 });
2095
2096 mTransactionCompletedThread.addPresentFence(mPreviousPresentFences[0]);
2097
2098 // Lock the mStateLock in case SurfaceFlinger is in the middle of applying a transaction.
2099 // If we do not lock here, a callback could be sent without all of its SurfaceControls and
2100 // metrics.
2101 {
2102 Mutex::Autolock _l(mStateLock);
2103 mTransactionCompletedThread.sendCallbacks();
2104 }
2105
2106 if (presentFenceTime->isValid()) {
2107 mScheduler->addPresentFence(presentFenceTime);
2108 }
2109
2110 if (!hasSyncFramework) {
2111 if (displayDevice && getHwComposer().isConnected(*displayDevice->getId()) &&
2112 displayDevice->isPoweredOn()) {
2113 mScheduler->enableHardwareVsync();
2114 }
2115 }
2116
2117 if (mAnimCompositionPending) {
2118 mAnimCompositionPending = false;
2119
2120 if (presentFenceTime->isValid()) {
2121 mAnimFrameTracker.setActualPresentFence(
2122 std::move(presentFenceTime));
2123 } else if (displayDevice && getHwComposer().isConnected(*displayDevice->getId())) {
2124 // The HWC doesn't support present fences, so use the refresh
2125 // timestamp instead.
2126 const nsecs_t presentTime =
2127 getHwComposer().getRefreshTimestamp(*displayDevice->getId());
2128 mAnimFrameTracker.setActualPresentTime(presentTime);
2129 }
2130 mAnimFrameTracker.advanceFrame();
2131 }
2132
2133 mTimeStats->incrementTotalFrames();
2134 if (mHadClientComposition) {
2135 mTimeStats->incrementClientCompositionFrames();
2136 }
2137
2138 mTimeStats->setPresentFenceGlobal(presentFenceTime);
2139
2140 if (displayDevice && getHwComposer().isConnected(*displayDevice->getId()) &&
2141 !displayDevice->isPoweredOn()) {
2142 return;
2143 }
2144
2145 nsecs_t currentTime = systemTime();
2146 if (mHasPoweredOff) {
2147 mHasPoweredOff = false;
2148 } else {
2149 nsecs_t elapsedTime = currentTime - getBE().mLastSwapTime;
2150 size_t numPeriods = static_cast<size_t>(elapsedTime / stats.vsyncPeriod);
2151 if (numPeriods < SurfaceFlingerBE::NUM_BUCKETS - 1) {
2152 getBE().mFrameBuckets[numPeriods] += elapsedTime;
2153 } else {
2154 getBE().mFrameBuckets[SurfaceFlingerBE::NUM_BUCKETS - 1] += elapsedTime;
2155 }
2156 getBE().mTotalTime += elapsedTime;
2157 }
2158 getBE().mLastSwapTime = currentTime;
2159
2160 {
2161 std::lock_guard lock(mTexturePoolMutex);
2162 if (mTexturePool.size() < mTexturePoolSize) {
2163 const size_t refillCount = mTexturePoolSize - mTexturePool.size();
2164 const size_t offset = mTexturePool.size();
2165 mTexturePool.resize(mTexturePoolSize);
2166 getRenderEngine().genTextures(refillCount, mTexturePool.data() + offset);
2167 ATRACE_INT("TexturePoolSize", mTexturePool.size());
2168 } else if (mTexturePool.size() > mTexturePoolSize) {
2169 const size_t deleteCount = mTexturePool.size() - mTexturePoolSize;
2170 const size_t offset = mTexturePoolSize;
2171 getRenderEngine().deleteTextures(deleteCount, mTexturePool.data() + offset);
2172 mTexturePool.resize(mTexturePoolSize);
2173 ATRACE_INT("TexturePoolSize", mTexturePool.size());
2174 }
2175 }
2176
2177 if (mLumaSampling && mRegionSamplingThread) {
2178 mRegionSamplingThread->notifyNewContent();
2179 }
2180
2181 // Even though ATRACE_INT64 already checks if tracing is enabled, it doesn't prevent the
2182 // side-effect of getTotalSize(), so we check that again here
2183 if (ATRACE_ENABLED()) {
2184 ATRACE_INT64("Total Buffer Size", GraphicBufferAllocator::get().getTotalSize());
2185 }
2186 }
2187
computeLayerBounds()2188 void SurfaceFlinger::computeLayerBounds() {
2189 for (const auto& pair : mDisplays) {
2190 const auto& displayDevice = pair.second;
2191 const auto display = displayDevice->getCompositionDisplay();
2192 for (const auto& layer : mDrawingState.layersSortedByZ) {
2193 // only consider the layers on the given layer stack
2194 if (!display->belongsInOutput(layer->getLayerStack(), layer->getPrimaryDisplayOnly())) {
2195 continue;
2196 }
2197
2198 layer->computeBounds(displayDevice->getViewport().toFloatRect(), ui::Transform());
2199 }
2200 }
2201 }
2202
rebuildLayerStacks()2203 void SurfaceFlinger::rebuildLayerStacks() {
2204 ATRACE_CALL();
2205 ALOGV("rebuildLayerStacks");
2206
2207 // rebuild the visible layer list per screen
2208 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
2209 ATRACE_NAME("rebuildLayerStacks VR Dirty");
2210 invalidateHwcGeometry();
2211
2212 for (const auto& pair : mDisplays) {
2213 const auto& displayDevice = pair.second;
2214 auto display = displayDevice->getCompositionDisplay();
2215 const auto& displayState = display->getState();
2216 Region opaqueRegion;
2217 Region dirtyRegion;
2218 compositionengine::Output::OutputLayers layersSortedByZ;
2219 Vector<sp<Layer>> deprecated_layersSortedByZ;
2220 Vector<sp<Layer>> layersNeedingFences;
2221 const ui::Transform& tr = displayState.transform;
2222 const Rect bounds = displayState.bounds;
2223 if (displayState.isEnabled) {
2224 computeVisibleRegions(displayDevice, dirtyRegion, opaqueRegion);
2225
2226 mDrawingState.traverseInZOrder([&](Layer* layer) {
2227 auto compositionLayer = layer->getCompositionLayer();
2228 if (compositionLayer == nullptr) {
2229 return;
2230 }
2231
2232 const auto displayId = displayDevice->getId();
2233 sp<compositionengine::LayerFE> layerFE = compositionLayer->getLayerFE();
2234 LOG_ALWAYS_FATAL_IF(layerFE.get() == nullptr);
2235
2236 bool needsOutputLayer = false;
2237
2238 if (display->belongsInOutput(layer->getLayerStack(),
2239 layer->getPrimaryDisplayOnly())) {
2240 Region drawRegion(tr.transform(
2241 layer->visibleNonTransparentRegion));
2242 drawRegion.andSelf(bounds);
2243 if (!drawRegion.isEmpty()) {
2244 needsOutputLayer = true;
2245 }
2246 }
2247
2248 if (needsOutputLayer) {
2249 layersSortedByZ.emplace_back(
2250 display->getOrCreateOutputLayer(displayId, compositionLayer,
2251 layerFE));
2252 deprecated_layersSortedByZ.add(layer);
2253
2254 auto& outputLayerState = layersSortedByZ.back()->editState();
2255 outputLayerState.visibleRegion =
2256 tr.transform(layer->visibleRegion.intersect(displayState.viewport));
2257 } else if (displayId) {
2258 // For layers that are being removed from a HWC display,
2259 // and that have queued frames, add them to a a list of
2260 // released layers so we can properly set a fence.
2261 bool hasExistingOutputLayer =
2262 display->getOutputLayerForLayer(compositionLayer.get()) != nullptr;
2263 bool hasQueuedFrames = std::find(mLayersWithQueuedFrames.cbegin(),
2264 mLayersWithQueuedFrames.cend(),
2265 layer) != mLayersWithQueuedFrames.cend();
2266
2267 if (hasExistingOutputLayer && hasQueuedFrames) {
2268 layersNeedingFences.add(layer);
2269 }
2270 }
2271 });
2272 }
2273
2274 display->setOutputLayersOrderedByZ(std::move(layersSortedByZ));
2275
2276 displayDevice->setVisibleLayersSortedByZ(deprecated_layersSortedByZ);
2277 displayDevice->setLayersNeedingFences(layersNeedingFences);
2278
2279 Region undefinedRegion{bounds};
2280 undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
2281
2282 display->editState().undefinedRegion = undefinedRegion;
2283 display->editState().dirtyRegion.orSelf(dirtyRegion);
2284 }
2285 }
2286 }
2287
2288 // Returns a data space that fits all visible layers. The returned data space
2289 // can only be one of
2290 // - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
2291 // - Dataspace::DISPLAY_P3
2292 // - Dataspace::DISPLAY_BT2020
2293 // The returned HDR data space is one of
2294 // - Dataspace::UNKNOWN
2295 // - Dataspace::BT2020_HLG
2296 // - Dataspace::BT2020_PQ
getBestDataspace(const sp<DisplayDevice> & display,Dataspace * outHdrDataSpace,bool * outIsHdrClientComposition) const2297 Dataspace SurfaceFlinger::getBestDataspace(const sp<DisplayDevice>& display,
2298 Dataspace* outHdrDataSpace,
2299 bool* outIsHdrClientComposition) const {
2300 Dataspace bestDataSpace = Dataspace::V0_SRGB;
2301 *outHdrDataSpace = Dataspace::UNKNOWN;
2302
2303 for (const auto& layer : display->getVisibleLayersSortedByZ()) {
2304 switch (layer->getDataSpace()) {
2305 case Dataspace::V0_SCRGB:
2306 case Dataspace::V0_SCRGB_LINEAR:
2307 case Dataspace::BT2020:
2308 case Dataspace::BT2020_ITU:
2309 case Dataspace::BT2020_LINEAR:
2310 case Dataspace::DISPLAY_BT2020:
2311 bestDataSpace = Dataspace::DISPLAY_BT2020;
2312 break;
2313 case Dataspace::DISPLAY_P3:
2314 bestDataSpace = Dataspace::DISPLAY_P3;
2315 break;
2316 case Dataspace::BT2020_PQ:
2317 case Dataspace::BT2020_ITU_PQ:
2318 bestDataSpace = Dataspace::DISPLAY_P3;
2319 *outHdrDataSpace = Dataspace::BT2020_PQ;
2320 *outIsHdrClientComposition = layer->getForceClientComposition(display);
2321 break;
2322 case Dataspace::BT2020_HLG:
2323 case Dataspace::BT2020_ITU_HLG:
2324 bestDataSpace = Dataspace::DISPLAY_P3;
2325 // When there's mixed PQ content and HLG content, we set the HDR
2326 // data space to be BT2020_PQ and convert HLG to PQ.
2327 if (*outHdrDataSpace == Dataspace::UNKNOWN) {
2328 *outHdrDataSpace = Dataspace::BT2020_HLG;
2329 }
2330 break;
2331 default:
2332 break;
2333 }
2334 }
2335
2336 return bestDataSpace;
2337 }
2338
2339 // Pick the ColorMode / Dataspace for the display device.
pickColorMode(const sp<DisplayDevice> & display,ColorMode * outMode,Dataspace * outDataSpace,RenderIntent * outRenderIntent) const2340 void SurfaceFlinger::pickColorMode(const sp<DisplayDevice>& display, ColorMode* outMode,
2341 Dataspace* outDataSpace, RenderIntent* outRenderIntent) const {
2342 if (mDisplayColorSetting == DisplayColorSetting::UNMANAGED) {
2343 *outMode = ColorMode::NATIVE;
2344 *outDataSpace = Dataspace::UNKNOWN;
2345 *outRenderIntent = RenderIntent::COLORIMETRIC;
2346 return;
2347 }
2348
2349 Dataspace hdrDataSpace;
2350 bool isHdrClientComposition = false;
2351 Dataspace bestDataSpace = getBestDataspace(display, &hdrDataSpace, &isHdrClientComposition);
2352
2353 auto* profile = display->getCompositionDisplay()->getDisplayColorProfile();
2354
2355 switch (mForceColorMode) {
2356 case ColorMode::SRGB:
2357 bestDataSpace = Dataspace::V0_SRGB;
2358 break;
2359 case ColorMode::DISPLAY_P3:
2360 bestDataSpace = Dataspace::DISPLAY_P3;
2361 break;
2362 default:
2363 break;
2364 }
2365
2366 // respect hdrDataSpace only when there is no legacy HDR support
2367 const bool isHdr = hdrDataSpace != Dataspace::UNKNOWN &&
2368 !profile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
2369 if (isHdr) {
2370 bestDataSpace = hdrDataSpace;
2371 }
2372
2373 RenderIntent intent;
2374 switch (mDisplayColorSetting) {
2375 case DisplayColorSetting::MANAGED:
2376 case DisplayColorSetting::UNMANAGED:
2377 intent = isHdr ? RenderIntent::TONE_MAP_COLORIMETRIC : RenderIntent::COLORIMETRIC;
2378 break;
2379 case DisplayColorSetting::ENHANCED:
2380 intent = isHdr ? RenderIntent::TONE_MAP_ENHANCE : RenderIntent::ENHANCE;
2381 break;
2382 default: // vendor display color setting
2383 intent = static_cast<RenderIntent>(mDisplayColorSetting);
2384 break;
2385 }
2386
2387 profile->getBestColorMode(bestDataSpace, intent, outDataSpace, outMode, outRenderIntent);
2388 }
2389
beginFrame(const sp<DisplayDevice> & displayDevice)2390 void SurfaceFlinger::beginFrame(const sp<DisplayDevice>& displayDevice) {
2391 auto display = displayDevice->getCompositionDisplay();
2392 const auto& displayState = display->getState();
2393
2394 bool dirty = !display->getDirtyRegion(false).isEmpty();
2395 bool empty = displayDevice->getVisibleLayersSortedByZ().size() == 0;
2396 bool wasEmpty = !displayState.lastCompositionHadVisibleLayers;
2397
2398 // If nothing has changed (!dirty), don't recompose.
2399 // If something changed, but we don't currently have any visible layers,
2400 // and didn't when we last did a composition, then skip it this time.
2401 // The second rule does two things:
2402 // - When all layers are removed from a display, we'll emit one black
2403 // frame, then nothing more until we get new layers.
2404 // - When a display is created with a private layer stack, we won't
2405 // emit any black frames until a layer is added to the layer stack.
2406 bool mustRecompose = dirty && !(empty && wasEmpty);
2407
2408 const char flagPrefix[] = {'-', '+'};
2409 static_cast<void>(flagPrefix);
2410 ALOGV_IF(displayDevice->isVirtual(), "%s: %s composition for %s (%cdirty %cempty %cwasEmpty)",
2411 __FUNCTION__, mustRecompose ? "doing" : "skipping",
2412 displayDevice->getDebugName().c_str(), flagPrefix[dirty], flagPrefix[empty],
2413 flagPrefix[wasEmpty]);
2414
2415 display->getRenderSurface()->beginFrame(mustRecompose);
2416
2417 if (mustRecompose) {
2418 display->editState().lastCompositionHadVisibleLayers = !empty;
2419 }
2420 }
2421
prepareFrame(const sp<DisplayDevice> & displayDevice)2422 void SurfaceFlinger::prepareFrame(const sp<DisplayDevice>& displayDevice) {
2423 auto display = displayDevice->getCompositionDisplay();
2424 const auto& displayState = display->getState();
2425
2426 if (!displayState.isEnabled) {
2427 return;
2428 }
2429
2430 status_t result = display->getRenderSurface()->prepareFrame();
2431 ALOGE_IF(result != NO_ERROR, "prepareFrame failed for %s: %d (%s)",
2432 displayDevice->getDebugName().c_str(), result, strerror(-result));
2433 }
2434
doComposition(const sp<DisplayDevice> & displayDevice,bool repaintEverything)2435 void SurfaceFlinger::doComposition(const sp<DisplayDevice>& displayDevice, bool repaintEverything) {
2436 ATRACE_CALL();
2437 ALOGV("doComposition");
2438
2439 auto display = displayDevice->getCompositionDisplay();
2440 const auto& displayState = display->getState();
2441
2442 if (displayState.isEnabled) {
2443 // transform the dirty region into this screen's coordinate space
2444 const Region dirtyRegion = display->getDirtyRegion(repaintEverything);
2445
2446 // repaint the framebuffer (if needed)
2447 doDisplayComposition(displayDevice, dirtyRegion);
2448
2449 display->editState().dirtyRegion.clear();
2450 display->getRenderSurface()->flip();
2451 }
2452 postFramebuffer(displayDevice);
2453 }
2454
postFrame()2455 void SurfaceFlinger::postFrame()
2456 {
2457 // |mStateLock| not needed as we are on the main thread
2458 const auto display = getDefaultDisplayDeviceLocked();
2459 if (display && getHwComposer().isConnected(*display->getId())) {
2460 uint32_t flipCount = display->getPageFlipCount();
2461 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
2462 logFrameStats();
2463 }
2464 }
2465 }
2466
postFramebuffer(const sp<DisplayDevice> & displayDevice)2467 void SurfaceFlinger::postFramebuffer(const sp<DisplayDevice>& displayDevice) {
2468 ATRACE_CALL();
2469 ALOGV("postFramebuffer");
2470
2471 auto display = displayDevice->getCompositionDisplay();
2472 const auto& displayState = display->getState();
2473 const auto displayId = display->getId();
2474
2475 if (displayState.isEnabled) {
2476 if (displayId) {
2477 getHwComposer().presentAndGetReleaseFences(*displayId);
2478 }
2479 display->getRenderSurface()->onPresentDisplayCompleted();
2480 for (auto& layer : display->getOutputLayersOrderedByZ()) {
2481 sp<Fence> releaseFence = Fence::NO_FENCE;
2482 bool usedClientComposition = true;
2483
2484 // The layer buffer from the previous frame (if any) is released
2485 // by HWC only when the release fence from this frame (if any) is
2486 // signaled. Always get the release fence from HWC first.
2487 if (layer->getState().hwc) {
2488 const auto& hwcState = *layer->getState().hwc;
2489 releaseFence =
2490 getHwComposer().getLayerReleaseFence(*displayId, hwcState.hwcLayer.get());
2491 usedClientComposition =
2492 hwcState.hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT;
2493 }
2494
2495 // If the layer was client composited in the previous frame, we
2496 // need to merge with the previous client target acquire fence.
2497 // Since we do not track that, always merge with the current
2498 // client target acquire fence when it is available, even though
2499 // this is suboptimal.
2500 if (usedClientComposition) {
2501 releaseFence =
2502 Fence::merge("LayerRelease", releaseFence,
2503 display->getRenderSurface()->getClientTargetAcquireFence());
2504 }
2505
2506 layer->getLayerFE().onLayerDisplayed(releaseFence);
2507 }
2508
2509 // We've got a list of layers needing fences, that are disjoint with
2510 // display->getVisibleLayersSortedByZ. The best we can do is to
2511 // supply them with the present fence.
2512 if (!displayDevice->getLayersNeedingFences().isEmpty()) {
2513 sp<Fence> presentFence =
2514 displayId ? getHwComposer().getPresentFence(*displayId) : Fence::NO_FENCE;
2515 for (auto& layer : displayDevice->getLayersNeedingFences()) {
2516 layer->getCompositionLayer()->getLayerFE()->onLayerDisplayed(presentFence);
2517 }
2518 }
2519
2520 if (displayId) {
2521 getHwComposer().clearReleaseFences(*displayId);
2522 }
2523 }
2524 }
2525
handleTransaction(uint32_t transactionFlags)2526 void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
2527 {
2528 ATRACE_CALL();
2529
2530 // here we keep a copy of the drawing state (that is the state that's
2531 // going to be overwritten by handleTransactionLocked()) outside of
2532 // mStateLock so that the side-effects of the State assignment
2533 // don't happen with mStateLock held (which can cause deadlocks).
2534 State drawingState(mDrawingState);
2535
2536 Mutex::Autolock _l(mStateLock);
2537 mDebugInTransaction = systemTime();
2538
2539 // Here we're guaranteed that some transaction flags are set
2540 // so we can call handleTransactionLocked() unconditionally.
2541 // We call getTransactionFlags(), which will also clear the flags,
2542 // with mStateLock held to guarantee that mCurrentState won't change
2543 // until the transaction is committed.
2544
2545 mVsyncModulator.onTransactionHandled();
2546 transactionFlags = getTransactionFlags(eTransactionMask);
2547 handleTransactionLocked(transactionFlags);
2548
2549 mDebugInTransaction = 0;
2550 invalidateHwcGeometry();
2551 // here the transaction has been committed
2552 }
2553
processDisplayHotplugEventsLocked()2554 void SurfaceFlinger::processDisplayHotplugEventsLocked() {
2555 for (const auto& event : mPendingHotplugEvents) {
2556 const std::optional<DisplayIdentificationInfo> info =
2557 getHwComposer().onHotplug(event.hwcDisplayId, event.connection);
2558
2559 if (!info) {
2560 continue;
2561 }
2562
2563 if (event.connection == HWC2::Connection::Connected) {
2564 if (!mPhysicalDisplayTokens.count(info->id)) {
2565 ALOGV("Creating display %s", to_string(info->id).c_str());
2566 if (event.hwcDisplayId == getHwComposer().getInternalHwcDisplayId()) {
2567 initScheduler(info->id);
2568 }
2569 mPhysicalDisplayTokens[info->id] = new BBinder();
2570 DisplayDeviceState state;
2571 state.displayId = info->id;
2572 state.isSecure = true; // All physical displays are currently considered secure.
2573 state.displayName = info->name;
2574 mCurrentState.displays.add(mPhysicalDisplayTokens[info->id], state);
2575 mInterceptor->saveDisplayCreation(state);
2576 }
2577 } else {
2578 ALOGV("Removing display %s", to_string(info->id).c_str());
2579
2580 ssize_t index = mCurrentState.displays.indexOfKey(mPhysicalDisplayTokens[info->id]);
2581 if (index >= 0) {
2582 const DisplayDeviceState& state = mCurrentState.displays.valueAt(index);
2583 mInterceptor->saveDisplayDeletion(state.sequenceId);
2584 mCurrentState.displays.removeItemsAt(index);
2585 }
2586 mPhysicalDisplayTokens.erase(info->id);
2587 }
2588
2589 processDisplayChangesLocked();
2590 }
2591
2592 mPendingHotplugEvents.clear();
2593 }
2594
dispatchDisplayHotplugEvent(PhysicalDisplayId displayId,bool connected)2595 void SurfaceFlinger::dispatchDisplayHotplugEvent(PhysicalDisplayId displayId, bool connected) {
2596 mScheduler->hotplugReceived(mAppConnectionHandle, displayId, connected);
2597 mScheduler->hotplugReceived(mSfConnectionHandle, displayId, connected);
2598 }
2599
setupNewDisplayDeviceInternal(const wp<IBinder> & displayToken,const std::optional<DisplayId> & displayId,const DisplayDeviceState & state,const sp<compositionengine::DisplaySurface> & dispSurface,const sp<IGraphicBufferProducer> & producer)2600 sp<DisplayDevice> SurfaceFlinger::setupNewDisplayDeviceInternal(
2601 const wp<IBinder>& displayToken, const std::optional<DisplayId>& displayId,
2602 const DisplayDeviceState& state, const sp<compositionengine::DisplaySurface>& dispSurface,
2603 const sp<IGraphicBufferProducer>& producer) {
2604 DisplayDeviceCreationArgs creationArgs(this, displayToken, displayId);
2605 creationArgs.sequenceId = state.sequenceId;
2606 creationArgs.isVirtual = state.isVirtual();
2607 creationArgs.isSecure = state.isSecure;
2608 creationArgs.displaySurface = dispSurface;
2609 creationArgs.hasWideColorGamut = false;
2610 creationArgs.supportedPerFrameMetadata = 0;
2611
2612 const bool isInternalDisplay = displayId && displayId == getInternalDisplayIdLocked();
2613 creationArgs.isPrimary = isInternalDisplay;
2614
2615 if (useColorManagement && displayId) {
2616 std::vector<ColorMode> modes = getHwComposer().getColorModes(*displayId);
2617 for (ColorMode colorMode : modes) {
2618 if (isWideColorMode(colorMode)) {
2619 creationArgs.hasWideColorGamut = true;
2620 }
2621
2622 std::vector<RenderIntent> renderIntents =
2623 getHwComposer().getRenderIntents(*displayId, colorMode);
2624 creationArgs.hwcColorModes.emplace(colorMode, renderIntents);
2625 }
2626 }
2627
2628 if (displayId) {
2629 getHwComposer().getHdrCapabilities(*displayId, &creationArgs.hdrCapabilities);
2630 creationArgs.supportedPerFrameMetadata =
2631 getHwComposer().getSupportedPerFrameMetadata(*displayId);
2632 }
2633
2634 auto nativeWindowSurface = getFactory().createNativeWindowSurface(producer);
2635 auto nativeWindow = nativeWindowSurface->getNativeWindow();
2636 creationArgs.nativeWindow = nativeWindow;
2637
2638 // Make sure that composition can never be stalled by a virtual display
2639 // consumer that isn't processing buffers fast enough. We have to do this
2640 // here, in case the display is composed entirely by HWC.
2641 if (state.isVirtual()) {
2642 nativeWindow->setSwapInterval(nativeWindow.get(), 0);
2643 }
2644
2645 creationArgs.displayInstallOrientation =
2646 isInternalDisplay ? primaryDisplayOrientation : DisplayState::eOrientationDefault;
2647
2648 // virtual displays are always considered enabled
2649 creationArgs.initialPowerMode = state.isVirtual() ? HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
2650
2651 sp<DisplayDevice> display = getFactory().createDisplayDevice(std::move(creationArgs));
2652
2653 if (maxFrameBufferAcquiredBuffers >= 3) {
2654 nativeWindowSurface->preallocateBuffers();
2655 }
2656
2657 ColorMode defaultColorMode = ColorMode::NATIVE;
2658 Dataspace defaultDataSpace = Dataspace::UNKNOWN;
2659 if (display->hasWideColorGamut()) {
2660 defaultColorMode = ColorMode::SRGB;
2661 defaultDataSpace = Dataspace::V0_SRGB;
2662 }
2663 display->getCompositionDisplay()->setColorMode(defaultColorMode, defaultDataSpace,
2664 RenderIntent::COLORIMETRIC);
2665 if (!state.isVirtual()) {
2666 LOG_ALWAYS_FATAL_IF(!displayId);
2667 display->setActiveConfig(getHwComposer().getActiveConfigIndex(*displayId));
2668 }
2669
2670 display->setLayerStack(state.layerStack);
2671 display->setProjection(state.orientation, state.viewport, state.frame);
2672 display->setDisplayName(state.displayName);
2673
2674 return display;
2675 }
2676
processDisplayChangesLocked()2677 void SurfaceFlinger::processDisplayChangesLocked() {
2678 // here we take advantage of Vector's copy-on-write semantics to
2679 // improve performance by skipping the transaction entirely when
2680 // know that the lists are identical
2681 const KeyedVector<wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
2682 const KeyedVector<wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
2683 if (!curr.isIdenticalTo(draw)) {
2684 mVisibleRegionsDirty = true;
2685 const size_t cc = curr.size();
2686 size_t dc = draw.size();
2687
2688 // find the displays that were removed
2689 // (ie: in drawing state but not in current state)
2690 // also handle displays that changed
2691 // (ie: displays that are in both lists)
2692 for (size_t i = 0; i < dc;) {
2693 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
2694 if (j < 0) {
2695 // in drawing state but not in current state
2696 if (const auto display = getDisplayDeviceLocked(draw.keyAt(i))) {
2697 // Save display ID before disconnecting.
2698 const auto displayId = display->getId();
2699 display->disconnect();
2700
2701 if (!display->isVirtual()) {
2702 LOG_ALWAYS_FATAL_IF(!displayId);
2703 dispatchDisplayHotplugEvent(displayId->value, false);
2704 }
2705 }
2706
2707 mDisplays.erase(draw.keyAt(i));
2708 } else {
2709 // this display is in both lists. see if something changed.
2710 const DisplayDeviceState& state(curr[j]);
2711 const wp<IBinder>& displayToken = curr.keyAt(j);
2712 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
2713 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
2714 if (state_binder != draw_binder) {
2715 // changing the surface is like destroying and
2716 // recreating the DisplayDevice, so we just remove it
2717 // from the drawing state, so that it get re-added
2718 // below.
2719 if (const auto display = getDisplayDeviceLocked(displayToken)) {
2720 display->disconnect();
2721 }
2722 mDisplays.erase(displayToken);
2723 mDrawingState.displays.removeItemsAt(i);
2724 dc--;
2725 // at this point we must loop to the next item
2726 continue;
2727 }
2728
2729 if (const auto display = getDisplayDeviceLocked(displayToken)) {
2730 if (state.layerStack != draw[i].layerStack) {
2731 display->setLayerStack(state.layerStack);
2732 }
2733 if ((state.orientation != draw[i].orientation) ||
2734 (state.viewport != draw[i].viewport) || (state.frame != draw[i].frame)) {
2735 display->setProjection(state.orientation, state.viewport, state.frame);
2736 }
2737 if (state.width != draw[i].width || state.height != draw[i].height) {
2738 display->setDisplaySize(state.width, state.height);
2739 }
2740 }
2741 }
2742 ++i;
2743 }
2744
2745 // find displays that were added
2746 // (ie: in current state but not in drawing state)
2747 for (size_t i = 0; i < cc; i++) {
2748 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
2749 const DisplayDeviceState& state(curr[i]);
2750
2751 sp<compositionengine::DisplaySurface> dispSurface;
2752 sp<IGraphicBufferProducer> producer;
2753 sp<IGraphicBufferProducer> bqProducer;
2754 sp<IGraphicBufferConsumer> bqConsumer;
2755 getFactory().createBufferQueue(&bqProducer, &bqConsumer, false);
2756
2757 std::optional<DisplayId> displayId;
2758 if (state.isVirtual()) {
2759 // Virtual displays without a surface are dormant:
2760 // they have external state (layer stack, projection,
2761 // etc.) but no internal state (i.e. a DisplayDevice).
2762 if (state.surface != nullptr) {
2763 // Allow VR composer to use virtual displays.
2764 if (mUseHwcVirtualDisplays || getHwComposer().isUsingVrComposer()) {
2765 int width = 0;
2766 int status = state.surface->query(NATIVE_WINDOW_WIDTH, &width);
2767 ALOGE_IF(status != NO_ERROR, "Unable to query width (%d)", status);
2768 int height = 0;
2769 status = state.surface->query(NATIVE_WINDOW_HEIGHT, &height);
2770 ALOGE_IF(status != NO_ERROR, "Unable to query height (%d)", status);
2771 int intFormat = 0;
2772 status = state.surface->query(NATIVE_WINDOW_FORMAT, &intFormat);
2773 ALOGE_IF(status != NO_ERROR, "Unable to query format (%d)", status);
2774 auto format = static_cast<ui::PixelFormat>(intFormat);
2775
2776 displayId =
2777 getHwComposer().allocateVirtualDisplay(width, height, &format);
2778 }
2779
2780 // TODO: Plumb requested format back up to consumer
2781
2782 sp<VirtualDisplaySurface> vds =
2783 new VirtualDisplaySurface(getHwComposer(), displayId, state.surface,
2784 bqProducer, bqConsumer,
2785 state.displayName);
2786
2787 dispSurface = vds;
2788 producer = vds;
2789 }
2790 } else {
2791 ALOGE_IF(state.surface != nullptr,
2792 "adding a supported display, but rendering "
2793 "surface is provided (%p), ignoring it",
2794 state.surface.get());
2795
2796 displayId = state.displayId;
2797 LOG_ALWAYS_FATAL_IF(!displayId);
2798 dispSurface = new FramebufferSurface(getHwComposer(), *displayId, bqConsumer);
2799 producer = bqProducer;
2800 }
2801
2802 const wp<IBinder>& displayToken = curr.keyAt(i);
2803 if (dispSurface != nullptr) {
2804 mDisplays.emplace(displayToken,
2805 setupNewDisplayDeviceInternal(displayToken, displayId, state,
2806 dispSurface, producer));
2807 if (!state.isVirtual()) {
2808 LOG_ALWAYS_FATAL_IF(!displayId);
2809 dispatchDisplayHotplugEvent(displayId->value, true);
2810 }
2811 }
2812 }
2813 }
2814 }
2815
2816 mDrawingState.displays = mCurrentState.displays;
2817 }
2818
handleTransactionLocked(uint32_t transactionFlags)2819 void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
2820 {
2821 // Notify all layers of available frames
2822 mCurrentState.traverseInZOrder([](Layer* layer) {
2823 layer->notifyAvailableFrames();
2824 });
2825
2826 /*
2827 * Traversal of the children
2828 * (perform the transaction for each of them if needed)
2829 */
2830
2831 if ((transactionFlags & eTraversalNeeded) || mTraversalNeededMainThread) {
2832 mCurrentState.traverseInZOrder([&](Layer* layer) {
2833 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
2834 if (!trFlags) return;
2835
2836 const uint32_t flags = layer->doTransaction(0);
2837 if (flags & Layer::eVisibleRegion)
2838 mVisibleRegionsDirty = true;
2839
2840 if (flags & Layer::eInputInfoChanged) {
2841 mInputInfoChanged = true;
2842 }
2843 });
2844 mTraversalNeededMainThread = false;
2845 }
2846
2847 /*
2848 * Perform display own transactions if needed
2849 */
2850
2851 if (transactionFlags & eDisplayTransactionNeeded) {
2852 processDisplayChangesLocked();
2853 processDisplayHotplugEventsLocked();
2854 }
2855
2856 if (transactionFlags & (eDisplayLayerStackChanged|eDisplayTransactionNeeded)) {
2857 // The transform hint might have changed for some layers
2858 // (either because a display has changed, or because a layer
2859 // as changed).
2860 //
2861 // Walk through all the layers in currentLayers,
2862 // and update their transform hint.
2863 //
2864 // If a layer is visible only on a single display, then that
2865 // display is used to calculate the hint, otherwise we use the
2866 // default display.
2867 //
2868 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
2869 // the hint is set before we acquire a buffer from the surface texture.
2870 //
2871 // NOTE: layer transactions have taken place already, so we use their
2872 // drawing state. However, SurfaceFlinger's own transaction has not
2873 // happened yet, so we must use the current state layer list
2874 // (soon to become the drawing state list).
2875 //
2876 sp<const DisplayDevice> hintDisplay;
2877 uint32_t currentlayerStack = 0;
2878 bool first = true;
2879 mCurrentState.traverseInZOrder([&](Layer* layer) {
2880 // NOTE: we rely on the fact that layers are sorted by
2881 // layerStack first (so we don't have to traverse the list
2882 // of displays for every layer).
2883 uint32_t layerStack = layer->getLayerStack();
2884 if (first || currentlayerStack != layerStack) {
2885 currentlayerStack = layerStack;
2886 // figure out if this layerstack is mirrored
2887 // (more than one display) if so, pick the default display,
2888 // if not, pick the only display it's on.
2889 hintDisplay = nullptr;
2890 for (const auto& [token, display] : mDisplays) {
2891 if (display->getCompositionDisplay()
2892 ->belongsInOutput(layer->getLayerStack(),
2893 layer->getPrimaryDisplayOnly())) {
2894 if (hintDisplay) {
2895 hintDisplay = nullptr;
2896 break;
2897 } else {
2898 hintDisplay = display;
2899 }
2900 }
2901 }
2902 }
2903
2904 if (!hintDisplay) {
2905 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
2906 // redraw after transform hint changes. See bug 8508397.
2907
2908 // could be null when this layer is using a layerStack
2909 // that is not visible on any display. Also can occur at
2910 // screen off/on times.
2911 hintDisplay = getDefaultDisplayDeviceLocked();
2912 }
2913
2914 // could be null if there is no display available at all to get
2915 // the transform hint from.
2916 if (hintDisplay) {
2917 layer->updateTransformHint(hintDisplay);
2918 }
2919
2920 first = false;
2921 });
2922 }
2923
2924
2925 /*
2926 * Perform our own transaction if needed
2927 */
2928
2929 if (mLayersAdded) {
2930 mLayersAdded = false;
2931 // Layers have been added.
2932 mVisibleRegionsDirty = true;
2933 }
2934
2935 // some layers might have been removed, so
2936 // we need to update the regions they're exposing.
2937 if (mLayersRemoved) {
2938 mLayersRemoved = false;
2939 mVisibleRegionsDirty = true;
2940 mDrawingState.traverseInZOrder([&](Layer* layer) {
2941 if (mLayersPendingRemoval.indexOf(layer) >= 0) {
2942 // this layer is not visible anymore
2943 Region visibleReg;
2944 visibleReg.set(layer->getScreenBounds());
2945 invalidateLayerStack(layer, visibleReg);
2946 }
2947 });
2948 }
2949
2950 commitInputWindowCommands();
2951 commitTransaction();
2952 }
2953
updateInputFlinger()2954 void SurfaceFlinger::updateInputFlinger() {
2955 ATRACE_CALL();
2956 if (!mInputFlinger) {
2957 return;
2958 }
2959
2960 if (mVisibleRegionsDirty || mInputInfoChanged) {
2961 mInputInfoChanged = false;
2962 updateInputWindowInfo();
2963 } else if (mInputWindowCommands.syncInputWindows) {
2964 // If the caller requested to sync input windows, but there are no
2965 // changes to input windows, notify immediately.
2966 setInputWindowsFinished();
2967 }
2968
2969 mInputWindowCommands.clear();
2970 }
2971
updateInputWindowInfo()2972 void SurfaceFlinger::updateInputWindowInfo() {
2973 std::vector<InputWindowInfo> inputHandles;
2974
2975 mDrawingState.traverseInReverseZOrder([&](Layer* layer) {
2976 if (layer->hasInput()) {
2977 // When calculating the screen bounds we ignore the transparent region since it may
2978 // result in an unwanted offset.
2979 inputHandles.push_back(layer->fillInputInfo());
2980 }
2981 });
2982
2983 mInputFlinger->setInputWindows(inputHandles,
2984 mInputWindowCommands.syncInputWindows ? mSetInputWindowsListener
2985 : nullptr);
2986 }
2987
commitInputWindowCommands()2988 void SurfaceFlinger::commitInputWindowCommands() {
2989 mInputWindowCommands = mPendingInputWindowCommands;
2990 mPendingInputWindowCommands.clear();
2991 }
2992
updateCursorAsync()2993 void SurfaceFlinger::updateCursorAsync()
2994 {
2995 for (const auto& [token, display] : mDisplays) {
2996 if (!display->getId()) {
2997 continue;
2998 }
2999
3000 for (auto& layer : display->getVisibleLayersSortedByZ()) {
3001 layer->updateCursorPosition(display);
3002 }
3003 }
3004 }
3005
latchAndReleaseBuffer(const sp<Layer> & layer)3006 void SurfaceFlinger::latchAndReleaseBuffer(const sp<Layer>& layer) {
3007 if (layer->hasReadyFrame()) {
3008 bool ignored = false;
3009 layer->latchBuffer(ignored, systemTime());
3010 }
3011 layer->releasePendingBuffer(systemTime());
3012 }
3013
initScheduler(DisplayId primaryDisplayId)3014 void SurfaceFlinger::initScheduler(DisplayId primaryDisplayId) {
3015 if (mScheduler) {
3016 // In practice it's not allowed to hotplug in/out the primary display once it's been
3017 // connected during startup, but some tests do it, so just warn and return.
3018 ALOGW("Can't re-init scheduler");
3019 return;
3020 }
3021
3022 int currentConfig = getHwComposer().getActiveConfigIndex(primaryDisplayId);
3023 mRefreshRateConfigs =
3024 std::make_unique<scheduler::RefreshRateConfigs>(refresh_rate_switching(false),
3025 getHwComposer().getConfigs(
3026 primaryDisplayId),
3027 currentConfig);
3028 mRefreshRateStats =
3029 std::make_unique<scheduler::RefreshRateStats>(*mRefreshRateConfigs, *mTimeStats,
3030 currentConfig, HWC_POWER_MODE_OFF);
3031 mRefreshRateStats->setConfigMode(currentConfig);
3032
3033 // start the EventThread
3034 mScheduler =
3035 getFactory().createScheduler([this](bool enabled) { setPrimaryVsyncEnabled(enabled); },
3036 *mRefreshRateConfigs);
3037 mAppConnectionHandle =
3038 mScheduler->createConnection("app", mVsyncModulator.getOffsets().app,
3039 mPhaseOffsets->getOffsetThresholdForNextVsync(),
3040 impl::EventThread::InterceptVSyncsCallback());
3041 mSfConnectionHandle =
3042 mScheduler->createConnection("sf", mVsyncModulator.getOffsets().sf,
3043 mPhaseOffsets->getOffsetThresholdForNextVsync(),
3044 [this](nsecs_t timestamp) {
3045 mInterceptor->saveVSyncEvent(timestamp);
3046 });
3047
3048 mEventQueue->setEventConnection(mScheduler->getEventConnection(mSfConnectionHandle));
3049 mVsyncModulator.setSchedulerAndHandles(mScheduler.get(), mAppConnectionHandle.get(),
3050 mSfConnectionHandle.get());
3051
3052 mRegionSamplingThread =
3053 new RegionSamplingThread(*this, *mScheduler,
3054 RegionSamplingThread::EnvironmentTimingTunables());
3055
3056 mScheduler->setChangeRefreshRateCallback(
3057 [this](RefreshRateType type, Scheduler::ConfigEvent event) {
3058 Mutex::Autolock lock(mStateLock);
3059 setRefreshRateTo(type, event);
3060 });
3061 }
3062
commitTransaction()3063 void SurfaceFlinger::commitTransaction()
3064 {
3065 if (!mLayersPendingRemoval.isEmpty()) {
3066 // Notify removed layers now that they can't be drawn from
3067 for (const auto& l : mLayersPendingRemoval) {
3068 recordBufferingStats(l->getName().string(),
3069 l->getOccupancyHistory(true));
3070
3071 // Ensure any buffers set to display on any children are released.
3072 if (l->isRemovedFromCurrentState()) {
3073 latchAndReleaseBuffer(l);
3074 }
3075
3076 // If the layer has been removed and has no parent, then it will not be reachable
3077 // when traversing layers on screen. Add the layer to the offscreenLayers set to
3078 // ensure we can copy its current to drawing state.
3079 if (!l->getParent()) {
3080 mOffscreenLayers.emplace(l.get());
3081 }
3082 }
3083 mLayersPendingRemoval.clear();
3084 }
3085
3086 // If this transaction is part of a window animation then the next frame
3087 // we composite should be considered an animation as well.
3088 mAnimCompositionPending = mAnimTransactionPending;
3089
3090 withTracingLock([&]() {
3091 mDrawingState = mCurrentState;
3092 // clear the "changed" flags in current state
3093 mCurrentState.colorMatrixChanged = false;
3094
3095 mDrawingState.traverseInZOrder([&](Layer* layer) {
3096 layer->commitChildList();
3097
3098 // If the layer can be reached when traversing mDrawingState, then the layer is no
3099 // longer offscreen. Remove the layer from the offscreenLayer set.
3100 if (mOffscreenLayers.count(layer)) {
3101 mOffscreenLayers.erase(layer);
3102 }
3103 });
3104
3105 commitOffscreenLayers();
3106 });
3107
3108 mTransactionPending = false;
3109 mAnimTransactionPending = false;
3110 mTransactionCV.broadcast();
3111 }
3112
withTracingLock(std::function<void ()> lockedOperation)3113 void SurfaceFlinger::withTracingLock(std::function<void()> lockedOperation) {
3114 if (mTracingEnabledChanged) {
3115 mTracingEnabled = mTracing.isEnabled();
3116 mTracingEnabledChanged = false;
3117 }
3118
3119 // Synchronize with Tracing thread
3120 std::unique_lock<std::mutex> lock;
3121 if (mTracingEnabled) {
3122 lock = std::unique_lock<std::mutex>(mDrawingStateLock);
3123 }
3124
3125 lockedOperation();
3126
3127 // Synchronize with Tracing thread
3128 if (mTracingEnabled) {
3129 lock.unlock();
3130 }
3131 }
3132
commitOffscreenLayers()3133 void SurfaceFlinger::commitOffscreenLayers() {
3134 for (Layer* offscreenLayer : mOffscreenLayers) {
3135 offscreenLayer->traverseInZOrder(LayerVector::StateSet::Drawing, [](Layer* layer) {
3136 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
3137 if (!trFlags) return;
3138
3139 layer->doTransaction(0);
3140 layer->commitChildList();
3141 });
3142 }
3143 }
3144
computeVisibleRegions(const sp<const DisplayDevice> & displayDevice,Region & outDirtyRegion,Region & outOpaqueRegion)3145 void SurfaceFlinger::computeVisibleRegions(const sp<const DisplayDevice>& displayDevice,
3146 Region& outDirtyRegion, Region& outOpaqueRegion) {
3147 ATRACE_CALL();
3148 ALOGV("computeVisibleRegions");
3149
3150 auto display = displayDevice->getCompositionDisplay();
3151
3152 Region aboveOpaqueLayers;
3153 Region aboveCoveredLayers;
3154 Region dirty;
3155
3156 outDirtyRegion.clear();
3157
3158 mDrawingState.traverseInReverseZOrder([&](Layer* layer) {
3159 // start with the whole surface at its current location
3160 const Layer::State& s(layer->getDrawingState());
3161
3162 // only consider the layers on the given layer stack
3163 if (!display->belongsInOutput(layer->getLayerStack(), layer->getPrimaryDisplayOnly())) {
3164 return;
3165 }
3166
3167 /*
3168 * opaqueRegion: area of a surface that is fully opaque.
3169 */
3170 Region opaqueRegion;
3171
3172 /*
3173 * visibleRegion: area of a surface that is visible on screen
3174 * and not fully transparent. This is essentially the layer's
3175 * footprint minus the opaque regions above it.
3176 * Areas covered by a translucent surface are considered visible.
3177 */
3178 Region visibleRegion;
3179
3180 /*
3181 * coveredRegion: area of a surface that is covered by all
3182 * visible regions above it (which includes the translucent areas).
3183 */
3184 Region coveredRegion;
3185
3186 /*
3187 * transparentRegion: area of a surface that is hinted to be completely
3188 * transparent. This is only used to tell when the layer has no visible
3189 * non-transparent regions and can be removed from the layer list. It
3190 * does not affect the visibleRegion of this layer or any layers
3191 * beneath it. The hint may not be correct if apps don't respect the
3192 * SurfaceView restrictions (which, sadly, some don't).
3193 */
3194 Region transparentRegion;
3195
3196
3197 // handle hidden surfaces by setting the visible region to empty
3198 if (CC_LIKELY(layer->isVisible())) {
3199 const bool translucent = !layer->isOpaque(s);
3200 Rect bounds(layer->getScreenBounds());
3201
3202 visibleRegion.set(bounds);
3203 ui::Transform tr = layer->getTransform();
3204 if (!visibleRegion.isEmpty()) {
3205 // Remove the transparent area from the visible region
3206 if (translucent) {
3207 if (tr.preserveRects()) {
3208 // transform the transparent region
3209 transparentRegion = tr.transform(layer->getActiveTransparentRegion(s));
3210 } else {
3211 // transformation too complex, can't do the
3212 // transparent region optimization.
3213 transparentRegion.clear();
3214 }
3215 }
3216
3217 // compute the opaque region
3218 const int32_t layerOrientation = tr.getOrientation();
3219 if (layer->getAlpha() == 1.0f && !translucent &&
3220 layer->getRoundedCornerState().radius == 0.0f &&
3221 ((layerOrientation & ui::Transform::ROT_INVALID) == false)) {
3222 // the opaque region is the layer's footprint
3223 opaqueRegion = visibleRegion;
3224 }
3225 }
3226 }
3227
3228 if (visibleRegion.isEmpty()) {
3229 layer->clearVisibilityRegions();
3230 return;
3231 }
3232
3233 // Clip the covered region to the visible region
3234 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
3235
3236 // Update aboveCoveredLayers for next (lower) layer
3237 aboveCoveredLayers.orSelf(visibleRegion);
3238
3239 // subtract the opaque region covered by the layers above us
3240 visibleRegion.subtractSelf(aboveOpaqueLayers);
3241
3242 // compute this layer's dirty region
3243 if (layer->contentDirty) {
3244 // we need to invalidate the whole region
3245 dirty = visibleRegion;
3246 // as well, as the old visible region
3247 dirty.orSelf(layer->visibleRegion);
3248 layer->contentDirty = false;
3249 } else {
3250 /* compute the exposed region:
3251 * the exposed region consists of two components:
3252 * 1) what's VISIBLE now and was COVERED before
3253 * 2) what's EXPOSED now less what was EXPOSED before
3254 *
3255 * note that (1) is conservative, we start with the whole
3256 * visible region but only keep what used to be covered by
3257 * something -- which mean it may have been exposed.
3258 *
3259 * (2) handles areas that were not covered by anything but got
3260 * exposed because of a resize.
3261 */
3262 const Region newExposed = visibleRegion - coveredRegion;
3263 const Region oldVisibleRegion = layer->visibleRegion;
3264 const Region oldCoveredRegion = layer->coveredRegion;
3265 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
3266 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
3267 }
3268 dirty.subtractSelf(aboveOpaqueLayers);
3269
3270 // accumulate to the screen dirty region
3271 outDirtyRegion.orSelf(dirty);
3272
3273 // Update aboveOpaqueLayers for next (lower) layer
3274 aboveOpaqueLayers.orSelf(opaqueRegion);
3275
3276 // Store the visible region in screen space
3277 layer->setVisibleRegion(visibleRegion);
3278 layer->setCoveredRegion(coveredRegion);
3279 layer->setVisibleNonTransparentRegion(
3280 visibleRegion.subtract(transparentRegion));
3281 });
3282
3283 outOpaqueRegion = aboveOpaqueLayers;
3284 }
3285
invalidateLayerStack(const sp<const Layer> & layer,const Region & dirty)3286 void SurfaceFlinger::invalidateLayerStack(const sp<const Layer>& layer, const Region& dirty) {
3287 for (const auto& [token, displayDevice] : mDisplays) {
3288 auto display = displayDevice->getCompositionDisplay();
3289 if (display->belongsInOutput(layer->getLayerStack(), layer->getPrimaryDisplayOnly())) {
3290 display->editState().dirtyRegion.orSelf(dirty);
3291 }
3292 }
3293 }
3294
handlePageFlip()3295 bool SurfaceFlinger::handlePageFlip()
3296 {
3297 ATRACE_CALL();
3298 ALOGV("handlePageFlip");
3299
3300 nsecs_t latchTime = systemTime();
3301
3302 bool visibleRegions = false;
3303 bool frameQueued = false;
3304 bool newDataLatched = false;
3305
3306 // Store the set of layers that need updates. This set must not change as
3307 // buffers are being latched, as this could result in a deadlock.
3308 // Example: Two producers share the same command stream and:
3309 // 1.) Layer 0 is latched
3310 // 2.) Layer 0 gets a new frame
3311 // 2.) Layer 1 gets a new frame
3312 // 3.) Layer 1 is latched.
3313 // Display is now waiting on Layer 1's frame, which is behind layer 0's
3314 // second frame. But layer 0's second frame could be waiting on display.
3315 mDrawingState.traverseInZOrder([&](Layer* layer) {
3316 if (layer->hasReadyFrame()) {
3317 frameQueued = true;
3318 const nsecs_t expectedPresentTime = getExpectedPresentTime();
3319 if (layer->shouldPresentNow(expectedPresentTime)) {
3320 mLayersWithQueuedFrames.push_back(layer);
3321 } else {
3322 ATRACE_NAME("!layer->shouldPresentNow()");
3323 layer->useEmptyDamage();
3324 }
3325 } else {
3326 layer->useEmptyDamage();
3327 }
3328 });
3329
3330 if (!mLayersWithQueuedFrames.empty()) {
3331 // mStateLock is needed for latchBuffer as LayerRejecter::reject()
3332 // writes to Layer current state. See also b/119481871
3333 Mutex::Autolock lock(mStateLock);
3334
3335 for (auto& layer : mLayersWithQueuedFrames) {
3336 if (layer->latchBuffer(visibleRegions, latchTime)) {
3337 mLayersPendingRefresh.push_back(layer);
3338 }
3339 layer->useSurfaceDamage();
3340 if (layer->isBufferLatched()) {
3341 newDataLatched = true;
3342 }
3343 }
3344 }
3345
3346 mVisibleRegionsDirty |= visibleRegions;
3347
3348 // If we will need to wake up at some time in the future to deal with a
3349 // queued frame that shouldn't be displayed during this vsync period, wake
3350 // up during the next vsync period to check again.
3351 if (frameQueued && (mLayersWithQueuedFrames.empty() || !newDataLatched)) {
3352 signalLayerUpdate();
3353 }
3354
3355 // enter boot animation on first buffer latch
3356 if (CC_UNLIKELY(mBootStage == BootStage::BOOTLOADER && newDataLatched)) {
3357 ALOGI("Enter boot animation");
3358 mBootStage = BootStage::BOOTANIMATION;
3359 }
3360
3361 // Only continue with the refresh if there is actually new work to do
3362 return !mLayersWithQueuedFrames.empty() && newDataLatched;
3363 }
3364
invalidateHwcGeometry()3365 void SurfaceFlinger::invalidateHwcGeometry()
3366 {
3367 mGeometryInvalid = true;
3368 }
3369
doDisplayComposition(const sp<DisplayDevice> & displayDevice,const Region & inDirtyRegion)3370 void SurfaceFlinger::doDisplayComposition(const sp<DisplayDevice>& displayDevice,
3371 const Region& inDirtyRegion) {
3372 auto display = displayDevice->getCompositionDisplay();
3373 // We only need to actually compose the display if:
3374 // 1) It is being handled by hardware composer, which may need this to
3375 // keep its virtual display state machine in sync, or
3376 // 2) There is work to be done (the dirty region isn't empty)
3377 if (!displayDevice->getId() && inDirtyRegion.isEmpty()) {
3378 ALOGV("Skipping display composition");
3379 return;
3380 }
3381
3382 ALOGV("doDisplayComposition");
3383 base::unique_fd readyFence;
3384 if (!doComposeSurfaces(displayDevice, Region::INVALID_REGION, &readyFence)) return;
3385
3386 // swap buffers (presentation)
3387 display->getRenderSurface()->queueBuffer(std::move(readyFence));
3388 }
3389
doComposeSurfaces(const sp<DisplayDevice> & displayDevice,const Region & debugRegion,base::unique_fd * readyFence)3390 bool SurfaceFlinger::doComposeSurfaces(const sp<DisplayDevice>& displayDevice,
3391 const Region& debugRegion, base::unique_fd* readyFence) {
3392 ATRACE_CALL();
3393 ALOGV("doComposeSurfaces");
3394
3395 auto display = displayDevice->getCompositionDisplay();
3396 const auto& displayState = display->getState();
3397 const auto displayId = display->getId();
3398 auto& renderEngine = getRenderEngine();
3399 const bool supportProtectedContent = renderEngine.supportsProtectedContent();
3400
3401 const Region bounds(displayState.bounds);
3402 const DisplayRenderArea renderArea(displayDevice);
3403 const bool hasClientComposition = getHwComposer().hasClientComposition(displayId);
3404 const bool hasFlipClientTargetRequest = getHwComposer().hasFlipClientTargetRequest(displayId);
3405 ATRACE_INT("hasClientComposition", hasClientComposition);
3406
3407 bool applyColorMatrix = false;
3408
3409 renderengine::DisplaySettings clientCompositionDisplay;
3410 std::vector<renderengine::LayerSettings> clientCompositionLayers;
3411 sp<GraphicBuffer> buf;
3412 base::unique_fd fd;
3413
3414 if (hasClientComposition) {
3415 ALOGV("hasClientComposition");
3416
3417 if (displayDevice->isPrimary() && supportProtectedContent) {
3418 bool needsProtected = false;
3419 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
3420 // If the layer is a protected layer, mark protected context is needed.
3421 if (layer->isProtected()) {
3422 needsProtected = true;
3423 break;
3424 }
3425 }
3426 if (needsProtected != renderEngine.isProtected()) {
3427 renderEngine.useProtectedContext(needsProtected);
3428 }
3429 if (needsProtected != display->getRenderSurface()->isProtected() &&
3430 needsProtected == renderEngine.isProtected()) {
3431 display->getRenderSurface()->setProtected(needsProtected);
3432 }
3433 }
3434
3435 buf = display->getRenderSurface()->dequeueBuffer(&fd);
3436
3437 if (buf == nullptr) {
3438 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
3439 "client composition for this frame",
3440 displayDevice->getDisplayName().c_str());
3441 return false;
3442 }
3443
3444 clientCompositionDisplay.physicalDisplay = displayState.scissor;
3445 clientCompositionDisplay.clip = displayState.scissor;
3446 const ui::Transform& displayTransform = displayState.transform;
3447 clientCompositionDisplay.globalTransform = displayTransform.asMatrix4();
3448 clientCompositionDisplay.orientation = displayState.orientation;
3449
3450 const auto* profile = display->getDisplayColorProfile();
3451 Dataspace outputDataspace = Dataspace::UNKNOWN;
3452 if (profile->hasWideColorGamut()) {
3453 outputDataspace = displayState.dataspace;
3454 }
3455 clientCompositionDisplay.outputDataspace = outputDataspace;
3456 clientCompositionDisplay.maxLuminance =
3457 profile->getHdrCapabilities().getDesiredMaxLuminance();
3458
3459 const bool hasDeviceComposition = getHwComposer().hasDeviceComposition(displayId);
3460 const bool skipClientColorTransform =
3461 getHwComposer()
3462 .hasDisplayCapability(displayId,
3463 HWC2::DisplayCapability::SkipClientColorTransform);
3464
3465 // Compute the global color transform matrix.
3466 applyColorMatrix = !hasDeviceComposition && !skipClientColorTransform;
3467 if (applyColorMatrix) {
3468 clientCompositionDisplay.colorTransform = displayState.colorTransformMat;
3469 }
3470 } else if (hasFlipClientTargetRequest) {
3471 buf = display->getRenderSurface()->dequeueBuffer(&fd);
3472
3473 if (buf == nullptr) {
3474 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
3475 "client composition for this frame",
3476 displayDevice->getDisplayName().c_str());
3477 return false;
3478 }
3479 }
3480
3481 /*
3482 * and then, render the layers targeted at the framebuffer
3483 */
3484
3485 ALOGV("Rendering client layers");
3486 bool firstLayer = true;
3487 Region clearRegion = Region::INVALID_REGION;
3488 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
3489 const Region viewportRegion(displayState.viewport);
3490 const Region clip(viewportRegion.intersect(layer->visibleRegion));
3491 ALOGV("Layer: %s", layer->getName().string());
3492 ALOGV(" Composition type: %s", toString(layer->getCompositionType(displayDevice)).c_str());
3493 if (!clip.isEmpty()) {
3494 switch (layer->getCompositionType(displayDevice)) {
3495 case Hwc2::IComposerClient::Composition::CURSOR:
3496 case Hwc2::IComposerClient::Composition::DEVICE:
3497 case Hwc2::IComposerClient::Composition::SIDEBAND:
3498 case Hwc2::IComposerClient::Composition::SOLID_COLOR: {
3499 LOG_ALWAYS_FATAL_IF(!displayId);
3500 const Layer::State& state(layer->getDrawingState());
3501 if (layer->getClearClientTarget(displayDevice) && !firstLayer &&
3502 layer->isOpaque(state) && (layer->getAlpha() == 1.0f) &&
3503 layer->getRoundedCornerState().radius == 0.0f && hasClientComposition) {
3504 // never clear the very first layer since we're
3505 // guaranteed the FB is already cleared
3506 renderengine::LayerSettings layerSettings;
3507 Region dummyRegion;
3508 bool prepared =
3509 layer->prepareClientLayer(renderArea, clip, dummyRegion,
3510 supportProtectedContent, layerSettings);
3511
3512 if (prepared) {
3513 layerSettings.source.buffer.buffer = nullptr;
3514 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
3515 layerSettings.alpha = half(0.0);
3516 layerSettings.disableBlending = true;
3517 clientCompositionLayers.push_back(layerSettings);
3518 }
3519 }
3520 break;
3521 }
3522 case Hwc2::IComposerClient::Composition::CLIENT: {
3523 renderengine::LayerSettings layerSettings;
3524 bool prepared =
3525 layer->prepareClientLayer(renderArea, clip, clearRegion,
3526 supportProtectedContent, layerSettings);
3527 if (prepared) {
3528 clientCompositionLayers.push_back(layerSettings);
3529 }
3530 break;
3531 }
3532 default:
3533 break;
3534 }
3535 } else {
3536 ALOGV(" Skipping for empty clip");
3537 }
3538 firstLayer = false;
3539 }
3540
3541 // Perform some cleanup steps if we used client composition.
3542 if (hasClientComposition) {
3543 clientCompositionDisplay.clearRegion = clearRegion;
3544
3545 // We boost GPU frequency here because there will be color spaces conversion
3546 // and it's expensive. We boost the GPU frequency so that GPU composition can
3547 // finish in time. We must reset GPU frequency afterwards, because high frequency
3548 // consumes extra battery.
3549 const bool expensiveRenderingExpected =
3550 clientCompositionDisplay.outputDataspace == Dataspace::DISPLAY_P3;
3551 if (expensiveRenderingExpected && displayId) {
3552 mPowerAdvisor.setExpensiveRenderingExpected(*displayId, true);
3553 }
3554 if (!debugRegion.isEmpty()) {
3555 Region::const_iterator it = debugRegion.begin();
3556 Region::const_iterator end = debugRegion.end();
3557 while (it != end) {
3558 const Rect& rect = *it++;
3559 renderengine::LayerSettings layerSettings;
3560 layerSettings.source.buffer.buffer = nullptr;
3561 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
3562 layerSettings.geometry.boundaries = rect.toFloatRect();
3563 layerSettings.alpha = half(1.0);
3564 clientCompositionLayers.push_back(layerSettings);
3565 }
3566 }
3567 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
3568 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
3569 readyFence);
3570 } else if (displayId) {
3571 mPowerAdvisor.setExpensiveRenderingExpected(*displayId, false);
3572 }
3573 return true;
3574 }
3575
drawWormhole(const Region & region) const3576 void SurfaceFlinger::drawWormhole(const Region& region) const {
3577 auto& engine(getRenderEngine());
3578 engine.fillRegionWithColor(region, 0, 0, 0, 0);
3579 }
3580
addClientLayer(const sp<Client> & client,const sp<IBinder> & handle,const sp<IGraphicBufferProducer> & gbc,const sp<Layer> & lbc,const sp<IBinder> & parentHandle,const sp<Layer> & parentLayer,bool addToCurrentState)3581 status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, const sp<IBinder>& handle,
3582 const sp<IGraphicBufferProducer>& gbc, const sp<Layer>& lbc,
3583 const sp<IBinder>& parentHandle,
3584 const sp<Layer>& parentLayer, bool addToCurrentState) {
3585 // add this layer to the current state list
3586 {
3587 Mutex::Autolock _l(mStateLock);
3588 sp<Layer> parent;
3589 if (parentHandle != nullptr) {
3590 parent = fromHandle(parentHandle);
3591 if (parent == nullptr) {
3592 return NAME_NOT_FOUND;
3593 }
3594 } else {
3595 parent = parentLayer;
3596 }
3597
3598 if (mNumLayers >= MAX_LAYERS) {
3599 ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers,
3600 MAX_LAYERS);
3601 return NO_MEMORY;
3602 }
3603
3604 mLayersByLocalBinderToken.emplace(handle->localBinder(), lbc);
3605
3606 if (parent == nullptr && addToCurrentState) {
3607 mCurrentState.layersSortedByZ.add(lbc);
3608 } else if (parent == nullptr) {
3609 lbc->onRemovedFromCurrentState();
3610 } else if (parent->isRemovedFromCurrentState()) {
3611 parent->addChild(lbc);
3612 lbc->onRemovedFromCurrentState();
3613 } else {
3614 parent->addChild(lbc);
3615 }
3616
3617 if (gbc != nullptr) {
3618 mGraphicBufferProducerList.insert(IInterface::asBinder(gbc).get());
3619 LOG_ALWAYS_FATAL_IF(mGraphicBufferProducerList.size() >
3620 mMaxGraphicBufferProducerListSize,
3621 "Suspected IGBP leak: %zu IGBPs (%zu max), %zu Layers",
3622 mGraphicBufferProducerList.size(),
3623 mMaxGraphicBufferProducerListSize, mNumLayers);
3624 }
3625 mLayersAdded = true;
3626 }
3627
3628 // attach this layer to the client
3629 client->attachLayer(handle, lbc);
3630
3631 return NO_ERROR;
3632 }
3633
peekTransactionFlags()3634 uint32_t SurfaceFlinger::peekTransactionFlags() {
3635 return mTransactionFlags;
3636 }
3637
getTransactionFlags(uint32_t flags)3638 uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
3639 return mTransactionFlags.fetch_and(~flags) & flags;
3640 }
3641
setTransactionFlags(uint32_t flags)3642 uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
3643 return setTransactionFlags(flags, Scheduler::TransactionStart::NORMAL);
3644 }
3645
setTransactionFlags(uint32_t flags,Scheduler::TransactionStart transactionStart)3646 uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags,
3647 Scheduler::TransactionStart transactionStart) {
3648 uint32_t old = mTransactionFlags.fetch_or(flags);
3649 mVsyncModulator.setTransactionStart(transactionStart);
3650 if ((old & flags)==0) { // wake the server up
3651 signalTransaction();
3652 }
3653 return old;
3654 }
3655
flushTransactionQueues()3656 bool SurfaceFlinger::flushTransactionQueues() {
3657 // to prevent onHandleDestroyed from being called while the lock is held,
3658 // we must keep a copy of the transactions (specifically the composer
3659 // states) around outside the scope of the lock
3660 std::vector<const TransactionState> transactions;
3661 bool flushedATransaction = false;
3662 {
3663 Mutex::Autolock _l(mStateLock);
3664
3665 auto it = mTransactionQueues.begin();
3666 while (it != mTransactionQueues.end()) {
3667 auto& [applyToken, transactionQueue] = *it;
3668
3669 while (!transactionQueue.empty()) {
3670 const auto& transaction = transactionQueue.front();
3671 if (!transactionIsReadyToBeApplied(transaction.desiredPresentTime,
3672 transaction.states)) {
3673 setTransactionFlags(eTransactionFlushNeeded);
3674 break;
3675 }
3676 transactions.push_back(transaction);
3677 applyTransactionState(transaction.states, transaction.displays, transaction.flags,
3678 mPendingInputWindowCommands, transaction.desiredPresentTime,
3679 transaction.buffer, transaction.callback,
3680 transaction.postTime, transaction.privileged,
3681 /*isMainThread*/ true);
3682 transactionQueue.pop();
3683 flushedATransaction = true;
3684 }
3685
3686 if (transactionQueue.empty()) {
3687 it = mTransactionQueues.erase(it);
3688 mTransactionCV.broadcast();
3689 } else {
3690 it = std::next(it, 1);
3691 }
3692 }
3693 }
3694 return flushedATransaction;
3695 }
3696
transactionFlushNeeded()3697 bool SurfaceFlinger::transactionFlushNeeded() {
3698 return !mTransactionQueues.empty();
3699 }
3700
containsAnyInvalidClientState(const Vector<ComposerState> & states)3701 bool SurfaceFlinger::containsAnyInvalidClientState(const Vector<ComposerState>& states) {
3702 for (const ComposerState& state : states) {
3703 // Here we need to check that the interface we're given is indeed
3704 // one of our own. A malicious client could give us a nullptr
3705 // IInterface, or one of its own or even one of our own but a
3706 // different type. All these situations would cause us to crash.
3707 if (state.client == nullptr) {
3708 return true;
3709 }
3710
3711 sp<IBinder> binder = IInterface::asBinder(state.client);
3712 if (binder == nullptr) {
3713 return true;
3714 }
3715
3716 if (binder->queryLocalInterface(ISurfaceComposerClient::descriptor) == nullptr) {
3717 return true;
3718 }
3719 }
3720 return false;
3721 }
3722
transactionIsReadyToBeApplied(int64_t desiredPresentTime,const Vector<ComposerState> & states)3723 bool SurfaceFlinger::transactionIsReadyToBeApplied(int64_t desiredPresentTime,
3724 const Vector<ComposerState>& states) {
3725 nsecs_t expectedPresentTime = getExpectedPresentTime();
3726 // Do not present if the desiredPresentTime has not passed unless it is more than one second
3727 // in the future. We ignore timestamps more than 1 second in the future for stability reasons.
3728 if (desiredPresentTime >= 0 && desiredPresentTime >= expectedPresentTime &&
3729 desiredPresentTime < expectedPresentTime + s2ns(1)) {
3730 return false;
3731 }
3732
3733 for (const ComposerState& state : states) {
3734 const layer_state_t& s = state.state;
3735 if (!(s.what & layer_state_t::eAcquireFenceChanged)) {
3736 continue;
3737 }
3738 if (s.acquireFence && s.acquireFence->getStatus() == Fence::Status::Unsignaled) {
3739 return false;
3740 }
3741 }
3742 return true;
3743 }
3744
setTransactionState(const Vector<ComposerState> & states,const Vector<DisplayState> & displays,uint32_t flags,const sp<IBinder> & applyToken,const InputWindowCommands & inputWindowCommands,int64_t desiredPresentTime,const client_cache_t & uncacheBuffer,const std::vector<ListenerCallbacks> & listenerCallbacks)3745 void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& states,
3746 const Vector<DisplayState>& displays, uint32_t flags,
3747 const sp<IBinder>& applyToken,
3748 const InputWindowCommands& inputWindowCommands,
3749 int64_t desiredPresentTime,
3750 const client_cache_t& uncacheBuffer,
3751 const std::vector<ListenerCallbacks>& listenerCallbacks) {
3752 ATRACE_CALL();
3753
3754 const int64_t postTime = systemTime();
3755
3756 bool privileged = callingThreadHasUnscopedSurfaceFlingerAccess();
3757
3758 Mutex::Autolock _l(mStateLock);
3759
3760 if (containsAnyInvalidClientState(states)) {
3761 return;
3762 }
3763
3764 // If its TransactionQueue already has a pending TransactionState or if it is pending
3765 auto itr = mTransactionQueues.find(applyToken);
3766 // if this is an animation frame, wait until prior animation frame has
3767 // been applied by SF
3768 if (flags & eAnimation) {
3769 while (itr != mTransactionQueues.end()) {
3770 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
3771 if (CC_UNLIKELY(err != NO_ERROR)) {
3772 ALOGW_IF(err == TIMED_OUT,
3773 "setTransactionState timed out "
3774 "waiting for animation frame to apply");
3775 break;
3776 }
3777 itr = mTransactionQueues.find(applyToken);
3778 }
3779 }
3780 if (itr != mTransactionQueues.end() ||
3781 !transactionIsReadyToBeApplied(desiredPresentTime, states)) {
3782 mTransactionQueues[applyToken].emplace(states, displays, flags, desiredPresentTime,
3783 uncacheBuffer, listenerCallbacks, postTime,
3784 privileged);
3785 setTransactionFlags(eTransactionFlushNeeded);
3786 return;
3787 }
3788
3789 applyTransactionState(states, displays, flags, inputWindowCommands, desiredPresentTime,
3790 uncacheBuffer, listenerCallbacks, postTime, privileged);
3791 }
3792
applyTransactionState(const Vector<ComposerState> & states,const Vector<DisplayState> & displays,uint32_t flags,const InputWindowCommands & inputWindowCommands,const int64_t desiredPresentTime,const client_cache_t & uncacheBuffer,const std::vector<ListenerCallbacks> & listenerCallbacks,const int64_t postTime,bool privileged,bool isMainThread)3793 void SurfaceFlinger::applyTransactionState(const Vector<ComposerState>& states,
3794 const Vector<DisplayState>& displays, uint32_t flags,
3795 const InputWindowCommands& inputWindowCommands,
3796 const int64_t desiredPresentTime,
3797 const client_cache_t& uncacheBuffer,
3798 const std::vector<ListenerCallbacks>& listenerCallbacks,
3799 const int64_t postTime, bool privileged,
3800 bool isMainThread) {
3801 uint32_t transactionFlags = 0;
3802
3803 if (flags & eAnimation) {
3804 // For window updates that are part of an animation we must wait for
3805 // previous animation "frames" to be handled.
3806 while (!isMainThread && mAnimTransactionPending) {
3807 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
3808 if (CC_UNLIKELY(err != NO_ERROR)) {
3809 // just in case something goes wrong in SF, return to the
3810 // caller after a few seconds.
3811 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
3812 "waiting for previous animation frame");
3813 mAnimTransactionPending = false;
3814 break;
3815 }
3816 }
3817 }
3818
3819 for (const DisplayState& display : displays) {
3820 transactionFlags |= setDisplayStateLocked(display);
3821 }
3822
3823 // In case the client has sent a Transaction that should receive callbacks but without any
3824 // SurfaceControls that should be included in the callback, send the listener and callbackIds
3825 // to the callback thread so it can send an empty callback
3826 if (!listenerCallbacks.empty()) {
3827 mTransactionCompletedThread.run();
3828 }
3829 for (const auto& [listener, callbackIds] : listenerCallbacks) {
3830 mTransactionCompletedThread.addCallback(listener, callbackIds);
3831 }
3832
3833 uint32_t clientStateFlags = 0;
3834 for (const ComposerState& state : states) {
3835 clientStateFlags |= setClientStateLocked(state, desiredPresentTime, listenerCallbacks,
3836 postTime, privileged);
3837 }
3838
3839 // If the state doesn't require a traversal and there are callbacks, send them now
3840 if (!(clientStateFlags & eTraversalNeeded) && !listenerCallbacks.empty()) {
3841 mTransactionCompletedThread.sendCallbacks();
3842 }
3843 transactionFlags |= clientStateFlags;
3844
3845 transactionFlags |= addInputWindowCommands(inputWindowCommands);
3846
3847 if (uncacheBuffer.isValid()) {
3848 ClientCache::getInstance().erase(uncacheBuffer);
3849 getRenderEngine().unbindExternalTextureBuffer(uncacheBuffer.id);
3850 }
3851
3852 // If a synchronous transaction is explicitly requested without any changes, force a transaction
3853 // anyway. This can be used as a flush mechanism for previous async transactions.
3854 // Empty animation transaction can be used to simulate back-pressure, so also force a
3855 // transaction for empty animation transactions.
3856 if (transactionFlags == 0 &&
3857 ((flags & eSynchronous) || (flags & eAnimation))) {
3858 transactionFlags = eTransactionNeeded;
3859 }
3860
3861 // If we are on the main thread, we are about to preform a traversal. Clear the traversal bit
3862 // so we don't have to wake up again next frame to preform an uneeded traversal.
3863 if (isMainThread && (transactionFlags & eTraversalNeeded)) {
3864 transactionFlags = transactionFlags & (~eTraversalNeeded);
3865 mTraversalNeededMainThread = true;
3866 }
3867
3868 if (transactionFlags) {
3869 if (mInterceptor->isEnabled()) {
3870 mInterceptor->saveTransaction(states, mCurrentState.displays, displays, flags);
3871 }
3872
3873 // this triggers the transaction
3874 const auto start = (flags & eEarlyWakeup) ? Scheduler::TransactionStart::EARLY
3875 : Scheduler::TransactionStart::NORMAL;
3876 setTransactionFlags(transactionFlags, start);
3877
3878 // if this is a synchronous transaction, wait for it to take effect
3879 // before returning.
3880 if (flags & eSynchronous) {
3881 mTransactionPending = true;
3882 }
3883 if (flags & eAnimation) {
3884 mAnimTransactionPending = true;
3885 }
3886 if (mPendingInputWindowCommands.syncInputWindows) {
3887 mPendingSyncInputWindows = true;
3888 }
3889
3890 // applyTransactionState can be called by either the main SF thread or by
3891 // another process through setTransactionState. While a given process may wish
3892 // to wait on synchronous transactions, the main SF thread should never
3893 // be blocked. Therefore, we only wait if isMainThread is false.
3894 while (!isMainThread && (mTransactionPending || mPendingSyncInputWindows)) {
3895 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
3896 if (CC_UNLIKELY(err != NO_ERROR)) {
3897 // just in case something goes wrong in SF, return to the
3898 // called after a few seconds.
3899 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
3900 mTransactionPending = false;
3901 mPendingSyncInputWindows = false;
3902 break;
3903 }
3904 }
3905 }
3906 }
3907
setDisplayStateLocked(const DisplayState & s)3908 uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s) {
3909 const ssize_t index = mCurrentState.displays.indexOfKey(s.token);
3910 if (index < 0) return 0;
3911
3912 uint32_t flags = 0;
3913 DisplayDeviceState& state = mCurrentState.displays.editValueAt(index);
3914
3915 const uint32_t what = s.what;
3916 if (what & DisplayState::eSurfaceChanged) {
3917 if (IInterface::asBinder(state.surface) != IInterface::asBinder(s.surface)) {
3918 state.surface = s.surface;
3919 flags |= eDisplayTransactionNeeded;
3920 }
3921 }
3922 if (what & DisplayState::eLayerStackChanged) {
3923 if (state.layerStack != s.layerStack) {
3924 state.layerStack = s.layerStack;
3925 flags |= eDisplayTransactionNeeded;
3926 }
3927 }
3928 if (what & DisplayState::eDisplayProjectionChanged) {
3929 if (state.orientation != s.orientation) {
3930 state.orientation = s.orientation;
3931 flags |= eDisplayTransactionNeeded;
3932 }
3933 if (state.frame != s.frame) {
3934 state.frame = s.frame;
3935 flags |= eDisplayTransactionNeeded;
3936 }
3937 if (state.viewport != s.viewport) {
3938 state.viewport = s.viewport;
3939 flags |= eDisplayTransactionNeeded;
3940 }
3941 }
3942 if (what & DisplayState::eDisplaySizeChanged) {
3943 if (state.width != s.width) {
3944 state.width = s.width;
3945 flags |= eDisplayTransactionNeeded;
3946 }
3947 if (state.height != s.height) {
3948 state.height = s.height;
3949 flags |= eDisplayTransactionNeeded;
3950 }
3951 }
3952
3953 return flags;
3954 }
3955
callingThreadHasUnscopedSurfaceFlingerAccess()3956 bool SurfaceFlinger::callingThreadHasUnscopedSurfaceFlingerAccess() {
3957 IPCThreadState* ipc = IPCThreadState::self();
3958 const int pid = ipc->getCallingPid();
3959 const int uid = ipc->getCallingUid();
3960 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
3961 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
3962 return false;
3963 }
3964 return true;
3965 }
3966
setClientStateLocked(const ComposerState & composerState,int64_t desiredPresentTime,const std::vector<ListenerCallbacks> & listenerCallbacks,int64_t postTime,bool privileged)3967 uint32_t SurfaceFlinger::setClientStateLocked(
3968 const ComposerState& composerState, int64_t desiredPresentTime,
3969 const std::vector<ListenerCallbacks>& listenerCallbacks, int64_t postTime,
3970 bool privileged) {
3971 const layer_state_t& s = composerState.state;
3972 sp<Client> client(static_cast<Client*>(composerState.client.get()));
3973
3974 sp<Layer> layer(client->getLayerUser(s.surface));
3975 if (layer == nullptr) {
3976 return 0;
3977 }
3978
3979 uint32_t flags = 0;
3980
3981 const uint64_t what = s.what;
3982 bool geometryAppliesWithResize =
3983 what & layer_state_t::eGeometryAppliesWithResize;
3984
3985 // If we are deferring transaction, make sure to push the pending state, as otherwise the
3986 // pending state will also be deferred.
3987 if (what & layer_state_t::eDeferTransaction_legacy) {
3988 layer->pushPendingState();
3989 }
3990
3991 if (what & layer_state_t::ePositionChanged) {
3992 if (layer->setPosition(s.x, s.y, !geometryAppliesWithResize)) {
3993 flags |= eTraversalNeeded;
3994 }
3995 }
3996 if (what & layer_state_t::eLayerChanged) {
3997 // NOTE: index needs to be calculated before we update the state
3998 const auto& p = layer->getParent();
3999 if (p == nullptr) {
4000 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
4001 if (layer->setLayer(s.z) && idx >= 0) {
4002 mCurrentState.layersSortedByZ.removeAt(idx);
4003 mCurrentState.layersSortedByZ.add(layer);
4004 // we need traversal (state changed)
4005 // AND transaction (list changed)
4006 flags |= eTransactionNeeded|eTraversalNeeded;
4007 }
4008 } else {
4009 if (p->setChildLayer(layer, s.z)) {
4010 flags |= eTransactionNeeded|eTraversalNeeded;
4011 }
4012 }
4013 }
4014 if (what & layer_state_t::eRelativeLayerChanged) {
4015 // NOTE: index needs to be calculated before we update the state
4016 const auto& p = layer->getParent();
4017 if (p == nullptr) {
4018 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
4019 if (layer->setRelativeLayer(s.relativeLayerHandle, s.z) && idx >= 0) {
4020 mCurrentState.layersSortedByZ.removeAt(idx);
4021 mCurrentState.layersSortedByZ.add(layer);
4022 // we need traversal (state changed)
4023 // AND transaction (list changed)
4024 flags |= eTransactionNeeded|eTraversalNeeded;
4025 }
4026 } else {
4027 if (p->setChildRelativeLayer(layer, s.relativeLayerHandle, s.z)) {
4028 flags |= eTransactionNeeded|eTraversalNeeded;
4029 }
4030 }
4031 }
4032 if (what & layer_state_t::eSizeChanged) {
4033 if (layer->setSize(s.w, s.h)) {
4034 flags |= eTraversalNeeded;
4035 }
4036 }
4037 if (what & layer_state_t::eAlphaChanged) {
4038 if (layer->setAlpha(s.alpha))
4039 flags |= eTraversalNeeded;
4040 }
4041 if (what & layer_state_t::eColorChanged) {
4042 if (layer->setColor(s.color))
4043 flags |= eTraversalNeeded;
4044 }
4045 if (what & layer_state_t::eColorTransformChanged) {
4046 if (layer->setColorTransform(s.colorTransform)) {
4047 flags |= eTraversalNeeded;
4048 }
4049 }
4050 if (what & layer_state_t::eBackgroundColorChanged) {
4051 if (layer->setBackgroundColor(s.color, s.bgColorAlpha, s.bgColorDataspace)) {
4052 flags |= eTraversalNeeded;
4053 }
4054 }
4055 if (what & layer_state_t::eMatrixChanged) {
4056 // TODO: b/109894387
4057 //
4058 // SurfaceFlinger's renderer is not prepared to handle cropping in the face of arbitrary
4059 // rotation. To see the problem observe that if we have a square parent, and a child
4060 // of the same size, then we rotate the child 45 degrees around it's center, the child
4061 // must now be cropped to a non rectangular 8 sided region.
4062 //
4063 // Of course we can fix this in the future. For now, we are lucky, SurfaceControl is
4064 // private API, and the WindowManager only uses rotation in one case, which is on a top
4065 // level layer in which cropping is not an issue.
4066 //
4067 // However given that abuse of rotation matrices could lead to surfaces extending outside
4068 // of cropped areas, we need to prevent non-root clients without permission ACCESS_SURFACE_FLINGER
4069 // (a.k.a. everyone except WindowManager and tests) from setting non rectangle preserving
4070 // transformations.
4071 if (layer->setMatrix(s.matrix, privileged))
4072 flags |= eTraversalNeeded;
4073 }
4074 if (what & layer_state_t::eTransparentRegionChanged) {
4075 if (layer->setTransparentRegionHint(s.transparentRegion))
4076 flags |= eTraversalNeeded;
4077 }
4078 if (what & layer_state_t::eFlagsChanged) {
4079 if (layer->setFlags(s.flags, s.mask))
4080 flags |= eTraversalNeeded;
4081 }
4082 if (what & layer_state_t::eCropChanged_legacy) {
4083 if (layer->setCrop_legacy(s.crop_legacy, !geometryAppliesWithResize))
4084 flags |= eTraversalNeeded;
4085 }
4086 if (what & layer_state_t::eCornerRadiusChanged) {
4087 if (layer->setCornerRadius(s.cornerRadius))
4088 flags |= eTraversalNeeded;
4089 }
4090 if (what & layer_state_t::eLayerStackChanged) {
4091 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
4092 // We only allow setting layer stacks for top level layers,
4093 // everything else inherits layer stack from its parent.
4094 if (layer->hasParent()) {
4095 ALOGE("Attempt to set layer stack on layer with parent (%s) is invalid",
4096 layer->getName().string());
4097 } else if (idx < 0) {
4098 ALOGE("Attempt to set layer stack on layer without parent (%s) that "
4099 "that also does not appear in the top level layer list. Something"
4100 " has gone wrong.", layer->getName().string());
4101 } else if (layer->setLayerStack(s.layerStack)) {
4102 mCurrentState.layersSortedByZ.removeAt(idx);
4103 mCurrentState.layersSortedByZ.add(layer);
4104 // we need traversal (state changed)
4105 // AND transaction (list changed)
4106 flags |= eTransactionNeeded|eTraversalNeeded|eDisplayLayerStackChanged;
4107 }
4108 }
4109 if (what & layer_state_t::eDeferTransaction_legacy) {
4110 if (s.barrierHandle_legacy != nullptr) {
4111 layer->deferTransactionUntil_legacy(s.barrierHandle_legacy, s.frameNumber_legacy);
4112 } else if (s.barrierGbp_legacy != nullptr) {
4113 const sp<IGraphicBufferProducer>& gbp = s.barrierGbp_legacy;
4114 if (authenticateSurfaceTextureLocked(gbp)) {
4115 const auto& otherLayer =
4116 (static_cast<MonitoredProducer*>(gbp.get()))->getLayer();
4117 layer->deferTransactionUntil_legacy(otherLayer, s.frameNumber_legacy);
4118 } else {
4119 ALOGE("Attempt to defer transaction to to an"
4120 " unrecognized GraphicBufferProducer");
4121 }
4122 }
4123 // We don't trigger a traversal here because if no other state is
4124 // changed, we don't want this to cause any more work
4125 }
4126 if (what & layer_state_t::eReparent) {
4127 bool hadParent = layer->hasParent();
4128 if (layer->reparent(s.parentHandleForChild)) {
4129 if (!hadParent) {
4130 mCurrentState.layersSortedByZ.remove(layer);
4131 }
4132 flags |= eTransactionNeeded|eTraversalNeeded;
4133 }
4134 }
4135 if (what & layer_state_t::eReparentChildren) {
4136 if (layer->reparentChildren(s.reparentHandle)) {
4137 flags |= eTransactionNeeded|eTraversalNeeded;
4138 }
4139 }
4140 if (what & layer_state_t::eDetachChildren) {
4141 layer->detachChildren();
4142 }
4143 if (what & layer_state_t::eOverrideScalingModeChanged) {
4144 layer->setOverrideScalingMode(s.overrideScalingMode);
4145 // We don't trigger a traversal here because if no other state is
4146 // changed, we don't want this to cause any more work
4147 }
4148 if (what & layer_state_t::eTransformChanged) {
4149 if (layer->setTransform(s.transform)) flags |= eTraversalNeeded;
4150 }
4151 if (what & layer_state_t::eTransformToDisplayInverseChanged) {
4152 if (layer->setTransformToDisplayInverse(s.transformToDisplayInverse))
4153 flags |= eTraversalNeeded;
4154 }
4155 if (what & layer_state_t::eCropChanged) {
4156 if (layer->setCrop(s.crop)) flags |= eTraversalNeeded;
4157 }
4158 if (what & layer_state_t::eFrameChanged) {
4159 if (layer->setFrame(s.frame)) flags |= eTraversalNeeded;
4160 }
4161 if (what & layer_state_t::eAcquireFenceChanged) {
4162 if (layer->setAcquireFence(s.acquireFence)) flags |= eTraversalNeeded;
4163 }
4164 if (what & layer_state_t::eDataspaceChanged) {
4165 if (layer->setDataspace(s.dataspace)) flags |= eTraversalNeeded;
4166 }
4167 if (what & layer_state_t::eHdrMetadataChanged) {
4168 if (layer->setHdrMetadata(s.hdrMetadata)) flags |= eTraversalNeeded;
4169 }
4170 if (what & layer_state_t::eSurfaceDamageRegionChanged) {
4171 if (layer->setSurfaceDamageRegion(s.surfaceDamageRegion)) flags |= eTraversalNeeded;
4172 }
4173 if (what & layer_state_t::eApiChanged) {
4174 if (layer->setApi(s.api)) flags |= eTraversalNeeded;
4175 }
4176 if (what & layer_state_t::eSidebandStreamChanged) {
4177 if (layer->setSidebandStream(s.sidebandStream)) flags |= eTraversalNeeded;
4178 }
4179 if (what & layer_state_t::eInputInfoChanged) {
4180 if (privileged) {
4181 layer->setInputInfo(s.inputInfo);
4182 flags |= eTraversalNeeded;
4183 } else {
4184 ALOGE("Attempt to update InputWindowInfo without permission ACCESS_SURFACE_FLINGER");
4185 }
4186 }
4187 if (what & layer_state_t::eMetadataChanged) {
4188 if (layer->setMetadata(s.metadata)) flags |= eTraversalNeeded;
4189 }
4190 if (what & layer_state_t::eColorSpaceAgnosticChanged) {
4191 if (layer->setColorSpaceAgnostic(s.colorSpaceAgnostic)) {
4192 flags |= eTraversalNeeded;
4193 }
4194 }
4195 std::vector<sp<CallbackHandle>> callbackHandles;
4196 if ((what & layer_state_t::eHasListenerCallbacksChanged) && (!listenerCallbacks.empty())) {
4197 for (const auto& [listener, callbackIds] : listenerCallbacks) {
4198 callbackHandles.emplace_back(new CallbackHandle(listener, callbackIds, s.surface));
4199 }
4200 }
4201 bool bufferChanged = what & layer_state_t::eBufferChanged;
4202 bool cacheIdChanged = what & layer_state_t::eCachedBufferChanged;
4203 sp<GraphicBuffer> buffer;
4204 if (bufferChanged && cacheIdChanged && s.buffer != nullptr) {
4205 buffer = s.buffer;
4206 bool success = ClientCache::getInstance().add(s.cachedBuffer, s.buffer);
4207 if (success) {
4208 getRenderEngine().cacheExternalTextureBuffer(s.buffer);
4209 success = ClientCache::getInstance()
4210 .registerErasedRecipient(s.cachedBuffer,
4211 wp<ClientCache::ErasedRecipient>(this));
4212 if (!success) {
4213 getRenderEngine().unbindExternalTextureBuffer(s.buffer->getId());
4214 }
4215 }
4216 } else if (cacheIdChanged) {
4217 buffer = ClientCache::getInstance().get(s.cachedBuffer);
4218 } else if (bufferChanged) {
4219 buffer = s.buffer;
4220 }
4221 if (buffer) {
4222 if (layer->setBuffer(buffer, postTime, desiredPresentTime, s.cachedBuffer)) {
4223 flags |= eTraversalNeeded;
4224 }
4225 }
4226 if (layer->setTransactionCompletedListeners(callbackHandles)) flags |= eTraversalNeeded;
4227 // Do not put anything that updates layer state or modifies flags after
4228 // setTransactionCompletedListener
4229 return flags;
4230 }
4231
addInputWindowCommands(const InputWindowCommands & inputWindowCommands)4232 uint32_t SurfaceFlinger::addInputWindowCommands(const InputWindowCommands& inputWindowCommands) {
4233 uint32_t flags = 0;
4234 if (!inputWindowCommands.transferTouchFocusCommands.empty()) {
4235 flags |= eTraversalNeeded;
4236 }
4237
4238 if (inputWindowCommands.syncInputWindows) {
4239 flags |= eTraversalNeeded;
4240 }
4241
4242 mPendingInputWindowCommands.merge(inputWindowCommands);
4243 return flags;
4244 }
4245
createLayer(const String8 & name,const sp<Client> & client,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags,LayerMetadata metadata,sp<IBinder> * handle,sp<IGraphicBufferProducer> * gbp,const sp<IBinder> & parentHandle,const sp<Layer> & parentLayer)4246 status_t SurfaceFlinger::createLayer(const String8& name, const sp<Client>& client, uint32_t w,
4247 uint32_t h, PixelFormat format, uint32_t flags,
4248 LayerMetadata metadata, sp<IBinder>* handle,
4249 sp<IGraphicBufferProducer>* gbp,
4250 const sp<IBinder>& parentHandle,
4251 const sp<Layer>& parentLayer) {
4252 if (int32_t(w|h) < 0) {
4253 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
4254 int(w), int(h));
4255 return BAD_VALUE;
4256 }
4257
4258 ALOG_ASSERT(parentLayer == nullptr || parentHandle == nullptr,
4259 "Expected only one of parentLayer or parentHandle to be non-null. "
4260 "Programmer error?");
4261
4262 status_t result = NO_ERROR;
4263
4264 sp<Layer> layer;
4265
4266 String8 uniqueName = getUniqueLayerName(name);
4267
4268 bool primaryDisplayOnly = false;
4269
4270 // window type is WINDOW_TYPE_DONT_SCREENSHOT from SurfaceControl.java
4271 // TODO b/64227542
4272 if (metadata.has(METADATA_WINDOW_TYPE)) {
4273 int32_t windowType = metadata.getInt32(METADATA_WINDOW_TYPE, 0);
4274 if (windowType == 441731) {
4275 metadata.setInt32(METADATA_WINDOW_TYPE, InputWindowInfo::TYPE_NAVIGATION_BAR_PANEL);
4276 primaryDisplayOnly = true;
4277 }
4278 }
4279
4280 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
4281 case ISurfaceComposerClient::eFXSurfaceBufferQueue:
4282 result = createBufferQueueLayer(client, uniqueName, w, h, flags, std::move(metadata),
4283 format, handle, gbp, &layer);
4284
4285 break;
4286 case ISurfaceComposerClient::eFXSurfaceBufferState:
4287 result = createBufferStateLayer(client, uniqueName, w, h, flags, std::move(metadata),
4288 handle, &layer);
4289 break;
4290 case ISurfaceComposerClient::eFXSurfaceColor:
4291 // check if buffer size is set for color layer.
4292 if (w > 0 || h > 0) {
4293 ALOGE("createLayer() failed, w or h cannot be set for color layer (w=%d, h=%d)",
4294 int(w), int(h));
4295 return BAD_VALUE;
4296 }
4297
4298 result = createColorLayer(client, uniqueName, w, h, flags, std::move(metadata), handle,
4299 &layer);
4300 break;
4301 case ISurfaceComposerClient::eFXSurfaceContainer:
4302 // check if buffer size is set for container layer.
4303 if (w > 0 || h > 0) {
4304 ALOGE("createLayer() failed, w or h cannot be set for container layer (w=%d, h=%d)",
4305 int(w), int(h));
4306 return BAD_VALUE;
4307 }
4308 result = createContainerLayer(client, uniqueName, w, h, flags, std::move(metadata),
4309 handle, &layer);
4310 break;
4311 default:
4312 result = BAD_VALUE;
4313 break;
4314 }
4315
4316 if (result != NO_ERROR) {
4317 return result;
4318 }
4319
4320 if (primaryDisplayOnly) {
4321 layer->setPrimaryDisplayOnly();
4322 }
4323
4324 bool addToCurrentState = callingThreadHasUnscopedSurfaceFlingerAccess();
4325 result = addClientLayer(client, *handle, *gbp, layer, parentHandle, parentLayer,
4326 addToCurrentState);
4327 if (result != NO_ERROR) {
4328 return result;
4329 }
4330 mInterceptor->saveSurfaceCreation(layer);
4331
4332 setTransactionFlags(eTransactionNeeded);
4333 return result;
4334 }
4335
getUniqueLayerName(const String8 & name)4336 String8 SurfaceFlinger::getUniqueLayerName(const String8& name)
4337 {
4338 bool matchFound = true;
4339 uint32_t dupeCounter = 0;
4340
4341 // Tack on our counter whether there is a hit or not, so everyone gets a tag
4342 String8 uniqueName = name + "#" + String8(std::to_string(dupeCounter).c_str());
4343
4344 // Grab the state lock since we're accessing mCurrentState
4345 Mutex::Autolock lock(mStateLock);
4346
4347 // Loop over layers until we're sure there is no matching name
4348 while (matchFound) {
4349 matchFound = false;
4350 mCurrentState.traverseInZOrder([&](Layer* layer) {
4351 if (layer->getName() == uniqueName) {
4352 matchFound = true;
4353 uniqueName = name + "#" + String8(std::to_string(++dupeCounter).c_str());
4354 }
4355 });
4356 }
4357
4358 ALOGV_IF(dupeCounter > 0, "duplicate layer name: changing %s to %s", name.c_str(),
4359 uniqueName.c_str());
4360
4361 return uniqueName;
4362 }
4363
createBufferQueueLayer(const sp<Client> & client,const String8 & name,uint32_t w,uint32_t h,uint32_t flags,LayerMetadata metadata,PixelFormat & format,sp<IBinder> * handle,sp<IGraphicBufferProducer> * gbp,sp<Layer> * outLayer)4364 status_t SurfaceFlinger::createBufferQueueLayer(const sp<Client>& client, const String8& name,
4365 uint32_t w, uint32_t h, uint32_t flags,
4366 LayerMetadata metadata, PixelFormat& format,
4367 sp<IBinder>* handle,
4368 sp<IGraphicBufferProducer>* gbp,
4369 sp<Layer>* outLayer) {
4370 // initialize the surfaces
4371 switch (format) {
4372 case PIXEL_FORMAT_TRANSPARENT:
4373 case PIXEL_FORMAT_TRANSLUCENT:
4374 format = PIXEL_FORMAT_RGBA_8888;
4375 break;
4376 case PIXEL_FORMAT_OPAQUE:
4377 format = PIXEL_FORMAT_RGBX_8888;
4378 break;
4379 }
4380
4381 sp<BufferQueueLayer> layer = getFactory().createBufferQueueLayer(
4382 LayerCreationArgs(this, client, name, w, h, flags, std::move(metadata)));
4383 status_t err = layer->setDefaultBufferProperties(w, h, format);
4384 if (err == NO_ERROR) {
4385 *handle = layer->getHandle();
4386 *gbp = layer->getProducer();
4387 *outLayer = layer;
4388 }
4389
4390 ALOGE_IF(err, "createBufferQueueLayer() failed (%s)", strerror(-err));
4391 return err;
4392 }
4393
createBufferStateLayer(const sp<Client> & client,const String8 & name,uint32_t w,uint32_t h,uint32_t flags,LayerMetadata metadata,sp<IBinder> * handle,sp<Layer> * outLayer)4394 status_t SurfaceFlinger::createBufferStateLayer(const sp<Client>& client, const String8& name,
4395 uint32_t w, uint32_t h, uint32_t flags,
4396 LayerMetadata metadata, sp<IBinder>* handle,
4397 sp<Layer>* outLayer) {
4398 sp<BufferStateLayer> layer = getFactory().createBufferStateLayer(
4399 LayerCreationArgs(this, client, name, w, h, flags, std::move(metadata)));
4400 *handle = layer->getHandle();
4401 *outLayer = layer;
4402
4403 return NO_ERROR;
4404 }
4405
createColorLayer(const sp<Client> & client,const String8 & name,uint32_t w,uint32_t h,uint32_t flags,LayerMetadata metadata,sp<IBinder> * handle,sp<Layer> * outLayer)4406 status_t SurfaceFlinger::createColorLayer(const sp<Client>& client, const String8& name, uint32_t w,
4407 uint32_t h, uint32_t flags, LayerMetadata metadata,
4408 sp<IBinder>* handle, sp<Layer>* outLayer) {
4409 *outLayer = getFactory().createColorLayer(
4410 LayerCreationArgs(this, client, name, w, h, flags, std::move(metadata)));
4411 *handle = (*outLayer)->getHandle();
4412 return NO_ERROR;
4413 }
4414
createContainerLayer(const sp<Client> & client,const String8 & name,uint32_t w,uint32_t h,uint32_t flags,LayerMetadata metadata,sp<IBinder> * handle,sp<Layer> * outLayer)4415 status_t SurfaceFlinger::createContainerLayer(const sp<Client>& client, const String8& name,
4416 uint32_t w, uint32_t h, uint32_t flags,
4417 LayerMetadata metadata, sp<IBinder>* handle,
4418 sp<Layer>* outLayer) {
4419 *outLayer = getFactory().createContainerLayer(
4420 LayerCreationArgs(this, client, name, w, h, flags, std::move(metadata)));
4421 *handle = (*outLayer)->getHandle();
4422 return NO_ERROR;
4423 }
4424
4425
markLayerPendingRemovalLocked(const sp<Layer> & layer)4426 void SurfaceFlinger::markLayerPendingRemovalLocked(const sp<Layer>& layer) {
4427 mLayersPendingRemoval.add(layer);
4428 mLayersRemoved = true;
4429 setTransactionFlags(eTransactionNeeded);
4430 }
4431
onHandleDestroyed(sp<Layer> & layer)4432 void SurfaceFlinger::onHandleDestroyed(sp<Layer>& layer)
4433 {
4434 Mutex::Autolock lock(mStateLock);
4435 // If a layer has a parent, we allow it to out-live it's handle
4436 // with the idea that the parent holds a reference and will eventually
4437 // be cleaned up. However no one cleans up the top-level so we do so
4438 // here.
4439 if (layer->getParent() == nullptr) {
4440 mCurrentState.layersSortedByZ.remove(layer);
4441 }
4442 markLayerPendingRemovalLocked(layer);
4443
4444 auto it = mLayersByLocalBinderToken.begin();
4445 while (it != mLayersByLocalBinderToken.end()) {
4446 if (it->second == layer) {
4447 it = mLayersByLocalBinderToken.erase(it);
4448 } else {
4449 it++;
4450 }
4451 }
4452
4453 layer.clear();
4454 }
4455
4456 // ---------------------------------------------------------------------------
4457
onInitializeDisplays()4458 void SurfaceFlinger::onInitializeDisplays() {
4459 const auto display = getDefaultDisplayDeviceLocked();
4460 if (!display) return;
4461
4462 const sp<IBinder> token = display->getDisplayToken().promote();
4463 LOG_ALWAYS_FATAL_IF(token == nullptr);
4464
4465 // reset screen orientation and use primary layer stack
4466 Vector<ComposerState> state;
4467 Vector<DisplayState> displays;
4468 DisplayState d;
4469 d.what = DisplayState::eDisplayProjectionChanged |
4470 DisplayState::eLayerStackChanged;
4471 d.token = token;
4472 d.layerStack = 0;
4473 d.orientation = DisplayState::eOrientationDefault;
4474 d.frame.makeInvalid();
4475 d.viewport.makeInvalid();
4476 d.width = 0;
4477 d.height = 0;
4478 displays.add(d);
4479 setTransactionState(state, displays, 0, nullptr, mPendingInputWindowCommands, -1, {}, {});
4480
4481 setPowerModeInternal(display, HWC_POWER_MODE_NORMAL);
4482
4483 const nsecs_t vsyncPeriod = getVsyncPeriod();
4484 mAnimFrameTracker.setDisplayRefreshPeriod(vsyncPeriod);
4485
4486 // Use phase of 0 since phase is not known.
4487 // Use latency of 0, which will snap to the ideal latency.
4488 DisplayStatInfo stats{0 /* vsyncTime */, vsyncPeriod};
4489 setCompositorTimingSnapped(stats, 0);
4490 }
4491
initializeDisplays()4492 void SurfaceFlinger::initializeDisplays() {
4493 // Async since we may be called from the main thread.
4494 postMessageAsync(
4495 new LambdaMessage([this]() NO_THREAD_SAFETY_ANALYSIS { onInitializeDisplays(); }));
4496 }
4497
setVsyncEnabledInHWC(DisplayId displayId,HWC2::Vsync enabled)4498 void SurfaceFlinger::setVsyncEnabledInHWC(DisplayId displayId, HWC2::Vsync enabled) {
4499 if (mHWCVsyncState != enabled) {
4500 getHwComposer().setVsyncEnabled(displayId, enabled);
4501 mHWCVsyncState = enabled;
4502 }
4503 }
4504
setPowerModeInternal(const sp<DisplayDevice> & display,int mode)4505 void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& display, int mode) {
4506 if (display->isVirtual()) {
4507 ALOGE("%s: Invalid operation on virtual display", __FUNCTION__);
4508 return;
4509 }
4510
4511 const auto displayId = display->getId();
4512 LOG_ALWAYS_FATAL_IF(!displayId);
4513
4514 ALOGD("Setting power mode %d on display %s", mode, to_string(*displayId).c_str());
4515
4516 int currentMode = display->getPowerMode();
4517 if (mode == currentMode) {
4518 return;
4519 }
4520
4521 display->setPowerMode(mode);
4522
4523 if (mInterceptor->isEnabled()) {
4524 mInterceptor->savePowerModeUpdate(display->getSequenceId(), mode);
4525 }
4526
4527 if (currentMode == HWC_POWER_MODE_OFF) {
4528 // Turn on the display
4529 getHwComposer().setPowerMode(*displayId, mode);
4530 if (display->isPrimary() && mode != HWC_POWER_MODE_DOZE_SUSPEND) {
4531 setVsyncEnabledInHWC(*displayId, mHWCVsyncPendingState);
4532 mScheduler->onScreenAcquired(mAppConnectionHandle);
4533 mScheduler->resyncToHardwareVsync(true, getVsyncPeriod());
4534 }
4535
4536 mVisibleRegionsDirty = true;
4537 mHasPoweredOff = true;
4538 repaintEverything();
4539
4540 struct sched_param param = {0};
4541 param.sched_priority = 1;
4542 if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) {
4543 ALOGW("Couldn't set SCHED_FIFO on display on");
4544 }
4545 } else if (mode == HWC_POWER_MODE_OFF) {
4546 // Turn off the display
4547 struct sched_param param = {0};
4548 if (sched_setscheduler(0, SCHED_OTHER, ¶m) != 0) {
4549 ALOGW("Couldn't set SCHED_OTHER on display off");
4550 }
4551
4552 if (display->isPrimary() && currentMode != HWC_POWER_MODE_DOZE_SUSPEND) {
4553 mScheduler->disableHardwareVsync(true);
4554 mScheduler->onScreenReleased(mAppConnectionHandle);
4555 }
4556
4557 // Make sure HWVsync is disabled before turning off the display
4558 setVsyncEnabledInHWC(*displayId, HWC2::Vsync::Disable);
4559
4560 getHwComposer().setPowerMode(*displayId, mode);
4561 mVisibleRegionsDirty = true;
4562 // from this point on, SF will stop drawing on this display
4563 } else if (mode == HWC_POWER_MODE_DOZE ||
4564 mode == HWC_POWER_MODE_NORMAL) {
4565 // Update display while dozing
4566 getHwComposer().setPowerMode(*displayId, mode);
4567 if (display->isPrimary() && currentMode == HWC_POWER_MODE_DOZE_SUSPEND) {
4568 mScheduler->onScreenAcquired(mAppConnectionHandle);
4569 mScheduler->resyncToHardwareVsync(true, getVsyncPeriod());
4570 }
4571 } else if (mode == HWC_POWER_MODE_DOZE_SUSPEND) {
4572 // Leave display going to doze
4573 if (display->isPrimary()) {
4574 mScheduler->disableHardwareVsync(true);
4575 mScheduler->onScreenReleased(mAppConnectionHandle);
4576 }
4577 getHwComposer().setPowerMode(*displayId, mode);
4578 } else {
4579 ALOGE("Attempting to set unknown power mode: %d\n", mode);
4580 getHwComposer().setPowerMode(*displayId, mode);
4581 }
4582
4583 if (display->isPrimary()) {
4584 mTimeStats->setPowerMode(mode);
4585 mRefreshRateStats->setPowerMode(mode);
4586 mScheduler->setDisplayPowerState(mode == HWC_POWER_MODE_NORMAL);
4587 }
4588
4589 ALOGD("Finished setting power mode %d on display %s", mode, to_string(*displayId).c_str());
4590 }
4591
setPowerMode(const sp<IBinder> & displayToken,int mode)4592 void SurfaceFlinger::setPowerMode(const sp<IBinder>& displayToken, int mode) {
4593 postMessageSync(new LambdaMessage([&]() NO_THREAD_SAFETY_ANALYSIS {
4594 const auto display = getDisplayDevice(displayToken);
4595 if (!display) {
4596 ALOGE("Attempt to set power mode %d for invalid display token %p", mode,
4597 displayToken.get());
4598 } else if (display->isVirtual()) {
4599 ALOGW("Attempt to set power mode %d for virtual display", mode);
4600 } else {
4601 setPowerModeInternal(display, mode);
4602 }
4603 }));
4604 }
4605
4606 // ---------------------------------------------------------------------------
4607
doDump(int fd,const DumpArgs & args,bool asProto)4608 status_t SurfaceFlinger::doDump(int fd, const DumpArgs& args,
4609 bool asProto) NO_THREAD_SAFETY_ANALYSIS {
4610 std::string result;
4611
4612 IPCThreadState* ipc = IPCThreadState::self();
4613 const int pid = ipc->getCallingPid();
4614 const int uid = ipc->getCallingUid();
4615
4616 if ((uid != AID_SHELL) &&
4617 !PermissionCache::checkPermission(sDump, pid, uid)) {
4618 StringAppendF(&result, "Permission Denial: can't dump SurfaceFlinger from pid=%d, uid=%d\n",
4619 pid, uid);
4620 } else {
4621 // Try to get the main lock, but give up after one second
4622 // (this would indicate SF is stuck, but we want to be able to
4623 // print something in dumpsys).
4624 status_t err = mStateLock.timedLock(s2ns(1));
4625 bool locked = (err == NO_ERROR);
4626 if (!locked) {
4627 StringAppendF(&result,
4628 "SurfaceFlinger appears to be unresponsive (%s [%d]), dumping anyways "
4629 "(no locks held)\n",
4630 strerror(-err), err);
4631 }
4632
4633 using namespace std::string_literals;
4634
4635 static const std::unordered_map<std::string, Dumper> dumpers = {
4636 {"--clear-layer-stats"s, dumper([this](std::string&) { mLayerStats.clear(); })},
4637 {"--disable-layer-stats"s, dumper([this](std::string&) { mLayerStats.disable(); })},
4638 {"--display-id"s, dumper(&SurfaceFlinger::dumpDisplayIdentificationData)},
4639 {"--dispsync"s, dumper([this](std::string& s) {
4640 mScheduler->dumpPrimaryDispSync(s);
4641 })},
4642 {"--dump-layer-stats"s, dumper([this](std::string& s) { mLayerStats.dump(s); })},
4643 {"--enable-layer-stats"s, dumper([this](std::string&) { mLayerStats.enable(); })},
4644 {"--frame-events"s, dumper(&SurfaceFlinger::dumpFrameEventsLocked)},
4645 {"--latency"s, argsDumper(&SurfaceFlinger::dumpStatsLocked)},
4646 {"--latency-clear"s, argsDumper(&SurfaceFlinger::clearStatsLocked)},
4647 {"--list"s, dumper(&SurfaceFlinger::listLayersLocked)},
4648 {"--static-screen"s, dumper(&SurfaceFlinger::dumpStaticScreenStats)},
4649 {"--timestats"s, protoDumper(&SurfaceFlinger::dumpTimeStats)},
4650 {"--vsync"s, dumper(&SurfaceFlinger::dumpVSync)},
4651 {"--wide-color"s, dumper(&SurfaceFlinger::dumpWideColorInfo)},
4652 };
4653
4654 const auto flag = args.empty() ? ""s : std::string(String8(args[0]));
4655
4656 if (const auto it = dumpers.find(flag); it != dumpers.end()) {
4657 (it->second)(args, asProto, result);
4658 } else if (!asProto) {
4659 dumpAllLocked(args, result);
4660 }
4661
4662 if (locked) {
4663 mStateLock.unlock();
4664 }
4665
4666 LayersProto layersProto = dumpProtoFromMainThread();
4667 if (asProto) {
4668 result.append(layersProto.SerializeAsString().c_str(), layersProto.ByteSize());
4669 } else {
4670 auto layerTree = LayerProtoParser::generateLayerTree(layersProto);
4671 result.append(LayerProtoParser::layerTreeToString(layerTree));
4672 result.append("\n");
4673 }
4674 }
4675 write(fd, result.c_str(), result.size());
4676 return NO_ERROR;
4677 }
4678
dumpCritical(int fd,const DumpArgs &,bool asProto)4679 status_t SurfaceFlinger::dumpCritical(int fd, const DumpArgs&, bool asProto) {
4680 if (asProto && mTracing.isEnabled()) {
4681 mTracing.writeToFileAsync();
4682 }
4683
4684 return doDump(fd, DumpArgs(), asProto);
4685 }
4686
listLayersLocked(std::string & result) const4687 void SurfaceFlinger::listLayersLocked(std::string& result) const {
4688 mCurrentState.traverseInZOrder(
4689 [&](Layer* layer) { StringAppendF(&result, "%s\n", layer->getName().string()); });
4690 }
4691
dumpStatsLocked(const DumpArgs & args,std::string & result) const4692 void SurfaceFlinger::dumpStatsLocked(const DumpArgs& args, std::string& result) const {
4693 StringAppendF(&result, "%" PRId64 "\n", getVsyncPeriod());
4694
4695 if (args.size() > 1) {
4696 const auto name = String8(args[1]);
4697 mCurrentState.traverseInZOrder([&](Layer* layer) {
4698 if (name == layer->getName()) {
4699 layer->dumpFrameStats(result);
4700 }
4701 });
4702 } else {
4703 mAnimFrameTracker.dumpStats(result);
4704 }
4705 }
4706
clearStatsLocked(const DumpArgs & args,std::string &)4707 void SurfaceFlinger::clearStatsLocked(const DumpArgs& args, std::string&) {
4708 mCurrentState.traverseInZOrder([&](Layer* layer) {
4709 if (args.size() < 2 || String8(args[1]) == layer->getName()) {
4710 layer->clearFrameStats();
4711 }
4712 });
4713
4714 mAnimFrameTracker.clearStats();
4715 }
4716
dumpTimeStats(const DumpArgs & args,bool asProto,std::string & result) const4717 void SurfaceFlinger::dumpTimeStats(const DumpArgs& args, bool asProto, std::string& result) const {
4718 mTimeStats->parseArgs(asProto, args, result);
4719 }
4720
4721 // This should only be called from the main thread. Otherwise it would need
4722 // the lock and should use mCurrentState rather than mDrawingState.
logFrameStats()4723 void SurfaceFlinger::logFrameStats() {
4724 mDrawingState.traverseInZOrder([&](Layer* layer) {
4725 layer->logFrameStats();
4726 });
4727
4728 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
4729 }
4730
appendSfConfigString(std::string & result) const4731 void SurfaceFlinger::appendSfConfigString(std::string& result) const {
4732 result.append(" [sf");
4733
4734 if (isLayerTripleBufferingDisabled())
4735 result.append(" DISABLE_TRIPLE_BUFFERING");
4736
4737 StringAppendF(&result, " PRESENT_TIME_OFFSET=%" PRId64, dispSyncPresentTimeOffset);
4738 StringAppendF(&result, " FORCE_HWC_FOR_RBG_TO_YUV=%d", useHwcForRgbToYuv);
4739 StringAppendF(&result, " MAX_VIRT_DISPLAY_DIM=%" PRIu64, maxVirtualDisplaySize);
4740 StringAppendF(&result, " RUNNING_WITHOUT_SYNC_FRAMEWORK=%d", !hasSyncFramework);
4741 StringAppendF(&result, " NUM_FRAMEBUFFER_SURFACE_BUFFERS=%" PRId64,
4742 maxFrameBufferAcquiredBuffers);
4743 result.append("]");
4744 }
4745
dumpVSync(std::string & result) const4746 void SurfaceFlinger::dumpVSync(std::string& result) const {
4747 mPhaseOffsets->dump(result);
4748 StringAppendF(&result,
4749 " present offset: %9" PRId64 " ns\t VSYNC period: %9" PRId64 " ns\n\n",
4750 dispSyncPresentTimeOffset, getVsyncPeriod());
4751
4752 StringAppendF(&result, "Scheduler enabled.");
4753 StringAppendF(&result, "+ Smart 90 for video detection: %s\n\n",
4754 mUseSmart90ForVideo ? "on" : "off");
4755 StringAppendF(&result, "Allowed Display Configs: ");
4756 for (int32_t configId : mAllowedDisplayConfigs) {
4757 StringAppendF(&result, "%" PRIu32 " Hz, ",
4758 mRefreshRateConfigs->getRefreshRateFromConfigId(configId).fps);
4759 }
4760 StringAppendF(&result, "(config override by backdoor: %s)\n\n",
4761 mDebugDisplayConfigSetByBackdoor ? "yes" : "no");
4762 mScheduler->dump(mAppConnectionHandle, result);
4763 StringAppendF(&result, "+ Refresh rate switching: %s\n",
4764 mRefreshRateConfigs->refreshRateSwitchingSupported() ? "on" : "off");
4765 }
4766
dumpStaticScreenStats(std::string & result) const4767 void SurfaceFlinger::dumpStaticScreenStats(std::string& result) const {
4768 result.append("Static screen stats:\n");
4769 for (size_t b = 0; b < SurfaceFlingerBE::NUM_BUCKETS - 1; ++b) {
4770 float bucketTimeSec = getBE().mFrameBuckets[b] / 1e9;
4771 float percent = 100.0f *
4772 static_cast<float>(getBE().mFrameBuckets[b]) / getBE().mTotalTime;
4773 StringAppendF(&result, " < %zd frames: %.3f s (%.1f%%)\n", b + 1, bucketTimeSec, percent);
4774 }
4775 float bucketTimeSec = getBE().mFrameBuckets[SurfaceFlingerBE::NUM_BUCKETS - 1] / 1e9;
4776 float percent = 100.0f *
4777 static_cast<float>(getBE().mFrameBuckets[SurfaceFlingerBE::NUM_BUCKETS - 1]) / getBE().mTotalTime;
4778 StringAppendF(&result, " %zd+ frames: %.3f s (%.1f%%)\n", SurfaceFlingerBE::NUM_BUCKETS - 1,
4779 bucketTimeSec, percent);
4780 }
4781
recordBufferingStats(const char * layerName,std::vector<OccupancyTracker::Segment> && history)4782 void SurfaceFlinger::recordBufferingStats(const char* layerName,
4783 std::vector<OccupancyTracker::Segment>&& history) {
4784 Mutex::Autolock lock(getBE().mBufferingStatsMutex);
4785 auto& stats = getBE().mBufferingStats[layerName];
4786 for (const auto& segment : history) {
4787 if (!segment.usedThirdBuffer) {
4788 stats.twoBufferTime += segment.totalTime;
4789 }
4790 if (segment.occupancyAverage < 1.0f) {
4791 stats.doubleBufferedTime += segment.totalTime;
4792 } else if (segment.occupancyAverage < 2.0f) {
4793 stats.tripleBufferedTime += segment.totalTime;
4794 }
4795 ++stats.numSegments;
4796 stats.totalTime += segment.totalTime;
4797 }
4798 }
4799
dumpFrameEventsLocked(std::string & result)4800 void SurfaceFlinger::dumpFrameEventsLocked(std::string& result) {
4801 result.append("Layer frame timestamps:\n");
4802
4803 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
4804 const size_t count = currentLayers.size();
4805 for (size_t i=0 ; i<count ; i++) {
4806 currentLayers[i]->dumpFrameEvents(result);
4807 }
4808 }
4809
dumpBufferingStats(std::string & result) const4810 void SurfaceFlinger::dumpBufferingStats(std::string& result) const {
4811 result.append("Buffering stats:\n");
4812 result.append(" [Layer name] <Active time> <Two buffer> "
4813 "<Double buffered> <Triple buffered>\n");
4814 Mutex::Autolock lock(getBE().mBufferingStatsMutex);
4815 typedef std::tuple<std::string, float, float, float> BufferTuple;
4816 std::map<float, BufferTuple, std::greater<float>> sorted;
4817 for (const auto& statsPair : getBE().mBufferingStats) {
4818 const char* name = statsPair.first.c_str();
4819 const SurfaceFlingerBE::BufferingStats& stats = statsPair.second;
4820 if (stats.numSegments == 0) {
4821 continue;
4822 }
4823 float activeTime = ns2ms(stats.totalTime) / 1000.0f;
4824 float twoBufferRatio = static_cast<float>(stats.twoBufferTime) /
4825 stats.totalTime;
4826 float doubleBufferRatio = static_cast<float>(
4827 stats.doubleBufferedTime) / stats.totalTime;
4828 float tripleBufferRatio = static_cast<float>(
4829 stats.tripleBufferedTime) / stats.totalTime;
4830 sorted.insert({activeTime, {name, twoBufferRatio,
4831 doubleBufferRatio, tripleBufferRatio}});
4832 }
4833 for (const auto& sortedPair : sorted) {
4834 float activeTime = sortedPair.first;
4835 const BufferTuple& values = sortedPair.second;
4836 StringAppendF(&result, " [%s] %.2f %.3f %.3f %.3f\n", std::get<0>(values).c_str(),
4837 activeTime, std::get<1>(values), std::get<2>(values), std::get<3>(values));
4838 }
4839 result.append("\n");
4840 }
4841
dumpDisplayIdentificationData(std::string & result) const4842 void SurfaceFlinger::dumpDisplayIdentificationData(std::string& result) const {
4843 for (const auto& [token, display] : mDisplays) {
4844 const auto displayId = display->getId();
4845 if (!displayId) {
4846 continue;
4847 }
4848 const auto hwcDisplayId = getHwComposer().fromPhysicalDisplayId(*displayId);
4849 if (!hwcDisplayId) {
4850 continue;
4851 }
4852
4853 StringAppendF(&result,
4854 "Display %s (HWC display %" PRIu64 "): ", to_string(*displayId).c_str(),
4855 *hwcDisplayId);
4856 uint8_t port;
4857 DisplayIdentificationData data;
4858 if (!getHwComposer().getDisplayIdentificationData(*hwcDisplayId, &port, &data)) {
4859 result.append("no identification data\n");
4860 continue;
4861 }
4862
4863 if (!isEdid(data)) {
4864 result.append("unknown identification data: ");
4865 for (uint8_t byte : data) {
4866 StringAppendF(&result, "%x ", byte);
4867 }
4868 result.append("\n");
4869 continue;
4870 }
4871
4872 const auto edid = parseEdid(data);
4873 if (!edid) {
4874 result.append("invalid EDID: ");
4875 for (uint8_t byte : data) {
4876 StringAppendF(&result, "%x ", byte);
4877 }
4878 result.append("\n");
4879 continue;
4880 }
4881
4882 StringAppendF(&result, "port=%u pnpId=%s displayName=\"", port, edid->pnpId.data());
4883 result.append(edid->displayName.data(), edid->displayName.length());
4884 result.append("\"\n");
4885 }
4886 }
4887
dumpWideColorInfo(std::string & result) const4888 void SurfaceFlinger::dumpWideColorInfo(std::string& result) const {
4889 StringAppendF(&result, "Device has wide color built-in display: %d\n", hasWideColorDisplay);
4890 StringAppendF(&result, "Device uses color management: %d\n", useColorManagement);
4891 StringAppendF(&result, "DisplayColorSetting: %s\n",
4892 decodeDisplayColorSetting(mDisplayColorSetting).c_str());
4893
4894 // TODO: print out if wide-color mode is active or not
4895
4896 for (const auto& [token, display] : mDisplays) {
4897 const auto displayId = display->getId();
4898 if (!displayId) {
4899 continue;
4900 }
4901
4902 StringAppendF(&result, "Display %s color modes:\n", to_string(*displayId).c_str());
4903 std::vector<ColorMode> modes = getHwComposer().getColorModes(*displayId);
4904 for (auto&& mode : modes) {
4905 StringAppendF(&result, " %s (%d)\n", decodeColorMode(mode).c_str(), mode);
4906 }
4907
4908 ColorMode currentMode = display->getCompositionDisplay()->getState().colorMode;
4909 StringAppendF(&result, " Current color mode: %s (%d)\n",
4910 decodeColorMode(currentMode).c_str(), currentMode);
4911 }
4912 result.append("\n");
4913 }
4914
dumpDrawingStateProto(uint32_t traceFlags) const4915 LayersProto SurfaceFlinger::dumpDrawingStateProto(uint32_t traceFlags) const {
4916 LayersProto layersProto;
4917 mDrawingState.traverseInZOrder([&](Layer* layer) {
4918 LayerProto* layerProto = layersProto.add_layers();
4919 layer->writeToProtoDrawingState(layerProto, traceFlags);
4920 layer->writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
4921 });
4922
4923 return layersProto;
4924 }
4925
dumpProtoFromMainThread(uint32_t traceFlags)4926 LayersProto SurfaceFlinger::dumpProtoFromMainThread(uint32_t traceFlags) {
4927 LayersProto layersProto;
4928 postMessageSync(new LambdaMessage([&]() { layersProto = dumpDrawingStateProto(traceFlags); }));
4929 return layersProto;
4930 }
4931
dumpVisibleLayersProtoInfo(const sp<DisplayDevice> & displayDevice) const4932 LayersProto SurfaceFlinger::dumpVisibleLayersProtoInfo(
4933 const sp<DisplayDevice>& displayDevice) const {
4934 LayersProto layersProto;
4935
4936 SizeProto* resolution = layersProto.mutable_resolution();
4937 resolution->set_w(displayDevice->getWidth());
4938 resolution->set_h(displayDevice->getHeight());
4939
4940 auto display = displayDevice->getCompositionDisplay();
4941 const auto& displayState = display->getState();
4942
4943 layersProto.set_color_mode(decodeColorMode(displayState.colorMode));
4944 layersProto.set_color_transform(decodeColorTransform(displayState.colorTransform));
4945 layersProto.set_global_transform(displayState.orientation);
4946
4947 const auto displayId = displayDevice->getId();
4948 LOG_ALWAYS_FATAL_IF(!displayId);
4949 mDrawingState.traverseInZOrder([&](Layer* layer) {
4950 if (!layer->visibleRegion.isEmpty() && !display->getOutputLayersOrderedByZ().empty()) {
4951 LayerProto* layerProto = layersProto.add_layers();
4952 layer->writeToProtoCompositionState(layerProto, displayDevice);
4953 }
4954 });
4955
4956 return layersProto;
4957 }
4958
dumpAllLocked(const DumpArgs & args,std::string & result) const4959 void SurfaceFlinger::dumpAllLocked(const DumpArgs& args, std::string& result) const {
4960 const bool colorize = !args.empty() && args[0] == String16("--color");
4961 Colorizer colorizer(colorize);
4962
4963 // figure out if we're stuck somewhere
4964 const nsecs_t now = systemTime();
4965 const nsecs_t inTransaction(mDebugInTransaction);
4966 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
4967
4968 /*
4969 * Dump library configuration.
4970 */
4971
4972 colorizer.bold(result);
4973 result.append("Build configuration:");
4974 colorizer.reset(result);
4975 appendSfConfigString(result);
4976 appendUiConfigString(result);
4977 appendGuiConfigString(result);
4978 result.append("\n");
4979
4980 result.append("\nDisplay identification data:\n");
4981 dumpDisplayIdentificationData(result);
4982
4983 result.append("\nWide-Color information:\n");
4984 dumpWideColorInfo(result);
4985
4986 colorizer.bold(result);
4987 result.append("Sync configuration: ");
4988 colorizer.reset(result);
4989 result.append(SyncFeatures::getInstance().toString());
4990 result.append("\n\n");
4991
4992 colorizer.bold(result);
4993 result.append("VSYNC configuration:\n");
4994 colorizer.reset(result);
4995 dumpVSync(result);
4996 result.append("\n");
4997
4998 dumpStaticScreenStats(result);
4999 result.append("\n");
5000
5001 StringAppendF(&result, "Total missed frame count: %u\n", mFrameMissedCount.load());
5002 StringAppendF(&result, "HWC missed frame count: %u\n", mHwcFrameMissedCount.load());
5003 StringAppendF(&result, "GPU missed frame count: %u\n\n", mGpuFrameMissedCount.load());
5004
5005 dumpBufferingStats(result);
5006
5007 /*
5008 * Dump the visible layer list
5009 */
5010 colorizer.bold(result);
5011 StringAppendF(&result, "Visible layers (count = %zu)\n", mNumLayers);
5012 StringAppendF(&result, "GraphicBufferProducers: %zu, max %zu\n",
5013 mGraphicBufferProducerList.size(), mMaxGraphicBufferProducerListSize);
5014 colorizer.reset(result);
5015
5016 {
5017 StringAppendF(&result, "Composition layers\n");
5018 mDrawingState.traverseInZOrder([&](Layer* layer) {
5019 auto compositionLayer = layer->getCompositionLayer();
5020 if (compositionLayer) compositionLayer->dump(result);
5021 });
5022 }
5023
5024 /*
5025 * Dump Display state
5026 */
5027
5028 colorizer.bold(result);
5029 StringAppendF(&result, "Displays (%zu entries)\n", mDisplays.size());
5030 colorizer.reset(result);
5031 for (const auto& [token, display] : mDisplays) {
5032 display->dump(result);
5033 }
5034 result.append("\n");
5035
5036 /*
5037 * Dump SurfaceFlinger global state
5038 */
5039
5040 colorizer.bold(result);
5041 result.append("SurfaceFlinger global state:\n");
5042 colorizer.reset(result);
5043
5044 getRenderEngine().dump(result);
5045
5046 DebugEGLImageTracker::getInstance()->dump(result);
5047
5048 if (const auto display = getDefaultDisplayDeviceLocked()) {
5049 display->getCompositionDisplay()->getState().undefinedRegion.dump(result,
5050 "undefinedRegion");
5051 StringAppendF(&result, " orientation=%d, isPoweredOn=%d\n", display->getOrientation(),
5052 display->isPoweredOn());
5053 }
5054 StringAppendF(&result,
5055 " transaction-flags : %08x\n"
5056 " gpu_to_cpu_unsupported : %d\n",
5057 mTransactionFlags.load(), !mGpuToCpuSupported);
5058
5059 if (const auto displayId = getInternalDisplayIdLocked();
5060 displayId && getHwComposer().isConnected(*displayId)) {
5061 const auto activeConfig = getHwComposer().getActiveConfig(*displayId);
5062 StringAppendF(&result,
5063 " refresh-rate : %f fps\n"
5064 " x-dpi : %f\n"
5065 " y-dpi : %f\n",
5066 1e9 / activeConfig->getVsyncPeriod(), activeConfig->getDpiX(),
5067 activeConfig->getDpiY());
5068 }
5069
5070 StringAppendF(&result, " transaction time: %f us\n", inTransactionDuration / 1000.0);
5071
5072 /*
5073 * Tracing state
5074 */
5075 mTracing.dump(result);
5076 result.append("\n");
5077
5078 /*
5079 * HWC layer minidump
5080 */
5081 for (const auto& [token, display] : mDisplays) {
5082 const auto displayId = display->getId();
5083 if (!displayId) {
5084 continue;
5085 }
5086
5087 StringAppendF(&result, "Display %s HWC layers:\n", to_string(*displayId).c_str());
5088 Layer::miniDumpHeader(result);
5089 const sp<DisplayDevice> displayDevice = display;
5090 mCurrentState.traverseInZOrder(
5091 [&](Layer* layer) { layer->miniDump(result, displayDevice); });
5092 result.append("\n");
5093 }
5094
5095 /*
5096 * Dump HWComposer state
5097 */
5098 colorizer.bold(result);
5099 result.append("h/w composer state:\n");
5100 colorizer.reset(result);
5101 bool hwcDisabled = mDebugDisableHWC || mDebugRegion;
5102 StringAppendF(&result, " h/w composer %s\n", hwcDisabled ? "disabled" : "enabled");
5103 getHwComposer().dump(result);
5104
5105 /*
5106 * Dump gralloc state
5107 */
5108 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
5109 alloc.dump(result);
5110
5111 /*
5112 * Dump VrFlinger state if in use.
5113 */
5114 if (mVrFlingerRequestsDisplay && mVrFlinger) {
5115 result.append("VrFlinger state:\n");
5116 result.append(mVrFlinger->Dump());
5117 result.append("\n");
5118 }
5119
5120 /**
5121 * Scheduler dump state.
5122 */
5123 result.append("\nScheduler state:\n");
5124 result.append(mScheduler->doDump() + "\n");
5125 StringAppendF(&result, "+ Smart video mode: %s\n\n", mUseSmart90ForVideo ? "on" : "off");
5126 result.append(mRefreshRateStats->doDump() + "\n");
5127
5128 result.append(mTimeStats->miniDump());
5129 result.append("\n");
5130 }
5131
getLayerSortedByZForHwcDisplay(DisplayId displayId)5132 const Vector<sp<Layer>>& SurfaceFlinger::getLayerSortedByZForHwcDisplay(DisplayId displayId) {
5133 // Note: mStateLock is held here
5134 for (const auto& [token, display] : mDisplays) {
5135 if (display->getId() == displayId) {
5136 return getDisplayDeviceLocked(token)->getVisibleLayersSortedByZ();
5137 }
5138 }
5139
5140 ALOGE("%s: Invalid display %s", __FUNCTION__, to_string(displayId).c_str());
5141 static const Vector<sp<Layer>> empty;
5142 return empty;
5143 }
5144
updateColorMatrixLocked()5145 void SurfaceFlinger::updateColorMatrixLocked() {
5146 mat4 colorMatrix;
5147 if (mGlobalSaturationFactor != 1.0f) {
5148 // Rec.709 luma coefficients
5149 float3 luminance{0.213f, 0.715f, 0.072f};
5150 luminance *= 1.0f - mGlobalSaturationFactor;
5151 mat4 saturationMatrix = mat4(
5152 vec4{luminance.r + mGlobalSaturationFactor, luminance.r, luminance.r, 0.0f},
5153 vec4{luminance.g, luminance.g + mGlobalSaturationFactor, luminance.g, 0.0f},
5154 vec4{luminance.b, luminance.b, luminance.b + mGlobalSaturationFactor, 0.0f},
5155 vec4{0.0f, 0.0f, 0.0f, 1.0f}
5156 );
5157 colorMatrix = mClientColorMatrix * saturationMatrix * mDaltonizer();
5158 } else {
5159 colorMatrix = mClientColorMatrix * mDaltonizer();
5160 }
5161
5162 if (mCurrentState.colorMatrix != colorMatrix) {
5163 mCurrentState.colorMatrix = colorMatrix;
5164 mCurrentState.colorMatrixChanged = true;
5165 setTransactionFlags(eTransactionNeeded);
5166 }
5167 }
5168
CheckTransactCodeCredentials(uint32_t code)5169 status_t SurfaceFlinger::CheckTransactCodeCredentials(uint32_t code) {
5170 #pragma clang diagnostic push
5171 #pragma clang diagnostic error "-Wswitch-enum"
5172 switch (static_cast<ISurfaceComposerTag>(code)) {
5173 // These methods should at minimum make sure that the client requested
5174 // access to SF.
5175 case BOOT_FINISHED:
5176 case CLEAR_ANIMATION_FRAME_STATS:
5177 case CREATE_DISPLAY:
5178 case DESTROY_DISPLAY:
5179 case ENABLE_VSYNC_INJECTIONS:
5180 case GET_ANIMATION_FRAME_STATS:
5181 case GET_HDR_CAPABILITIES:
5182 case SET_ACTIVE_CONFIG:
5183 case SET_ALLOWED_DISPLAY_CONFIGS:
5184 case GET_ALLOWED_DISPLAY_CONFIGS:
5185 case SET_ACTIVE_COLOR_MODE:
5186 case INJECT_VSYNC:
5187 case SET_POWER_MODE:
5188 case GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES:
5189 case SET_DISPLAY_CONTENT_SAMPLING_ENABLED:
5190 case GET_DISPLAYED_CONTENT_SAMPLE:
5191 case NOTIFY_POWER_HINT: {
5192 if (!callingThreadHasUnscopedSurfaceFlingerAccess()) {
5193 IPCThreadState* ipc = IPCThreadState::self();
5194 ALOGE("Permission Denial: can't access SurfaceFlinger pid=%d, uid=%d",
5195 ipc->getCallingPid(), ipc->getCallingUid());
5196 return PERMISSION_DENIED;
5197 }
5198 return OK;
5199 }
5200 case GET_LAYER_DEBUG_INFO: {
5201 IPCThreadState* ipc = IPCThreadState::self();
5202 const int pid = ipc->getCallingPid();
5203 const int uid = ipc->getCallingUid();
5204 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
5205 ALOGE("Layer debug info permission denied for pid=%d, uid=%d", pid, uid);
5206 return PERMISSION_DENIED;
5207 }
5208 return OK;
5209 }
5210 // Used by apps to hook Choreographer to SurfaceFlinger.
5211 case CREATE_DISPLAY_EVENT_CONNECTION:
5212 // The following calls are currently used by clients that do not
5213 // request necessary permissions. However, they do not expose any secret
5214 // information, so it is OK to pass them.
5215 case AUTHENTICATE_SURFACE:
5216 case GET_ACTIVE_COLOR_MODE:
5217 case GET_ACTIVE_CONFIG:
5218 case GET_PHYSICAL_DISPLAY_IDS:
5219 case GET_PHYSICAL_DISPLAY_TOKEN:
5220 case GET_DISPLAY_COLOR_MODES:
5221 case GET_DISPLAY_NATIVE_PRIMARIES:
5222 case GET_DISPLAY_CONFIGS:
5223 case GET_DISPLAY_STATS:
5224 case GET_SUPPORTED_FRAME_TIMESTAMPS:
5225 // Calling setTransactionState is safe, because you need to have been
5226 // granted a reference to Client* and Handle* to do anything with it.
5227 case SET_TRANSACTION_STATE:
5228 case CREATE_CONNECTION:
5229 case GET_COLOR_MANAGEMENT:
5230 case GET_COMPOSITION_PREFERENCE:
5231 case GET_PROTECTED_CONTENT_SUPPORT:
5232 case IS_WIDE_COLOR_DISPLAY:
5233 case GET_DISPLAY_BRIGHTNESS_SUPPORT:
5234 case SET_DISPLAY_BRIGHTNESS: {
5235 return OK;
5236 }
5237 case CAPTURE_LAYERS:
5238 case CAPTURE_SCREEN:
5239 case ADD_REGION_SAMPLING_LISTENER:
5240 case REMOVE_REGION_SAMPLING_LISTENER: {
5241 // codes that require permission check
5242 IPCThreadState* ipc = IPCThreadState::self();
5243 const int pid = ipc->getCallingPid();
5244 const int uid = ipc->getCallingUid();
5245 if ((uid != AID_GRAPHICS) &&
5246 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
5247 ALOGE("Permission Denial: can't read framebuffer pid=%d, uid=%d", pid, uid);
5248 return PERMISSION_DENIED;
5249 }
5250 return OK;
5251 }
5252 // The following codes are deprecated and should never be allowed to access SF.
5253 case CONNECT_DISPLAY_UNUSED:
5254 case CREATE_GRAPHIC_BUFFER_ALLOC_UNUSED: {
5255 ALOGE("Attempting to access SurfaceFlinger with unused code: %u", code);
5256 return PERMISSION_DENIED;
5257 }
5258 case CAPTURE_SCREEN_BY_ID: {
5259 IPCThreadState* ipc = IPCThreadState::self();
5260 const int uid = ipc->getCallingUid();
5261 if (uid == AID_ROOT || uid == AID_GRAPHICS || uid == AID_SYSTEM || uid == AID_SHELL) {
5262 return OK;
5263 }
5264 return PERMISSION_DENIED;
5265 }
5266 }
5267
5268 // These codes are used for the IBinder protocol to either interrogate the recipient
5269 // side of the transaction for its canonical interface descriptor or to dump its state.
5270 // We let them pass by default.
5271 if (code == IBinder::INTERFACE_TRANSACTION || code == IBinder::DUMP_TRANSACTION ||
5272 code == IBinder::PING_TRANSACTION || code == IBinder::SHELL_COMMAND_TRANSACTION ||
5273 code == IBinder::SYSPROPS_TRANSACTION) {
5274 return OK;
5275 }
5276 // Numbers from 1000 to 1034 are currently used for backdoors. The code
5277 // in onTransact verifies that the user is root, and has access to use SF.
5278 if (code >= 1000 && code <= 1035) {
5279 ALOGV("Accessing SurfaceFlinger through backdoor code: %u", code);
5280 return OK;
5281 }
5282 ALOGE("Permission Denial: SurfaceFlinger did not recognize request code: %u", code);
5283 return PERMISSION_DENIED;
5284 #pragma clang diagnostic pop
5285 }
5286
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)5287 status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
5288 uint32_t flags) {
5289 status_t credentialCheck = CheckTransactCodeCredentials(code);
5290 if (credentialCheck != OK) {
5291 return credentialCheck;
5292 }
5293
5294 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
5295 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
5296 CHECK_INTERFACE(ISurfaceComposer, data, reply);
5297 IPCThreadState* ipc = IPCThreadState::self();
5298 const int uid = ipc->getCallingUid();
5299 if (CC_UNLIKELY(uid != AID_SYSTEM
5300 && !PermissionCache::checkCallingPermission(sHardwareTest))) {
5301 const int pid = ipc->getCallingPid();
5302 ALOGE("Permission Denial: "
5303 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
5304 return PERMISSION_DENIED;
5305 }
5306 int n;
5307 switch (code) {
5308 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
5309 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
5310 return NO_ERROR;
5311 case 1002: // SHOW_UPDATES
5312 n = data.readInt32();
5313 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
5314 invalidateHwcGeometry();
5315 repaintEverything();
5316 return NO_ERROR;
5317 case 1004:{ // repaint everything
5318 repaintEverything();
5319 return NO_ERROR;
5320 }
5321 case 1005:{ // force transaction
5322 Mutex::Autolock _l(mStateLock);
5323 setTransactionFlags(
5324 eTransactionNeeded|
5325 eDisplayTransactionNeeded|
5326 eTraversalNeeded);
5327 return NO_ERROR;
5328 }
5329 case 1006:{ // send empty update
5330 signalRefresh();
5331 return NO_ERROR;
5332 }
5333 case 1008: // toggle use of hw composer
5334 n = data.readInt32();
5335 mDebugDisableHWC = n != 0;
5336 invalidateHwcGeometry();
5337 repaintEverything();
5338 return NO_ERROR;
5339 case 1009: // toggle use of transform hint
5340 n = data.readInt32();
5341 mDebugDisableTransformHint = n != 0;
5342 invalidateHwcGeometry();
5343 repaintEverything();
5344 return NO_ERROR;
5345 case 1010: // interrogate.
5346 reply->writeInt32(0);
5347 reply->writeInt32(0);
5348 reply->writeInt32(mDebugRegion);
5349 reply->writeInt32(0);
5350 reply->writeInt32(mDebugDisableHWC);
5351 return NO_ERROR;
5352 case 1013: {
5353 const auto display = getDefaultDisplayDevice();
5354 if (!display) {
5355 return NAME_NOT_FOUND;
5356 }
5357
5358 reply->writeInt32(display->getPageFlipCount());
5359 return NO_ERROR;
5360 }
5361 case 1014: {
5362 Mutex::Autolock _l(mStateLock);
5363 // daltonize
5364 n = data.readInt32();
5365 switch (n % 10) {
5366 case 1:
5367 mDaltonizer.setType(ColorBlindnessType::Protanomaly);
5368 break;
5369 case 2:
5370 mDaltonizer.setType(ColorBlindnessType::Deuteranomaly);
5371 break;
5372 case 3:
5373 mDaltonizer.setType(ColorBlindnessType::Tritanomaly);
5374 break;
5375 default:
5376 mDaltonizer.setType(ColorBlindnessType::None);
5377 break;
5378 }
5379 if (n >= 10) {
5380 mDaltonizer.setMode(ColorBlindnessMode::Correction);
5381 } else {
5382 mDaltonizer.setMode(ColorBlindnessMode::Simulation);
5383 }
5384
5385 updateColorMatrixLocked();
5386 return NO_ERROR;
5387 }
5388 case 1015: {
5389 Mutex::Autolock _l(mStateLock);
5390 // apply a color matrix
5391 n = data.readInt32();
5392 if (n) {
5393 // color matrix is sent as a column-major mat4 matrix
5394 for (size_t i = 0 ; i < 4; i++) {
5395 for (size_t j = 0; j < 4; j++) {
5396 mClientColorMatrix[i][j] = data.readFloat();
5397 }
5398 }
5399 } else {
5400 mClientColorMatrix = mat4();
5401 }
5402
5403 // Check that supplied matrix's last row is {0,0,0,1} so we can avoid
5404 // the division by w in the fragment shader
5405 float4 lastRow(transpose(mClientColorMatrix)[3]);
5406 if (any(greaterThan(abs(lastRow - float4{0, 0, 0, 1}), float4{1e-4f}))) {
5407 ALOGE("The color transform's last row must be (0, 0, 0, 1)");
5408 }
5409
5410 updateColorMatrixLocked();
5411 return NO_ERROR;
5412 }
5413 // This is an experimental interface
5414 // Needs to be shifted to proper binder interface when we productize
5415 case 1016: {
5416 n = data.readInt32();
5417 // TODO(b/113612090): Evaluate if this can be removed.
5418 mScheduler->setRefreshSkipCount(n);
5419 return NO_ERROR;
5420 }
5421 case 1017: {
5422 n = data.readInt32();
5423 mForceFullDamage = n != 0;
5424 return NO_ERROR;
5425 }
5426 case 1018: { // Modify Choreographer's phase offset
5427 n = data.readInt32();
5428 mScheduler->setPhaseOffset(mAppConnectionHandle, static_cast<nsecs_t>(n));
5429 return NO_ERROR;
5430 }
5431 case 1019: { // Modify SurfaceFlinger's phase offset
5432 n = data.readInt32();
5433 mScheduler->setPhaseOffset(mSfConnectionHandle, static_cast<nsecs_t>(n));
5434 return NO_ERROR;
5435 }
5436 case 1020: { // Layer updates interceptor
5437 n = data.readInt32();
5438 if (n) {
5439 ALOGV("Interceptor enabled");
5440 mInterceptor->enable(mDrawingState.layersSortedByZ, mDrawingState.displays);
5441 }
5442 else{
5443 ALOGV("Interceptor disabled");
5444 mInterceptor->disable();
5445 }
5446 return NO_ERROR;
5447 }
5448 case 1021: { // Disable HWC virtual displays
5449 n = data.readInt32();
5450 mUseHwcVirtualDisplays = !n;
5451 return NO_ERROR;
5452 }
5453 case 1022: { // Set saturation boost
5454 Mutex::Autolock _l(mStateLock);
5455 mGlobalSaturationFactor = std::max(0.0f, std::min(data.readFloat(), 2.0f));
5456
5457 updateColorMatrixLocked();
5458 return NO_ERROR;
5459 }
5460 case 1023: { // Set native mode
5461 int32_t colorMode;
5462
5463 mDisplayColorSetting = static_cast<DisplayColorSetting>(data.readInt32());
5464 if (data.readInt32(&colorMode) == NO_ERROR) {
5465 mForceColorMode = static_cast<ColorMode>(colorMode);
5466 }
5467 invalidateHwcGeometry();
5468 repaintEverything();
5469 return NO_ERROR;
5470 }
5471 // Deprecate, use 1030 to check whether the device is color managed.
5472 case 1024: {
5473 return NAME_NOT_FOUND;
5474 }
5475 case 1025: { // Set layer tracing
5476 n = data.readInt32();
5477 if (n) {
5478 ALOGD("LayerTracing enabled");
5479 Mutex::Autolock lock(mStateLock);
5480 mTracingEnabledChanged = true;
5481 mTracing.enable();
5482 reply->writeInt32(NO_ERROR);
5483 } else {
5484 ALOGD("LayerTracing disabled");
5485 bool writeFile = false;
5486 {
5487 Mutex::Autolock lock(mStateLock);
5488 mTracingEnabledChanged = true;
5489 writeFile = mTracing.disable();
5490 }
5491
5492 if (writeFile) {
5493 reply->writeInt32(mTracing.writeToFile());
5494 } else {
5495 reply->writeInt32(NO_ERROR);
5496 }
5497 }
5498 return NO_ERROR;
5499 }
5500 case 1026: { // Get layer tracing status
5501 reply->writeBool(mTracing.isEnabled());
5502 return NO_ERROR;
5503 }
5504 // Is a DisplayColorSetting supported?
5505 case 1027: {
5506 const auto display = getDefaultDisplayDevice();
5507 if (!display) {
5508 return NAME_NOT_FOUND;
5509 }
5510
5511 DisplayColorSetting setting = static_cast<DisplayColorSetting>(data.readInt32());
5512 switch (setting) {
5513 case DisplayColorSetting::MANAGED:
5514 reply->writeBool(useColorManagement);
5515 break;
5516 case DisplayColorSetting::UNMANAGED:
5517 reply->writeBool(true);
5518 break;
5519 case DisplayColorSetting::ENHANCED:
5520 reply->writeBool(display->hasRenderIntent(RenderIntent::ENHANCE));
5521 break;
5522 default: // vendor display color setting
5523 reply->writeBool(
5524 display->hasRenderIntent(static_cast<RenderIntent>(setting)));
5525 break;
5526 }
5527 return NO_ERROR;
5528 }
5529 // Is VrFlinger active?
5530 case 1028: {
5531 Mutex::Autolock _l(mStateLock);
5532 reply->writeBool(getHwComposer().isUsingVrComposer());
5533 return NO_ERROR;
5534 }
5535 // Set buffer size for SF tracing (value in KB)
5536 case 1029: {
5537 n = data.readInt32();
5538 if (n <= 0 || n > MAX_TRACING_MEMORY) {
5539 ALOGW("Invalid buffer size: %d KB", n);
5540 reply->writeInt32(BAD_VALUE);
5541 return BAD_VALUE;
5542 }
5543
5544 ALOGD("Updating trace buffer to %d KB", n);
5545 mTracing.setBufferSize(n * 1024);
5546 reply->writeInt32(NO_ERROR);
5547 return NO_ERROR;
5548 }
5549 // Is device color managed?
5550 case 1030: {
5551 reply->writeBool(useColorManagement);
5552 return NO_ERROR;
5553 }
5554 // Override default composition data space
5555 // adb shell service call SurfaceFlinger 1031 i32 1 DATASPACE_NUMBER DATASPACE_NUMBER \
5556 // && adb shell stop zygote && adb shell start zygote
5557 // to restore: adb shell service call SurfaceFlinger 1031 i32 0 && \
5558 // adb shell stop zygote && adb shell start zygote
5559 case 1031: {
5560 Mutex::Autolock _l(mStateLock);
5561 n = data.readInt32();
5562 if (n) {
5563 n = data.readInt32();
5564 if (n) {
5565 Dataspace dataspace = static_cast<Dataspace>(n);
5566 if (!validateCompositionDataspace(dataspace)) {
5567 return BAD_VALUE;
5568 }
5569 mDefaultCompositionDataspace = dataspace;
5570 }
5571 n = data.readInt32();
5572 if (n) {
5573 Dataspace dataspace = static_cast<Dataspace>(n);
5574 if (!validateCompositionDataspace(dataspace)) {
5575 return BAD_VALUE;
5576 }
5577 mWideColorGamutCompositionDataspace = dataspace;
5578 }
5579 } else {
5580 // restore composition data space.
5581 mDefaultCompositionDataspace = defaultCompositionDataspace;
5582 mWideColorGamutCompositionDataspace = wideColorGamutCompositionDataspace;
5583 }
5584 return NO_ERROR;
5585 }
5586 // Set trace flags
5587 case 1033: {
5588 n = data.readUint32();
5589 ALOGD("Updating trace flags to 0x%x", n);
5590 mTracing.setTraceFlags(n);
5591 reply->writeInt32(NO_ERROR);
5592 return NO_ERROR;
5593 }
5594 case 1034: {
5595 // TODO(b/129297325): expose this via developer menu option
5596 n = data.readInt32();
5597 if (n && !mRefreshRateOverlay &&
5598 mRefreshRateConfigs->refreshRateSwitchingSupported()) {
5599 RefreshRateType type;
5600 {
5601 std::lock_guard<std::mutex> lock(mActiveConfigLock);
5602 type = mDesiredActiveConfig.type;
5603 }
5604 mRefreshRateOverlay = std::make_unique<RefreshRateOverlay>(*this);
5605 mRefreshRateOverlay->changeRefreshRate(type);
5606 } else if (!n) {
5607 mRefreshRateOverlay.reset();
5608 }
5609 return NO_ERROR;
5610 }
5611 case 1035: {
5612 n = data.readInt32();
5613 mDebugDisplayConfigSetByBackdoor = false;
5614 if (n >= 0) {
5615 const auto displayToken = getInternalDisplayToken();
5616 status_t result = setAllowedDisplayConfigs(displayToken, {n});
5617 if (result != NO_ERROR) {
5618 return result;
5619 }
5620 mDebugDisplayConfigSetByBackdoor = true;
5621 }
5622 return NO_ERROR;
5623 }
5624 }
5625 }
5626 return err;
5627 }
5628
repaintEverything()5629 void SurfaceFlinger::repaintEverything() {
5630 mRepaintEverything = true;
5631 signalTransaction();
5632 }
5633
repaintEverythingForHWC()5634 void SurfaceFlinger::repaintEverythingForHWC() {
5635 mRepaintEverything = true;
5636 mEventQueue->invalidate();
5637 }
5638
5639 // A simple RAII class to disconnect from an ANativeWindow* when it goes out of scope
5640 class WindowDisconnector {
5641 public:
WindowDisconnector(ANativeWindow * window,int api)5642 WindowDisconnector(ANativeWindow* window, int api) : mWindow(window), mApi(api) {}
~WindowDisconnector()5643 ~WindowDisconnector() {
5644 native_window_api_disconnect(mWindow, mApi);
5645 }
5646
5647 private:
5648 ANativeWindow* mWindow;
5649 const int mApi;
5650 };
5651
captureScreen(const sp<IBinder> & displayToken,sp<GraphicBuffer> * outBuffer,bool & outCapturedSecureLayers,const Dataspace reqDataspace,const ui::PixelFormat reqPixelFormat,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,bool useIdentityTransform,ISurfaceComposer::Rotation rotation,bool captureSecureLayers)5652 status_t SurfaceFlinger::captureScreen(const sp<IBinder>& displayToken,
5653 sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers,
5654 const Dataspace reqDataspace,
5655 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
5656 uint32_t reqWidth, uint32_t reqHeight,
5657 bool useIdentityTransform,
5658 ISurfaceComposer::Rotation rotation,
5659 bool captureSecureLayers) {
5660 ATRACE_CALL();
5661
5662 if (!displayToken) return BAD_VALUE;
5663
5664 auto renderAreaRotation = fromSurfaceComposerRotation(rotation);
5665
5666 sp<DisplayDevice> display;
5667 {
5668 Mutex::Autolock _l(mStateLock);
5669
5670 display = getDisplayDeviceLocked(displayToken);
5671 if (!display) return BAD_VALUE;
5672
5673 // set the requested width/height to the logical display viewport size
5674 // by default
5675 if (reqWidth == 0 || reqHeight == 0) {
5676 reqWidth = uint32_t(display->getViewport().width());
5677 reqHeight = uint32_t(display->getViewport().height());
5678 }
5679 }
5680
5681 DisplayRenderArea renderArea(display, sourceCrop, reqWidth, reqHeight, reqDataspace,
5682 renderAreaRotation, captureSecureLayers);
5683
5684 auto traverseLayers = std::bind(&SurfaceFlinger::traverseLayersInDisplay, this, display,
5685 std::placeholders::_1);
5686 return captureScreenCommon(renderArea, traverseLayers, outBuffer, reqPixelFormat,
5687 useIdentityTransform, outCapturedSecureLayers);
5688 }
5689
pickDataspaceFromColorMode(const ColorMode colorMode)5690 static Dataspace pickDataspaceFromColorMode(const ColorMode colorMode) {
5691 switch (colorMode) {
5692 case ColorMode::DISPLAY_P3:
5693 case ColorMode::BT2100_PQ:
5694 case ColorMode::BT2100_HLG:
5695 case ColorMode::DISPLAY_BT2020:
5696 return Dataspace::DISPLAY_P3;
5697 default:
5698 return Dataspace::V0_SRGB;
5699 }
5700 }
5701
getDisplayByIdOrLayerStack(uint64_t displayOrLayerStack)5702 const sp<DisplayDevice> SurfaceFlinger::getDisplayByIdOrLayerStack(uint64_t displayOrLayerStack) {
5703 const sp<IBinder> displayToken = getPhysicalDisplayTokenLocked(DisplayId{displayOrLayerStack});
5704 if (displayToken) {
5705 return getDisplayDeviceLocked(displayToken);
5706 }
5707 // Couldn't find display by displayId. Try to get display by layerStack since virtual displays
5708 // may not have a displayId.
5709 for (const auto& [token, display] : mDisplays) {
5710 if (display->getLayerStack() == displayOrLayerStack) {
5711 return display;
5712 }
5713 }
5714 return nullptr;
5715 }
5716
captureScreen(uint64_t displayOrLayerStack,Dataspace * outDataspace,sp<GraphicBuffer> * outBuffer)5717 status_t SurfaceFlinger::captureScreen(uint64_t displayOrLayerStack, Dataspace* outDataspace,
5718 sp<GraphicBuffer>* outBuffer) {
5719 sp<DisplayDevice> display;
5720 uint32_t width;
5721 uint32_t height;
5722 ui::Transform::orientation_flags captureOrientation;
5723 {
5724 Mutex::Autolock _l(mStateLock);
5725 display = getDisplayByIdOrLayerStack(displayOrLayerStack);
5726 if (!display) {
5727 return BAD_VALUE;
5728 }
5729
5730 width = uint32_t(display->getViewport().width());
5731 height = uint32_t(display->getViewport().height());
5732
5733 captureOrientation = fromSurfaceComposerRotation(
5734 static_cast<ISurfaceComposer::Rotation>(display->getOrientation()));
5735 if (captureOrientation == ui::Transform::orientation_flags::ROT_90) {
5736 captureOrientation = ui::Transform::orientation_flags::ROT_270;
5737 } else if (captureOrientation == ui::Transform::orientation_flags::ROT_270) {
5738 captureOrientation = ui::Transform::orientation_flags::ROT_90;
5739 }
5740 *outDataspace =
5741 pickDataspaceFromColorMode(display->getCompositionDisplay()->getState().colorMode);
5742 }
5743
5744 DisplayRenderArea renderArea(display, Rect(), width, height, *outDataspace, captureOrientation,
5745 false /* captureSecureLayers */);
5746
5747 auto traverseLayers = std::bind(&SurfaceFlinger::traverseLayersInDisplay, this, display,
5748 std::placeholders::_1);
5749 bool ignored = false;
5750 return captureScreenCommon(renderArea, traverseLayers, outBuffer, ui::PixelFormat::RGBA_8888,
5751 false /* useIdentityTransform */,
5752 ignored /* outCapturedSecureLayers */);
5753 }
5754
captureLayers(const sp<IBinder> & layerHandleBinder,sp<GraphicBuffer> * outBuffer,const Dataspace reqDataspace,const ui::PixelFormat reqPixelFormat,const Rect & sourceCrop,const std::unordered_set<sp<IBinder>,ISurfaceComposer::SpHash<IBinder>> & excludeHandles,float frameScale,bool childrenOnly)5755 status_t SurfaceFlinger::captureLayers(
5756 const sp<IBinder>& layerHandleBinder, sp<GraphicBuffer>* outBuffer,
5757 const Dataspace reqDataspace, const ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
5758 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>& excludeHandles,
5759 float frameScale, bool childrenOnly) {
5760 ATRACE_CALL();
5761
5762 class LayerRenderArea : public RenderArea {
5763 public:
5764 LayerRenderArea(SurfaceFlinger* flinger, const sp<Layer>& layer, const Rect crop,
5765 int32_t reqWidth, int32_t reqHeight, Dataspace reqDataSpace,
5766 bool childrenOnly)
5767 : RenderArea(reqWidth, reqHeight, CaptureFill::CLEAR, reqDataSpace),
5768 mLayer(layer),
5769 mCrop(crop),
5770 mNeedsFiltering(false),
5771 mFlinger(flinger),
5772 mChildrenOnly(childrenOnly) {}
5773 const ui::Transform& getTransform() const override { return mTransform; }
5774 Rect getBounds() const override {
5775 const Layer::State& layerState(mLayer->getDrawingState());
5776 return mLayer->getBufferSize(layerState);
5777 }
5778 int getHeight() const override {
5779 return mLayer->getBufferSize(mLayer->getDrawingState()).getHeight();
5780 }
5781 int getWidth() const override {
5782 return mLayer->getBufferSize(mLayer->getDrawingState()).getWidth();
5783 }
5784 bool isSecure() const override { return false; }
5785 bool needsFiltering() const override { return mNeedsFiltering; }
5786 const sp<const DisplayDevice> getDisplayDevice() const override { return nullptr; }
5787 Rect getSourceCrop() const override {
5788 if (mCrop.isEmpty()) {
5789 return getBounds();
5790 } else {
5791 return mCrop;
5792 }
5793 }
5794 class ReparentForDrawing {
5795 public:
5796 const sp<Layer>& oldParent;
5797 const sp<Layer>& newParent;
5798
5799 ReparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent,
5800 const Rect& drawingBounds)
5801 : oldParent(oldParent), newParent(newParent) {
5802 // Compute and cache the bounds for the new parent layer.
5803 newParent->computeBounds(drawingBounds.toFloatRect(), ui::Transform());
5804 oldParent->setChildrenDrawingParent(newParent);
5805 }
5806 ~ReparentForDrawing() { oldParent->setChildrenDrawingParent(oldParent); }
5807 };
5808
5809 void render(std::function<void()> drawLayers) override {
5810 const Rect sourceCrop = getSourceCrop();
5811 // no need to check rotation because there is none
5812 mNeedsFiltering = sourceCrop.width() != getReqWidth() ||
5813 sourceCrop.height() != getReqHeight();
5814
5815 if (!mChildrenOnly) {
5816 mTransform = mLayer->getTransform().inverse();
5817 drawLayers();
5818 } else {
5819 Rect bounds = getBounds();
5820 screenshotParentLayer = mFlinger->getFactory().createContainerLayer(
5821 LayerCreationArgs(mFlinger, nullptr, String8("Screenshot Parent"),
5822 bounds.getWidth(), bounds.getHeight(), 0,
5823 LayerMetadata()));
5824
5825 ReparentForDrawing reparent(mLayer, screenshotParentLayer, sourceCrop);
5826 drawLayers();
5827 }
5828 }
5829
5830 private:
5831 const sp<Layer> mLayer;
5832 const Rect mCrop;
5833
5834 // In the "childrenOnly" case we reparent the children to a screenshot
5835 // layer which has no properties set and which does not draw.
5836 sp<ContainerLayer> screenshotParentLayer;
5837 ui::Transform mTransform;
5838 bool mNeedsFiltering;
5839
5840 SurfaceFlinger* mFlinger;
5841 const bool mChildrenOnly;
5842 };
5843
5844 int reqWidth = 0;
5845 int reqHeight = 0;
5846 sp<Layer> parent;
5847 Rect crop(sourceCrop);
5848 std::unordered_set<sp<Layer>, ISurfaceComposer::SpHash<Layer>> excludeLayers;
5849
5850 {
5851 Mutex::Autolock _l(mStateLock);
5852
5853 parent = fromHandle(layerHandleBinder);
5854 if (parent == nullptr || parent->isRemovedFromCurrentState()) {
5855 ALOGE("captureLayers called with an invalid or removed parent");
5856 return NAME_NOT_FOUND;
5857 }
5858
5859 const int uid = IPCThreadState::self()->getCallingUid();
5860 const bool forSystem = uid == AID_GRAPHICS || uid == AID_SYSTEM;
5861 if (!forSystem && parent->getCurrentState().flags & layer_state_t::eLayerSecure) {
5862 ALOGW("Attempting to capture secure layer: PERMISSION_DENIED");
5863 return PERMISSION_DENIED;
5864 }
5865
5866 if (sourceCrop.width() <= 0) {
5867 crop.left = 0;
5868 crop.right = parent->getBufferSize(parent->getCurrentState()).getWidth();
5869 }
5870
5871 if (sourceCrop.height() <= 0) {
5872 crop.top = 0;
5873 crop.bottom = parent->getBufferSize(parent->getCurrentState()).getHeight();
5874 }
5875 reqWidth = crop.width() * frameScale;
5876 reqHeight = crop.height() * frameScale;
5877
5878 for (const auto& handle : excludeHandles) {
5879 sp<Layer> excludeLayer = fromHandle(handle);
5880 if (excludeLayer != nullptr) {
5881 excludeLayers.emplace(excludeLayer);
5882 } else {
5883 ALOGW("Invalid layer handle passed as excludeLayer to captureLayers");
5884 return NAME_NOT_FOUND;
5885 }
5886 }
5887 } // mStateLock
5888
5889 // really small crop or frameScale
5890 if (reqWidth <= 0) {
5891 reqWidth = 1;
5892 }
5893 if (reqHeight <= 0) {
5894 reqHeight = 1;
5895 }
5896
5897 LayerRenderArea renderArea(this, parent, crop, reqWidth, reqHeight, reqDataspace, childrenOnly);
5898 auto traverseLayers = [parent, childrenOnly,
5899 &excludeLayers](const LayerVector::Visitor& visitor) {
5900 parent->traverseChildrenInZOrder(LayerVector::StateSet::Drawing, [&](Layer* layer) {
5901 if (!layer->isVisible()) {
5902 return;
5903 } else if (childrenOnly && layer == parent.get()) {
5904 return;
5905 }
5906
5907 sp<Layer> p = layer;
5908 while (p != nullptr) {
5909 if (excludeLayers.count(p) != 0) {
5910 return;
5911 }
5912 p = p->getParent();
5913 }
5914
5915 visitor(layer);
5916 });
5917 };
5918
5919 bool outCapturedSecureLayers = false;
5920 return captureScreenCommon(renderArea, traverseLayers, outBuffer, reqPixelFormat, false,
5921 outCapturedSecureLayers);
5922 }
5923
captureScreenCommon(RenderArea & renderArea,TraverseLayersFunction traverseLayers,sp<GraphicBuffer> * outBuffer,const ui::PixelFormat reqPixelFormat,bool useIdentityTransform,bool & outCapturedSecureLayers)5924 status_t SurfaceFlinger::captureScreenCommon(RenderArea& renderArea,
5925 TraverseLayersFunction traverseLayers,
5926 sp<GraphicBuffer>* outBuffer,
5927 const ui::PixelFormat reqPixelFormat,
5928 bool useIdentityTransform,
5929 bool& outCapturedSecureLayers) {
5930 ATRACE_CALL();
5931
5932 // TODO(b/116112787) Make buffer usage a parameter.
5933 const uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
5934 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
5935 *outBuffer =
5936 getFactory().createGraphicBuffer(renderArea.getReqWidth(), renderArea.getReqHeight(),
5937 static_cast<android_pixel_format>(reqPixelFormat), 1,
5938 usage, "screenshot");
5939
5940 return captureScreenCommon(renderArea, traverseLayers, *outBuffer, useIdentityTransform,
5941 outCapturedSecureLayers);
5942 }
5943
captureScreenCommon(RenderArea & renderArea,TraverseLayersFunction traverseLayers,const sp<GraphicBuffer> & buffer,bool useIdentityTransform,bool & outCapturedSecureLayers)5944 status_t SurfaceFlinger::captureScreenCommon(RenderArea& renderArea,
5945 TraverseLayersFunction traverseLayers,
5946 const sp<GraphicBuffer>& buffer,
5947 bool useIdentityTransform,
5948 bool& outCapturedSecureLayers) {
5949 // This mutex protects syncFd and captureResult for communication of the return values from the
5950 // main thread back to this Binder thread
5951 std::mutex captureMutex;
5952 std::condition_variable captureCondition;
5953 std::unique_lock<std::mutex> captureLock(captureMutex);
5954 int syncFd = -1;
5955 std::optional<status_t> captureResult;
5956
5957 const int uid = IPCThreadState::self()->getCallingUid();
5958 const bool forSystem = uid == AID_GRAPHICS || uid == AID_SYSTEM;
5959
5960 sp<LambdaMessage> message = new LambdaMessage([&] {
5961 // If there is a refresh pending, bug out early and tell the binder thread to try again
5962 // after the refresh.
5963 if (mRefreshPending) {
5964 ATRACE_NAME("Skipping screenshot for now");
5965 std::unique_lock<std::mutex> captureLock(captureMutex);
5966 captureResult = std::make_optional<status_t>(EAGAIN);
5967 captureCondition.notify_one();
5968 return;
5969 }
5970
5971 status_t result = NO_ERROR;
5972 int fd = -1;
5973 {
5974 Mutex::Autolock _l(mStateLock);
5975 renderArea.render([&] {
5976 result = captureScreenImplLocked(renderArea, traverseLayers, buffer.get(),
5977 useIdentityTransform, forSystem, &fd,
5978 outCapturedSecureLayers);
5979 });
5980 }
5981
5982 {
5983 std::unique_lock<std::mutex> captureLock(captureMutex);
5984 syncFd = fd;
5985 captureResult = std::make_optional<status_t>(result);
5986 captureCondition.notify_one();
5987 }
5988 });
5989
5990 status_t result = postMessageAsync(message);
5991 if (result == NO_ERROR) {
5992 captureCondition.wait(captureLock, [&] { return captureResult; });
5993 while (*captureResult == EAGAIN) {
5994 captureResult.reset();
5995 result = postMessageAsync(message);
5996 if (result != NO_ERROR) {
5997 return result;
5998 }
5999 captureCondition.wait(captureLock, [&] { return captureResult; });
6000 }
6001 result = *captureResult;
6002 }
6003
6004 if (result == NO_ERROR) {
6005 sync_wait(syncFd, -1);
6006 close(syncFd);
6007 }
6008
6009 return result;
6010 }
6011
renderScreenImplLocked(const RenderArea & renderArea,TraverseLayersFunction traverseLayers,ANativeWindowBuffer * buffer,bool useIdentityTransform,int * outSyncFd)6012 void SurfaceFlinger::renderScreenImplLocked(const RenderArea& renderArea,
6013 TraverseLayersFunction traverseLayers,
6014 ANativeWindowBuffer* buffer, bool useIdentityTransform,
6015 int* outSyncFd) {
6016 ATRACE_CALL();
6017
6018 const auto reqWidth = renderArea.getReqWidth();
6019 const auto reqHeight = renderArea.getReqHeight();
6020 const auto rotation = renderArea.getRotationFlags();
6021 const auto transform = renderArea.getTransform();
6022 const auto sourceCrop = renderArea.getSourceCrop();
6023
6024 renderengine::DisplaySettings clientCompositionDisplay;
6025 std::vector<renderengine::LayerSettings> clientCompositionLayers;
6026
6027 // assume that bounds are never offset, and that they are the same as the
6028 // buffer bounds.
6029 clientCompositionDisplay.physicalDisplay = Rect(reqWidth, reqHeight);
6030 clientCompositionDisplay.clip = sourceCrop;
6031 clientCompositionDisplay.globalTransform = transform.asMatrix4();
6032
6033 // Now take into account the rotation flag. We append a transform that
6034 // rotates the layer stack about the origin, then translate by buffer
6035 // boundaries to be in the right quadrant.
6036 mat4 rotMatrix;
6037 int displacementX = 0;
6038 int displacementY = 0;
6039 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
6040 switch (rotation) {
6041 case ui::Transform::ROT_90:
6042 rotMatrix = mat4::rotate(rot90InRadians, vec3(0, 0, 1));
6043 displacementX = renderArea.getBounds().getHeight();
6044 break;
6045 case ui::Transform::ROT_180:
6046 rotMatrix = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1));
6047 displacementY = renderArea.getBounds().getWidth();
6048 displacementX = renderArea.getBounds().getHeight();
6049 break;
6050 case ui::Transform::ROT_270:
6051 rotMatrix = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1));
6052 displacementY = renderArea.getBounds().getWidth();
6053 break;
6054 default:
6055 break;
6056 }
6057
6058 // We need to transform the clipping window into the right spot.
6059 // First, rotate the clipping rectangle by the rotation hint to get the
6060 // right orientation
6061 const vec4 clipTL = vec4(sourceCrop.left, sourceCrop.top, 0, 1);
6062 const vec4 clipBR = vec4(sourceCrop.right, sourceCrop.bottom, 0, 1);
6063 const vec4 rotClipTL = rotMatrix * clipTL;
6064 const vec4 rotClipBR = rotMatrix * clipBR;
6065 const int newClipLeft = std::min(rotClipTL[0], rotClipBR[0]);
6066 const int newClipTop = std::min(rotClipTL[1], rotClipBR[1]);
6067 const int newClipRight = std::max(rotClipTL[0], rotClipBR[0]);
6068 const int newClipBottom = std::max(rotClipTL[1], rotClipBR[1]);
6069
6070 // Now reposition the clipping rectangle with the displacement vector
6071 // computed above.
6072 const mat4 displacementMat = mat4::translate(vec4(displacementX, displacementY, 0, 1));
6073 clientCompositionDisplay.clip =
6074 Rect(newClipLeft + displacementX, newClipTop + displacementY,
6075 newClipRight + displacementX, newClipBottom + displacementY);
6076
6077 mat4 clipTransform = displacementMat * rotMatrix;
6078 clientCompositionDisplay.globalTransform =
6079 clipTransform * clientCompositionDisplay.globalTransform;
6080
6081 clientCompositionDisplay.outputDataspace = renderArea.getReqDataSpace();
6082 clientCompositionDisplay.maxLuminance = DisplayDevice::sDefaultMaxLumiance;
6083
6084 const float alpha = RenderArea::getCaptureFillValue(renderArea.getCaptureFill());
6085
6086 renderengine::LayerSettings fillLayer;
6087 fillLayer.source.buffer.buffer = nullptr;
6088 fillLayer.source.solidColor = half3(0.0, 0.0, 0.0);
6089 fillLayer.geometry.boundaries = FloatRect(0.0, 0.0, 1.0, 1.0);
6090 fillLayer.alpha = half(alpha);
6091 clientCompositionLayers.push_back(fillLayer);
6092
6093 Region clearRegion = Region::INVALID_REGION;
6094 traverseLayers([&](Layer* layer) {
6095 renderengine::LayerSettings layerSettings;
6096 bool prepared = layer->prepareClientLayer(renderArea, useIdentityTransform, clearRegion,
6097 false, layerSettings);
6098 if (prepared) {
6099 clientCompositionLayers.push_back(layerSettings);
6100 }
6101 });
6102
6103 clientCompositionDisplay.clearRegion = clearRegion;
6104 // Use an empty fence for the buffer fence, since we just created the buffer so
6105 // there is no need for synchronization with the GPU.
6106 base::unique_fd bufferFence;
6107 base::unique_fd drawFence;
6108 getRenderEngine().useProtectedContext(false);
6109 getRenderEngine().drawLayers(clientCompositionDisplay, clientCompositionLayers, buffer,
6110 /*useFramebufferCache=*/false, std::move(bufferFence), &drawFence);
6111
6112 *outSyncFd = drawFence.release();
6113 }
6114
captureScreenImplLocked(const RenderArea & renderArea,TraverseLayersFunction traverseLayers,ANativeWindowBuffer * buffer,bool useIdentityTransform,bool forSystem,int * outSyncFd,bool & outCapturedSecureLayers)6115 status_t SurfaceFlinger::captureScreenImplLocked(const RenderArea& renderArea,
6116 TraverseLayersFunction traverseLayers,
6117 ANativeWindowBuffer* buffer,
6118 bool useIdentityTransform, bool forSystem,
6119 int* outSyncFd, bool& outCapturedSecureLayers) {
6120 ATRACE_CALL();
6121
6122 traverseLayers([&](Layer* layer) {
6123 outCapturedSecureLayers =
6124 outCapturedSecureLayers || (layer->isVisible() && layer->isSecure());
6125 });
6126
6127 // We allow the system server to take screenshots of secure layers for
6128 // use in situations like the Screen-rotation animation and place
6129 // the impetus on WindowManager to not persist them.
6130 if (outCapturedSecureLayers && !forSystem) {
6131 ALOGW("FB is protected: PERMISSION_DENIED");
6132 return PERMISSION_DENIED;
6133 }
6134 renderScreenImplLocked(renderArea, traverseLayers, buffer, useIdentityTransform, outSyncFd);
6135 return NO_ERROR;
6136 }
6137
setInputWindowsFinished()6138 void SurfaceFlinger::setInputWindowsFinished() {
6139 Mutex::Autolock _l(mStateLock);
6140
6141 mPendingSyncInputWindows = false;
6142 mTransactionCV.broadcast();
6143 }
6144
6145 // ---------------------------------------------------------------------------
6146
traverseInZOrder(const LayerVector::Visitor & visitor) const6147 void SurfaceFlinger::State::traverseInZOrder(const LayerVector::Visitor& visitor) const {
6148 layersSortedByZ.traverseInZOrder(stateSet, visitor);
6149 }
6150
traverseInReverseZOrder(const LayerVector::Visitor & visitor) const6151 void SurfaceFlinger::State::traverseInReverseZOrder(const LayerVector::Visitor& visitor) const {
6152 layersSortedByZ.traverseInReverseZOrder(stateSet, visitor);
6153 }
6154
traverseLayersInDisplay(const sp<const DisplayDevice> & display,const LayerVector::Visitor & visitor)6155 void SurfaceFlinger::traverseLayersInDisplay(const sp<const DisplayDevice>& display,
6156 const LayerVector::Visitor& visitor) {
6157 // We loop through the first level of layers without traversing,
6158 // as we need to determine which layers belong to the requested display.
6159 for (const auto& layer : mDrawingState.layersSortedByZ) {
6160 if (!layer->belongsToDisplay(display->getLayerStack(), false)) {
6161 continue;
6162 }
6163 // relative layers are traversed in Layer::traverseInZOrder
6164 layer->traverseInZOrder(LayerVector::StateSet::Drawing, [&](Layer* layer) {
6165 if (!layer->belongsToDisplay(display->getLayerStack(), false)) {
6166 return;
6167 }
6168 if (!layer->isVisible()) {
6169 return;
6170 }
6171 visitor(layer);
6172 });
6173 }
6174 }
6175
setAllowedDisplayConfigsInternal(const sp<DisplayDevice> & display,const std::vector<int32_t> & allowedConfigs)6176 void SurfaceFlinger::setAllowedDisplayConfigsInternal(const sp<DisplayDevice>& display,
6177 const std::vector<int32_t>& allowedConfigs) {
6178 if (!display->isPrimary()) {
6179 return;
6180 }
6181
6182 const auto allowedDisplayConfigs = DisplayConfigs(allowedConfigs.begin(),
6183 allowedConfigs.end());
6184 if (allowedDisplayConfigs == mAllowedDisplayConfigs) {
6185 return;
6186 }
6187
6188 ALOGV("Updating allowed configs");
6189 mAllowedDisplayConfigs = std::move(allowedDisplayConfigs);
6190
6191 // TODO(b/140204874): This hack triggers a notification that something has changed, so
6192 // that listeners that care about a change in allowed configs can get the notification.
6193 // Giving current ActiveConfig so that most other listeners would just drop the event
6194 mScheduler->onConfigChanged(mAppConnectionHandle, display->getId()->value,
6195 display->getActiveConfig());
6196
6197 if (mRefreshRateConfigs->refreshRateSwitchingSupported()) {
6198 // Set the highest allowed config by iterating backwards on available refresh rates
6199 const auto& refreshRates = mRefreshRateConfigs->getRefreshRateMap();
6200 for (auto iter = refreshRates.crbegin(); iter != refreshRates.crend(); ++iter) {
6201 if (isDisplayConfigAllowed(iter->second.configId)) {
6202 ALOGV("switching to allowed config %d", iter->second.configId);
6203 setDesiredActiveConfig(
6204 {iter->first, iter->second.configId, Scheduler::ConfigEvent::Changed});
6205 break;
6206 }
6207 }
6208 } else if (!isDisplayConfigAllowed(display->getActiveConfig())) {
6209 ALOGV("switching to config %d", allowedConfigs[0]);
6210 setDesiredActiveConfig(
6211 {RefreshRateType::DEFAULT, allowedConfigs[0], Scheduler::ConfigEvent::Changed});
6212 }
6213 }
6214
setAllowedDisplayConfigs(const sp<IBinder> & displayToken,const std::vector<int32_t> & allowedConfigs)6215 status_t SurfaceFlinger::setAllowedDisplayConfigs(const sp<IBinder>& displayToken,
6216 const std::vector<int32_t>& allowedConfigs) {
6217 ATRACE_CALL();
6218
6219 if (!displayToken || allowedConfigs.empty()) {
6220 return BAD_VALUE;
6221 }
6222
6223 if (mDebugDisplayConfigSetByBackdoor) {
6224 // ignore this request as config is overridden by backdoor
6225 return NO_ERROR;
6226 }
6227
6228 postMessageSync(new LambdaMessage([&]() {
6229 const auto display = getDisplayDeviceLocked(displayToken);
6230 if (!display) {
6231 ALOGE("Attempt to set allowed display configs for invalid display token %p",
6232 displayToken.get());
6233 } else if (display->isVirtual()) {
6234 ALOGW("Attempt to set allowed display configs for virtual display");
6235 } else {
6236 Mutex::Autolock lock(mStateLock);
6237 setAllowedDisplayConfigsInternal(display, allowedConfigs);
6238 }
6239 }));
6240
6241 return NO_ERROR;
6242 }
6243
getAllowedDisplayConfigs(const sp<IBinder> & displayToken,std::vector<int32_t> * outAllowedConfigs)6244 status_t SurfaceFlinger::getAllowedDisplayConfigs(const sp<IBinder>& displayToken,
6245 std::vector<int32_t>* outAllowedConfigs) {
6246 ATRACE_CALL();
6247
6248 if (!displayToken || !outAllowedConfigs) {
6249 return BAD_VALUE;
6250 }
6251
6252 Mutex::Autolock lock(mStateLock);
6253
6254 const auto display = getDisplayDeviceLocked(displayToken);
6255 if (!display) {
6256 return NAME_NOT_FOUND;
6257 }
6258
6259 if (display->isPrimary()) {
6260 outAllowedConfigs->assign(mAllowedDisplayConfigs.begin(), mAllowedDisplayConfigs.end());
6261 }
6262
6263 return NO_ERROR;
6264 }
6265
onSetInputWindowsFinished()6266 void SurfaceFlinger::SetInputWindowsListener::onSetInputWindowsFinished() {
6267 mFlinger->setInputWindowsFinished();
6268 }
6269
fromHandle(const sp<IBinder> & handle)6270 sp<Layer> SurfaceFlinger::fromHandle(const sp<IBinder>& handle) {
6271 BBinder *b = handle->localBinder();
6272 if (b == nullptr) {
6273 return nullptr;
6274 }
6275 auto it = mLayersByLocalBinderToken.find(b);
6276 if (it != mLayersByLocalBinderToken.end()) {
6277 return it->second.promote();
6278 }
6279 return nullptr;
6280 }
6281
bufferErased(const client_cache_t & clientCacheId)6282 void SurfaceFlinger::bufferErased(const client_cache_t& clientCacheId) {
6283 getRenderEngine().unbindExternalTextureBuffer(clientCacheId.id);
6284 }
6285
6286 } // namespace android
6287
6288 #if defined(__gl_h_)
6289 #error "don't include gl/gl.h in this file"
6290 #endif
6291
6292 #if defined(__gl2_h_)
6293 #error "don't include gl2/gl2.h in this file"
6294 #endif
6295