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" |
| 15 | #include <cstring> |
| 16 | |
| 17 | // Verify that replay of a recording into a clipped canvas |
| 18 | // produces the correct bitmap. |
| 19 | // This arose from http://crbug.com/401593 which has |
| 20 | // https://code.google.com/p/skia/issues/detail?id=1291 as its root cause. |
| 21 | |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | class Drawer { |
| 25 | public: |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 26 | explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) { |
| 27 | fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100)); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 28 | SkCanvas canvas(fCircleBM); |
| 29 | canvas.clear(0xffffffff); |
| 30 | SkPaint circlePaint; |
| 31 | circlePaint.setColor(0xff000000); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 32 | canvas.drawCircle(50, 50, 50, circlePaint); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | const SkImageInfo& imageInfo() const { return fImageInfo; } |
| 36 | |
| 37 | void draw(SkCanvas* canvas, const SkRect& clipRect, SkXfermode::Mode mode) const { |
| 38 | SkPaint greenPaint; |
| 39 | greenPaint.setColor(0xff008000); |
| 40 | SkPaint blackPaint; |
| 41 | blackPaint.setColor(0xff000000); |
| 42 | SkPaint whitePaint; |
| 43 | whitePaint.setColor(0xffffffff); |
| 44 | SkPaint layerPaint; |
| 45 | layerPaint.setColor(0xff000000); |
| 46 | layerPaint.setXfermodeMode(mode); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 47 | SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()), |
| 48 | SkIntToScalar(fImageInfo.height()))); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 49 | |
| 50 | canvas->clipRect(clipRect); |
| 51 | canvas->clear(0xff000000); |
| 52 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 53 | canvas->saveLayer(nullptr, &blackPaint); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 54 | canvas->drawRect(canvasRect, greenPaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 55 | canvas->saveLayer(nullptr, &layerPaint); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 56 | canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 57 | canvas->restore(); |
| 58 | canvas->restore(); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | const SkImageInfo fImageInfo; |
| 63 | SkBitmap fCircleBM; |
| 64 | }; |
| 65 | |
| 66 | class RecordingStrategy { |
| 67 | public: |
| 68 | virtual ~RecordingStrategy() {} |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 69 | virtual const SkBitmap& recordAndReplay(const Drawer& drawer, |
| 70 | const SkRect& intoClip, |
| 71 | SkXfermode::Mode) = 0; |
| 72 | }; |
| 73 | |
| 74 | class BitmapBackedCanvasStrategy : public RecordingStrategy { |
| 75 | // This version just draws into a bitmap-backed canvas. |
| 76 | public: |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 77 | BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 78 | fBitmap.allocPixels(imageInfo); |
| 79 | } |
| 80 | |
| 81 | virtual const SkBitmap& recordAndReplay(const Drawer& drawer, |
| 82 | const SkRect& intoClip, |
| 83 | SkXfermode::Mode mode) { |
| 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 | |
| 107 | virtual const SkBitmap& recordAndReplay(const Drawer& drawer, |
| 108 | const SkRect& intoClip, |
| 109 | SkXfermode::Mode mode) { |
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 | 90d0ff0 | 2014-11-24 12:02:31 -0800 | [diff] [blame] | 117 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
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 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame] | 147 | for (int iMode = 0; iMode < int(SkXfermode::kLastMode); iMode++) { |
| 148 | const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 149 | SkXfermode::Mode mode = SkXfermode::Mode(iMode); |
| 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", |
| 165 | iMode, SkXfermode::ModeName(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 | } |