| 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 |  | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 20 | #include <system/window.h> | 
 | 21 |  | 
| Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 22 | #include <gui/ConsumerBase.h> | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 23 | #include <gui/BufferQueue.h> | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 24 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 25 | #include <utils/Vector.h> | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 26 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 27 |  | 
 | 28 | namespace android { | 
 | 29 |  | 
| Mathias Agopian | 8f938a5 | 2013-07-12 22:06:26 -0700 | [diff] [blame] | 30 | class BufferQueue; | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 31 | class GraphicBuffer; | 
 | 32 | class String8; | 
| Mathias Agopian | 8f938a5 | 2013-07-12 22:06:26 -0700 | [diff] [blame] | 33 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 34 | /** | 
 | 35 |  * CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU | 
 | 36 |  * access to the underlying gralloc buffers provided by BufferQueue. Multiple | 
 | 37 |  * buffers may be acquired by it at once, to be used concurrently by the | 
 | 38 |  * CpuConsumer owner. Sets gralloc usage flags to be software-read-only. | 
 | 39 |  * This queue is synchronous by default. | 
 | 40 |  */ | 
 | 41 |  | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 42 | class CpuConsumer : public ConsumerBase | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 43 | { | 
 | 44 |   public: | 
| Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 45 |     typedef ConsumerBase::FrameAvailableListener FrameAvailableListener; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 46 |  | 
 | 47 |     struct LockedBuffer { | 
 | 48 |         uint8_t    *data; | 
 | 49 |         uint32_t    width; | 
 | 50 |         uint32_t    height; | 
 | 51 |         PixelFormat format; | 
 | 52 |         uint32_t    stride; | 
 | 53 |         Rect        crop; | 
 | 54 |         uint32_t    transform; | 
 | 55 |         uint32_t    scalingMode; | 
 | 56 |         int64_t     timestamp; | 
| Eino-Ville Talvala | 5b75a51 | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 57 |         android_dataspace dataSpace; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 58 |         uint64_t    frameNumber; | 
| Lajos Molnar | 6a26be7 | 2015-01-22 16:37:37 -0800 | [diff] [blame] | 59 |         // this is the same as format, except for formats that are compatible with | 
 | 60 |         // a flexible format (e.g. HAL_PIXEL_FORMAT_YCbCr_420_888). In the latter | 
 | 61 |         // case this contains that flexible format | 
 | 62 |         PixelFormat flexFormat; | 
 | 63 |         // Values below are only valid when using HAL_PIXEL_FORMAT_YCbCr_420_888 | 
 | 64 |         // or compatible format, in which case LockedBuffer::data | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 65 |         // contains the Y channel, and stride is the Y channel stride. For other | 
 | 66 |         // formats, these will all be 0. | 
 | 67 |         uint8_t    *dataCb; | 
 | 68 |         uint8_t    *dataCr; | 
 | 69 |         uint32_t    chromaStride; | 
 | 70 |         uint32_t    chromaStep; | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 71 |  | 
 | 72 |         LockedBuffer() : | 
 | 73 |             data(NULL), | 
 | 74 |             width(0), | 
 | 75 |             height(0), | 
 | 76 |             format(PIXEL_FORMAT_NONE), | 
 | 77 |             stride(0), | 
| Pablo Ceballos | 3b0d488 | 2015-10-23 16:20:19 -0700 | [diff] [blame] | 78 |             crop(Rect::EMPTY_RECT), | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 79 |             transform(0), | 
 | 80 |             scalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), | 
 | 81 |             timestamp(0), | 
 | 82 |             dataSpace(HAL_DATASPACE_UNKNOWN), | 
 | 83 |             frameNumber(0), | 
 | 84 |             flexFormat(PIXEL_FORMAT_NONE), | 
 | 85 |             dataCb(NULL), | 
 | 86 |             dataCr(NULL), | 
 | 87 |             chromaStride(0), | 
 | 88 |             chromaStep(0) | 
 | 89 |         {} | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 90 |     }; | 
 | 91 |  | 
 | 92 |     // Create a new CPU consumer. The maxLockedBuffers parameter specifies | 
 | 93 |     // how many buffers can be locked for user access at the same time. | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 94 |     CpuConsumer(const sp<IGraphicBufferConsumer>& bq, | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 95 |             size_t maxLockedBuffers, bool controlledByApp = false); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 96 |  | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 97 |     virtual ~CpuConsumer(); | 
 | 98 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 99 |     // set the name of the CpuConsumer that will be used to identify it in | 
 | 100 |     // log messages. | 
 | 101 |     void setName(const String8& name); | 
 | 102 |  | 
 | 103 |     // Gets the next graphics buffer from the producer and locks it for CPU use, | 
 | 104 |     // filling out the passed-in locked buffer structure with the native pointer | 
 | 105 |     // and metadata. Returns BAD_VALUE if no new buffer is available, and | 
| Igor Murashkin | a5b7513 | 2013-08-14 18:49:12 -0700 | [diff] [blame] | 106 |     // NOT_ENOUGH_DATA if the maximum number of buffers is already locked. | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 107 |     // | 
 | 108 |     // Only a fixed number of buffers can be locked at a time, determined by the | 
 | 109 |     // construction-time maxLockedBuffers parameter. If INVALID_OPERATION is | 
 | 110 |     // returned by lockNextBuffer, then old buffers must be returned to the queue | 
 | 111 |     // by calling unlockBuffer before more buffers can be acquired. | 
 | 112 |     status_t lockNextBuffer(LockedBuffer *nativeBuffer); | 
 | 113 |  | 
 | 114 |     // Returns a locked buffer to the queue, allowing it to be reused. Since | 
 | 115 |     // only a fixed number of buffers may be locked at a time, old buffers must | 
 | 116 |     // be released by calling unlockBuffer to ensure new buffers can be acquired by | 
 | 117 |     // lockNextBuffer. | 
 | 118 |     status_t unlockBuffer(const LockedBuffer &nativeBuffer); | 
 | 119 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 120 |   private: | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 121 |     // Maximum number of buffers that can be locked at a time | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 122 |     size_t mMaxLockedBuffers; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 123 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 124 |     status_t releaseAcquiredBufferLocked(size_t lockedIdx); | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 125 |  | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 126 |     virtual void freeBufferLocked(int slotIndex); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 127 |  | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 128 |     // Tracking for buffers acquired by the user | 
 | 129 |     struct AcquiredBuffer { | 
 | 130 |         // Need to track the original mSlot index and the buffer itself because | 
 | 131 |         // the mSlot entry may be freed/reused before the acquired buffer is | 
 | 132 |         // released. | 
 | 133 |         int mSlot; | 
| Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 134 |         sp<GraphicBuffer> mGraphicBuffer; | 
 | 135 |         void *mBufferPointer; | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 136 |  | 
 | 137 |         AcquiredBuffer() : | 
 | 138 |                 mSlot(BufferQueue::INVALID_BUFFER_SLOT), | 
 | 139 |                 mBufferPointer(NULL) { | 
 | 140 |         } | 
 | 141 |     }; | 
 | 142 |     Vector<AcquiredBuffer> mAcquiredBuffers; | 
| Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 143 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 144 |     // Count of currently locked buffers | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 145 |     size_t mCurrentLockedBuffers; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 146 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 147 | }; | 
 | 148 |  | 
 | 149 | } // namespace android | 
 | 150 |  | 
 | 151 | #endif // ANDROID_GUI_CPUCONSUMER_H |