forked from eclipse-efx/efxclipse-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NGDriftFXSurface.java
338 lines (283 loc) · 11.4 KB
/
NGDriftFXSurface.java
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
/*******************************************************************************
* Copyright (c) 2019 BestSolution.at and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christoph Caks <ccaks@bestsolution.at> - initial API and implementation
*******************************************************************************/
package org.eclipse.fx.drift.impl;
import java.util.Optional;
import org.eclipse.fx.drift.BaseDriftFXSurface;
import org.eclipse.fx.drift.internal.FPSCounter;
import org.eclipse.fx.drift.internal.Log;
import org.eclipse.fx.drift.internal.Placement;
import org.eclipse.fx.drift.internal.SurfaceData;
import org.eclipse.fx.drift.internal.frontend.FrontSwapChain;
import org.eclipse.fx.drift.internal.frontend.FxImage;
import org.eclipse.fx.drift.internal.frontend.SimpleFrontSwapChain;
import com.sun.javafx.geom.transform.BaseTransform;
import com.sun.javafx.sg.prism.NGNode;
import com.sun.prism.Graphics;
import com.sun.prism.Texture;
import com.sun.prism.paint.Color;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;
// Note: this implementation is against internal JavafX API
@SuppressWarnings("restriction")
public class NGDriftFXSurface extends NGNode {
private static final boolean showFPS = Boolean.getBoolean("driftfx.showfps");
private static final boolean profile = Boolean.getBoolean("driftfx.profile");
private SurfaceData surfaceData;
private FrontSwapChain nextSwapChain;
private FrontSwapChain swapChain;
private BaseDriftFXSurface node;
/** image currently in use by javafx renderer - we may not dispose it */
private FxImage<?> curImage;
private FPSCounter fxFpsCounter = new FPSCounter(100);
public void setSwapChain(FrontSwapChain swapChain) {
this.nextSwapChain = swapChain;
}
public NGDriftFXSurface(BaseDriftFXSurface node) {
this.node = node;
Timeline historyTick = new Timeline(new KeyFrame(Duration.millis(100), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
fxFpsCounter.historyTick();
if (swapChain != null) ((SimpleFrontSwapChain)swapChain).fpsCounter.historyTick();
}
}));
historyTick.setCycleCount(Timeline.INDEFINITE);
historyTick.play();
}
public void destroy() {
}
@Override
protected void finalize() throws Throwable {
super.finalize();
destroy();
}
private float center(float dst, float src) {
return (dst - src) / 2f;
}
private float end(float dst, float src) {
return dst - src;
}
static class Pos {
float x;
float width;
float y;
float height;
Pos(float x, float width, float y, float height) {
this.x = x;
this.width = width;
this.y = y;
this.height = height;
}
}
private Pos computeCover(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
float dstRatio = dstWidth / dstHeight;
float srcRatio = srcWidth / srcHeight;
float width, height;
if (dstRatio > srcRatio) {
width = dstWidth;
height = width / srcRatio;
}
else {
height = dstHeight;
width = height * srcRatio;
}
return new Pos(center(dstWidth, width), width, center(dstHeight, height), height);
}
private Pos computeContain(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
float dstRatio = dstWidth / dstHeight;
float srcRatio = srcWidth / srcHeight;
float width, height;
if (srcRatio <= dstRatio) {
height = dstHeight;
width = height * srcRatio;
}
else {
width = dstWidth;
height = width / srcRatio;
}
return new Pos(center(dstWidth, width), width, center(dstHeight, height), height);
}
private Pos computeCenter(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(center(dstWidth, srcWidth), srcWidth, center(dstHeight, srcHeight), srcHeight);
}
private Pos computeTopLeft(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(0, srcWidth, 0, srcHeight);
}
private Pos computeTopCenter(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(center(dstWidth, srcWidth), srcWidth, 0, srcHeight);
}
private Pos computeTopRight(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(end(dstWidth, srcWidth), srcWidth, 0, srcHeight);
}
private Pos computeCenterLeft(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(0, srcWidth, center(dstHeight, srcHeight), srcHeight);
}
private Pos computeCenterRight(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(end (dstWidth, srcWidth), srcWidth, center(dstHeight, srcHeight), srcHeight);
}
private Pos computeBottomLeft(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(0, srcWidth, end(dstHeight, srcHeight), srcHeight);
}
private Pos computeBottomCenter(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(center(dstWidth, srcWidth), srcWidth, end(dstHeight, srcHeight), srcHeight);
}
private Pos computeBottomRight(float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
return new Pos(end(dstWidth, srcWidth), srcWidth, end(dstHeight, srcHeight), srcHeight);
}
private Pos computePlacement(Placement placement, float dstWidth, float dstHeight, float srcWidth, float srcHeight) {
switch (placement) {
case COVER: return computeCover(dstWidth, dstHeight, srcWidth, srcHeight);
case CONTAIN: return computeContain(dstWidth, dstHeight, srcWidth, srcHeight);
case TOP_LEFT: return computeTopLeft(dstWidth, dstHeight, srcWidth, srcHeight);
case TOP_CENTER: return computeTopCenter(dstWidth, dstHeight, srcWidth, srcHeight);
case TOP_RIGHT: return computeTopRight(dstWidth, dstHeight, srcWidth, srcHeight);
case CENTER_LEFT: return computeCenterLeft(dstWidth, dstHeight, srcWidth, srcHeight);
case CENTER_RIGHT: return computeCenterRight(dstWidth, dstHeight, srcWidth, srcHeight);
case BOTTOM_LEFT: return computeBottomLeft(dstWidth, dstHeight, srcWidth, srcHeight);
case BOTTOM_CENTER: return computeBottomCenter(dstWidth, dstHeight, srcWidth, srcHeight);
case BOTTOM_RIGHT: return computeBottomRight(dstWidth, dstHeight, srcWidth, srcHeight);
case CENTER:
default:
return computeCenter(dstWidth, dstHeight, srcWidth, srcHeight);
}
}
private Placement toPlacement(int placement) {
if (placement < 0 || placement >= Placement.values().length) {
return Placement.CENTER;
}
return Placement.values()[placement];
}
private void drawStats(Graphics g) {
DriftDebug.assertQuantumRenderer();
if (swapChain != null) {
g.setPaint(new Color(0,0,0,0.5f));
g.fillRect(155, 0, 150, 85);
String info = "Texture: " + swapChain.getSize().x + "x" + swapChain.getSize().y;
info += "\nTransfer: " + swapChain.getTransferType().id;
NGRenderUtil.writeText(g, -155, 0, 12, info, Color.WHITE, false);
}
NGRenderUtil.drawFPSGraph(g, 0, 0, 150, 40, "JavaFX", fxFpsCounter);
if (swapChain != null) {
FPSCounter c = ((SimpleFrontSwapChain)swapChain).fpsCounter;
NGRenderUtil.drawFPSGraph(g, 0, 45, 150, 40, "Renderer", c);
}
}
private void drawTexture(Graphics g, Texture t) {
float frameContainerWidth = surfaceData.width;
float frameContainerHeight = surfaceData.height;
// TODO
//Placement placement = toPlacement(swapChain.presentationHint);
Placement placement = Placement.CENTER;
float textureRatio = t.getContentWidth() / (float) t.getContentHeight();
float frameRatio = surfaceData.width / surfaceData.height;
Pos framePos = new Pos(0, frameContainerWidth, 0, frameContainerHeight);
if (Math.abs(textureRatio - frameRatio) > 0.001f) {
// aspect ratio is not matching, we need to do compute the position within the frame container
framePos = computeContain(frameContainerWidth, frameContainerHeight, t.getContentWidth(), t.getContentHeight());
}
int frameTextureWidth = t.getContentWidth();
int frameTextureHeight = t.getContentHeight();
float currentContainerWidth = this.surfaceData.width;
float currentContainerHeight = this.surfaceData.height;
Pos pos = computePlacement(placement, currentContainerWidth, currentContainerHeight, framePos.width, framePos.height);
// flip it vertically
g.scale(1, -1);
g.translate(0, -currentContainerHeight);
pos.y = currentContainerHeight - pos.y - pos.height;
g.drawTexture(t, pos.x, pos.y,
pos.x + pos.width, pos.y + pos.height, 0, 0, frameTextureWidth, frameTextureHeight);
}
// @Override
protected void renderContent(Graphics g) {
DriftDebug.assertQuantumRenderer();
fxFpsCounter.tickStart();
if (nextSwapChain != null) {
try {
if (swapChain != null) {
if (curImage != null) {
// System.err.println("Surface -> releasing curImage because swapchain recreation");
swapChain.release(curImage);
curImage = null;
}
swapChain.release();
}
nextSwapChain.allocate(g.getResourceFactory());
swapChain = nextSwapChain;
nextSwapChain = null;
}
catch (Exception e) {
Log.error("ERROR during swapchain recreation");
}
}
if (swapChain != null) {
if (swapChain.isDisposeScheduled()) {
if (curImage != null) {
swapChain.release(curImage);
curImage = null;
}
Optional<FxImage<?>> next = swapChain.getNext();
if (next.isPresent()) {
swapChain.release(next.get());
}
swapChain.release();
swapChain = null;
}
}
BaseTransform saved = g.getTransformNoClone().copy();
if (swapChain != null) {
Optional<FxImage<?>> nextImage = swapChain.getNext();
if (nextImage.isPresent()) {
if (curImage != null) {
// System.err.println("Surface -> releasing curImage");
swapChain.release(curImage);
}
curImage = nextImage.get();
// System.err.println("DriftFX Surface: Showing " + curImage.getData().number + " " + curImage.getTexture().getContentWidth() + " x " + curImage.getTexture().getContentHeight());
curImage.update();
// QuantumRendererHelper.syncExecute(() -> {
// boolean isCtx = GL.isContextCurrent(QuantumRendererHelper.context);
// if (!isCtx) throw new RuntimeException("QuantumRendererHelper has no context!");
// curImage.update();
// });
// GL.glFinish();
// GPUSync sync = QuantumRendererHelper.syncExecuteWithFence(curImage::update);
// WaitSyncResult r = sync.ClientWaitSync(Duration.ZERO);
// System.err.println("=> " + r);
// sync.Delete();
}
if (curImage != null) {
drawTexture(g, curImage.getTexture());
}
}
// restore transform
g.setTransform(saved);
fxFpsCounter.tick();
if (showFPS) {
drawStats(g);
}
}
public void updateSurface(SurfaceData surfaceData) {
Log.debug("[J] NativeSurface updateSurface("+surfaceData+")");
if (isValid(surfaceData)) {
this.surfaceData = surfaceData;
}
}
private boolean isValid(SurfaceData data) {
return true;
}
@Override
protected boolean hasOverlappingContents() {
return false;
}
}