fixes for bc_cat
[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 #include "bufferclass_example_private.h"
29
30 #define MIN(a, b) ((a) < (b) ? (a) : (b))
31
32 static void FillYUV420Image(void __iomem *pvDest, int width, int height,
33                             int bytestride)
34 {
35         static int iPhase;
36         int i, j;
37         unsigned char u, v, y;
38         unsigned char *pui8y = (unsigned char __force *)pvDest;
39         unsigned short *pui16uv;
40         unsigned int count = 0;
41
42         for (j = 0; j < height; j++)
43                 for (i = 0; i < width; i++) {
44                         y = (((i + iPhase) >> 6) % (2) == 0) ? 0x7f : 0x00;
45
46                         pui8y[count++] = y;
47                 }
48
49         pui16uv = (unsigned short *)
50             ((unsigned char __force *)pvDest + (width * height));
51         count = 0;
52
53         for (j = 0; j < height; j += 2)
54                 for (i = 0; i < width; i += 2) {
55                         u = (j < (height / 2)) ?
56                                 ((i < (width / 2)) ? 0xFF : 0x33) :
57                                 ((i < (width / 2)) ?  0x33 : 0xAA);
58                         v = (j < (height / 2)) ?
59                                 ((i < (width / 2)) ?  0xAC : 0x0) :
60                                 ((i < (width / 2)) ?  0x03 : 0xEE);
61
62                         pui16uv[count++] = (v << 8) | u;
63
64                 }
65
66         iPhase++;
67 }
68
69 static void FillYUV422Image(void __iomem *pvDest, int width, int height,
70                             int bytestride)
71 {
72         static int iPhase;
73         int x, y;
74         unsigned char u, v, y0, y1;
75         unsigned int *pui32yuv = (unsigned int __force *)pvDest;
76         unsigned int count = 0;
77
78         for (y = 0; y < height; y++)
79                 for (x = 0; x < width; x += 2) {
80                         u = (y < (height / 2)) ?
81                               ((x < (width / 2)) ? 0xFF : 0x33) :
82                               ((x < (width / 2)) ? 0x33 : 0xAA);
83                         v = (y < (height / 2)) ?
84                               ((x < (width / 2)) ? 0xAA : 0x0) :
85                               ((x < (width / 2)) ? 0x03 : 0xEE);
86
87                         y0 = y1 =
88                             (((x + iPhase) >> 6) % (2) == 0) ? 0x7f : 0x00;
89
90                         pui32yuv[count++] =
91                             (y1 << 24) | (v << 16) | (y0 << 8) | u;
92
93                 }
94
95         iPhase++;
96 }
97
98 static void FillRGB565Image(void __iomem *pvDest, int width, int height,
99                             int bytestride)
100 {
101         int i, Count;
102         unsigned long *pui32Addr = (unsigned long __force *)pvDest;
103         unsigned short *pui16Addr = (unsigned short __force *)pvDest;
104         unsigned long Colour32;
105         unsigned short Colour16;
106         static unsigned char Colour8;
107
108         Colour16 = (Colour8 >> 3) | ((Colour8 >> 2) << 5) |
109                    ((Colour8 >> 3) << 11);
110         Colour32 = Colour16 | Colour16 << 16;
111
112         Count = (height * bytestride) >> 2;
113
114         for (i = 0; i < Count; i++)
115                 pui32Addr[i] = Colour32;
116
117         Count = height;
118
119         pui16Addr = (unsigned short *)
120                         ((unsigned char __force *)pvDest + (2 * Colour8));
121
122         for (i = 0; i < Count; i++) {
123                 *pui16Addr = 0xF800;
124
125                 pui16Addr =
126                     (unsigned short *)((unsigned char *)pui16Addr + bytestride);
127         }
128         Count = bytestride >> 2;
129
130         pui32Addr = (unsigned long *)((unsigned char __force *)pvDest +
131                               (bytestride * (MIN(height - 1, 0xFF) - Colour8)));
132
133         for (i = 0; i < Count; i++)
134                 pui32Addr[i] = 0x001F001F;
135
136         Colour8 = (Colour8 + 1) % MIN(height - 1, 0xFF);
137 }
138
139 int FillBuffer(unsigned int ui32BufferIndex)
140 {
141         struct BC_EXAMPLE_DEVINFO *psDevInfo = GetAnchorPtr();
142         struct BC_EXAMPLE_BUFFER *psBuffer;
143         struct BUFFER_INFO *psBufferInfo;
144         struct PVRSRV_SYNC_DATA *psSyncData;
145
146         if (psDevInfo == NULL)
147                 return -1;
148
149         psBuffer = &psDevInfo->psSystemBuffer[ui32BufferIndex];
150         psBufferInfo = &psDevInfo->sBufferInfo;
151
152         psSyncData = psBuffer->psSyncData;
153
154         if (psSyncData) {
155                 if (psSyncData->ui32ReadOpsPending !=
156                     psSyncData->ui32ReadOpsComplete)
157                         return -1;
158
159                 psSyncData->ui32WriteOpsPending++;
160         }
161
162         switch (psBufferInfo->pixelformat) {
163         case PVRSRV_PIXEL_FORMAT_RGB565:
164         default:
165                 FillRGB565Image(psBuffer->sCPUVAddr, BC_EXAMPLE_WIDTH,
166                                 BC_EXAMPLE_HEIGHT, BC_EXAMPLE_STRIDE);
167                 break;
168         case PVRSRV_PIXEL_FORMAT_FOURCC_ORG_UYVY:
169                 FillYUV422Image(psBuffer->sCPUVAddr, BC_EXAMPLE_WIDTH,
170                                 BC_EXAMPLE_HEIGHT, BC_EXAMPLE_STRIDE);
171                 break;
172         case PVRSRV_PIXEL_FORMAT_NV12:
173                 FillYUV420Image(psBuffer->sCPUVAddr, BC_EXAMPLE_WIDTH,
174                                 BC_EXAMPLE_HEIGHT, BC_EXAMPLE_STRIDE);
175                 break;
176         }
177
178         if (psSyncData)
179                 psSyncData->ui32WriteOpsComplete++;
180
181         return 0;
182 }
183
184 int GetBufferCount(unsigned int *pui32BufferCount)
185 {
186         struct BC_EXAMPLE_DEVINFO *psDevInfo = GetAnchorPtr();
187
188         if (psDevInfo == NULL)
189                 return -1;
190
191         *pui32BufferCount = psDevInfo->sBufferInfo.ui32BufferCount;
192
193         return 0;
194 }