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 Oleg V. Khaschansky
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl.opengl;
021:
022: import org.apache.harmony.awt.gl.Surface;
023: import org.apache.harmony.awt.gl.ImageSurface;
024:
025: import java.awt.image.*;
026: import java.awt.color.ColorSpace;
027:
028: public class OGLSurface extends Surface {
029: private static final ColorModel cm = new DirectColorModel(
030: ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0x00ff0000,
031: 0x0000ff00, 0x000000ff, 0xff000000, true,
032: DataBuffer.TYPE_INT);
033:
034: OGLGraphics2D oglg;
035:
036: private ImageSurface imageSurface;
037: boolean sceneUpdated = true;
038: boolean cachedTopToBottom = true;
039: int cachedData[];
040:
041: OGLSurface(int width, int height, OGLGraphics2D g2d) {
042: super .width = width;
043: super .height = height;
044: oglg = g2d;
045: }
046:
047: @Override
048: public ColorModel getColorModel() {
049: return cm;
050: }
051:
052: @Override
053: public WritableRaster getRaster() {
054: WritableRaster res = cm.createCompatibleWritableRaster(width,
055: height);
056: DataBufferInt dbi = (DataBufferInt) res.getDataBuffer();
057: oglg.readPixels(0, 0, width, height, dbi.getData(), true);
058: return res;
059: }
060:
061: /**
062: * Clients should use the returned data only for reading
063: * @return image data
064: */
065: @Override
066: public synchronized Object getData() {
067: if (!sceneUpdated && cachedData != null && cachedTopToBottom) {
068: return cachedData;
069: }
070:
071: if (cachedData == null) {
072: cachedData = new int[width * height];
073: }
074:
075: oglg.readPixels(0, 0, width, height, cachedData, true);
076: sceneUpdated = false;
077: cachedTopToBottom = true;
078:
079: return cachedData;
080: }
081:
082: public synchronized Object getBottomToTopData() {
083: if (!sceneUpdated && cachedData != null && !cachedTopToBottom) {
084: return cachedData;
085: }
086:
087: if (cachedData == null) {
088: cachedData = new int[width * height];
089: }
090:
091: oglg.readPixels(0, 0, width, height, cachedData, false);
092: sceneUpdated = false;
093: cachedTopToBottom = false;
094:
095: return cachedData;
096: }
097:
098: @Override
099: public int getSurfaceType() {
100: return BufferedImage.TYPE_INT_ARGB_PRE;
101: }
102:
103: @Override
104: public long lock() {
105: return 0;
106: }
107:
108: @Override
109: public void unlock() {
110: }
111:
112: @Override
113: public void dispose() {
114: imageSurface.dispose();
115: }
116:
117: @Override
118: public Surface getImageSurface() {
119: if (imageSurface == null) {
120: imageSurface = new ImageSurface(getColorModel(),
121: getRaster());
122: } else {
123: imageSurface.setRaster(getRaster());
124: }
125:
126: return imageSurface;
127: }
128:
129: final void updateScene() {
130: sceneUpdated = true;
131: clearValidCaches();
132: }
133: }
|