1 /****************************************************************************
2 *
3 *    The MIT License (MIT)
4 *
5 *    Copyright 2012 - 2020 Vivante Corporation, Santa Clara, California.
6 *    All Rights Reserved.
7 *
8 *    Permission is hereby granted, free of charge, to any person obtaining
9 *    a copy of this software and associated documentation files (the
10 *    'Software'), to deal in the Software without restriction, including
11 *    without limitation the rights to use, copy, modify, merge, publish,
12 *    distribute, sub license, and/or sell copies of the Software, and to
13 *    permit persons to whom the Software is furnished to do so, subject
14 *    to the following conditions:
15 *
16 *    The above copyright notice and this permission notice (including the
17 *    next paragraph) shall be included in all copies or substantial
18 *    portions of the Software.
19 *
20 *    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
21 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 *    IN NO EVENT SHALL VIVANTE AND/OR ITS SUPPLIERS BE LIABLE FOR ANY
24 *    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 *    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 *****************************************************************************/
29 
30 #include <string.h>
31 
32 #include "vg_lite.h"
33 
_memcpy(void * dst,void * src,int size)34 static void _memcpy(void *dst, void *src, int size) {
35     int i;
36     for (i = 0; i < size; i++) {
37         ((unsigned char*)dst)[i] = ((unsigned char*)src)[i];
38     }
39 }
40 
41 /* Get the plane memory pointer and strides info. */
get_buffer_planes(vg_lite_buffer_t * buffer,uint8_t ** memory,uint32_t * strides)42 static uint32_t get_buffer_planes(vg_lite_buffer_t *buffer,
43                               uint8_t **memory,
44                               uint32_t *strides)
45 {
46     uint32_t count = 1;
47 
48     switch (buffer->format) {
49         case VG_LITE_RGBA8888:
50         case VG_LITE_BGRA8888:
51         case VG_LITE_RGBX8888:
52         case VG_LITE_BGRX8888:
53         case VG_LITE_RGB565:
54         case VG_LITE_BGR565:
55         case VG_LITE_RGBA4444:
56         case VG_LITE_BGRA4444:
57         case VG_LITE_BGRA5551:
58         case VG_LITE_A8:
59         case VG_LITE_L8:
60         case VG_LITE_A4:
61         case VG_LITE_INDEX_1:
62         case VG_LITE_INDEX_2:
63         case VG_LITE_INDEX_4:
64         case VG_LITE_INDEX_8:
65         case VG_LITE_YUYV:
66         case VG_LITE_YUY2:
67         case VG_LITE_RGBA2222:
68             count = 1;
69             memory[0] = (uint8_t *)buffer->memory;
70             memory[1] = memory[2] = ((uint8_t*)0);
71             strides[0] = buffer->stride;
72             strides[1] = strides[2] = 0;
73             break;
74 
75         case VG_LITE_NV12:
76         case VG_LITE_NV16:
77             count = 2;
78             memory[0] = (uint8_t *)buffer->memory;
79             memory[1] = (uint8_t *)buffer->yuv.uv_memory;
80             memory[2] = 0;
81             strides[0] = buffer->stride;
82             strides[1] = buffer->yuv.uv_stride;
83             strides[2] = 0;
84             break;
85 
86         case VG_LITE_AYUY2:
87             count = 2;
88             memory[0] = (uint8_t *)buffer->memory;
89             memory[1] = 0;
90             memory[2] = (uint8_t *)buffer->yuv.v_memory;
91             strides[0] = buffer->stride;
92             strides[1] = 0;
93             strides[2] = buffer->yuv.alpha_stride;
94             break;
95 
96         case VG_LITE_ANV12:
97             count = 3;
98             memory[0] = (uint8_t *)buffer->memory;
99             memory[1] = (uint8_t *)buffer->yuv.uv_memory;
100             memory[2] = (uint8_t *)buffer->yuv.v_memory;
101             strides[0] = buffer->stride;
102             strides[1] = buffer->yuv.uv_stride;
103             strides[2] = buffer->yuv.alpha_stride;
104             break;
105 
106         case VG_LITE_YV12:
107         case VG_LITE_YV24:
108         case VG_LITE_YV16:
109             count = 3;
110             memory[0] = (uint8_t *)buffer->memory;
111             memory[1] = (uint8_t *)buffer->yuv.uv_memory;
112             memory[2] = (uint8_t *)buffer->yuv.v_memory;
113             strides[0] = buffer->stride;
114             strides[1] = buffer->yuv.uv_stride;
115             strides[2] = buffer->yuv.v_stride;
116             break;
117 
118         case VG_LITE_YUY2_TILED:
119         case VG_LITE_NV12_TILED:
120         case VG_LITE_ANV12_TILED:
121         case VG_LITE_AYUY2_TILED:
122         default:
123             count = 0;
124 
125             break;
126     }
127     return count;
128 }
129 
vg_lite_buffer_upload(vg_lite_buffer_t * buffer,uint8_t * data[3],uint32_t stride[3])130 vg_lite_error_t vg_lite_buffer_upload(vg_lite_buffer_t  *buffer,
131                                       uint8_t           *data[3],
132                                       uint32_t           stride[3])
133 {
134     vg_lite_error_t error = VG_LITE_SUCCESS;
135     int32_t plane_count;
136     uint8_t  *buffer_memory[3] = {((uint8_t*)0)};
137     uint32_t  buffer_strides[3] = {0};
138     uint8_t  *pdata;
139     int32_t i, j;
140 
141     /* Get buffer memory info. */
142     plane_count = get_buffer_planes(buffer, buffer_memory, buffer_strides);
143 
144     if (plane_count > 0 && plane_count <= 3) {
145         /* Copy the data to buffer. */
146         for (i = 0; i < plane_count;  i++) {
147             pdata = data[i];
148             for (j = 0; j < buffer->height; j++) {
149                 _memcpy(buffer_memory[i], pdata, buffer_strides[i]);
150                 buffer_memory[i] += buffer_strides[i];
151                 pdata += stride[i];
152             }
153         }
154     }
155     else {
156         error = VG_LITE_INVALID_ARGUMENT;
157     }
158 
159     return error;
160 }
161