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