Package org.apache.sis.image
Class ComputedTiles
- All Implemented Interfaces:
TileObserver,Disposable
Weak reference to a
ComputedImage image together with information about tile status.
This class also contains necessary information for releasing resources when image is disposed.
This class shall not contain any strong reference to the ComputedImage.
Despite the ComputedTiles class name, this class does not contain any reference
to the tiles. Instead, it contains keys for getting the tiles from TileCache.GLOBAL.
Consequently, this class "contains" the tiles only indirectly.
- Since:
- 1.1
- Version:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<TileCache.Key,Integer> Indices of all cached tiles.private static final intWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data.private static final intWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data.private static final intWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data.private WritableRenderedImage[]AllComputedImage.sourcesthat are writable, ornullif none.private static final intWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data. -
Constructor Summary
ConstructorsConstructorDescriptionComputedTiles(ComputedImage image, WritableRenderedImage[] ws) Creates a new weak reference to the given image and registers thisComputedTilesas a listener of all given sources. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Integervoiddispose()Invoked when theComputedImagehas been garbage-collected.(package private) final booleanendWrite(TileCache.Key key, boolean success) Decrements the count of writers for the specified tile.(package private) final booleangetWritableTileIndices(List<Point> indices) Adds in the given list the indices of all tiles which are checked out for writing.private static Integer(package private) final booleanisTileDirty(TileCache.Key key) Returnstrueif the specified tile needs to be recomputed.(package private) final booleanReturnstrueif the specified tile is checked out for a write operation.private static booleanisWritable(Integer value) Returnstrueif the given value isCOMPUTINGor a greater unsigned value.(package private) final booleanmarkDirtyTiles(int minTileX, int minTileY, int maxTileX, int maxTileY, boolean error) Marks all tiles in the given range of indices as in need of being recomputed.(package private) final booleanstartWrite(TileCache.Key key) Increments the count of writers for the specified tile.voidtileUpdate(WritableRenderedImage source, int tileX, int tileY, boolean willBeWritable) Invoked when a source is changing the content of one of its tile.(package private) final booleanprivate voidunregister(WritableRenderedImage[] ws, int i, RuntimeException failure) Stops observing writable sources for modifications.Methods inherited from class java.lang.ref.Reference
clear, enqueue, get, isEnqueued
-
Field Details
-
VALID
private static final int VALIDWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data. Those values are stored incachedTilesmap.VALIDmeans that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALIDif the tile has been garbage-collected after it has been marked.DIRTYmeans that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERRORmeans that the previous attempt to compute this tile failed.- All other values means that the tile has been checkout out for a write operation. That value is incremented/decremented when the writable tile is acquired/released. Write operation status have precedence over the dirty state.
COMPUTINGis a special case of above point when calculation just started.
- See Also:
-
DIRTY
private static final int DIRTYWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data. Those values are stored incachedTilesmap.VALIDmeans that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALIDif the tile has been garbage-collected after it has been marked.DIRTYmeans that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERRORmeans that the previous attempt to compute this tile failed.- All other values means that the tile has been checkout out for a write operation. That value is incremented/decremented when the writable tile is acquired/released. Write operation status have precedence over the dirty state.
COMPUTINGis a special case of above point when calculation just started.
- See Also:
-
ERROR
private static final int ERRORWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data. Those values are stored incachedTilesmap.VALIDmeans that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALIDif the tile has been garbage-collected after it has been marked.DIRTYmeans that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERRORmeans that the previous attempt to compute this tile failed.- All other values means that the tile has been checkout out for a write operation. That value is incremented/decremented when the writable tile is acquired/released. Write operation status have precedence over the dirty state.
COMPUTINGis a special case of above point when calculation just started.
- See Also:
-
COMPUTING
private static final int COMPUTINGWhether a tile in the cache is ready for use or needs to be recomputed because one if its sources changed its data. Those values are stored incachedTilesmap.VALIDmeans that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALIDif the tile has been garbage-collected after it has been marked.DIRTYmeans that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERRORmeans that the previous attempt to compute this tile failed.- All other values means that the tile has been checkout out for a write operation. That value is incremented/decremented when the writable tile is acquired/released. Write operation status have precedence over the dirty state.
COMPUTINGis a special case of above point when calculation just started.
- See Also:
-
cachedTiles
-
sources
AllComputedImage.sourcesthat are writable, ornullif none. This is used for removing tile observers when theComputedImageis garbage-collected.
-
-
Constructor Details
-
ComputedTiles
ComputedTiles(ComputedImage image, WritableRenderedImage[] ws) Creates a new weak reference to the given image and registers thisComputedTilesas a listener of all given sources. The listeners will be automatically removed when theComputedImageis garbage collected.- Parameters:
image- the image for which to release tiles on garbage-collection.ws- sources to observe for changes, ornullif none.
-
-
Method Details
-
isWritable
-
isTileWritable
Returnstrueif the specified tile is checked out for a write operation.- Parameters:
key- indices of the tile to check.- Returns:
- whether the specified tile is checked out for a write operation.
-
isTileDirty
Returnstrueif the specified tile needs to be recomputed. An absent tile is considered as dirty. If previous attempt to compute the tile failed, then anImagingOpExceptionis thrown again.- Parameters:
key- indices of the tile to check.- Returns:
- whether the specified tile needs to be recomputed.
- Throws:
ImagingOpException- if we already tried and failed to compute the specified tile.
-
trySetComputing
If the specified tile is absent orDIRTY, sets its status toCOMPUTINGand returnstrue. Otherwise if there are no errors, does nothing and returnsfalse.- Parameters:
key- indices of the tile to compute if dirty.- Returns:
- whether the specified tile was absent or dirty.
- Throws:
ImagingOpException- if we already tried and failed to compute the specified tile.
-
startWrite
Increments the count of writers for the specified tile. If the specified tile was marked dirty or in error, that previous status is discarded.- Parameters:
key- indices of the tile to mark writable.- Returns:
trueif the tile goes from having no writers to having one writer.- Throws:
ArithmeticException- if too many writers.
-
endWrite
Decrements the count of writers for the specified tile.- Parameters:
key- indices of the tile which was marked writable.success- whether the operation should be considered successful.- Returns:
trueif the tile goes from having one writer to having no writers.
-
increment
- Parameters:
value- the value to increment.computing- must beCOMPUTING.- Returns:
- the incremented value.
-
decrement
-
getWritableTileIndices
Adds in the given list the indices of all tiles which are checked out for writing. If the given list isnull, then this method stops the search at the first writable tile.- Parameters:
indices- the list where to add indices, ornullif none.- Returns:
- whether at least one tile is checked out for writing.
-
markDirtyTiles
final boolean markDirtyTiles(int minTileX, int minTileY, int maxTileX, int maxTileY, boolean error) Marks all tiles in the given range of indices as in need of being recomputed. This method is invoked when some tiles of at least one source image changed. All arguments, including maximum values, are inclusive.- Parameters:
error-falsefor marking valid tiles as dirty, ortruefor marking tiles in error.- Returns:
trueif at least one tile got its status updated.- See Also:
-
tileUpdate
Invoked when a source is changing the content of one of its tile. This method is interested only in events fired after the change is done. The tiles that depend on the modified tile are marked in need to be recomputed.- Specified by:
tileUpdatein interfaceTileObserver- Parameters:
source- the image that own the tile which is about to be updated.tileX- the x index of the tile that is being updated.tileY- the y index of the tile that is being updated.willBeWritable- iftrue, the tile is grabbed for writing; otherwise it is being released.
-
dispose
public void dispose()Invoked when theComputedImagehas been garbage-collected. This method removes all cached tiles that were owned by the image and stops observing all sources. This method should not perform other cleaning work because it is not guaranteed to be invoked if thisComputedTilesis not registered as aTileObserverand ifTileCache.GLOBALdoes not contain any tile for theComputedImage. The reason is because there would be nothing preventing this weak reference to be garbage collected beforedispose()is invoked.- Specified by:
disposein interfaceDisposable- See Also:
-
unregister
Stops observing writable sources for modifications. This method is invoked when theComputedImageis garbage collected. It may also be invoked for rolling back observer registrations if an error occurred duringComputedTilesconstruction. This method clears thesourcesfield immediately for allowing the garbage collector to release the sources in the event where thisComputedTileswould live longer than expected.- Parameters:
ws- a copy ofsources. Cannot be null.i- index after the last source to stop observing.failure- if this method is invoked because an exception occurred, that exception.
-