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