1 /*
   2  * Copyright (c) 2011, 2012, 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 #import "SurfaceData.h"
  27 #import "BufImgSurfaceData.h"
  28 #import "AWTFont.h"
  29 #import <Cocoa/Cocoa.h>
  30 #import "JNIUtilities.h"
  31 
  32 // these flags are not defined on Tiger on PPC, so we need to make them a no-op
  33 #if !defined(kCGBitmapByteOrder32Host)
  34 #define kCGBitmapByteOrder32Host 0
  35 #endif
  36 #if !defined(kCGBitmapByteOrder16Host)
  37 #define kCGBitmapByteOrder16Host 0
  38 #endif
  39 
  40 // NOTE : Modify the printSurfaceDataDiagnostics API if you change this enum
  41 enum SDRenderType
  42 {
  43     SD_Nothing,
  44     SD_Stroke,
  45     SD_Fill,
  46     SD_EOFill,
  47     SD_Shade,
  48     SD_LinearGradient,
  49     SD_RadialGradient,
  50     SD_Pattern,
  51     SD_Image,
  52     SD_Text,
  53     SD_CopyArea,
  54     SD_Queue,
  55     SD_External
  56 };
  57 typedef enum SDRenderType SDRenderType;
  58 
  59 struct _stateShadingInfo
  60 {
  61     CGPoint    start;
  62     CGPoint    end;
  63     CGFloat    colors[8];
  64     BOOL    cyclic;
  65     CGFloat    length; // of the total segment (used by the cyclic gradient)
  66     CGFloat    period; // of the cycle (used by the cyclic gradient)
  67     CGFloat    offset; // of the cycle from the start (used by the cyclic gradient)
  68 };
  69 typedef struct _stateShadingInfo StateShadingInfo;
  70 
  71 struct _stateGradientInfo
  72 {
  73     CGPoint    start;
  74     CGPoint    end;
  75     CGFloat  radius;
  76     CGFloat* colordata;
  77     CGFloat* fractionsdata;
  78     jint     fractionsLength;
  79 };
  80 typedef struct _stateGradientInfo StateGradientInfo;
  81 
  82 struct _statePatternInfo
  83 {
  84     CGFloat    tx;
  85     CGFloat    ty;
  86     CGFloat    sx;
  87     CGFloat    sy;
  88     jint    width;
  89     jint    height;
  90     jobject    sdata;
  91 };
  92 typedef struct _statePatternInfo StatePatternInfo;
  93 
  94 struct _stateGraphicsInfo
  95 {
  96     BOOL                adjustedLineWidth;
  97     BOOL                adjustedAntialias;
  98     BOOL                antialiased;
  99     jint                interpolation;
 100     BOOL                simpleColor;
 101     BOOL                simpleStroke;
 102     CGAffineTransform    ctm;
 103     CGFloat                offsetX;
 104     CGFloat                offsetY;
 105     struct CGPoint*        batchedLines;
 106     UInt32                batchedLinesCount;
 107 };
 108 typedef struct _stateGraphicsInfo StateGraphicsInfo;
 109 
 110 typedef struct _QuartzSDOps QuartzSDOps;
 111 typedef void BeginContextFunc(JNIEnv *env, QuartzSDOps *qsdo, SDRenderType renderType);
 112 typedef void FinishContextFunc(JNIEnv *env, QuartzSDOps *qsdo);
 113 struct _QuartzSDOps
 114 {
 115     BufImgSDOps                sdo; // must be the first entry!
 116 
 117     BeginContextFunc*        BeginSurface;        // used to set graphics states (clip, color, stroke, etc...)
 118     FinishContextFunc*        FinishSurface;        // used to finish drawing primitives
 119     BOOL                    newContext;
 120     CGContextRef            cgRef;
 121 
 122     jint*                    javaGraphicsStates;
 123     jobject                    javaGraphicsStatesObjects;
 124 
 125     SDRenderType            renderType;
 126 
 127     // rdar://problem/5214320
 128     // Gradient/Texture fills of Java GeneralPath don't respect the even odd winding rule (quartz pipeline).
 129     BOOL                    isEvenOddFill;        // Tracks whether the original render type passed into
 130                                                 // SetUpCGContext(...) is SD_EOFILL.
 131                                                 // The reason for this field is because SetUpCGContext(...) can
 132                                                 // change the render type after calling SetUpPaint(...), and right
 133                                                 // after that, the possibly new render type is then assigned into
 134                                                 // qsdo->renderType.  Sigh!!!
 135                                                 // This field is potentially used within CompleteCGContext(...) or
 136                                                 // its callees.
 137 
 138     StateShadingInfo*        shadingInfo;        // tracks shading and its parameters
 139     StateGradientInfo*       gradientInfo;       // tracks gradient and its parameters
 140     StatePatternInfo*        patternInfo;        // tracks pattern and its parameters
 141     StateGraphicsInfo        graphicsStateInfo;    // tracks other graphics state
 142 
 143     BOOL  syncContentsToLayer;    // should changed pixels be synced to a CALayer
 144     CGRect updateRect;     // used by the layer synchronization code to track update rects.
 145 };
 146 
 147 void SetUpCGContext(JNIEnv *env, QuartzSDOps *qsdo, SDRenderType renderType);
 148 SDRenderType DoShapeUsingCG(CGContextRef cgRef, jint *types, jfloat *coords, jint numtypes, BOOL fill, CGFloat offsetX, CGFloat offsetY);
 149 SDRenderType SetUpPaint(JNIEnv *env, QuartzSDOps *qsdo, SDRenderType renderType);
 150 void CompleteCGContext(JNIEnv *env, QuartzSDOps *qsdo);
 151 
 152 NSColor* ByteParametersToNSColor(JNIEnv* env, jint *javaGraphicsStates, NSColor* defColor);
 153 
 154 #define JNI_COCOA_RENDERER_EXIT(env) \
 155  JNI_COCOA_EXIT_WITH_ACTION(env, qsdo->FinishSurface(env, qsdo))