1 |
// (c): Tuomas J. Lukka |
// (c): Tuomas J. Lukka |
2 |
|
|
3 |
package gzz.gfx.gl; |
package gzz.gfx.gl; |
4 |
|
import gzz.*; |
5 |
import gzz.mem.*; |
import gzz.mem.*; |
6 |
import gzz.client.*; |
import gzz.client.*; |
7 |
|
|
10 |
*/ |
*/ |
11 |
public abstract class FileSetTextureMemoryConsumer extends TextureMemoryConsumer { |
public abstract class FileSetTextureMemoryConsumer extends TextureMemoryConsumer { |
12 |
|
|
13 |
Runnable r_runTexImage = new Runnable() { |
Runnable r_runGLThread = new Runnable() { |
14 |
public void run() { runTexImage(); } |
public void run() { runGLThread(); } |
15 |
}; |
}; |
16 |
Runnable r_runRequest = new Runnable() { |
Runnable r_runRequest = new Runnable() { |
17 |
public void run() { runRequest(); } |
public void run() { runRequest(); } |
19 |
|
|
20 |
static final int STATE_IDLE = 0, |
static final int STATE_IDLE = 0, |
21 |
STATE_REQUESTED = 1, |
STATE_REQUESTED = 1, |
22 |
STATE_TEXIMAGING = 2; |
STATE_TEXIMAGING = 2, |
23 |
|
STATE_DOWNSAMPLING = 3 |
24 |
|
; |
25 |
|
|
26 |
|
GL.Texture currentTexture; |
27 |
|
int curTexW, curTexH; |
28 |
|
int curW, curH; |
29 |
|
|
30 |
|
int goalTexW, goalTexH; |
31 |
|
|
32 |
|
String filename; |
33 |
|
int fullW, fullH; |
34 |
|
int fullTexW, fullTexH; |
35 |
|
|
36 |
public class File { |
int state; |
37 |
int w, h; |
|
38 |
String name; |
GL.Image img; |
39 |
|
private void clearImg() { |
40 |
|
if(img != null) |
41 |
|
img.deleteObject(); |
42 |
|
img = null; |
43 |
|
} |
44 |
|
private void clearTexture() { |
45 |
|
if(currentTexture != null) |
46 |
|
currentTexture.deleteObject(); |
47 |
|
currentTexture = null; |
48 |
} |
} |
49 |
|
|
50 |
int state; |
synchronized public int setReservation(int bytes, float quality, Obs o) { |
51 |
|
// Only care about bytes. |
52 |
|
int w = fullTexW, h = fullTexH; |
53 |
|
int e = 0; |
54 |
|
int b; |
55 |
|
while(w >= 1) { |
56 |
|
b = getBytes(w, h); |
57 |
|
if(b <= bytes) break; |
58 |
|
} |
59 |
|
if(w == 0) { |
60 |
|
clearImg(); |
61 |
|
clearTexture(); |
62 |
|
return 0; |
63 |
|
} |
64 |
|
if(w <= curTexW) { |
65 |
|
state = STATE_DOWNSAMPLING; |
66 |
|
} else { // Need to load a larger version. |
67 |
|
} |
68 |
|
return b; |
69 |
|
} |
70 |
|
|
71 |
|
synchronized private void runRequest() { |
72 |
|
// The only GL function callable outside GL thread ;) |
73 |
|
// This may take quite some time. |
74 |
|
this.img = GL.createImage(filename); |
75 |
|
|
|
private void runRequest() { |
|
76 |
state = STATE_TEXIMAGING; |
state = STATE_TEXIMAGING; |
77 |
// High priority, we're using a lot of memory |
// High priority, we're using a lot of memory |
78 |
AbstractUpdateManager.doWhenIdle(r_runRequest, -10); |
AbstractUpdateManager.doWhenIdle(r_runGLThread, -10); |
79 |
} |
} |
80 |
|
|
81 |
private void runTexImage() { |
synchronized private void runGLThread() { |
82 |
|
// Now we are in the GL thread. |
83 |
|
// |
84 |
|
// Check that we still actually *want* this image |
85 |
|
if(state == STATE_TEXIMAGING) { |
86 |
|
|
87 |
|
GL.Texture newTexture = GL.createTexture(); |
88 |
|
|
89 |
|
// Make sure the data is freed NOW. |
90 |
|
clearImg(); |
91 |
|
state = STATE_IDLE; |
92 |
|
} else if(state == STATE_DOWNSAMPLING) { |
93 |
|
} else { |
94 |
|
clearImg(); |
95 |
|
return; |
96 |
|
} |
97 |
} |
} |
98 |
|
|
99 |
|
|