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