源码:android14
硬件抽象层的接口定义在 hardware.h
/hardware/libhardware/include/hardware/hardware.h
定义了以下三个结构体
77 struct hw_module_t;
78 struct hw_module_methods_t;
79 struct hw_device_t;
typedef struct hw_module_t {
87 /** tag must be initialized to HARDWARE_MODULE_TAG */
88 uint32_t tag;
89
90 /**
91 * The API version of the implemented module. The module owner is
92 * responsible for updating the version when a module interface has
93 * changed.
94 *
95 * The derived modules such as gralloc and audio own and manage this field.
96 * The module user must interpret the version field to decide whether or
97 * not to inter-operate with the supplied module implementation.
98 * For example, SurfaceFlinger is responsible for making sure that
99 * it knows how to manage different versions of the gralloc-module API,
100 * and AudioFlinger must know how to do the same for audio-module API.
101 *
102 * The module API version should include a major and a minor component.
103 * For example, version 1.0 could be represented as 0x0100. This format
104 * implies that versions 0x0100-0x01ff are all API-compatible.
105 *
106 * In the future, libhardware will expose a hw_get_module_version()
107 * (or equivalent) function that will take minimum/maximum supported
108 * versions as arguments and would be able to reject modules with
109 * versions outside of the supplied range.
110 */
111 uint16_t module_api_version;
112 #define version_major module_api_version
113 /**
114 * version_major/version_minor defines are supplied here for temporary
115 * source code compatibility. They will be removed in the next version.
116 * ALL clients must convert to the new version format.
117 */
118
119 /**
120 * The API version of the HAL module interface. This is meant to
121 * version the hw_module_t, hw_module_methods_t, and hw_device_t
122 * structures and definitions.
123 *
124 * The HAL interface owns this field. Module users/implementations
125 * must NOT rely on this value for version information.
126 *
127 * Presently, 0 is the only valid value.
128 */
129 uint16_t hal_api_version;
130 #define version_minor hal_api_version
131
132 /** Identifier of module */
133 const char *id;
134
135 /** Name of this module */
136 const char *name;
137
138 /** Author/owner/implementor of the module */
139 const char *author;
140
141 /** Modules methods */
142 struct hw_module_methods_t* methods;
143
144 /** module's dso */
145 void* dso;
146
147 #ifdef __LP64__
148 uint64_t reserved[32-7];
149 #else
150 /** padding to 128 bytes, reserved for future use */
151 uint32_t reserved[32-7];
152 #endif
153
154 } hw_module_t;
156 typedef struct hw_module_methods_t {
157 /** Open a specific device */
158 int (*open)(const struct hw_module_t* module, const char* id,
159 struct hw_device_t** device);
160
161 } hw_module_methods_t;
typedef struct hw_device_t {
168 /** tag must be initialized to HARDWARE_DEVICE_TAG */
169 uint32_t tag;
170
171 /**
172 * Version of the module-specific device API. This value is used by
173 * the derived-module user to manage different device implementations.
174 *
175 * The module user is responsible for checking the module_api_version
176 * and device version fields to ensure that the user is capable of
177 * communicating with the specific module implementation.
178 *
179 * One module can support multiple devices with different versions. This
180 * can be useful when a device interface changes in an incompatible way
181 * but it is still necessary to support older implementations at the same
182 * time. One such example is the Camera 2.0 API.
183 *
184 * This field is interpreted by the module user and is ignored by the
185 * HAL interface itself.
186 */
187 uint32_t version;
188
189 /** reference to the module this device belongs to */
190 struct hw_module_t* module;
191
192 /** padding reserved for future use */
193 #ifdef __LP64__
194 uint64_t reserved[12];
195 #else
196 uint32_t reserved[12];
197 #endif
198
199 /** Close this device */
200 int (*close)(struct hw_device_t* device);
201
202 } hw_device_t;
其中还有 hw_get_module方法,该方法用于获取模块
213 #define HAL_MODULE_INFO_SYM HMI
214
215 /**
216 * Name of the hal_module_info as a string
217 */
218 #define HAL_MODULE_INFO_SYM_AS_STR "HMI"
int hw_get_module(const char *id, const struct hw_module_t **module);
第一个参数获取模块的 id,根据id查找对应库文件,匹配成功后通过第二个参数返回模块对象。
Gralloc 图形缓冲
当期Gralloc 已经有从1.0到4.0了,以4.0参考为例
在这里插入代码片 package android.hardware.graphics.allocator@4.0;
18
19 import android.hardware.graphics.mapper@4.0;
20
21 interface IAllocator {
2
42 allocate(BufferDescriptor descriptor, uint32_t count)
43 generates (Error error,
44 uint32_t stride,
45 vec<handle> buffers);
46 };
图形缓存用来保存图形数据的内存,可以被不同的进程访问,所以也可以说是共享内存,在android系统中, 有Gralloc负责分配图形缓存。
通过上述的代码可以看懂,接口类为IAllocator,里面有一个allocate的接口,可以看到里面有两个参数
第一个参数descriptor:包含缓存的信息,宽,高,格式等。
第二个参数count:表示申请缓存的个数,图形缓存以handle格式返回。
Graphic 图形缓存
/frameworks/native/libs/nativebase/include/nativebase/nativebase.h
typedef struct ANativeWindowBuffer
67 {
68 #ifdef __cplusplus
69 ANativeWindowBuffer() {
70 common.magic = ANDROID_NATIVE_BUFFER_MAGIC;
71 common.version = sizeof(ANativeWindowBuffer);
72 memset(common.reserved, 0, sizeof(common.reserved));
73 }
74
75 // Implement the methods that sp<ANativeWindowBuffer> expects so that it
76 // can be used to automatically refcount ANativeWindowBuffer's.
77 void incStrong(const void* /*id*/) const {
78 common.incRef(const_cast<android_native_base_t*>(&common));
79 }
80 void decStrong(const void* /*id*/) const {
81 common.decRef(const_cast<android_native_base_t*>(&common));
82 }
83 #endif
84
85 struct android_native_base_t common;
86
87 int width;
88 int height;
89 int stride;
90 int format;
91 int usage_deprecated;
92 uintptr_t layerCount;
93
94 void* reserved[1];
95
96 const native_handle_t* handle;
97 uint64_t usage;
98
99 // we needed extra space for storing the 64-bits usage flags
100 // the number of slots to use from reserved_proc depends on the
101 // architecture.
102 void* reserved_proc[8 - (sizeof(uint64_t) / sizeof(void*))];
103 } ANativeWindowBuffer_t;
在ANativeWindowBuffer中定义了 const native_handle_t* handle;
接下来定义GraphicBuffer.h
/frameworks/native/libs/ui/include/ui/GraphicBuffer.h
49 class GraphicBuffer
50 : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>,
51 public Flattenable<GraphicBuffer>
52 {
53 friend class Flattenable<GraphicBuffer>;
54 public:
55
56 enum {
57 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
58 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
59 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
60 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
61
62 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
63 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
64 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
65 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
66
67 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
68
69 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
70
71 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
72 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
73 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
74 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
75 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
76 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK,
77
78 USAGE_CURSOR = GRALLOC_USAGE_CURSOR,
79 };
可以看到GraphicBuffer类 继承于ANativeWindowBuffer,因为要用前面ANativeWindowBuffer 中定义的handle,handle的作用用于保存缓冲对应文件描述符。
/frameworks/native/libs/ui/GraphicBuffer.cpp
78 GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
79 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
80 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
81 {
82 }
180 status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
181 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
182 std::string requestorName)
183 {
184 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
185 uint32_t outStride = 0;
186 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
187 inUsage, &handle, &outStride, mId,
188 std::move(requestorName));
189 if (err == NO_ERROR) {
190 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
191
192 width = static_cast<int>(inWidth);
193 height = static_cast<int>(inHeight);
194 format = inFormat;
195 layerCount = inLayerCount;
196 usage = inUsage;
197 usage_deprecated = int(usage);
198 stride = static_cast<int>(outStride);
199 }
200 return err;
201 }
GraphicBufferAllocator& allocator = GraphicBufferAllocator::get() 可以看到获取 allocator 的图形缓存的引用。
申请成功后缓存保存到handle中,要使用需要获取GraphicBuffer 的对象去除地址进行保存图形数据。
同样还是在GraphicBuffer中获取地址。
status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
300 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
301 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
302 }
303
304 status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
305 const Rect& rect, void** vaddr, int fenceFd,
306 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
307 if (rect.left < 0 || rect.right > width ||
308 rect.top < 0 || rect.bottom > height) {
309 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
310 rect.left, rect.top, rect.right, rect.bottom,
311 width, height);
312 return BAD_VALUE;
313 }
314
315 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect,
316 vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
317
318 return res;
319 }
通过调用GraphicBuffer 的lockAsync方法即可获取缓存的内存地址,应用程序得到地址后可以向图形缓冲读写图形数据。