1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - dsp -
3   File:     dsp_graphics.h
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-02#$
14   $Rev: 8184 $
15   $Author: hatamoto_minoru $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef TWL_DSP_GRAPHICS_H_
19 #define TWL_DSP_GRAPHICS_H_
20 
21 #include <twl/dsp/common/pipe.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /*---------------------------------------------------------------------------*/
28 /* Constants */
29 typedef DSPWord DSPGraphicsScalingMode;
30 #define DSP_GRAPHICS_SCALING_MODE_N_NEIGHBOR (DSPGraphicsScalingMode)0x0001
31 #define DSP_GRAPHICS_SCALING_MODE_BILINEAR   (DSPGraphicsScalingMode)0x0002
32 #define DSP_GRAPHICS_SCALING_MODE_BICUBIC    (DSPGraphicsScalingMode)0x0003
33 
34 #define DSP_GRAPHICS_SCALING_MODE_NPARTSHRINK (DSPGraphicsScalingMode)0x000A
35 
36 // Registers to use
37 #define DSP_GRAPHICS_COM_REGISTER 0    // Register number for processing content notifications
38 #define DSP_GRAPHICS_REP_REGISTER 1    // Register number for processing result notifications
39 
40 /*---------------------------------------------------------------------------*/
41 /* Structures and enumerated types used between the DSP and ARM processors */
42 
43 // Values exchanged with the semaphore register
44 typedef enum DspState
45 {
46     DSP_STATE_FAIL        = 0x0000,
47     DSP_STATE_SUCCESS     = 0x0001
48 //    DSP_STATE_END_ONEPROC = 0x0002
49 } DSPSTATE;
50 
51 // Values that will be specified by an ARM processor, indicating the process for the DSP to run
52 typedef enum _GraphicsFuncID
53 {
54     DSP_G_FUNCID_SCALING = 0x0001,
55     DSP_G_FUNCID_YUV2RGB,
56 
57     DSP_FUNCID_NUM
58 } DSPGraphicsFuncID;
59 
60 // Data structures to use when converting between YUV and RGB
61 typedef struct _Yuv2RgbParam
62 {
63     u32 size;
64     u32 src;
65     u32 dst;
66 } DSPYuv2RgbParam;
67 
68 // Data structures to use for scaling
69 typedef struct _ScalingParam
70 {
71     u32 src;
72     u32 dst;
73     u16 mode;
74     u16 img_width;
75     u16 img_height;
76     u16 rate_w;
77     u16 rate_h;
78     u16 block_x;
79     u16 block_y;
80     u16 width;
81     u16 height;
82     u16 pad[1];    // This makes the size of _ScalingParam a multiple of 4
83 } DSPScalingParam;
84 
85 typedef void (*DSP_GraphicsCallback)(void);  // User-specified callback function to invoke when processing is complete
86 
87 #ifdef SDK_TWL
88 /*---------------------------------------------------------------------------*
89     Macro Definitions
90 *---------------------------------------------------------------------------*/
91 // Macros that calculate the size to be output with DSP_Scaling* functions
92 #define DSP_CALC_SCALING_SIZE(value, ratio) ((u32)(value * (u32)(ratio * 1000) / 1000))
93 
94 #define DSP_CALC_SCALING_SIZE_FX(value, ratio) ((u32)( value * (u32)(ratio * 1000 / 4096.0f) / 1000))
95 
96 /*---------------------------------------------------------------------------*
97     Internal Variable Definitions
98 *---------------------------------------------------------------------------*/
99 static DSP_GraphicsCallback callBackFunc;   // Callback function to invoke when asynchronous processing is complete
100 
101 static volatile BOOL isBusy;                         // TRUE if some process is running on the DSP
102 static volatile BOOL isAsync;                        // TRUE if some asynchronous process is running on the DSP
103 
104 /*---------------------------------------------------------------------------*
105     Internal core functions placed on LTDMAIN
106 *---------------------------------------------------------------------------*/
107 void DSPi_OpenStaticComponentGraphicsCore(FSFile *file);
108 BOOL DSPi_LoadGraphicsCore(FSFile *file, int slotB, int slotC);
109 void DSPi_UnloadGraphicsCore(void);
110 BOOL DSPi_ConvertYuvToRgbCore(const void* src, void* dst, u32 size, DSP_GraphicsCallback callback, BOOL async);
111 BOOL DSPi_ScalingCore(const void* src, void* dst, u16 img_width, u16 img_height, f32 rw, f32 ry, DSPGraphicsScalingMode mode,
112                       u16 x, u16 y, u16 width, u16 height, DSP_GraphicsCallback callback, BOOL async);
113 BOOL DSPi_ScalingFxCore(const void* src, void* dst, u16 img_width, u16 img_height, fx32 rw, fx32 ry, DSPGraphicsScalingMode mode,
114                       u16 x, u16 y, u16 width, u16 height, DSP_GraphicsCallback callback, BOOL async);
115 
116 
117 /*---------------------------------------------------------------------------*
118   Name:         DSP_OpenStaticComponentGraphics
119 
120   Description:  Opens the memory file for a graphics component.
121                 Although you will no longer have to make file system preparations in advance, this will be linked as static memory and thus increase the program size.
122 
123 
124   Arguments:    file: The FSFile structure to open the memory file.
125 
126   Returns:      None.
127  *---------------------------------------------------------------------------*/
DSP_OpenStaticComponentGraphics(FSFile * file)128 static inline void DSP_OpenStaticComponentGraphics(FSFile *file)
129 {
130     if (OS_IsRunOnTwl())
131     {
132         DSPi_OpenStaticComponentGraphicsCore(file);
133     }
134 }
135 
136 /*---------------------------------------------------------------------------*
137   Name:         DSP_LoadGraphics
138 
139   Description:  Loads a DSP graphics component.
140 
141   Arguments:    file: The graphics component file.
142                 slotB: WRAM-B that was allowed to be used for code memory.
143                 slotC: WRAM-C that was allowed to be used for data memory.
144 
145   Returns:      TRUE if the graphics component is loaded properly in the DSP.
146  *---------------------------------------------------------------------------*/
DSP_LoadGraphics(FSFile * file,int slotB,int slotC)147 static inline BOOL DSP_LoadGraphics(FSFile *file, int slotB, int slotC)
148 {
149     if (OS_IsRunOnTwl() == TRUE)
150     {
151         return DSPi_LoadGraphicsCore(file, slotB, slotC);
152     }
153     return FALSE;
154 }
155 
156 /*---------------------------------------------------------------------------*
157   Name:         DSP_UnloadGraphics
158 
159   Description:  Shuts down the DSP graphics component.
160 
161   Arguments:    None.
162 
163   Returns:      None.
164  *---------------------------------------------------------------------------*/
DSP_UnloadGraphics(void)165 static inline void DSP_UnloadGraphics(void)
166 {
167     if (OS_IsRunOnTwl())
168     {
169         DSPi_UnloadGraphicsCore();
170     }
171 }
172 
173 //--------------------------------------------------------------------------------
174 //    YUV-to-RGB conversion
175 //
176 /*---------------------------------------------------------------------------*
177   Name:         DSPi_YuvToRgbConvert[Async]
178 
179   Description:  Converts from YUV to RGB.
180 
181   Arguments:    src: Address of the data to process
182                 dest: Address to store the processed data
183                 size: The src data size
184      'Async' only [callback: Pointer to the callback function to invoke when conversion has finished]
185 
186   Returns:      TRUE if successful.
187  *---------------------------------------------------------------------------*/
DSP_ConvertYuvToRgb(const void * src,void * dst,u32 size)188 static inline BOOL DSP_ConvertYuvToRgb(const void* src, void* dst, u32 size)
189 {
190     if (OS_IsRunOnTwl() && !isBusy)
191     {
192         return DSPi_ConvertYuvToRgbCore(src, dst, size, NULL, FALSE);
193     }
194     return FALSE;
195 }
196 
DSP_ConvertYuvToRgbAsync(const void * src,void * dst,u32 size,DSP_GraphicsCallback callback)197 static inline BOOL DSP_ConvertYuvToRgbAsync(const void* src, void* dst, u32 size, DSP_GraphicsCallback callback)
198 {
199     if (OS_IsRunOnTwl() && !isBusy)
200     {
201         return DSPi_ConvertYuvToRgbCore(src, dst, size, callback, TRUE);
202     }
203     return FALSE;
204 }
205 
206 //--------------------------------------------------------------------------------
207 //
208 //
209 
210 //--------------------------------------------------------------------------------
211 //    Scaling
212 //
213 
214 /*---------------------------------------------------------------------------*
215   Name:         DSP_Scaling[Fx][Async][Ex]
216 
217   Description:  Enlarges or shrinks image data.
218                 *You can process this asynchronously with the 'Async' version. When it has completed, the registered callback will be invoked.
219                 *You can specify an arbitrary image region with the 'Ex' version.
220 
221   Arguments:    src: Pointer to the image data to process (2-byte aligned)
222                 dst: Pointer to store the processing results (this must be 2-byte aligned with the processed data size allocated)
223                 img_width: Width of src
224                 img_height: Height of src
225                 rx: Horizontal factor (31-0.001, truncated at the fourth decimal place) [This is the same as the 'Fx' version]
226                 ry: Vertical factor (31-0.001, truncated at the fourth decimal place) [This is the same as the 'Fx' version]
227                 mode: Interpolation method (nearest-neighbor, bilinear, or bicubic)
228        'Ex' only [x: x-coordinate at the upper-left of the region to process]
229        'Ex' only [y: y-coordinate at the upper-left of the region to process]
230        'Ex' only [width: Width of the region to process]
231        'Ex' only [height: Height of the region to process]
232     'Async' only [callback: Callback function to invoke when processing is complete]
233 
234   Returns:      Returns TRUE on success.
235  *---------------------------------------------------------------------------*/
DSP_Scaling(const void * src,void * dst,u16 img_width,u16 img_height,f32 rx,f32 ry,DSPGraphicsScalingMode mode)236 static inline BOOL DSP_Scaling(const void* src, void* dst, u16 img_width, u16 img_height, f32 rx, f32 ry, DSPGraphicsScalingMode mode)
237 {
238     if (OS_IsRunOnTwl() && !isBusy)
239     {
240         return DSPi_ScalingCore(src, dst, img_width, img_height, rx, ry, mode, 0, 0, img_width, img_height, NULL, FALSE);
241     }
242     return FALSE;
243 }
244 
DSP_ScalingFx(const void * src,void * dst,u16 img_width,u16 img_height,fx32 rx,fx32 ry,DSPGraphicsScalingMode mode)245 static inline BOOL DSP_ScalingFx(const void* src, void* dst, u16 img_width, u16 img_height, fx32 rx, fx32 ry, DSPGraphicsScalingMode mode)
246 {
247     if (OS_IsRunOnTwl() && !isBusy)
248     {
249         return DSPi_ScalingFxCore(src, dst, img_width, img_height, rx, ry, mode, 0, 0, img_width, img_height, NULL, FALSE);
250     }
251     return FALSE;
252 }
253 
DSP_ScalingAsync(const void * src,void * dst,u16 img_width,u16 img_height,f32 rx,f32 ry,DSPGraphicsScalingMode mode,DSP_GraphicsCallback callback)254 static inline BOOL DSP_ScalingAsync(const void* src, void* dst, u16 img_width, u16 img_height, f32 rx, f32 ry,
255                              DSPGraphicsScalingMode mode, DSP_GraphicsCallback callback)
256 {
257     if (OS_IsRunOnTwl() && !isBusy)
258     {
259         return DSPi_ScalingCore(src, dst, img_width, img_height, rx, ry, mode, 0, 0, img_width, img_height, callback, TRUE);
260     }
261     return FALSE;
262 }
263 
DSP_ScalingFxAsync(const void * src,void * dst,u16 img_width,u16 img_height,fx32 rx,fx32 ry,DSPGraphicsScalingMode mode,DSP_GraphicsCallback callback)264 static inline BOOL DSP_ScalingFxAsync(const void* src, void* dst, u16 img_width, u16 img_height, fx32 rx, fx32 ry,
265                              DSPGraphicsScalingMode mode, DSP_GraphicsCallback callback)
266 {
267     if (OS_IsRunOnTwl() && !isBusy)
268     {
269         return DSPi_ScalingFxCore(src, dst, img_width, img_height, rx, ry, mode, 0, 0, img_width, img_height, callback, TRUE);
270     }
271     return FALSE;
272 }
273 
DSP_ScalingEx(const void * src,void * dst,u16 img_width,u16 img_height,f32 rx,f32 ry,DSPGraphicsScalingMode mode,u16 x,u16 y,u16 width,u16 height)274 static inline BOOL DSP_ScalingEx(const void* src, void* dst, u16 img_width, u16 img_height, f32 rx, f32 ry, DSPGraphicsScalingMode mode,
275                                 u16 x, u16 y, u16 width, u16 height)
276 {
277     if (OS_IsRunOnTwl() && !isBusy)
278     {
279         return DSPi_ScalingCore(src, dst, img_width, img_height, rx, ry, mode, x, y, width, height, NULL, FALSE);
280     }
281     return FALSE;
282 }
283 
DSP_ScalingFxEx(const void * src,void * dst,u16 img_width,u16 img_height,fx32 rx,fx32 ry,DSPGraphicsScalingMode mode,u16 x,u16 y,u16 width,u16 height)284 static inline BOOL DSP_ScalingFxEx(const void* src, void* dst, u16 img_width, u16 img_height, fx32 rx, fx32 ry, DSPGraphicsScalingMode mode,
285                                 u16 x, u16 y, u16 width, u16 height)
286 {
287     if (OS_IsRunOnTwl() && !isBusy)
288     {
289         return DSPi_ScalingFxCore(src, dst, img_width, img_height, rx, ry, mode, x, y, width, height, NULL, FALSE);
290     }
291     return FALSE;
292 }
293 
DSP_ScalingAsyncEx(const void * src,void * dst,u16 img_width,u16 img_height,f32 rx,f32 ry,DSPGraphicsScalingMode mode,u16 x,u16 y,u16 width,u16 height,DSP_GraphicsCallback callback)294 static inline BOOL DSP_ScalingAsyncEx(const void* src, void* dst, u16 img_width, u16 img_height, f32 rx, f32 ry, DSPGraphicsScalingMode mode,
295                                 u16 x, u16 y, u16 width, u16 height, DSP_GraphicsCallback callback)
296 {
297     if (OS_IsRunOnTwl() && !isBusy)
298     {
299         return DSPi_ScalingCore(src, dst, img_width, img_height, rx, ry, mode, x, y, width, height, callback, TRUE);
300     }
301     return FALSE;
302 }
303 
DSP_ScalingFxAsyncEx(const void * src,void * dst,u16 img_width,u16 img_height,fx32 rx,fx32 ry,DSPGraphicsScalingMode mode,u16 x,u16 y,u16 width,u16 height,DSP_GraphicsCallback callback)304 static inline BOOL DSP_ScalingFxAsyncEx(const void* src, void* dst, u16 img_width, u16 img_height, fx32 rx, fx32 ry, DSPGraphicsScalingMode mode,
305                                   u16 x, u16 y, u16 width, u16 height, DSP_GraphicsCallback callback)
306 {
307     if (OS_IsRunOnTwl() && !isBusy)
308     {
309         return DSPi_ScalingFxCore(src, dst, img_width, img_height, rx, ry, mode, x, y, width, height, callback, TRUE);
310     }
311     return FALSE;
312 }
313 
314 /*---------------------------------------------------------------------------*
315   Name:         DSP_CalcScalingFactor[Fx]
316 
317   Description:  This function reverse-calculates the factor to specify to DSP_Scaling from an input and an output value.
318                 This contains error correction for truncated values less than 0.001, but due to factor restrictions it is not guaranteed to perfectly derive factors that can be calculated for any output size.
319 
320 
321   Arguments:    src_size: Input size
322                 dst_size: Output size
323 
324   Returns:      Returns the factor (f32 or fx32) that will result in dst_size.
325  *---------------------------------------------------------------------------*/
DSP_CalcScalingFactorF32(const u16 src_size,const u16 dst_size)326 static inline f32 DSP_CalcScalingFactorF32(const u16 src_size, const u16 dst_size)
327 {
328     // Add 0.0009 to fix errors in the DSP_Scaling arguments, which are restricted to approximately 0.001
329     return (dst_size / (f32)src_size + 0.0009f);
330 }
331 
DSP_CalcScalingFactorFx32(const u16 src_size,const u16 dst_size)332 static inline fx32 DSP_CalcScalingFactorFx32(const u16 src_size, const u16 dst_size)
333 {
334     return FX_F32_TO_FX32(dst_size / (f32)src_size + 0.0009f);
335 }
336 
337 /*===========================================================================*/
338 
339 #endif    // SDK_TWL
340 
341 #ifdef __cplusplus
342 } /* extern "C" */
343 #endif
344 
345 #endif /* TWL_DSP_GRAPHICS_H_ */
346