Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 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 | |
| 17 | #ifndef ANDROID_GUI_CPUCONSUMER_H |
| 18 | #define ANDROID_GUI_CPUCONSUMER_H |
| 19 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 20 | #include <gui/ConsumerBase.h> |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 21 | |
| 22 | #include <ui/GraphicBuffer.h> |
| 23 | |
| 24 | #include <utils/String8.h> |
| 25 | #include <utils/Vector.h> |
| 26 | #include <utils/threads.h> |
| 27 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | /** |
| 32 | * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU |
| 33 | * access to the underlying gralloc buffers provided by BufferQueue. Multiple |
| 34 | * buffers may be acquired by it at once, to be used concurrently by the |
| 35 | * CpuConsumer owner. Sets gralloc usage flags to be software-read-only. |
| 36 | * This queue is synchronous by default. |
| 37 | */ |
| 38 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 39 | class CpuConsumer : public ConsumerBase |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 42 | typedef ConsumerBase::FrameAvailableListener FrameAvailableListener; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 43 | |
| 44 | struct LockedBuffer { |
| 45 | uint8_t *data; |
| 46 | uint32_t width; |
| 47 | uint32_t height; |
| 48 | PixelFormat format; |
| 49 | uint32_t stride; |
| 50 | Rect crop; |
| 51 | uint32_t transform; |
| 52 | uint32_t scalingMode; |
| 53 | int64_t timestamp; |
| 54 | uint64_t frameNumber; |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 55 | // Values below are only valid when using |
| 56 | // HAL_PIXEL_FORMAT_YCbCr_420_888, in which case LockedBuffer::data |
| 57 | // contains the Y channel, and stride is the Y channel stride. For other |
| 58 | // formats, these will all be 0. |
| 59 | uint8_t *dataCb; |
| 60 | uint8_t *dataCr; |
| 61 | uint32_t chromaStride; |
| 62 | uint32_t chromaStep; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | // Create a new CPU consumer. The maxLockedBuffers parameter specifies |
| 66 | // how many buffers can be locked for user access at the same time. |
Eino-Ville Talvala | eb0d129 | 2013-02-28 11:01:32 -0800 | [diff] [blame] | 67 | CpuConsumer(uint32_t maxLockedBuffers, bool synchronousMode = true); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 68 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 69 | virtual ~CpuConsumer(); |
| 70 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 71 | // set the name of the CpuConsumer that will be used to identify it in |
| 72 | // log messages. |
| 73 | void setName(const String8& name); |
| 74 | |
| 75 | // Gets the next graphics buffer from the producer and locks it for CPU use, |
| 76 | // filling out the passed-in locked buffer structure with the native pointer |
| 77 | // and metadata. Returns BAD_VALUE if no new buffer is available, and |
| 78 | // INVALID_OPERATION if the maximum number of buffers is already locked. |
| 79 | // |
| 80 | // Only a fixed number of buffers can be locked at a time, determined by the |
| 81 | // construction-time maxLockedBuffers parameter. If INVALID_OPERATION is |
| 82 | // returned by lockNextBuffer, then old buffers must be returned to the queue |
| 83 | // by calling unlockBuffer before more buffers can be acquired. |
| 84 | status_t lockNextBuffer(LockedBuffer *nativeBuffer); |
| 85 | |
| 86 | // Returns a locked buffer to the queue, allowing it to be reused. Since |
| 87 | // only a fixed number of buffers may be locked at a time, old buffers must |
| 88 | // be released by calling unlockBuffer to ensure new buffers can be acquired by |
| 89 | // lockNextBuffer. |
| 90 | status_t unlockBuffer(const LockedBuffer &nativeBuffer); |
| 91 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 92 | sp<IGraphicBufferProducer> getProducerInterface() const { return getBufferQueue(); } |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 93 | |
| 94 | private: |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 95 | // Maximum number of buffers that can be locked at a time |
| 96 | uint32_t mMaxLockedBuffers; |
| 97 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 98 | status_t releaseAcquiredBufferLocked(int lockedIdx); |
| 99 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 100 | virtual void freeBufferLocked(int slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 101 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 102 | // Tracking for buffers acquired by the user |
| 103 | struct AcquiredBuffer { |
| 104 | // Need to track the original mSlot index and the buffer itself because |
| 105 | // the mSlot entry may be freed/reused before the acquired buffer is |
| 106 | // released. |
| 107 | int mSlot; |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 108 | sp<GraphicBuffer> mGraphicBuffer; |
| 109 | void *mBufferPointer; |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 110 | |
| 111 | AcquiredBuffer() : |
| 112 | mSlot(BufferQueue::INVALID_BUFFER_SLOT), |
| 113 | mBufferPointer(NULL) { |
| 114 | } |
| 115 | }; |
| 116 | Vector<AcquiredBuffer> mAcquiredBuffers; |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 117 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 118 | // Count of currently locked buffers |
| 119 | uint32_t mCurrentLockedBuffers; |
| 120 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // namespace android |
| 124 | |
| 125 | #endif // ANDROID_GUI_CPUCONSUMER_H |