libnx
gfx.h
Go to the documentation of this file.
1 /**
2  * @file gfx.h
3  * @brief High-level graphics API.
4  * This API exposes a framebuffer (technically speaking, a windowbuffer) to be used for drawing graphics.
5  * @author yellows8
6  * @copyright libnx Authors
7  */
8 #pragma once
9 #include "../types.h"
10 
11 /// Converts red, green, blue, and alpha components to packed RGBA8.
12 #define RGBA8(r,g,b,a) (((r)&0xff)|(((g)&0xff)<<8)|(((b)&0xff)<<16)|(((a)&0xff)<<24))
13 
14 /// Same as \ref RGBA8 except with alpha=0xff.
15 #define RGBA8_MAXALPHA(r,g,b) RGBA8(r,g,b,0xff)
16 
17 /// GfxMode set by \ref gfxSetMode. The default is GfxMode_LinearDouble. Note that the text-console (see console.h) sets this to GfxMode_TiledDouble.
18 typedef enum
19 {
20  GfxMode_TiledSingle, ///< Single-buffering with raw tiled (block-linear) framebuffer.
21  GfxMode_TiledDouble, ///< Double-buffering with raw tiled (block-linear) framebuffer.
22  GfxMode_LinearDouble ///< Double-buffering with linear framebuffer, which is transferred to the actual framebuffer by \ref gfxFlushBuffers().
23 } GfxMode;
24 
25 /// Framebuffer pixel-format is RGBA8888, there's no known way to change this.
26 
27 /**
28  * @brief Initializes the graphics subsystem.
29  * @warning Do not use \ref viInitialize when using this function.
30  */
31 void gfxInitDefault(void);
32 
33 /**
34  * @brief Uninitializes the graphics subsystem.
35  * @warning Do not use \ref viExit when using this function.
36  */
37 void gfxExit(void);
38 
39 /**
40  * @brief Sets the resolution to be used when initializing the graphics subsystem.
41  * @param[in] width Horizontal resolution, in pixels.
42  * @param[in] height Vertical resolution, in pixels.
43  * @note The default resolution is 720p.
44  * @note This can only be used before calling \ref gfxInitDefault, this will use \ref fatalSimple otherwise. If the input is 0, the default resolution will be used during \ref gfxInitDefault. This sets the maximum resolution for the framebuffer, used during \ref gfxInitDefault. This is also used as the current resolution when crop isn't set. The width/height are reset to the default when \ref gfxExit is used.
45  * @note Normally you should only use this when you need a maximum resolution larger than the default, see above.
46  * @note The width and height are aligned to 4.
47  */
48 void gfxInitResolution(u32 width, u32 height);
49 
50 /// Wrapper for \ref gfxInitResolution with resolution=1080p. Use this if you want to support 1080p or >720p in docked-mode.
51 void gfxInitResolutionDefault(void);
52 
53 /// Configure framebuffer crop, by default crop is all-zero. Use all-zero input to reset to default. \ref gfxExit resets this to the default.
54 /// When the input is invalid this returns without changing the crop data, this includes the input values being larger than the framebuf width/height.
55 /// This will update the display width/height returned by \ref gfxGetFramebuffer, with that width/height being reset to the default when required.
56 /// \ref gfxGetFramebufferDisplayOffset uses absolute x/y, it will not adjust for non-zero crop left/top.
57 /// The new crop config will not take affect with double-buffering disabled. When used during frame-drawing, this should be called before \ref gfxGetFramebuffer.
58 /// The right and bottom params are aligned to 4.
59 void gfxConfigureCrop(s32 left, s32 top, s32 right, s32 bottom);
60 
61 /// Wrapper for \ref gfxConfigureCrop. Use this to set the resolution, within the bounds of the maximum resolution. Use all-zero input to reset to default.
62 void gfxConfigureResolution(s32 width, s32 height);
63 
64 /// If enabled, \ref gfxConfigureResolution will be used with the input resolution for the current OperationMode. Then \ref gfxConfigureResolution will automatically be used with the specified resolution each time OperationMode changes.
65 void gfxConfigureAutoResolution(bool enable, s32 handheld_width, s32 handheld_height, s32 docked_width, s32 docked_height);
66 
67 /// Wrapper for \ref gfxConfigureAutoResolution. handheld_resolution=720p, docked_resolution={all-zero for using current maximum resolution}.
68 void gfxConfigureAutoResolutionDefault(bool enable);
69 
70 /// Waits for vertical sync.
71 void gfxWaitForVsync(void);
72 
73 /// Swaps the framebuffers (for double-buffering).
74 void gfxSwapBuffers(void);
75 
76 /// Get the current framebuffer address, with optional output ptrs for the display framebuffer width/height. The display width/height is adjusted by \ref gfxConfigureCrop and \ref gfxConfigureResolution.
77 u8* gfxGetFramebuffer(u32* width, u32* height);
78 
79 /// Get the framebuffer width/height without crop.
80 void gfxGetFramebufferResolution(u32* width, u32* height);
81 
82 /// Use this to get the actual byte-size of the framebuffer for use with memset/etc.
83 size_t gfxGetFramebufferSize(void);
84 
85 /// Sets the \ref GfxMode.
86 void gfxSetMode(GfxMode mode);
87 
88 /// Controls whether a vertical-flip is done when determining the pixel-offset within the actual framebuffer. By default this is enabled.
89 void gfxSetDrawFlip(bool flip);
90 
91 /// Configures transform. See the NATIVE_WINDOW_TRANSFORM_* enums in buffer_producer.h. The default is NATIVE_WINDOW_TRANSFORM_FLIP_V.
92 void gfxConfigureTransform(u32 transform);
93 
94 /// Flushes the framebuffer in the data cache. When \ref GfxMode is GfxMode_LinearDouble, this also transfers the linear-framebuffer to the actual framebuffer.
95 void gfxFlushBuffers(void);
96 
97 /// Use this to get the pixel-offset in the framebuffer. Returned value is in pixels, not bytes.
98 /// This implements tegra blocklinear, with hard-coded constants etc.
99 /// Do not use this when \ref GfxMode is GfxMode_LinearDouble.
101  u32 tmp_pos;
102 
103  extern size_t g_gfx_framebuf_aligned_width;
104  extern size_t g_gfx_framebuf_display_height;
105  extern bool g_gfx_drawflip;
106 
107  //if (x >= g_gfx_framebuf_width || y >= g_gfx_framebuf_display_height) return (gfxGetFramebufferSize()-4)/4;//Return the last pixel-offset in the buffer, the data located here is not displayed due to alignment. (Disabled for perf)
108 
109  if (g_gfx_drawflip) y = g_gfx_framebuf_display_height-1-y;
110 
111  tmp_pos = ((y & 127) / 16) + (x/16*8) + ((y/16/8)*(g_gfx_framebuf_aligned_width/16*8));
112  tmp_pos *= 16*16 * 4;
113 
114  tmp_pos += ((y%16)/8)*512 + ((x%16)/8)*256 + ((y%8)/2)*64 + ((x%8)/4)*32 + (y%2)*16 + (x%4)*4;//This line is a modified version of code from the Tegra X1 datasheet.
115 
116  return tmp_pos / 4;
117 }
void gfxConfigureAutoResolution(bool enable, s32 handheld_width, s32 handheld_height, s32 docked_width, s32 docked_height)
If enabled, gfxConfigureResolution will be used with the input resolution for the current OperationMo...
void gfxConfigureTransform(u32 transform)
Configures transform. See the NATIVE_WINDOW_TRANSFORM_* enums in buffer_producer.h. The default is NATIVE_WINDOW_TRANSFORM_FLIP_V.
GfxMode
GfxMode set by gfxSetMode. The default is GfxMode_LinearDouble. Note that the text-console (see conso...
Definition: gfx.h:18
size_t gfxGetFramebufferSize(void)
Use this to get the actual byte-size of the framebuffer for use with memset/etc.
void gfxSwapBuffers(void)
Swaps the framebuffers (for double-buffering).
void gfxSetMode(GfxMode mode)
Sets the GfxMode.
uint8_t u8
8-bit unsigned integer.
Definition: types.h:21
void gfxInitDefault(void)
Framebuffer pixel-format is RGBA8888, there&#39;s no known way to change this.
uint32_t u32
32-bit unsigned integer.
Definition: types.h:23
void gfxConfigureAutoResolutionDefault(bool enable)
Wrapper for gfxConfigureAutoResolution. handheld_resolution=720p, docked_resolution={all-zero for usi...
void gfxConfigureResolution(s32 width, s32 height)
Wrapper for gfxConfigureCrop. Use this to set the resolution, within the bounds of the maximum resolu...
void gfxInitResolutionDefault(void)
Wrapper for gfxInitResolution with resolution=1080p. Use this if you want to support 1080p or >720p i...
int32_t s32
32-bit signed integer.
Definition: types.h:29
Single-buffering with raw tiled (block-linear) framebuffer.
Definition: gfx.h:20
static u32 gfxGetFramebufferDisplayOffset(u32 x, u32 y)
Use this to get the pixel-offset in the framebuffer.
Definition: gfx.h:100
void gfxSetDrawFlip(bool flip)
Controls whether a vertical-flip is done when determining the pixel-offset within the actual framebuf...
void gfxExit(void)
Uninitializes the graphics subsystem.
void gfxInitResolution(u32 width, u32 height)
Sets the resolution to be used when initializing the graphics subsystem.
void gfxGetFramebufferResolution(u32 *width, u32 *height)
Get the framebuffer width/height without crop.
void gfxConfigureCrop(s32 left, s32 top, s32 right, s32 bottom)
Configure framebuffer crop, by default crop is all-zero.
Double-buffering with linear framebuffer, which is transferred to the actual framebuffer by gfxFlushB...
Definition: gfx.h:22
Double-buffering with raw tiled (block-linear) framebuffer.
Definition: gfx.h:21
u8 * gfxGetFramebuffer(u32 *width, u32 *height)
Get the current framebuffer address, with optional output ptrs for the display framebuffer width/heig...
void gfxFlushBuffers(void)
Flushes the framebuffer in the data cache. When GfxMode is GfxMode_LinearDouble, this also transfers ...
void gfxWaitForVsync(void)
Waits for vertical sync.