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