1 /*
   2  * Copyright (c) 2003, 2013, 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 <stdlib.h>
  29 #include <math.h>
  30 #include <jlong.h>
  31 
  32 #include "sun_java2d_opengl_OGLTextRenderer.h"
  33 
  34 #include "SurfaceData.h"
  35 #include "OGLContext.h"
  36 #include "OGLSurfaceData.h"
  37 #include "OGLRenderQueue.h"
  38 #include "OGLTextRenderer.h"
  39 #include "OGLVertexCache.h"
  40 #include "AccelGlyphCache.h"
  41 #include "fontscalerdefs.h"
  42 
  43 /**
  44  * The following constants define the inner and outer bounds of the
  45  * accelerated glyph cache.
  46  */
  47 #define OGLTR_CACHE_WIDTH       512
  48 #define OGLTR_CACHE_HEIGHT      512
  49 #define OGLTR_CACHE_CELL_WIDTH  16
  50 #define OGLTR_CACHE_CELL_HEIGHT 16
  51 
  52 /**
  53  * The current "glyph mode" state.  This variable is used to track the
  54  * codepath used to render a particular glyph.  This variable is reset to
  55  * MODE_NOT_INITED at the beginning of every call to OGLTR_DrawGlyphList().
  56  * As each glyph is rendered, the glyphMode variable is updated to reflect
  57  * the current mode, so if the current mode is the same as the mode used
  58  * to render the previous glyph, we can avoid doing costly setup operations
  59  * each time.
  60  */
  61 typedef enum {
  62     MODE_NOT_INITED,
  63     MODE_USE_CACHE_GRAY,
  64     MODE_USE_CACHE_LCD,
  65     MODE_NO_CACHE_GRAY,
  66     MODE_NO_CACHE_LCD
  67 } GlyphMode;
  68 static GlyphMode glyphMode = MODE_NOT_INITED;
  69 
  70 /**
  71  * This enum indicates the current state of the hardware glyph cache.
  72  * Initially the CacheStatus is set to CACHE_NOT_INITED, and then it is
  73  * set to either GRAY or LCD when the glyph cache is initialized.
  74  */
  75 typedef enum {
  76     CACHE_NOT_INITED,
  77     CACHE_GRAY,
  78     CACHE_LCD
  79 } CacheStatus;
  80 static CacheStatus cacheStatus = CACHE_NOT_INITED;
  81 
  82 /**
  83  * This is the one glyph cache.  Once it is initialized as either GRAY or
  84  * LCD, it stays in that mode for the duration of the application.  It should
  85  * be safe to use this one glyph cache for all screens in a multimon
  86  * environment, since the glyph cache texture is shared between all contexts,
  87  * and (in theory) OpenGL drivers should be smart enough to manage that
  88  * texture across all screens.
  89  */
  90 static GlyphCacheInfo *glyphCache = NULL;
  91 
  92 /**
  93  * The handle to the LCD text fragment program object.
  94  */
  95 static GLhandleARB lcdTextProgram = 0;
  96 
  97 /**
  98  * The size of one of the gamma LUT textures in any one dimension along
  99  * the edge, in texels.
 100  */
 101 #define LUT_EDGE 16
 102 
 103 /**
 104  * These are the texture object handles for the gamma and inverse gamma
 105  * lookup tables.
 106  */
 107 static GLuint gammaLutTextureID = 0;
 108 static GLuint invGammaLutTextureID = 0;
 109 
 110 /**
 111  * This value tracks the previous LCD contrast setting, so if the contrast
 112  * value hasn't changed since the last time the lookup tables were
 113  * generated (not very common), then we can skip updating the tables.
 114  */
 115 static jint lastLCDContrast = -1;
 116 
 117 /**
 118  * This value tracks the previous LCD rgbOrder setting, so if the rgbOrder
 119  * value has changed since the last time, it indicates that we need to
 120  * invalidate the cache, which may already store glyph images in the reverse
 121  * order.  Note that in most real world applications this value will not
 122  * change over the course of the application, but tests like Font2DTest
 123  * allow for changing the ordering at runtime, so we need to handle that case.
 124  */
 125 static jboolean lastRGBOrder = JNI_TRUE;
 126 
 127 /**
 128  * This constant defines the size of the tile to use in the
 129  * OGLTR_DrawLCDGlyphNoCache() method.  See below for more on why we
 130  * restrict this value to a particular size.
 131  */
 132 #define OGLTR_NOCACHE_TILE_SIZE 32
 133 
 134 /**
 135  * These constants define the size of the "cached destination" texture.
 136  * This texture is only used when rendering LCD-optimized text, as that
 137  * codepath needs direct access to the destination.  There is no way to
 138  * access the framebuffer directly from an OpenGL shader, so we need to first
 139  * copy the destination region corresponding to a particular glyph into
 140  * this cached texture, and then that texture will be accessed inside the
 141  * shader.  Copying the destination into this cached texture can be a very
 142  * expensive operation (accounting for about half the rendering time for
 143  * LCD text), so to mitigate this cost we try to bulk read a horizontal
 144  * region of the destination at a time.  (These values are empirically
 145  * derived for the common case where text runs horizontally.)
 146  *
 147  * Note: It is assumed in various calculations below that:
 148  *     (OGLTR_CACHED_DEST_WIDTH  >= OGLTR_CACHE_CELL_WIDTH)  &&
 149  *     (OGLTR_CACHED_DEST_WIDTH  >= OGLTR_NOCACHE_TILE_SIZE) &&
 150  *     (OGLTR_CACHED_DEST_HEIGHT >= OGLTR_CACHE_CELL_HEIGHT) &&
 151  *     (OGLTR_CACHED_DEST_HEIGHT >= OGLTR_NOCACHE_TILE_SIZE)
 152  */
 153 #define OGLTR_CACHED_DEST_WIDTH  512
 154 #define OGLTR_CACHED_DEST_HEIGHT 32
 155 
 156 /**
 157  * The handle to the "cached destination" texture object.
 158  */
 159 static GLuint cachedDestTextureID = 0;
 160 
 161 /**
 162  * The current bounds of the "cached destination" texture, in destination
 163  * coordinate space.  The width/height of these bounds will not exceed the
 164  * OGLTR_CACHED_DEST_WIDTH/HEIGHT values defined above.  These bounds are
 165  * only considered valid when the isCachedDestValid flag is JNI_TRUE.
 166  */
 167 static SurfaceDataBounds cachedDestBounds;
 168 
 169 /**
 170  * This flag indicates whether the "cached destination" texture contains
 171  * valid data.  This flag is reset to JNI_FALSE at the beginning of every
 172  * call to OGLTR_DrawGlyphList().  Once we copy valid destination data
 173  * into the cached texture, this flag is set to JNI_TRUE.  This way, we can
 174  * limit the number of times we need to copy destination data, which is a
 175  * very costly operation.
 176  */
 177 static jboolean isCachedDestValid = JNI_FALSE;
 178 
 179 /**
 180  * The bounds of the previously rendered LCD glyph, in destination
 181  * coordinate space.  We use these bounds to determine whether the glyph
 182  * currently being rendered overlaps the previously rendered glyph (i.e.
 183  * its bounding box intersects that of the previously rendered glyph).  If
 184  * so, we need to re-read the destination area associated with that previous
 185  * glyph so that we can correctly blend with the actual destination data.
 186  */
 187 static SurfaceDataBounds previousGlyphBounds;
 188 
 189 /**
 190  * Initializes the one glyph cache (texture and data structure).
 191  * If lcdCache is JNI_TRUE, the texture will contain RGB data,
 192  * otherwise we will simply store the grayscale/monochrome glyph images
 193  * as intensity values (which work well with the GL_MODULATE function).
 194  */
 195 static jboolean
 196 OGLTR_InitGlyphCache(jboolean lcdCache)
 197 {
 198     GlyphCacheInfo *gcinfo;
 199     GLclampf priority = 1.0f;
 200     GLenum internalFormat = lcdCache ? GL_RGB8 : GL_INTENSITY8;
 201     GLenum pixelFormat = lcdCache ? GL_RGB : GL_LUMINANCE;
 202 
 203     J2dTraceLn(J2D_TRACE_INFO, "OGLTR_InitGlyphCache");
 204 
 205     // init glyph cache data structure
 206     gcinfo = AccelGlyphCache_Init(OGLTR_CACHE_WIDTH,
 207                                   OGLTR_CACHE_HEIGHT,
 208                                   OGLTR_CACHE_CELL_WIDTH,
 209                                   OGLTR_CACHE_CELL_HEIGHT,
 210                                   OGLVertexCache_FlushVertexCache);
 211     if (gcinfo == NULL) {
 212         J2dRlsTraceLn(J2D_TRACE_ERROR,
 213                       "OGLTR_InitGlyphCache: could not init OGL glyph cache");
 214         return JNI_FALSE;
 215     }
 216 
 217     // init cache texture object
 218     j2d_glGenTextures(1, &gcinfo->cacheID);
 219     j2d_glBindTexture(GL_TEXTURE_2D, gcinfo->cacheID);
 220     j2d_glPrioritizeTextures(1, &gcinfo->cacheID, &priority);
 221     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 222     j2d_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 223 
 224     j2d_glTexImage2D(GL_TEXTURE_2D, 0, internalFormat,
 225                      OGLTR_CACHE_WIDTH, OGLTR_CACHE_HEIGHT, 0,
 226                      pixelFormat, GL_UNSIGNED_BYTE, NULL);
 227 
 228     cacheStatus = (lcdCache ? CACHE_LCD : CACHE_GRAY);
 229     glyphCache = gcinfo;
 230 
 231     return JNI_TRUE;
 232 }
 233 
 234 /**
 235  * Adds the given glyph to the glyph cache (texture and data structure)
 236  * associated with the given OGLContext.
 237  */
 238 static void
 239 OGLTR_AddToGlyphCache(GlyphInfo *glyph, jboolean rgbOrder)
 240 {
 241     GLenum pixelFormat;
 242     CacheCellInfo *ccinfo;
 243 
 244     J2dTraceLn(J2D_TRACE_INFO, "OGLTR_AddToGlyphCache");
 245 
 246     if ((glyphCache == NULL) || (glyph->image == NULL)) {
 247         return;
 248     }
 249 
 250     if (cacheStatus == CACHE_LCD) {
 251         pixelFormat = rgbOrder ? GL_RGB : GL_BGR;
 252     } else {
 253         pixelFormat = GL_LUMINANCE;
 254     }
 255 
 256     AccelGlyphCache_AddGlyph(glyphCache, glyph);
 257     ccinfo = (CacheCellInfo *) glyph->cellInfo;
 258 
 259     if (ccinfo != NULL) {
 260         // store glyph image in texture cell
 261         j2d_glTexSubImage2D(GL_TEXTURE_2D, 0,
 262                             ccinfo->x, ccinfo->y,
 263                             glyph->width, glyph->height,
 264                             pixelFormat, GL_UNSIGNED_BYTE, glyph->image);
 265     }
 266 }
 267 
 268 /**
 269  * This is the GLSL fragment shader source code for rendering LCD-optimized
 270  * text.  Do not be frightened; it is much easier to understand than the
 271  * equivalent ASM-like fragment program!
 272  *
 273  * The "uniform" variables at the top are initialized once the program is
 274  * linked, and are updated at runtime as needed (e.g. when the source color
 275  * changes, we will modify the "src_adj" value in OGLTR_UpdateLCDTextColor()).
 276  *
 277  * The "main" function is executed for each "fragment" (or pixel) in the
 278  * glyph image. The pow() routine operates on vectors, gives precise results,
 279  * and provides acceptable level of performance, so we use it to perform
 280  * the gamma adjustment.
 281  *
 282  * The variables involved in the equation can be expressed as follows:
 283  *
 284  *   Cs = Color component of the source (foreground color) [0.0, 1.0]
 285  *   Cd = Color component of the destination (background color) [0.0, 1.0]
 286  *   Cr = Color component to be written to the destination [0.0, 1.0]
 287  *   Ag = Glyph alpha (aka intensity or coverage) [0.0, 1.0]
 288  *   Ga = Gamma adjustment in the range [1.0, 2.5]
 289  *   (^ means raised to the power)
 290  *
 291  * And here is the theoretical equation approximated by this shader:
 292  *
 293  *            Cr = (Ag*(Cs^Ga) + (1-Ag)*(Cd^Ga)) ^ (1/Ga)
 294  */
 295 static const char *lcdTextShaderSource =
 296     "uniform vec3 src_adj;"
 297     "uniform sampler2D glyph_tex;"
 298     "uniform sampler2D dst_tex;"
 299     "uniform vec3 gamma;"
 300     "uniform vec3 invgamma;"
 301     ""
 302     "void main(void)"
 303     "{"
 304          // load the RGB value from the glyph image at the current texcoord
 305     "    vec3 glyph_clr = vec3(texture2D(glyph_tex, gl_TexCoord[0].st));"
 306     "    if (glyph_clr == vec3(0.0)) {"
 307              // zero coverage, so skip this fragment
 308     "        discard;"
 309     "    }"
 310          // load the RGB value from the corresponding destination pixel
 311     "    vec3 dst_clr = vec3(texture2D(dst_tex, gl_TexCoord[1].st));"
 312          // gamma adjust the dest color
 313     "    vec3 dst_adj = pow(dst_clr.rgb, gamma);"
 314          // linearly interpolate the three color values
 315     "    vec3 result = mix(dst_adj, src_adj, glyph_clr);"
 316          // gamma re-adjust the resulting color (alpha is always set to 1.0)
 317     "    gl_FragColor = vec4(pow(result.rgb, invgamma), 1.0);"
 318     "}";
 319 
 320 /**
 321  * Compiles and links the LCD text shader program.  If successful, this
 322  * function returns a handle to the newly created shader program; otherwise
 323  * returns 0.
 324  */
 325 static GLhandleARB
 326 OGLTR_CreateLCDTextProgram()
 327 {
 328     GLhandleARB lcdTextProgram;
 329     GLint loc;
 330 
 331     J2dTraceLn(J2D_TRACE_INFO, "OGLTR_CreateLCDTextProgram");
 332 
 333     lcdTextProgram = OGLContext_CreateFragmentProgram(lcdTextShaderSource);
 334     if (lcdTextProgram == 0) {
 335         J2dRlsTraceLn(J2D_TRACE_ERROR,
 336                       "OGLTR_CreateLCDTextProgram: error creating program");
 337         return 0;
 338     }
 339 
 340     // "use" the program object temporarily so that we can set the uniforms
 341     j2d_glUseProgramObjectARB(lcdTextProgram);
 342 
 343     // set the "uniform" values
 344     loc = j2d_glGetUniformLocationARB(lcdTextProgram, "glyph_tex");
 345     j2d_glUniform1iARB(loc, 0); // texture unit 0
 346     loc = j2d_glGetUniformLocationARB(lcdTextProgram, "dst_tex");
 347     j2d_glUniform1iARB(loc, 1); // texture unit 1
 348     
 349     // "unuse" the program object; it will be re-bound later as needed
 350     j2d_glUseProgramObjectARB(0);
 351 
 352     return lcdTextProgram;
 353 }
 354 
 355 /**
 356  * (Re)Initializes the gamma related uniforms.
 357  *
 358  * The given contrast value is an int in the range [100, 250] which we will
 359  * then scale to fit in the range [1.0, 2.5].  
 360  */
 361 static jboolean
 362 OGLTR_UpdateLCDTextContrast(jint contrast)
 363 {
 364     double g = ((double)contrast) / 100.0;
 365     double ig = 1.0 / g;
 366     GLint loc;
 367 
 368     J2dTraceLn1(J2D_TRACE_INFO,
 369                 "OGLTR_UpdateLCDTextContrast: contrast=%d", contrast);
 370     
 371     loc = j2d_glGetUniformLocationARB(lcdTextProgram, "gamma");
 372     j2d_glUniform3fARB(loc, g, g, g);
 373 
 374     loc = j2d_glGetUniformLocationARB(lcdTextProgram, "invgamma");
 375     j2d_glUniform3fARB(loc, ig, ig, ig);
 376 
 377     return JNI_TRUE;
 378 }
 379 
 380 /**
 381  * Updates the current gamma-adjusted source color ("src_adj") of the LCD
 382  * text shader program.  Note that we could calculate this value in the
 383  * shader (e.g. just as we do for "dst_adj"), but would be unnecessary work
 384  * (and a measurable performance hit, maybe around 5%) since this value is
 385  * constant over the entire glyph list.  So instead we just calculate the
 386  * gamma-adjusted value once and update the uniform parameter of the LCD
 387  * shader as needed.
 388  */
 389 static jboolean
 390 OGLTR_UpdateLCDTextColor(jint contrast)
 391 {
 392     double gamma = ((double)contrast) / 100.0;
 393     GLfloat radj, gadj, badj;
 394     GLfloat clr[4];
 395     GLint loc;
 396 
 397     J2dTraceLn1(J2D_TRACE_INFO,
 398                 "OGLTR_UpdateLCDTextColor: contrast=%d", contrast);
 399 
 400     /*
 401      * Note: Ideally we would update the "src_adj" uniform parameter only
 402      * when there is a change in the source color.  Fortunately, the cost
 403      * of querying the current OpenGL color state and updating the uniform
 404      * value is quite small, and in the common case we only need to do this
 405      * once per GlyphList, so we gain little from trying to optimize too
 406      * eagerly here.
 407      */
 408 
 409     // get the current OpenGL primary color state
 410     j2d_glGetFloatv(GL_CURRENT_COLOR, clr);
 411 
 412     // gamma adjust the primary color
 413     radj = (GLfloat)pow(clr[0], gamma);
 414     gadj = (GLfloat)pow(clr[1], gamma);
 415     badj = (GLfloat)pow(clr[2], gamma);
 416 
 417     // update the "src_adj" parameter of the shader program with this value
 418     loc = j2d_glGetUniformLocationARB(lcdTextProgram, "src_adj");
 419     j2d_glUniform3fARB(loc, radj, gadj, badj);
 420 
 421     return JNI_TRUE;
 422 }
 423 
 424 /**
 425  * Enables the LCD text shader and updates any related state, such as the
 426  * gamma lookup table textures.
 427  */
 428 static jboolean
 429 OGLTR_EnableLCDGlyphModeState(GLuint glyphTextureID, jint contrast)
 430 {
 431     // bind the texture containing glyph data to texture unit 0
 432     j2d_glActiveTextureARB(GL_TEXTURE0_ARB);
 433     j2d_glBindTexture(GL_TEXTURE_2D, glyphTextureID);
 434 
 435     // bind the texture tile containing destination data to texture unit 1
 436     j2d_glActiveTextureARB(GL_TEXTURE1_ARB);
 437     if (cachedDestTextureID == 0) {
 438         cachedDestTextureID =
 439             OGLContext_CreateBlitTexture(GL_RGB8, GL_RGB,
 440                                          OGLTR_CACHED_DEST_WIDTH,
 441                                          OGLTR_CACHED_DEST_HEIGHT);
 442         if (cachedDestTextureID == 0) {
 443             return JNI_FALSE;
 444         }
 445     }
 446     j2d_glBindTexture(GL_TEXTURE_2D, cachedDestTextureID);
 447 
 448     // note that GL_TEXTURE_2D was already enabled for texture unit 0,
 449     // but we need to explicitly enable it for texture unit 1
 450     j2d_glEnable(GL_TEXTURE_2D);
 451 
 452     // create the LCD text shader, if necessary
 453     if (lcdTextProgram == 0) {
 454         lcdTextProgram = OGLTR_CreateLCDTextProgram();
 455         if (lcdTextProgram == 0) {
 456             return JNI_FALSE;
 457         }
 458     }
 459 
 460     // enable the LCD text shader
 461     j2d_glUseProgramObjectARB(lcdTextProgram);
 462 
 463     // update the current contrast settings, if necessary
 464     if (lastLCDContrast != contrast) {
 465         if (!OGLTR_UpdateLCDTextContrast(contrast)) {
 466             return JNI_FALSE;
 467         }
 468         lastLCDContrast = contrast;
 469     }
 470 
 471     // update the current color settings
 472     if (!OGLTR_UpdateLCDTextColor(contrast)) {
 473         return JNI_FALSE;
 474     }
 475 
 476     // bind the gamma LUT textures
 477     j2d_glActiveTextureARB(GL_TEXTURE2_ARB);
 478     j2d_glBindTexture(GL_TEXTURE_3D, invGammaLutTextureID);
 479     j2d_glEnable(GL_TEXTURE_3D);
 480     j2d_glActiveTextureARB(GL_TEXTURE3_ARB);
 481     j2d_glBindTexture(GL_TEXTURE_3D, gammaLutTextureID);
 482     j2d_glEnable(GL_TEXTURE_3D);
 483 
 484     return JNI_TRUE;
 485 }
 486 
 487 void
 488 OGLTR_EnableGlyphVertexCache(OGLContext *oglc)
 489 {
 490     J2dTraceLn(J2D_TRACE_INFO, "OGLTR_EnableGlyphVertexCache");
 491 
 492     if (!OGLVertexCache_InitVertexCache(oglc)) {
 493         return;
 494     }
 495 
 496     if (glyphCache == NULL) {
 497         if (!OGLTR_InitGlyphCache(JNI_FALSE)) {
 498             return;
 499         }
 500     }
 501 
 502     j2d_glEnable(GL_TEXTURE_2D);
 503     j2d_glBindTexture(GL_TEXTURE_2D, glyphCache->cacheID);
 504     j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 505 
 506     // for grayscale/monochrome text, the current OpenGL source color
 507     // is modulated with the glyph image as part of the texture
 508     // application stage, so we use GL_MODULATE here
 509     OGLC_UPDATE_TEXTURE_FUNCTION(oglc, GL_MODULATE);
 510 }
 511 
 512 void
 513 OGLTR_DisableGlyphVertexCache(OGLContext *oglc)
 514 {
 515     J2dTraceLn(J2D_TRACE_INFO, "OGLTR_DisableGlyphVertexCache");
 516 
 517     OGLVertexCache_FlushVertexCache();
 518     OGLVertexCache_RestoreColorState(oglc);
 519 
 520     j2d_glDisable(GL_TEXTURE_2D);
 521     j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
 522     j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
 523     j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
 524     j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 525 }
 526 
 527 /**
 528  * Disables any pending state associated with the current "glyph mode".
 529  */
 530 static void
 531 OGLTR_DisableGlyphModeState()
 532 {
 533     switch (glyphMode) {
 534     case MODE_NO_CACHE_LCD:
 535         j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
 536         j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
 537         /* FALLTHROUGH */
 538 
 539     case MODE_USE_CACHE_LCD:
 540         j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 541         j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
 542         j2d_glUseProgramObjectARB(0);
 543         j2d_glActiveTextureARB(GL_TEXTURE3_ARB);
 544         j2d_glDisable(GL_TEXTURE_3D);
 545         j2d_glActiveTextureARB(GL_TEXTURE2_ARB);
 546         j2d_glDisable(GL_TEXTURE_3D);
 547         j2d_glActiveTextureARB(GL_TEXTURE1_ARB);
 548         j2d_glDisable(GL_TEXTURE_2D);
 549         j2d_glActiveTextureARB(GL_TEXTURE0_ARB);
 550         break;
 551 
 552     case MODE_NO_CACHE_GRAY:
 553     case MODE_USE_CACHE_GRAY:
 554     case MODE_NOT_INITED:
 555     default:
 556         break;
 557     }
 558 }
 559 
 560 static jboolean
 561 OGLTR_DrawGrayscaleGlyphViaCache(OGLContext *oglc,
 562                                  GlyphInfo *ginfo, jint x, jint y)
 563 {
 564     CacheCellInfo *cell;
 565     jfloat x1, y1, x2, y2;
 566 
 567     if (glyphMode != MODE_USE_CACHE_GRAY) {
 568         OGLTR_DisableGlyphModeState();
 569         CHECK_PREVIOUS_OP(OGL_STATE_GLYPH_OP);
 570         glyphMode = MODE_USE_CACHE_GRAY;
 571     }
 572 
 573     if (ginfo->cellInfo == NULL) {
 574         // attempt to add glyph to accelerated glyph cache
 575         OGLTR_AddToGlyphCache(ginfo, JNI_FALSE);
 576 
 577         if (ginfo->cellInfo == NULL) {
 578             // we'll just no-op in the rare case that the cell is NULL
 579             return JNI_TRUE;
 580         }
 581     }
 582 
 583     cell = (CacheCellInfo *) (ginfo->cellInfo);
 584     cell->timesRendered++;
 585 
 586     x1 = (jfloat)x;
 587     y1 = (jfloat)y;
 588     x2 = x1 + ginfo->width;
 589     y2 = y1 + ginfo->height;
 590 
 591     OGLVertexCache_AddGlyphQuad(oglc,
 592                                 cell->tx1, cell->ty1,
 593                                 cell->tx2, cell->ty2,
 594                                 x1, y1, x2, y2);
 595 
 596     return JNI_TRUE;
 597 }
 598 
 599 /**
 600  * Evaluates to true if the rectangle defined by gx1/gy1/gx2/gy2 is
 601  * inside outerBounds.
 602  */
 603 #define INSIDE(gx1, gy1, gx2, gy2, outerBounds) \
 604     (((gx1) >= outerBounds.x1) && ((gy1) >= outerBounds.y1) && \
 605      ((gx2) <= outerBounds.x2) && ((gy2) <= outerBounds.y2))
 606 
 607 /**
 608  * Evaluates to true if the rectangle defined by gx1/gy1/gx2/gy2 intersects
 609  * the rectangle defined by bounds.
 610  */
 611 #define INTERSECTS(gx1, gy1, gx2, gy2, bounds) \
 612     ((bounds.x2 > (gx1)) && (bounds.y2 > (gy1)) && \
 613      (bounds.x1 < (gx2)) && (bounds.y1 < (gy2)))
 614 
 615 /**
 616  * This method checks to see if the given LCD glyph bounds fall within the
 617  * cached destination texture bounds.  If so, this method can return
 618  * immediately.  If not, this method will copy a chunk of framebuffer data
 619  * into the cached destination texture and then update the current cached
 620  * destination bounds before returning.
 621  */
 622 static void
 623 OGLTR_UpdateCachedDestination(OGLSDOps *dstOps, GlyphInfo *ginfo,
 624                               jint gx1, jint gy1, jint gx2, jint gy2,
 625                               jint glyphIndex, jint totalGlyphs)
 626 {
 627     jint dx1, dy1, dx2, dy2;
 628     jint dx1adj, dy1adj;
 629 
 630     if (isCachedDestValid && INSIDE(gx1, gy1, gx2, gy2, cachedDestBounds)) {
 631         // glyph is already within the cached destination bounds; no need
 632         // to read back the entire destination region again, but we do
 633         // need to see if the current glyph overlaps the previous glyph...
 634 
 635         if (INTERSECTS(gx1, gy1, gx2, gy2, previousGlyphBounds)) {
 636             // the current glyph overlaps the destination region touched
 637             // by the previous glyph, so now we need to read back the part
 638             // of the destination corresponding to the previous glyph
 639             dx1 = previousGlyphBounds.x1;
 640             dy1 = previousGlyphBounds.y1;
 641             dx2 = previousGlyphBounds.x2;
 642             dy2 = previousGlyphBounds.y2;
 643 
 644             // this accounts for lower-left origin of the destination region
 645             dx1adj = dstOps->xOffset + dx1;
 646             dy1adj = dstOps->yOffset + dstOps->height - dy2;
 647 
 648             // copy destination into subregion of cached texture tile:
 649             //   dx1-cachedDestBounds.x1 == +xoffset from left side of texture
 650             //   cachedDestBounds.y2-dy2 == +yoffset from bottom of texture
 651             j2d_glActiveTextureARB(GL_TEXTURE1_ARB);
 652             j2d_glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
 653                                     dx1 - cachedDestBounds.x1,
 654                                     cachedDestBounds.y2 - dy2,
 655                                     dx1adj, dy1adj,
 656                                     dx2-dx1, dy2-dy1);
 657         }
 658     } else {
 659         jint remainingWidth;
 660 
 661         // destination region is not valid, so we need to read back a
 662         // chunk of the destination into our cached texture
 663 
 664         // position the upper-left corner of the destination region on the
 665         // "top" line of glyph list
 666         // REMIND: this isn't ideal; it would be better if we had some idea
 667         //         of the bounding box of the whole glyph list (this is
 668         //         do-able, but would require iterating through the whole
 669         //         list up front, which may present its own problems)
 670         dx1 = gx1;
 671         dy1 = gy1;
 672 
 673         if (ginfo->advanceX > 0) {
 674             // estimate the width based on our current position in the glyph
 675             // list and using the x advance of the current glyph (this is just
 676             // a quick and dirty heuristic; if this is a "thin" glyph image,
 677             // then we're likely to underestimate, and if it's "thick" then we
 678             // may end up reading back more than we need to)
 679             remainingWidth =
 680                 (jint)(ginfo->advanceX * (totalGlyphs - glyphIndex));
 681             if (remainingWidth > OGLTR_CACHED_DEST_WIDTH) {
 682                 remainingWidth = OGLTR_CACHED_DEST_WIDTH;
 683             } else if (remainingWidth < ginfo->width) {
 684                 // in some cases, the x-advance may be slightly smaller
 685                 // than the actual width of the glyph; if so, adjust our
 686                 // estimate so that we can accommodate the entire glyph
 687                 remainingWidth = ginfo->width;
 688             }
 689         } else {
 690             // a negative advance is possible when rendering rotated text,
 691             // in which case it is difficult to estimate an appropriate
 692             // region for readback, so we will pick a region that
 693             // encompasses just the current glyph
 694             remainingWidth = ginfo->width;
 695         }
 696         dx2 = dx1 + remainingWidth;
 697 
 698         // estimate the height (this is another sloppy heuristic; we'll
 699         // make the cached destination region tall enough to encompass most
 700         // glyphs that are small enough to fit in the glyph cache, and then
 701         // we add a little something extra to account for descenders
 702         dy2 = dy1 + OGLTR_CACHE_CELL_HEIGHT + 2;
 703 
 704         // this accounts for lower-left origin of the destination region
 705         dx1adj = dstOps->xOffset + dx1;
 706         dy1adj = dstOps->yOffset + dstOps->height - dy2;
 707 
 708         // copy destination into cached texture tile (the lower-left corner
 709         // of the destination region will be positioned at the lower-left
 710         // corner (0,0) of the texture)
 711         j2d_glActiveTextureARB(GL_TEXTURE1_ARB);
 712         j2d_glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
 713                                 0, 0, dx1adj, dy1adj,
 714                                 dx2-dx1, dy2-dy1);
 715 
 716         // update the cached bounds and mark it valid
 717         cachedDestBounds.x1 = dx1;
 718         cachedDestBounds.y1 = dy1;
 719         cachedDestBounds.x2 = dx2;
 720         cachedDestBounds.y2 = dy2;
 721         isCachedDestValid = JNI_TRUE;
 722     }
 723 
 724     // always update the previous glyph bounds
 725     previousGlyphBounds.x1 = gx1;
 726     previousGlyphBounds.y1 = gy1;
 727     previousGlyphBounds.x2 = gx2;
 728     previousGlyphBounds.y2 = gy2;
 729 }
 730 
 731 static jboolean
 732 OGLTR_DrawLCDGlyphViaCache(OGLContext *oglc, OGLSDOps *dstOps,
 733                            GlyphInfo *ginfo, jint x, jint y,
 734                            jint glyphIndex, jint totalGlyphs,
 735                            jboolean rgbOrder, jint contrast)
 736 {
 737     CacheCellInfo *cell;
 738     jint dx1, dy1, dx2, dy2;
 739     jfloat dtx1, dty1, dtx2, dty2;
 740 
 741     if (glyphMode != MODE_USE_CACHE_LCD) {
 742         OGLTR_DisableGlyphModeState();
 743         CHECK_PREVIOUS_OP(GL_TEXTURE_2D);
 744         j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 745 
 746         if (glyphCache == NULL) {
 747             if (!OGLTR_InitGlyphCache(JNI_TRUE)) {
 748                 return JNI_FALSE;
 749             }
 750         }
 751 
 752         if (rgbOrder != lastRGBOrder) {
 753             // need to invalidate the cache in this case; see comments
 754             // for lastRGBOrder above
 755             AccelGlyphCache_Invalidate(glyphCache);
 756             lastRGBOrder = rgbOrder;
 757         }
 758 
 759         if (!OGLTR_EnableLCDGlyphModeState(glyphCache->cacheID, contrast)) {
 760             return JNI_FALSE;
 761         }
 762 
 763         // when a fragment shader is enabled, the texture function state is
 764         // ignored, so the following line is not needed...
 765         // OGLC_UPDATE_TEXTURE_FUNCTION(oglc, GL_MODULATE);
 766 
 767         glyphMode = MODE_USE_CACHE_LCD;
 768     }
 769 
 770     if (ginfo->cellInfo == NULL) {
 771         // rowBytes will always be a multiple of 3, so the following is safe
 772         j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, ginfo->rowBytes / 3);
 773 
 774         // make sure the glyph cache texture is bound to texture unit 0
 775         j2d_glActiveTextureARB(GL_TEXTURE0_ARB);
 776 
 777         // attempt to add glyph to accelerated glyph cache
 778         OGLTR_AddToGlyphCache(ginfo, rgbOrder);
 779 
 780         if (ginfo->cellInfo == NULL) {
 781             // we'll just no-op in the rare case that the cell is NULL
 782             return JNI_TRUE;
 783         }
 784     }
 785 
 786     cell = (CacheCellInfo *) (ginfo->cellInfo);
 787     cell->timesRendered++;
 788 
 789     // location of the glyph in the destination's coordinate space
 790     dx1 = x;
 791     dy1 = y;
 792     dx2 = dx1 + ginfo->width;
 793     dy2 = dy1 + ginfo->height;
 794 
 795     // copy destination into second cached texture, if necessary
 796     OGLTR_UpdateCachedDestination(dstOps, ginfo,
 797                                   dx1, dy1, dx2, dy2,
 798                                   glyphIndex, totalGlyphs);
 799 
 800     // texture coordinates of the destination tile
 801     dtx1 = ((jfloat)(dx1 - cachedDestBounds.x1)) / OGLTR_CACHED_DEST_WIDTH;
 802     dty1 = ((jfloat)(cachedDestBounds.y2 - dy1)) / OGLTR_CACHED_DEST_HEIGHT;
 803     dtx2 = ((jfloat)(dx2 - cachedDestBounds.x1)) / OGLTR_CACHED_DEST_WIDTH;
 804     dty2 = ((jfloat)(cachedDestBounds.y2 - dy2)) / OGLTR_CACHED_DEST_HEIGHT;
 805 
 806     // render composed texture to the destination surface
 807     j2d_glBegin(GL_QUADS);
 808     j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, cell->tx1, cell->ty1);
 809     j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx1, dty1);
 810     j2d_glVertex2i(dx1, dy1);
 811     j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, cell->tx2, cell->ty1);
 812     j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx2, dty1);
 813     j2d_glVertex2i(dx2, dy1);
 814     j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, cell->tx2, cell->ty2);
 815     j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx2, dty2);
 816     j2d_glVertex2i(dx2, dy2);
 817     j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, cell->tx1, cell->ty2);
 818     j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx1, dty2);
 819     j2d_glVertex2i(dx1, dy2);
 820     j2d_glEnd();
 821 
 822     return JNI_TRUE;
 823 }
 824 
 825 static jboolean
 826 OGLTR_DrawGrayscaleGlyphNoCache(OGLContext *oglc,
 827                                 GlyphInfo *ginfo, jint x, jint y)
 828 {
 829     jint tw, th;
 830     jint sx, sy, sw, sh;
 831     jint x0;
 832     jint w = ginfo->width;
 833     jint h = ginfo->height;
 834 
 835     if (glyphMode != MODE_NO_CACHE_GRAY) {
 836         OGLTR_DisableGlyphModeState();
 837         CHECK_PREVIOUS_OP(OGL_STATE_MASK_OP);
 838         glyphMode = MODE_NO_CACHE_GRAY;
 839     }
 840 
 841     x0 = x;
 842     tw = OGLVC_MASK_CACHE_TILE_WIDTH;
 843     th = OGLVC_MASK_CACHE_TILE_HEIGHT;
 844 
 845     for (sy = 0; sy < h; sy += th, y += th) {
 846         x = x0;
 847         sh = ((sy + th) > h) ? (h - sy) : th;
 848 
 849         for (sx = 0; sx < w; sx += tw, x += tw) {
 850             sw = ((sx + tw) > w) ? (w - sx) : tw;
 851 
 852             OGLVertexCache_AddMaskQuad(oglc,
 853                                        sx, sy, x, y, sw, sh,
 854                                        w, ginfo->image);
 855         }
 856     }
 857 
 858     return JNI_TRUE;
 859 }
 860 
 861 static jboolean
 862 OGLTR_DrawLCDGlyphNoCache(OGLContext *oglc, OGLSDOps *dstOps,
 863                           GlyphInfo *ginfo, jint x, jint y,
 864                           jint rowBytesOffset,
 865                           jboolean rgbOrder, jint contrast)
 866 {
 867     GLfloat tx1, ty1, tx2, ty2;
 868     GLfloat dtx1, dty1, dtx2, dty2;
 869     jint tw, th;
 870     jint sx, sy, sw, sh, dxadj, dyadj;
 871     jint x0;
 872     jint w = ginfo->width;
 873     jint h = ginfo->height;
 874     GLenum pixelFormat = rgbOrder ? GL_RGB : GL_BGR;
 875 
 876     if (glyphMode != MODE_NO_CACHE_LCD) {
 877         OGLTR_DisableGlyphModeState();
 878         CHECK_PREVIOUS_OP(GL_TEXTURE_2D);
 879         j2d_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 880 
 881         if (oglc->blitTextureID == 0) {
 882             if (!OGLContext_InitBlitTileTexture(oglc)) {
 883                 return JNI_FALSE;
 884             }
 885         }
 886 
 887         if (!OGLTR_EnableLCDGlyphModeState(oglc->blitTextureID, contrast)) {
 888             return JNI_FALSE;
 889         }
 890 
 891         // when a fragment shader is enabled, the texture function state is
 892         // ignored, so the following line is not needed...
 893         // OGLC_UPDATE_TEXTURE_FUNCTION(oglc, GL_MODULATE);
 894 
 895         glyphMode = MODE_NO_CACHE_LCD;
 896     }
 897 
 898     // rowBytes will always be a multiple of 3, so the following is safe
 899     j2d_glPixelStorei(GL_UNPACK_ROW_LENGTH, ginfo->rowBytes / 3);
 900 
 901     x0 = x;
 902     tx1 = 0.0f;
 903     ty1 = 0.0f;
 904     dtx1 = 0.0f;
 905     dty2 = 0.0f;
 906     tw = OGLTR_NOCACHE_TILE_SIZE;
 907     th = OGLTR_NOCACHE_TILE_SIZE;
 908 
 909     for (sy = 0; sy < h; sy += th, y += th) {
 910         x = x0;
 911         sh = ((sy + th) > h) ? (h - sy) : th;
 912 
 913         for (sx = 0; sx < w; sx += tw, x += tw) {
 914             sw = ((sx + tw) > w) ? (w - sx) : tw;
 915 
 916             // update the source pointer offsets
 917             j2d_glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
 918             j2d_glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
 919 
 920             // copy LCD mask into glyph texture tile
 921             j2d_glActiveTextureARB(GL_TEXTURE0_ARB);
 922             j2d_glTexSubImage2D(GL_TEXTURE_2D, 0,
 923                                 0, 0, sw, sh,
 924                                 pixelFormat, GL_UNSIGNED_BYTE,
 925                                 ginfo->image + rowBytesOffset);
 926 
 927             // update the lower-right glyph texture coordinates
 928             tx2 = ((GLfloat)sw) / OGLC_BLIT_TILE_SIZE;
 929             ty2 = ((GLfloat)sh) / OGLC_BLIT_TILE_SIZE;
 930 
 931             // this accounts for lower-left origin of the destination region
 932             dxadj = dstOps->xOffset + x;
 933             dyadj = dstOps->yOffset + dstOps->height - (y + sh);
 934 
 935             // copy destination into cached texture tile (the lower-left
 936             // corner of the destination region will be positioned at the
 937             // lower-left corner (0,0) of the texture)
 938             j2d_glActiveTextureARB(GL_TEXTURE1_ARB);
 939             j2d_glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
 940                                     0, 0,
 941                                     dxadj, dyadj,
 942                                     sw, sh);
 943 
 944             // update the remaining destination texture coordinates
 945             dtx2 = ((GLfloat)sw) / OGLTR_CACHED_DEST_WIDTH;
 946             dty1 = ((GLfloat)sh) / OGLTR_CACHED_DEST_HEIGHT;
 947 
 948             // render composed texture to the destination surface
 949             j2d_glBegin(GL_QUADS);
 950             j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, tx1, ty1);
 951             j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx1, dty1);
 952             j2d_glVertex2i(x, y);
 953             j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, tx2, ty1);
 954             j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx2, dty1);
 955             j2d_glVertex2i(x + sw, y);
 956             j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, tx2, ty2);
 957             j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx2, dty2);
 958             j2d_glVertex2i(x + sw, y + sh);
 959             j2d_glMultiTexCoord2fARB(GL_TEXTURE0_ARB, tx1, ty2);
 960             j2d_glMultiTexCoord2fARB(GL_TEXTURE1_ARB, dtx1, dty2);
 961             j2d_glVertex2i(x, y + sh);
 962             j2d_glEnd();
 963         }
 964     }
 965 
 966     return JNI_TRUE;
 967 }
 968 
 969 // see DrawGlyphList.c for more on this macro...
 970 #define FLOOR_ASSIGN(l, r) \
 971     if ((r)<0) (l) = ((int)floor(r)); else (l) = ((int)(r))
 972 
 973 void
 974 OGLTR_DrawGlyphList(JNIEnv *env, OGLContext *oglc, OGLSDOps *dstOps,
 975                     jint totalGlyphs, jboolean usePositions,
 976                     jboolean subPixPos, jboolean rgbOrder, jint lcdContrast,
 977                     jfloat glyphListOrigX, jfloat glyphListOrigY,
 978                     unsigned char *images, unsigned char *positions)
 979 {
 980     int glyphCounter;
 981 
 982     J2dTraceLn(J2D_TRACE_INFO, "OGLTR_DrawGlyphList");
 983 
 984     RETURN_IF_NULL(oglc);
 985     RETURN_IF_NULL(dstOps);
 986     RETURN_IF_NULL(images);
 987     if (usePositions) {
 988         RETURN_IF_NULL(positions);
 989     }
 990 
 991     glyphMode = MODE_NOT_INITED;
 992     isCachedDestValid = JNI_FALSE;
 993 
 994     for (glyphCounter = 0; glyphCounter < totalGlyphs; glyphCounter++) {
 995         jint x, y;
 996         jfloat glyphx, glyphy;
 997         jboolean grayscale, ok;
 998         GlyphInfo *ginfo = (GlyphInfo *)jlong_to_ptr(NEXT_LONG(images));
 999 
1000         if (ginfo == NULL) {
1001             // this shouldn't happen, but if it does we'll just break out...
1002             J2dRlsTraceLn(J2D_TRACE_ERROR,
1003                           "OGLTR_DrawGlyphList: glyph info is null");
1004             break;
1005         }
1006 
1007         grayscale = (ginfo->rowBytes == ginfo->width);
1008 
1009         if (usePositions) {
1010             jfloat posx = NEXT_FLOAT(positions);
1011             jfloat posy = NEXT_FLOAT(positions);
1012             glyphx = glyphListOrigX + posx + ginfo->topLeftX;
1013             glyphy = glyphListOrigY + posy + ginfo->topLeftY;
1014             FLOOR_ASSIGN(x, glyphx);
1015             FLOOR_ASSIGN(y, glyphy);
1016         } else {
1017             glyphx = glyphListOrigX + ginfo->topLeftX;
1018             glyphy = glyphListOrigY + ginfo->topLeftY;
1019             FLOOR_ASSIGN(x, glyphx);
1020             FLOOR_ASSIGN(y, glyphy);
1021             glyphListOrigX += ginfo->advanceX;
1022             glyphListOrigY += ginfo->advanceY;
1023         }
1024 
1025         if (ginfo->image == NULL) {
1026             continue;
1027         }
1028 
1029         if (grayscale) {
1030             // grayscale or monochrome glyph data
1031             if (cacheStatus != CACHE_LCD &&
1032                 ginfo->width <= OGLTR_CACHE_CELL_WIDTH &&
1033                 ginfo->height <= OGLTR_CACHE_CELL_HEIGHT)
1034             {
1035                 ok = OGLTR_DrawGrayscaleGlyphViaCache(oglc, ginfo, x, y);
1036             } else {
1037                 ok = OGLTR_DrawGrayscaleGlyphNoCache(oglc, ginfo, x, y);
1038             }
1039         } else {
1040             // LCD-optimized glyph data
1041             jint rowBytesOffset = 0;
1042 
1043             if (subPixPos) {
1044                 jint frac = (jint)((glyphx - x) * 3);
1045                 if (frac != 0) {
1046                     rowBytesOffset = 3 - frac;
1047                     x += 1;
1048                 }
1049             }
1050 
1051             if (rowBytesOffset == 0 &&
1052                 cacheStatus != CACHE_GRAY &&
1053                 ginfo->width <= OGLTR_CACHE_CELL_WIDTH &&
1054                 ginfo->height <= OGLTR_CACHE_CELL_HEIGHT)
1055             {
1056                 ok = OGLTR_DrawLCDGlyphViaCache(oglc, dstOps,
1057                                                 ginfo, x, y,
1058                                                 glyphCounter, totalGlyphs,
1059                                                 rgbOrder, lcdContrast);
1060             } else {
1061                 ok = OGLTR_DrawLCDGlyphNoCache(oglc, dstOps,
1062                                                ginfo, x, y,
1063                                                rowBytesOffset,
1064                                                rgbOrder, lcdContrast);
1065             }
1066         }
1067 
1068         if (!ok) {
1069             break;
1070         }
1071     }
1072 
1073     OGLTR_DisableGlyphModeState();
1074 }
1075 
1076 JNIEXPORT void JNICALL
1077 Java_sun_java2d_opengl_OGLTextRenderer_drawGlyphList
1078     (JNIEnv *env, jobject self,
1079      jint numGlyphs, jboolean usePositions,
1080      jboolean subPixPos, jboolean rgbOrder, jint lcdContrast,
1081      jfloat glyphListOrigX, jfloat glyphListOrigY,
1082      jlongArray imgArray, jfloatArray posArray)
1083 {
1084     unsigned char *images;
1085 
1086     J2dTraceLn(J2D_TRACE_INFO, "OGLTextRenderer_drawGlyphList");
1087 
1088     images = (unsigned char *)
1089         (*env)->GetPrimitiveArrayCritical(env, imgArray, NULL);
1090     if (images != NULL) {
1091         OGLContext *oglc = OGLRenderQueue_GetCurrentContext();
1092         OGLSDOps *dstOps = OGLRenderQueue_GetCurrentDestination();
1093 
1094         if (usePositions) {
1095             unsigned char *positions = (unsigned char *)
1096                 (*env)->GetPrimitiveArrayCritical(env, posArray, NULL);
1097             if (positions != NULL) {
1098                 OGLTR_DrawGlyphList(env, oglc, dstOps,
1099                                     numGlyphs, usePositions,
1100                                     subPixPos, rgbOrder, lcdContrast,
1101                                     glyphListOrigX, glyphListOrigY,
1102                                     images, positions);
1103                 (*env)->ReleasePrimitiveArrayCritical(env, posArray,
1104                                                       positions, JNI_ABORT);
1105             }
1106         } else {
1107             OGLTR_DrawGlyphList(env, oglc, dstOps,
1108                                 numGlyphs, usePositions,
1109                                 subPixPos, rgbOrder, lcdContrast,
1110                                 glyphListOrigX, glyphListOrigY,
1111                                 images, NULL);
1112         }
1113 
1114         // 6358147: reset current state, and ensure rendering is
1115         // flushed to dest
1116         if (oglc != NULL) {
1117             RESET_PREVIOUS_OP();
1118             j2d_glFlush();
1119         }
1120 
1121         (*env)->ReleasePrimitiveArrayCritical(env, imgArray,
1122                                               images, JNI_ABORT);
1123     }
1124 }
1125 
1126 #endif /* !HEADLESS */