2012-11-07 21:38:38 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2013-08-29 15:54:56 +04:00
|
|
|
#include "SkCanvas.h"
|
2014-04-18 22:04:41 +04:00
|
|
|
#include "SkPictureRecorder.h"
|
2012-11-07 21:38:38 +04:00
|
|
|
#include "SkTileGrid.h"
|
2014-01-25 00:56:26 +04:00
|
|
|
#include "Test.h"
|
2012-11-07 21:38:38 +04:00
|
|
|
|
|
|
|
enum Tile {
|
|
|
|
kTopLeft_Tile = 0x1,
|
|
|
|
kTopRight_Tile = 0x2,
|
|
|
|
kBottomLeft_Tile = 0x4,
|
|
|
|
kBottomRight_Tile = 0x8,
|
|
|
|
|
|
|
|
kAll_Tile = kTopLeft_Tile | kTopRight_Tile | kBottomLeft_Tile | kBottomRight_Tile,
|
|
|
|
};
|
|
|
|
|
2014-03-28 21:57:14 +04:00
|
|
|
class MockCanvas : public SkCanvas {
|
|
|
|
public:
|
|
|
|
MockCanvas(const SkBitmap& bm) : SkCanvas(bm) {}
|
|
|
|
|
|
|
|
virtual void drawRect(const SkRect& rect, const SkPaint&) {
|
|
|
|
// This capture occurs before quick reject.
|
|
|
|
fRects.push(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTDArray<SkRect> fRects;
|
|
|
|
};
|
|
|
|
|
2014-08-27 21:39:42 +04:00
|
|
|
static void verifyTileHits(skiatest::Reporter* reporter, SkRect rect,
|
2013-12-19 02:15:12 +04:00
|
|
|
uint32_t tileMask, int borderPixels = 0) {
|
2014-04-18 03:35:06 +04:00
|
|
|
SkTileGridFactory::TileGridInfo info;
|
2013-12-19 02:15:12 +04:00
|
|
|
info.fMargin.set(borderPixels, borderPixels);
|
|
|
|
info.fOffset.setZero();
|
|
|
|
info.fTileInterval.set(10 - 2 * borderPixels, 10 - 2 * borderPixels);
|
2014-08-11 21:09:34 +04:00
|
|
|
SkTileGrid grid(2, 2, info);
|
2013-12-19 02:15:12 +04:00
|
|
|
grid.insert(NULL, rect, false);
|
|
|
|
REPORTER_ASSERT(reporter, grid.tileCount(0, 0) ==
|
|
|
|
((tileMask & kTopLeft_Tile)? 1 : 0));
|
|
|
|
REPORTER_ASSERT(reporter, grid.tileCount(1, 0) ==
|
|
|
|
((tileMask & kTopRight_Tile)? 1 : 0));
|
|
|
|
REPORTER_ASSERT(reporter, grid.tileCount(0, 1) ==
|
|
|
|
((tileMask & kBottomLeft_Tile)? 1 : 0));
|
|
|
|
REPORTER_ASSERT(reporter, grid.tileCount(1, 1) ==
|
|
|
|
((tileMask & kBottomRight_Tile)? 1 : 0));
|
|
|
|
}
|
2012-11-07 21:38:38 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
DEF_TEST(TileGrid_UnalignedQuery, reporter) {
|
2014-03-28 21:57:14 +04:00
|
|
|
// Use SkTileGridPicture to generate a SkTileGrid with a helper
|
2014-04-18 03:35:06 +04:00
|
|
|
SkTileGridFactory::TileGridInfo info;
|
2013-12-19 02:15:12 +04:00
|
|
|
info.fMargin.setEmpty();
|
|
|
|
info.fOffset.setZero();
|
|
|
|
info.fTileInterval.set(10, 10);
|
2014-03-28 21:57:14 +04:00
|
|
|
SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(8), SkIntToScalar(8));
|
|
|
|
SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11),
|
|
|
|
SkIntToScalar(1), SkIntToScalar(1));
|
2014-04-18 03:35:06 +04:00
|
|
|
SkTileGridFactory factory(info);
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(20, 20, &factory, 0);
|
2014-03-28 21:57:14 +04:00
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawRect(rect1, paint);
|
|
|
|
canvas->drawRect(rect2, paint);
|
2014-04-13 23:09:42 +04:00
|
|
|
SkAutoTUnref<SkPicture> picture(recorder.endRecording());
|
2014-03-28 21:57:14 +04:00
|
|
|
|
|
|
|
SkBitmap store;
|
|
|
|
store.allocN32Pixels(1, 1);
|
2012-12-13 20:39:53 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
// Test parts of top-left tile
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
|
|
|
mockCanvas.translate(-7.99f, -7.99f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
// Corner overlap
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
|
|
|
mockCanvas.translate(-9.5f, -9.5f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
// Intersect bottom right tile, but does not overlap rect 2
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
|
|
|
mockCanvas.translate(-16.0f, -16.0f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
// Out of bounds queries, snap to border tiles
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
|
|
|
mockCanvas.translate(2.0f, 0.0f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
|
|
|
mockCanvas.translate(0.0f, 2.0f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
|
|
|
mockCanvas.translate(-22.0f, -16.0f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(store);
|
|
|
|
mockCanvas.translate(-16.0f, -22.0f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
|
2012-12-13 20:39:53 +04:00
|
|
|
}
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
2012-12-13 20:39:53 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
DEF_TEST(TileGrid_OverlapOffsetQueryAlignment, reporter) {
|
|
|
|
// Use SkTileGridPicture to generate a SkTileGrid with a helper
|
2014-04-18 03:35:06 +04:00
|
|
|
SkTileGridFactory::TileGridInfo info;
|
2013-12-19 02:15:12 +04:00
|
|
|
info.fMargin.set(1, 1);
|
|
|
|
info.fOffset.set(-1, -1);
|
|
|
|
info.fTileInterval.set(8, 8);
|
2013-02-27 22:35:16 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
// rect landing entirely in top left tile
|
2014-03-28 21:57:14 +04:00
|
|
|
SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(1), SkIntToScalar(1));
|
2013-12-19 02:15:12 +04:00
|
|
|
// rect landing entirely in center tile
|
2014-03-28 21:57:14 +04:00
|
|
|
SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(12), SkIntToScalar(12),
|
|
|
|
SkIntToScalar(1), SkIntToScalar(1));
|
2013-12-19 02:15:12 +04:00
|
|
|
// rect landing entirely in bottomright tile
|
2014-03-28 21:57:14 +04:00
|
|
|
SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(19), SkIntToScalar(19),
|
|
|
|
SkIntToScalar(1), SkIntToScalar(1));
|
2014-04-18 03:35:06 +04:00
|
|
|
SkTileGridFactory factory(info);
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(20, 20, &factory, 0);
|
2014-03-28 21:57:14 +04:00
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawRect(rect1, paint);
|
|
|
|
canvas->drawRect(rect2, paint);
|
|
|
|
canvas->drawRect(rect3, paint);
|
2014-04-13 23:09:42 +04:00
|
|
|
SkAutoTUnref<SkPicture> picture(recorder.endRecording());
|
2013-02-27 22:35:16 +04:00
|
|
|
|
2014-03-28 21:57:14 +04:00
|
|
|
SkBitmap tileBitmap;
|
|
|
|
tileBitmap.allocN32Pixels(10, 10);
|
|
|
|
SkBitmap moreThanATileBitmap;
|
|
|
|
moreThanATileBitmap.allocN32Pixels(11, 11);
|
|
|
|
SkBitmap tinyBitmap;
|
|
|
|
tinyBitmap.allocN32Pixels(2, 2);
|
2013-12-19 02:15:12 +04:00
|
|
|
// Test parts of top-left tile
|
|
|
|
{
|
|
|
|
// The offset should cancel the top and left borders of the top left tile
|
|
|
|
// So a look-up at interval 0-10 should be grid aligned,
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(tileBitmap);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
2013-02-27 22:35:16 +04:00
|
|
|
}
|
2013-12-19 02:15:12 +04:00
|
|
|
{
|
|
|
|
// Encroaching border by one pixel
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(moreThanATileBitmap);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// Tile stride is 8 (tileWidth - 2 * border pixels
|
|
|
|
// so translating by 8, should make query grid-aligned
|
|
|
|
// with middle tile.
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(tileBitmap);
|
|
|
|
mockCanvas.translate(SkIntToScalar(-8), SkIntToScalar(-8));
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(tileBitmap);
|
|
|
|
mockCanvas.translate(-7.9f, -7.9f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]);
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(tileBitmap);
|
|
|
|
mockCanvas.translate(-8.1f, -8.1f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2014-03-28 21:57:14 +04:00
|
|
|
REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count());
|
|
|
|
REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]);
|
|
|
|
REPORTER_ASSERT(reporter, rect3 == mockCanvas.fRects[1]);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// Regression test for crbug.com/234688
|
|
|
|
// Once the 2x2 device region is inset by margin, it yields an empty
|
|
|
|
// adjusted region, sitting right on top of the tile boundary.
|
2014-03-28 21:57:14 +04:00
|
|
|
MockCanvas mockCanvas(tinyBitmap);
|
|
|
|
mockCanvas.translate(-8.0f, -8.0f);
|
2014-04-13 23:09:42 +04:00
|
|
|
picture->draw(&mockCanvas);
|
2013-12-19 02:15:12 +04:00
|
|
|
// This test passes by not asserting. We do not validate the rects recorded
|
|
|
|
// because the result is numerically unstable (floating point equality).
|
|
|
|
// The content of any one of the four tiles of the tilegrid would be a valid
|
|
|
|
// result since any bbox that covers the center point of the canvas will be
|
|
|
|
// recorded in all four tiles.
|
|
|
|
}
|
|
|
|
}
|
2013-02-27 22:35:16 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
DEF_TEST(TileGrid, reporter) {
|
|
|
|
// Out of bounds
|
2014-08-27 21:39:42 +04:00
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(30, 0, 1, 1), 0);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(0, 30, 1, 1), 0);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(-10, 0, 1, 1), 0);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(0, -10, 1, 1), 0);
|
2013-02-09 01:03:30 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
// Dilation for AA consideration
|
2014-08-27 21:39:42 +04:00
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 9, 9), kTopLeft_Tile);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 10, 10), kAll_Tile);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(9, 9, 1, 1), kAll_Tile);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(10, 10, 1, 1), kAll_Tile);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(11, 11, 1, 1), kBottomRight_Tile);
|
2012-12-13 20:39:53 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
// BorderPixels
|
2014-08-27 21:39:42 +04:00
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 6, 6), kTopLeft_Tile, 1);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(0, 0, 7, 7), kAll_Tile, 1);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(9, 9, 1, 1), kAll_Tile, 1);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(10, 10, 1, 1), kBottomRight_Tile, 1);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(17, 17, 1, 1), kBottomRight_Tile, 1);
|
2012-11-07 21:38:38 +04:00
|
|
|
|
2013-12-19 02:15:12 +04:00
|
|
|
// BBoxes that overlap tiles
|
2014-08-27 21:39:42 +04:00
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kTopRight_Tile);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile |
|
2013-12-19 02:15:12 +04:00
|
|
|
kBottomLeft_Tile);
|
2014-08-27 21:39:42 +04:00
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(5, 5, 10, 10), kAll_Tile);
|
|
|
|
verifyTileHits(reporter, SkRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile);
|
2013-12-19 02:15:12 +04:00
|
|
|
}
|