void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) { SkIRect adjustedQuery = query; // The inset is to counteract the outset that was applied in 'insert' // The outset/inset is to optimize for lookups of size // 'tileInterval + 2 * margin' that are aligned with the tile grid. adjustedQuery.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); adjustedQuery.offset(fInfo.fOffset); adjustedQuery.sort(); // in case the inset inverted the rectangle // Convert the query rectangle from device coordinates to tile coordinates // by rounding outwards to the nearest tile boundary so that the resulting tile // region includes the query rectangle. (using truncating division to "floor") int tileStartX = adjustedQuery.left() / fInfo.fTileInterval.width(); int tileEndX = (adjustedQuery.right() + fInfo.fTileInterval.width() - 1) / fInfo.fTileInterval.width(); int tileStartY = adjustedQuery.top() / fInfo.fTileInterval.height(); int tileEndY = (adjustedQuery.bottom() + fInfo.fTileInterval.height() - 1) / fInfo.fTileInterval.height(); tileStartX = SkPin32(tileStartX, 0, fXTileCount - 1); tileEndX = SkPin32(tileEndX, tileStartX+1, fXTileCount); tileStartY = SkPin32(tileStartY, 0, fYTileCount - 1); tileEndY = SkPin32(tileEndY, tileStartY+1, fYTileCount); int queryTileCount = (tileEndX - tileStartX) * (tileEndY - tileStartY); SkASSERT(queryTileCount); if (queryTileCount == 1) { *results = this->tile(tileStartX, tileStartY); } else { results->reset(); SkTDArray<int> curPositions; curPositions.setCount(queryTileCount); // Note: Reserving space for 1024 tile pointers on the stack. If the // malloc becomes a bottleneck, we may consider increasing that number. // Typical large web page, say 2k x 16k, would require 512 tiles of // size 256 x 256 pixels. SkAutoSTArray<1024, SkTDArray<void *>*> storage(queryTileCount); SkTDArray<void *>** tileRange = storage.get(); int tile = 0; for (int x = tileStartX; x < tileEndX; ++x) { for (int y = tileStartY; y < tileEndY; ++y) { tileRange[tile] = &this->tile(x, y); curPositions[tile] = tileRange[tile]->count() ? 0 : kTileFinished; ++tile; } } void *nextElement; while(NULL != (nextElement = fNextDatumFunction(tileRange, curPositions))) { results->push(nextElement); } } }
void SkTileGrid::search(const SkIRect& query, SkTDArray<void*>* results) { SkIRect adjustedQuery = query; adjustedQuery.inset(fInfo.fMargin.width(), fInfo.fMargin.height()); adjustedQuery.offset(fInfo.fOffset); // Convert the query rectangle from device coordinates to tile coordinates // by rounding outwards to the nearest tile boundary so that the resulting tile // region includes the query rectangle. (using truncating division to "floor") int tileStartX = adjustedQuery.left() / fInfo.fTileInterval.width(); int tileEndX = (adjustedQuery.right() + fInfo.fTileInterval.width() - 1) / fInfo.fTileInterval.width(); int tileStartY = adjustedQuery.top() / fInfo.fTileInterval.height(); int tileEndY = (adjustedQuery.bottom() + fInfo.fTileInterval.height() - 1) / fInfo.fTileInterval.height(); if (tileStartX >= fXTileCount || tileStartY >= fYTileCount || tileEndX <= 0 || tileEndY <= 0) { return; // query does not intersect the grid } // clamp to grid if (tileStartX < 0) tileStartX = 0; if (tileStartY < 0) tileStartY = 0; if (tileEndX > fXTileCount) tileEndX = fXTileCount; if (tileEndY > fYTileCount) tileEndY = fYTileCount; int queryTileCount = (tileEndX - tileStartX) * (tileEndY - tileStartY); if (queryTileCount == 1) { *results = this->tile(tileStartX, tileStartY); } else { results->reset(); SkTDArray<int> curPositions; curPositions.setCount(queryTileCount); // Note: Reserving space for 1024 tile pointers on the stack. If the // malloc becomes a bottleneck, we may consider increasing that number. // Typical large web page, say 2k x 16k, would require 512 tiles of // size 256 x 256 pixels. SkAutoSTArray<1024, SkTDArray<void *>*> storage(queryTileCount); SkTDArray<void *>** tileRange = storage.get(); int tile = 0; for (int x = tileStartX; x < tileEndX; ++x) { for (int y = tileStartY; y < tileEndY; ++y) { tileRange[tile] = &this->tile(x, y); curPositions[tile] = tileRange[tile]->count() ? 0 : kTileFinished; ++tile; } } void *nextElement; while(NULL != (nextElement = fNextDatumFunction(tileRange, curPositions))) { results->push(nextElement); } } }