| 175 | static std::shared_ptr<AndroidHardwareBufferCompat> gFunction; |
| 176 | |
| 177 | static AHardwareBuffer* creatAHardwareBuffer(int width, int height, void *data){ |
| 178 | // 创建和初始化硬件缓冲区 |
| 179 | AHardwareBuffer_Desc bufferDesc = {}; |
| 180 | bufferDesc.width = width; |
| 181 | bufferDesc.height = height; |
| 182 | bufferDesc.layers = 1; |
| 183 | bufferDesc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
| 184 | bufferDesc.usage = AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE; |
| 185 | |
| 186 | AHardwareBuffer* buffer = nullptr; |
| 187 | int result = gFunction->Allocate(&bufferDesc, &buffer); |
| 188 | if(result != 0) { |
| 189 | // Handle allocation error |
| 190 | MNN_PRINT("alloc AHardwareBuffer failed %d\n", result); |
| 191 | } |
| 192 | |
| 193 | if(nullptr != data){ |
| 194 | void* map = nullptr; |
| 195 | ARect rect = { 0, 0, width, height }; // Define the region to lock |
| 196 | result = gFunction->Lock(buffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, &rect, &map); |
| 197 | if (result != 0) { |
| 198 | // Handle lock failure |
| 199 | MNN_PRINT("Handle lock failed\n"); |
| 200 | } |
| 201 | if (map) { |
| 202 | // Now write your pixel data to 'data' |
| 203 | // For example, fill it with a solid color: |
| 204 | memcpy(map, data, width * height * 4); // Assuming RGBA8888 format |
| 205 | } |
| 206 | |
| 207 | gFunction->Unlock(buffer, nullptr); |
| 208 | } |
| 209 | return buffer; |
| 210 | } |
| 211 | static void copyDataFromAHardWareBuffer(AHardwareBuffer* buffer, int width, int height, void *data){ |
| 212 | int result = 0; |
| 213 | if(nullptr != data){ |