| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 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 | #pragma once | 
|  | 18 |  | 
|  | 19 | #include "BufferLayer.h" | 
|  | 20 |  | 
|  | 21 | #include <utils/String8.h> | 
|  | 22 |  | 
|  | 23 | namespace android { | 
|  | 24 |  | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 25 | namespace frametimeline { | 
|  | 26 | class SurfaceFrame; | 
|  | 27 | } | 
|  | 28 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 29 | /* | 
|  | 30 | * A new BufferQueue and a new BufferLayerConsumer are created when the | 
|  | 31 | * BufferLayer is first referenced. | 
|  | 32 | * | 
|  | 33 | * This also implements onFrameAvailable(), which notifies SurfaceFlinger | 
|  | 34 | * that new data has arrived. | 
|  | 35 | */ | 
| chaviw | 2d9a429 | 2019-12-02 16:00:24 -0800 | [diff] [blame] | 36 | class BufferQueueLayer : public BufferLayer { | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 37 | public: | 
| chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 38 | // Only call while mStateLock is held | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 39 | explicit BufferQueueLayer(const LayerCreationArgs&); | 
|  | 40 | ~BufferQueueLayer() override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 41 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 42 | // Implements Layer. | 
| chaviw | 8a01fa4 | 2019-08-19 12:39:31 -0700 | [diff] [blame] | 43 | const char* getType() const override { return "BufferQueueLayer"; } | 
|  | 44 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 45 | void onLayerDisplayed(const sp<Fence>& releaseFence) override; | 
|  | 46 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 47 | std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override; | 
|  | 48 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 49 | // If a buffer was replaced this frame, release the former buffer | 
|  | 50 | void releasePendingBuffer(nsecs_t dequeueReadyTime) override; | 
|  | 51 |  | 
|  | 52 | void setDefaultBufferSize(uint32_t w, uint32_t h) override; | 
|  | 53 |  | 
|  | 54 | int32_t getQueuedFrameCount() const override; | 
|  | 55 |  | 
| Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 56 | bool shouldPresentNow(nsecs_t expectedPresentTime) const override; | 
| Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 57 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 58 | // Implements BufferLayer. | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 59 | bool fenceHasSignaled() const override; | 
| Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 60 | bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 61 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 62 | status_t setDefaultBufferProperties(uint32_t w, uint32_t h, PixelFormat format); | 
|  | 63 | sp<IGraphicBufferProducer> getProducer() const; | 
| chaviw | 2d9a429 | 2019-12-02 16:00:24 -0800 | [diff] [blame] | 64 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 65 | protected: | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 66 | void gatherBufferInfo() override; | 
|  | 67 |  | 
| chaviw | 2d9a429 | 2019-12-02 16:00:24 -0800 | [diff] [blame] | 68 | // ----------------------------------------------------------------------- | 
|  | 69 | // Interface implementation for BufferLayerConsumer::ContentsChangedListener | 
|  | 70 | // ----------------------------------------------------------------------- | 
|  | 71 | class ContentsChangedListener : public BufferLayerConsumer::ContentsChangedListener { | 
|  | 72 | public: | 
|  | 73 | ContentsChangedListener(BufferQueueLayer* bufferQueueLayer) | 
|  | 74 | : mBufferQueueLayer(bufferQueueLayer) {} | 
|  | 75 | void abandon(); | 
|  | 76 |  | 
|  | 77 | protected: | 
|  | 78 | void onFrameAvailable(const BufferItem& item) override; | 
|  | 79 | void onFrameReplaced(const BufferItem& item) override; | 
|  | 80 | void onSidebandStreamChanged() override; | 
|  | 81 | void onFrameDequeued(const uint64_t bufferId) override; | 
|  | 82 | void onFrameDetached(const uint64_t bufferId) override; | 
|  | 83 | void onFrameCancelled(const uint64_t bufferId) override; | 
|  | 84 |  | 
|  | 85 | private: | 
|  | 86 | BufferQueueLayer* mBufferQueueLayer = nullptr; | 
|  | 87 | Mutex mMutex; | 
|  | 88 | }; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 89 |  | 
|  | 90 | private: | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 91 | uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override; | 
|  | 92 |  | 
|  | 93 | bool getAutoRefresh() const override; | 
|  | 94 | bool getSidebandStreamChanged() const override; | 
|  | 95 |  | 
|  | 96 | bool latchSidebandStream(bool& recomputeVisibleRegions) override; | 
|  | 97 | void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override; | 
|  | 98 |  | 
|  | 99 | bool hasFrameUpdate() const override; | 
|  | 100 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 101 | status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime, | 
|  | 102 | nsecs_t expectedPresentTime) override; | 
|  | 103 |  | 
|  | 104 | status_t updateActiveBuffer() override; | 
|  | 105 | status_t updateFrameNumber(nsecs_t latchTime) override; | 
|  | 106 |  | 
|  | 107 | sp<Layer> createClone() override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | void onFirstRef() override; | 
|  | 110 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 111 | void onFrameAvailable(const BufferItem& item); | 
|  | 112 | void onFrameReplaced(const BufferItem& item); | 
|  | 113 | void onSidebandStreamChanged(); | 
|  | 114 | void onFrameDequeued(const uint64_t bufferId); | 
|  | 115 | void onFrameDetached(const uint64_t bufferId); | 
|  | 116 | void onFrameCancelled(const uint64_t bufferId); | 
|  | 117 |  | 
|  | 118 | // Temporary - Used only for LEGACY camera mode. | 
|  | 119 | uint32_t getProducerStickyTransform() const; | 
|  | 120 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 121 | sp<BufferLayerConsumer> mConsumer; | 
|  | 122 | sp<IGraphicBufferProducer> mProducer; | 
|  | 123 |  | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 124 | bool mUpdateTexImageFailed{false}; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 125 |  | 
| Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 126 | uint64_t mPreviousBufferId = 0; | 
|  | 127 | uint64_t mPreviousReleasedFrameNumber = 0; | 
|  | 128 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 129 | // Local copy of the queued contents of the incoming BufferQueue | 
|  | 130 | mutable Mutex mQueueItemLock; | 
|  | 131 | Condition mQueueItemCondition; | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 132 |  | 
|  | 133 | struct BufferData { | 
|  | 134 | BufferData(BufferItem item, std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame) | 
|  | 135 | : item(item), surfaceFrame(std::move(surfaceFrame)) {} | 
|  | 136 | BufferItem item; | 
|  | 137 | std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame; | 
|  | 138 | }; | 
|  | 139 | std::vector<BufferData> mQueueItems; | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 140 | std::atomic<uint64_t> mLastFrameNumberReceived{0}; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 141 |  | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 142 | bool mAutoRefresh{false}; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 143 |  | 
|  | 144 | // thread-safe | 
| Lloyd Pique | f1c675b | 2018-09-12 20:45:39 -0700 | [diff] [blame] | 145 | std::atomic<int32_t> mQueuedFrames{0}; | 
|  | 146 | std::atomic<bool> mSidebandStreamChanged{false}; | 
| chaviw | 2d9a429 | 2019-12-02 16:00:24 -0800 | [diff] [blame] | 147 |  | 
|  | 148 | sp<ContentsChangedListener> mContentsChangedListener; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 149 | }; | 
|  | 150 |  | 
|  | 151 | } // namespace android |