Mathias Agopian | 6950e42 | 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 | |
| 23 | #include <ui/android_native_buffer.h> |
| 24 | #include <ui/PixelFormat.h> |
| 25 | #include <ui/Rect.h> |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 26 | #include <utils/Flattenable.h> |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 27 | #include <pixelflinger/pixelflinger.h> |
| 28 | |
Iliyan Malchev | b2a153a | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 29 | struct ANativeWindowBuffer; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class GraphicBufferMapper; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 34 | |
| 35 | // =========================================================================== |
| 36 | // GraphicBuffer |
| 37 | // =========================================================================== |
| 38 | |
| 39 | class GraphicBuffer |
| 40 | : public EGLNativeBase< |
Iliyan Malchev | b2a153a | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 41 | ANativeWindowBuffer, |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 42 | GraphicBuffer, |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 43 | LightRefBase<GraphicBuffer> >, public Flattenable |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 44 | { |
| 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, |
| 52 | |
| 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 | d6f5bde | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 57 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 58 | USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, |
Glenn Kasten | d6f5bde | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 59 | |
| 60 | USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED, |
| 61 | |
Mathias Agopian | 6950e42 | 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, |
| 65 | USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK |
| 66 | }; |
| 67 | |
| 68 | GraphicBuffer(); |
| 69 | |
| 70 | // creates w * h buffer |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 71 | GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage); |
| 72 | |
| 73 | // create a buffer from an existing handle |
| 74 | GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage, |
| 75 | uint32_t stride, native_handle_t* handle, bool keepOwnership); |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 76 | |
Iliyan Malchev | b2a153a | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 77 | // create a buffer from an existing ANativeWindowBuffer |
| 78 | GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership); |
Jamie Gennis | 1ef773f | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 79 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 80 | // return status |
| 81 | status_t initCheck() const; |
| 82 | |
| 83 | uint32_t getWidth() const { return width; } |
| 84 | uint32_t getHeight() const { return height; } |
| 85 | uint32_t getStride() const { return stride; } |
| 86 | uint32_t getUsage() const { return usage; } |
| 87 | PixelFormat getPixelFormat() const { return format; } |
| 88 | Rect getBounds() const { return Rect(width, height); } |
| 89 | |
| 90 | status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage); |
| 91 | |
| 92 | status_t lock(uint32_t usage, void** vaddr); |
| 93 | status_t lock(uint32_t usage, const Rect& rect, void** vaddr); |
| 94 | status_t lock(GGLSurface* surface, uint32_t usage); |
| 95 | status_t unlock(); |
Mathias Agopian | e869aee | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 96 | |
Iliyan Malchev | b2a153a | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 97 | ANativeWindowBuffer* getNativeBuffer() const; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 98 | |
| 99 | void setIndex(int index); |
| 100 | int getIndex() const; |
| 101 | |
Mathias Agopian | e869aee | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 102 | // for debugging |
| 103 | static void dumpAllocationsToSystemLog(); |
| 104 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 105 | private: |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 106 | virtual ~GraphicBuffer(); |
| 107 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 108 | enum { |
| 109 | ownNone = 0, |
| 110 | ownHandle = 1, |
| 111 | ownData = 2, |
| 112 | }; |
| 113 | |
Mathias Agopian | 7623da4 | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 114 | inline const GraphicBufferMapper& getBufferMapper() const { |
| 115 | return mBufferMapper; |
| 116 | } |
| 117 | inline GraphicBufferMapper& getBufferMapper() { |
| 118 | return mBufferMapper; |
| 119 | } |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 120 | uint8_t mOwner; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | friend class Surface; |
| 124 | friend class BpSurface; |
| 125 | friend class BnSurface; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 126 | friend class SurfaceTextureClient; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 127 | friend class LightRefBase<GraphicBuffer>; |
| 128 | GraphicBuffer(const GraphicBuffer& rhs); |
| 129 | GraphicBuffer& operator = (const GraphicBuffer& rhs); |
| 130 | const GraphicBuffer& operator = (const GraphicBuffer& rhs) const; |
| 131 | |
| 132 | status_t initSize(uint32_t w, uint32_t h, PixelFormat format, |
| 133 | uint32_t usage); |
| 134 | |
Mathias Agopian | c86727f | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 135 | void free_handle(); |
| 136 | |
| 137 | // Flattenable interface |
| 138 | size_t getFlattenedSize() const; |
| 139 | size_t getFdCount() const; |
| 140 | status_t flatten(void* buffer, size_t size, |
| 141 | int fds[], size_t count) const; |
| 142 | status_t unflatten(void const* buffer, size_t size, |
| 143 | int fds[], size_t count); |
| 144 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 145 | |
| 146 | GraphicBufferMapper& mBufferMapper; |
| 147 | ssize_t mInitCheck; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 148 | int mIndex; |
Jamie Gennis | 1ef773f | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 149 | |
| 150 | // If we're wrapping another buffer then this reference will make sure it |
| 151 | // doesn't get freed. |
Iliyan Malchev | b2a153a | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 152 | sp<ANativeWindowBuffer> mWrappedBuffer; |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | }; // namespace android |
| 156 | |
| 157 | #endif // ANDROID_GRAPHIC_BUFFER_H |