3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #ifndef SkTileGrid_DEFINED
10 #define SkTileGrid_DEFINED
12 #include "SkBBoxHierarchy.h"
13 #include "SkPictureStateTree.h"
14 #include "SkTileGridPicture.h" // for TileGridInfo
17 * Subclass of SkBBoxHierarchy that stores elements in buckets that correspond
18 * to tile regions, disposed in a regular grid. This is useful when the tile
19 * structure that will be use in search() calls is known prior to insertion.
20 * Calls to search will return in constant time.
22 * Note: Current implementation of search() only supports looking-up regions
23 * that are an exact match to a single tile. Implementation could be augmented
24 * to support arbitrary rectangles, but performance would be sub-optimal.
26 class SkTileGrid : public SkBBoxHierarchy {
29 // Number of tiles for which data is allocated on the stack in
30 // SkTileGrid::search. If malloc becomes a bottleneck, we may consider
31 // increasing this number. Typical large web page, say 2k x 16k, would
32 // require 512 tiles of size 256 x 256 pixels.
33 kStackAllocationTileCount = 1024
36 typedef void* (*SkTileGridNextDatumFunctionPtr)(SkTDArray<void*>** tileData, SkAutoSTArray<kStackAllocationTileCount, int>& tileIndices);
38 SkTileGrid(int xTileCount, int yTileCount, const SkTileGridPicture::TileGridInfo& info,
39 SkTileGridNextDatumFunctionPtr nextDatumFunction);
41 virtual ~SkTileGrid();
44 * Insert a data pointer and corresponding bounding box
45 * @param data The data pointer, may be NULL
46 * @param bounds The bounding box, should not be empty
47 * @param defer Ignored, TileArray does not defer insertions
49 virtual void insert(void* data, const SkIRect& bounds, bool) SK_OVERRIDE;
51 virtual void flushDeferredInserts() SK_OVERRIDE {};
54 * Populate 'results' with data pointers corresponding to bounding boxes that intersect 'query'
55 * The query argument is expected to be an exact match to a tile of the grid
57 virtual void search(const SkIRect& query, SkTDArray<void*>* results) SK_OVERRIDE;
59 virtual void clear() SK_OVERRIDE;
62 * Gets the number of insertions
64 virtual int getCount() const SK_OVERRIDE;
66 virtual int getDepth() const SK_OVERRIDE { return -1; }
68 virtual void rewindInserts() SK_OVERRIDE;
70 // Used by search() and in SkTileGridHelper implementations
75 int tileCount(int x, int y); // For testing only.
78 SkTDArray<void*>& tile(int x, int y);
80 int fXTileCount, fYTileCount, fTileCount;
81 SkTileGridPicture::TileGridInfo fInfo;
82 SkTDArray<void*>* fTileData;
85 SkTileGridNextDatumFunctionPtr fNextDatumFunction;
87 typedef SkBBoxHierarchy INHERITED;
91 * Generic implementation for SkTileGridNextDatumFunctionPtr. user code may instantiate
92 * this template to get a valid SkTileGridNextDatumFunction implementation
94 * Returns the next element of tileData[i][tileIndices[i]] for all i and advances
95 * tileIndices[] past them. The order in which data are returned by successive
96 * calls to this method must reflect the order in which the were originally
97 * recorded into the tile grid.
99 * \param tileData array of pointers to arrays of tile data
100 * \param tileIndices per-tile data indices, indices are incremented for tiles that contain
102 * \tparam T a type to which it is safe to cast a datum and that has an operator <
103 * such that 'a < b' is true if 'a' was inserted into the tile grid before 'b'.
105 template <typename T>
106 void* SkTileGridNextDatum(SkTDArray<void*>** tileData, SkAutoSTArray<SkTileGrid::kStackAllocationTileCount, int>& tileIndices) {
108 int tileCount = tileIndices.count();
109 int minIndex = tileCount;
111 // Find the next Datum; track where it's found so we reduce the size of the second loop.
112 for (int tile = 0; tile < tileCount; ++tile) {
113 int pos = tileIndices[tile];
114 if (pos != SkTileGrid::kTileFinished) {
115 T* candidate = (T*)(*tileData[tile])[pos];
116 if (NULL == minVal || (*candidate) < (*minVal)) {
120 } else if (!((*minVal) < (*candidate))) {
121 // We don't require operator==; if !(candidate<minVal) && !(minVal<candidate),
122 // candidate==minVal and we have to add this tile to the range searched.
127 // Increment indices past the next datum
128 if (minVal != NULL) {
129 for (int tile = minIndex; tile <= maxIndex; ++tile) {
130 int pos = tileIndices[tile];
131 if (pos != SkTileGrid::kTileFinished && (*tileData[tile])[pos] == minVal) {
132 if (++(tileIndices[tile]) >= tileData[tile]->count()) {
133 tileIndices[tile] = SkTileGrid::kTileFinished;