blob: 32aec36f9f712995a421a070fad6e9252833df8f [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"
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
dneto327f9052014-09-15 10:53:16 -070022namespace {
23
24class Drawer {
25 public:
mtklein46616af2014-09-30 14:47:10 -070026 explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
27 fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100));
dneto327f9052014-09-15 10:53:16 -070028 SkCanvas canvas(fCircleBM);
29 canvas.clear(0xffffffff);
30 SkPaint circlePaint;
31 circlePaint.setColor(0xff000000);
mtklein46616af2014-09-30 14:47:10 -070032 canvas.drawCircle(50, 50, 50, circlePaint);
dneto327f9052014-09-15 10:53:16 -070033 }
34
35 const SkImageInfo& imageInfo() const { return fImageInfo; }
36
reed374772b2016-10-05 17:33:02 -070037 void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
dneto327f9052014-09-15 10:53:16 -070038 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);
reed374772b2016-10-05 17:33:02 -070046 layerPaint.setBlendMode(mode);
mtklein46616af2014-09-30 14:47:10 -070047 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
48 SkIntToScalar(fImageInfo.height())));
dneto327f9052014-09-15 10:53:16 -070049
50 canvas->clipRect(clipRect);
51 canvas->clear(0xff000000);
52
halcanary96fcdcc2015-08-27 07:41:13 -070053 canvas->saveLayer(nullptr, &blackPaint);
mtklein46616af2014-09-30 14:47:10 -070054 canvas->drawRect(canvasRect, greenPaint);
halcanary96fcdcc2015-08-27 07:41:13 -070055 canvas->saveLayer(nullptr, &layerPaint);
mtklein46616af2014-09-30 14:47:10 -070056 canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint);
dneto327f9052014-09-15 10:53:16 -070057 canvas->restore();
58 canvas->restore();
59 }
60
61 private:
62 const SkImageInfo fImageInfo;
63 SkBitmap fCircleBM;
64};
65
66class RecordingStrategy {
67 public:
68 virtual ~RecordingStrategy() {}
dneto327f9052014-09-15 10:53:16 -070069 virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
70 const SkRect& intoClip,
reed374772b2016-10-05 17:33:02 -070071 SkBlendMode) = 0;
dneto327f9052014-09-15 10:53:16 -070072};
73
74class BitmapBackedCanvasStrategy : public RecordingStrategy {
75 // This version just draws into a bitmap-backed canvas.
76 public:
mtklein46616af2014-09-30 14:47:10 -070077 BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -070078 fBitmap.allocPixels(imageInfo);
79 }
80
reed374772b2016-10-05 17:33:02 -070081 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
82 SkBlendMode mode) override {
dneto327f9052014-09-15 10:53:16 -070083 SkCanvas canvas(fBitmap);
84 canvas.clear(0xffffffff);
85 // Note that the scene is drawn just into the clipped region!
86 canvas.clipRect(intoClip);
87 drawer.draw(&canvas, intoClip, mode); // Shouild be canvas-wide...
88 return fBitmap;
89 }
90
91 private:
92 SkBitmap fBitmap;
93};
94
mtklein46616af2014-09-30 14:47:10 -070095class PictureStrategy : public RecordingStrategy {
96 // This version draws the entire scene into an SkPictureRecorder.
dneto327f9052014-09-15 10:53:16 -070097 // Then it then replays the scene through a clip rectangle.
98 // This backend proved to be buggy.
99 public:
mtklein46616af2014-09-30 14:47:10 -0700100 PictureStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -0700101 fBitmap.allocPixels(imageInfo);
mtklein46616af2014-09-30 14:47:10 -0700102 fWidth = imageInfo.width();
103 fHeight = imageInfo.height();
dneto327f9052014-09-15 10:53:16 -0700104 }
105
reed374772b2016-10-05 17:33:02 -0700106 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
107 SkBlendMode mode) override {
mtklein703dd2e2015-01-09 06:41:48 -0800108 SkRTreeFactory factory;
dneto327f9052014-09-15 10:53:16 -0700109 SkPictureRecorder recorder;
110 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
mtklein46616af2014-09-30 14:47:10 -0700111 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
112 SkIntToScalar(fHeight),
113 &factory);
dneto327f9052014-09-15 10:53:16 -0700114 drawer.draw(canvas, canvasRect, mode);
reedca2622b2016-03-18 07:25:55 -0700115 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
dneto327f9052014-09-15 10:53:16 -0700116
117 SkCanvas replayCanvas(fBitmap);
118 replayCanvas.clear(0xffffffff);
119 replayCanvas.clipRect(intoClip);
120 picture->playback(&replayCanvas);
121 return fBitmap;
122 }
123
124 private:
125 SkBitmap fBitmap;
126 int fWidth;
127 int fHeight;
128};
129
mtklein46616af2014-09-30 14:47:10 -0700130} // namespace
dneto327f9052014-09-15 10:53:16 -0700131
132
133DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
134#define FINEGRAIN 0
dneto327f9052014-09-15 10:53:16 -0700135 const Drawer drawer;
136
mtklein46616af2014-09-30 14:47:10 -0700137 BitmapBackedCanvasStrategy golden(drawer.imageInfo());
138 PictureStrategy picture(drawer.imageInfo());
dneto327f9052014-09-15 10:53:16 -0700139
140#if !FINEGRAIN
141 unsigned numErrors = 0;
142 SkString errors;
143#endif
144
reed374772b2016-10-05 17:33:02 -0700145 for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
mtklein46616af2014-09-30 14:47:10 -0700146 const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
reed374772b2016-10-05 17:33:02 -0700147 SkBlendMode mode = SkBlendMode(iMode);
dneto327f9052014-09-15 10:53:16 -0700148
149 const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
mtklein46616af2014-09-30 14:47:10 -0700150 const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
dneto327f9052014-09-15 10:53:16 -0700151
152 size_t pixelsSize = goldenBM.getSize();
mtklein46616af2014-09-30 14:47:10 -0700153 REPORTER_ASSERT(reporter, pixelsSize == pictureBM.getSize());
dneto327f9052014-09-15 10:53:16 -0700154
155 // The pixel arrays should match.
156#if FINEGRAIN
mtklein46616af2014-09-30 14:47:10 -0700157 REPORTER_ASSERT(reporter,
158 0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
dneto327f9052014-09-15 10:53:16 -0700159#else
mtklein46616af2014-09-30 14:47:10 -0700160 if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
dneto327f9052014-09-15 10:53:16 -0700161 numErrors++;
mtklein46616af2014-09-30 14:47:10 -0700162 errors.appendf("For SkXfermode %d %s: SkPictureRecorder bitmap is wrong\n",
163 iMode, SkXfermode::ModeName(mode));
dneto327f9052014-09-15 10:53:16 -0700164 }
165#endif
166 }
mtklein46616af2014-09-30 14:47:10 -0700167
dneto327f9052014-09-15 10:53:16 -0700168#if !FINEGRAIN
mtklein46616af2014-09-30 14:47:10 -0700169 REPORTER_ASSERT_MESSAGE(reporter, 0 == numErrors, errors.c_str());
dneto327f9052014-09-15 10:53:16 -0700170#endif
171}