1 /* 2 * Copyright (c) 2019, Linaro Limited 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #include <zephyr.h> 7 8 #include <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_alloc(size_t size)22struct video_buffer *video_buffer_alloc(size_t size) 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_alloc(&video_buffer_pool, 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_release(struct video_buffer * vbuf)54void video_buffer_release(struct video_buffer *vbuf) 55 { 56 struct mem_block *block = NULL; 57 int i; 58 59 /* vbuf to block */ 60 for (i = 0; i < ARRAY_SIZE(video_block); i++) { 61 if (video_block[i].data == vbuf->buffer) { 62 block = &video_block[i]; 63 break; 64 } 65 } 66 67 vbuf->buffer = NULL; 68 if (block) { 69 k_heap_free(&video_buffer_pool, block->data); 70 } 71 } 72