1 | /* |
2 | * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Oracle designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Oracle in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 | * or visit www.oracle.com if you need additional information or have any |
23 | * questions. |
24 | */ |
25 | |
26 | #ifndef HEADLESS |
27 | |
28 | #include <jlong.h> |
29 | |
30 | #include "OGLBufImgOps.h" |
31 | #include "OGLContext.h" |
32 | #include "OGLRenderQueue.h" |
33 | #include "OGLSurfaceData.h" |
34 | #include "GraphicsPrimitiveMgr.h" |
35 | |
36 | /** Evaluates to true if the given bit is set on the local flags variable. */ |
37 | #define IS_SET(flagbit) \ |
38 | (((flags) & (flagbit)) != 0) |
39 | |
40 | /**************************** ConvolveOp support ****************************/ |
41 | |
42 | /** |
43 | * The ConvolveOp shader is fairly straightforward. For each texel in |
44 | * the source texture, the shader samples the MxN texels in the surrounding |
45 | * area, multiplies each by its corresponding kernel value, and then sums |
46 | * them all together to produce a single color result. Finally, the |
47 | * resulting value is multiplied by the current OpenGL color, which contains |
48 | * the extra alpha value. |
49 | * |
50 | * Note that this shader source code includes some "holes" marked by "%s". |
51 | * This allows us to build different shader programs (e.g. one for |
52 | * 3x3, one for 5x5, and so on) simply by filling in these "holes" with |
53 | * a call to sprintf(). See the OGLBufImgOps_CreateConvolveProgram() method |
54 | * for more details. |
55 | * |
56 | * REMIND: Currently this shader (and the supporting code in the |
57 | * EnableConvolveOp() method) only supports 3x3 and 5x5 filters. |
58 | * Early shader-level hardware did not support non-constant sized |
59 | * arrays but modern hardware should support them (although I |
60 | * don't know of any simple way to find out, other than to compile |
61 | * the shader at runtime and see if the drivers complain). |
62 | */ |
63 | static const char *convolveShaderSource = |
64 | // maximum size supported by this shader |
65 | "const int MAX_KERNEL_SIZE = %s;" |
66 | // image to be convolved |
67 | "uniform sampler%s baseImage;" |
68 | // image edge limits: |
69 | // imgEdge.xy = imgMin.xy (anything < will be treated as edge case) |
70 | // imgEdge.zw = imgMax.xy (anything > will be treated as edge case) |
71 | "uniform vec4 imgEdge;" |
72 | // value for each location in the convolution kernel: |
73 | // kernelVals[i].x = offsetX[i] |
74 | // kernelVals[i].y = offsetY[i] |
75 | // kernelVals[i].z = kernel[i] |
76 | "uniform vec3 kernelVals[MAX_KERNEL_SIZE];" |
77 | "" |
78 | "void main(void)" |
79 | "{" |
80 | " int i;" |
81 | " vec4 sum;" |
82 | "" |
83 | " if (any(lessThan(gl_TexCoord[0].st, imgEdge.xy)) ||" |
84 | " any(greaterThan(gl_TexCoord[0].st, imgEdge.zw)))" |
85 | " {" |
86 | // (placeholder for edge condition code) |
87 | " %s" |
88 | " } else {" |
89 | " sum = vec4(0.0);" |
90 | " for (i = 0; i < MAX_KERNEL_SIZE; i++) {" |
91 | " sum +=" |
92 | " kernelVals[i].z *" |
93 | " texture%s(baseImage," |
94 | " gl_TexCoord[0].st + kernelVals[i].xy);" |
95 | " }" |
96 | " }" |
97 | "" |
98 | // modulate with gl_Color in order to apply extra alpha |
99 | " gl_FragColor = sum * gl_Color;" |
100 | "}" ; |
101 | |
102 | /** |
103 | * Flags that can be bitwise-or'ed together to control how the shader |
104 | * source code is generated. |
105 | */ |
106 | #define CONVOLVE_RECT (1 << 0) |
107 | #define CONVOLVE_EDGE_ZERO_FILL (1 << 1) |
108 | #define CONVOLVE_5X5 (1 << 2) |
109 | |
110 | /** |
111 | * The handles to the ConvolveOp fragment program objects. The index to |
112 | * the array should be a bitwise-or'ing of the CONVOLVE_* flags defined |
113 | * above. Note that most applications will likely need to initialize one |
114 | * or two of these elements, so the array is usually sparsely populated. |
115 | */ |
116 | static GLhandleARB convolvePrograms[8]; |
117 | |
118 | /** |
119 | * The maximum kernel size supported by the ConvolveOp shader. |
120 | */ |
121 | #define MAX_KERNEL_SIZE 25 |
122 | |
123 | /** |
124 | * Compiles and links the ConvolveOp shader program. If successful, this |
125 | * function returns a handle to the newly created shader program; otherwise |
126 | * returns 0. |
127 | */ |
128 | static GLhandleARB |
129 | OGLBufImgOps_CreateConvolveProgram(jint flags) |
130 | { |
131 | GLhandleARB convolveProgram; |
132 | GLint loc; |
133 | char *kernelMax = IS_SET(CONVOLVE_5X5) ? "25" : "9" ; |
134 | char *target = IS_SET(CONVOLVE_RECT) ? "2DRect" : "2D" ; |
135 | char edge[100]; |
136 | char finalSource[2000]; |
137 | |
138 | J2dTraceLn1(J2D_TRACE_INFO, |
139 | "OGLBufImgOps_CreateConvolveProgram: flags=%d" , |
140 | flags); |
141 | |
142 | if (IS_SET(CONVOLVE_EDGE_ZERO_FILL)) { |
143 | // EDGE_ZERO_FILL: fill in zero at the edges |
144 | sprintf(edge, "sum = vec4(0.0);" ); |
145 | } else { |
146 | // EDGE_NO_OP: use the source pixel color at the edges |
147 | sprintf(edge, |
148 | "sum = texture%s(baseImage, gl_TexCoord[0].st);" , |
149 | target); |
150 | } |
151 | |
152 | // compose the final source code string from the various pieces |
153 | sprintf(finalSource, convolveShaderSource, |
154 | kernelMax, target, edge, target); |
155 | |
156 | convolveProgram = OGLContext_CreateFragmentProgram(finalSource); |
157 | if (convolveProgram == 0) { |
158 | J2dRlsTraceLn(J2D_TRACE_ERROR, |
159 | "OGLBufImgOps_CreateConvolveProgram: error creating program" ); |
160 | return 0; |
161 | } |
162 | |
163 | // "use" the program object temporarily so that we can set the uniforms |
164 | j2d_glUseProgramObjectARB(convolveProgram); |
165 | |
166 | // set the "uniform" texture unit binding |
167 | loc = j2d_glGetUniformLocationARB(convolveProgram, "baseImage" ); |
168 | j2d_glUniform1iARB(loc, 0); // texture unit 0 |
169 | |
170 | // "unuse" the program object; it will be re-bound later as needed |
171 | j2d_glUseProgramObjectARB(0); |
172 | |
173 | return convolveProgram; |
174 | } |
175 | |
176 | void |
177 | OGLBufImgOps_EnableConvolveOp(OGLContext *oglc, jlong pSrcOps, |
178 | jboolean edgeZeroFill, |
179 | jint kernelWidth, jint kernelHeight, |
180 | unsigned char *kernel) |
181 | { |
182 | OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps); |
183 | jint kernelSize = kernelWidth * kernelHeight; |
184 | GLhandleARB convolveProgram; |
185 | GLfloat xoff, yoff; |
186 | GLfloat edgeX, edgeY, minX, minY, maxX, maxY; |
187 | GLfloat kernelVals[MAX_KERNEL_SIZE*3]; |
188 | jint i, j, kIndex; |
189 | GLint loc; |
190 | jint flags = 0; |
191 | |
192 | J2dTraceLn2(J2D_TRACE_INFO, |
193 | "OGLBufImgOps_EnableConvolveOp: kernelW=%d kernelH=%d" , |
194 | kernelWidth, kernelHeight); |
195 | |
196 | RETURN_IF_NULL(oglc); |
197 | RETURN_IF_NULL(srcOps); |
198 | RESET_PREVIOUS_OP(); |
199 | |
200 | if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) { |
201 | flags |= CONVOLVE_RECT; |
202 | |
203 | // for GL_TEXTURE_RECTANGLE_ARB, texcoords are specified in the |
204 | // range [0,srcw] and [0,srch], so to achieve an x/y offset of |
205 | // exactly one pixel we simply use the value 1 here |
206 | xoff = 1.0f; |
207 | yoff = 1.0f; |
208 | } else { |
209 | // for GL_TEXTURE_2D, texcoords are specified in the range [0,1], |
210 | // so to achieve an x/y offset of approximately one pixel we have |
211 | // to normalize to that range here |
212 | xoff = 1.0f / srcOps->textureWidth; |
213 | yoff = 1.0f / srcOps->textureHeight; |
214 | } |
215 | if (edgeZeroFill) { |
216 | flags |= CONVOLVE_EDGE_ZERO_FILL; |
217 | } |
218 | if (kernelWidth == 5 && kernelHeight == 5) { |
219 | flags |= CONVOLVE_5X5; |
220 | } |
221 | |
222 | // locate/initialize the shader program for the given flags |
223 | if (convolvePrograms[flags] == 0) { |
224 | convolvePrograms[flags] = OGLBufImgOps_CreateConvolveProgram(flags); |
225 | if (convolvePrograms[flags] == 0) { |
226 | // shouldn't happen, but just in case... |
227 | return; |
228 | } |
229 | } |
230 | convolveProgram = convolvePrograms[flags]; |
231 | |
232 | // enable the convolve shader |
233 | j2d_glUseProgramObjectARB(convolveProgram); |
234 | |
235 | // update the "uniform" image min/max values |
236 | edgeX = (kernelWidth/2) * xoff; |
237 | edgeY = (kernelHeight/2) * yoff; |
238 | minX = edgeX; |
239 | minY = edgeY; |
240 | if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) { |
241 | // texcoords are in the range [0,srcw] and [0,srch] |
242 | maxX = ((GLfloat)srcOps->width) - edgeX; |
243 | maxY = ((GLfloat)srcOps->height) - edgeY; |
244 | } else { |
245 | // texcoords are in the range [0,1] |
246 | maxX = (((GLfloat)srcOps->width) / srcOps->textureWidth) - edgeX; |
247 | maxY = (((GLfloat)srcOps->height) / srcOps->textureHeight) - edgeY; |
248 | } |
249 | loc = j2d_glGetUniformLocationARB(convolveProgram, "imgEdge" ); |
250 | j2d_glUniform4fARB(loc, minX, minY, maxX, maxY); |
251 | |
252 | // update the "uniform" kernel offsets and values |
253 | loc = j2d_glGetUniformLocationARB(convolveProgram, "kernelVals" ); |
254 | kIndex = 0; |
255 | for (i = -kernelHeight/2; i < kernelHeight/2+1; i++) { |
256 | for (j = -kernelWidth/2; j < kernelWidth/2+1; j++) { |
257 | kernelVals[kIndex+0] = j*xoff; |
258 | kernelVals[kIndex+1] = i*yoff; |
259 | kernelVals[kIndex+2] = NEXT_FLOAT(kernel); |
260 | kIndex += 3; |
261 | } |
262 | } |
263 | j2d_glUniform3fvARB(loc, kernelSize, kernelVals); |
264 | } |
265 | |
266 | void |
267 | OGLBufImgOps_DisableConvolveOp(OGLContext *oglc) |
268 | { |
269 | J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_DisableConvolveOp" ); |
270 | |
271 | RETURN_IF_NULL(oglc); |
272 | |
273 | // disable the ConvolveOp shader |
274 | j2d_glUseProgramObjectARB(0); |
275 | } |
276 | |
277 | /**************************** RescaleOp support *****************************/ |
278 | |
279 | /** |
280 | * The RescaleOp shader is one of the simplest possible. Each fragment |
281 | * from the source image is multiplied by the user's scale factor and added |
282 | * to the user's offset value (these are component-wise operations). |
283 | * Finally, the resulting value is multiplied by the current OpenGL color, |
284 | * which contains the extra alpha value. |
285 | * |
286 | * The RescaleOp spec says that the operation is performed regardless of |
287 | * whether the source data is premultiplied or non-premultiplied. This is |
288 | * a problem for the OpenGL pipeline in that a non-premultiplied |
289 | * BufferedImage will have already been converted into premultiplied |
290 | * when uploaded to an OpenGL texture. Therefore, we have a special mode |
291 | * called RESCALE_NON_PREMULT (used only for source images that were |
292 | * originally non-premultiplied) that un-premultiplies the source color |
293 | * prior to the rescale operation, then re-premultiplies the resulting |
294 | * color before returning from the fragment shader. |
295 | * |
296 | * Note that this shader source code includes some "holes" marked by "%s". |
297 | * This allows us to build different shader programs (e.g. one for |
298 | * GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on) |
299 | * simply by filling in these "holes" with a call to sprintf(). See the |
300 | * OGLBufImgOps_CreateRescaleProgram() method for more details. |
301 | */ |
302 | static const char *rescaleShaderSource = |
303 | // image to be rescaled |
304 | "uniform sampler%s baseImage;" |
305 | // vector containing scale factors |
306 | "uniform vec4 scaleFactors;" |
307 | // vector containing offsets |
308 | "uniform vec4 offsets;" |
309 | "" |
310 | "void main(void)" |
311 | "{" |
312 | " vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);" |
313 | // (placeholder for un-premult code) |
314 | " %s" |
315 | // rescale source value |
316 | " vec4 result = (srcColor * scaleFactors) + offsets;" |
317 | // (placeholder for re-premult code) |
318 | " %s" |
319 | // modulate with gl_Color in order to apply extra alpha |
320 | " gl_FragColor = result * gl_Color;" |
321 | "}" ; |
322 | |
323 | /** |
324 | * Flags that can be bitwise-or'ed together to control how the shader |
325 | * source code is generated. |
326 | */ |
327 | #define RESCALE_RECT (1 << 0) |
328 | #define RESCALE_NON_PREMULT (1 << 1) |
329 | |
330 | /** |
331 | * The handles to the RescaleOp fragment program objects. The index to |
332 | * the array should be a bitwise-or'ing of the RESCALE_* flags defined |
333 | * above. Note that most applications will likely need to initialize one |
334 | * or two of these elements, so the array is usually sparsely populated. |
335 | */ |
336 | static GLhandleARB rescalePrograms[4]; |
337 | |
338 | /** |
339 | * Compiles and links the RescaleOp shader program. If successful, this |
340 | * function returns a handle to the newly created shader program; otherwise |
341 | * returns 0. |
342 | */ |
343 | static GLhandleARB |
344 | OGLBufImgOps_CreateRescaleProgram(jint flags) |
345 | { |
346 | GLhandleARB rescaleProgram; |
347 | GLint loc; |
348 | char *target = IS_SET(RESCALE_RECT) ? "2DRect" : "2D" ; |
349 | char *preRescale = "" ; |
350 | char *postRescale = "" ; |
351 | char finalSource[2000]; |
352 | |
353 | J2dTraceLn1(J2D_TRACE_INFO, |
354 | "OGLBufImgOps_CreateRescaleProgram: flags=%d" , |
355 | flags); |
356 | |
357 | if (IS_SET(RESCALE_NON_PREMULT)) { |
358 | preRescale = "srcColor.rgb /= srcColor.a;" ; |
359 | postRescale = "result.rgb *= result.a;" ; |
360 | } |
361 | |
362 | // compose the final source code string from the various pieces |
363 | sprintf(finalSource, rescaleShaderSource, |
364 | target, target, preRescale, postRescale); |
365 | |
366 | rescaleProgram = OGLContext_CreateFragmentProgram(finalSource); |
367 | if (rescaleProgram == 0) { |
368 | J2dRlsTraceLn(J2D_TRACE_ERROR, |
369 | "OGLBufImgOps_CreateRescaleProgram: error creating program" ); |
370 | return 0; |
371 | } |
372 | |
373 | // "use" the program object temporarily so that we can set the uniforms |
374 | j2d_glUseProgramObjectARB(rescaleProgram); |
375 | |
376 | // set the "uniform" values |
377 | loc = j2d_glGetUniformLocationARB(rescaleProgram, "baseImage" ); |
378 | j2d_glUniform1iARB(loc, 0); // texture unit 0 |
379 | |
380 | // "unuse" the program object; it will be re-bound later as needed |
381 | j2d_glUseProgramObjectARB(0); |
382 | |
383 | return rescaleProgram; |
384 | } |
385 | |
386 | void |
387 | OGLBufImgOps_EnableRescaleOp(OGLContext *oglc, jlong pSrcOps, |
388 | jboolean nonPremult, |
389 | unsigned char *scaleFactors, |
390 | unsigned char *offsets) |
391 | { |
392 | OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps); |
393 | GLhandleARB rescaleProgram; |
394 | GLint loc; |
395 | jint flags = 0; |
396 | |
397 | J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_EnableRescaleOp" ); |
398 | |
399 | RETURN_IF_NULL(oglc); |
400 | RETURN_IF_NULL(srcOps); |
401 | RESET_PREVIOUS_OP(); |
402 | |
403 | // choose the appropriate shader, depending on the source texture target |
404 | if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) { |
405 | flags |= RESCALE_RECT; |
406 | } |
407 | if (nonPremult) { |
408 | flags |= RESCALE_NON_PREMULT; |
409 | } |
410 | |
411 | // locate/initialize the shader program for the given flags |
412 | if (rescalePrograms[flags] == 0) { |
413 | rescalePrograms[flags] = OGLBufImgOps_CreateRescaleProgram(flags); |
414 | if (rescalePrograms[flags] == 0) { |
415 | // shouldn't happen, but just in case... |
416 | return; |
417 | } |
418 | } |
419 | rescaleProgram = rescalePrograms[flags]; |
420 | |
421 | // enable the rescale shader |
422 | j2d_glUseProgramObjectARB(rescaleProgram); |
423 | |
424 | // update the "uniform" scale factor values (note that the Java-level |
425 | // dispatching code always passes down 4 values here, regardless of |
426 | // the original source image type) |
427 | loc = j2d_glGetUniformLocationARB(rescaleProgram, "scaleFactors" ); |
428 | { |
429 | GLfloat sf1 = NEXT_FLOAT(scaleFactors); |
430 | GLfloat sf2 = NEXT_FLOAT(scaleFactors); |
431 | GLfloat sf3 = NEXT_FLOAT(scaleFactors); |
432 | GLfloat sf4 = NEXT_FLOAT(scaleFactors); |
433 | j2d_glUniform4fARB(loc, sf1, sf2, sf3, sf4); |
434 | } |
435 | |
436 | // update the "uniform" offset values (note that the Java-level |
437 | // dispatching code always passes down 4 values here, and that the |
438 | // offsets will have already been normalized to the range [0,1]) |
439 | loc = j2d_glGetUniformLocationARB(rescaleProgram, "offsets" ); |
440 | { |
441 | GLfloat off1 = NEXT_FLOAT(offsets); |
442 | GLfloat off2 = NEXT_FLOAT(offsets); |
443 | GLfloat off3 = NEXT_FLOAT(offsets); |
444 | GLfloat off4 = NEXT_FLOAT(offsets); |
445 | j2d_glUniform4fARB(loc, off1, off2, off3, off4); |
446 | } |
447 | } |
448 | |
449 | void |
450 | OGLBufImgOps_DisableRescaleOp(OGLContext *oglc) |
451 | { |
452 | J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_DisableRescaleOp" ); |
453 | |
454 | RETURN_IF_NULL(oglc); |
455 | |
456 | // disable the RescaleOp shader |
457 | j2d_glUseProgramObjectARB(0); |
458 | } |
459 | |
460 | /**************************** LookupOp support ******************************/ |
461 | |
462 | /** |
463 | * The LookupOp shader takes a fragment color (from the source texture) as |
464 | * input, subtracts the optional user offset value, and then uses the |
465 | * resulting value to index into the lookup table texture to provide |
466 | * a new color result. Finally, the resulting value is multiplied by |
467 | * the current OpenGL color, which contains the extra alpha value. |
468 | * |
469 | * The lookup step requires 3 texture accesses (or 4, when alpha is included), |
470 | * which is somewhat unfortunate because it's not ideal from a performance |
471 | * standpoint, but that sort of thing is getting faster with newer hardware. |
472 | * In the 3-band case, we could consider using a three-dimensional texture |
473 | * and performing the lookup with a single texture access step. We already |
474 | * use this approach in the LCD text shader, and it works well, but for the |
475 | * purposes of this LookupOp shader, it's probably overkill. Also, there's |
476 | * a difference in that the LCD text shader only needs to populate the 3D LUT |
477 | * once, but here we would need to populate it on every invocation, which |
478 | * would likely be a waste of VRAM and CPU/GPU cycles. |
479 | * |
480 | * The LUT texture is currently hardcoded as 4 rows/bands, each containing |
481 | * 256 elements. This means that we currently only support user-provided |
482 | * tables with no more than 256 elements in each band (this is checked at |
483 | * at the Java level). If the user provides a table with less than 256 |
484 | * elements per band, our shader will still work fine, but if elements are |
485 | * accessed with an index >= the size of the LUT, then the shader will simply |
486 | * produce undefined values. Typically the user would provide an offset |
487 | * value that would prevent this from happening, but it's worth pointing out |
488 | * this fact because the software LookupOp implementation would usually |
489 | * throw an ArrayIndexOutOfBoundsException in this scenario (although it is |
490 | * not something demanded by the spec). |
491 | * |
492 | * The LookupOp spec says that the operation is performed regardless of |
493 | * whether the source data is premultiplied or non-premultiplied. This is |
494 | * a problem for the OpenGL pipeline in that a non-premultiplied |
495 | * BufferedImage will have already been converted into premultiplied |
496 | * when uploaded to an OpenGL texture. Therefore, we have a special mode |
497 | * called LOOKUP_NON_PREMULT (used only for source images that were |
498 | * originally non-premultiplied) that un-premultiplies the source color |
499 | * prior to the lookup operation, then re-premultiplies the resulting |
500 | * color before returning from the fragment shader. |
501 | * |
502 | * Note that this shader source code includes some "holes" marked by "%s". |
503 | * This allows us to build different shader programs (e.g. one for |
504 | * GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on) |
505 | * simply by filling in these "holes" with a call to sprintf(). See the |
506 | * OGLBufImgOps_CreateLookupProgram() method for more details. |
507 | */ |
508 | static const char *lookupShaderSource = |
509 | // source image (bound to texture unit 0) |
510 | "uniform sampler%s baseImage;" |
511 | // lookup table (bound to texture unit 1) |
512 | "uniform sampler2D lookupTable;" |
513 | // offset subtracted from source index prior to lookup step |
514 | "uniform vec4 offset;" |
515 | "" |
516 | "void main(void)" |
517 | "{" |
518 | " vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);" |
519 | // (placeholder for un-premult code) |
520 | " %s" |
521 | // subtract offset from original index |
522 | " vec4 srcIndex = srcColor - offset;" |
523 | // use source value as input to lookup table (note that |
524 | // "v" texcoords are hardcoded to hit texel centers of |
525 | // each row/band in texture) |
526 | " vec4 result;" |
527 | " result.r = texture2D(lookupTable, vec2(srcIndex.r, 0.125)).r;" |
528 | " result.g = texture2D(lookupTable, vec2(srcIndex.g, 0.375)).r;" |
529 | " result.b = texture2D(lookupTable, vec2(srcIndex.b, 0.625)).r;" |
530 | // (placeholder for alpha store code) |
531 | " %s" |
532 | // (placeholder for re-premult code) |
533 | " %s" |
534 | // modulate with gl_Color in order to apply extra alpha |
535 | " gl_FragColor = result * gl_Color;" |
536 | "}" ; |
537 | |
538 | /** |
539 | * Flags that can be bitwise-or'ed together to control how the shader |
540 | * source code is generated. |
541 | */ |
542 | #define LOOKUP_RECT (1 << 0) |
543 | #define LOOKUP_USE_SRC_ALPHA (1 << 1) |
544 | #define LOOKUP_NON_PREMULT (1 << 2) |
545 | |
546 | /** |
547 | * The handles to the LookupOp fragment program objects. The index to |
548 | * the array should be a bitwise-or'ing of the LOOKUP_* flags defined |
549 | * above. Note that most applications will likely need to initialize one |
550 | * or two of these elements, so the array is usually sparsely populated. |
551 | */ |
552 | static GLhandleARB lookupPrograms[8]; |
553 | |
554 | /** |
555 | * The handle to the lookup table texture object used by the shader. |
556 | */ |
557 | static GLuint lutTextureID = 0; |
558 | |
559 | /** |
560 | * Compiles and links the LookupOp shader program. If successful, this |
561 | * function returns a handle to the newly created shader program; otherwise |
562 | * returns 0. |
563 | */ |
564 | static GLhandleARB |
565 | OGLBufImgOps_CreateLookupProgram(jint flags) |
566 | { |
567 | GLhandleARB lookupProgram; |
568 | GLint loc; |
569 | char *target = IS_SET(LOOKUP_RECT) ? "2DRect" : "2D" ; |
570 | char *alpha; |
571 | char *preLookup = "" ; |
572 | char *postLookup = "" ; |
573 | char finalSource[2000]; |
574 | |
575 | J2dTraceLn1(J2D_TRACE_INFO, |
576 | "OGLBufImgOps_CreateLookupProgram: flags=%d" , |
577 | flags); |
578 | |
579 | if (IS_SET(LOOKUP_USE_SRC_ALPHA)) { |
580 | // when numComps is 1 or 3, the alpha is not looked up in the table; |
581 | // just keep the alpha from the source fragment |
582 | alpha = "result.a = srcColor.a;" ; |
583 | } else { |
584 | // when numComps is 4, the alpha is looked up in the table, just |
585 | // like the other color components from the source fragment |
586 | alpha = |
587 | "result.a = texture2D(lookupTable, vec2(srcIndex.a, 0.875)).r;" ; |
588 | } |
589 | if (IS_SET(LOOKUP_NON_PREMULT)) { |
590 | preLookup = "srcColor.rgb /= srcColor.a;" ; |
591 | postLookup = "result.rgb *= result.a;" ; |
592 | } |
593 | |
594 | // compose the final source code string from the various pieces |
595 | sprintf(finalSource, lookupShaderSource, |
596 | target, target, preLookup, alpha, postLookup); |
597 | |
598 | lookupProgram = OGLContext_CreateFragmentProgram(finalSource); |
599 | if (lookupProgram == 0) { |
600 | J2dRlsTraceLn(J2D_TRACE_ERROR, |
601 | "OGLBufImgOps_CreateLookupProgram: error creating program" ); |
602 | return 0; |
603 | } |
604 | |
605 | // "use" the program object temporarily so that we can set the uniforms |
606 | j2d_glUseProgramObjectARB(lookupProgram); |
607 | |
608 | // set the "uniform" values |
609 | loc = j2d_glGetUniformLocationARB(lookupProgram, "baseImage" ); |
610 | j2d_glUniform1iARB(loc, 0); // texture unit 0 |
611 | loc = j2d_glGetUniformLocationARB(lookupProgram, "lookupTable" ); |
612 | j2d_glUniform1iARB(loc, 1); // texture unit 1 |
613 | |
614 | // "unuse" the program object; it will be re-bound later as needed |
615 | j2d_glUseProgramObjectARB(0); |
616 | |
617 | return lookupProgram; |
618 | } |
619 | |
620 | void |
621 | OGLBufImgOps_EnableLookupOp(OGLContext *oglc, jlong pSrcOps, |
622 | jboolean nonPremult, jboolean shortData, |
623 | jint numBands, jint bandLength, jint offset, |
624 | void *tableValues) |
625 | { |
626 | OGLSDOps *srcOps = (OGLSDOps *)jlong_to_ptr(pSrcOps); |
627 | int bytesPerElem = (shortData ? 2 : 1); |
628 | GLhandleARB lookupProgram; |
629 | GLfloat foff; |
630 | GLint loc; |
631 | void *bands[4]; |
632 | int i; |
633 | jint flags = 0; |
634 | |
635 | J2dTraceLn4(J2D_TRACE_INFO, |
636 | "OGLBufImgOps_EnableLookupOp: short=%d num=%d len=%d off=%d" , |
637 | shortData, numBands, bandLength, offset); |
638 | |
639 | for (i = 0; i < 4; i++) { |
640 | bands[i] = NULL; |
641 | } |
642 | RETURN_IF_NULL(oglc); |
643 | RETURN_IF_NULL(srcOps); |
644 | RESET_PREVIOUS_OP(); |
645 | |
646 | // choose the appropriate shader, depending on the source texture target |
647 | // and the number of bands involved |
648 | if (srcOps->textureTarget == GL_TEXTURE_RECTANGLE_ARB) { |
649 | flags |= LOOKUP_RECT; |
650 | } |
651 | if (numBands != 4) { |
652 | flags |= LOOKUP_USE_SRC_ALPHA; |
653 | } |
654 | if (nonPremult) { |
655 | flags |= LOOKUP_NON_PREMULT; |
656 | } |
657 | |
658 | // locate/initialize the shader program for the given flags |
659 | if (lookupPrograms[flags] == 0) { |
660 | lookupPrograms[flags] = OGLBufImgOps_CreateLookupProgram(flags); |
661 | if (lookupPrograms[flags] == 0) { |
662 | // shouldn't happen, but just in case... |
663 | return; |
664 | } |
665 | } |
666 | lookupProgram = lookupPrograms[flags]; |
667 | |
668 | // enable the lookup shader |
669 | j2d_glUseProgramObjectARB(lookupProgram); |
670 | |
671 | // update the "uniform" offset value |
672 | loc = j2d_glGetUniformLocationARB(lookupProgram, "offset" ); |
673 | foff = offset / 255.0f; |
674 | j2d_glUniform4fARB(loc, foff, foff, foff, foff); |
675 | |
676 | // bind the lookup table to texture unit 1 and enable texturing |
677 | j2d_glActiveTextureARB(GL_TEXTURE1_ARB); |
678 | if (lutTextureID == 0) { |
679 | /* |
680 | * Create the lookup table texture with 4 rows (one band per row) |
681 | * and 256 columns (one LUT band element per column) and with an |
682 | * internal format of 16-bit luminance values, which will be |
683 | * sufficient for either byte or short LUT data. Note that the |
684 | * texture wrap mode will be set to the default of GL_CLAMP_TO_EDGE, |
685 | * which means that out-of-range index value will be clamped |
686 | * appropriately. |
687 | */ |
688 | lutTextureID = |
689 | OGLContext_CreateBlitTexture(GL_LUMINANCE16, GL_LUMINANCE, |
690 | 256, 4); |
691 | if (lutTextureID == 0) { |
692 | // should never happen, but just to be safe... |
693 | return; |
694 | } |
695 | } |
696 | j2d_glBindTexture(GL_TEXTURE_2D, lutTextureID); |
697 | j2d_glEnable(GL_TEXTURE_2D); |
698 | |
699 | // update the lookup table with the user-provided values |
700 | if (numBands == 1) { |
701 | // replicate the single band for R/G/B; alpha band is unused |
702 | for (i = 0; i < 3; i++) { |
703 | bands[i] = tableValues; |
704 | } |
705 | bands[3] = NULL; |
706 | } else if (numBands == 3) { |
707 | // user supplied band for each of R/G/B; alpha band is unused |
708 | for (i = 0; i < 3; i++) { |
709 | bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem); |
710 | } |
711 | bands[3] = NULL; |
712 | } else if (numBands == 4) { |
713 | // user supplied band for each of R/G/B/A |
714 | for (i = 0; i < 4; i++) { |
715 | bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem); |
716 | } |
717 | } |
718 | |
719 | // upload the bands one row at a time into our lookup table texture |
720 | for (i = 0; i < 4; i++) { |
721 | if (bands[i] == NULL) { |
722 | continue; |
723 | } |
724 | j2d_glTexSubImage2D(GL_TEXTURE_2D, 0, |
725 | 0, i, bandLength, 1, |
726 | GL_LUMINANCE, |
727 | shortData ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE, |
728 | bands[i]); |
729 | } |
730 | |
731 | // restore texture unit 0 (the default) as the active one since |
732 | // the OGLBlitTextureToSurface() method is responsible for binding the |
733 | // source image texture, which will happen later |
734 | j2d_glActiveTextureARB(GL_TEXTURE0_ARB); |
735 | } |
736 | |
737 | void |
738 | OGLBufImgOps_DisableLookupOp(OGLContext *oglc) |
739 | { |
740 | J2dTraceLn(J2D_TRACE_INFO, "OGLBufImgOps_DisableLookupOp" ); |
741 | |
742 | RETURN_IF_NULL(oglc); |
743 | |
744 | // disable the LookupOp shader |
745 | j2d_glUseProgramObjectARB(0); |
746 | |
747 | // disable the lookup table on texture unit 1 |
748 | j2d_glActiveTextureARB(GL_TEXTURE1_ARB); |
749 | j2d_glDisable(GL_TEXTURE_2D); |
750 | j2d_glActiveTextureARB(GL_TEXTURE0_ARB); |
751 | } |
752 | |
753 | #endif /* !HEADLESS */ |
754 | |