diff options
author | Kenneth Russel <[email protected]> | 2008-07-16 08:03:23 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2008-07-16 08:03:23 +0000 |
commit | 0867db5f547bedae63ed2be34cc02d7bf39e6ec9 (patch) | |
tree | 9f76b5101dc62c413574b5bdc27dd5cb299457f2 /src/classes/com/sun/opengl/util/texture/TextureData.java | |
parent | 9fc74686947d23bdd715580ad2ad2d74de24c8a0 (diff) |
Reorganized TextureIO classes to enable code sharing between desktop
and mobile platforms
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/branches/JOGL_2_SANDBOX@1723 232f8b59-042b-4e1e-8c03-345bb8c30851
Diffstat (limited to 'src/classes/com/sun/opengl/util/texture/TextureData.java')
-rwxr-xr-x | src/classes/com/sun/opengl/util/texture/TextureData.java | 261 |
1 files changed, 231 insertions, 30 deletions
diff --git a/src/classes/com/sun/opengl/util/texture/TextureData.java b/src/classes/com/sun/opengl/util/texture/TextureData.java index 8cfd246b0..464305311 100755 --- a/src/classes/com/sun/opengl/util/texture/TextureData.java +++ b/src/classes/com/sun/opengl/util/texture/TextureData.java @@ -39,6 +39,7 @@ package com.sun.opengl.util.texture; import java.nio.*; import javax.media.opengl.*; +import javax.media.opengl.util.*; import com.sun.opengl.util.*; /** @@ -51,85 +52,263 @@ import com.sun.opengl.util.*; * @author Kenneth Russell */ -public interface TextureData { +public class TextureData { + protected int width; + protected int height; + private int border; + protected int pixelFormat; + protected int pixelType; + protected int internalFormat; // perhaps inferred from pixelFormat? + protected boolean mipmap; // indicates whether mipmaps should be generated + // (ignored if mipmaps are supplied from the file) + private boolean dataIsCompressed; + protected boolean mustFlipVertically; // Must flip texture coordinates + // vertically to get OpenGL output + // to look correct + protected Buffer buffer; // the actual data... + private Buffer[] mipmapData; // ...or a series of mipmaps + private Flusher flusher; + protected int rowLength; + protected int alignment; // 1, 2, or 4 bytes + protected int estimatedMemorySize; + + // These booleans are a concession to the AWTTextureData subclass + protected boolean haveEXTABGR; + protected boolean haveGL12; + + /** + * Constructs a new TextureData object with the specified parameters + * and data contained in the given Buffer. The optional Flusher can + * be used to clean up native resources associated with this + * TextureData when processing is complete; for example, closing of + * memory-mapped files that might otherwise require a garbage + * collection to reclaim and close. + * + * @param internalFormat the OpenGL internal format for the + * resulting texture; must be specified, may + * not be 0 + * @param width the width in pixels of the texture + * @param height the height in pixels of the texture + * @param border the number of pixels of border this texture + * data has (0 or 1) + * @param pixelFormat the OpenGL pixel format for the + * resulting texture; must be specified, may + * not be 0 + * @param pixelType the OpenGL type of the pixels of the texture + * @param mipmap indicates whether mipmaps should be + * autogenerated (using GLU) for the resulting + * texture. Currently if mipmap is true then + * dataIsCompressed may not be true. + * @param dataIsCompressed indicates whether the texture data is in + * compressed form + * (e.g. GL_COMPRESSED_RGB_S3TC_DXT1_EXT) + * @param mustFlipVertically indicates whether the texture + * coordinates must be flipped vertically + * in order to properly display the + * texture + * @param buffer the buffer containing the texture data + * @param flusher optional flusher to perform cleanup tasks + * upon call to flush() + * + * @throws IllegalArgumentException if any parameters of the texture + * data were invalid, such as requesting mipmap generation for a + * compressed texture + */ + public TextureData(int internalFormat, + int width, + int height, + int border, + int pixelFormat, + int pixelType, + boolean mipmap, + boolean dataIsCompressed, + boolean mustFlipVertically, + Buffer buffer, + Flusher flusher) throws IllegalArgumentException { + if (mipmap && dataIsCompressed) { + throw new IllegalArgumentException("Can not generate mipmaps for compressed textures"); + } + + this.width = width; + this.height = height; + this.border = border; + this.pixelFormat = pixelFormat; + this.pixelType = pixelType; + this.internalFormat = internalFormat; + this.mipmap = mipmap; + this.dataIsCompressed = dataIsCompressed; + this.mustFlipVertically = mustFlipVertically; + this.buffer = buffer; + this.flusher = flusher; + alignment = 1; // FIXME: is this correct enough in all situations? + estimatedMemorySize = estimatedMemorySize(buffer); + } + + /** + * Constructs a new TextureData object with the specified parameters + * and data for multiple mipmap levels contained in the given array + * of Buffers. The optional Flusher can be used to clean up native + * resources associated with this TextureData when processing is + * complete; for example, closing of memory-mapped files that might + * otherwise require a garbage collection to reclaim and close. + * + * @param internalFormat the OpenGL internal format for the + * resulting texture; must be specified, may + * not be 0 + * @param width the width in pixels of the topmost mipmap + * level of the texture + * @param height the height in pixels of the topmost mipmap + * level of the texture + * @param border the number of pixels of border this texture + * data has (0 or 1) + * @param pixelFormat the OpenGL pixel format for the + * resulting texture; must be specified, may + * not be 0 + * @param pixelType the OpenGL type of the pixels of the texture + * @param dataIsCompressed indicates whether the texture data is in + * compressed form + * (e.g. GL_COMPRESSED_RGB_S3TC_DXT1_EXT) + * @param mustFlipVertically indicates whether the texture + * coordinates must be flipped vertically + * in order to properly display the + * texture + * @param mipmapData the buffers containing all mipmap levels + * of the texture's data + * @param flusher optional flusher to perform cleanup tasks + * upon call to flush() + * + * @throws IllegalArgumentException if any parameters of the texture + * data were invalid, such as requesting mipmap generation for a + * compressed texture + */ + public TextureData(int internalFormat, + int width, + int height, + int border, + int pixelFormat, + int pixelType, + boolean dataIsCompressed, + boolean mustFlipVertically, + Buffer[] mipmapData, + Flusher flusher) throws IllegalArgumentException { + this.width = width; + this.height = height; + this.border = border; + this.pixelFormat = pixelFormat; + this.pixelType = pixelType; + this.internalFormat = internalFormat; + this.dataIsCompressed = dataIsCompressed; + this.mustFlipVertically = mustFlipVertically; + this.mipmapData = (Buffer[]) mipmapData.clone(); + this.flusher = flusher; + alignment = 1; // FIXME: is this correct enough in all situations? + for (int i = 0; i < mipmapData.length; i++) { + estimatedMemorySize += estimatedMemorySize(mipmapData[i]); + } + } + + /** Used only by subclasses */ + protected TextureData() { + } + /** Returns the width in pixels of the texture data. */ - public int getWidth(); + public int getWidth() { return width; } /** Returns the height in pixels of the texture data. */ - public int getHeight(); + public int getHeight() { return height; } /** Returns the border in pixels of the texture data. */ - public int getBorder(); + public int getBorder() { return border; } /** Returns the intended OpenGL pixel format of the texture data. */ - public int getPixelFormat(); + public int getPixelFormat() { + return pixelFormat; + } /** Returns the intended OpenGL pixel type of the texture data. */ - public int getPixelType(); + public int getPixelType() { + return pixelType; + } /** Returns the intended OpenGL internal format of the texture data. */ - public int getInternalFormat(); + public int getInternalFormat() { return internalFormat; } /** Returns whether mipmaps should be generated for the texture data. */ - public boolean getMipmap(); + public boolean getMipmap() { return mipmap; } /** Indicates whether the texture data is in compressed form. */ - public boolean isDataCompressed(); + public boolean isDataCompressed() { return dataIsCompressed; } /** Indicates whether the texture coordinates must be flipped vertically for proper display. */ - public boolean getMustFlipVertically(); + public boolean getMustFlipVertically() { return mustFlipVertically; } /** Returns the texture data, or null if it is specified as a set of mipmaps. */ - public Buffer getBuffer(); + public Buffer getBuffer() { + return buffer; + } /** Returns all mipmap levels for the texture data, or null if it is specified as a single image. */ - public Buffer[] getMipmapData(); + public Buffer[] getMipmapData() { return mipmapData; } /** Returns the required byte alignment for the texture data. */ - public int getAlignment(); + public int getAlignment() { return alignment; } /** Returns the row length needed for correct GL_UNPACK_ROW_LENGTH specification. This is currently only supported for non-mipmapped, non-compressed textures. */ - public int getRowLength(); + public int getRowLength() { return rowLength; } /** Sets the width in pixels of the texture data. */ - public void setWidth(int width); + public void setWidth(int width) { this.width = width; } /** Sets the height in pixels of the texture data. */ - public void setHeight(int height); + public void setHeight(int height) { this.height = height; } /** Sets the border in pixels of the texture data. */ - public void setBorder(int border); + public void setBorder(int border) { this.border = border; } /** Sets the intended OpenGL pixel format of the texture data. */ - public void setPixelFormat(int pixelFormat); + public void setPixelFormat(int pixelFormat) { this.pixelFormat = pixelFormat; } /** Sets the intended OpenGL pixel type of the texture data. */ - public void setPixelType(int pixelType); + public void setPixelType(int pixelType) { this.pixelType = pixelType; } /** Sets the intended OpenGL internal format of the texture data. */ - public void setInternalFormat(int internalFormat); + public void setInternalFormat(int internalFormat) { this.internalFormat = internalFormat; } /** Sets whether mipmaps should be generated for the texture data. */ - public void setMipmap(boolean mipmap); + public void setMipmap(boolean mipmap) { this.mipmap = mipmap; } /** Sets whether the texture data is in compressed form. */ - public void setIsDataCompressed(boolean compressed); + public void setIsDataCompressed(boolean compressed) { this.dataIsCompressed = compressed; } /** Sets whether the texture coordinates must be flipped vertically for proper display. */ - public void setMustFlipVertically(boolean mustFlipVertically); + public void setMustFlipVertically(boolean mustFlipVertically) { this.mustFlipVertically = mustFlipVertically; } /** Sets the texture data. */ - public void setBuffer(Buffer buffer); + public void setBuffer(Buffer buffer) { + this.buffer = buffer; + estimatedMemorySize = estimatedMemorySize(buffer); + } /** Sets the required byte alignment for the texture data. */ - public void setAlignment(int alignment) ; + public void setAlignment(int alignment) { this.alignment = alignment; } /** Sets the row length needed for correct GL_UNPACK_ROW_LENGTH specification. This is currently only supported for non-mipmapped, non-compressed textures. */ - public void setRowLength(int rowLength) ; + public void setRowLength(int rowLength) { this.rowLength = rowLength; } /** Indicates to this TextureData whether the GL_EXT_abgr extension is available. Used for optimization along some code paths to avoid data copies. */ - public void setHaveEXTABGR(boolean haveEXTABGR) ; + public void setHaveEXTABGR(boolean haveEXTABGR) { + this.haveEXTABGR = haveEXTABGR; + } /** Indicates to this TextureData whether OpenGL version 1.2 is available. If not, falls back to relatively inefficient code paths for several input data types (several kinds of packed pixel formats, in particular). */ - public void setHaveGL12(boolean haveGL12) ; + public void setHaveGL12(boolean haveGL12) { + this.haveGL12 = haveGL12; + } /** Returns an estimate of the amount of memory in bytes this TextureData will consume once uploaded to the graphics card. It should only be treated as an estimate; most applications should not need to query this but instead let the OpenGL implementation page textures in and out as necessary. */ - public int getEstimatedMemorySize() ; + public int getEstimatedMemorySize() { + return estimatedMemorySize; + } /** Flushes resources associated with this TextureData by calling Flusher.flush(). */ - public void flush() ; + public void flush() { + if (flusher != null) { + flusher.flush(); + flusher = null; + } + } /** Defines a callback mechanism to allow the user to explicitly deallocate native resources (memory-mapped files, etc.) @@ -139,4 +318,26 @@ public interface TextureData { TextureData. */ public void flush(); } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + protected int estimatedMemorySize(Buffer buffer) { + if (buffer == null) { + return 0; + } + int capacity = buffer.capacity(); + if (buffer instanceof ByteBuffer) { + return capacity; + } else if (buffer instanceof IntBuffer) { + return capacity * BufferUtil.SIZEOF_INT; + } else if (buffer instanceof FloatBuffer) { + return capacity * BufferUtil.SIZEOF_FLOAT; + } else if (buffer instanceof ShortBuffer) { + return capacity * BufferUtil.SIZEOF_SHORT; + } + throw new RuntimeException("Unexpected buffer type " + + buffer.getClass().getName()); + } } |