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/ANativeObjectBase.h> 24 #include <ui/PixelFormat.h> 25 #include <ui/Rect.h> 26 #include <utils/Flattenable.h> 27 #include <utils/RefBase.h> 28 29 30 struct ANativeWindowBuffer; 31 32 namespace android { 33 34 class GraphicBufferMapper; 35 36 // =========================================================================== 37 // GraphicBuffer 38 // =========================================================================== 39 40 class GraphicBuffer 41 : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >, 42 public Flattenable 43 { 44 public: 45 46 enum { 47 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER, 48 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY, 49 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN, 50 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK, 51 52 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER, 53 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY, 54 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN, 55 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK, 56 57 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, 58 59 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED, 60 61 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE, 62 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER, 63 USAGE_HW_2D = GRALLOC_USAGE_HW_2D, 64 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER, 65 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER, 66 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK 67 }; 68 69 GraphicBuffer(); 70 71 // creates w * h buffer 72 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage); 73 74 // create a buffer from an existing handle 75 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage, 76 uint32_t stride, native_handle_t* handle, bool keepOwnership); 77 78 // create a buffer from an existing ANativeWindowBuffer 79 GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership); 80 81 // return status 82 status_t initCheck() const; 83 84 uint32_t getWidth() const { return width; } 85 uint32_t getHeight() const { return height; } 86 uint32_t getStride() const { return stride; } 87 uint32_t getUsage() const { return usage; } 88 PixelFormat getPixelFormat() const { return format; } 89 Rect getBounds() const { return Rect(width, height); } 90 91 status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage); 92 93 status_t lock(uint32_t usage, void** vaddr); 94 status_t lock(uint32_t usage, const Rect& rect, void** vaddr); 95 // For HAL_PIXEL_FORMAT_YCbCr_420_888 96 status_t lockYCbCr(uint32_t usage, android_ycbcr *ycbcr); 97 status_t lockYCbCr(uint32_t usage, const Rect& rect, android_ycbcr *ycbcr); 98 status_t unlock(); 99 100 ANativeWindowBuffer* getNativeBuffer() const; 101 102 void setIndex(int index); 103 int getIndex() const; 104 105 // for debugging 106 static void dumpAllocationsToSystemLog(); 107 108 private: 109 virtual ~GraphicBuffer(); 110 111 enum { 112 ownNone = 0, 113 ownHandle = 1, 114 ownData = 2, 115 }; 116 117 inline const GraphicBufferMapper& getBufferMapper() const { 118 return mBufferMapper; 119 } 120 inline GraphicBufferMapper& getBufferMapper() { 121 return mBufferMapper; 122 } 123 uint8_t mOwner; 124 125 private: 126 friend class Surface; 127 friend class BpSurface; 128 friend class BnSurface; 129 friend class LightRefBase<GraphicBuffer>; 130 GraphicBuffer(const GraphicBuffer& rhs); 131 GraphicBuffer& operator = (const GraphicBuffer& rhs); 132 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const; 133 134 status_t initSize(uint32_t w, uint32_t h, PixelFormat format, 135 uint32_t usage); 136 137 void free_handle(); 138 139 // Flattenable interface 140 size_t getFlattenedSize() const; 141 size_t getFdCount() const; 142 status_t flatten(void* buffer, size_t size, 143 int fds[], size_t count) const; 144 status_t unflatten(void const* buffer, size_t size, 145 int fds[], size_t count); 146 147 148 GraphicBufferMapper& mBufferMapper; 149 ssize_t mInitCheck; 150 int mIndex; 151 152 // If we're wrapping another buffer then this reference will make sure it 153 // doesn't get freed. 154 sp<ANativeWindowBuffer> mWrappedBuffer; 155 }; 156 157 }; // namespace android 158 159 #endif // ANDROID_GRAPHIC_BUFFER_H 160