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