1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#include "drape/texture_of_colors.hpp"
#include "base/shared_buffer_manager.hpp"
#include "base/stl_add.hpp"
#include "std/cstring.hpp"
namespace dp
{
namespace
{
int const kResourceSize = 2;
int const kBytesPerPixel = 4;
}
ColorPalette::ColorPalette(m2::PointU const & canvasSize)
: m_textureSize(canvasSize)
, m_cursor(m2::PointU::Zero())
{}
ref_ptr<Texture::ResourceInfo> ColorPalette::ReserveResource(bool predefined, ColorKey const & key, bool & newResource)
{
lock_guard<mutex> lock(m_mappingLock);
TPalette & palette = predefined ? m_predefinedPalette : m_palette;
TPalette::iterator itm = palette.find(key.m_color);
newResource = (itm == palette.end());
if (newResource)
{
PendingColor pendingColor;
pendingColor.m_color = key.m_color;
pendingColor.m_rect = m2::RectU(m_cursor.x, m_cursor.y,
m_cursor.x + kResourceSize, m_cursor.y + kResourceSize);
{
lock_guard<mutex> g(m_lock);
m_pendingNodes.push_back(pendingColor);
}
m_cursor.x += kResourceSize;
if (m_cursor.x >= m_textureSize.x)
{
m_cursor.y += kResourceSize;
m_cursor.x = 0;
ASSERT(m_cursor.y < m_textureSize.y, ());
}
float const sizeX = static_cast<float>(m_textureSize.x);
float const sizeY = static_cast<float>(m_textureSize.y);
m2::PointF const resCenter = m2::RectF(pendingColor.m_rect).Center();
float const x = resCenter.x / sizeX;
float const y = resCenter.y / sizeY;
auto res = palette.emplace(key.m_color, ColorResourceInfo(m2::RectF(x, y, x, y)));
ASSERT(res.second, ());
itm = res.first;
}
return make_ref(&itm->second);
}
ref_ptr<Texture::ResourceInfo> ColorPalette::MapResource(ColorKey const & key, bool & newResource)
{
TPalette::iterator itm = m_predefinedPalette.find(key.m_color);
if (itm != m_predefinedPalette.end())
{
newResource = false;
return make_ref(&itm->second);
}
return ReserveResource(false /* predefined */, key, newResource);
}
void ColorPalette::UploadResources(ref_ptr<Texture> texture)
{
ASSERT(texture->GetFormat() == dp::RGBA8, ());
if (m_pendingNodes.empty())
return;
buffer_vector<PendingColor, 16> pendingNodes;
{
lock_guard<mutex> g(m_lock);
m_pendingNodes.swap(pendingNodes);
}
buffer_vector<size_t, 3> ranges;
ranges.push_back(0);
uint32_t minX = pendingNodes[0].m_rect.minX();
for (size_t i = 0; i < pendingNodes.size(); ++i)
{
m2::RectU const & currentRect = pendingNodes[i].m_rect;
if (minX > currentRect.minX())
{
ranges.push_back(i);
minX = currentRect.minX();
}
}
ranges.push_back(pendingNodes.size());
for (size_t i = 1; i < ranges.size(); ++i)
{
size_t startRange = ranges[i - 1];
size_t endRange = ranges[i];
m2::RectU const & startRect = pendingNodes[startRange].m_rect;
m2::RectU const & endRect = pendingNodes[endRange - 1].m_rect;
m2::RectU uploadRect;
if (startRect.minY() == endRect.minY() &&
startRect.maxY() == endRect.maxY())
{
uploadRect = m2::RectU(startRect.minX(), startRect.minY(), endRect.maxX(), endRect.maxY());
}
else
{
ASSERT(startRect.minX() == 0, ());
uploadRect = m2::RectU(0, startRect.minY(), m_textureSize.x, endRect.maxY());
}
size_t const pixelStride = uploadRect.SizeX();
size_t const byteCount = kBytesPerPixel * uploadRect.SizeX() * uploadRect.SizeY();
size_t const bufferSize = my::NextPowOf2(byteCount);
SharedBufferManager::shared_buffer_ptr_t buffer = SharedBufferManager::instance().reserveSharedBuffer(bufferSize);
uint8_t * pointer = SharedBufferManager::GetRawPointer(buffer);
if (m_isDebug)
memset(pointer, 0, bufferSize);
uint32_t currentY = startRect.minY();
for (size_t j = startRange; j < endRange; ++j)
{
ASSERT(pointer < SharedBufferManager::GetRawPointer(buffer) + byteCount, ());
PendingColor const & c = pendingNodes[j];
if (c.m_rect.minY() > currentY)
{
pointer += kBytesPerPixel * pixelStride;
currentY = c.m_rect.minY();
}
uint32_t const byteStride = pixelStride * kBytesPerPixel;
uint8_t const red = c.m_color.GetRed();
uint8_t const green = c.m_color.GetGreen();
uint8_t const blue = c.m_color.GetBlue();
uint8_t const alpha = c.m_color.GetAlfa();
for (size_t row = 0; row < kResourceSize; row++)
{
for (size_t column = 0; column < kResourceSize; column++)
{
pointer[row * byteStride + column * kBytesPerPixel] = red;
pointer[row * byteStride + column * kBytesPerPixel + 1] = green;
pointer[row * byteStride + column * kBytesPerPixel + 2] = blue;
pointer[row * byteStride + column * kBytesPerPixel + 3] = alpha;
}
}
pointer += kResourceSize * kBytesPerPixel;
ASSERT(pointer <= SharedBufferManager::GetRawPointer(buffer) + byteCount, ());
}
pointer = SharedBufferManager::GetRawPointer(buffer);
texture->UploadData(uploadRect.minX(), uploadRect.minY(),
uploadRect.SizeX(), uploadRect.SizeY(), make_ref(pointer));
}
}
void ColorTexture::ReserveColor(dp::Color const & color)
{
bool newResource = false;
m_indexer->ReserveResource(true /* predefined */, ColorKey(color), newResource);
}
int ColorTexture::GetColorSizeInPixels()
{
return kResourceSize;
}
}
|