De-parameterize SkNextDatumFunction.

Just a simple refactor to make it clear we're only using this
one method and this one type.

BUG=skia:
R=robertphillips@google.com, mtklein@google.com

Author: mtklein@chromium.org

Review URL: https://codereview.chromium.org/465523002
This commit is contained in:
mtklein 2014-08-11 10:09:34 -07:00 committed by Commit bot
parent 81cc04d0a6
commit 534cc4c569
4 changed files with 43 additions and 69 deletions

View File

@ -6,7 +6,6 @@
*/
#include "SkBBHFactory.h"
#include "SkPictureStateTree.h"
#include "SkQuadTree.h"
#include "SkRTree.h"
#include "SkTileGrid.h"
@ -39,6 +38,5 @@ SkBBoxHierarchy* SkTileGridFactory::operator()(int width, int height) const {
// "-1"s below.
int xTileCount = (width + fInfo.fTileInterval.width() - 1) / fInfo.fTileInterval.width();
int yTileCount = (height + fInfo.fTileInterval.height() - 1) / fInfo.fTileInterval.height();
return SkNEW_ARGS(SkTileGrid, (xTileCount, yTileCount, fInfo,
SkTileGridNextDatum<SkPictureStateTree::Draw>));
return SkNEW_ARGS(SkTileGrid, (xTileCount, yTileCount, fInfo));
}

View File

@ -7,9 +7,9 @@
*/
#include "SkTileGrid.h"
#include "SkPictureStateTree.h"
SkTileGrid::SkTileGrid(int xTileCount, int yTileCount, const SkTileGridFactory::TileGridInfo& info,
SkTileGridNextDatumFunctionPtr nextDatumFunction) {
SkTileGrid::SkTileGrid(int xTileCount, int yTileCount, const SkTileGridFactory::TileGridInfo& info) {
fXTileCount = xTileCount;
fYTileCount = yTileCount;
fInfo = info;
@ -21,7 +21,6 @@ SkTileGrid::SkTileGrid(int xTileCount, int yTileCount, const SkTileGridFactory::
fInsertionCount = 0;
fGridBounds = SkIRect::MakeXYWH(0, 0, fInfo.fTileInterval.width() * fXTileCount,
fInfo.fTileInterval.height() * fYTileCount);
fNextDatumFunction = nextDatumFunction;
fTileData = SkNEW_ARRAY(SkTDArray<void *>, fTileCount);
}
@ -69,6 +68,43 @@ void SkTileGrid::insert(void* data, const SkIRect& bounds, bool) {
fInsertionCount++;
}
static void* next_datum(const SkTDArray<void*>** tileData,
SkAutoSTArray<SkTileGrid::kStackAllocationTileCount, int>& tileIndices) {
SkPictureStateTree::Draw* minVal = NULL;
int tileCount = tileIndices.count();
int minIndex = tileCount;
int maxIndex = 0;
// Find the next Datum; track where it's found so we reduce the size of the second loop.
for (int tile = 0; tile < tileCount; ++tile) {
int pos = tileIndices[tile];
if (pos != SkTileGrid::kTileFinished) {
SkPictureStateTree::Draw* candidate = (SkPictureStateTree::Draw*)(*tileData[tile])[pos];
if (NULL == minVal || (*candidate) < (*minVal)) {
minVal = candidate;
minIndex = tile;
maxIndex = tile;
} else if (!((*minVal) < (*candidate))) {
// We don't require operator==; if !(candidate<minVal) && !(minVal<candidate),
// candidate==minVal and we have to add this tile to the range searched.
maxIndex = tile;
}
}
}
// Increment indices past the next datum
if (minVal != NULL) {
for (int tile = minIndex; tile <= maxIndex; ++tile) {
int pos = tileIndices[tile];
if (pos != SkTileGrid::kTileFinished && (*tileData[tile])[pos] == minVal) {
if (++(tileIndices[tile]) >= tileData[tile]->count()) {
tileIndices[tile] = SkTileGrid::kTileFinished;
}
}
}
return minVal;
}
return NULL;
}
void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) const {
SkIRect adjustedQuery = query;
// The inset is to counteract the outset that was applied in 'insert'
@ -109,8 +145,7 @@ void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) const {
++tile;
}
}
void *nextElement;
while(NULL != (nextElement = fNextDatumFunction(tileRange, curPositions))) {
while(void* nextElement = next_datum(tileRange, curPositions)) {
results->push(nextElement);
}
}

View File

@ -33,12 +33,7 @@ public:
kStackAllocationTileCount = 1024
};
typedef void* (*SkTileGridNextDatumFunctionPtr)(
const SkTDArray<void*>** tileData,
SkAutoSTArray<kStackAllocationTileCount, int>& tileIndices);
SkTileGrid(int xTileCount, int yTileCount, const SkTileGridFactory::TileGridInfo& info,
SkTileGridNextDatumFunctionPtr nextDatumFunction);
SkTileGrid(int xTileCount, int yTileCount, const SkTileGridFactory::TileGridInfo& info);
virtual ~SkTileGrid();
@ -85,62 +80,8 @@ private:
SkTDArray<void*>* fTileData;
int fInsertionCount;
SkIRect fGridBounds;
SkTileGridNextDatumFunctionPtr fNextDatumFunction;
typedef SkBBoxHierarchy INHERITED;
};
/**
* Generic implementation for SkTileGridNextDatumFunctionPtr. user code may instantiate
* this template to get a valid SkTileGridNextDatumFunction implementation
*
* Returns the next element of tileData[i][tileIndices[i]] for all i and advances
* tileIndices[] past them. The order in which data are returned by successive
* calls to this method must reflect the order in which the were originally
* recorded into the tile grid.
*
* \param tileData array of pointers to arrays of tile data
* \param tileIndices per-tile data indices, indices are incremented for tiles that contain
* the next datum.
* \tparam T a type to which it is safe to cast a datum and that has an operator <
* such that 'a < b' is true if 'a' was inserted into the tile grid before 'b'.
*/
template <typename T>
void* SkTileGridNextDatum(const SkTDArray<void*>** tileData,
SkAutoSTArray<SkTileGrid::kStackAllocationTileCount, int>& tileIndices) {
T* minVal = NULL;
int tileCount = tileIndices.count();
int minIndex = tileCount;
int maxIndex = 0;
// Find the next Datum; track where it's found so we reduce the size of the second loop.
for (int tile = 0; tile < tileCount; ++tile) {
int pos = tileIndices[tile];
if (pos != SkTileGrid::kTileFinished) {
T* candidate = (T*)(*tileData[tile])[pos];
if (NULL == minVal || (*candidate) < (*minVal)) {
minVal = candidate;
minIndex = tile;
maxIndex = tile;
} else if (!((*minVal) < (*candidate))) {
// We don't require operator==; if !(candidate<minVal) && !(minVal<candidate),
// candidate==minVal and we have to add this tile to the range searched.
maxIndex = tile;
}
}
}
// Increment indices past the next datum
if (minVal != NULL) {
for (int tile = minIndex; tile <= maxIndex; ++tile) {
int pos = tileIndices[tile];
if (pos != SkTileGrid::kTileFinished && (*tileData[tile])[pos] == minVal) {
if (++(tileIndices[tile]) >= tileData[tile]->count()) {
tileIndices[tile] = SkTileGrid::kTileFinished;
}
}
}
return minVal;
}
return NULL;
}
#endif

View File

@ -37,7 +37,7 @@ static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect,
info.fMargin.set(borderPixels, borderPixels);
info.fOffset.setZero();
info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels);
SkTileGrid grid(2, 2, info, NULL);
SkTileGrid grid(2, 2, info);
grid.insert(NULL, rect, false);
REPORTER_ASSERT(reporter, grid.tileCount(0, 0) ==
((tileMask & kTopLeft_Tile)? 1 : 0));