1.3.13.1607/3_00_00_08+flat+Lindent
[sgx.git] / pvr / bufferclass_example_private.c
1 /**********************************************************************
2  *
3  * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
4  * 
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  * 
9  * This program is distributed in the hope it will be useful but, except 
10  * as otherwise stated in writing, without any warranty; without even the 
11  * implied warranty of merchantability or fitness for a particular purpose. 
12  * See the GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  * 
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * Imagination Technologies Ltd. <gpl-support@imgtec.com>
23  * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK 
24  *
25  ******************************************************************************/
26
27 #include "bufferclass_example.h"
28
29 #define MIN(a,b) ((a)<(b)?(a):(b))
30
31 void FillYUV420Image(void *pvDest, int width, int height, int bytestride)
32 {
33         static int iPhase = 0;
34         int i, j;
35         unsigned char u, v, y;
36         unsigned char *pui8y = (unsigned char *)pvDest;
37         unsigned short *pui16uv;
38         unsigned int count = 0;
39
40         for (j = 0; j < height; j++) {
41                 for (i = 0; i < width; i++) {
42                         y = (((i + iPhase) >> 6) % (2) == 0) ? 0x7f : 0x00;
43
44                         pui8y[count++] = y;
45                 }
46         }
47
48         pui16uv =
49             (unsigned short *)((unsigned char *)pvDest + (width * height));
50         count = 0;
51
52         for (j = 0; j < height; j += 2) {
53                 for (i = 0; i < width; i += 2) {
54                         u = (j <
55                              (height / 2)) ? ((i <
56                                                (width /
57                                                 2)) ? 0xFF : 0x33) : ((i <
58                                                                        (width /
59                                                                         2)) ?
60                                                                       0x33 :
61                                                                       0xAA);
62                         v = (j <
63                              (height / 2)) ? ((i <
64                                                (width /
65                                                 2)) ? 0xAC : 0x0) : ((i <
66                                                                       (width /
67                                                                        2)) ?
68                                                                      0x03 :
69                                                                      0xEE);
70
71                         pui16uv[count++] = (v << 8) | u;
72
73                 }
74         }
75
76         iPhase++;
77 }
78
79 void FillYUV422Image(void *pvDest, int width, int height, int bytestride)
80 {
81         static int iPhase = 0;
82         int x, y;
83         unsigned char u, v, y0, y1;
84         unsigned int *pui32yuv = (unsigned int *)pvDest;
85         unsigned int count = 0;
86
87         for (y = 0; y < height; y++) {
88                 for (x = 0; x < width; x += 2) {
89                         u = (y <
90                              (height / 2)) ? ((x <
91                                                (width /
92                                                 2)) ? 0xFF : 0x33) : ((x <
93                                                                        (width /
94                                                                         2)) ?
95                                                                       0x33 :
96                                                                       0xAA);
97                         v = (y <
98                              (height / 2)) ? ((x <
99                                                (width /
100                                                 2)) ? 0xAA : 0x0) : ((x <
101                                                                       (width /
102                                                                        2)) ?
103                                                                      0x03 :
104                                                                      0xEE);
105
106                         y0 = y1 =
107                             (((x + iPhase) >> 6) % (2) == 0) ? 0x7f : 0x00;
108
109                         pui32yuv[count++] =
110                             (y1 << 24) | (v << 16) | (y0 << 8) | u;
111
112                 }
113         }
114
115         iPhase++;
116 }
117
118 void FillRGB565Image(void *pvDest, int width, int height, int bytestride)
119 {
120         int i, Count;
121         unsigned long *pui32Addr = (unsigned long *)pvDest;
122         unsigned short *pui16Addr = (unsigned short *)pvDest;
123         unsigned long Colour32;
124         unsigned short Colour16;
125         static unsigned char Colour8 = 0;
126
127         Colour16 =
128             (Colour8 >> 3) | ((Colour8 >> 2) << 5) | ((Colour8 >> 3) << 11);
129         Colour32 = Colour16 | Colour16 << 16;
130
131         Count = (height * bytestride) >> 2;
132
133         for (i = 0; i < Count; i++) {
134                 pui32Addr[i] = Colour32;
135         }
136
137         Count = height;
138
139         pui16Addr = (unsigned short *)((unsigned char *)pvDest + (2 * Colour8));
140
141         for (i = 0; i < Count; i++) {
142                 *pui16Addr = 0xF800;
143
144                 pui16Addr =
145                     (unsigned short *)((unsigned char *)pui16Addr + bytestride);
146         }
147         Count = bytestride >> 2;
148
149         pui32Addr =
150             (unsigned long *)((unsigned char *)pvDest +
151                               (bytestride * (MIN(height - 1, 0xFF) - Colour8)));
152
153         for (i = 0; i < Count; i++) {
154                 pui32Addr[i] = 0x001F001F;
155         }
156
157         Colour8 = (Colour8 + 1) % MIN(height - 1, 0xFF);
158 }
159
160 int FillBuffer(unsigned int ui32BufferIndex)
161 {
162         BC_EXAMPLE_DEVINFO *psDevInfo = GetAnchorPtr();
163         BC_EXAMPLE_BUFFER *psBuffer;
164         BUFFER_INFO *psBufferInfo;
165         PVRSRV_SYNC_DATA *psSyncData;
166
167         if (psDevInfo == IMG_NULL) {
168                 return -1;
169         }
170
171         psBuffer = &psDevInfo->psSystemBuffer[ui32BufferIndex];
172         psBufferInfo = &psDevInfo->sBufferInfo;
173
174         psSyncData = psBuffer->psSyncData;
175
176         if (psSyncData) {
177
178                 if (psSyncData->ui32ReadOpsPending !=
179                     psSyncData->ui32ReadOpsComplete) {
180                         return -1;
181                 }
182
183                 psSyncData->ui32WriteOpsPending++;
184         }
185
186         switch (psBufferInfo->pixelformat) {
187         case PVRSRV_PIXEL_FORMAT_RGB565:
188         default:
189                 {
190                         FillRGB565Image(psBuffer->sCPUVAddr, BC_EXAMPLE_WIDTH,
191                                         BC_EXAMPLE_HEIGHT, BC_EXAMPLE_STRIDE);
192                         break;
193                 }
194         case PVRSRV_PIXEL_FORMAT_FOURCC_ORG_UYVY:
195                 {
196                         FillYUV422Image(psBuffer->sCPUVAddr, BC_EXAMPLE_WIDTH,
197                                         BC_EXAMPLE_HEIGHT, BC_EXAMPLE_STRIDE);
198                         break;
199                 }
200         case PVRSRV_PIXEL_FORMAT_NV12:
201                 {
202                         FillYUV420Image(psBuffer->sCPUVAddr, BC_EXAMPLE_WIDTH,
203                                         BC_EXAMPLE_HEIGHT, BC_EXAMPLE_STRIDE);
204                         break;
205                 }
206         }
207
208         if (psSyncData) {
209                 psSyncData->ui32WriteOpsComplete++;
210         }
211
212         return 0;
213 }
214
215 int GetBufferCount(unsigned int *pui32BufferCount)
216 {
217         BC_EXAMPLE_DEVINFO *psDevInfo = GetAnchorPtr();
218
219         if (psDevInfo == IMG_NULL) {
220                 return -1;
221         }
222
223         *pui32BufferCount = psDevInfo->sBufferInfo.ui32BufferCount;
224
225         return 0;
226 }