BufferQueue.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2012 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. #define LOG_TAG "BufferQueue"
  17. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  18. //#define LOG_NDEBUG 0
  19. #ifndef NO_BUFFERHUB
  20. #include <gui/BufferHubConsumer.h>
  21. #include <gui/BufferHubProducer.h>
  22. #endif
  23. #include <gui/BufferQueue.h>
  24. #include <gui/BufferQueueConsumer.h>
  25. #include <gui/BufferQueueCore.h>
  26. #include <gui/BufferQueueProducer.h>
  27. namespace android {
  28. BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
  29. const wp<ConsumerListener>& consumerListener):
  30. mConsumerListener(consumerListener) {}
  31. BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
  32. void BufferQueue::ProxyConsumerListener::onDisconnect() {
  33. sp<ConsumerListener> listener(mConsumerListener.promote());
  34. if (listener != nullptr) {
  35. listener->onDisconnect();
  36. }
  37. }
  38. void BufferQueue::ProxyConsumerListener::onFrameAvailable(
  39. const BufferItem& item) {
  40. sp<ConsumerListener> listener(mConsumerListener.promote());
  41. if (listener != nullptr) {
  42. listener->onFrameAvailable(item);
  43. }
  44. }
  45. void BufferQueue::ProxyConsumerListener::onFrameReplaced(
  46. const BufferItem& item) {
  47. sp<ConsumerListener> listener(mConsumerListener.promote());
  48. if (listener != nullptr) {
  49. listener->onFrameReplaced(item);
  50. }
  51. }
  52. void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
  53. sp<ConsumerListener> listener(mConsumerListener.promote());
  54. if (listener != nullptr) {
  55. listener->onBuffersReleased();
  56. }
  57. }
  58. void BufferQueue::ProxyConsumerListener::onSidebandStreamChanged() {
  59. sp<ConsumerListener> listener(mConsumerListener.promote());
  60. if (listener != nullptr) {
  61. listener->onSidebandStreamChanged();
  62. }
  63. }
  64. void BufferQueue::ProxyConsumerListener::addAndGetFrameTimestamps(
  65. const NewFrameEventsEntry* newTimestamps,
  66. FrameEventHistoryDelta* outDelta) {
  67. sp<ConsumerListener> listener(mConsumerListener.promote());
  68. if (listener != nullptr) {
  69. listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
  70. }
  71. }
  72. void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
  73. sp<IGraphicBufferConsumer>* outConsumer,
  74. bool consumerIsSurfaceFlinger) {
  75. LOG_ALWAYS_FATAL_IF(outProducer == nullptr,
  76. "BufferQueue: outProducer must not be NULL");
  77. LOG_ALWAYS_FATAL_IF(outConsumer == nullptr,
  78. "BufferQueue: outConsumer must not be NULL");
  79. sp<BufferQueueCore> core(new BufferQueueCore());
  80. LOG_ALWAYS_FATAL_IF(core == nullptr,
  81. "BufferQueue: failed to create BufferQueueCore");
  82. sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
  83. LOG_ALWAYS_FATAL_IF(producer == nullptr,
  84. "BufferQueue: failed to create BufferQueueProducer");
  85. sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
  86. LOG_ALWAYS_FATAL_IF(consumer == nullptr,
  87. "BufferQueue: failed to create BufferQueueConsumer");
  88. *outProducer = producer;
  89. *outConsumer = consumer;
  90. }
  91. #ifndef NO_BUFFERHUB
  92. void BufferQueue::createBufferHubQueue(sp<IGraphicBufferProducer>* outProducer,
  93. sp<IGraphicBufferConsumer>* outConsumer) {
  94. LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BufferQueue: outProducer must not be NULL");
  95. LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BufferQueue: outConsumer must not be NULL");
  96. sp<IGraphicBufferProducer> producer;
  97. sp<IGraphicBufferConsumer> consumer;
  98. dvr::ProducerQueueConfigBuilder configBuilder;
  99. std::shared_ptr<dvr::ProducerQueue> producerQueue =
  100. dvr::ProducerQueue::Create(configBuilder.Build(), dvr::UsagePolicy{});
  101. LOG_ALWAYS_FATAL_IF(producerQueue == nullptr, "BufferQueue: failed to create ProducerQueue.");
  102. std::shared_ptr<dvr::ConsumerQueue> consumerQueue = producerQueue->CreateConsumerQueue();
  103. LOG_ALWAYS_FATAL_IF(consumerQueue == nullptr, "BufferQueue: failed to create ConsumerQueue.");
  104. producer = BufferHubProducer::Create(producerQueue);
  105. consumer = BufferHubConsumer::Create(consumerQueue);
  106. LOG_ALWAYS_FATAL_IF(producer == nullptr, "BufferQueue: failed to create BufferQueueProducer");
  107. LOG_ALWAYS_FATAL_IF(consumer == nullptr, "BufferQueue: failed to create BufferQueueConsumer");
  108. *outProducer = producer;
  109. *outConsumer = consumer;
  110. }
  111. #endif
  112. }; // namespace android