-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathGPUCanvasContext.h
More file actions
89 lines (70 loc) · 2.49 KB
/
GPUCanvasContext.h
File metadata and controls
89 lines (70 loc) · 2.49 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
#pragma once
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include "Unions.h"
#include "webgpu/webgpu_cpp.h"
#include "RNFHybridObject.h"
#include "Canvas.h"
#include "GPU.h"
#include "GPUCanvasConfiguration.h"
#include "GPUTexture.h"
#include "SurfaceRegistry.h"
namespace rnwgpu {
namespace m = margelo;
class GPUCanvasContext : public m::HybridObject {
public:
GPUCanvasContext(std::shared_ptr<GPU> gpu, int contextId, int width,
int height)
: HybridObject("GPUCanvasContext"), _gpu(std::move(gpu)) {
_canvas = std::make_shared<Canvas>(nullptr, width, height);
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
_surfaceInfo =
registry.getSurfaceInfoOrCreate(contextId, _gpu->get(), width, height);
}
public:
std::string getBrand() { return _name; }
std::shared_ptr<Canvas> getCanvas() { return _canvas; }
void loadHybridMethods() override {
registerHybridGetter("__brand", &GPUCanvasContext::getBrand, this);
registerHybridGetter("canvas", &GPUCanvasContext::getCanvas, this);
registerHybridMethod("configure", &GPUCanvasContext::configure, this);
registerHybridMethod("unconfigure", &GPUCanvasContext::unconfigure, this);
registerHybridMethod("getCurrentTexture",
&GPUCanvasContext::getCurrentTexture, this);
registerHybridMethod("present", &GPUCanvasContext::present, this);
}
size_t getMemoryPressure() override {
int width = 0;
int height = 0;
if (_surfaceInfo) {
auto size = _surfaceInfo->getSize();
width = size.width;
height = size.height;
}
if (_canvas) {
width = std::max(width, _canvas->getWidth());
height = std::max(height, _canvas->getHeight());
}
if (width <= 0 || height <= 0) {
return 4 * 1024 * 1024; // default to 4MB when size is unknown
}
constexpr size_t kBytesPerPixel = 4; // RGBA8 fallback
constexpr size_t kFloor = 4 * 1024 * 1024;
size_t estimated = static_cast<size_t>(width) *
static_cast<size_t>(height) * kBytesPerPixel;
return std::max(estimated, kFloor);
}
// TODO: is this ok?
inline const wgpu::Surface get() { return nullptr; }
void configure(std::shared_ptr<GPUCanvasConfiguration> configuration);
void unconfigure();
std::shared_ptr<GPUTexture> getCurrentTexture();
void present();
private:
std::shared_ptr<Canvas> _canvas;
std::shared_ptr<SurfaceInfo> _surfaceInfo;
std::shared_ptr<GPU> _gpu;
};
} // namespace rnwgpu