Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 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 | |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 17 | #include <dvr/dvr_api.h> |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 18 | #include <gui/BufferHubProducer.h> |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | #include <log/log.h> |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 21 | #include <system/window.h> |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame] | 22 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 23 | namespace android { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 24 | |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 25 | using namespace dvr; |
| 26 | |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 27 | /* static */ |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 28 | sp<BufferHubProducer> BufferHubProducer::Create(const std::shared_ptr<ProducerQueue>& queue) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 29 | sp<BufferHubProducer> producer = new BufferHubProducer; |
| 30 | producer->queue_ = queue; |
| 31 | return producer; |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 32 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 33 | |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 34 | /* static */ |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 35 | sp<BufferHubProducer> BufferHubProducer::Create(ProducerQueueParcelable parcelable) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 36 | if (!parcelable.IsValid()) { |
| 37 | ALOGE("BufferHubProducer::Create: Invalid producer parcelable."); |
| 38 | return nullptr; |
| 39 | } |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 40 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 41 | sp<BufferHubProducer> producer = new BufferHubProducer; |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 42 | producer->queue_ = ProducerQueue::Import(parcelable.TakeChannelHandle()); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 43 | return producer; |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 46 | status_t BufferHubProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 47 | ALOGV("requestBuffer: slot=%d", slot); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 48 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 49 | std::unique_lock<std::mutex> lock(mutex_); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 50 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 51 | if (connected_api_ == kNoConnectedApi) { |
| 52 | ALOGE("requestBuffer: BufferHubProducer has no connected producer"); |
| 53 | return NO_INIT; |
| 54 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 55 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 56 | if (slot < 0 || slot >= max_buffer_count_) { |
| 57 | ALOGE("requestBuffer: slot index %d out of range [0, %d)", slot, max_buffer_count_); |
| 58 | return BAD_VALUE; |
| 59 | } else if (!buffers_[slot].mBufferState.isDequeued()) { |
| 60 | ALOGE("requestBuffer: slot %d is not owned by the producer (state = %s)", slot, |
| 61 | buffers_[slot].mBufferState.string()); |
| 62 | return BAD_VALUE; |
| 63 | } else if (buffers_[slot].mGraphicBuffer != nullptr) { |
| 64 | ALOGE("requestBuffer: slot %d is not empty.", slot); |
| 65 | return BAD_VALUE; |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 66 | } else if (buffers_[slot].mProducerBuffer == nullptr) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 67 | ALOGE("requestBuffer: slot %d is not dequeued.", slot); |
| 68 | return BAD_VALUE; |
| 69 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 70 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 71 | const auto& producer_buffer = buffers_[slot].mProducerBuffer; |
| 72 | sp<GraphicBuffer> graphic_buffer = producer_buffer->buffer()->buffer(); |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 73 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 74 | buffers_[slot].mGraphicBuffer = graphic_buffer; |
| 75 | buffers_[slot].mRequestBufferCalled = true; |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 76 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 77 | *buf = graphic_buffer; |
| 78 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 81 | status_t BufferHubProducer::setMaxDequeuedBufferCount(int max_dequeued_buffers) { |
| 82 | ALOGV("setMaxDequeuedBufferCount: max_dequeued_buffers=%d", max_dequeued_buffers); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 83 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 84 | std::unique_lock<std::mutex> lock(mutex_); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 85 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 86 | if (max_dequeued_buffers <= 0 || |
| 87 | max_dequeued_buffers > |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 88 | int(BufferHubQueue::kMaxQueueCapacity - kDefaultUndequeuedBuffers)) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 89 | ALOGE("setMaxDequeuedBufferCount: %d out of range (0, %zu]", max_dequeued_buffers, |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 90 | BufferHubQueue::kMaxQueueCapacity); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 91 | return BAD_VALUE; |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 92 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 93 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 94 | // The new dequeued_buffers count should not be violated by the number |
| 95 | // of currently dequeued buffers. |
| 96 | int dequeued_count = 0; |
| 97 | for (const auto& buf : buffers_) { |
| 98 | if (buf.mBufferState.isDequeued()) { |
| 99 | dequeued_count++; |
| 100 | } |
| 101 | } |
| 102 | if (dequeued_count > max_dequeued_buffers) { |
| 103 | ALOGE("setMaxDequeuedBufferCount: the requested dequeued_buffers" |
| 104 | "count (%d) exceeds the current dequeued buffer count (%d)", |
| 105 | max_dequeued_buffers, dequeued_count); |
| 106 | return BAD_VALUE; |
| 107 | } |
| 108 | |
| 109 | max_dequeued_buffer_count_ = max_dequeued_buffers; |
| 110 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 113 | status_t BufferHubProducer::setAsyncMode(bool async) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 114 | if (async) { |
| 115 | // TODO(b/36724099) BufferHubQueue's consumer end always acquires the buffer |
| 116 | // automatically and behaves differently from IGraphicBufferConsumer. Thus, |
| 117 | // android::BufferQueue's async mode (a.k.a. allocating an additional buffer |
| 118 | // to prevent dequeueBuffer from being blocking) technically does not apply |
| 119 | // here. |
| 120 | // |
| 121 | // In Daydream, non-blocking producer side dequeue is guaranteed by careful |
| 122 | // buffer consumer implementations. In another word, BufferHubQueue based |
| 123 | // dequeueBuffer should never block whether setAsyncMode(true) is set or |
| 124 | // not. |
| 125 | // |
| 126 | // See: IGraphicBufferProducer::setAsyncMode and |
| 127 | // BufferQueueProducer::setAsyncMode for more about original implementation. |
| 128 | ALOGW("BufferHubProducer::setAsyncMode: BufferHubQueue should always be " |
| 129 | "asynchronous. This call makes no effact."); |
| 130 | return NO_ERROR; |
| 131 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 132 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 135 | status_t BufferHubProducer::dequeueBuffer(int* out_slot, sp<Fence>* out_fence, uint32_t width, |
| 136 | uint32_t height, PixelFormat format, uint64_t usage, |
| 137 | uint64_t* /*outBufferAge*/, |
| 138 | FrameEventHistoryDelta* /* out_timestamps */) { |
Jiwen 'Steve' Cai | e7cf62a | 2018-04-30 22:56:08 -0700 | [diff] [blame] | 139 | ALOGV("dequeueBuffer: w=%u, h=%u, format=%d, usage=%" PRIu64, width, height, format, usage); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 140 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 141 | status_t ret; |
| 142 | std::unique_lock<std::mutex> lock(mutex_); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 143 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 144 | if (connected_api_ == kNoConnectedApi) { |
| 145 | ALOGE("dequeueBuffer: BufferQueue has no connected producer"); |
| 146 | return NO_INIT; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 149 | const uint32_t kLayerCount = 1; |
| 150 | if (int32_t(queue_->capacity()) < max_dequeued_buffer_count_ + kDefaultUndequeuedBuffers) { |
| 151 | // Lazy allocation. When the capacity of |queue_| has not reached |
| 152 | // |max_dequeued_buffer_count_|, allocate new buffer. |
| 153 | // TODO(jwcai) To save memory, the really reasonable thing to do is to go |
| 154 | // over existing slots and find first existing one to dequeue. |
| 155 | ret = AllocateBuffer(width, height, kLayerCount, format, usage); |
| 156 | if (ret < 0) return ret; |
| 157 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 158 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 159 | size_t slot = 0; |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 160 | std::shared_ptr<ProducerBuffer> producer_buffer; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 161 | |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 162 | for (size_t retry = 0; retry < BufferHubQueue::kMaxQueueCapacity; retry++) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 163 | LocalHandle fence; |
| 164 | auto buffer_status = queue_->Dequeue(dequeue_timeout_ms_, &slot, &fence); |
| 165 | if (!buffer_status) return NO_MEMORY; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 166 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 167 | producer_buffer = buffer_status.take(); |
| 168 | if (!producer_buffer) return NO_MEMORY; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 169 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 170 | if (width == producer_buffer->width() && height == producer_buffer->height() && |
| 171 | uint32_t(format) == producer_buffer->format()) { |
| 172 | // The producer queue returns a producer buffer matches the request. |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 173 | break; |
| 174 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 175 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 176 | // Needs reallocation. |
| 177 | // TODO(jwcai) Consider use VLOG instead if we find this log is not useful. |
| 178 | ALOGI("dequeueBuffer: requested buffer (w=%u, h=%u, format=%u) is different " |
| 179 | "from the buffer returned at slot: %zu (w=%u, h=%u, format=%u). Need " |
| 180 | "re-allocattion.", |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 181 | width, height, format, slot, producer_buffer->width(), producer_buffer->height(), |
| 182 | producer_buffer->format()); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 183 | // Mark the slot as reallocating, so that later we can set |
| 184 | // BUFFER_NEEDS_REALLOCATION when the buffer actually get dequeued. |
| 185 | buffers_[slot].mIsReallocating = true; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 186 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 187 | // Remove the old buffer once the allocation before allocating its |
| 188 | // replacement. |
| 189 | RemoveBuffer(slot); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 190 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 191 | // Allocate a new producer buffer with new buffer configs. Note that if |
| 192 | // there are already multiple buffers in the queue, the next one returned |
| 193 | // from |queue_->Dequeue| may not be the new buffer we just reallocated. |
| 194 | // Retry up to BufferHubQueue::kMaxQueueCapacity times. |
| 195 | ret = AllocateBuffer(width, height, kLayerCount, format, usage); |
| 196 | if (ret < 0) return ret; |
| 197 | } |
| 198 | |
| 199 | // With the BufferHub backed solution. Buffer slot returned from |
| 200 | // |queue_->Dequeue| is guaranteed to avaiable for producer's use. |
| 201 | // It's either in free state (if the buffer has never been used before) or |
| 202 | // in queued state (if the buffer has been dequeued and queued back to |
| 203 | // BufferHubQueue). |
| 204 | LOG_ALWAYS_FATAL_IF((!buffers_[slot].mBufferState.isFree() && |
| 205 | !buffers_[slot].mBufferState.isQueued()), |
| 206 | "dequeueBuffer: slot %zu is not free or queued, actual state: %s.", slot, |
| 207 | buffers_[slot].mBufferState.string()); |
| 208 | |
| 209 | buffers_[slot].mBufferState.freeQueued(); |
| 210 | buffers_[slot].mBufferState.dequeue(); |
Jiwen 'Steve' Cai | e7cf62a | 2018-04-30 22:56:08 -0700 | [diff] [blame] | 211 | ALOGV("dequeueBuffer: slot=%zu", slot); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 212 | |
| 213 | // TODO(jwcai) Handle fence properly. |BufferHub| has full fence support, we |
| 214 | // just need to exopose that through |BufferHubQueue| once we need fence. |
| 215 | *out_fence = Fence::NO_FENCE; |
| 216 | *out_slot = int(slot); |
| 217 | ret = NO_ERROR; |
| 218 | |
| 219 | if (buffers_[slot].mIsReallocating) { |
| 220 | ret |= BUFFER_NEEDS_REALLOCATION; |
| 221 | buffers_[slot].mIsReallocating = false; |
| 222 | } |
| 223 | |
| 224 | return ret; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 227 | status_t BufferHubProducer::detachBuffer(int slot) { |
| 228 | ALOGV("detachBuffer: slot=%d", slot); |
| 229 | std::unique_lock<std::mutex> lock(mutex_); |
| 230 | |
| 231 | return DetachBufferLocked(static_cast<size_t>(slot)); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 234 | status_t BufferHubProducer::DetachBufferLocked(size_t slot) { |
| 235 | if (connected_api_ == kNoConnectedApi) { |
| 236 | ALOGE("detachBuffer: BufferHubProducer is not connected."); |
| 237 | return NO_INIT; |
| 238 | } |
| 239 | |
| 240 | if (slot >= static_cast<size_t>(max_buffer_count_)) { |
| 241 | ALOGE("detachBuffer: slot index %zu out of range [0, %d)", slot, max_buffer_count_); |
| 242 | return BAD_VALUE; |
| 243 | } else if (!buffers_[slot].mBufferState.isDequeued()) { |
| 244 | ALOGE("detachBuffer: slot %zu is not owned by the producer (state = %s)", slot, |
| 245 | buffers_[slot].mBufferState.string()); |
| 246 | return BAD_VALUE; |
| 247 | } else if (!buffers_[slot].mRequestBufferCalled) { |
| 248 | ALOGE("detachBuffer: buffer in slot %zu has not been requested", slot); |
| 249 | return BAD_VALUE; |
| 250 | } |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 251 | std::shared_ptr<ProducerBuffer> producer_buffer = queue_->GetBuffer(slot); |
| 252 | if (producer_buffer == nullptr || producer_buffer->buffer() == nullptr) { |
| 253 | ALOGE("detachBuffer: Invalid ProducerBuffer at slot %zu.", slot); |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 254 | return BAD_VALUE; |
| 255 | } |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 256 | sp<GraphicBuffer> graphic_buffer = producer_buffer->buffer()->buffer(); |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 257 | if (graphic_buffer == nullptr) { |
| 258 | ALOGE("detachBuffer: Invalid GraphicBuffer at slot %zu.", slot); |
| 259 | return BAD_VALUE; |
| 260 | } |
| 261 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 262 | // Remove the ProducerBuffer from the ProducerQueue. |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 263 | status_t error = RemoveBuffer(slot); |
| 264 | if (error != NO_ERROR) { |
| 265 | ALOGE("detachBuffer: Failed to remove buffer, slot=%zu, error=%d.", slot, error); |
| 266 | return error; |
| 267 | } |
| 268 | |
| 269 | // Here we need to convert the existing ProducerBuffer into a DetachedBufferHandle and inject |
| 270 | // the handle into the GraphicBuffer object at the requested slot. |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 271 | auto status_or_handle = producer_buffer->Detach(); |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 272 | if (!status_or_handle.ok()) { |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 273 | ALOGE("detachBuffer: Failed to detach from a ProducerBuffer at slot %zu, error=%d.", slot, |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 274 | status_or_handle.error()); |
| 275 | return BAD_VALUE; |
| 276 | } |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 277 | |
Jiwen 'Steve' Cai | 7756557 | 2018-10-15 11:31:29 -0700 | [diff] [blame] | 278 | // TODO(b/70912269): Reimplement BufferHubProducer::DetachBufferLocked() once GraphicBuffer can |
| 279 | // be directly backed by BufferHub. |
| 280 | return INVALID_OPERATION; |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | status_t BufferHubProducer::detachNextBuffer(sp<GraphicBuffer>* out_buffer, sp<Fence>* out_fence) { |
| 284 | ALOGV("detachNextBuffer."); |
| 285 | |
| 286 | if (out_buffer == nullptr || out_fence == nullptr) { |
| 287 | ALOGE("detachNextBuffer: Invalid parameter: out_buffer=%p, out_fence=%p", out_buffer, |
| 288 | out_fence); |
| 289 | return BAD_VALUE; |
| 290 | } |
| 291 | |
| 292 | std::unique_lock<std::mutex> lock(mutex_); |
| 293 | |
| 294 | if (connected_api_ == kNoConnectedApi) { |
| 295 | ALOGE("detachNextBuffer: BufferHubProducer is not connected."); |
| 296 | return NO_INIT; |
| 297 | } |
| 298 | |
| 299 | // detachNextBuffer is equivalent to calling dequeueBuffer, requestBuffer, and detachBuffer in |
| 300 | // sequence, except for two things: |
| 301 | // |
| 302 | // 1) It is unnecessary to know the dimensions, format, or usage of the next buffer, i.e. the |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 303 | // function just returns whatever ProducerBuffer is available from the ProducerQueue and no |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 304 | // buffer allocation or re-allocation will happen. |
| 305 | // 2) It will not block, since if it cannot find an appropriate buffer to return, it will return |
| 306 | // an error instead. |
| 307 | size_t slot = 0; |
| 308 | LocalHandle fence; |
| 309 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 310 | // First, dequeue a ProducerBuffer from the ProducerQueue with no timeout. Report error |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 311 | // immediately if ProducerQueue::Dequeue() fails. |
| 312 | auto status_or_buffer = queue_->Dequeue(/*timeout=*/0, &slot, &fence); |
| 313 | if (!status_or_buffer.ok()) { |
| 314 | ALOGE("detachNextBuffer: Failed to dequeue buffer, error=%d.", status_or_buffer.error()); |
| 315 | return NO_MEMORY; |
| 316 | } |
| 317 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 318 | std::shared_ptr<ProducerBuffer> producer_buffer = status_or_buffer.take(); |
| 319 | if (producer_buffer == nullptr) { |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 320 | ALOGE("detachNextBuffer: Dequeued buffer is null."); |
| 321 | return NO_MEMORY; |
| 322 | } |
| 323 | |
| 324 | // With the BufferHub backed solution, slot returned from |queue_->Dequeue| is guaranteed to |
| 325 | // be available for producer's use. It's either in free state (if the buffer has never been used |
| 326 | // before) or in queued state (if the buffer has been dequeued and queued back to |
| 327 | // BufferHubQueue). |
| 328 | if (!buffers_[slot].mBufferState.isFree() && !buffers_[slot].mBufferState.isQueued()) { |
| 329 | ALOGE("detachNextBuffer: slot %zu is not free or queued, actual state: %s.", slot, |
| 330 | buffers_[slot].mBufferState.string()); |
| 331 | return BAD_VALUE; |
| 332 | } |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 333 | if (buffers_[slot].mProducerBuffer == nullptr) { |
| 334 | ALOGE("detachNextBuffer: ProducerBuffer at slot %zu is null.", slot); |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 335 | return BAD_VALUE; |
| 336 | } |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 337 | if (buffers_[slot].mProducerBuffer->id() != producer_buffer->id()) { |
| 338 | ALOGE("detachNextBuffer: ProducerBuffer at slot %zu has mismatched id, actual: " |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 339 | "%d, expected: %d.", |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 340 | slot, buffers_[slot].mProducerBuffer->id(), producer_buffer->id()); |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 341 | return BAD_VALUE; |
| 342 | } |
| 343 | |
| 344 | ALOGV("detachNextBuffer: slot=%zu", slot); |
| 345 | buffers_[slot].mBufferState.freeQueued(); |
| 346 | buffers_[slot].mBufferState.dequeue(); |
| 347 | |
| 348 | // Second, request the buffer. |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 349 | sp<GraphicBuffer> graphic_buffer = producer_buffer->buffer()->buffer(); |
| 350 | buffers_[slot].mGraphicBuffer = producer_buffer->buffer()->buffer(); |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 351 | |
| 352 | // Finally, detach the buffer and then return. |
| 353 | status_t error = DetachBufferLocked(slot); |
| 354 | if (error == NO_ERROR) { |
| 355 | *out_fence = new Fence(fence.Release()); |
| 356 | *out_buffer = graphic_buffer; |
| 357 | } |
| 358 | return error; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Jiwen 'Steve' Cai | 57ae3ee | 2018-05-03 17:51:52 -0700 | [diff] [blame] | 361 | status_t BufferHubProducer::attachBuffer(int* out_slot, const sp<GraphicBuffer>& buffer) { |
| 362 | // In the BufferHub design, all buffers are allocated and owned by the BufferHub. Thus only |
| 363 | // GraphicBuffers that are originated from BufferHub can be attached to a BufferHubProducer. |
| 364 | ALOGV("queueBuffer: buffer=%p", buffer.get()); |
| 365 | |
| 366 | if (out_slot == nullptr) { |
| 367 | ALOGE("attachBuffer: out_slot cannot be NULL."); |
| 368 | return BAD_VALUE; |
| 369 | } |
Jiwen 'Steve' Cai | 7756557 | 2018-10-15 11:31:29 -0700 | [diff] [blame] | 370 | if (buffer == nullptr) { |
Jiwen 'Steve' Cai | 57ae3ee | 2018-05-03 17:51:52 -0700 | [diff] [blame] | 371 | ALOGE("attachBuffer: invalid GraphicBuffer."); |
| 372 | return BAD_VALUE; |
| 373 | } |
| 374 | |
| 375 | std::unique_lock<std::mutex> lock(mutex_); |
| 376 | |
| 377 | if (connected_api_ == kNoConnectedApi) { |
| 378 | ALOGE("attachBuffer: BufferQueue has no connected producer"); |
| 379 | return NO_INIT; |
| 380 | } |
| 381 | |
| 382 | // Before attaching the buffer, caller is supposed to call |
| 383 | // IGraphicBufferProducer::setGenerationNumber to inform the |
| 384 | // BufferHubProducer the next generation number. |
| 385 | if (buffer->getGenerationNumber() != generation_number_) { |
| 386 | ALOGE("attachBuffer: Mismatched generation number, buffer: %u, queue: %u.", |
| 387 | buffer->getGenerationNumber(), generation_number_); |
| 388 | return BAD_VALUE; |
| 389 | } |
| 390 | |
Jiwen 'Steve' Cai | 7756557 | 2018-10-15 11:31:29 -0700 | [diff] [blame] | 391 | // TODO(b/70912269): Reimplement BufferHubProducer::DetachBufferLocked() once GraphicBuffer can |
| 392 | // be directly backed by BufferHub. |
| 393 | return INVALID_OPERATION; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 396 | status_t BufferHubProducer::queueBuffer(int slot, const QueueBufferInput& input, |
| 397 | QueueBufferOutput* output) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 398 | ALOGV("queueBuffer: slot %d", slot); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 399 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 400 | if (output == nullptr) { |
| 401 | return BAD_VALUE; |
| 402 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 403 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 404 | int64_t timestamp; |
| 405 | bool is_auto_timestamp; |
| 406 | android_dataspace dataspace; |
| 407 | Rect crop(Rect::EMPTY_RECT); |
| 408 | int scaling_mode; |
| 409 | uint32_t transform; |
| 410 | sp<Fence> fence; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 411 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 412 | input.deflate(×tamp, &is_auto_timestamp, &dataspace, &crop, &scaling_mode, &transform, |
| 413 | &fence); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 414 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 415 | // Check input scaling mode is valid. |
| 416 | switch (scaling_mode) { |
| 417 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 418 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 419 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
| 420 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
| 421 | break; |
| 422 | default: |
| 423 | ALOGE("queueBuffer: unknown scaling mode %d", scaling_mode); |
| 424 | return BAD_VALUE; |
| 425 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 426 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 427 | // Check input fence is valid. |
| 428 | if (fence == nullptr) { |
| 429 | ALOGE("queueBuffer: fence is NULL"); |
| 430 | return BAD_VALUE; |
| 431 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 432 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 433 | std::unique_lock<std::mutex> lock(mutex_); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 434 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 435 | if (connected_api_ == kNoConnectedApi) { |
| 436 | ALOGE("queueBuffer: BufferQueue has no connected producer"); |
| 437 | return NO_INIT; |
| 438 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 439 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 440 | if (slot < 0 || slot >= max_buffer_count_) { |
| 441 | ALOGE("queueBuffer: slot index %d out of range [0, %d)", slot, max_buffer_count_); |
| 442 | return BAD_VALUE; |
| 443 | } else if (!buffers_[slot].mBufferState.isDequeued()) { |
| 444 | ALOGE("queueBuffer: slot %d is not owned by the producer (state = %s)", slot, |
| 445 | buffers_[slot].mBufferState.string()); |
| 446 | return BAD_VALUE; |
| 447 | } else if ((!buffers_[slot].mRequestBufferCalled || buffers_[slot].mGraphicBuffer == nullptr)) { |
| 448 | ALOGE("queueBuffer: slot %d is not requested (mRequestBufferCalled=%d, " |
| 449 | "mGraphicBuffer=%p)", |
| 450 | slot, buffers_[slot].mRequestBufferCalled, buffers_[slot].mGraphicBuffer.get()); |
| 451 | return BAD_VALUE; |
| 452 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 453 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 454 | // Post the producer buffer with timestamp in the metadata. |
| 455 | const auto& producer_buffer = buffers_[slot].mProducerBuffer; |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 456 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 457 | // Check input crop is not out of boundary of current buffer. |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 458 | Rect buffer_rect(producer_buffer->width(), producer_buffer->height()); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 459 | Rect cropped_rect(Rect::EMPTY_RECT); |
| 460 | crop.intersect(buffer_rect, &cropped_rect); |
| 461 | if (cropped_rect != crop) { |
| 462 | ALOGE("queueBuffer: slot %d has out-of-boundary crop.", slot); |
| 463 | return BAD_VALUE; |
| 464 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 465 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 466 | LocalHandle fence_fd(fence->isValid() ? fence->dup() : -1); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 467 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 468 | DvrNativeBufferMetadata meta_data; |
| 469 | meta_data.timestamp = timestamp; |
| 470 | meta_data.is_auto_timestamp = int32_t(is_auto_timestamp); |
| 471 | meta_data.dataspace = int32_t(dataspace); |
| 472 | meta_data.crop_left = crop.left; |
| 473 | meta_data.crop_top = crop.top; |
| 474 | meta_data.crop_right = crop.right; |
| 475 | meta_data.crop_bottom = crop.bottom; |
| 476 | meta_data.scaling_mode = int32_t(scaling_mode); |
| 477 | meta_data.transform = int32_t(transform); |
Jiwen 'Steve' Cai | cb4751c | 2017-04-14 17:56:55 -0700 | [diff] [blame] | 478 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 479 | producer_buffer->PostAsync(&meta_data, fence_fd); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 480 | buffers_[slot].mBufferState.queue(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 481 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 482 | output->width = producer_buffer->width(); |
| 483 | output->height = producer_buffer->height(); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 484 | output->transformHint = 0; // default value, we don't use it yet. |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 485 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 486 | // |numPendingBuffers| counts of the number of buffers that has been enqueued |
| 487 | // by the producer but not yet acquired by the consumer. Due to the nature |
| 488 | // of BufferHubQueue design, this is hard to trace from the producer's client |
| 489 | // side, but it's safe to assume it's zero. |
| 490 | output->numPendingBuffers = 0; |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 491 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 492 | // Note that we are not setting nextFrameNumber here as it seems to be only |
| 493 | // used by surface flinger. See more at b/22802885, ag/791760. |
| 494 | output->nextFrameNumber = 0; |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 495 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 496 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 499 | status_t BufferHubProducer::cancelBuffer(int slot, const sp<Fence>& fence) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 500 | ALOGV(__FUNCTION__); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 501 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 502 | std::unique_lock<std::mutex> lock(mutex_); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 503 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 504 | if (connected_api_ == kNoConnectedApi) { |
| 505 | ALOGE("cancelBuffer: BufferQueue has no connected producer"); |
| 506 | return NO_INIT; |
| 507 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 508 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 509 | if (slot < 0 || slot >= max_buffer_count_) { |
| 510 | ALOGE("cancelBuffer: slot index %d out of range [0, %d)", slot, max_buffer_count_); |
| 511 | return BAD_VALUE; |
| 512 | } else if (!buffers_[slot].mBufferState.isDequeued()) { |
| 513 | ALOGE("cancelBuffer: slot %d is not owned by the producer (state = %s)", slot, |
| 514 | buffers_[slot].mBufferState.string()); |
| 515 | return BAD_VALUE; |
| 516 | } else if (fence == nullptr) { |
| 517 | ALOGE("cancelBuffer: fence is NULL"); |
| 518 | return BAD_VALUE; |
| 519 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 520 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 521 | auto producer_buffer = buffers_[slot].mProducerBuffer; |
| 522 | queue_->Enqueue(producer_buffer, size_t(slot), 0U); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 523 | buffers_[slot].mBufferState.cancel(); |
| 524 | buffers_[slot].mFence = fence; |
| 525 | ALOGV("cancelBuffer: slot %d", slot); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 526 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 527 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 530 | status_t BufferHubProducer::query(int what, int* out_value) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 531 | ALOGV(__FUNCTION__); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 532 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 533 | std::unique_lock<std::mutex> lock(mutex_); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 534 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 535 | if (out_value == nullptr) { |
| 536 | ALOGE("query: out_value was NULL"); |
| 537 | return BAD_VALUE; |
| 538 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 539 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 540 | int value = 0; |
| 541 | switch (what) { |
| 542 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
| 543 | // TODO(b/36187402) This should be the maximum number of buffers that this |
| 544 | // producer queue's consumer can acquire. Set to be at least one. Need to |
| 545 | // find a way to set from the consumer side. |
| 546 | value = kDefaultUndequeuedBuffers; |
| 547 | break; |
| 548 | case NATIVE_WINDOW_BUFFER_AGE: |
| 549 | value = 0; |
| 550 | break; |
| 551 | case NATIVE_WINDOW_WIDTH: |
| 552 | value = int32_t(queue_->default_width()); |
| 553 | break; |
| 554 | case NATIVE_WINDOW_HEIGHT: |
| 555 | value = int32_t(queue_->default_height()); |
| 556 | break; |
| 557 | case NATIVE_WINDOW_FORMAT: |
| 558 | value = int32_t(queue_->default_format()); |
| 559 | break; |
| 560 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: |
| 561 | // BufferHubQueue is always operating in async mode, thus semantically |
| 562 | // consumer can never be running behind. See BufferQueueCore.cpp core |
| 563 | // for more information about the original meaning of this flag. |
| 564 | value = 0; |
| 565 | break; |
| 566 | case NATIVE_WINDOW_CONSUMER_USAGE_BITS: |
| 567 | // TODO(jwcai) This is currently not implement as we don't need |
| 568 | // IGraphicBufferConsumer parity. |
| 569 | value = 0; |
| 570 | break; |
| 571 | case NATIVE_WINDOW_DEFAULT_DATASPACE: |
| 572 | // TODO(jwcai) Return the default value android::BufferQueue is using as |
| 573 | // there is no way dvr::ConsumerQueue can set it. |
| 574 | value = 0; // HAL_DATASPACE_UNKNOWN |
| 575 | break; |
| 576 | case NATIVE_WINDOW_STICKY_TRANSFORM: |
| 577 | // TODO(jwcai) Return the default value android::BufferQueue is using as |
| 578 | // there is no way dvr::ConsumerQueue can set it. |
| 579 | value = 0; |
| 580 | break; |
| 581 | case NATIVE_WINDOW_CONSUMER_IS_PROTECTED: |
| 582 | // In Daydream's implementation, the consumer end (i.e. VR Compostior) |
| 583 | // knows how to handle protected buffers. |
| 584 | value = 1; |
| 585 | break; |
| 586 | default: |
| 587 | return BAD_VALUE; |
| 588 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 589 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 590 | ALOGV("query: key=%d, v=%d", what, value); |
| 591 | *out_value = value; |
| 592 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 595 | status_t BufferHubProducer::connect(const sp<IProducerListener>& /* listener */, int api, |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 596 | bool /* producer_controlled_by_app */, |
| 597 | QueueBufferOutput* output) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 598 | // Consumer interaction are actually handled by buffer hub, and we need |
| 599 | // to maintain consumer operations here. We only need to perform basic input |
| 600 | // parameter checks here. |
| 601 | ALOGV(__FUNCTION__); |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 602 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 603 | if (output == nullptr) { |
| 604 | return BAD_VALUE; |
| 605 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 606 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 607 | std::unique_lock<std::mutex> lock(mutex_); |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 608 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 609 | if (connected_api_ != kNoConnectedApi) { |
| 610 | return BAD_VALUE; |
| 611 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 612 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 613 | if (!queue_->is_connected()) { |
| 614 | ALOGE("BufferHubProducer::connect: This BufferHubProducer is not " |
| 615 | "connected to bufferhud. Has it been taken out as a parcelable?"); |
| 616 | return BAD_VALUE; |
| 617 | } |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 618 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 619 | switch (api) { |
| 620 | case NATIVE_WINDOW_API_EGL: |
| 621 | case NATIVE_WINDOW_API_CPU: |
| 622 | case NATIVE_WINDOW_API_MEDIA: |
| 623 | case NATIVE_WINDOW_API_CAMERA: |
| 624 | connected_api_ = api; |
Jiwen 'Steve' Cai | cb4751c | 2017-04-14 17:56:55 -0700 | [diff] [blame] | 625 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 626 | output->width = queue_->default_width(); |
| 627 | output->height = queue_->default_height(); |
Jiwen 'Steve' Cai | cb4751c | 2017-04-14 17:56:55 -0700 | [diff] [blame] | 628 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 629 | // default values, we don't use them yet. |
| 630 | output->transformHint = 0; |
| 631 | output->numPendingBuffers = 0; |
| 632 | output->nextFrameNumber = 0; |
| 633 | output->bufferReplaced = false; |
Jiwen 'Steve' Cai | cb4751c | 2017-04-14 17:56:55 -0700 | [diff] [blame] | 634 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 635 | break; |
| 636 | default: |
| 637 | ALOGE("BufferHubProducer::connect: unknow API %d", api); |
| 638 | return BAD_VALUE; |
| 639 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 640 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 641 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 644 | status_t BufferHubProducer::disconnect(int api, DisconnectMode /*mode*/) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 645 | // Consumer interaction are actually handled by buffer hub, and we need |
| 646 | // to maintain consumer operations here. We only need to perform basic input |
| 647 | // parameter checks here. |
| 648 | ALOGV(__FUNCTION__); |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 649 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 650 | std::unique_lock<std::mutex> lock(mutex_); |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 651 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 652 | if (kNoConnectedApi == connected_api_) { |
| 653 | return NO_INIT; |
| 654 | } else if (api != connected_api_) { |
| 655 | return BAD_VALUE; |
| 656 | } |
Jiwen 'Steve' Cai | 1601bcf | 2017-03-24 14:03:06 -0700 | [diff] [blame] | 657 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 658 | FreeAllBuffers(); |
| 659 | connected_api_ = kNoConnectedApi; |
| 660 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 663 | status_t BufferHubProducer::setSidebandStream(const sp<NativeHandle>& stream) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 664 | if (stream != nullptr) { |
| 665 | // TODO(jwcai) Investigate how is is used, maybe use BufferHubBuffer's |
| 666 | // metadata. |
| 667 | ALOGE("SidebandStream is not currently supported."); |
| 668 | return INVALID_OPERATION; |
| 669 | } |
| 670 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 673 | void BufferHubProducer::allocateBuffers(uint32_t /* width */, uint32_t /* height */, |
| 674 | PixelFormat /* format */, uint64_t /* usage */) { |
| 675 | // TODO(jwcai) |allocateBuffers| aims to preallocate up to the maximum number |
| 676 | // of buffers permitted by the current BufferQueue configuration (aka |
| 677 | // |max_buffer_count_|). |
| 678 | ALOGE("BufferHubProducer::allocateBuffers not implemented."); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 681 | status_t BufferHubProducer::allowAllocation(bool /* allow */) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 682 | ALOGE("BufferHubProducer::allowAllocation not implemented."); |
| 683 | return INVALID_OPERATION; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 686 | status_t BufferHubProducer::setGenerationNumber(uint32_t generation_number) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 687 | ALOGV(__FUNCTION__); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 688 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 689 | std::unique_lock<std::mutex> lock(mutex_); |
| 690 | generation_number_ = generation_number; |
| 691 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 694 | String8 BufferHubProducer::getConsumerName() const { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 695 | // BufferHub based implementation could have one to many producer/consumer |
| 696 | // relationship, thus |getConsumerName| from the producer side does not |
| 697 | // make any sense. |
| 698 | ALOGE("BufferHubProducer::getConsumerName not supported."); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 699 | return String8("BufferHubQueue::StubConsumer"); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 702 | status_t BufferHubProducer::setSharedBufferMode(bool shared_buffer_mode) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 703 | if (shared_buffer_mode) { |
| 704 | ALOGE("BufferHubProducer::setSharedBufferMode(true) is not supported."); |
| 705 | // TODO(b/36373181) Front buffer mode for buffer hub queue as ANativeWindow. |
| 706 | return INVALID_OPERATION; |
| 707 | } |
| 708 | // Setting to default should just work as a no-op. |
| 709 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 710 | } |
| 711 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 712 | status_t BufferHubProducer::setAutoRefresh(bool auto_refresh) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 713 | if (auto_refresh) { |
| 714 | ALOGE("BufferHubProducer::setAutoRefresh(true) is not supported."); |
| 715 | return INVALID_OPERATION; |
| 716 | } |
| 717 | // Setting to default should just work as a no-op. |
| 718 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 721 | status_t BufferHubProducer::setDequeueTimeout(nsecs_t timeout) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 722 | ALOGV(__FUNCTION__); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 723 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 724 | std::unique_lock<std::mutex> lock(mutex_); |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 725 | dequeue_timeout_ms_ = static_cast<int>(timeout / (1000 * 1000)); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 726 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 729 | status_t BufferHubProducer::getLastQueuedBuffer(sp<GraphicBuffer>* /* out_buffer */, |
| 730 | sp<Fence>* /* out_fence */, |
| 731 | float /*out_transform_matrix*/[16]) { |
| 732 | ALOGE("BufferHubProducer::getLastQueuedBuffer not implemented."); |
| 733 | return INVALID_OPERATION; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 734 | } |
| 735 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 736 | void BufferHubProducer::getFrameTimestamps(FrameEventHistoryDelta* /*outDelta*/) { |
| 737 | ALOGE("BufferHubProducer::getFrameTimestamps not implemented."); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 740 | status_t BufferHubProducer::getUniqueId(uint64_t* out_id) const { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 741 | ALOGV(__FUNCTION__); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 742 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 743 | *out_id = unique_id_; |
| 744 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 747 | status_t BufferHubProducer::getConsumerUsage(uint64_t* out_usage) const { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 748 | ALOGV(__FUNCTION__); |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 749 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 750 | // same value as returned by querying NATIVE_WINDOW_CONSUMER_USAGE_BITS |
| 751 | *out_usage = 0; |
| 752 | return NO_ERROR; |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 755 | status_t BufferHubProducer::TakeAsParcelable(ProducerQueueParcelable* out_parcelable) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 756 | if (!out_parcelable || out_parcelable->IsValid()) return BAD_VALUE; |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 757 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 758 | if (connected_api_ != kNoConnectedApi) { |
| 759 | ALOGE("BufferHubProducer::TakeAsParcelable: BufferHubProducer has " |
| 760 | "connected client. Must disconnect first."); |
| 761 | return BAD_VALUE; |
| 762 | } |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 763 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 764 | if (!queue_->is_connected()) { |
| 765 | ALOGE("BufferHubProducer::TakeAsParcelable: This BufferHubProducer " |
| 766 | "is not connected to bufferhud. Has it been taken out as a " |
| 767 | "parcelable?"); |
| 768 | return BAD_VALUE; |
| 769 | } |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 770 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 771 | auto status = queue_->TakeAsParcelable(); |
| 772 | if (!status) { |
| 773 | ALOGE("BufferHubProducer::TakeAsParcelable: Failed to take out " |
| 774 | "ProducuerQueueParcelable from the producer queue, error: %s.", |
| 775 | status.GetErrorMessage().c_str()); |
| 776 | return BAD_VALUE; |
| 777 | } |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 778 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 779 | *out_parcelable = status.take(); |
| 780 | return NO_ERROR; |
Jiwen 'Steve' Cai | 5afb740 | 2017-11-09 18:50:32 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 783 | status_t BufferHubProducer::AllocateBuffer(uint32_t width, uint32_t height, uint32_t layer_count, |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 784 | PixelFormat format, uint64_t usage) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 785 | auto status = queue_->AllocateBuffer(width, height, layer_count, uint32_t(format), usage); |
| 786 | if (!status) { |
| 787 | ALOGE("BufferHubProducer::AllocateBuffer: Failed to allocate buffer: %s", |
| 788 | status.GetErrorMessage().c_str()); |
| 789 | return NO_MEMORY; |
| 790 | } |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 791 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 792 | size_t slot = status.get(); |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 793 | auto producer_buffer = queue_->GetBuffer(slot); |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 794 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 795 | LOG_ALWAYS_FATAL_IF(producer_buffer == nullptr, |
| 796 | "Failed to get the producer buffer at slot: %zu", slot); |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 797 | |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 798 | buffers_[slot].mProducerBuffer = producer_buffer; |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 799 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 800 | return NO_ERROR; |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 803 | status_t BufferHubProducer::RemoveBuffer(size_t slot) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 804 | auto status = queue_->RemoveBuffer(slot); |
| 805 | if (!status) { |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 806 | ALOGE("BufferHubProducer::RemoveBuffer: Failed to remove buffer at slot: %zu, error: %s.", |
| 807 | slot, status.GetErrorMessage().c_str()); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 808 | return INVALID_OPERATION; |
| 809 | } |
Jiwen 'Steve' Cai | d6cb17f | 2017-05-08 16:15:35 -0700 | [diff] [blame] | 810 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 811 | // Reset in memory objects related the the buffer. |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 812 | buffers_[slot].mProducerBuffer = nullptr; |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 813 | buffers_[slot].mBufferState.detachProducer(); |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 814 | buffers_[slot].mFence = Fence::NO_FENCE; |
| 815 | buffers_[slot].mGraphicBuffer = nullptr; |
| 816 | buffers_[slot].mRequestBufferCalled = false; |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 817 | return NO_ERROR; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Jiwen 'Steve' Cai | 0f95084 | 2018-01-16 17:05:54 -0800 | [diff] [blame] | 820 | status_t BufferHubProducer::FreeAllBuffers() { |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 821 | for (size_t slot = 0; slot < BufferHubQueue::kMaxQueueCapacity; slot++) { |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 822 | // Reset in memory objects related the the buffer. |
Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 823 | buffers_[slot].mProducerBuffer = nullptr; |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 824 | buffers_[slot].mBufferState.reset(); |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 825 | buffers_[slot].mFence = Fence::NO_FENCE; |
Jiwen 'Steve' Cai | 4287535 | 2018-04-30 17:55:11 -0700 | [diff] [blame] | 826 | buffers_[slot].mGraphicBuffer = nullptr; |
| 827 | buffers_[slot].mRequestBufferCalled = false; |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 828 | } |
Jiwen 'Steve' Cai | 005f45d | 2017-08-04 17:34:37 -0700 | [diff] [blame] | 829 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 830 | auto status = queue_->FreeAllBuffers(); |
| 831 | if (!status) { |
| 832 | ALOGE("BufferHubProducer::FreeAllBuffers: Failed to free all buffers on " |
| 833 | "the queue: %s", |
| 834 | status.GetErrorMessage().c_str()); |
| 835 | } |
Jiwen 'Steve' Cai | 005f45d | 2017-08-04 17:34:37 -0700 | [diff] [blame] | 836 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 837 | if (queue_->capacity() != 0 || queue_->count() != 0) { |
| 838 | LOG_ALWAYS_FATAL("BufferHubProducer::FreeAllBuffers: Not all buffers are freed."); |
| 839 | } |
Jiwen 'Steve' Cai | 005f45d | 2017-08-04 17:34:37 -0700 | [diff] [blame] | 840 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 841 | return NO_ERROR; |
Jiwen 'Steve' Cai | 005f45d | 2017-08-04 17:34:37 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 844 | status_t BufferHubProducer::exportToParcel(Parcel* parcel) { |
| 845 | status_t res = TakeAsParcelable(&pending_producer_parcelable_); |
| 846 | if (res != NO_ERROR) return res; |
| 847 | |
| 848 | if (!pending_producer_parcelable_.IsValid()) { |
| 849 | ALOGE("BufferHubProducer::exportToParcel: Invalid parcelable object."); |
| 850 | return BAD_VALUE; |
| 851 | } |
| 852 | |
| 853 | res = parcel->writeUint32(USE_BUFFER_HUB); |
| 854 | if (res != NO_ERROR) { |
| 855 | ALOGE("BufferHubProducer::exportToParcel: Cannot write magic, res=%d.", res); |
| 856 | return res; |
| 857 | } |
| 858 | |
| 859 | return pending_producer_parcelable_.writeToParcel(parcel); |
| 860 | } |
| 861 | |
| 862 | IBinder* BufferHubProducer::onAsBinder() { |
| 863 | ALOGE("BufferHubProducer::onAsBinder: BufferHubProducer should never be used as an Binder " |
| 864 | "object."); |
| 865 | return nullptr; |
| 866 | } |
| 867 | |
Jiwen 'Steve' Cai | 9a6ddf7 | 2018-01-18 14:37:24 -0800 | [diff] [blame] | 868 | } // namespace android |