Merge branch 'development' into master_joker
[openttd-joker.git] / src / blitter / 32bpp_optimized.cpp
blobec05a723289d683be5d1fa18a4c85777459825b2
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file 32bpp_optimized.cpp Implementation of the optimized 32 bpp blitter. */
12 #include "../stdafx.h"
13 #include "../zoom_func.h"
14 #include "../settings_type.h"
15 #include "32bpp_optimized.hpp"
17 #include "../safeguards.h"
19 /** Instantiation of the optimized 32bpp blitter factory. */
20 static FBlitter_32bppOptimized iFBlitter_32bppOptimized;
22 /**
23 * Draws a sprite to a (screen) buffer. It is templated to allow faster operation.
25 * @tparam mode blitter mode
26 * @param bp further blitting parameters
27 * @param zoom zoom level at which we are drawing
29 template <BlitterMode mode>
30 inline void Blitter_32bppOptimized::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
32 const SpriteData *src = (const SpriteData *)bp->sprite;
34 /* src_px : each line begins with uint32 n = 'number of bytes in this line',
35 * then n times is the Colour struct for this line */
36 const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]);
37 /* src_n : each line begins with uint32 n = 'number of bytes in this line',
38 * then interleaved stream of 'm' and 'n' channels. 'm' is remap,
39 * 'n' is number of bytes with the same alpha channel class */
40 const uint16 *src_n = (const uint16 *)(src->data + src->offset[zoom][1]);
42 /* skip upper lines in src_px and src_n */
43 for (uint i = bp->skip_top; i != 0; i--) {
44 src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
45 src_n = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
48 /* skip lines in dst */
49 Colour *dst = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
51 /* store so we don't have to access it via bp everytime (compiler assumes pointer aliasing) */
52 const byte *remap = bp->remap;
54 for (int y = 0; y < bp->height; y++) {
55 /* next dst line begins here */
56 Colour *dst_ln = dst + bp->pitch;
58 /* next src line begins here */
59 const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
60 src_px++;
62 /* next src_n line begins here */
63 const uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
64 src_n += 2;
66 /* we will end this line when we reach this point */
67 Colour *dst_end = dst + bp->skip_left;
69 /* number of pixels with the same aplha channel class */
70 uint n;
72 while (dst < dst_end) {
73 n = *src_n++;
75 if (src_px->a == 0) {
76 dst += n;
77 src_px ++;
78 src_n++;
79 } else {
80 if (dst + n > dst_end) {
81 uint d = dst_end - dst;
82 src_px += d;
83 src_n += d;
85 dst = dst_end - bp->skip_left;
86 dst_end = dst + bp->width;
88 n = min<uint>(n - d, (uint)bp->width);
89 goto draw;
91 dst += n;
92 src_px += n;
93 src_n += n;
97 dst -= bp->skip_left;
98 dst_end -= bp->skip_left;
100 dst_end += bp->width;
102 while (dst < dst_end) {
103 n = min<uint>(*src_n++, (uint)(dst_end - dst));
105 if (src_px->a == 0) {
106 dst += n;
107 src_px++;
108 src_n++;
109 continue;
112 draw:;
114 switch (mode) {
115 case BM_COLOUR_REMAP:
116 if (src_px->a == 255) {
117 do {
118 uint m = *src_n;
119 /* In case the m-channel is zero, do not remap this pixel in any way */
120 if (m == 0) {
121 *dst = src_px->data;
122 } else {
123 uint r = remap[GB(m, 0, 8)];
124 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
126 dst++;
127 src_px++;
128 src_n++;
129 } while (--n != 0);
130 } else {
131 do {
132 uint m = *src_n;
133 if (m == 0) {
134 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
135 } else {
136 uint r = remap[GB(m, 0, 8)];
137 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
139 dst++;
140 src_px++;
141 src_n++;
142 } while (--n != 0);
144 break;
146 case BM_CRASH_REMAP:
147 if (src_px->a == 255) {
148 do {
149 uint m = *src_n;
150 if (m == 0) {
151 uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
152 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
153 } else {
154 uint r = remap[GB(m, 0, 8)];
155 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
157 dst++;
158 src_px++;
159 src_n++;
160 } while (--n != 0);
161 } else {
162 do {
163 uint m = *src_n;
164 if (m == 0) {
165 if (src_px->a != 0) {
166 uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
167 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
169 } else {
170 uint r = remap[GB(m, 0, 8)];
171 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
173 dst++;
174 src_px++;
175 src_n++;
176 } while (--n != 0);
178 break;
180 case BM_BLACK_REMAP:
181 do {
182 *dst = Colour(0, 0, 0);
183 dst++;
184 src_px++;
185 src_n++;
186 } while (--n != 0);
187 break;
189 case BM_TRANSPARENT:
190 /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
191 * This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
192 * we produce a result the newgrf maker didn't expect ;) */
194 /* Make the current colour a bit more black, so it looks like this image is transparent */
195 src_n += n;
196 if (src_px->a == 255) {
197 src_px += n;
198 do {
199 *dst = MakeTransparent(*dst, 3, 4);
200 dst++;
201 } while (--n != 0);
202 } else {
203 do {
204 *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
205 dst++;
206 src_px++;
207 } while (--n != 0);
209 break;
211 default:
212 if (src_px->a == 255) {
213 /* faster than memcpy(), n is usually low */
214 src_n += n;
215 do {
216 *dst = src_px->data;
217 dst++;
218 src_px++;
219 } while (--n != 0);
220 } else {
221 src_n += n;
222 do {
223 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
224 dst++;
225 src_px++;
226 } while (--n != 0);
228 break;
232 dst = dst_ln;
233 src_px = src_px_ln;
234 src_n = src_n_ln;
239 * Draws a sprite to a (screen) buffer. Calls adequate templated function.
241 * @param bp further blitting parameters
242 * @param mode blitter mode
243 * @param zoom zoom level at which we are drawing
245 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
247 switch (mode) {
248 default: NOT_REACHED();
249 case BM_NORMAL: Draw<BM_NORMAL> (bp, zoom); return;
250 case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
251 case BM_TRANSPARENT: Draw<BM_TRANSPARENT> (bp, zoom); return;
252 case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP> (bp, zoom); return;
253 case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP> (bp, zoom); return;
257 Sprite *Blitter_32bppOptimized::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
259 /* streams of pixels (a, r, g, b channels)
261 * stored in separated stream so data are always aligned on 4B boundary */
262 Colour *dst_px_orig[ZOOM_LVL_COUNT];
264 /* interleaved stream of 'm' channel and 'n' channel
265 * 'n' is number of following pixels with the same alpha channel class
266 * there are 3 classes: 0, 255, others
268 * it has to be stored in one stream so fewer registers are used -
269 * x86 has problems with register allocation even with this solution */
270 uint16 *dst_n_orig[ZOOM_LVL_COUNT];
272 /* lengths of streams */
273 uint32 lengths[ZOOM_LVL_COUNT][2];
275 ZoomLevel zoom_min;
276 ZoomLevel zoom_max;
278 if (sprite->type == ST_FONT) {
279 zoom_min = ZOOM_LVL_NORMAL;
280 zoom_max = ZOOM_LVL_NORMAL;
281 } else {
282 zoom_min = _settings_client.gui.zoom_min;
283 zoom_max = (ZoomLevel) min(_settings_client.gui.zoom_max, ZOOM_LVL_DRAW_SPR);
284 if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_DRAW_SPR;
287 BlitterSpriteFlags flags = SF_NO_REMAP | SF_NO_ANIM;
289 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
290 const SpriteLoader::Sprite *src_orig = &sprite[z];
292 uint size = src_orig->height * src_orig->width;
294 dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
295 dst_n_orig[z] = CallocT<uint16>(size * 2 + src_orig->height * 4 * 2);
297 uint32 *dst_px_ln = (uint32 *)dst_px_orig[z];
298 uint32 *dst_n_ln = (uint32 *)dst_n_orig[z];
300 const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
302 for (uint y = src_orig->height; y > 0; y--) {
303 Colour *dst_px = (Colour *)(dst_px_ln + 1);
304 uint16 *dst_n = (uint16 *)(dst_n_ln + 1);
306 uint16 *dst_len = dst_n++;
308 uint last = 3;
309 int len = 0;
311 for (uint x = src_orig->width; x > 0; x--) {
312 uint8 a = src->a;
313 uint t = a > 0 && a < 255 ? 1 : a;
315 if (last != t || len == 65535) {
316 if (last != 3) {
317 *dst_len = len;
318 dst_len = dst_n++;
320 len = 0;
323 last = t;
324 len++;
326 if (a != 0) {
327 dst_px->a = a;
328 if (a != 0 && a != 255) flags |= SF_TRANSLUCENT;
329 *dst_n = src->m;
330 if (src->m != 0) {
331 flags &= ~SF_NO_REMAP;
332 if (src->m >= PALETTE_ANIM_START) flags &= ~SF_NO_ANIM;
334 /* Get brightest value */
335 uint8 rgb_max = max(src->r, max(src->g, src->b));
337 /* Black pixel (8bpp or old 32bpp image), so use default value */
338 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
339 *dst_n |= rgb_max << 8;
341 /* Pre-convert the mapping channel to a RGB value */
342 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max);
343 dst_px->r = colour.r;
344 dst_px->g = colour.g;
345 dst_px->b = colour.b;
346 } else {
347 dst_px->r = src->r;
348 dst_px->g = src->g;
349 dst_px->b = src->b;
351 dst_px++;
352 dst_n++;
353 } else if (len == 1) {
354 dst_px++;
355 *dst_n = src->m;
356 dst_n++;
359 src++;
362 if (last != 3) {
363 *dst_len = len;
366 dst_px = (Colour *)AlignPtr(dst_px, 4);
367 dst_n = (uint16 *)AlignPtr(dst_n, 4);
369 *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln;
370 *dst_n_ln = (uint8 *)dst_n - (uint8 *)dst_n_ln;
372 dst_px_ln = (uint32 *)dst_px;
373 dst_n_ln = (uint32 *)dst_n;
376 lengths[z][0] = (byte *)dst_px_ln - (byte *)dst_px_orig[z]; // all are aligned to 4B boundary
377 lengths[z][1] = (byte *)dst_n_ln - (byte *)dst_n_orig[z];
380 uint len = 0; // total length of data
381 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
382 len += lengths[z][0] + lengths[z][1];
385 Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(SpriteData) + len);
387 dest_sprite->height = sprite->height;
388 dest_sprite->width = sprite->width;
389 dest_sprite->x_offs = sprite->x_offs;
390 dest_sprite->y_offs = sprite->y_offs;
392 SpriteData *dst = (SpriteData *)dest_sprite->data;
393 memset(dst, 0, sizeof(*dst));
394 /* Store sprite flags. */
395 dst->flags = flags;
397 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
398 dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
399 dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
401 memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
402 memcpy(dst->data + dst->offset[z][1], dst_n_orig[z], lengths[z][1]);
404 free(dst_px_orig[z]);
405 free(dst_n_orig[z]);
408 return dest_sprite;