1 /* 2 * Copyright (c) 2019, Linaro Limited 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #include <zephyr/kernel.h> 7 8 #include <zephyr/drivers/video.h> 9 10 K_HEAP_DEFINE(video_buffer_pool, 11 CONFIG_VIDEO_BUFFER_POOL_SZ_MAX * 12 CONFIG_VIDEO_BUFFER_POOL_NUM_MAX); 13 14 static struct video_buffer video_buf[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX]; 15 16 struct mem_block { 17 void *data; 18 }; 19 20 static struct mem_block video_block[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX]; 21 video_buffer_aligned_alloc(size_t size,size_t align)22struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align) 23 { 24 struct video_buffer *vbuf = NULL; 25 struct mem_block *block; 26 int i; 27 28 /* find available video buffer */ 29 for (i = 0; i < ARRAY_SIZE(video_buf); i++) { 30 if (video_buf[i].buffer == NULL) { 31 vbuf = &video_buf[i]; 32 block = &video_block[i]; 33 break; 34 } 35 } 36 37 if (vbuf == NULL) { 38 return NULL; 39 } 40 41 /* Alloc buffer memory */ 42 block->data = k_heap_aligned_alloc(&video_buffer_pool, align, size, K_FOREVER); 43 if (block->data == NULL) { 44 return NULL; 45 } 46 47 vbuf->buffer = block->data; 48 vbuf->size = size; 49 vbuf->bytesused = 0; 50 51 return vbuf; 52 } 53 video_buffer_alloc(size_t size)54struct video_buffer *video_buffer_alloc(size_t size) 55 { 56 return video_buffer_aligned_alloc(size, sizeof(void *)); 57 } 58 video_buffer_release(struct video_buffer * vbuf)59void video_buffer_release(struct video_buffer *vbuf) 60 { 61 struct mem_block *block = NULL; 62 int i; 63 64 /* vbuf to block */ 65 for (i = 0; i < ARRAY_SIZE(video_block); i++) { 66 if (video_block[i].data == vbuf->buffer) { 67 block = &video_block[i]; 68 break; 69 } 70 } 71 72 vbuf->buffer = NULL; 73 if (block) { 74 k_heap_free(&video_buffer_pool, block->data); 75 } 76 } 77