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