forked from eclipse-efx/efxclipse-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainMemoryImage.java
123 lines (99 loc) · 4.1 KB
/
MainMemoryImage.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
package org.eclipse.fx.drift.internal.backend;
import static org.eclipse.fx.drift.internal.GL.GL_BGRA;
import static org.eclipse.fx.drift.internal.GL.GL_PIXEL_PACK_BUFFER;
import static org.eclipse.fx.drift.internal.GL.GL_READ_ONLY;
import static org.eclipse.fx.drift.internal.GL.GL_RGBA;
import static org.eclipse.fx.drift.internal.GL.GL_STATIC_READ;
import static org.eclipse.fx.drift.internal.GL.GL_TEXTURE_2D;
import static org.eclipse.fx.drift.internal.GL.GL_UNSIGNED_BYTE;
import static org.eclipse.fx.drift.internal.GL.GL_UNSIGNED_INT_8_8_8_8_REV;
import static org.eclipse.fx.drift.internal.GL.glBindBuffer;
import static org.eclipse.fx.drift.internal.GL.glBindTexture;
import static org.eclipse.fx.drift.internal.GL.glBufferData;
import static org.eclipse.fx.drift.internal.GL.glDeleteBuffer;
import static org.eclipse.fx.drift.internal.GL.glDeleteTexture;
import static org.eclipse.fx.drift.internal.GL.glGenBuffer;
import static org.eclipse.fx.drift.internal.GL.glGenTexture;
import static org.eclipse.fx.drift.internal.GL.glGetTexImage;
import static org.eclipse.fx.drift.internal.GL.glMapBuffer;
import static org.eclipse.fx.drift.internal.GL.glTexImage2D;
import static org.eclipse.fx.drift.internal.GL.glUnmapBuffer;
import static org.eclipse.fx.drift.internal.SYS.free;
import static org.eclipse.fx.drift.internal.SYS.malloc;
import static org.eclipse.fx.drift.internal.SYS.memcpy;
import org.eclipse.fx.drift.Vec2i;
import org.eclipse.fx.drift.internal.Log;
import org.eclipse.fx.drift.internal.common.ImageData;
import org.eclipse.fx.drift.internal.common.MainMemoryImageData;
import org.eclipse.fx.drift.internal.prism.Prism;
public class MainMemoryImage implements Image {
public static final ImageType TYPE = new ImageType("MainMemory");
private int number;
private Vec2i size;
private MainMemoryImageData data;
int glTexture;
long memPointer;
int memSize;
public MainMemoryImage(int number, Vec2i size) {
this.number = number;
this.size = size;
}
@Override
public ImageData getData() {
return data;
}
@Override
public void allocate() {
glTexture = glGenTexture();
glBindTexture(GL_TEXTURE_2D, glTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
memSize = size.x * size.y * 4;
memPointer = malloc(memSize);
Log.debug("*allocated " + number + " 0x" + Long.toHexString(memPointer) + "("+size.x+"x"+size.y+": " + memSize + "B)");
this.data = new MainMemoryImageData(number, size, memPointer, memSize);
}
@Override
public void release() {
glDeleteTexture(glTexture);
Log.debug("*release " + glTexture + " 0x" + Long.toHexString(memPointer));
free(memPointer);
}
@Override
public void onAcquire() {
}
@Override
public void onPresent() {
synchronized (data) {
downloadToMemorySimple(glTexture, memPointer);
}
}
private void downloadToMemorySimple(int tex, long pPixels) {
// int format = GL_RGBA; // TODO need GL_BGRA on windows
// int format = GL_BGRA;
// we change the colors here for directx upload
int format = Prism.isD3D() ? GL_BGRA : GL_RGBA;
glBindTexture(GL_TEXTURE_2D, glTexture);
glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_INT_8_8_8_8_REV, memPointer);
glBindTexture(GL_TEXTURE_2D, 0);
Log.debug("*downloaded " + tex + " => 0x" + Long.toHexString(memPointer));
}
private void downloadToMemoryBuf(int tex, int size, long pPixels) {
int buf = glGenBuffer();
glBindBuffer(GL_PIXEL_PACK_BUFFER, buf);
glBufferData(GL_PIXEL_PACK_BUFFER, size, 0, GL_STATIC_READ);
int format = GL_RGBA; // TODO need GL_BGRA on windows
glBindTexture(GL_TEXTURE_2D, tex);
glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
glBindTexture(GL_TEXTURE_2D, 0);
long glBuf = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
memcpy(pPixels, glBuf, size);
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glDeleteBuffer(buf);
}
@Override
public int getGLTexture() {
return glTexture;
}
}