blob: 9dad24e0d1ac064de89992d07f393598e22fa772 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
dneto327f9052014-09-15 10:53:16 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkPicture.h"
13#include "include/core/SkPictureRecorder.h"
14#include "include/core/SkStream.h"
15#include "include/core/SkString.h"
Mike Reed34a0c972021-01-25 17:49:32 -050016#include "include/core/SkSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkBlendModePriv.h"
Hal Canary8a001442018-09-19 11:31:27 -040018
dneto327f9052014-09-15 10:53:16 -070019#include <cstring>
20
21// Verify that replay of a recording into a clipped canvas
22// produces the correct bitmap.
23// This arose from http://crbug.com/401593 which has
24// https://code.google.com/p/skia/issues/detail?id=1291 as its root cause.
25
dneto327f9052014-09-15 10:53:16 -070026namespace {
27
28class Drawer {
29 public:
mtklein46616af2014-09-30 14:47:10 -070030 explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
Mike Reed34a0c972021-01-25 17:49:32 -050031 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
32 surf->getCanvas()->clear(0xffffffff);
dneto327f9052014-09-15 10:53:16 -070033 SkPaint circlePaint;
34 circlePaint.setColor(0xff000000);
Mike Reed34a0c972021-01-25 17:49:32 -050035 surf->getCanvas()->drawCircle(50, 50, 50, circlePaint);
36 fCircleImage = surf->makeImageSnapshot();
dneto327f9052014-09-15 10:53:16 -070037 }
38
39 const SkImageInfo& imageInfo() const { return fImageInfo; }
40
reed374772b2016-10-05 17:33:02 -070041 void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
dneto327f9052014-09-15 10:53:16 -070042 SkPaint greenPaint;
43 greenPaint.setColor(0xff008000);
44 SkPaint blackPaint;
45 blackPaint.setColor(0xff000000);
46 SkPaint whitePaint;
47 whitePaint.setColor(0xffffffff);
48 SkPaint layerPaint;
49 layerPaint.setColor(0xff000000);
reed374772b2016-10-05 17:33:02 -070050 layerPaint.setBlendMode(mode);
mtklein46616af2014-09-30 14:47:10 -070051 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
52 SkIntToScalar(fImageInfo.height())));
dneto327f9052014-09-15 10:53:16 -070053
54 canvas->clipRect(clipRect);
55 canvas->clear(0xff000000);
56
halcanary96fcdcc2015-08-27 07:41:13 -070057 canvas->saveLayer(nullptr, &blackPaint);
mtklein46616af2014-09-30 14:47:10 -070058 canvas->drawRect(canvasRect, greenPaint);
halcanary96fcdcc2015-08-27 07:41:13 -070059 canvas->saveLayer(nullptr, &layerPaint);
Mike Reed34a0c972021-01-25 17:49:32 -050060 canvas->drawImageRect(fCircleImage, SkRect::MakeXYWH(20,20,60,60),
61 SkSamplingOptions(), &blackPaint);
dneto327f9052014-09-15 10:53:16 -070062 canvas->restore();
63 canvas->restore();
64 }
65
66 private:
67 const SkImageInfo fImageInfo;
Mike Reed34a0c972021-01-25 17:49:32 -050068 sk_sp<SkImage> fCircleImage;
dneto327f9052014-09-15 10:53:16 -070069};
70
71class RecordingStrategy {
72 public:
73 virtual ~RecordingStrategy() {}
dneto327f9052014-09-15 10:53:16 -070074 virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
75 const SkRect& intoClip,
reed374772b2016-10-05 17:33:02 -070076 SkBlendMode) = 0;
dneto327f9052014-09-15 10:53:16 -070077};
78
79class BitmapBackedCanvasStrategy : public RecordingStrategy {
80 // This version just draws into a bitmap-backed canvas.
81 public:
mtklein46616af2014-09-30 14:47:10 -070082 BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -070083 fBitmap.allocPixels(imageInfo);
84 }
85
reed374772b2016-10-05 17:33:02 -070086 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
87 SkBlendMode mode) override {
dneto327f9052014-09-15 10:53:16 -070088 SkCanvas canvas(fBitmap);
89 canvas.clear(0xffffffff);
90 // Note that the scene is drawn just into the clipped region!
91 canvas.clipRect(intoClip);
92 drawer.draw(&canvas, intoClip, mode); // Shouild be canvas-wide...
93 return fBitmap;
94 }
95
96 private:
97 SkBitmap fBitmap;
98};
99
mtklein46616af2014-09-30 14:47:10 -0700100class PictureStrategy : public RecordingStrategy {
101 // This version draws the entire scene into an SkPictureRecorder.
dneto327f9052014-09-15 10:53:16 -0700102 // Then it then replays the scene through a clip rectangle.
103 // This backend proved to be buggy.
104 public:
mtklein46616af2014-09-30 14:47:10 -0700105 PictureStrategy(const SkImageInfo& imageInfo) {
dneto327f9052014-09-15 10:53:16 -0700106 fBitmap.allocPixels(imageInfo);
mtklein46616af2014-09-30 14:47:10 -0700107 fWidth = imageInfo.width();
108 fHeight = imageInfo.height();
dneto327f9052014-09-15 10:53:16 -0700109 }
110
reed374772b2016-10-05 17:33:02 -0700111 const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
112 SkBlendMode mode) override {
mtklein703dd2e2015-01-09 06:41:48 -0800113 SkRTreeFactory factory;
dneto327f9052014-09-15 10:53:16 -0700114 SkPictureRecorder recorder;
115 SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
mtklein46616af2014-09-30 14:47:10 -0700116 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
117 SkIntToScalar(fHeight),
118 &factory);
dneto327f9052014-09-15 10:53:16 -0700119 drawer.draw(canvas, canvasRect, mode);
reedca2622b2016-03-18 07:25:55 -0700120 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
dneto327f9052014-09-15 10:53:16 -0700121
122 SkCanvas replayCanvas(fBitmap);
123 replayCanvas.clear(0xffffffff);
124 replayCanvas.clipRect(intoClip);
125 picture->playback(&replayCanvas);
126 return fBitmap;
127 }
128
129 private:
130 SkBitmap fBitmap;
131 int fWidth;
132 int fHeight;
133};
134
mtklein46616af2014-09-30 14:47:10 -0700135} // namespace
dneto327f9052014-09-15 10:53:16 -0700136
137
138DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
139#define FINEGRAIN 0
dneto327f9052014-09-15 10:53:16 -0700140 const Drawer drawer;
141
mtklein46616af2014-09-30 14:47:10 -0700142 BitmapBackedCanvasStrategy golden(drawer.imageInfo());
143 PictureStrategy picture(drawer.imageInfo());
dneto327f9052014-09-15 10:53:16 -0700144
145#if !FINEGRAIN
146 unsigned numErrors = 0;
147 SkString errors;
148#endif
149
reed374772b2016-10-05 17:33:02 -0700150 for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
mtklein46616af2014-09-30 14:47:10 -0700151 const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
reed374772b2016-10-05 17:33:02 -0700152 SkBlendMode mode = SkBlendMode(iMode);
dneto327f9052014-09-15 10:53:16 -0700153
154 const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
mtklein46616af2014-09-30 14:47:10 -0700155 const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
dneto327f9052014-09-15 10:53:16 -0700156
Mike Reedf0ffb892017-10-03 14:47:21 -0400157 size_t pixelsSize = goldenBM.computeByteSize();
158 REPORTER_ASSERT(reporter, pixelsSize == pictureBM.computeByteSize());
dneto327f9052014-09-15 10:53:16 -0700159
160 // The pixel arrays should match.
161#if FINEGRAIN
mtklein46616af2014-09-30 14:47:10 -0700162 REPORTER_ASSERT(reporter,
163 0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
dneto327f9052014-09-15 10:53:16 -0700164#else
John Stilesc1c3c6d2020-08-15 23:22:53 -0400165 if (0 != memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
dneto327f9052014-09-15 10:53:16 -0700166 numErrors++;
mtklein46616af2014-09-30 14:47:10 -0700167 errors.appendf("For SkXfermode %d %s: SkPictureRecorder bitmap is wrong\n",
Mike Reedd4706732016-11-15 16:44:34 -0500168 iMode, SkBlendMode_Name(mode));
dneto327f9052014-09-15 10:53:16 -0700169 }
170#endif
171 }
mtklein46616af2014-09-30 14:47:10 -0700172
dneto327f9052014-09-15 10:53:16 -0700173#if !FINEGRAIN
Brian Salomon1c80e992018-01-29 09:50:47 -0500174 REPORTER_ASSERT(reporter, 0 == numErrors, errors.c_str());
dneto327f9052014-09-15 10:53:16 -0700175#endif
176}