1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <EGL/egl.h>
20 #include <EGL/eglext.h>
21 #include <cutils/compiler.h>
22 #include <gui/BufferItem.h>
23 #include <gui/BufferQueueDefs.h>
24 #include <sys/cdefs.h>
25 #include <system/graphics.h>
26 
27 namespace android {
28 
29 class SurfaceTexture;
30 class DequeueBufferCallbacks;
31 
32 /*
33  * ImageConsumer implements the parts of SurfaceTexture that deal with
34  * images consumed by HWUI view system.
35  */
36 class ImageConsumer {
37 public:
38     typedef status_t (*SurfaceTexture_createReleaseFence)(bool useFenceSync, EGLSyncKHR* eglFence,
39                                                           EGLDisplay* display, int* releaseFence,
40                                                           void* fencePassThroughHandle);
41 
42     typedef status_t (*SurfaceTexture_fenceWait)(int fence, void* fencePassThroughHandle);
43 
44     sp<GraphicBuffer> dequeueBuffer(int* outSlotid, android_dataspace* outDataspace,
45                                     HdrMetadata* outHdrMetadata, bool* outQueueEmpty,
46                                     SurfaceTexture& cb,
47                                     SurfaceTexture_createReleaseFence createFence,
48                                     SurfaceTexture_fenceWait fenceWait,
49                                     void* fencePassThroughHandle);
50 
51     /**
52      * onReleaseBufferLocked amends the ConsumerBase method to update the
53      * mImageSlots array in addition to the ConsumerBase.
54      */
55     void onReleaseBufferLocked(int slot);
56 
57 private:
58     /**
59      * ImageSlot contains the information and object references that
60      * ImageConsumer maintains about a BufferQueue buffer slot.
61      */
62     class ImageSlot {
63     public:
ImageSlot()64         ImageSlot() : mEglFence(EGL_NO_SYNC_KHR) {}
65 
eglFence()66         inline EGLSyncKHR& eglFence() { return mEglFence; }
67 
68     private:
69         /**
70          * mEglFence is the EGL sync object that must signal before the buffer
71          * associated with this buffer slot may be dequeued.
72          */
73         EGLSyncKHR mEglFence;
74     };
75 
76     /**
77      * ImageConsumer stores the SkImages that have been allocated by the BufferQueue
78      * for each buffer slot.  It is initialized to null pointers, and gets
79      * filled in with the result of BufferQueue::acquire when the
80      * client dequeues a buffer from a
81      * slot that has not yet been used. The buffer allocated to a slot will also
82      * be replaced if the requested buffer usage or geometry differs from that
83      * of the buffer allocated to a slot.
84      */
85     ImageSlot mImageSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
86 };
87 
88 } /* namespace android */
89