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