blob: db4262c7e9530dd765d65dcb5ced4f372177c015 [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
10#include "../include/core/SkCanvas.h"
11#include "../include/core/SkPicture.h"
12#include "../include/core/SkStream.h"
13#include "../include/core/SkString.h"
dneto327f9052014-09-15 10:53:16 -070014#include "../include/core/SkPictureRecorder.h"
Mike Reedd4706732016-11-15 16:44:34 -050015#include "../src/core/SkBlendModePriv.h"
dneto327f9052014-09-15 10:53:16 -070016#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
dneto327f9052014-09-15 10:53:16 -070023namespace {
24
25class Drawer {
26 public:
mtklein46616af2014-09-30 14:47:10 -070027 explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
28 fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100));
dneto327f9052014-09-15 10:53:16 -070029 SkCanvas canvas(fCircleBM);
30 canvas.clear(0xffffffff);
31 SkPaint circlePaint;
32 circlePaint.setColor(0xff000000);
mtklein46616af2014-09-30 14:47:10 -070033 canvas.drawCircle(50, 50, 50, circlePaint);
dneto327f9052014-09-15 10:53:16 -070034 }
35
36 const SkImageInfo& imageInfo() const { return fImageInfo; }
37
reed374772b2016-10-05 17:33:02 -070038 void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
dneto327f9052014-09-15 10:53:16 -070039 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);
reed374772b2016-10-05 17:33:02 -070047 layerPaint.setBlendMode(mode);
mtklein46616af2014-09-30 14:47:10 -070048 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
49 SkIntToScalar(fImageInfo.height())));
dneto327f9052014-09-15 10:53:16 -070050
51 canvas->clipRect(clipRect);
52 canvas->clear(0xff000000);
53
halcanary96fcdcc2015-08-27 07:41:13 -070054 canvas->saveLayer(nullptr, &blackPaint);
mtklein46616af2014-09-30 14:47:10 -070055 canvas->drawRect(canvasRect, greenPaint);
halcanary96fcdcc2015-08-27 07:41:13 -070056 canvas->saveLayer(nullptr, &layerPaint);
mtklein46616af2014-09-30 14:47:10 -070057 canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint);
dneto327f9052014-09-15 10:53:16 -070058 canvas->restore();
59 canvas->restore();
60 }
61
62 private:
63 const SkImageInfo fImageInfo;
64 SkBitmap fCircleBM;
65};
66
67class RecordingStrategy {
68 public:
69 virtual ~RecordingStrategy() {}
dneto327f9052014-09-15 10:53:16 -070070 virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
71 const SkRect& intoClip,
reed374772b2016-10-05 17:33:02 -070072 SkBlendMode) = 0;
dneto327f9052014-09-15 10:53:16 -070073};
74
75class BitmapBackedCanvasStrategy : public RecordingStrategy {
76 // This version just draws into a bitmap-backed canvas.
77 public:
mtklein46616af2014-09-30 14:47:10 -070078 BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -070079 fBitmap.allocPixels(imageInfo);
80 }
81
reed374772b2016-10-05 17:33:02 -070082 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
83 SkBlendMode mode) override {
dneto327f9052014-09-15 10:53:16 -070084 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
mtklein46616af2014-09-30 14:47:10 -070096class PictureStrategy : public RecordingStrategy {
97 // This version draws the entire scene into an SkPictureRecorder.
dneto327f9052014-09-15 10:53:16 -070098 // Then it then replays the scene through a clip rectangle.
99 // This backend proved to be buggy.
100 public:
mtklein46616af2014-09-30 14:47:10 -0700101 PictureStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -0700102 fBitmap.allocPixels(imageInfo);
mtklein46616af2014-09-30 14:47:10 -0700103 fWidth = imageInfo.width();
104 fHeight = imageInfo.height();
dneto327f9052014-09-15 10:53:16 -0700105 }
106
reed374772b2016-10-05 17:33:02 -0700107 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
108 SkBlendMode mode) override {
mtklein703dd2e2015-01-09 06:41:48 -0800109 SkRTreeFactory factory;
dneto327f9052014-09-15 10:53:16 -0700110 SkPictureRecorder recorder;
111 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
mtklein46616af2014-09-30 14:47:10 -0700112 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
113 SkIntToScalar(fHeight),
114 &factory);
dneto327f9052014-09-15 10:53:16 -0700115 drawer.draw(canvas, canvasRect, mode);
reedca2622b2016-03-18 07:25:55 -0700116 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
dneto327f9052014-09-15 10:53:16 -0700117
118 SkCanvas replayCanvas(fBitmap);
119 replayCanvas.clear(0xffffffff);
120 replayCanvas.clipRect(intoClip);
121 picture->playback(&replayCanvas);
122 return fBitmap;
123 }
124
125 private:
126 SkBitmap fBitmap;
127 int fWidth;
128 int fHeight;
129};
130
mtklein46616af2014-09-30 14:47:10 -0700131} // namespace
dneto327f9052014-09-15 10:53:16 -0700132
133
134DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
135#define FINEGRAIN 0
dneto327f9052014-09-15 10:53:16 -0700136 const Drawer drawer;
137
mtklein46616af2014-09-30 14:47:10 -0700138 BitmapBackedCanvasStrategy golden(drawer.imageInfo());
139 PictureStrategy picture(drawer.imageInfo());
dneto327f9052014-09-15 10:53:16 -0700140
141#if !FINEGRAIN
142 unsigned numErrors = 0;
143 SkString errors;
144#endif
145
reed374772b2016-10-05 17:33:02 -0700146 for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
mtklein46616af2014-09-30 14:47:10 -0700147 const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
reed374772b2016-10-05 17:33:02 -0700148 SkBlendMode mode = SkBlendMode(iMode);
dneto327f9052014-09-15 10:53:16 -0700149
150 const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
mtklein46616af2014-09-30 14:47:10 -0700151 const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
dneto327f9052014-09-15 10:53:16 -0700152
153 size_t pixelsSize = goldenBM.getSize();
mtklein46616af2014-09-30 14:47:10 -0700154 REPORTER_ASSERT(reporter, pixelsSize == pictureBM.getSize());
dneto327f9052014-09-15 10:53:16 -0700155
156 // The pixel arrays should match.
157#if FINEGRAIN
mtklein46616af2014-09-30 14:47:10 -0700158 REPORTER_ASSERT(reporter,
159 0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
dneto327f9052014-09-15 10:53:16 -0700160#else
mtklein46616af2014-09-30 14:47:10 -0700161 if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
dneto327f9052014-09-15 10:53:16 -0700162 numErrors++;
mtklein46616af2014-09-30 14:47:10 -0700163 errors.appendf("For SkXfermode %d %s: SkPictureRecorder bitmap is wrong\n",
Mike Reedd4706732016-11-15 16:44:34 -0500164 iMode, SkBlendMode_Name(mode));
dneto327f9052014-09-15 10:53:16 -0700165 }
166#endif
167 }
mtklein46616af2014-09-30 14:47:10 -0700168
dneto327f9052014-09-15 10:53:16 -0700169#if !FINEGRAIN
mtklein46616af2014-09-30 14:47:10 -0700170 REPORTER_ASSERT_MESSAGE(reporter, 0 == numErrors, errors.c_str());
dneto327f9052014-09-15 10:53:16 -0700171#endif
172}