1 /*
   2  * Copyright (c) 2019, 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 "MTLBufImgOps.h"
  31 #include "MTLContext.h"
  32 #include "MTLRenderQueue.h"
  33 #include "MTLSurfaceDataBase.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 MTLBufImgOps_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 MTLBufImgOps_CreateConvolveProgram(jint flags)
 130 {
 131     //TODO
 132     J2dTraceNotImplPrimitive("MTLBufImgOps_CreateConvolveProgram");
 133     return NULL;
 134 }
 135 
 136 void
 137 MTLBufImgOps_EnableConvolveOp(MTLContext *mtlc, jlong pSrcOps,
 138                               jboolean edgeZeroFill,
 139                               jint kernelWidth, jint kernelHeight,
 140                               unsigned char *kernel)
 141 {
 142     //TODO
 143     J2dTraceNotImplPrimitive("MTLBufImgOps_EnableConvolveOp");
 144 }
 145 
 146 void
 147 MTLBufImgOps_DisableConvolveOp(MTLContext *mtlc)
 148 {
 149     //TODO
 150     J2dTraceNotImplPrimitive("MTLBufImgOps_EnableConvolveOp");
 151     J2dTraceLn(J2D_TRACE_INFO, "MTLBufImgOps_DisableConvolveOp");
 152 }
 153 
 154 /**************************** RescaleOp support *****************************/
 155 
 156 /**
 157  * The RescaleOp shader is one of the simplest possible.  Each fragment
 158  * from the source image is multiplied by the user's scale factor and added
 159  * to the user's offset value (these are component-wise operations).
 160  * Finally, the resulting value is multiplied by the current OpenGL color,
 161  * which contains the extra alpha value.
 162  *
 163  * The RescaleOp spec says that the operation is performed regardless of
 164  * whether the source data is premultiplied or non-premultiplied.  This is
 165  * a problem for the OpenGL pipeline in that a non-premultiplied
 166  * BufferedImage will have already been converted into premultiplied
 167  * when uploaded to an OpenGL texture.  Therefore, we have a special mode
 168  * called RESCALE_NON_PREMULT (used only for source images that were
 169  * originally non-premultiplied) that un-premultiplies the source color
 170  * prior to the rescale operation, then re-premultiplies the resulting
 171  * color before returning from the fragment shader.
 172  *
 173  * Note that this shader source code includes some "holes" marked by "%s".
 174  * This allows us to build different shader programs (e.g. one for
 175  * GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on)
 176  * simply by filling in these "holes" with a call to sprintf().  See the
 177  * MTLBufImgOps_CreateRescaleProgram() method for more details.
 178  */
 179 static const char *rescaleShaderSource =
 180     // image to be rescaled
 181     "uniform sampler%s baseImage;"
 182     // vector containing scale factors
 183     "uniform vec4 scaleFactors;"
 184     // vector containing offsets
 185     "uniform vec4 offsets;"
 186     ""
 187     "void main(void)"
 188     "{"
 189     "    vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);"
 190          // (placeholder for un-premult code)
 191     "    %s"
 192          // rescale source value
 193     "    vec4 result = (srcColor * scaleFactors) + offsets;"
 194          // (placeholder for re-premult code)
 195     "    %s"
 196          // modulate with gl_Color in order to apply extra alpha
 197     "    gl_FragColor = result * gl_Color;"
 198     "}";
 199 
 200 /**
 201  * Flags that can be bitwise-or'ed together to control how the shader
 202  * source code is generated.
 203  */
 204 #define RESCALE_RECT        (1 << 0)
 205 #define RESCALE_NON_PREMULT (1 << 1)
 206 
 207 /**
 208  * The handles to the RescaleOp fragment program objects.  The index to
 209  * the array should be a bitwise-or'ing of the RESCALE_* flags defined
 210  * above.  Note that most applications will likely need to initialize one
 211  * or two of these elements, so the array is usually sparsely populated.
 212  */
 213 static GLhandleARB rescalePrograms[4];
 214 
 215 /**
 216  * Compiles and links the RescaleOp shader program.  If successful, this
 217  * function returns a handle to the newly created shader program; otherwise
 218  * returns 0.
 219  */
 220 static GLhandleARB
 221 MTLBufImgOps_CreateRescaleProgram(jint flags)
 222 {
 223     //TODO
 224     J2dTraceNotImplPrimitive("MTLBufImgOps_CreateRescaleProgram");
 225     return NULL;
 226 }
 227 
 228 void
 229 MTLBufImgOps_EnableRescaleOp(MTLContext *mtlc, jlong pSrcOps,
 230                              jboolean nonPremult,
 231                              unsigned char *scaleFactors,
 232                              unsigned char *offsets)
 233 {
 234     //TODO
 235     J2dTraceNotImplPrimitive("MTLBufImgOps_EnableRescaleOp");
 236 }
 237 
 238 void
 239 MTLBufImgOps_DisableRescaleOp(MTLContext *mtlc)
 240 {
 241     //TODO
 242     J2dTraceNotImplPrimitive("MTLBufImgOps_DisableRescaleOp");
 243     J2dTraceLn(J2D_TRACE_INFO, "MTLBufImgOps_DisableRescaleOp");
 244     RETURN_IF_NULL(mtlc);
 245 }
 246 
 247 /**************************** LookupOp support ******************************/
 248 
 249 /**
 250  * The LookupOp shader takes a fragment color (from the source texture) as
 251  * input, subtracts the optional user offset value, and then uses the
 252  * resulting value to index into the lookup table texture to provide
 253  * a new color result.  Finally, the resulting value is multiplied by
 254  * the current OpenGL color, which contains the extra alpha value.
 255  *
 256  * The lookup step requires 3 texture accesses (or 4, when alpha is included),
 257  * which is somewhat unfortunate because it's not ideal from a performance
 258  * standpoint, but that sort of thing is getting faster with newer hardware.
 259  * In the 3-band case, we could consider using a three-dimensional texture
 260  * and performing the lookup with a single texture access step.  We already
 261  * use this approach in the LCD text shader, and it works well, but for the
 262  * purposes of this LookupOp shader, it's probably overkill.  Also, there's
 263  * a difference in that the LCD text shader only needs to populate the 3D LUT
 264  * once, but here we would need to populate it on every invocation, which
 265  * would likely be a waste of VRAM and CPU/GPU cycles.
 266  *
 267  * The LUT texture is currently hardcoded as 4 rows/bands, each containing
 268  * 256 elements.  This means that we currently only support user-provided
 269  * tables with no more than 256 elements in each band (this is checked at
 270  * at the Java level).  If the user provides a table with less than 256
 271  * elements per band, our shader will still work fine, but if elements are
 272  * accessed with an index >= the size of the LUT, then the shader will simply
 273  * produce undefined values.  Typically the user would provide an offset
 274  * value that would prevent this from happening, but it's worth pointing out
 275  * this fact because the software LookupOp implementation would usually
 276  * throw an ArrayIndexOutOfBoundsException in this scenario (although it is
 277  * not something demanded by the spec).
 278  *
 279  * The LookupOp spec says that the operation is performed regardless of
 280  * whether the source data is premultiplied or non-premultiplied.  This is
 281  * a problem for the OpenGL pipeline in that a non-premultiplied
 282  * BufferedImage will have already been converted into premultiplied
 283  * when uploaded to an OpenGL texture.  Therefore, we have a special mode
 284  * called LOOKUP_NON_PREMULT (used only for source images that were
 285  * originally non-premultiplied) that un-premultiplies the source color
 286  * prior to the lookup operation, then re-premultiplies the resulting
 287  * color before returning from the fragment shader.
 288  *
 289  * Note that this shader source code includes some "holes" marked by "%s".
 290  * This allows us to build different shader programs (e.g. one for
 291  * GL_TEXTURE_2D targets, one for GL_TEXTURE_RECTANGLE_ARB targets, and so on)
 292  * simply by filling in these "holes" with a call to sprintf().  See the
 293  * MTLBufImgOps_CreateLookupProgram() method for more details.
 294  */
 295 static const char *lookupShaderSource =
 296     // source image (bound to texture unit 0)
 297     "uniform sampler%s baseImage;"
 298     // lookup table (bound to texture unit 1)
 299     "uniform sampler2D lookupTable;"
 300     // offset subtracted from source index prior to lookup step
 301     "uniform vec4 offset;"
 302     ""
 303     "void main(void)"
 304     "{"
 305     "    vec4 srcColor = texture%s(baseImage, gl_TexCoord[0].st);"
 306          // (placeholder for un-premult code)
 307     "    %s"
 308          // subtract offset from original index
 309     "    vec4 srcIndex = srcColor - offset;"
 310          // use source value as input to lookup table (note that
 311          // "v" texcoords are hardcoded to hit texel centers of
 312          // each row/band in texture)
 313     "    vec4 result;"
 314     "    result.r = texture2D(lookupTable, vec2(srcIndex.r, 0.125)).r;"
 315     "    result.g = texture2D(lookupTable, vec2(srcIndex.g, 0.375)).r;"
 316     "    result.b = texture2D(lookupTable, vec2(srcIndex.b, 0.625)).r;"
 317          // (placeholder for alpha store code)
 318     "    %s"
 319          // (placeholder for re-premult code)
 320     "    %s"
 321          // modulate with gl_Color in order to apply extra alpha
 322     "    gl_FragColor = result * gl_Color;"
 323     "}";
 324 
 325 /**
 326  * Flags that can be bitwise-or'ed together to control how the shader
 327  * source code is generated.
 328  */
 329 #define LOOKUP_RECT          (1 << 0)
 330 #define LOOKUP_USE_SRC_ALPHA (1 << 1)
 331 #define LOOKUP_NON_PREMULT   (1 << 2)
 332 
 333 /**
 334  * The handles to the LookupOp fragment program objects.  The index to
 335  * the array should be a bitwise-or'ing of the LOOKUP_* flags defined
 336  * above.  Note that most applications will likely need to initialize one
 337  * or two of these elements, so the array is usually sparsely populated.
 338  */
 339 static GLhandleARB lookupPrograms[8];
 340 
 341 /**
 342  * The handle to the lookup table texture object used by the shader.
 343  */
 344 static GLuint lutTextureID = 0;
 345 
 346 /**
 347  * Compiles and links the LookupOp shader program.  If successful, this
 348  * function returns a handle to the newly created shader program; otherwise
 349  * returns 0.
 350  */
 351 static GLhandleARB
 352 MTLBufImgOps_CreateLookupProgram(jint flags)
 353 {
 354     //TODO
 355     J2dTraceNotImplPrimitive("MTLBufImgOps_CreateLookupProgram");
 356 
 357     return NULL;
 358 }
 359 
 360 void
 361 MTLBufImgOps_EnableLookupOp(MTLContext *mtlc, jlong pSrcOps,
 362                             jboolean nonPremult, jboolean shortData,
 363                             jint numBands, jint bandLength, jint offset,
 364                             void *tableValues)
 365 {
 366     //TODO
 367     J2dTraceNotImplPrimitive("MTLBufImgOps_EnableLookupOp");
 368 }
 369 
 370 void
 371 MTLBufImgOps_DisableLookupOp(MTLContext *mtlc)
 372 {
 373     //TODO
 374     J2dTraceNotImplPrimitive("MTLBufImgOps_DisableLookupOp");
 375     J2dTraceLn(J2D_TRACE_INFO, "MTLBufImgOps_DisableLookupOp");
 376 }
 377 
 378 #endif /* !HEADLESS */