Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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_GRAPHIC_BUFFER_H |
| 18 | #define ANDROID_GRAPHIC_BUFFER_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 23 | #include <ui/ANativeObjectBase.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 24 | #include <ui/PixelFormat.h> |
| 25 | #include <ui/Rect.h> |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 26 | #include <utils/Flattenable.h> |
Mathias Agopian | e041716 | 2013-03-06 18:50:52 -0800 | [diff] [blame] | 27 | #include <utils/RefBase.h> |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 28 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 29 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 30 | struct ANativeWindowBuffer; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | class GraphicBufferMapper; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 35 | |
| 36 | // =========================================================================== |
| 37 | // GraphicBuffer |
| 38 | // =========================================================================== |
| 39 | |
| 40 | class GraphicBuffer |
Mathias Agopian | e041716 | 2013-03-06 18:50:52 -0800 | [diff] [blame] | 41 | : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >, |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 42 | public Flattenable<GraphicBuffer> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 43 | { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 44 | friend class Flattenable<GraphicBuffer>; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 45 | public: |
| 46 | |
| 47 | enum { |
| 48 | USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER, |
| 49 | USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY, |
| 50 | USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN, |
| 51 | USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 52 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 53 | USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER, |
| 54 | USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY, |
| 55 | USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 56 | USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK, |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 57 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 58 | USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 59 | |
| 60 | USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED, |
| 61 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 62 | USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE, |
| 63 | USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER, |
| 64 | USAGE_HW_2D = GRALLOC_USAGE_HW_2D, |
Jamie Gennis | 3599bf2 | 2011-08-10 11:48:07 -0700 | [diff] [blame] | 65 | USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER, |
Jamie Gennis | b7d87c4 | 2011-11-21 16:51:47 -0800 | [diff] [blame] | 66 | USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER, |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 67 | USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK, |
| 68 | |
| 69 | USAGE_CURSOR = GRALLOC_USAGE_CURSOR, |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | GraphicBuffer(); |
| 73 | |
| 74 | // creates w * h buffer |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 75 | GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 76 | uint32_t inUsage); |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 77 | |
| 78 | // create a buffer from an existing handle |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 79 | GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 80 | uint32_t inUsage, uint32_t inStride, native_handle_t* inHandle, |
| 81 | bool keepOwnership); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 82 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 83 | // create a buffer from an existing ANativeWindowBuffer |
| 84 | GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership); |
Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 85 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 86 | // return status |
| 87 | status_t initCheck() const; |
| 88 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 89 | uint32_t getWidth() const { return static_cast<uint32_t>(width); } |
| 90 | uint32_t getHeight() const { return static_cast<uint32_t>(height); } |
| 91 | uint32_t getStride() const { return static_cast<uint32_t>(stride); } |
| 92 | uint32_t getUsage() const { return static_cast<uint32_t>(usage); } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 93 | PixelFormat getPixelFormat() const { return format; } |
| 94 | Rect getBounds() const { return Rect(width, height); } |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 95 | uint64_t getId() const { return mId; } |
| 96 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 97 | uint32_t getGenerationNumber() const { return mGenerationNumber; } |
| 98 | void setGenerationNumber(uint32_t generation) { |
| 99 | mGenerationNumber = generation; |
| 100 | } |
| 101 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 102 | status_t reallocate(uint32_t inWidth, uint32_t inHeight, |
| 103 | PixelFormat inFormat, uint32_t inUsage); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 104 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 105 | bool needsReallocation(uint32_t inWidth, uint32_t inHeight, |
| 106 | PixelFormat inFormat, uint32_t inUsage); |
| 107 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 108 | status_t lock(uint32_t inUsage, void** vaddr); |
| 109 | status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 110 | // For HAL_PIXEL_FORMAT_YCbCr_420_888 |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 111 | status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr); |
| 112 | status_t lockYCbCr(uint32_t inUsage, const Rect& rect, |
| 113 | android_ycbcr *ycbcr); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 114 | status_t unlock(); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 115 | status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd); |
| 116 | status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, |
| 117 | int fenceFd); |
| 118 | status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr, |
| 119 | int fenceFd); |
| 120 | status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, |
| 121 | android_ycbcr *ycbcr, int fenceFd); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 122 | status_t unlockAsync(int *fenceFd); |
Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 123 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 124 | ANativeWindowBuffer* getNativeBuffer() const; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 125 | |
Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 126 | // for debugging |
| 127 | static void dumpAllocationsToSystemLog(); |
| 128 | |
Mathias Agopian | 87f9b87 | 2013-07-31 19:18:22 -0700 | [diff] [blame] | 129 | // Flattenable protocol |
| 130 | size_t getFlattenedSize() const; |
| 131 | size_t getFdCount() const; |
| 132 | status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; |
| 133 | status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); |
| 134 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 135 | private: |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 136 | ~GraphicBuffer(); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 137 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 138 | enum { |
| 139 | ownNone = 0, |
| 140 | ownHandle = 1, |
| 141 | ownData = 2, |
| 142 | }; |
| 143 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 144 | inline const GraphicBufferMapper& getBufferMapper() const { |
| 145 | return mBufferMapper; |
| 146 | } |
| 147 | inline GraphicBufferMapper& getBufferMapper() { |
| 148 | return mBufferMapper; |
| 149 | } |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 150 | uint8_t mOwner; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | friend class Surface; |
| 154 | friend class BpSurface; |
| 155 | friend class BnSurface; |
| 156 | friend class LightRefBase<GraphicBuffer>; |
| 157 | GraphicBuffer(const GraphicBuffer& rhs); |
| 158 | GraphicBuffer& operator = (const GraphicBuffer& rhs); |
| 159 | const GraphicBuffer& operator = (const GraphicBuffer& rhs) const; |
| 160 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 161 | status_t initSize(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 162 | uint32_t inUsage); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 163 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 164 | void free_handle(); |
| 165 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 166 | GraphicBufferMapper& mBufferMapper; |
| 167 | ssize_t mInitCheck; |
Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 168 | |
| 169 | // If we're wrapping another buffer then this reference will make sure it |
| 170 | // doesn't get freed. |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 171 | sp<ANativeWindowBuffer> mWrappedBuffer; |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 172 | |
| 173 | uint64_t mId; |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 174 | |
| 175 | // Stores the generation number of this buffer. If this number does not |
| 176 | // match the BufferQueue's internal generation number (set through |
| 177 | // IGBP::setGenerationNumber), attempts to attach the buffer will fail. |
| 178 | uint32_t mGenerationNumber; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | }; // namespace android |
| 182 | |
| 183 | #endif // ANDROID_GRAPHIC_BUFFER_H |