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 Alexey A. Petrenko
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl.windows;
021:
022: import java.awt.Graphics2D;
023: import java.awt.GraphicsConfiguration;
024: import java.awt.ImageCapabilities;
025: import java.awt.image.BufferedImage;
026: import java.awt.image.ImageObserver;
027:
028: import org.apache.harmony.awt.gl.GLVolatileImage;
029: import org.apache.harmony.awt.gl.Surface;
030: import org.apache.harmony.awt.gl.windows.BitmapSurface;
031: import org.apache.harmony.awt.internal.nls.Messages;
032: import org.apache.harmony.awt.wtk.NativeWindow;
033:
034: /**
035: * Volatile image for Windows
036: *
037: */
038: public class WinVolatileImage extends GLVolatileImage {
039: private final long hwnd;
040: long gi;
041: private final int width;
042: private final int height;
043: Surface surface;
044:
045: private WinGraphicsConfiguration gc = null;
046:
047: /***************************************************************************
048: *
049: * Constructors
050: *
051: ***************************************************************************/
052: public WinVolatileImage(NativeWindow nw, int width, int height) {
053: if (width <= 0 || height <= 0) {
054: // awt.19=Illegal size of volatile image.
055: throw new IllegalArgumentException(Messages
056: .getString("awt.19")); //$NON-NLS-1$
057: }
058: hwnd = nw.getId();
059: this .width = width;
060: this .height = height;
061: if (WinGraphicsDevice.useGDI)
062: gi = WinGDIGraphics2D.createCompatibleImageInfo(hwnd,
063: width, height);
064: else
065: gi = WinGDIPGraphics2D.createCompatibleImageInfo(hwnd,
066: width, height);
067: surface = new BitmapSurface(gi, width, height);
068: }
069:
070: public WinVolatileImage(WinGraphicsConfiguration gc, int width,
071: int height) {
072: if (width <= 0 || height <= 0) {
073: // awt.19=Illegal size of volatile image.
074: throw new IllegalArgumentException(Messages
075: .getString("awt.19")); //$NON-NLS-1$
076: }
077:
078: hwnd = 0;
079: this .gc = gc;
080: this .width = width;
081: this .height = height;
082: if (WinGraphicsDevice.useGDI)
083: gi = WinGDIGraphics2D.createCompatibleImageInfo(
084: ((WinGraphicsDevice) gc.getDevice()).getIDBytes(),
085: width, height);
086: else
087: gi = WinGDIPGraphics2D.createCompatibleImageInfo(
088: ((WinGraphicsDevice) gc.getDevice()).getIDBytes(),
089: width, height);
090: surface = new BitmapSurface(gi, width, height);
091: }
092:
093: @Override
094: public boolean contentsLost() {
095: return gi == 0;
096: }
097:
098: @Override
099: public Graphics2D createGraphics() {
100: if (gi == 0) {
101: if (hwnd != 0 && gc == null) {
102: if (WinGraphicsDevice.useGDI)
103: gi = WinGDIGraphics2D.createCompatibleImageInfo(
104: hwnd, width, height);
105: else
106: gi = WinGDIPGraphics2D.createCompatibleImageInfo(
107: hwnd, width, height);
108: } else if (hwnd == 0 && gc != null) {
109: if (WinGraphicsDevice.useGDI)
110: gi = WinGDIGraphics2D.createCompatibleImageInfo(
111: ((WinGraphicsDevice) gc.getDevice())
112: .getIDBytes(), width, height);
113: else
114: gi = WinGDIPGraphics2D.createCompatibleImageInfo(
115: ((WinGraphicsDevice) gc.getDevice())
116: .getIDBytes(), width, height);
117: }
118: }
119: if (WinGraphicsDevice.useGDI)
120: return new WinGDIGraphics2D(this , width, height);
121: return new WinGDIPGraphics2D(this , width, height);
122: }
123:
124: @Override
125: public ImageCapabilities getCapabilities() {
126: return new ImageCapabilities(false);
127: }
128:
129: @Override
130: public int getHeight() {
131: return height;
132: }
133:
134: @Override
135: public BufferedImage getSnapshot() {
136: return null;
137: }
138:
139: @Override
140: public int getWidth() {
141: return width;
142: }
143:
144: @Override
145: public int validate(GraphicsConfiguration gc) {
146: if (gi != 0) {
147: return IMAGE_OK;
148: }
149:
150: if (WinGraphicsDevice.useGDI)
151: gi = WinGDIGraphics2D.createCompatibleImageInfo(
152: ((WinGraphicsDevice) gc.getDevice()).getIDBytes(),
153: width, height);
154: else
155: gi = WinGDIPGraphics2D.createCompatibleImageInfo(
156: ((WinGraphicsDevice) gc.getDevice()).getIDBytes(),
157: width, height);
158: return IMAGE_RESTORED;
159: }
160:
161: @Override
162: public Object getProperty(String name, ImageObserver observer) {
163: return UndefinedProperty;
164: }
165:
166: @Override
167: public int getWidth(ImageObserver observer) {
168: return width;
169: }
170:
171: @Override
172: public int getHeight(ImageObserver observer) {
173: return height;
174: }
175:
176: @Override
177: protected void finalize() throws Throwable {
178: flush();
179: super .finalize();
180: }
181:
182: @Override
183: public Surface getImageSurface() {
184: return surface;
185: }
186:
187: @Override
188: public void flush() {
189: if (gi != 0) {
190: if (WinGraphicsDevice.useGDI)
191: WinGDIGraphics2D.disposeGraphicsInfo(gi);
192: else
193: WinGDIPGraphics2D.disposeGraphicsInfo(gi);
194: gi = 0;
195: }
196: if (surface != null)
197: surface.dispose();
198: super .flush();
199: }
200:
201: long getHWND() {
202: return hwnd;
203: }
204:
205: WinGraphicsConfiguration getGraphicsConfiguration() {
206: return gc;
207: }
208: }
|