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 java.util.ArrayList;
023:
024: public interface OGLContextManager {
025: public static class OffscreenBufferObject {
026: private static final int MAX_CACHED_BUFFERS = 10;
027: private static final ArrayList availableBuffers = new ArrayList();
028:
029: public final long id;
030: public final int width;
031: public final int height;
032: public final OGLContextManager config;
033: public final long hdc;
034:
035: public OffscreenBufferObject(long id, long hdc, int width,
036: int height, OGLContextManager config) {
037: this .id = id;
038: this .hdc = hdc;
039: this .width = width;
040: this .height = height;
041: this .config = config;
042: }
043:
044: public static final OffscreenBufferObject getCachedBuffer(
045: int w, int h, OGLContextManager config) {
046: for (int i = 0; i < availableBuffers.size(); i++) { // First try to find cached pbuffer
047: OffscreenBufferObject pbuffer = (OffscreenBufferObject) availableBuffers
048: .get(i);
049: if (pbuffer.width >= w && pbuffer.height >= h
050: && pbuffer.config == config) {
051: availableBuffers.remove(i);
052: return pbuffer;
053: }
054: }
055:
056: return null;
057: }
058:
059: public static final OffscreenBufferObject freeCachedBuffer(
060: OffscreenBufferObject pbuffer) {
061: if (availableBuffers.size() <= MAX_CACHED_BUFFERS) {
062: availableBuffers.add(pbuffer);
063: return null;
064: }
065:
066: // Try to find smaller pbuffer in the cache and replace it
067: for (int i = 0; i < availableBuffers.size(); i++) {
068: OffscreenBufferObject cached = (OffscreenBufferObject) availableBuffers
069: .get(i);
070: if (cached.width < pbuffer.width
071: || cached.height < pbuffer.height
072: || cached.config != pbuffer.config) {
073: availableBuffers.remove(i);
074: availableBuffers.add(pbuffer);
075: pbuffer = cached;
076: return pbuffer;
077: }
078: }
079:
080: return pbuffer;
081: }
082:
083: public static final void clearCache() {
084: for (int i = 0; i < availableBuffers.size(); i++) {
085: OffscreenBufferObject cached = (OffscreenBufferObject) availableBuffers
086: .get(i);
087: cached.config
088: .freeOffscreenBuffer(cached.id, cached.hdc);
089: }
090: availableBuffers.clear();
091: }
092: }
093:
094: /**
095: * Creates OpenGL context based on GraphicsConfiguration
096: * @param drawable - window handle, pbuffer, or linux drawable
097: * @param hdc - if drawable is offscreen
098: * @return context handle
099: */
100: public long getOGLContext(long drawable, long hdc);
101:
102: /**
103: * Destroys existing OpenGL context
104: * @param oglContext - context
105: */
106: public void destroyOGLContext(long oglContext);
107:
108: /**
109: * Makes OpenGL context current
110: * @param oglContext - OpenGL context handle
111: * @param drawable - window handle, pbuffer, or linux drawable
112: * @param hdc - if drawable is offscreen
113: * @return false if context was alrady current, true otherwise
114: */
115: public boolean makeCurrent(long oglContext, long drawable, long hdc);
116:
117: public boolean makeContextCurrent(long oglContext, long draw,
118: long read, long drawHDC, long readHDC);
119:
120: public void swapBuffers(long drawable, long hdc);
121:
122: public OffscreenBufferObject createOffscreenBuffer(int w, int h);
123:
124: public void freeOffscreenBuffer(OffscreenBufferObject buffer);
125:
126: public void freeOffscreenBuffer(long id, long hdc);
127: }
|