Feature: Ctrl-click to remove fully autoreplaced vehicles from list (#9639)
[openttd-github.git] / src / blitter / 32bpp_optimized.cpp
blobbeafcff37f611ed24af0a359e9ffcf80252cd207
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file 32bpp_optimized.cpp Implementation of the optimized 32 bpp blitter. */
10 #include "../stdafx.h"
11 #include "../zoom_func.h"
12 #include "../settings_type.h"
13 #include "32bpp_optimized.hpp"
15 #include "../safeguards.h"
17 /** Instantiation of the optimized 32bpp blitter factory. */
18 static FBlitter_32bppOptimized iFBlitter_32bppOptimized;
20 /**
21 * Draws a sprite to a (screen) buffer. It is templated to allow faster operation.
23 * @tparam mode blitter mode
24 * @param bp further blitting parameters
25 * @param zoom zoom level at which we are drawing
27 template <BlitterMode mode, bool Tpal_to_rgb>
28 inline void Blitter_32bppOptimized::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
30 const SpriteData *src = (const SpriteData *)bp->sprite;
32 /* src_px : each line begins with uint32 n = 'number of bytes in this line',
33 * then n times is the Colour struct for this line */
34 const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]);
35 /* src_n : each line begins with uint32 n = 'number of bytes in this line',
36 * then interleaved stream of 'm' and 'n' channels. 'm' is remap,
37 * 'n' is number of bytes with the same alpha channel class */
38 const uint16 *src_n = (const uint16 *)(src->data + src->offset[zoom][1]);
40 /* skip upper lines in src_px and src_n */
41 for (uint i = bp->skip_top; i != 0; i--) {
42 src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
43 src_n = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
46 /* skip lines in dst */
47 Colour *dst = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
49 /* store so we don't have to access it via bp every time (compiler assumes pointer aliasing) */
50 const byte *remap = bp->remap;
52 for (int y = 0; y < bp->height; y++) {
53 /* next dst line begins here */
54 Colour *dst_ln = dst + bp->pitch;
56 /* next src line begins here */
57 const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
58 src_px++;
60 /* next src_n line begins here */
61 const uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
62 src_n += 2;
64 /* we will end this line when we reach this point */
65 Colour *dst_end = dst + bp->skip_left;
67 /* number of pixels with the same alpha channel class */
68 uint n;
70 while (dst < dst_end) {
71 n = *src_n++;
73 if (src_px->a == 0) {
74 dst += n;
75 src_px ++;
76 src_n++;
77 } else {
78 if (dst + n > dst_end) {
79 uint d = dst_end - dst;
80 src_px += d;
81 src_n += d;
83 dst = dst_end - bp->skip_left;
84 dst_end = dst + bp->width;
86 n = std::min(n - d, (uint)bp->width);
87 goto draw;
89 dst += n;
90 src_px += n;
91 src_n += n;
95 dst -= bp->skip_left;
96 dst_end -= bp->skip_left;
98 dst_end += bp->width;
100 while (dst < dst_end) {
101 n = std::min<uint>(*src_n++, dst_end - dst);
103 if (src_px->a == 0) {
104 dst += n;
105 src_px++;
106 src_n++;
107 continue;
110 draw:;
112 switch (mode) {
113 case BM_COLOUR_REMAP:
114 if (src_px->a == 255) {
115 do {
116 uint m = *src_n;
117 /* In case the m-channel is zero, do not remap this pixel in any way */
118 if (m == 0) {
119 *dst = src_px->data;
120 } else {
121 uint r = remap[GB(m, 0, 8)];
122 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
124 dst++;
125 src_px++;
126 src_n++;
127 } while (--n != 0);
128 } else {
129 do {
130 uint m = *src_n;
131 if (m == 0) {
132 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
133 } else {
134 uint r = remap[GB(m, 0, 8)];
135 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
137 dst++;
138 src_px++;
139 src_n++;
140 } while (--n != 0);
142 break;
144 case BM_CRASH_REMAP:
145 if (src_px->a == 255) {
146 do {
147 uint m = *src_n;
148 if (m == 0) {
149 uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
150 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
151 } else {
152 uint r = remap[GB(m, 0, 8)];
153 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
155 dst++;
156 src_px++;
157 src_n++;
158 } while (--n != 0);
159 } else {
160 do {
161 uint m = *src_n;
162 if (m == 0) {
163 if (src_px->a != 0) {
164 uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
165 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
167 } else {
168 uint r = remap[GB(m, 0, 8)];
169 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
171 dst++;
172 src_px++;
173 src_n++;
174 } while (--n != 0);
176 break;
178 case BM_BLACK_REMAP:
179 do {
180 *dst = Colour(0, 0, 0);
181 dst++;
182 src_px++;
183 src_n++;
184 } while (--n != 0);
185 break;
187 case BM_TRANSPARENT:
188 /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
189 * This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
190 * we produce a result the newgrf maker didn't expect ;) */
192 /* Make the current colour a bit more black, so it looks like this image is transparent */
193 src_n += n;
194 if (src_px->a == 255) {
195 src_px += n;
196 do {
197 *dst = MakeTransparent(*dst, 3, 4);
198 dst++;
199 } while (--n != 0);
200 } else {
201 do {
202 *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
203 dst++;
204 src_px++;
205 } while (--n != 0);
207 break;
209 default:
210 if (src_px->a == 255) {
211 /* faster than memcpy(), n is usually low */
212 do {
213 if (Tpal_to_rgb && *src_n != 0) {
214 /* Convert the mapping channel to a RGB value */
215 *dst = this->AdjustBrightness(this->LookupColourInPalette(GB(*src_n, 0, 8)), GB(*src_n, 8, 8)).data;
216 } else {
217 *dst = src_px->data;
219 dst++;
220 src_px++;
221 src_n++;
222 } while (--n != 0);
223 } else {
224 do {
225 if (Tpal_to_rgb && *src_n != 0) {
226 /* Convert the mapping channel to a RGB value */
227 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(GB(*src_n, 0, 8)), GB(*src_n, 8, 8));
228 *dst = ComposeColourRGBANoCheck(colour.r, colour.g, colour.b, src_px->a, *dst);
229 } else {
230 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
232 dst++;
233 src_px++;
234 src_n++;
235 } while (--n != 0);
237 break;
241 dst = dst_ln;
242 src_px = src_px_ln;
243 src_n = src_n_ln;
247 template <bool Tpal_to_rgb>
248 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
250 switch (mode) {
251 default: NOT_REACHED();
252 case BM_NORMAL: Draw<BM_NORMAL, Tpal_to_rgb>(bp, zoom); return;
253 case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP, Tpal_to_rgb>(bp, zoom); return;
254 case BM_TRANSPARENT: Draw<BM_TRANSPARENT, Tpal_to_rgb>(bp, zoom); return;
255 case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP, Tpal_to_rgb>(bp, zoom); return;
256 case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP, Tpal_to_rgb>(bp, zoom); return;
260 template void Blitter_32bppOptimized::Draw<true>(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
261 template void Blitter_32bppOptimized::Draw<false>(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
264 * Draws a sprite to a (screen) buffer. Calls adequate templated function.
266 * @param bp further blitting parameters
267 * @param mode blitter mode
268 * @param zoom zoom level at which we are drawing
270 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
272 this->Draw<false>(bp, mode, zoom);
275 template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
277 /* streams of pixels (a, r, g, b channels)
279 * stored in separated stream so data are always aligned on 4B boundary */
280 Colour *dst_px_orig[ZOOM_LVL_COUNT];
282 /* interleaved stream of 'm' channel and 'n' channel
283 * 'n' is number of following pixels with the same alpha channel class
284 * there are 3 classes: 0, 255, others
286 * it has to be stored in one stream so fewer registers are used -
287 * x86 has problems with register allocation even with this solution */
288 uint16 *dst_n_orig[ZOOM_LVL_COUNT];
290 /* lengths of streams */
291 uint32 lengths[ZOOM_LVL_COUNT][2];
293 ZoomLevel zoom_min;
294 ZoomLevel zoom_max;
296 if (sprite->type == ST_FONT) {
297 zoom_min = ZOOM_LVL_NORMAL;
298 zoom_max = ZOOM_LVL_NORMAL;
299 } else {
300 zoom_min = _settings_client.gui.zoom_min;
301 zoom_max = _settings_client.gui.zoom_max;
302 if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
305 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
306 const SpriteLoader::Sprite *src_orig = &sprite[z];
308 uint size = src_orig->height * src_orig->width;
310 dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
311 dst_n_orig[z] = CallocT<uint16>(size * 2 + src_orig->height * 4 * 2);
313 uint32 *dst_px_ln = (uint32 *)dst_px_orig[z];
314 uint32 *dst_n_ln = (uint32 *)dst_n_orig[z];
316 const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
318 for (uint y = src_orig->height; y > 0; y--) {
319 Colour *dst_px = (Colour *)(dst_px_ln + 1);
320 uint16 *dst_n = (uint16 *)(dst_n_ln + 1);
322 uint16 *dst_len = dst_n++;
324 uint last = 3;
325 int len = 0;
327 for (uint x = src_orig->width; x > 0; x--) {
328 uint8 a = src->a;
329 uint t = a > 0 && a < 255 ? 1 : a;
331 if (last != t || len == 65535) {
332 if (last != 3) {
333 *dst_len = len;
334 dst_len = dst_n++;
336 len = 0;
339 last = t;
340 len++;
342 if (a != 0) {
343 dst_px->a = a;
344 *dst_n = src->m;
345 if (src->m != 0) {
346 /* Get brightest value */
347 uint8 rgb_max = std::max({ src->r, src->g, src->b });
349 /* Black pixel (8bpp or old 32bpp image), so use default value */
350 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
351 *dst_n |= rgb_max << 8;
353 if (Tpal_to_rgb) {
354 /* Pre-convert the mapping channel to a RGB value */
355 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max);
356 dst_px->r = colour.r;
357 dst_px->g = colour.g;
358 dst_px->b = colour.b;
359 } else {
360 dst_px->r = src->r;
361 dst_px->g = src->g;
362 dst_px->b = src->b;
364 } else {
365 dst_px->r = src->r;
366 dst_px->g = src->g;
367 dst_px->b = src->b;
369 dst_px++;
370 dst_n++;
371 } else if (len == 1) {
372 dst_px++;
373 *dst_n = src->m;
374 dst_n++;
377 src++;
380 if (last != 3) {
381 *dst_len = len;
384 dst_px = (Colour *)AlignPtr(dst_px, 4);
385 dst_n = (uint16 *)AlignPtr(dst_n, 4);
387 *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln;
388 *dst_n_ln = (uint8 *)dst_n - (uint8 *)dst_n_ln;
390 dst_px_ln = (uint32 *)dst_px;
391 dst_n_ln = (uint32 *)dst_n;
394 lengths[z][0] = (byte *)dst_px_ln - (byte *)dst_px_orig[z]; // all are aligned to 4B boundary
395 lengths[z][1] = (byte *)dst_n_ln - (byte *)dst_n_orig[z];
398 uint len = 0; // total length of data
399 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
400 len += lengths[z][0] + lengths[z][1];
403 Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(SpriteData) + len);
405 dest_sprite->height = sprite->height;
406 dest_sprite->width = sprite->width;
407 dest_sprite->x_offs = sprite->x_offs;
408 dest_sprite->y_offs = sprite->y_offs;
410 SpriteData *dst = (SpriteData *)dest_sprite->data;
411 memset(dst, 0, sizeof(*dst));
413 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
414 dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
415 dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
417 memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
418 memcpy(dst->data + dst->offset[z][1], dst_n_orig[z], lengths[z][1]);
420 free(dst_px_orig[z]);
421 free(dst_n_orig[z]);
424 return dest_sprite;
427 template Sprite *Blitter_32bppOptimized::EncodeInternal<true>(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator);
428 template Sprite *Blitter_32bppOptimized::EncodeInternal<false>(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator);
430 Sprite *Blitter_32bppOptimized::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
432 return this->EncodeInternal<true>(sprite, allocator);