1 /*
2  * Copyright (C) 2010 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 #include <stdint.h>
18 #include <sys/types.h>
19 
20 #include <utils/Errors.h>
21 #include <utils/NativeHandle.h>
22 #include <utils/RefBase.h>
23 #include <utils/String8.h>
24 #include <utils/Timers.h>
25 #include <utils/Vector.h>
26 
27 #include <binder/Parcel.h>
28 #include <binder/IInterface.h>
29 
30 #include <gui/BufferQueueDefs.h>
31 
32 #include <gui/IGraphicBufferProducer.h>
33 #include <gui/IProducerListener.h>
34 #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
35 #include <gui/bufferqueue/2.0/H2BGraphicBufferProducer.h>
36 
37 namespace android {
38 // ----------------------------------------------------------------------------
39 
40 using H2BGraphicBufferProducerV1_0 =
41         ::android::hardware::graphics::bufferqueue::V1_0::utils::
42         H2BGraphicBufferProducer;
43 using H2BGraphicBufferProducerV2_0 =
44         ::android::hardware::graphics::bufferqueue::V2_0::utils::
45         H2BGraphicBufferProducer;
46 
47 enum {
48     REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
49     DEQUEUE_BUFFER,
50     DETACH_BUFFER,
51     DETACH_NEXT_BUFFER,
52     ATTACH_BUFFER,
53     QUEUE_BUFFER,
54     CANCEL_BUFFER,
55     QUERY,
56     CONNECT,
57     DISCONNECT,
58     SET_SIDEBAND_STREAM,
59     ALLOCATE_BUFFERS,
60     ALLOW_ALLOCATION,
61     SET_GENERATION_NUMBER,
62     GET_CONSUMER_NAME,
63     SET_MAX_DEQUEUED_BUFFER_COUNT,
64     SET_ASYNC_MODE,
65     SET_SHARED_BUFFER_MODE,
66     SET_AUTO_REFRESH,
67     SET_DEQUEUE_TIMEOUT,
68     GET_LAST_QUEUED_BUFFER,
69     GET_FRAME_TIMESTAMPS,
70     GET_UNIQUE_ID,
71     GET_CONSUMER_USAGE,
72     SET_LEGACY_BUFFER_DROP,
73     SET_AUTO_PREROTATION,
74     REQUEST_BUFFERS,
75     DEQUEUE_BUFFERS,
76     DETACH_BUFFERS,
77     ATTACH_BUFFERS,
78     QUEUE_BUFFERS,
79     CANCEL_BUFFERS,
80     QUERY_MULTIPLE,
81     GET_LAST_QUEUED_BUFFER2,
82     SET_FRAME_RATE,
83     SET_ADDITIONAL_OPTIONS,
84 };
85 
86 class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
87 {
88 public:
BpGraphicBufferProducer(const sp<IBinder> & impl)89     explicit BpGraphicBufferProducer(const sp<IBinder>& impl)
90         : BpInterface<IGraphicBufferProducer>(impl)
91     {
92     }
93 
94     ~BpGraphicBufferProducer() override;
95 
requestBuffer(int bufferIdx,sp<GraphicBuffer> * buf)96     virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
97         Parcel data, reply;
98         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
99         data.writeInt32(bufferIdx);
100         status_t result = remote()->transact(REQUEST_BUFFER, data, &reply);
101         if (result != NO_ERROR) {
102             return result;
103         }
104         bool nonNull = reply.readInt32();
105         if (nonNull) {
106             *buf = new GraphicBuffer();
107             result = reply.read(**buf);
108             if(result != NO_ERROR) {
109                 (*buf).clear();
110                 return result;
111             }
112         }
113         result = reply.readInt32();
114         return result;
115     }
116 
requestBuffers(const std::vector<int32_t> & slots,std::vector<RequestBufferOutput> * outputs)117     virtual status_t requestBuffers(
118             const std::vector<int32_t>& slots,
119             std::vector<RequestBufferOutput>* outputs) override {
120         Parcel data, reply;
121         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
122         data.writeInt32Vector(slots);
123         status_t result = remote()->transact(REQUEST_BUFFERS, data, &reply);
124         if (result != NO_ERROR) {
125             return result;
126         }
127         result = reply.resizeOutVector(outputs);
128         for (RequestBufferOutput& output : *outputs) {
129             if (result != NO_ERROR) {
130                 return result;
131             }
132             result = reply.read(output);
133         }
134 
135         return result;
136     }
137 
setMaxDequeuedBufferCount(int maxDequeuedBuffers)138     virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
139         Parcel data, reply;
140         data.writeInterfaceToken(
141                 IGraphicBufferProducer::getInterfaceDescriptor());
142         data.writeInt32(maxDequeuedBuffers);
143         status_t result = remote()->transact(SET_MAX_DEQUEUED_BUFFER_COUNT,
144                 data, &reply);
145         if (result != NO_ERROR) {
146             return result;
147         }
148         result = reply.readInt32();
149         return result;
150     }
151 
setAsyncMode(bool async)152     virtual status_t setAsyncMode(bool async) {
153         Parcel data, reply;
154         data.writeInterfaceToken(
155                 IGraphicBufferProducer::getInterfaceDescriptor());
156         data.writeInt32(async);
157         status_t result = remote()->transact(SET_ASYNC_MODE,
158                 data, &reply);
159         if (result != NO_ERROR) {
160             return result;
161         }
162         result = reply.readInt32();
163         return result;
164     }
165 
dequeueBuffer(int * buf,sp<Fence> * fence,uint32_t width,uint32_t height,PixelFormat format,uint64_t usage,uint64_t * outBufferAge,FrameEventHistoryDelta * outTimestamps)166     virtual status_t dequeueBuffer(int* buf, sp<Fence>* fence, uint32_t width, uint32_t height,
167                                    PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
168                                    FrameEventHistoryDelta* outTimestamps) {
169         Parcel data, reply;
170         bool getFrameTimestamps = (outTimestamps != nullptr);
171 
172         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
173         data.writeUint32(width);
174         data.writeUint32(height);
175         data.writeInt32(static_cast<int32_t>(format));
176         data.writeUint64(usage);
177         data.writeBool(getFrameTimestamps);
178 
179         status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
180         if (result != NO_ERROR) {
181             return result;
182         }
183 
184         *buf = reply.readInt32();
185         *fence = new Fence();
186         result = reply.read(**fence);
187         if (result != NO_ERROR) {
188             fence->clear();
189             return result;
190         }
191         if (outBufferAge) {
192             result = reply.readUint64(outBufferAge);
193         } else {
194             // Read the value even if outBufferAge is nullptr:
195             uint64_t bufferAge;
196             result = reply.readUint64(&bufferAge);
197         }
198         if (result != NO_ERROR) {
199             ALOGE("IGBP::dequeueBuffer failed to read buffer age: %d", result);
200             return result;
201         }
202         if (getFrameTimestamps) {
203             result = reply.read(*outTimestamps);
204             if (result != NO_ERROR) {
205                 ALOGE("IGBP::dequeueBuffer failed to read timestamps: %d",
206                         result);
207                 return result;
208             }
209         }
210         result = reply.readInt32();
211         return result;
212     }
213 
dequeueBuffers(const std::vector<DequeueBufferInput> & inputs,std::vector<DequeueBufferOutput> * outputs)214     virtual status_t dequeueBuffers(
215             const std::vector<DequeueBufferInput>& inputs,
216             std::vector<DequeueBufferOutput>* outputs) {
217         Parcel data, reply;
218         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
219         data.writeVectorSize(inputs);
220         for (const auto& input : inputs) {
221             data.write(input);
222         }
223         status_t result = remote()->transact(DEQUEUE_BUFFERS, data, &reply);
224         if (result != NO_ERROR) {
225             return result;
226         }
227         result = reply.resizeOutVector(outputs);
228         for (auto& output : *outputs) {
229             if (result != NO_ERROR) {
230                 return result;
231             }
232             result = reply.read(output);
233         }
234         return result;
235     }
236 
detachBuffer(int slot)237     virtual status_t detachBuffer(int slot) {
238         Parcel data, reply;
239         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
240         data.writeInt32(slot);
241         status_t result = remote()->transact(DETACH_BUFFER, data, &reply);
242         if (result != NO_ERROR) {
243             return result;
244         }
245         result = reply.readInt32();
246         return result;
247     }
248 
detachBuffers(const std::vector<int32_t> & slots,std::vector<status_t> * results)249     virtual status_t detachBuffers(const std::vector<int32_t>& slots,
250                                    std::vector<status_t>* results) {
251         Parcel data, reply;
252         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
253         data.writeInt32Vector(slots);
254         status_t result = remote()->transact(DETACH_BUFFERS, data, &reply);
255         if (result != NO_ERROR) {
256             return result;
257         }
258         result = reply.readInt32Vector(results);
259         return result;
260     }
261 
detachNextBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence)262     virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
263             sp<Fence>* outFence) {
264         if (outBuffer == nullptr) {
265             ALOGE("detachNextBuffer: outBuffer must not be NULL");
266             return BAD_VALUE;
267         } else if (outFence == nullptr) {
268             ALOGE("detachNextBuffer: outFence must not be NULL");
269             return BAD_VALUE;
270         }
271         Parcel data, reply;
272         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
273         status_t result = remote()->transact(DETACH_NEXT_BUFFER, data, &reply);
274         if (result != NO_ERROR) {
275             return result;
276         }
277         result = reply.readInt32();
278         if (result == NO_ERROR) {
279             bool nonNull = reply.readInt32();
280             if (nonNull) {
281                 *outBuffer = new GraphicBuffer;
282                 result = reply.read(**outBuffer);
283                 if (result != NO_ERROR) {
284                     outBuffer->clear();
285                     return result;
286                 }
287             }
288             nonNull = reply.readInt32();
289             if (nonNull) {
290                 *outFence = new Fence;
291                 result = reply.read(**outFence);
292                 if (result != NO_ERROR) {
293                     outBuffer->clear();
294                     outFence->clear();
295                     return result;
296                 }
297             }
298         }
299         return result;
300     }
301 
attachBuffer(int * slot,const sp<GraphicBuffer> & buffer)302     virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) {
303         Parcel data, reply;
304         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
305         data.write(*buffer.get());
306         status_t result = remote()->transact(ATTACH_BUFFER, data, &reply);
307         if (result != NO_ERROR) {
308             return result;
309         }
310 
311         *slot = reply.readInt32();
312         result = reply.readInt32();
313         if (result == NO_ERROR &&
314                 (*slot < 0 || *slot >= BufferQueueDefs::NUM_BUFFER_SLOTS)) {
315             ALOGE("attachBuffer returned invalid slot %d", *slot);
316             android_errorWriteLog(0x534e4554, "37478824");
317             return UNKNOWN_ERROR;
318         }
319 
320         return result;
321     }
322 
attachBuffers(const std::vector<sp<GraphicBuffer>> & buffers,std::vector<AttachBufferOutput> * outputs)323     virtual status_t attachBuffers(
324             const std::vector<sp<GraphicBuffer>>& buffers,
325             std::vector<AttachBufferOutput>* outputs) {
326         Parcel data, reply;
327         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
328         data.writeVectorSize(buffers);
329         for (const sp<GraphicBuffer>& buffer : buffers) {
330             data.write(*buffer.get());
331         }
332         status_t result = remote()->transact(ATTACH_BUFFERS, data, &reply);
333         if (result != NO_ERROR) {
334             return result;
335         }
336         result = reply.resizeOutVector(outputs);
337         for (AttachBufferOutput& output : *outputs) {
338             if (result != NO_ERROR) {
339                 return result;
340             }
341             result = reply.read(output);
342         }
343         if (result == NO_ERROR) {
344             for (AttachBufferOutput& output : *outputs) {
345                 if (output.result == NO_ERROR && output.slot < 0) {
346                     ALOGE("attachBuffers returned invalid slot %d",
347                           output.slot);
348                     android_errorWriteLog(0x534e4554, "37478824");
349                     output.result = UNKNOWN_ERROR;
350                 }
351             }
352         }
353         return result;
354     }
355 
queueBuffer(int buf,const QueueBufferInput & input,QueueBufferOutput * output)356     virtual status_t queueBuffer(int buf,
357             const QueueBufferInput& input, QueueBufferOutput* output) {
358         Parcel data, reply;
359 
360         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
361         data.writeInt32(buf);
362         data.write(input);
363 
364         status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
365         if (result != NO_ERROR) {
366             return result;
367         }
368 
369         result = reply.read(*output);
370         if (result != NO_ERROR) {
371             return result;
372         }
373 
374         result = reply.readInt32();
375         return result;
376     }
377 
queueBuffers(const std::vector<QueueBufferInput> & inputs,std::vector<QueueBufferOutput> * outputs)378     virtual status_t queueBuffers(const std::vector<QueueBufferInput>& inputs,
379                                   std::vector<QueueBufferOutput>* outputs) {
380         Parcel data, reply;
381         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
382         data.writeVectorSize(inputs);
383         for (const QueueBufferInput& input : inputs) {
384             data.write(input);
385         }
386         status_t result = remote()->transact(QUEUE_BUFFERS, data, &reply);
387         if (result != NO_ERROR) {
388             return result;
389         }
390         result = reply.resizeOutVector(outputs);
391         for (QueueBufferOutput& output : *outputs) {
392             if (result != NO_ERROR) {
393                 return result;
394             }
395             result = reply.read(output);
396         }
397         return result;
398     }
399 
cancelBuffer(int buf,const sp<Fence> & fence)400     virtual status_t cancelBuffer(int buf, const sp<Fence>& fence) {
401         Parcel data, reply;
402         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
403         data.writeInt32(buf);
404         data.write(*fence.get());
405         status_t result = remote()->transact(CANCEL_BUFFER, data, &reply);
406         if (result != NO_ERROR) {
407             return result;
408         }
409         result = reply.readInt32();
410         return result;
411     }
412 
cancelBuffers(const std::vector<CancelBufferInput> & inputs,std::vector<status_t> * results)413     virtual status_t cancelBuffers(
414             const std::vector<CancelBufferInput>& inputs,
415             std::vector<status_t>* results) {
416         Parcel data, reply;
417         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
418         data.writeVectorSize(inputs);
419         for (const CancelBufferInput& input : inputs) {
420             data.write(input);
421         }
422         status_t result = remote()->transact(CANCEL_BUFFERS, data, &reply);
423         if (result != NO_ERROR) {
424             return result;
425         }
426         result = reply.readInt32Vector(results);
427         return result;
428     }
429 
query(int what,int * value)430     virtual int query(int what, int* value) {
431         Parcel data, reply;
432         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
433         data.writeInt32(what);
434         status_t result = remote()->transact(QUERY, data, &reply);
435         if (result != NO_ERROR) {
436             return result;
437         }
438         value[0] = reply.readInt32();
439         result = reply.readInt32();
440         return result;
441     }
442 
query(const std::vector<int32_t> inputs,std::vector<QueryOutput> * outputs)443     virtual status_t query(const std::vector<int32_t> inputs,
444                            std::vector<QueryOutput>* outputs) {
445         Parcel data, reply;
446         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
447         data.writeInt32Vector(inputs);
448         status_t result = remote()->transact(QUERY_MULTIPLE, data, &reply);
449         if (result != NO_ERROR) {
450             return result;
451         }
452         result = reply.resizeOutVector(outputs);
453         for (QueryOutput& output : *outputs) {
454             if (result != NO_ERROR) {
455                 return result;
456             }
457             result = reply.read(output);
458         }
459         return result;
460     }
461 
connect(const sp<IProducerListener> & listener,int api,bool producerControlledByApp,QueueBufferOutput * output)462     virtual status_t connect(const sp<IProducerListener>& listener,
463             int api, bool producerControlledByApp, QueueBufferOutput* output) {
464         Parcel data, reply;
465         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
466         if (listener != nullptr) {
467             data.writeInt32(1);
468             data.writeStrongBinder(IInterface::asBinder(listener));
469         } else {
470             data.writeInt32(0);
471         }
472         data.writeInt32(api);
473         data.writeInt32(producerControlledByApp);
474         status_t result = remote()->transact(CONNECT, data, &reply);
475         if (result != NO_ERROR) {
476             return result;
477         }
478         reply.read(*output);
479         result = reply.readInt32();
480         return result;
481     }
482 
disconnect(int api,DisconnectMode mode)483     virtual status_t disconnect(int api, DisconnectMode mode) {
484         Parcel data, reply;
485         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
486         data.writeInt32(api);
487         data.writeInt32(static_cast<int32_t>(mode));
488         status_t result =remote()->transact(DISCONNECT, data, &reply);
489         if (result != NO_ERROR) {
490             return result;
491         }
492         result = reply.readInt32();
493         return result;
494     }
495 
setSidebandStream(const sp<NativeHandle> & stream)496     virtual status_t setSidebandStream(const sp<NativeHandle>& stream) {
497         Parcel data, reply;
498         status_t result;
499         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
500         if (stream.get()) {
501             data.writeInt32(true);
502             data.writeNativeHandle(stream->handle());
503         } else {
504             data.writeInt32(false);
505         }
506         if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) {
507             result = reply.readInt32();
508         }
509         return result;
510     }
511 
allocateBuffers(uint32_t width,uint32_t height,PixelFormat format,uint64_t usage)512     virtual void allocateBuffers(uint32_t width, uint32_t height,
513             PixelFormat format, uint64_t usage) {
514         Parcel data, reply;
515         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
516         data.writeUint32(width);
517         data.writeUint32(height);
518         data.writeInt32(static_cast<int32_t>(format));
519         data.writeUint64(usage);
520         status_t result = remote()->transact(ALLOCATE_BUFFERS, data, &reply, IBinder::FLAG_ONEWAY);
521         if (result != NO_ERROR) {
522             ALOGE("allocateBuffers failed to transact: %d", result);
523         }
524     }
525 
allowAllocation(bool allow)526     virtual status_t allowAllocation(bool allow) {
527         Parcel data, reply;
528         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
529         data.writeInt32(static_cast<int32_t>(allow));
530         status_t result = remote()->transact(ALLOW_ALLOCATION, data, &reply);
531         if (result != NO_ERROR) {
532             return result;
533         }
534         result = reply.readInt32();
535         return result;
536     }
537 
setGenerationNumber(uint32_t generationNumber)538     virtual status_t setGenerationNumber(uint32_t generationNumber) {
539         Parcel data, reply;
540         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
541         data.writeUint32(generationNumber);
542         status_t result = remote()->transact(SET_GENERATION_NUMBER, data, &reply);
543         if (result == NO_ERROR) {
544             result = reply.readInt32();
545         }
546         return result;
547     }
548 
getConsumerName() const549     virtual String8 getConsumerName() const {
550         Parcel data, reply;
551         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
552         status_t result = remote()->transact(GET_CONSUMER_NAME, data, &reply);
553         if (result != NO_ERROR) {
554             ALOGE("getConsumerName failed to transact: %d", result);
555             return String8("TransactFailed");
556         }
557         return reply.readString8();
558     }
559 
setSharedBufferMode(bool sharedBufferMode)560     virtual status_t setSharedBufferMode(bool sharedBufferMode) {
561         Parcel data, reply;
562         data.writeInterfaceToken(
563                 IGraphicBufferProducer::getInterfaceDescriptor());
564         data.writeInt32(sharedBufferMode);
565         status_t result = remote()->transact(SET_SHARED_BUFFER_MODE, data,
566                 &reply);
567         if (result == NO_ERROR) {
568             result = reply.readInt32();
569         }
570         return result;
571     }
572 
setAutoRefresh(bool autoRefresh)573     virtual status_t setAutoRefresh(bool autoRefresh) {
574         Parcel data, reply;
575         data.writeInterfaceToken(
576                 IGraphicBufferProducer::getInterfaceDescriptor());
577         data.writeInt32(autoRefresh);
578         status_t result = remote()->transact(SET_AUTO_REFRESH, data, &reply);
579         if (result == NO_ERROR) {
580             result = reply.readInt32();
581         }
582         return result;
583     }
584 
setDequeueTimeout(nsecs_t timeout)585     virtual status_t setDequeueTimeout(nsecs_t timeout) {
586         Parcel data, reply;
587         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
588         data.writeInt64(timeout);
589         status_t result = remote()->transact(SET_DEQUEUE_TIMEOUT, data, &reply);
590         if (result != NO_ERROR) {
591             ALOGE("setDequeueTimeout failed to transact: %d", result);
592             return result;
593         }
594         return reply.readInt32();
595     }
596 
setLegacyBufferDrop(bool drop)597     virtual status_t setLegacyBufferDrop(bool drop) {
598         Parcel data, reply;
599         data.writeInterfaceToken(
600                 IGraphicBufferProducer::getInterfaceDescriptor());
601         data.writeInt32(drop);
602         status_t result = remote()->transact(SET_LEGACY_BUFFER_DROP,
603                 data, &reply);
604         if (result != NO_ERROR) {
605             return result;
606         }
607         result = reply.readInt32();
608         return result;
609     }
610 
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,float outTransformMatrix[16])611     virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
612             sp<Fence>* outFence, float outTransformMatrix[16]) override {
613         Parcel data, reply;
614         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
615         status_t result = remote()->transact(GET_LAST_QUEUED_BUFFER, data,
616                 &reply);
617         if (result != NO_ERROR) {
618             ALOGE("getLastQueuedBuffer failed to transact: %d", result);
619             return result;
620         }
621         result = reply.readInt32();
622         if (result != NO_ERROR) {
623             return result;
624         }
625         bool hasBuffer = reply.readBool();
626         sp<GraphicBuffer> buffer;
627         if (hasBuffer) {
628             buffer = new GraphicBuffer();
629             result = reply.read(*buffer);
630             if (result == NO_ERROR) {
631                 result = reply.read(outTransformMatrix, sizeof(float) * 16);
632             }
633         }
634         if (result != NO_ERROR) {
635             ALOGE("getLastQueuedBuffer failed to read buffer: %d", result);
636             return result;
637         }
638         sp<Fence> fence(new Fence);
639         result = reply.read(*fence);
640         if (result != NO_ERROR) {
641             ALOGE("getLastQueuedBuffer failed to read fence: %d", result);
642             return result;
643         }
644         *outBuffer = buffer;
645         *outFence = fence;
646         return result;
647     }
648 
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,Rect * outRect,uint32_t * outTransform)649     virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
650                                          Rect* outRect, uint32_t* outTransform) override {
651         Parcel data, reply;
652         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
653         status_t result = remote()->transact(GET_LAST_QUEUED_BUFFER2, data, &reply);
654         if (result != NO_ERROR) {
655             ALOGE("getLastQueuedBuffer failed to transact: %d", result);
656             return result;
657         }
658         status_t remoteError = NO_ERROR;
659         result = reply.readInt32(&remoteError);
660         if (result != NO_ERROR) {
661             ALOGE("getLastQueuedBuffer failed to read status: %d", result);
662             return result;
663         }
664         if (remoteError != NO_ERROR) {
665             return remoteError;
666         }
667         bool hasBuffer = false;
668         result = reply.readBool(&hasBuffer);
669         if (result != NO_ERROR) {
670             ALOGE("getLastQueuedBuffer failed to read buffer: %d", result);
671             return result;
672         }
673         sp<GraphicBuffer> buffer;
674         if (hasBuffer) {
675             buffer = new GraphicBuffer();
676             result = reply.read(*buffer);
677             if (result == NO_ERROR) {
678                 result = reply.read(*outRect);
679             }
680             if (result == NO_ERROR) {
681                 result = reply.readUint32(outTransform);
682             }
683         }
684         if (result != NO_ERROR) {
685             ALOGE("getLastQueuedBuffer failed to read buffer: %d", result);
686             return result;
687         }
688         sp<Fence> fence(new Fence);
689         result = reply.read(*fence);
690         if (result != NO_ERROR) {
691             ALOGE("getLastQueuedBuffer failed to read fence: %d", result);
692             return result;
693         }
694         *outBuffer = buffer;
695         *outFence = fence;
696         return result;
697     }
698 
getFrameTimestamps(FrameEventHistoryDelta * outDelta)699     virtual void getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
700         Parcel data, reply;
701         status_t result = data.writeInterfaceToken(
702                 IGraphicBufferProducer::getInterfaceDescriptor());
703         if (result != NO_ERROR) {
704             ALOGE("IGBP::getFrameTimestamps failed to write token: %d", result);
705             return;
706         }
707         result = remote()->transact(GET_FRAME_TIMESTAMPS, data, &reply);
708         if (result != NO_ERROR) {
709             ALOGE("IGBP::getFrameTimestamps failed to transact: %d", result);
710             return;
711         }
712         result = reply.read(*outDelta);
713         if (result != NO_ERROR) {
714             ALOGE("IGBP::getFrameTimestamps failed to read timestamps: %d",
715                     result);
716         }
717     }
718 
getUniqueId(uint64_t * outId) const719     virtual status_t getUniqueId(uint64_t* outId) const {
720         Parcel data, reply;
721         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
722         status_t result = remote()->transact(GET_UNIQUE_ID, data, &reply);
723         if (result != NO_ERROR) {
724             ALOGE("getUniqueId failed to transact: %d", result);
725         }
726         status_t actualResult = NO_ERROR;
727         result = reply.readInt32(&actualResult);
728         if (result != NO_ERROR) {
729             return result;
730         }
731         result = reply.readUint64(outId);
732         if (result != NO_ERROR) {
733             return result;
734         }
735         return actualResult;
736     }
737 
getConsumerUsage(uint64_t * outUsage) const738     virtual status_t getConsumerUsage(uint64_t* outUsage) const {
739         Parcel data, reply;
740         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
741         status_t result = remote()->transact(GET_CONSUMER_USAGE, data, &reply);
742         if (result != NO_ERROR) {
743             ALOGE("getConsumerUsage failed to transact: %d", result);
744         }
745         status_t actualResult = NO_ERROR;
746         result = reply.readInt32(&actualResult);
747         if (result != NO_ERROR) {
748             return result;
749         }
750         result = reply.readUint64(outUsage);
751         if (result != NO_ERROR) {
752             return result;
753         }
754         return actualResult;
755     }
756 
setAutoPrerotation(bool autoPrerotation)757     virtual status_t setAutoPrerotation(bool autoPrerotation) {
758         Parcel data, reply;
759         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
760         data.writeBool(autoPrerotation);
761         status_t result = remote()->transact(SET_AUTO_PREROTATION, data, &reply);
762         if (result == NO_ERROR) {
763             result = reply.readInt32();
764         }
765         return result;
766     }
767 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
setFrameRate(float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy)768     virtual status_t setFrameRate(float frameRate, int8_t compatibility,
769                                   int8_t changeFrameRateStrategy) override {
770         Parcel data, reply;
771         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
772         data.writeFloat(frameRate);
773         data.writeInt32(compatibility);
774         data.writeInt32(changeFrameRateStrategy);
775         status_t result = remote()->transact(SET_FRAME_RATE, data, &reply);
776         if (result == NO_ERROR) {
777             result = reply.readInt32();
778         }
779         return result;
780     }
781 #endif
782 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
setAdditionalOptions(const std::vector<gui::AdditionalOptions> & options)783     virtual status_t setAdditionalOptions(const std::vector<gui::AdditionalOptions>& options) {
784         Parcel data, reply;
785         data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
786         if (options.size() > 100) {
787             return BAD_VALUE;
788         }
789         data.writeInt32(options.size());
790         for (const auto& it : options) {
791             data.writeCString(it.name.c_str());
792             data.writeInt64(it.value);
793         }
794         status_t result = remote()->transact(SET_ADDITIONAL_OPTIONS, data, &reply);
795         if (result == NO_ERROR) {
796             result = reply.readInt32();
797         }
798         return result;
799     }
800 #endif
801 };
802 
803 // Out-of-line virtual method definition to trigger vtable emission in this
804 // translation unit (see clang warning -Wweak-vtables)
~BpGraphicBufferProducer()805 BpGraphicBufferProducer::~BpGraphicBufferProducer() {}
806 
807 class HpGraphicBufferProducer : public HpInterface<
808         BpGraphicBufferProducer,
809         H2BGraphicBufferProducerV1_0,
810         H2BGraphicBufferProducerV2_0> {
811 public:
HpGraphicBufferProducer(const sp<IBinder> & base)812     explicit HpGraphicBufferProducer(const sp<IBinder>& base) : PBase(base) {}
813 
requestBuffer(int slot,sp<GraphicBuffer> * buf)814     status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) override {
815         return mBase->requestBuffer(slot, buf);
816     }
817 
requestBuffers(const std::vector<int32_t> & slots,std::vector<RequestBufferOutput> * outputs)818     status_t requestBuffers(
819             const std::vector<int32_t>& slots,
820             std::vector<RequestBufferOutput>* outputs) override {
821         return mBase->requestBuffers(slots, outputs);
822     }
823 
setMaxDequeuedBufferCount(int maxDequeuedBuffers)824     status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) override {
825         return mBase->setMaxDequeuedBufferCount(maxDequeuedBuffers);
826     }
827 
setAsyncMode(bool async)828     status_t setAsyncMode(bool async) override {
829         return mBase->setAsyncMode(async);
830     }
831 
dequeueBuffer(int * slot,sp<Fence> * fence,uint32_t w,uint32_t h,PixelFormat format,uint64_t usage,uint64_t * outBufferAge,FrameEventHistoryDelta * outTimestamps)832     status_t dequeueBuffer(int* slot, sp<Fence>* fence, uint32_t w, uint32_t h, PixelFormat format,
833                            uint64_t usage, uint64_t* outBufferAge,
834                            FrameEventHistoryDelta* outTimestamps) override {
835         return mBase->dequeueBuffer(slot, fence, w, h, format, usage, outBufferAge, outTimestamps);
836     }
837 
dequeueBuffers(const std::vector<DequeueBufferInput> & inputs,std::vector<DequeueBufferOutput> * outputs)838     status_t dequeueBuffers(
839             const std::vector<DequeueBufferInput>& inputs,
840             std::vector<DequeueBufferOutput>* outputs) override {
841         return mBase->dequeueBuffers(inputs, outputs);
842     }
843 
detachBuffer(int slot)844     status_t detachBuffer(int slot) override {
845         return mBase->detachBuffer(slot);
846     }
847 
detachBuffers(const std::vector<int32_t> & slots,std::vector<status_t> * results)848     status_t detachBuffers(const std::vector<int32_t>& slots,
849                            std::vector<status_t>* results) override {
850         return mBase->detachBuffers(slots, results);
851     }
852 
detachNextBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence)853     status_t detachNextBuffer(
854             sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) override {
855         return mBase->detachNextBuffer(outBuffer, outFence);
856     }
857 
attachBuffer(int * outSlot,const sp<GraphicBuffer> & buffer)858     status_t attachBuffer(
859             int* outSlot, const sp<GraphicBuffer>& buffer) override {
860         return mBase->attachBuffer(outSlot, buffer);
861     }
862 
attachBuffers(const std::vector<sp<GraphicBuffer>> & buffers,std::vector<AttachBufferOutput> * outputs)863     status_t attachBuffers(
864             const std::vector<sp<GraphicBuffer>>& buffers,
865             std::vector<AttachBufferOutput>* outputs) override {
866         return mBase->attachBuffers(buffers, outputs);
867     }
868 
queueBuffer(int slot,const QueueBufferInput & input,QueueBufferOutput * output)869     status_t queueBuffer(
870             int slot,
871             const QueueBufferInput& input,
872             QueueBufferOutput* output) override {
873         return mBase->queueBuffer(slot, input, output);
874     }
875 
queueBuffers(const std::vector<QueueBufferInput> & inputs,std::vector<QueueBufferOutput> * outputs)876     status_t queueBuffers(const std::vector<QueueBufferInput>& inputs,
877                           std::vector<QueueBufferOutput>* outputs) override {
878         return mBase->queueBuffers(inputs, outputs);
879     }
880 
cancelBuffer(int slot,const sp<Fence> & fence)881     status_t cancelBuffer(int slot, const sp<Fence>& fence) override {
882         return mBase->cancelBuffer(slot, fence);
883     }
884 
cancelBuffers(const std::vector<CancelBufferInput> & inputs,std::vector<status_t> * results)885     status_t cancelBuffers(
886             const std::vector<CancelBufferInput>& inputs,
887             std::vector<status_t>* results) override {
888         return mBase->cancelBuffers(inputs, results);
889     }
890 
query(int what,int * value)891     int query(int what, int* value) override {
892         return mBase->query(what, value);
893     }
894 
query(const std::vector<int32_t> inputs,std::vector<QueryOutput> * outputs)895     status_t query(const std::vector<int32_t> inputs,
896                    std::vector<QueryOutput>* outputs) override {
897         return mBase->query(inputs, outputs);
898     }
899 
connect(const sp<IProducerListener> & listener,int api,bool producerControlledByApp,QueueBufferOutput * output)900     status_t connect(
901             const sp<IProducerListener>& listener,
902             int api, bool producerControlledByApp,
903             QueueBufferOutput* output) override {
904         return mBase->connect(listener, api, producerControlledByApp, output);
905     }
906 
disconnect(int api,DisconnectMode mode=DisconnectMode::Api)907     status_t disconnect(
908             int api, DisconnectMode mode = DisconnectMode::Api) override {
909         return mBase->disconnect(api, mode);
910     }
911 
setSidebandStream(const sp<NativeHandle> & stream)912     status_t setSidebandStream(const sp<NativeHandle>& stream) override {
913         return mBase->setSidebandStream(stream);
914     }
915 
allocateBuffers(uint32_t width,uint32_t height,PixelFormat format,uint64_t usage)916     void allocateBuffers(uint32_t width, uint32_t height,
917             PixelFormat format, uint64_t usage) override {
918         return mBase->allocateBuffers(width, height, format, usage);
919     }
920 
allowAllocation(bool allow)921     status_t allowAllocation(bool allow) override {
922         return mBase->allowAllocation(allow);
923     }
924 
setGenerationNumber(uint32_t generationNumber)925     status_t setGenerationNumber(uint32_t generationNumber) override {
926         return mBase->setGenerationNumber(generationNumber);
927     }
928 
getConsumerName() const929     String8 getConsumerName() const override {
930         return mBase->getConsumerName();
931     }
932 
setSharedBufferMode(bool sharedBufferMode)933     status_t setSharedBufferMode(bool sharedBufferMode) override {
934         return mBase->setSharedBufferMode(sharedBufferMode);
935     }
936 
setAutoRefresh(bool autoRefresh)937     status_t setAutoRefresh(bool autoRefresh) override {
938         return mBase->setAutoRefresh(autoRefresh);
939     }
940 
setDequeueTimeout(nsecs_t timeout)941     status_t setDequeueTimeout(nsecs_t timeout) override {
942         return mBase->setDequeueTimeout(timeout);
943     }
944 
setLegacyBufferDrop(bool drop)945     status_t setLegacyBufferDrop(bool drop) override {
946         return mBase->setLegacyBufferDrop(drop);
947     }
948 
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,float outTransformMatrix[16])949     status_t getLastQueuedBuffer(
950             sp<GraphicBuffer>* outBuffer,
951             sp<Fence>* outFence,
952             float outTransformMatrix[16]) override {
953         return mBase->getLastQueuedBuffer(
954                 outBuffer, outFence, outTransformMatrix);
955     }
956 
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,Rect * outRect,uint32_t * outTransform)957     status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence, Rect* outRect,
958                                  uint32_t* outTransform) override {
959         return mBase->getLastQueuedBuffer(outBuffer, outFence, outRect, outTransform);
960     }
961 
getFrameTimestamps(FrameEventHistoryDelta * outDelta)962     void getFrameTimestamps(FrameEventHistoryDelta* outDelta) override {
963         return mBase->getFrameTimestamps(outDelta);
964     }
965 
getUniqueId(uint64_t * outId) const966     status_t getUniqueId(uint64_t* outId) const override {
967         return mBase->getUniqueId(outId);
968     }
969 
getConsumerUsage(uint64_t * outUsage) const970     status_t getConsumerUsage(uint64_t* outUsage) const override {
971         return mBase->getConsumerUsage(outUsage);
972     }
973 
setAutoPrerotation(bool autoPrerotation)974     status_t setAutoPrerotation(bool autoPrerotation) override {
975         return mBase->setAutoPrerotation(autoPrerotation);
976     }
977 };
978 
979 IMPLEMENT_HYBRID_META_INTERFACE(GraphicBufferProducer,
980         "android.gui.IGraphicBufferProducer");
981 
982 // ----------------------------------------------------------------------
983 
setLegacyBufferDrop(bool drop)984 status_t IGraphicBufferProducer::setLegacyBufferDrop(bool drop) {
985     // No-op for IGBP other than BufferQueue.
986     (void) drop;
987     return INVALID_OPERATION;
988 }
989 
setAutoPrerotation(bool autoPrerotation)990 status_t IGraphicBufferProducer::setAutoPrerotation(bool autoPrerotation) {
991     // No-op for IGBP other than BufferQueue.
992     (void)autoPrerotation;
993     return INVALID_OPERATION;
994 }
995 
996 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
setFrameRate(float,int8_t,int8_t)997 status_t IGraphicBufferProducer::setFrameRate(float /*frameRate*/, int8_t /*compatibility*/,
998                                               int8_t /*changeFrameRateStrategy*/) {
999     // No-op for IGBP other than BufferQueue.
1000     return INVALID_OPERATION;
1001 }
1002 #endif
1003 
1004 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
setAdditionalOptions(const std::vector<gui::AdditionalOptions> &)1005 status_t IGraphicBufferProducer::setAdditionalOptions(const std::vector<gui::AdditionalOptions>&) {
1006     // No-op for IGBP other than BufferQueue.
1007     return INVALID_OPERATION;
1008 }
1009 #endif
1010 
exportToParcel(Parcel * parcel)1011 status_t IGraphicBufferProducer::exportToParcel(Parcel* parcel) {
1012     status_t res = OK;
1013     res = parcel->writeUint32(USE_BUFFER_QUEUE);
1014     if (res != NO_ERROR) {
1015         ALOGE("exportToParcel: Cannot write magic, res=%d.", res);
1016         return res;
1017     }
1018 
1019     return parcel->writeStrongBinder(IInterface::asBinder(this));
1020 }
1021 
1022 /* static */
exportToParcel(const sp<IGraphicBufferProducer> & producer,Parcel * parcel)1023 status_t IGraphicBufferProducer::exportToParcel(const sp<IGraphicBufferProducer>& producer,
1024                                                 Parcel* parcel) {
1025     if (parcel == nullptr) {
1026         ALOGE("exportToParcel: Invalid parcel object.");
1027         return BAD_VALUE;
1028     }
1029 
1030     if (producer == nullptr) {
1031         status_t res = OK;
1032         res = parcel->writeUint32(IGraphicBufferProducer::USE_BUFFER_QUEUE);
1033         if (res != NO_ERROR) return res;
1034         return parcel->writeStrongBinder(nullptr);
1035     } else {
1036         return producer->exportToParcel(parcel);
1037     }
1038 }
1039 
1040 /* static */
createFromParcel(const Parcel * parcel)1041 sp<IGraphicBufferProducer> IGraphicBufferProducer::createFromParcel(const Parcel* parcel) {
1042     uint32_t outMagic = 0;
1043     status_t res = NO_ERROR;
1044 
1045     res = parcel->readUint32(&outMagic);
1046     if (res != NO_ERROR) {
1047         ALOGE("createFromParcel: Failed to read magic, error=%d.", res);
1048         return nullptr;
1049     }
1050 
1051     switch (outMagic) {
1052         case USE_BUFFER_QUEUE: {
1053             sp<IBinder> binder;
1054             res = parcel->readNullableStrongBinder(&binder);
1055             if (res != NO_ERROR) {
1056                 ALOGE("createFromParcel: Can't read strong binder.");
1057                 return nullptr;
1058             }
1059             return interface_cast<IGraphicBufferProducer>(binder);
1060         }
1061         case USE_BUFFER_HUB: {
1062             ALOGE("createFromParcel: BufferHub not implemented.");
1063             return nullptr;
1064         }
1065         default: {
1066             ALOGE("createFromParcel: Unexpected mgaic: 0x%x.", outMagic);
1067             return nullptr;
1068         }
1069     }
1070 }
1071 
1072 // ----------------------------------------------------------------------------
1073 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)1074 status_t BnGraphicBufferProducer::onTransact(
1075     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1076 {
1077     switch(code) {
1078         case REQUEST_BUFFER: {
1079             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1080             int bufferIdx = data.readInt32();
1081             sp<GraphicBuffer> buffer;
1082             int result = requestBuffer(bufferIdx, &buffer);
1083             reply->writeInt32(buffer != nullptr);
1084             if (buffer != nullptr) {
1085                 reply->write(*buffer);
1086             }
1087             reply->writeInt32(result);
1088             return NO_ERROR;
1089         }
1090         case REQUEST_BUFFERS: {
1091             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1092             std::vector<int32_t> slots;
1093             std::vector<RequestBufferOutput> outputs;
1094             status_t result = data.readInt32Vector(&slots);
1095             if (result != NO_ERROR) {
1096                 return result;
1097             }
1098             (void)requestBuffers(slots, &outputs);
1099             result = reply->writeVectorSize(outputs);
1100             for (const RequestBufferOutput& output : outputs) {
1101                 if (result != NO_ERROR) {
1102                     return result;
1103                 }
1104                 result = reply->write(output);
1105             }
1106             return result;
1107         }
1108         case SET_MAX_DEQUEUED_BUFFER_COUNT: {
1109             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1110             int maxDequeuedBuffers = data.readInt32();
1111             int result = setMaxDequeuedBufferCount(maxDequeuedBuffers);
1112             reply->writeInt32(result);
1113             return NO_ERROR;
1114         }
1115         case SET_ASYNC_MODE: {
1116             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1117             bool async = data.readInt32();
1118             int result = setAsyncMode(async);
1119             reply->writeInt32(result);
1120             return NO_ERROR;
1121         }
1122         case DEQUEUE_BUFFER: {
1123             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1124             uint32_t width = data.readUint32();
1125             uint32_t height = data.readUint32();
1126             PixelFormat format = static_cast<PixelFormat>(data.readInt32());
1127             uint64_t usage = data.readUint64();
1128             uint64_t bufferAge = 0;
1129             bool getTimestamps = data.readBool();
1130 
1131             int buf = 0;
1132             sp<Fence> fence = Fence::NO_FENCE;
1133             FrameEventHistoryDelta frameTimestamps;
1134             int result = dequeueBuffer(&buf, &fence, width, height, format, usage, &bufferAge,
1135                                        getTimestamps ? &frameTimestamps : nullptr);
1136 
1137             if (fence == nullptr) {
1138                 ALOGE("dequeueBuffer returned a NULL fence, setting to Fence::NO_FENCE");
1139                 fence = Fence::NO_FENCE;
1140             }
1141             reply->writeInt32(buf);
1142             reply->write(*fence);
1143             reply->writeUint64(bufferAge);
1144             if (getTimestamps) {
1145                 reply->write(frameTimestamps);
1146             }
1147             reply->writeInt32(result);
1148             return NO_ERROR;
1149         }
1150         case DEQUEUE_BUFFERS: {
1151             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1152             std::vector<DequeueBufferInput> inputs;
1153             std::vector<DequeueBufferOutput> outputs;
1154             status_t result = data.resizeOutVector(&inputs);
1155             if (result != NO_ERROR) {
1156                 return result;
1157             }
1158             for (DequeueBufferInput& input : inputs) {
1159                 result = data.read(input);
1160                 if (result != NO_ERROR) {
1161                     return result;
1162                 }
1163             }
1164             (void)dequeueBuffers(inputs, &outputs);
1165             result = reply->writeVectorSize(outputs);
1166             for (const DequeueBufferOutput& output : outputs) {
1167                 if (result != NO_ERROR) {
1168                     return result;
1169                 }
1170                 result = reply->write(output);
1171             }
1172             return result;
1173         }
1174         case DETACH_BUFFER: {
1175             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1176             int slot = data.readInt32();
1177             int result = detachBuffer(slot);
1178             reply->writeInt32(result);
1179             return NO_ERROR;
1180         }
1181         case DETACH_BUFFERS: {
1182             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1183             std::vector<int32_t> slots;
1184             std::vector<status_t> results;
1185             status_t result = data.readInt32Vector(&slots);
1186             if (result != NO_ERROR) {
1187                 return result;
1188             }
1189             (void)detachBuffers(slots, &results);
1190             return reply->writeInt32Vector(results);
1191         }
1192         case DETACH_NEXT_BUFFER: {
1193             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1194             sp<GraphicBuffer> buffer;
1195             sp<Fence> fence;
1196             int32_t result = detachNextBuffer(&buffer, &fence);
1197             reply->writeInt32(result);
1198             if (result == NO_ERROR) {
1199                 reply->writeInt32(buffer != nullptr);
1200                 if (buffer != nullptr) {
1201                     reply->write(*buffer);
1202                 }
1203                 reply->writeInt32(fence != nullptr);
1204                 if (fence != nullptr) {
1205                     reply->write(*fence);
1206                 }
1207             }
1208             return NO_ERROR;
1209         }
1210         case ATTACH_BUFFER: {
1211             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1212             sp<GraphicBuffer> buffer = new GraphicBuffer();
1213             status_t result = data.read(*buffer.get());
1214             int slot = 0;
1215             if (result == NO_ERROR) {
1216                 result = attachBuffer(&slot, buffer);
1217             }
1218             reply->writeInt32(slot);
1219             reply->writeInt32(result);
1220             return NO_ERROR;
1221         }
1222         case ATTACH_BUFFERS: {
1223             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1224             std::vector<sp<GraphicBuffer>> buffers;
1225             status_t result = data.resizeOutVector(&buffers);
1226             if (result != NO_ERROR) {
1227                 return result;
1228             }
1229             for (sp<GraphicBuffer>& buffer : buffers) {
1230                 buffer = new GraphicBuffer();
1231                 result = data.read(*buffer.get());
1232                 if (result != NO_ERROR) {
1233                     return result;
1234                 }
1235             }
1236             std::vector<AttachBufferOutput> outputs;
1237             (void)attachBuffers(buffers, &outputs);
1238             result = reply->writeVectorSize(outputs);
1239             for (const AttachBufferOutput& output : outputs) {
1240                 if (result != NO_ERROR) {
1241                     return result;
1242                 }
1243                 result = reply->write(output);
1244             }
1245             return result;
1246         }
1247         case QUEUE_BUFFER: {
1248             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1249 
1250             int buf = data.readInt32();
1251             QueueBufferInput input(data);
1252             QueueBufferOutput output;
1253             status_t result = queueBuffer(buf, input, &output);
1254             reply->write(output);
1255             reply->writeInt32(result);
1256 
1257             return NO_ERROR;
1258         }
1259         case QUEUE_BUFFERS: {
1260             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1261             std::vector<QueueBufferInput> inputs;
1262             status_t result = data.resizeOutVector(&inputs);
1263             if (result != NO_ERROR) {
1264                 return result;
1265             }
1266             for (QueueBufferInput& input : inputs) {
1267                 result = data.read(input);
1268                 if (result != NO_ERROR) {
1269                     return result;
1270                 }
1271             }
1272             std::vector<QueueBufferOutput> outputs;
1273             (void)queueBuffers(inputs, &outputs);
1274             result = reply->writeVectorSize(outputs);
1275             for (const QueueBufferOutput& output : outputs) {
1276                 if (result != NO_ERROR) {
1277                     return result;
1278                 }
1279                 result = reply->write(output);
1280             }
1281             return result;
1282         }
1283         case CANCEL_BUFFER: {
1284             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1285             int buf = data.readInt32();
1286             sp<Fence> fence = new Fence();
1287             status_t result = data.read(*fence.get());
1288             if (result == NO_ERROR) {
1289                 result = cancelBuffer(buf, fence);
1290             }
1291             reply->writeInt32(result);
1292             return NO_ERROR;
1293         }
1294         case CANCEL_BUFFERS: {
1295             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1296             std::vector<CancelBufferInput> inputs;
1297             status_t result = data.resizeOutVector(&inputs);
1298             for (CancelBufferInput& input : inputs) {
1299                 if (result != NO_ERROR) {
1300                     return result;
1301                 }
1302                 result = data.read(input);
1303             }
1304             if (result != NO_ERROR) {
1305                 return result;
1306             }
1307             std::vector<status_t> results;
1308             result = cancelBuffers(inputs, &results);
1309             if (result != NO_ERROR) {
1310                 return result;
1311             }
1312             return reply->writeInt32Vector(results);
1313         }
1314         case QUERY: {
1315             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1316             int value = 0;
1317             int what = data.readInt32();
1318             int res = query(what, &value);
1319             reply->writeInt32(value);
1320             reply->writeInt32(res);
1321             return NO_ERROR;
1322         }
1323         case QUERY_MULTIPLE: {
1324             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1325             std::vector<int32_t> inputs;
1326             status_t result = data.readInt32Vector(&inputs);
1327             if (result != NO_ERROR) {
1328                 return result;
1329             }
1330             std::vector<QueryOutput> outputs;
1331             result = query(inputs, &outputs);
1332             if (result != NO_ERROR) {
1333                 return result;
1334             }
1335             result = reply->writeVectorSize(outputs);
1336             for (const QueryOutput& output : outputs) {
1337                 if (result != NO_ERROR) {
1338                     return result;
1339                 }
1340                 result = reply->write(output);
1341             }
1342             return result;
1343         }
1344         case CONNECT: {
1345             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1346             sp<IProducerListener> listener;
1347             if (data.readInt32() == 1) {
1348                 listener = IProducerListener::asInterface(data.readStrongBinder());
1349             }
1350             int api = data.readInt32();
1351             bool producerControlledByApp = data.readInt32();
1352             QueueBufferOutput output;
1353             status_t res = connect(listener, api, producerControlledByApp, &output);
1354             reply->write(output);
1355             reply->writeInt32(res);
1356             return NO_ERROR;
1357         }
1358         case DISCONNECT: {
1359             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1360             int api = data.readInt32();
1361             DisconnectMode mode = static_cast<DisconnectMode>(data.readInt32());
1362             status_t res = disconnect(api, mode);
1363             reply->writeInt32(res);
1364             return NO_ERROR;
1365         }
1366         case SET_SIDEBAND_STREAM: {
1367             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1368             sp<NativeHandle> stream;
1369             if (data.readInt32()) {
1370                 stream = NativeHandle::create(data.readNativeHandle(), true);
1371             }
1372             status_t result = setSidebandStream(stream);
1373             reply->writeInt32(result);
1374             return NO_ERROR;
1375         }
1376         case ALLOCATE_BUFFERS: {
1377             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1378             uint32_t width = data.readUint32();
1379             uint32_t height = data.readUint32();
1380             PixelFormat format = static_cast<PixelFormat>(data.readInt32());
1381             uint64_t usage = data.readUint64();
1382             allocateBuffers(width, height, format, usage);
1383             return NO_ERROR;
1384         }
1385         case ALLOW_ALLOCATION: {
1386             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1387             bool allow = static_cast<bool>(data.readInt32());
1388             status_t result = allowAllocation(allow);
1389             reply->writeInt32(result);
1390             return NO_ERROR;
1391         }
1392         case SET_GENERATION_NUMBER: {
1393             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1394             uint32_t generationNumber = data.readUint32();
1395             status_t result = setGenerationNumber(generationNumber);
1396             reply->writeInt32(result);
1397             return NO_ERROR;
1398         }
1399         case GET_CONSUMER_NAME: {
1400             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1401             reply->writeString8(getConsumerName());
1402             return NO_ERROR;
1403         }
1404         case SET_SHARED_BUFFER_MODE: {
1405             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1406             bool sharedBufferMode = data.readInt32();
1407             status_t result = setSharedBufferMode(sharedBufferMode);
1408             reply->writeInt32(result);
1409             return NO_ERROR;
1410         }
1411         case SET_AUTO_REFRESH: {
1412             CHECK_INTERFACE(IGraphicBuffer, data, reply);
1413             bool autoRefresh = data.readInt32();
1414             status_t result = setAutoRefresh(autoRefresh);
1415             reply->writeInt32(result);
1416             return NO_ERROR;
1417         }
1418         case SET_DEQUEUE_TIMEOUT: {
1419             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1420             nsecs_t timeout = data.readInt64();
1421             status_t result = setDequeueTimeout(timeout);
1422             reply->writeInt32(result);
1423             return NO_ERROR;
1424         }
1425         case GET_LAST_QUEUED_BUFFER: {
1426             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1427             sp<GraphicBuffer> buffer(nullptr);
1428             sp<Fence> fence(Fence::NO_FENCE);
1429             float transform[16] = {};
1430             status_t result = getLastQueuedBuffer(&buffer, &fence, transform);
1431             reply->writeInt32(result);
1432             if (result != NO_ERROR) {
1433                 return result;
1434             }
1435             if (!buffer.get()) {
1436                 reply->writeBool(false);
1437             } else {
1438                 reply->writeBool(true);
1439                 result = reply->write(*buffer);
1440                 if (result == NO_ERROR) {
1441                     reply->write(transform, sizeof(float) * 16);
1442                 }
1443             }
1444             if (result != NO_ERROR) {
1445                 ALOGE("getLastQueuedBuffer failed to write buffer: %d", result);
1446                 return result;
1447             }
1448             if (fence == nullptr) {
1449                 ALOGE("getLastQueuedBuffer returned a NULL fence, setting to Fence::NO_FENCE");
1450                 fence = Fence::NO_FENCE;
1451             }
1452             result = reply->write(*fence);
1453             if (result != NO_ERROR) {
1454                 ALOGE("getLastQueuedBuffer failed to write fence: %d", result);
1455                 return result;
1456             }
1457             return NO_ERROR;
1458         }
1459         case GET_LAST_QUEUED_BUFFER2: {
1460             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1461             sp<GraphicBuffer> buffer(nullptr);
1462             sp<Fence> fence(Fence::NO_FENCE);
1463             Rect crop;
1464             uint32_t transform;
1465             status_t result = getLastQueuedBuffer(&buffer, &fence, &crop, &transform);
1466             reply->writeInt32(result);
1467             if (result != NO_ERROR) {
1468                 return result;
1469             }
1470             if (!buffer.get()) {
1471                 reply->writeBool(false);
1472             } else {
1473                 reply->writeBool(true);
1474                 result = reply->write(*buffer);
1475                 if (result == NO_ERROR) {
1476                     result = reply->write(crop);
1477                 }
1478                 if (result == NO_ERROR) {
1479                     result = reply->writeUint32(transform);
1480                 }
1481             }
1482             if (result != NO_ERROR) {
1483                 ALOGE("getLastQueuedBuffer failed to write buffer: %d", result);
1484                 return result;
1485             }
1486             if (fence == nullptr) {
1487                 ALOGE("getLastQueuedBuffer returned a NULL fence, setting to Fence::NO_FENCE");
1488                 fence = Fence::NO_FENCE;
1489             }
1490             result = reply->write(*fence);
1491             if (result != NO_ERROR) {
1492                 ALOGE("getLastQueuedBuffer failed to write fence: %d", result);
1493                 return result;
1494             }
1495             return NO_ERROR;
1496         }
1497 
1498         case GET_FRAME_TIMESTAMPS: {
1499             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1500             FrameEventHistoryDelta frameTimestamps;
1501             getFrameTimestamps(&frameTimestamps);
1502             status_t result = reply->write(frameTimestamps);
1503             if (result != NO_ERROR) {
1504                 ALOGE("BnGBP::GET_FRAME_TIMESTAMPS failed to write buffer: %d",
1505                         result);
1506                 return result;
1507             }
1508             return NO_ERROR;
1509         }
1510         case GET_UNIQUE_ID: {
1511             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1512             uint64_t outId = 0;
1513             status_t actualResult = getUniqueId(&outId);
1514             status_t result = reply->writeInt32(actualResult);
1515             if (result != NO_ERROR) {
1516                 return result;
1517             }
1518             result = reply->writeUint64(outId);
1519             if (result != NO_ERROR) {
1520                 return result;
1521             }
1522             return NO_ERROR;
1523         }
1524         case GET_CONSUMER_USAGE: {
1525             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1526             uint64_t outUsage = 0;
1527             status_t actualResult = getConsumerUsage(&outUsage);
1528             status_t result = reply->writeInt32(actualResult);
1529             if (result != NO_ERROR) {
1530                 return result;
1531             }
1532             result = reply->writeUint64(outUsage);
1533             if (result != NO_ERROR) {
1534                 return result;
1535             }
1536             return NO_ERROR;
1537         }
1538         case SET_LEGACY_BUFFER_DROP: {
1539             CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
1540             bool drop = data.readInt32();
1541             int result = setLegacyBufferDrop(drop);
1542             reply->writeInt32(result);
1543             return NO_ERROR;
1544         }
1545         case SET_AUTO_PREROTATION: {
1546             CHECK_INTERFACE(IGraphicBuffer, data, reply);
1547             bool autoPrerotation = data.readBool();
1548             status_t result = setAutoPrerotation(autoPrerotation);
1549             reply->writeInt32(result);
1550             return NO_ERROR;
1551         }
1552 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
1553         case SET_FRAME_RATE: {
1554             CHECK_INTERFACE(IGraphicBuffer, data, reply);
1555             float frameRate = data.readFloat();
1556             int8_t compatibility = data.readInt32();
1557             int8_t changeFrameRateStrategy = data.readInt32();
1558             status_t result = setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1559             reply->writeInt32(result);
1560             return NO_ERROR;
1561         }
1562 #endif
1563 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1564         case SET_ADDITIONAL_OPTIONS: {
1565             CHECK_INTERFACE(IGraphicBuffer, data, reply);
1566             int optionCount = data.readInt32();
1567             if (optionCount < 0 || optionCount > 100) {
1568                 return BAD_VALUE;
1569             }
1570             std::vector<gui::AdditionalOptions> opts;
1571             opts.reserve(optionCount);
1572             for (int i = 0; i < optionCount; i++) {
1573                 const char* name = data.readCString();
1574                 int64_t value = 0;
1575                 if (name == nullptr || data.readInt64(&value) != NO_ERROR) {
1576                     return BAD_VALUE;
1577                 }
1578                 opts.emplace_back(name, value);
1579             }
1580             status_t result = setAdditionalOptions(opts);
1581             reply->writeInt32(result);
1582             return NO_ERROR;
1583         }
1584 #endif
1585     }
1586     return BBinder::onTransact(code, data, reply, flags);
1587 }
1588 
1589 }; // namespace android
1590