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 #include <string.h>
  30 
  31 #include "sun_java2d_SunGraphics2D.h"
  32 #include "sun_java2d_pipe_BufferedPaints.h"
  33 
  34 #include "MTLPaints.h"
  35 #include "MTLContext.h"
  36 #include "MTLRenderQueue.h"
  37 #include "MTLSurfaceData.h"
  38 
  39 void
  40 MTLPaints_ResetPaint(MTLContext *mtlc)
  41 {
  42     //TODO
  43     J2dTraceNotImplPrimitive("MTLPaints_ResetPaint");
  44     J2dTraceLn(J2D_TRACE_INFO, "MTLPaints_ResetPaint");
  45 }
  46 
  47 void
  48 MTLPaints_SetColor(MTLContext *mtlc, jint pixel)
  49 {
  50     //TODO
  51     J2dTraceNotImplPrimitive("MTLPaints_SetColor");
  52     J2dTraceLn1(J2D_TRACE_INFO, "MTLPaints_SetColor: pixel=%08x", pixel);
  53 }
  54 
  55 /************************* GradientPaint support ****************************/
  56 
  57 static GLuint gradientTexID = 0;
  58 
  59 static void
  60 MTLPaints_InitGradientTexture()
  61 {
  62     //TODO
  63     J2dTraceNotImplPrimitive("MTLPaints_InitGradientTexture");
  64     J2dTraceLn(J2D_TRACE_INFO, "MTLPaints_InitGradientTexture");
  65 }
  66 
  67 void
  68 MTLPaints_SetGradientPaint(MTLContext *mtlc,
  69                            jboolean useMask, jboolean cyclic,
  70                            jdouble p0, jdouble p1, jdouble p3,
  71                            jint pixel1, jint pixel2)
  72 {
  73     //TODO
  74     J2dTraceNotImplPrimitive("MTLPaints_SetGradientPaint");
  75     J2dTraceLn(J2D_TRACE_INFO, "MTLPaints_SetGradientPaint");
  76 }
  77 
  78 /************************** TexturePaint support ****************************/
  79 
  80 void
  81 MTLPaints_SetTexturePaint(MTLContext *mtlc,
  82                           jboolean useMask,
  83                           jlong pSrcOps, jboolean filter,
  84                           jdouble xp0, jdouble xp1, jdouble xp3,
  85                           jdouble yp0, jdouble yp1, jdouble yp3)
  86 {
  87     //TODO
  88     J2dTraceNotImplPrimitive("MTLPaints_SetTexturePaint");
  89 }
  90 
  91 /****************** Shared MultipleGradientPaint support ********************/
  92 
  93 /**
  94  * These constants are identical to those defined in the
  95  * MultipleGradientPaint.CycleMethod enum; they are copied here for
  96  * convenience (ideally we would pull them directly from the Java level,
  97  * but that entails more hassle than it is worth).
  98  */
  99 #define CYCLE_NONE    0
 100 #define CYCLE_REFLECT 1
 101 #define CYCLE_REPEAT  2
 102 
 103 /**
 104  * The following constants are flags that can be bitwise-or'ed together
 105  * to control how the MultipleGradientPaint shader source code is generated:
 106  *
 107  *   MULTI_CYCLE_METHOD
 108  *     Placeholder for the CycleMethod enum constant.
 109  *
 110  *   MULTI_LARGE
 111  *     If set, use the (slower) shader that supports a larger number of
 112  *     gradient colors; otherwise, use the optimized codepath.  See
 113  *     the MAX_FRACTIONS_SMALL/LARGE constants below for more details.
 114  *
 115  *   MULTI_USE_MASK
 116  *     If set, apply the alpha mask value from texture unit 0 to the
 117  *     final color result (only used in the MaskFill case).
 118  *
 119  *   MULTI_LINEAR_RGB
 120  *     If set, convert the linear RGB result back into the sRGB color space.
 121  */
 122 #define MULTI_CYCLE_METHOD (3 << 0)
 123 #define MULTI_LARGE        (1 << 2)
 124 #define MULTI_USE_MASK     (1 << 3)
 125 #define MULTI_LINEAR_RGB   (1 << 4)
 126 
 127 /**
 128  * This value determines the size of the array of programs for each
 129  * MultipleGradientPaint type.  This value reflects the maximum value that
 130  * can be represented by performing a bitwise-or of all the MULTI_*
 131  * constants defined above.
 132  */
 133 #define MAX_PROGRAMS 32
 134 
 135 /** Evaluates to true if the given bit is set on the local flags variable. */
 136 #define IS_SET(flagbit) \
 137     (((flags) & (flagbit)) != 0)
 138 
 139 /** Composes the given parameters as flags into the given flags variable.*/
 140 #define COMPOSE_FLAGS(flags, cycleMethod, large, useMask, linear) \
 141     do {                                                   \
 142         flags |= ((cycleMethod) & MULTI_CYCLE_METHOD);     \
 143         if (large)   flags |= MULTI_LARGE;                 \
 144         if (useMask) flags |= MULTI_USE_MASK;              \
 145         if (linear)  flags |= MULTI_LINEAR_RGB;            \
 146     } while (0)
 147 
 148 /** Extracts the CycleMethod enum value from the given flags variable. */
 149 #define EXTRACT_CYCLE_METHOD(flags) \
 150     ((flags) & MULTI_CYCLE_METHOD)
 151 
 152 /**
 153  * The maximum number of gradient "stops" supported by the fragment shader
 154  * and related code.  When the MULTI_LARGE flag is set, we will use
 155  * MAX_FRACTIONS_LARGE; otherwise, we use MAX_FRACTIONS_SMALL.  By having
 156  * two separate values, we can have one highly optimized shader (SMALL) that
 157  * supports only a few fractions/colors, and then another, less optimal
 158  * shader that supports more stops.
 159  */
 160 #define MAX_FRACTIONS sun_java2d_pipe_BufferedPaints_MULTI_MAX_FRACTIONS
 161 #define MAX_FRACTIONS_LARGE MAX_FRACTIONS
 162 #define MAX_FRACTIONS_SMALL 4
 163 
 164 /**
 165  * The maximum number of gradient colors supported by all of the gradient
 166  * fragment shaders.  Note that this value must be a power of two, as it
 167  * determines the size of the 1D texture created below.  It also must be
 168  * greater than or equal to MAX_FRACTIONS (there is no strict requirement
 169  * that the two values be equal).
 170  */
 171 #define MAX_COLORS 16
 172 
 173 /**
 174  * The handle to the gradient color table texture object used by the shaders.
 175  */
 176 static jint multiGradientTexID = 0;
 177 
 178 /**
 179  * This is essentially a template of the shader source code that can be used
 180  * for either LinearGradientPaint or RadialGradientPaint.  It includes the
 181  * structure and some variables that are common to each; the remaining
 182  * code snippets (for CycleMethod, ColorSpaceType, and mask modulation)
 183  * are filled in prior to compiling the shader at runtime depending on the
 184  * paint parameters.  See MTLPaints_CreateMultiGradProgram() for more details.
 185  */
 186 static const char *multiGradientShaderSource =
 187     // gradient texture size (in texels)
 188     "const int TEXTURE_SIZE = %d;"
 189     // maximum number of fractions/colors supported by this shader
 190     "const int MAX_FRACTIONS = %d;"
 191     // size of a single texel
 192     "const float FULL_TEXEL = (1.0 / float(TEXTURE_SIZE));"
 193     // size of half of a single texel
 194     "const float HALF_TEXEL = (FULL_TEXEL / 2.0);"
 195     // texture containing the gradient colors
 196     "uniform sampler1D colors;"
 197     // array of gradient stops/fractions
 198     "uniform float fractions[MAX_FRACTIONS];"
 199     // array of scale factors (one for each interval)
 200     "uniform float scaleFactors[MAX_FRACTIONS-1];"
 201     // (placeholder for mask variable)
 202     "%s"
 203     // (placeholder for Linear/RadialGP-specific variables)
 204     "%s"
 205     ""
 206     "void main(void)"
 207     "{"
 208     "    float dist;"
 209          // (placeholder for Linear/RadialGradientPaint-specific code)
 210     "    %s"
 211     ""
 212     "    float tc;"
 213          // (placeholder for CycleMethod-specific code)
 214     "    %s"
 215     ""
 216          // calculate interpolated color
 217     "    vec4 result = texture1D(colors, tc);"
 218     ""
 219          // (placeholder for ColorSpace conversion code)
 220     "    %s"
 221     ""
 222          // (placeholder for mask modulation code)
 223     "    %s"
 224     ""
 225          // modulate with gl_Color in order to apply extra alpha
 226     "    gl_FragColor = result * gl_Color;"
 227     "}";
 228 
 229 /**
 230  * This code takes a "dist" value as input (as calculated earlier by the
 231  * LGP/RGP-specific code) in the range [0,1] and produces a texture
 232  * coordinate value "tc" that represents the position of the chosen color
 233  * in the one-dimensional gradient texture (also in the range [0,1]).
 234  *
 235  * One naive way to implement this would be to iterate through the fractions
 236  * to figure out in which interval "dist" falls, and then compute the
 237  * relative distance between the two nearest stops.  This approach would
 238  * require an "if" check on every iteration, and it is best to avoid
 239  * conditionals in fragment shaders for performance reasons.  Also, one might
 240  * be tempted to use a break statement to jump out of the loop once the
 241  * interval was found, but break statements (and non-constant loop bounds)
 242  * are not natively available on most graphics hardware today, so that is
 243  * a non-starter.
 244  *
 245  * The more optimal approach used here avoids these issues entirely by using
 246  * an accumulation function that is equivalent to the process described above.
 247  * The scaleFactors array is pre-initialized at enable time as follows:
 248  *     scaleFactors[i] = 1.0 / (fractions[i+1] - fractions[i]);
 249  *
 250  * For each iteration, we subtract fractions[i] from dist and then multiply
 251  * that value by scaleFactors[i].  If we are within the target interval,
 252  * this value will be a fraction in the range [0,1] indicating the relative
 253  * distance between fraction[i] and fraction[i+1].  If we are below the
 254  * target interval, this value will be negative, so we clamp it to zero
 255  * to avoid accumulating any value.  If we are above the target interval,
 256  * the value will be greater than one, so we clamp it to one.  Upon exiting
 257  * the loop, we will have accumulated zero or more 1.0's and a single
 258  * fractional value.  This accumulated value tells us the position of the
 259  * fragment color in the one-dimensional gradient texture, i.e., the
 260  * texcoord called "tc".
 261  */
 262 static const char *texCoordCalcCode =
 263     "int i;"
 264     "float relFraction = 0.0;"
 265     "for (i = 0; i < MAX_FRACTIONS-1; i++) {"
 266     "    relFraction +="
 267     "        clamp((dist - fractions[i]) * scaleFactors[i], 0.0, 1.0);"
 268     "}"
 269     // we offset by half a texel so that we find the linearly interpolated
 270     // color between the two texel centers of interest
 271     "tc = HALF_TEXEL + (FULL_TEXEL * relFraction);";
 272 
 273 /** Code for NO_CYCLE that gets plugged into the CycleMethod placeholder. */
 274 static const char *noCycleCode =
 275     "if (dist <= 0.0) {"
 276     "    tc = 0.0;"
 277     "} else if (dist >= 1.0) {"
 278     "    tc = 1.0;"
 279     "} else {"
 280          // (placeholder for texcoord calculation)
 281     "    %s"
 282     "}";
 283 
 284 /** Code for REFLECT that gets plugged into the CycleMethod placeholder. */
 285 static const char *reflectCode =
 286     "dist = 1.0 - (abs(fract(dist * 0.5) - 0.5) * 2.0);"
 287     // (placeholder for texcoord calculation)
 288     "%s";
 289 
 290 /** Code for REPEAT that gets plugged into the CycleMethod placeholder. */
 291 static const char *repeatCode =
 292     "dist = fract(dist);"
 293     // (placeholder for texcoord calculation)
 294     "%s";
 295 
 296 static void
 297 MTLPaints_InitMultiGradientTexture()
 298 {
 299     //TODO
 300     J2dTraceNotImplPrimitive("MTLPaints_InitMultiGradientTexture");
 301 }
 302 
 303 /**
 304  * Compiles and links the MultipleGradientPaint shader program.  If
 305  * successful, this function returns a handle to the newly created
 306  * shader program; otherwise returns 0.
 307  */
 308 static GLhandleARB
 309 MTLPaints_CreateMultiGradProgram(jint flags,
 310                                  char *paintVars, char *distCode)
 311 {
 312 
 313     //TODO
 314     J2dTraceNotImplPrimitive("MTLPaints_CreateMultiGradProgram");
 315     return NULL;
 316 }
 317 
 318 /**
 319  * Called from the MTLPaints_SetLinear/RadialGradientPaint() methods
 320  * in order to setup the fraction/color values that are common to both.
 321  */
 322 static void
 323 MTLPaints_SetMultiGradientPaint(GLhandleARB multiGradProgram,
 324                                 jint numStops,
 325                                 void *pFractions, void *pPixels)
 326 {
 327     //TODO
 328     J2dTraceNotImplPrimitive("MTLPaints_SetMultiGradientPaint");
 329 
 330 }
 331 
 332 /********************** LinearGradientPaint support *************************/
 333 
 334 /**
 335  * The handles to the LinearGradientPaint fragment program objects.  The
 336  * index to the array should be a bitwise-or'ing of the MULTI_* flags defined
 337  * above.  Note that most applications will likely need to initialize one
 338  * or two of these elements, so the array is usually sparsely populated.
 339  */
 340 static GLhandleARB linearGradPrograms[MAX_PROGRAMS];
 341 
 342 /**
 343  * Compiles and links the LinearGradientPaint shader program.  If successful,
 344  * this function returns a handle to the newly created shader program;
 345  * otherwise returns 0.
 346  */
 347 static GLhandleARB
 348 MTLPaints_CreateLinearGradProgram(jint flags)
 349 {
 350     char *paintVars;
 351     char *distCode;
 352 
 353     J2dTraceLn1(J2D_TRACE_INFO,
 354                 "MTLPaints_CreateLinearGradProgram",
 355                 flags);
 356 
 357     /*
 358      * To simplify the code and to make it easier to upload a number of
 359      * uniform values at once, we pack a bunch of scalar (float) values
 360      * into vec3 values below.  Here's how the values are related:
 361      *
 362      *   params.x = p0
 363      *   params.y = p1
 364      *   params.z = p3
 365      *
 366      *   yoff = dstOps->yOffset + dstOps->height
 367      */
 368     paintVars =
 369         "uniform vec3 params;"
 370         "uniform float yoff;";
 371     distCode =
 372         // note that gl_FragCoord is in window space relative to the
 373         // lower-left corner, so we have to flip the y-coordinate here
 374         "vec3 fragCoord = vec3(gl_FragCoord.x, yoff-gl_FragCoord.y, 1.0);"
 375         "dist = dot(params, fragCoord);";
 376 
 377     return MTLPaints_CreateMultiGradProgram(flags, paintVars, distCode);
 378 }
 379 
 380 void
 381 MTLPaints_SetLinearGradientPaint(MTLContext *mtlc, BMTLSDOps *dstOps,
 382                                  jboolean useMask, jboolean linear,
 383                                  jint cycleMethod, jint numStops,
 384                                  jfloat p0, jfloat p1, jfloat p3,
 385                                  void *fractions, void *pixels)
 386 {
 387     //TODO
 388     J2dTraceNotImplPrimitive("MTLPaints_SetMultiGradientPaint");
 389 }
 390 
 391 /********************** RadialGradientPaint support *************************/
 392 
 393 /**
 394  * The handles to the RadialGradientPaint fragment program objects.  The
 395  * index to the array should be a bitwise-or'ing of the MULTI_* flags defined
 396  * above.  Note that most applications will likely need to initialize one
 397  * or two of these elements, so the array is usually sparsely populated.
 398  */
 399 static GLhandleARB radialGradPrograms[MAX_PROGRAMS];
 400 
 401 /**
 402  * Compiles and links the RadialGradientPaint shader program.  If successful,
 403  * this function returns a handle to the newly created shader program;
 404  * otherwise returns 0.
 405  */
 406 static GLhandleARB
 407 MTLPaints_CreateRadialGradProgram(jint flags)
 408 {
 409     char *paintVars;
 410     char *distCode;
 411 
 412     J2dTraceLn1(J2D_TRACE_INFO,
 413                 "MTLPaints_CreateRadialGradProgram",
 414                 flags);
 415 
 416     /*
 417      * To simplify the code and to make it easier to upload a number of
 418      * uniform values at once, we pack a bunch of scalar (float) values
 419      * into vec3 and vec4 values below.  Here's how the values are related:
 420      *
 421      *   m0.x = m00
 422      *   m0.y = m01
 423      *   m0.z = m02
 424      *
 425      *   m1.x = m10
 426      *   m1.y = m11
 427      *   m1.z = m12
 428      *
 429      *   precalc.x = focusX
 430      *   precalc.y = yoff = dstOps->yOffset + dstOps->height
 431      *   precalc.z = 1.0 - (focusX * focusX)
 432      *   precalc.w = 1.0 / precalc.z
 433      */
 434     paintVars =
 435         "uniform vec3 m0;"
 436         "uniform vec3 m1;"
 437         "uniform vec4 precalc;";
 438 
 439     /*
 440      * The following code is derived from Daniel Rice's whitepaper on
 441      * radial gradient performance (attached to the bug report for 6521533).
 442      * Refer to that document as well as the setup code in the Java-level
 443      * BufferedPaints.setRadialGradientPaint() method for more details.
 444      */
 445     distCode =
 446         // note that gl_FragCoord is in window space relative to the
 447         // lower-left corner, so we have to flip the y-coordinate here
 448         "vec3 fragCoord ="
 449         "    vec3(gl_FragCoord.x, precalc.y - gl_FragCoord.y, 1.0);"
 450         "float x = dot(fragCoord, m0);"
 451         "float y = dot(fragCoord, m1);"
 452         "float xfx = x - precalc.x;"
 453         "dist = (precalc.x*xfx + sqrt(xfx*xfx + y*y*precalc.z))*precalc.w;";
 454 
 455     return MTLPaints_CreateMultiGradProgram(flags, paintVars, distCode);
 456 }
 457 
 458 void
 459 MTLPaints_SetRadialGradientPaint(MTLContext *mtlc, BMTLSDOps *dstOps,
 460                                  jboolean useMask, jboolean linear,
 461                                  jint cycleMethod, jint numStops,
 462                                  jfloat m00, jfloat m01, jfloat m02,
 463                                  jfloat m10, jfloat m11, jfloat m12,
 464                                  jfloat focusX,
 465                                  void *fractions, void *pixels)
 466 {
 467     //TODO
 468     J2dTraceNotImplPrimitive("MTLPaints_SetRadialGradientPaint");
 469 }
 470 
 471 #endif /* !HEADLESS */