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 int
Whether 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 int
Whether 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 int
Whether 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.sources
that are writable, ornull
if none.private static final int
Whether 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 thisComputedTiles
as a listener of all given sources. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Integer
void
dispose()
Invoked when theComputedImage
has been garbage-collected.(package private) final boolean
endWrite
(TileCache.Key key, boolean success) Decrements the count of writers for the specified tile.(package private) final boolean
getWritableTileIndices
(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 boolean
isTileDirty
(TileCache.Key key) Returnstrue
if the specified tile needs to be recomputed.(package private) final boolean
Returnstrue
if the specified tile is checked out for a write operation.private static boolean
isWritable
(Integer value) Returnstrue
if the given value isCOMPUTING
or a greater unsigned value.(package private) 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.(package private) final boolean
startWrite
(TileCache.Key key) Increments the count of writers for the specified tile.void
tileUpdate
(WritableRenderedImage source, int tileX, int tileY, boolean willBeWritable) Invoked when a source is changing the content of one of its tile.(package private) final boolean
private void
unregister
(WritableRenderedImage[] ws, int i, RuntimeException failure) Stops observing writable sources for modifications.Methods inherited from class java.lang.ref.Reference
clear, clone, enqueue, get, isEnqueued, reachabilityFence, refersTo
-
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 incachedTiles
map.VALID
means that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALID
if the tile has been garbage-collected after it has been marked.DIRTY
means that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERROR
means 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.
COMPUTING
is 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 incachedTiles
map.VALID
means that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALID
if the tile has been garbage-collected after it has been marked.DIRTY
means that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERROR
means 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.
COMPUTING
is 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 incachedTiles
map.VALID
means that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALID
if the tile has been garbage-collected after it has been marked.DIRTY
means that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERROR
means 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.
COMPUTING
is 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 incachedTiles
map.VALID
means that the tile, if presents, is ready for use. A tile may be non-existent in the cache despite being markedVALID
if the tile has been garbage-collected after it has been marked.DIRTY
means that the tile needs to be recomputed. If the tile is present, its data should be discarded but its storage space will be reused.ERROR
means 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.
COMPUTING
is a special case of above point when calculation just started.
- See Also:
-
cachedTiles
-
sources
AllComputedImage.sources
that are writable, ornull
if none. This is used for removing tile observers when theComputedImage
is garbage-collected.
-
-
Constructor Details
-
ComputedTiles
ComputedTiles(ComputedImage image, WritableRenderedImage[] ws) Creates a new weak reference to the given image and registers thisComputedTiles
as a listener of all given sources. The listeners will be automatically removed when theComputedImage
is garbage collected.- Parameters:
image
- the image for which to release tiles on garbage-collection.ws
- sources to observe for changes, ornull
if none.
-
-
Method Details
-
isWritable
-
isTileWritable
Returnstrue
if 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
Returnstrue
if the specified tile needs to be recomputed. An absent tile is considered as dirty. If previous attempt to compute the tile failed, then anImagingOpException
is 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 toCOMPUTING
and 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:
true
if 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:
true
if 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, ornull
if 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
-false
for marking valid tiles as dirty, ortrue
for marking tiles in error.- Returns:
true
if 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:
tileUpdate
in 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 theComputedImage
has 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 thisComputedTiles
is not registered as aTileObserver
and ifTileCache.GLOBAL
does 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:
dispose
in interfaceDisposable
- See Also:
-
unregister
Stops observing writable sources for modifications. This method is invoked when theComputedImage
is garbage collected. It may also be invoked for rolling back observer registrations if an error occurred duringComputedTiles
construction. This method clears thesources
field immediately for allowing the garbage collector to release the sources in the event where thisComputedTiles
would 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.
-