commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 1 | #include "DMQuiltTask.h" |
| 2 | #include "DMUtil.h" |
| 3 | #include "DMWriteTask.h" |
| 4 | |
mtklein | e4636aa | 2014-07-09 13:10:58 -0700 | [diff] [blame] | 5 | #include "SkBBHFactory.h" |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 6 | #include "SkCommandLineFlags.h" |
| 7 | #include "SkPicture.h" |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 8 | #include "SkThreadPool.h" |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 9 | |
mtklein | e4636aa | 2014-07-09 13:10:58 -0700 | [diff] [blame] | 10 | DEFINE_bool(quilt, true, "If true, draw GM via a picture into a quilt of small tiles and compare."); |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 11 | DEFINE_int32(quiltTile, 256, "Dimension of (square) quilt tile."); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 12 | |
mtklein | e4636aa | 2014-07-09 13:10:58 -0700 | [diff] [blame] | 13 | static const char* kSuffixes[] = { "nobbh", "rtree", "quadtree", "tilegrid", "skr" }; |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 14 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 15 | namespace DM { |
| 16 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 17 | QuiltTask::QuiltTask(const Task& parent, skiagm::GM* gm, SkBitmap reference, QuiltTask::Mode mode) |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 18 | : CpuTask(parent) |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 19 | , fMode(mode) |
| 20 | , fName(UnderJoin(parent.name().c_str(), kSuffixes[mode])) |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 21 | , fGM(gm) |
| 22 | , fReference(reference) |
| 23 | {} |
| 24 | |
| 25 | static int tiles_needed(int fullDimension, int tileDimension) { |
| 26 | return (fullDimension + tileDimension - 1) / tileDimension; |
| 27 | } |
| 28 | |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 29 | class Tile : public SkRunnable { |
| 30 | public: |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 31 | Tile(int x, int y, const SkPicture& picture, SkBitmap* quilt) |
| 32 | : fX(x * FLAGS_quiltTile) |
| 33 | , fY(y * FLAGS_quiltTile) |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 34 | , fPicture(picture) |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 35 | , fQuilt(quilt) {} |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 36 | |
| 37 | virtual void run() SK_OVERRIDE { |
| 38 | SkBitmap tile; |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 39 | fQuilt->extractSubset(&tile, SkIRect::MakeXYWH(fX, fY, FLAGS_quiltTile, FLAGS_quiltTile)); |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 40 | SkCanvas tileCanvas(tile); |
| 41 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 42 | tileCanvas.translate(SkIntToScalar(-fX), SkIntToScalar(-fY)); |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 43 | fPicture.draw(&tileCanvas); |
| 44 | tileCanvas.flush(); |
| 45 | |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 46 | delete this; |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | const int fX, fY; |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 51 | const SkPicture& fPicture; |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame] | 52 | SkBitmap* fQuilt; |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 55 | void QuiltTask::draw() { |
mtklein | e4636aa | 2014-07-09 13:10:58 -0700 | [diff] [blame] | 56 | SkAutoTDelete<SkBBHFactory> factory; |
| 57 | switch (fMode) { |
| 58 | case kRTree_Mode: |
| 59 | factory.reset(SkNEW(SkRTreeFactory)); |
| 60 | break; |
| 61 | case kQuadTree_Mode: |
| 62 | factory.reset(SkNEW(SkQuadTreeFactory)); |
| 63 | break; |
| 64 | case kTileGrid_Mode: { |
| 65 | const SkTileGridFactory::TileGridInfo tiles = { |
| 66 | { FLAGS_quiltTile, FLAGS_quiltTile }, |
| 67 | /*overlap: */{0, 0}, |
| 68 | /*offset: */{0, 0}, |
| 69 | }; |
| 70 | factory.reset(SkNEW_ARGS(SkTileGridFactory, (tiles))); |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | case kNoBBH_Mode: |
| 75 | case kSkRecord_Mode: |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | // A couple GMs draw wrong when using a bounding box hierarchy. |
| 80 | // This almost certainly means we have a bug to fix, but for now |
| 81 | // just draw without a bounding box hierarchy. |
| 82 | if (fGM->getFlags() & skiagm::GM::kNoBBH_Flag) { |
| 83 | factory.reset(NULL); |
| 84 | } |
| 85 | |
| 86 | SkAutoTUnref<const SkPicture> recorded( |
| 87 | RecordPicture(fGM.get(), factory.get(), kSkRecord_Mode == fMode)); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 88 | |
| 89 | SkBitmap full; |
commit-bot@chromium.org | 2664207 | 2014-05-15 17:33:31 +0000 | [diff] [blame] | 90 | AllocatePixels(fReference, &full); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 91 | |
mtklein | e4636aa | 2014-07-09 13:10:58 -0700 | [diff] [blame] | 92 | if (fGM->getFlags() & skiagm::GM::kSkipTiled_Flag) { |
| 93 | // Some GMs don't draw exactly the same when tiled. Draw them in one go. |
| 94 | SkCanvas canvas(full); |
| 95 | recorded->draw(&canvas); |
| 96 | canvas.flush(); |
| 97 | } else { |
| 98 | // Draw tiles in parallel into the same bitmap, simulating aggressive impl-side painting. |
| 99 | SkThreadPool pool(SkThreadPool::kThreadPerCore); |
| 100 | for (int y = 0; y < tiles_needed(full.height(), FLAGS_quiltTile); y++) { |
| 101 | for (int x = 0; x < tiles_needed(full.width(), FLAGS_quiltTile); x++) { |
| 102 | // Deletes itself when done. |
| 103 | pool.add(new Tile(x, y, *recorded, &full)); |
| 104 | } |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 108 | if (!BitmapsEqual(full, fReference)) { |
| 109 | this->fail(); |
| 110 | this->spawnChild(SkNEW_ARGS(WriteTask, (*this, full))); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool QuiltTask::shouldSkip() const { |
| 115 | if (fGM->getFlags() & skiagm::GM::kSkipPicture_Flag) { |
| 116 | return true; |
| 117 | } |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 118 | return !FLAGS_quilt; |
| 119 | } |
| 120 | |
| 121 | } // namespace DM |