blob: 25e99ba23d91eff41c4cb1070ab00b3605b4e9fd [file] [log] [blame]
dneto327f9052014-09-15 10:53:16 -07001/*
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 Reedc090c642017-05-16 10:39:06 -040010#include "../include/core/SkBitmap.h"
dneto327f9052014-09-15 10:53:16 -070011#include "../include/core/SkCanvas.h"
12#include "../include/core/SkPicture.h"
13#include "../include/core/SkStream.h"
14#include "../include/core/SkString.h"
dneto327f9052014-09-15 10:53:16 -070015#include "../include/core/SkPictureRecorder.h"
Mike Reedd4706732016-11-15 16:44:34 -050016#include "../src/core/SkBlendModePriv.h"
dneto327f9052014-09-15 10:53:16 -070017#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
dneto327f9052014-09-15 10:53:16 -070024namespace {
25
26class Drawer {
27 public:
mtklein46616af2014-09-30 14:47:10 -070028 explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
29 fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100));
dneto327f9052014-09-15 10:53:16 -070030 SkCanvas canvas(fCircleBM);
31 canvas.clear(0xffffffff);
32 SkPaint circlePaint;
33 circlePaint.setColor(0xff000000);
mtklein46616af2014-09-30 14:47:10 -070034 canvas.drawCircle(50, 50, 50, circlePaint);
dneto327f9052014-09-15 10:53:16 -070035 }
36
37 const SkImageInfo& imageInfo() const { return fImageInfo; }
38
reed374772b2016-10-05 17:33:02 -070039 void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
dneto327f9052014-09-15 10:53:16 -070040 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);
reed374772b2016-10-05 17:33:02 -070048 layerPaint.setBlendMode(mode);
mtklein46616af2014-09-30 14:47:10 -070049 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
50 SkIntToScalar(fImageInfo.height())));
dneto327f9052014-09-15 10:53:16 -070051
52 canvas->clipRect(clipRect);
53 canvas->clear(0xff000000);
54
halcanary96fcdcc2015-08-27 07:41:13 -070055 canvas->saveLayer(nullptr, &blackPaint);
mtklein46616af2014-09-30 14:47:10 -070056 canvas->drawRect(canvasRect, greenPaint);
halcanary96fcdcc2015-08-27 07:41:13 -070057 canvas->saveLayer(nullptr, &layerPaint);
mtklein46616af2014-09-30 14:47:10 -070058 canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint);
dneto327f9052014-09-15 10:53:16 -070059 canvas->restore();
60 canvas->restore();
61 }
62
63 private:
64 const SkImageInfo fImageInfo;
65 SkBitmap fCircleBM;
66};
67
68class RecordingStrategy {
69 public:
70 virtual ~RecordingStrategy() {}
dneto327f9052014-09-15 10:53:16 -070071 virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
72 const SkRect& intoClip,
reed374772b2016-10-05 17:33:02 -070073 SkBlendMode) = 0;
dneto327f9052014-09-15 10:53:16 -070074};
75
76class BitmapBackedCanvasStrategy : public RecordingStrategy {
77 // This version just draws into a bitmap-backed canvas.
78 public:
mtklein46616af2014-09-30 14:47:10 -070079 BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -070080 fBitmap.allocPixels(imageInfo);
81 }
82
reed374772b2016-10-05 17:33:02 -070083 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
84 SkBlendMode mode) override {
dneto327f9052014-09-15 10:53:16 -070085 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
mtklein46616af2014-09-30 14:47:10 -070097class PictureStrategy : public RecordingStrategy {
98 // This version draws the entire scene into an SkPictureRecorder.
dneto327f9052014-09-15 10:53:16 -070099 // Then it then replays the scene through a clip rectangle.
100 // This backend proved to be buggy.
101 public:
mtklein46616af2014-09-30 14:47:10 -0700102 PictureStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -0700103 fBitmap.allocPixels(imageInfo);
mtklein46616af2014-09-30 14:47:10 -0700104 fWidth = imageInfo.width();
105 fHeight = imageInfo.height();
dneto327f9052014-09-15 10:53:16 -0700106 }
107
reed374772b2016-10-05 17:33:02 -0700108 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
109 SkBlendMode mode) override {
mtklein703dd2e2015-01-09 06:41:48 -0800110 SkRTreeFactory factory;
dneto327f9052014-09-15 10:53:16 -0700111 SkPictureRecorder recorder;
112 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
mtklein46616af2014-09-30 14:47:10 -0700113 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
114 SkIntToScalar(fHeight),
115 &factory);
dneto327f9052014-09-15 10:53:16 -0700116 drawer.draw(canvas, canvasRect, mode);
reedca2622b2016-03-18 07:25:55 -0700117 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
dneto327f9052014-09-15 10:53:16 -0700118
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
mtklein46616af2014-09-30 14:47:10 -0700132} // namespace
dneto327f9052014-09-15 10:53:16 -0700133
134
135DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
136#define FINEGRAIN 0
dneto327f9052014-09-15 10:53:16 -0700137 const Drawer drawer;
138
mtklein46616af2014-09-30 14:47:10 -0700139 BitmapBackedCanvasStrategy golden(drawer.imageInfo());
140 PictureStrategy picture(drawer.imageInfo());
dneto327f9052014-09-15 10:53:16 -0700141
142#if !FINEGRAIN
143 unsigned numErrors = 0;
144 SkString errors;
145#endif
146
reed374772b2016-10-05 17:33:02 -0700147 for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
mtklein46616af2014-09-30 14:47:10 -0700148 const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
reed374772b2016-10-05 17:33:02 -0700149 SkBlendMode mode = SkBlendMode(iMode);
dneto327f9052014-09-15 10:53:16 -0700150
151 const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
mtklein46616af2014-09-30 14:47:10 -0700152 const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
dneto327f9052014-09-15 10:53:16 -0700153
Mike Reedf0ffb892017-10-03 14:47:21 -0400154 size_t pixelsSize = goldenBM.computeByteSize();
155 REPORTER_ASSERT(reporter, pixelsSize == pictureBM.computeByteSize());
dneto327f9052014-09-15 10:53:16 -0700156
157 // The pixel arrays should match.
158#if FINEGRAIN
mtklein46616af2014-09-30 14:47:10 -0700159 REPORTER_ASSERT(reporter,
160 0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
dneto327f9052014-09-15 10:53:16 -0700161#else
mtklein46616af2014-09-30 14:47:10 -0700162 if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
dneto327f9052014-09-15 10:53:16 -0700163 numErrors++;
mtklein46616af2014-09-30 14:47:10 -0700164 errors.appendf("For SkXfermode %d %s: SkPictureRecorder bitmap is wrong\n",
Mike Reedd4706732016-11-15 16:44:34 -0500165 iMode, SkBlendMode_Name(mode));
dneto327f9052014-09-15 10:53:16 -0700166 }
167#endif
168 }
mtklein46616af2014-09-30 14:47:10 -0700169
dneto327f9052014-09-15 10:53:16 -0700170#if !FINEGRAIN
mtklein46616af2014-09-30 14:47:10 -0700171 REPORTER_ASSERT_MESSAGE(reporter, 0 == numErrors, errors.c_str());
dneto327f9052014-09-15 10:53:16 -0700172#endif
173}