dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Test.h" |
| 9 | |
| 10 | #include "../include/core/SkCanvas.h" |
| 11 | #include "../include/core/SkPicture.h" |
| 12 | #include "../include/core/SkStream.h" |
| 13 | #include "../include/core/SkString.h" |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 14 | #include "../include/core/SkPictureRecorder.h" |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 15 | #include "../src/core/SkBlendModePriv.h" |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 16 | #include <cstring> |
| 17 | |
| 18 | // Verify that replay of a recording into a clipped canvas |
| 19 | // produces the correct bitmap. |
| 20 | // This arose from http://crbug.com/401593 which has |
| 21 | // https://code.google.com/p/skia/issues/detail?id=1291 as its root cause. |
| 22 | |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | class Drawer { |
| 26 | public: |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 27 | explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) { |
| 28 | fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100)); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 29 | SkCanvas canvas(fCircleBM); |
| 30 | canvas.clear(0xffffffff); |
| 31 | SkPaint circlePaint; |
| 32 | circlePaint.setColor(0xff000000); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 33 | canvas.drawCircle(50, 50, 50, circlePaint); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | const SkImageInfo& imageInfo() const { return fImageInfo; } |
| 37 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 38 | void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 39 | SkPaint greenPaint; |
| 40 | greenPaint.setColor(0xff008000); |
| 41 | SkPaint blackPaint; |
| 42 | blackPaint.setColor(0xff000000); |
| 43 | SkPaint whitePaint; |
| 44 | whitePaint.setColor(0xffffffff); |
| 45 | SkPaint layerPaint; |
| 46 | layerPaint.setColor(0xff000000); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 47 | layerPaint.setBlendMode(mode); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 48 | SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()), |
| 49 | SkIntToScalar(fImageInfo.height()))); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 50 | |
| 51 | canvas->clipRect(clipRect); |
| 52 | canvas->clear(0xff000000); |
| 53 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | canvas->saveLayer(nullptr, &blackPaint); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 55 | canvas->drawRect(canvasRect, greenPaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | canvas->saveLayer(nullptr, &layerPaint); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 57 | canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 58 | canvas->restore(); |
| 59 | canvas->restore(); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | const SkImageInfo fImageInfo; |
| 64 | SkBitmap fCircleBM; |
| 65 | }; |
| 66 | |
| 67 | class RecordingStrategy { |
| 68 | public: |
| 69 | virtual ~RecordingStrategy() {} |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 70 | virtual const SkBitmap& recordAndReplay(const Drawer& drawer, |
| 71 | const SkRect& intoClip, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 72 | SkBlendMode) = 0; |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | class BitmapBackedCanvasStrategy : public RecordingStrategy { |
| 76 | // This version just draws into a bitmap-backed canvas. |
| 77 | public: |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 78 | BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 79 | fBitmap.allocPixels(imageInfo); |
| 80 | } |
| 81 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 82 | const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip, |
| 83 | SkBlendMode mode) override { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 84 | SkCanvas canvas(fBitmap); |
| 85 | canvas.clear(0xffffffff); |
| 86 | // Note that the scene is drawn just into the clipped region! |
| 87 | canvas.clipRect(intoClip); |
| 88 | drawer.draw(&canvas, intoClip, mode); // Shouild be canvas-wide... |
| 89 | return fBitmap; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | SkBitmap fBitmap; |
| 94 | }; |
| 95 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 96 | class PictureStrategy : public RecordingStrategy { |
| 97 | // This version draws the entire scene into an SkPictureRecorder. |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 98 | // Then it then replays the scene through a clip rectangle. |
| 99 | // This backend proved to be buggy. |
| 100 | public: |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 101 | PictureStrategy(const SkImageInfo& imageInfo) { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 102 | fBitmap.allocPixels(imageInfo); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 103 | fWidth = imageInfo.width(); |
| 104 | fHeight = imageInfo.height(); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 105 | } |
| 106 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 107 | const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip, |
| 108 | SkBlendMode mode) override { |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 109 | SkRTreeFactory factory; |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 110 | SkPictureRecorder recorder; |
| 111 | SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight))); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 112 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth), |
| 113 | SkIntToScalar(fHeight), |
| 114 | &factory); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 115 | drawer.draw(canvas, canvasRect, mode); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 116 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 117 | |
| 118 | SkCanvas replayCanvas(fBitmap); |
| 119 | replayCanvas.clear(0xffffffff); |
| 120 | replayCanvas.clipRect(intoClip); |
| 121 | picture->playback(&replayCanvas); |
| 122 | return fBitmap; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | SkBitmap fBitmap; |
| 127 | int fWidth; |
| 128 | int fHeight; |
| 129 | }; |
| 130 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 131 | } // namespace |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 132 | |
| 133 | |
| 134 | DEF_TEST(SkRecordingAccuracyXfermode, reporter) { |
| 135 | #define FINEGRAIN 0 |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 136 | const Drawer drawer; |
| 137 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 138 | BitmapBackedCanvasStrategy golden(drawer.imageInfo()); |
| 139 | PictureStrategy picture(drawer.imageInfo()); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 140 | |
| 141 | #if !FINEGRAIN |
| 142 | unsigned numErrors = 0; |
| 143 | SkString errors; |
| 144 | #endif |
| 145 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 146 | for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) { |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 147 | const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 148 | SkBlendMode mode = SkBlendMode(iMode); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 149 | |
| 150 | const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 151 | const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 152 | |
| 153 | size_t pixelsSize = goldenBM.getSize(); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, pixelsSize == pictureBM.getSize()); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 155 | |
| 156 | // The pixel arrays should match. |
| 157 | #if FINEGRAIN |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, |
| 159 | 0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 160 | #else |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 161 | if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 162 | numErrors++; |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 163 | errors.appendf("For SkXfermode %d %s: SkPictureRecorder bitmap is wrong\n", |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 164 | iMode, SkBlendMode_Name(mode)); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 165 | } |
| 166 | #endif |
| 167 | } |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 168 | |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 169 | #if !FINEGRAIN |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 170 | REPORTER_ASSERT_MESSAGE(reporter, 0 == numErrors, errors.c_str()); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 171 | #endif |
| 172 | } |