Path: blob/master/src/java.desktop/share/native/common/java2d/opengl/OGLMaskFill.c
41159 views
/*1* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifndef HEADLESS2627#include "sun_java2d_opengl_OGLMaskFill.h"2829#include "OGLMaskFill.h"30#include "OGLRenderQueue.h"31#include "OGLVertexCache.h"3233/**34* This implementation first copies the alpha tile into a texture and then35* maps that texture to the destination surface. This approach appears to36* offer the best performance despite being a two-step process.37*38* When the source paint is a Color, we can simply use the GL_MODULATE39* function to multiply the current color (already premultiplied with the40* extra alpha value from the AlphaComposite) with the alpha value from41* the mask texture tile. In picture form, this process looks like:42*43* A R G B44* primary color Pa Pr Pg Pb (modulated with...)45* texture unit 0 Ca Ca Ca Ca46* ---------------------------------------47* resulting color Ra Rr Rg Rb48*49* where:50* Px = current color (already premultiplied by extra alpha)51* Cx = coverage value from mask tile52* Rx = resulting color/alpha component53*54* When the source paint is not a Color, it means that we are rendering with55* a complex paint (e.g. GradientPaint, TexturePaint). In this case, we56* rely on the GL_ARB_multitexture extension to effectively multiply the57* paint fragments (autogenerated on texture unit 1, see the58* OGLPaints_Set{Gradient,Texture,etc}Paint() methods for more details)59* with the coverage values from the mask texture tile (provided on texture60* unit 0), all of which is multiplied with the current color value (which61* contains the extra alpha value). In picture form:62*63* A R G B64* primary color Ea Ea Ea Ea (modulated with...)65* texture unit 0 Ca Ca Ca Ca (modulated with...)66* texture unit 1 Pa Pr Pg Pb67* ---------------------------------------68* resulting color Ra Rr Rg Rb69*70* where:71* Ea = extra alpha72* Cx = coverage value from mask tile73* Px = gradient/texture paint color (generated for each fragment)74* Rx = resulting color/alpha component75*76* Here are some descriptions of the many variables used in this method:77* x,y - upper left corner of the tile destination78* w,h - width/height of the mask tile79* x0 - placekeeper for the original destination x location80* tw,th - width/height of the actual texture tile in pixels81* sx1,sy1 - upper left corner of the mask tile source region82* sx2,sy2 - lower left corner of the mask tile source region83* sx,sy - "current" upper left corner of the mask tile region of interest84*/85void86OGLMaskFill_MaskFill(OGLContext *oglc,87jint x, jint y, jint w, jint h,88jint maskoff, jint maskscan, jint masklen,89unsigned char *pMask)90{91J2dTraceLn(J2D_TRACE_INFO, "OGLMaskFill_MaskFill");9293RETURN_IF_NULL(oglc);94CHECK_PREVIOUS_OP(OGL_STATE_MASK_OP);9596J2dTraceLn4(J2D_TRACE_VERBOSE, " x=%d y=%d w=%d h=%d", x, y, w, h);97J2dTraceLn2(J2D_TRACE_VERBOSE, " maskoff=%d maskscan=%d",98maskoff, maskscan);99100{101jint tw, th, x0;102jint sx1, sy1, sx2, sy2;103jint sx, sy, sw, sh;104105x0 = x;106tw = OGLVC_MASK_CACHE_TILE_WIDTH;107th = OGLVC_MASK_CACHE_TILE_HEIGHT;108sx1 = maskoff % maskscan;109sy1 = maskoff / maskscan;110sx2 = sx1 + w;111sy2 = sy1 + h;112113for (sy = sy1; sy < sy2; sy += th, y += th) {114x = x0;115sh = ((sy + th) > sy2) ? (sy2 - sy) : th;116117for (sx = sx1; sx < sx2; sx += tw, x += tw) {118sw = ((sx + tw) > sx2) ? (sx2 - sx) : tw;119120OGLVertexCache_AddMaskQuad(oglc,121sx, sy, x, y, sw, sh,122maskscan, pMask);123}124}125}126}127128JNIEXPORT void JNICALL129Java_sun_java2d_opengl_OGLMaskFill_maskFill130(JNIEnv *env, jobject self,131jint x, jint y, jint w, jint h,132jint maskoff, jint maskscan, jint masklen,133jbyteArray maskArray)134{135OGLContext *oglc = OGLRenderQueue_GetCurrentContext();136unsigned char *mask;137138J2dTraceLn(J2D_TRACE_ERROR, "OGLMaskFill_maskFill");139140if (maskArray != NULL) {141mask = (unsigned char *)142(*env)->GetPrimitiveArrayCritical(env, maskArray, NULL);143} else {144mask = NULL;145}146147OGLMaskFill_MaskFill(oglc,148x, y, w, h,149maskoff, maskscan, masklen, mask);150151// 6358147: reset current state, and ensure rendering is flushed to dest152if (oglc != NULL) {153RESET_PREVIOUS_OP();154j2d_glFlush();155}156157if (mask != NULL) {158(*env)->ReleasePrimitiveArrayCritical(env, maskArray, mask, JNI_ABORT);159}160}161162#endif /* !HEADLESS */163164165