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