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 // tag as surfaceflinger
18 #define LOG_TAG "SurfaceFlinger"
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <binder/Parcel.h>
24 #include <binder/IPCThreadState.h>
25 #include <binder/IServiceManager.h>
26 
27 #include <gui/IDisplayEventConnection.h>
28 #include <gui/IGraphicBufferProducer.h>
29 #include <gui/IRegionSamplingListener.h>
30 #include <gui/ISurfaceComposer.h>
31 #include <gui/ISurfaceComposerClient.h>
32 #include <gui/LayerDebugInfo.h>
33 #include <gui/LayerState.h>
34 
35 #include <system/graphics.h>
36 
37 #include <ui/DisplayInfo.h>
38 #include <ui/DisplayStatInfo.h>
39 #include <ui/HdrCapabilities.h>
40 
41 #include <utils/Log.h>
42 
43 // ---------------------------------------------------------------------------
44 
45 namespace android {
46 
47 using ui::ColorMode;
48 
49 class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
50 {
51 public:
BpSurfaceComposer(const sp<IBinder> & impl)52     explicit BpSurfaceComposer(const sp<IBinder>& impl)
53         : BpInterface<ISurfaceComposer>(impl)
54     {
55     }
56 
57     virtual ~BpSurfaceComposer();
58 
createConnection()59     virtual sp<ISurfaceComposerClient> createConnection()
60     {
61         Parcel data, reply;
62         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
63         remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
64         return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
65     }
66 
setTransactionState(const Vector<ComposerState> & state,const Vector<DisplayState> & displays,uint32_t flags,const sp<IBinder> & applyToken,const InputWindowCommands & commands,int64_t desiredPresentTime,const client_cache_t & uncacheBuffer,const std::vector<ListenerCallbacks> & listenerCallbacks)67     virtual void setTransactionState(const Vector<ComposerState>& state,
68                                      const Vector<DisplayState>& displays, uint32_t flags,
69                                      const sp<IBinder>& applyToken,
70                                      const InputWindowCommands& commands,
71                                      int64_t desiredPresentTime,
72                                      const client_cache_t& uncacheBuffer,
73                                      const std::vector<ListenerCallbacks>& listenerCallbacks) {
74         Parcel data, reply;
75         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
76 
77         data.writeUint32(static_cast<uint32_t>(state.size()));
78         for (const auto& s : state) {
79             s.write(data);
80         }
81 
82         data.writeUint32(static_cast<uint32_t>(displays.size()));
83         for (const auto& d : displays) {
84             d.write(data);
85         }
86 
87         data.writeUint32(flags);
88         data.writeStrongBinder(applyToken);
89         commands.write(data);
90         data.writeInt64(desiredPresentTime);
91         data.writeWeakBinder(uncacheBuffer.token);
92         data.writeUint64(uncacheBuffer.id);
93 
94         if (data.writeVectorSize(listenerCallbacks) == NO_ERROR) {
95             for (const auto& [listener, callbackIds] : listenerCallbacks) {
96                 data.writeStrongBinder(IInterface::asBinder(listener));
97                 data.writeInt64Vector(callbackIds);
98             }
99         }
100 
101         remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
102     }
103 
bootFinished()104     virtual void bootFinished()
105     {
106         Parcel data, reply;
107         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
108         remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
109     }
110 
captureScreen(const sp<IBinder> & display,sp<GraphicBuffer> * outBuffer,bool & outCapturedSecureLayers,const ui::Dataspace reqDataspace,const ui::PixelFormat reqPixelFormat,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,bool useIdentityTransform,ISurfaceComposer::Rotation rotation,bool captureSecureLayers)111     virtual status_t captureScreen(const sp<IBinder>& display, sp<GraphicBuffer>* outBuffer,
112                                    bool& outCapturedSecureLayers, const ui::Dataspace reqDataspace,
113                                    const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
114                                    uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
115                                    ISurfaceComposer::Rotation rotation, bool captureSecureLayers) {
116         Parcel data, reply;
117         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
118         data.writeStrongBinder(display);
119         data.writeInt32(static_cast<int32_t>(reqDataspace));
120         data.writeInt32(static_cast<int32_t>(reqPixelFormat));
121         data.write(sourceCrop);
122         data.writeUint32(reqWidth);
123         data.writeUint32(reqHeight);
124         data.writeInt32(static_cast<int32_t>(useIdentityTransform));
125         data.writeInt32(static_cast<int32_t>(rotation));
126         data.writeInt32(static_cast<int32_t>(captureSecureLayers));
127         status_t result = remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
128         if (result != NO_ERROR) {
129             ALOGE("captureScreen failed to transact: %d", result);
130             return result;
131         }
132         result = reply.readInt32();
133         if (result != NO_ERROR) {
134             ALOGE("captureScreen failed to readInt32: %d", result);
135             return result;
136         }
137 
138         *outBuffer = new GraphicBuffer();
139         reply.read(**outBuffer);
140         outCapturedSecureLayers = reply.readBool();
141 
142         return result;
143     }
144 
captureScreen(uint64_t displayOrLayerStack,ui::Dataspace * outDataspace,sp<GraphicBuffer> * outBuffer)145     virtual status_t captureScreen(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace,
146                                    sp<GraphicBuffer>* outBuffer) {
147         Parcel data, reply;
148         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
149         data.writeUint64(displayOrLayerStack);
150         status_t result = remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN_BY_ID, data, &reply);
151         if (result != NO_ERROR) {
152             ALOGE("captureScreen failed to transact: %d", result);
153             return result;
154         }
155         result = reply.readInt32();
156         if (result != NO_ERROR) {
157             ALOGE("captureScreen failed to readInt32: %d", result);
158             return result;
159         }
160 
161         *outDataspace = static_cast<ui::Dataspace>(reply.readInt32());
162         *outBuffer = new GraphicBuffer();
163         reply.read(**outBuffer);
164         return result;
165     }
166 
captureLayers(const sp<IBinder> & layerHandleBinder,sp<GraphicBuffer> * outBuffer,const ui::Dataspace reqDataspace,const ui::PixelFormat reqPixelFormat,const Rect & sourceCrop,const std::unordered_set<sp<IBinder>,SpHash<IBinder>> & excludeLayers,float frameScale,bool childrenOnly)167     virtual status_t captureLayers(
168             const sp<IBinder>& layerHandleBinder, sp<GraphicBuffer>* outBuffer,
169             const ui::Dataspace reqDataspace, const ui::PixelFormat reqPixelFormat,
170             const Rect& sourceCrop,
171             const std::unordered_set<sp<IBinder>, SpHash<IBinder>>& excludeLayers, float frameScale,
172             bool childrenOnly) {
173         Parcel data, reply;
174         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
175         data.writeStrongBinder(layerHandleBinder);
176         data.writeInt32(static_cast<int32_t>(reqDataspace));
177         data.writeInt32(static_cast<int32_t>(reqPixelFormat));
178         data.write(sourceCrop);
179         data.writeInt32(excludeLayers.size());
180         for (auto el : excludeLayers) {
181             data.writeStrongBinder(el);
182         }
183         data.writeFloat(frameScale);
184         data.writeBool(childrenOnly);
185         status_t result = remote()->transact(BnSurfaceComposer::CAPTURE_LAYERS, data, &reply);
186         if (result != NO_ERROR) {
187             ALOGE("captureLayers failed to transact: %d", result);
188             return result;
189         }
190         result = reply.readInt32();
191         if (result != NO_ERROR) {
192             ALOGE("captureLayers failed to readInt32: %d", result);
193             return result;
194         }
195 
196         *outBuffer = new GraphicBuffer();
197         reply.read(**outBuffer);
198 
199         return result;
200     }
201 
authenticateSurfaceTexture(const sp<IGraphicBufferProducer> & bufferProducer) const202     virtual bool authenticateSurfaceTexture(
203             const sp<IGraphicBufferProducer>& bufferProducer) const
204     {
205         Parcel data, reply;
206         int err = NO_ERROR;
207         err = data.writeInterfaceToken(
208                 ISurfaceComposer::getInterfaceDescriptor());
209         if (err != NO_ERROR) {
210             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
211                     "interface descriptor: %s (%d)", strerror(-err), -err);
212             return false;
213         }
214         err = data.writeStrongBinder(IInterface::asBinder(bufferProducer));
215         if (err != NO_ERROR) {
216             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
217                     "strong binder to parcel: %s (%d)", strerror(-err), -err);
218             return false;
219         }
220         err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
221                 &reply);
222         if (err != NO_ERROR) {
223             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
224                     "performing transaction: %s (%d)", strerror(-err), -err);
225             return false;
226         }
227         int32_t result = 0;
228         err = reply.readInt32(&result);
229         if (err != NO_ERROR) {
230             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
231                     "retrieving result: %s (%d)", strerror(-err), -err);
232             return false;
233         }
234         return result != 0;
235     }
236 
getSupportedFrameTimestamps(std::vector<FrameEvent> * outSupported) const237     virtual status_t getSupportedFrameTimestamps(
238             std::vector<FrameEvent>* outSupported) const {
239         if (!outSupported) {
240             return UNEXPECTED_NULL;
241         }
242         outSupported->clear();
243 
244         Parcel data, reply;
245 
246         status_t err = data.writeInterfaceToken(
247                 ISurfaceComposer::getInterfaceDescriptor());
248         if (err != NO_ERROR) {
249             return err;
250         }
251 
252         err = remote()->transact(
253                 BnSurfaceComposer::GET_SUPPORTED_FRAME_TIMESTAMPS,
254                 data, &reply);
255         if (err != NO_ERROR) {
256             return err;
257         }
258 
259         int32_t result = 0;
260         err = reply.readInt32(&result);
261         if (err != NO_ERROR) {
262             return err;
263         }
264         if (result != NO_ERROR) {
265             return result;
266         }
267 
268         std::vector<int32_t> supported;
269         err = reply.readInt32Vector(&supported);
270         if (err != NO_ERROR) {
271             return err;
272         }
273 
274         outSupported->reserve(supported.size());
275         for (int32_t s : supported) {
276             outSupported->push_back(static_cast<FrameEvent>(s));
277         }
278         return NO_ERROR;
279     }
280 
createDisplayEventConnection(VsyncSource vsyncSource)281     virtual sp<IDisplayEventConnection> createDisplayEventConnection(VsyncSource vsyncSource)
282     {
283         Parcel data, reply;
284         sp<IDisplayEventConnection> result;
285         int err = data.writeInterfaceToken(
286                 ISurfaceComposer::getInterfaceDescriptor());
287         if (err != NO_ERROR) {
288             return result;
289         }
290         data.writeInt32(static_cast<int32_t>(vsyncSource));
291         err = remote()->transact(
292                 BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
293                 data, &reply);
294         if (err != NO_ERROR) {
295             ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
296                     "transaction: %s (%d)", strerror(-err), -err);
297             return result;
298         }
299         result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
300         return result;
301     }
302 
createDisplay(const String8 & displayName,bool secure)303     virtual sp<IBinder> createDisplay(const String8& displayName, bool secure)
304     {
305         Parcel data, reply;
306         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
307         data.writeString8(displayName);
308         data.writeInt32(secure ? 1 : 0);
309         remote()->transact(BnSurfaceComposer::CREATE_DISPLAY, data, &reply);
310         return reply.readStrongBinder();
311     }
312 
destroyDisplay(const sp<IBinder> & display)313     virtual void destroyDisplay(const sp<IBinder>& display)
314     {
315         Parcel data, reply;
316         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
317         data.writeStrongBinder(display);
318         remote()->transact(BnSurfaceComposer::DESTROY_DISPLAY, data, &reply);
319     }
320 
getPhysicalDisplayIds() const321     virtual std::vector<PhysicalDisplayId> getPhysicalDisplayIds() const {
322         Parcel data, reply;
323         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
324         if (remote()->transact(BnSurfaceComposer::GET_PHYSICAL_DISPLAY_IDS, data, &reply) ==
325             NO_ERROR) {
326             std::vector<PhysicalDisplayId> displayIds;
327             if (reply.readUint64Vector(&displayIds) == NO_ERROR) {
328                 return displayIds;
329             }
330         }
331 
332         return {};
333     }
334 
getPhysicalDisplayToken(PhysicalDisplayId displayId) const335     virtual sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId) const {
336         Parcel data, reply;
337         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
338         data.writeUint64(displayId);
339         remote()->transact(BnSurfaceComposer::GET_PHYSICAL_DISPLAY_TOKEN, data, &reply);
340         return reply.readStrongBinder();
341     }
342 
setPowerMode(const sp<IBinder> & display,int mode)343     virtual void setPowerMode(const sp<IBinder>& display, int mode)
344     {
345         Parcel data, reply;
346         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
347         data.writeStrongBinder(display);
348         data.writeInt32(mode);
349         remote()->transact(BnSurfaceComposer::SET_POWER_MODE, data, &reply);
350     }
351 
getDisplayConfigs(const sp<IBinder> & display,Vector<DisplayInfo> * configs)352     virtual status_t getDisplayConfigs(const sp<IBinder>& display,
353             Vector<DisplayInfo>* configs)
354     {
355         Parcel data, reply;
356         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
357         data.writeStrongBinder(display);
358         remote()->transact(BnSurfaceComposer::GET_DISPLAY_CONFIGS, data, &reply);
359         status_t result = reply.readInt32();
360         if (result == NO_ERROR) {
361             size_t numConfigs = reply.readUint32();
362             configs->clear();
363             configs->resize(numConfigs);
364             for (size_t c = 0; c < numConfigs; ++c) {
365                 memcpy(&(configs->editItemAt(c)),
366                         reply.readInplace(sizeof(DisplayInfo)),
367                         sizeof(DisplayInfo));
368             }
369         }
370         return result;
371     }
372 
getDisplayStats(const sp<IBinder> & display,DisplayStatInfo * stats)373     virtual status_t getDisplayStats(const sp<IBinder>& display,
374             DisplayStatInfo* stats)
375     {
376         Parcel data, reply;
377         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
378         data.writeStrongBinder(display);
379         remote()->transact(BnSurfaceComposer::GET_DISPLAY_STATS, data, &reply);
380         status_t result = reply.readInt32();
381         if (result == NO_ERROR) {
382             memcpy(stats,
383                     reply.readInplace(sizeof(DisplayStatInfo)),
384                     sizeof(DisplayStatInfo));
385         }
386         return result;
387     }
388 
getActiveConfig(const sp<IBinder> & display)389     virtual int getActiveConfig(const sp<IBinder>& display)
390     {
391         Parcel data, reply;
392         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
393         data.writeStrongBinder(display);
394         remote()->transact(BnSurfaceComposer::GET_ACTIVE_CONFIG, data, &reply);
395         return reply.readInt32();
396     }
397 
setActiveConfig(const sp<IBinder> & display,int id)398     virtual status_t setActiveConfig(const sp<IBinder>& display, int id)
399     {
400         Parcel data, reply;
401         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
402         if (result != NO_ERROR) {
403             ALOGE("setActiveConfig failed to writeInterfaceToken: %d", result);
404             return result;
405         }
406         result = data.writeStrongBinder(display);
407         if (result != NO_ERROR) {
408             ALOGE("setActiveConfig failed to writeStrongBinder: %d", result);
409             return result;
410         }
411         result = data.writeInt32(id);
412         if (result != NO_ERROR) {
413             ALOGE("setActiveConfig failed to writeInt32: %d", result);
414             return result;
415         }
416         result = remote()->transact(BnSurfaceComposer::SET_ACTIVE_CONFIG, data, &reply);
417         if (result != NO_ERROR) {
418             ALOGE("setActiveConfig failed to transact: %d", result);
419             return result;
420         }
421         return reply.readInt32();
422     }
423 
getDisplayColorModes(const sp<IBinder> & display,Vector<ColorMode> * outColorModes)424     virtual status_t getDisplayColorModes(const sp<IBinder>& display,
425             Vector<ColorMode>* outColorModes) {
426         Parcel data, reply;
427         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
428         if (result != NO_ERROR) {
429             ALOGE("getDisplayColorModes failed to writeInterfaceToken: %d", result);
430             return result;
431         }
432         result = data.writeStrongBinder(display);
433         if (result != NO_ERROR) {
434             ALOGE("getDisplayColorModes failed to writeStrongBinder: %d", result);
435             return result;
436         }
437         result = remote()->transact(BnSurfaceComposer::GET_DISPLAY_COLOR_MODES, data, &reply);
438         if (result != NO_ERROR) {
439             ALOGE("getDisplayColorModes failed to transact: %d", result);
440             return result;
441         }
442         result = static_cast<status_t>(reply.readInt32());
443         if (result == NO_ERROR) {
444             size_t numModes = reply.readUint32();
445             outColorModes->clear();
446             outColorModes->resize(numModes);
447             for (size_t i = 0; i < numModes; ++i) {
448                 outColorModes->replaceAt(static_cast<ColorMode>(reply.readInt32()), i);
449             }
450         }
451         return result;
452     }
453 
getDisplayNativePrimaries(const sp<IBinder> & display,ui::DisplayPrimaries & primaries)454     virtual status_t getDisplayNativePrimaries(const sp<IBinder>& display,
455             ui::DisplayPrimaries& primaries) {
456         Parcel data, reply;
457         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
458         if (result != NO_ERROR) {
459             ALOGE("getDisplayNativePrimaries failed to writeInterfaceToken: %d", result);
460             return result;
461         }
462         result = data.writeStrongBinder(display);
463         if (result != NO_ERROR) {
464             ALOGE("getDisplayNativePrimaries failed to writeStrongBinder: %d", result);
465             return result;
466         }
467         result = remote()->transact(BnSurfaceComposer::GET_DISPLAY_NATIVE_PRIMARIES, data, &reply);
468         if (result != NO_ERROR) {
469             ALOGE("getDisplayNativePrimaries failed to transact: %d", result);
470             return result;
471         }
472         result = reply.readInt32();
473         if (result == NO_ERROR) {
474             memcpy(&primaries, reply.readInplace(sizeof(ui::DisplayPrimaries)),
475                     sizeof(ui::DisplayPrimaries));
476         }
477         return result;
478     }
479 
getActiveColorMode(const sp<IBinder> & display)480     virtual ColorMode getActiveColorMode(const sp<IBinder>& display) {
481         Parcel data, reply;
482         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
483         if (result != NO_ERROR) {
484             ALOGE("getActiveColorMode failed to writeInterfaceToken: %d", result);
485             return static_cast<ColorMode>(result);
486         }
487         result = data.writeStrongBinder(display);
488         if (result != NO_ERROR) {
489             ALOGE("getActiveColorMode failed to writeStrongBinder: %d", result);
490             return static_cast<ColorMode>(result);
491         }
492         result = remote()->transact(BnSurfaceComposer::GET_ACTIVE_COLOR_MODE, data, &reply);
493         if (result != NO_ERROR) {
494             ALOGE("getActiveColorMode failed to transact: %d", result);
495             return static_cast<ColorMode>(result);
496         }
497         return static_cast<ColorMode>(reply.readInt32());
498     }
499 
setActiveColorMode(const sp<IBinder> & display,ColorMode colorMode)500     virtual status_t setActiveColorMode(const sp<IBinder>& display,
501             ColorMode colorMode) {
502         Parcel data, reply;
503         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
504         if (result != NO_ERROR) {
505             ALOGE("setActiveColorMode failed to writeInterfaceToken: %d", result);
506             return result;
507         }
508         result = data.writeStrongBinder(display);
509         if (result != NO_ERROR) {
510             ALOGE("setActiveColorMode failed to writeStrongBinder: %d", result);
511             return result;
512         }
513         result = data.writeInt32(static_cast<int32_t>(colorMode));
514         if (result != NO_ERROR) {
515             ALOGE("setActiveColorMode failed to writeInt32: %d", result);
516             return result;
517         }
518         result = remote()->transact(BnSurfaceComposer::SET_ACTIVE_COLOR_MODE, data, &reply);
519         if (result != NO_ERROR) {
520             ALOGE("setActiveColorMode failed to transact: %d", result);
521             return result;
522         }
523         return static_cast<status_t>(reply.readInt32());
524     }
525 
clearAnimationFrameStats()526     virtual status_t clearAnimationFrameStats() {
527         Parcel data, reply;
528         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
529         if (result != NO_ERROR) {
530             ALOGE("clearAnimationFrameStats failed to writeInterfaceToken: %d", result);
531             return result;
532         }
533         result = remote()->transact(BnSurfaceComposer::CLEAR_ANIMATION_FRAME_STATS, data, &reply);
534         if (result != NO_ERROR) {
535             ALOGE("clearAnimationFrameStats failed to transact: %d", result);
536             return result;
537         }
538         return reply.readInt32();
539     }
540 
getAnimationFrameStats(FrameStats * outStats) const541     virtual status_t getAnimationFrameStats(FrameStats* outStats) const {
542         Parcel data, reply;
543         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
544         remote()->transact(BnSurfaceComposer::GET_ANIMATION_FRAME_STATS, data, &reply);
545         reply.read(*outStats);
546         return reply.readInt32();
547     }
548 
getHdrCapabilities(const sp<IBinder> & display,HdrCapabilities * outCapabilities) const549     virtual status_t getHdrCapabilities(const sp<IBinder>& display,
550             HdrCapabilities* outCapabilities) const {
551         Parcel data, reply;
552         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
553         status_t result = data.writeStrongBinder(display);
554         if (result != NO_ERROR) {
555             ALOGE("getHdrCapabilities failed to writeStrongBinder: %d", result);
556             return result;
557         }
558         result = remote()->transact(BnSurfaceComposer::GET_HDR_CAPABILITIES,
559                 data, &reply);
560         if (result != NO_ERROR) {
561             ALOGE("getHdrCapabilities failed to transact: %d", result);
562             return result;
563         }
564         result = reply.readInt32();
565         if (result == NO_ERROR) {
566             result = reply.read(*outCapabilities);
567         }
568         return result;
569     }
570 
enableVSyncInjections(bool enable)571     virtual status_t enableVSyncInjections(bool enable) {
572         Parcel data, reply;
573         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
574         if (result != NO_ERROR) {
575             ALOGE("enableVSyncInjections failed to writeInterfaceToken: %d", result);
576             return result;
577         }
578         result = data.writeBool(enable);
579         if (result != NO_ERROR) {
580             ALOGE("enableVSyncInjections failed to writeBool: %d", result);
581             return result;
582         }
583         result = remote()->transact(BnSurfaceComposer::ENABLE_VSYNC_INJECTIONS, data, &reply,
584                                     IBinder::FLAG_ONEWAY);
585         if (result != NO_ERROR) {
586             ALOGE("enableVSyncInjections failed to transact: %d", result);
587             return result;
588         }
589         return result;
590     }
591 
injectVSync(nsecs_t when)592     virtual status_t injectVSync(nsecs_t when) {
593         Parcel data, reply;
594         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
595         if (result != NO_ERROR) {
596             ALOGE("injectVSync failed to writeInterfaceToken: %d", result);
597             return result;
598         }
599         result = data.writeInt64(when);
600         if (result != NO_ERROR) {
601             ALOGE("injectVSync failed to writeInt64: %d", result);
602             return result;
603         }
604         result = remote()->transact(BnSurfaceComposer::INJECT_VSYNC, data, &reply,
605                                     IBinder::FLAG_ONEWAY);
606         if (result != NO_ERROR) {
607             ALOGE("injectVSync failed to transact: %d", result);
608             return result;
609         }
610         return result;
611     }
612 
getLayerDebugInfo(std::vector<LayerDebugInfo> * outLayers) const613     virtual status_t getLayerDebugInfo(std::vector<LayerDebugInfo>* outLayers) const
614     {
615         if (!outLayers) {
616             return UNEXPECTED_NULL;
617         }
618 
619         Parcel data, reply;
620 
621         status_t err = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
622         if (err != NO_ERROR) {
623             return err;
624         }
625 
626         err = remote()->transact(BnSurfaceComposer::GET_LAYER_DEBUG_INFO, data, &reply);
627         if (err != NO_ERROR) {
628             return err;
629         }
630 
631         int32_t result = 0;
632         err = reply.readInt32(&result);
633         if (err != NO_ERROR) {
634             return err;
635         }
636         if (result != NO_ERROR) {
637             return result;
638         }
639 
640         outLayers->clear();
641         return reply.readParcelableVector(outLayers);
642     }
643 
getCompositionPreference(ui::Dataspace * defaultDataspace,ui::PixelFormat * defaultPixelFormat,ui::Dataspace * wideColorGamutDataspace,ui::PixelFormat * wideColorGamutPixelFormat) const644     virtual status_t getCompositionPreference(ui::Dataspace* defaultDataspace,
645                                               ui::PixelFormat* defaultPixelFormat,
646                                               ui::Dataspace* wideColorGamutDataspace,
647                                               ui::PixelFormat* wideColorGamutPixelFormat) const {
648         Parcel data, reply;
649         status_t error = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
650         if (error != NO_ERROR) {
651             return error;
652         }
653         error = remote()->transact(BnSurfaceComposer::GET_COMPOSITION_PREFERENCE, data, &reply);
654         if (error != NO_ERROR) {
655             return error;
656         }
657         error = static_cast<status_t>(reply.readInt32());
658         if (error == NO_ERROR) {
659             *defaultDataspace = static_cast<ui::Dataspace>(reply.readInt32());
660             *defaultPixelFormat = static_cast<ui::PixelFormat>(reply.readInt32());
661             *wideColorGamutDataspace = static_cast<ui::Dataspace>(reply.readInt32());
662             *wideColorGamutPixelFormat = static_cast<ui::PixelFormat>(reply.readInt32());
663         }
664         return error;
665     }
666 
getColorManagement(bool * outGetColorManagement) const667     virtual status_t getColorManagement(bool* outGetColorManagement) const {
668         Parcel data, reply;
669         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
670         remote()->transact(BnSurfaceComposer::GET_COLOR_MANAGEMENT, data, &reply);
671         bool result;
672         status_t err = reply.readBool(&result);
673         if (err == NO_ERROR) {
674             *outGetColorManagement = result;
675         }
676         return err;
677     }
678 
getDisplayedContentSamplingAttributes(const sp<IBinder> & display,ui::PixelFormat * outFormat,ui::Dataspace * outDataspace,uint8_t * outComponentMask) const679     virtual status_t getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
680                                                            ui::PixelFormat* outFormat,
681                                                            ui::Dataspace* outDataspace,
682                                                            uint8_t* outComponentMask) const {
683         if (!outFormat || !outDataspace || !outComponentMask) return BAD_VALUE;
684         Parcel data, reply;
685         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
686         data.writeStrongBinder(display);
687 
688         status_t error =
689                 remote()->transact(BnSurfaceComposer::GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES,
690                                    data, &reply);
691         if (error != NO_ERROR) {
692             return error;
693         }
694 
695         uint32_t value = 0;
696         error = reply.readUint32(&value);
697         if (error != NO_ERROR) {
698             return error;
699         }
700         *outFormat = static_cast<ui::PixelFormat>(value);
701 
702         error = reply.readUint32(&value);
703         if (error != NO_ERROR) {
704             return error;
705         }
706         *outDataspace = static_cast<ui::Dataspace>(value);
707 
708         error = reply.readUint32(&value);
709         if (error != NO_ERROR) {
710             return error;
711         }
712         *outComponentMask = static_cast<uint8_t>(value);
713         return error;
714     }
715 
setDisplayContentSamplingEnabled(const sp<IBinder> & display,bool enable,uint8_t componentMask,uint64_t maxFrames) const716     virtual status_t setDisplayContentSamplingEnabled(const sp<IBinder>& display, bool enable,
717                                                       uint8_t componentMask,
718                                                       uint64_t maxFrames) const {
719         Parcel data, reply;
720         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
721         data.writeStrongBinder(display);
722         data.writeBool(enable);
723         data.writeByte(static_cast<int8_t>(componentMask));
724         data.writeUint64(maxFrames);
725         status_t result =
726                 remote()->transact(BnSurfaceComposer::SET_DISPLAY_CONTENT_SAMPLING_ENABLED, data,
727                                    &reply);
728         return result;
729     }
730 
getDisplayedContentSample(const sp<IBinder> & display,uint64_t maxFrames,uint64_t timestamp,DisplayedFrameStats * outStats) const731     virtual status_t getDisplayedContentSample(const sp<IBinder>& display, uint64_t maxFrames,
732                                                uint64_t timestamp,
733                                                DisplayedFrameStats* outStats) const {
734         if (!outStats) return BAD_VALUE;
735 
736         Parcel data, reply;
737         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
738         data.writeStrongBinder(display);
739         data.writeUint64(maxFrames);
740         data.writeUint64(timestamp);
741 
742         status_t result =
743                 remote()->transact(BnSurfaceComposer::GET_DISPLAYED_CONTENT_SAMPLE, data, &reply);
744 
745         if (result != NO_ERROR) {
746             return result;
747         }
748 
749         result = reply.readUint64(&outStats->numFrames);
750         if (result != NO_ERROR) {
751             return result;
752         }
753 
754         result = reply.readUint64Vector(&outStats->component_0_sample);
755         if (result != NO_ERROR) {
756             return result;
757         }
758         result = reply.readUint64Vector(&outStats->component_1_sample);
759         if (result != NO_ERROR) {
760             return result;
761         }
762         result = reply.readUint64Vector(&outStats->component_2_sample);
763         if (result != NO_ERROR) {
764             return result;
765         }
766         result = reply.readUint64Vector(&outStats->component_3_sample);
767         return result;
768     }
769 
getProtectedContentSupport(bool * outSupported) const770     virtual status_t getProtectedContentSupport(bool* outSupported) const {
771         Parcel data, reply;
772         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
773         status_t error =
774                 remote()->transact(BnSurfaceComposer::GET_PROTECTED_CONTENT_SUPPORT, data, &reply);
775         if (error != NO_ERROR) {
776             return error;
777         }
778         error = reply.readBool(outSupported);
779         return error;
780     }
781 
isWideColorDisplay(const sp<IBinder> & token,bool * outIsWideColorDisplay) const782     virtual status_t isWideColorDisplay(const sp<IBinder>& token,
783                                         bool* outIsWideColorDisplay) const {
784         Parcel data, reply;
785         status_t error = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
786         if (error != NO_ERROR) {
787             return error;
788         }
789         error = data.writeStrongBinder(token);
790         if (error != NO_ERROR) {
791             return error;
792         }
793 
794         error = remote()->transact(BnSurfaceComposer::IS_WIDE_COLOR_DISPLAY, data, &reply);
795         if (error != NO_ERROR) {
796             return error;
797         }
798         error = reply.readBool(outIsWideColorDisplay);
799         return error;
800     }
801 
addRegionSamplingListener(const Rect & samplingArea,const sp<IBinder> & stopLayerHandle,const sp<IRegionSamplingListener> & listener)802     virtual status_t addRegionSamplingListener(const Rect& samplingArea,
803                                                const sp<IBinder>& stopLayerHandle,
804                                                const sp<IRegionSamplingListener>& listener) {
805         Parcel data, reply;
806         status_t error = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
807         if (error != NO_ERROR) {
808             ALOGE("addRegionSamplingListener: Failed to write interface token");
809             return error;
810         }
811         error = data.write(samplingArea);
812         if (error != NO_ERROR) {
813             ALOGE("addRegionSamplingListener: Failed to write sampling area");
814             return error;
815         }
816         error = data.writeStrongBinder(stopLayerHandle);
817         if (error != NO_ERROR) {
818             ALOGE("addRegionSamplingListener: Failed to write stop layer handle");
819             return error;
820         }
821         error = data.writeStrongBinder(IInterface::asBinder(listener));
822         if (error != NO_ERROR) {
823             ALOGE("addRegionSamplingListener: Failed to write listener");
824             return error;
825         }
826         error = remote()->transact(BnSurfaceComposer::ADD_REGION_SAMPLING_LISTENER, data, &reply);
827         if (error != NO_ERROR) {
828             ALOGE("addRegionSamplingListener: Failed to transact");
829         }
830         return error;
831     }
832 
removeRegionSamplingListener(const sp<IRegionSamplingListener> & listener)833     virtual status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) {
834         Parcel data, reply;
835         status_t error = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
836         if (error != NO_ERROR) {
837             ALOGE("removeRegionSamplingListener: Failed to write interface token");
838             return error;
839         }
840         error = data.writeStrongBinder(IInterface::asBinder(listener));
841         if (error != NO_ERROR) {
842             ALOGE("removeRegionSamplingListener: Failed to write listener");
843             return error;
844         }
845         error = remote()->transact(BnSurfaceComposer::REMOVE_REGION_SAMPLING_LISTENER, data,
846                                    &reply);
847         if (error != NO_ERROR) {
848             ALOGE("removeRegionSamplingListener: Failed to transact");
849         }
850         return error;
851     }
852 
setAllowedDisplayConfigs(const sp<IBinder> & displayToken,const std::vector<int32_t> & allowedConfigs)853     virtual status_t setAllowedDisplayConfigs(const sp<IBinder>& displayToken,
854                                               const std::vector<int32_t>& allowedConfigs) {
855         Parcel data, reply;
856         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
857         if (result != NO_ERROR) {
858             ALOGE("setAllowedDisplayConfigs failed to writeInterfaceToken: %d", result);
859             return result;
860         }
861         result = data.writeStrongBinder(displayToken);
862         if (result != NO_ERROR) {
863             ALOGE("setAllowedDisplayConfigs failed to writeStrongBinder: %d", result);
864             return result;
865         }
866         result = data.writeInt32Vector(allowedConfigs);
867         if (result != NO_ERROR) {
868             ALOGE("setAllowedDisplayConfigs failed to writeInt32Vector: %d", result);
869             return result;
870         }
871         result = remote()->transact(BnSurfaceComposer::SET_ALLOWED_DISPLAY_CONFIGS, data, &reply);
872         if (result != NO_ERROR) {
873             ALOGE("setAllowedDisplayConfigs failed to transact: %d", result);
874             return result;
875         }
876         return reply.readInt32();
877     }
878 
getAllowedDisplayConfigs(const sp<IBinder> & displayToken,std::vector<int32_t> * outAllowedConfigs)879     virtual status_t getAllowedDisplayConfigs(const sp<IBinder>& displayToken,
880                                               std::vector<int32_t>* outAllowedConfigs) {
881         if (!outAllowedConfigs) return BAD_VALUE;
882         Parcel data, reply;
883         status_t result = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
884         if (result != NO_ERROR) {
885             ALOGE("getAllowedDisplayConfigs failed to writeInterfaceToken: %d", result);
886             return result;
887         }
888         result = data.writeStrongBinder(displayToken);
889         if (result != NO_ERROR) {
890             ALOGE("getAllowedDisplayConfigs failed to writeStrongBinder: %d", result);
891             return result;
892         }
893         result = remote()->transact(BnSurfaceComposer::GET_ALLOWED_DISPLAY_CONFIGS, data, &reply);
894         if (result != NO_ERROR) {
895             ALOGE("getAllowedDisplayConfigs failed to transact: %d", result);
896             return result;
897         }
898         result = reply.readInt32Vector(outAllowedConfigs);
899         if (result != NO_ERROR) {
900             ALOGE("getAllowedDisplayConfigs failed to readInt32Vector: %d", result);
901             return result;
902         }
903         return reply.readInt32();
904     }
905 
getDisplayBrightnessSupport(const sp<IBinder> & displayToken,bool * outSupport) const906     virtual status_t getDisplayBrightnessSupport(const sp<IBinder>& displayToken,
907                                                  bool* outSupport) const {
908         Parcel data, reply;
909         status_t error = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
910         if (error != NO_ERROR) {
911             ALOGE("getDisplayBrightnessSupport: failed to write interface token: %d", error);
912             return error;
913         }
914         error = data.writeStrongBinder(displayToken);
915         if (error != NO_ERROR) {
916             ALOGE("getDisplayBrightnessSupport: failed to write display token: %d", error);
917             return error;
918         }
919         error = remote()->transact(BnSurfaceComposer::GET_DISPLAY_BRIGHTNESS_SUPPORT, data, &reply);
920         if (error != NO_ERROR) {
921             ALOGE("getDisplayBrightnessSupport: failed to transact: %d", error);
922             return error;
923         }
924         bool support;
925         error = reply.readBool(&support);
926         if (error != NO_ERROR) {
927             ALOGE("getDisplayBrightnessSupport: failed to read support: %d", error);
928             return error;
929         }
930         *outSupport = support;
931         return NO_ERROR;
932     }
933 
setDisplayBrightness(const sp<IBinder> & displayToken,float brightness) const934     virtual status_t setDisplayBrightness(const sp<IBinder>& displayToken, float brightness) const {
935         Parcel data, reply;
936         status_t error = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
937         if (error != NO_ERROR) {
938             ALOGE("setDisplayBrightness: failed to write interface token: %d", error);
939             return error;
940         }
941         error = data.writeStrongBinder(displayToken);
942         if (error != NO_ERROR) {
943             ALOGE("setDisplayBrightness: failed to write display token: %d", error);
944             return error;
945         }
946         error = data.writeFloat(brightness);
947         if (error != NO_ERROR) {
948             ALOGE("setDisplayBrightness: failed to write brightness: %d", error);
949             return error;
950         }
951         error = remote()->transact(BnSurfaceComposer::SET_DISPLAY_BRIGHTNESS, data, &reply);
952         if (error != NO_ERROR) {
953             ALOGE("setDisplayBrightness: failed to transact: %d", error);
954             return error;
955         }
956         return NO_ERROR;
957     }
958 
notifyPowerHint(int32_t hintId)959     virtual status_t notifyPowerHint(int32_t hintId) {
960         Parcel data, reply;
961         status_t error = data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
962         if (error != NO_ERROR) {
963             ALOGE("notifyPowerHint: failed to write interface token: %d", error);
964             return error;
965         }
966         error = data.writeInt32(hintId);
967         if (error != NO_ERROR) {
968             ALOGE("notifyPowerHint: failed to write hintId: %d", error);
969             return error;
970         }
971         error = remote()->transact(BnSurfaceComposer::NOTIFY_POWER_HINT, data, &reply,
972                                    IBinder::FLAG_ONEWAY);
973         if (error != NO_ERROR) {
974             ALOGE("notifyPowerHint: failed to transact: %d", error);
975             return error;
976         }
977         return NO_ERROR;
978     }
979 };
980 
981 // Out-of-line virtual method definition to trigger vtable emission in this
982 // translation unit (see clang warning -Wweak-vtables)
~BpSurfaceComposer()983 BpSurfaceComposer::~BpSurfaceComposer() {}
984 
985 IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
986 
987 // ----------------------------------------------------------------------
988 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)989 status_t BnSurfaceComposer::onTransact(
990     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
991 {
992     switch(code) {
993         case CREATE_CONNECTION: {
994             CHECK_INTERFACE(ISurfaceComposer, data, reply);
995             sp<IBinder> b = IInterface::asBinder(createConnection());
996             reply->writeStrongBinder(b);
997             return NO_ERROR;
998         }
999         case SET_TRANSACTION_STATE: {
1000             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1001 
1002             size_t count = data.readUint32();
1003             if (count > data.dataSize()) {
1004                 return BAD_VALUE;
1005             }
1006             Vector<ComposerState> state;
1007             state.setCapacity(count);
1008             for (size_t i = 0; i < count; i++) {
1009                 ComposerState s;
1010                 if (s.read(data) == BAD_VALUE) {
1011                     return BAD_VALUE;
1012                 }
1013                 state.add(s);
1014             }
1015 
1016             count = data.readUint32();
1017             if (count > data.dataSize()) {
1018                 return BAD_VALUE;
1019             }
1020             DisplayState d;
1021             Vector<DisplayState> displays;
1022             displays.setCapacity(count);
1023             for (size_t i = 0; i < count; i++) {
1024                 if (d.read(data) == BAD_VALUE) {
1025                     return BAD_VALUE;
1026                 }
1027                 displays.add(d);
1028             }
1029 
1030             uint32_t stateFlags = data.readUint32();
1031             sp<IBinder> applyToken = data.readStrongBinder();
1032             InputWindowCommands inputWindowCommands;
1033             inputWindowCommands.read(data);
1034 
1035             int64_t desiredPresentTime = data.readInt64();
1036 
1037             client_cache_t uncachedBuffer;
1038             uncachedBuffer.token = data.readWeakBinder();
1039             uncachedBuffer.id = data.readUint64();
1040 
1041             std::vector<ListenerCallbacks> listenerCallbacks;
1042             int32_t listenersSize = data.readInt32();
1043             for (int32_t i = 0; i < listenersSize; i++) {
1044                 auto listener =
1045                         interface_cast<ITransactionCompletedListener>(data.readStrongBinder());
1046                 std::vector<CallbackId> callbackIds;
1047                 data.readInt64Vector(&callbackIds);
1048                 listenerCallbacks.emplace_back(listener, callbackIds);
1049             }
1050 
1051             setTransactionState(state, displays, stateFlags, applyToken, inputWindowCommands,
1052                                 desiredPresentTime, uncachedBuffer, listenerCallbacks);
1053             return NO_ERROR;
1054         }
1055         case BOOT_FINISHED: {
1056             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1057             bootFinished();
1058             return NO_ERROR;
1059         }
1060         case CAPTURE_SCREEN: {
1061             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1062             sp<IBinder> display = data.readStrongBinder();
1063             ui::Dataspace reqDataspace = static_cast<ui::Dataspace>(data.readInt32());
1064             ui::PixelFormat reqPixelFormat = static_cast<ui::PixelFormat>(data.readInt32());
1065             sp<GraphicBuffer> outBuffer;
1066             Rect sourceCrop(Rect::EMPTY_RECT);
1067             data.read(sourceCrop);
1068             uint32_t reqWidth = data.readUint32();
1069             uint32_t reqHeight = data.readUint32();
1070             bool useIdentityTransform = static_cast<bool>(data.readInt32());
1071             int32_t rotation = data.readInt32();
1072             bool captureSecureLayers = static_cast<bool>(data.readInt32());
1073 
1074             bool capturedSecureLayers = false;
1075             status_t res = captureScreen(display, &outBuffer, capturedSecureLayers, reqDataspace,
1076                                          reqPixelFormat, sourceCrop, reqWidth, reqHeight,
1077                                          useIdentityTransform,
1078                                          static_cast<ISurfaceComposer::Rotation>(rotation),
1079                                          captureSecureLayers);
1080 
1081             reply->writeInt32(res);
1082             if (res == NO_ERROR) {
1083                 reply->write(*outBuffer);
1084                 reply->writeBool(capturedSecureLayers);
1085             }
1086             return NO_ERROR;
1087         }
1088         case CAPTURE_SCREEN_BY_ID: {
1089             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1090             uint64_t displayOrLayerStack = data.readUint64();
1091             ui::Dataspace outDataspace = ui::Dataspace::V0_SRGB;
1092             sp<GraphicBuffer> outBuffer;
1093             status_t res = captureScreen(displayOrLayerStack, &outDataspace, &outBuffer);
1094             reply->writeInt32(res);
1095             if (res == NO_ERROR) {
1096                 reply->writeInt32(static_cast<int32_t>(outDataspace));
1097                 reply->write(*outBuffer);
1098             }
1099             return NO_ERROR;
1100         }
1101         case CAPTURE_LAYERS: {
1102             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1103             sp<IBinder> layerHandleBinder = data.readStrongBinder();
1104             ui::Dataspace reqDataspace = static_cast<ui::Dataspace>(data.readInt32());
1105             ui::PixelFormat reqPixelFormat = static_cast<ui::PixelFormat>(data.readInt32());
1106             sp<GraphicBuffer> outBuffer;
1107             Rect sourceCrop(Rect::EMPTY_RECT);
1108             data.read(sourceCrop);
1109 
1110             std::unordered_set<sp<IBinder>, SpHash<IBinder>> excludeHandles;
1111             int numExcludeHandles = data.readInt32();
1112             excludeHandles.reserve(numExcludeHandles);
1113             for (int i = 0; i < numExcludeHandles; i++) {
1114                 excludeHandles.emplace(data.readStrongBinder());
1115             }
1116 
1117             float frameScale = data.readFloat();
1118             bool childrenOnly = data.readBool();
1119 
1120             status_t res =
1121                     captureLayers(layerHandleBinder, &outBuffer, reqDataspace, reqPixelFormat,
1122                                   sourceCrop, excludeHandles, frameScale, childrenOnly);
1123             reply->writeInt32(res);
1124             if (res == NO_ERROR) {
1125                 reply->write(*outBuffer);
1126             }
1127             return NO_ERROR;
1128         }
1129         case AUTHENTICATE_SURFACE: {
1130             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1131             sp<IGraphicBufferProducer> bufferProducer =
1132                     interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
1133             int32_t result = authenticateSurfaceTexture(bufferProducer) ? 1 : 0;
1134             reply->writeInt32(result);
1135             return NO_ERROR;
1136         }
1137         case GET_SUPPORTED_FRAME_TIMESTAMPS: {
1138             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1139             std::vector<FrameEvent> supportedTimestamps;
1140             status_t result = getSupportedFrameTimestamps(&supportedTimestamps);
1141             status_t err = reply->writeInt32(result);
1142             if (err != NO_ERROR) {
1143                 return err;
1144             }
1145             if (result != NO_ERROR) {
1146                 return result;
1147             }
1148 
1149             std::vector<int32_t> supported;
1150             supported.reserve(supportedTimestamps.size());
1151             for (FrameEvent s : supportedTimestamps) {
1152                 supported.push_back(static_cast<int32_t>(s));
1153             }
1154             return reply->writeInt32Vector(supported);
1155         }
1156         case CREATE_DISPLAY_EVENT_CONNECTION: {
1157             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1158             sp<IDisplayEventConnection> connection(createDisplayEventConnection(
1159                     static_cast<ISurfaceComposer::VsyncSource>(data.readInt32())));
1160             reply->writeStrongBinder(IInterface::asBinder(connection));
1161             return NO_ERROR;
1162         }
1163         case CREATE_DISPLAY: {
1164             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1165             String8 displayName = data.readString8();
1166             bool secure = bool(data.readInt32());
1167             sp<IBinder> display(createDisplay(displayName, secure));
1168             reply->writeStrongBinder(display);
1169             return NO_ERROR;
1170         }
1171         case DESTROY_DISPLAY: {
1172             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1173             sp<IBinder> display = data.readStrongBinder();
1174             destroyDisplay(display);
1175             return NO_ERROR;
1176         }
1177         case GET_PHYSICAL_DISPLAY_TOKEN: {
1178             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1179             PhysicalDisplayId displayId = data.readUint64();
1180             sp<IBinder> display = getPhysicalDisplayToken(displayId);
1181             reply->writeStrongBinder(display);
1182             return NO_ERROR;
1183         }
1184         case GET_DISPLAY_CONFIGS: {
1185             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1186             Vector<DisplayInfo> configs;
1187             sp<IBinder> display = data.readStrongBinder();
1188             status_t result = getDisplayConfigs(display, &configs);
1189             reply->writeInt32(result);
1190             if (result == NO_ERROR) {
1191                 reply->writeUint32(static_cast<uint32_t>(configs.size()));
1192                 for (size_t c = 0; c < configs.size(); ++c) {
1193                     memcpy(reply->writeInplace(sizeof(DisplayInfo)),
1194                             &configs[c], sizeof(DisplayInfo));
1195                 }
1196             }
1197             return NO_ERROR;
1198         }
1199         case GET_DISPLAY_STATS: {
1200             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1201             DisplayStatInfo stats;
1202             sp<IBinder> display = data.readStrongBinder();
1203             status_t result = getDisplayStats(display, &stats);
1204             reply->writeInt32(result);
1205             if (result == NO_ERROR) {
1206                 memcpy(reply->writeInplace(sizeof(DisplayStatInfo)),
1207                         &stats, sizeof(DisplayStatInfo));
1208             }
1209             return NO_ERROR;
1210         }
1211         case GET_ACTIVE_CONFIG: {
1212             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1213             sp<IBinder> display = data.readStrongBinder();
1214             int id = getActiveConfig(display);
1215             reply->writeInt32(id);
1216             return NO_ERROR;
1217         }
1218         case SET_ACTIVE_CONFIG: {
1219             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1220             sp<IBinder> display = data.readStrongBinder();
1221             int id = data.readInt32();
1222             status_t result = setActiveConfig(display, id);
1223             reply->writeInt32(result);
1224             return NO_ERROR;
1225         }
1226         case GET_DISPLAY_COLOR_MODES: {
1227             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1228             Vector<ColorMode> colorModes;
1229             sp<IBinder> display = nullptr;
1230             status_t result = data.readStrongBinder(&display);
1231             if (result != NO_ERROR) {
1232                 ALOGE("getDisplayColorModes failed to readStrongBinder: %d", result);
1233                 return result;
1234             }
1235             result = getDisplayColorModes(display, &colorModes);
1236             reply->writeInt32(result);
1237             if (result == NO_ERROR) {
1238                 reply->writeUint32(static_cast<uint32_t>(colorModes.size()));
1239                 for (size_t i = 0; i < colorModes.size(); ++i) {
1240                     reply->writeInt32(static_cast<int32_t>(colorModes[i]));
1241                 }
1242             }
1243             return NO_ERROR;
1244         }
1245         case GET_DISPLAY_NATIVE_PRIMARIES: {
1246             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1247             ui::DisplayPrimaries primaries;
1248             sp<IBinder> display = nullptr;
1249 
1250             status_t result = data.readStrongBinder(&display);
1251             if (result != NO_ERROR) {
1252                 ALOGE("getDisplayNativePrimaries failed to readStrongBinder: %d", result);
1253                 return result;
1254             }
1255 
1256             result = getDisplayNativePrimaries(display, primaries);
1257             reply->writeInt32(result);
1258             if (result == NO_ERROR) {
1259                 memcpy(reply->writeInplace(sizeof(ui::DisplayPrimaries)), &primaries,
1260                         sizeof(ui::DisplayPrimaries));
1261             }
1262 
1263             return NO_ERROR;
1264         }
1265         case GET_ACTIVE_COLOR_MODE: {
1266             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1267             sp<IBinder> display = nullptr;
1268             status_t result = data.readStrongBinder(&display);
1269             if (result != NO_ERROR) {
1270                 ALOGE("getActiveColorMode failed to readStrongBinder: %d", result);
1271                 return result;
1272             }
1273             ColorMode colorMode = getActiveColorMode(display);
1274             result = reply->writeInt32(static_cast<int32_t>(colorMode));
1275             return result;
1276         }
1277         case SET_ACTIVE_COLOR_MODE: {
1278             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1279             sp<IBinder> display = nullptr;
1280             status_t result = data.readStrongBinder(&display);
1281             if (result != NO_ERROR) {
1282                 ALOGE("getActiveColorMode failed to readStrongBinder: %d", result);
1283                 return result;
1284             }
1285             int32_t colorModeInt = 0;
1286             result = data.readInt32(&colorModeInt);
1287             if (result != NO_ERROR) {
1288                 ALOGE("setActiveColorMode failed to readInt32: %d", result);
1289                 return result;
1290             }
1291             result = setActiveColorMode(display,
1292                     static_cast<ColorMode>(colorModeInt));
1293             result = reply->writeInt32(result);
1294             return result;
1295         }
1296         case CLEAR_ANIMATION_FRAME_STATS: {
1297             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1298             status_t result = clearAnimationFrameStats();
1299             reply->writeInt32(result);
1300             return NO_ERROR;
1301         }
1302         case GET_ANIMATION_FRAME_STATS: {
1303             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1304             FrameStats stats;
1305             status_t result = getAnimationFrameStats(&stats);
1306             reply->write(stats);
1307             reply->writeInt32(result);
1308             return NO_ERROR;
1309         }
1310         case SET_POWER_MODE: {
1311             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1312             sp<IBinder> display = data.readStrongBinder();
1313             int32_t mode = data.readInt32();
1314             setPowerMode(display, mode);
1315             return NO_ERROR;
1316         }
1317         case GET_HDR_CAPABILITIES: {
1318             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1319             sp<IBinder> display = nullptr;
1320             status_t result = data.readStrongBinder(&display);
1321             if (result != NO_ERROR) {
1322                 ALOGE("getHdrCapabilities failed to readStrongBinder: %d",
1323                         result);
1324                 return result;
1325             }
1326             HdrCapabilities capabilities;
1327             result = getHdrCapabilities(display, &capabilities);
1328             reply->writeInt32(result);
1329             if (result == NO_ERROR) {
1330                 reply->write(capabilities);
1331             }
1332             return NO_ERROR;
1333         }
1334         case ENABLE_VSYNC_INJECTIONS: {
1335             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1336             bool enable = false;
1337             status_t result = data.readBool(&enable);
1338             if (result != NO_ERROR) {
1339                 ALOGE("enableVSyncInjections failed to readBool: %d", result);
1340                 return result;
1341             }
1342             return enableVSyncInjections(enable);
1343         }
1344         case INJECT_VSYNC: {
1345             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1346             int64_t when = 0;
1347             status_t result = data.readInt64(&when);
1348             if (result != NO_ERROR) {
1349                 ALOGE("enableVSyncInjections failed to readInt64: %d", result);
1350                 return result;
1351             }
1352             return injectVSync(when);
1353         }
1354         case GET_LAYER_DEBUG_INFO: {
1355             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1356             std::vector<LayerDebugInfo> outLayers;
1357             status_t result = getLayerDebugInfo(&outLayers);
1358             reply->writeInt32(result);
1359             if (result == NO_ERROR)
1360             {
1361                 result = reply->writeParcelableVector(outLayers);
1362             }
1363             return result;
1364         }
1365         case GET_COMPOSITION_PREFERENCE: {
1366             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1367             ui::Dataspace defaultDataspace;
1368             ui::PixelFormat defaultPixelFormat;
1369             ui::Dataspace wideColorGamutDataspace;
1370             ui::PixelFormat wideColorGamutPixelFormat;
1371             status_t error =
1372                     getCompositionPreference(&defaultDataspace, &defaultPixelFormat,
1373                                              &wideColorGamutDataspace, &wideColorGamutPixelFormat);
1374             reply->writeInt32(error);
1375             if (error == NO_ERROR) {
1376                 reply->writeInt32(static_cast<int32_t>(defaultDataspace));
1377                 reply->writeInt32(static_cast<int32_t>(defaultPixelFormat));
1378                 reply->writeInt32(static_cast<int32_t>(wideColorGamutDataspace));
1379                 reply->writeInt32(static_cast<int32_t>(wideColorGamutPixelFormat));
1380             }
1381             return error;
1382         }
1383         case GET_COLOR_MANAGEMENT: {
1384             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1385             bool result;
1386             status_t error = getColorManagement(&result);
1387             if (error == NO_ERROR) {
1388                 reply->writeBool(result);
1389             }
1390             return error;
1391         }
1392         case GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES: {
1393             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1394 
1395             sp<IBinder> display = data.readStrongBinder();
1396             ui::PixelFormat format;
1397             ui::Dataspace dataspace;
1398             uint8_t component = 0;
1399             auto result =
1400                     getDisplayedContentSamplingAttributes(display, &format, &dataspace, &component);
1401             if (result == NO_ERROR) {
1402                 reply->writeUint32(static_cast<uint32_t>(format));
1403                 reply->writeUint32(static_cast<uint32_t>(dataspace));
1404                 reply->writeUint32(static_cast<uint32_t>(component));
1405             }
1406             return result;
1407         }
1408         case SET_DISPLAY_CONTENT_SAMPLING_ENABLED: {
1409             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1410 
1411             sp<IBinder> display = nullptr;
1412             bool enable = false;
1413             int8_t componentMask = 0;
1414             uint64_t maxFrames = 0;
1415             status_t result = data.readStrongBinder(&display);
1416             if (result != NO_ERROR) {
1417                 ALOGE("setDisplayContentSamplingEnabled failure in reading Display token: %d",
1418                       result);
1419                 return result;
1420             }
1421 
1422             result = data.readBool(&enable);
1423             if (result != NO_ERROR) {
1424                 ALOGE("setDisplayContentSamplingEnabled failure in reading enable: %d", result);
1425                 return result;
1426             }
1427 
1428             result = data.readByte(static_cast<int8_t*>(&componentMask));
1429             if (result != NO_ERROR) {
1430                 ALOGE("setDisplayContentSamplingEnabled failure in reading component mask: %d",
1431                       result);
1432                 return result;
1433             }
1434 
1435             result = data.readUint64(&maxFrames);
1436             if (result != NO_ERROR) {
1437                 ALOGE("setDisplayContentSamplingEnabled failure in reading max frames: %d", result);
1438                 return result;
1439             }
1440 
1441             return setDisplayContentSamplingEnabled(display, enable,
1442                                                     static_cast<uint8_t>(componentMask), maxFrames);
1443         }
1444         case GET_DISPLAYED_CONTENT_SAMPLE: {
1445             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1446 
1447             sp<IBinder> display = data.readStrongBinder();
1448             uint64_t maxFrames = 0;
1449             uint64_t timestamp = 0;
1450 
1451             status_t result = data.readUint64(&maxFrames);
1452             if (result != NO_ERROR) {
1453                 ALOGE("getDisplayedContentSample failure in reading max frames: %d", result);
1454                 return result;
1455             }
1456 
1457             result = data.readUint64(&timestamp);
1458             if (result != NO_ERROR) {
1459                 ALOGE("getDisplayedContentSample failure in reading timestamp: %d", result);
1460                 return result;
1461             }
1462 
1463             DisplayedFrameStats stats;
1464             result = getDisplayedContentSample(display, maxFrames, timestamp, &stats);
1465             if (result == NO_ERROR) {
1466                 reply->writeUint64(stats.numFrames);
1467                 reply->writeUint64Vector(stats.component_0_sample);
1468                 reply->writeUint64Vector(stats.component_1_sample);
1469                 reply->writeUint64Vector(stats.component_2_sample);
1470                 reply->writeUint64Vector(stats.component_3_sample);
1471             }
1472             return result;
1473         }
1474         case GET_PROTECTED_CONTENT_SUPPORT: {
1475             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1476             bool result;
1477             status_t error = getProtectedContentSupport(&result);
1478             if (error == NO_ERROR) {
1479                 reply->writeBool(result);
1480             }
1481             return error;
1482         }
1483         case IS_WIDE_COLOR_DISPLAY: {
1484             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1485             sp<IBinder> display = nullptr;
1486             status_t error = data.readStrongBinder(&display);
1487             if (error != NO_ERROR) {
1488                 return error;
1489             }
1490             bool result;
1491             error = isWideColorDisplay(display, &result);
1492             if (error == NO_ERROR) {
1493                 reply->writeBool(result);
1494             }
1495             return error;
1496         }
1497         case GET_PHYSICAL_DISPLAY_IDS: {
1498             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1499             return reply->writeUint64Vector(getPhysicalDisplayIds());
1500         }
1501         case ADD_REGION_SAMPLING_LISTENER: {
1502             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1503             Rect samplingArea;
1504             status_t result = data.read(samplingArea);
1505             if (result != NO_ERROR) {
1506                 ALOGE("addRegionSamplingListener: Failed to read sampling area");
1507                 return result;
1508             }
1509             sp<IBinder> stopLayerHandle;
1510             result = data.readNullableStrongBinder(&stopLayerHandle);
1511             if (result != NO_ERROR) {
1512                 ALOGE("addRegionSamplingListener: Failed to read stop layer handle");
1513                 return result;
1514             }
1515             sp<IRegionSamplingListener> listener;
1516             result = data.readNullableStrongBinder(&listener);
1517             if (result != NO_ERROR) {
1518                 ALOGE("addRegionSamplingListener: Failed to read listener");
1519                 return result;
1520             }
1521             return addRegionSamplingListener(samplingArea, stopLayerHandle, listener);
1522         }
1523         case REMOVE_REGION_SAMPLING_LISTENER: {
1524             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1525             sp<IRegionSamplingListener> listener;
1526             status_t result = data.readNullableStrongBinder(&listener);
1527             if (result != NO_ERROR) {
1528                 ALOGE("removeRegionSamplingListener: Failed to read listener");
1529                 return result;
1530             }
1531             return removeRegionSamplingListener(listener);
1532         }
1533         case SET_ALLOWED_DISPLAY_CONFIGS: {
1534             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1535             sp<IBinder> displayToken = data.readStrongBinder();
1536             std::vector<int32_t> allowedConfigs;
1537             data.readInt32Vector(&allowedConfigs);
1538             status_t result = setAllowedDisplayConfigs(displayToken, allowedConfigs);
1539             reply->writeInt32(result);
1540             return result;
1541         }
1542         case GET_ALLOWED_DISPLAY_CONFIGS: {
1543             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1544             sp<IBinder> displayToken = data.readStrongBinder();
1545             std::vector<int32_t> allowedConfigs;
1546             status_t result = getAllowedDisplayConfigs(displayToken, &allowedConfigs);
1547             reply->writeInt32Vector(allowedConfigs);
1548             reply->writeInt32(result);
1549             return result;
1550         }
1551         case GET_DISPLAY_BRIGHTNESS_SUPPORT: {
1552             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1553             sp<IBinder> displayToken;
1554             status_t error = data.readNullableStrongBinder(&displayToken);
1555             if (error != NO_ERROR) {
1556                 ALOGE("getDisplayBrightnessSupport: failed to read display token: %d", error);
1557                 return error;
1558             }
1559             bool support = false;
1560             error = getDisplayBrightnessSupport(displayToken, &support);
1561             reply->writeBool(support);
1562             return error;
1563         }
1564         case SET_DISPLAY_BRIGHTNESS: {
1565             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1566             sp<IBinder> displayToken;
1567             status_t error = data.readNullableStrongBinder(&displayToken);
1568             if (error != NO_ERROR) {
1569                 ALOGE("setDisplayBrightness: failed to read display token: %d", error);
1570                 return error;
1571             }
1572             float brightness = -1.0f;
1573             error = data.readFloat(&brightness);
1574             if (error != NO_ERROR) {
1575                 ALOGE("setDisplayBrightness: failed to read brightness: %d", error);
1576                 return error;
1577             }
1578             return setDisplayBrightness(displayToken, brightness);
1579         }
1580         case NOTIFY_POWER_HINT: {
1581             CHECK_INTERFACE(ISurfaceComposer, data, reply);
1582             int32_t hintId;
1583             status_t error = data.readInt32(&hintId);
1584             if (error != NO_ERROR) {
1585                 ALOGE("notifyPowerHint: failed to read hintId: %d", error);
1586                 return error;
1587             }
1588             return notifyPowerHint(hintId);
1589         }
1590         default: {
1591             return BBinder::onTransact(code, data, reply, flags);
1592         }
1593     }
1594 }
1595 
1596 } // namespace android
1597