GRAPHICS: Add basic caching to 9patch colors

This commit is contained in:
Borja Lorente 2016-07-31 18:25:02 +02:00
parent 03f2d9b01e
commit 02a18134c3
2 changed files with 20 additions and 10 deletions

View File

@ -235,6 +235,11 @@ void NinePatchBitmap::blit(Graphics::Surface &target, int dx, int dy, int dw, in
drawRegions(srf, dx, dy, dw, dh); drawRegions(srf, dx, dy, dw, dh);
//TODO: This can be further optimized by keeping the data between draws,
// and using a unique identifier for each palette, so that it only gets
// recalculated when the palette changes.
_cached_colors.clear();
for (uint i = 0; i < srf.w; ++i) { for (uint i = 0; i < srf.w; ++i) {
for (uint j = 0; j < srf.h; ++j) { for (uint j = 0; j < srf.h; ++j) {
uint32 color = *(uint32*)srf.getBasePtr(i, j); uint32 color = *(uint32*)srf.getBasePtr(i, j);
@ -334,18 +339,21 @@ static inline uint32 dist(uint32 a, uint32 b) {
} }
byte NinePatchBitmap::closestGrayscale(uint32 color, byte* palette, byte paletteLength) { byte NinePatchBitmap::closestGrayscale(uint32 color, byte* palette, byte paletteLength) {
byte target = grayscale(color); if (!_cached_colors.contains(color)) {
byte bestNdx = 0; byte target = grayscale(color);
byte bestColor = grayscale(palette[0], palette[1], palette[2]); byte bestNdx = 0;
for (byte i = 1; i < paletteLength; ++i) { byte bestColor = grayscale(palette[0], palette[1], palette[2]);
byte current = grayscale(palette[i * 3], palette[(i * 3) + 1], palette[(i * 3) + 2]); for (byte i = 1; i < paletteLength; ++i) {
if (dist(target, bestColor) >= dist(target, current)) { byte current = grayscale(palette[i * 3], palette[(i * 3) + 1], palette[(i * 3) + 2]);
bestColor = current; if (dist(target, bestColor) >= dist(target, current)) {
bestNdx = i; bestColor = current;
bestNdx = i;
}
} }
_cached_colors[color] = bestNdx;
} }
return bestNdx; return _cached_colors[color];
} }
} // end of namespace Graphics } // end of namespace Graphics

View File

@ -48,6 +48,7 @@
#include "common/array.h" #include "common/array.h"
#include "common/rect.h" #include "common/rect.h"
#include "common/hashmap.h"
namespace Graphics { namespace Graphics {
@ -81,6 +82,7 @@ class NinePatchBitmap {
bool _destroy_bmp; bool _destroy_bmp;
int _width, _height; int _width, _height;
int _cached_dw, _cached_dh; int _cached_dw, _cached_dh;
Common::HashMap<uint32, int> _cached_colors;
public: public:
NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bitmap); NinePatchBitmap(Graphics::TransparentSurface *bmp, bool owns_bitmap);
@ -88,7 +90,7 @@ public:
void blit(Graphics::Surface &target, int dx, int dy, int dw, int dh, byte *palette = NULL, byte numColors = 0); void blit(Graphics::Surface &target, int dx, int dy, int dw, int dh, byte *palette = NULL, byte numColors = 0);
void blitClip(Graphics::Surface &target, Common::Rect clip, int dx, int dy, int dw, int dh); void blitClip(Graphics::Surface &target, Common::Rect clip, int dx, int dy, int dw, int dh);
int getWidth() { return _width; } int getWidth() { return _width; }
int getHeight() { return _height; } int getHeight() { return _height; }
int getMinWidth() { return _h._fix; } int getMinWidth() { return _h._fix; }