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