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" |
| 14 | #include "../include/record/SkRecording.h" |
| 15 | #include "../include/core/SkPictureRecorder.h" |
| 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 | |
| 38 | void draw(SkCanvas* canvas, const SkRect& clipRect, SkXfermode::Mode mode) const { |
| 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); |
| 47 | layerPaint.setXfermodeMode(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 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 54 | canvas->saveLayer(NULL, &blackPaint); |
| 55 | canvas->drawRect(canvasRect, greenPaint); |
| 56 | canvas->saveLayer(NULL, &layerPaint); |
| 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, |
| 72 | SkXfermode::Mode) = 0; |
| 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 | |
| 82 | virtual const SkBitmap& recordAndReplay(const Drawer& drawer, |
| 83 | const SkRect& intoClip, |
| 84 | SkXfermode::Mode mode) { |
| 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 | |
| 108 | virtual const SkBitmap& recordAndReplay(const Drawer& drawer, |
| 109 | const SkRect& intoClip, |
| 110 | SkXfermode::Mode mode) { |
| 111 | SkTileGridFactory::TileGridInfo tileGridInfo = { {100,100}, {0,0}, {0,0} }; |
| 112 | SkTileGridFactory factory(tileGridInfo); |
| 113 | SkPictureRecorder recorder; |
| 114 | SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight))); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 115 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth), |
| 116 | SkIntToScalar(fHeight), |
| 117 | &factory); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 118 | drawer.draw(canvas, canvasRect, mode); |
| 119 | SkAutoTDelete<SkPicture> picture(recorder.endRecording()); |
| 120 | |
| 121 | SkCanvas replayCanvas(fBitmap); |
| 122 | replayCanvas.clear(0xffffffff); |
| 123 | replayCanvas.clipRect(intoClip); |
| 124 | picture->playback(&replayCanvas); |
| 125 | return fBitmap; |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | SkBitmap fBitmap; |
| 130 | int fWidth; |
| 131 | int fHeight; |
| 132 | }; |
| 133 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 134 | } // namespace |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 135 | |
| 136 | |
| 137 | DEF_TEST(SkRecordingAccuracyXfermode, reporter) { |
| 138 | #define FINEGRAIN 0 |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 139 | const Drawer drawer; |
| 140 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 141 | BitmapBackedCanvasStrategy golden(drawer.imageInfo()); |
| 142 | PictureStrategy picture(drawer.imageInfo()); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 143 | |
| 144 | #if !FINEGRAIN |
| 145 | unsigned numErrors = 0; |
| 146 | SkString errors; |
| 147 | #endif |
| 148 | |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 149 | for (int iMode = 0; iMode < int(SkXfermode::kLastMode); iMode++) { |
| 150 | const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 151 | SkXfermode::Mode mode = SkXfermode::Mode(iMode); |
| 152 | |
| 153 | const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 154 | const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 155 | |
| 156 | size_t pixelsSize = goldenBM.getSize(); |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 157 | REPORTER_ASSERT(reporter, pixelsSize == pictureBM.getSize()); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 158 | |
| 159 | // The pixel arrays should match. |
| 160 | #if FINEGRAIN |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 161 | REPORTER_ASSERT(reporter, |
| 162 | 0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 163 | #else |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 164 | if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) { |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 165 | numErrors++; |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 166 | errors.appendf("For SkXfermode %d %s: SkPictureRecorder bitmap is wrong\n", |
| 167 | iMode, SkXfermode::ModeName(mode)); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 168 | } |
| 169 | #endif |
| 170 | } |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 171 | |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 172 | #if !FINEGRAIN |
mtklein | 46616af | 2014-09-30 14:47:10 -0700 | [diff] [blame^] | 173 | REPORTER_ASSERT_MESSAGE(reporter, 0 == numErrors, errors.c_str()); |
dneto | 327f905 | 2014-09-15 10:53:16 -0700 | [diff] [blame] | 174 | #endif |
| 175 | } |