• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 #ifndef CODEC2_HIDL_V1_0_UTILS_OUTPUT_BUFFER_QUEUE
18 #define CODEC2_HIDL_V1_0_UTILS_OUTPUT_BUFFER_QUEUE
19 
20 #include <gui/FrameTimestamps.h>
21 #include <gui/IGraphicBufferProducer.h>
22 #include <codec2/hidl/1.0/types.h>
23 #include <codec2/hidl/1.2/types.h>
24 #include <C2Work.h>
25 
26 struct C2_HIDE _C2BlockPoolData;
27 class C2SurfaceSyncMemory;
28 
29 namespace android {
30 namespace hardware {
31 namespace media {
32 namespace c2 {
33 
34 
35 // BufferQueue-Based Block Operations
36 // ==================================
37 
38 // Manage BufferQueue and graphic blocks for both component and codec.
39 // Manage graphic blocks ownership consistently during surface change.
40 struct OutputBufferQueue {
41 
42     OutputBufferQueue();
43 
44     ~OutputBufferQueue();
45 
46     // Configure a new surface to render graphic blocks.
47     // Graphic blocks from older surface will be migrated to new surface.
48     bool configure(const sp<IGraphicBufferProducer>& igbp,
49                    uint32_t generation,
50                    uint64_t bqId,
51                    int maxDequeueBufferCount,
52                    std::shared_ptr<V1_2::SurfaceSyncObj> *syncObj);
53 
54     // If there are waiters to allocate from the old surface, wake up and expire
55     // them.
56     void expireOldWaiters();
57 
58     // Stop using the current output surface. Pending buffer opeations will not
59     // perform anymore.
60     void stop();
61 
62     // Render a graphic block to current surface.
63     status_t outputBuffer(
64             const C2ConstGraphicBlock& block,
65             const BnGraphicBufferProducer::QueueBufferInput& input,
66             BnGraphicBufferProducer::QueueBufferOutput* output);
67 
68     // Retrieve frame event history from the output surface.
69     void pollForRenderedFrames(FrameEventHistoryDelta* delta);
70 
71     // Call holdBufferQueueBlock() on output blocks in the given workList.
72     // The OutputBufferQueue will take the ownership of output blocks.
73     //
74     // Note: This function should be called after WorkBundle has been received
75     // from another process.
76     void holdBufferQueueBlocks(
77             const std::list<std::unique_ptr<C2Work>>& workList);
78 
79     // Update # of max dequeue buffer from BQ. If # of max dequeued buffer is shared
80     // via shared memory between HAL and framework, Update # of max dequeued buffer
81     // and synchronize.
82     void updateMaxDequeueBufferCount(int maxDequeueBufferCount);
83 
84 private:
85 
86     std::mutex mMutex;
87     sp<IGraphicBufferProducer> mIgbp;
88     uint32_t mGeneration;
89     uint64_t mBqId;
90     int32_t mMaxDequeueBufferCount;
91     std::shared_ptr<int> mOwner;
92     // To migrate existing buffers
93     sp<GraphicBuffer> mBuffers[BufferQueueDefs::NUM_BUFFER_SLOTS]; // find a better way
94     std::weak_ptr<_C2BlockPoolData> mPoolDatas[BufferQueueDefs::NUM_BUFFER_SLOTS];
95     std::shared_ptr<C2SurfaceSyncMemory> mSyncMem;
96     bool mStopped;
97     std::mutex mOldMutex;
98     std::shared_ptr<C2SurfaceSyncMemory> mOldMem;
99 
100     bool registerBuffer(const C2ConstGraphicBlock& block);
101 };
102 
103 }  // namespace c2
104 }  // namespace media
105 }  // namespace hardware
106 }  // namespace android
107 
108 #endif  // CODEC2_HIDL_V1_0_UTILS_OUTPUT_BUFFER_QUEUE
109