Repository navigation
Expand file tree
/
Copy pathPackedPixels.cpp
More file actions
159 lines (146 loc) · 3.86 KB
/
Copy pathPackedPixels.cpp
File metadata and controls
159 lines (146 loc) · 3.86 KB
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
#include "PackedPixels.h"
#include <new>
#include <deki/providers/Memory.h>
#include "QuadBlit.h"
namespace DekiRendering
{
PackedPixels::~PackedPixels()
{
for (size_t i = 0; i < m_BlockCount; ++i)
{
Deki::Memory::Free(m_Blocks[i]);
}
if (m_Kept)
{
Deki::Memory::Free(m_Kept);
}
}
const uint8_t* PackedPixels::RunsOf(const Picture& picture)
{
if (!picture.first || picture.width <= 0 || picture.height <= 0 ||
static_cast<size_t>(picture.width) * static_cast<size_t>(picture.height) > kLargestPicture)
{
return nullptr;
}
if (!m_Kept)
{
m_Kept = static_cast<Kept*>(Deki::Memory::Allocate(sizeof(Kept) * kPlaces, Deki::Memory::External));
if (!m_Kept)
{
return nullptr;
}
for (size_t i = 0; i < kPlaces; ++i)
{
new (&m_Kept[i]) Kept();
}
}
Kept* place = PlaceOf(picture);
if (place->runs)
{
return place->runs;
}
if (m_Full || m_KeptCount == kMostPictures)
{
return nullptr;
}
const uint8_t* runs = Add(picture);
if (runs)
{
*place = Kept{ picture, runs };
++m_KeptCount;
}
return runs;
}
PackedPixels::Kept* PackedPixels::PlaceOf(const Picture& picture)
{
// From where the picture's address and revision point, to the first
// place that is its own or free. There is always a free one: the table
// holds more places than pictures.
uint32_t at = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(picture.first)) * 2654435761u;
at ^= picture.revision * 0x9E3779B1u;
at ^= at >> 15;
for (;; ++at)
{
Kept& kept = m_Kept[at % kPlaces];
if (!kept.runs || kept.picture == picture)
{
return &kept;
}
}
}
const uint8_t* PackedPixels::Add(const Picture& picture)
{
// In the block being filled, or in a new one when it has no room left.
if (m_BlockCount > 0)
{
if (const uint8_t* runs = AddToLastBlock(picture))
{
return runs;
}
}
if (!TakeBlock())
{
m_Full = true;
return nullptr;
}
return AddToLastBlock(picture);
}
const uint8_t* PackedPixels::AddToLastBlock(const Picture& picture)
{
const size_t at = (m_UsedInLast + 1) & ~static_cast<size_t>(1); // the offsets are 16-bit
if (at >= kBlockBytes)
{
return nullptr;
}
uint8_t* block = m_Blocks[m_BlockCount - 1];
const size_t bytes = QuadBlit::PackRuns(picture.first, picture.width, picture.height, picture.stride, picture.flipH,
picture.flipV, block + at, kBlockBytes - at);
if (bytes == 0)
{
return nullptr;
}
m_UsedInLast = at + bytes;
return block + at;
}
bool PackedPixels::TakeBlock()
{
if (m_BlockCount == kMostBlocks)
{
return false;
}
// Not the last of the target's external memory: what a game loads next
// needs it more. (0 is a host that does not measure.)
const size_t free = Deki::Memory::GetAvailableExternal();
if (free != 0 && free < kBlockBytes * kBlocksLeftFree)
{
return false;
}
uint8_t* block = static_cast<uint8_t*>(Deki::Memory::Allocate(kBlockBytes, Deki::Memory::External));
if (!block)
{
return false;
}
m_Blocks[m_BlockCount++] = block;
m_UsedInLast = 0;
return true;
}
size_t PackedPixels::Used() const
{
return m_BlockCount == 0 ? 0 : (m_BlockCount - 1) * kBlockBytes + m_UsedInLast;
}
void PackedPixels::Clear()
{
// The first block stays, to fill again; the others go back.
while (m_BlockCount > 1)
{
Deki::Memory::Free(m_Blocks[--m_BlockCount]);
}
m_UsedInLast = 0;
for (size_t i = 0; m_Kept && i < kPlaces; ++i)
{
m_Kept[i] = Kept{};
}
m_KeptCount = 0;
m_Full = false;
}
} // namespace DekiRendering