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