forked from 117HD/RLHD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAWTContextWrapper.java
More file actions
149 lines (118 loc) · 3.5 KB
/
AWTContextWrapper.java
File metadata and controls
149 lines (118 loc) · 3.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package rs117.hd.opengl.commandbuffer;
import java.awt.Canvas;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.*;
import net.runelite.rlawt.AWTContext;
import org.lwjgl.opengl.*;
import rs117.hd.HdPlugin;
@Slf4j
@Singleton
public final class AWTContextWrapper {
private static final boolean VALIDATE = log.isDebugEnabled();
public enum Owner { CLIENT, RENDER_THREAD, NONE }
private final Object ownershipLock = new Object();
private volatile Owner currentOwner = Owner.CLIENT;
private static final int MAX_OWNERSHIP_LOG = 8;
private final ConcurrentLinkedQueue<String> ownershipLog = new ConcurrentLinkedQueue<>();
private final StringBuilder sb = new StringBuilder();
@Inject
private Client client;
@Getter
private AWTContext context;
@Getter
private Canvas canvas;
public boolean initialize() {
AWTContext.loadNatives();
canvas = client.getCanvas();
synchronized (canvas.getTreeLock()) {
// Delay plugin startup until the client's canvas is valid
if (!canvas.isValid())
return false;
context = new AWTContext(canvas);
context.configurePixelFormat(0, 0, 0);
}
context.createGLContext();
canvas.setIgnoreRepaint(true);
return true;
}
public void shutdown() {
if (context != null)
context.destroy();
context = null;
// Validate the canvas so it becomes valid without having to manually resize the client
if(canvas != null)
canvas.validate();
canvas = null;
}
public int setSwapInterval(int interval) { return context.setSwapInterval(interval); }
public int getBufferMode() { return context.getBufferMode(); }
public int getBackBuffer() { return context.getFramebuffer(false); }
public Owner getOwner() { return currentOwner; }
public void makeCurrent(Owner newOwner) {
if (currentOwner == newOwner)
return;
synchronized (ownershipLock) {
while (currentOwner != Owner.NONE) {
try {
ownershipLock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
try {
context.makeCurrent();
if(newOwner == Owner.RENDER_THREAD && HdPlugin.GL_RENDER_THREAD_CAPS == null){
HdPlugin.GL_RENDER_THREAD_CAPS = GL.createCapabilities();
}
updateOwner(newOwner, "makeCurrent");
} catch (Exception e) {
printOwnershipLog();
log.error("Failed to make OpenGL context current for {}", newOwner, e);
throw new RuntimeException(e);
}
}
}
public void detachCurrent(String reason) {
try {
context.detachCurrent();
updateOwner(Owner.NONE, reason);
} catch (Exception e) {
printOwnershipLog();
log.error("Failed to detach OpenGL context: " + reason, e);
throw new RuntimeException(e);
}
}
private void updateOwner(Owner newOwner, String action) {
if (currentOwner != newOwner && VALIDATE) {
if (ownershipLog.size() >= MAX_OWNERSHIP_LOG)
ownershipLog.poll();
sb.append(currentOwner)
.append(" -> ")
.append(newOwner)
.append(" via ")
.append(action);
ownershipLog.add(sb.toString());
sb.setLength(0);
}
currentOwner = newOwner;
synchronized (ownershipLock) {
ownershipLock.notifyAll();
}
}
private void printOwnershipLog() {
if (!VALIDATE) return;
if (ownershipLog.isEmpty()) {
log.error("No recent ownership transitions recorded.");
} else {
log.error("Recent ownership transitions:");
for (String entry : ownershipLog) {
log.error(" - {}", entry);
}
}
}
}