Repository navigation
Expand file tree
/
Copy pathFilamentSWTCanvas.java
More file actions
104 lines (87 loc) · 2.96 KB
/
Copy pathFilamentSWTCanvas.java
File metadata and controls
104 lines (87 loc) · 2.96 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
package me.leeho.filament.swt;
import java.awt.Dimension;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import com.google.android.filament.Engine;
import com.google.android.filament.FilamentPanel;
import com.google.android.filament.FilamentTarget;
import com.google.android.filament.Renderer;
import com.google.android.filament.SwapChain;
/**
*
* modify from {@link FilamentPanel}
* @author LeeHo
*
*/
public class FilamentSWTCanvas implements FilamentTarget {
private static final int SURFACE_PADDING = 0;
private Control canvas;
private Dimension mBackendDimension = new Dimension(0, 0);
private NativeSurfaceEx mNativeSurface;
private SwapChain mSwapChain;
private int mWidth;
private int mHeight;
public FilamentSWTCanvas(Control canvas) {
this.canvas = canvas;
}
public Control getCanvas() {
return canvas;
}
/**
* This must be called on Filament thread. Prepare to renders to offscreen
* native window.
*/
public boolean beginFrame(Engine engine, Renderer renderer) {
ensureSurface(engine);
return renderer.beginFrame(mSwapChain, 0L);
}
/**
* This must be called on Filament thread. Pixels are read from the Gl (located
* in VRAM) and copied to the JVM (in RAM). Colors are converted from ABGR to
* RGBA. A BufferedImage is created (copy RAM to RAM of all pixels). Finally the
* image is drawn to screen via Graphics.drawImage (one more trip from RAM to
* VRAM).
*/
public void endFrame(Renderer renderer) {
// By the time readPixel callback is invoked, the dimension of the Panel may
// have changed.
// Capture dimension so they match the current GL framebuffer.
renderer.endFrame();
}
private void createSurfaces(Engine engine, int width, int height) {
mBackendDimension = new Dimension(width, height);
mNativeSurface = new NativeSurfaceEx(canvas.view.id, width, height);
mSwapChain = engine.createSwapChainFromNativeSurface(mNativeSurface, 0L);
}
private void destroySurfaces(Engine engine) {
if (mNativeSurface == null) {
return;
}
// Previous frames using this surface may still be in the pipeline, so we must
// wait for
// them to finish.
engine.flushAndWait();
mNativeSurface.dispose();
engine.destroySwapChain(mSwapChain);
}
private void ensureSurface(Engine engine) {
// Capture the dimension at the time the surface was ensured.
Point size = canvas.getSize();
mWidth = size.x;
mHeight = size.y;
if (mBackendDimension.width < mWidth || mBackendDimension.height < mHeight) {
destroySurfaces(engine);
// A surface needs to be allocated. Allocate something a little bit bigger than
// necessary in order to avoid reallocating too often if the Panel is resized.
int widthToAllocate = mWidth + SURFACE_PADDING;
int heightToAllocate = mHeight + SURFACE_PADDING;
createSurfaces(engine, widthToAllocate, heightToAllocate);
}
}
/**
* This must be called on Filament thread.
*/
public void destroy(Engine engine) {
destroySurfaces(engine);
}
}