FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / apps / gui / skin_engine / skin_buffer.c
blobc7e01ba5cd1b3f470f5d5b8cd51ae81672406a87
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: wps_parser.c 19880 2009-01-29 20:49:43Z mcuelenaere $
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 * Copyright (C) 2009 Jonathan Gordon
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "config.h"
27 #include "buffer.h"
28 #include "settings.h"
29 #include "screen_access.h"
30 #include "wps_internals.h"
31 #include "skin_tokens.h"
32 #include "skin_buffer.h"
34 /* skin buffer management.
35 * This module is used to allocate space in a single global skin buffer for
36 * tokens for both/all screens.
38 * This is mostly just copy/paste from firmware/buffer.c
41 * MAIN_ and REMOTE_BUFFER are just for reasonable size calibration,
42 * both screens can use the whole buffer as they need; it's not split
43 * between screens
45 * Buffer can be allocated from either "end" of the global buffer.
46 * items with unknown sizes get allocated from the start (0->) (data)
47 * items with known sizes get allocated from the end (<-buf_size) (tokens)
48 * After loading 2 skins the buffer will look like this:
49 * |tokens skin1|images skin2|---SPACE---|data skin2|data skin1|
50 * Make sure to never start allocating from the beginning before letting us know
51 * how much was used. and RESPECT THE buf_free RETURN VALUES!
56 #define MAIN_BUFFER ((LCD_HEIGHT*LCD_WIDTH*LCD_DEPTH/8) \
57 + (2*LCD_HEIGHT*LCD_WIDTH/8))
59 #if (NB_SCREENS > 1)
60 #define REMOTE_BUFFER ((LCD_REMOTE_HEIGHT*LCD_REMOTE_WIDTH*LCD_REMOTE_DEPTH/8) \
61 + (2*LCD_REMOTE_HEIGHT*LCD_REMOTE_WIDTH/8))
62 #else
63 #define REMOTE_BUFFER 0
64 #endif
67 #define SKIN_BUFFER_SIZE (MAIN_BUFFER + REMOTE_BUFFER) + \
68 (WPS_MAX_TOKENS * sizeof(struct wps_token))
71 static unsigned char buffer[SKIN_BUFFER_SIZE];
72 static unsigned char *buffer_front = NULL; /* start of the free space,
73 increases with allocation*/
74 static unsigned char *buffer_back = NULL; /* end of the free space
75 decreases with allocation */
76 static size_t buf_size = SKIN_BUFFER_SIZE;
78 void skin_buffer_init(void)
80 #if 0 /* this will go in again later probably */
81 if (buffer == NULL)
83 buf_size = SKIN_BUFFER_SIZE;/* global_settings.skin_buf_size */
85 buffer = buffer_alloc(buf_size);
86 buffer_front = buffer;
87 buffer_back = bufer + buf_size;
89 else
90 #endif
92 /* reset the buffer.... */
93 buffer_front = buffer;
94 buffer_back = buffer + buf_size;
98 /* get the number of bytes currently being used */
99 size_t skin_buffer_usage(void)
101 return buf_size - (buffer_back-buffer_front);
104 size_t skin_buffer_freespace(void)
106 return buffer_back-buffer_front;
109 /* Allocate size bytes from the buffer
110 * allocates from the back end (data end)
112 void* skin_buffer_alloc(size_t size)
114 if (skin_buffer_freespace() <= size)
116 return NULL;
118 buffer_back -= size;
119 /* 32-bit aligned */
120 buffer_back = (void *)(((unsigned long)buffer_back) & ~3);
121 return buffer_back;
124 /* Get a pointer to the skin buffer and the count of how much is free
125 * used to do your own buffer management.
126 * Any memory used will be overwritten next time wps_buffer_alloc()
127 * is called unless skin_buffer_increment() is called first
129 * This is from the start of the buffer, it is YOUR responsility to make
130 * sure you dont ever use more then *freespace, and bear in mind this will only
131 * be valid untill skin_buffer_alloc() is next called...
132 * so call skin_buffer_increment() and skin_buffer_freespace() regularly
134 void* skin_buffer_grab(size_t *freespace)
136 *freespace = buf_size - skin_buffer_usage();
137 return buffer_front;
140 /* Use after skin_buffer_grab() to specify how much buffer was used */
141 void skin_buffer_increment(size_t used, bool align)
143 buffer_front += used;
144 if (align)
146 /* 32-bit aligned */
147 buffer_front = (void *)(((unsigned long)buffer_front + 3) & ~3);
151 /* free previously skin_buffer_increment()'ed space. This just moves the pointer
152 * back 'used' bytes so make sure you actually want to do this */
153 void skin_buffer_free_from_front(size_t used)
155 buffer_front -= used;
156 /* 32-bit aligned */
157 buffer_front = (void *)(((unsigned long)buffer_front + 3) & ~3);