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