forked from processing/processing4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPGraphicsWebGPU.java
More file actions
134 lines (117 loc) · 3.29 KB
/
PGraphicsWebGPU.java
File metadata and controls
134 lines (117 loc) · 3.29 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
package processing.webgpu;
import processing.core.PGraphics;
import processing.core.PSurface;
public class PGraphicsWebGPU extends PGraphics {
private long surfaceId = 0;
@Override
public PSurface createSurface() {
return surface = new PSurfaceGLFW(this);
}
protected void initWebGPUSurface(long windowHandle, long displayHandle, int width, int height, float scaleFactor) {
surfaceId = PWebGPU.createSurface(windowHandle, displayHandle, width, height, scaleFactor);
if (surfaceId == 0) {
System.err.println("Failed to create WebGPU surface");
}
}
@Override
public void setSize(int w, int h) {
super.setSize(w, h);
if (surfaceId != 0) {
PWebGPU.windowResized(surfaceId, pixelWidth, pixelHeight);
}
}
@Override
public void beginDraw() {
super.beginDraw();
checkSettings();
System.out.println("Beginning draw on surfaceId: " + surfaceId);
PWebGPU.beginDraw(surfaceId);
}
@Override
public void flush() {
super.flush();
PWebGPU.flush(surfaceId);
}
@Override
public void endDraw() {
super.endDraw();
PWebGPU.endDraw(surfaceId);
}
@Override
public void dispose() {
super.dispose();
if (surfaceId != 0) {
PWebGPU.destroySurface(surfaceId);
surfaceId = 0;
}
PWebGPU.exit();
}
@Override
protected void backgroundImpl() {
if (surfaceId == 0) {
return;
}
PWebGPU.backgroundColor(surfaceId, backgroundR, backgroundG, backgroundB, backgroundA);
}
@Override
protected void fillFromCalc() {
super.fillFromCalc();
if (surfaceId == 0) {
return;
}
if (fill) {
PWebGPU.setFill(surfaceId, fillR, fillG, fillB, fillA);
} else {
PWebGPU.noFill(surfaceId);
}
}
@Override
protected void strokeFromCalc() {
super.strokeFromCalc();
if (surfaceId == 0) {
return;
}
if (stroke) {
PWebGPU.setStrokeColor(surfaceId, strokeR, strokeG, strokeB, strokeA);
} else {
PWebGPU.noStroke(surfaceId);
}
}
@Override
public void strokeWeight(float weight) {
super.strokeWeight(weight);
if (surfaceId == 0) {
return;
}
PWebGPU.setStrokeWeight(surfaceId, weight);
}
@Override
public void noFill() {
super.noFill();
if (surfaceId == 0) {
return;
}
PWebGPU.noFill(surfaceId);
}
@Override
public void noStroke() {
super.noStroke();
if (surfaceId == 0) {
return;
}
PWebGPU.noStroke(surfaceId);
}
@Override
protected void rectImpl(float x1, float y1, float x2, float y2) {
rectImpl(x1, y1, x2, y2, 0, 0, 0, 0);
}
@Override
protected void rectImpl(float x1, float y1, float x2, float y2,
float tl, float tr, float br, float bl) {
if (surfaceId == 0) {
return;
}
// rectImpl receives corner coordinates, so let's convert to x,y,w,h
PWebGPU.rect(surfaceId, x1, y1, x2 - x1, y2 - y1, tl, tr, br, bl);
}
}