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 MTLContext_h_Included 27 #define MTLContext_h_Included 28 29 #include <simd/simd.h> 30 31 #include "sun_java2d_pipe_BufferedContext.h" 32 #include "sun_java2d_metal_MTLContext.h" 33 #include "sun_java2d_metal_MTLContext_MTLContextCaps.h" 34 35 #import <Metal/Metal.h> 36 #include "j2d_md.h" 37 #include "MTLSurfaceDataBase.h" 38 #include "MTLTexturePool.h" 39 #include "MTLPipelineStatesStorage.h" 40 41 /** 42 * The MTLBlendRule structure encapsulates the two enumerated values that 43 * comprise a given Porter-Duff blending (compositing) rule. For example, 44 * the "SrcOver" rule can be represented by: 45 * rule.src = GL_ONE; 46 * rule.dst = GL_ONE_MINUS_SRC_ALPHA; 47 * 48 * GLenum src; 49 * The constant representing the source factor in this Porter-Duff rule. 50 * 51 * GLenum dst; 52 * The constant representing the destination factor in this Porter-Duff rule. 53 */ 54 typedef struct { 55 jint src; 56 jint dst; 57 } MTLBlendRule; 58 59 /** 60 * The MTLContext class contains cached state relevant to the native 61 * MTL context stored within the native ctxInfo field. Each Java-level 62 * MTLContext object is associated with a native-level MTLContext class. 63 * */ 64 @interface MTLContext : NSObject 65 66 @property jint compState; 67 @property jfloat extraAlpha; 68 @property jint alphaCompositeRule; 69 @property jint xorPixel; 70 @property jint pixel; 71 72 @property jdouble p0; 73 @property jdouble p1; 74 @property jdouble p3; 75 @property jboolean cyclic; 76 @property jint pixel1; 77 @property jint pixel2; 78 79 @property jubyte r; 80 @property jubyte g; 81 @property jubyte b; 82 @property jubyte a; 83 @property jint paintState; 84 @property jboolean useMask; 85 @property jboolean useTransform; 86 @property simd_float4x4 transform4x4; 87 @property jint blitTextureID; 88 @property jint textureFunction; 89 @property jboolean vertexCacheEnabled; 90 91 @property (readonly, strong) id<MTLDevice> device; 92 @property (strong) id<MTLLibrary> library; 93 @property (strong) id<MTLRenderPipelineState> pipelineState; 94 @property (strong) id<MTLCommandQueue> commandQueue; 95 @property (readonly,strong) id<MTLCommandBuffer> commandBuffer; 96 @property (strong) id<MTLBuffer> vertexBuffer; 97 @property jint color; 98 @property MTLScissorRect clipRect; 99 @property jboolean useClip; 100 @property (strong)MTLPipelineStatesStorage* pipelineStateStorage; 101 @property (strong)MTLTexturePool* texturePool; 102 103 - (void)releaseCommandBuffer; 104 /** 105 * Fetches the MTLContext associated with the given destination surface, 106 * makes the context current for those surfaces, updates the destination 107 * viewport, and then returns a pointer to the MTLContext. 108 */ 109 + (MTLContext*) setSurfacesEnv:(JNIEnv*)env src:(jlong)pSrc dst:(jlong)pDst; 110 111 - (id)initWithDevice:(id<MTLDevice>)d shadersLib:(NSString*)shadersLib; 112 113 /** 114 * Resets the current clip state (disables both scissor and depth tests). 115 */ 116 - (void)resetClip; 117 118 /** 119 * Sets the Metal scissor bounds to the provided rectangular clip bounds. 120 */ 121 - (void)setClipRectX1:(jint)x1 Y1:(jint)y1 X2:(jint)x2 Y2:(jint)y2; 122 123 /** 124 * Sets up a complex (shape) clip using the OpenGL depth buffer. This 125 * method prepares the depth buffer so that the clip Region spans can 126 * be "rendered" into it. The depth buffer is first cleared, then the 127 * depth func is setup so that when we render the clip spans, 128 * nothing is rendered into the color buffer, but for each pixel that would 129 * be rendered, a non-zero value is placed into that location in the depth 130 * buffer. With depth test enabled, pixels will only be rendered into the 131 * color buffer if the corresponding value at that (x,y) location in the 132 * depth buffer differs from the incoming depth value. 133 */ 134 - (void)beginShapeClip; 135 136 /** 137 * Finishes setting up the shape clip by resetting the depth func 138 * so that future rendering operations will once again be written into the 139 * color buffer (while respecting the clip set up in the depth buffer). 140 */ 141 - (void)endShapeClip; 142 143 /** 144 * Initializes the OpenGL state responsible for applying extra alpha. This 145 * step is only necessary for any operation that uses glDrawPixels() or 146 * glCopyPixels() with a non-1.0f extra alpha value. Since the source is 147 * always premultiplied, we apply the extra alpha value to both alpha and 148 * color components using GL_*_SCALE. 149 */ 150 - (void)setExtraAlpha:(jfloat)ea; 151 152 /** 153 * Resets all OpenGL compositing state (disables blending and logic 154 * operations). 155 */ 156 - (void)resetComposite; 157 158 /** 159 * Initializes the OpenGL blending state. XOR mode is disabled and the 160 * appropriate blend functions are setup based on the AlphaComposite rule 161 * constant. 162 */ 163 - (void)setAlphaCompositeRule:(jint)rule extraAlpha:(jfloat)extraAlpha 164 flags:(jint)flags; 165 166 /** 167 * Initializes the OpenGL logic op state to XOR mode. Blending is disabled 168 * before enabling logic op mode. The XOR pixel value will be applied 169 * later in the MTLContext_SetColor() method. 170 */ 171 - (void)setXorComposite:(jint)xorPixel; 172 - (jboolean)isBlendingDisabled; 173 174 /** 175 * Resets the OpenGL transform state back to the identity matrix. 176 */ 177 - (void)resetTransform; 178 179 /** 180 * Initializes the OpenGL transform state by setting the modelview transform 181 * using the given matrix parameters. 182 * 183 * REMIND: it may be worthwhile to add serial id to AffineTransform, so we 184 * could do a quick check to see if the xform has changed since 185 * last time... a simple object compare won't suffice... 186 */ 187 - (void)setTransformM00:(jdouble) m00 M10:(jdouble) m10 188 M01:(jdouble) m01 M11:(jdouble) m11 189 M02:(jdouble) m02 M12:(jdouble) m12; 190 191 /** 192 * Initializes a small texture tile for use with tiled blit operations (see 193 * MTLBlitLoops.c and MTLMaskBlit.c for usage examples). The texture ID for 194 * the tile is stored in the given MTLContext. The tile is initially filled 195 * with garbage values, but the tile is updated as needed (via 196 * glTexSubImage2D()) with real RGBA values used in tiled blit situations. 197 * The internal format for the texture is GL_RGBA8, which should be sufficient 198 * for storing system memory surfaces of any known format (see PixelFormats 199 * for a list of compatible surface formats). 200 */ 201 - (jboolean)initBlitTileTexture; 202 203 204 /** 205 * Creates a 2D texture of the given format and dimensions and returns the 206 * texture object identifier. This method is typically used to create a 207 * temporary texture for intermediate work, such as in the 208 * MTLContext_InitBlitTileTexture() method below. 209 */ 210 - (jint)createBlitTextureFormat:(jint)internalFormat pixelFormat:(jint)pixelFormat 211 width:(jint)width height:(jint)height; 212 213 - (void)destroyContextResources; 214 215 - (void)setColorR:(int)r G:(int)g B:(int)b A:(int)a; 216 - (void)setColorInt:(int)pixel; 217 218 - (id<MTLRenderCommandEncoder>)createSamplingEncoderForDest:(id<MTLTexture>)dest clearRed:(int)clearRed; 219 - (id<MTLRenderCommandEncoder>)createSamplingEncoderForDest:(id<MTLTexture>)dest; 220 - (id<MTLBlitCommandEncoder>)createBlitEncoder; 221 // NOTE: debug parameners will be removed soon 222 - (id<MTLRenderCommandEncoder>)createRenderEncoderForDest:(id<MTLTexture>)dest clearRed:(int) clearRed/*debug param*/; 223 - (id<MTLRenderCommandEncoder>)createRenderEncoderForDest:(id<MTLTexture>)dest; 224 - (void)setGradientPaintUseMask:(jboolean)useMask cyclic:(jboolean)cyclic p0:(jdouble) p0 p1:(jdouble) p1 p3:(jdouble)p3 225 pixel1:(jint)pixel1 pixel2:(jint) pixel2; 226 - (void) setEncoderTransform:(id<MTLRenderCommandEncoder>) encoder dest:(id<MTLTexture>) dest; 227 - (void)dealloc; 228 @end 229 230 /** 231 * See BufferedContext.java for more on these flags... 232 */ 233 #define MTLC_NO_CONTEXT_FLAGS \ 234 sun_java2d_pipe_BufferedContext_NO_CONTEXT_FLAGS 235 #define MTLC_SRC_IS_OPAQUE \ 236 sun_java2d_pipe_BufferedContext_SRC_IS_OPAQUE 237 #define MTLC_USE_MASK \ 238 sun_java2d_pipe_BufferedContext_USE_MASK 239 240 #endif /* MTLContext_h_Included */