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