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/>.
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
;
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
);
60 /* next src_n line begins here */
61 const uint16
*src_n_ln
= (const uint16
*)((const byte
*)src_n
+ *(const uint32
*)src_n
);
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 */
70 while (dst
< dst_end
) {
78 if (dst
+ n
> dst_end
) {
79 uint d
= dst_end
- dst
;
83 dst
= dst_end
- bp
->skip_left
;
84 dst_end
= dst
+ bp
->width
;
86 n
= std::min(n
- d
, (uint
)bp
->width
);
96 dst_end
-= bp
->skip_left
;
100 while (dst
< dst_end
) {
101 n
= std::min
<uint
>(*src_n
++, dst_end
- dst
);
103 if (src_px
->a
== 0) {
113 case BM_COLOUR_REMAP
:
114 if (src_px
->a
== 255) {
117 /* In case the m-channel is zero, do not remap this pixel in any way */
121 uint r
= remap
[GB(m
, 0, 8)];
122 if (r
!= 0) *dst
= this->AdjustBrightness(this->LookupColourInPalette(r
), GB(m
, 8, 8));
132 *dst
= ComposeColourRGBANoCheck(src_px
->r
, src_px
->g
, src_px
->b
, src_px
->a
, *dst
);
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
);
145 if (src_px
->a
== 255) {
149 uint8 g
= MakeDark(src_px
->r
, src_px
->g
, src_px
->b
);
150 *dst
= ComposeColourRGBA(g
, g
, g
, src_px
->a
, *dst
);
152 uint r
= remap
[GB(m
, 0, 8)];
153 if (r
!= 0) *dst
= this->AdjustBrightness(this->LookupColourInPalette(r
), GB(m
, 8, 8));
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
);
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
);
180 *dst
= Colour(0, 0, 0);
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 */
194 if (src_px
->a
== 255) {
197 *dst
= MakeTransparent(*dst
, 3, 4);
202 *dst
= MakeTransparent(*dst
, (256 * 4 - src_px
->a
), 256 * 4);
210 if (src_px
->a
== 255) {
211 /* faster than memcpy(), n is usually low */
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
;
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
);
230 *dst
= ComposeColourRGBANoCheck(src_px
->r
, src_px
->g
, src_px
->b
, src_px
->a
, *dst
);
247 template <bool Tpal_to_rgb
>
248 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams
*bp
, BlitterMode mode
, ZoomLevel zoom
)
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];
296 if (sprite
->type
== ST_FONT
) {
297 zoom_min
= ZOOM_LVL_NORMAL
;
298 zoom_max
= ZOOM_LVL_NORMAL
;
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
++;
327 for (uint x
= src_orig
->width
; x
> 0; x
--) {
329 uint t
= a
> 0 && a
< 255 ? 1 : a
;
331 if (last
!= t
|| len
== 65535) {
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;
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
;
371 } else if (len
== 1) {
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
]);
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
);