junov@chromium.org | adc58e4 | 2012-11-07 17:38:38 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "Test.h" |
| 10 | #include "SkTileGrid.h" |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 11 | #include "SkTileGridPicture.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkDevice.h" |
junov@chromium.org | adc58e4 | 2012-11-07 17:38:38 +0000 | [diff] [blame] | 14 | |
| 15 | enum Tile { |
| 16 | kTopLeft_Tile = 0x1, |
| 17 | kTopRight_Tile = 0x2, |
| 18 | kBottomLeft_Tile = 0x4, |
| 19 | kBottomRight_Tile = 0x8, |
| 20 | |
| 21 | kAll_Tile = kTopLeft_Tile | kTopRight_Tile | kBottomLeft_Tile | kBottomRight_Tile, |
| 22 | }; |
| 23 | |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | class MockCanvas : public SkCanvas { |
| 26 | public: |
| 27 | MockCanvas(SkDevice* device) : SkCanvas(device) |
| 28 | {} |
| 29 | |
| 30 | virtual void drawRect(const SkRect& rect, const SkPaint& paint) |
| 31 | { |
| 32 | // This capture occurs before quick reject. |
| 33 | fRects.push(rect); |
| 34 | } |
| 35 | |
| 36 | SkTDArray<SkRect> fRects; |
| 37 | }; |
| 38 | } |
| 39 | |
junov@chromium.org | adc58e4 | 2012-11-07 17:38:38 +0000 | [diff] [blame] | 40 | class TileGridTest { |
| 41 | public: |
| 42 | static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, uint32_t tileMask) { |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 43 | SkTileGrid grid(10, 10, 2, 2, NULL); |
junov@chromium.org | adc58e4 | 2012-11-07 17:38:38 +0000 | [diff] [blame] | 44 | grid.insert(NULL, rect, false); |
| 45 | REPORTER_ASSERT(reporter, grid.tile(0,0).count() == |
| 46 | ((tileMask & kTopLeft_Tile)? 1 : 0)); |
| 47 | REPORTER_ASSERT(reporter, grid.tile(1,0).count() == |
| 48 | ((tileMask & kTopRight_Tile)? 1 : 0)); |
| 49 | REPORTER_ASSERT(reporter, grid.tile(0,1).count() == |
| 50 | ((tileMask & kBottomLeft_Tile)? 1 : 0)); |
| 51 | REPORTER_ASSERT(reporter, grid.tile(1,1).count() == |
| 52 | ((tileMask & kBottomRight_Tile)? 1 : 0)); |
| 53 | } |
| 54 | |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 55 | static void TestUnalignedQuery(skiatest::Reporter* reporter) { |
| 56 | // Use SkTileGridPicture to generate a SkTileGrid with a helper |
| 57 | SkTileGridPicture picture(10, 10, 20, 20); |
skia.committer@gmail.com | 61b05dc | 2012-12-14 02:02:06 +0000 | [diff] [blame] | 58 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 59 | SkIntToScalar(8), SkIntToScalar(8)); |
| 60 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(11), SkIntToScalar(11), |
| 61 | SkIntToScalar(1), SkIntToScalar(1)); |
| 62 | SkCanvas* canvas = picture.beginRecording(20, 20, SkPicture::kOptimizeForClippedPlayback_RecordingFlag); |
| 63 | SkPaint paint; |
| 64 | canvas->drawRect(rect1, paint); |
| 65 | canvas->drawRect(rect2, paint); |
| 66 | picture.endRecording(); |
| 67 | |
| 68 | SkBitmap store; |
| 69 | store.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 70 | store.allocPixels(); |
| 71 | |
| 72 | // Test parts of top-left tile |
| 73 | { |
| 74 | SkDevice device(store); |
| 75 | MockCanvas mockCanvas(&device); |
| 76 | picture.draw(&mockCanvas); |
| 77 | REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 78 | REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
| 79 | } |
| 80 | { |
| 81 | SkDevice device(store); |
| 82 | MockCanvas mockCanvas(&device); |
| 83 | mockCanvas.translate(SkFloatToScalar(-7.99f), SkFloatToScalar(-7.99f)); |
| 84 | picture.draw(&mockCanvas); |
| 85 | REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 86 | REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
| 87 | } |
| 88 | // Corner overlap |
| 89 | { |
| 90 | SkDevice device(store); |
| 91 | MockCanvas mockCanvas(&device); |
| 92 | mockCanvas.translate(SkFloatToScalar(-9.5f), SkFloatToScalar(-9.5f)); |
| 93 | picture.draw(&mockCanvas); |
| 94 | REPORTER_ASSERT(reporter, 2 == mockCanvas.fRects.count()); |
| 95 | REPORTER_ASSERT(reporter, rect1 == mockCanvas.fRects[0]); |
| 96 | REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[1]); |
| 97 | } |
| 98 | // Intersect bottom right tile, but does not overlap rect 2 |
| 99 | { |
| 100 | SkDevice device(store); |
| 101 | MockCanvas mockCanvas(&device); |
| 102 | mockCanvas.translate(SkFloatToScalar(-16.0f), SkFloatToScalar(-16.0f)); |
| 103 | picture.draw(&mockCanvas); |
| 104 | REPORTER_ASSERT(reporter, 1 == mockCanvas.fRects.count()); |
| 105 | REPORTER_ASSERT(reporter, rect2 == mockCanvas.fRects[0]); |
| 106 | } |
| 107 | } |
| 108 | |
junov@chromium.org | adc58e4 | 2012-11-07 17:38:38 +0000 | [diff] [blame] | 109 | static void Test(skiatest::Reporter* reporter) { |
| 110 | // Out of bounds |
| 111 | verifyTileHits(reporter, SkIRect::MakeXYWH(30, 0, 1, 1), 0); |
| 112 | verifyTileHits(reporter, SkIRect::MakeXYWH(0, 30, 1, 1), 0); |
| 113 | verifyTileHits(reporter, SkIRect::MakeXYWH(-10, 0, 1, 1), 0); |
| 114 | verifyTileHits(reporter, SkIRect::MakeXYWH(0, -10, 1, 1), 0); |
| 115 | |
| 116 | // Dilation for AA consideration |
| 117 | verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 8, 8), kTopLeft_Tile); |
| 118 | verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 9, 9), kAll_Tile); |
| 119 | verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1), kAll_Tile); |
| 120 | verifyTileHits(reporter, SkIRect::MakeXYWH(11, 11, 1, 1), kBottomRight_Tile); |
| 121 | |
| 122 | // BBoxes that overlap tiles |
| 123 | verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1), kTopLeft_Tile | kTopRight_Tile); |
| 124 | verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10), kTopLeft_Tile | |
| 125 | kBottomLeft_Tile); |
| 126 | verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10), kAll_Tile); |
| 127 | verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40), kAll_Tile); |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 128 | |
| 129 | TestUnalignedQuery(reporter); |
junov@chromium.org | adc58e4 | 2012-11-07 17:38:38 +0000 | [diff] [blame] | 130 | } |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | #include "TestClassDef.h" |
| 135 | DEFINE_TESTCLASS("TileGrid", TileGridTestClass, TileGridTest::Test) |