• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 #pragma once
18 
19 #include <android/gui/DisplayBrightness.h>
20 #include <android/gui/IDisplayEventConnection.h>
21 #include <android/gui/IFpsListener.h>
22 #include <android/gui/IHdrLayerInfoListener.h>
23 #include <android/gui/IRegionSamplingListener.h>
24 #include <android/gui/IScreenCaptureListener.h>
25 #include <android/gui/ITransactionTraceListener.h>
26 #include <android/gui/ITunnelModeEnabledListener.h>
27 #include <android/gui/IWindowInfosListener.h>
28 #include <binder/IBinder.h>
29 #include <binder/IInterface.h>
30 #include <ftl/flags.h>
31 #include <gui/FrameTimelineInfo.h>
32 #include <gui/ITransactionCompletedListener.h>
33 #include <gui/SpHash.h>
34 #include <math/vec4.h>
35 #include <stdint.h>
36 #include <sys/types.h>
37 #include <ui/ConfigStoreTypes.h>
38 #include <ui/DisplayId.h>
39 #include <ui/DisplayMode.h>
40 #include <ui/DisplayedFrameStats.h>
41 #include <ui/FrameStats.h>
42 #include <ui/GraphicBuffer.h>
43 #include <ui/GraphicTypes.h>
44 #include <ui/PixelFormat.h>
45 #include <ui/Rotation.h>
46 #include <utils/Errors.h>
47 #include <utils/RefBase.h>
48 #include <utils/Timers.h>
49 #include <utils/Vector.h>
50 
51 #include <optional>
52 #include <unordered_set>
53 #include <vector>
54 
55 #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
56 
57 namespace android {
58 
59 struct client_cache_t;
60 struct ComposerState;
61 struct DisplayStatInfo;
62 struct DisplayState;
63 struct InputWindowCommands;
64 class LayerDebugInfo;
65 class HdrCapabilities;
66 class IGraphicBufferProducer;
67 class ISurfaceComposerClient;
68 class Rect;
69 enum class FrameEvent;
70 
71 using gui::IDisplayEventConnection;
72 using gui::IRegionSamplingListener;
73 using gui::IScreenCaptureListener;
74 using gui::SpHash;
75 
76 namespace gui {
77 
78 struct DisplayCaptureArgs;
79 struct LayerCaptureArgs;
80 
81 } // namespace gui
82 
83 namespace ui {
84 
85 struct DisplayMode;
86 struct DisplayState;
87 struct DynamicDisplayInfo;
88 struct StaticDisplayInfo;
89 
90 } // namespace ui
91 
92 /*
93  * This class defines the Binder IPC interface for accessing various
94  * SurfaceFlinger features.
95  */
96 class ISurfaceComposer: public IInterface {
97 public:
98     DECLARE_META_INTERFACE(SurfaceComposer)
99 
100     static constexpr size_t MAX_LAYERS = 4096;
101 
102     // flags for setTransactionState()
103     enum {
104         eSynchronous = 0x01,
105         eAnimation = 0x02,
106 
107         // Explicit indication that this transaction and others to follow will likely result in a
108         // lot of layers being composed, and thus, SurfaceFlinger should wake-up earlier to avoid
109         // missing frame deadlines. In this case SurfaceFlinger will wake up at
110         // (sf vsync offset - debug.sf.early_phase_offset_ns). SurfaceFlinger will continue to be
111         // in the early configuration until it receives eEarlyWakeupEnd. These flags are
112         // expected to be used by WindowManager only and are guarded by
113         // android.permission.ACCESS_SURFACE_FLINGER
114         eEarlyWakeupStart = 0x08,
115         eEarlyWakeupEnd = 0x10,
116         eOneWay = 0x20
117     };
118 
119     enum VsyncSource {
120         eVsyncSourceApp = 0,
121         eVsyncSourceSurfaceFlinger = 1
122     };
123 
124     enum class EventRegistration {
125         modeChanged = 1 << 0,
126         frameRateOverride = 1 << 1,
127     };
128 
129     using EventRegistrationFlags = ftl::Flags<EventRegistration>;
130 
131     /*
132      * Create a connection with SurfaceFlinger.
133      */
134     virtual sp<ISurfaceComposerClient> createConnection() = 0;
135 
136     /* return an IDisplayEventConnection */
137     virtual sp<IDisplayEventConnection> createDisplayEventConnection(
138             VsyncSource vsyncSource = eVsyncSourceApp,
139             EventRegistrationFlags eventRegistration = {}) = 0;
140 
141     /* open/close transactions. requires ACCESS_SURFACE_FLINGER permission */
142     virtual status_t setTransactionState(
143             const FrameTimelineInfo& frameTimelineInfo, const Vector<ComposerState>& state,
144             const Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken,
145             const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime,
146             bool isAutoTimestamp, const client_cache_t& uncacheBuffer, bool hasListenerCallbacks,
147             const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId) = 0;
148 
149     /* signal that we're done booting.
150      * Requires ACCESS_SURFACE_FLINGER permission
151      */
152     virtual void bootFinished() = 0;
153 
154     /* verify that an IGraphicBufferProducer was created by SurfaceFlinger.
155      */
156     virtual bool authenticateSurfaceTexture(
157             const sp<IGraphicBufferProducer>& surface) const = 0;
158 
159     /* Returns the frame timestamps supported by SurfaceFlinger.
160      */
161     virtual status_t getSupportedFrameTimestamps(
162             std::vector<FrameEvent>* outSupported) const = 0;
163 
164     /**
165      * Gets immutable information about given physical display.
166      */
167     virtual status_t getStaticDisplayInfo(const sp<IBinder>& display, ui::StaticDisplayInfo*) = 0;
168 
169     /**
170      * Gets dynamic information about given physical display.
171      */
172     virtual status_t getDynamicDisplayInfo(const sp<IBinder>& display, ui::DynamicDisplayInfo*) = 0;
173 
174     virtual status_t getDisplayNativePrimaries(const sp<IBinder>& display,
175             ui::DisplayPrimaries& primaries) = 0;
176     virtual status_t setActiveColorMode(const sp<IBinder>& display,
177             ui::ColorMode colorMode) = 0;
178 
179     /**
180      * Sets the user-preferred display mode that a device should boot in.
181      */
182     virtual status_t setBootDisplayMode(const sp<IBinder>& display, ui::DisplayModeId) = 0;
183 
184     /* Clears the frame statistics for animations.
185      *
186      * Requires the ACCESS_SURFACE_FLINGER permission.
187      */
188     virtual status_t clearAnimationFrameStats() = 0;
189 
190     /* Gets the frame statistics for animations.
191      *
192      * Requires the ACCESS_SURFACE_FLINGER permission.
193      */
194     virtual status_t getAnimationFrameStats(FrameStats* outStats) const = 0;
195 
196     /* Overrides the supported HDR modes for the given display device.
197      *
198      * Requires the ACCESS_SURFACE_FLINGER permission.
199      */
200     virtual status_t overrideHdrTypes(const sp<IBinder>& display,
201                                       const std::vector<ui::Hdr>& hdrTypes) = 0;
202 
203     /* Pulls surfaceflinger atoms global stats and layer stats to pipe to statsd.
204      *
205      * Requires the calling uid be from system server.
206      */
207     virtual status_t onPullAtom(const int32_t atomId, std::string* outData, bool* success) = 0;
208 
209     virtual status_t enableVSyncInjections(bool enable) = 0;
210 
211     virtual status_t injectVSync(nsecs_t when) = 0;
212 
213     /* Gets the list of active layers in Z order for debugging purposes
214      *
215      * Requires the ACCESS_SURFACE_FLINGER permission.
216      */
217     virtual status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* outLayers) = 0;
218 
219     virtual status_t getColorManagement(bool* outGetColorManagement) const = 0;
220 
221     /* Gets the composition preference of the default data space and default pixel format,
222      * as well as the wide color gamut data space and wide color gamut pixel format.
223      * If the wide color gamut data space is V0_SRGB, then it implies that the platform
224      * has no wide color gamut support.
225      *
226      * Requires the ACCESS_SURFACE_FLINGER permission.
227      */
228     virtual status_t getCompositionPreference(ui::Dataspace* defaultDataspace,
229                                               ui::PixelFormat* defaultPixelFormat,
230                                               ui::Dataspace* wideColorGamutDataspace,
231                                               ui::PixelFormat* wideColorGamutPixelFormat) const = 0;
232     /*
233      * Requires the ACCESS_SURFACE_FLINGER permission.
234      */
235     virtual status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
236                                                            ui::PixelFormat* outFormat,
237                                                            ui::Dataspace* outDataspace,
238                                                            uint8_t* outComponentMask) const = 0;
239 
240     /* Turns on the color sampling engine on the display.
241      *
242      * Requires the ACCESS_SURFACE_FLINGER permission.
243      */
244     virtual status_t setDisplayContentSamplingEnabled(const sp<IBinder>& display, bool enable,
245                                                       uint8_t componentMask,
246                                                       uint64_t maxFrames) = 0;
247 
248     /* Returns statistics on the color profile of the last frame displayed for a given display
249      *
250      * Requires the ACCESS_SURFACE_FLINGER permission.
251      */
252     virtual status_t getDisplayedContentSample(const sp<IBinder>& display, uint64_t maxFrames,
253                                                uint64_t timestamp,
254                                                DisplayedFrameStats* outStats) const = 0;
255 
256     /*
257      * Gets whether SurfaceFlinger can support protected content in GPU composition.
258      * Requires the ACCESS_SURFACE_FLINGER permission.
259      */
260     virtual status_t getProtectedContentSupport(bool* outSupported) const = 0;
261 
262     /* Registers a listener to stream median luma updates from SurfaceFlinger.
263      *
264      * The sampling area is bounded by both samplingArea and the given stopLayerHandle
265      * (i.e., only layers behind the stop layer will be captured and sampled).
266      *
267      * Multiple listeners may be provided so long as they have independent listeners.
268      * If multiple listeners are provided, the effective sampling region for each listener will
269      * be bounded by whichever stop layer has a lower Z value.
270      *
271      * Requires the same permissions as captureLayers and captureScreen.
272      */
273     virtual status_t addRegionSamplingListener(const Rect& samplingArea,
274                                                const sp<IBinder>& stopLayerHandle,
275                                                const sp<IRegionSamplingListener>& listener) = 0;
276 
277     /*
278      * Removes a listener that was streaming median luma updates from SurfaceFlinger.
279      */
280     virtual status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) = 0;
281 
282     /* Registers a listener that streams fps updates from SurfaceFlinger.
283      *
284      * The listener will stream fps updates for the layer tree rooted at the layer denoted by the
285      * task ID, i.e., the layer must have the task ID as part of its layer metadata with key
286      * METADATA_TASK_ID. If there is no such layer, then no fps is expected to be reported.
287      *
288      * Multiple listeners may be supported.
289      *
290      * Requires the READ_FRAME_BUFFER permission.
291      */
292     virtual status_t addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener) = 0;
293     /*
294      * Removes a listener that was streaming fps updates from SurfaceFlinger.
295      */
296     virtual status_t removeFpsListener(const sp<gui::IFpsListener>& listener) = 0;
297 
298     /* Registers a listener to receive tunnel mode enabled updates from SurfaceFlinger.
299      *
300      * Requires ACCESS_SURFACE_FLINGER permission.
301      */
302     virtual status_t addTunnelModeEnabledListener(
303             const sp<gui::ITunnelModeEnabledListener>& listener) = 0;
304 
305     /*
306      * Removes a listener that was receiving tunnel mode enabled updates from SurfaceFlinger.
307      *
308      * Requires ACCESS_SURFACE_FLINGER permission.
309      */
310     virtual status_t removeTunnelModeEnabledListener(
311             const sp<gui::ITunnelModeEnabledListener>& listener) = 0;
312 
313     /* Sets the refresh rate boundaries for the display.
314      *
315      * The primary refresh rate range represents display manager's general guidance on the display
316      * modes we'll consider when switching refresh rates. Unless we get an explicit signal from an
317      * app, we should stay within this range.
318      *
319      * The app request refresh rate range allows us to consider more display modes when switching
320      * refresh rates. Although we should generally stay within the primary range, specific
321      * considerations, such as layer frame rate settings specified via the setFrameRate() api, may
322      * cause us to go outside the primary range. We never go outside the app request range. The app
323      * request range will be greater than or equal to the primary refresh rate range, never smaller.
324      *
325      * defaultMode is used to narrow the list of display modes SurfaceFlinger will consider
326      * switching between. Only modes with a mode group and resolution matching defaultMode
327      * will be considered for switching. The defaultMode corresponds to an ID of mode in the list
328      * of supported modes returned from getDynamicDisplayInfo().
329      */
330     virtual status_t setDesiredDisplayModeSpecs(
331             const sp<IBinder>& displayToken, ui::DisplayModeId defaultMode,
332             bool allowGroupSwitching, float primaryRefreshRateMin, float primaryRefreshRateMax,
333             float appRequestRefreshRateMin, float appRequestRefreshRateMax) = 0;
334 
335     virtual status_t getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
336                                                 ui::DisplayModeId* outDefaultMode,
337                                                 bool* outAllowGroupSwitching,
338                                                 float* outPrimaryRefreshRateMin,
339                                                 float* outPrimaryRefreshRateMax,
340                                                 float* outAppRequestRefreshRateMin,
341                                                 float* outAppRequestRefreshRateMax) = 0;
342 
343     /*
344      * Sets the global configuration for all the shadows drawn by SurfaceFlinger. Shadow follows
345      * material design guidelines.
346      *
347      * ambientColor
348      *      Color to the ambient shadow. The alpha is premultiplied.
349      *
350      * spotColor
351      *      Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow
352      *      depends on the light position.
353      *
354      * lightPosY/lightPosZ
355      *      Position of the light used to cast the spot shadow. The X value is always the display
356      *      width / 2.
357      *
358      * lightRadius
359      *      Radius of the light casting the shadow.
360      */
361     virtual status_t setGlobalShadowSettings(const half4& ambientColor, const half4& spotColor,
362                                              float lightPosY, float lightPosZ,
363                                              float lightRadius) = 0;
364 
365     /*
366      * Gets whether a display supports DISPLAY_DECORATION layers.
367      *
368      * displayToken
369      *      The token of the display.
370      * outSupport
371      *      An output parameter for whether/how the display supports
372      *      DISPLAY_DECORATION layers.
373      *
374      * Returns NO_ERROR upon success. Otherwise,
375      *      NAME_NOT_FOUND if the display is invalid, or
376      *      BAD_VALUE      if the output parameter is invalid.
377      */
378     virtual status_t getDisplayDecorationSupport(
379             const sp<IBinder>& displayToken,
380             std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
381                     outSupport) const = 0;
382 
383     /*
384      * Sets the intended frame rate for a surface. See ANativeWindow_setFrameRate() for more info.
385      */
386     virtual status_t setFrameRate(const sp<IGraphicBufferProducer>& surface, float frameRate,
387                                   int8_t compatibility, int8_t changeFrameRateStrategy) = 0;
388 
389     /*
390      * Set the override frame rate for a specified uid by GameManagerService.
391      * Passing the frame rate and uid to SurfaceFlinger to update the override mapping
392      * in the scheduler.
393      */
394     virtual status_t setOverrideFrameRate(uid_t uid, float frameRate) = 0;
395 
396     /*
397      * Sets the frame timeline vsync info received from choreographer that corresponds to next
398      * buffer submitted on that surface.
399      */
400     virtual status_t setFrameTimelineInfo(const sp<IGraphicBufferProducer>& surface,
401                                           const FrameTimelineInfo& frameTimelineInfo) = 0;
402 
403     /*
404      * Adds a TransactionTraceListener to listen for transaction tracing state updates.
405      */
406     virtual status_t addTransactionTraceListener(
407             const sp<gui::ITransactionTraceListener>& listener) = 0;
408 
409     /**
410      * Gets priority of the RenderEngine in SurfaceFlinger.
411      */
412     virtual int getGPUContextPriority() = 0;
413 
414     /**
415      * Gets the number of buffers SurfaceFlinger would need acquire. This number
416      * would be propagated to the client via MIN_UNDEQUEUED_BUFFERS so that the
417      * client could allocate enough buffers to match SF expectations of the
418      * pipeline depth. SurfaceFlinger will make sure that it will give the app at
419      * least the time configured as the 'appDuration' before trying to latch
420      * the buffer.
421      *
422      * The total buffers needed for a given configuration is basically the
423      * numbers of vsyncs a single buffer is used across the stack. For the default
424      * configuration a buffer is held ~1 vsync by the app, ~1 vsync by SurfaceFlinger
425      * and 1 vsync by the display. The extra buffers are calculated as the
426      * number of additional buffers on top of the 2 buffers already present
427      * in MIN_UNDEQUEUED_BUFFERS.
428      */
429     virtual status_t getMaxAcquiredBufferCount(int* buffers) const = 0;
430 
431     virtual status_t addWindowInfosListener(
432             const sp<gui::IWindowInfosListener>& windowInfosListener) const = 0;
433     virtual status_t removeWindowInfosListener(
434             const sp<gui::IWindowInfosListener>& windowInfosListener) const = 0;
435 };
436 
437 // ----------------------------------------------------------------------------
438 
439 class BnSurfaceComposer: public BnInterface<ISurfaceComposer> {
440 public:
441     enum ISurfaceComposerTag {
442         // Note: BOOT_FINISHED must remain this value, it is called from
443         // Java by ActivityManagerService.
444         BOOT_FINISHED = IBinder::FIRST_CALL_TRANSACTION,
445         CREATE_CONNECTION,
446         GET_STATIC_DISPLAY_INFO,
447         CREATE_DISPLAY_EVENT_CONNECTION,
448         CREATE_DISPLAY,             // Deprecated. Autogenerated by .aidl now.
449         DESTROY_DISPLAY,            // Deprecated. Autogenerated by .aidl now.
450         GET_PHYSICAL_DISPLAY_TOKEN, // Deprecated. Autogenerated by .aidl now.
451         SET_TRANSACTION_STATE,
452         AUTHENTICATE_SURFACE,
453         GET_SUPPORTED_FRAME_TIMESTAMPS,
454         GET_DISPLAY_MODES,       // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
455         GET_ACTIVE_DISPLAY_MODE, // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
456         GET_DISPLAY_STATE,
457         CAPTURE_DISPLAY, // Deprecated. Autogenerated by .aidl now.
458         CAPTURE_LAYERS,  // Deprecated. Autogenerated by .aidl now.
459         CLEAR_ANIMATION_FRAME_STATS,
460         GET_ANIMATION_FRAME_STATS,
461         SET_POWER_MODE, // Deprecated. Autogenerated by .aidl now.
462         GET_DISPLAY_STATS,
463         GET_HDR_CAPABILITIES,    // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
464         GET_DISPLAY_COLOR_MODES, // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
465         GET_ACTIVE_COLOR_MODE,   // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
466         SET_ACTIVE_COLOR_MODE,
467         ENABLE_VSYNC_INJECTIONS,
468         INJECT_VSYNC,
469         GET_LAYER_DEBUG_INFO,
470         GET_COMPOSITION_PREFERENCE,
471         GET_COLOR_MANAGEMENT,
472         GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES,
473         SET_DISPLAY_CONTENT_SAMPLING_ENABLED,
474         GET_DISPLAYED_CONTENT_SAMPLE,
475         GET_PROTECTED_CONTENT_SUPPORT,
476         IS_WIDE_COLOR_DISPLAY, // Deprecated. Autogenerated by .aidl now.
477         GET_DISPLAY_NATIVE_PRIMARIES,
478         GET_PHYSICAL_DISPLAY_IDS, // Deprecated. Autogenerated by .aidl now.
479         ADD_REGION_SAMPLING_LISTENER,
480         REMOVE_REGION_SAMPLING_LISTENER,
481         SET_DESIRED_DISPLAY_MODE_SPECS,
482         GET_DESIRED_DISPLAY_MODE_SPECS,
483         GET_DISPLAY_BRIGHTNESS_SUPPORT, // Deprecated. Autogenerated by .aidl now.
484         SET_DISPLAY_BRIGHTNESS,         // Deprecated. Autogenerated by .aidl now.
485         CAPTURE_DISPLAY_BY_ID,          // Deprecated. Autogenerated by .aidl now.
486         NOTIFY_POWER_BOOST,             // Deprecated. Autogenerated by .aidl now.
487         SET_GLOBAL_SHADOW_SETTINGS,
488         GET_AUTO_LOW_LATENCY_MODE_SUPPORT, // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
489         SET_AUTO_LOW_LATENCY_MODE,         // Deprecated. Autogenerated by .aidl now.
490         GET_GAME_CONTENT_TYPE_SUPPORT,     // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
491         SET_GAME_CONTENT_TYPE,             // Deprecated. Use GET_DYNAMIC_DISPLAY_INFO instead.
492         SET_FRAME_RATE,
493         // Deprecated. Use DisplayManager.setShouldAlwaysRespectAppRequestedMode(true);
494         ACQUIRE_FRAME_RATE_FLEXIBILITY_TOKEN,
495         SET_FRAME_TIMELINE_INFO,
496         ADD_TRANSACTION_TRACE_LISTENER,
497         GET_GPU_CONTEXT_PRIORITY,
498         GET_MAX_ACQUIRED_BUFFER_COUNT,
499         GET_DYNAMIC_DISPLAY_INFO,
500         ADD_FPS_LISTENER,
501         REMOVE_FPS_LISTENER,
502         OVERRIDE_HDR_TYPES,
503         ADD_HDR_LAYER_INFO_LISTENER,    // Deprecated. Autogenerated by .aidl now.
504         REMOVE_HDR_LAYER_INFO_LISTENER, // Deprecated. Autogenerated by .aidl now.
505         ON_PULL_ATOM,
506         ADD_TUNNEL_MODE_ENABLED_LISTENER,
507         REMOVE_TUNNEL_MODE_ENABLED_LISTENER,
508         ADD_WINDOW_INFOS_LISTENER,
509         REMOVE_WINDOW_INFOS_LISTENER,
510         GET_PRIMARY_PHYSICAL_DISPLAY_ID, // Deprecated. Autogenerated by .aidl now.
511         GET_DISPLAY_DECORATION_SUPPORT,
512         GET_BOOT_DISPLAY_MODE_SUPPORT, // Deprecated. Autogenerated by .aidl now.
513         SET_BOOT_DISPLAY_MODE,
514         CLEAR_BOOT_DISPLAY_MODE, // Deprecated. Autogenerated by .aidl now.
515         SET_OVERRIDE_FRAME_RATE,
516         // Always append new enum to the end.
517     };
518 
519     virtual status_t onTransact(uint32_t code, const Parcel& data,
520             Parcel* reply, uint32_t flags = 0);
521 };
522 
523 } // namespace android
524