001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: *
024: */
025:
026: package com.sun.pisces;
027:
028: import com.sun.me.gci.surface.GCIDrawingSurface;
029: import com.sun.me.gci.surface.GCISurfaceInfo;
030:
031: /**
032: * Wrapper for GCIDrawingSurface for Pisces rendering.
033: * @see PiscesShapeRenderer
034: */
035: public final class PiscesGCISurface extends AbstractSurface {
036: /**
037: * Pointer to native image data. Accessed from native code.
038: * This field is used when GCISurface stores image data in a buffer allocated
039: * by native code.
040: */
041: private long nativeArray;
042:
043: /**
044: * Java image data arrays for different image-types. GCIDrawingSurface uses
045: * different Java arrays to store data. The real data-type used depends on
046: * image-type.
047: * Accessed from native code.
048: */
049: private int[] javaArrayInt;
050: private short[] javaArrayShort;
051: private byte[] javaArrayByte;
052:
053: /**
054: * This property says which of previously declared data arrays
055: * (nativeArray, javaArrayInt, javaArrayShort, javaArrayByte) is
056: * currently used to store image-data. This is set in acquireSurface()
057: * accordingly to underlying GCIDrawingSurface properties. The value
058: * is also used in native code for acquirement of appropriate image memory
059: * from java. Values must be one of the following TYPE_OF_ARRAY_.
060: * Another value is errorneous.
061: */
062: private int typeOfArray;
063:
064: /**
065: * Field's @typeOfArray possible values.
066: * Corresponding #defines are used in native code. If you modify them here,
067: * please, make relevant changes to JPiscesGCISurface.c also.
068: */
069: public static final int TYPE_OF_ARRAY_NATIVE = 0;
070: public static final int TYPE_OF_ARRAY_JAVA_INT = 1;
071: public static final int TYPE_OF_ARRAY_JAVA_SHORT = 2;
072: public static final int TYPE_OF_ARRAY_JAVA_BYTE = 3;
073:
074: /**
075: * Underlying GCIDrawingSurface. This is destination of our rendering.
076: */
077: private final GCIDrawingSurface gciSurface;
078:
079: /**
080: * The GCISurfaceInfo of gciSurface held between calls to acquireSurface
081: * and releaseSurface.
082: */
083: private GCISurfaceInfo gciSurfaceInfo;
084:
085: private final int imageType;
086: /**
087: * How much to right-shift value in bits to get value in pixels.
088: */
089: private final int bitsToPixelsShift;
090:
091: private int offset;
092: private int scanlineStride;
093: private int pixelStride;
094:
095: /**
096: * Instantiates wrapper class of GCIDrawingSurface for Pisces rendering.
097: * It acquires all needed information from surface and ensures
098: * initialization of native pisces surface structures.
099: * @param surface
100: * @see GCIDrawingSurface
101: */
102: public PiscesGCISurface(GCIDrawingSurface surface) {
103: gciSurface = surface;
104:
105: switch (surface.getFormat()) {
106: case GCIDrawingSurface.FORMAT_RGB_888:
107: case GCIDrawingSurface.FORMAT_XRGB_8888:
108: case GCIDrawingSurface.FORMAT_ARGB_8888:
109: imageType = RendererBase.TYPE_INT_RGB;
110: bitsToPixelsShift = 5; // 2 ^ 5 = 32
111: break;
112: case GCIDrawingSurface.FORMAT_RGB_565:
113: imageType = RendererBase.TYPE_USHORT_565_RGB;
114: bitsToPixelsShift = 4; // 2 ^ 4 = 16
115: break;
116: case GCIDrawingSurface.FORMAT_GRAY_8:
117: imageType = RendererBase.TYPE_BYTE_GRAY;
118: bitsToPixelsShift = 3; // 2 ^ 3 = 8
119: break;
120: case GCIDrawingSurface.FORMAT_ARGB_8888_PRE:
121: imageType = RendererBase.TYPE_INT_ARGB_PRE;
122: bitsToPixelsShift = 5; // 2 ^ 5 = 32
123: break;
124: default:
125: throw new IllegalArgumentException(
126: "Pisces does not support" + " "
127: + surface.getFormat() + " yet");
128: }
129:
130: int width = surface.getWidth();
131: int height = surface.getHeight();
132:
133: if (surface.isSurfaceInfoDynamic()) {
134: initialize(imageType, width, height, true);
135: } else {
136: // acquire surface once
137: acquireSurface();
138: initialize(imageType, width, height, false);
139: }
140: }
141:
142: /**
143: * Gets all neccessary information from GCIDrawingSurface.
144: * Anytime surface changes at runtime. This method must be called to ensure
145: * pisces and GCI surfaces synchronization.
146: */
147: public void acquireSurface() {
148: if (gciSurfaceInfo != null) {
149: // we have already acquired the surface
150: return;
151: }
152: gciSurfaceInfo = gciSurface.getSurfaceInfo();
153: if (gciSurfaceInfo == null) {
154: throw new RuntimeException(
155: "Unable to get surface information");
156: }
157:
158: offset = gciSurfaceInfo.getBitOffset() >> bitsToPixelsShift;
159: scanlineStride = gciSurfaceInfo.getYBitStride() >> bitsToPixelsShift;
160: pixelStride = gciSurfaceInfo.getXBitStride() >> bitsToPixelsShift;
161:
162: if (gciSurface.isNativeSurface()) {
163: typeOfArray = TYPE_OF_ARRAY_NATIVE;
164: nativeArray = gciSurfaceInfo.getBasePointer();
165: } else {
166: switch (gciSurfaceInfo.getPixelArrayType()) {
167: case GCISurfaceInfo.TYPE_JAVA_ARRAY_INT:
168: javaArrayInt = (int[]) gciSurfaceInfo.getPixelArray();
169: typeOfArray = TYPE_OF_ARRAY_JAVA_INT;
170: break;
171: case GCISurfaceInfo.TYPE_JAVA_ARRAY_SHORT:
172: javaArrayShort = (short[]) gciSurfaceInfo
173: .getPixelArray();
174: typeOfArray = TYPE_OF_ARRAY_JAVA_SHORT;
175: break;
176: case GCISurfaceInfo.TYPE_JAVA_ARRAY_BYTE:
177: javaArrayByte = (byte[]) gciSurfaceInfo.getPixelArray();
178: typeOfArray = TYPE_OF_ARRAY_JAVA_BYTE;
179: break;
180: default:
181: break;
182: }
183: }
184: }
185:
186: /**
187: * Detaches (releases) underlying GCIDrawing surface and resets Pisces
188: * native surface structures. No rendering is possible after
189: * releaseSurface().
190: */
191: public void releaseSurface() {
192: if (gciSurfaceInfo != null) {
193: // allow garbage collection
194: javaArrayByte = null;
195: javaArrayInt = null;
196: javaArrayShort = null;
197:
198: nativeArray = 0;
199:
200: gciSurfaceInfo.release();
201: gciSurfaceInfo = null;
202: }
203: }
204:
205: /**
206: * This method initializes (C-native-code) underlying pisces
207: * surface/renderer structures.
208: * @see JPiscesGCISurface.c source on details.
209: */
210: private native void initialize(int imageType, int width,
211: int height, boolean isDynamic);
212: }
|