native-buffer-guidelines.md 3.5 KB
Newer Older
S
shegangbin 已提交
1
# NativeBuffer开发指导
2 3 4

## 场景介绍

5
NativeBuffer是提供**共享内存**的模块。开发者可以通过`NativeBuffer`接口实现共享内存的申请、使用、属性查询、释放等操作。
6 7
针对NativeBuffer,常见的开发场景如下:

S
shegangbin 已提交
8
* 通过`NativeBuffer`提供的Native API接口申请`OH_NativeBuffer`实例,获取内存的属性信息,把对应的ION内存映射到进程空间。
9 10 11 12 13 14 15

## 接口说明

| 接口名 | 描述 | 
| -------- | -------- |
| OH_NativeBuffer_Alloc (const OH_NativeBuffer_Config \*config) | 通过OH_NativeBuffer_Config创建OH_NativeBuffer实例,每次调用都会产生一个新的OH_NativeBuffer实例。 | 
| OH_NativeBuffer_Reference (OH_NativeBuffer \*buffer) | 将OH_NativeBuffer对象的引用计数加1。 | 
S
shegangbin 已提交
16
| OH_NativeBuffer_Unreference (OH_NativeBuffer \*buffer) | 将OH_NativeBuffer对象的引用计数减1,当引用计数为0的时候,该NativeBuffer对象会被析构掉。 | 
17 18 19 20 21 22 23 24 25
| OH_NativeBuffer_GetConfig (OH_NativeBuffer \*buffer, OH_NativeBuffer_Config \*config) | 用于获取OH_NativeBuffer的属性。 | 
| OH_NativeBuffer_Map (OH_NativeBuffer \*buffer, void \*\*virAddr) | 将OH_NativeBuffer对应的ION内存映射到进程空间。 | 
| OH_NativeBuffer_Unmap (OH_NativeBuffer \*buffer) | 将OH_NativeBuffer对应的ION内存从进程空间移除。 | 
| OH_NativeBuffer_GetSeqNum (OH_NativeBuffer \*buffer) | 获取OH_NativeBuffer的序列号。 | 

详细的接口说明请参考[native_buffer](../reference/native-apis/_o_h___native_buffer.md)

## 开发步骤

26
以下步骤描述了如何使用`NativeBuffer`提供的Native API接口,创建`OH_NativeBuffer`实例获取内存的属性信息,并把对应的ION内存映射到进程空间。
S
fix  
shegangbin 已提交
27

S
shegangbin 已提交
28 29 30 31 32 33 34
**添加动态链接库**

CMakeLists.txt中添加以下lib。
```txt
libnative_buffer.so
```

S
shegangbin 已提交
35
**头文件**
S
fix  
shegangbin 已提交
36 37 38
```c++
#include <native_buffer/native_buffer.h>
```
39

S
shegangbin 已提交
40
1. **创建OH_NativeBuffer实例**
41
    ```c++
S
shegangbin 已提交
42 43
    #include <iostream>

44
    OH_NativeBuffer_Config config {
S
shegangbin 已提交
45 46 47
        .width = 0x100,
        .height = 0x100,
    };
48 49
    OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
    if (buffer == nullptr) {
S
fix  
shegangbin 已提交
50
        std::cout << "OH_NativeBuffer_Alloc Failed" << std::endl;
51 52 53 54
    }
    ```
   
2. **将OH_NativeBuffer对应的ION内存映射到进程空间**
S
shegangbin 已提交
55
    应用如需要访问这块buffer的内存空间,需要通过OH_NativeBuffer_Map接口将buffer对应的ION内存映射到进程空间
56 57
    ```c++
    // 将ION内存映射到进程空间
S
fix  
shegangbin 已提交
58
    void* virAddr = nullptr;
S
shegangbin 已提交
59
    int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr); // 映射后通过第二个参数virAddr返回内存的首地址
S
fix  
shegangbin 已提交
60 61
    if (ret != 0) {
        std::cout << "OH_NativeBuffer_Map Failed" << std::endl;
62
    }
S
shegangbin 已提交
63 64

    // 使用后请及时将OH_NativeBuffer对应的ION内存从进程空间移除
65
    ret = OH_NativeBuffer_Unmap(buffer);
S
fix  
shegangbin 已提交
66 67
    if (ret != 0) {
        std::cout << "OH_NativeBuffer_Unmap Failed" << std::endl;
68 69 70 71 72 73
    }
    ```

3. **获取内存的属性信息**
    ```c++
    // 获取OH_NativeBuffer的属性
S
shegangbin 已提交
74 75
    OH_NativeBuffer_Config config2 = {};
    OH_NativeBuffer_GetConfig(buffer, &config2);
76 77 78 79 80 81 82 83
    // 获取OH_NativeBuffer的序列号
     uint32_t hwBufferID = OH_NativeBuffer_GetSeqNum(buffer);
    ```

4. **销毁OH_NativeBuffer**
    ```c++
    // 调用OH_NativeBuffer_Unreference引用计数减1,之后buffer的引用计数为0,buffer会销毁
    ret = OH_NativeBuffer_Unreference(buffer);
S
fix  
shegangbin 已提交
84 85
    if (ret != 0) {
        std::cout << "OH_NativeBuffer_Unreference Failed" << std::endl;
86 87
    }
    ```