Class LazilyLoadedTexture
- java.lang.Object
-
- gov.nasa.worldwind.avlist.AVListImpl
-
- gov.nasa.worldwind.render.LazilyLoadedTexture
-
- Direct Known Subclasses:
AbstractTacticalSymbol.IconTexture
public class LazilyLoadedTexture extends AVListImpl implements WWTexture
Represents a texture derived from a lazily loaded image source such as an image file or aBufferedImage.The interface contains a method,
isTextureInitializationFailed()to determine whether the instance failed to convert an image source to a texture. If such a failure occurs, the method returns true and no further attempts are made to create the texture.This class performs lazy retrieval and loading of an image source, attempting to retrieve and load the image source only when the
bind(DrawContext)orapplyInternalTransform(DrawContext)methods are called. If the image source is aBufferedImagethe associatedTextureobject is created and available immediately whenbindorapplyInternalTransformare called. If the image source is a local file or remote stream (URL), retrieval and loading is performed on a separate thread from the EDT.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classLazilyLoadedTexture.RequestTaskAttempts to find this texture's image file locally, and if that fails attempts to find it remotely.
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.ObjectfileLockProvides a semaphore to synchronize access to the texture file if duplicate request tasks are active.protected FileStorefileStoreIdentifies theFileStoreof the supporting file cache for this model.protected booleanhasMipmapDataIndicates whether the image read from the image source has mip-map data.protected java.lang.IntegerheightThe texture height, if the height is known.protected java.lang.ObjectimageSourceThe original image source specified at construction.protected java.beans.PropertyChangeListenerlistenerThe object to notify when an image is eventually loaded in memory.protected com.jogamp.opengl.util.texture.TextureCoordstexCoordsThe texture's texture coordinates, as determined by JOGL when the texture is created.protected com.jogamp.opengl.util.texture.TextureDatatextureDataThe texture data created as the image source is read.protected booleantextureInitializationFailedIndicates that texture initialization failed.protected booleanuseAnisotropyThe current anisotropy flag.protected booleanuseMipMapsThe mip-map flag specified at construction.protected java.lang.IntegerwidthThe texture width, if the width is known.
-
Constructor Summary
Constructors Constructor Description LazilyLoadedTexture(java.lang.Object imageSource)Constructs a texture object for a specified image source.LazilyLoadedTexture(java.lang.Object imageSource, boolean useMipMaps)Constructs a texture object for a specified image source.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapplyInternalTransform(DrawContext dc)Applies any necessary transformations to the texture prior to its being rendered.booleanbind(DrawContext dc)Binds this instance'sTextureto theGLContextif the texture has been created, otherwise initiates image source retrieval and texture creation in a separate thread.protected java.lang.RunnablecreateRequestTask()Returns an object that implements the Runnable interface, and who'srunmethod retrieves and loads this texture's image source.booleanequals(java.lang.Object o)intgetHeight()Indicates the texture's height, which is the same as the image source's height.intgetHeight(DrawContext dc)Returns the texture's heightjava.lang.ObjectgetImageSource()Returns the texture's image source.com.jogamp.opengl.util.texture.TextureCoordsgetTexCoords()Returns the texture's texture coordinates, which may be other than [0,0],[1,1] if the texture size is not a power of two or the texture must be flipped when rendered.protected com.jogamp.opengl.util.texture.TexturegetTexture(DrawContext dc)Returns theTextureassociated with this instance.protected com.jogamp.opengl.util.texture.TextureDatagetTextureData()Returns this texture's texture data if it has been retrieved but aTexturehas not yet been created for it.intgetWidth()Indicates the texture's width, which is the same as the image source's width.intgetWidth(DrawContext dc)Returns the texture's width.inthashCode()protected voidinitialize(java.lang.Object imageSource, boolean useMipMaps, java.beans.PropertyChangeListener listener)Initializes this object's fields during construction.protected booleanisBufferedImageSource()Indicates whether the image source is aBufferedImagebooleanisTextureCurrent(DrawContext dc)Indicates whether the texture is currently available for use without regenerating it from its image source.booleanisTextureInitializationFailed()Indicates whether an attempt was made to retrieve and read the texture but it failed.booleanisUseAnisotropy()Indicates whether texture anisotropy is applied to the texture when rendered.booleanisUseMipMaps()Indicates whether the texture should use mip-maps.protected booleanloadTextureData(java.net.URL fileUrl)Loads the image from disk into memory.protected com.jogamp.opengl.util.texture.TexturemakeBufferedImageTexture(DrawContext dc)Creates this instance'sTextureif the image source is aBufferedImage.protected com.jogamp.opengl.util.texture.TexturemakeTextureFromTextureData(DrawContext dc)Creates aTexturefrom this instance'sTextureDataif theTextureDataexists.protected voidnotifyTextureLoaded()protected com.jogamp.opengl.util.texture.TextureDatareadImage(java.net.URL fileUrl)Reads and returns aTextureDatafor an image from a specified file URL.protected com.jogamp.opengl.util.texture.TexturerequestTexture(DrawContext dc)If this instance's image source is aBufferedImage, creates and returns the texture, otherwise creates a task in a separate thread to retrieve it from its local or remote location.protected voidsetTextureData(com.jogamp.opengl.util.texture.TextureData textureData)Specifies texture data for the tile.protected voidsetTextureParameters(DrawContext dc, com.jogamp.opengl.util.texture.Texture texture)Sets a specified texture's OpenGLTextureparameters.voidsetUseAnisotropy(boolean useAnisotropy)Specifies whether texture anisotropy is applied to the texture when rendered.-
Methods inherited from class gov.nasa.worldwind.avlist.AVListImpl
addPropertyChangeListener, addPropertyChangeListener, clearList, copy, firePropertyChange, firePropertyChange, getBooleanValue, getBooleanValue, getChangeSupport, getDoubleValue, getDoubleValue, getEntries, getIntegerValue, getIntegerValue, getLongValue, getLongValue, getRestorableStateForAVPair, getStringValue, getStringValue, getStringValue, getValue, getValues, hasKey, removeKey, removePropertyChangeListener, removePropertyChangeListener, setValue, setValues
-
-
-
-
Field Detail
-
imageSource
protected java.lang.Object imageSource
The original image source specified at construction.
-
useMipMaps
protected boolean useMipMaps
The mip-map flag specified at construction.
-
useAnisotropy
protected boolean useAnisotropy
The current anisotropy flag.
-
width
protected java.lang.Integer width
The texture width, if the width is known. Otherwise it's -1.
-
height
protected java.lang.Integer height
The texture height, if the height is known. Otherwise it's -1.
-
texCoords
protected com.jogamp.opengl.util.texture.TextureCoords texCoords
The texture's texture coordinates, as determined by JOGL when the texture is created.
-
textureData
protected volatile com.jogamp.opengl.util.texture.TextureData textureData
The texture data created as the image source is read. It's removed - set to null - once the textures is fully created. This intermediate texture data is necessary because the image source is read in a non-EDT thread. This field isvolatilein order to synchronize atomic access among threads. This field is not used if the image source isBufferedImage.
-
textureInitializationFailed
protected boolean textureInitializationFailed
Indicates that texture initialization failed. This texture should not be used if true.
-
hasMipmapData
protected boolean hasMipmapData
Indicates whether the image read from the image source has mip-map data.
-
fileStore
protected FileStore fileStore
Identifies theFileStoreof the supporting file cache for this model.
-
fileLock
protected final java.lang.Object fileLock
Provides a semaphore to synchronize access to the texture file if duplicate request tasks are active.
-
listener
protected java.beans.PropertyChangeListener listener
The object to notify when an image is eventually loaded in memory. The current layer at the time the image source is requested is assigned to this field.
-
-
Constructor Detail
-
LazilyLoadedTexture
public LazilyLoadedTexture(java.lang.Object imageSource)
Constructs a texture object for a specified image source. Requests that mip-maps be used.- Parameters:
imageSource- the source of the image, either a file pathStringor aBufferedImage.- Throws:
java.lang.IllegalArgumentException- if theimageSourceis null.
-
LazilyLoadedTexture
public LazilyLoadedTexture(java.lang.Object imageSource, boolean useMipMaps)Constructs a texture object for a specified image source.- Parameters:
imageSource- the source of the image, either a file pathStringor aBufferedImage.useMipMaps- Indicates whether to generate and use mip-maps for the image.- Throws:
java.lang.IllegalArgumentException- if theimageSourceis null.
-
-
Method Detail
-
initialize
protected void initialize(java.lang.Object imageSource, boolean useMipMaps, java.beans.PropertyChangeListener listener)Initializes this object's fields during construction.- Parameters:
imageSource- the image source.useMipMaps- the mip-map flag.listener- the change listener.- Throws:
java.lang.IllegalArgumentException- if the image source is null.
-
getImageSource
public java.lang.Object getImageSource()
Description copied from interface:WWTextureReturns the texture's image source.- Specified by:
getImageSourcein interfaceWWTexture- Returns:
- the texture's image source.
-
isBufferedImageSource
protected boolean isBufferedImageSource()
Indicates whether the image source is aBufferedImage- Returns:
- true if the image source is a
BufferedImage, otherwise false.
-
getWidth
public int getWidth()
Indicates the texture's width, which is the same as the image source's width. The width is known only after the image source has been retrieved from disk or network and read asTextureData. It's value is -1 until then.- Returns:
- the texture's width if the texture has been retrieved, otherwise -1.
-
getHeight
public int getHeight()
Indicates the texture's height, which is the same as the image source's height. The height is known only after the image source has been retrieved from disk or network and read asTextureData. It's value is -1 until then.- Returns:
- the texture's height if the texture has been retrieved, otherwise -1.
-
getWidth
public int getWidth(DrawContext dc)
Returns the texture's width.This method behaves identically to
getWidth(). TheDrawContextargument is not used.
-
getHeight
public int getHeight(DrawContext dc)
Returns the texture's heightThis method behaves identically to
getHeight(). TheDrawContextargument is not used.
-
isUseMipMaps
public boolean isUseMipMaps()
Indicates whether the texture should use mip-maps. If they are not available in the source image they are created.- Returns:
- true if mip-maps are used, false if not.
-
getTexCoords
public com.jogamp.opengl.util.texture.TextureCoords getTexCoords()
Description copied from interface:WWTextureReturns the texture's texture coordinates, which may be other than [0,0],[1,1] if the texture size is not a power of two or the texture must be flipped when rendered.- Specified by:
getTexCoordsin interfaceWWTexture- Returns:
- returns the texture's texture coordinates.
-
isTextureCurrent
public boolean isTextureCurrent(DrawContext dc)
Description copied from interface:WWTextureIndicates whether the texture is currently available for use without regenerating it from its image source.- Specified by:
isTextureCurrentin interfaceWWTexture- Parameters:
dc- the current draw context- Returns:
- true if the texture is available and consistent with its image source, otherwise false.
-
isUseAnisotropy
public boolean isUseAnisotropy()
Indicates whether texture anisotropy is applied to the texture when rendered.- Returns:
- useAnisotropy true if anisotropy is to be applied, otherwise false.
-
setUseAnisotropy
public void setUseAnisotropy(boolean useAnisotropy)
Specifies whether texture anisotropy is applied to the texture when rendered.- Parameters:
useAnisotropy- true if anisotropy is to be applied, otherwise false.
-
isTextureInitializationFailed
public boolean isTextureInitializationFailed()
Indicates whether an attempt was made to retrieve and read the texture but it failed. If this flag is true, this texture should not be used.- Specified by:
isTextureInitializationFailedin interfaceWWTexture- Returns:
- true if texture retrieval or creation failed, otherwise true, even if the image source has not yet been retrieved.
-
getTexture
protected com.jogamp.opengl.util.texture.Texture getTexture(DrawContext dc)
Returns theTextureassociated with this instance.- Parameters:
dc- the current draw context.- Returns:
- this instance's texture, or null if the texture does not currently exist.
-
getTextureData
protected com.jogamp.opengl.util.texture.TextureData getTextureData()
Returns this texture's texture data if it has been retrieved but aTexturehas not yet been created for it.If this object's texture data field is non-null, a new texture is created from the texture data when the tile is next bound or otherwise initialized. This object's texture data field is then set to null.
- Returns:
- the texture data, which may be null indicating that the image source has not been read or that a texture has been created.
-
setTextureData
protected void setTextureData(com.jogamp.opengl.util.texture.TextureData textureData)
Specifies texture data for the tile. If texture data is non-null, a new texture is created from the texture data when the tile is next bound.When a texture is created from the texture data, the texture data field is set to null to indicate that the data has been converted to a texture and its resources may be released.
- Parameters:
textureData- the texture data, which may be null.
-
bind
public boolean bind(DrawContext dc)
Binds this instance'sTextureto theGLContextif the texture has been created, otherwise initiates image source retrieval and texture creation in a separate thread.
-
applyInternalTransform
public void applyInternalTransform(DrawContext dc)
Description copied from interface:WWTextureApplies any necessary transformations to the texture prior to its being rendered. A common transformation is mapping texture coordinates from a flipped or non-square state to conventionally oriented OpenGL values.- Specified by:
applyInternalTransformin interfaceWWTexture- Parameters:
dc- the current draw context.
-
requestTexture
protected com.jogamp.opengl.util.texture.Texture requestTexture(DrawContext dc)
If this instance's image source is aBufferedImage, creates and returns the texture, otherwise creates a task in a separate thread to retrieve it from its local or remote location.- Parameters:
dc- the current draw context.- Returns:
- the new texture, or null if the texture is not yet available.
-
createRequestTask
protected java.lang.Runnable createRequestTask()
Returns an object that implements the Runnable interface, and who'srunmethod retrieves and loads this texture's image source.- Returns:
- a new request task that retrieves and loads this texture's image source.
-
makeBufferedImageTexture
protected com.jogamp.opengl.util.texture.Texture makeBufferedImageTexture(DrawContext dc)
Creates this instance'sTextureif the image source is aBufferedImage.- Parameters:
dc- the current draw context.- Returns:
- the newly created texture, or null if the texture was not created.
- Throws:
java.lang.IllegalStateException- if the image source is null or not aBufferedImage.
-
makeTextureFromTextureData
protected com.jogamp.opengl.util.texture.Texture makeTextureFromTextureData(DrawContext dc)
Creates aTexturefrom this instance'sTextureDataif theTextureDataexists.- Parameters:
dc- the current draw context.- Returns:
- the newly created texture, or null if this instance has no current
TextureDataor if texture creation failed.
-
setTextureParameters
protected void setTextureParameters(DrawContext dc, com.jogamp.opengl.util.texture.Texture texture)
Sets a specified texture's OpenGLTextureparameters.- Parameters:
dc- the current draw context.texture- the texture whose parameters to set.
-
notifyTextureLoaded
protected void notifyTextureLoaded()
-
loadTextureData
protected boolean loadTextureData(java.net.URL fileUrl)
Loads the image from disk into memory. If successful, texture data is created and available viagetTextureData().- Parameters:
fileUrl- the URL of the image file.- Returns:
- true if the image was successfully loaded, otherwise false.
-
readImage
protected com.jogamp.opengl.util.texture.TextureData readImage(java.net.URL fileUrl)
Reads and returns aTextureDatafor an image from a specified file URL.- Parameters:
fileUrl- the URL of the image file to read.- Returns:
- a
TextureDatainstance for the image.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-