001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: * Created on 26.11.2005
021: *
022: */package org.apache.harmony.awt.gl.render;
023:
024: import java.awt.AlphaComposite;
025: import java.awt.Color;
026: import java.awt.Composite;
027: import java.awt.Rectangle;
028: import java.awt.geom.AffineTransform;
029: import java.awt.image.BufferedImage;
030:
031: import org.apache.harmony.awt.gl.ImageSurface;
032: import org.apache.harmony.awt.gl.MultiRectArea;
033: import org.apache.harmony.awt.gl.Surface;
034: import org.apache.harmony.awt.gl.XORComposite;
035:
036: /**
037: * This kind of blitters is intended for drawing one image on the buffered
038: * or volatile image. For the moment we can blit natively Buffered Images which
039: * have sRGB, Linear_RGB, Linear_Gray Color Space and type different
040: * from BufferedImage.TYPE_CUSTOM, Volatile Images and Images which received
041: * using Toolkit and Component classes.
042: */
043: public class NativeImageBlitter implements Blitter {
044:
045: final static NativeImageBlitter inst = new NativeImageBlitter();
046:
047: public static NativeImageBlitter getInstance() {
048: return inst;
049: }
050:
051: public void blit(int srcX, int srcY, Surface srcSurf, int dstX,
052: int dstY, Surface dstSurf, int width, int height,
053: AffineTransform sysxform, AffineTransform xform,
054: Composite comp, Color bgcolor, MultiRectArea clip) {
055:
056: if (!srcSurf.isNativeDrawable()) {
057: JavaBlitter.inst.blit(srcX, srcY, srcSurf, dstX, dstY,
058: dstSurf, width, height, sysxform, xform, comp,
059: bgcolor, clip);
060: } else {
061: if (xform == null) {
062: blit(srcX, srcY, srcSurf, dstX, dstY, dstSurf, width,
063: height, sysxform, comp, bgcolor, clip);
064: } else {
065: double scaleX = xform.getScaleX();
066: double scaleY = xform.getScaleY();
067: double scaledX = dstX / scaleX;
068: double scaledY = dstY / scaleY;
069: AffineTransform at = new AffineTransform();
070: at.setToTranslation(scaledX, scaledY);
071: xform.concatenate(at);
072: sysxform.concatenate(xform);
073: blit(srcX, srcY, srcSurf, 0, 0, dstSurf, width, height,
074: sysxform, comp, bgcolor, clip);
075: }
076: }
077: }
078:
079: public void blit(int srcX, int srcY, Surface srcSurf, int dstX,
080: int dstY, Surface dstSurf, int width, int height,
081: AffineTransform sysxform, Composite comp, Color bgcolor,
082: MultiRectArea clip) {
083:
084: if (!srcSurf.isNativeDrawable()) {
085: JavaBlitter.inst.blit(srcX, srcY, srcSurf, dstX, dstY,
086: dstSurf, width, height, sysxform, comp, bgcolor,
087: clip);
088: } else {
089: int type = sysxform.getType();
090: switch (type) {
091: case AffineTransform.TYPE_TRANSLATION:
092: dstX += sysxform.getTranslateX();
093: dstY += sysxform.getTranslateY();
094: case AffineTransform.TYPE_IDENTITY:
095: blit(srcX, srcY, srcSurf, dstX, dstY, dstSurf, width,
096: height, comp, bgcolor, clip);
097: break;
098: default:
099: // TODO Need to realize Affine Transformation
100: if (srcSurf instanceof ImageSurface) {
101: JavaBlitter.inst.blit(srcX, srcY, srcSurf, dstX,
102: dstY, dstSurf, width, height, sysxform,
103: comp, bgcolor, clip);
104: } else {
105: int w = srcSurf.getWidth();
106: int h = srcSurf.getHeight();
107: BufferedImage tmp = new BufferedImage(w, h,
108: BufferedImage.TYPE_INT_RGB);
109: Surface tmpSurf = Surface.getImageSurface(tmp);
110: blit(0, 0, srcSurf, 0, 0, tmpSurf, w, h,
111: AlphaComposite.SrcOver, null, null);
112: JavaBlitter.inst.blit(srcX, srcY, tmpSurf, dstX,
113: dstY, dstSurf, width, height, sysxform,
114: comp, bgcolor, clip);
115: }
116: }
117: }
118: }
119:
120: public void blit(int srcX, int srcY, Surface srcSurf, int dstX,
121: int dstY, Surface dstSurf, int width, int height,
122: Composite comp, Color bgcolor, MultiRectArea clip) {
123:
124: if (!srcSurf.isNativeDrawable()) {
125: JavaBlitter.inst.blit(srcX, srcY, srcSurf, dstX, dstY,
126: dstSurf, width, height, comp, bgcolor, clip);
127: } else {
128: long dstSurfStruct = dstSurf.getSurfaceDataPtr();
129: Object dstData = dstSurf.getData();
130: int clipRects[];
131: if (clip != null) {
132: clipRects = clip.rect;
133: } else {
134: clipRects = new int[] { 5, 0, 0, dstSurf.getWidth(),
135: dstSurf.getHeight() };
136: }
137:
138: if (!(srcSurf instanceof ImageSurface)) {
139: srcSurf = srcSurf.getImageSurface();
140: if (bgcolor != null) {
141: bgcolor = null;
142: }
143: }
144:
145: long srcSurfStruct = srcSurf.getSurfaceDataPtr();
146: Object srcData = srcSurf.getData();
147: if (comp instanceof AlphaComposite) {
148: AlphaComposite ac = (AlphaComposite) comp;
149: int compType = ac.getRule();
150: float alpha = ac.getAlpha();
151: if (bgcolor != null) {
152: bltBG(srcX, srcY, srcSurfStruct, srcData, dstX,
153: dstY, dstSurfStruct, dstData, width,
154: height, bgcolor.getRGB(), compType, alpha,
155: clipRects, srcSurf.invalidated());
156: dstSurf.addDirtyRegion(new Rectangle(dstX, dstY,
157: width, height));
158: } else {
159: blt(srcX, srcY, srcSurfStruct, srcData, dstX, dstY,
160: dstSurfStruct, dstData, width, height,
161: compType, alpha, clipRects, srcSurf
162: .invalidated());
163: dstSurf.addDirtyRegion(new Rectangle(dstX, dstY,
164: width, height));
165: }
166: } else if (comp instanceof XORComposite) {
167: XORComposite xcomp = (XORComposite) comp;
168: xor(srcX, srcY, srcSurfStruct, srcData, dstX, dstY,
169: dstSurfStruct, dstData, width, height, xcomp
170: .getXORColor().getRGB(), clipRects,
171: srcSurf.invalidated());
172: dstSurf.addDirtyRegion(new Rectangle(dstX, dstY, width,
173: height));
174: } else {
175: if (srcSurf instanceof ImageSurface) {
176: JavaBlitter.inst.blit(srcX, srcY, srcSurf, dstX,
177: dstY, dstSurf, width, height, comp,
178: bgcolor, clip);
179: } else {
180: int w = srcSurf.getWidth();
181: int h = srcSurf.getHeight();
182: BufferedImage tmp = new BufferedImage(w, h,
183: BufferedImage.TYPE_INT_RGB);
184: Surface tmpSurf = Surface.getImageSurface(tmp);
185: long tmpSurfStruct = tmpSurf.getSurfaceDataPtr();
186: Object tmpData = tmpSurf.getData();
187: int tmpClip[] = new int[] { 5, 0, 0,
188: srcSurf.getWidth(), srcSurf.getHeight() };
189:
190: blt(0, 0, srcSurfStruct, srcData, 0, 0,
191: tmpSurfStruct, tmpData, w, h,
192: AlphaComposite.SRC_OVER, 1.0f, tmpClip,
193: srcSurf.invalidated());
194: JavaBlitter.inst.blit(srcX, srcY, tmpSurf, dstX,
195: dstY, dstSurf, width, height, comp,
196: bgcolor, clip);
197: }
198: }
199: }
200:
201: }
202:
203: private native void bltBG(int srcX, int srcY, long srsSurfDataPtr,
204: Object srcData, int dstX, int dstY, long dstSurfDataPtr,
205: Object dstData, int width, int height, int bgcolor,
206: int compType, float alpha, int clip[], boolean invalidated);
207:
208: private native void blt(int srcX, int srcY, long srsSurfDataPtr,
209: Object srcData, int dstX, int dstY, long dstSurfDataPtr,
210: Object dstData, int width, int height, int compType,
211: float alpha, int clip[], boolean invalidated);
212:
213: private native void xor(int srcX, int srcY, long srsSurfDataPtr,
214: Object srcData, int dstX, int dstY, long dstSurfDataPtr,
215: Object dstData, int width, int height, int xorcolor,
216: int clip[], boolean invalidated);
217:
218: }
|