Repository navigation
Expand file tree
/
Copy pathgraphics.cpp
More file actions
465 lines (434 loc) · 19.7 KB
/
Copy pathgraphics.cpp
File metadata and controls
465 lines (434 loc) · 19.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "graphics.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
namespace {
uint32_t encode(Color color) {
return 0x80000000u |
(static_cast<uint32_t>(color.r) << 16) |
(static_cast<uint32_t>(color.g) << 8) |
color.b;
}
bool insideRounded(int px, int py, int width, int height, int radius) {
if (radius <= 0) return true;
const int cx = px < radius ? radius :
(px >= width - radius ? width - radius - 1 : px);
const int cy = py < radius ? radius :
(py >= height - radius ? height - radius - 1 : py);
const int dx = px - cx;
const int dy = py - cy;
return dx * dx + dy * dy <= radius * radius;
}
constexpr int kFontAtlasSize = 1024;
uint8_t fontAtlas[kFontAtlasSize * kFontAtlasSize] = {};
stbtt_bakedchar fontCharacters[96] = {};
bool fontAttempted = false;
bool fontReady = false;
bool initializeFont() {
if (fontAttempted) return fontReady;
fontAttempted = true;
FILE* file = fopen("/app0/assets/Gontserrat-Regular.ttf", "rb");
if (!file) return false;
fseek(file, 0, SEEK_END);
const long size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size <= 0 || size > 2 * 1024 * 1024) {
fclose(file);
return false;
}
std::vector<uint8_t> data(static_cast<size_t>(size));
const bool read = fread(data.data(), 1, data.size(), file) == data.size();
fclose(file);
if (!read) return false;
fontReady = stbtt_BakeFontBitmap(data.data(), 0, 48.0f, fontAtlas,
kFontAtlasSize, kFontAtlasSize, 32, 96, fontCharacters) > 0;
return fontReady;
}
} // namespace
Scene2D::Scene2D(int width, int height, int pixelDepth)
: width_(width),
height_(height),
depth_(pixelDepth),
frameBufferSize_(width * height * pixelDepth) {}
Scene2D::~Scene2D() = default;
bool Scene2D::Init(size_t memorySize, int frameBufferCount) {
video_ = sceVideoOutOpen(ORBIS_VIDEO_USER_MAIN, ORBIS_VIDEO_OUT_BUS_MAIN, 0, 0);
if (video_ < 0 || !initializeFlipQueue() ||
!allocateVideoMemory(memorySize, 0x200000) ||
!allocateFrameBuffers(frameBufferCount)) {
return false;
}
sceVideoOutSetFlipRate(video_, 0);
return true;
}
bool Scene2D::initializeFlipQueue() {
if (sceKernelCreateEqueue(&flipQueue_, "stremio flip queue") < 0) {
return false;
}
return sceVideoOutAddFlipEvent(flipQueue_, video_, 0) >= 0;
}
bool Scene2D::allocateVideoMemory(size_t size, int alignment) {
directMemorySize_ = (size + alignment - 1) / alignment * alignment;
if (sceKernelAllocateDirectMemory(
0, sceKernelGetDirectMemorySize(), directMemorySize_, alignment,
3, &directMemoryOffset_) < 0) {
return false;
}
if (sceKernelMapDirectMemory(
&videoMemory_, directMemorySize_, 0x33, 0,
directMemoryOffset_, alignment) < 0) {
sceKernelReleaseDirectMemory(directMemoryOffset_, directMemorySize_);
return false;
}
videoMemoryCursor_ = reinterpret_cast<uintptr_t>(videoMemory_);
return true;
}
char* Scene2D::allocateDisplayMemory(size_t size) {
char* result = reinterpret_cast<char*>(videoMemoryCursor_);
videoMemoryCursor_ += size;
return result;
}
bool Scene2D::allocateFrameBuffers(int count) {
frameBufferCount_ = count;
frameBuffers_ = new char*[count];
for (int index = 0; index < count; ++index) {
frameBuffers_[index] = allocateDisplayMemory(frameBufferSize_);
}
sceVideoOutSetBufferAttribute(
&attribute_, 0x80000000, 1, 0, width_, height_, width_);
return sceVideoOutRegisterBuffers(
video_, 0, reinterpret_cast<void**>(frameBuffers_),
count, &attribute_) == 0;
}
void Scene2D::SetActiveFrameBuffer(int index) {
activeFrameBuffer_ = index;
}
void Scene2D::SubmitFlip(int frameId) {
sceVideoOutSubmitFlip(
video_, activeFrameBuffer_, ORBIS_VIDEO_OUT_FLIP_VSYNC, frameId);
}
void Scene2D::FrameWait(int frameId) {
for (;;) {
OrbisVideoOutFlipStatus status = {};
sceVideoOutGetFlipStatus(video_, &status);
if (status.flipArg == frameId) return;
OrbisKernelEvent event = {};
int count = 0;
if (sceKernelWaitEqueue(flipQueue_, &event, 1, &count, nullptr) != 0) return;
}
}
void Scene2D::FrameBufferSwap() {
activeFrameBuffer_ = (activeFrameBuffer_ + 1) % frameBufferCount_;
}
void Scene2D::FrameBufferFill(Color color) {
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
std::fill_n(buffer, static_cast<size_t>(width_) * height_, encode(color));
}
void Scene2D::DrawPixel(int x, int y, Color color) {
if (x < 0 || y < 0 || x >= width_ || y >= height_) return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
buffer[static_cast<size_t>(y) * width_ + x] = encode(color);
}
void Scene2D::DrawRectangle(int x, int y, int width, int height, Color color) {
const int left = std::max(0, x);
const int top = std::max(0, y);
const int right = std::min(width_, x + width);
const int bottom = std::min(height_, y + height);
if (left >= right || top >= bottom) return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
const uint32_t value = encode(color);
for (int row = top; row < bottom; ++row) {
std::fill(buffer + static_cast<size_t>(row) * width_ + left,
buffer + static_cast<size_t>(row) * width_ + right,
value);
}
}
void Scene2D::DrawVerticalFade(
int x, int y, int width, int height, Color color,
uint8_t topOpacity, uint8_t bottomOpacity) {
if (width <= 0 || height <= 0) return;
const int left = std::max(0, x);
const int right = std::min(width_, x + width);
const int top = std::max(0, y);
const int bottom = std::min(height_, y + height);
if (left >= right || top >= bottom) return;
// Blend against the pixels already on screen. The previous shortcut
// blended against a fixed dark color, which made artwork disappear and
// turned every supposedly translucent surface into an opaque blue slab.
constexpr int kBands = 12;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
for (int band = 0; band < kBands; ++band) {
const int bandTop = y + height * band / kBands;
const int bandBottom = y + height * (band + 1) / kBands;
const uint32_t opacity = topOpacity +
(static_cast<int>(bottomOpacity) - topOpacity) * band /
(kBands - 1);
const uint32_t inverse = 255 - opacity;
uint8_t redTable[256], greenTable[256], blueTable[256];
for (int value = 0; value < 256; ++value) {
redTable[value] = static_cast<uint8_t>(
(color.r * opacity + value * inverse) / 255);
greenTable[value] = static_cast<uint8_t>(
(color.g * opacity + value * inverse) / 255);
blueTable[value] = static_cast<uint8_t>(
(color.b * opacity + value * inverse) / 255);
}
const int clippedTop = std::max(top, bandTop);
const int clippedBottom = std::min(bottom, bandBottom);
for (int row = clippedTop; row < clippedBottom; ++row) {
uint32_t* destination = buffer + static_cast<size_t>(row) * width_;
for (int column = left; column < right; ++column) {
const uint32_t dst = destination[column];
const uint32_t red = redTable[(dst >> 16) & 255];
const uint32_t green = greenTable[(dst >> 8) & 255];
const uint32_t blue = blueTable[dst & 255];
destination[column] = 0x80000000u |
(red << 16) | (green << 8) | blue;
}
}
}
}
void Scene2D::DrawRoundedRectangle(
int x, int y, int width, int height, int radius, Color color) {
const int left = std::max(0, x);
const int top = std::max(0, y);
const int right = std::min(width_, x + width);
const int bottom = std::min(height_, y + height);
if (left >= right || top >= bottom) return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
const uint32_t value = encode(color);
for (int row = top; row < bottom; ++row) {
for (int column = left; column < right; ++column) {
if (insideRounded(column - x, row - y, width, height, radius))
buffer[static_cast<size_t>(row) * width_ + column] = value;
}
}
}
void Scene2D::BlitRgb(
int x, int y, int width, int height, const uint32_t* pixels) {
if (!pixels || width <= 0 || height <= 0) return;
const int firstRow = std::max(0, -y);
const int lastRow = std::min(height, height_ - y);
const int firstColumn = std::max(0, -x);
const int lastColumn = std::min(width, width_ - x);
if (firstRow >= lastRow || firstColumn >= lastColumn) return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
for (int row = firstRow; row < lastRow; ++row) {
uint32_t* destination = buffer + static_cast<size_t>(y + row) * width_ + x;
const uint32_t* source = pixels + static_cast<size_t>(row) * width;
for (int column = firstColumn; column < lastColumn; ++column) {
destination[column] = 0x80000000u | source[column];
}
}
}
void Scene2D::BlitRgbScaled(int x, int y, int width, int height,
const uint32_t* pixels, int sourceWidth, int sourceHeight) {
if (!pixels || width <= 0 || height <= 0 ||
sourceWidth <= 0 || sourceHeight <= 0) return;
const int left = std::max(0, x);
const int top = std::max(0, y);
const int right = std::min(width_, x + width);
const int bottom = std::min(height_, y + height);
if (left >= right || top >= bottom) return;
// Mapping X with a division inside the inner 1080p loop used to execute
// roughly two million integer divisions per blit. Cache the 1-D map;
// the player draws into two buffers, so this is a major frame-time win.
std::vector<int> sourceColumns(static_cast<size_t>(right - left));
for (int destinationX = left; destinationX < right; ++destinationX)
sourceColumns[static_cast<size_t>(destinationX - left)] =
(destinationX - x) * sourceWidth / width;
uint32_t* buffer = reinterpret_cast<uint32_t*>(
frameBuffers_[activeFrameBuffer_]);
for (int destinationY = top; destinationY < bottom; ++destinationY) {
const int sourceY = (destinationY - y) * sourceHeight / height;
uint32_t* destination = buffer +
static_cast<size_t>(destinationY) * width_;
const uint32_t* source = pixels +
static_cast<size_t>(sourceY) * sourceWidth;
for (int destinationX = left; destinationX < right; ++destinationX) {
const int sourceX = sourceColumns[
static_cast<size_t>(destinationX - left)];
destination[destinationX] = 0x80000000u | source[sourceX];
}
}
}
void Scene2D::BlitRgbMasked(
int x, int y, int width, int height, const uint32_t* pixels) {
if (!pixels || width <= 0 || height <= 0) return;
const int firstRow = std::max(0, -y);
const int lastRow = std::min(height, height_ - y);
const int firstColumn = std::max(0, -x);
const int lastColumn = std::min(width, width_ - x);
if (firstRow >= lastRow || firstColumn >= lastColumn) return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
for (int row = firstRow; row < lastRow; ++row) {
uint32_t* destination = buffer + static_cast<size_t>(y + row) * width_ + x;
const uint32_t* source = pixels + static_cast<size_t>(row) * width;
// Rounded poster masks only trim a run at each edge. Find those runs
// once, then copy the solid span without a branch for every pixel.
int first = firstColumn;
while (first < lastColumn && (source[first] & 0xff000000u) != 0) ++first;
int last = lastColumn;
while (last > first && (source[last - 1] & 0xff000000u) != 0) --last;
for (int column = first; column < last; ++column)
destination[column] = 0x80000000u | source[column];
}
}
void Scene2D::BlitRgba(
int x, int y, int width, int height, const uint32_t* pixels) {
if (!pixels || width <= 0 || height <= 0) return;
const int firstRow = std::max(0, -y);
const int lastRow = std::min(height, height_ - y);
const int firstColumn = std::max(0, -x);
const int lastColumn = std::min(width, width_ - x);
if (firstRow >= lastRow || firstColumn >= lastColumn) return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
for (int row = firstRow; row < lastRow; ++row) {
uint32_t* destination = buffer + static_cast<size_t>(y + row) * width_ + x;
const uint32_t* source = pixels + static_cast<size_t>(row) * width;
for (int column = firstColumn; column < lastColumn; ++column) {
const uint32_t src = source[column];
const uint32_t alpha = src >> 24;
if (alpha == 0) continue;
if (alpha == 255) {
destination[column] = 0x80000000u | (src & 0x00ffffffu);
continue;
}
const uint32_t dst = destination[column];
const uint32_t inverse = 255 - alpha;
const uint32_t red = ((((src >> 16) & 255) * alpha) +
(((dst >> 16) & 255) * inverse)) / 255;
const uint32_t green = ((((src >> 8) & 255) * alpha) +
(((dst >> 8) & 255) * inverse)) / 255;
const uint32_t blue = (((src & 255) * alpha) +
((dst & 255) * inverse)) / 255;
destination[column] = 0x80000000u |
(red << 16) | (green << 8) | blue;
}
}
}
void Scene2D::BlitRgbRounded(
int x, int y, int width, int height, int radius,
const uint32_t* pixels, uint8_t opacity) {
if (!pixels || x < 0 || y < 0 || x + width > width_ || y + height > height_)
return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
for (int row = 0; row < height; ++row) {
uint32_t* destination = buffer + static_cast<size_t>(y + row) * width_ + x;
const uint32_t* source = pixels + static_cast<size_t>(row) * width;
for (int column = 0; column < width; ++column) {
if (!insideRounded(column, row, width, height, radius)) continue;
if (opacity == 255) {
destination[column] = 0x80000000u | source[column];
} else {
const uint32_t src = source[column];
const uint32_t dst = destination[column];
const uint32_t inverse = 255 - opacity;
const uint32_t r = (((src >> 16) & 255) * opacity +
((dst >> 16) & 255) * inverse) / 255;
const uint32_t g = (((src >> 8) & 255) * opacity +
((dst >> 8) & 255) * inverse) / 255;
const uint32_t b = ((src & 255) * opacity +
(dst & 255) * inverse) / 255;
destination[column] = 0x80000000u | (r << 16) | (g << 8) | b;
}
}
}
}
void Scene2D::BlitRgbScaledRounded(
int x, int y, int width, int height, int radius,
const uint32_t* pixels, int sourceWidth, int sourceHeight,
uint8_t opacity) {
if (!pixels || width <= 0 || height <= 0 || sourceWidth <= 0 ||
sourceHeight <= 0) return;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
for (int row = 0; row < height; ++row) {
const int py = y + row;
if (py < 0 || py >= height_) continue;
for (int column = 0; column < width; ++column) {
const int px = x + column;
if (px < 0 || px >= width_ ||
!insideRounded(column, row, width, height, radius)) continue;
const uint32_t src = pixels[
static_cast<size_t>(row * sourceHeight / height) * sourceWidth +
column * sourceWidth / width];
uint32_t& dst = buffer[static_cast<size_t>(py) * width_ + px];
const uint32_t inverse = 255 - opacity;
const uint32_t red = (((src >> 16) & 255) * opacity +
((dst >> 16) & 255) * inverse) / 255;
const uint32_t green = (((src >> 8) & 255) * opacity +
((dst >> 8) & 255) * inverse) / 255;
const uint32_t blue = ((src & 255) * opacity +
(dst & 255) * inverse) / 255;
dst = 0x80000000u | (red << 16) | (green << 8) | blue;
}
}
}
void Scene2D::DrawText(
int x, int y, const char* text, Color color, int scale) {
if (!text || scale <= 0) return;
std::string ascii(text);
if (ascii.empty()) return;
for (char& character : ascii) {
const unsigned char value = static_cast<unsigned char>(character);
if (value < 32 || value > 126) character = '?';
}
if (!initializeFont()) return;
const float displayScale = static_cast<float>(scale) / 4.0f;
float cursorX = 0.0f;
float cursorY = 22.0f;
uint32_t* buffer = reinterpret_cast<uint32_t*>(frameBuffers_[activeFrameBuffer_]);
for (unsigned char character : ascii) {
stbtt_aligned_quad quad = {};
stbtt_GetBakedQuad(fontCharacters, kFontAtlasSize, kFontAtlasSize,
character - 32, &cursorX, &cursorY, &quad, 1);
const int glyphWidth = std::max(0, static_cast<int>(quad.x1 - quad.x0));
const int glyphHeight = std::max(0, static_cast<int>(quad.y1 - quad.y0));
const int atlasX = static_cast<int>(quad.s0 * kFontAtlasSize);
const int atlasY = static_cast<int>(quad.t0 * kFontAtlasSize);
for (int gy = 0; gy < glyphHeight; ++gy) {
for (int gx = 0; gx < glyphWidth; ++gx) {
const uint8_t alpha = fontAtlas[
(atlasY + gy) * kFontAtlasSize + atlasX + gx];
if (alpha < 8) continue;
const int baseX = x + static_cast<int>(quad.x0 * displayScale) +
static_cast<int>(gx * displayScale);
const int baseY = y + static_cast<int>(quad.y0 * displayScale) +
static_cast<int>(gy * displayScale);
const int block = std::max(1, static_cast<int>(std::ceil(displayScale)));
for (int sy = 0; sy < block; ++sy) for (int sx = 0; sx < block; ++sx) {
const int px = baseX + sx;
const int py = baseY + sy;
if (px < 0 || py < 0 || px >= width_ || py >= height_) continue;
uint32_t& destination = buffer[static_cast<size_t>(py) * width_ + px];
const uint32_t inverse = 255 - alpha;
const uint32_t r = (color.r * alpha +
((destination >> 16) & 255) * inverse) / 255;
const uint32_t g = (color.g * alpha +
((destination >> 8) & 255) * inverse) / 255;
const uint32_t b = (color.b * alpha +
(destination & 255) * inverse) / 255;
destination = 0x80000000u | (r << 16) | (g << 8) | b;
}
}
}
}
}
int Scene2D::MeasureText(const char* text, int scale) {
if (!text || scale <= 0 || !initializeFont()) return 0;
const float displayScale = static_cast<float>(scale) / 4.0f;
float cursorX = 0.0f;
float cursorY = 22.0f;
for (const unsigned char character : std::string(text)) {
const unsigned char glyph = character >= 32 && character <= 126
? character : '?';
stbtt_aligned_quad quad = {};
stbtt_GetBakedQuad(fontCharacters, kFontAtlasSize, kFontAtlasSize,
glyph - 32, &cursorX, &cursorY, &quad, 1);
}
return static_cast<int>(std::ceil(cursorX * displayScale));
}