aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/modules/engines/psl1ght/rsxutil.c
blob: 7567a8c285d038c8480ada13ce1f999f4614c2b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * This software is distributed under the terms of the MIT License
 */

#include <ppu-lv2.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <sysutil/video.h>
#include <rsx/gcm_sys.h>
#include <rsx/rsx.h>
#include <io/pad.h>
#include <time.h>
#include <cairo/cairo.h>
#include <math.h>

#include "rsxutil.h"

#define GCM_LABEL_INDEX 255

static void
waitRSXIdle(gcmContextData *context);

static int flipped = FALSE;

void
waitFlip()
{
   if (flipped)
     {
        while (gcmGetFlipStatus () != 0)
          usleep (200);  /* Sleep, to not stress the cpu. */
        gcmResetFlipStatus ();
        flipped = FALSE;
     }
}

int
flipBuffer(gcmContextData *context, s32 buffer)
{
   if (gcmSetFlip (context, buffer) == 0)
     {
        rsxFlushBuffer (context);
        // Prevent the RSX from continuing until the flip has finished.
        gcmSetWaitFlip (context);

        flipped = TRUE;
        return TRUE;
     }
   return FALSE;
}

int
makeBuffer(rsxBuffer *buffer, u16 width, u16 height, int id)
{
   int depth = sizeof(u32);
   int pitch = depth * width;
   int size = depth * width * height;

   buffer->ptr = (uint32_t *)rsxMemalign (64, size);

   if (buffer->ptr == NULL)
     goto error;

   if (rsxAddressToOffset (buffer->ptr, &buffer->offset) != 0)
     goto error;

   /* Register the display buffer with the RSX */
   if (gcmSetDisplayBuffer (id, buffer->offset, pitch, width, height) != 0)
     goto error;

   buffer->width = width;
   buffer->height = height;
   buffer->id = id;

   return TRUE;

error:
   if (buffer->ptr != NULL)
     rsxFree (buffer->ptr);

   return FALSE;
}

int
copyBuffer(gcmContextData *context, rsxBuffer *source, rsxBuffer *destination)
{
   rsxSetTransferData(context, GCM_TRANSFER_LOCAL_TO_LOCAL,
                      destination->offset,
                      destination->width * sizeof(u32),
                      source->offset,
                      source->width * sizeof(u32),
                      source->width * sizeof(u32),
                      source->height);
}

int
getResolution(u16 *width, u16 *height)
{
   videoState state;
   videoResolution resolution;

   /* Get the state of the display */
   if (videoGetState (0, 0, &state) == 0 &&
       videoGetResolution (state.displayMode.resolution, &resolution) == 0)
     {
        if (width)
          *width = resolution.width;
        if (height)
          *height = resolution.height;

        return TRUE;
     }
   return FALSE;
}

static u8
getPreferredResolution(u16 width, u16 height)
{
   videoDeviceInfo info;
   videoResolution res;
   int area = width * height;
   int mode_area;
   int min_area_diff = abs (area - (720 * 480));
   int area_diff;
   u8 resolution = VIDEO_RESOLUTION_480;
   int i;

   videoGetDeviceInfo(0, 0, &info);

   for (i = 0; i < info.availableModeCount; i++) {
        videoGetResolution (info.availableModes[i].resolution, &res);
        mode_area = res.width * res.height;
        area_diff = abs (area - mode_area);
        if (area_diff < min_area_diff)
          {
             min_area_diff = area_diff;
             resolution = info.availableModes[i].resolution;
          }
     }

   return resolution;
}

int
setResolution(gcmContextData *context, u16 *width, u16 *height)
{
   videoState state;
   videoConfiguration vconfig;
   videoResolution res;
   u8 resolution;

   resolution = getPreferredResolution (*width, *height);

   /* Get the state of the display */
   if (videoGetState (0, 0, &state) != 0)
     return FALSE;

   /* Make sure display is enabled */
   if (state.state != 0)
     return FALSE;

   if (videoGetResolution (resolution, &res) != 0)
     return FALSE;

   /* Configure the buffer format to xRGB */
   memset (&vconfig, 0, sizeof(videoConfiguration));
   vconfig.resolution = resolution;
   vconfig.format = VIDEO_BUFFER_FORMAT_XRGB;
   vconfig.pitch = res.width * sizeof(u32);
   vconfig.aspect = VIDEO_ASPECT_AUTO;

   flushRSX(context);

   if (videoConfigure (0, &vconfig, NULL, 0) != 0)
     return FALSE;

   *width = res.width;
   *height = res.height;

   return TRUE;
}

gcmContextData *
initScreen(void *host_addr, u32 size)
{
   gcmContextData *context = NULL; /* Context to keep track of the RSX buffer. */
   videoState state;
   videoConfiguration vconfig;
   videoResolution res; /* Screen Resolution */

   /* Initilise Reality, which sets up the command buffer and shared IO memory */
   context = rsxInit (CB_SIZE, size, host_addr);
   if (context == NULL)
     goto error;

   /* Get the state of the display */
   if (videoGetState (0, 0, &state) != 0)
     goto error;

   /* Make sure display is enabled */
   if (state.state != 0)
     goto error;

   /* Get the current resolution */
   if (videoGetResolution (state.displayMode.resolution, &res) != 0)
     goto error;

   /* Configure the buffer format to xRGB */
   memset (&vconfig, 0, sizeof(videoConfiguration));
   vconfig.resolution = state.displayMode.resolution;
   vconfig.format = VIDEO_BUFFER_FORMAT_XRGB;
   vconfig.pitch = res.width * sizeof(u32);
   vconfig.aspect = state.displayMode.aspect;

   flushRSX(context);

   if (videoConfigure (0, &vconfig, NULL, 0) != 0)
     goto error;

   if (videoGetState (0, 0, &state) != 0)
     goto error;

   gcmSetFlipMode (GCM_FLIP_VSYNC); // Wait for VSYNC to flip

   gcmResetFlipStatus();

   return context;

error:
   if (context)
     rsxFinish (context, 0);

   return NULL;
}

void
freeScreen(gcmContextData *context)
{
   rsxFinish (context, 0);
}

static void
waitFinish(gcmContextData *context, u32 sLabelVal)
{
   rsxSetWriteBackendLabel (context, GCM_LABEL_INDEX, sLabelVal);

   rsxFlushBuffer (context);

   while (*(vu32 *)gcmGetLabelAddress (GCM_LABEL_INDEX) != sLabelVal)
     usleep(30);
}

static void
waitRSXIdle(gcmContextData *context)
{
   static u32 sLabelVal = 1;

   rsxSetWriteBackendLabel (context, GCM_LABEL_INDEX, sLabelVal);
   rsxSetWaitLabel (context, GCM_LABEL_INDEX, sLabelVal);

   sLabelVal++;

   waitFinish(context, sLabelVal++);
}

void
flushRSX(gcmContextData *context)
{
   if (flipped)
     waitFlip ();
   waitRSXIdle(context);
}